drv_sys_i2c0.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-11-11 Wayne First version
  10. *
  11. ******************************************************************************/
  12. #include <rtthread.h>
  13. #include "NuMicro.h"
  14. #define ERROR rt_kprintf
  15. #define RETRY_COUNT 3
  16. /*---------------------------------------------------------------------------------------------------------*/
  17. /* Global variables */
  18. /*---------------------------------------------------------------------------------------------------------*/
  19. static uint8_t g_u8DeviceAddr = 0xB0;
  20. static uint8_t g_uPageNum;
  21. static uint8_t g_u8RegAddr;
  22. static uint8_t g_u8WriteData;
  23. static volatile uint8_t g_u8RxData;
  24. static volatile uint8_t g_u8DataLen;
  25. static volatile uint8_t g_u8EndFlag = 0;
  26. static void I2C0_MasterRx(uint32_t u32Status)
  27. {
  28. if (u32Status == 0x08)
  29. {
  30. /* START has been transmitted and prepare SLA+W */
  31. I2C0->DAT = g_u8DeviceAddr | (g_uPageNum << 1);
  32. I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | I2C_CTL_SI;
  33. }
  34. else if (u32Status == 0x18)
  35. {
  36. /* SLA+W has been transmitted and ACK has been received */
  37. I2C0->DAT = g_u8RegAddr;
  38. I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | I2C_CTL_SI;
  39. }
  40. else if (u32Status == 0x20)
  41. {
  42. /* SLA+W has been transmitted and NACK has been received */
  43. I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | (I2C_CTL_STA | I2C_CTL_STO | I2C_CTL_SI);
  44. }
  45. else if (u32Status == 0x28)
  46. {
  47. /* DATA has been transmitted and ACK has been received */
  48. I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | (I2C_CTL_STA | I2C_CTL_SI); // repeat start
  49. }
  50. else if (u32Status == 0x10)
  51. {
  52. /* Repeat START has been transmitted and prepare SLA+R */
  53. I2C0->DAT = (g_u8DeviceAddr | (g_uPageNum << 1)) | 0x01;
  54. I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | I2C_CTL_SI;
  55. }
  56. else if (u32Status == 0x40)
  57. {
  58. /* SLA+R has been transmitted and ACK has been received */
  59. I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | I2C_CTL_SI;
  60. }
  61. else if (u32Status == 0x58)
  62. {
  63. /* DATA has been received and NACK has been returned */
  64. g_u8RxData = I2C0->DAT;
  65. I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | (I2C_CTL_STO | I2C_CTL_SI);
  66. g_u8EndFlag = 1;
  67. }
  68. else
  69. {
  70. ERROR("Status 0x%x is NOT processed\n", u32Status);
  71. }
  72. }
  73. static void I2C0_MasterTx(uint32_t u32Status)
  74. {
  75. if (u32Status == 0x08)
  76. {
  77. /* START has been transmitted */
  78. I2C0->DAT = g_u8DeviceAddr | (g_uPageNum << 1); /* Write SLA+W to Register I2CDAT */
  79. I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | I2C_CTL_SI;
  80. }
  81. else if (u32Status == 0x18)
  82. {
  83. /* SLA+W has been transmitted and ACK has been received */
  84. I2C0->DAT = g_u8RegAddr;
  85. I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | I2C_CTL_SI;
  86. }
  87. else if (u32Status == 0x20)
  88. {
  89. /* SLA+W has been transmitted and NACK has been received */
  90. I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | (I2C_CTL_STA | I2C_CTL_STO | I2C_CTL_SI);
  91. }
  92. else if (u32Status == 0x28)
  93. {
  94. /* DATA has been transmitted and ACK has been received */
  95. if ((g_u8DataLen == 0) && (g_u8EndFlag == 0))
  96. {
  97. I2C0->DAT = g_u8WriteData;
  98. I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | I2C_CTL_SI;
  99. g_u8DataLen++;
  100. }
  101. else
  102. {
  103. g_u8DataLen = 0;
  104. I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | (I2C_CTL_STO | I2C_CTL_SI);
  105. g_u8EndFlag = 1;
  106. }
  107. }
  108. else
  109. {
  110. ERROR("Status 0x%x is NOT processed\n", u32Status);
  111. }
  112. }
  113. static uint32_t ma35d1_write_i2c_data(uint32_t u32Addr, uint32_t u32Data)
  114. {
  115. uint32_t I2C_TIME_OUT_COUNT = 0x20000;
  116. uint32_t u32Status;
  117. uint32_t u32time_out = 0;
  118. g_uPageNum = 0;
  119. if (g_uPageNum == 0xff)
  120. {
  121. ERROR("\n address error !!\n");
  122. return 0;
  123. }
  124. g_u8RegAddr = u32Addr;
  125. g_u8WriteData = u32Data;
  126. g_u8EndFlag = 0x0;
  127. I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | I2C_CTL_STA;
  128. while (1)
  129. {
  130. if (I2C0->CTL0 & I2C_CTL_SI)
  131. {
  132. u32time_out = 0;
  133. u32Status = I2C0->STATUS0;
  134. I2C0_MasterTx(u32Status);
  135. }
  136. if (g_u8EndFlag)
  137. {
  138. break;
  139. }
  140. u32time_out++;
  141. if (u32time_out > I2C_TIME_OUT_COUNT)
  142. {
  143. ERROR("i2c Write Time Out!\n");
  144. return 0; // error
  145. }
  146. }
  147. return 1;
  148. }
  149. static uint32_t ma35d1_read_i2c_data(uint32_t u32Addr, uint32_t *pu32Data)
  150. {
  151. uint32_t I2C_TIME_OUT_COUNT = 0x200000;
  152. uint32_t u32Status;
  153. uint32_t u32time_out = 0;
  154. g_uPageNum = 0;
  155. if (g_uPageNum == 0xff)
  156. {
  157. ERROR("\n address error !!\n");
  158. return 0;
  159. }
  160. g_u8RegAddr = u32Addr;
  161. g_u8EndFlag = 0x0;
  162. I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | I2C_CTL_STA;
  163. while (1)
  164. {
  165. if (I2C0->CTL0 & I2C_CTL_SI)
  166. {
  167. u32time_out = 0;
  168. u32Status = I2C0->STATUS0;
  169. I2C0_MasterRx(u32Status);
  170. }
  171. if (g_u8EndFlag)
  172. {
  173. break;
  174. }
  175. u32time_out++;
  176. if (u32time_out > I2C_TIME_OUT_COUNT)
  177. {
  178. ERROR("i2c Read Time Out!\n");
  179. return 1; // error
  180. }
  181. }
  182. *pu32Data = g_u8RxData;
  183. return 2;
  184. }
  185. static uint32_t ma35d1_read_pmic_data(uint32_t u32Addr, uint32_t *pu32Data)
  186. {
  187. uint32_t j = RETRY_COUNT;
  188. while (j-- > 0)
  189. {
  190. if (ma35d1_read_i2c_data(u32Addr, pu32Data) == 2)
  191. {
  192. break;
  193. }
  194. }
  195. if (j <= 0)
  196. {
  197. ERROR("\n READ ERROR! \n");
  198. return 0;
  199. }
  200. return 1;
  201. }
  202. static uint32_t ma35d1_write_pmic_data(uint32_t u32Addr, uint32_t u32Data)
  203. {
  204. uint32_t j = RETRY_COUNT;
  205. while (j-- > 0)
  206. {
  207. if (ma35d1_write_i2c_data(u32Addr, u32Data) == 1)
  208. {
  209. break;
  210. }
  211. }
  212. if (j <= 0)
  213. {
  214. ERROR("\n WRITE ERROR [%d]! \n", j);
  215. return 0;
  216. }
  217. return 1;
  218. }
  219. static void ma35d1_i2c0_init(uint32_t sys_clk)
  220. {
  221. unsigned long clk_rate;
  222. uint32_t u32Div, speed;
  223. outpw((void *)0x40460208, inpw((void *)0x40460208) | (0x3fff << 16)); // enable GPIO clock
  224. outpw((void *)0x40460210, inpw((void *)0x40460210) | (0x1 << 0)); // I2C0 CLK
  225. outpw(0x40460098, ((inpw(0x40460098) & ~0x0f000000) | (0x6 << 24))); // PD.6 I2C0_SDA
  226. outpw(0x40460098, ((inpw(0x40460098) & ~0xf0000000) | (0x6 << 28))); // PD.7 I2C0_CLK
  227. outpw(0x400400F0, 0x5 << 12); // pull high
  228. /* i2c_clk = 100KHz */
  229. clk_rate = sys_clk;
  230. speed = 100 * 1000;
  231. /* assume speed above 1000 are Hz-specified */
  232. if (speed > 1000) speed = speed / 1000;
  233. if (speed > 400) speed = 400;
  234. u32Div = (uint32_t)(((clk_rate * 10U) / (speed * 4U) + 5U) / 10U - 1U);
  235. I2C0->CLKDIV = u32Div;
  236. I2C0->CTL0 = I2C0->CTL0 | I2C_CTL0_I2CEN_Msk; /* i2c enable */
  237. }
  238. uint32_t ma35d1_set_cpu_voltage(uint32_t sys_clk, uint32_t u32Vol)
  239. {
  240. /*
  241. RegValue = (Voltage - 0.3) / 0.01
  242. RegValue(hex) Voltage(v)
  243. 0x5B 1.22
  244. 0x5C 1.23
  245. 0x5D 1.24
  246. 0x5F 1.25
  247. 0x60 1.26
  248. 0x61 1.27
  249. 0x62 1.28
  250. 0x63 1.29
  251. 0x64 1.30
  252. 0x65 1.31
  253. 0x66 1.32
  254. 0x67 1.33
  255. 0x68 1.34
  256. 0x69 1.35
  257. */
  258. uint32_t u32Data = 0;
  259. if ((u32Vol < 0x59) || (u32Vol > 0x68))
  260. return 0;
  261. ma35d1_i2c0_init(sys_clk);
  262. ma35d1_write_pmic_data(0xA4, u32Vol);
  263. ma35d1_read_pmic_data(0xA4, &u32Data);
  264. return (u32Data == u32Vol);
  265. }