correctly pack structure, remove unused fields

This commit is contained in:
Stephane D'Alu 2016-02-10 00:56:51 +01:00
parent e34ef249e7
commit 260a9edc32
4 changed files with 54 additions and 31 deletions

View File

@ -15,7 +15,7 @@
*/
/**
*
* Illuminance calculation code provided by www.taosinc.com
* DOC: http://ams.com/eng/content/download/250096/975518/143687
*/
#define I2C_HELPERS_AUTOMATIC_DRV TRUE
@ -131,10 +131,6 @@
#define TSL2561_LUX_M8C (0x0000) // 0.000 * 2^LUX_SCALE
#define CEILING(x,y) (((x) + (y) - 1) / (y))
/*===========================================================================*/
/* Driver exported variables. */
/*===========================================================================*/
@ -147,6 +143,8 @@
/* Driver local functions. */
/*===========================================================================*/
#define CEILING(x,y) (((x) + (y) - 1) / (y))
static inline unsigned int
calculateIlluminance(TSL2561_integration_time_t integration_time,
TSL2561_gain_t gain,
@ -276,7 +274,7 @@ void
TSL2561_init(TSL2561_drv *drv, TSL2561_config *config) {
drv->config = config;
drv->gain = TSL2561_GAIN_1X;
drv->integration_time = TSL2561_INTEGRATIONTIME_SHORT;
drv->integration_time = TSL2561_INTEGRATIONTIME_LONG;
drv->state = SENSOR_INIT;
i2c_reg_recv8(TSL2561_COMMAND_BIT | TSL2561_REG_ID,
@ -297,7 +295,7 @@ TSL2561_check(TSL2561_drv *drv) {
msg_t
TSL2561_stop(TSL2561_drv *drv) {
struct PACKED {
struct __attribute__((packed)) {
uint8_t reg;
uint8_t conf;
} tx = { TSL2561_COMMAND_BIT | TSL2561_REG_CONTROL,
@ -308,7 +306,7 @@ TSL2561_stop(TSL2561_drv *drv) {
msg_t
TSL2561_start(TSL2561_drv *drv) {
struct PACKED {
struct __attribute__((packed)) {
uint8_t reg;
uint8_t conf;
} tx = { TSL2561_COMMAND_BIT | TSL2561_REG_CONTROL,
@ -320,7 +318,7 @@ TSL2561_start(TSL2561_drv *drv) {
msg_t
TSL2561_setIntegrationTime(TSL2561_drv *drv,
TSL2561_integration_time_t time) {
struct PACKED {
struct __attribute__((packed)) {
uint8_t reg;
uint8_t conf;
} tx = { TSL2561_COMMAND_BIT | TSL2561_REG_TIMING,
@ -338,7 +336,7 @@ TSL2561_setIntegrationTime(TSL2561_drv *drv,
msg_t
TSL2561_setGain(TSL2561_drv *drv,
TSL2561_gain_t gain) {
struct PACKED {
struct __attribute__((packed)) {
uint8_t reg;
uint8_t conf;
} tx = { TSL2561_COMMAND_BIT | TSL2561_REG_TIMING,

View File

@ -87,16 +87,26 @@ typedef struct {
I2CHelper i2c; /* keep it first */
} TSL2561_config;
/**
* @brief Available integration time
*
* @details Available integration time are:
* 13.7ms, 101ms, 402ms
*/
typedef enum {
TSL2561_INTEGRATIONTIME_SHORT = 0x00, // 13.7ms
TSL2561_INTEGRATIONTIME_MEDIUM = 0x01, // 101ms
TSL2561_INTEGRATIONTIME_LONG = 0x02, // 402ms
TSL2561_INTEGRATIONTIME_SHORT = 0x00, /**< @brief 13.7ms */
TSL2561_INTEGRATIONTIME_MEDIUM = 0x01, /**< @brief 101.0ms */
TSL2561_INTEGRATIONTIME_LONG = 0x02, /**< @brief 402.0ms */
} TSL2561_integration_time_t;
/**
* @brief Available gain
*
* @details Available gain are 1x, 16x
*/
typedef enum {
TSL2561_GAIN_1X = 0x00, // No gain
TSL2561_GAIN_16X = 0x10, // 16x gain
TSL2561_GAIN_1X = 0x00, /**< @brief 1x gain */
TSL2561_GAIN_16X = 0x10, /**< @brief 16x gain */
} TSL2561_gain_t;
/**
@ -105,8 +115,6 @@ typedef enum {
typedef struct {
TSL2561_config *config;
sensor_state_t state;
unsigned int delay;
uint16_t cfg;
TSL2561_gain_t gain;
TSL2561_integration_time_t integration_time;
struct PACKED {

View File

@ -163,29 +163,48 @@ TSL2591_check(TSL2591_drv *drv) {
msg_t
TSL2591_start(TSL2591_drv *drv) {
struct PACKED {
struct __attribute__((packed)) {
uint8_t reg;
uint8_t conf;
} tx = { TSL2591_REG_COMMAND | TSL2591_REG_NORMAL | TSL2591_REG_ENABLE,
TSL2591_ENABLE_POWERON | TSL2591_ENABLE_AEN | TSL2591_ENABLE_AIEN };
return i2c_send((uint8_t*)&tx, sizeof(tx));
} tx_config = {
TSL2591_REG_COMMAND | TSL2591_REG_NORMAL | TSL2591_REG_CONFIG,
(uint8_t)(drv->integration_time | drv->gain) };
struct __attribute__((packed)) {
uint8_t reg;
uint8_t conf;
} tx_start = {
TSL2591_REG_COMMAND | TSL2591_REG_NORMAL | TSL2591_REG_ENABLE,
TSL2591_ENABLE_POWERON };
msg_t msg;
if (((msg = i2c_send((uint8_t*)&tx_config, sizeof(tx_config))) < MSG_OK) ||
((msg = i2c_send((uint8_t*)&tx_start, sizeof(tx_start ))) < MSG_OK)) {
drv->state = SENSOR_ERROR;
return msg;
}
drv->state = SENSOR_STARTED;
return MSG_OK;
}
msg_t
TSL2591_stop(TSL2591_drv *drv) {
struct PACKED {
struct __attribute__((packed)) {
uint8_t reg;
uint8_t conf;
} tx = { TSL2591_REG_COMMAND | TSL2591_REG_NORMAL | TSL2591_REG_ENABLE,
TSL2591_ENABLE_POWEROFF };
} tx_stop = {
TSL2591_REG_COMMAND | TSL2591_REG_NORMAL | TSL2591_REG_ENABLE,
TSL2591_ENABLE_POWEROFF };
return i2c_send((uint8_t*)&tx, sizeof(tx));
return i2c_send((uint8_t*)&tx_stop, sizeof(tx_stop));
}
msg_t
TSL2591_setIntegrationTime(TSL2591_drv *drv,
TSL2591_integration_time_t time) {
struct PACKED {
struct __attribute__((packed)) {
uint8_t reg;
uint8_t conf;
} tx = { TSL2591_REG_COMMAND | TSL2591_REG_NORMAL | TSL2591_REG_CONFIG,
@ -203,7 +222,7 @@ TSL2591_setIntegrationTime(TSL2591_drv *drv,
msg_t
TSL2591_setGain(TSL2591_drv *drv,
TSL2591_gain_t gain) {
struct PACKED {
struct __attribute__((packed)) {
uint8_t reg;
uint8_t conf;
} tx = { TSL2591_REG_COMMAND | TSL2591_REG_NORMAL | TSL2591_REG_CONFIG,

View File

@ -113,9 +113,7 @@ typedef enum {
typedef struct {
TSL2591_config *config;
sensor_state_t state;
unsigned int delay;
uint16_t cfg;
TSL2591_gain_t gain;
TSL2591_gain_t gain;
TSL2591_integration_time_t integration_time;
} TSL2591_drv;