SPI-Brigde (capseld), for polling interrupt, it is neccessary to adapt the constructor and the function getInt()
SC18IS602.cpp@3:9cf83f16c17d, 2020-04-23 (annotated)
- Committer:
- x1dmoesc
- Date:
- Thu Apr 23 22:55:50 2020 +0000
- Revision:
- 3:9cf83f16c17d
- Parent:
- 2:cb90c271c412
last version;
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:cb90c271c412 | 18 | wait(0.01); |
x1dmoesc | 1:2a7edc2be6df | 19 | |
x1dmoesc | 2:cb90c271c412 | 20 | //getInt_ptr = NULL; |
x1dmoesc | 0:833cb2c6da5d | 21 | |
x1dmoesc | 0:833cb2c6da5d | 22 | } |
x1dmoesc | 0:833cb2c6da5d | 23 | |
x1dmoesc | 0:833cb2c6da5d | 24 | |
x1dmoesc | 0:833cb2c6da5d | 25 | |
x1dmoesc | 0:833cb2c6da5d | 26 | //******************************************************************************// |
x1dmoesc | 0:833cb2c6da5d | 27 | // constructor |
x1dmoesc | 0:833cb2c6da5d | 28 | //******************************************************************************// |
x1dmoesc | 0:833cb2c6da5d | 29 | SC18IS602::SC18IS602(I2C *_i2c, PCA9555 *_pca , uint8_t uiAdr) // |
x1dmoesc | 0:833cb2c6da5d | 30 | : SC18IS602_W(HARD_ADR | (uiAdr & USER_ADR_MASK) << 1), // Initialisation list: const WRITE |
x1dmoesc | 0:833cb2c6da5d | 31 | SC18IS602_R( SC18IS602_W | 0x01), // const READ |
x1dmoesc | 0:833cb2c6da5d | 32 | iINT(1){ |
x1dmoesc | 0:833cb2c6da5d | 33 | // |
x1dmoesc | 0:833cb2c6da5d | 34 | i2c = _i2c; // |
x1dmoesc | 0:833cb2c6da5d | 35 | pca = _pca; // |
x1dmoesc | 0:833cb2c6da5d | 36 | bAck = NACK; // |
x1dmoesc | 0:833cb2c6da5d | 37 | for(int i = 0; i < BUFFER_SIZE; i++) // clear buffer |
x1dmoesc | 0:833cb2c6da5d | 38 | cCmd[i] = 0; |
x1dmoesc | 2:cb90c271c412 | 39 | |
x1dmoesc | 0:833cb2c6da5d | 40 | if(getInt()) clearInt(); // |
x1dmoesc | 2:cb90c271c412 | 41 | wait(0.01); |
x1dmoesc | 0:833cb2c6da5d | 42 | } |
x1dmoesc | 0:833cb2c6da5d | 43 | |
x1dmoesc | 0:833cb2c6da5d | 44 | |
x1dmoesc | 0:833cb2c6da5d | 45 | |
x1dmoesc | 0:833cb2c6da5d | 46 | //******************************************************************************// |
x1dmoesc | 0:833cb2c6da5d | 47 | // constructor |
x1dmoesc | 0:833cb2c6da5d | 48 | //******************************************************************************// |
x1dmoesc | 0:833cb2c6da5d | 49 | SC18IS602::SC18IS602(I2C *_i2c, DigitalIn *_IntPin, uint8_t uiAdr)// |
x1dmoesc | 0:833cb2c6da5d | 50 | : SC18IS602_W(HARD_ADR | (uiAdr & USER_ADR_MASK) << 1), // Initialisation list: const WRITE |
x1dmoesc | 0:833cb2c6da5d | 51 | SC18IS602_R( SC18IS602_W | 0x01), // const READ |
x1dmoesc | 0:833cb2c6da5d | 52 | iINT(2){ |
x1dmoesc | 0:833cb2c6da5d | 53 | // |
x1dmoesc | 0:833cb2c6da5d | 54 | i2c = _i2c; // |
x1dmoesc | 0:833cb2c6da5d | 55 | IntPin = _IntPin; // |
x1dmoesc | 0:833cb2c6da5d | 56 | bAck = NACK; // |
x1dmoesc | 0:833cb2c6da5d | 57 | for(int i = 0; i < BUFFER_SIZE; i++) // clear buffer |
x1dmoesc | 0:833cb2c6da5d | 58 | cCmd[i] = 0; |
x1dmoesc | 0:833cb2c6da5d | 59 | |
x1dmoesc | 0:833cb2c6da5d | 60 | if(getInt()) clearInt(); // |
x1dmoesc | 2:cb90c271c412 | 61 | wait(0.01); |
x1dmoesc | 0:833cb2c6da5d | 62 | |
x1dmoesc | 0:833cb2c6da5d | 63 | } |
x1dmoesc | 0:833cb2c6da5d | 64 | |
x1dmoesc | 0:833cb2c6da5d | 65 | |
x1dmoesc | 0:833cb2c6da5d | 66 | //******************************************************************************// |
x1dmoesc | 0:833cb2c6da5d | 67 | // |
x1dmoesc | 0:833cb2c6da5d | 68 | //******************************************************************************// |
x1dmoesc | 0:833cb2c6da5d | 69 | bool SC18IS602::configSPI(uint8_t uiConf){ // |
x1dmoesc | 0:833cb2c6da5d | 70 | // |
x1dmoesc | 0:833cb2c6da5d | 71 | cCmd[0] = ADR_SPI_CONF; // |
x1dmoesc | 0:833cb2c6da5d | 72 | cCmd[1] = uiConf & 0x2F; // clear reserved bits 0b0010'1111 |
x1dmoesc | 0:833cb2c6da5d | 73 | sprintf(cDebug, "Config SPI:%*s0x%02x", 5, " ", uiConf); |
x1dmoesc | 0:833cb2c6da5d | 74 | return sendViaI2C(cCmd, 2, cDebug); // |
x1dmoesc | 0:833cb2c6da5d | 75 | } |
x1dmoesc | 0:833cb2c6da5d | 76 | |
x1dmoesc | 0:833cb2c6da5d | 77 | |
x1dmoesc | 0:833cb2c6da5d | 78 | |
x1dmoesc | 0:833cb2c6da5d | 79 | //******************************************************************************// |
x1dmoesc | 0:833cb2c6da5d | 80 | // |
x1dmoesc | 0:833cb2c6da5d | 81 | //******************************************************************************// |
x1dmoesc | 0:833cb2c6da5d | 82 | bool SC18IS602::enableGPIO(uint8_t uiConf){ // |
x1dmoesc | 0:833cb2c6da5d | 83 | // |
x1dmoesc | 2:cb90c271c412 | 84 | cCmd[0] = ADR_GPIO.EN; // |
x1dmoesc | 0:833cb2c6da5d | 85 | cCmd[1] = uiConf & 0x0F; // clear reserved bits 0b0000'1111 |
x1dmoesc | 0:833cb2c6da5d | 86 | return sendViaI2C(cCmd, 2, "Enable GPIO"); // |
x1dmoesc | 0:833cb2c6da5d | 87 | } |
x1dmoesc | 0:833cb2c6da5d | 88 | |
x1dmoesc | 0:833cb2c6da5d | 89 | |
x1dmoesc | 0:833cb2c6da5d | 90 | |
x1dmoesc | 0:833cb2c6da5d | 91 | |
x1dmoesc | 0:833cb2c6da5d | 92 | //******************************************************************************// |
x1dmoesc | 0:833cb2c6da5d | 93 | // |
x1dmoesc | 0:833cb2c6da5d | 94 | //******************************************************************************// |
x1dmoesc | 0:833cb2c6da5d | 95 | bool SC18IS602::configGPIO(uint8_t uiConf){ // |
x1dmoesc | 0:833cb2c6da5d | 96 | // |
x1dmoesc | 2:cb90c271c412 | 97 | cCmd[0] = ADR_GPIO.CONF; // |
x1dmoesc | 0:833cb2c6da5d | 98 | cCmd[1] = uiConf; // clear reserved bits 0b0000'1111 |
x1dmoesc | 0:833cb2c6da5d | 99 | return sendViaI2C(cCmd, 2); // |
x1dmoesc | 0:833cb2c6da5d | 100 | } |
x1dmoesc | 0:833cb2c6da5d | 101 | |
x1dmoesc | 0:833cb2c6da5d | 102 | |
x1dmoesc | 0:833cb2c6da5d | 103 | |
x1dmoesc | 0:833cb2c6da5d | 104 | /******************************************************************************/ |
x1dmoesc | 0:833cb2c6da5d | 105 | // sends via I2C and returns ACK or NACK |
x1dmoesc | 0:833cb2c6da5d | 106 | /******************************************************************************/ |
x1dmoesc | 0:833cb2c6da5d | 107 | bool SC18IS602::sendViaI2C(const char *cData, int iLength, string sDebug) |
x1dmoesc | 0:833cb2c6da5d | 108 | { |
x1dmoesc | 2:cb90c271c412 | 109 | bAck = not i2c->write(SC18IS602_W, cData, iLength); |
x1dmoesc | 2:cb90c271c412 | 110 | //printf("%d\n", bAck); |
x1dmoesc | 0:833cb2c6da5d | 111 | if (bAck == ACK) { |
x1dmoesc | 0:833cb2c6da5d | 112 | return ACK; |
x1dmoesc | 0:833cb2c6da5d | 113 | |
x1dmoesc | 0:833cb2c6da5d | 114 | } else { |
x1dmoesc | 0:833cb2c6da5d | 115 | if(sizeof(sDebug) != NULL) printf("%s:%*s", sDebug.c_str(), 10, " "); |
x1dmoesc | 0:833cb2c6da5d | 116 | printf("NACK\n"); |
x1dmoesc | 0:833cb2c6da5d | 117 | return NACK; |
x1dmoesc | 0:833cb2c6da5d | 118 | } |
x1dmoesc | 0:833cb2c6da5d | 119 | } |
x1dmoesc | 0:833cb2c6da5d | 120 | |
x1dmoesc | 0:833cb2c6da5d | 121 | |
x1dmoesc | 0:833cb2c6da5d | 122 | |
x1dmoesc | 0:833cb2c6da5d | 123 | /******************************************************************************/ |
x1dmoesc | 0:833cb2c6da5d | 124 | // sends via I2C and returns ACK or NACK |
x1dmoesc | 0:833cb2c6da5d | 125 | /******************************************************************************/ |
x1dmoesc | 0:833cb2c6da5d | 126 | bool SC18IS602::readViaI2C(char *cData, int iLength, string sDebug){ |
x1dmoesc | 0:833cb2c6da5d | 127 | |
x1dmoesc | 2:cb90c271c412 | 128 | bAck = not i2c->read(SC18IS602_R, cData, iLength); |
x1dmoesc | 0:833cb2c6da5d | 129 | |
x1dmoesc | 0:833cb2c6da5d | 130 | if (bAck == ACK) { |
x1dmoesc | 0:833cb2c6da5d | 131 | return ACK; |
x1dmoesc | 0:833cb2c6da5d | 132 | |
x1dmoesc | 0:833cb2c6da5d | 133 | } else { |
x1dmoesc | 0:833cb2c6da5d | 134 | if(sizeof(sDebug) != NULL) printf("%s:%*s", sDebug.c_str(), 10, " "); |
x1dmoesc | 0:833cb2c6da5d | 135 | printf("NACK\n"); |
x1dmoesc | 0:833cb2c6da5d | 136 | return NACK; |
x1dmoesc | 0:833cb2c6da5d | 137 | } |
x1dmoesc | 0:833cb2c6da5d | 138 | } |
x1dmoesc | 0:833cb2c6da5d | 139 | |
x1dmoesc | 0:833cb2c6da5d | 140 | |
x1dmoesc | 0:833cb2c6da5d | 141 | //******************************************************************************// |
x1dmoesc | 0:833cb2c6da5d | 142 | // get interrupt status |
x1dmoesc | 0:833cb2c6da5d | 143 | //******************************************************************************// |
x1dmoesc | 0:833cb2c6da5d | 144 | bool SC18IS602::getInt(){ |
x1dmoesc | 0:833cb2c6da5d | 145 | //printf("Wait for Int...\n"); |
x1dmoesc | 0:833cb2c6da5d | 146 | //wait(0.1); |
x1dmoesc | 2:cb90c271c412 | 147 | if(iINT == 0){ |
x1dmoesc | 1:2a7edc2be6df | 148 | return INTERRUPT; |
x1dmoesc | 2:cb90c271c412 | 149 | } |
x1dmoesc | 2:cb90c271c412 | 150 | |
x1dmoesc | 2:cb90c271c412 | 151 | if(iINT == 1){ |
x1dmoesc | 2:cb90c271c412 | 152 | //printf("GPIO\n"); |
x1dmoesc | 2:cb90c271c412 | 153 | return pca->getGPIO1_B7(true); |
x1dmoesc | 1:2a7edc2be6df | 154 | } |
x1dmoesc | 2:cb90c271c412 | 155 | |
x1dmoesc | 2:cb90c271c412 | 156 | if(iINT == 2){ |
x1dmoesc | 2:cb90c271c412 | 157 | //printf("PIN\n"); |
x1dmoesc | 2:cb90c271c412 | 158 | return IntPin->read(); |
x1dmoesc | 2:cb90c271c412 | 159 | } |
x1dmoesc | 0:833cb2c6da5d | 160 | |
x1dmoesc | 0:833cb2c6da5d | 161 | return not INTERRUPT; |
x1dmoesc | 0:833cb2c6da5d | 162 | } |
x1dmoesc | 0:833cb2c6da5d | 163 | |
x1dmoesc | 0:833cb2c6da5d | 164 | |
x1dmoesc | 0:833cb2c6da5d | 165 | |
x1dmoesc | 0:833cb2c6da5d | 166 | /******************************************************************************/ |
x1dmoesc | 0:833cb2c6da5d | 167 | // clears the interrupt pin |
x1dmoesc | 0:833cb2c6da5d | 168 | // returns 0(ACK) on success otherwise 1 (NACK) |
x1dmoesc | 0:833cb2c6da5d | 169 | /******************************************************************************/ |
x1dmoesc | 0:833cb2c6da5d | 170 | bool SC18IS602::clearInt(){ |
x1dmoesc | 0:833cb2c6da5d | 171 | cCmd[0] = 0xF1; |
x1dmoesc | 0:833cb2c6da5d | 172 | return sendViaI2C(cCmd, 1, "clear interrupt"); |
x1dmoesc | 0:833cb2c6da5d | 173 | } |
x1dmoesc | 0:833cb2c6da5d | 174 | |
x1dmoesc | 0:833cb2c6da5d | 175 | |
x1dmoesc | 0:833cb2c6da5d | 176 | //******************************************************************************// |
x1dmoesc | 0:833cb2c6da5d | 177 | // toggle GPIO Pins // |
x1dmoesc | 0:833cb2c6da5d | 178 | //******************************************************************************// |
x1dmoesc | 0:833cb2c6da5d | 179 | bool SC18IS602::gpio_toggle(uint8_t uiPort){ // |
x1dmoesc | 0:833cb2c6da5d | 180 | |
x1dmoesc | 0:833cb2c6da5d | 181 | uiPort &= 0x0F; // clear reserved bits |
x1dmoesc | 0:833cb2c6da5d | 182 | |
x1dmoesc | 2:cb90c271c412 | 183 | cCmd[0] = ADR_GPIO.READ; // Read from GPIO port |
x1dmoesc | 0:833cb2c6da5d | 184 | sendViaI2C(cCmd, 1, "Read GPIO"); |
x1dmoesc | 0:833cb2c6da5d | 185 | bAck = (bool) readViaI2C(&cCmd[1], 1); |
x1dmoesc | 0:833cb2c6da5d | 186 | |
x1dmoesc | 0:833cb2c6da5d | 187 | if(bAck == NACK) return bAck; // if NACK, return |
x1dmoesc | 0:833cb2c6da5d | 188 | |
x1dmoesc | 2:cb90c271c412 | 189 | cCmd[0] = ADR_GPIO.WRITE; |
x1dmoesc | 0:833cb2c6da5d | 190 | cCmd[1] ^= uiPort; // toogle given pins (uiTogllePin) |
x1dmoesc | 0:833cb2c6da5d | 191 | return sendViaI2C(cCmd, 2, "GPIO tog"); |
x1dmoesc | 0:833cb2c6da5d | 192 | |
x1dmoesc | 0:833cb2c6da5d | 193 | } |
x1dmoesc | 0:833cb2c6da5d | 194 | |
x1dmoesc | 0:833cb2c6da5d | 195 | |
x1dmoesc | 0:833cb2c6da5d | 196 | /******************************************************************************/ |
x1dmoesc | 0:833cb2c6da5d | 197 | // swith the GPIO Pin for debugging on |
x1dmoesc | 0:833cb2c6da5d | 198 | /******************************************************************************/ |
x1dmoesc | 2:cb90c271c412 | 199 | bool SC18IS602::gpio_pin3_off() |
x1dmoesc | 0:833cb2c6da5d | 200 | { |
x1dmoesc | 2:cb90c271c412 | 201 | cCmd[0] = ADR_GPIO.WRITE; |
x1dmoesc | 2:cb90c271c412 | 202 | cCmd[1] = (1 << GPIO.CS3); |
x1dmoesc | 0:833cb2c6da5d | 203 | return sendViaI2C(cCmd, 2, "GPIO off"); |
x1dmoesc | 0:833cb2c6da5d | 204 | } |
x1dmoesc | 0:833cb2c6da5d | 205 | |
x1dmoesc | 0:833cb2c6da5d | 206 | |
x1dmoesc | 0:833cb2c6da5d | 207 | /******************************************************************************/ |
x1dmoesc | 0:833cb2c6da5d | 208 | // swith the GPIO Pin for debugging off |
x1dmoesc | 0:833cb2c6da5d | 209 | /******************************************************************************/ |
x1dmoesc | 2:cb90c271c412 | 210 | bool SC18IS602::gpio_pin3_on() |
x1dmoesc | 0:833cb2c6da5d | 211 | { |
x1dmoesc | 2:cb90c271c412 | 212 | cCmd[0] = ADR_GPIO.WRITE; |
x1dmoesc | 2:cb90c271c412 | 213 | cCmd[1] = (0 << GPIO.CS3); |
x1dmoesc | 0:833cb2c6da5d | 214 | return sendViaI2C(cCmd, 2, "GPIO on"); |
x1dmoesc | 0:833cb2c6da5d | 215 | } |
x1dmoesc | 0:833cb2c6da5d | 216 | |
x1dmoesc | 0:833cb2c6da5d | 217 | |
x1dmoesc | 0:833cb2c6da5d | 218 | |
x1dmoesc | 0:833cb2c6da5d | 219 | //******************************************************************************// |
x1dmoesc | 0:833cb2c6da5d | 220 | // |
x1dmoesc | 0:833cb2c6da5d | 221 | //******************************************************************************// |
x1dmoesc | 3:9cf83f16c17d | 222 | bool SC18IS602::sendViaSPI(uint8_t uiCS, char cAdrByte, char *cDataBytes, uint8_t uiNum) |
x1dmoesc | 0:833cb2c6da5d | 223 | { |
x1dmoesc | 0:833cb2c6da5d | 224 | if((int)uiNum >= BUFFER_SIZE - 1) // If number of sending Bytes greather than BUFFER_SIZE - 1 (Register-Adress-Byte) |
x1dmoesc | 0:833cb2c6da5d | 225 | return NACK; // returns NACK (failure) |
x1dmoesc | 3:9cf83f16c17d | 226 | |
x1dmoesc | 3:9cf83f16c17d | 227 | |
x1dmoesc | 3:9cf83f16c17d | 228 | if( uiCS != CMD_RW.CS0 and uiCS != CMD_RW.CS1 |
x1dmoesc | 3:9cf83f16c17d | 229 | and uiCS != CMD_RW.CS2 and uiCS != CMD_RW.CS3){ // If uiCS not element of CMD_RW, than return NACK (faiure) |
x1dmoesc | 3:9cf83f16c17d | 230 | return NACK; |
x1dmoesc | 3:9cf83f16c17d | 231 | } |
x1dmoesc | 0:833cb2c6da5d | 232 | |
x1dmoesc | 0:833cb2c6da5d | 233 | |
x1dmoesc | 0:833cb2c6da5d | 234 | uiNumByte = 0; |
x1dmoesc | 3:9cf83f16c17d | 235 | cCmd[uiNumByte] = uiCS; // send via SPI and CS0 (SS0) |
x1dmoesc | 0:833cb2c6da5d | 236 | cCmd[++uiNumByte] = cAdrByte; |
x1dmoesc | 0:833cb2c6da5d | 237 | for(int n = uiNum - 1 ; n >= 0; n--) { |
x1dmoesc | 0:833cb2c6da5d | 238 | cCmd[++uiNumByte] = cDataBytes[n]; |
x1dmoesc | 0:833cb2c6da5d | 239 | } |
x1dmoesc | 0:833cb2c6da5d | 240 | |
x1dmoesc | 0:833cb2c6da5d | 241 | |
x1dmoesc | 0:833cb2c6da5d | 242 | uiNumByte++; // char cCmd counts from 0, so the number is +1 |
x1dmoesc | 2:cb90c271c412 | 243 | bAck = sendViaI2C(cCmd, uiNumByte, "TX via SPI"); // send via SPI |
x1dmoesc | 3:9cf83f16c17d | 244 | waitFor(INTERRUPT); // If there was an interrupt, wait until it is cleared |
x1dmoesc | 0:833cb2c6da5d | 245 | clearInt(); |
x1dmoesc | 2:cb90c271c412 | 246 | waitFor(not INTERRUPT); // If there was an interrupt, wait until it is cleared |
x1dmoesc | 0:833cb2c6da5d | 247 | |
x1dmoesc | 0:833cb2c6da5d | 248 | return bAck; |
x1dmoesc | 0:833cb2c6da5d | 249 | } |
x1dmoesc | 0:833cb2c6da5d | 250 | |
x1dmoesc | 0:833cb2c6da5d | 251 | |
x1dmoesc | 0:833cb2c6da5d | 252 | |
x1dmoesc | 0:833cb2c6da5d | 253 | //******************************************************************************// |
x1dmoesc | 0:833cb2c6da5d | 254 | // returns reading data. The first byte is the status byte |
x1dmoesc | 0:833cb2c6da5d | 255 | //******************************************************************************// |
x1dmoesc | 3:9cf83f16c17d | 256 | bool SC18IS602::readViaSPI(uint8_t uiCS, char cAdrByte, char *cDataBytes, uint8_t uiNum) |
x1dmoesc | 2:cb90c271c412 | 257 | { |
x1dmoesc | 0:833cb2c6da5d | 258 | if((int)uiNum >= BUFFER_SIZE - 1) // If number of sending Bytes greather than BUFFER_SIZE - 1 (Register-Adress-Byte) |
x1dmoesc | 0:833cb2c6da5d | 259 | return NACK; // returns NACK (failure) |
x1dmoesc | 3:9cf83f16c17d | 260 | |
x1dmoesc | 3:9cf83f16c17d | 261 | |
x1dmoesc | 3:9cf83f16c17d | 262 | if( uiCS != CMD_RW.CS0 and uiCS != CMD_RW.CS1 |
x1dmoesc | 3:9cf83f16c17d | 263 | and uiCS != CMD_RW.CS2 and uiCS != CMD_RW.CS3){ // If uiCS not element of CMD_RW, than return NACK (faiure) |
x1dmoesc | 3:9cf83f16c17d | 264 | return NACK; |
x1dmoesc | 3:9cf83f16c17d | 265 | } |
x1dmoesc | 2:cb90c271c412 | 266 | |
x1dmoesc | 0:833cb2c6da5d | 267 | |
x1dmoesc | 0:833cb2c6da5d | 268 | // send first time Read comand |
x1dmoesc | 0:833cb2c6da5d | 269 | uiNumByte = 0; |
x1dmoesc | 3:9cf83f16c17d | 270 | cCmd[uiNumByte++] = uiCS; // send via SPI and CS0 (SS0) |
x1dmoesc | 2:cb90c271c412 | 271 | cCmd[uiNumByte++] = cAdrByte; |
x1dmoesc | 2:cb90c271c412 | 272 | |
x1dmoesc | 2:cb90c271c412 | 273 | for(int n = uiNum - 1 ; n >= 0; n--) { |
x1dmoesc | 0:833cb2c6da5d | 274 | cCmd[uiNumByte++] = 0x00; |
x1dmoesc | 0:833cb2c6da5d | 275 | } |
x1dmoesc | 2:cb90c271c412 | 276 | |
x1dmoesc | 0:833cb2c6da5d | 277 | |
x1dmoesc | 2:cb90c271c412 | 278 | if(getInt() == INTERRUPT){ // If interrupt isn't cleard, then |
x1dmoesc | 2:cb90c271c412 | 279 | //printf("Interrupt..."); |
x1dmoesc | 2:cb90c271c412 | 280 | clearInt(); // clear interrupt |
x1dmoesc | 2:cb90c271c412 | 281 | waitFor(not INTERRUPT); // wait until interrupt is cleard |
x1dmoesc | 2:cb90c271c412 | 282 | } |
x1dmoesc | 2:cb90c271c412 | 283 | |
x1dmoesc | 2:cb90c271c412 | 284 | |
x1dmoesc | 2:cb90c271c412 | 285 | |
x1dmoesc | 2:cb90c271c412 | 286 | bAck = sendViaI2C(cCmd, uiNumByte, "Send Rx Cmd via SPI"); |
x1dmoesc | 2:cb90c271c412 | 287 | waitFor(INTERRUPT); |
x1dmoesc | 0:833cb2c6da5d | 288 | clearInt(); |
x1dmoesc | 2:cb90c271c412 | 289 | |
x1dmoesc | 2:cb90c271c412 | 290 | |
x1dmoesc | 2:cb90c271c412 | 291 | // send secound time read comand (dummy) to receive data from first read command |
x1dmoesc | 2:cb90c271c412 | 292 | cCmd[0] = CMD_RW.CS0; // send via SPI and CS0 (SS0) |
x1dmoesc | 2:cb90c271c412 | 293 | //cCmd[++uiNumByte] = cAdrByte; |
x1dmoesc | 2:cb90c271c412 | 294 | |
x1dmoesc | 2:cb90c271c412 | 295 | //for(int n = uiNum - 1 ; n >= 0; n--) { |
x1dmoesc | 2:cb90c271c412 | 296 | // cCmd[++uiNumByte] = 0x00; |
x1dmoesc | 2:cb90c271c412 | 297 | //} |
x1dmoesc | 2:cb90c271c412 | 298 | //uiNumByte++; |
x1dmoesc | 2:cb90c271c412 | 299 | |
x1dmoesc | 0:833cb2c6da5d | 300 | |
x1dmoesc | 2:cb90c271c412 | 301 | waitFor(not INTERRUPT); // If there was an interrupt, wait until it is cleared |
x1dmoesc | 2:cb90c271c412 | 302 | bAck = sendViaI2C(cCmd, uiNumByte, "Send dummy data via SPI"); |
x1dmoesc | 2:cb90c271c412 | 303 | waitFor(INTERRUPT); // wait until sending is finished and an interrupt occurs |
x1dmoesc | 2:cb90c271c412 | 304 | clearInt(); |
x1dmoesc | 2:cb90c271c412 | 305 | uiNumByte--; |
x1dmoesc | 2:cb90c271c412 | 306 | |
x1dmoesc | 2:cb90c271c412 | 307 | bAck = (bool) readViaI2C(cDataBytes, uiNumByte, "RX via SPI"); |
x1dmoesc | 2:cb90c271c412 | 308 | |
x1dmoesc | 2:cb90c271c412 | 309 | /*printf("Rx: "); |
x1dmoesc | 2:cb90c271c412 | 310 | for(int i = 0; i < uiNumByte; i++){ |
x1dmoesc | 2:cb90c271c412 | 311 | printf("0x%02x ", cDataBytes[i]); |
x1dmoesc | 2:cb90c271c412 | 312 | } |
x1dmoesc | 2:cb90c271c412 | 313 | printf("\n");*/ |
x1dmoesc | 2:cb90c271c412 | 314 | |
x1dmoesc | 0:833cb2c6da5d | 315 | |
x1dmoesc | 0:833cb2c6da5d | 316 | int n = 0; |
x1dmoesc | 0:833cb2c6da5d | 317 | for(int i = 0; i < int(uiNumByte/2); i++){ |
x1dmoesc | 0:833cb2c6da5d | 318 | n = uiNumByte - 1 - i; |
x1dmoesc | 0:833cb2c6da5d | 319 | //printf("%d <-> %d\n", i, n); |
x1dmoesc | 0:833cb2c6da5d | 320 | cDataBytes[i] = cDataBytes[i] ^ cDataBytes[n]; |
x1dmoesc | 0:833cb2c6da5d | 321 | cDataBytes[n] = cDataBytes[i] ^ cDataBytes[n]; |
x1dmoesc | 0:833cb2c6da5d | 322 | cDataBytes[i] = cDataBytes[i] ^ cDataBytes[n]; |
x1dmoesc | 0:833cb2c6da5d | 323 | } |
x1dmoesc | 0:833cb2c6da5d | 324 | |
x1dmoesc | 0:833cb2c6da5d | 325 | return bAck; |
x1dmoesc | 0:833cb2c6da5d | 326 | } |
x1dmoesc | 0:833cb2c6da5d | 327 | |
x1dmoesc | 0:833cb2c6da5d | 328 | |
x1dmoesc | 1:2a7edc2be6df | 329 | |
x1dmoesc | 2:cb90c271c412 | 330 | //******************************************************************************// |
x1dmoesc | 2:cb90c271c412 | 331 | // |
x1dmoesc | 2:cb90c271c412 | 332 | //******************************************************************************// |
x1dmoesc | 2:cb90c271c412 | 333 | void SC18IS602::waitFor(bool bInt){ |
x1dmoesc | 2:cb90c271c412 | 334 | iTimeOut = 1000; |
x1dmoesc | 2:cb90c271c412 | 335 | |
x1dmoesc | 2:cb90c271c412 | 336 | while((getInt() != bInt) and iTimeOut > 0) { |
x1dmoesc | 2:cb90c271c412 | 337 | iTimeOut--; |
x1dmoesc | 2:cb90c271c412 | 338 | wait(1e-3); |
x1dmoesc | 2:cb90c271c412 | 339 | } |
x1dmoesc | 2:cb90c271c412 | 340 | |
x1dmoesc | 2:cb90c271c412 | 341 | if(iTimeOut == 0) printf("TimeOut: Interrupt\n"); |
x1dmoesc | 2:cb90c271c412 | 342 | } |
x1dmoesc | 2:cb90c271c412 | 343 | |
x1dmoesc | 1:2a7edc2be6df | 344 | |
x1dmoesc | 1:2a7edc2be6df | 345 | //******************************************************************************// |
x1dmoesc | 1:2a7edc2be6df | 346 | // musst modified |
x1dmoesc | 1:2a7edc2be6df | 347 | //******************************************************************************// |
x1dmoesc | 1:2a7edc2be6df | 348 | /*void SC18IS602::setIntFuncPtr( bool (*Int_ptr)(void)){ |
x1dmoesc | 1:2a7edc2be6df | 349 | getInt_ptr = Int_ptr; |
x1dmoesc | 1:2a7edc2be6df | 350 | |
x1dmoesc | 1:2a7edc2be6df | 351 | }*/ |