Rev 24 | Rev 26 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 24 | Rev 25 | ||
---|---|---|---|
Line 138... | Line 138... | ||
138 | #define TWCR_CLEARBUS TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|(0<<TWWC) |
138 | #define TWCR_CLEARBUS TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|(0<<TWWC) |
Line 139... | Line 139... | ||
139 | 139 | ||
140 | ISR (TWI_vect) |
140 | ISR (TWI_vect) |
141 | { |
141 | { |
142 | uint8_t data; |
- | |
- | 142 | uint8_t data; |
|
143 | 143 | static uint8_t crc; |
|
144 | // check event |
144 | // check event |
145 | switch (TW_STATUS) |
145 | switch (TW_STATUS) |
146 | { |
146 | { |
147 | case TW_SR_SLA_ACK: // slave addressed in receiver mode and ack has been returned |
147 | case TW_SR_SLA_ACK: // slave addressed in receiver mode and ack has been returned |
Line 197... | Line 197... | ||
197 | I2C_TxBuffer = 0; |
197 | I2C_TxBuffer = 0; |
198 | I2C_TxBufferSize = 0; |
198 | I2C_TxBufferSize = 0; |
199 | break; |
199 | break; |
200 | } |
200 | } |
201 | Rx_Idx = 0; // set rx buffer index to start of the buffer |
201 | Rx_Idx = 0; // set rx buffer index to start of the buffer |
- | 202 | crc = data; |
|
202 | } |
203 | } |
203 | else // Rx_Idx != 0xFF |
204 | else // Rx_Idx != 0xFF |
204 | { |
205 | { |
205 | // fill receiver buffer with the byte that has been received |
206 | // fill receiver buffer with the byte that has been received |
206 | // if buffer exist and there is still some free space |
207 | // if buffer exist and there is still some free space |
207 | if((I2C_RxBuffer != 0) && (Rx_Idx < I2C_RxBufferSize)) |
208 | if((I2C_RxBuffer != 0) && (Rx_Idx < I2C_RxBufferSize)) |
208 | { |
209 | { |
209 | I2C_RxBuffer[Rx_Idx++] = data; |
210 | I2C_RxBuffer[Rx_Idx] = data; |
- | 211 | crc += data; |
|
- | 212 | } |
|
- | 213 | else if (Rx_Idx == I2C_RxBufferSize) // crc byte was transferred |
|
- | 214 | { |
|
- | 215 | if(crc == data) |
|
- | 216 | { |
|
- | 217 | // copy primary rx buffer to target |
|
- | 218 | } |
|
210 | } |
219 | } |
211 | // else ignore the data |
220 | // else ignore data |
- | 221 | Rx_Idx++; |
|
212 | } |
222 | } |
213 | TWCR_ACK; |
223 | TWCR_ACK; |
214 | return; |
224 | return; |
Line 215... | Line 225... | ||
215 | 225 | ||
216 | case TW_ST_SLA_ACK: // slave transmitter selected |
226 | case TW_ST_SLA_ACK: // slave transmitter selected |
217 | // reset index to start of tx buffer |
227 | // reset index to start of tx buffer |
- | 228 | Tx_Idx = 0; |
|
218 | Tx_Idx = 0; |
229 | crc = 0; |
219 | // if tx bufer exist and there is at least ine byte to transfer |
230 | // if tx buffer exist and there is at least one byte to transfer |
220 | if((I2C_TxBuffer != 0) && (I2C_TxBufferSize > 1)) |
231 | if((I2C_TxBuffer != 0) && (I2C_TxBufferSize > 1)) |
221 | { |
232 | { |
- | 233 | data = I2C_TxBuffer[Tx_Idx]; |
|
222 | TWDR = I2C_TxBuffer[Tx_Idx++]; |
234 | |
223 | } |
235 | } |
224 | else |
236 | else |
225 | { // send 0x00 if no tx buffer exist or all bytes of the tx buffer have been transmitted |
237 | { // send 0x00 if no tx buffer exist or all bytes of the tx buffer have been transmitted |
226 | TWDR = 0x00; |
238 | data = 0x00; |
- | 239 | } |
|
- | 240 | crc += data; |
|
- | 241 | Tx_Idx++; |
|
227 | } |
242 | TWDR = data; |
228 | TWCR_ACK; |
243 | TWCR_ACK; |
Line 229... | Line 244... | ||
229 | return; |
244 | return; |
230 | 245 | ||
231 | case TW_ST_DATA_ACK: // data byte has been transmitted ack has been received |
246 | case TW_ST_DATA_ACK: // data byte has been transmitted ack has been received |
232 | // put next byte from tx buffer to the data register |
247 | // put next byte from tx buffer to the data register |
233 | if((I2C_TxBuffer != 0) && (Tx_Idx < I2C_TxBufferSize)) |
248 | if((I2C_TxBuffer != 0) && (Tx_Idx < I2C_TxBufferSize)) |
- | 249 | { |
|
- | 250 | data = I2C_TxBuffer[Tx_Idx]; |
|
- | 251 | crc += data; |
|
- | 252 | } |
|
- | 253 | else if (Tx_Idx == I2C_TxBufferSize) |
|
234 | { |
254 | { // send crc byte at the end |
235 | TWDR = I2C_TxBuffer[Tx_Idx++]; |
255 | data = crc; |
236 | } |
256 | } |
237 | else |
257 | else |
238 | { // send dummy byte instead |
258 | { |
- | 259 | data = 0x00; |
|
- | 260 | } |
|
239 | TWDR = 0x00; |
261 | Tx_Idx++; |
240 | } |
262 | TWDR = data; |
Line 241... | Line 263... | ||
241 | TWCR_ACK; |
263 | TWCR_ACK; |
242 | return; |
264 | return; |