driver_iic.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * @File name : driver_iic.c
  3. * @Author : Bluetrum IOT Team
  4. * @Date : 2023-02-14
  5. * @Description : This file provides functions to manage the most functionalities
  6. * of the IIC peripheral.
  7. *
  8. * Copyright (c) by Bluetrum, All Rights Reserved.
  9. */
  10. #include "driver_iic.h"
  11. /**
  12. * @brief Initializes the IIC peripheral according to the specified
  13. * parameters in the iic_init_typedef.
  14. * @param iic_init_struct: pointer to a iic_init_typedef structure that
  15. * contains the configuration information for the specified IIC peripheral.
  16. * @retval None
  17. */
  18. void iic_init(iic_init_typedef *iic_init_struct)
  19. {
  20. /*--- Configure the iic clock source ---*/
  21. CLKCON1 &= ~(1 << 23);
  22. CLKCON1 |= iic_init_struct->clk_source;
  23. /*--- Configure the iic SCL pose div conter ---*/
  24. IIC->con0 &= ~IICCON0_POSDIV;
  25. IIC->con0 |= ((iic_init_struct->scl_pose_div) << 4) & IICCON0_POSDIV;
  26. /*--- Configure the iic SDA hold cnt when SCL falling ---*/
  27. IIC->con0 &= ~IICCON0_HOLDCNT;
  28. IIC->con0 |= ((iic_init_struct->sda_hold_cnt) << 2) & IICCON0_HOLDCNT;
  29. /*--- Configure the iic controller work mode ---*/
  30. IIC->con0 &= ~IICCON0_MODE;
  31. IIC->con0 |= ((iic_init_struct->mode_sel) << 12) & IICCON0_MODE;
  32. /*--- Enable model function ---*/
  33. RSTCON0 |= (1 << 3);
  34. }
  35. /**
  36. * @brief De-initialize the specified IIC peripheral.
  37. * @retval None
  38. */
  39. void iic_deinit(void)
  40. {
  41. IIC->con0 = 0;
  42. clk_gate2_cmd(CLK_GATE2_IIC, CLK_DIS);
  43. }
  44. /**
  45. * @brief Enable or disable the specified IIC peripheral.
  46. * @param state: state of the IIC peripheral.
  47. * This parameter can be: ENABLE or DISABLE.
  48. * @retval None
  49. */
  50. void iic_cmd(FUNCTIONAL_STATE state)
  51. {
  52. if (state != DISABLE) {
  53. IIC->con0 |= IICCON0_IICEN;
  54. } else {
  55. IIC->con0 &= ~IICCON0_IICEN;
  56. }
  57. }
  58. /**
  59. * @brief Initializes the IIC control regisger according to param.
  60. * @param transmit_mode: select the iic transmit direction.
  61. * This parameter can be: IIC_SEND_MODE or IIC_RECEIVE_MODE.
  62. * @param dev_addr: the slave device address.
  63. * @param reg_addr: the address of the slave device register.
  64. * @param len: the numbers of bytes of data.
  65. * @retval None
  66. */
  67. static void iic_transmit_config(IIC_TRANSMIT_MODE transmit_mode, uint8_t dev_addr, uint16_t reg_addr, uint8_t len)
  68. {
  69. IIC->con1 = 0;
  70. IIC->con1 |= IICCON1_STOP_EN;
  71. IIC->con1 |= IICCON1_ADR0_EN;
  72. IIC->con1 |= IICCON1_CTL0_EN;
  73. IIC->con1 |= IICCON1_START0_EN;
  74. IIC->con1 |= len & IICCON1_DATA_CNT;
  75. if (reg_addr & 0xff00) {
  76. IIC->con1 |= IICCON1_ADR1_EN;
  77. }
  78. IIC->cmd_addr = (dev_addr & 0xfe) | (reg_addr << 8);
  79. if (transmit_mode == IIC_SEND_MODE) {
  80. IIC->con1 |= IICCON1_WDAT_EN;
  81. } else if (transmit_mode == IIC_RECEIVE_MODE) {
  82. IIC->con1 |= IICCON1_TXNAK_EN;
  83. IIC->con1 |= IICCON1_RDAT_EN;
  84. IIC->con1 |= IICCON1_CTL1_EN;
  85. IIC->con1 |= IICCON1_START1_EN;
  86. IIC->cmd_addr |= ((dev_addr | 0x01) << 24);
  87. }
  88. IIC->dma_cnt &= ~(0x01 << 0);
  89. }
  90. /**
  91. * @brief Send data by iic bus, this function will block program.
  92. * @param dev_addr: the slave device address.
  93. * @param reg_addr: the address of the slave device register.
  94. * @param data: the data needed to transmit.
  95. * @param len: the numbers of bytes of data. A maximum of 4 bytes cna be
  96. * transmitted at a time. So len should less than or equal to 4.
  97. * @param timeout_ms: timeout period for waiting for a result.
  98. * @retval The status of result. It can be (FAILED or SUCCESS).
  99. */
  100. STATUS_STATE iic_send_data(uint8_t dev_addr, uint16_t reg_addr, uint32_t data, uint8_t len, uint8_t timeout_ms)
  101. {
  102. iic_transmit_config(IIC_SEND_MODE, dev_addr, reg_addr, len);
  103. IIC->data = data;
  104. IIC->con0 |= IICCON0_KS;
  105. if (timeout_ms == 0) {
  106. while ((IIC->con0 & IICCON0_DONE) == RESET);
  107. IIC->con0 |= IICCON0_CLR_DONE;
  108. return SUCCESS;
  109. } else {
  110. while (timeout_ms) {
  111. if ((IIC->con0 & IICCON0_DONE) != RESET) {
  112. IIC->con0 |= IICCON0_CLR_DONE;
  113. return SUCCESS;
  114. }
  115. timeout_ms--;
  116. delay_ms(1);
  117. }
  118. }
  119. return FAILED;
  120. }
  121. AT(.com_periph.iic.dma)
  122. STATUS_STATE iic_dma_start(IIC_TRANSMIT_MODE transmit_mode, uint8_t dev_addr, uint16_t reg_addr, uint8_t *buf, uint16_t len)
  123. {
  124. if (!len) {
  125. return FAILED;
  126. }
  127. IIC->con1 = 0;
  128. IIC->con1 |= IICCON1_CTL0_EN;
  129. IIC->con1 |= IICCON1_START0_EN;
  130. IIC->con1 |= IICCON1_ADR0_EN;
  131. if (reg_addr & 0xff00) {
  132. IIC->con1 |= IICCON1_ADR1_EN;
  133. }
  134. IIC->con1 |= IICCON1_STOP_EN;
  135. IIC->con1 |= len & IICCON1_DATA_CNT;
  136. IIC->cmd_addr = (dev_addr & 0xfe) | (reg_addr << 8);
  137. /* DMA Config */
  138. IIC->dma_addr = ALIGN4_HI((u32)buf);
  139. IIC->dma_cnt = ((len - 1) << 16);
  140. IIC->dma_cnt &= ~(0x01 << 1); // dma enable
  141. IIC->dma_cnt |= (0x01 << 0); // dma enable
  142. #if 1
  143. if (transmit_mode == IIC_SEND_MODE) {
  144. /* Write Mode Config */
  145. IIC->con1 |= IICCON1_WDAT_EN;
  146. IIC->dma_cnt |= (0x01 << 1); //dma read enable
  147. } else if (transmit_mode == IIC_RECEIVE_MODE) {
  148. /* Read Mode Config */
  149. IIC->con1 |= IICCON1_CTL1_EN;
  150. IIC->con1 |= IICCON1_RDAT_EN;
  151. IIC->con1 |= IICCON1_START1_EN;
  152. IIC->con1 |= IICCON1_TXNAK_EN;
  153. IIC->cmd_addr |= (dev_addr | 0x01) << 24;
  154. }
  155. #endif
  156. IIC->con0 |= IICCON0_KS;
  157. return SUCCESS;
  158. }
  159. /**
  160. * @brief Receive data by iic bus, this function will block program.
  161. * @param dev_addr: the slave device address.
  162. * @param reg_addr: the address of the slave device.
  163. * @param len: the numbers of bytes of data.
  164. * @param timeout_ms: timeout period for waiting for a result.
  165. * @retval The status of result. It can be (FAILED or SUCCESS).
  166. */
  167. STATUS_STATE iic_receive_data(uint8_t dev_addr, uint16_t reg_addr, uint32_t *data, uint8_t len, uint8_t timeout_ms)
  168. {
  169. iic_transmit_config(IIC_RECEIVE_MODE, dev_addr, reg_addr, len);
  170. IIC->con0 |= IICCON0_KS;
  171. if (timeout_ms == 0) {
  172. while ((IIC->con0 & IICCON0_DONE) == RESET);
  173. IIC->con0 |= IICCON0_CLR_DONE;
  174. *data = IIC->data;
  175. return SUCCESS;
  176. } else {
  177. while (timeout_ms) {
  178. if ((IIC->con0 & IICCON0_DONE) != RESET) {
  179. IIC->con0 |= IICCON0_CLR_DONE;
  180. *data = IIC->data;
  181. return SUCCESS;
  182. }
  183. timeout_ms--;
  184. delay_ms(1);
  185. }
  186. }
  187. return FAILED;
  188. }
  189. /**
  190. * @brief Enable or disable the specified IIC interrupt.
  191. * @param isr: Function to be executed for service interruption.
  192. * @param pr: Priority of service interruption.
  193. * @param interrupt_type: specifies the IIC interrupt sources to be enable or disable.
  194. * this parameter can be one of the following values:
  195. * @arg IIC_IT_DONE: IIC transmit finished interrupt.
  196. * @param state: the state of the IIC peripheral.
  197. * this parameter can be: ENABLE or DISABLE.
  198. * @retval None
  199. */
  200. void iic_pic_config(isr_t isr, int pr, IIC_IT_TYPEDEF interrupt_type, FUNCTIONAL_STATE state)
  201. {
  202. uint32_t interrupt_bit = 0;
  203. uint32_t all_interrupt_type_mask = IIC_IT_DONE;
  204. if (interrupt_type == 0) {
  205. return;
  206. }
  207. if (interrupt_type & IIC_IT_DONE) {
  208. interrupt_bit |= IICCON0_INTEN;
  209. }
  210. if (state != DISABLE) {
  211. IIC->con0 |= interrupt_bit;
  212. sys_irq_init(IRQn_IIC, pr, isr);
  213. } else {
  214. IIC->con0 &= ~(uint32_t)interrupt_bit;
  215. if (interrupt_type == all_interrupt_type_mask) {
  216. PICEN &= ~BIT(IRQn_IIC);
  217. }
  218. }
  219. }
  220. /**
  221. * @brief Get the IIC interrupt pending.
  222. * @param interrupt_type: specifies the flag to get.
  223. * this parameter can be one of the following values:
  224. * @arg IIC_IT_DONE: IIC transmit finished flag.
  225. * @arg IIC_IT_ACK: IIC receive slave device ack flag.
  226. * @retval The state of iic_flag (SET or RESET).
  227. */
  228. AT(.com_periph.iic.get)
  229. FLAG_STATE iic_get_flag(IIC_IT_TYPEDEF interrupt_type)
  230. {
  231. uint32_t interrupt_bit = 0;
  232. if (interrupt_type & IIC_IT_DONE) {
  233. interrupt_bit |= IICCON0_DONE;
  234. }
  235. if (interrupt_type & IIC_IT_ACK) {
  236. interrupt_bit |= IICCON0_ACKSTATUS;
  237. }
  238. if ((IIC->con0 & interrupt_bit) != RESET) {
  239. return SET;
  240. } else {
  241. return RESET;
  242. }
  243. }
  244. /**
  245. * @brief Clear all status.
  246. * @retval None
  247. */
  248. AT(.com_periph.iic.clear)
  249. void iic_clear_all_flag(void)
  250. {
  251. IIC->con0 |= IICCON0_CLR_ALL;
  252. }
  253. /**
  254. * @brief Send NACK to the master when in slave mode.
  255. * @retval None
  256. */
  257. AT(.com_periph.iic.nack_en)
  258. void iic_send_nack_en(FUNCTIONAL_STATE state)
  259. {
  260. if (state != DISABLE) {
  261. IIC->con0 |= IICCON0_RX_NACK_EN;
  262. } else {
  263. IIC->con0 &= ~IICCON0_RX_NACK_EN;
  264. }
  265. }
  266. /**
  267. * @brief Data sample edge selection in slave mode.
  268. * @retval None
  269. */
  270. AT(.com_periph.iic.smp_sel)
  271. void iic_smp_sel(IIC_SMP_SEL_TYPEDEF edge)
  272. {
  273. if (edge != IIC_SMP_SEL_FALLING) {
  274. IIC->con0 |= IICCON0_SMP_SEL;
  275. } else {
  276. IIC->con0 &= ~IICCON0_SMP_SEL;
  277. }
  278. }