SPI-Brigde (capseld), for polling interrupt, it is neccessary to adapt the constructor and the function getInt()

Committer:
x1dmoesc
Date:
Fri Feb 01 09:40:03 2019 +0000
Revision:
0:833cb2c6da5d
Child:
1:2a7edc2be6df
Rename lib; Rename func writeViaI2C -> sendViaI2C

Who changed what in which revision?

UserRevisionLine numberNew contents of line
x1dmoesc 0:833cb2c6da5d 1 #include "SC18IS602.h"
x1dmoesc 0:833cb2c6da5d 2 #include "mbed.h"
x1dmoesc 0:833cb2c6da5d 3
x1dmoesc 0:833cb2c6da5d 4 //******************************************************************************//
x1dmoesc 0:833cb2c6da5d 5 // constructor
x1dmoesc 0:833cb2c6da5d 6 //******************************************************************************//
x1dmoesc 0:833cb2c6da5d 7 SC18IS602::SC18IS602(I2C *_i2c, uint8_t uiAdr) //
x1dmoesc 0:833cb2c6da5d 8 : SC18IS602_W(HARD_ADR | (uiAdr & USER_ADR_MASK) << 1), // Initialisation list: const WRITE
x1dmoesc 0:833cb2c6da5d 9 SC18IS602_R( SC18IS602_W | 0x01), // const READ
x1dmoesc 0:833cb2c6da5d 10 iINT(0){
x1dmoesc 0:833cb2c6da5d 11 //
x1dmoesc 0:833cb2c6da5d 12 i2c = _i2c; //
x1dmoesc 0:833cb2c6da5d 13 bAck = NACK; //
x1dmoesc 0:833cb2c6da5d 14 for(int i = 0; i < BUFFER_SIZE; i++) // clear buffer
x1dmoesc 0:833cb2c6da5d 15 cCmd[i] = 0;
x1dmoesc 0:833cb2c6da5d 16
x1dmoesc 0:833cb2c6da5d 17 if(getInt()) clearInt(); //
x1dmoesc 0:833cb2c6da5d 18
x1dmoesc 0:833cb2c6da5d 19 }
x1dmoesc 0:833cb2c6da5d 20
x1dmoesc 0:833cb2c6da5d 21
x1dmoesc 0:833cb2c6da5d 22
x1dmoesc 0:833cb2c6da5d 23 //******************************************************************************//
x1dmoesc 0:833cb2c6da5d 24 // constructor
x1dmoesc 0:833cb2c6da5d 25 //******************************************************************************//
x1dmoesc 0:833cb2c6da5d 26 SC18IS602::SC18IS602(I2C *_i2c, PCA9555 *_pca , uint8_t uiAdr) //
x1dmoesc 0:833cb2c6da5d 27 : SC18IS602_W(HARD_ADR | (uiAdr & USER_ADR_MASK) << 1), // Initialisation list: const WRITE
x1dmoesc 0:833cb2c6da5d 28 SC18IS602_R( SC18IS602_W | 0x01), // const READ
x1dmoesc 0:833cb2c6da5d 29 iINT(1){
x1dmoesc 0:833cb2c6da5d 30 //
x1dmoesc 0:833cb2c6da5d 31 i2c = _i2c; //
x1dmoesc 0:833cb2c6da5d 32 pca = _pca; //
x1dmoesc 0:833cb2c6da5d 33 bAck = NACK; //
x1dmoesc 0:833cb2c6da5d 34 for(int i = 0; i < BUFFER_SIZE; i++) // clear buffer
x1dmoesc 0:833cb2c6da5d 35 cCmd[i] = 0;
x1dmoesc 0:833cb2c6da5d 36
x1dmoesc 0:833cb2c6da5d 37 if(getInt()) clearInt(); //
x1dmoesc 0:833cb2c6da5d 38
x1dmoesc 0:833cb2c6da5d 39 }
x1dmoesc 0:833cb2c6da5d 40
x1dmoesc 0:833cb2c6da5d 41
x1dmoesc 0:833cb2c6da5d 42
x1dmoesc 0:833cb2c6da5d 43 //******************************************************************************//
x1dmoesc 0:833cb2c6da5d 44 // constructor
x1dmoesc 0:833cb2c6da5d 45 //******************************************************************************//
x1dmoesc 0:833cb2c6da5d 46 SC18IS602::SC18IS602(I2C *_i2c, DigitalIn *_IntPin, uint8_t uiAdr)//
x1dmoesc 0:833cb2c6da5d 47 : SC18IS602_W(HARD_ADR | (uiAdr & USER_ADR_MASK) << 1), // Initialisation list: const WRITE
x1dmoesc 0:833cb2c6da5d 48 SC18IS602_R( SC18IS602_W | 0x01), // const READ
x1dmoesc 0:833cb2c6da5d 49 iINT(2){
x1dmoesc 0:833cb2c6da5d 50 //
x1dmoesc 0:833cb2c6da5d 51 i2c = _i2c; //
x1dmoesc 0:833cb2c6da5d 52 IntPin = _IntPin; //
x1dmoesc 0:833cb2c6da5d 53 bAck = NACK; //
x1dmoesc 0:833cb2c6da5d 54 for(int i = 0; i < BUFFER_SIZE; i++) // clear buffer
x1dmoesc 0:833cb2c6da5d 55 cCmd[i] = 0;
x1dmoesc 0:833cb2c6da5d 56
x1dmoesc 0:833cb2c6da5d 57 if(getInt()) clearInt(); //
x1dmoesc 0:833cb2c6da5d 58
x1dmoesc 0:833cb2c6da5d 59 }
x1dmoesc 0:833cb2c6da5d 60
x1dmoesc 0:833cb2c6da5d 61
x1dmoesc 0:833cb2c6da5d 62 //******************************************************************************//
x1dmoesc 0:833cb2c6da5d 63 //
x1dmoesc 0:833cb2c6da5d 64 //******************************************************************************//
x1dmoesc 0:833cb2c6da5d 65 bool SC18IS602::configSPI(uint8_t uiConf){ //
x1dmoesc 0:833cb2c6da5d 66 //
x1dmoesc 0:833cb2c6da5d 67 cCmd[0] = ADR_SPI_CONF; //
x1dmoesc 0:833cb2c6da5d 68 cCmd[1] = uiConf & 0x2F; // clear reserved bits 0b0010'1111
x1dmoesc 0:833cb2c6da5d 69 sprintf(cDebug, "Config SPI:%*s0x%02x", 5, " ", uiConf);
x1dmoesc 0:833cb2c6da5d 70 return sendViaI2C(cCmd, 2, cDebug); //
x1dmoesc 0:833cb2c6da5d 71 }
x1dmoesc 0:833cb2c6da5d 72
x1dmoesc 0:833cb2c6da5d 73
x1dmoesc 0:833cb2c6da5d 74
x1dmoesc 0:833cb2c6da5d 75 //******************************************************************************//
x1dmoesc 0:833cb2c6da5d 76 //
x1dmoesc 0:833cb2c6da5d 77 //******************************************************************************//
x1dmoesc 0:833cb2c6da5d 78 bool SC18IS602::enableGPIO(uint8_t uiConf){ //
x1dmoesc 0:833cb2c6da5d 79 //
x1dmoesc 0:833cb2c6da5d 80 cCmd[0] = ADR_GPIO_EN; //
x1dmoesc 0:833cb2c6da5d 81 cCmd[1] = uiConf & 0x0F; // clear reserved bits 0b0000'1111
x1dmoesc 0:833cb2c6da5d 82 return sendViaI2C(cCmd, 2, "Enable GPIO"); //
x1dmoesc 0:833cb2c6da5d 83 }
x1dmoesc 0:833cb2c6da5d 84
x1dmoesc 0:833cb2c6da5d 85
x1dmoesc 0:833cb2c6da5d 86
x1dmoesc 0:833cb2c6da5d 87
x1dmoesc 0:833cb2c6da5d 88 //******************************************************************************//
x1dmoesc 0:833cb2c6da5d 89 //
x1dmoesc 0:833cb2c6da5d 90 //******************************************************************************//
x1dmoesc 0:833cb2c6da5d 91 bool SC18IS602::configGPIO(uint8_t uiConf){ //
x1dmoesc 0:833cb2c6da5d 92 //
x1dmoesc 0:833cb2c6da5d 93 cCmd[0] = ADR_GPIO_CONF; //
x1dmoesc 0:833cb2c6da5d 94 cCmd[1] = uiConf; // clear reserved bits 0b0000'1111
x1dmoesc 0:833cb2c6da5d 95 return sendViaI2C(cCmd, 2); //
x1dmoesc 0:833cb2c6da5d 96 }
x1dmoesc 0:833cb2c6da5d 97
x1dmoesc 0:833cb2c6da5d 98
x1dmoesc 0:833cb2c6da5d 99
x1dmoesc 0:833cb2c6da5d 100 /******************************************************************************/
x1dmoesc 0:833cb2c6da5d 101 // sends via I2C and returns ACK or NACK
x1dmoesc 0:833cb2c6da5d 102 /******************************************************************************/
x1dmoesc 0:833cb2c6da5d 103 bool SC18IS602::sendViaI2C(const char *cData, int iLength, string sDebug)
x1dmoesc 0:833cb2c6da5d 104 {
x1dmoesc 0:833cb2c6da5d 105 bAck = (bool) i2c->write(SC18IS602_W, cData, iLength);
x1dmoesc 0:833cb2c6da5d 106
x1dmoesc 0:833cb2c6da5d 107 if (bAck == ACK) {
x1dmoesc 0:833cb2c6da5d 108 return ACK;
x1dmoesc 0:833cb2c6da5d 109
x1dmoesc 0:833cb2c6da5d 110 } else {
x1dmoesc 0:833cb2c6da5d 111 if(sizeof(sDebug) != NULL) printf("%s:%*s", sDebug.c_str(), 10, " ");
x1dmoesc 0:833cb2c6da5d 112 printf("NACK\n");
x1dmoesc 0:833cb2c6da5d 113 return NACK;
x1dmoesc 0:833cb2c6da5d 114 }
x1dmoesc 0:833cb2c6da5d 115 }
x1dmoesc 0:833cb2c6da5d 116
x1dmoesc 0:833cb2c6da5d 117
x1dmoesc 0:833cb2c6da5d 118
x1dmoesc 0:833cb2c6da5d 119 /******************************************************************************/
x1dmoesc 0:833cb2c6da5d 120 // sends via I2C and returns ACK or NACK
x1dmoesc 0:833cb2c6da5d 121 /******************************************************************************/
x1dmoesc 0:833cb2c6da5d 122 bool SC18IS602::readViaI2C(char *cData, int iLength, string sDebug){
x1dmoesc 0:833cb2c6da5d 123
x1dmoesc 0:833cb2c6da5d 124 bAck = (bool) i2c->read(SC18IS602_R, cData, iLength);
x1dmoesc 0:833cb2c6da5d 125
x1dmoesc 0:833cb2c6da5d 126 if (bAck == ACK) {
x1dmoesc 0:833cb2c6da5d 127 return ACK;
x1dmoesc 0:833cb2c6da5d 128
x1dmoesc 0:833cb2c6da5d 129 } else {
x1dmoesc 0:833cb2c6da5d 130 if(sizeof(sDebug) != NULL) printf("%s:%*s", sDebug.c_str(), 10, " ");
x1dmoesc 0:833cb2c6da5d 131 printf("NACK\n");
x1dmoesc 0:833cb2c6da5d 132 return NACK;
x1dmoesc 0:833cb2c6da5d 133 }
x1dmoesc 0:833cb2c6da5d 134 }
x1dmoesc 0:833cb2c6da5d 135
x1dmoesc 0:833cb2c6da5d 136
x1dmoesc 0:833cb2c6da5d 137 //******************************************************************************//
x1dmoesc 0:833cb2c6da5d 138 // get interrupt status
x1dmoesc 0:833cb2c6da5d 139 //******************************************************************************//
x1dmoesc 0:833cb2c6da5d 140 bool SC18IS602::getInt(){
x1dmoesc 0:833cb2c6da5d 141 //printf("Wait for Int...\n");
x1dmoesc 0:833cb2c6da5d 142 //wait(0.1);
x1dmoesc 0:833cb2c6da5d 143 if(iINT == 0) return INTERRUPT;
x1dmoesc 0:833cb2c6da5d 144 if(iINT == 1) return pca->getGPIO1_B7(true);
x1dmoesc 0:833cb2c6da5d 145 if(iINT == 2) return IntPin->read();
x1dmoesc 0:833cb2c6da5d 146
x1dmoesc 0:833cb2c6da5d 147 return not INTERRUPT;
x1dmoesc 0:833cb2c6da5d 148 }
x1dmoesc 0:833cb2c6da5d 149
x1dmoesc 0:833cb2c6da5d 150
x1dmoesc 0:833cb2c6da5d 151
x1dmoesc 0:833cb2c6da5d 152 /******************************************************************************/
x1dmoesc 0:833cb2c6da5d 153 // clears the interrupt pin
x1dmoesc 0:833cb2c6da5d 154 // returns 0(ACK) on success otherwise 1 (NACK)
x1dmoesc 0:833cb2c6da5d 155 /******************************************************************************/
x1dmoesc 0:833cb2c6da5d 156 bool SC18IS602::clearInt(){
x1dmoesc 0:833cb2c6da5d 157 cCmd[0] = 0xF1;
x1dmoesc 0:833cb2c6da5d 158 return sendViaI2C(cCmd, 1, "clear interrupt");
x1dmoesc 0:833cb2c6da5d 159 }
x1dmoesc 0:833cb2c6da5d 160
x1dmoesc 0:833cb2c6da5d 161
x1dmoesc 0:833cb2c6da5d 162 //******************************************************************************//
x1dmoesc 0:833cb2c6da5d 163 // toggle GPIO Pins //
x1dmoesc 0:833cb2c6da5d 164 //******************************************************************************//
x1dmoesc 0:833cb2c6da5d 165 bool SC18IS602::gpio_toggle(uint8_t uiPort){ //
x1dmoesc 0:833cb2c6da5d 166
x1dmoesc 0:833cb2c6da5d 167 uiPort &= 0x0F; // clear reserved bits
x1dmoesc 0:833cb2c6da5d 168
x1dmoesc 0:833cb2c6da5d 169 cCmd[0] = ADR_GPIO_READ; // Read from GPIO port
x1dmoesc 0:833cb2c6da5d 170 sendViaI2C(cCmd, 1, "Read GPIO");
x1dmoesc 0:833cb2c6da5d 171 bAck = (bool) readViaI2C(&cCmd[1], 1);
x1dmoesc 0:833cb2c6da5d 172
x1dmoesc 0:833cb2c6da5d 173 if(bAck == NACK) return bAck; // if NACK, return
x1dmoesc 0:833cb2c6da5d 174
x1dmoesc 0:833cb2c6da5d 175 cCmd[0] = ADR_GPIO_WRITE;
x1dmoesc 0:833cb2c6da5d 176 cCmd[1] ^= uiPort; // toogle given pins (uiTogllePin)
x1dmoesc 0:833cb2c6da5d 177 return sendViaI2C(cCmd, 2, "GPIO tog");
x1dmoesc 0:833cb2c6da5d 178
x1dmoesc 0:833cb2c6da5d 179 }
x1dmoesc 0:833cb2c6da5d 180
x1dmoesc 0:833cb2c6da5d 181
x1dmoesc 0:833cb2c6da5d 182 /******************************************************************************/
x1dmoesc 0:833cb2c6da5d 183 // swith the GPIO Pin for debugging on
x1dmoesc 0:833cb2c6da5d 184 /******************************************************************************/
x1dmoesc 0:833cb2c6da5d 185 bool SC18IS602::GPIO_pin3_off()
x1dmoesc 0:833cb2c6da5d 186 {
x1dmoesc 0:833cb2c6da5d 187 cCmd[0] = ADR_GPIO_WRITE;
x1dmoesc 0:833cb2c6da5d 188 cCmd[1] = (1 << GPIO_CS3);
x1dmoesc 0:833cb2c6da5d 189 return sendViaI2C(cCmd, 2, "GPIO off");
x1dmoesc 0:833cb2c6da5d 190 }
x1dmoesc 0:833cb2c6da5d 191
x1dmoesc 0:833cb2c6da5d 192
x1dmoesc 0:833cb2c6da5d 193 /******************************************************************************/
x1dmoesc 0:833cb2c6da5d 194 // swith the GPIO Pin for debugging off
x1dmoesc 0:833cb2c6da5d 195 /******************************************************************************/
x1dmoesc 0:833cb2c6da5d 196 bool SC18IS602::GPIO_pin3_on()
x1dmoesc 0:833cb2c6da5d 197 {
x1dmoesc 0:833cb2c6da5d 198 cCmd[0] = ADR_GPIO_WRITE;
x1dmoesc 0:833cb2c6da5d 199 cCmd[1] = (0 << GPIO_CS3);
x1dmoesc 0:833cb2c6da5d 200 return sendViaI2C(cCmd, 2, "GPIO on");
x1dmoesc 0:833cb2c6da5d 201 }
x1dmoesc 0:833cb2c6da5d 202
x1dmoesc 0:833cb2c6da5d 203
x1dmoesc 0:833cb2c6da5d 204
x1dmoesc 0:833cb2c6da5d 205 //******************************************************************************//
x1dmoesc 0:833cb2c6da5d 206 //
x1dmoesc 0:833cb2c6da5d 207 //******************************************************************************//
x1dmoesc 0:833cb2c6da5d 208 bool SC18IS602::sendViaSPI(char cAdrByte, char *cDataBytes, uint8_t uiNum)
x1dmoesc 0:833cb2c6da5d 209 {
x1dmoesc 0:833cb2c6da5d 210 if((int)uiNum >= BUFFER_SIZE - 1) // If number of sending Bytes greather than BUFFER_SIZE - 1 (Register-Adress-Byte)
x1dmoesc 0:833cb2c6da5d 211 return NACK; // returns NACK (failure)
x1dmoesc 0:833cb2c6da5d 212
x1dmoesc 0:833cb2c6da5d 213
x1dmoesc 0:833cb2c6da5d 214 uiNumByte = 0;
x1dmoesc 0:833cb2c6da5d 215 cCmd[uiNumByte] = CMD_RW_CS0; // send via SPI and CS0 (SS0)
x1dmoesc 0:833cb2c6da5d 216 cCmd[++uiNumByte] = cAdrByte;
x1dmoesc 0:833cb2c6da5d 217 for(int n = uiNum - 1 ; n >= 0; n--) {
x1dmoesc 0:833cb2c6da5d 218 cCmd[++uiNumByte] = cDataBytes[n];
x1dmoesc 0:833cb2c6da5d 219 }
x1dmoesc 0:833cb2c6da5d 220
x1dmoesc 0:833cb2c6da5d 221
x1dmoesc 0:833cb2c6da5d 222 uiNumByte++; // char cCmd counts from 0, so the number is +1
x1dmoesc 0:833cb2c6da5d 223 bAck = sendViaI2C(cCmd, uiNumByte, "TX via SPI"); // send via SPI
x1dmoesc 0:833cb2c6da5d 224
x1dmoesc 0:833cb2c6da5d 225
x1dmoesc 0:833cb2c6da5d 226 while(getInt() == not INTERRUPT); // wait until sending is finished and an interrupt occurs
x1dmoesc 0:833cb2c6da5d 227 clearInt();
x1dmoesc 0:833cb2c6da5d 228 while(getInt() == INTERRUPT); // If there was an interrupt, wait until it is cleared
x1dmoesc 0:833cb2c6da5d 229
x1dmoesc 0:833cb2c6da5d 230 return bAck;
x1dmoesc 0:833cb2c6da5d 231 }
x1dmoesc 0:833cb2c6da5d 232
x1dmoesc 0:833cb2c6da5d 233
x1dmoesc 0:833cb2c6da5d 234
x1dmoesc 0:833cb2c6da5d 235 //******************************************************************************//
x1dmoesc 0:833cb2c6da5d 236 // returns reading data. The first byte is the status byte
x1dmoesc 0:833cb2c6da5d 237 //******************************************************************************//
x1dmoesc 0:833cb2c6da5d 238 bool SC18IS602::readViaSPI(char cAdrByte, char *cDataBytes, uint8_t uiNum)
x1dmoesc 0:833cb2c6da5d 239 {
x1dmoesc 0:833cb2c6da5d 240 if((int)uiNum >= BUFFER_SIZE - 1) // If number of sending Bytes greather than BUFFER_SIZE - 1 (Register-Adress-Byte)
x1dmoesc 0:833cb2c6da5d 241 return NACK; // returns NACK (failure)
x1dmoesc 0:833cb2c6da5d 242
x1dmoesc 0:833cb2c6da5d 243
x1dmoesc 0:833cb2c6da5d 244 // send first time Read comand
x1dmoesc 0:833cb2c6da5d 245 uiNumByte = 0;
x1dmoesc 0:833cb2c6da5d 246 cCmd[uiNumByte] = CMD_RW_CS0; // send via SPI and CS0 (SS0)
x1dmoesc 0:833cb2c6da5d 247 cCmd[++uiNumByte] = cAdrByte;
x1dmoesc 0:833cb2c6da5d 248
x1dmoesc 0:833cb2c6da5d 249 for(int n = uiNum - 1 ; n >= 0; n--) {
x1dmoesc 0:833cb2c6da5d 250 cCmd[++uiNumByte] = 0x00;
x1dmoesc 0:833cb2c6da5d 251 }
x1dmoesc 0:833cb2c6da5d 252 uiNumByte++; // char cCmd counts from 0, so the number is +1
x1dmoesc 0:833cb2c6da5d 253
x1dmoesc 0:833cb2c6da5d 254 while(getInt() == INTERRUPT); // If there was an interrupt, wait until it is cleared
x1dmoesc 0:833cb2c6da5d 255 bAck = sendViaI2C(cCmd, uiNumByte, "RX via SPI");
x1dmoesc 0:833cb2c6da5d 256 while(getInt() == not INTERRUPT);
x1dmoesc 0:833cb2c6da5d 257 clearInt();
x1dmoesc 0:833cb2c6da5d 258
x1dmoesc 0:833cb2c6da5d 259
x1dmoesc 0:833cb2c6da5d 260 // send secound time read comand (dummy) to receive data from first read command
x1dmoesc 0:833cb2c6da5d 261 uiNumByte = 0;
x1dmoesc 0:833cb2c6da5d 262 cCmd[uiNumByte++] = CMD_RW_CS0;
x1dmoesc 0:833cb2c6da5d 263 for(int n = uiNum - 1 ; n >= 0; n--) {
x1dmoesc 0:833cb2c6da5d 264 cCmd[uiNumByte++] = 0x00;
x1dmoesc 0:833cb2c6da5d 265 }
x1dmoesc 0:833cb2c6da5d 266
x1dmoesc 0:833cb2c6da5d 267 while(getInt() == INTERRUPT); // If there was an interrupt, wait until it is cleared
x1dmoesc 0:833cb2c6da5d 268 bAck = sendViaI2C(cCmd, uiNumByte, "RX via SPI");
x1dmoesc 0:833cb2c6da5d 269 while(getInt() == not INTERRUPT); // wait until sending is finished and an interrupt occurs
x1dmoesc 0:833cb2c6da5d 270 clearInt();
x1dmoesc 0:833cb2c6da5d 271
x1dmoesc 0:833cb2c6da5d 272
x1dmoesc 0:833cb2c6da5d 273 bAck = (bool) readViaI2C(cDataBytes, uiNumByte);
x1dmoesc 0:833cb2c6da5d 274
x1dmoesc 0:833cb2c6da5d 275 int n = 0;
x1dmoesc 0:833cb2c6da5d 276 for(int i = 0; i < int(uiNumByte/2); i++){
x1dmoesc 0:833cb2c6da5d 277 n = uiNumByte - 1 - i;
x1dmoesc 0:833cb2c6da5d 278 //printf("%d <-> %d\n", i, n);
x1dmoesc 0:833cb2c6da5d 279 cDataBytes[i] = cDataBytes[i] ^ cDataBytes[n];
x1dmoesc 0:833cb2c6da5d 280 cDataBytes[n] = cDataBytes[i] ^ cDataBytes[n];
x1dmoesc 0:833cb2c6da5d 281 cDataBytes[i] = cDataBytes[i] ^ cDataBytes[n];
x1dmoesc 0:833cb2c6da5d 282 }
x1dmoesc 0:833cb2c6da5d 283
x1dmoesc 0:833cb2c6da5d 284 return bAck;
x1dmoesc 0:833cb2c6da5d 285 }
x1dmoesc 0:833cb2c6da5d 286
x1dmoesc 0:833cb2c6da5d 287