stm32f4xx_fmpi2c.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_fmpi2c.c
  4. * @author MCD Application Team
  5. * @version V1.5.0
  6. * @date 06-March-2015
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Inter-Integrated circuit Fast Mode Plus (FMPI2C):
  9. * + Initialization and Configuration
  10. * + Communications handling
  11. * + SMBUS management
  12. * + FMPI2C registers management
  13. * + Data transfers management
  14. * + DMA transfers management
  15. * + Interrupts and flags management
  16. *
  17. * @verbatim
  18. ============================================================================
  19. ##### How to use this driver #####
  20. ============================================================================
  21. [..]
  22. (#) Enable peripheral clock using RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2Cx, ENABLE)
  23. function for FMPI2C peripheral.
  24. (#) Enable SDA, SCL and SMBA (when used) GPIO clocks using
  25. RCC_AHBPeriphClockCmd() function.
  26. (#) Peripherals alternate function:
  27. (++) Connect the pin to the desired peripherals' Alternate
  28. Function (AF) using GPIO_PinAFConfig() function.
  29. (++) Configure the desired pin in alternate function by:
  30. GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
  31. (++) Select the type, OpenDrain and speed via
  32. GPIO_PuPd, GPIO_OType and GPIO_Speed members
  33. (++) Call GPIO_Init() function.
  34. (#) Program the Mode, Timing , Own address, Ack and Acknowledged Address
  35. using the FMPI2C_Init() function.
  36. (#) Optionally you can enable/configure the following parameters without
  37. re-initialization (i.e there is no need to call again FMPI2C_Init() function):
  38. (++) Enable the acknowledge feature using FMPI2C_AcknowledgeConfig() function.
  39. (++) Enable the dual addressing mode using FMPI2C_DualAddressCmd() function.
  40. (++) Enable the general call using the FMPI2C_GeneralCallCmd() function.
  41. (++) Enable the clock stretching using FMPI2C_StretchClockCmd() function.
  42. (++) Enable the PEC Calculation using FMPI2C_CalculatePEC() function.
  43. (++) For SMBus Mode:
  44. (+++) Enable the SMBusAlert pin using FMPI2C_SMBusAlertCmd() function.
  45. (#) Enable the NVIC and the corresponding interrupt using the function
  46. FMPI2C_ITConfig() if you need to use interrupt mode.
  47. (#) When using the DMA mode
  48. (++) Configure the DMA using DMA_Init() function.
  49. (++) Active the needed channel Request using FMPI2C_DMACmd() function.
  50. (#) Enable the FMPI2C using the FMPI2C_Cmd() function.
  51. (#) Enable the DMA using the DMA_Cmd() function when using DMA mode in the
  52. transfers.
  53. [..]
  54. (@) When using FMPI2C in Fast Mode Plus, SCL and SDA pin 20mA current drive capability
  55. must be enabled by setting the driving capability control bit in SYSCFG.
  56. @endverbatim
  57. ******************************************************************************
  58. * @attention
  59. *
  60. * <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
  61. *
  62. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  63. * You may not use this file except in compliance with the License.
  64. * You may obtain a copy of the License at:
  65. *
  66. * http://www.st.com/software_license_agreement_liberty_v2
  67. *
  68. * Unless required by applicable law or agreed to in writing, software
  69. * distributed under the License is distributed on an "AS IS" BASIS,
  70. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  71. * See the License for the specific language governing permissions and
  72. * limitations under the License.
  73. *
  74. ******************************************************************************
  75. */
  76. /* Includes ------------------------------------------------------------------*/
  77. #include "stm32f4xx_FMPI2C.h"
  78. #include "stm32f4xx_rcc.h"
  79. /** @addtogroup STM32F30x_StdPeriph_Driver
  80. * @{
  81. */
  82. /** @defgroup FMPI2C
  83. * @brief FMPI2C driver modules
  84. * @{
  85. */
  86. /* Private typedef -----------------------------------------------------------*/
  87. /* Private define ------------------------------------------------------------*/
  88. #define CR1_CLEAR_MASK ((uint32_t)0x00CFE0FF) /*<! FMPI2C CR1 clear register Mask */
  89. #define CR2_CLEAR_MASK ((uint32_t)0x07FF7FFF) /*<! FMPI2C CR2 clear register Mask */
  90. #define TIMING_CLEAR_MASK ((uint32_t)0xF0FFFFFF) /*<! FMPI2C TIMING clear register Mask */
  91. #define ERROR_IT_MASK ((uint32_t)0x00003F00) /*<! FMPI2C Error interrupt register Mask */
  92. #define TC_IT_MASK ((uint32_t)0x000000C0) /*<! FMPI2C TC interrupt register Mask */
  93. /* Private macro -------------------------------------------------------------*/
  94. /* Private variables ---------------------------------------------------------*/
  95. /* Private function prototypes -----------------------------------------------*/
  96. /* Private functions ---------------------------------------------------------*/
  97. /** @defgroup FMPI2C_Private_Functions
  98. * @{
  99. */
  100. /** @defgroup FMPI2C_Group1 Initialization and Configuration functions
  101. * @brief Initialization and Configuration functions
  102. *
  103. @verbatim
  104. ===============================================================================
  105. ##### Initialization and Configuration functions #####
  106. ===============================================================================
  107. [..] This section provides a set of functions allowing to initialize the FMPI2C Mode,
  108. FMPI2C Timing, FMPI2C filters, FMPI2C Addressing mode, FMPI2C OwnAddress1.
  109. [..] The FMPI2C_Init() function follows the FMPI2C configuration procedures (these procedures
  110. are available in reference manual).
  111. [..] When the Software Reset is performed using FMPI2C_SoftwareResetCmd() function, the internal
  112. states machines are reset and communication control bits, as well as status bits come
  113. back to their reset value.
  114. [..] Before enabling Stop mode using FMPI2C_StopModeCmd() FMPI2C Clock source must be set to
  115. HSI and Digital filters must be disabled.
  116. [..] Before enabling Own Address 2 via FMPI2C_DualAddressCmd() function, OA2 and mask should be
  117. configured using FMPI2C_OwnAddress2Config() function.
  118. [..] FMPI2C_SlaveByteControlCmd() enable Slave byte control that allow user to get control of
  119. each byte in slave mode when NBYTES is set to 0x01.
  120. @endverbatim
  121. * @{
  122. */
  123. /**
  124. * @brief Deinitializes the FMPI2Cx peripheral registers to their default reset values.
  125. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  126. * @retval None
  127. */
  128. void FMPI2C_DeInit(FMPI2C_TypeDef* FMPI2Cx)
  129. {
  130. /* Check the parameters */
  131. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  132. if (FMPI2Cx == FMPI2C1)
  133. {
  134. /* Enable FMPI2C1 reset state */
  135. RCC_APB1PeriphResetCmd(RCC_APB1Periph_FMPI2C1, ENABLE);
  136. /* Release FMPI2C1 from reset state */
  137. RCC_APB1PeriphResetCmd(RCC_APB1Periph_FMPI2C1, DISABLE);
  138. }
  139. }
  140. /**
  141. * @brief Initializes the FMPI2Cx peripheral according to the specified
  142. * parameters in the FMPI2C_InitStruct.
  143. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  144. * @param FMPI2C_InitStruct: pointer to a FMPI2C_InitTypeDef structure that
  145. * contains the configuration information for the specified FMPI2C peripheral.
  146. * @retval None
  147. */
  148. void FMPI2C_Init(FMPI2C_TypeDef* FMPI2Cx, FMPI2C_InitTypeDef* FMPI2C_InitStruct)
  149. {
  150. uint32_t tmpreg = 0;
  151. /* Check the parameters */
  152. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  153. assert_param(IS_FMPI2C_ANALOG_FILTER(FMPI2C_InitStruct->FMPI2C_AnalogFilter));
  154. assert_param(IS_FMPI2C_DIGITAL_FILTER(FMPI2C_InitStruct->FMPI2C_DigitalFilter));
  155. assert_param(IS_FMPI2C_MODE(FMPI2C_InitStruct->FMPI2C_Mode));
  156. assert_param(IS_FMPI2C_OWN_ADDRESS1(FMPI2C_InitStruct->FMPI2C_OwnAddress1));
  157. assert_param(IS_FMPI2C_ACK(FMPI2C_InitStruct->FMPI2C_Ack));
  158. assert_param(IS_FMPI2C_ACKNOWLEDGE_ADDRESS(FMPI2C_InitStruct->FMPI2C_AcknowledgedAddress));
  159. /* Disable FMPI2Cx Peripheral */
  160. FMPI2Cx->CR1 &= (uint32_t)~((uint32_t)FMPI2C_CR1_PE);
  161. /*---------------------------- FMPI2Cx FILTERS Configuration ------------------*/
  162. /* Get the FMPI2Cx CR1 value */
  163. tmpreg = FMPI2Cx->CR1;
  164. /* Clear FMPI2Cx CR1 register */
  165. tmpreg &= CR1_CLEAR_MASK;
  166. /* Configure FMPI2Cx: analog and digital filter */
  167. /* Set ANFOFF bit according to FMPI2C_AnalogFilter value */
  168. /* Set DFN bits according to FMPI2C_DigitalFilter value */
  169. tmpreg |= (uint32_t)FMPI2C_InitStruct->FMPI2C_AnalogFilter |(FMPI2C_InitStruct->FMPI2C_DigitalFilter << 8);
  170. /* Write to FMPI2Cx CR1 */
  171. FMPI2Cx->CR1 = tmpreg;
  172. /*---------------------------- FMPI2Cx TIMING Configuration -------------------*/
  173. /* Configure FMPI2Cx: Timing */
  174. /* Set TIMINGR bits according to FMPI2C_Timing */
  175. /* Write to FMPI2Cx TIMING */
  176. FMPI2Cx->TIMINGR = FMPI2C_InitStruct->FMPI2C_Timing & TIMING_CLEAR_MASK;
  177. /* Enable FMPI2Cx Peripheral */
  178. FMPI2Cx->CR1 |= FMPI2C_CR1_PE;
  179. /*---------------------------- FMPI2Cx OAR1 Configuration ---------------------*/
  180. /* Clear tmpreg local variable */
  181. tmpreg = 0;
  182. /* Clear OAR1 register */
  183. FMPI2Cx->OAR1 = (uint32_t)tmpreg;
  184. /* Clear OAR2 register */
  185. FMPI2Cx->OAR2 = (uint32_t)tmpreg;
  186. /* Configure FMPI2Cx: Own Address1 and acknowledged address */
  187. /* Set OA1MODE bit according to FMPI2C_AcknowledgedAddress value */
  188. /* Set OA1 bits according to FMPI2C_OwnAddress1 value */
  189. tmpreg = (uint32_t)((uint32_t)FMPI2C_InitStruct->FMPI2C_AcknowledgedAddress | \
  190. (uint32_t)FMPI2C_InitStruct->FMPI2C_OwnAddress1);
  191. /* Write to FMPI2Cx OAR1 */
  192. FMPI2Cx->OAR1 = tmpreg;
  193. /* Enable Own Address1 acknowledgement */
  194. FMPI2Cx->OAR1 |= FMPI2C_OAR1_OA1EN;
  195. /*---------------------------- FMPI2Cx MODE Configuration ---------------------*/
  196. /* Configure FMPI2Cx: mode */
  197. /* Set SMBDEN and SMBHEN bits according to FMPI2C_Mode value */
  198. tmpreg = FMPI2C_InitStruct->FMPI2C_Mode;
  199. /* Write to FMPI2Cx CR1 */
  200. FMPI2Cx->CR1 |= tmpreg;
  201. /*---------------------------- FMPI2Cx ACK Configuration ----------------------*/
  202. /* Get the FMPI2Cx CR2 value */
  203. tmpreg = FMPI2Cx->CR2;
  204. /* Clear FMPI2Cx CR2 register */
  205. tmpreg &= CR2_CLEAR_MASK;
  206. /* Configure FMPI2Cx: acknowledgement */
  207. /* Set NACK bit according to FMPI2C_Ack value */
  208. tmpreg |= FMPI2C_InitStruct->FMPI2C_Ack;
  209. /* Write to FMPI2Cx CR2 */
  210. FMPI2Cx->CR2 = tmpreg;
  211. }
  212. /**
  213. * @brief Fills each FMPI2C_InitStruct member with its default value.
  214. * @param FMPI2C_InitStruct: pointer to an FMPI2C_InitTypeDef structure which will be initialized.
  215. * @retval None
  216. */
  217. void FMPI2C_StructInit(FMPI2C_InitTypeDef* FMPI2C_InitStruct)
  218. {
  219. /*---------------- Reset FMPI2C init structure parameters values --------------*/
  220. /* Initialize the FMPI2C_Timing member */
  221. FMPI2C_InitStruct->FMPI2C_Timing = 0;
  222. /* Initialize the FMPI2C_AnalogFilter member */
  223. FMPI2C_InitStruct->FMPI2C_AnalogFilter = FMPI2C_AnalogFilter_Enable;
  224. /* Initialize the FMPI2C_DigitalFilter member */
  225. FMPI2C_InitStruct->FMPI2C_DigitalFilter = 0;
  226. /* Initialize the FMPI2C_Mode member */
  227. FMPI2C_InitStruct->FMPI2C_Mode = FMPI2C_Mode_FMPI2C;
  228. /* Initialize the FMPI2C_OwnAddress1 member */
  229. FMPI2C_InitStruct->FMPI2C_OwnAddress1 = 0;
  230. /* Initialize the FMPI2C_Ack member */
  231. FMPI2C_InitStruct->FMPI2C_Ack = FMPI2C_Ack_Disable;
  232. /* Initialize the FMPI2C_AcknowledgedAddress member */
  233. FMPI2C_InitStruct->FMPI2C_AcknowledgedAddress = FMPI2C_AcknowledgedAddress_7bit;
  234. }
  235. /**
  236. * @brief Enables or disables the specified FMPI2C peripheral.
  237. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  238. * @param NewState: new state of the FMPI2Cx peripheral.
  239. * This parameter can be: ENABLE or DISABLE.
  240. * @retval None
  241. */
  242. void FMPI2C_Cmd(FMPI2C_TypeDef* FMPI2Cx, FunctionalState NewState)
  243. {
  244. /* Check the parameters */
  245. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  246. assert_param(IS_FUNCTIONAL_STATE(NewState));
  247. if (NewState != DISABLE)
  248. {
  249. /* Enable the selected FMPI2C peripheral */
  250. FMPI2Cx->CR1 |= FMPI2C_CR1_PE;
  251. }
  252. else
  253. {
  254. /* Disable the selected FMPI2C peripheral */
  255. FMPI2Cx->CR1 &= (uint32_t)~((uint32_t)FMPI2C_CR1_PE);
  256. }
  257. }
  258. /**
  259. * @brief Enables or disables the specified FMPI2C software reset.
  260. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  261. * @retval None
  262. */
  263. void FMPI2C_SoftwareResetCmd(FMPI2C_TypeDef* FMPI2Cx)
  264. {
  265. /* Check the parameters */
  266. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  267. /* Disable peripheral */
  268. FMPI2Cx->CR1 &= (uint32_t)~((uint32_t)FMPI2C_CR1_PE);
  269. /* Perform a dummy read to delay the disable of peripheral for minimum
  270. 3 APB clock cycles to perform the software reset functionality */
  271. *(__IO uint32_t *)(uint32_t)FMPI2Cx;
  272. /* Enable peripheral */
  273. FMPI2Cx->CR1 |= FMPI2C_CR1_PE;
  274. }
  275. /**
  276. * @brief Enables or disables the specified FMPI2C interrupts.
  277. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  278. * @param FMPI2C_IT: specifies the FMPI2C interrupts sources to be enabled or disabled.
  279. * This parameter can be any combination of the following values:
  280. * @arg FMPI2C_IT_ERRI: Error interrupt mask
  281. * @arg FMPI2C_IT_TCI: Transfer Complete interrupt mask
  282. * @arg FMPI2C_IT_STOPI: Stop Detection interrupt mask
  283. * @arg FMPI2C_IT_NACKI: Not Acknowledge received interrupt mask
  284. * @arg FMPI2C_IT_ADDRI: Address Match interrupt mask
  285. * @arg FMPI2C_IT_RXI: RX interrupt mask
  286. * @arg FMPI2C_IT_TXI: TX interrupt mask
  287. * @param NewState: new state of the specified FMPI2C interrupts.
  288. * This parameter can be: ENABLE or DISABLE.
  289. * @retval None
  290. */
  291. void FMPI2C_ITConfig(FMPI2C_TypeDef* FMPI2Cx, uint32_t FMPI2C_IT, FunctionalState NewState)
  292. {
  293. /* Check the parameters */
  294. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  295. assert_param(IS_FUNCTIONAL_STATE(NewState));
  296. assert_param(IS_FMPI2C_CONFIG_IT(FMPI2C_IT));
  297. if (NewState != DISABLE)
  298. {
  299. /* Enable the selected FMPI2C interrupts */
  300. FMPI2Cx->CR1 |= FMPI2C_IT;
  301. }
  302. else
  303. {
  304. /* Disable the selected FMPI2C interrupts */
  305. FMPI2Cx->CR1 &= (uint32_t)~((uint32_t)FMPI2C_IT);
  306. }
  307. }
  308. /**
  309. * @brief Enables or disables the FMPI2C Clock stretching.
  310. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  311. * @param NewState: new state of the FMPI2Cx Clock stretching.
  312. * This parameter can be: ENABLE or DISABLE.
  313. * @retval None
  314. */
  315. void FMPI2C_StretchClockCmd(FMPI2C_TypeDef* FMPI2Cx, FunctionalState NewState)
  316. {
  317. /* Check the parameters */
  318. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  319. assert_param(IS_FUNCTIONAL_STATE(NewState));
  320. if (NewState != DISABLE)
  321. {
  322. /* Enable clock stretching */
  323. FMPI2Cx->CR1 &= (uint32_t)~((uint32_t)FMPI2C_CR1_NOSTRETCH);
  324. }
  325. else
  326. {
  327. /* Disable clock stretching */
  328. FMPI2Cx->CR1 |= FMPI2C_CR1_NOSTRETCH;
  329. }
  330. }
  331. /**
  332. * @brief Enables or disables FMPI2Cp from stop mode.
  333. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  334. * @param NewState: new state of the FMPI2Cx stop mode.
  335. * This parameter can be: ENABLE or DISABLE.
  336. * @retval None
  337. */
  338. void FMPI2C_StopModeCmd(FMPI2C_TypeDef* FMPI2Cx, FunctionalState NewState)
  339. {
  340. /* Check the parameters */
  341. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  342. assert_param(IS_FUNCTIONAL_STATE(NewState));
  343. if (NewState != DISABLE)
  344. {
  345. /* Enable wakeup from stop mode */
  346. FMPI2Cx->CR1 |= FMPI2C_CR1_WUPEN;
  347. }
  348. else
  349. {
  350. /* Disable wakeup from stop mode */
  351. FMPI2Cx->CR1 &= (uint32_t)~((uint32_t)FMPI2C_CR1_WUPEN);
  352. }
  353. }
  354. /**
  355. * @brief Enables or disables the FMPI2C own address 2.
  356. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  357. * @param NewState: new state of the FMPI2C own address 2.
  358. * This parameter can be: ENABLE or DISABLE.
  359. * @retval None
  360. */
  361. void FMPI2C_DualAddressCmd(FMPI2C_TypeDef* FMPI2Cx, FunctionalState NewState)
  362. {
  363. /* Check the parameters */
  364. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  365. assert_param(IS_FUNCTIONAL_STATE(NewState));
  366. if (NewState != DISABLE)
  367. {
  368. /* Enable own address 2 */
  369. FMPI2Cx->OAR2 |= FMPI2C_OAR2_OA2EN;
  370. }
  371. else
  372. {
  373. /* Disable own address 2 */
  374. FMPI2Cx->OAR2 &= (uint32_t)~((uint32_t)FMPI2C_OAR2_OA2EN);
  375. }
  376. }
  377. /**
  378. * @brief Configures the FMPI2C slave own address 2 and mask.
  379. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  380. * @param Address: specifies the slave address to be programmed.
  381. * @param Mask: specifies own address 2 mask to be programmed.
  382. * This parameter can be one of the following values:
  383. * @arg FMPI2C_OA2_NoMask: no mask.
  384. * @arg FMPI2C_OA2_Mask01: OA2[1] is masked and don't care.
  385. * @arg FMPI2C_OA2_Mask02: OA2[2:1] are masked and don't care.
  386. * @arg FMPI2C_OA2_Mask03: OA2[3:1] are masked and don't care.
  387. * @arg FMPI2C_OA2_Mask04: OA2[4:1] are masked and don't care.
  388. * @arg FMPI2C_OA2_Mask05: OA2[5:1] are masked and don't care.
  389. * @arg FMPI2C_OA2_Mask06: OA2[6:1] are masked and don't care.
  390. * @arg FMPI2C_OA2_Mask07: OA2[7:1] are masked and don't care.
  391. * @retval None
  392. */
  393. void FMPI2C_OwnAddress2Config(FMPI2C_TypeDef* FMPI2Cx, uint16_t Address, uint8_t Mask)
  394. {
  395. uint32_t tmpreg = 0;
  396. /* Check the parameters */
  397. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  398. assert_param(IS_FMPI2C_OWN_ADDRESS2(Address));
  399. assert_param(IS_FMPI2C_OWN_ADDRESS2_MASK(Mask));
  400. /* Get the old register value */
  401. tmpreg = FMPI2Cx->OAR2;
  402. /* Reset FMPI2Cx OA2 bit [7:1] and OA2MSK bit [1:0] */
  403. tmpreg &= (uint32_t)~((uint32_t)(FMPI2C_OAR2_OA2 | FMPI2C_OAR2_OA2MSK));
  404. /* Set FMPI2Cx SADD */
  405. tmpreg |= (uint32_t)(((uint32_t)Address & FMPI2C_OAR2_OA2) | \
  406. (((uint32_t)Mask << 8) & FMPI2C_OAR2_OA2MSK)) ;
  407. /* Store the new register value */
  408. FMPI2Cx->OAR2 = tmpreg;
  409. }
  410. /**
  411. * @brief Enables or disables the FMPI2C general call mode.
  412. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  413. * @param NewState: new state of the FMPI2C general call mode.
  414. * This parameter can be: ENABLE or DISABLE.
  415. * @retval None
  416. */
  417. void FMPI2C_GeneralCallCmd(FMPI2C_TypeDef* FMPI2Cx, FunctionalState NewState)
  418. {
  419. /* Check the parameters */
  420. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  421. assert_param(IS_FUNCTIONAL_STATE(NewState));
  422. if (NewState != DISABLE)
  423. {
  424. /* Enable general call mode */
  425. FMPI2Cx->CR1 |= FMPI2C_CR1_GCEN;
  426. }
  427. else
  428. {
  429. /* Disable general call mode */
  430. FMPI2Cx->CR1 &= (uint32_t)~((uint32_t)FMPI2C_CR1_GCEN);
  431. }
  432. }
  433. /**
  434. * @brief Enables or disables the FMPI2C slave byte control.
  435. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  436. * @param NewState: new state of the FMPI2C slave byte control.
  437. * This parameter can be: ENABLE or DISABLE.
  438. * @retval None
  439. */
  440. void FMPI2C_SlaveByteControlCmd(FMPI2C_TypeDef* FMPI2Cx, FunctionalState NewState)
  441. {
  442. /* Check the parameters */
  443. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  444. assert_param(IS_FUNCTIONAL_STATE(NewState));
  445. if (NewState != DISABLE)
  446. {
  447. /* Enable slave byte control */
  448. FMPI2Cx->CR1 |= FMPI2C_CR1_SBC;
  449. }
  450. else
  451. {
  452. /* Disable slave byte control */
  453. FMPI2Cx->CR1 &= (uint32_t)~((uint32_t)FMPI2C_CR1_SBC);
  454. }
  455. }
  456. /**
  457. * @brief Configures the slave address to be transmitted after start generation.
  458. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  459. * @param Address: specifies the slave address to be programmed.
  460. * @note This function should be called before generating start condition.
  461. * @retval None
  462. */
  463. void FMPI2C_SlaveAddressConfig(FMPI2C_TypeDef* FMPI2Cx, uint16_t Address)
  464. {
  465. uint32_t tmpreg = 0;
  466. /* Check the parameters */
  467. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  468. assert_param(IS_FMPI2C_SLAVE_ADDRESS(Address));
  469. /* Get the old register value */
  470. tmpreg = FMPI2Cx->CR2;
  471. /* Reset FMPI2Cx SADD bit [9:0] */
  472. tmpreg &= (uint32_t)~((uint32_t)FMPI2C_CR2_SADD);
  473. /* Set FMPI2Cx SADD */
  474. tmpreg |= (uint32_t)((uint32_t)Address & FMPI2C_CR2_SADD);
  475. /* Store the new register value */
  476. FMPI2Cx->CR2 = tmpreg;
  477. }
  478. /**
  479. * @brief Enables or disables the FMPI2C 10-bit addressing mode for the master.
  480. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  481. * @param NewState: new state of the FMPI2C 10-bit addressing mode.
  482. * This parameter can be: ENABLE or DISABLE.
  483. * @note This function should be called before generating start condition.
  484. * @retval None
  485. */
  486. void FMPI2C_10BitAddressingModeCmd(FMPI2C_TypeDef* FMPI2Cx, FunctionalState NewState)
  487. {
  488. /* Check the parameters */
  489. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  490. assert_param(IS_FUNCTIONAL_STATE(NewState));
  491. if (NewState != DISABLE)
  492. {
  493. /* Enable 10-bit addressing mode */
  494. FMPI2Cx->CR2 |= FMPI2C_CR2_ADD10;
  495. }
  496. else
  497. {
  498. /* Disable 10-bit addressing mode */
  499. FMPI2Cx->CR2 &= (uint32_t)~((uint32_t)FMPI2C_CR2_ADD10);
  500. }
  501. }
  502. /**
  503. * @}
  504. */
  505. /** @defgroup FMPI2C_Group2 Communications handling functions
  506. * @brief Communications handling functions
  507. *
  508. @verbatim
  509. ===============================================================================
  510. ##### Communications handling functions #####
  511. ===============================================================================
  512. [..] This section provides a set of functions that handles FMPI2C communication.
  513. [..] Automatic End mode is enabled using FMPI2C_AutoEndCmd() function. When Reload
  514. mode is enabled via FMPI2C_ReloadCmd() AutoEnd bit has no effect.
  515. [..] FMPI2C_NumberOfBytesConfig() function set the number of bytes to be transferred,
  516. this configuration should be done before generating start condition in master
  517. mode.
  518. [..] When switching from master write operation to read operation in 10Bit addressing
  519. mode, master can only sends the 1st 7 bits of the 10 bit address, followed by
  520. Read direction by enabling HEADR bit using FMPI2C_10BitAddressHeader() function.
  521. [..] In master mode, when transferring more than 255 bytes Reload mode should be used
  522. to handle communication. In the first phase of transfer, Nbytes should be set to
  523. 255. After transferring these bytes TCR flag is set and FMPI2C_TransferHandling()
  524. function should be called to handle remaining communication.
  525. [..] In master mode, when software end mode is selected when all data is transferred
  526. TC flag is set FMPI2C_TransferHandling() function should be called to generate STOP
  527. or generate ReStart.
  528. @endverbatim
  529. * @{
  530. */
  531. /**
  532. * @brief Enables or disables the FMPI2C automatic end mode (stop condition is
  533. * automatically sent when nbytes data are transferred).
  534. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  535. * @param NewState: new state of the FMPI2C automatic end mode.
  536. * This parameter can be: ENABLE or DISABLE.
  537. * @note This function has effect if Reload mode is disabled.
  538. * @retval None
  539. */
  540. void FMPI2C_AutoEndCmd(FMPI2C_TypeDef* FMPI2Cx, FunctionalState NewState)
  541. {
  542. /* Check the parameters */
  543. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  544. assert_param(IS_FUNCTIONAL_STATE(NewState));
  545. if (NewState != DISABLE)
  546. {
  547. /* Enable Auto end mode */
  548. FMPI2Cx->CR2 |= FMPI2C_CR2_AUTOEND;
  549. }
  550. else
  551. {
  552. /* Disable Auto end mode */
  553. FMPI2Cx->CR2 &= (uint32_t)~((uint32_t)FMPI2C_CR2_AUTOEND);
  554. }
  555. }
  556. /**
  557. * @brief Enables or disables the FMPI2C nbytes reload mode.
  558. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  559. * @param NewState: new state of the nbytes reload mode.
  560. * This parameter can be: ENABLE or DISABLE.
  561. * @retval None
  562. */
  563. void FMPI2C_ReloadCmd(FMPI2C_TypeDef* FMPI2Cx, FunctionalState NewState)
  564. {
  565. /* Check the parameters */
  566. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  567. assert_param(IS_FUNCTIONAL_STATE(NewState));
  568. if (NewState != DISABLE)
  569. {
  570. /* Enable Auto Reload mode */
  571. FMPI2Cx->CR2 |= FMPI2C_CR2_RELOAD;
  572. }
  573. else
  574. {
  575. /* Disable Auto Reload mode */
  576. FMPI2Cx->CR2 &= (uint32_t)~((uint32_t)FMPI2C_CR2_RELOAD);
  577. }
  578. }
  579. /**
  580. * @brief Configures the number of bytes to be transmitted/received.
  581. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  582. * @param Number_Bytes: specifies the number of bytes to be programmed.
  583. * @retval None
  584. */
  585. void FMPI2C_NumberOfBytesConfig(FMPI2C_TypeDef* FMPI2Cx, uint8_t Number_Bytes)
  586. {
  587. uint32_t tmpreg = 0;
  588. /* Check the parameters */
  589. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  590. /* Get the old register value */
  591. tmpreg = FMPI2Cx->CR2;
  592. /* Reset FMPI2Cx Nbytes bit [7:0] */
  593. tmpreg &= (uint32_t)~((uint32_t)FMPI2C_CR2_NBYTES);
  594. /* Set FMPI2Cx Nbytes */
  595. tmpreg |= (uint32_t)(((uint32_t)Number_Bytes << 16 ) & FMPI2C_CR2_NBYTES);
  596. /* Store the new register value */
  597. FMPI2Cx->CR2 = tmpreg;
  598. }
  599. /**
  600. * @brief Configures the type of transfer request for the master.
  601. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  602. * @param FMPI2C_Direction: specifies the transfer request direction to be programmed.
  603. * This parameter can be one of the following values:
  604. * @arg FMPI2C_Direction_Transmitter: Master request a write transfer
  605. * @arg FMPI2C_Direction_Receiver: Master request a read transfer
  606. * @retval None
  607. */
  608. void FMPI2C_MasterRequestConfig(FMPI2C_TypeDef* FMPI2Cx, uint16_t FMPI2C_Direction)
  609. {
  610. /* Check the parameters */
  611. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  612. assert_param(IS_FMPI2C_DIRECTION(FMPI2C_Direction));
  613. /* Test on the direction to set/reset the read/write bit */
  614. if (FMPI2C_Direction == FMPI2C_Direction_Transmitter)
  615. {
  616. /* Request a write Transfer */
  617. FMPI2Cx->CR2 &= (uint32_t)~((uint32_t)FMPI2C_CR2_RD_WRN);
  618. }
  619. else
  620. {
  621. /* Request a read Transfer */
  622. FMPI2Cx->CR2 |= FMPI2C_CR2_RD_WRN;
  623. }
  624. }
  625. /**
  626. * @brief Generates FMPI2Cx communication START condition.
  627. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  628. * @param NewState: new state of the FMPI2C START condition generation.
  629. * This parameter can be: ENABLE or DISABLE.
  630. * @retval None
  631. */
  632. void FMPI2C_GenerateSTART(FMPI2C_TypeDef* FMPI2Cx, FunctionalState NewState)
  633. {
  634. /* Check the parameters */
  635. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  636. assert_param(IS_FUNCTIONAL_STATE(NewState));
  637. if (NewState != DISABLE)
  638. {
  639. /* Generate a START condition */
  640. FMPI2Cx->CR2 |= FMPI2C_CR2_START;
  641. }
  642. else
  643. {
  644. /* Disable the START condition generation */
  645. FMPI2Cx->CR2 &= (uint32_t)~((uint32_t)FMPI2C_CR2_START);
  646. }
  647. }
  648. /**
  649. * @brief Generates FMPI2Cx communication STOP condition.
  650. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  651. * @param NewState: new state of the FMPI2C STOP condition generation.
  652. * This parameter can be: ENABLE or DISABLE.
  653. * @retval None
  654. */
  655. void FMPI2C_GenerateSTOP(FMPI2C_TypeDef* FMPI2Cx, FunctionalState NewState)
  656. {
  657. /* Check the parameters */
  658. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  659. assert_param(IS_FUNCTIONAL_STATE(NewState));
  660. if (NewState != DISABLE)
  661. {
  662. /* Generate a STOP condition */
  663. FMPI2Cx->CR2 |= FMPI2C_CR2_STOP;
  664. }
  665. else
  666. {
  667. /* Disable the STOP condition generation */
  668. FMPI2Cx->CR2 &= (uint32_t)~((uint32_t)FMPI2C_CR2_STOP);
  669. }
  670. }
  671. /**
  672. * @brief Enables or disables the FMPI2C 10-bit header only mode with read direction.
  673. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  674. * @param NewState: new state of the FMPI2C 10-bit header only mode.
  675. * This parameter can be: ENABLE or DISABLE.
  676. * @note This mode can be used only when switching from master transmitter mode
  677. * to master receiver mode.
  678. * @retval None
  679. */
  680. void FMPI2C_10BitAddressHeaderCmd(FMPI2C_TypeDef* FMPI2Cx, FunctionalState NewState)
  681. {
  682. /* Check the parameters */
  683. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  684. assert_param(IS_FUNCTIONAL_STATE(NewState));
  685. if (NewState != DISABLE)
  686. {
  687. /* Enable 10-bit header only mode */
  688. FMPI2Cx->CR2 |= FMPI2C_CR2_HEAD10R;
  689. }
  690. else
  691. {
  692. /* Disable 10-bit header only mode */
  693. FMPI2Cx->CR2 &= (uint32_t)~((uint32_t)FMPI2C_CR2_HEAD10R);
  694. }
  695. }
  696. /**
  697. * @brief Generates FMPI2C communication Acknowledge.
  698. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  699. * @param NewState: new state of the Acknowledge.
  700. * This parameter can be: ENABLE or DISABLE.
  701. * @retval None
  702. */
  703. void FMPI2C_AcknowledgeConfig(FMPI2C_TypeDef* FMPI2Cx, FunctionalState NewState)
  704. {
  705. /* Check the parameters */
  706. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  707. assert_param(IS_FUNCTIONAL_STATE(NewState));
  708. if (NewState != DISABLE)
  709. {
  710. /* Enable ACK generation */
  711. FMPI2Cx->CR2 &= (uint32_t)~((uint32_t)FMPI2C_CR2_NACK);
  712. }
  713. else
  714. {
  715. /* Enable NACK generation */
  716. FMPI2Cx->CR2 |= FMPI2C_CR2_NACK;
  717. }
  718. }
  719. /**
  720. * @brief Returns the FMPI2C slave matched address .
  721. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  722. * @retval The value of the slave matched address .
  723. */
  724. uint8_t FMPI2C_GetAddressMatched(FMPI2C_TypeDef* FMPI2Cx)
  725. {
  726. /* Check the parameters */
  727. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  728. /* Return the slave matched address in the SR1 register */
  729. return (uint8_t)(((uint32_t)FMPI2Cx->ISR & FMPI2C_ISR_ADDCODE) >> 16) ;
  730. }
  731. /**
  732. * @brief Returns the FMPI2C slave received request.
  733. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  734. * @retval The value of the received request.
  735. */
  736. uint16_t FMPI2C_GetTransferDirection(FMPI2C_TypeDef* FMPI2Cx)
  737. {
  738. uint32_t tmpreg = 0;
  739. uint16_t direction = 0;
  740. /* Check the parameters */
  741. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  742. /* Return the slave matched address in the SR1 register */
  743. tmpreg = (uint32_t)(FMPI2Cx->ISR & FMPI2C_ISR_DIR);
  744. /* If write transfer is requested */
  745. if (tmpreg == 0)
  746. {
  747. /* write transfer is requested */
  748. direction = FMPI2C_Direction_Transmitter;
  749. }
  750. else
  751. {
  752. /* Read transfer is requested */
  753. direction = FMPI2C_Direction_Receiver;
  754. }
  755. return direction;
  756. }
  757. /**
  758. * @brief Handles FMPI2Cx communication when starting transfer or during transfer (TC or TCR flag are set).
  759. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  760. * @param Address: specifies the slave address to be programmed.
  761. * @param Number_Bytes: specifies the number of bytes to be programmed.
  762. * This parameter must be a value between 0 and 255.
  763. * @param ReloadEndMode: new state of the FMPI2C START condition generation.
  764. * This parameter can be one of the following values:
  765. * @arg FMPI2C_Reload_Mode: Enable Reload mode .
  766. * @arg FMPI2C_AutoEnd_Mode: Enable Automatic end mode.
  767. * @arg FMPI2C_SoftEnd_Mode: Enable Software end mode.
  768. * @param StartStopMode: new state of the FMPI2C START condition generation.
  769. * This parameter can be one of the following values:
  770. * @arg FMPI2C_No_StartStop: Don't Generate stop and start condition.
  771. * @arg FMPI2C_Generate_Stop: Generate stop condition (Number_Bytes should be set to 0).
  772. * @arg FMPI2C_Generate_Start_Read: Generate Restart for read request.
  773. * @arg FMPI2C_Generate_Start_Write: Generate Restart for write request.
  774. * @retval None
  775. */
  776. void FMPI2C_TransferHandling(FMPI2C_TypeDef* FMPI2Cx, uint16_t Address, uint8_t Number_Bytes, uint32_t ReloadEndMode, uint32_t StartStopMode)
  777. {
  778. uint32_t tmpreg = 0;
  779. /* Check the parameters */
  780. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  781. assert_param(IS_FMPI2C_SLAVE_ADDRESS(Address));
  782. assert_param(IS_RELOAD_END_MODE(ReloadEndMode));
  783. assert_param(IS_START_STOP_MODE(StartStopMode));
  784. /* Get the CR2 register value */
  785. tmpreg = FMPI2Cx->CR2;
  786. /* clear tmpreg specific bits */
  787. tmpreg &= (uint32_t)~((uint32_t)(FMPI2C_CR2_SADD | FMPI2C_CR2_NBYTES | FMPI2C_CR2_RELOAD | FMPI2C_CR2_AUTOEND | FMPI2C_CR2_RD_WRN | FMPI2C_CR2_START | FMPI2C_CR2_STOP));
  788. /* update tmpreg */
  789. tmpreg |= (uint32_t)(((uint32_t)Address & FMPI2C_CR2_SADD) | (((uint32_t)Number_Bytes << 16 ) & FMPI2C_CR2_NBYTES) | \
  790. (uint32_t)ReloadEndMode | (uint32_t)StartStopMode);
  791. /* update CR2 register */
  792. FMPI2Cx->CR2 = tmpreg;
  793. }
  794. /**
  795. * @}
  796. */
  797. /** @defgroup FMPI2C_Group3 SMBUS management functions
  798. * @brief SMBUS management functions
  799. *
  800. @verbatim
  801. ===============================================================================
  802. ##### SMBUS management functions #####
  803. ===============================================================================
  804. [..] This section provides a set of functions that handles SMBus communication
  805. and timeouts detection.
  806. [..] The SMBus Device default address (0b1100 001) is enabled by calling FMPI2C_Init()
  807. function and setting FMPI2C_Mode member of FMPI2C_InitTypeDef() structure to
  808. FMPI2C_Mode_SMBusDevice.
  809. [..] The SMBus Host address (0b0001 000) is enabled by calling FMPI2C_Init()
  810. function and setting FMPI2C_Mode member of FMPI2C_InitTypeDef() structure to
  811. FMPI2C_Mode_SMBusHost.
  812. [..] The Alert Response Address (0b0001 100) is enabled using FMPI2C_SMBusAlertCmd()
  813. function.
  814. [..] To detect cumulative SCL stretch in master and slave mode, TIMEOUTB should be
  815. configured (in accordance to SMBus specification) using FMPI2C_TimeoutBConfig()
  816. function then FMPI2C_ExtendedClockTimeoutCmd() function should be called to enable
  817. the detection.
  818. [..] SCL low timeout is detected by configuring TIMEOUTB using FMPI2C_TimeoutBConfig()
  819. function followed by the call of FMPI2C_ClockTimeoutCmd(). When adding to this
  820. procedure the call of FMPI2C_IdleClockTimeoutCmd() function, Bus Idle condition
  821. (both SCL and SDA high) is detected also.
  822. @endverbatim
  823. * @{
  824. */
  825. /**
  826. * @brief Enables or disables FMPI2C SMBus alert.
  827. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  828. * @param NewState: new state of the FMPI2Cx SMBus alert.
  829. * This parameter can be: ENABLE or DISABLE.
  830. * @retval None
  831. */
  832. void FMPI2C_SMBusAlertCmd(FMPI2C_TypeDef* FMPI2Cx, FunctionalState NewState)
  833. {
  834. /* Check the parameters */
  835. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  836. assert_param(IS_FUNCTIONAL_STATE(NewState));
  837. if (NewState != DISABLE)
  838. {
  839. /* Enable SMBus alert */
  840. FMPI2Cx->CR1 |= FMPI2C_CR1_ALERTEN;
  841. }
  842. else
  843. {
  844. /* Disable SMBus alert */
  845. FMPI2Cx->CR1 &= (uint32_t)~((uint32_t)FMPI2C_CR1_ALERTEN);
  846. }
  847. }
  848. /**
  849. * @brief Enables or disables FMPI2C Clock Timeout (SCL Timeout detection).
  850. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  851. * @param NewState: new state of the FMPI2Cx clock Timeout.
  852. * This parameter can be: ENABLE or DISABLE.
  853. * @retval None
  854. */
  855. void FMPI2C_ClockTimeoutCmd(FMPI2C_TypeDef* FMPI2Cx, FunctionalState NewState)
  856. {
  857. /* Check the parameters */
  858. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  859. assert_param(IS_FUNCTIONAL_STATE(NewState));
  860. if (NewState != DISABLE)
  861. {
  862. /* Enable Clock Timeout */
  863. FMPI2Cx->TIMEOUTR |= FMPI2C_TIMEOUTR_TIMOUTEN;
  864. }
  865. else
  866. {
  867. /* Disable Clock Timeout */
  868. FMPI2Cx->TIMEOUTR &= (uint32_t)~((uint32_t)FMPI2C_TIMEOUTR_TIMOUTEN);
  869. }
  870. }
  871. /**
  872. * @brief Enables or disables FMPI2C Extended Clock Timeout (SCL cumulative Timeout detection).
  873. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  874. * @param NewState: new state of the FMPI2Cx Extended clock Timeout.
  875. * This parameter can be: ENABLE or DISABLE.
  876. * @retval None
  877. */
  878. void FMPI2C_ExtendedClockTimeoutCmd(FMPI2C_TypeDef* FMPI2Cx, FunctionalState NewState)
  879. {
  880. /* Check the parameters */
  881. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  882. assert_param(IS_FUNCTIONAL_STATE(NewState));
  883. if (NewState != DISABLE)
  884. {
  885. /* Enable Clock Timeout */
  886. FMPI2Cx->TIMEOUTR |= FMPI2C_TIMEOUTR_TEXTEN;
  887. }
  888. else
  889. {
  890. /* Disable Clock Timeout */
  891. FMPI2Cx->TIMEOUTR &= (uint32_t)~((uint32_t)FMPI2C_TIMEOUTR_TEXTEN);
  892. }
  893. }
  894. /**
  895. * @brief Enables or disables FMPI2C Idle Clock Timeout (Bus idle SCL and SDA
  896. * high detection).
  897. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  898. * @param NewState: new state of the FMPI2Cx Idle clock Timeout.
  899. * This parameter can be: ENABLE or DISABLE.
  900. * @retval None
  901. */
  902. void FMPI2C_IdleClockTimeoutCmd(FMPI2C_TypeDef* FMPI2Cx, FunctionalState NewState)
  903. {
  904. /* Check the parameters */
  905. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  906. assert_param(IS_FUNCTIONAL_STATE(NewState));
  907. if (NewState != DISABLE)
  908. {
  909. /* Enable Clock Timeout */
  910. FMPI2Cx->TIMEOUTR |= FMPI2C_TIMEOUTR_TIDLE;
  911. }
  912. else
  913. {
  914. /* Disable Clock Timeout */
  915. FMPI2Cx->TIMEOUTR &= (uint32_t)~((uint32_t)FMPI2C_TIMEOUTR_TIDLE);
  916. }
  917. }
  918. /**
  919. * @brief Configures the FMPI2C Bus Timeout A (SCL Timeout when TIDLE = 0 or Bus
  920. * idle SCL and SDA high when TIDLE = 1).
  921. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  922. * @param Timeout: specifies the TimeoutA to be programmed.
  923. * @retval None
  924. */
  925. void FMPI2C_TimeoutAConfig(FMPI2C_TypeDef* FMPI2Cx, uint16_t Timeout)
  926. {
  927. uint32_t tmpreg = 0;
  928. /* Check the parameters */
  929. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  930. assert_param(IS_FMPI2C_TIMEOUT(Timeout));
  931. /* Get the old register value */
  932. tmpreg = FMPI2Cx->TIMEOUTR;
  933. /* Reset FMPI2Cx TIMEOUTA bit [11:0] */
  934. tmpreg &= (uint32_t)~((uint32_t)FMPI2C_TIMEOUTR_TIMEOUTA);
  935. /* Set FMPI2Cx TIMEOUTA */
  936. tmpreg |= (uint32_t)((uint32_t)Timeout & FMPI2C_TIMEOUTR_TIMEOUTA) ;
  937. /* Store the new register value */
  938. FMPI2Cx->TIMEOUTR = tmpreg;
  939. }
  940. /**
  941. * @brief Configures the FMPI2C Bus Timeout B (SCL cumulative Timeout).
  942. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  943. * @param Timeout: specifies the TimeoutB to be programmed.
  944. * @retval None
  945. */
  946. void FMPI2C_TimeoutBConfig(FMPI2C_TypeDef* FMPI2Cx, uint16_t Timeout)
  947. {
  948. uint32_t tmpreg = 0;
  949. /* Check the parameters */
  950. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  951. assert_param(IS_FMPI2C_TIMEOUT(Timeout));
  952. /* Get the old register value */
  953. tmpreg = FMPI2Cx->TIMEOUTR;
  954. /* Reset FMPI2Cx TIMEOUTB bit [11:0] */
  955. tmpreg &= (uint32_t)~((uint32_t)FMPI2C_TIMEOUTR_TIMEOUTB);
  956. /* Set FMPI2Cx TIMEOUTB */
  957. tmpreg |= (uint32_t)(((uint32_t)Timeout << 16) & FMPI2C_TIMEOUTR_TIMEOUTB) ;
  958. /* Store the new register value */
  959. FMPI2Cx->TIMEOUTR = tmpreg;
  960. }
  961. /**
  962. * @brief Enables or disables FMPI2C PEC calculation.
  963. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  964. * @param NewState: new state of the FMPI2Cx PEC calculation.
  965. * This parameter can be: ENABLE or DISABLE.
  966. * @retval None
  967. */
  968. void FMPI2C_CalculatePEC(FMPI2C_TypeDef* FMPI2Cx, FunctionalState NewState)
  969. {
  970. /* Check the parameters */
  971. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  972. assert_param(IS_FUNCTIONAL_STATE(NewState));
  973. if (NewState != DISABLE)
  974. {
  975. /* Enable PEC calculation */
  976. FMPI2Cx->CR1 |= FMPI2C_CR1_PECEN;
  977. }
  978. else
  979. {
  980. /* Disable PEC calculation */
  981. FMPI2Cx->CR1 &= (uint32_t)~((uint32_t)FMPI2C_CR1_PECEN);
  982. }
  983. }
  984. /**
  985. * @brief Enables or disables FMPI2C PEC transmission/reception request.
  986. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  987. * @param NewState: new state of the FMPI2Cx PEC request.
  988. * This parameter can be: ENABLE or DISABLE.
  989. * @retval None
  990. */
  991. void FMPI2C_PECRequestCmd(FMPI2C_TypeDef* FMPI2Cx, FunctionalState NewState)
  992. {
  993. /* Check the parameters */
  994. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  995. assert_param(IS_FUNCTIONAL_STATE(NewState));
  996. if (NewState != DISABLE)
  997. {
  998. /* Enable PEC transmission/reception request */
  999. FMPI2Cx->CR1 |= FMPI2C_CR2_PECBYTE;
  1000. }
  1001. else
  1002. {
  1003. /* Disable PEC transmission/reception request */
  1004. FMPI2Cx->CR1 &= (uint32_t)~((uint32_t)FMPI2C_CR2_PECBYTE);
  1005. }
  1006. }
  1007. /**
  1008. * @brief Returns the FMPI2C PEC.
  1009. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  1010. * @retval The value of the PEC .
  1011. */
  1012. uint8_t FMPI2C_GetPEC(FMPI2C_TypeDef* FMPI2Cx)
  1013. {
  1014. /* Check the parameters */
  1015. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  1016. /* Return the slave matched address in the SR1 register */
  1017. return (uint8_t)((uint32_t)FMPI2Cx->PECR & FMPI2C_PECR_PEC);
  1018. }
  1019. /**
  1020. * @}
  1021. */
  1022. /** @defgroup FMPI2C_Group4 FMPI2C registers management functions
  1023. * @brief FMPI2C registers management functions
  1024. *
  1025. @verbatim
  1026. ===============================================================================
  1027. ##### FMPI2C registers management functions #####
  1028. ===============================================================================
  1029. [..] This section provides a functions that allow user the management of
  1030. FMPI2C registers.
  1031. @endverbatim
  1032. * @{
  1033. */
  1034. /**
  1035. * @brief Reads the specified FMPI2C register and returns its value.
  1036. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  1037. * @param FMPI2C_Register: specifies the register to read.
  1038. * This parameter can be one of the following values:
  1039. * @arg FMPI2C_Register_CR1: CR1 register.
  1040. * @arg FMPI2C_Register_CR2: CR2 register.
  1041. * @arg FMPI2C_Register_OAR1: OAR1 register.
  1042. * @arg FMPI2C_Register_OAR2: OAR2 register.
  1043. * @arg FMPI2C_Register_TIMINGR: TIMING register.
  1044. * @arg FMPI2C_Register_TIMEOUTR: TIMEOUTR register.
  1045. * @arg FMPI2C_Register_ISR: ISR register.
  1046. * @arg FMPI2C_Register_ICR: ICR register.
  1047. * @arg FMPI2C_Register_PECR: PECR register.
  1048. * @arg FMPI2C_Register_RXDR: RXDR register.
  1049. * @arg FMPI2C_Register_TXDR: TXDR register.
  1050. * @retval The value of the read register.
  1051. */
  1052. uint32_t FMPI2C_ReadRegister(FMPI2C_TypeDef* FMPI2Cx, uint8_t FMPI2C_Register)
  1053. {
  1054. __IO uint32_t tmp = 0;
  1055. /* Check the parameters */
  1056. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  1057. assert_param(IS_FMPI2C_REGISTER(FMPI2C_Register));
  1058. tmp = (uint32_t)FMPI2Cx;
  1059. tmp += FMPI2C_Register;
  1060. /* Return the selected register value */
  1061. return (*(__IO uint32_t *) tmp);
  1062. }
  1063. /**
  1064. * @}
  1065. */
  1066. /** @defgroup FMPI2C_Group5 Data transfers management functions
  1067. * @brief Data transfers management functions
  1068. *
  1069. @verbatim
  1070. ===============================================================================
  1071. ##### Data transfers management functions #####
  1072. ===============================================================================
  1073. [..] This subsection provides a set of functions allowing to manage
  1074. the FMPI2C data transfers.
  1075. [..] The read access of the FMPI2C_RXDR register can be done using
  1076. the FMPI2C_ReceiveData() function and returns the received value.
  1077. Whereas a write access to the FMPI2C_TXDR can be done using FMPI2C_SendData()
  1078. function and stores the written data into TXDR.
  1079. @endverbatim
  1080. * @{
  1081. */
  1082. /**
  1083. * @brief Sends a data byte through the FMPI2Cx peripheral.
  1084. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  1085. * @param Data: Byte to be transmitted..
  1086. * @retval None
  1087. */
  1088. void FMPI2C_SendData(FMPI2C_TypeDef* FMPI2Cx, uint8_t Data)
  1089. {
  1090. /* Check the parameters */
  1091. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  1092. /* Write in the DR register the data to be sent */
  1093. FMPI2Cx->TXDR = (uint8_t)Data;
  1094. }
  1095. /**
  1096. * @brief Returns the most recent received data by the FMPI2Cx peripheral.
  1097. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  1098. * @retval The value of the received data.
  1099. */
  1100. uint8_t FMPI2C_ReceiveData(FMPI2C_TypeDef* FMPI2Cx)
  1101. {
  1102. /* Check the parameters */
  1103. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  1104. /* Return the data in the DR register */
  1105. return (uint8_t)FMPI2Cx->RXDR;
  1106. }
  1107. /**
  1108. * @}
  1109. */
  1110. /** @defgroup FMPI2C_Group6 DMA transfers management functions
  1111. * @brief DMA transfers management functions
  1112. *
  1113. @verbatim
  1114. ===============================================================================
  1115. ##### DMA transfers management functions #####
  1116. ===============================================================================
  1117. [..] This section provides two functions that can be used only in DMA mode.
  1118. [..] In DMA Mode, the FMPI2C communication can be managed by 2 DMA Channel
  1119. requests:
  1120. (#) FMPI2C_DMAReq_Tx: specifies the Tx buffer DMA transfer request.
  1121. (#) FMPI2C_DMAReq_Rx: specifies the Rx buffer DMA transfer request.
  1122. [..] In this Mode it is advised to use the following function:
  1123. (+) FMPI2C_DMACmd(FMPI2C_TypeDef* FMPI2Cx, uint32_t FMPI2C_DMAReq, FunctionalState NewState);
  1124. @endverbatim
  1125. * @{
  1126. */
  1127. /**
  1128. * @brief Enables or disables the FMPI2C DMA interface.
  1129. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  1130. * @param FMPI2C_DMAReq: specifies the FMPI2C DMA transfer request to be enabled or disabled.
  1131. * This parameter can be any combination of the following values:
  1132. * @arg FMPI2C_DMAReq_Tx: Tx DMA transfer request
  1133. * @arg FMPI2C_DMAReq_Rx: Rx DMA transfer request
  1134. * @param NewState: new state of the selected FMPI2C DMA transfer request.
  1135. * This parameter can be: ENABLE or DISABLE.
  1136. * @retval None
  1137. */
  1138. void FMPI2C_DMACmd(FMPI2C_TypeDef* FMPI2Cx, uint32_t FMPI2C_DMAReq, FunctionalState NewState)
  1139. {
  1140. /* Check the parameters */
  1141. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  1142. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1143. assert_param(IS_FMPI2C_DMA_REQ(FMPI2C_DMAReq));
  1144. if (NewState != DISABLE)
  1145. {
  1146. /* Enable the selected FMPI2C DMA requests */
  1147. FMPI2Cx->CR1 |= FMPI2C_DMAReq;
  1148. }
  1149. else
  1150. {
  1151. /* Disable the selected FMPI2C DMA requests */
  1152. FMPI2Cx->CR1 &= (uint32_t)~FMPI2C_DMAReq;
  1153. }
  1154. }
  1155. /**
  1156. * @}
  1157. */
  1158. /** @defgroup FMPI2C_Group7 Interrupts and flags management functions
  1159. * @brief Interrupts and flags management functions
  1160. *
  1161. @verbatim
  1162. ===============================================================================
  1163. ##### Interrupts and flags management functions #####
  1164. ===============================================================================
  1165. [..] This section provides functions allowing to configure the FMPI2C Interrupts
  1166. sources and check or clear the flags or pending bits status.
  1167. The user should identify which mode will be used in his application to manage
  1168. the communication: Polling mode, Interrupt mode or DMA mode(refer FMPI2C_Group6) .
  1169. *** Polling Mode ***
  1170. ====================
  1171. [..] In Polling Mode, the FMPI2C communication can be managed by 15 flags:
  1172. (#) FMPI2C_FLAG_TXE: to indicate the status of Transmit data register empty flag.
  1173. (#) FMPI2C_FLAG_TXIS: to indicate the status of Transmit interrupt status flag .
  1174. (#) FMPI2C_FLAG_RXNE: to indicate the status of Receive data register not empty flag.
  1175. (#) FMPI2C_FLAG_ADDR: to indicate the status of Address matched flag (slave mode).
  1176. (#) FMPI2C_FLAG_NACKF: to indicate the status of NACK received flag.
  1177. (#) FMPI2C_FLAG_STOPF: to indicate the status of STOP detection flag.
  1178. (#) FMPI2C_FLAG_TC: to indicate the status of Transfer complete flag(master mode).
  1179. (#) FMPI2C_FLAG_TCR: to indicate the status of Transfer complete reload flag.
  1180. (#) FMPI2C_FLAG_BERR: to indicate the status of Bus error flag.
  1181. (#) FMPI2C_FLAG_ARLO: to indicate the status of Arbitration lost flag.
  1182. (#) FMPI2C_FLAG_OVR: to indicate the status of Overrun/Underrun flag.
  1183. (#) FMPI2C_FLAG_PECERR: to indicate the status of PEC error in reception flag.
  1184. (#) FMPI2C_FLAG_TIMEOUT: to indicate the status of Timeout or Tlow detection flag.
  1185. (#) FMPI2C_FLAG_ALERT: to indicate the status of SMBus Alert flag.
  1186. (#) FMPI2C_FLAG_BUSY: to indicate the status of Bus busy flag.
  1187. [..] In this Mode it is advised to use the following functions:
  1188. (+) FlagStatus FMPI2C_GetFlagStatus(FMPI2C_TypeDef* FMPI2Cx, uint32_t FMPI2C_FLAG);
  1189. (+) void FMPI2C_ClearFlag(FMPI2C_TypeDef* FMPI2Cx, uint32_t FMPI2C_FLAG);
  1190. [..]
  1191. (@)Do not use the BUSY flag to handle each data transmission or reception.It is
  1192. better to use the TXIS and RXNE flags instead.
  1193. *** Interrupt Mode ***
  1194. ======================
  1195. [..] In Interrupt Mode, the FMPI2C communication can be managed by 7 interrupt sources
  1196. and 15 pending bits:
  1197. [..] Interrupt Source:
  1198. (#) FMPI2C_IT_ERRI: specifies the interrupt source for the Error interrupt.
  1199. (#) FMPI2C_IT_TCI: specifies the interrupt source for the Transfer Complete interrupt.
  1200. (#) FMPI2C_IT_STOPI: specifies the interrupt source for the Stop Detection interrupt.
  1201. (#) FMPI2C_IT_NACKI: specifies the interrupt source for the Not Acknowledge received interrupt.
  1202. (#) FMPI2C_IT_ADDRI: specifies the interrupt source for the Address Match interrupt.
  1203. (#) FMPI2C_IT_RXI: specifies the interrupt source for the RX interrupt.
  1204. (#) FMPI2C_IT_TXI: specifies the interrupt source for the TX interrupt.
  1205. [..] Pending Bits:
  1206. (#) FMPI2C_IT_TXIS: to indicate the status of Transmit interrupt status flag.
  1207. (#) FMPI2C_IT_RXNE: to indicate the status of Receive data register not empty flag.
  1208. (#) FMPI2C_IT_ADDR: to indicate the status of Address matched flag (slave mode).
  1209. (#) FMPI2C_IT_NACKF: to indicate the status of NACK received flag.
  1210. (#) FMPI2C_IT_STOPF: to indicate the status of STOP detection flag.
  1211. (#) FMPI2C_IT_TC: to indicate the status of Transfer complete flag (master mode).
  1212. (#) FMPI2C_IT_TCR: to indicate the status of Transfer complete reload flag.
  1213. (#) FMPI2C_IT_BERR: to indicate the status of Bus error flag.
  1214. (#) FMPI2C_IT_ARLO: to indicate the status of Arbitration lost flag.
  1215. (#) FMPI2C_IT_OVR: to indicate the status of Overrun/Underrun flag.
  1216. (#) FMPI2C_IT_PECERR: to indicate the status of PEC error in reception flag.
  1217. (#) FMPI2C_IT_TIMEOUT: to indicate the status of Timeout or Tlow detection flag.
  1218. (#) FMPI2C_IT_ALERT: to indicate the status of SMBus Alert flag.
  1219. [..] In this Mode it is advised to use the following functions:
  1220. (+) void FMPI2C_ClearITPendingBit(FMPI2C_TypeDef* FMPI2Cx, uint32_t FMPI2C_IT);
  1221. (+) ITStatus FMPI2C_GetITStatus(FMPI2C_TypeDef* FMPI2Cx, uint32_t FMPI2C_IT);
  1222. @endverbatim
  1223. * @{
  1224. */
  1225. /**
  1226. * @brief Checks whether the specified FMPI2C flag is set or not.
  1227. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  1228. * @param FMPI2C_FLAG: specifies the flag to check.
  1229. * This parameter can be one of the following values:
  1230. * @arg FMPI2C_FLAG_TXE: Transmit data register empty
  1231. * @arg FMPI2C_FLAG_TXIS: Transmit interrupt status
  1232. * @arg FMPI2C_FLAG_RXNE: Receive data register not empty
  1233. * @arg FMPI2C_FLAG_ADDR: Address matched (slave mode)
  1234. * @arg FMPI2C_FLAG_NACKF: NACK received flag
  1235. * @arg FMPI2C_FLAG_STOPF: STOP detection flag
  1236. * @arg FMPI2C_FLAG_TC: Transfer complete (master mode)
  1237. * @arg FMPI2C_FLAG_TCR: Transfer complete reload
  1238. * @arg FMPI2C_FLAG_BERR: Bus error
  1239. * @arg FMPI2C_FLAG_ARLO: Arbitration lost
  1240. * @arg FMPI2C_FLAG_OVR: Overrun/Underrun
  1241. * @arg FMPI2C_FLAG_PECERR: PEC error in reception
  1242. * @arg FMPI2C_FLAG_TIMEOUT: Timeout or Tlow detection flag
  1243. * @arg FMPI2C_FLAG_ALERT: SMBus Alert
  1244. * @arg FMPI2C_FLAG_BUSY: Bus busy
  1245. * @retval The new state of FMPI2C_FLAG (SET or RESET).
  1246. */
  1247. FlagStatus FMPI2C_GetFlagStatus(FMPI2C_TypeDef* FMPI2Cx, uint32_t FMPI2C_FLAG)
  1248. {
  1249. uint32_t tmpreg = 0;
  1250. FlagStatus bitstatus = RESET;
  1251. /* Check the parameters */
  1252. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  1253. assert_param(IS_FMPI2C_GET_FLAG(FMPI2C_FLAG));
  1254. /* Get the ISR register value */
  1255. tmpreg = FMPI2Cx->ISR;
  1256. /* Get flag status */
  1257. tmpreg &= FMPI2C_FLAG;
  1258. if(tmpreg != 0)
  1259. {
  1260. /* FMPI2C_FLAG is set */
  1261. bitstatus = SET;
  1262. }
  1263. else
  1264. {
  1265. /* FMPI2C_FLAG is reset */
  1266. bitstatus = RESET;
  1267. }
  1268. return bitstatus;
  1269. }
  1270. /**
  1271. * @brief Clears the FMPI2Cx's pending flags.
  1272. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  1273. * @param FMPI2C_FLAG: specifies the flag to clear.
  1274. * This parameter can be any combination of the following values:
  1275. * @arg FMPI2C_FLAG_ADDR: Address matched (slave mode)
  1276. * @arg FMPI2C_FLAG_NACKF: NACK received flag
  1277. * @arg FMPI2C_FLAG_STOPF: STOP detection flag
  1278. * @arg FMPI2C_FLAG_BERR: Bus error
  1279. * @arg FMPI2C_FLAG_ARLO: Arbitration lost
  1280. * @arg FMPI2C_FLAG_OVR: Overrun/Underrun
  1281. * @arg FMPI2C_FLAG_PECERR: PEC error in reception
  1282. * @arg FMPI2C_FLAG_TIMEOUT: Timeout or Tlow detection flag
  1283. * @arg FMPI2C_FLAG_ALERT: SMBus Alert
  1284. * @retval The new state of FMPI2C_FLAG (SET or RESET).
  1285. */
  1286. void FMPI2C_ClearFlag(FMPI2C_TypeDef* FMPI2Cx, uint32_t FMPI2C_FLAG)
  1287. {
  1288. /* Check the parameters */
  1289. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  1290. assert_param(IS_FMPI2C_CLEAR_FLAG(FMPI2C_FLAG));
  1291. /* Clear the selected flag */
  1292. FMPI2Cx->ICR = FMPI2C_FLAG;
  1293. }
  1294. /**
  1295. * @brief Checks whether the specified FMPI2C interrupt has occurred or not.
  1296. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  1297. * @param FMPI2C_IT: specifies the interrupt source to check.
  1298. * This parameter can be one of the following values:
  1299. * @arg FMPI2C_IT_TXIS: Transmit interrupt status
  1300. * @arg FMPI2C_IT_RXNE: Receive data register not empty
  1301. * @arg FMPI2C_IT_ADDR: Address matched (slave mode)
  1302. * @arg FMPI2C_IT_NACKF: NACK received flag
  1303. * @arg FMPI2C_IT_STOPF: STOP detection flag
  1304. * @arg FMPI2C_IT_TC: Transfer complete (master mode)
  1305. * @arg FMPI2C_IT_TCR: Transfer complete reload
  1306. * @arg FMPI2C_IT_BERR: Bus error
  1307. * @arg FMPI2C_IT_ARLO: Arbitration lost
  1308. * @arg FMPI2C_IT_OVR: Overrun/Underrun
  1309. * @arg FMPI2C_IT_PECERR: PEC error in reception
  1310. * @arg FMPI2C_IT_TIMEOUT: Timeout or Tlow detection flag
  1311. * @arg FMPI2C_IT_ALERT: SMBus Alert
  1312. * @retval The new state of FMPI2C_IT (SET or RESET).
  1313. */
  1314. ITStatus FMPI2C_GetITStatus(FMPI2C_TypeDef* FMPI2Cx, uint32_t FMPI2C_IT)
  1315. {
  1316. uint32_t tmpreg = 0;
  1317. ITStatus bitstatus = RESET;
  1318. uint32_t enablestatus = 0;
  1319. /* Check the parameters */
  1320. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  1321. assert_param(IS_FMPI2C_GET_IT(FMPI2C_IT));
  1322. /* Check if the interrupt source is enabled or not */
  1323. /* If Error interrupt */
  1324. if((uint32_t)(FMPI2C_IT & ERROR_IT_MASK))
  1325. {
  1326. enablestatus = (uint32_t)((FMPI2C_CR1_ERRIE) & (FMPI2Cx->CR1));
  1327. }
  1328. /* If TC interrupt */
  1329. else if((uint32_t)(FMPI2C_IT & TC_IT_MASK))
  1330. {
  1331. enablestatus = (uint32_t)((FMPI2C_CR1_TCIE) & (FMPI2Cx->CR1));
  1332. }
  1333. else
  1334. {
  1335. enablestatus = (uint32_t)((FMPI2C_IT) & (FMPI2Cx->CR1));
  1336. }
  1337. /* Get the ISR register value */
  1338. tmpreg = FMPI2Cx->ISR;
  1339. /* Get flag status */
  1340. tmpreg &= FMPI2C_IT;
  1341. /* Check the status of the specified FMPI2C flag */
  1342. if((tmpreg != RESET) && enablestatus)
  1343. {
  1344. /* FMPI2C_IT is set */
  1345. bitstatus = SET;
  1346. }
  1347. else
  1348. {
  1349. /* FMPI2C_IT is reset */
  1350. bitstatus = RESET;
  1351. }
  1352. /* Return the FMPI2C_IT status */
  1353. return bitstatus;
  1354. }
  1355. /**
  1356. * @brief Clears the FMPI2Cx's interrupt pending bits.
  1357. * @param FMPI2Cx: where x can be 1 to select the FMPI2C peripheral.
  1358. * @param FMPI2C_IT: specifies the interrupt pending bit to clear.
  1359. * This parameter can be any combination of the following values:
  1360. * @arg FMPI2C_IT_ADDR: Address matched (slave mode)
  1361. * @arg FMPI2C_IT_NACKF: NACK received flag
  1362. * @arg FMPI2C_IT_STOPF: STOP detection flag
  1363. * @arg FMPI2C_IT_BERR: Bus error
  1364. * @arg FMPI2C_IT_ARLO: Arbitration lost
  1365. * @arg FMPI2C_IT_OVR: Overrun/Underrun
  1366. * @arg FMPI2C_IT_PECERR: PEC error in reception
  1367. * @arg FMPI2C_IT_TIMEOUT: Timeout or Tlow detection flag
  1368. * @arg FMPI2C_IT_ALERT: SMBus Alert
  1369. * @retval The new state of FMPI2C_IT (SET or RESET).
  1370. */
  1371. void FMPI2C_ClearITPendingBit(FMPI2C_TypeDef* FMPI2Cx, uint32_t FMPI2C_IT)
  1372. {
  1373. /* Check the parameters */
  1374. assert_param(IS_FMPI2C_ALL_PERIPH(FMPI2Cx));
  1375. assert_param(IS_FMPI2C_CLEAR_IT(FMPI2C_IT));
  1376. /* Clear the selected flag */
  1377. FMPI2Cx->ICR = FMPI2C_IT;
  1378. }
  1379. /**
  1380. * @}
  1381. */
  1382. /**
  1383. * @}
  1384. */
  1385. /**
  1386. * @}
  1387. */
  1388. /**
  1389. * @}
  1390. */
  1391. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/