added init function from NXP Rapid ioT SDK, read_proximity_sensors function now works well.

Dependents:   rIoTwear-touch rIoTwear-snake

Committer:
batman52
Date:
Fri Dec 27 15:16:40 2019 +0000
Revision:
3:b0a2ba594005
Parent:
2:f88a77463a32
removed printf in service method

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dudmuck 0:d46a1b9267a3 1 #include "sx9500.h"
dudmuck 0:d46a1b9267a3 2
dudmuck 0:d46a1b9267a3 3 #define SX9500_I2C_ADDRESS 0x50 //0x28
dudmuck 0:d46a1b9267a3 4
dudmuck 1:aa30dc96dc77 5
dudmuck 1:aa30dc96dc77 6 SX9500::SX9500(I2C& r, PinName en_pin, PinName nirq_pin) : m_i2c(r), m_txen(en_pin), m_nirq(nirq_pin)
dudmuck 0:d46a1b9267a3 7 {
dudmuck 1:aa30dc96dc77 8 m_nirq.mode(PullUp); // no pullup on board?
dudmuck 1:aa30dc96dc77 9 RegProxCtrl0.octet = read_single(SX9500_REG_PROXCTRL0);
dudmuck 0:d46a1b9267a3 10 }
dudmuck 0:d46a1b9267a3 11
dudmuck 0:d46a1b9267a3 12 SX9500::~SX9500()
dudmuck 0:d46a1b9267a3 13 {
dudmuck 0:d46a1b9267a3 14 }
dudmuck 0:d46a1b9267a3 15
dudmuck 0:d46a1b9267a3 16 void SX9500::reset()
dudmuck 0:d46a1b9267a3 17 {
dudmuck 0:d46a1b9267a3 18 write(SX9500_REG_RESET, SX9500_RESET_CMD);
dudmuck 0:d46a1b9267a3 19 }
dudmuck 0:d46a1b9267a3 20
dudmuck 1:aa30dc96dc77 21 void SX9500::print_sensor(char CSn)
dudmuck 0:d46a1b9267a3 22 {
dudmuck 0:d46a1b9267a3 23 uint8_t buf[2];
dudmuck 1:aa30dc96dc77 24
dudmuck 1:aa30dc96dc77 25 write(SX9500_REG_SENSORSEL, CSn);
dudmuck 1:aa30dc96dc77 26
dudmuck 1:aa30dc96dc77 27 read(SX9500_REG_USEMSB, buf, 2);
dudmuck 1:aa30dc96dc77 28 printf("%d useful:0x%02x%02x\r\n", CSn, buf[0], buf[1]);
dudmuck 1:aa30dc96dc77 29
dudmuck 1:aa30dc96dc77 30 read(SX9500_REG_AVGMSB, buf, 2);
batman52 2:f88a77463a32 31 //printf("%d avg:0x%02x%02x\r\n", CSn, buf[0], buf[1]);
dudmuck 1:aa30dc96dc77 32
dudmuck 1:aa30dc96dc77 33 read(SX9500_REG_DIFFMSB, buf, 2);
batman52 2:f88a77463a32 34 // printf("%d diff:0x%02x%02x\r\n", CSn, buf[0], buf[1]);
batman52 2:f88a77463a32 35 }
batman52 2:f88a77463a32 36
batman52 2:f88a77463a32 37
batman52 2:f88a77463a32 38 /*
batman52 2:f88a77463a32 39 * Copyright (c) 2018 NXP
batman52 2:f88a77463a32 40 *
batman52 2:f88a77463a32 41 * Redistribution and use in source and binary forms, with or without modification,
batman52 2:f88a77463a32 42 * are permitted provided that the following conditions are met:
batman52 2:f88a77463a32 43 *
batman52 2:f88a77463a32 44 * o Redistributions of source code must retain the above copyright notice, this list
batman52 2:f88a77463a32 45 * of conditions and the following disclaimer.
batman52 2:f88a77463a32 46 *
batman52 2:f88a77463a32 47 * o Redistributions in binary form must reproduce the above copyright notice, this
batman52 2:f88a77463a32 48 * list of conditions and the following disclaimer in the documentation and/or
batman52 2:f88a77463a32 49 * other materials provided with the distribution.
batman52 2:f88a77463a32 50 *
batman52 2:f88a77463a32 51 * o Neither the name of the copyright holder nor the names of its
batman52 2:f88a77463a32 52 * contributors may be used to endorse or promote products derived from this
batman52 2:f88a77463a32 53 * software without specific prior written permission.
batman52 2:f88a77463a32 54 *
batman52 2:f88a77463a32 55 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
batman52 2:f88a77463a32 56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
batman52 2:f88a77463a32 57 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
batman52 2:f88a77463a32 58 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
batman52 2:f88a77463a32 59 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
batman52 2:f88a77463a32 60 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
batman52 2:f88a77463a32 61 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
batman52 2:f88a77463a32 62 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
batman52 2:f88a77463a32 63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
batman52 2:f88a77463a32 64 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
batman52 2:f88a77463a32 65 */
batman52 2:f88a77463a32 66
batman52 2:f88a77463a32 67 uint8_t SX9500::init(void)
batman52 2:f88a77463a32 68 {
batman52 2:f88a77463a32 69 uint8_t lenRegTable = sizeof(sx9500_i2c_reg_setup) / sizeof(smtc_reg_data_t);
batman52 2:f88a77463a32 70 uint8_t val;
batman52 2:f88a77463a32 71 uint8_t i = 0;
batman52 2:f88a77463a32 72
batman52 2:f88a77463a32 73 // read IRQSRC to release NIRQ pin
batman52 2:f88a77463a32 74 read(SX9500_REG_IRQSRC, &val,1);
batman52 2:f88a77463a32 75
batman52 2:f88a77463a32 76 while (i < lenRegTable)
batman52 2:f88a77463a32 77 {
batman52 2:f88a77463a32 78 /* Write all registers/values contained in i2c_reg */
batman52 2:f88a77463a32 79 write(sx9500_i2c_reg_setup[i].reg, sx9500_i2c_reg_setup[i].val);
batman52 2:f88a77463a32 80
batman52 2:f88a77463a32 81 /* Read back value from register and verify write */
batman52 2:f88a77463a32 82 val = read_single(sx9500_i2c_reg_setup[i].reg);
batman52 2:f88a77463a32 83
batman52 2:f88a77463a32 84 if (val != sx9500_i2c_reg_setup[i].val)
batman52 2:f88a77463a32 85 {
batman52 2:f88a77463a32 86 return SX9500_INTERNAL_ERROR;
batman52 2:f88a77463a32 87 }
batman52 2:f88a77463a32 88
batman52 2:f88a77463a32 89 i++;
batman52 2:f88a77463a32 90 }
batman52 2:f88a77463a32 91
batman52 2:f88a77463a32 92 return SX9500_SUCCESS;
batman52 2:f88a77463a32 93
batman52 2:f88a77463a32 94 }
batman52 2:f88a77463a32 95
batman52 2:f88a77463a32 96 /*
batman52 2:f88a77463a32 97 * END Copyright (c) 2018 NXP
batman52 2:f88a77463a32 98 *
batman52 2:f88a77463a32 99 */
batman52 2:f88a77463a32 100
batman52 2:f88a77463a32 101 SX9500_TouchState_t SX9500::read_proximity_sensors()
batman52 2:f88a77463a32 102 {
batman52 2:f88a77463a32 103 SX9500_TouchState_t touchState;
batman52 2:f88a77463a32 104 RegStat_t prox;
batman52 2:f88a77463a32 105
batman52 2:f88a77463a32 106 read(SX9500_REG_STAT, &prox.octet, 2);
batman52 2:f88a77463a32 107
batman52 2:f88a77463a32 108 memset((void *)&touchState, 0, sizeof(SX9500_TouchState_t));
batman52 2:f88a77463a32 109 if(prox.octet > 0)
batman52 2:f88a77463a32 110 {
batman52 2:f88a77463a32 111 if(prox.bits.proxstat0)
batman52 2:f88a77463a32 112 touchState.downPressed = true;
batman52 2:f88a77463a32 113
batman52 2:f88a77463a32 114 if(prox.bits.proxstat1)
batman52 2:f88a77463a32 115 touchState.rightPressed = true;
batman52 2:f88a77463a32 116
batman52 2:f88a77463a32 117 if(prox.bits.proxstat2)
batman52 2:f88a77463a32 118 touchState.upPressed = true;
batman52 2:f88a77463a32 119
batman52 2:f88a77463a32 120 if(prox.bits.proxstat3)
batman52 2:f88a77463a32 121 touchState.leftPressed = true;
batman52 2:f88a77463a32 122 }
batman52 2:f88a77463a32 123
batman52 2:f88a77463a32 124 return touchState;
dudmuck 0:d46a1b9267a3 125 }
dudmuck 0:d46a1b9267a3 126
dudmuck 0:d46a1b9267a3 127 void SX9500::write(uint8_t addr, uint8_t data)
dudmuck 0:d46a1b9267a3 128 {
dudmuck 0:d46a1b9267a3 129 uint8_t cmd[2];
dudmuck 0:d46a1b9267a3 130
dudmuck 0:d46a1b9267a3 131 cmd[0] = addr;
dudmuck 0:d46a1b9267a3 132 cmd[1] = data;
dudmuck 0:d46a1b9267a3 133
dudmuck 0:d46a1b9267a3 134 if (m_i2c.write(SX9500_I2C_ADDRESS, (char *)cmd, 2))
dudmuck 0:d46a1b9267a3 135 printf("SX9500 write-fail\n");
dudmuck 0:d46a1b9267a3 136 }
dudmuck 0:d46a1b9267a3 137
dudmuck 0:d46a1b9267a3 138 void SX9500::read(uint8_t addr, uint8_t *dst_buf, int length)
dudmuck 0:d46a1b9267a3 139 {
dudmuck 0:d46a1b9267a3 140 char cmd[2];
dudmuck 0:d46a1b9267a3 141
dudmuck 0:d46a1b9267a3 142 cmd[0] = addr;
dudmuck 0:d46a1b9267a3 143 if (m_i2c.write(SX9500_I2C_ADDRESS, cmd, 1, true))
dudmuck 0:d46a1b9267a3 144 printf("SX9500 write-fail\n");
dudmuck 0:d46a1b9267a3 145 if (m_i2c.read(SX9500_I2C_ADDRESS, (char *)dst_buf, length))
dudmuck 0:d46a1b9267a3 146 printf("SX9500 read-fail\n");
dudmuck 0:d46a1b9267a3 147 }
dudmuck 0:d46a1b9267a3 148
dudmuck 0:d46a1b9267a3 149 uint8_t SX9500::read_single(uint8_t addr)
dudmuck 0:d46a1b9267a3 150 {
dudmuck 0:d46a1b9267a3 151 char cmd[2];
dudmuck 0:d46a1b9267a3 152
dudmuck 0:d46a1b9267a3 153 cmd[0] = addr;
dudmuck 0:d46a1b9267a3 154 if (m_i2c.write(SX9500_I2C_ADDRESS, cmd, 1, true))
dudmuck 0:d46a1b9267a3 155 printf("SX9500 write-fail\n");
dudmuck 0:d46a1b9267a3 156 if (m_i2c.read(SX9500_I2C_ADDRESS, cmd, 1))
dudmuck 0:d46a1b9267a3 157 printf("SX9500 read-fail\n");
dudmuck 0:d46a1b9267a3 158
dudmuck 0:d46a1b9267a3 159 return cmd[0];
dudmuck 0:d46a1b9267a3 160 }
dudmuck 1:aa30dc96dc77 161
dudmuck 1:aa30dc96dc77 162 bool SX9500::get_active()
dudmuck 0:d46a1b9267a3 163 {
dudmuck 1:aa30dc96dc77 164 if (m_txen.read())
dudmuck 1:aa30dc96dc77 165 return true;
dudmuck 1:aa30dc96dc77 166 else
dudmuck 1:aa30dc96dc77 167 return false;
dudmuck 1:aa30dc96dc77 168 }
dudmuck 1:aa30dc96dc77 169
dudmuck 1:aa30dc96dc77 170 void SX9500::set_active(bool en)
dudmuck 1:aa30dc96dc77 171 {
dudmuck 1:aa30dc96dc77 172 if (en) {
dudmuck 1:aa30dc96dc77 173 m_txen = 1;
dudmuck 1:aa30dc96dc77 174 } else {
dudmuck 1:aa30dc96dc77 175 /* lowest power (non)operation */
dudmuck 1:aa30dc96dc77 176 m_txen = 0;
dudmuck 1:aa30dc96dc77 177 write(SX9500_REG_PROXCTRL0, 0); // turn off all sensor pins
dudmuck 1:aa30dc96dc77 178 }
dudmuck 0:d46a1b9267a3 179 }
dudmuck 0:d46a1b9267a3 180
dudmuck 1:aa30dc96dc77 181 void SX9500::service()
dudmuck 1:aa30dc96dc77 182 {
dudmuck 1:aa30dc96dc77 183 if (m_nirq.read())
dudmuck 1:aa30dc96dc77 184 return;
dudmuck 1:aa30dc96dc77 185
dudmuck 1:aa30dc96dc77 186 RegIrqSrc.octet = read_single(SX9500_REG_IRQSRC);
batman52 3:b0a2ba594005 187
batman52 3:b0a2ba594005 188 /*
dudmuck 1:aa30dc96dc77 189 printf("95irq:");
dudmuck 1:aa30dc96dc77 190 if (RegIrqSrc.bits.conv_done) {
dudmuck 1:aa30dc96dc77 191 printf("conv_done:\r\n");
dudmuck 1:aa30dc96dc77 192 print_sensor(0);
dudmuck 1:aa30dc96dc77 193 print_sensor(1);
dudmuck 1:aa30dc96dc77 194 }
dudmuck 1:aa30dc96dc77 195 if (RegIrqSrc.bits.comp_done) {
dudmuck 1:aa30dc96dc77 196 printf("comp_done ");
dudmuck 1:aa30dc96dc77 197 }
dudmuck 1:aa30dc96dc77 198 if (RegIrqSrc.bits.far || RegIrqSrc.bits.close) {
dudmuck 1:aa30dc96dc77 199 RegStat_t stat;
dudmuck 1:aa30dc96dc77 200 printf("stat ");
dudmuck 1:aa30dc96dc77 201 stat.octet = read_single(SX9500_REG_STAT);
dudmuck 1:aa30dc96dc77 202 if (stat.bits.proxstat0)
dudmuck 1:aa30dc96dc77 203 printf("cs0 ");
dudmuck 1:aa30dc96dc77 204 if (stat.bits.proxstat1)
dudmuck 1:aa30dc96dc77 205 printf("cs1 ");
dudmuck 1:aa30dc96dc77 206 }
dudmuck 1:aa30dc96dc77 207
dudmuck 1:aa30dc96dc77 208 if (RegIrqSrc.bits.reset) {
dudmuck 1:aa30dc96dc77 209 printf("reset ");
dudmuck 1:aa30dc96dc77 210 }
dudmuck 1:aa30dc96dc77 211
dudmuck 1:aa30dc96dc77 212 printf("\r\n");
batman52 3:b0a2ba594005 213 */
dudmuck 1:aa30dc96dc77 214 }
dudmuck 1:aa30dc96dc77 215