stm32f0xx_i2c_cpal_hal.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_i2c_cpal_hal.c
  4. * @author MCD Application Team
  5. * @version V1.2.0
  6. * @date 24-July-2014
  7. * @brief This file provides all the CPAL_I2C_HAL (hardware Abstraction Layer)
  8. * firmware functions.
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  13. *
  14. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  15. * You may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at:
  17. *
  18. * http://www.st.com/software_license_agreement_liberty_v2
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. *
  26. ******************************************************************************
  27. */
  28. /* Includes ------------------------------------------------------------------*/
  29. #include "stm32f0xx_i2c_cpal_hal.h"
  30. /* Private typedef -----------------------------------------------------------*/
  31. /* Private defines -----------------------------------------------------------*/
  32. /* Private macro -------------------------------------------------------------*/
  33. /* Private variables ---------------------------------------------------------*/
  34. /*========= Local Structures declaration =========*/
  35. CPAL_InitTypeDef* I2C_DevStructures[CPAL_I2C_DEV_NUM] =
  36. {
  37. #ifdef CPAL_USE_I2C1
  38. &I2C1_DevStructure,
  39. #else
  40. pNULL,
  41. #endif
  42. #ifdef CPAL_USE_I2C2
  43. &I2C2_DevStructure,
  44. #else
  45. pNULL,
  46. #endif
  47. };
  48. #ifdef CPAL_USE_I2C1
  49. CPAL_InitTypeDef I2C1_DevStructure = { CPAL_I2C1, /* I2C1 device number */
  50. CPAL_DIRECTION_TXRX, /* Transmitter and Receiver direction selected */
  51. CPAL_MODE_MASTER, /* Mode Master selected */
  52. #ifdef CPAL_I2C_DMA_PROGMODEL
  53. CPAL_PROGMODEL_DMA, /* DMA Programming Model selected */
  54. #else
  55. CPAL_PROGMODEL_INTERRUPT, /* IT Programming Model selected */
  56. #endif /* CPAL_I2C_DMA_PROGMODEL */
  57. (CPAL_TransferTypeDef*) pNULL, /* Point pCPAL_TransferTx to a Null pointer */
  58. (CPAL_TransferTypeDef*) pNULL, /* Point pCPAL_TransferRx to a Null pointer */
  59. CPAL_STATE_DISABLED, /* Device Disabled */
  60. CPAL_I2C_ERR_NONE, /* No Device Error */
  61. ((uint32_t)0x00000000), /* No Options selected */
  62. ((uint32_t)CPAL_I2C_TIMEOUT_DEFAULT),/* Set timeout value to CPAL_I2C_TIMEOUT_DEFAULT */
  63. (I2C_InitTypeDef*) pNULL}; /* Point pCPAL_I2C_Struct to a Null pointer */
  64. #endif /* CPAL_USE_I2C1 */
  65. #ifdef CPAL_USE_I2C2
  66. CPAL_InitTypeDef I2C2_DevStructure = { CPAL_I2C2, /* I2C2 device number */
  67. CPAL_DIRECTION_TXRX, /* Transmitter and Receiver direction selected */
  68. CPAL_MODE_MASTER, /* Mode Master selected */
  69. #ifdef CPAL_I2C_DMA_PROGMODEL
  70. CPAL_PROGMODEL_DMA, /* DMA Programming Model selected */
  71. #else
  72. CPAL_PROGMODEL_INTERRUPT, /* IT Programming Model selected */
  73. #endif /* CPAL_I2C_DMA_PROGMODEL */
  74. (CPAL_TransferTypeDef*) pNULL, /* Point pCPAL_TransferTx to a Null pointer */
  75. (CPAL_TransferTypeDef*) pNULL, /* Point pCPAL_TransferRx to a Null pointer */
  76. CPAL_STATE_DISABLED, /* Device Disabled */
  77. CPAL_I2C_ERR_NONE, /* No Device Error */
  78. ((uint32_t)0x00000000), /* No Options selected */
  79. ((uint32_t)CPAL_I2C_TIMEOUT_DEFAULT),/* Set timeout value to CPAL_I2C_TIMEOUT_DEFAULT */
  80. (I2C_InitTypeDef*) pNULL}; /* Point pCPAL_I2C_Struct to a Null pointer */
  81. #endif /* CPAL_USE_I2C2 */
  82. DMA_InitTypeDef CPAL_DMA_InitStructure;
  83. I2C_TypeDef* CPAL_I2C_DEVICE[2] = {I2C1,I2C2};
  84. const uint32_t CPAL_I2C_CLK[2] = {CPAL_I2C1_CLK,CPAL_I2C2_CLK};
  85. const uint32_t CPAL_I2C_TXDR[2] = {CPAL_I2C1_TXDR,CPAL_I2C2_TXDR};
  86. const uint32_t CPAL_I2C_RXDR[2] = {CPAL_I2C1_RXDR,CPAL_I2C2_RXDR};
  87. const uint32_t CPAL_I2C_AF[2] = {CPAL_I2C1_AF,CPAL_I2C2_AF};
  88. const GPIO_TypeDef* CPAL_I2C_SCL_GPIO_PORT[2] = {CPAL_I2C1_SCL_GPIO_PORT,CPAL_I2C2_SCL_GPIO_PORT};
  89. const uint16_t CPAL_I2C_SCL_GPIO_PIN[2] = {CPAL_I2C1_SCL_GPIO_PIN,CPAL_I2C2_SCL_GPIO_PIN};
  90. const uint32_t CPAL_I2C_SCL_GPIO_CLK[2] = {CPAL_I2C1_SCL_GPIO_CLK,CPAL_I2C2_SCL_GPIO_CLK};
  91. const uint16_t CPAL_I2C_SCL_GPIO_PINSOURCE[2] = {CPAL_I2C1_SCL_GPIO_PINSOURCE,CPAL_I2C2_SCL_GPIO_PINSOURCE};
  92. const GPIO_TypeDef* CPAL_I2C_SDA_GPIO_PORT[2] = {CPAL_I2C1_SDA_GPIO_PORT,CPAL_I2C2_SDA_GPIO_PORT};
  93. const uint16_t CPAL_I2C_SDA_GPIO_PIN[2] = {CPAL_I2C1_SDA_GPIO_PIN,CPAL_I2C2_SDA_GPIO_PIN};
  94. const uint32_t CPAL_I2C_SDA_GPIO_CLK[2] = {CPAL_I2C1_SDA_GPIO_CLK,CPAL_I2C2_SDA_GPIO_CLK};
  95. const uint16_t CPAL_I2C_SDA_GPIO_PINSOURCE[2] = {CPAL_I2C1_SDA_GPIO_PINSOURCE,CPAL_I2C2_SDA_GPIO_PINSOURCE};
  96. const uint32_t CPAL_I2C_DMA_CLK[2] = {CPAL_I2C1_DMA_CLK,CPAL_I2C2_DMA_CLK};
  97. DMA_Channel_TypeDef* CPAL_I2C_DMA_TX_Channel[2] = {CPAL_I2C1_DMA_TX_Channel, CPAL_I2C2_DMA_TX_Channel};
  98. DMA_Channel_TypeDef* CPAL_I2C_DMA_RX_Channel[2] = {CPAL_I2C1_DMA_RX_Channel, CPAL_I2C2_DMA_RX_Channel};
  99. const IRQn_Type CPAL_I2C_DMA_IRQn[2] = {CPAL_I2C1_DMA_IRQn, CPAL_I2C2_DMA_IRQn};
  100. const IRQn_Type CPAL_I2C_IT_IRQn[2] = {CPAL_I2C1_IT_IRQn, CPAL_I2C2_IT_IRQn};
  101. const uint8_t I2C_IT_PRIO[2] = {I2C1_IT_PRIO, I2C2_IT_PRIO};
  102. const uint8_t I2C_IT_DMA_PRIO[2] = {I2C1_IT_DMA_PRIO, I2C2_IT_DMA_PRIO};
  103. DMA_TypeDef* CPAL_I2C_DMA[2] = {CPAL_I2C1_DMA,CPAL_I2C2_DMA};
  104. const uint32_t CPAL_I2C_DMA_TX_TC_FLAG[2] = {CPAL_I2C1_DMA_TX_TC_FLAG, CPAL_I2C2_DMA_TX_TC_FLAG};
  105. const uint32_t CPAL_I2C_DMA_TX_HT_FLAG[2] = {CPAL_I2C1_DMA_TX_HT_FLAG, CPAL_I2C2_DMA_TX_HT_FLAG};
  106. const uint32_t CPAL_I2C_DMA_TX_TE_FLAG[2] = {CPAL_I2C1_DMA_TX_TE_FLAG, CPAL_I2C2_DMA_TX_TE_FLAG};
  107. const uint32_t CPAL_I2C_DMA_RX_TC_FLAG[2] = {CPAL_I2C1_DMA_RX_TC_FLAG, CPAL_I2C2_DMA_RX_TC_FLAG};
  108. const uint32_t CPAL_I2C_DMA_RX_HT_FLAG[2] = {CPAL_I2C1_DMA_RX_HT_FLAG, CPAL_I2C2_DMA_RX_HT_FLAG};
  109. const uint32_t CPAL_I2C_DMA_RX_TE_FLAG[2] = {CPAL_I2C1_DMA_RX_TE_FLAG, CPAL_I2C2_DMA_RX_TE_FLAG};
  110. /* Private function prototypes -----------------------------------------------*/
  111. /* Private functions ---------------------------------------------------------*/
  112. /*================== CPAL_I2C_HAL_Config ==================*/
  113. /**
  114. * @brief Reset then enable the I2C device clock.
  115. * @param Device : I2C Device instance.
  116. * @retval None
  117. */
  118. void CPAL_I2C_HAL_CLKInit(CPAL_DevTypeDef Device)
  119. {
  120. /* Reset I2Cx device clock in order to avoid non-cleared error flags */
  121. __I2C_RCC_RESET(CPAL_I2C_CLK [Device]);
  122. /* Enable I2Cx device clock */
  123. __I2C_CLK_CMD(CPAL_I2C_CLK [Device], ENABLE);
  124. }
  125. /**
  126. * @brief Reset then disable the I2C device clock.
  127. * @param Device : I2C Device instance
  128. * @retval None.
  129. */
  130. void CPAL_I2C_HAL_CLKDeInit(CPAL_DevTypeDef Device)
  131. {
  132. /* Reset I2Cx device clock in order to avoid non-cleared error flags */
  133. __I2C_RCC_RESET(CPAL_I2C_CLK[Device]);
  134. /* Disable I2Cx device clock */
  135. __I2C_CLK_CMD(CPAL_I2C_CLK[Device], DISABLE);
  136. }
  137. /**
  138. * @brief Configure the IO pins used by the I2C device.
  139. * @param Device : I2C Device instance.
  140. * @retval None.
  141. */
  142. void CPAL_I2C_HAL_GPIOInit(CPAL_DevTypeDef Device)
  143. {
  144. GPIO_InitTypeDef GPIO_InitStructure;
  145. /* Enable I2Cx SCL and SDA Pin Clock */
  146. __I2C_GPIO_CLK_CMD((CPAL_I2C_SCL_GPIO_CLK[Device] | CPAL_I2C_SDA_GPIO_CLK[Device]), ENABLE);
  147. /* Connect PXx to I2C_SCL */
  148. GPIO_PinAFConfig((GPIO_TypeDef*)CPAL_I2C_SCL_GPIO_PORT[Device],CPAL_I2C_SCL_GPIO_PINSOURCE[Device],CPAL_I2C_AF[Device]);
  149. /* Connect PXx to I2C_SDA */
  150. GPIO_PinAFConfig((GPIO_TypeDef*)CPAL_I2C_SDA_GPIO_PORT[Device],CPAL_I2C_SDA_GPIO_PINSOURCE[Device],CPAL_I2C_AF[Device]);
  151. /* Set GPIO frequency to 50MHz */
  152. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  153. /* Select Alternate function mode */
  154. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  155. /* Select output Open Drain type */
  156. GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  157. /* Disable internal Pull-up */
  158. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  159. /* Initialize I2Cx SCL Pin */
  160. GPIO_InitStructure.GPIO_Pin = CPAL_I2C_SCL_GPIO_PIN[Device];
  161. GPIO_Init((GPIO_TypeDef*)CPAL_I2C_SCL_GPIO_PORT[Device], &GPIO_InitStructure);
  162. /* Initialize I2Cx SDA Pin */
  163. GPIO_InitStructure.GPIO_Pin = CPAL_I2C_SDA_GPIO_PIN[Device];
  164. GPIO_Init((GPIO_TypeDef*)CPAL_I2C_SDA_GPIO_PORT[Device], &GPIO_InitStructure);
  165. }
  166. /**
  167. * @brief Deinitialize the IO pins used by the I2C device
  168. * (configured to their default state).
  169. * @param Device : I2C Device instance.
  170. * @retval None.
  171. */
  172. void CPAL_I2C_HAL_GPIODeInit(CPAL_DevTypeDef Device)
  173. {
  174. GPIO_InitTypeDef GPIO_InitStructure;
  175. /* Set GPIO frequency to 50MHz */
  176. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  177. /* Select Input floating mode */
  178. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  179. /* Select output Open Drain type */
  180. GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  181. /* Disable internal Pull-up */
  182. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  183. /* Deinitialize I2Cx SCL Pin */
  184. GPIO_InitStructure.GPIO_Pin = CPAL_I2C_SCL_GPIO_PIN[Device];
  185. GPIO_Init((GPIO_TypeDef*)CPAL_I2C_SCL_GPIO_PORT[Device], &GPIO_InitStructure);
  186. /* Deinitialize I2Cx SDA Pin */
  187. GPIO_InitStructure.GPIO_Pin = CPAL_I2C_SDA_GPIO_PIN[Device];
  188. GPIO_Init((GPIO_TypeDef*)CPAL_I2C_SDA_GPIO_PORT[Device], &GPIO_InitStructure);
  189. }
  190. #ifdef CPAL_I2C_DMA_PROGMODEL
  191. /**
  192. * @brief Enable the DMA clock and initialize needed DMA Channels
  193. * used by the I2C device.
  194. * @param Device : I2C Device instance.
  195. * @param Direction : Transfer direction.
  196. * @param Options : Transfer Options.
  197. * @retval None.
  198. */
  199. void CPAL_I2C_HAL_DMAInit(CPAL_DevTypeDef Device, CPAL_DirectionTypeDef Direction, uint32_t Options)
  200. {
  201. /* Enable I2Cx DMA */
  202. __DMA_CLK_CMD(CPAL_I2C_DMA_CLK[Device], ENABLE);
  203. /* I2Cx Common Channel Configuration */
  204. CPAL_DMA_InitStructure.DMA_BufferSize = 0xFFFF;
  205. CPAL_DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  206. CPAL_DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  207. CPAL_DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte ;
  208. CPAL_DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  209. CPAL_DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
  210. CPAL_DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  211. CPAL_DMA_InitStructure.DMA_MemoryBaseAddr = 0;
  212. CPAL_DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  213. /* If TX Direction (Transmission) selected */
  214. if ((Direction & CPAL_DIRECTION_TX) != 0)
  215. {
  216. /* Select I2Cx TXDR Address register as DMA PeripheralBaseAddress */
  217. CPAL_DMA_InitStructure.DMA_PeripheralBaseAddr = CPAL_I2C_TXDR [Device];
  218. /* Select Memory to Peripheral transfer direction */
  219. CPAL_DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  220. /* Initialize I2Cx DMA Tx Channel */
  221. DMA_Init((DMA_Channel_TypeDef*)CPAL_I2C_DMA_TX_Channel[Device], &CPAL_DMA_InitStructure);
  222. }
  223. /* If RX Direction (Reception) selected */
  224. if ((Direction & CPAL_DIRECTION_RX ) != 0)
  225. {
  226. /* Select I2Cx RXDR Address register as DMA PeripheralBaseAddress */
  227. CPAL_DMA_InitStructure.DMA_PeripheralBaseAddr = CPAL_I2C_RXDR [Device];
  228. /* Select Peripheral to Memory transfer direction */
  229. CPAL_DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  230. /* Initialize I2Cx DMA Rx Channel */
  231. DMA_Init((DMA_Channel_TypeDef*)CPAL_I2C_DMA_RX_Channel[Device], &CPAL_DMA_InitStructure);
  232. }
  233. }
  234. /**
  235. * @brief Configure the DMA channel specific for TX transfer.
  236. * @param Device : I2C Device instance.
  237. * @param TXferStruct : DMA TX Transfer Parameters.
  238. * @param Options : Transfer Options.
  239. * @retval None.
  240. */
  241. void CPAL_I2C_HAL_DMATXConfig(CPAL_DevTypeDef Device, CPAL_TransferTypeDef* TxXferStruct, uint32_t Options )
  242. {
  243. /* Set Memory Base Address */
  244. CPAL_DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)(TxXferStruct->pbBuffer);
  245. /* Set number of data */
  246. CPAL_DMA_InitStructure.DMA_BufferSize = TxXferStruct->wNumData;
  247. /* Select I2Cx TXDR Address register as DMA PeripheralBaseAddress */
  248. CPAL_DMA_InitStructure.DMA_PeripheralBaseAddr = CPAL_I2C_TXDR [Device];
  249. /* If TX DMA Circular Mode Option Bit Selected */
  250. if ((Options & CPAL_OPT_DMATX_CIRCULAR) != 0)
  251. {
  252. /* Select DMA Circular Mode */
  253. CPAL_DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  254. }
  255. /* If TX DMA Circular Mode Option Bit not selected */
  256. else
  257. {
  258. /* Select DMA Normal Mode */
  259. CPAL_DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  260. }
  261. /* Select Peripheral to Memory transfer direction */
  262. CPAL_DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  263. /* Initialize I2Cx DMA Tx Channel */
  264. DMA_Init((DMA_Channel_TypeDef*)CPAL_I2C_DMA_TX_Channel[Device], &CPAL_DMA_InitStructure);
  265. }
  266. /**
  267. * @brief Configure the DMA channel specific for RX transfer.
  268. * @param Device : I2C Device instance.
  269. * @param RXferStruct : DMA RX Transfer Parameters.
  270. * @param Options : Transfer Options.
  271. * @retval None.
  272. */
  273. void CPAL_I2C_HAL_DMARXConfig(CPAL_DevTypeDef Device, CPAL_TransferTypeDef* RxXferStruct, uint32_t Options )
  274. {
  275. /* Set Memory Base Address */
  276. CPAL_DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)(RxXferStruct->pbBuffer);
  277. /* Set number of data */
  278. CPAL_DMA_InitStructure.DMA_BufferSize = RxXferStruct->wNumData;
  279. /* Select I2Cx RXDR Address register as DMA PeripheralBaseAddress */
  280. CPAL_DMA_InitStructure.DMA_PeripheralBaseAddr = CPAL_I2C_RXDR [Device];
  281. /* If RX DMA Circular Mode Option Bit Selected */
  282. if ((Options & CPAL_OPT_DMARX_CIRCULAR) != 0)
  283. {
  284. /* Select DMA Circular Mode */
  285. CPAL_DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  286. }
  287. /* If RX DMA Circular Mode Option Bit not selected */
  288. else
  289. {
  290. /* Select DMA Normal Mode */
  291. CPAL_DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  292. }
  293. /* Select Peripheral to Memory transfer direction */
  294. CPAL_DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  295. /* Initialize I2Cx DMA Rx Channel */
  296. DMA_Init((DMA_Channel_TypeDef*)CPAL_I2C_DMA_RX_Channel[Device], &CPAL_DMA_InitStructure);
  297. }
  298. /**
  299. * @brief Deinitialize the DMA channel used by I2C Device(configured to their default state).
  300. * DMA clock is not disabled.
  301. * @param Device : I2C Device instance.
  302. * @param Direction : Transfer direction.
  303. * @retval None.
  304. */
  305. void CPAL_I2C_HAL_DMADeInit(CPAL_DevTypeDef Device, CPAL_DirectionTypeDef Direction)
  306. {
  307. /* If TX Direction (Transmission) selected */
  308. if ((Direction & CPAL_DIRECTION_TX) != 0)
  309. {
  310. /* Deinitialize I2Cx DMA Tx Channel */
  311. DMA_DeInit((DMA_Channel_TypeDef*)CPAL_I2C_DMA_TX_Channel[Device]);
  312. }
  313. /* If RX Direction (Reception) selected */
  314. if ((Direction & CPAL_DIRECTION_RX) != 0)
  315. {
  316. /* Deinitialize I2Cx DMA Rx Channel */
  317. DMA_DeInit((DMA_Channel_TypeDef*)CPAL_I2C_DMA_RX_Channel[Device]);
  318. }
  319. }
  320. #endif /* CPAL_I2C_DMA_PROGMODEL */
  321. /**
  322. * @brief Configure NVIC and interrupts used by I2C Device according to
  323. * enabled options
  324. * @param Device : I2C Device instance.
  325. * @param Options : I2C Transfer Options.
  326. * @retval None.
  327. */
  328. void CPAL_I2C_HAL_ITInit(CPAL_DevTypeDef Device, uint32_t Options)
  329. {
  330. NVIC_InitTypeDef NVIC_InitStructure;
  331. /* Enable the IRQ channel */
  332. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  333. /* Configure NVIC for I2Cx Interrupt */
  334. NVIC_InitStructure.NVIC_IRQChannel = CPAL_I2C_IT_IRQn [Device] ;
  335. NVIC_InitStructure.NVIC_IRQChannelPriority = I2C_IT_PRIO[Device];
  336. NVIC_Init(&NVIC_InitStructure);
  337. /* If I2C ERR Interrupt Option Bit not selected */
  338. if ((Options & CPAL_OPT_I2C_ERRIT_DISABLE) == 0)
  339. {
  340. /* Enable I2C Error Interrupts */
  341. __CPAL_I2C_HAL_ENABLE_ERRIT(Device);
  342. }
  343. #ifdef CPAL_I2C_DMA_PROGMODEL
  344. /* If one or more DMA Interrupt option Bits selected */
  345. if (((Options & CPAL_OPT_I2C_DMA_TX_IT_MASK) != 0) || ((Options & CPAL_OPT_I2C_DMA_RX_IT_MASK) != 0))
  346. {
  347. /* Configure NVIC for DMA TX channel interrupt */
  348. NVIC_InitStructure.NVIC_IRQChannel = CPAL_I2C_DMA_IRQn [Device];
  349. NVIC_InitStructure.NVIC_IRQChannelPriority = I2C_IT_DMA_PRIO[Device];
  350. NVIC_Init(&NVIC_InitStructure);
  351. /* If DMA TX TC interrupt Option Bits Selected */
  352. if ((Options & CPAL_OPT_DMATX_TCIT) != 0)
  353. {
  354. /* Enable DMA TX Channel TCIT */
  355. __I2C_HAL_ENABLE_DMATX_TCIT(Device);
  356. }
  357. /* If DMA TX HT interrupt Option Bits Selected */
  358. if ((Options & CPAL_OPT_DMATX_HTIT) != 0)
  359. {
  360. /* Enable DMA TX Channel HTIT */
  361. __I2C_HAL_ENABLE_DMATX_HTIT(Device);
  362. }
  363. /* If DMA TX TE interrupt Option Bits Selected */
  364. if ((Options & CPAL_OPT_DMATX_TEIT) != 0)
  365. {
  366. /* Enable DMA TX Channel TEIT */
  367. __I2C_HAL_ENABLE_DMATX_TEIT(Device);
  368. }
  369. /* If DMA RX TC interrupt Option Bits Selected */
  370. if ((Options & CPAL_OPT_DMARX_TCIT) != 0)
  371. {
  372. /* Enable DMA RX Channel TCIT */
  373. __I2C_HAL_ENABLE_DMARX_TCIT(Device);
  374. }
  375. /* If DMA RX HT interrupt Option Bits Selected */
  376. if ((Options & CPAL_OPT_DMARX_HTIT) != 0)
  377. {
  378. /* Enable DMA RX Channel HTIT */
  379. __I2C_HAL_ENABLE_DMARX_HTIT(Device);
  380. }
  381. /* If DMA RX TE interrupt Option Bits Selected */
  382. if ((Options & CPAL_OPT_DMARX_TEIT) != 0)
  383. {
  384. /* Enable DMA RX Channel TEIT */
  385. __I2C_HAL_ENABLE_DMARX_TEIT(Device);
  386. }
  387. }
  388. #endif /* CPAL_I2C_DMA_PROGMODEL */
  389. }
  390. /**
  391. * @brief Deinitialize NVIC and interrupts used by I2C Device in
  392. * the current Configuration.
  393. * @param Device : I2C Device instance.
  394. * @param Options : I2C Transfer Options.
  395. * @retval None.
  396. */
  397. void CPAL_I2C_HAL_ITDeInit(CPAL_DevTypeDef Device, uint32_t Options )
  398. {
  399. NVIC_InitTypeDef NVIC_InitStructure;
  400. NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  401. /* Disable the IRQ channel */
  402. NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
  403. /* Disable I2Cx EVT IRQn */
  404. NVIC_InitStructure.NVIC_IRQChannel = CPAL_I2C_IT_IRQn [Device];
  405. NVIC_Init(&NVIC_InitStructure);
  406. #ifdef CPAL_I2C_DMA_PROGMODEL
  407. /* If one or more DMA It option Bits selected */
  408. if (((Options & CPAL_OPT_I2C_DMA_TX_IT_MASK) != 0) || ((Options & CPAL_OPT_I2C_DMA_RX_IT_MASK) != 0))
  409. {
  410. /* Disable I2Cx DMA TX IRQn */
  411. NVIC_InitStructure.NVIC_IRQChannel = CPAL_I2C_DMA_IRQn [Device];
  412. NVIC_Init(&NVIC_InitStructure);
  413. }
  414. #endif /* CPAL_I2C_DMA_PROGMODEL */
  415. }
  416. /*================== CPAL_I2C1_IRQhandler ==================*/
  417. #ifdef CPAL_USE_I2C1
  418. /**
  419. * @brief This function handles I2C1 interrupt request.
  420. * @param None.
  421. * @retval CPAL_PASS.
  422. */
  423. uint32_t I2C1_IRQHandler(void)
  424. {
  425. /* If interrupt sources is I2C event */
  426. if ((__CPAL_I2C_HAL_GET_ERROR(0) != 0) && ((I2C1_DevStructure.wCPAL_Options & CPAL_OPT_I2C_ERRIT_DISABLE) == 0))
  427. {
  428. CPAL_LOG("\n\r\n\rLOG <I2C2_ER_IRQHandler> : I2C1 Device Error IT ");
  429. /* Call the Common Error handler function */
  430. return CPAL_I2C_ER_IRQHandler(&I2C1_DevStructure);
  431. }
  432. else
  433. {
  434. /* Call the Common Event handler function */
  435. return CPAL_I2C_EV_IRQHandler(&I2C1_DevStructure);
  436. }
  437. }
  438. #ifdef CPAL_I2C_DMA_PROGMODEL
  439. /**
  440. * @brief This function handles I2C1 DMA interrupt request.
  441. * @param None.
  442. * @retval CPAL_PASS.
  443. */
  444. uint32_t CPAL_I2C1_DMA_IRQHandler(void)
  445. {
  446. /* If interrupt source is DMA RX */
  447. if (__CPAL_I2C_HAL_GET_DMARX_IT(0) != 0)
  448. {
  449. /* Call the Common DMA RX handler function */
  450. return CPAL_I2C_DMA_RX_IRQHandler(&I2C1_DevStructure);
  451. }
  452. else
  453. {
  454. /* Call the Common DMA TX handler function */
  455. return CPAL_I2C_DMA_TX_IRQHandler(&I2C1_DevStructure);
  456. }
  457. }
  458. #endif /* CPAL_I2C_DMA_PROGMODEL */
  459. #endif /* CPAL_USE_I2C1 */
  460. /*================== CPAL_I2C2_IRQhandler ==================*/
  461. #ifdef CPAL_USE_I2C2
  462. /**
  463. * @brief This function handles I2C2 interrupt request.
  464. * @param None.
  465. * @retval CPAL_PASS.
  466. */
  467. uint32_t I2C2_IRQHandler(void)
  468. {
  469. /* If interrupt sources is I2C event */
  470. if ((__CPAL_I2C_HAL_GET_ERROR(1) != 0) && ((I2C2_DevStructure.wCPAL_Options & CPAL_OPT_I2C_ERRIT_DISABLE) == 0))
  471. {
  472. CPAL_LOG("\n\r\n\rLOG <I2C2_ER_IRQHandler> : I2C2 Device Error IT ");
  473. /* Call the Common Error handler function */
  474. return CPAL_I2C_ER_IRQHandler(&I2C2_DevStructure);
  475. }
  476. else
  477. {
  478. /* Call the Common Event handler function */
  479. return CPAL_I2C_EV_IRQHandler(&I2C2_DevStructure);
  480. }
  481. }
  482. #ifdef CPAL_I2C_DMA_PROGMODEL
  483. /**
  484. * @brief This function handles I2C2 DMA interrupt request.
  485. * @param None.
  486. * @retval CPAL_PASS.
  487. */
  488. uint32_t CPAL_I2C2_DMA_IRQHandler(void)
  489. {
  490. /* If interrupt source is DMA RX */
  491. if (__CPAL_I2C_HAL_GET_DMARX_IT(1) != 0)
  492. {
  493. /* Call the Common DMA RX handler function */
  494. return CPAL_I2C_DMA_RX_IRQHandler(&I2C2_DevStructure);
  495. }
  496. else
  497. {
  498. /* Call the Common DMA TX handler function */
  499. return CPAL_I2C_DMA_TX_IRQHandler(&I2C2_DevStructure);
  500. }
  501. }
  502. #endif /* CPAL_I2C_DMA_PROGMODEL */
  503. #endif /* CPAL_USE_I2C2 */
  504. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/