stm32f10x_cec.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /**
  2. ******************************************************************************
  3. * @file stm32f10x_cec.c
  4. * @author MCD Application Team
  5. * @version V3.3.0
  6. * @date 04/16/2010
  7. * @brief This file provides all the CEC firmware functions.
  8. ******************************************************************************
  9. * @copy
  10. *
  11. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *
  18. * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
  19. */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32f10x_cec.h"
  22. #include "stm32f10x_rcc.h"
  23. /** @addtogroup STM32F10x_StdPeriph_Driver
  24. * @{
  25. */
  26. /** @defgroup CEC
  27. * @brief CEC driver modules
  28. * @{
  29. */
  30. /** @defgroup CEC_Private_TypesDefinitions
  31. * @{
  32. */
  33. /**
  34. * @}
  35. */
  36. /** @defgroup CEC_Private_Defines
  37. * @{
  38. */
  39. /* ------------ CEC registers bit address in the alias region ----------- */
  40. #define CEC_OFFSET (CEC_BASE - PERIPH_BASE)
  41. /* --- CFGR Register ---*/
  42. /* Alias word address of PE bit */
  43. #define CFGR_OFFSET (CEC_OFFSET + 0x00)
  44. #define PE_BitNumber 0x00
  45. #define CFGR_PE_BB (PERIPH_BB_BASE + (CFGR_OFFSET * 32) + (PE_BitNumber * 4))
  46. /* Alias word address of IE bit */
  47. #define IE_BitNumber 0x01
  48. #define CFGR_IE_BB (PERIPH_BB_BASE + (CFGR_OFFSET * 32) + (IE_BitNumber * 4))
  49. /* --- CSR Register ---*/
  50. /* Alias word address of TSOM bit */
  51. #define CSR_OFFSET (CEC_OFFSET + 0x10)
  52. #define TSOM_BitNumber 0x00
  53. #define CSR_TSOM_BB (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (TSOM_BitNumber * 4))
  54. /* Alias word address of TEOM bit */
  55. #define TEOM_BitNumber 0x01
  56. #define CSR_TEOM_BB (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (TEOM_BitNumber * 4))
  57. #define CFGR_CLEAR_Mask (uint8_t)(0xF3) /* CFGR register Mask */
  58. #define FLAG_Mask ((uint32_t)0x00FFFFFF) /* CEC FLAG mask */
  59. /**
  60. * @}
  61. */
  62. /** @defgroup CEC_Private_Macros
  63. * @{
  64. */
  65. /**
  66. * @}
  67. */
  68. /** @defgroup CEC_Private_Variables
  69. * @{
  70. */
  71. /**
  72. * @}
  73. */
  74. /** @defgroup CEC_Private_FunctionPrototypes
  75. * @{
  76. */
  77. /**
  78. * @}
  79. */
  80. /** @defgroup CEC_Private_Functions
  81. * @{
  82. */
  83. /**
  84. * @brief Deinitializes the CEC peripheral registers to their default reset
  85. * values.
  86. * @param None
  87. * @retval None
  88. */
  89. void CEC_DeInit(void)
  90. {
  91. /* Enable CEC reset state */
  92. RCC_APB1PeriphResetCmd(RCC_APB1Periph_CEC, ENABLE);
  93. /* Release CEC from reset state */
  94. RCC_APB1PeriphResetCmd(RCC_APB1Periph_CEC, DISABLE);
  95. }
  96. /**
  97. * @brief Initializes the CEC peripheral according to the specified
  98. * parameters in the CEC_InitStruct.
  99. * @param CEC_InitStruct: pointer to an CEC_InitTypeDef structure that
  100. * contains the configuration information for the specified
  101. * CEC peripheral.
  102. * @retval None
  103. */
  104. void CEC_Init(CEC_InitTypeDef* CEC_InitStruct)
  105. {
  106. uint16_t tmpreg = 0;
  107. /* Check the parameters */
  108. assert_param(IS_CEC_BIT_TIMING_ERROR_MODE(CEC_InitStruct->CEC_BitTimingMode));
  109. assert_param(IS_CEC_BIT_PERIOD_ERROR_MODE(CEC_InitStruct->CEC_BitPeriodMode));
  110. /*---------------------------- CEC CFGR Configuration -----------------*/
  111. /* Get the CEC CFGR value */
  112. tmpreg = CEC->CFGR;
  113. /* Clear BTEM and BPEM bits */
  114. tmpreg &= CFGR_CLEAR_Mask;
  115. /* Configure CEC: Bit Timing Error and Bit Period Error */
  116. tmpreg |= (uint16_t)(CEC_InitStruct->CEC_BitTimingMode | CEC_InitStruct->CEC_BitPeriodMode);
  117. /* Write to CEC CFGR register*/
  118. CEC->CFGR = tmpreg;
  119. }
  120. /**
  121. * @brief Enables or disables the specified CEC peripheral.
  122. * @param NewState: new state of the CEC peripheral.
  123. * This parameter can be: ENABLE or DISABLE.
  124. * @retval None
  125. */
  126. void CEC_Cmd(FunctionalState NewState)
  127. {
  128. /* Check the parameters */
  129. assert_param(IS_FUNCTIONAL_STATE(NewState));
  130. *(__IO uint32_t *) CFGR_PE_BB = (uint32_t)NewState;
  131. if(NewState == DISABLE)
  132. {
  133. /* Wait until the PE bit is cleared by hardware (Idle Line detected) */
  134. while((CEC->CFGR & CEC_CFGR_PE) != (uint32_t)RESET)
  135. {
  136. }
  137. }
  138. }
  139. /**
  140. * @brief Enables or disables the CEC interrupt.
  141. * @param NewState: new state of the CEC interrupt.
  142. * This parameter can be: ENABLE or DISABLE.
  143. * @retval None
  144. */
  145. void CEC_ITConfig(FunctionalState NewState)
  146. {
  147. /* Check the parameters */
  148. assert_param(IS_FUNCTIONAL_STATE(NewState));
  149. *(__IO uint32_t *) CFGR_IE_BB = (uint32_t)NewState;
  150. }
  151. /**
  152. * @brief Defines the Own Address of the CEC device.
  153. * @param CEC_OwnAddress: The CEC own address
  154. * @retval None
  155. */
  156. void CEC_OwnAddressConfig(uint8_t CEC_OwnAddress)
  157. {
  158. /* Check the parameters */
  159. assert_param(IS_CEC_ADDRESS(CEC_OwnAddress));
  160. /* Set the CEC own address */
  161. CEC->OAR = CEC_OwnAddress;
  162. }
  163. /**
  164. * @brief Sets the CEC prescaler value.
  165. * @param CEC_Prescaler: CEC prescaler new value
  166. * @retval None
  167. */
  168. void CEC_SetPrescaler(uint16_t CEC_Prescaler)
  169. {
  170. /* Check the parameters */
  171. assert_param(IS_CEC_PRESCALER(CEC_Prescaler));
  172. /* Set the Prescaler value*/
  173. CEC->PRES = CEC_Prescaler;
  174. }
  175. /**
  176. * @brief Transmits single data through the CEC peripheral.
  177. * @param Data: the data to transmit.
  178. * @retval None
  179. */
  180. void CEC_SendDataByte(uint8_t Data)
  181. {
  182. /* Transmit Data */
  183. CEC->TXD = Data ;
  184. }
  185. /**
  186. * @brief Returns the most recent received data by the CEC peripheral.
  187. * @param None
  188. * @retval The received data.
  189. */
  190. uint8_t CEC_ReceiveDataByte(void)
  191. {
  192. /* Receive Data */
  193. return (uint8_t)(CEC->RXD);
  194. }
  195. /**
  196. * @brief Starts a new message.
  197. * @param None
  198. * @retval None
  199. */
  200. void CEC_StartOfMessage(void)
  201. {
  202. /* Starts of new message */
  203. *(__IO uint32_t *) CSR_TSOM_BB = (uint32_t)0x1;
  204. }
  205. /**
  206. * @brief Transmits message with or without an EOM bit.
  207. * @param NewState: new state of the CEC Tx End Of Message.
  208. * This parameter can be: ENABLE or DISABLE.
  209. * @retval None
  210. */
  211. void CEC_EndOfMessageCmd(FunctionalState NewState)
  212. {
  213. /* Check the parameters */
  214. assert_param(IS_FUNCTIONAL_STATE(NewState));
  215. /* The data byte will be transmitted with or without an EOM bit*/
  216. *(__IO uint32_t *) CSR_TEOM_BB = (uint32_t)NewState;
  217. }
  218. /**
  219. * @brief Gets the CEC flag status
  220. * @param CEC_FLAG: specifies the CEC flag to check.
  221. * This parameter can be one of the following values:
  222. * @arg CEC_FLAG_BTE: Bit Timing Error
  223. * @arg CEC_FLAG_BPE: Bit Period Error
  224. * @arg CEC_FLAG_RBTFE: Rx Block Transfer Finished Error
  225. * @arg CEC_FLAG_SBE: Start Bit Error
  226. * @arg CEC_FLAG_ACKE: Block Acknowledge Error
  227. * @arg CEC_FLAG_LINE: Line Error
  228. * @arg CEC_FLAG_TBTFE: Tx Block Transfer Finsihed Error
  229. * @arg CEC_FLAG_TEOM: Tx End Of Message
  230. * @arg CEC_FLAG_TERR: Tx Error
  231. * @arg CEC_FLAG_TBTRF: Tx Byte Transfer Request or Block Transfer Finished
  232. * @arg CEC_FLAG_RSOM: Rx Start Of Message
  233. * @arg CEC_FLAG_REOM: Rx End Of Message
  234. * @arg CEC_FLAG_RERR: Rx Error
  235. * @arg CEC_FLAG_RBTF: Rx Byte/Block Transfer Finished
  236. * @retval The new state of CEC_FLAG (SET or RESET)
  237. */
  238. FlagStatus CEC_GetFlagStatus(uint32_t CEC_FLAG)
  239. {
  240. FlagStatus bitstatus = RESET;
  241. uint32_t cecreg = 0, cecbase = 0;
  242. /* Check the parameters */
  243. assert_param(IS_CEC_GET_FLAG(CEC_FLAG));
  244. /* Get the CEC peripheral base address */
  245. cecbase = (uint32_t)(CEC_BASE);
  246. /* Read flag register index */
  247. cecreg = CEC_FLAG >> 28;
  248. /* Get bit[23:0] of the flag */
  249. CEC_FLAG &= FLAG_Mask;
  250. if(cecreg != 0)
  251. {
  252. /* Flag in CEC ESR Register */
  253. CEC_FLAG = (uint32_t)(CEC_FLAG >> 16);
  254. /* Get the CEC ESR register address */
  255. cecbase += 0xC;
  256. }
  257. else
  258. {
  259. /* Get the CEC CSR register address */
  260. cecbase += 0x10;
  261. }
  262. if(((*(__IO uint32_t *)cecbase) & CEC_FLAG) != (uint32_t)RESET)
  263. {
  264. /* CEC_FLAG is set */
  265. bitstatus = SET;
  266. }
  267. else
  268. {
  269. /* CEC_FLAG is reset */
  270. bitstatus = RESET;
  271. }
  272. /* Return the CEC_FLAG status */
  273. return bitstatus;
  274. }
  275. /**
  276. * @brief Clears the CEC's pending flags.
  277. * @param CEC_FLAG: specifies the flag to clear.
  278. * This parameter can be any combination of the following values:
  279. * @arg CEC_FLAG_TERR: Tx Error
  280. * @arg CEC_FLAG_TBTRF: Tx Byte Transfer Request or Block Transfer Finished
  281. * @arg CEC_FLAG_RSOM: Rx Start Of Message
  282. * @arg CEC_FLAG_REOM: Rx End Of Message
  283. * @arg CEC_FLAG_RERR: Rx Error
  284. * @arg CEC_FLAG_RBTF: Rx Byte/Block Transfer Finished
  285. * @retval None
  286. */
  287. void CEC_ClearFlag(uint32_t CEC_FLAG)
  288. {
  289. uint32_t tmp = 0x0;
  290. /* Check the parameters */
  291. assert_param(IS_CEC_CLEAR_FLAG(CEC_FLAG));
  292. tmp = CEC->CSR & 0x2;
  293. /* Clear the selected CEC flags */
  294. CEC->CSR &= (uint32_t)(((~(uint32_t)CEC_FLAG) & 0xFFFFFFFC) | tmp);
  295. }
  296. /**
  297. * @brief Checks whether the specified CEC interrupt has occurred or not.
  298. * @param CEC_IT: specifies the CEC interrupt source to check.
  299. * This parameter can be one of the following values:
  300. * @arg CEC_IT_TERR: Tx Error
  301. * @arg CEC_IT_TBTF: Tx Block Transfer Finished
  302. * @arg CEC_IT_RERR: Rx Error
  303. * @arg CEC_IT_RBTF: Rx Block Transfer Finished
  304. * @retval The new state of CEC_IT (SET or RESET).
  305. */
  306. ITStatus CEC_GetITStatus(uint8_t CEC_IT)
  307. {
  308. ITStatus bitstatus = RESET;
  309. uint32_t enablestatus = 0;
  310. /* Check the parameters */
  311. assert_param(IS_CEC_GET_IT(CEC_IT));
  312. /* Get the CEC IT enable bit status */
  313. enablestatus = (CEC->CFGR & (uint8_t)CEC_CFGR_IE) ;
  314. /* Check the status of the specified CEC interrupt */
  315. if (((CEC->CSR & CEC_IT) != (uint32_t)RESET) && enablestatus)
  316. {
  317. /* CEC_IT is set */
  318. bitstatus = SET;
  319. }
  320. else
  321. {
  322. /* CEC_IT is reset */
  323. bitstatus = RESET;
  324. }
  325. /* Return the CEC_IT status */
  326. return bitstatus;
  327. }
  328. /**
  329. * @brief Clears the CEC's interrupt pending bits.
  330. * @param CEC_IT: specifies the CEC interrupt pending bit to clear.
  331. * This parameter can be any combination of the following values:
  332. * @arg CEC_IT_TERR: Tx Error
  333. * @arg CEC_IT_TBTF: Tx Block Transfer Finished
  334. * @arg CEC_IT_RERR: Rx Error
  335. * @arg CEC_IT_RBTF: Rx Block Transfer Finished
  336. * @retval None
  337. */
  338. void CEC_ClearITPendingBit(uint16_t CEC_IT)
  339. {
  340. uint32_t tmp = 0x0;
  341. /* Check the parameters */
  342. assert_param(IS_CEC_GET_IT(CEC_IT));
  343. tmp = CEC->CSR & 0x2;
  344. /* Clear the selected CEC interrupt pending bits */
  345. CEC->CSR &= (uint32_t)(((~(uint32_t)CEC_IT) & 0xFFFFFFFC) | tmp);
  346. }
  347. /**
  348. * @}
  349. */
  350. /**
  351. * @}
  352. */
  353. /**
  354. * @}
  355. */
  356. /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/