sx9500 driver
Dependents: Senet NAMote scpi_sx127x NAMote72_Utility scpi_sx127x_firstTest
Revision 1:aa30dc96dc77, committed 2015-05-08
- Comitter:
- dudmuck
- Date:
- Fri May 08 01:32:54 2015 +0000
- Parent:
- 0:d46a1b9267a3
- Commit message:
- added interrupt handling
Changed in this revision
sx9500.cpp | Show annotated file Show diff for this revision Revisions of this file |
sx9500.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r d46a1b9267a3 -r aa30dc96dc77 sx9500.cpp --- a/sx9500.cpp Wed Mar 18 00:57:23 2015 +0000 +++ b/sx9500.cpp Fri May 08 01:32:54 2015 +0000 @@ -2,8 +2,11 @@ #define SX9500_I2C_ADDRESS 0x50 //0x28 -SX9500::SX9500(I2C& r, PinName en_pin) : m_i2c(r), m_txen(en_pin) + +SX9500::SX9500(I2C& r, PinName en_pin, PinName nirq_pin) : m_i2c(r), m_txen(en_pin), m_nirq(nirq_pin) { + m_nirq.mode(PullUp); // no pullup on board? + RegProxCtrl0.octet = read_single(SX9500_REG_PROXCTRL0); } SX9500::~SX9500() @@ -15,25 +18,20 @@ write(SX9500_REG_RESET, SX9500_RESET_CMD); } -uint16_t SX9500::get_sensor() +void SX9500::print_sensor(char CSn) { - //uint16_t offset; - uint8_t status; uint8_t buf[2]; - - m_txen = 1; - - write(SX9500_REG_IRQMSK, 0x10); - write(SX9500_REG_IRQSRC, 0x10); - - do { - status = read_single(SX9500_REG_IRQSRC); - } while (!(status & 0x10)); - - read(SX9500_REG_OFFSETMSB, buf, 2); - /*printf("MSB:%x\n", read_single(SX9500_REG_OFFSETMSB)); - printf("LSB:%x\n", read_single(SX9500_REG_OFFSETLSB));*/ - return (buf[0] << 8) + buf[1]; + + write(SX9500_REG_SENSORSEL, CSn); + + read(SX9500_REG_USEMSB, buf, 2); + printf("%d useful:0x%02x%02x\r\n", CSn, buf[0], buf[1]); + + read(SX9500_REG_AVGMSB, buf, 2); + printf("%d avg:0x%02x%02x\r\n", CSn, buf[0], buf[1]); + + read(SX9500_REG_DIFFMSB, buf, 2); + printf("%d diff:0x%02x%02x\r\n", CSn, buf[0], buf[1]); } void SX9500::write(uint8_t addr, uint8_t data) @@ -70,10 +68,55 @@ return cmd[0]; } - -void SX9500::standby(void) + +bool SX9500::get_active() { - m_txen = 0; - write(SX9500_REG_PROXCTRL0, 0); // turn off all sensor pins + if (m_txen.read()) + return true; + else + return false; +} + +void SX9500::set_active(bool en) +{ + if (en) { + m_txen = 1; + } else { + /* lowest power (non)operation */ + m_txen = 0; + write(SX9500_REG_PROXCTRL0, 0); // turn off all sensor pins + } } +void SX9500::service() +{ + if (m_nirq.read()) + return; + + RegIrqSrc.octet = read_single(SX9500_REG_IRQSRC); + printf("95irq:"); + if (RegIrqSrc.bits.conv_done) { + printf("conv_done:\r\n"); + print_sensor(0); + print_sensor(1); + } + if (RegIrqSrc.bits.comp_done) { + printf("comp_done "); + } + if (RegIrqSrc.bits.far || RegIrqSrc.bits.close) { + RegStat_t stat; + printf("stat "); + stat.octet = read_single(SX9500_REG_STAT); + if (stat.bits.proxstat0) + printf("cs0 "); + if (stat.bits.proxstat1) + printf("cs1 "); + } + + if (RegIrqSrc.bits.reset) { + printf("reset "); + } + + printf("\r\n"); +} +
diff -r d46a1b9267a3 -r aa30dc96dc77 sx9500.h --- a/sx9500.h Wed Mar 18 00:57:23 2015 +0000 +++ b/sx9500.h Fri May 08 01:32:54 2015 +0000 @@ -26,20 +26,71 @@ #define SX9500_RESET_CMD 0xDE +typedef union { + struct { // sx9500 register 0x09 + uint8_t txen_stat : 1; // 0 + uint8_t reserved : 2; // 1,2 + uint8_t conv_done : 1; // 3 + uint8_t comp_done : 1; // 4 + uint8_t far : 1; // 5 + uint8_t close : 1; // 6 + uint8_t reset : 1; // 7 + } bits; + uint8_t octet; +} RegIrqSrc_t; + +typedef union { + struct { // sx9500 register 0x09 + uint8_t compstat : 4; // 0,1,2,3 + uint8_t proxstat0 : 1; // 4 + uint8_t proxstat1 : 1; // 5 + uint8_t proxstat2 : 1; // 6 + uint8_t proxstat3 : 1; // 7 + } bits; + uint8_t octet; +} RegStat_t; + +typedef union { + struct { // sx9500 register 0x06 + uint8_t sensor_en : 4; // 0,1,2,3 + uint8_t scan_period : 3; // 4,5,6 + uint8_t reserved : 1; // 7 + } bits; + uint8_t octet; +} RegProxCtrl0_t; + +typedef union { + struct { // sx9500 register 0x09 + uint8_t raw_filt : 2; // 0,1 + uint8_t reserved : 2; // 2,3 + uint8_t doze_period : 2; // 4,5 + uint8_t doze_en : 1; // 6 + uint8_t res7 : 1; // 7 + } bits; + uint8_t octet; +} RegProxCtrl3_t; + class SX9500 { public: - SX9500(I2C& r, PinName en_pin); + SX9500(I2C& r, PinName en_pin, PinName nirq_pin); ~SX9500(); //void try_read(void); uint8_t read_single(uint8_t addr); void read(uint8_t addr, uint8_t *dst_buf, int length); void write(uint8_t addr, uint8_t data); void reset(void); - uint16_t get_sensor(void); - void standby(void); + //uint16_t get_sensor(char CSn); + void print_sensor(char CSn); + void set_active(bool); + bool get_active(void); + void service(void); + + RegIrqSrc_t RegIrqSrc; + RegProxCtrl0_t RegProxCtrl0; private: I2C& m_i2c; DigitalOut m_txen; + DigitalIn m_nirq; // polled irq pin, because i2c is shared };