Bez popisu
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_TWI_Master.c 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*****************************************************************************
  2. *
  3. * Atmel Corporation
  4. *
  5. * File : USI_TWI_Master.c
  6. * Date : $Date: 2016-7-15 $
  7. * Updated by : $Author: Atmel $
  8. *
  9. * Support mail : avr@atmel.com
  10. *
  11. * Supported devices : All device with USI module can be used.
  12. * The example is written for the ATmega169, ATtiny26 and ATtiny2313
  13. *
  14. * AppNote : AVR310 - Using the USI module as a TWI Master
  15. *
  16. * Description : This is an implementation of an TWI master using
  17. * the USI module as basis. The implementation assumes the AVR to
  18. * be the only TWI master in the system and can therefore not be
  19. * used in a multi-master system.
  20. * Usage : Initialize the USI module by calling the USI_TWI_Master_Initialise()
  21. * function. Hence messages/data are transceived on the bus using
  22. * the USI_TWI_Transceive() function. The transceive function
  23. * returns a status byte, which can be used to evaluate the
  24. * success of the transmission.
  25. *
  26. ****************************************************************************/
  27. #if __GNUC__
  28. #include <avr/io.h>
  29. #else
  30. #include <inavr.h>
  31. #include <ioavr.h>
  32. #endif
  33. #include "USI_TWI_Master.h"
  34. unsigned char USI_TWI_Master_Transfer(unsigned char);
  35. unsigned char USI_TWI_Master_Stop(void);
  36. union USI_TWI_state {
  37. unsigned char errorState; // Can reuse the TWI_state for error states due to that it will not be need if there
  38. // exists an error.
  39. struct {
  40. unsigned char addressMode : 1;
  41. unsigned char masterWriteDataMode : 1;
  42. unsigned char unused : 6;
  43. };
  44. } USI_TWI_state;
  45. /*---------------------------------------------------------------
  46. USI TWI single master initialization function
  47. ---------------------------------------------------------------*/
  48. void USI_TWI_Master_Initialise(void)
  49. {
  50. PORT_USI |= (1 << PIN_USI_SDA); // Enable pullup on SDA, to set high as released state.
  51. PORT_USI |= (1 << PIN_USI_SCL); // Enable pullup on SCL, to set high as released state.
  52. DDR_USI |= (1 << PIN_USI_SCL); // Enable SCL as output.
  53. DDR_USI |= (1 << PIN_USI_SDA); // Enable SDA as output.
  54. USIDR = 0xFF; // Preload dataregister with "released level" data.
  55. USICR = (0 << USISIE) | (0 << USIOIE) | // Disable Interrupts.
  56. (1 << USIWM1) | (0 << USIWM0) | // Set USI in Two-wire mode.
  57. (1 << USICS1) | (0 << USICS0) | (1 << USICLK) | // Software stobe as counter clock source
  58. (0 << USITC);
  59. USISR = (1 << USISIF) | (1 << USIOIF) | (1 << USIPF) | (1 << USIDC) | // Clear flags,
  60. (0x0 << USICNT0); // and reset counter.
  61. }
  62. /*---------------------------------------------------------------
  63. Use this function to get hold of the error message from the last transmission
  64. ---------------------------------------------------------------*/
  65. unsigned char USI_TWI_Get_State_Info(void)
  66. {
  67. return (USI_TWI_state.errorState); // Return error state.
  68. }
  69. /*---------------------------------------------------------------
  70. USI Transmit and receive function. LSB of first byte in data
  71. indicates if a read or write cycles is performed. If set a read
  72. operation is performed.
  73. Function generates (Repeated) Start Condition, sends address and
  74. R/W, Reads/Writes Data, and verifies/sends ACK.
  75. Success or error code is returned. Error codes are defined in
  76. USI_TWI_Master.h
  77. ---------------------------------------------------------------*/
  78. #ifndef __GNUC__
  79. __x // AVR compiler
  80. #endif
  81. unsigned char
  82. USI_TWI_Start_Transceiver_With_Data(unsigned char *msg, unsigned char msgSize)
  83. {
  84. unsigned char tempUSISR_8bit = (1 << USISIF) | (1 << USIOIF) | (1 << USIPF) | (1 << USIDC)
  85. | // Prepare register value to: Clear flags, and
  86. (0x0 << USICNT0); // set USI to shift 8 bits i.e. count 16 clock edges.
  87. unsigned char tempUSISR_1bit = (1 << USISIF) | (1 << USIOIF) | (1 << USIPF) | (1 << USIDC)
  88. | // Prepare register value to: Clear flags, and
  89. (0xE << USICNT0); // set USI to shift 1 bit i.e. count 2 clock edges.
  90. USI_TWI_state.errorState = 0;
  91. USI_TWI_state.addressMode = TRUE;
  92. #ifdef PARAM_VERIFICATION
  93. if (msg > (unsigned char *)RAMEND) // Test if address is outside SRAM space
  94. {
  95. USI_TWI_state.errorState = USI_TWI_DATA_OUT_OF_BOUND;
  96. return (FALSE);
  97. }
  98. if (msgSize <= 1) // Test if the transmission buffer is empty
  99. {
  100. USI_TWI_state.errorState = USI_TWI_NO_DATA;
  101. return (FALSE);
  102. }
  103. #endif
  104. #ifdef NOISE_TESTING // Test if any unexpected conditions have arrived prior to this execution.
  105. if (USISR & (1 << USISIF)) {
  106. USI_TWI_state.errorState = USI_TWI_UE_START_CON;
  107. return (FALSE);
  108. }
  109. if (USISR & (1 << USIPF)) {
  110. USI_TWI_state.errorState = USI_TWI_UE_STOP_CON;
  111. return (FALSE);
  112. }
  113. if (USISR & (1 << USIDC)) {
  114. USI_TWI_state.errorState = USI_TWI_UE_DATA_COL;
  115. return (FALSE);
  116. }
  117. #endif
  118. if (!(*msg
  119. & (1 << TWI_READ_BIT))) // The LSB in the address byte determines if is a masterRead or masterWrite operation.
  120. {
  121. USI_TWI_state.masterWriteDataMode = TRUE;
  122. }
  123. /* Release SCL to ensure that (repeated) Start can be performed */
  124. PORT_USI |= (1 << PIN_USI_SCL); // Release SCL.
  125. while (!(PIN_USI & (1 << PIN_USI_SCL)))
  126. ; // Verify that SCL becomes high.
  127. #ifdef TWI_FAST_MODE
  128. DELAY_T4TWI; // Delay for T4TWI if TWI_FAST_MODE
  129. #else
  130. DELAY_T2TWI; // Delay for T2TWI if TWI_STANDARD_MODE
  131. #endif
  132. /* Generate Start Condition */
  133. PORT_USI &= ~(1 << PIN_USI_SDA); // Force SDA LOW.
  134. DELAY_T4TWI;
  135. PORT_USI &= ~(1 << PIN_USI_SCL); // Pull SCL LOW.
  136. PORT_USI |= (1 << PIN_USI_SDA); // Release SDA.
  137. #ifdef SIGNAL_VERIFY
  138. if (!(USISR & (1 << USISIF))) {
  139. USI_TWI_state.errorState = USI_TWI_MISSING_START_CON;
  140. return (FALSE);
  141. }
  142. #endif
  143. /*Write address and Read/Write data */
  144. do {
  145. /* If masterWrite cycle (or inital address tranmission)*/
  146. if (USI_TWI_state.addressMode || USI_TWI_state.masterWriteDataMode) {
  147. /* Write a byte */
  148. PORT_USI &= ~(1 << PIN_USI_SCL); // Pull SCL LOW.
  149. USIDR = *(msg++); // Setup data.
  150. USI_TWI_Master_Transfer(tempUSISR_8bit); // Send 8 bits on bus.
  151. /* Clock and verify (N)ACK from slave */
  152. DDR_USI &= ~(1 << PIN_USI_SDA); // Enable SDA as input.
  153. if (USI_TWI_Master_Transfer(tempUSISR_1bit) & (1 << TWI_NACK_BIT)) {
  154. if (USI_TWI_state.addressMode)
  155. USI_TWI_state.errorState = USI_TWI_NO_ACK_ON_ADDRESS;
  156. else
  157. USI_TWI_state.errorState = USI_TWI_NO_ACK_ON_DATA;
  158. return (FALSE);
  159. }
  160. USI_TWI_state.addressMode = FALSE; // Only perform address transmission once.
  161. }
  162. /* Else masterRead cycle*/
  163. else {
  164. /* Read a data byte */
  165. DDR_USI &= ~(1 << PIN_USI_SDA); // Enable SDA as input.
  166. *(msg++) = USI_TWI_Master_Transfer(tempUSISR_8bit);
  167. /* Prepare to generate ACK (or NACK in case of End Of Transmission) */
  168. if (msgSize == 1) // If transmission of last byte was performed.
  169. {
  170. USIDR = 0xFF; // Load NACK to confirm End Of Transmission.
  171. } else {
  172. USIDR = 0x00; // Load ACK. Set data register bit 7 (output for SDA) low.
  173. }
  174. USI_TWI_Master_Transfer(tempUSISR_1bit); // Generate ACK/NACK.
  175. }
  176. } while (--msgSize); // Until all data sent/received.
  177. USI_TWI_Master_Stop(); // Send a STOP condition on the TWI bus.
  178. /* Transmission successfully completed*/
  179. return (TRUE);
  180. }
  181. /*---------------------------------------------------------------
  182. Core function for shifting data in and out from the USI.
  183. Data to be sent has to be placed into the USIDR prior to calling
  184. this function. Data read, will be return'ed from the function.
  185. ---------------------------------------------------------------*/
  186. unsigned char USI_TWI_Master_Transfer(unsigned char temp)
  187. {
  188. USISR = temp; // Set USISR according to temp.
  189. // Prepare clocking.
  190. temp = (0 << USISIE) | (0 << USIOIE) | // Interrupts disabled
  191. (1 << USIWM1) | (0 << USIWM0) | // Set USI in Two-wire mode.
  192. (1 << USICS1) | (0 << USICS0) | (1 << USICLK) | // Software clock strobe as source.
  193. (1 << USITC); // Toggle Clock Port.
  194. do {
  195. DELAY_T2TWI;
  196. USICR = temp; // Generate positve SCL edge.
  197. while (!(PIN_USI & (1 << PIN_USI_SCL)))
  198. ; // Wait for SCL to go high.
  199. DELAY_T4TWI;
  200. USICR = temp; // Generate negative SCL edge.
  201. } while (!(USISR & (1 << USIOIF))); // Check for transfer complete.
  202. DELAY_T2TWI;
  203. temp = USIDR; // Read out data.
  204. USIDR = 0xFF; // Release SDA.
  205. DDR_USI |= (1 << PIN_USI_SDA); // Enable SDA as output.
  206. return temp; // Return the data from the USIDR
  207. }
  208. /*---------------------------------------------------------------
  209. Function for generating a TWI Stop Condition. Used to release
  210. the TWI bus.
  211. ---------------------------------------------------------------*/
  212. unsigned char USI_TWI_Master_Stop(void)
  213. {
  214. PORT_USI &= ~(1 << PIN_USI_SDA); // Pull SDA low.
  215. PORT_USI |= (1 << PIN_USI_SCL); // Release SCL.
  216. while (!(PIN_USI & (1 << PIN_USI_SCL)))
  217. ; // Wait for SCL to go high.
  218. DELAY_T4TWI;
  219. PORT_USI |= (1 << PIN_USI_SDA); // Release SDA.
  220. DELAY_T2TWI;
  221. #ifdef SIGNAL_VERIFY
  222. if (!(USISR & (1 << USIPF))) {
  223. USI_TWI_state.errorState = USI_TWI_MISSING_STOP_CON;
  224. return (FALSE);
  225. }
  226. #endif
  227. return (TRUE);
  228. }