Commit 7391eadf authored by Andreas Werner's avatar Andreas Werner
Browse files

add ltc master

parent 375db260
......@@ -21,32 +21,39 @@ struct ltc6811 {
/**
* Driver index
*/
const bool isFirst;
uint32_t index;
/**
* Driver index
* Task
*/
uint32_t index;
OS_DEFINE_TASK(adcTask, 500);
/**
* I2C Master
* Bitmaks for select mutlible slaves
*/
struct i2c *i2c;
int32_t selectSlaves;
/**
* SPI Slave (connection to the first slave)
* Only be defined if ismaster == true
* Slaves
*/
struct ltc6811_slave **slaves;
/**
* SPI Slave
*/
struct spi_slave *spi;
};
struct ltc6811_slave {
struct hal gen;
/**
* First Slave
* Driver index
*/
struct ltc6811 *first;
uint32_t index;
/**
* Slaves
* I2C Master
*/
struct ltc6811 **slaves;
struct i2c *i2c;
/**
* Bitmaks for select mutlible slaves
* First Slave
*/
int32_t selectSlaves;
struct ltc6811 *master;
/**
* ADCs
*/
......@@ -58,14 +65,14 @@ struct ltc6811 {
*/
struct i2c_ltc6811 {
struct i2c_generic gen;
struct ltc6811 *ltc;
struct ltc6811_slave *ltc;
};
/**
* adcs
*/
struct adc_ltc6811 {
struct adc_generic gen;
struct ltc6811 *ltc;
struct ltc6811_slave *ltc;
};
/**\endcond*/
......@@ -113,8 +120,8 @@ enum ltc_cmd {
};
struct ltc6811 *ltc6811_init(uint32_t index);
struct ltc6811_slave *ltc6811_slave_init(uint32_t index);
int32_t ltc6811_connect(struct ltc6811 *ltc, struct spi *spi, uint8_t cs, uint16_t gpio, uint32_t bautrate);
int32_t ltc6811_connectSlave(struct ltc6811 *ltc, struct ltc6811 *slave);
int32_t ltc6811_write(struct ltc6811 *ltc, enum ltc_cmd cmd, uint8_t *data, uint32_t len);
int32_t ltc6811_read(struct ltc6811 *ltc, enum ltc_cmd cmd, uint8_t *data, uint32_t len);
uint16_t ltc6811_calcPEC(uint8_t* data, uint8_t len);
......@@ -122,70 +129,79 @@ int32_t ltc6811_writeRegister(struct ltc6811 *ltc, enum ltc_cmd cmd, uint8_t *ne
int32_t ltc6811_readRegister(struct ltc6811 *ltc, enum ltc_cmd cmd, uint8_t *registerContent);
/**\cond INTERNAL*/
#define LTC6811_ADC_DEV(id, adcID) \
struct adc_ltc6811 ltc6811_adc_##id##_##adcID = { \
.ltc = &ltc6811_dev_##id, \
#define LTC6811_ADC_DEV(masterid, id, adcID) \
struct adc_ltc6811 ltc6811_adc_##masterid##_##id##_##adcID = { \
.ltc = &ltc6811_slave_##masterid##_##id, \
}; \
ADC_ADDDEV(ltc6811, ltc6811_adc_##id##_##adcID)
ADC_ADDDEV(ltc6811, ltc6811_adc_##masterid##_##id##_##adcID)
#define ADC_PRV
#include <adc_prv.h>
extern const struct adc_ops lpc6811_adc_ops;
/**\endcond*/
#define LTC6811_ADDDEV(id, isfirst, numberofslave) \
extern struct adc_ltc6811 *ltc6811_adcs_##id[12]; \
#define LTC6811_ADDDEV(id, numberofslave) \
struct ltc6811_slave *ltc6811_slaves_##id[numberofslave]; \
struct ltc6811 ltc6811_dev_##id = { \
HAL_NAME("LTC6811 " #id) \
.gen.init = false, \
.numberOfSlaves = numberofslave, \
.allSlavesMask = ((1 << numberofslave) - 1), \
.selectSlaves = ((1 << numberofslave) - 1), \
.isFirst = isfirst, \
.slaves = ltc6811_slaves_##id, \
}; \
HAL_ADD(ltc6811, ltc6811_dev_##id);
#define LTC6811_SLAVE_ADDDEV(masterid, id) \
extern struct adc_ltc6811 *ltc6811_adcs_##id[12]; \
struct ltc6811_slave ltc6811_slave_##masterid##_##id = { \
HAL_NAME("LTC6811 " #id) \
.gen.init = false, \
.master = &ltc6811_dev_##masterid,\
.adcs = ltc6811_adcs_##id, \
}; \
HAL_ADD(ltc6811, ltc6811_dev_##id); \
LTC6811_ADC_DEV(id, 0); \
LTC6811_ADC_DEV(id, 1); \
LTC6811_ADC_DEV(id, 2); \
LTC6811_ADC_DEV(id, 3); \
LTC6811_ADC_DEV(id, 4); \
LTC6811_ADC_DEV(id, 5); \
LTC6811_ADC_DEV(id, 6); \
LTC6811_ADC_DEV(id, 7); \
LTC6811_ADC_DEV(id, 8); \
LTC6811_ADC_DEV(id, 9); \
LTC6811_ADC_DEV(id, 10); \
LTC6811_ADC_DEV(id, 11); \
HAL_ADD(ltc6811, ltc6811_slave_##masterid##_##id); \
LTC6811_ADC_DEV(masterid, id, 0); \
LTC6811_ADC_DEV(masterid, id, 1); \
LTC6811_ADC_DEV(masterid, id, 2); \
LTC6811_ADC_DEV(masterid, id, 3); \
LTC6811_ADC_DEV(masterid, id, 4); \
LTC6811_ADC_DEV(masterid, id, 5); \
LTC6811_ADC_DEV(masterid, id, 6); \
LTC6811_ADC_DEV(masterid, id, 7); \
LTC6811_ADC_DEV(masterid, id, 8); \
LTC6811_ADC_DEV(masterid, id, 9); \
LTC6811_ADC_DEV(masterid, id, 10); \
LTC6811_ADC_DEV(masterid, id, 11); \
struct adc_ltc6811 *ltc6811_adcs_##id[12] = { \
&ltc6811_adc_##id##_0, \
&ltc6811_adc_##id##_1, \
&ltc6811_adc_##id##_2, \
&ltc6811_adc_##id##_3, \
&ltc6811_adc_##id##_4, \
&ltc6811_adc_##id##_5, \
&ltc6811_adc_##id##_6, \
&ltc6811_adc_##id##_7, \
&ltc6811_adc_##id##_8, \
&ltc6811_adc_##id##_9, \
&ltc6811_adc_##id##_10, \
&ltc6811_adc_##id##_11, \
&ltc6811_adc_##masterid##_##id##_0, \
&ltc6811_adc_##masterid##_##id##_1, \
&ltc6811_adc_##masterid##_##id##_2, \
&ltc6811_adc_##masterid##_##id##_3, \
&ltc6811_adc_##masterid##_##id##_4, \
&ltc6811_adc_##masterid##_##id##_5, \
&ltc6811_adc_##masterid##_##id##_6, \
&ltc6811_adc_##masterid##_##id##_7, \
&ltc6811_adc_##masterid##_##id##_8, \
&ltc6811_adc_##masterid##_##id##_9, \
&ltc6811_adc_##masterid##_##id##_10, \
&ltc6811_adc_##masterid##_##id##_11, \
};
#define LTC6811_ID(id) HAL_GET_ID(hal, ltc6811, ltc6811_dev_##id)
#define LTC6811_ADC_ID(id, adcid) HAL_GET_ID(adc, ltc6811, ltc6811_adc_##id##_##adcid)
#define LTC6811_GET_ALL_ADCS(id, adcs) { \
(adcs)[0] = adc_init(LTC6811_ADC_ID(id, 0), 12,0); \
(adcs)[1] = adc_init(LTC6811_ADC_ID(id, 1), 12,0); \
(adcs)[2] = adc_init(LTC6811_ADC_ID(id, 2), 12,0); \
(adcs)[3] = adc_init(LTC6811_ADC_ID(id, 3), 12,0); \
(adcs)[4] = adc_init(LTC6811_ADC_ID(id, 4), 12,0); \
(adcs)[5] = adc_init(LTC6811_ADC_ID(id, 5), 12,0); \
(adcs)[6] = adc_init(LTC6811_ADC_ID(id, 6), 12,0); \
(adcs)[7] = adc_init(LTC6811_ADC_ID(id, 7), 12,0); \
(adcs)[8] = adc_init(LTC6811_ADC_ID(id, 8), 12,0); \
(adcs)[9] = adc_init(LTC6811_ADC_ID(id, 9), 12,0); \
(adcs)[10] = adc_init(LTC6811_ADC_ID(id, 10), 12,0); \
(adcs)[11] = adc_init(LTC6811_ADC_ID(id, 11), 12,0); \
#define LTC6811_SLAVE_ID(masterid, id) HAL_GET_ID(hal, ltc6811, ltc6811_slave_##masterid##_##id)
#define LTC6811_ADC_ID(masterid, id, adcid) HAL_GET_ID(adc, ltc6811, ltc6811_adc_##masterid##_##id##_##adcid)
#define LTC6811_GET_ALL_ADCS(masterid, id, adcs) { \
(adcs)[0] = adc_init(LTC6811_ADC_ID(masterid, id, 0), 12,0); \
(adcs)[1] = adc_init(LTC6811_ADC_ID(masterid, id, 1), 12,0); \
(adcs)[2] = adc_init(LTC6811_ADC_ID(masterid, id, 2), 12,0); \
(adcs)[3] = adc_init(LTC6811_ADC_ID(masterid, id, 3), 12,0); \
(adcs)[4] = adc_init(LTC6811_ADC_ID(masterid, id, 4), 12,0); \
(adcs)[5] = adc_init(LTC6811_ADC_ID(masterid, id, 5), 12,0); \
(adcs)[6] = adc_init(LTC6811_ADC_ID(masterid, id, 6), 12,0); \
(adcs)[7] = adc_init(LTC6811_ADC_ID(masterid, id, 7), 12,0); \
(adcs)[8] = adc_init(LTC6811_ADC_ID(masterid, id, 8), 12,0); \
(adcs)[9] = adc_init(LTC6811_ADC_ID(masterid, id, 9), 12,0); \
(adcs)[10] = adc_init(LTC6811_ADC_ID(masterid, id, 10), 12,0); \
(adcs)[11] = adc_init(LTC6811_ADC_ID(masterid, id, 11), 12,0); \
} \
#endif
......@@ -67,11 +67,9 @@ int32_t ltc6811_connect(struct ltc6811 *ltc, struct spi *spi, uint8_t cs, uint16
ltc6811_connect_error0:
return -1;
}
int32_t ltc6811_connectSlave(struct ltc6811 *ltc, struct ltc6811 *slave) {
static int32_t ltc6811_connectSlave(struct ltc6811 *ltc, struct ltc6811_slave *slave) {
int i;
if (ltc->slaves == NULL) {
ltc->slaves = calloc(sizeof(struct ltc6811 *), ltc->numberOfSlaves);
}
for (i = 0; i < ltc->numberOfSlaves; i++) {
if (ltc->slaves[i] == NULL) {
break;
......@@ -81,10 +79,31 @@ int32_t ltc6811_connectSlave(struct ltc6811 *ltc, struct ltc6811 *slave) {
return -1;
}
ltc->slaves[i] = slave;
slave->first = ltc;
return 0;
}
struct ltc6811_slave *ltc6811_slave_init(uint32_t index) {
int32_t ret;
struct ltc6811_slave *ltc = (struct ltc6811_slave *) HAL_GET_GLOBAL_DEV(index);
if (ltc->gen.init) {
return ltc;
}
ret = hal_init(ltc);
if (ret < 0) {
goto ltc6811_slave_init_error0;
}
ltc->index = index;
ltc->gen.init = true;
PRINTF("LTC6811 Slave Init\n");
ret = ltc6811_connectSlave(ltc->master, ltc);
if (ret < 0) {
goto ltc6811_slave_init_error0;
}
return ltc;
ltc6811_slave_init_error0:
return NULL;
}
int32_t ltc6811_write(struct ltc6811 *ltc, enum ltc_cmd cmd, uint8_t *data, uint32_t dataLen) {
uint32_t len = 2 /* Command */ + 2 /* Command PEC */ + dataLen /* Data inclusive PEC! */;
uint16_t *sendData = alloca(len);
......@@ -93,10 +112,6 @@ int32_t ltc6811_write(struct ltc6811 *ltc, enum ltc_cmd cmd, uint8_t *data, uint
uint16_t pec;
int32_t ret;
int i;
if (!ltc->isFirst) {
/* This device is not the first ltc */
ltc = ltc->first;
}
memset(sendData, 0xFF, len);
tmp[0] = (cmd >> 8) & 0xFF;
tmp[1] = (cmd >> 0) & 0xFF;
......@@ -124,10 +139,6 @@ int32_t ltc6811_read(struct ltc6811 *ltc, enum ltc_cmd cmd, uint8_t *data, uint3
uint16_t pec;
int32_t ret;
int i;
if (!ltc->isFirst) {
/* This device is not the first ltc */
ltc = ltc->first;
}
memset(sendData, 0xFF, len);
tmp[0] = (cmd >> 8) & 0xFF;
tmp[1] = (cmd >> 0) & 0xFF;
......@@ -140,7 +151,7 @@ int32_t ltc6811_read(struct ltc6811 *ltc, enum ltc_cmd cmd, uint8_t *data, uint3
if (ret < 0) {
return -1;
}
for (i = 0; i < dataLen; i++) {
for (i = 0; i < lenData; i++) {
data[i] = recvData[4 + i];
}
......@@ -151,10 +162,6 @@ int32_t ltc6811_writeRegister(struct ltc6811 *ltc, enum ltc_cmd cmd, uint8_t *ne
int i;
int16_t pec;
uint8_t *data = alloca(ltc->numberOfSlaves * 8);
if (!ltc->isFirst) {
/* This device is not the first ltc */
ltc = ltc->first;
}
for (i = 0; i < ltc->numberOfSlaves; i++) {
/* the first data is used by the last slave in the chain */
/* check if salve is seleced write new value */
......@@ -188,10 +195,6 @@ int32_t ltc6811_readRegister(struct ltc6811 *ltc, enum ltc_cmd cmd, uint8_t *reg
int16_t orgPec;
uint8_t *data = alloca(ltc->numberOfSlaves * 8);
int32_t ret;
if (!ltc->isFirst) {
/* This device is not the first ltc */
ltc = ltc->first;
}
ret = ltc6811_read(ltc, cmd, data, ltc->numberOfSlaves * 8);
if (ret < 0) {
PRINTF("CMD: 0x%x: Read error\n", cmd);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment