stm32f0xx_i2c_cpal.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_i2c_cpal.c
  4. * @author MCD Application Team
  5. * @version V1.2.0
  6. * @date 24-July-2014
  7. * @brief This file provides all the CPAL firmware functions for I2C peripheral.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  12. *
  13. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14. * You may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at:
  16. *
  17. * http://www.st.com/software_license_agreement_liberty_v2
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. ******************************************************************************
  26. */
  27. /* Includes ------------------------------------------------------------------*/
  28. #include "stm32f0xx_i2c_cpal.h"
  29. /* Private typedef -----------------------------------------------------------*/
  30. /* Private defines -----------------------------------------------------------*/
  31. /* Private macro -------------------------------------------------------------*/
  32. /* This macro allows to test on a flag status and to start Timeout procedure if the
  33. waiting time exceeds the allowed timeout period.
  34. @note This macro has not been implemented as a function because the entered parameter
  35. 'cmd' itself can be a macro (if it was implemented as a function, the check on the
  36. flag would be done only once, while the required behavior is to check the flag
  37. continuously).*/
  38. #define __CPAL_I2C_TIMEOUT_DETECT ((pDevInitStruct->wCPAL_Timeout == CPAL_I2C_TIMEOUT_MIN) ||\
  39. (pDevInitStruct->wCPAL_Timeout == CPAL_I2C_TIMEOUT_DEFAULT))
  40. #define __CPAL_I2C_TIMEOUT(cmd, timeout) pDevInitStruct->wCPAL_Timeout = CPAL_I2C_TIMEOUT_MIN + (timeout);\
  41. while (((cmd) == 0) && (!__CPAL_I2C_TIMEOUT_DETECT));\
  42. if (__CPAL_I2C_TIMEOUT_DETECT)\
  43. {\
  44. return CPAL_I2C_Timeout (pDevInitStruct); \
  45. }\
  46. pDevInitStruct->wCPAL_Timeout = CPAL_I2C_TIMEOUT_DEFAULT
  47. /* Private variables ---------------------------------------------------------*/
  48. /*========= Table Exported from HAL =========*/
  49. extern I2C_TypeDef* CPAL_I2C_DEVICE[];
  50. #ifdef CPAL_I2C_DMA_PROGMODEL
  51. extern DMA_TypeDef* CPAL_I2C_DMA[];
  52. extern DMA_Channel_TypeDef* CPAL_I2C_DMA_TX_Channel[];
  53. extern DMA_Channel_TypeDef* CPAL_I2C_DMA_RX_Channel[];
  54. extern const uint32_t CPAL_I2C_DMA_TX_TC_FLAG[];
  55. extern const uint32_t CPAL_I2C_DMA_RX_TC_FLAG[];
  56. extern const uint32_t CPAL_I2C_DMA_TX_HT_FLAG[];
  57. extern const uint32_t CPAL_I2C_DMA_RX_HT_FLAG[];
  58. extern const uint32_t CPAL_I2C_DMA_TX_TE_FLAG[];
  59. extern const uint32_t CPAL_I2C_DMA_RX_TE_FLAG[];
  60. #endif /* CPAL_I2C_DMA_PROGMODEL */
  61. /*========= Local structures used in CPAL_I2C_StructInit() function ==========*/
  62. I2C_InitTypeDef I2C_InitStructure;
  63. __IO uint32_t Num_Data = 0;
  64. uint32_t CR2_tmp = 0;
  65. /* Private function prototypes -----------------------------------------------*/
  66. /*========= Local Master events handlers =========*/
  67. #ifdef CPAL_I2C_MASTER_MODE
  68. static uint32_t I2C_MASTER_TCR_Handle(CPAL_InitTypeDef* pDevInitStruct); /* Handle Master TCR Interrupt event */
  69. static uint32_t I2C_MASTER_TC_Handle(CPAL_InitTypeDef* pDevInitStruct); /* Handle Master TC Interrupt event */
  70. static uint32_t I2C_MASTER_STOP_Handle(CPAL_InitTypeDef* pDevInitStruct); /* Handle Master STOP Interrupt event */
  71. static uint32_t I2C_MASTER_NACK_Handle(CPAL_InitTypeDef* pDevInitStruct); /* Handle Master NACK Interrupt event */
  72. #ifdef CPAL_I2C_IT_PROGMODEL
  73. static uint32_t I2C_MASTER_TXIS_Handle(CPAL_InitTypeDef* pDevInitStruct); /* Handle Master TXIS Interrupt event */
  74. static uint32_t I2C_MASTER_RXNE_Handle(CPAL_InitTypeDef* pDevInitStruct); /* Handle Master RXNE Interrupt event */
  75. #endif /* CPAL_I2C_IT_PROGMODEL */
  76. #endif /* CPAL_I2C_MASTER_MODE */
  77. /*========= Local Slave events handlers =========*/
  78. #ifdef CPAL_I2C_SLAVE_MODE
  79. static uint32_t I2C_SLAVE_ADDR_Handle(CPAL_InitTypeDef* pDevInitStruct); /* Handle Slave ADDR Interrupt event */
  80. static uint32_t I2C_SLAVE_STOP_Handle(CPAL_InitTypeDef* pDevInitStruct); /* Handle Slave STOPF Interrupt event */
  81. static uint32_t I2C_SLAVE_NACK_Handle(CPAL_InitTypeDef* pDevInitStruct); /* Handle Slave NACK Interrupt event */
  82. #ifdef CPAL_I2C_IT_PROGMODEL
  83. static uint32_t I2C_SLAVE_TXIS_Handle(CPAL_InitTypeDef* pDevInitStruct); /* Handle Slave TXIS Interrupt event */
  84. static uint32_t I2C_SLAVE_RXNE_Handle(CPAL_InitTypeDef* pDevInitStruct); /* Handle Slave RXNE Interrupt event */
  85. #endif /* CPAL_I2C_IT_PROGMODEL */
  86. #endif /* CPAL_I2C_SLAVE_MODE */
  87. #ifdef CPAL_I2C_DMA_PROGMODEL
  88. /*========= Local DMA Manager =========*/
  89. static uint32_t I2C_Enable_DMA (CPAL_InitTypeDef* pDevInitStruct, CPAL_DirectionTypeDef Direction);
  90. #endif /* CPAL_I2C_DMA_PROGMODEL */
  91. /*========= CPAL Timeout handler =========*/
  92. static uint32_t CPAL_I2C_Timeout (CPAL_InitTypeDef* pDevInitStruct);
  93. /* Private functions ---------------------------------------------------------*/
  94. /*================== USER CPAL Functions ==================*/
  95. /**
  96. * @brief Initialize the peripheral and all related clocks, GPIOs, DMA and
  97. * Interrupts according to the specified parameters in the
  98. * CPAL_InitTypeDef structure.
  99. * @param pDevInitStruct : Pointer to the peripheral configuration structure.
  100. * @retval CPAL_PASS or CPAL_FAIL
  101. */
  102. uint32_t CPAL_I2C_Init(CPAL_InitTypeDef* pDevInitStruct)
  103. {
  104. CPAL_LOG("\n\r\n\rLOG <CPAL_I2C_Init> : I2C Device Init");
  105. /* If CPAL_State is not BUSY */
  106. if ((pDevInitStruct->CPAL_State == CPAL_STATE_READY)
  107. || (pDevInitStruct->CPAL_State == CPAL_STATE_ERROR)
  108. || (pDevInitStruct->CPAL_State == CPAL_STATE_DISABLED))
  109. {
  110. /*
  111. - If CPAL_State is CPAL_STATE_ERROR (an Error occurred in transaction):
  112. Perform the initialization routines (device will be deinitialized during initialization).
  113. - If CPAL_State is CPAL_STATE_READY:
  114. Perform the initialization routines
  115. - If CPAL_State is CPAL_STATE_DISABLED:
  116. Perform the Initialization routines */
  117. #ifndef CPAL_I2C_DMA_PROGMODEL
  118. if (pDevInitStruct->CPAL_ProgModel == CPAL_PROGMODEL_DMA)
  119. {
  120. /* Update CPAL_State to CPAL_STATE_ERROR */
  121. pDevInitStruct->CPAL_State = CPAL_STATE_ERROR;
  122. /* Exit Init function */
  123. return CPAL_FAIL;
  124. }
  125. #endif /* CPAL_I2C_DMA_PROGMODEL */
  126. #ifndef CPAL_I2C_IT_PROGMODEL
  127. if (pDevInitStruct->CPAL_ProgModel == CPAL_PROGMODEL_INTERRUPT)
  128. {
  129. /* Update CPAL_State to CPAL_STATE_ERROR */
  130. pDevInitStruct->CPAL_State = CPAL_STATE_ERROR;
  131. /* Exit Init function */
  132. return CPAL_FAIL;
  133. }
  134. #endif /* CPAL_I2C_IT_PROGMODEL */
  135. /* Disable I2Cx device */
  136. __CPAL_I2C_HAL_DISABLE_DEV(pDevInitStruct->CPAL_Dev);
  137. CPAL_LOG("\n\rLOG : I2C Device Disabled");
  138. /* Deinitialize I2Cx GPIO */
  139. CPAL_I2C_HAL_GPIODeInit(pDevInitStruct->CPAL_Dev);
  140. CPAL_LOG("\n\rLOG : I2C Device IOs Deinit");
  141. /* Deinitialize I2Cx clock */
  142. CPAL_I2C_HAL_CLKDeInit(pDevInitStruct->CPAL_Dev);
  143. CPAL_LOG("\n\rLOG : I2C Device Clock Deinit");
  144. #ifdef CPAL_I2C_DMA_PROGMODEL
  145. /* Deinitialize DMA Channels */
  146. if (pDevInitStruct->CPAL_ProgModel == CPAL_PROGMODEL_DMA)
  147. {
  148. CPAL_I2C_HAL_DMADeInit(pDevInitStruct->CPAL_Dev, pDevInitStruct->CPAL_Direction);
  149. CPAL_LOG("\n\rLOG : I2C Device DMA Deinit");
  150. }
  151. #endif /* CPAL_I2C_DMA_PROGMODEL */
  152. /*--------------------------------------------------------------------------
  153. GPIO pins configuration
  154. ---------------------------------------------------------------------------*/
  155. /* Initialize I2Cx GPIO */
  156. CPAL_I2C_HAL_GPIOInit(pDevInitStruct->CPAL_Dev);
  157. CPAL_LOG("\n\rLOG : I2C Device IOs Init");
  158. /*--------------------------------------------------------------------------
  159. Peripheral Clock Initialization
  160. ---------------------------------------------------------------------------*/
  161. /* Initialize I2Cx clock */
  162. CPAL_I2C_HAL_CLKInit(pDevInitStruct->CPAL_Dev);
  163. CPAL_LOG("\n\rLOG : I2C Device Clock Init");
  164. /*--------------------------------------------------------------------------
  165. Peripheral Initialization
  166. ---------------------------------------------------------------------------*/
  167. /* Initialize I2Cx device with parameters stored in pCPAL_I2C_Struct */
  168. I2C_Init(CPAL_I2C_DEVICE[pDevInitStruct->CPAL_Dev], pDevInitStruct->pCPAL_I2C_Struct);
  169. CPAL_LOG("\n\rLOG : I2C Device Config");
  170. /* If General Call mode option bit selected */
  171. if ((pDevInitStruct->wCPAL_Options & CPAL_OPT_I2C_GENCALL) != 0)
  172. {
  173. /* Enable GENERAL CALL address mode */
  174. __CPAL_I2C_HAL_ENABLE_GENCALL(pDevInitStruct->CPAL_Dev);
  175. CPAL_LOG("\n\rLOG : I2C Device GENCALL Mode Enabled");
  176. }
  177. /* If OA2 Address mode option bit selected */
  178. if ((pDevInitStruct->wCPAL_Options & CPAL_OPT_I2C_DUALADDR) != 0)
  179. {
  180. /* Configure OA2 */
  181. __CPAL_I2C_HAL_OA2_CONF(pDevInitStruct->CPAL_Dev, (uint32_t)(pDevInitStruct->wCPAL_Options & 0x000000FE));
  182. /* Configure OA2 masks */
  183. __CPAL_I2C_HAL_OA2_MASK_CONF(pDevInitStruct->CPAL_Dev, (uint32_t)((pDevInitStruct->wCPAL_Options & CPAL_OPT_I2C_OA2_MASK) >> 25));
  184. /* Enable OA2 address mode */
  185. __CPAL_I2C_HAL_ENABLE_OA2(pDevInitStruct->CPAL_Dev);
  186. CPAL_LOG("\n\rLOG : I2C Device OA2 ADDR Mode Enabled");
  187. }
  188. /* If WakeUp from STOP option bit selected */
  189. if ((pDevInitStruct->wCPAL_Options & CPAL_OPT_I2C_WAKEUP_STOP) != 0)
  190. {
  191. /* Enable WakeUp from STOP mode */
  192. __CPAL_I2C_HAL_ENABLE_WAKEUP(pDevInitStruct->CPAL_Dev);
  193. CPAL_LOG("\n\rLOG : I2C Device WakeUp from Stop Mode Enabled");
  194. }
  195. else
  196. {
  197. /* Disable WakeUp from STOP mode */
  198. __CPAL_I2C_HAL_DISABLE_WAKEUP(pDevInitStruct->CPAL_Dev);
  199. }
  200. /* If NACK Slave Own Address option bit selected */
  201. if ((pDevInitStruct->wCPAL_Options & CPAL_OPT_I2C_NACK_ADD) != 0)
  202. {
  203. /* Disable Acknowledgement of own Address */
  204. __CPAL_I2C_HAL_DISABLE_DEV(pDevInitStruct->CPAL_Dev);
  205. CPAL_LOG("\n\rLOG : I2C Device NACK Own Address Mode Enabled");
  206. }
  207. #ifdef CPAL_I2C_DMA_PROGMODEL
  208. /*----------------------------------------------------------------------------
  209. DMA Initialization :
  210. ---------------------------------------------------------------------------*/
  211. /* If DMA Programming model is selected*/
  212. if (pDevInitStruct->CPAL_ProgModel == CPAL_PROGMODEL_DMA)
  213. {
  214. /* Initialize I2Cx DMA channels */
  215. CPAL_I2C_HAL_DMAInit(pDevInitStruct->CPAL_Dev, pDevInitStruct->CPAL_Direction, pDevInitStruct->wCPAL_Options);
  216. CPAL_LOG("\n\rLOG : I2C Device DMA Init");
  217. }
  218. #endif /* CPAL_I2C_DMA_PROGMODEL */
  219. /*----------------------------------------------------------------------------
  220. Peripheral and DMA interrupts Initialization
  221. ---------------------------------------------------------------------------*/
  222. /* Initialize I2Cx interrupts */
  223. CPAL_I2C_HAL_ITInit(pDevInitStruct->CPAL_Dev, pDevInitStruct->wCPAL_Options);
  224. CPAL_LOG("\n\rLOG : I2C Device IT Init");
  225. /* Update CPAL_State to CPAL_STATE_READY */
  226. pDevInitStruct->CPAL_State = CPAL_STATE_READY;
  227. CPAL_LOG("\n\rLOG : I2C Device Ready");
  228. /* Initialize Timeout procedure */
  229. _CPAL_TIMEOUT_INIT();
  230. return CPAL_PASS;
  231. }
  232. /* If CPAL_State is BUSY (a transaction is still on going) exit Init function */
  233. else
  234. {
  235. CPAL_LOG("\n\rERROR : I2C Device Busy");
  236. return CPAL_FAIL;
  237. }
  238. }
  239. /**
  240. * @brief Deinitialize the peripheral and all related clocks, GPIOs, DMA and NVIC
  241. * to their reset values.
  242. * @param pDevInitStruct: Pointer to the peripheral configuration structure.
  243. * @retval CPAL_PASS or CPAL_FAIL
  244. * @note The Peripheral clock is disabled but the GPIO Ports clocks remains
  245. * enabled after this deinitialization.
  246. */
  247. uint32_t CPAL_I2C_DeInit(CPAL_InitTypeDef* pDevInitStruct)
  248. {
  249. CPAL_LOG("\n\r\n\rLOG <CPAL_I2C_DeInit> : I2C Device Deinit");
  250. /* If CPAL_State is not BUSY */
  251. if ((pDevInitStruct->CPAL_State == CPAL_STATE_READY)
  252. || (pDevInitStruct->CPAL_State == CPAL_STATE_ERROR)
  253. || (pDevInitStruct->CPAL_State == CPAL_STATE_DISABLED))
  254. {
  255. /*
  256. - If CPAL_State is CPAL_STATE_ERROR (an Error occurred in transaction):
  257. Perform the deinitialization routines
  258. - If CPAL_State is CPAL_STATE_READY:
  259. Perform the deinitialization routines
  260. - If CPAL_State is CPAL_STATE_DISABLED:
  261. Perform the deinitialization routines */
  262. /*--------------------------------------------------------------------------
  263. GPIO pins Deinitialization
  264. Note: The GPIO clock remains enabled after this deinitialization
  265. ---------------------------------------------------------------------------*/
  266. /* Deinitialize I2Cx GPIO */
  267. CPAL_I2C_HAL_GPIODeInit(pDevInitStruct->CPAL_Dev);
  268. CPAL_LOG("\n\rLOG : I2C Device IOs Deinit");
  269. /*--------------------------------------------------------------------------
  270. Peripheral Deinitialization
  271. ---------------------------------------------------------------------------*/
  272. /* Disable I2Cx device */
  273. __CPAL_I2C_HAL_DISABLE_DEV(pDevInitStruct->CPAL_Dev);
  274. CPAL_LOG("\n\rLOG : I2C Device Disabled");
  275. /*--------------------------------------------------------------------------
  276. Peripheral Clock Deinitialization
  277. ---------------------------------------------------------------------------*/
  278. /* Deinitialize I2Cx clock */
  279. CPAL_I2C_HAL_CLKDeInit(pDevInitStruct->CPAL_Dev);
  280. CPAL_LOG("\n\rLOG : I2C Device Clock Deinit");
  281. #ifdef CPAL_I2C_DMA_PROGMODEL
  282. /*--------------------------------------------------------------------------
  283. DMA Deinitialization : if DMA Programming model is selected
  284. ---------------------------------------------------------------------------*/
  285. if (pDevInitStruct->CPAL_ProgModel == CPAL_PROGMODEL_DMA)
  286. {
  287. CPAL_I2C_HAL_DMADeInit(pDevInitStruct->CPAL_Dev, pDevInitStruct->CPAL_Direction);
  288. CPAL_LOG("\n\rLOG : I2C Device DMA Deinit");
  289. }
  290. #endif /* CPAL_I2C_DMA_PROGMODEL */
  291. /*--------------------------------------------------------------------------
  292. Interrupts Deinitialization
  293. ---------------------------------------------------------------------------*/
  294. CPAL_I2C_HAL_ITDeInit(pDevInitStruct->CPAL_Dev, pDevInitStruct->wCPAL_Options);
  295. CPAL_LOG("\n\rLOG : I2C Device IT Deinit");
  296. /*--------------------------------------------------------------------------
  297. Structure fields initialization
  298. ----------------------------------------------------------------------------*/
  299. /* Initialize pDevInitStruct state parameters to their default values */
  300. pDevInitStruct-> CPAL_State = CPAL_STATE_DISABLED; /* Device Disabled */
  301. pDevInitStruct-> wCPAL_DevError = CPAL_I2C_ERR_NONE; /* No Device Error */
  302. pDevInitStruct-> wCPAL_Timeout = ((uint32_t)CPAL_I2C_TIMEOUT_DEFAULT); /* Set timeout value to CPAL_I2C_TIMEOUT_DEFAULT */
  303. CPAL_LOG("\n\rLOG :Set State fields to default");
  304. /*----------------------------------------------------------------------------
  305. Deinitialize Timeout Procedure
  306. -----------------------------------------------------------------------------*/
  307. _CPAL_TIMEOUT_DEINIT();
  308. return CPAL_PASS;
  309. }
  310. /* If CPAL_State is BUSY (a transaction is still on going) Exit Init function */
  311. else
  312. {
  313. CPAL_LOG("\n\rERROR : I2C Device Busy");
  314. return CPAL_FAIL;
  315. }
  316. }
  317. /**
  318. * @brief Initialize the peripheral structure with default values according
  319. * to the specified parameters in the CPAL_I2CDevTypeDef structure.
  320. * @param pDevInitStruct: Pointer to the peripheral configuration structure.
  321. * @retval CPAL_PASS or CPAL_FAIL.
  322. */
  323. uint32_t CPAL_I2C_StructInit(CPAL_InitTypeDef* pDevInitStruct)
  324. {
  325. /* Initialize I2C_InitStructure to their default values */
  326. I2C_InitStructure.I2C_Timing = 0; /* Initialize the I2C_Timing member */
  327. I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; /* Initialize the I2C_Mode member */
  328. I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable; /* Initialize the I2C_AnalogFilter member */
  329. I2C_InitStructure.I2C_DigitalFilter = 0x00; /* Initialize the I2C_DigitalFilter member */
  330. I2C_InitStructure.I2C_OwnAddress1 = 0; /* Initialize the I2C_OwnAddress1 member */
  331. I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; /* Initialize the I2C_Ack member */
  332. I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; /* Initialize the I2C_AcknowledgedAddress member */
  333. /* Initialize pDevInitStruct parameter to their default values */
  334. pDevInitStruct->CPAL_Direction = CPAL_DIRECTION_TXRX; /* Transmitter and Receiver direction selected */
  335. pDevInitStruct->CPAL_Mode = CPAL_MODE_MASTER; /* Mode Master selected */
  336. pDevInitStruct->CPAL_ProgModel = CPAL_PROGMODEL_DMA; /* DMA Programming Model selected */
  337. pDevInitStruct->pCPAL_TransferTx = pNULL; /* Point pCPAL_TransferTx to a Null pointer */
  338. pDevInitStruct->pCPAL_TransferRx = pNULL; /* Point pCPAL_TransferRx to a Null pointer */
  339. pDevInitStruct->CPAL_State = CPAL_STATE_DISABLED; /* Device Disabled */
  340. pDevInitStruct->wCPAL_DevError = CPAL_I2C_ERR_NONE; /* No Device Error */
  341. pDevInitStruct->wCPAL_Options = ((uint32_t)0x00000000); /* No Options selected */
  342. pDevInitStruct->wCPAL_Timeout = ((uint32_t)CPAL_I2C_TIMEOUT_DEFAULT); /* Set timeout value to CPAL_I2C_TIMEOUT_DEFAULT */
  343. pDevInitStruct->pCPAL_I2C_Struct = &I2C_InitStructure; /* Point to I2C_InitStructure (with default values) */
  344. CPAL_LOG("\n\r\n\rLOG <CPAL_I2C_StructInit> : I2C Device Structure set to Default Value");
  345. return CPAL_PASS;
  346. }
  347. #if defined (CPAL_I2C_MASTER_MODE) || ! defined (CPAL_I2C_LISTEN_MODE)
  348. /**
  349. * @brief Allows to send a data or a buffer of data through the peripheral to
  350. * a selected device in a selected location address.
  351. * @param pDevInitStruct: Pointer to the peripheral configuration structure.
  352. * @retval CPAL_PASS or CPAL_FAIL.
  353. */
  354. uint32_t CPAL_I2C_Write(CPAL_InitTypeDef* pDevInitStruct)
  355. {
  356. CR2_tmp = 0;
  357. /* Check I2C State:
  358. - If busy --> exit Write operation
  359. - If disabled --> exit Write operation
  360. - If error --> exit Write operation
  361. - If ready -->
  362. *- Update CPAL_State to CPAL_STATE_BUSY
  363. *- If CPAL_OPT_I2C_NOSTOP_MODE is not selected :
  364. - Check if device is busy.
  365. *- Update CPAL_State to CPAL_STATE_READY_TX
  366. *- If DMA Prog Model :
  367. - Configure and enable DMA
  368. *- If Master mode :
  369. - If 10Bit Mode : Enable ADD10
  370. - Configure Slave address
  371. *- If Memory Address mode (master)
  372. - Send target and memory address
  373. *- Update CPAL_State to CPAL_STATE_BUSY_TX
  374. *- If Master mode :
  375. - Configure AUTOEND, RELOAD and NBYTES
  376. - If Interrupt Prog Model :
  377. - Generate start and enable interrupts
  378. - If DMA Prog Model :
  379. - Enable TX DMA request
  380. - Generate start and enable interrupts
  381. *- If Slave mode :
  382. - If Interrupt Prog Model :
  383. - Enable interrupts
  384. - If DMA Prog Model :
  385. - Enable TX DMA request
  386. - Enable interrupts */
  387. CPAL_LOG("\n\r\n\rLOG <CPAL_I2C_Write> : I2C Device Write OP");
  388. /* If Device is Busy (a transaction is still on going) Exit Write function */
  389. if (((pDevInitStruct->CPAL_State & CPAL_STATE_BUSY) != 0)
  390. || (pDevInitStruct->CPAL_State == CPAL_STATE_READY_TX)
  391. || (pDevInitStruct->CPAL_State == CPAL_STATE_READY_RX))
  392. {
  393. CPAL_LOG("\n\rERROR : I2C Device Busy");
  394. return CPAL_FAIL;
  395. }
  396. /* If CPAL_State is CPAL_STATE_DISABLED (device is not initialized) Exit Write function */
  397. else if (pDevInitStruct->CPAL_State == CPAL_STATE_DISABLED)
  398. {
  399. CPAL_LOG("\n\rERROR : I2C Device Not Initialized");
  400. return CPAL_FAIL;
  401. }
  402. /* If CPAL_State is CPAL_STATE_ERROR (Error occurred ) */
  403. else if (pDevInitStruct->CPAL_State == CPAL_STATE_ERROR)
  404. {
  405. CPAL_LOG("\n\rERROR : I2C Device Error");
  406. return CPAL_FAIL;
  407. }
  408. /* If CPAL_State is CPAL_STATE_READY ( Start Communication )*/
  409. else
  410. {
  411. /* Update CPAL_State to CPAL_STATE_BUSY */
  412. pDevInitStruct->CPAL_State = CPAL_STATE_BUSY;
  413. /* IF CPAL_OPT_I2C_NOSTOP_MODE is not selected */
  414. if ((pDevInitStruct->wCPAL_Options & CPAL_OPT_I2C_NOSTOP_MODE) == 0)
  415. {
  416. /* Wait until BUSY flag is reset */
  417. __CPAL_I2C_TIMEOUT(!(__CPAL_I2C_HAL_GET_BUSY(pDevInitStruct->CPAL_Dev)), CPAL_I2C_TIMEOUT_BUSY);
  418. }
  419. /* Update CPAL_State to CPAL_STATE_READY_TX */
  420. pDevInitStruct->CPAL_State = CPAL_STATE_READY_TX;
  421. CPAL_LOG("\n\rLOG : I2C Device Ready TX");
  422. #ifdef CPAL_I2C_DMA_PROGMODEL
  423. /* If DMA programming model */
  424. if (pDevInitStruct->CPAL_ProgModel == CPAL_PROGMODEL_DMA)
  425. {
  426. /* Configure and enable TX DMA channel */
  427. I2C_Enable_DMA(pDevInitStruct, CPAL_DIRECTION_TX);
  428. }
  429. #endif /* CPAL_I2C_DMA_PROGMODEL */
  430. #ifdef CPAL_I2C_MASTER_MODE
  431. /* If master mode selected */
  432. if (pDevInitStruct->CPAL_Mode == CPAL_MODE_MASTER)
  433. {
  434. #ifdef CPAL_I2C_10BIT_ADDR_MODE
  435. /* If 10 Bit addressing mode */
  436. if (pDevInitStruct->pCPAL_I2C_Struct->I2C_AcknowledgedAddress == I2C_AcknowledgedAddress_10bit)
  437. {
  438. /* Enable 10Bit addressing mode */
  439. CR2_tmp |= I2C_CR2_ADD10;
  440. }
  441. #endif /* CPAL_I2C_10BIT_ADDR_MODE */
  442. /* Configure slave address */
  443. CR2_tmp |= (uint32_t)((pDevInitStruct->pCPAL_TransferTx->wAddr1) & 0x000003FF);
  444. }
  445. #ifdef CPAL_I2C_MEM_ADDR
  446. /* If CPAL_OPT_NO_MEM_ADDR is not selected and master mode selected */
  447. if (((pDevInitStruct->wCPAL_Options & CPAL_OPT_NO_MEM_ADDR) == 0)
  448. && (pDevInitStruct->CPAL_Mode == CPAL_MODE_MASTER ))
  449. {
  450. CPAL_LOG("\n\rLOG : I2C Device Master Mem Addr Mode");
  451. /* Enable reload */
  452. CR2_tmp |= I2C_CR2_RELOAD;
  453. /* Disable error interrupt to manage error without interrupt */
  454. __CPAL_I2C_HAL_DISABLE_ERRIT(pDevInitStruct->CPAL_Dev);
  455. /* If 8 Bit register mode */
  456. if ((pDevInitStruct->wCPAL_Options & CPAL_OPT_16BIT_REG) == 0)
  457. {
  458. /* Configure Nbytes */
  459. CR2_tmp |= (uint32_t)((uint32_t)(1) << 16);
  460. /* Update CR2 Register */
  461. __CPAL_I2C_HAL_CR2_UPDATE(pDevInitStruct->CPAL_Dev, CR2_tmp);
  462. /* Generate start */
  463. __CPAL_I2C_HAL_START(pDevInitStruct->CPAL_Dev);
  464. /* Wait until TXIS flag is set or NACK is set */
  465. __CPAL_I2C_TIMEOUT((__CPAL_I2C_HAL_GET_TXIS(pDevInitStruct->CPAL_Dev) || __CPAL_I2C_HAL_GET_NACK(pDevInitStruct->CPAL_Dev)), CPAL_I2C_TIMEOUT_TXIS);
  466. /* If acknowledge failure detected generate stop and abort communication */
  467. if (__CPAL_I2C_HAL_GET_NACK(pDevInitStruct->CPAL_Dev))
  468. {
  469. /* Generate stop if autoend option is disabled */
  470. if((CPAL_I2C_DEVICE[(pDevInitStruct->CPAL_Dev)]->CR2 & I2C_CR2_AUTOEND) != I2C_CR2_AUTOEND)
  471. {
  472. /* Generate stop */
  473. __CPAL_I2C_HAL_STOP(pDevInitStruct->CPAL_Dev);
  474. }
  475. /* Clear NACK flag */
  476. __CPAL_I2C_HAL_CLEAR_NACK(pDevInitStruct->CPAL_Dev);
  477. /* Wait until STOP flag is set */
  478. __CPAL_I2C_TIMEOUT(__CPAL_I2C_HAL_GET_STOP(pDevInitStruct->CPAL_Dev), CPAL_I2C_TIMEOUT_BUSY);
  479. /* Clear STOP flag */
  480. __CPAL_I2C_HAL_CLEAR_STOP(pDevInitStruct->CPAL_Dev);
  481. /* Update wCPAL_DevError */
  482. pDevInitStruct->wCPAL_DevError = CPAL_I2C_ERR_AF;
  483. /* Set CPAL_State to CPAL_STATE_ERROR */
  484. pDevInitStruct->CPAL_State = CPAL_STATE_ERROR;
  485. return CPAL_FAIL;
  486. }
  487. /* Send register address */
  488. __CPAL_I2C_HAL_SEND(pDevInitStruct->CPAL_Dev, (uint8_t)(pDevInitStruct->pCPAL_TransferTx->wAddr2));
  489. }
  490. #ifdef CPAL_16BIT_REG_OPTION
  491. /* If 16 Bit register mode */
  492. else
  493. {
  494. /* Configure Nbytes */
  495. CR2_tmp |= (uint32_t)((uint32_t)(2) << 16);
  496. /* Update CR2 Register */
  497. __CPAL_I2C_HAL_CR2_UPDATE(pDevInitStruct->CPAL_Dev, CR2_tmp);
  498. /* Generate start */
  499. __CPAL_I2C_HAL_START(pDevInitStruct->CPAL_Dev);
  500. /* Wait until TXIS flag is set or NACK is set */
  501. __CPAL_I2C_TIMEOUT((__CPAL_I2C_HAL_GET_TXIS(pDevInitStruct->CPAL_Dev) || __CPAL_I2C_HAL_GET_NACK(pDevInitStruct->CPAL_Dev)), CPAL_I2C_TIMEOUT_TXIS);
  502. /* If acknowledge failure detected generate stop and abort communication */
  503. if (__CPAL_I2C_HAL_GET_NACK(pDevInitStruct->CPAL_Dev))
  504. {
  505. /* Generate stop if autoend option is disabled */
  506. if((CPAL_I2C_DEVICE[(pDevInitStruct->CPAL_Dev)]->CR2 & I2C_CR2_AUTOEND) != I2C_CR2_AUTOEND)
  507. {
  508. /* Generate stop */
  509. __CPAL_I2C_HAL_STOP(pDevInitStruct->CPAL_Dev);
  510. }
  511. /* Clear NACK flag */
  512. __CPAL_I2C_HAL_CLEAR_NACK(pDevInitStruct->CPAL_Dev);
  513. /* Wait until STOP flag is set */
  514. __CPAL_I2C_TIMEOUT(__CPAL_I2C_HAL_GET_STOP(pDevInitStruct->CPAL_Dev), CPAL_I2C_TIMEOUT_BUSY);
  515. /* Clear STOP flag */
  516. __CPAL_I2C_HAL_CLEAR_STOP(pDevInitStruct->CPAL_Dev);
  517. /* Update wCPAL_DevError */
  518. pDevInitStruct->wCPAL_DevError = CPAL_I2C_ERR_AF;
  519. /* Set CPAL_State to CPAL_STATE_ERROR */
  520. pDevInitStruct->CPAL_State = CPAL_STATE_ERROR;
  521. return CPAL_FAIL;
  522. }
  523. /* Send register address (MSB) */
  524. __CPAL_I2C_HAL_SEND(pDevInitStruct->CPAL_Dev, (uint8_t)(((pDevInitStruct->pCPAL_TransferTx->wAddr2)& 0xFF00) >> 8));
  525. /* Wait until TXIS flag is set or NACK is set */
  526. __CPAL_I2C_TIMEOUT((__CPAL_I2C_HAL_GET_TXIS(pDevInitStruct->CPAL_Dev) || __CPAL_I2C_HAL_GET_NACK(pDevInitStruct->CPAL_Dev)), CPAL_I2C_TIMEOUT_TXIS);
  527. /* If acknowledge failure detected generate stop and abort communication */
  528. if (__CPAL_I2C_HAL_GET_NACK(pDevInitStruct->CPAL_Dev))
  529. {
  530. /* Generate stop if autoend option is disabled */
  531. if((CPAL_I2C_DEVICE[(pDevInitStruct->CPAL_Dev)]->CR2 & I2C_CR2_AUTOEND) != I2C_CR2_AUTOEND)
  532. {
  533. /* Generate stop */
  534. __CPAL_I2C_HAL_STOP(pDevInitStruct->CPAL_Dev);
  535. }
  536. /* Clear NACK flag */
  537. __CPAL_I2C_HAL_CLEAR_NACK(pDevInitStruct->CPAL_Dev);
  538. /* Wait until STOP flag is set */
  539. __CPAL_I2C_TIMEOUT(__CPAL_I2C_HAL_GET_STOP(pDevInitStruct->CPAL_Dev), CPAL_I2C_TIMEOUT_BUSY);
  540. /* Clear STOP flag */
  541. __CPAL_I2C_HAL_CLEAR_STOP(pDevInitStruct->CPAL_Dev);
  542. /* Update wCPAL_DevError */
  543. pDevInitStruct->wCPAL_DevError = CPAL_I2C_ERR_AF;
  544. /* Set CPAL_State to CPAL_STATE_ERROR */
  545. pDevInitStruct->CPAL_State = CPAL_STATE_ERROR;
  546. return CPAL_FAIL;
  547. }
  548. /* Send register address (LSB) */
  549. __CPAL_I2C_HAL_SEND(pDevInitStruct->CPAL_Dev, (uint8_t)((pDevInitStruct->pCPAL_TransferTx->wAddr2)& 0x00FF));
  550. }
  551. #endif /* CPAL_16BIT_REG_OPTION */
  552. /* If I2C ERR Interrupt Option Bit not selected */
  553. if ((pDevInitStruct->wCPAL_Options & CPAL_OPT_I2C_ERRIT_DISABLE) == 0)
  554. {
  555. /* Enable I2C Error Interrupts */
  556. __CPAL_I2C_HAL_ENABLE_ERRIT(pDevInitStruct->CPAL_Dev);
  557. }
  558. /* Wait until TCR flag is set */
  559. __CPAL_I2C_TIMEOUT(__CPAL_I2C_HAL_GET_TCR(pDevInitStruct->CPAL_Dev), CPAL_I2C_TIMEOUT_TCR);
  560. /* Set Nbytes to zero */
  561. CR2_tmp &= ~I2C_CR2_NBYTES;
  562. }
  563. #endif /* CPAL_I2C_MEM_ADDR */
  564. #endif /* CPAL_I2C_MASTER_MODE */
  565. /* Update CPAL_State to CPAL_STATE_BUSY_TX */
  566. pDevInitStruct->CPAL_State = CPAL_STATE_BUSY_TX;
  567. CPAL_LOG("\n\rLOG : I2C Device Busy TX");
  568. #ifdef CPAL_I2C_MASTER_MODE
  569. /* If master mode selected */
  570. if (pDevInitStruct->CPAL_Mode == CPAL_MODE_MASTER)
  571. {
  572. CPAL_LOG("\n\rLOG : I2C Device Master");
  573. /* If automatic end mode is selected */
  574. if ((pDevInitStruct->wCPAL_Options & CPAL_OPT_I2C_AUTOMATIC_END) != 0)
  575. {
  576. /* Enable automatic end mode */
  577. CR2_tmp |= I2C_CR2_AUTOEND;
  578. }
  579. /* If number of data is equal or lower than 255 bytes */
  580. if (pDevInitStruct->pCPAL_TransferTx->wNumData <= 0xFF )
  581. {
  582. /* Update Num_Data */
  583. Num_Data = pDevInitStruct->pCPAL_TransferTx->wNumData;
  584. /* Set Nbytes to wNumData */
  585. CR2_tmp |= (uint32_t)((uint32_t)(Num_Data) << 16);
  586. /* Disable reload */
  587. CR2_tmp &= ~I2C_CR2_RELOAD;
  588. }
  589. /* If number of data is greater than 255 bytes */
  590. else
  591. {
  592. /* Set Nbytes to 255 */
  593. CR2_tmp |= (uint32_t)((uint32_t)(255) << 16);
  594. /* Enable reload */
  595. CR2_tmp |= I2C_CR2_RELOAD;
  596. }
  597. /* If CPAL_OPT_NO_MEM_ADDR is selected */
  598. if ((pDevInitStruct->wCPAL_Options & CPAL_OPT_NO_MEM_ADDR) != 0)
  599. {
  600. /* Generate start */
  601. CR2_tmp |= I2C_CR2_START;
  602. }
  603. /* Update CR2 Register */
  604. __CPAL_I2C_HAL_CR2_UPDATE(pDevInitStruct->CPAL_Dev, CR2_tmp);
  605. /* If interrupt programming model */
  606. if (pDevInitStruct->CPAL_ProgModel == CPAL_PROGMODEL_INTERRUPT)
  607. {
  608. CPAL_LOG("\n\rLOG : I2C Device IT Enabled");
  609. /* Enable master interrupts */
  610. __CPAL_I2C_HAL_ENABLE_MASTER_TXIT(pDevInitStruct->CPAL_Dev);
  611. }
  612. /* If DMA programming model */
  613. else
  614. {
  615. CPAL_LOG("\n\rLOG : I2C Device DMA TX Enabled");
  616. /* Enable TX DMA request */
  617. __CPAL_I2C_HAL_ENABLE_TXDMAREQ(pDevInitStruct->CPAL_Dev);
  618. /* Enable master interrupts */
  619. __CPAL_I2C_HAL_ENABLE_MASTER_IT(pDevInitStruct->CPAL_Dev);
  620. }
  621. }
  622. /* If slave mode selected */
  623. else
  624. #endif /* CPAL_I2C_MASTER_MODE */
  625. {
  626. #ifdef CPAL_I2C_SLAVE_MODE
  627. CPAL_LOG("\n\rLOG : I2C Device Slave");
  628. /* If NACK Slave Own Address option bit selected */
  629. if ((pDevInitStruct->wCPAL_Options & CPAL_OPT_I2C_NACK_ADD) != 0)
  630. {
  631. /* Enable Acknowledgement of Own address */
  632. __CPAL_I2C_HAL_ENABLE_DEV(pDevInitStruct->CPAL_Dev);
  633. }
  634. /* If interrupt programming model */
  635. if (pDevInitStruct->CPAL_ProgModel == CPAL_PROGMODEL_INTERRUPT)
  636. {
  637. CPAL_LOG("\n\rLOG : I2C Device IT Enabled");
  638. /* Enable slave interrupts */
  639. __CPAL_I2C_HAL_ENABLE_SLAVE_TXIT(pDevInitStruct->CPAL_Dev);
  640. }
  641. /* If DMA programming model */
  642. else
  643. {
  644. CPAL_LOG("\n\rLOG : I2C Device DMA TX Enabled");
  645. /* Enable TX DMA request */
  646. __CPAL_I2C_HAL_ENABLE_TXDMAREQ(pDevInitStruct->CPAL_Dev);
  647. /* Enable slave interrupts */
  648. __CPAL_I2C_HAL_ENABLE_SLAVE_IT(pDevInitStruct->CPAL_Dev);
  649. }
  650. #endif /* CPAL_I2C_SLAVE_MODE */
  651. }
  652. }
  653. return CPAL_PASS;
  654. }
  655. /**
  656. * @brief Allows to receive a data or a buffer of data through the peripheral
  657. * from a selected device in a selected location address.
  658. * @param pDevInitStruct: Pointer to the peripheral configuration structure.
  659. * @retval CPAL_PASS or CPAL_FAIL.
  660. */
  661. uint32_t CPAL_I2C_Read(CPAL_InitTypeDef* pDevInitStruct)
  662. {
  663. CR2_tmp = 0;
  664. /* Check I2C State:
  665. - If busy --> exit Read operation
  666. - If disabled --> exit Read operation
  667. - If error --> exit Read operation
  668. - If ready -->
  669. *- Update CPAL_State to CPAL_STATE_BUSY
  670. *- If CPAL_OPT_I2C_NOSTOP_MODE is not selected :
  671. - Check if device is busy
  672. *- Update CPAL_State to CPAL_STATE_READY_RX
  673. *- If DMA Prog Model :
  674. - Configure and enable DMA
  675. *- If Master mode :
  676. - If 10Bit Mode : Enable ADD10
  677. - Configure Slave address
  678. *- If Memory Address mode (master)
  679. - Send target and memory address
  680. *- Update CPAL_State to CPAL_STATE_BUSY_RX
  681. *- If Master mode :
  682. - Configure HEADR10, AUTOEND, RELOAD and NBYTES
  683. - If Interrupt Prog Model :
  684. - Generate start and enable interrupts
  685. - If DMA Prog Model :
  686. - Enable RX DMA request
  687. - Generate start and enable interrupts
  688. *- If Slave mode :
  689. - If Interrupt Prog Model :
  690. - Enable interrupts
  691. - If DMA Prog Model :
  692. - Enable RX DMA request
  693. - Enable interrupts */
  694. CPAL_LOG("\n\r\n\rLOG <CPAL_I2C_Read> : I2C Device Perform Read OP");
  695. /* If Device is Busy (a transaction is still on going) Exit Read function */
  696. if (((pDevInitStruct->CPAL_State & CPAL_STATE_BUSY) != 0)
  697. || (pDevInitStruct->CPAL_State == CPAL_STATE_READY_TX)
  698. || (pDevInitStruct->CPAL_State == CPAL_STATE_READY_RX))
  699. {
  700. CPAL_LOG("\n\rERROR : I2C Device Busy");
  701. return CPAL_FAIL;
  702. }
  703. /* If CPAL_State is CPAL_STATE_DISABLED (device is not initialized) Exit Read function */
  704. else if (pDevInitStruct->CPAL_State == CPAL_STATE_DISABLED)
  705. {
  706. CPAL_LOG("\n\rERROR : I2C Device Not Initialized");
  707. return CPAL_FAIL;
  708. }
  709. /* If CPAL_State is CPAL_STATE_ERROR (Error occurred ) */
  710. else if (pDevInitStruct->CPAL_State == CPAL_STATE_ERROR)
  711. {
  712. CPAL_LOG("\n\rERROR : I2C Device Error");
  713. return CPAL_FAIL;
  714. }
  715. /* If CPAL_State is CPAL_STATE_READY */
  716. else
  717. {
  718. /* Update CPAL_State to CPAL_STATE_BUSY */
  719. pDevInitStruct->CPAL_State = CPAL_STATE_BUSY;
  720. /* If CPAL_OPT_I2C_NOSTOP_MODE is not selected */
  721. if ((pDevInitStruct->wCPAL_Options & CPAL_OPT_I2C_NOSTOP_MODE) == 0)
  722. {
  723. /* Wait until BUSY flag is reset */
  724. __CPAL_I2C_TIMEOUT(!(__CPAL_I2C_HAL_GET_BUSY(pDevInitStruct->CPAL_Dev)), CPAL_I2C_TIMEOUT_BUSY);
  725. }
  726. /* Update CPAL_State to CPAL_STATE_READY_RX */
  727. pDevInitStruct->CPAL_State = CPAL_STATE_READY_RX;
  728. CPAL_LOG("\n\rLOG : I2C Device Ready RX");
  729. #ifdef CPAL_I2C_DMA_PROGMODEL
  730. /* If DMA programming model */
  731. if (pDevInitStruct->CPAL_ProgModel == CPAL_PROGMODEL_DMA)
  732. {
  733. /* Configure and enable RX DMA channel */
  734. I2C_Enable_DMA(pDevInitStruct, CPAL_DIRECTION_RX);
  735. }
  736. #endif /* CPAL_I2C_DMA_PROGMODEL */
  737. #ifdef CPAL_I2C_MASTER_MODE
  738. /* If master mode selected */
  739. if (pDevInitStruct->CPAL_Mode == CPAL_MODE_MASTER)
  740. {
  741. #ifdef CPAL_I2C_10BIT_ADDR_MODE
  742. /* If 10 Bit addressing mode */
  743. if (pDevInitStruct->pCPAL_I2C_Struct->I2C_AcknowledgedAddress == I2C_AcknowledgedAddress_10bit)
  744. {
  745. /* Enable 10Bit addressing mode */
  746. CR2_tmp |= I2C_CR2_ADD10;
  747. }
  748. #endif /* CPAL_I2C_10BIT_ADDR_MODE */
  749. /* Configure slave address */
  750. CR2_tmp |= (uint32_t)((pDevInitStruct->pCPAL_TransferRx->wAddr1) & 0x000003FF);
  751. }
  752. #ifdef CPAL_I2C_MEM_ADDR
  753. /* If No Memory Address option bit is not selected and master mode selected */
  754. if (((pDevInitStruct->wCPAL_Options & CPAL_OPT_NO_MEM_ADDR) == 0)
  755. && (pDevInitStruct->CPAL_Mode == CPAL_MODE_MASTER ))
  756. {
  757. CPAL_LOG("\n\rLOG : I2C Device Master Mem Addr Mode");
  758. /* Disable error interrupt to manage error without interrupt */
  759. __CPAL_I2C_HAL_DISABLE_ERRIT(pDevInitStruct->CPAL_Dev);
  760. /* If 8 Bit register mode */
  761. if ((pDevInitStruct->wCPAL_Options & CPAL_OPT_16BIT_REG) == 0)
  762. {
  763. /* Configure Nbytes */
  764. CR2_tmp |= (uint32_t)((uint32_t)(1) << 16);
  765. /* Update CR2 Register */
  766. __CPAL_I2C_HAL_CR2_UPDATE(pDevInitStruct->CPAL_Dev, CR2_tmp);
  767. /* Generate start */
  768. __CPAL_I2C_HAL_START(pDevInitStruct->CPAL_Dev);
  769. /* Wait until TXIS flag is set or NACK is set */
  770. __CPAL_I2C_TIMEOUT((__CPAL_I2C_HAL_GET_TXIS(pDevInitStruct->CPAL_Dev) || __CPAL_I2C_HAL_GET_NACK(pDevInitStruct->CPAL_Dev)), CPAL_I2C_TIMEOUT_TXIS);
  771. /* If acknowledge failure detected generate stop and abort communication */
  772. if (__CPAL_I2C_HAL_GET_NACK(pDevInitStruct->CPAL_Dev))
  773. {
  774. /* Generate stop if autoend option is disabled */
  775. if((CPAL_I2C_DEVICE[(pDevInitStruct->CPAL_Dev)]->CR2 & I2C_CR2_AUTOEND) != I2C_CR2_AUTOEND)
  776. {
  777. /* Generate stop */
  778. __CPAL_I2C_HAL_STOP(pDevInitStruct->CPAL_Dev);
  779. }
  780. /* Clear NACK flag */
  781. __CPAL_I2C_HAL_CLEAR_NACK(pDevInitStruct->CPAL_Dev);
  782. /* Wait until STOP flag is set */
  783. __CPAL_I2C_TIMEOUT(__CPAL_I2C_HAL_GET_STOP(pDevInitStruct->CPAL_Dev), CPAL_I2C_TIMEOUT_BUSY);
  784. /* Clear STOP flag */
  785. __CPAL_I2C_HAL_CLEAR_STOP(pDevInitStruct->CPAL_Dev);
  786. /* Update wCPAL_DevError */
  787. pDevInitStruct->wCPAL_DevError = CPAL_I2C_ERR_AF;
  788. /* Set CPAL_State to CPAL_STATE_ERROR */
  789. pDevInitStruct->CPAL_State = CPAL_STATE_ERROR;
  790. return CPAL_FAIL;
  791. }
  792. /* Send register address */
  793. __CPAL_I2C_HAL_SEND(pDevInitStruct->CPAL_Dev, (uint8_t)(pDevInitStruct->pCPAL_TransferRx->wAddr2));
  794. }
  795. #ifdef CPAL_16BIT_REG_OPTION
  796. /* If 16 Bit register mode */
  797. else
  798. {
  799. /* Configure Nbytes */
  800. CR2_tmp |= (uint32_t)((uint32_t)(2) << 16);
  801. /* Update CR2 Register */
  802. __CPAL_I2C_HAL_CR2_UPDATE(pDevInitStruct->CPAL_Dev, CR2_tmp);
  803. /* Generate start */
  804. __CPAL_I2C_HAL_START(pDevInitStruct->CPAL_Dev);
  805. /* Wait until TXIS flag is set or NACK is set */
  806. __CPAL_I2C_TIMEOUT((__CPAL_I2C_HAL_GET_TXIS(pDevInitStruct->CPAL_Dev) || __CPAL_I2C_HAL_GET_NACK(pDevInitStruct->CPAL_Dev)), CPAL_I2C_TIMEOUT_TXIS);
  807. /* If acknowledge failure detected generate stop and abort communication */
  808. if (__CPAL_I2C_HAL_GET_NACK(pDevInitStruct->CPAL_Dev))
  809. {
  810. /* Generate stop if autoend option is disabled */
  811. if((CPAL_I2C_DEVICE[(pDevInitStruct->CPAL_Dev)]->CR2 & I2C_CR2_AUTOEND) != I2C_CR2_AUTOEND)
  812. {
  813. /* Generate stop */
  814. __CPAL_I2C_HAL_STOP(pDevInitStruct->CPAL_Dev);
  815. }
  816. /* Clear NACK flag */
  817. __CPAL_I2C_HAL_CLEAR_NACK(pDevInitStruct->CPAL_Dev);
  818. /* Wait until STOP flag is set */
  819. __CPAL_I2C_TIMEOUT(__CPAL_I2C_HAL_GET_STOP(pDevInitStruct->CPAL_Dev), CPAL_I2C_TIMEOUT_BUSY);
  820. /* Clear STOP flag */
  821. __CPAL_I2C_HAL_CLEAR_STOP(pDevInitStruct->CPAL_Dev);
  822. /* Update wCPAL_DevError */
  823. pDevInitStruct->wCPAL_DevError = CPAL_I2C_ERR_AF;
  824. /* Set CPAL_State to CPAL_STATE_ERROR */
  825. pDevInitStruct->CPAL_State = CPAL_STATE_ERROR;
  826. return CPAL_FAIL;
  827. }
  828. /* Send register address (MSB) */
  829. __CPAL_I2C_HAL_SEND(pDevInitStruct->CPAL_Dev, (uint8_t)(((pDevInitStruct->pCPAL_TransferRx->wAddr2)& 0xFF00) >> 8));
  830. /* Wait until TXIS flag is set or NACK is set */
  831. __CPAL_I2C_TIMEOUT((__CPAL_I2C_HAL_GET_TXIS(pDevInitStruct->CPAL_Dev) || __CPAL_I2C_HAL_GET_NACK(pDevInitStruct->CPAL_Dev)), CPAL_I2C_TIMEOUT_TXIS);
  832. /* If acknowledge failure detected generate stop and abort communication */
  833. if (__CPAL_I2C_HAL_GET_NACK(pDevInitStruct->CPAL_Dev))
  834. {
  835. /* Generate stop if autoend option is disabled */
  836. if((CPAL_I2C_DEVICE[(pDevInitStruct->CPAL_Dev)]->CR2 & I2C_CR2_AUTOEND) != I2C_CR2_AUTOEND)
  837. {
  838. /* Generate stop */
  839. __CPAL_I2C_HAL_STOP(pDevInitStruct->CPAL_Dev);
  840. }
  841. /* Clear NACK flag */
  842. __CPAL_I2C_HAL_CLEAR_NACK(pDevInitStruct->CPAL_Dev);
  843. /* Wait until STOP flag is set */
  844. __CPAL_I2C_TIMEOUT(__CPAL_I2C_HAL_GET_STOP(pDevInitStruct->CPAL_Dev), CPAL_I2C_TIMEOUT_BUSY);
  845. /* Clear STOP flag */
  846. __CPAL_I2C_HAL_CLEAR_STOP(pDevInitStruct->CPAL_Dev);
  847. /* Update wCPAL_DevError */
  848. pDevInitStruct->wCPAL_DevError = CPAL_I2C_ERR_AF;
  849. /* Set CPAL_State to CPAL_STATE_ERROR */
  850. pDevInitStruct->CPAL_State = CPAL_STATE_ERROR;
  851. return CPAL_FAIL;
  852. }
  853. /* Send register address (LSB) */
  854. __CPAL_I2C_HAL_SEND(pDevInitStruct->CPAL_Dev, (uint8_t)((pDevInitStruct->pCPAL_TransferRx->wAddr2)& 0x00FF));
  855. }
  856. #endif /* CPAL_16BIT_REG_OPTION */
  857. /* If I2C ERR Interrupt Option Bit not selected */
  858. if ((pDevInitStruct->wCPAL_Options & CPAL_OPT_I2C_ERRIT_DISABLE) == 0)
  859. {
  860. /* Enable I2C Error Interrupts */
  861. __CPAL_I2C_HAL_ENABLE_ERRIT(pDevInitStruct->CPAL_Dev);
  862. }
  863. /* Wait until TC flag is set */
  864. __CPAL_I2C_TIMEOUT(__CPAL_I2C_HAL_GET_TC(pDevInitStruct->CPAL_Dev), CPAL_I2C_TIMEOUT_TC);
  865. /* Set Nbytes to zero */
  866. CR2_tmp &= ~I2C_CR2_NBYTES;
  867. }
  868. #endif /* CPAL_I2C_MEM_ADDR */
  869. #endif /* CPAL_I2C_MASTER_MODE */
  870. /* Update CPAL_State to CPAL_STATE_BUSY_RX */
  871. pDevInitStruct->CPAL_State = CPAL_STATE_BUSY_RX;
  872. CPAL_LOG("\n\rLOG : I2C Device Busy RX");
  873. #ifdef CPAL_I2C_MASTER_MODE
  874. /* If master mode selected */
  875. if (pDevInitStruct->CPAL_Mode == CPAL_MODE_MASTER)
  876. {
  877. CPAL_LOG("\n\rLOG : I2C Device Master");
  878. /* Enable transfer request */
  879. CR2_tmp |= I2C_CR2_RD_WRN;
  880. #ifdef CPAL_I2C_10BIT_ADDR_MODE
  881. /* If 10 Bit addressing mode */
  882. if (pDevInitStruct->pCPAL_I2C_Struct->I2C_AcknowledgedAddress == I2C_AcknowledgedAddress_10bit)
  883. {
  884. /* If CPAL_OPT_NO_MEM_ADDR is not selected and CPAL_OPT_I2C_10BIT_HEADR option enabled */
  885. if (((pDevInitStruct->wCPAL_Options & CPAL_OPT_I2C_10BIT_HEADR) != 0) || ((pDevInitStruct->wCPAL_Options & CPAL_OPT_NO_MEM_ADDR) == 0))
  886. {
  887. /* Disable 10Bit addressing complete sequence for Read */
  888. CR2_tmp |= I2C_CR2_HEAD10R;
  889. }
  890. }
  891. #endif /* CPAL_I2C_10BIT_ADDR_MODE */
  892. /* If automatic end mode is selected */
  893. if ((pDevInitStruct->wCPAL_Options & CPAL_OPT_I2C_AUTOMATIC_END) != 0)
  894. {
  895. /* Enable automatic end mode */
  896. CR2_tmp |= I2C_CR2_AUTOEND;
  897. }
  898. /* If number of data is equal or lower than 255 bytes */
  899. if (pDevInitStruct->pCPAL_TransferRx->wNumData <= 0xFF )
  900. {
  901. /* Update Num_Data */
  902. Num_Data = pDevInitStruct->pCPAL_TransferRx->wNumData;
  903. /* Set Nbytes to wNumData */
  904. CR2_tmp |= (uint32_t)((uint32_t)(Num_Data) << 16);
  905. /* Disable reload */
  906. CR2_tmp &= ~I2C_CR2_RELOAD;
  907. }
  908. /* If number of data is greater than 255 bytes */
  909. else
  910. {
  911. /* Set Nbytes to wNumData */
  912. CR2_tmp |= (uint32_t)((uint32_t)(255) << 16);
  913. /* Enaable reload */
  914. CR2_tmp |= I2C_CR2_RELOAD;
  915. }
  916. /* Generate start */
  917. CR2_tmp |= I2C_CR2_START;
  918. /* If interrupt programming model */
  919. if (pDevInitStruct->CPAL_ProgModel == CPAL_PROGMODEL_INTERRUPT)
  920. {
  921. CPAL_LOG("\n\rLOG : I2C Device IT Enabled");
  922. /* Update CR2 Register */
  923. __CPAL_I2C_HAL_CR2_UPDATE(pDevInitStruct->CPAL_Dev, CR2_tmp);
  924. /* Enable master interrupts */
  925. __CPAL_I2C_HAL_ENABLE_MASTER_RXIT(pDevInitStruct->CPAL_Dev);
  926. }
  927. /* If DMA programming model */
  928. else
  929. {
  930. CPAL_LOG("\n\rLOG : I2C Device DMA RX Enabled");
  931. /* Enable RX DMA request */
  932. __CPAL_I2C_HAL_ENABLE_RXDMAREQ(pDevInitStruct->CPAL_Dev);
  933. /* Update CR2 Register */
  934. __CPAL_I2C_HAL_CR2_UPDATE(pDevInitStruct->CPAL_Dev, CR2_tmp);
  935. /* Enable master interrupt */
  936. __CPAL_I2C_HAL_ENABLE_MASTER_IT(pDevInitStruct->CPAL_Dev);
  937. }
  938. }
  939. /* If slave mode selected */
  940. else
  941. #endif /* CPAL_I2C_MASTER_MODE */
  942. {
  943. #ifdef CPAL_I2C_SLAVE_MODE
  944. CPAL_LOG("\n\rLOG : I2C Device Slave");
  945. /* If NACK Slave Own Address option bit selected */
  946. if ((pDevInitStruct->wCPAL_Options & CPAL_OPT_I2C_NACK_ADD) != 0)
  947. {
  948. /* Enable Acknowledgement of Own address */
  949. __CPAL_I2C_HAL_ENABLE_DEV(pDevInitStruct->CPAL_Dev);
  950. }
  951. /* If interrupt programming model */
  952. if (pDevInitStruct->CPAL_ProgModel == CPAL_PROGMODEL_INTERRUPT)
  953. {
  954. CPAL_LOG("\n\rLOG : I2C Device IT Enabled");
  955. /* Enable slave interrupts */
  956. __CPAL_I2C_HAL_ENABLE_SLAVE_RXIT(pDevInitStruct->CPAL_Dev);
  957. }
  958. /* If DMA programming model */
  959. else
  960. {
  961. CPAL_LOG("\n\rLOG : I2C Device DMA RX Enabled");
  962. /* Enable RX DMA request */
  963. __CPAL_I2C_HAL_ENABLE_RXDMAREQ(pDevInitStruct->CPAL_Dev);
  964. /* Enable slave interrupt */
  965. __CPAL_I2C_HAL_ENABLE_SLAVE_IT(pDevInitStruct->CPAL_Dev);
  966. }
  967. #endif /* CPAL_I2C_SLAVE_MODE */
  968. }
  969. }
  970. return CPAL_PASS;
  971. }
  972. #endif /* CPAL_I2C_MASTER_MODE || ! CPAL_I2C_LISTEN_MODE */
  973. #if defined (CPAL_I2C_LISTEN_MODE) && defined (CPAL_I2C_SLAVE_MODE)
  974. /**
  975. * @brief Allows slave device to start a communication without knowing in advance
  976. * the nature of the operation (read or write). Slave waits until it receive
  977. * its own address.CPAL_I2C_SLAVE_READ_UserCallback is called for a read request
  978. * and CPAL_I2C_SLAVE_WRITE_UserCallback for a write request in I2C_SLAVE_ADDR_Handle.
  979. * User must implement inorder to configure DMA, interrupts and transfer parameters.
  980. * @param pDevInitStruct: Pointer to the peripheral configuration structure.
  981. * @retval CPAL_PASS or CPAL_FAIL.
  982. */
  983. uint32_t CPAL_I2C_Listen(CPAL_InitTypeDef* pDevInitStruct)
  984. {
  985. /* Check I2C State:
  986. - If busy --> exit operation
  987. - If disabled --> exit operation
  988. - If error --> exit operation
  989. - If ready -->
  990. - Enable Event Interrupt */
  991. CPAL_LOG("\n\r\n\rLOG <CPAL_I2C_Listen> : I2C Device in listen mode");
  992. /* If Device is Busy (a transaction is still on going) Exit function */
  993. if (((pDevInitStruct->CPAL_State & CPAL_STATE_BUSY) != 0)
  994. || (pDevInitStruct->CPAL_State == CPAL_STATE_READY_TX)
  995. || (pDevInitStruct->CPAL_State == CPAL_STATE_READY_RX))
  996. {
  997. CPAL_LOG("\n\rERROR : I2C Device Busy");
  998. return CPAL_FAIL;
  999. }
  1000. /* If CPAL_State is CPAL_STATE_DISABLED (device is not initialized) Exit function */
  1001. else if (pDevInitStruct->CPAL_State == CPAL_STATE_DISABLED)
  1002. {
  1003. CPAL_LOG("\n\rERROR : I2C Device Not Initialized");
  1004. return CPAL_FAIL;
  1005. }
  1006. /* If CPAL_State is CPAL_STATE_ERROR (Error occurred ) */
  1007. else if (pDevInitStruct->CPAL_State == CPAL_STATE_ERROR)
  1008. {
  1009. CPAL_LOG("\n\rERROR : I2C Device Error");
  1010. return CPAL_FAIL;
  1011. }
  1012. /* If CPAL_State is CPAL_STATE_READY */
  1013. else
  1014. {
  1015. /* If NACK Slave Own Address option bit selected */
  1016. if ((pDevInitStruct->wCPAL_Options & CPAL_OPT_I2C_NACK_ADD) != 0)
  1017. {
  1018. /* Enable Acknowledgement of Own address */
  1019. __CPAL_I2C_HAL_ENABLE_DEV(pDevInitStruct->CPAL_Dev);
  1020. }
  1021. /* Set device to slave mode */
  1022. pDevInitStruct->CPAL_Mode = CPAL_MODE_SLAVE;
  1023. /* Update CPAL_State to CPAL_STATE_BUSY */
  1024. pDevInitStruct->CPAL_State = CPAL_STATE_BUSY;
  1025. CPAL_LOG("\n\rLOG : I2C Device EVT IT Enabled");
  1026. /* Enable Slave Interrupts*/
  1027. __CPAL_I2C_HAL_ENABLE_SLAVE_IT(pDevInitStruct->CPAL_Dev);
  1028. }
  1029. return CPAL_PASS;
  1030. }
  1031. #endif /* CPAL_I2C_LISTEN_MODE && CPAL_I2C_SLAVE_MODE */
  1032. /**
  1033. * @brief Wait until target device is ready for communication (This function is
  1034. * used with Memory devices).
  1035. * @param pDevInitStruct: Pointer to the peripheral configuration structure.
  1036. * @retval CPAL_PASS or CPAL_FAIL.
  1037. */
  1038. uint32_t CPAL_I2C_IsDeviceReady(CPAL_InitTypeDef* pDevInitStruct)
  1039. {
  1040. CR2_tmp = 0;
  1041. CPAL_LOG("\n\r\n\rLOG <CPAL_I2C_IsDeviceReady> : Wait until I2C Device is Ready");
  1042. /* Set CPAL_State to CPAL_STATE_BUSY */
  1043. pDevInitStruct->CPAL_State = CPAL_STATE_BUSY;
  1044. /* Disable I2Cx device */
  1045. __CPAL_I2C_HAL_DISABLE_DEV(pDevInitStruct->CPAL_Dev);
  1046. /* Enable I2Cx device */
  1047. __CPAL_I2C_HAL_ENABLE_DEV(pDevInitStruct->CPAL_Dev);
  1048. /* Disable interrupts */
  1049. __CPAL_I2C_HAL_DISABLE_ALLIT(pDevInitStruct->CPAL_Dev);
  1050. /* Configure slave address */
  1051. CR2_tmp |= (uint32_t)((pDevInitStruct->pCPAL_TransferTx->wAddr1) & 0x000003FF) | I2C_CR2_AUTOEND;
  1052. /* Update CR2 Register */
  1053. __CPAL_I2C_HAL_CR2_UPDATE(pDevInitStruct->CPAL_Dev, CR2_tmp);
  1054. /* Generate Start */
  1055. __CPAL_I2C_HAL_START(pDevInitStruct->CPAL_Dev);
  1056. /* Set 35ms timeout */
  1057. pDevInitStruct->wCPAL_Timeout = CPAL_I2C_TIMEOUT_MIN + 35;
  1058. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  1059. /* Wait until STOPF flag is set or a NACK flag is set*/
  1060. while((__CPAL_I2C_HAL_GET_STOP(pDevInitStruct->CPAL_Dev) == RESET) && (__CPAL_I2C_HAL_GET_NACK(pDevInitStruct->CPAL_Dev) == RESET));
  1061. /* Reinitialize Timeout Value to default */
  1062. pDevInitStruct->wCPAL_Timeout = CPAL_I2C_TIMEOUT_DEFAULT;
  1063. /* Check if the NACKF flag has not been set */
  1064. if (__CPAL_I2C_HAL_GET_NACK(pDevInitStruct->CPAL_Dev) != RESET)
  1065. {
  1066. /* Clear NACK flag */
  1067. __CPAL_I2C_HAL_CLEAR_NACK(pDevInitStruct->CPAL_Dev);
  1068. /* Clear Stop flag */
  1069. __CPAL_I2C_HAL_CLEAR_STOP(pDevInitStruct->CPAL_Dev);
  1070. /* Enable error interrupt */
  1071. __CPAL_I2C_HAL_ENABLE_ERRIT(pDevInitStruct->CPAL_Dev);
  1072. /* Set CPAL_State to CPAL_STATE_READY */
  1073. pDevInitStruct->CPAL_State = CPAL_STATE_READY;
  1074. return CPAL_FAIL;
  1075. }
  1076. else
  1077. {
  1078. /* Clear Stop flag */
  1079. __CPAL_I2C_HAL_CLEAR_STOP(pDevInitStruct->CPAL_Dev);
  1080. /* Enable error interrupt */
  1081. __CPAL_I2C_HAL_ENABLE_ERRIT(pDevInitStruct->CPAL_Dev);
  1082. /* Set CPAL_State to CPAL_STATE_READY */
  1083. pDevInitStruct->CPAL_State = CPAL_STATE_READY;
  1084. CPAL_LOG("\n\rLOG : I2C Target device Ready");
  1085. return CPAL_PASS;
  1086. }
  1087. }
  1088. /*================== CPAL_I2C_Interrupt_Handler ==================*/
  1089. /**
  1090. * @brief This function handles I2C interrupt request for preparing communication
  1091. * and for transfer phase in case of using Interrupt Programming Model.
  1092. * @param pDevInitStruct: Pointer to the peripheral configuration structure.
  1093. * @retval CPAL_PASS.
  1094. */
  1095. uint32_t CPAL_I2C_EV_IRQHandler( CPAL_InitTypeDef* pDevInitStruct)
  1096. {
  1097. __IO uint32_t I2CFlagStatus = 0x00000000;
  1098. /* Read I2C status registers (ISR) */
  1099. I2CFlagStatus = __CPAL_I2C_HAL_GET_EVENT(pDevInitStruct->CPAL_Dev);
  1100. #ifdef CPAL_I2C_MASTER_MODE
  1101. /*----------------------------------------------------------------------------------------------*/
  1102. /*---------------------------------- If Master Mode selected ----------------------------------*/
  1103. if (pDevInitStruct->CPAL_Mode == CPAL_MODE_MASTER)
  1104. {
  1105. #ifdef CPAL_I2C_IT_PROGMODEL
  1106. /*----------------------------------------*/
  1107. /*------------- If TXIS event ------------*/
  1108. if (((I2CFlagStatus & CPAL_I2C_EVT_TXIS) != 0) && (pDevInitStruct->CPAL_State == CPAL_STATE_BUSY_TX))
  1109. {
  1110. I2C_MASTER_TXIS_Handle(pDevInitStruct);
  1111. }
  1112. /*----------------------------------------*/
  1113. /*------------- If RXNE event ------------*/
  1114. if (((I2CFlagStatus & CPAL_I2C_EVT_RXNE) != 0) && (pDevInitStruct->CPAL_State == CPAL_STATE_BUSY_RX))
  1115. {
  1116. I2C_MASTER_RXNE_Handle(pDevInitStruct);
  1117. }
  1118. #endif /* CPAL_I2C_IT_PROGMODEL */
  1119. /*----------------------------------------*/
  1120. /*-------------- If TCR event ------------*/
  1121. if ((I2CFlagStatus & CPAL_I2C_EVT_TCR) != 0)
  1122. {
  1123. I2C_MASTER_TCR_Handle(pDevInitStruct);
  1124. }
  1125. /*----------------------------------------*/
  1126. /*------------- If TC event --------------*/
  1127. if ((I2CFlagStatus & CPAL_I2C_EVT_TC ) != 0)
  1128. {
  1129. I2C_MASTER_TC_Handle(pDevInitStruct);
  1130. }
  1131. /*----------------------------------------*/
  1132. /*------------- If STOP event ------------*/
  1133. if ((I2CFlagStatus & CPAL_I2C_EVT_STOP) != 0)
  1134. {
  1135. I2C_MASTER_STOP_Handle(pDevInitStruct);
  1136. }
  1137. /*----------------------------------------*/
  1138. /*------------- If NACK event ------------*/
  1139. if((I2CFlagStatus & CPAL_I2C_EVT_NACK ) != 0)
  1140. {
  1141. I2C_MASTER_NACK_Handle(pDevInitStruct);
  1142. }
  1143. }
  1144. #endif /* CPAL_I2C_MASTER_MODE */
  1145. #ifdef CPAL_I2C_SLAVE_MODE
  1146. /*----------------------------------------------------------------------------------------------*/
  1147. /*---------------------------------- If Slave Mode selected ------------------------------------*/
  1148. if (pDevInitStruct->CPAL_Mode == CPAL_MODE_SLAVE)
  1149. {
  1150. /*----------------------------------------*/
  1151. /*------------- If ADDR event ------------*/
  1152. if ((I2CFlagStatus & CPAL_I2C_EVT_ADDR ) != 0)
  1153. {
  1154. I2C_SLAVE_ADDR_Handle(pDevInitStruct);
  1155. }
  1156. #ifdef CPAL_I2C_IT_PROGMODEL
  1157. /*----------------------------------------*/
  1158. /*------------- If TXIS event ------------*/
  1159. if ((I2CFlagStatus & CPAL_I2C_EVT_TXIS) != 0)
  1160. {
  1161. I2C_SLAVE_TXIS_Handle(pDevInitStruct);
  1162. }
  1163. /*----------------------------------------*/
  1164. /*------------- If RXNE event ------------*/
  1165. if ((I2CFlagStatus & CPAL_I2C_EVT_RXNE) != 0)
  1166. {
  1167. I2C_SLAVE_RXNE_Handle(pDevInitStruct);
  1168. }
  1169. #endif /* CPAL_I2C_IT_PROGMODEL */
  1170. /*----------------------------------------*/
  1171. /*------------- If NACK event ------------*/
  1172. if ((I2CFlagStatus & CPAL_I2C_EVT_NACK) != 0)
  1173. {
  1174. I2C_SLAVE_NACK_Handle(pDevInitStruct);
  1175. }
  1176. /*----------------------------------------*/
  1177. /*------------- If STOP event ------------*/
  1178. if ((I2CFlagStatus & CPAL_I2C_EVT_STOP) != 0)
  1179. {
  1180. I2C_SLAVE_STOP_Handle(pDevInitStruct);
  1181. }
  1182. }
  1183. #endif /* CPAL_I2C_SLAVE_MODE */
  1184. return CPAL_PASS;
  1185. }
  1186. /**
  1187. * @brief Allows to handle errors occurred during initialization or communication
  1188. * in order to recover the correct communication status or call specific
  1189. * user functions.
  1190. * @param pDevInitStruct: Pointer to the peripheral configuration structure.
  1191. * @retval CPAL_PASS.
  1192. */
  1193. uint32_t CPAL_I2C_ER_IRQHandler(CPAL_InitTypeDef* pDevInitStruct)
  1194. {
  1195. /* Read error register and affect to wCPAL_DevError */
  1196. pDevInitStruct->wCPAL_DevError = __CPAL_I2C_HAL_GET_ERROR(pDevInitStruct->CPAL_Dev);
  1197. /* Set CPAL_State to CPAL_STATE_ERROR */
  1198. pDevInitStruct->CPAL_State = CPAL_STATE_ERROR;
  1199. CPAL_LOG("\n\r\n\rERROR <CPAL_I2C_ErrorHandler> : I2C Device Error");
  1200. /* If Bus error occurred ---------------------------------------------------*/
  1201. if ((pDevInitStruct->wCPAL_DevError & CPAL_I2C_ERR_BERR) != 0)
  1202. {
  1203. CPAL_LOG("\n\rERROR : I2C Device BERR");
  1204. /* Clear error flag */
  1205. __CPAL_I2C_HAL_CLEAR_BERR(pDevInitStruct->CPAL_Dev);
  1206. #ifdef USE_MULTIPLE_ERROR_CALLBACK
  1207. /* Call Bus Error UserCallback */
  1208. CPAL_I2C_BERR_UserCallback(pDevInitStruct->CPAL_Dev);
  1209. #endif /* USE_MULTIPLE_ERROR_CALLBACK */
  1210. }
  1211. /* If Arbitration Loss error occurred --------------------------------------*/
  1212. if ((pDevInitStruct->wCPAL_DevError & CPAL_I2C_ERR_ARLO) != 0)
  1213. {
  1214. CPAL_LOG("\n\rERROR : I2C Device ARLO");
  1215. /* Clear error flag */
  1216. __CPAL_I2C_HAL_CLEAR_ARLO(pDevInitStruct->CPAL_Dev);
  1217. #ifdef USE_MULTIPLE_ERROR_CALLBACK
  1218. /* Call Arbitration Lost UserCallback */
  1219. CPAL_I2C_ARLO_UserCallback(pDevInitStruct->CPAL_Dev);
  1220. #endif /* USE_MULTIPLE_ERROR_CALLBACK */
  1221. }
  1222. /* If Overrun error occurred -----------------------------------------------*/
  1223. if ((pDevInitStruct->wCPAL_DevError & CPAL_I2C_ERR_OVR) != 0)
  1224. {
  1225. CPAL_LOG("\n\rERROR : I2C Device OVR");
  1226. /* No I2C software reset is performed here in order to allow user to get back
  1227. the last data received correctly */
  1228. /* Clear error flag */
  1229. __CPAL_I2C_HAL_CLEAR_OVR(pDevInitStruct->CPAL_Dev);
  1230. #ifdef USE_MULTIPLE_ERROR_CALLBACK
  1231. /* Call Overrun error UserCallback */
  1232. CPAL_I2C_OVR_UserCallback(pDevInitStruct->CPAL_Dev);
  1233. #endif /* USE_MULTIPLE_ERROR_CALLBACK */
  1234. }
  1235. /* USE_SINGLE_ERROR_CALLBACK is defined in stm32f0xx_i2c_cpal_conf.h file */
  1236. #ifdef USE_SINGLE_ERROR_CALLBACK
  1237. /* Call Error UserCallback */
  1238. CPAL_I2C_ERR_UserCallback(pDevInitStruct->CPAL_Dev, pDevInitStruct->wCPAL_DevError);
  1239. #endif /* USE_SINGLE_ERROR_CALLBACK */
  1240. return CPAL_PASS;
  1241. }
  1242. #ifdef CPAL_I2C_DMA_PROGMODEL
  1243. /**
  1244. * @brief Handle I2C DMA TX interrupt request when DMA programming Model is
  1245. * used for data transmission.
  1246. * @param pDevInitStruct: Pointer to the peripheral configuration structure.
  1247. * @retval CPAL_PASS.
  1248. */
  1249. uint32_t CPAL_I2C_DMA_TX_IRQHandler(CPAL_InitTypeDef* pDevInitStruct)
  1250. {
  1251. /* Reinitialize timeout value to default (no timeout initiated) */
  1252. pDevInitStruct->wCPAL_Timeout = CPAL_I2C_TIMEOUT_DEFAULT;
  1253. CPAL_LOG("\n\r\n\rLOG <CPAL_I2C_DMA_TX_IRQHandler> : I2C Device TX DMA ");
  1254. /*------------- If TC interrupt ------------*/
  1255. if((__CPAL_I2C_HAL_GET_DMATX_TCIT(pDevInitStruct->CPAL_Dev)) != 0)
  1256. {
  1257. CPAL_LOG("\n\rLOG : I2C Device TX Complete");
  1258. /* If DMA normal mode */
  1259. if ((pDevInitStruct->wCPAL_Options & CPAL_OPT_DMATX_CIRCULAR) == 0)
  1260. {
  1261. /* Update remaining number of data */
  1262. pDevInitStruct->pCPAL_TransferTx->wNumData = 0;
  1263. /* Call DMA TX TC UserCallback */
  1264. CPAL_I2C_DMATXTC_UserCallback(pDevInitStruct);
  1265. /* Disable DMA request and channel */
  1266. __CPAL_I2C_HAL_DISABLE_TXDMAREQ(pDevInitStruct->CPAL_Dev);
  1267. __CPAL_I2C_HAL_DISABLE_DMATX(pDevInitStruct->CPAL_Dev);
  1268. CPAL_LOG("\n\rLOG : I2C Device TX DMA Disabled");
  1269. }
  1270. /* If DMA circular mode */
  1271. else
  1272. {
  1273. /* Call DMA TX TC UserCallback */
  1274. CPAL_I2C_DMATXTC_UserCallback(pDevInitStruct);
  1275. }
  1276. }
  1277. /*------------- If HT interrupt ------------*/
  1278. else if ((__CPAL_I2C_HAL_GET_DMATX_HTIT(pDevInitStruct->CPAL_Dev)) != 0)
  1279. {
  1280. CPAL_LOG("\n\rLOG : I2C Device TX DMA Half Transfer ");
  1281. /* Call DMA TX HT UserCallback */
  1282. CPAL_I2C_DMATXHT_UserCallback(pDevInitStruct);
  1283. }
  1284. /*------------- If TE interrupt ------------*/
  1285. else if ((__CPAL_I2C_HAL_GET_DMATX_TEIT(pDevInitStruct->CPAL_Dev)) != 0)
  1286. {
  1287. CPAL_LOG("\n\rERROR : I2C Device TX DMA Transfer Error ");
  1288. /* Update CPAL_State to CPAL_STATE_ERROR */
  1289. pDevInitStruct->CPAL_State = CPAL_STATE_ERROR;
  1290. /* Update remaining number of data */
  1291. pDevInitStruct->pCPAL_TransferTx->wNumData = __CPAL_I2C_HAL_DMATX_GET_CNDT(pDevInitStruct->CPAL_Dev);
  1292. /* Call TX transfer complete UserCallback */
  1293. CPAL_I2C_RXTC_UserCallback(pDevInitStruct);
  1294. }
  1295. /* Clear DMA interrupt Flag */
  1296. __CPAL_I2C_HAL_CLEAR_DMATX_IT(pDevInitStruct->CPAL_Dev);
  1297. return CPAL_PASS;
  1298. }
  1299. /**
  1300. * @brief Handle I2C DMA RX interrupt request when DMA programming Model is
  1301. * used for data reception.
  1302. * @param pDevInitStruct: Pointer to the peripheral configuration structure.
  1303. * @retval CPAL_PASS.
  1304. */
  1305. uint32_t CPAL_I2C_DMA_RX_IRQHandler(CPAL_InitTypeDef* pDevInitStruct)
  1306. {
  1307. /* Reinitialize Timeout Value to default (no timeout initiated) */
  1308. pDevInitStruct->wCPAL_Timeout = CPAL_I2C_TIMEOUT_DEFAULT;
  1309. CPAL_LOG("\n\r\n\rLOG <CPAL_I2C_DMA_RX_IRQHandler> : I2C Device RX DMA ");
  1310. /*------------- If TC interrupt ------------*/
  1311. if ((__CPAL_I2C_HAL_GET_DMARX_TCIT(pDevInitStruct->CPAL_Dev)) != 0)
  1312. {
  1313. CPAL_LOG("\n\rLOG : I2C Device RX Complete");
  1314. /* If DMA normal mode */
  1315. if ((pDevInitStruct->wCPAL_Options & CPAL_OPT_DMARX_CIRCULAR) == 0)
  1316. {
  1317. /* Update remaining number of data */
  1318. pDevInitStruct->pCPAL_TransferRx->wNumData = 0;
  1319. /* Disable DMA Request and Channel */
  1320. __CPAL_I2C_HAL_DISABLE_RXDMAREQ(pDevInitStruct->CPAL_Dev);
  1321. __CPAL_I2C_HAL_DISABLE_DMARX(pDevInitStruct->CPAL_Dev);
  1322. CPAL_LOG("\n\rLOG : I2C Device RX DMA Disabled");
  1323. /* Call DMA RX TC UserCallback */
  1324. CPAL_I2C_DMARXTC_UserCallback(pDevInitStruct);
  1325. /* If No Stop Condition Generation option bit selected */
  1326. if (((pDevInitStruct->wCPAL_Options & CPAL_OPT_I2C_NOSTOP) != 0) && (pDevInitStruct->CPAL_Mode == CPAL_MODE_SLAVE))
  1327. {
  1328. /* Disable slave interrupt */
  1329. __CPAL_I2C_HAL_DISABLE_SLAVE_IT(pDevInitStruct->CPAL_Dev);
  1330. /* Update CPAL_State to CPAL_STATE_READY */
  1331. pDevInitStruct->CPAL_State = CPAL_STATE_READY;
  1332. /* Call TX transfer complete UserCallback */
  1333. CPAL_I2C_TXTC_UserCallback(pDevInitStruct);
  1334. }
  1335. }
  1336. /* If DMA circular mode */
  1337. else
  1338. {
  1339. /* Call DMA RX TC UserCallback */
  1340. CPAL_I2C_DMARXTC_UserCallback(pDevInitStruct);
  1341. }
  1342. }
  1343. /*------------- If HT interrupt ------------*/
  1344. else if ((__CPAL_I2C_HAL_GET_DMARX_HTIT(pDevInitStruct->CPAL_Dev)) != 0)
  1345. {
  1346. CPAL_LOG("\n\rLOG : I2C Device RX DMA Half Transfer");
  1347. /* Call DMA RX HT UserCallback */
  1348. CPAL_I2C_DMARXHT_UserCallback(pDevInitStruct);
  1349. }
  1350. /*------------- If TE interrupt ------------*/
  1351. else if ((__CPAL_I2C_HAL_GET_DMARX_TEIT(pDevInitStruct->CPAL_Dev)) != 0)
  1352. {
  1353. CPAL_LOG("\n\rERROR : I2C Device RX DMA Transfer Error ");
  1354. /* Update CPAL_State to CPAL_STATE_ERROR */
  1355. pDevInitStruct->CPAL_State = CPAL_STATE_ERROR;
  1356. /* Update remaining number of data */
  1357. pDevInitStruct->pCPAL_TransferRx->wNumData = __CPAL_I2C_HAL_DMARX_GET_CNDT(pDevInitStruct->CPAL_Dev);
  1358. /* Call DMA RX TE UserCallback */
  1359. CPAL_I2C_DMARXTE_UserCallback(pDevInitStruct);
  1360. }
  1361. /* Clear DMA interrupt Flag */
  1362. __CPAL_I2C_HAL_CLEAR_DMARX_IT(pDevInitStruct->CPAL_Dev);
  1363. return CPAL_PASS;
  1364. }
  1365. #endif /* CPAL_I2C_DMA_PROGMODEL */
  1366. /*================== CPAL_I2C_Timeout_Function ==================*/
  1367. /**
  1368. * @brief This function Manages I2C Timeouts when waiting for specific events.
  1369. * @param None
  1370. * @retval CPAL_PASS or CPAL_FAIL.
  1371. */
  1372. void CPAL_I2C_TIMEOUT_Manager(void)
  1373. {
  1374. uint32_t index = 0;
  1375. /* Manage I2C timeouts conditions */
  1376. for (index = 0; index < CPAL_I2C_DEV_NUM; index ++)
  1377. {
  1378. if (I2C_DevStructures[index] != pNULL)
  1379. {
  1380. /* If Timeout occurred */
  1381. if (I2C_DevStructures[index]->wCPAL_Timeout == CPAL_I2C_TIMEOUT_DETECTED)
  1382. {
  1383. /* Reinitialize timeout value */
  1384. I2C_DevStructures[index]->wCPAL_Timeout = CPAL_I2C_TIMEOUT_DEFAULT;
  1385. /* Update CPAL_State to CPAL_STATE_ERROR */
  1386. I2C_DevStructures[index]->CPAL_State = CPAL_STATE_ERROR;
  1387. /* In case of Device Error Timeout_Callback should not be called */
  1388. if (I2C_DevStructures[index]->wCPAL_DevError == CPAL_I2C_ERR_NONE)
  1389. {
  1390. /* Update wCPAL_DevError to CPAL_I2C_ERR_TIMEOUT */
  1391. I2C_DevStructures[index]->wCPAL_DevError = CPAL_I2C_ERR_TIMEOUT;
  1392. CPAL_LOG("\n\r\n\rLOG <CPAL_I2C_TIMEOUT_Manager> : I2C Device Timeout Error");
  1393. /* Call CPAL_TIMEOUT_UserCallback */
  1394. CPAL_TIMEOUT_UserCallback(I2C_DevStructures[index]);
  1395. }
  1396. }
  1397. /* If Timeout is triggered (wCPAL_Timeout != CPAL_I2C_TIMEOUT_DEFAULT)*/
  1398. else if (I2C_DevStructures[index]->wCPAL_Timeout != CPAL_I2C_TIMEOUT_DEFAULT)
  1399. {
  1400. /* Decrement the timeout value */
  1401. I2C_DevStructures[index]->wCPAL_Timeout--;
  1402. }
  1403. }
  1404. }
  1405. }
  1406. /**
  1407. * @brief This function Manages I2C Timeouts when Timeout occurred.
  1408. * @param pDevInitStruct: Pointer to the peripheral configuration structure.
  1409. * @retval CPAL_PASS or CPAL_FAIL.
  1410. */
  1411. uint32_t CPAL_I2C_Timeout (CPAL_InitTypeDef* pDevInitStruct)
  1412. {
  1413. /* Reinitialize timeout value */
  1414. pDevInitStruct->wCPAL_Timeout = CPAL_I2C_TIMEOUT_DEFAULT;
  1415. /* update CPAL_State to CPAL_STATE_ERROR */
  1416. pDevInitStruct->CPAL_State = CPAL_STATE_ERROR;
  1417. /* update wCPAL_DevError to CPAL_I2C_ERR_TIMEOUT */
  1418. pDevInitStruct->wCPAL_DevError = CPAL_I2C_ERR_TIMEOUT;
  1419. /* Call Timeout Callback and quit current function */
  1420. return (CPAL_TIMEOUT_UserCallback(pDevInitStruct));
  1421. }
  1422. /*================== CPAL_I2C_Event_Handler ==================*/
  1423. #ifdef CPAL_I2C_MASTER_MODE
  1424. /**
  1425. * @brief Handles Master TCR interrupt event.
  1426. * @param pDevInitStruct: Pointer to the peripheral configuration structure.
  1427. * @retval CPAL_PASS or CPAL_FAIL.
  1428. */
  1429. static uint32_t I2C_MASTER_TCR_Handle(CPAL_InitTypeDef* pDevInitStruct)
  1430. {
  1431. CR2_tmp = 0;
  1432. /* If DMA programming model */
  1433. if (pDevInitStruct->CPAL_ProgModel == CPAL_PROGMODEL_DMA)
  1434. {
  1435. /* If master transmitter */
  1436. if (pDevInitStruct->CPAL_State == CPAL_STATE_BUSY_TX)
  1437. {
  1438. /* Update wNumData */
  1439. pDevInitStruct->pCPAL_TransferTx->wNumData = pDevInitStruct->pCPAL_TransferTx->wNumData - 0xff;
  1440. }
  1441. /* If master receiver */
  1442. else
  1443. {
  1444. /* Update wNumData */
  1445. pDevInitStruct->pCPAL_TransferRx->wNumData = pDevInitStruct->pCPAL_TransferRx->wNumData - 0xff;
  1446. }
  1447. }
  1448. /* If master transmitter */
  1449. if (pDevInitStruct->CPAL_State == CPAL_STATE_BUSY_TX)
  1450. {
  1451. /* If remaining number of data is equal or lower than 255 */
  1452. if (pDevInitStruct->pCPAL_TransferTx->wNumData <= 0xff)
  1453. {
  1454. /* Update Num_Data */
  1455. Num_Data = pDevInitStruct->pCPAL_TransferTx->wNumData;
  1456. /* Set Nbytes to wNumData */
  1457. CR2_tmp |= (uint32_t)((uint32_t)(Num_Data) << 16);
  1458. /* Disable reload */
  1459. CR2_tmp &= ~I2C_CR2_RELOAD;
  1460. }
  1461. /* If remaining number of data is greater than 255 */
  1462. else
  1463. {
  1464. /* Set Nbytes to wNumData */
  1465. CR2_tmp |= (uint32_t)((uint32_t)(255) << 16);
  1466. /* Enaable reload */
  1467. CR2_tmp |= I2C_CR2_RELOAD;
  1468. }
  1469. }
  1470. /* If master receiver */
  1471. else
  1472. {
  1473. /* If remaining number of data is equal or lower than 255 */
  1474. if (pDevInitStruct->pCPAL_TransferRx->wNumData <= 0xff)
  1475. {
  1476. /* Update num data */
  1477. Num_Data = pDevInitStruct->pCPAL_TransferRx->wNumData;
  1478. /* Set Nbytes to wNumData */
  1479. CR2_tmp |= (uint32_t)((uint32_t)(Num_Data) << 16);
  1480. /* Disable reload */
  1481. CR2_tmp &= ~I2C_CR2_RELOAD;
  1482. }
  1483. /* If remaining number of data is greater than 255 */
  1484. else
  1485. {
  1486. /* Set Nbytes to wNumData */
  1487. CR2_tmp |= (uint32_t)((uint32_t)(255) << 16);
  1488. /* Enaable reload */
  1489. CR2_tmp |= I2C_CR2_RELOAD;
  1490. }
  1491. }
  1492. /* Update CR2 Register */
  1493. __CPAL_I2C_HAL_CR2_UPDATE(pDevInitStruct->CPAL_Dev, CR2_tmp);
  1494. return CPAL_PASS;
  1495. }
  1496. /**
  1497. * @brief Handles Master TC interrupt event.
  1498. * @param pDevInitStruct: Pointer to the peripheral configuration structure.
  1499. * @retval CPAL_PASS or CPAL_FAIL.
  1500. */
  1501. static uint32_t I2C_MASTER_TC_Handle(CPAL_InitTypeDef* pDevInitStruct)
  1502. {
  1503. /* No Stop Condition Generation option bit is not selected */
  1504. if ((pDevInitStruct->wCPAL_Options & CPAL_OPT_I2C_NOSTOP) == 0)
  1505. {
  1506. /* Generate stop condition */
  1507. __CPAL_I2C_HAL_STOP(pDevInitStruct->CPAL_Dev);
  1508. }
  1509. /* No Stop Condition Generation option bit is selected */
  1510. else
  1511. {
  1512. /* Disable master interrupts */
  1513. __CPAL_I2C_HAL_DISABLE_MASTER_IT(pDevInitStruct->CPAL_Dev);
  1514. /* If master transmitter */
  1515. if (pDevInitStruct->CPAL_State == CPAL_STATE_BUSY_TX)
  1516. {
  1517. #ifdef CPAL_I2C_IT_PROGMODEL
  1518. /* If Interrupt Programming Model */
  1519. if (pDevInitStruct->CPAL_ProgModel == CPAL_PROGMODEL_INTERRUPT)
  1520. {
  1521. /* Disable TX interrupt */
  1522. __CPAL_I2C_HAL_DISABLE_TXIE_IT(pDevInitStruct->CPAL_Dev);
  1523. }
  1524. #endif /* CPAL_I2C_IT_PROGMODEL */
  1525. /* Update CPAL_State to CPAL_STATE_READY */
  1526. pDevInitStruct->CPAL_State = CPAL_STATE_READY;
  1527. /* Call TX Transfer complete Callback */
  1528. CPAL_I2C_TXTC_UserCallback(pDevInitStruct);
  1529. }
  1530. /* If master receiver */
  1531. else
  1532. {
  1533. #ifdef CPAL_I2C_IT_PROGMODEL
  1534. /* If Interrupt Programming Model */
  1535. if (pDevInitStruct->CPAL_ProgModel == CPAL_PROGMODEL_INTERRUPT)
  1536. {
  1537. /* Disable RX interrupt */
  1538. __CPAL_I2C_HAL_DISABLE_RXIE_IT(pDevInitStruct->CPAL_Dev);
  1539. }
  1540. #endif /* CPAL_I2C_IT_PROGMODEL */
  1541. /* Update CPAL_State to CPAL_STATE_READY */
  1542. pDevInitStruct->CPAL_State = CPAL_STATE_READY;
  1543. /* Call RX Transfer complete Callback */
  1544. CPAL_I2C_RXTC_UserCallback(pDevInitStruct);
  1545. }
  1546. }
  1547. return CPAL_PASS;
  1548. }
  1549. /**
  1550. * @brief Handles Master STOP interrupt event.
  1551. * @param pDevInitStruct: Pointer to the peripheral configuration structure.
  1552. * @retval CPAL_PASS or CPAL_FAIL.
  1553. */
  1554. static uint32_t I2C_MASTER_STOP_Handle(CPAL_InitTypeDef* pDevInitStruct)
  1555. {
  1556. /* If NACK received by master */
  1557. if (pDevInitStruct->wCPAL_DevError == CPAL_I2C_ERR_AF)
  1558. {
  1559. /* Set CPAL_State to CPAL_STATE_ERROR */
  1560. pDevInitStruct->CPAL_State = CPAL_STATE_ERROR;
  1561. /* Clear STOP flag */
  1562. __CPAL_I2C_HAL_CLEAR_STOP(pDevInitStruct->CPAL_Dev);
  1563. }
  1564. else
  1565. {
  1566. /* Clear STOP flag */
  1567. __CPAL_I2C_HAL_CLEAR_STOP(pDevInitStruct->CPAL_Dev);
  1568. CPAL_LOG("\n\r\n\rLOG <I2C_EV_IRQHandler> : I2C Device Master IT");
  1569. CPAL_LOG("\n\rLOG : I2C Device Stop Generated");
  1570. /* Disable master interrupt */
  1571. __CPAL_I2C_HAL_DISABLE_MASTER_IT(pDevInitStruct->CPAL_Dev);
  1572. /* Wait until BUSY flag is reset */
  1573. __CPAL_I2C_TIMEOUT(!(__CPAL_I2C_HAL_GET_BUSY(pDevInitStruct->CPAL_Dev)), CPAL_I2C_TIMEOUT_BUSY);
  1574. /* If master transmitter */
  1575. if (pDevInitStruct->CPAL_State == CPAL_STATE_BUSY_TX)
  1576. {
  1577. #ifdef CPAL_I2C_IT_PROGMODEL
  1578. /* If Interrupt Programming Model */
  1579. if (pDevInitStruct->CPAL_ProgModel == CPAL_PROGMODEL_INTERRUPT)
  1580. {
  1581. /* Disable TX interrupt */
  1582. __CPAL_I2C_HAL_DISABLE_TXIE_IT(pDevInitStruct->CPAL_Dev);
  1583. }
  1584. #endif /* CPAL_I2C_IT_PROGMODEL */
  1585. /* Update CPAL_State to CPAL_STATE_READY */
  1586. pDevInitStruct->CPAL_State = CPAL_STATE_READY;
  1587. /* Call TX Transfer complete Callback */
  1588. CPAL_I2C_TXTC_UserCallback(pDevInitStruct);
  1589. }
  1590. /* If master receiver */
  1591. else
  1592. {
  1593. #ifdef CPAL_I2C_IT_PROGMODEL
  1594. /* If Interrupt Programming Model */
  1595. if (pDevInitStruct->CPAL_ProgModel == CPAL_PROGMODEL_INTERRUPT)
  1596. {
  1597. /* Disable RX interrupt */
  1598. __CPAL_I2C_HAL_DISABLE_RXIE_IT(pDevInitStruct->CPAL_Dev);
  1599. }
  1600. #endif /* CPAL_I2C_IT_PROGMODEL */
  1601. /* Update CPAL_State to CPAL_STATE_READY */
  1602. pDevInitStruct->CPAL_State = CPAL_STATE_READY;
  1603. /* Call RX Transfer complete Callback */
  1604. CPAL_I2C_RXTC_UserCallback(pDevInitStruct);
  1605. }
  1606. }
  1607. return CPAL_PASS;
  1608. }
  1609. /**
  1610. * @brief Handles Master NACK interrupt event.
  1611. * @param pDevInitStruct: Pointer to the peripheral configuration structure.
  1612. * @retval CPAL_PASS or CPAL_FAIL.
  1613. */
  1614. static uint32_t I2C_MASTER_NACK_Handle(CPAL_InitTypeDef* pDevInitStruct)
  1615. {
  1616. /* Update wCPAL_DevError */
  1617. pDevInitStruct->wCPAL_DevError = CPAL_I2C_ERR_AF;
  1618. /* Clear NACK flag */
  1619. __CPAL_I2C_HAL_CLEAR_NACK(pDevInitStruct->CPAL_Dev);
  1620. /* USE_SINGLE_ERROR_CALLBACK is defined in stm32f0xx_i2c_cpal_conf.h file */
  1621. #ifdef USE_SINGLE_ERROR_CALLBACK
  1622. /* Call Error UserCallback */
  1623. CPAL_I2C_ERR_UserCallback(pDevInitStruct->CPAL_Dev, pDevInitStruct->wCPAL_DevError);
  1624. #elif defined(USE_MULTIPLE_ERROR_CALLBACK)
  1625. /* Call AF UserCallback */
  1626. CPAL_I2C_AF_UserCallback(pDevInitStruct->CPAL_Dev);
  1627. #endif /* USE_SINGLE_ERROR_CALLBACK */
  1628. return CPAL_PASS;
  1629. }
  1630. #ifdef CPAL_I2C_IT_PROGMODEL
  1631. /**
  1632. * @brief Handles Master transmission TXIS interrupt event.
  1633. * @param pDevInitStruct: Pointer to the peripheral configuration structure.
  1634. * @retval CPAL_PASS or CPAL_FAIL.
  1635. */
  1636. static uint32_t I2C_MASTER_TXIS_Handle(CPAL_InitTypeDef* pDevInitStruct)
  1637. {
  1638. /* Call TX UserCallback */
  1639. CPAL_I2C_TX_UserCallback(pDevInitStruct);
  1640. /* Write Byte */
  1641. __CPAL_I2C_HAL_SEND((pDevInitStruct->CPAL_Dev), (*(pDevInitStruct->pCPAL_TransferTx->pbBuffer)));
  1642. /* Decrement remaining number of data */
  1643. pDevInitStruct->pCPAL_TransferTx->wNumData--;
  1644. if (pDevInitStruct->pCPAL_TransferTx->wNumData != 0)
  1645. {
  1646. /* Point to next data */
  1647. pDevInitStruct->pCPAL_TransferTx->pbBuffer++;
  1648. }
  1649. return CPAL_PASS;
  1650. }
  1651. /**
  1652. * @brief Handles Master reception RXNE interrupt event.
  1653. * @param pDevInitStruct: Pointer to the peripheral configuration structure.
  1654. * @retval CPAL_PASS or CPAL_FAIL.
  1655. */
  1656. static uint32_t I2C_MASTER_RXNE_Handle(CPAL_InitTypeDef* pDevInitStruct)
  1657. {
  1658. /* Read Byte */
  1659. *(pDevInitStruct->pCPAL_TransferRx->pbBuffer) = __CPAL_I2C_HAL_RECEIVE(pDevInitStruct->CPAL_Dev);
  1660. /* Call RX UserCallback */
  1661. CPAL_I2C_RX_UserCallback(pDevInitStruct);
  1662. /* Decrement remaining number of data */
  1663. pDevInitStruct->pCPAL_TransferRx->wNumData--;
  1664. /* If data remaining for reception */
  1665. if (pDevInitStruct->pCPAL_TransferRx->wNumData != 0)
  1666. {
  1667. /* Point to next data */
  1668. pDevInitStruct->pCPAL_TransferRx->pbBuffer++;
  1669. }
  1670. return CPAL_PASS;
  1671. }
  1672. #endif /* CPAL_I2C_IT_PROGMODEL */
  1673. #endif /* CPAL_I2C_MASTER_MODE */
  1674. #ifdef CPAL_I2C_SLAVE_MODE
  1675. /**
  1676. * @brief Handles Slave ADDR interrupt event.
  1677. * @param pDevInitStruct: Pointer to the peripheral configuration structure.
  1678. * @retval CPAL_PASS or CPAL_FAIL.
  1679. */
  1680. static uint32_t I2C_SLAVE_ADDR_Handle(CPAL_InitTypeDef* pDevInitStruct)
  1681. {
  1682. #ifdef CPAL_I2C_LISTEN_MODE
  1683. /* If slave receive request for write */
  1684. if (__CPAL_I2C_HAL_GET_DIR(pDevInitStruct->CPAL_Dev) == 0)
  1685. {
  1686. pDevInitStruct->CPAL_State = CPAL_STATE_BUSY_RX;
  1687. /* Call Slave receive UserCallback */
  1688. CPAL_I2C_SLAVE_READ_UserCallback(pDevInitStruct);
  1689. }
  1690. /* If slave receive request for read */
  1691. else
  1692. {
  1693. pDevInitStruct->CPAL_State = CPAL_STATE_BUSY_TX;
  1694. /* Call Slave Transmit UserCallback */
  1695. CPAL_I2C_SLAVE_WRITE_UserCallback(pDevInitStruct);
  1696. }
  1697. #else
  1698. uint32_t slaveaddr = 0;
  1699. /* If 7 Bit Addressing Mode */
  1700. if (pDevInitStruct->pCPAL_I2C_Struct->I2C_AcknowledgedAddress == I2C_AcknowledgedAddress_7bit)
  1701. {
  1702. /* Get slave matched address */
  1703. slaveaddr = __CPAL_I2C_HAL_GET_ADDCODE(pDevInitStruct->CPAL_Dev);
  1704. /* if matched address is not equal to OA1 */
  1705. if (slaveaddr !=__CPAL_I2C_HAL_GET_OA1(pDevInitStruct->CPAL_Dev))
  1706. {
  1707. /* If General Call addressing mode selected */
  1708. if ( slaveaddr == 0x00000000)
  1709. {
  1710. CPAL_LOG("\n\rLOG : I2C Device GENCALL Mode");
  1711. /* Call GENCALL UserCallback */
  1712. CPAL_I2C_GENCALL_UserCallback(pDevInitStruct);
  1713. }
  1714. /* If DUAL addressing mode selected */
  1715. else
  1716. {
  1717. CPAL_LOG("\n\rLOG : I2C Device DUAL ADDR Mode Selected");
  1718. /* Call DUALF UserCallback */
  1719. CPAL_I2C_DUALF_UserCallback(pDevInitStruct);
  1720. }
  1721. }
  1722. }
  1723. #endif /* CPAL_I2C_LISTEN_MODE */
  1724. /* Clear ADDR flag */
  1725. __CPAL_I2C_HAL_CLEAR_ADDR(pDevInitStruct->CPAL_Dev);
  1726. return CPAL_PASS;
  1727. }
  1728. /**
  1729. * @brief Handles Slave STOP interrupt event.
  1730. * @param pDevInitStruct: Pointer to the peripheral configuration structure.
  1731. * @retval CPAL_PASS or CPAL_FAIL.
  1732. */
  1733. static uint32_t I2C_SLAVE_STOP_Handle(CPAL_InitTypeDef* pDevInitStruct)
  1734. {
  1735. /* Clear STOP flag */
  1736. __CPAL_I2C_HAL_CLEAR_STOP(pDevInitStruct->CPAL_Dev);
  1737. CPAL_LOG("\n\r\n\rLOG <I2C_EV_IRQHandler> : I2C Device Slave IT");
  1738. CPAL_LOG("\n\rLOG : I2C Device Stop Detected");
  1739. /* Disable slave interrupt */
  1740. __CPAL_I2C_HAL_DISABLE_SLAVE_IT(pDevInitStruct->CPAL_Dev);
  1741. /* Wait until BUSY flag is reset */
  1742. __CPAL_I2C_TIMEOUT(!(__CPAL_I2C_HAL_GET_BUSY(pDevInitStruct->CPAL_Dev)), CPAL_I2C_TIMEOUT_BUSY);
  1743. /* If NACK Slave Own Address option bit selected */
  1744. if ((pDevInitStruct->wCPAL_Options & CPAL_OPT_I2C_NACK_ADD) != 0)
  1745. {
  1746. /* Disable Acknowledgement of own Address */
  1747. __CPAL_I2C_HAL_DISABLE_DEV(pDevInitStruct->CPAL_Dev);
  1748. }
  1749. /* If slave transmitter */
  1750. if (pDevInitStruct->CPAL_State == CPAL_STATE_BUSY_TX)
  1751. {
  1752. #ifdef CPAL_I2C_IT_PROGMODEL
  1753. /* If Interrupt Programming Model */
  1754. if (pDevInitStruct->CPAL_ProgModel == CPAL_PROGMODEL_INTERRUPT)
  1755. {
  1756. /* Disable TX interrupt */
  1757. __CPAL_I2C_HAL_DISABLE_TXIE_IT(pDevInitStruct->CPAL_Dev);
  1758. }
  1759. #endif /* CPAL_I2C_IT_PROGMODEL */
  1760. /* Update CPAL_State to CPAL_STATE_READY */
  1761. pDevInitStruct->CPAL_State = CPAL_STATE_READY;
  1762. /* Call TX Transfer complete Callback */
  1763. CPAL_I2C_TXTC_UserCallback(pDevInitStruct);
  1764. }
  1765. /* If slave receiver */
  1766. else
  1767. {
  1768. #ifdef CPAL_I2C_IT_PROGMODEL
  1769. /* If Interrupt Programming Model */
  1770. if (pDevInitStruct->CPAL_ProgModel == CPAL_PROGMODEL_INTERRUPT)
  1771. {
  1772. /* Disable RX interrupt */
  1773. __CPAL_I2C_HAL_DISABLE_RXIE_IT(pDevInitStruct->CPAL_Dev);
  1774. }
  1775. #endif /* CPAL_I2C_IT_PROGMODEL */
  1776. /* Update CPAL_State to CPAL_STATE_READY */
  1777. pDevInitStruct->CPAL_State = CPAL_STATE_READY;
  1778. /* Call RX Transfer complete Callback */
  1779. CPAL_I2C_RXTC_UserCallback(pDevInitStruct);
  1780. }
  1781. return CPAL_PASS;
  1782. }
  1783. /**
  1784. * @brief Handles Slave NACK interrupt event.
  1785. * @param pDevInitStruct: Pointer to the peripheral configuration structure.
  1786. * @retval CPAL_PASS or CPAL_FAIL.
  1787. */
  1788. static uint32_t I2C_SLAVE_NACK_Handle(CPAL_InitTypeDef* pDevInitStruct)
  1789. {
  1790. /* Clear NACK flag */
  1791. __CPAL_I2C_HAL_CLEAR_NACK(pDevInitStruct->CPAL_Dev);
  1792. /* No Stop Condition Generation option bit selected and slave transmitter */
  1793. if (((pDevInitStruct->wCPAL_Options & CPAL_OPT_I2C_NOSTOP) != 0) && (pDevInitStruct->CPAL_State == CPAL_STATE_BUSY_TX))
  1794. {
  1795. /* Disable slave interrupt */
  1796. __CPAL_I2C_HAL_DISABLE_SLAVE_IT(pDevInitStruct->CPAL_Dev);
  1797. #ifdef CPAL_I2C_IT_PROGMODEL
  1798. /* If Interrupt Programming Model */
  1799. if (pDevInitStruct->CPAL_ProgModel == CPAL_PROGMODEL_INTERRUPT)
  1800. {
  1801. /* Disable TX interrupt */
  1802. __CPAL_I2C_HAL_DISABLE_TXIE_IT(pDevInitStruct->CPAL_Dev);
  1803. }
  1804. #endif /* CPAL_I2C_IT_PROGMODEL */
  1805. /* Update CPAL_State to CPAL_STATE_READY */
  1806. pDevInitStruct->CPAL_State = CPAL_STATE_READY;
  1807. /* Call TX Transfer complete Callback */
  1808. CPAL_I2C_TXTC_UserCallback(pDevInitStruct);
  1809. }
  1810. return CPAL_PASS;
  1811. }
  1812. #ifdef CPAL_I2C_IT_PROGMODEL
  1813. /**
  1814. * @brief Handles Slave transmission TXIS interrupt event.
  1815. * @param pDevInitStruct: Pointer to the peripheral configuration structure.
  1816. * @retval CPAL_PASS or CPAL_FAIL.
  1817. */
  1818. static uint32_t I2C_SLAVE_TXIS_Handle(CPAL_InitTypeDef* pDevInitStruct)
  1819. {
  1820. /* If data remaining for transmission */
  1821. if (pDevInitStruct->pCPAL_TransferTx->wNumData != 0)
  1822. {
  1823. /* Call TX UserCallback */
  1824. CPAL_I2C_TX_UserCallback(pDevInitStruct);
  1825. /* Write Byte */
  1826. __CPAL_I2C_HAL_SEND((pDevInitStruct->CPAL_Dev), (*(pDevInitStruct->pCPAL_TransferTx->pbBuffer)));
  1827. /* Decrement remaining number of data */
  1828. pDevInitStruct->pCPAL_TransferTx->wNumData--;
  1829. if (pDevInitStruct->pCPAL_TransferTx->wNumData != 0)
  1830. {
  1831. /* Point to next data */
  1832. pDevInitStruct->pCPAL_TransferTx->pbBuffer++;
  1833. }
  1834. }
  1835. return CPAL_PASS;
  1836. }
  1837. /**
  1838. * @brief Handles Slave reception RXNE interrupt event.
  1839. * @param pDevInitStruct: Pointer to the peripheral configuration structure.
  1840. * @retval CPAL_PASS or CPAL_FAIL.
  1841. */
  1842. static uint32_t I2C_SLAVE_RXNE_Handle(CPAL_InitTypeDef* pDevInitStruct)
  1843. {
  1844. /* If data remaining for reception */
  1845. if (pDevInitStruct->pCPAL_TransferRx->wNumData != 0)
  1846. {
  1847. /* Read Byte */
  1848. *(pDevInitStruct->pCPAL_TransferRx->pbBuffer) = __CPAL_I2C_HAL_RECEIVE(pDevInitStruct->CPAL_Dev);
  1849. /* Call RX UserCallback */
  1850. CPAL_I2C_RX_UserCallback(pDevInitStruct);
  1851. /* Decrement remaining number of data */
  1852. pDevInitStruct->pCPAL_TransferRx->wNumData--;
  1853. /* If data remaining for reception */
  1854. if (pDevInitStruct->pCPAL_TransferRx->wNumData != 0)
  1855. {
  1856. /* Point to next data */
  1857. pDevInitStruct->pCPAL_TransferRx->pbBuffer++;
  1858. }
  1859. /* If all data received and No Stop Condition Generation option bit selected */
  1860. else if ((pDevInitStruct->wCPAL_Options & CPAL_OPT_I2C_NOSTOP) != 0)
  1861. {
  1862. /* Disable slave interrupt */
  1863. __CPAL_I2C_HAL_DISABLE_SLAVE_IT(pDevInitStruct->CPAL_Dev);
  1864. /* Disable RX interrupt */
  1865. __CPAL_I2C_HAL_DISABLE_RXIE_IT(pDevInitStruct->CPAL_Dev);
  1866. /* Update CPAL_State to CPAL_STATE_READY */
  1867. pDevInitStruct->CPAL_State = CPAL_STATE_READY;
  1868. /* Call RX Transfer complete Callback */
  1869. CPAL_I2C_RXTC_UserCallback(pDevInitStruct);
  1870. }
  1871. }
  1872. return CPAL_PASS;
  1873. }
  1874. #endif /* CPAL_I2C_IT_PROGMODEL */
  1875. #endif /* CPAL_I2C_SLAVE_MODE */
  1876. /*================== Local DMA and IT Manager ==================*/
  1877. #ifdef CPAL_I2C_DMA_PROGMODEL
  1878. /**
  1879. * @brief This function Configures and enables I2C DMA before starting transfer phase.
  1880. * @param pDevInitStruct: Pointer to the peripheral configuration structure.
  1881. * @param Direction : Transfer direction.
  1882. * @retval CPAL_PASS or CPAL_FAIL.
  1883. */
  1884. static uint32_t I2C_Enable_DMA (CPAL_InitTypeDef* pDevInitStruct, CPAL_DirectionTypeDef Direction)
  1885. {
  1886. /* If data transmission will be performed */
  1887. if ((pDevInitStruct->CPAL_State == CPAL_STATE_BUSY_TX) || (Direction == CPAL_DIRECTION_TX))
  1888. {
  1889. /* Configure TX DMA channels */
  1890. CPAL_I2C_HAL_DMATXConfig(pDevInitStruct->CPAL_Dev, pDevInitStruct->pCPAL_TransferTx, pDevInitStruct->wCPAL_Options);
  1891. /* Enable TX DMA channels */
  1892. __CPAL_I2C_HAL_ENABLE_DMATX(pDevInitStruct->CPAL_Dev);
  1893. CPAL_LOG("\n\rLOG : I2C Device DMA TX Configured and Enabled");
  1894. }
  1895. /* If data reception will be performed */
  1896. else if ((pDevInitStruct->CPAL_State == CPAL_STATE_BUSY_RX) || (Direction == CPAL_DIRECTION_RX))
  1897. {
  1898. /* Configure RX DMA channels */
  1899. CPAL_I2C_HAL_DMARXConfig(pDevInitStruct->CPAL_Dev, pDevInitStruct->pCPAL_TransferRx, pDevInitStruct->wCPAL_Options);
  1900. /* Enable RX DMA channels */
  1901. __CPAL_I2C_HAL_ENABLE_DMARX(pDevInitStruct->CPAL_Dev);
  1902. CPAL_LOG("\n\rLOG : I2C Device DMA RX Configured and Enabled");
  1903. }
  1904. return CPAL_PASS;
  1905. }
  1906. #endif /* CPAL_I2C_DMA_PROGMODEL */
  1907. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/