eerom.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include "bsp.h"
  2. #include "I2C.h"
  3. #include "eerom.h"
  4. #include "utils.h"
  5. //#define EEROM_DELAY 3
  6. #define EEROM_DELAY 5
  7. #define DEFAULT_EEROM 0
  8. #define EEROM_TEST_ENABLE 0
  9. #define EEROM_TEST_COUNT 256
  10. #define EEROM_TEST_ADDR 256
  11. //IIC send a byte
  12. //return ACK from slave
  13. // 1: ACK
  14. // 0: NACK
  15. #ifdef __GNUC__
  16. #pragma GCC optimize ("O0")
  17. #elif defined(__ICCARM__)
  18. #pragma optimize=none
  19. #endif
  20. static uint8_t I2C_SendByte(uint8_t txd)
  21. {
  22. uint8_t t;
  23. I2C_GPIO_Out_Config();
  24. BSP_I2Cx_SCL_LOW();
  25. for(t=0;t<8;t++)
  26. {
  27. if(txd&0x80)
  28. BSP_I2Cx_SDA_HIGH();
  29. else
  30. BSP_I2Cx_SDA_LOW();
  31. txd<<=1;
  32. Delay_us(I2C_DELAY_COUNT); //For TEA5767, the delay here is necessary
  33. BSP_I2Cx_SCL_HIGH();
  34. Delay_us(I2C_DELAY_COUNT);
  35. BSP_I2Cx_SCL_LOW();
  36. Delay_us(I2C_DELAY_COUNT);
  37. }
  38. return 0;
  39. }
  40. //Read a byte; ack=1, send an ack, ack=0, NACK
  41. #ifdef __GNUC__
  42. #pragma GCC optimize ("O0")
  43. #elif defined(__ICCARM__)
  44. #pragma optimize=none
  45. #endif
  46. static uint8_t I2C_ReceiveByte(uint8_t ack)
  47. {
  48. uint8_t i,receive=0;
  49. BSP_I2Cx_SCL_LOW();
  50. I2C_GPIO_In_Config();//SDA input
  51. for(i=0;i<8;i++ )
  52. {
  53. BSP_I2Cx_SCL_LOW();
  54. Delay_us(I2C_DELAY_COUNT);
  55. BSP_I2Cx_SCL_HIGH();
  56. Delay_us(I2C_DELAY_COUNT/2);
  57. receive <<= 1;
  58. if(BSP_I2Cx_SDA_STATUS())receive++;
  59. Delay_us(I2C_DELAY_COUNT/2);
  60. }
  61. if(ack)
  62. I2C_Ack();//Send ACK
  63. else
  64. I2C_NAck();
  65. BSP_I2Cx_SCL_LOW();
  66. return receive;
  67. }
  68. //Get data at the AT24CXX
  69. //ReadAddr: start byteaddress
  70. //return: data
  71. #ifdef __GNUC__
  72. #pragma GCC optimize ("O0")
  73. #elif defined(__ICCARM__)
  74. #pragma optimize=none
  75. #endif
  76. uint8_t I2C_ReadOneByte(uint16_t ReadAddr)
  77. {
  78. uint8_t temp=0;
  79. uint8_t test[10]={0};
  80. I2C_Start();
  81. test[0]=I2C_SendByte(EE_WR_ADDRESS(ReadAddr)); //send chip address and write command
  82. //test[0]=I2C_SendByte(BSP_EEROM_WR_ADDRESS); //
  83. test[0]++;
  84. test[1]=I2C_Wait_Ack();
  85. test[1]++;
  86. //#if EEPROM>=32
  87. #if 1
  88. temp=(uint8_t)(ReadAddr>>8);
  89. test[2]=I2C_SendByte(temp);//Send address high byte
  90. test[2]++;
  91. test[3]=I2C_Wait_Ack();
  92. test[3]++;
  93. #endif
  94. temp=(uint8_t)(ReadAddr&0xff);
  95. test[4]=I2C_SendByte(temp); //Send address low byte
  96. test[4]++;
  97. test[5]=I2C_Wait_Ack();
  98. test[5]++;
  99. I2C_Start();
  100. //test[6]=I2C_SendByte(EE_RD_ADDRESS(ReadAddr)); //Start recv
  101. test[6]=I2C_SendByte(BSP_EEROM_RD_ADDRESS); //Start recv
  102. test[6]++;
  103. test[7]=I2C_Wait_Ack();
  104. test[7]++;
  105. temp=I2C_ReceiveByte(0);
  106. I2C_Stop();//stop
  107. test[8]++;
  108. return temp;
  109. }
  110. // write data into AT24CXX
  111. //WriteAddr : dst address
  112. //DataToWrite: data
  113. #ifdef __GNUC__
  114. #pragma GCC optimize ("O0")
  115. #elif defined(__ICCARM__)
  116. #pragma optimize=none
  117. #endif
  118. void I2C_WriteOneByte(uint16_t WriteAddr,uint8_t DataToWrite)
  119. {
  120. uint8_t temp[10]={0};
  121. BSP_EEROM_WRITE_ENABLE();
  122. I2C_Start();
  123. temp[0]=I2C_SendByte(EE_WR_ADDRESS(WriteAddr)); //send chip address and write command
  124. temp[0]++;
  125. temp[1]=I2C_Wait_Ack();
  126. temp[1]++;
  127. //#if EEPROM>=32
  128. #if 1
  129. temp[2]=I2C_SendByte(WriteAddr>>8);//Send address high byte
  130. temp[2]++;
  131. temp[3]=I2C_Wait_Ack();
  132. temp[3]++;
  133. #endif
  134. temp[4]=I2C_SendByte(WriteAddr&0xff); //Send address low byte
  135. temp[4]++;
  136. temp[5]=I2C_Wait_Ack();
  137. temp[5]++;
  138. temp[6]=I2C_SendByte(DataToWrite); //Send data byte
  139. temp[6]++;
  140. temp[7]=I2C_Wait_Ack();
  141. temp[7]++;
  142. I2C_Stop();//
  143. temp[8]++;
  144. BSP_EEROM_WRITE_FORBIDEN();
  145. }
  146. #ifdef __GNUC__
  147. #pragma GCC optimize ("O0")
  148. #elif defined(__ICCARM__)
  149. #pragma optimize=none
  150. #endif
  151. void I2C_WriteBuffer(uint8_t* pBuffer, uint16_t WriteAddr, uint16_t NumByteToWrite)
  152. {
  153. BSP_EEROM_WRITE_ENABLE();
  154. I2C_Start();
  155. I2C_SendByte(EE_WR_ADDRESS(WriteAddr)); //send chip address and write command
  156. I2C_Wait_Ack();
  157. //#if EEPROM>=32
  158. #if 1
  159. I2C_SendByte(WriteAddr>>8);//Send address high byte
  160. I2C_Wait_Ack();
  161. #endif
  162. I2C_SendByte(WriteAddr&0xff); //Send address low byte
  163. I2C_Wait_Ack();
  164. while(NumByteToWrite--)
  165. {
  166. I2C_SendByte(*pBuffer++); //Send data byte
  167. I2C_Wait_Ack();
  168. }
  169. I2C_Stop();//
  170. BSP_EEROM_WRITE_FORBIDEN();
  171. }
  172. #ifdef __GNUC__
  173. #pragma GCC optimize ("O0")
  174. #elif defined(__ICCARM__)
  175. #pragma optimize=none
  176. #endif
  177. void I2C_ReadBuffer(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t NumByteToRead)
  178. {
  179. I2C_Start();
  180. I2C_SendByte(EE_WR_ADDRESS(ReadAddr)); //send chip address and write command
  181. //I2C_SendByte(BSP_EEROM_WR_ADDRESS);
  182. I2C_Wait_Ack();
  183. //#if EEPROM>=32
  184. #if 1
  185. I2C_SendByte(ReadAddr>>8); //Send address high byte
  186. I2C_Wait_Ack();
  187. #endif
  188. I2C_SendByte(ReadAddr&0xff); //Send address low byte
  189. I2C_Wait_Ack();
  190. I2C_Start();
  191. I2C_SendByte(EE_RD_ADDRESS(ReadAddr)); //Send readaddress
  192. while(NumByteToRead--)
  193. {
  194. I2C_Wait_Ack();
  195. if(NumByteToRead == 1)
  196. *pBuffer++ = I2C_ReceiveByte(0);
  197. else
  198. *pBuffer++ = I2C_ReceiveByte(1);
  199. }
  200. I2C_NAck();//nACK
  201. I2C_Stop();//
  202. }
  203. #ifdef __GNUC__
  204. #pragma GCC optimize ("O0")
  205. #elif defined(__ICCARM__)
  206. #pragma optimize=none
  207. #endif
  208. void I2C_WriteMultipleBytes(uint8_t* pBuffer, uint16_t WriteAddr, uint16_t NumByteToWrite)
  209. {
  210. uint16_t i=0;
  211. uint16_t val=0;
  212. for(i=0;i<NumByteToWrite;i++)
  213. {
  214. I2C_WriteOneByte(WriteAddr+i,pBuffer[i]);
  215. Delay_ms(EEROM_DELAY);
  216. val = I2C_ReadOneByte(WriteAddr+i);
  217. if(val==pBuffer[i])
  218. {
  219. val=0;
  220. }
  221. }
  222. }
  223. #ifdef __GNUC__
  224. #pragma GCC optimize ("O0")
  225. #elif defined(__ICCARM__)
  226. #pragma optimize=none
  227. #endif
  228. void I2C_ReadMultipleBytes(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t NumByteToRead)
  229. {
  230. uint16_t i=0;
  231. for(i=0;i<NumByteToRead;i++)
  232. {
  233. pBuffer[i] = I2C_ReadOneByte(ReadAddr+i);
  234. }
  235. }
  236. void TestEEROM(void)
  237. {
  238. uint32_t startTick, curTick;
  239. int n=0;
  240. startTick = HAL_GetTick();
  241. for(n=0; n< EEROM_TEST_COUNT; n++)
  242. {
  243. //I2C_WriteOneByte(EEROM_TEST_ADDR+n, (EEROM_TEST_COUNT - n) & 0xff);
  244. I2C_WriteOneByte(EEROM_TEST_ADDR + n, (n & 0xff));
  245. //I2C_WriteOneByte(EEROM_TEST_ADDR+n, 0xa5);
  246. Delay_ms(3);
  247. }
  248. curTick = HAL_GetTick();
  249. //n=sprintf((char*)debugBuffer, "EEROM write time: %d ~ %d\r\n\r\n", startTick, curTick);
  250. //ReportMessage(debugBuffer,n);
  251. for(n=0; n<EEROM_TEST_COUNT; n++)
  252. {
  253. //debugBuffer[n] = I2C_ReadOneByte(EEROM_TEST_ADDR+n);
  254. I2C_ReadOneByte(EEROM_TEST_ADDR+n);
  255. //DebugRun();
  256. }
  257. }