| 1 | /** |
|---|
| 2 | * Texas Instruments HDQ implementation for the Arduino API |
|---|
| 3 | * (cleft) Matthieu Lalonde 2008 (matth@mlalonde.net) |
|---|
| 4 | * Creative Commons BY-SA-NC |
|---|
| 5 | * |
|---|
| 6 | * http://trac.mlalonde.net/cral/browser/HDQ/ |
|---|
| 7 | * |
|---|
| 8 | * Revision 1 |
|---|
| 9 | * |
|---|
| 10 | **/ |
|---|
| 11 | |
|---|
| 12 | #ifndef _HDQ_H_ |
|---|
| 13 | #define _HDQ_H_ |
|---|
| 14 | |
|---|
| 15 | /** |
|---|
| 16 | * Default pin to use if none is specified to the constructor |
|---|
| 17 | **/ |
|---|
| 18 | #define HDQ_DEFAULT_PIN 7 /* Arduino pin 7 */ |
|---|
| 19 | |
|---|
| 20 | /** |
|---|
| 21 | * Read/write command mask |
|---|
| 22 | **/ |
|---|
| 23 | #define HDQ_ADDR_MASK_READ 0x00 |
|---|
| 24 | #define HDQ_ADDR_MASK_WRITE 0x80 /* B10000000 */ |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | * HDQ bit timings |
|---|
| 28 | **/ |
|---|
| 29 | #define HDQ_DELAY_BIT_START 30 |
|---|
| 30 | #define HDQ_DELAY_BIT_WRITE 100 |
|---|
| 31 | #define HDQ_DELAY_BIT_END 70 |
|---|
| 32 | #define HDQ_DELAY_BIT_TOTAL 200 |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | * This is the number of times the slave wait loop |
|---|
| 36 | * will run before we time out. |
|---|
| 37 | * As far as I can tell the loop uses ~6 instructions |
|---|
| 38 | * thus giving about 200uS delay which is a full bit write |
|---|
| 39 | **/ |
|---|
| 40 | #define HDQ_DELAY_FAIL_TRIES 225 |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | * HDQ Default timings |
|---|
| 44 | **/ |
|---|
| 45 | #define HDQ_DELAY_TB 250 /* Min: 190uS */ |
|---|
| 46 | #define HDQ_DELAY_TBR 50 /* Min: 40uS */ |
|---|
| 47 | #define HDQ_DELAY_TSTRH 1 /* Min: 5nS */ |
|---|
| 48 | #define HDQ_DELAY_TDSU 50 /* Max: 50uS */ |
|---|
| 49 | #define HDQ_DELAY_TDH 100 /* Min: 100uS */ |
|---|
| 50 | #define HDQ_DELAY_TSSU 145 /* Max: 145uS */ |
|---|
| 51 | #define HDQ_DELAY_TCYCH 190 /* Min: 190uS */ |
|---|
| 52 | #define HDQ_DELAY_TSTRB 32 /* Min: 32uS */ |
|---|
| 53 | #define HDQ_DELAY_TDSUB 50 /* Max: 50uS */ |
|---|
| 54 | #define HDQ_DELAY_TDV 80 /* Min 80uS */ |
|---|
| 55 | #define HDQ_DELAY_TSSUB 145 /* Max: 145uS */ |
|---|
| 56 | #define HDQ_DELAY_TCYCB_MIN 190 /* Min: 190uS */ |
|---|
| 57 | #define HDQ_DELAY_TCYCB_MAX 250 /* Max: 250uS */ |
|---|
| 58 | #define HDQ_DELAY_TRSPS_MIN 190 /* Min: 190uS */ |
|---|
| 59 | #define HDQ_DELAY_TRSPS_MAX 320 /* Max: 320uS */ |
|---|
| 60 | #define HDQ_DELAY_TRSPS_DIFF 130 /* HDQ_DELAY_TRSPS_MAX - HDQ_DELAY_TRSPS_MIN */ |
|---|
| 61 | |
|---|
| 62 | class HDQ |
|---|
| 63 | { |
|---|
| 64 | public: |
|---|
| 65 | /** |
|---|
| 66 | * Constructor |
|---|
| 67 | * @param pinArg: pin number to attach to |
|---|
| 68 | **/ |
|---|
| 69 | HDQ(uint8_t pinArg = HDQ_DEFAULT_PIN); |
|---|
| 70 | |
|---|
| 71 | /** |
|---|
| 72 | * sendBreak: writes a break to the HDQ line |
|---|
| 73 | **/ |
|---|
| 74 | void doBreak(void); |
|---|
| 75 | |
|---|
| 76 | /** |
|---|
| 77 | * write: send a payload to the device |
|---|
| 78 | * @param reg: the address of the register to write to |
|---|
| 79 | * @param payload: data to be sent |
|---|
| 80 | * @return: false, unless if verif is set, then |
|---|
| 81 | * it will read back the register and |
|---|
| 82 | * return true if it matches the payload |
|---|
| 83 | **/ |
|---|
| 84 | boolean write(uint8_t reg, uint8_t payload, boolean verif); |
|---|
| 85 | boolean write(uint8_t reg, uint8_t payload); |
|---|
| 86 | |
|---|
| 87 | /** |
|---|
| 88 | * read: read from the device |
|---|
| 89 | * @param register: address of the register to read |
|---|
| 90 | * @return a uint8_t integer |
|---|
| 91 | **/ |
|---|
| 92 | uint8_t read(uint8_t reg); |
|---|
| 93 | |
|---|
| 94 | private: |
|---|
| 95 | /** |
|---|
| 96 | * Port variables definition |
|---|
| 97 | **/ |
|---|
| 98 | uint8_t pin; |
|---|
| 99 | uint8_t port; |
|---|
| 100 | uint8_t bitmask; |
|---|
| 101 | volatile uint8_t *outputReg; |
|---|
| 102 | volatile uint8_t *inputReg; |
|---|
| 103 | volatile uint8_t *modeReg; |
|---|
| 104 | |
|---|
| 105 | /** |
|---|
| 106 | * writeByte: write a raw byte of data to the bus |
|---|
| 107 | * @param payload: the byte to send |
|---|
| 108 | * |
|---|
| 109 | **/ |
|---|
| 110 | void writeByte(uint8_t payload); |
|---|
| 111 | }; |
|---|
| 112 | #endif |
|---|