Skip to content

Commit

Permalink
[peripherals] adxl345_spi stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
flixr committed Feb 14, 2013
1 parent 1aae965 commit a2387a8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
10 changes: 8 additions & 2 deletions sw/airborne/peripherals/adxl345.h
Expand Up @@ -47,23 +47,29 @@ struct Adxl345Config {
bool_t int_invert; ///< Invert Interrupt FALSE: active high, TRUE: active low
bool_t full_res; ///< Full Resolution: FALSE: 10bit, TRUE: full with 4mg/LSB
bool_t justify_msb; ///< justify: FALSE: right with sign-extension, TRUE: MSB (left)
bool_t self_test; ///< Enable self-test-force causing shift in output data.
bool_t spi_3_wire; ///< Set 3-wire SPI mode, if FALSE: 4-wire SPI mode
enum Adxl345Ranges range; ///< g Range
enum Adxl345Rates rate; ///< Data Output Rate
};

static inline void adxl345_set_default_config(struct Adxl345Config *c)
{
c->rate = ADXL345_RATE_100HZ;
c->drdy_int_enable = FALSE;
c->int_invert = TRUE;
c->full_res = TRUE;
c->justify_msb = FALSE;
c->self_test = FALSE;
c->spi_3_wire = FALSE;

c->rate = ADXL345_RATE_100HZ;
c->range = ADXL345_RANGE_16G;
}

static inline uint8_t adxl345_data_format(struct Adxl345Config *c)
{
return ((c->int_invert << 5) | (c->full_res << 3) | (c->justify_msb << 2) | (c->range));
return ((c->self_test << 7) | (c->spi_3_wire << 6) | (c->int_invert << 5) |
(c->full_res << 3) | (c->justify_msb << 2) | (c->range));
}

#endif // ADXL345_H
26 changes: 15 additions & 11 deletions sw/airborne/peripherals/adxl345_spi.c
Expand Up @@ -65,7 +65,8 @@ void adxl345_spi_init(struct Adxl345_Spi *adxl, struct spi_periph *spi_p, uint8_


static void adxl345_spi_write_to_reg(struct Adxl345_Spi *adxl, uint8_t _reg, uint8_t _val) {
//adxl->spi_trans.output_length = 2;
adxl->spi_trans.output_length = 2;
adxl->spi_trans.input_length = 0;
adxl->tx_buf[0] = _reg;
adxl->tx_buf[1] = _val;
spi_submit(adxl->spi_p, &(adxl->spi_trans));
Expand Down Expand Up @@ -114,8 +115,9 @@ void adxl345_spi_start_configure(struct Adxl345_Spi *adxl)
void adxl345_spi_read(struct Adxl345_Spi *adxl)
{
if (adxl->initialized && adxl->spi_trans.status == SPITransDone) {
//adxl->spi_trans.output_length = 1;
//adxl->tx_buf[0] = ADXL345_REG_DATA_X0;
adxl->spi_trans.output_length = 1;
adxl->spi_trans.input_length = 7;
/* set read bit and multiple byte bit, then address */
adxl->tx_buf[0] = (1<<7|1<<6|ADXL345_REG_DATA_X0);
spi_submit(adxl->spi_p, &(adxl->spi_trans));
}
Expand All @@ -139,14 +141,16 @@ void adxl345_spi_event(struct Adxl345_Spi *adxl)
}
}
else if (adxl->init_status != ADXL_CONF_UNINIT) { // Configuring but not yet initialized
if (adxl->spi_trans.status == SPITransSuccess || adxl->spi_trans.status == SPITransDone) {
adxl->spi_trans.status = SPITransDone;
adxl345_spi_send_config(adxl);
}
if (adxl->spi_trans.status == SPITransFailed) {
adxl->init_status--;
adxl->spi_trans.status = SPITransDone;
adxl345_spi_send_config(adxl); // Retry config (TODO max retry)
switch (adxl->spi_trans.status) {
case SPITransFailed:
adxl->init_status--; // Retry config (TODO max retry)
case SPITransSuccess:
case SPITransDone:
adxl->spi_trans.status = SPITransDone;
adxl345_spi_send_config(adxl);
break;
default:
break;
}
}
}

0 comments on commit a2387a8

Please sign in to comment.