You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.8 KiB

/**
* Texas Instruments HDQ implementation for the Arduino API
* (cleft) Matthieu Lalonde 2008 (matth@mlalonde.net)
* Creative Commons BY-SA-NC
*
* http://trac.mlalonde.net/cral/browser/HDQ/
*
* Revision 1
*
**/
#ifndef _HDQ_H_
#define _HDQ_H_
/**
* Default pin to use if none is specified to the constructor
**/
#define HDQ_DEFAULT_PIN 7 /* Arduino pin 7 */
/**
* Read/write command mask
**/
#define HDQ_ADDR_MASK_READ 0x00
#define HDQ_ADDR_MASK_WRITE 0x80 /* B10000000 */
/**
* HDQ bit timings
**/
#define HDQ_DELAY_BIT_START 30
#define HDQ_DELAY_BIT_WRITE 100
#define HDQ_DELAY_BIT_END 70
#define HDQ_DELAY_BIT_TOTAL 200
/**
* This is the number of times the slave wait loop
* will run before we time out.
* As far as I can tell the loop uses ~6 instructions
* thus giving about 200uS delay which is a full bit write
**/
#define HDQ_DELAY_FAIL_TRIES 225
/**
* HDQ Default timings
**/
#define HDQ_DELAY_TB 250 /* Min: 190uS */
#define HDQ_DELAY_TBR 50 /* Min: 40uS */
#define HDQ_DELAY_TSTRH 1 /* Min: 5nS */
#define HDQ_DELAY_TDSU 50 /* Max: 50uS */
#define HDQ_DELAY_TDH 100 /* Min: 100uS */
#define HDQ_DELAY_TSSU 145 /* Max: 145uS */
#define HDQ_DELAY_TCYCH 190 /* Min: 190uS */
#define HDQ_DELAY_TSTRB 32 /* Min: 32uS */
#define HDQ_DELAY_TDSUB 50 /* Max: 50uS */
#define HDQ_DELAY_TDV 80 /* Min 80uS */
#define HDQ_DELAY_TSSUB 145 /* Max: 145uS */
#define HDQ_DELAY_TCYCB_MIN 190 /* Min: 190uS */
#define HDQ_DELAY_TCYCB_MAX 250 /* Max: 250uS */
#define HDQ_DELAY_TRSPS_MIN 190 /* Min: 190uS */
#define HDQ_DELAY_TRSPS_MAX 320 /* Max: 320uS */
#define HDQ_DELAY_TRSPS_DIFF 130 /* HDQ_DELAY_TRSPS_MAX - HDQ_DELAY_TRSPS_MIN */
class HDQ
{
public:
/**
* Constructor
* @param pinArg: pin number to attach to
**/
HDQ(uint8_t pinArg = HDQ_DEFAULT_PIN);
/**
* sendBreak: writes a break to the HDQ line
**/
void doBreak(void);
/**
* write: send a payload to the device
* @param reg: the address of the register to write to
* @param payload: data to be sent
* @return: false, unless if verif is set, then
* it will read back the register and
* return true if it matches the payload
**/
boolean write(uint8_t reg, uint8_t payload, boolean verif);
boolean write(uint8_t reg, uint8_t payload);
/**
* read: read from the device
* @param register: address of the register to read
* @return a uint8_t integer
**/
uint8_t read(uint8_t reg);
private:
/**
* Port variables definition
**/
uint8_t pin;
uint8_t port;
uint8_t bitmask;
volatile uint8_t *outputReg;
volatile uint8_t *inputReg;
volatile uint8_t *modeReg;
/**
* writeByte: write a raw byte of data to the bus
* @param payload: the byte to send
*
**/
void writeByte(uint8_t payload);
};
#endif