Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Senet NAMote scpi_sx127x NAMote72_Utility scpi_sx127x_firstTest
Revision 0:d46a1b9267a3, committed 2015-03-18
- Comitter:
- dudmuck
- Date:
- Wed Mar 18 00:57:23 2015 +0000
- Child:
- 1:aa30dc96dc77
- Commit message:
- sx9500 driver
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 |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sx9500.cpp Wed Mar 18 00:57:23 2015 +0000
@@ -0,0 +1,79 @@
+#include "sx9500.h"
+
+#define SX9500_I2C_ADDRESS 0x50 //0x28
+
+SX9500::SX9500(I2C& r, PinName en_pin) : m_i2c(r), m_txen(en_pin)
+{
+}
+
+SX9500::~SX9500()
+{
+}
+
+void SX9500::reset()
+{
+ write(SX9500_REG_RESET, SX9500_RESET_CMD);
+}
+
+uint16_t SX9500::get_sensor()
+{
+ //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];
+}
+
+void SX9500::write(uint8_t addr, uint8_t data)
+{
+ uint8_t cmd[2];
+
+ cmd[0] = addr;
+ cmd[1] = data;
+
+ if (m_i2c.write(SX9500_I2C_ADDRESS, (char *)cmd, 2))
+ printf("SX9500 write-fail\n");
+}
+
+void SX9500::read(uint8_t addr, uint8_t *dst_buf, int length)
+{
+ char cmd[2];
+
+ cmd[0] = addr;
+ if (m_i2c.write(SX9500_I2C_ADDRESS, cmd, 1, true))
+ printf("SX9500 write-fail\n");
+ if (m_i2c.read(SX9500_I2C_ADDRESS, (char *)dst_buf, length))
+ printf("SX9500 read-fail\n");
+}
+
+uint8_t SX9500::read_single(uint8_t addr)
+{
+ char cmd[2];
+
+ cmd[0] = addr;
+ if (m_i2c.write(SX9500_I2C_ADDRESS, cmd, 1, true))
+ printf("SX9500 write-fail\n");
+ if (m_i2c.read(SX9500_I2C_ADDRESS, cmd, 1))
+ printf("SX9500 read-fail\n");
+
+ return cmd[0];
+}
+
+void SX9500::standby(void)
+{
+ m_txen = 0;
+ write(SX9500_REG_PROXCTRL0, 0); // turn off all sensor pins
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sx9500.h Wed Mar 18 00:57:23 2015 +0000
@@ -0,0 +1,45 @@
+#include "mbed.h"
+
+
+#define SX9500_REG_IRQSRC 0x00
+#define SX9500_REG_STAT 0x01
+#define SX9500_REG_IRQMSK 0x03
+#define SX9500_REG_PROXCTRL0 0x06
+#define SX9500_REG_PROXCTRL1 0x07
+#define SX9500_REG_PROXCTRL2 0x08
+#define SX9500_REG_PROXCTRL3 0x09
+#define SX9500_REG_PROXCTRL4 0x0A
+#define SX9500_REG_PROXCTRL5 0x0B
+#define SX9500_REG_PROXCTRL6 0x0C
+#define SX9500_REG_PROXCTRL7 0x0D
+#define SX9500_REG_PROXCTRL8 0x0E
+#define SX9500_REG_SENSORSEL 0x20
+#define SX9500_REG_USEMSB 0x21
+#define SX9500_REG_USELSB 0x22
+#define SX9500_REG_AVGMSB 0x23
+#define SX9500_REG_AVGLSB 0x24
+#define SX9500_REG_DIFFMSB 0x25
+#define SX9500_REG_DIFFLSB 0x26
+#define SX9500_REG_OFFSETMSB 0x27
+#define SX9500_REG_OFFSETLSB 0x28
+#define SX9500_REG_RESET 0x7F
+
+#define SX9500_RESET_CMD 0xDE
+
+class SX9500 {
+ public:
+ SX9500(I2C& r, PinName en_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);
+
+ private:
+ I2C& m_i2c;
+ DigitalOut m_txen;
+};
+