SPI-Brigde (capseld), for polling interrupt, it is neccessary to adapt the constructor and the function getInt()
SC18IS602.cpp@2:cb90c271c412, 2019-02-18 (annotated)
- Committer:
- x1dmoesc
- Date:
- Mon Feb 18 16:03:44 2019 +0000
- Revision:
- 2:cb90c271c412
- Parent:
- 1:2a7edc2be6df
- Child:
- 3:9cf83f16c17d
change class construct
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 | 0:833cb2c6da5d | 222 | bool SC18IS602::sendViaSPI(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 | 0:833cb2c6da5d | 226 | |
x1dmoesc | 0:833cb2c6da5d | 227 | |
x1dmoesc | 0:833cb2c6da5d | 228 | uiNumByte = 0; |
x1dmoesc | 2:cb90c271c412 | 229 | cCmd[uiNumByte] = CMD_RW.CS0; // send via SPI and CS0 (SS0) |
x1dmoesc | 0:833cb2c6da5d | 230 | cCmd[++uiNumByte] = cAdrByte; |
x1dmoesc | 0:833cb2c6da5d | 231 | for(int n = uiNum - 1 ; n >= 0; n--) { |
x1dmoesc | 0:833cb2c6da5d | 232 | cCmd[++uiNumByte] = cDataBytes[n]; |
x1dmoesc | 0:833cb2c6da5d | 233 | } |
x1dmoesc | 0:833cb2c6da5d | 234 | |
x1dmoesc | 0:833cb2c6da5d | 235 | |
x1dmoesc | 0:833cb2c6da5d | 236 | uiNumByte++; // char cCmd counts from 0, so the number is +1 |
x1dmoesc | 2:cb90c271c412 | 237 | bAck = sendViaI2C(cCmd, uiNumByte, "TX via SPI"); // send via SPI |
x1dmoesc | 2:cb90c271c412 | 238 | waitFor(INTERRUPT); // If there was an interrupt, wait until it is cleared |
x1dmoesc | 0:833cb2c6da5d | 239 | clearInt(); |
x1dmoesc | 2:cb90c271c412 | 240 | waitFor(not INTERRUPT); // If there was an interrupt, wait until it is cleared |
x1dmoesc | 0:833cb2c6da5d | 241 | |
x1dmoesc | 0:833cb2c6da5d | 242 | return bAck; |
x1dmoesc | 0:833cb2c6da5d | 243 | } |
x1dmoesc | 0:833cb2c6da5d | 244 | |
x1dmoesc | 0:833cb2c6da5d | 245 | |
x1dmoesc | 0:833cb2c6da5d | 246 | |
x1dmoesc | 0:833cb2c6da5d | 247 | //******************************************************************************// |
x1dmoesc | 0:833cb2c6da5d | 248 | // returns reading data. The first byte is the status byte |
x1dmoesc | 0:833cb2c6da5d | 249 | //******************************************************************************// |
x1dmoesc | 0:833cb2c6da5d | 250 | bool SC18IS602::readViaSPI(char cAdrByte, char *cDataBytes, uint8_t uiNum) |
x1dmoesc | 2:cb90c271c412 | 251 | { |
x1dmoesc | 0:833cb2c6da5d | 252 | if((int)uiNum >= BUFFER_SIZE - 1) // If number of sending Bytes greather than BUFFER_SIZE - 1 (Register-Adress-Byte) |
x1dmoesc | 0:833cb2c6da5d | 253 | return NACK; // returns NACK (failure) |
x1dmoesc | 2:cb90c271c412 | 254 | |
x1dmoesc | 0:833cb2c6da5d | 255 | |
x1dmoesc | 0:833cb2c6da5d | 256 | // send first time Read comand |
x1dmoesc | 0:833cb2c6da5d | 257 | uiNumByte = 0; |
x1dmoesc | 2:cb90c271c412 | 258 | cCmd[uiNumByte++] = CMD_RW.CS0; // send via SPI and CS0 (SS0) |
x1dmoesc | 2:cb90c271c412 | 259 | cCmd[uiNumByte++] = cAdrByte; |
x1dmoesc | 2:cb90c271c412 | 260 | |
x1dmoesc | 2:cb90c271c412 | 261 | for(int n = uiNum - 1 ; n >= 0; n--) { |
x1dmoesc | 0:833cb2c6da5d | 262 | cCmd[uiNumByte++] = 0x00; |
x1dmoesc | 0:833cb2c6da5d | 263 | } |
x1dmoesc | 2:cb90c271c412 | 264 | |
x1dmoesc | 0:833cb2c6da5d | 265 | |
x1dmoesc | 2:cb90c271c412 | 266 | if(getInt() == INTERRUPT){ // If interrupt isn't cleard, then |
x1dmoesc | 2:cb90c271c412 | 267 | //printf("Interrupt..."); |
x1dmoesc | 2:cb90c271c412 | 268 | clearInt(); // clear interrupt |
x1dmoesc | 2:cb90c271c412 | 269 | waitFor(not INTERRUPT); // wait until interrupt is cleard |
x1dmoesc | 2:cb90c271c412 | 270 | } |
x1dmoesc | 2:cb90c271c412 | 271 | |
x1dmoesc | 2:cb90c271c412 | 272 | |
x1dmoesc | 2:cb90c271c412 | 273 | |
x1dmoesc | 2:cb90c271c412 | 274 | bAck = sendViaI2C(cCmd, uiNumByte, "Send Rx Cmd via SPI"); |
x1dmoesc | 2:cb90c271c412 | 275 | waitFor(INTERRUPT); |
x1dmoesc | 0:833cb2c6da5d | 276 | clearInt(); |
x1dmoesc | 2:cb90c271c412 | 277 | |
x1dmoesc | 2:cb90c271c412 | 278 | |
x1dmoesc | 2:cb90c271c412 | 279 | // send secound time read comand (dummy) to receive data from first read command |
x1dmoesc | 2:cb90c271c412 | 280 | cCmd[0] = CMD_RW.CS0; // send via SPI and CS0 (SS0) |
x1dmoesc | 2:cb90c271c412 | 281 | //cCmd[++uiNumByte] = cAdrByte; |
x1dmoesc | 2:cb90c271c412 | 282 | |
x1dmoesc | 2:cb90c271c412 | 283 | //for(int n = uiNum - 1 ; n >= 0; n--) { |
x1dmoesc | 2:cb90c271c412 | 284 | // cCmd[++uiNumByte] = 0x00; |
x1dmoesc | 2:cb90c271c412 | 285 | //} |
x1dmoesc | 2:cb90c271c412 | 286 | //uiNumByte++; |
x1dmoesc | 2:cb90c271c412 | 287 | |
x1dmoesc | 0:833cb2c6da5d | 288 | |
x1dmoesc | 2:cb90c271c412 | 289 | waitFor(not INTERRUPT); // If there was an interrupt, wait until it is cleared |
x1dmoesc | 2:cb90c271c412 | 290 | bAck = sendViaI2C(cCmd, uiNumByte, "Send dummy data via SPI"); |
x1dmoesc | 2:cb90c271c412 | 291 | waitFor(INTERRUPT); // wait until sending is finished and an interrupt occurs |
x1dmoesc | 2:cb90c271c412 | 292 | clearInt(); |
x1dmoesc | 2:cb90c271c412 | 293 | uiNumByte--; |
x1dmoesc | 2:cb90c271c412 | 294 | |
x1dmoesc | 2:cb90c271c412 | 295 | bAck = (bool) readViaI2C(cDataBytes, uiNumByte, "RX via SPI"); |
x1dmoesc | 2:cb90c271c412 | 296 | |
x1dmoesc | 2:cb90c271c412 | 297 | /*printf("Rx: "); |
x1dmoesc | 2:cb90c271c412 | 298 | for(int i = 0; i < uiNumByte; i++){ |
x1dmoesc | 2:cb90c271c412 | 299 | printf("0x%02x ", cDataBytes[i]); |
x1dmoesc | 2:cb90c271c412 | 300 | } |
x1dmoesc | 2:cb90c271c412 | 301 | printf("\n");*/ |
x1dmoesc | 2:cb90c271c412 | 302 | |
x1dmoesc | 0:833cb2c6da5d | 303 | |
x1dmoesc | 0:833cb2c6da5d | 304 | int n = 0; |
x1dmoesc | 0:833cb2c6da5d | 305 | for(int i = 0; i < int(uiNumByte/2); i++){ |
x1dmoesc | 0:833cb2c6da5d | 306 | n = uiNumByte - 1 - i; |
x1dmoesc | 0:833cb2c6da5d | 307 | //printf("%d <-> %d\n", i, n); |
x1dmoesc | 0:833cb2c6da5d | 308 | cDataBytes[i] = cDataBytes[i] ^ cDataBytes[n]; |
x1dmoesc | 0:833cb2c6da5d | 309 | cDataBytes[n] = cDataBytes[i] ^ cDataBytes[n]; |
x1dmoesc | 0:833cb2c6da5d | 310 | cDataBytes[i] = cDataBytes[i] ^ cDataBytes[n]; |
x1dmoesc | 0:833cb2c6da5d | 311 | } |
x1dmoesc | 0:833cb2c6da5d | 312 | |
x1dmoesc | 0:833cb2c6da5d | 313 | return bAck; |
x1dmoesc | 0:833cb2c6da5d | 314 | } |
x1dmoesc | 0:833cb2c6da5d | 315 | |
x1dmoesc | 0:833cb2c6da5d | 316 | |
x1dmoesc | 1:2a7edc2be6df | 317 | |
x1dmoesc | 2:cb90c271c412 | 318 | //******************************************************************************// |
x1dmoesc | 2:cb90c271c412 | 319 | // |
x1dmoesc | 2:cb90c271c412 | 320 | //******************************************************************************// |
x1dmoesc | 2:cb90c271c412 | 321 | void SC18IS602::waitFor(bool bInt){ |
x1dmoesc | 2:cb90c271c412 | 322 | iTimeOut = 1000; |
x1dmoesc | 2:cb90c271c412 | 323 | |
x1dmoesc | 2:cb90c271c412 | 324 | while((getInt() != bInt) and iTimeOut > 0) { |
x1dmoesc | 2:cb90c271c412 | 325 | iTimeOut--; |
x1dmoesc | 2:cb90c271c412 | 326 | wait(1e-3); |
x1dmoesc | 2:cb90c271c412 | 327 | } |
x1dmoesc | 2:cb90c271c412 | 328 | |
x1dmoesc | 2:cb90c271c412 | 329 | if(iTimeOut == 0) printf("TimeOut: Interrupt\n"); |
x1dmoesc | 2:cb90c271c412 | 330 | } |
x1dmoesc | 2:cb90c271c412 | 331 | |
x1dmoesc | 1:2a7edc2be6df | 332 | |
x1dmoesc | 1:2a7edc2be6df | 333 | //******************************************************************************// |
x1dmoesc | 1:2a7edc2be6df | 334 | // musst modified |
x1dmoesc | 1:2a7edc2be6df | 335 | //******************************************************************************// |
x1dmoesc | 1:2a7edc2be6df | 336 | /*void SC18IS602::setIntFuncPtr( bool (*Int_ptr)(void)){ |
x1dmoesc | 1:2a7edc2be6df | 337 | getInt_ptr = Int_ptr; |
x1dmoesc | 1:2a7edc2be6df | 338 | |
x1dmoesc | 1:2a7edc2be6df | 339 | }*/ |