A small USV for ALIX boards with i2c support
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

usi-i2c-slave.c 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /***************************************************************************
  2. * Copyright (C) 01/2009 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; version 2 of the License, *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #include <avr/io.h>
  20. #include <avr/interrupt.h>
  21. #include "alix-usv.h"
  22. #include "usi-i2c-slave.h"
  23. #define USI_MASK 0x0F
  24. #define USI_MASK_ACKBIT 0x10 /* one (ack)bit / 8bits */
  25. #define USI_MASK_OUTPUT 0x20 /* SDA is output / input */
  26. #define USI_RESET 0x00 /* wait for Start Condition */
  27. #define USI_START 0x01 /* Start detected */
  28. #define USI_SLA 0x02 /* wait for Slave Address */
  29. #define USI_SLAW_ACK 0x03 /* ACK Slave Address + Write (Master writes) */
  30. #define USI_SLAR_ACK 0x04 /* ACK Slave Address + Read (Master reads) */
  31. #define USI_DATW 0x05 /* receive Data */
  32. #define USI_DATW_ACK 0x06 /* transmit ACK for received Data */
  33. #define USI_DATR 0x07 /* transmit Data */
  34. #define USI_DATR_ACK 0x08 /* receive ACK for transmitted Data */
  35. static uint8_t usi_state;
  36. void usi_statemachine(void)
  37. {
  38. static uint8_t bcnt;
  39. uint8_t tmp = USIDR;
  40. uint8_t state = usi_state & USI_MASK;
  41. /* Start Condition detected */
  42. if (state == USI_START) {
  43. usi_state = USI_SLA;
  44. /* Slave Address received => prepare ACK/NACK */
  45. } else if (state == USI_SLA) {
  46. bcnt = 0;
  47. if (tmp == (I2C_ADDRESS | 0x00))
  48. usi_state = USI_SLAW_ACK | USI_MASK_ACKBIT | USI_MASK_OUTPUT;
  49. else if (tmp == (I2C_ADDRESS | 0x01))
  50. usi_state = USI_SLAR_ACK | USI_MASK_ACKBIT | USI_MASK_OUTPUT;
  51. else
  52. usi_state = USI_RESET;
  53. /* ACK after SLA+W / Data received => prepare data receive */
  54. } else if (state == USI_SLAW_ACK || state == USI_DATW_ACK) {
  55. usi_state = USI_DATW;
  56. /* Data received => prepare ACK transmit */
  57. } else if (state == USI_DATW) {
  58. usi_write(tmp, bcnt++);
  59. usi_state = USI_DATW_ACK | USI_MASK_ACKBIT | USI_MASK_OUTPUT;
  60. /* ACK after SLA+R transmitted / ACK after Data received => prepare data transmit */
  61. } else if (state == USI_SLAR_ACK || (state == USI_DATR_ACK && tmp == 0x00)) {
  62. USIDR = usi_read(bcnt++);
  63. usi_state = USI_DATR | USI_MASK_OUTPUT;
  64. /* Data transmitted => prepare ACK receive */
  65. } else if (state == USI_DATR) {
  66. usi_state = USI_DATR_ACK | USI_MASK_ACKBIT;
  67. /* NACK after Data received / default => go idle */
  68. } else {
  69. usi_state = USI_RESET;
  70. }
  71. state = usi_state & USI_MASK;
  72. if (state == USI_RESET) {
  73. /* input */
  74. DDRA &= ~I2C_SDA;
  75. /* Enable StartCondition Interrupt, TWI Mode, count both edges */
  76. USICR = (1<<USISIE) | (1<<USIWM1) | (1<<USICS1);
  77. /* Clear Interrupt Flags, Clear Stop Condition Flag, wait for 2 edges */
  78. USISR = (1<<USISIF) | (1<<USIOIF) | (1<<USIPF) | ((16 -2)<<USICNT0);
  79. } else if (state == USI_SLA) {
  80. /* Enable Overflow Interrupt, TWI Mode, extend SCL on Overflow */
  81. USICR = (1<<USISIE) | (1<<USIOIE) | (1<<USIWM1) | (1<<USIWM0) | (1<<USICS1);
  82. /* Clear Interrupt Flags, Clear Stop Condition Flag, wait for 16 edges */
  83. USISR = (1<<USISIF) | (1<<USIOIF) | (1<<USIPF) | ((16 -16)<<USICNT0);
  84. } else {
  85. if (usi_state & USI_MASK_OUTPUT) {
  86. DDRA |= I2C_SDA;
  87. } else {
  88. DDRA &= ~I2C_SDA;
  89. }
  90. if (usi_state & USI_MASK_ACKBIT) {
  91. USIDR = 0x00;
  92. USISR = (1<<USIOIF) | ((16 -2)<<USICNT0);
  93. } else {
  94. USISR = (1<<USIOIF) | ((16 -16)<<USICNT0);
  95. }
  96. }
  97. }
  98. ISR(USI_START_vect)
  99. {
  100. usi_state = USI_START;
  101. /* check for STOP Condition */
  102. while (PINA & I2C_SCL) {
  103. if (PINA & I2C_SDA) {
  104. usi_state = USI_RESET;
  105. break;
  106. }
  107. }
  108. usi_statemachine();
  109. }
  110. ISR(USI_OVF_vect)
  111. {
  112. usi_statemachine();
  113. }