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