diff options
Diffstat (limited to 'keyboards/lfkeyboards/TWIlib.c')
| -rw-r--r-- | keyboards/lfkeyboards/TWIlib.c | 283 |
1 files changed, 283 insertions, 0 deletions
diff --git a/keyboards/lfkeyboards/TWIlib.c b/keyboards/lfkeyboards/TWIlib.c new file mode 100644 index 000000000..abb12cc87 --- /dev/null +++ b/keyboards/lfkeyboards/TWIlib.c | |||
| @@ -0,0 +1,283 @@ | |||
| 1 | /* | ||
| 2 | * TWIlib.c | ||
| 3 | * | ||
| 4 | * Created: 6/01/2014 10:41:33 PM | ||
| 5 | * Author: Chris Herring | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include <avr/io.h> | ||
| 9 | #include <avr/interrupt.h> | ||
| 10 | #include "TWIlib.h" | ||
| 11 | #include "util/delay.h" | ||
| 12 | #include "print.h" | ||
| 13 | |||
| 14 | void TWIInit() | ||
| 15 | { | ||
| 16 | TWIInfo.mode = Ready; | ||
| 17 | TWIInfo.errorCode = 0xFF; | ||
| 18 | TWIInfo.repStart = 0; | ||
| 19 | // Set pre-scalers (no pre-scaling) | ||
| 20 | TWSR = 0; | ||
| 21 | // Set bit rate | ||
| 22 | TWBR = ((F_CPU / TWI_FREQ) - 16) / 2; | ||
| 23 | // Enable TWI and interrupt | ||
| 24 | TWCR = (1 << TWIE) | (1 << TWEN); | ||
| 25 | } | ||
| 26 | |||
| 27 | uint8_t isTWIReady() | ||
| 28 | { | ||
| 29 | if ( (TWIInfo.mode == Ready) | (TWIInfo.mode == RepeatedStartSent) ) | ||
| 30 | { | ||
| 31 | return 1; | ||
| 32 | } | ||
| 33 | else | ||
| 34 | { | ||
| 35 | if(TWIInfo.mode == Initializing){ | ||
| 36 | switch(TWIInfo.errorCode){ | ||
| 37 | case TWI_SUCCESS: | ||
| 38 | case TWI_NO_RELEVANT_INFO: | ||
| 39 | break; | ||
| 40 | case TWI_LOST_ARBIT: | ||
| 41 | case TWI_MT_DATA_NACK: | ||
| 42 | // Some kind of I2C error, reset and re-init | ||
| 43 | xprintf("I2C init error: %d\n", TWIInfo.errorCode); | ||
| 44 | TWCR = (1 << TWINT)|(1 << TWSTO); | ||
| 45 | TWIInit(); | ||
| 46 | break; | ||
| 47 | default: | ||
| 48 | xprintf("Other i2c init error: %d\n", TWIInfo.errorCode); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | return 0; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | void TWITransmitData(void *const TXdata, uint8_t dataLen, uint8_t repStart, uint8_t blocking) | ||
| 57 | { | ||
| 58 | // Wait until ready | ||
| 59 | while (!isTWIReady()) {_delay_us(1);} | ||
| 60 | // Reset the I2C stuff | ||
| 61 | TWCR = (1 << TWINT)|(1 << TWSTO); | ||
| 62 | TWIInit(); | ||
| 63 | // Set repeated start mode | ||
| 64 | TWIInfo.repStart = repStart; | ||
| 65 | // Copy transmit info to global variables | ||
| 66 | TWITransmitBuffer = (uint8_t *)TXdata; | ||
| 67 | TXBuffLen = dataLen; | ||
| 68 | TXBuffIndex = 0; | ||
| 69 | |||
| 70 | // If a repeated start has been sent, then devices are already listening for an address | ||
| 71 | // and another start does not need to be sent. | ||
| 72 | if (TWIInfo.mode == RepeatedStartSent) | ||
| 73 | { | ||
| 74 | TWIInfo.mode = Initializing; | ||
| 75 | TWDR = TWITransmitBuffer[TXBuffIndex++]; // Load data to transmit buffer | ||
| 76 | TWISendTransmit(); // Send the data | ||
| 77 | } | ||
| 78 | else // Otherwise, just send the normal start signal to begin transmission. | ||
| 79 | { | ||
| 80 | TWIInfo.mode = Initializing; | ||
| 81 | TWISendStart(); | ||
| 82 | } | ||
| 83 | if(blocking){ | ||
| 84 | // Wait until ready | ||
| 85 | while (!isTWIReady()){_delay_us(1);} | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | // uint8_t TWITransmitData(void *const TXdata, uint8_t dataLen, uint8_t repStart) | ||
| 91 | // { | ||
| 92 | // if (dataLen <= TXMAXBUFLEN) | ||
| 93 | // { | ||
| 94 | // // Wait until ready | ||
| 95 | // while (!isTWIReady()) {_delay_us(1);} | ||
| 96 | // // Set repeated start mode | ||
| 97 | // TWIInfo.repStart = repStart; | ||
| 98 | // // Copy data into the transmit buffer | ||
| 99 | // uint8_t *data = (uint8_t *)TXdata; | ||
| 100 | // for (int i = 0; i < dataLen; i++) | ||
| 101 | // { | ||
| 102 | // TWITransmitBuffer[i] = data[i]; | ||
| 103 | // } | ||
| 104 | // // Copy transmit info to global variables | ||
| 105 | // TXBuffLen = dataLen; | ||
| 106 | // TXBuffIndex = 0; | ||
| 107 | |||
| 108 | // // If a repeated start has been sent, then devices are already listening for an address | ||
| 109 | // // and another start does not need to be sent. | ||
| 110 | // if (TWIInfo.mode == RepeatedStartSent) | ||
| 111 | // { | ||
| 112 | // TWIInfo.mode = Initializing; | ||
| 113 | // TWDR = TWITransmitBuffer[TXBuffIndex++]; // Load data to transmit buffer | ||
| 114 | // TWISendTransmit(); // Send the data | ||
| 115 | // } | ||
| 116 | // else // Otherwise, just send the normal start signal to begin transmission. | ||
| 117 | // { | ||
| 118 | // TWIInfo.mode = Initializing; | ||
| 119 | // TWISendStart(); | ||
| 120 | // } | ||
| 121 | |||
| 122 | // } | ||
| 123 | // else | ||
| 124 | // { | ||
| 125 | // return 1; // return an error if data length is longer than buffer | ||
| 126 | // } | ||
| 127 | // return 0; | ||
| 128 | // } | ||
| 129 | |||
| 130 | uint8_t TWIReadData(uint8_t TWIaddr, uint8_t bytesToRead, uint8_t repStart) | ||
| 131 | { | ||
| 132 | // Check if number of bytes to read can fit in the RXbuffer | ||
| 133 | if (bytesToRead < RXMAXBUFLEN) | ||
| 134 | { | ||
| 135 | // Reset buffer index and set RXBuffLen to the number of bytes to read | ||
| 136 | RXBuffIndex = 0; | ||
| 137 | RXBuffLen = bytesToRead; | ||
| 138 | // Create the one value array for the address to be transmitted | ||
| 139 | uint8_t TXdata[1]; | ||
| 140 | // Shift the address and AND a 1 into the read write bit (set to write mode) | ||
| 141 | TXdata[0] = (TWIaddr << 1) | 0x01; | ||
| 142 | // Use the TWITransmitData function to initialize the transfer and address the slave | ||
| 143 | TWITransmitData(TXdata, 1, repStart, 0); | ||
| 144 | } | ||
| 145 | else | ||
| 146 | { | ||
| 147 | return 0; | ||
| 148 | } | ||
| 149 | return 1; | ||
| 150 | } | ||
| 151 | |||
| 152 | ISR (TWI_vect) | ||
| 153 | { | ||
| 154 | switch (TWI_STATUS) | ||
| 155 | { | ||
| 156 | // ----\/ ---- MASTER TRANSMITTER OR WRITING ADDRESS ----\/ ---- // | ||
| 157 | case TWI_MT_SLAW_ACK: // SLA+W transmitted and ACK received | ||
| 158 | // Set mode to Master Transmitter | ||
| 159 | TWIInfo.mode = MasterTransmitter; | ||
| 160 | case TWI_START_SENT: // Start condition has been transmitted | ||
| 161 | case TWI_MT_DATA_ACK: // Data byte has been transmitted, ACK received | ||
| 162 | if (TXBuffIndex < TXBuffLen) // If there is more data to send | ||
| 163 | { | ||
| 164 | TWDR = TWITransmitBuffer[TXBuffIndex++]; // Load data to transmit buffer | ||
| 165 | TWIInfo.errorCode = TWI_NO_RELEVANT_INFO; | ||
| 166 | TWISendTransmit(); // Send the data | ||
| 167 | } | ||
| 168 | // This transmission is complete however do not release bus yet | ||
| 169 | else if (TWIInfo.repStart) | ||
| 170 | { | ||
| 171 | TWIInfo.errorCode = 0xFF; | ||
| 172 | TWISendStart(); | ||
| 173 | } | ||
| 174 | // All transmissions are complete, exit | ||
| 175 | else | ||
| 176 | { | ||
| 177 | TWIInfo.mode = Ready; | ||
| 178 | TWIInfo.errorCode = 0xFF; | ||
| 179 | TWISendStop(); | ||
| 180 | } | ||
| 181 | break; | ||
| 182 | |||
| 183 | // ----\/ ---- MASTER RECEIVER ----\/ ---- // | ||
| 184 | |||
| 185 | case TWI_MR_SLAR_ACK: // SLA+R has been transmitted, ACK has been received | ||
| 186 | // Switch to Master Receiver mode | ||
| 187 | TWIInfo.mode = MasterReceiver; | ||
| 188 | // If there is more than one byte to be read, receive data byte and return an ACK | ||
| 189 | if (RXBuffIndex < RXBuffLen-1) | ||
| 190 | { | ||
| 191 | TWIInfo.errorCode = TWI_NO_RELEVANT_INFO; | ||
| 192 | TWISendACK(); | ||
| 193 | } | ||
| 194 | // Otherwise when a data byte (the only data byte) is received, return NACK | ||
| 195 | else | ||
| 196 | { | ||
| 197 | TWIInfo.errorCode = TWI_NO_RELEVANT_INFO; | ||
| 198 | TWISendNACK(); | ||
| 199 | } | ||
| 200 | break; | ||
| 201 | |||
| 202 | case TWI_MR_DATA_ACK: // Data has been received, ACK has been transmitted. | ||
| 203 | |||
| 204 | /// -- HANDLE DATA BYTE --- /// | ||
| 205 | TWIReceiveBuffer[RXBuffIndex++] = TWDR; | ||
| 206 | // If there is more than one byte to be read, receive data byte and return an ACK | ||
| 207 | if (RXBuffIndex < RXBuffLen-1) | ||
| 208 | { | ||
| 209 | TWIInfo.errorCode = TWI_NO_RELEVANT_INFO; | ||
| 210 | TWISendACK(); | ||
| 211 | } | ||
| 212 | // Otherwise when a data byte (the only data byte) is received, return NACK | ||
| 213 | else | ||
| 214 | { | ||
| 215 | TWIInfo.errorCode = TWI_NO_RELEVANT_INFO; | ||
| 216 | TWISendNACK(); | ||
| 217 | } | ||
| 218 | break; | ||
| 219 | |||
| 220 | case TWI_MR_DATA_NACK: // Data byte has been received, NACK has been transmitted. End of transmission. | ||
| 221 | |||
| 222 | /// -- HANDLE DATA BYTE --- /// | ||
| 223 | TWIReceiveBuffer[RXBuffIndex++] = TWDR; | ||
| 224 | // This transmission is complete however do not release bus yet | ||
| 225 | if (TWIInfo.repStart) | ||
| 226 | { | ||
| 227 | TWIInfo.errorCode = 0xFF; | ||
| 228 | TWISendStart(); | ||
| 229 | } | ||
| 230 | // All transmissions are complete, exit | ||
| 231 | else | ||
| 232 | { | ||
| 233 | TWIInfo.mode = Ready; | ||
| 234 | TWIInfo.errorCode = 0xFF; | ||
| 235 | TWISendStop(); | ||
| 236 | } | ||
| 237 | break; | ||
| 238 | |||
| 239 | // ----\/ ---- MT and MR common ----\/ ---- // | ||
| 240 | |||
| 241 | case TWI_MR_SLAR_NACK: // SLA+R transmitted, NACK received | ||
| 242 | case TWI_MT_SLAW_NACK: // SLA+W transmitted, NACK received | ||
| 243 | case TWI_MT_DATA_NACK: // Data byte has been transmitted, NACK received | ||
| 244 | case TWI_LOST_ARBIT: // Arbitration has been lost | ||
| 245 | // Return error and send stop and set mode to ready | ||
| 246 | if (TWIInfo.repStart) | ||
| 247 | { | ||
| 248 | TWIInfo.errorCode = TWI_STATUS; | ||
| 249 | TWISendStart(); | ||
| 250 | } | ||
| 251 | // All transmissions are complete, exit | ||
| 252 | else | ||
| 253 | { | ||
| 254 | TWIInfo.mode = Ready; | ||
| 255 | TWIInfo.errorCode = TWI_STATUS; | ||
| 256 | TWISendStop(); | ||
| 257 | } | ||
| 258 | break; | ||
| 259 | case TWI_REP_START_SENT: // Repeated start has been transmitted | ||
| 260 | // Set the mode but DO NOT clear TWINT as the next data is not yet ready | ||
| 261 | TWIInfo.mode = RepeatedStartSent; | ||
| 262 | break; | ||
| 263 | |||
| 264 | // ----\/ ---- SLAVE RECEIVER ----\/ ---- // | ||
| 265 | |||
| 266 | // TODO IMPLEMENT SLAVE RECEIVER FUNCTIONALITY | ||
| 267 | |||
| 268 | // ----\/ ---- SLAVE TRANSMITTER ----\/ ---- // | ||
| 269 | |||
| 270 | // TODO IMPLEMENT SLAVE TRANSMITTER FUNCTIONALITY | ||
| 271 | |||
| 272 | // ----\/ ---- MISCELLANEOUS STATES ----\/ ---- // | ||
| 273 | case TWI_NO_RELEVANT_INFO: // It is not really possible to get into this ISR on this condition | ||
| 274 | // Rather, it is there to be manually set between operations | ||
| 275 | break; | ||
| 276 | case TWI_ILLEGAL_START_STOP: // Illegal START/STOP, abort and return error | ||
| 277 | TWIInfo.errorCode = TWI_ILLEGAL_START_STOP; | ||
| 278 | TWIInfo.mode = Ready; | ||
| 279 | TWISendStop(); | ||
| 280 | break; | ||
| 281 | } | ||
| 282 | |||
| 283 | } | ||
