Real Time Clock Module with I2C Interface  0.3
⇒ back to Project Page
 All Data Structures Files Functions Variables Typedefs Macros Pages
i2c.c
Go to the documentation of this file.
1 /****************************************************************************/
39 /******************************************
40  \internal
41  Include files
42 *******************************************/
43 
44 #include <avr/io.h>
45 #include <avr/interrupt.h>
46 #include <util/delay.h>
47 
48 #include "basic_definitions.h"
49 #include "port_definitions.h"
50 #include "i2c.h"
51 
52 #if RTC_LCD_DEBUG == 1
53 #include <stdlib.h>
54 #include "lcd.h"
55 #endif
56 
57 
58 /******************************************
59  \internal
60  Global variables
61 *******************************************/
62 
63 extern volatile uint8_t i2c_slave_mode_blocked; // device is blocked an can not receive data from I2C master
64 
65 
66 
67 
68 /****************************************************************************/
83 /****************************************************************************/
84 
85 void i2c_pin_init(void) {
86 
87  //pin initialization for I2C bus
88  I2C_PORT = I2C_PORT & ~(1 << I2C_SDA_ADDR) & ~(1 << I2C_SCL_ADDR);
89 
90 }
91 
92 
93 /****************************************************************************/
113 /****************************************************************************/
114 
115 uint8_t i2c_set_scl(uint32_t f_cpu, uint32_t f_scl) {
116 
117  uint32_t uint32_tmp;
118 
119 
120  // calculate first part of equation
121  uint32_tmp = (f_cpu / f_scl) / 2;
122 
123  //check for maximum value
124  if (uint32_tmp <= 8) { //value too big - set it to maximum
125  TWSR = TWSR & ~(1 << TWPS1) & ~(1 << TWPS0); //Prescaler value: 1
126  TWBR = 0x00; //Bitrate value: 0
127  return I2C_SCL_ERR;
128  }
129 
130  //check for needed prescaler value
131  uint32_tmp = uint32_tmp - 8;
132 
133  if (uint32_tmp <= I2C_MAX_PRESCALER_1) { //Prescaler value: 1
134  TWSR = TWSR & ~(1 << TWPS1) & ~(1 << TWPS0); //Prescaler value: 1
135  TWBR = uint32_tmp; //Bitrate value
136  } else if (uint32_tmp <= I2C_MAX_PRESCALER_4) {
137  TWSR = TWSR & ~(1 << TWPS1); //Prescaler value: 4
138  TWSR = TWSR | (1 << TWPS0);
139  TWBR = uint32_tmp / 4; //Bitrate value
140  } else if (uint32_tmp <= I2C_MAX_PRESCALER_16) {
141  TWSR = TWSR | (1 << TWPS1); //Prescaler value: 16
142  TWSR = TWSR & ~(1 << TWPS0);
143  TWBR = uint32_tmp / 16; //Bitrate value
144  } else if (uint32_tmp <= I2C_MAX_PRESCALER_64) {
145  TWSR = TWSR | (1 << TWPS1) | (1 << TWPS0); //Prescaler value: 64
146  TWBR = uint32_tmp / 64; //Bitrate value
147  } else {
148  TWSR = TWSR | (1 << TWPS1) | (1 << TWPS0); //Prescaler value: 64
149  TWBR = 255; //Bitrate value
150  return I2C_SCL_ERR; //value too small - set it to minimum
151  }
152 
153  return I2C_SCL_SET_SUCC;
154 
155 }
156 
157 
158 /****************************************************************************/
189 /****************************************************************************/
190 
191 uint8_t i2c_master_data_io(uint8_t *data, uint8_t number_bytes, uint8_t end_noack) {
192 
193  uint8_t i;
194  uint8_t status;
195  static uint8_t TWIE_tmp;
196  static uint8_t TWEA_tmp;
197  static uint8_t SREG_tmp;
198  uint8_t i2c_retry_counter;
199 
200 #if RTC_LCD_DEBUG == 1
201  static uint16_t i2c_error_counter = 0;
202  static char tmp_char[40];
203 #endif
204 
205 
206  /******************************************
207  \internal
208  check for correct input data
209  *******************************************/
210 
211  if ((number_bytes < 2) || (number_bytes > I2C_MASTER_BUFFER_LENGTH)) {
212  return I2C_IO_NUM_ERROR;
213  }
214 
215  /******************************************
216  \internal
217  switch off global interrupts
218  *******************************************/
219 
220  SREG_tmp = SREG; //save SREG
221  cli(); //clear global interrupt enable
222 
223 
224  /******************************************
225  \internal
226  check for pending slave mode communication
227  *******************************************/
228 
229  if (i2c_slave_mode_blocked == 1) {
230  SREG = SREG_tmp;
231  return I2C_IO_BLOCK_ERROR;
232  } else {
233  TWIE_tmp = (TWCR >> TWIE) & 0x1; //save TWIE bit
234  TWCR = TWCR & ~(1 << TWIE);
235  TWEA_tmp = (TWCR >> TWEA) & 0x1; //save TWEA bit
236  TWCR = TWCR & ~(1 << TWEA);
237  SREG = SREG_tmp;
238  }
239 
240 
241 #if RTC_LCD_DEBUG == 1
242  if (TWEA_tmp != 1) {
243  lcd_set_cursor(16, 2);
244  lcd_send_string("!A", FALSE);
245  itoa(i2c_error_counter, tmp_char, 10);
246  lcd_send_string(tmp_char, FALSE);
247  }
248 #endif
249 
250  //wait for a short time
251  _delay_us(I2C_WAIT_BEFORE_STA_US);
252 
253 
254  /******************************************
255  \internal
256  check for free TWI module (no pending
257  communication from slave mode...)
258  *******************************************/
259 
260  if (((TWCR & (1 << TWSTO)) > 0) || ((TWSR & 0xF8) == I2C_BUS_ERROR)) {
261 
262 #if I2C_RESET_BUS_ON_ERROR == 1
263  if ((TWCR & (1 << TWSTO)) > 0) {
264  TWCR = 0;
265  } else {
266  TWCR = (1 << TWSTO); //send stop condition
267  }
268 #endif
269  //clear the TWINT flag
270  if ((TWCR & (1 << TWINT)) > 0) {
271  TWCR = TWCR | (1 << TWINT);
272  }
273  //while (!(TWCR & (1 << TWSTO)));
274  TWCR = TWCR | (1 << TWEN) | (TWIE_tmp << TWIE) | (TWEA_tmp << TWEA);
275 #if I2C_RESET_BUS_ON_ERROR != 1
276  return I2C_IO_BUS_ERROR;
277 #endif
278 
279  }
280 
281 
282  /******************************************
283  \internal
284  TWINT flag is set --> may be an access from
285  another master
286  *******************************************/
287 
288  if ((TWCR & (1 << TWINT)) > 0) {
289 
290  //if no interrupt is enabled --> reset TWINT-Flag - otherwise ISR will do
291  if (TWIE_tmp == 0) {
292  TWCR = TWCR | (1 << TWINT);
293  }
294  TWCR = TWCR | (1 << TWEN) | (TWIE_tmp << TWIE) | (TWEA_tmp << TWEA);
295  return I2C_IO_BLOCK_ERROR;
296  }
297 
298 
299  /******************************************
300  \internal
301  send a start condition and wait for ACK
302  *******************************************/
303 
304  TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN); //start
305  i2c_retry_counter = I2C_MAX_STA_WAIT_CYCLES;
306  while (!(TWCR & (1 << TWINT)) && (i2c_retry_counter > 0)) {
307  i2c_retry_counter--;
308  _delay_us(I2C_STA_RETRY_WAIT_US);
309  }
310 
311  //check for locked bus - reset it if locked
312  if (i2c_retry_counter == 0) {
313  TWCR = 0;
314 #if RTC_LCD_DEBUG == 1
315  i2c_error_counter++;
316  lcd_set_cursor(14, 2);
317  lcd_send_string("!S", FALSE);
318  itoa(i2c_error_counter, tmp_char, 10);
319  lcd_send_string(tmp_char, FALSE);
320 #endif
321  _delay_us(I2C_STA_RETRY_WAIT_US);
322  TWCR = TWCR | (1 << TWSTO) | (1 << TWEN) | (TWIE_tmp << TWIE) | (TWEA_tmp << TWEA);
323  return I2C_STA_ERROR;
324  }
325 
326  //check for status
327  status = TWSR & 0xF8;
328  if ((status != I2C_START_COND_TRANS) && (status != I2C_RSTART_COND_TRANS)) { //start condition transmitted?
329 
330  if (status == I2C_IO_BUS_ERROR) {
331  TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO); //send stop condition
332  //while (!(TWCR & (1 << TWSTO)));
333  }
334 
335  TWCR = TWCR | (TWIE_tmp << TWIE) | (TWEA_tmp << TWEA); //restore interrupt flag
336 
337  return status;
338  };
339 
340  /******************************************
341  \internal
342  send SLA+W or SLA+R and wait for ACK
343  *******************************************/
344 
345  TWDR = data[0];
346  TWCR = (1 << TWINT) | (1 << TWEN); //send data
347 
348  i2c_retry_counter = I2C_MAX_TIMEOUT_WAIT_CYCLES;
349  while (!(TWCR & (1 << TWINT)) && (i2c_retry_counter > 0)) {
350  i2c_retry_counter--;
351  _delay_us(I2C_TIMEOUT_RETRY_WAIT_US);
352  }
353 
354  //check for locked bus - reset it if locked
355  if (i2c_retry_counter == 0) {
356  TWCR = TWCR | (1 << TWSTO); //send stop condition
357  _delay_us(I2C_TIMEOUT_RETRY_WAIT_US);
358  TWCR = TWCR | (1 << TWEN) | (TWIE_tmp << TWIE) | (TWEA_tmp << TWEA);
359  return I2C_TIMEOUT_ERROR;
360  }
361 
362  //check for status
363  status = TWSR & 0xF8;
364  if ((status != I2C_SLAW_TRANS) && (status != I2C_SLAR_TRANS)) { //ACK after slave address transmission received?
365  if (status == I2C_M_ARB_LOST) { //arbitration lost? do nothing (maybe switch to slave mode in calling function)
366  TWCR = (1 << TWINT) | (TWIE_tmp << TWIE) | (TWEA_tmp << TWEA);
367  return status;
368  } else { //send stop condition
369  TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
370  //while (!(TWCR & (1 << TWSTO)));
371  TWCR = TWCR | (TWIE_tmp << TWIE) | (TWEA_tmp << TWEA);
372  return status;
373  }
374  }
375 
376 
377  /******************************************
378  \internal
379  send data to slave
380  *******************************************/
381 
382  if (status == I2C_SLAW_TRANS) {
383 
384  //send all bytes to slave (without SLA+W)
385  for (i = 1; i < number_bytes; i++) {
386 
387  TWDR = data[i];
388  TWCR = (1 << TWINT) | (1 << TWEN); //send data
389 
390  i2c_retry_counter = I2C_MAX_TIMEOUT_WAIT_CYCLES;
391  while (!(TWCR & (1 << TWINT)) && (i2c_retry_counter > 0)) {
392  i2c_retry_counter--;
393  _delay_us(I2C_TIMEOUT_RETRY_WAIT_US);
394  }
395 
396  //check for locked bus - reset it if locked
397  if (i2c_retry_counter == 0) {
398  TWCR = TWCR | (1 << TWSTO); //send stop condition
399  _delay_us(I2C_TIMEOUT_RETRY_WAIT_US);
400  TWCR = TWCR | (1 << TWEN) | (TWIE_tmp << TWIE) | (TWEA_tmp << TWEA);
401  return I2C_TIMEOUT_ERROR;
402  }
403 
404  status = TWSR & 0xF8;
405 
406  if (status != I2C_DATA_TRANS) { //ACK after data transmission received?
407  if (status == I2C_M_ARB_LOST) { //arbitration lost? do nothing (maybe switch to slave mode in calling function)
408  TWCR = (1 << TWINT) | (TWIE_tmp << TWIE) | (TWEA_tmp << TWEA);
409  return status;
410  } else { //send stop condition
411  TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
412  //while (!(TWCR & (1 << TWSTO)));
413  TWCR = TWCR | (TWIE_tmp << TWIE) | (TWEA_tmp << TWEA);
414  if ((i == (number_bytes - 1)) && (end_noack == TRUE)) { //NOACK from slave after last byte?
415  return I2C_IO_SUCC;
416  } else {
417  return status;
418  }
419  }
420  }
421 
422  }
423 
424  /******************************************
425  \internal
426  read data from slave
427  *******************************************/
428 
429  } else if (status == I2C_SLAR_TRANS) {
430 
431  //read only n-1 bytes from slave (without SLA+R
432  for (i = 1; i < (number_bytes - 1); i++) {
433 
434  TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA); //send ACK
435 
436  i2c_retry_counter = I2C_MAX_TIMEOUT_WAIT_CYCLES;
437  while (!(TWCR & (1 << TWINT)) && (i2c_retry_counter > 0)) {
438  i2c_retry_counter--;
439  _delay_us(I2C_TIMEOUT_RETRY_WAIT_US);
440  }
441 
442  //check for locked bus - reset it if locked
443  if (i2c_retry_counter == 0) {
444  TWCR = TWCR | (1 << TWSTO); //send stop condition
445  _delay_us(I2C_TIMEOUT_RETRY_WAIT_US);
446  TWCR = TWCR | (1 << TWEN) | (TWIE_tmp << TWIE) | (TWEA_tmp << TWEA);
447  return I2C_TIMEOUT_ERROR;
448  }
449 
450  status = TWSR & 0xF8;
451 
452  if (status != I2C_DATA_RCV_ACK) { //Data received and ACK sent?
453  if (status == I2C_M_ARB_LOST) { //arbitration lost? do nothing (maybe switch to slave mode in calling function)
454  TWCR = (1 << TWINT) | (TWIE_tmp << TWIE) | (TWEA_tmp << TWEA);
455  return status;
456  } else { //send stop condition
457  TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
458  //while (!(TWCR & (1 << TWSTO)));
459  TWCR = TWCR | (TWIE_tmp << TWIE) | (TWEA_tmp << TWEA);
460  return status;
461  }
462  }
463  data[i] = TWDR; //read data
464 
465  }
466 
467  //read last byte and send NOACK after it
468  TWCR = (1 << TWINT) | (1 << TWEN); //send NOACK
469 
470  i2c_retry_counter = I2C_MAX_TIMEOUT_WAIT_CYCLES;
471  while (!(TWCR & (1 << TWINT)) && (i2c_retry_counter > 0)) {
472  i2c_retry_counter--;
473  _delay_us(I2C_TIMEOUT_RETRY_WAIT_US);
474  }
475 
476  //check for locked bus - reset it if locked
477  if (i2c_retry_counter == 0) {
478  TWCR = TWCR | (1 << TWSTO); //send stop condition
479  _delay_us(I2C_TIMEOUT_RETRY_WAIT_US);
480  TWCR = TWCR | (1 << TWEN) | (TWIE_tmp << TWIE) | (TWEA_tmp << TWEA);
481  return I2C_TIMEOUT_ERROR;
482  }
483 
484  status = TWSR & 0xF8;
485 
486  if (status != I2C_DATA_RCV_NACK) { //Data received and ACK sent?
487  if (status == I2C_M_ARB_LOST) { //arbitration lost? do nothing (maybe switch to slave mode in calling function)
488  TWCR = (1 << TWINT) | (TWIE_tmp << TWIE) | (TWEA_tmp << TWEA);
489  return status;
490  } else { //send stop condition
491  TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
492  //while (!(TWCR & (1 << TWSTO)));
493  TWCR = TWCR | (TWIE_tmp << TWIE) | (TWEA_tmp << TWEA);
494  return status;
495  }
496  }
497  data[i] = TWDR; //read data
498 
499 
500  }
501 
502  /******************************************
503  \internal
504  send stop condition (end of transmission)
505  *******************************************/
506  TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
507 
508  //while (!(TWCR & (1 << TWSTO)));
509 
510  TWCR = TWCR | (TWIE_tmp << TWIE) | (TWEA_tmp << TWEA);
511 
512 
513  return I2C_IO_SUCC;
514 
515 
516 }
517 
518 
519 /****************************************************************************/
563 /****************************************************************************/
564 #if I2C_SLAVE_INTERRUPT_OPERATION == 1
565 uint8_t i2c_slave_data_io(volatile uint8_t *data, volatile uint8_t *number_bytes, volatile uint8_t command) {
566 #else
567 uint8_t i2c_slave_data_io(uint8_t *data, uint8_t *number_bytes, uint8_t command) {
568 #endif
569 
570  static uint8_t address_counter = 0; //initial value: 0
571  uint8_t current_address;
572  uint8_t status;
573  static uint8_t TWIE_tmp;
574 
575 #if RTC_LCD_DEBUG == 1
576  static uint16_t i2c_error_counter = 0;
577  static char tmp_char[40];
578 #endif
579 
580 
581 
582  /******************************************
583  \internal
584  after being addressed as slave:
585  *******************************************/
586 
587  //save Interrupt Flag
588  TWIE_tmp = (TWCR >> TWIE) & 0x1;
589 
590  //check Status Register
591  status = TWSR & 0xF8;
592 
593  switch (status) {
594 
595 
596  /******************************************
597  \internal
598  SLAVE RECEIVE mode
599  *******************************************/
600 
601 
602  //SLA + Write Bit received and ACK was returned
603  case I2C_SLAW_ACK_RCVD:
604  //or Arbitration lost as Master (SLAW), SLAW received and ACK was returned
606  //or General Call + Write Bit received and ACK was returned
607  case I2C_GCA_ACK_RCVD:
608  //Arbitration lost as Master (SLAW), General Call + Write Bit received and ACK was returned
610 
611  address_counter = 0; //set adress counter to 0
612  *number_bytes = 0; //set number of transferred bytes to 0
613  //slave mode aborted or not?
614  if (command == I2C_SLAVE_MODE_ABORT) {
615  TWCR = (1 << TWINT) | (1 << TWEN) | (TWIE_tmp << TWIE); //next step: data byte will be received and NOACK returned
616  } else {
617  TWCR = (1 << TWINT) | (1 << TWEN) | (TWIE_tmp << TWIE) | (1 << TWEA); //next step: data byte will be received and ACK returned
618  }
619 
620  break;
621 
622 
623 
624  //Data received, ACK returned
625  case I2C_DATA_ACK_RCVD:
626  //Data received, NOACK returned
627  case I2C_DATA_NOACK_RCVD:
628  //General Call Data received, ACK returned
630  //General Call Data received, NOACK returned
632 
633  //first byte? --> register address
634  if (address_counter == 0) {
635 
636  data[0] = TWDR;
637  *number_bytes = *number_bytes + 1;
638  address_counter++;
639 
640  //if next byte is last byte of buffer (or overflow) --> after next byte NOACK will be will be returned
641  if ((data[0] + address_counter) > (I2C_SLAVE_BUFFER_LENGTH - 1)) {
642  TWCR = (1 << TWINT) | (1 << TWEN) | (TWIE_tmp << TWIE); //next step: data byte will be received and NOACK returned
643  } else {
644  //slave mode aborted or not?
645  if ((command == I2C_SLAVE_MODE_ABORT) ||
646  ((status == I2C_DATA_NOACK_RCVD) && (command == I2C_SLAVE_MODE_END)) ||
647  ((status == I2C_GCA_DATA_NOACK_RCVD) && (command == I2C_SLAVE_MODE_END))) {
648  TWCR = (1 << TWINT) | (1 << TWEN) | (TWIE_tmp << TWIE); //next step: data byte will be received and NOACK returned
649  } else {
650  TWCR = (1 << TWINT) | (1 << TWEN) | (TWIE_tmp << TWIE) | (1 << TWEA); //next step: data byte will be received and ACK returned
651  }
652  }
653 
654  } else {
655 
656  //calculate current address
657  current_address = data[0] + address_counter;
658 
659  //valid address, also for next byte
660  if (current_address < (I2C_SLAVE_BUFFER_LENGTH - 1)) {
661 
662  data[current_address] = TWDR;
663  *number_bytes = *number_bytes + 1;
664  address_counter++;
665  //slave mode aborted or not?
666  if ((command == I2C_SLAVE_MODE_ABORT) ||
667  ((status == I2C_DATA_NOACK_RCVD) && (command == I2C_SLAVE_MODE_END)) ||
668  ((status == I2C_GCA_DATA_NOACK_RCVD) && (command == I2C_SLAVE_MODE_END))) {
669  TWCR = (1 << TWINT) | (1 << TWEN) | (TWIE_tmp << TWIE); //next step: data byte will be received and NOACK returned
670  } else {
671  TWCR = (1 << TWINT) | (1 << TWEN) | (TWIE_tmp << TWIE) | (1 << TWEA); //next step: data byte will be received and ACK returned
672  }
673 
674  //current address is valid, but last byte of buffer
675  } else if (current_address < I2C_SLAVE_BUFFER_LENGTH) {
676 
677  data[current_address] = TWDR;
678  *number_bytes = *number_bytes + 1;
679  address_counter++;
680  TWCR = (1 << TWINT) | (1 << TWEN) | (TWIE_tmp << TWIE); //next step: data byte will be received and NOACK returned
681 
682  //current address is invalid
683  } else {
684 
685  TWCR = (1 << TWINT) | (1 << TWEN) | (TWIE_tmp << TWIE); //next step: data byte will be received and NOACK returned
686 
687  }
688 
689  }
690 
691  break;
692 
693 
694  /******************************************
695  \internal
696  END OF TRANSMISSION:
697  *******************************************/
698 
699  //stop or repeated start condition received (while adressed)
701 
702  //slave mode continued or not?
703  if (command == I2C_SLAVE_MODE_CONTINUE) {
704  TWCR = (1 << TWINT) | (1 << TWEN) | (TWIE_tmp << TWIE) | (1 << TWEA); //next step: data byte will be received and ACK returned
705  } else {
706  TWCR = (1 << TWINT) | (1 << TWEN) | (TWIE_tmp << TWIE); //next step: data byte will be received and NOACK returned
707  }
708 
709  break;
710 
711 
712 
713  /******************************************
714  \internal
715  SLAVE SEND mode
716  *******************************************/
717 
718 
719  //SLA + Read Bit received and ACK was returned
720  case I2C_SLAR_ACK_RCVD:
721  //or Arbitration lost as Master (SLAR), SLAR received and ACK was returned
723 
724  address_counter = 0; //set adress counter to 0
725  *number_bytes = 0; //set number of transferred bytes to 0
726 
727  //calculate current address
728  current_address = data[0] + 1;
729 
730  //valid address, also for next byte
731  if (current_address < (I2C_SLAVE_BUFFER_LENGTH - 1)) {
732 
733  TWDR = data[current_address];
734  *number_bytes = *number_bytes + 1;
735  address_counter++;
736  //slave mode aborted or not?
737  if (command == I2C_SLAVE_MODE_ABORT) {
738  TWCR = (1 << TWINT) | (1 << TWEN) | (TWIE_tmp << TWIE); //next step: data byte will be transmitted and NOACK returned
739  } else {
740  TWCR = (1 << TWINT) | (1 << TWEN) | (TWIE_tmp << TWIE) | (1 << TWEA); //next step: data byte will be transmitted and ACK received
741  }
742 
743  //current address is valid, but last byte of buffer
744  } else if (current_address < I2C_SLAVE_BUFFER_LENGTH) {
745 
746  TWDR = data[current_address];
747  *number_bytes = *number_bytes + 1;
748  address_counter++;
749  TWCR = (1 << TWINT) | (1 << TWEN) | (TWIE_tmp << TWIE); //next step: data byte will be transmitted and NOACK received/returned
750 
751  //current address is invalid
752  } else {
753 
754  TWDR = I2C_REGISTER_ERROR_VALUE; //send error value to indicate an invalid address
755  TWCR = (1 << TWINT) | (1 << TWEN) | (TWIE_tmp << TWIE); //next step: data byte will be transmitted and NOACK received/returned
756 
757  }
758 
759  break;
760 
761 
762  //Data transmitted, ACK received
763  case I2C_DATA_ACK_TRANS:
764 
765  //calculate current address
766  current_address = data[0] + address_counter + 1;
767 
768  //valid address, also for next byte
769  if (current_address < (I2C_SLAVE_BUFFER_LENGTH - 1)) {
770 
771  TWDR = data[current_address];
772  *number_bytes = *number_bytes + 1;
773  address_counter++;
774  //slave mode aborted or not?
775  if (command == I2C_SLAVE_MODE_ABORT) {
776  TWCR = (1 << TWINT) | (1 << TWEN) | (TWIE_tmp << TWIE); //next step: data byte will be transmitted and NOACK returned
777  } else {
778  TWCR = (1 << TWINT) | (1 << TWEN) | (TWIE_tmp << TWIE) | (1 << TWEA); //next step: data byte will be transmitted and ACK received
779  }
780 
781  //current address is valid, but last byte of buffer
782  } else if (current_address < I2C_SLAVE_BUFFER_LENGTH) {
783 
784  TWDR = data[current_address];
785  *number_bytes = *number_bytes + 1;
786  address_counter++;
787  TWCR = (1 << TWINT) | (1 << TWEN) | (TWIE_tmp << TWIE); //next step: data byte will be received and NOACK received/returned
788 
789  //current address is invalid
790  } else {
791 
792  TWDR = I2C_REGISTER_ERROR_VALUE; //send error value to indicate an invalid address
793  TWCR = (1 << TWINT) | (1 << TWEN) | (TWIE_tmp << TWIE); //next step: data byte will be received and NOACK received/returned
794 
795  }
796 
797  break;
798 
799 
800  /******************************************
801  \internal
802  END OF TRANSMISSION:
803  *******************************************/
804 
805  //Data transmitted, NOACK received
807  //Last Data Byte transmitted, ACK received
809 
810  //slave mode continued or not?
811  if (command == I2C_SLAVE_MODE_CONTINUE) {
812  TWCR = (1 << TWINT) | (1 << TWEN) | (TWIE_tmp << TWIE) | (1 << TWEA); //next step: ACK will be returned for new request
813  } else {
814  TWCR = (1 << TWINT) | (1 << TWEN) | (TWIE_tmp << TWIE); //next step: NOACK will be returned for new request
815  }
816 
817  break;
818 
819 
820 
821  default:
822 
823 #if RTC_LCD_DEBUG == 1
824  i2c_error_counter++;
825  lcd_set_cursor(8, 2);
826  itoa(status, tmp_char, 16);
827  lcd_send_string(tmp_char, FALSE);
828  lcd_send_string(" ", FALSE);
829  itoa(i2c_error_counter, tmp_char, 10);
830  lcd_send_string(tmp_char, FALSE);
831 #endif
832 
833 #if I2C_RESET_BUS_ON_ERROR == 1
834  if ((TWCR & (1 << TWSTO)) > 0) {
835  TWCR = 0;
836  }
837 #endif
838 
839  //slave mode continued or not?
840  if (command == I2C_SLAVE_MODE_CONTINUE) {
841  TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN) | (TWIE_tmp << TWIE) | (1 << TWEA); //go into non-addressed mode and end slave mode
842  } else {
843  TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN) | (TWIE_tmp << TWIE); //go into non-addressed mode
844  }
845 
846  return I2C_IO_BUS_ERROR;
847 
848 
849  }
850 
851 
852  return status;
853 
854 }