1 /**
2   ******************************************************************************
3   * @file    tae32f53xx_ll.c
4   * @author  MCD Application Team
5   * @brief   LL module driver.
6   *          This is the common part of the LL initialization
7   *
8   @verbatim
9     ==============================================================================
10                      ##### How to use this driver #####
11     ==============================================================================
12       [..]
13       The common LL driver contains a set of generic and common APIs that can be
14       used by the PPP peripheral drivers and the user to start using the LL.
15       [..]
16       The LL contains two APIs' categories:
17            (+) Common LL APIs
18            (+) Services LL APIs
19   @endverbatim
20   ******************************************************************************
21   * @attention
22   *
23   * <h2><center>&copy; Copyright (c) 2020 Tai-Action.
24   * All rights reserved.</center></h2>
25   *
26   * This software is licensed by Tai-Action under BSD 3-Clause license,
27   * the "License"; You may not use this file except in compliance with the
28   * License. You may obtain a copy of the License at:
29   *                        opensource.org/licenses/BSD-3-Clause
30   *
31   ******************************************************************************
32   */
33 
34 /* Includes ------------------------------------------------------------------*/
35 #include "tae32f53xx_ll.h"
36 
37 
38 #define DBG_TAG             "TAE32F53xx LL"
39 #define DBG_LVL             DBG_ERROR
40 #include "dbg/tae32f53xx_dbg.h"
41 
42 
43 /** @defgroup TAE32F53xx_LL_Driver TAE32F53xx LL Driver
44   * @brief    TAE32F53xx LL Driver
45   * @{
46   */
47 
48 /** @defgroup TAE32F53xx_LL TAE32F53xx LL
49   * @brief    TAE32F53xx LL
50   * @{
51   */
52 
53 #ifdef LL_MODULE_ENABLED
54 
55 /* Private typedef -----------------------------------------------------------*/
56 /* Private define ------------------------------------------------------------*/
57 /* Private macro -------------------------------------------------------------*/
58 /** @defgroup TAE32F53xx_LL_Private_Macros TAE32F53xx LL Private Macros
59   * @brief    TAE32F53xx LL Private Macros
60   * @{
61   */
62 
63 /**
64   * @brief  Judge is Tick freq or not
65   * @param  FREQ Freq to be judged
66   * @retval 0 isn't Tick freq
67   * @retval 1 is Tick freq
68   */
69 #define IS_TICKFREQ(FREQ)   (((FREQ) == LL_TICK_FREQ_10HZ)  || \
70                              ((FREQ) == LL_TICK_FREQ_100HZ) || \
71                              ((FREQ) == LL_TICK_FREQ_1KHZ))
72 
73 /**
74   * @}
75   */
76 
77 /* Private variables ---------------------------------------------------------*/
78 /** @defgroup TAE32F53xx_LL_Private_Variables TAE32F53xx LL Private Variables
79   * @brief    TAE32F53xx LL Private Variables
80   * @{
81   */
82 
83 /**
84   * @brief SysTick counter
85   */
86 __IO uint32_t uwTick;
87 
88 /**
89   * @brief SysTick interrupt priority
90   */
91 uint32_t uwTickPrio = (1UL << __NVIC_PRIO_BITS) - 1;
92 
93 /**
94   * @brief SysTick interrupt frequency
95   */
96 LL_TickFreqETypeDef uwTickFreq = LL_TICK_FREQ_DEFAULT;
97 
98 /**
99   * @}
100   */
101 
102 /* Private function prototypes -----------------------------------------------*/
103 /* Exported functions --------------------------------------------------------*/
104 /** @defgroup TAE32F53xx_LL_Exported_Functions TAE32F53xx LL Exported Functions
105   * @brief    TAE32F53xx LL Exported Functions
106   * @{
107   */
108 
109 /** @defgroup TAE32F53xx_LL_Exported_Functions_Group1 TAE32F53xx Initialization and de-initialization Functions
110  *  @brief    Initialization and de-initialization functions
111  *
112 @verbatim
113  ===============================================================================
114               ##### Initialization and de-initialization functions #####
115  ===============================================================================
116    [..]  This section provides functions allowing to:
117       (+) Initializes the Flash interface, the NVIC allocation and initial clock
118           configuration. It initializes the systick also when timeout is needed
119           and the backup domain when enabled.
120       (+) de-Initializes common part of the LL.
121       (+) Configure The time base source to have 1ms time base with a dedicated
122           Tick interrupt priority.
123         (++) SysTick timer is used by default as source of time base, but user
124              can eventually implement his proper time base source (a general purpose
125              timer for example or other time source), keeping in mind that Time base
126              duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
127              handled in milliseconds basis.
128         (++) Time base configuration function (LL_InitTick ()) is called automatically
129              at the beginning of the program after reset by LL_Init()
130         (++) Source of time base is configured to generate interrupts at regular
131              time intervals. Care must be taken if LL_Delay() is called from a
132              peripheral ISR process, the Tick interrupt line must have higher priority
133             (numerically lower) than the peripheral interrupt. Otherwise the caller
134             ISR process will be blocked.
135         (++) functions affecting time base configurations are declared as __WEAK
136              to make override possible in case of other implementations in user file.
137 @endverbatim
138   * @{
139   */
140 
141 /**
142   * @brief  This function is used to initialize the LL Library; it must be the first
143   *         instruction to be executed in the main program (before to call any other
144   *         LL function), it performs the following:
145   *           Configure the Flash prefetch.
146   *           Configures the SysTick to generate an interrupt each 1 millisecond,
147   *           which is clocked by the LSI (at this stage, the clock is not yet
148   *           configured and thus the system is running from the internal LSI at 32 KHz).
149   *           Set NVIC Group Priority to 4.
150   *           Calls the LL_MspInit() callback function defined in user file
151   *           "tae32f53xx_ll_msp.c" to do the global low level hardware initialization
152   *
153   * @note   SysTick is used as time base for the LL_Delay() function, the application
154   *         need to ensure that the SysTick time base is always set to 1 millisecond
155   *         to have correct LL operation.
156   * @param  None
157   * @retval LL status
158   */
LL_Init(void)159 LL_StatusETypeDef LL_Init(void)
160 {
161 #ifdef LL_FLASH_MODULE_ENABLED
162     /* Configure Flash prefetch */
163 #if PREFETCH_ENABLE
164     /* Prefetch enable */
165     __LL_FLASH_I_BUS_PREFETCH_ENABLE();
166     __LL_FLASH_D_BUS_PREFETCH_ENABLE();
167 #else
168     /* Prefetch disable */
169     __LL_FLASH_I_BUS_PREFETCH_DISABLE();
170     __LL_FLASH_D_BUS_PREFETCH_DISABLE();
171 #endif /* PREFETCH_ENABLE */
172 #endif   /* LL_FLASH_MODULE_ENABLED */
173 
174     /* Set Interrupt Group Priority */
175     LL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_3);
176 
177     /* Use systick as time base source and configure 1ms tick (default clock after Reset is LSI) */
178     LL_InitTick(TICK_INT_PRIORITY);
179 
180     /* Init the low level hardware */
181     LL_MspInit();
182 
183     /* Return function status */
184     return LL_OK;
185 }
186 
187 /**
188   * @brief This function de-Initializes common part of the LL and stops the systick of time base.
189   * @note This function is optional.
190   * @param  None
191   * @retval LL status
192   */
LL_DeInit(void)193 LL_StatusETypeDef LL_DeInit(void)
194 {
195     /* Reset of all peripherals */
196     LL_SYSCTRL_AllPeriphRstAssert();
197 
198     /* De-Init the low level hardware */
199     LL_MspDeInit();
200 
201     /* Return function status */
202     return LL_OK;
203 }
204 
205 /**
206   * @brief  Initialize the MSP.
207   * @param  None
208   * @retval None
209   */
LL_MspInit(void)210 __WEAK void LL_MspInit(void)
211 {
212     /* NOTE : This function should not be modified, when the callback is needed,
213               the LL_MspInit could be implemented in the user file
214      */
215 }
216 
217 /**
218   * @brief  DeInitializes the MSP.
219   * @param  None
220   * @retval None
221   */
LL_MspDeInit(void)222 __WEAK void LL_MspDeInit(void)
223 {
224     /* NOTE : This function should not be modified, when the callback is needed,
225               the LL_MspDeInit could be implemented in the user file
226      */
227 }
228 
229 
230 /**
231   * @brief This function configures the source of the time base.
232   *        The time source is configured to have 1ms time base with a dedicated
233   *        Tick interrupt priority.
234   * @note This function is called automatically at the beginning of program after
235   *       reset by LL_Init().
236   * @note In the default implementation, SysTick timer is the source of time base.
237   *       It is used to generate interrupts at regular time intervals.
238   *       Care must be taken if LL_Delay() is called from a peripheral ISR process,
239   *       The SysTick interrupt must have higher priority (numerically lower)
240   *       than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
241   *       The function is declared as __WEAK to be overwritten in case of other
242   *       implementation  in user file.
243   * @param TickPriority Tick interrupt priority.
244   * @retval LL status
245   */
LL_InitTick(uint32_t TickPriority)246 __WEAK LL_StatusETypeDef LL_InitTick(uint32_t TickPriority)
247 {
248     /* Configure the SysTick to have interrupt in 1ms time basis*/
249     if (LL_SYSTICK_Config(LL_SYSCTRL_SysclkGet() / (1000U / uwTickFreq)) > 0U) {
250         return LL_ERROR;
251     }
252 
253     /* Configure the SysTick IRQ priority */
254     if (TickPriority < (1UL << __NVIC_PRIO_BITS)) {
255         LL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
256         uwTickPrio = TickPriority;
257     } else {
258         return LL_ERROR;
259     }
260 
261     /* Return function status */
262     return LL_OK;
263 }
264 
265 
266 /**
267   * @}
268   */
269 
270 /** @defgroup TAE32F53xx_LL_Exported_Functions_Group2 TAE32F53xx LL Control functions
271   * @brief    TAE32F53xx LL Control functions
272   *
273 @verbatim
274  ===============================================================================
275                       ##### LL Control functions #####
276  ===============================================================================
277     [..]  This section provides functions allowing to:
278       (+) Provide a tick value in millisecond
279       (+) Provide a blocking delay in millisecond
280       (+) Suspend the time base source interrupt
281       (+) Resume the time base source interrupt
282       (+) Get the LL API driver version
283       (+) Get the unique device identifier
284 @endverbatim
285   * @{
286   */
287 
288 /**
289   * @brief This function is called to increment a global variable "uwTick"
290   *        used as application time base.
291   * @note In the default implementation, this variable is incremented each 1ms
292   *       in SysTick ISR.
293   * @note This function is declared as __WEAK to be overwritten in case of other
294   *      implementations in user file.
295   * @param  None
296   * @retval None
297   */
LL_IncTick(void)298 __WEAK void LL_IncTick(void)
299 {
300     uwTick += uwTickFreq;
301 }
302 
303 /**
304   * @brief Provides a tick value in millisecond
305   * @note  This function is declared as __WEAK to be overwritten in case of other
306   *       implementations in user file.
307   * @param  None
308   * @retval tick value
309   */
LL_GetTick(void)310 __WEAK uint32_t LL_GetTick(void)
311 {
312     return uwTick;
313 }
314 
315 /**
316   * @brief This function returns a tick priority
317   * @param  None
318   * @retval tick priority
319   */
LL_GetTickPrio(void)320 uint32_t LL_GetTickPrio(void)
321 {
322     return uwTickPrio;
323 }
324 
325 /**
326   * @brief  Set new tick Freq
327   * @param  Freq New tick freq
328   * @retval LL status
329   */
LL_SetTickFreq(LL_TickFreqETypeDef Freq)330 LL_StatusETypeDef LL_SetTickFreq(LL_TickFreqETypeDef Freq)
331 {
332     LL_StatusETypeDef status = LL_OK;
333     assert_param(IS_TICKFREQ(Freq));
334 
335     if (uwTickFreq != Freq) {
336         /* Apply the new tick Freq  */
337         status = LL_InitTick(uwTickPrio);
338 
339         if (status == LL_OK) {
340             uwTickFreq = Freq;
341         }
342     }
343 
344     return status;
345 }
346 
347 /**
348   * @brief  Get tick frequency
349   * @param  None
350   * @retval tick period in Hz
351   */
LL_GetTickFreq(void)352 LL_TickFreqETypeDef LL_GetTickFreq(void)
353 {
354     return uwTickFreq;
355 }
356 
357 /**
358   * @brief This function provides minimum delay (in milliseconds) based
359   *        on variable incremented.
360   * @note In the default implementation , SysTick timer is the source of time base.
361   *       It is used to generate interrupts at regular time intervals where uwTick
362   *       is incremented.
363   * @note This function is declared as __WEAK to be overwritten in case of other
364   *       implementations in user file.
365   * @param Delay specifies the delay time length, in milliseconds.
366   * @retval None
367   */
LL_Delay(uint32_t Delay)368 __WEAK void LL_Delay(uint32_t Delay)
369 {
370     uint32_t tickstart = LL_GetTick();
371     uint32_t wait = Delay;
372 
373     /* Add a freq to guarantee minimum wait */
374     if (wait < LL_MAX_DELAY) {
375         wait += (uint32_t)(uwTickFreq);
376     }
377 
378     while ((LL_GetTick() - tickstart) < wait) {
379     }
380 }
381 
382 /**
383   * @brief Suspend Tick increment.
384   * @note In the default implementation , SysTick timer is the source of time base. It is
385   *       used to generate interrupts at regular time intervals. Once LL_SuspendTick()
386   *       is called, the SysTick interrupt will be disabled and so Tick increment
387   *       is suspended.
388   * @note This function is declared as __WEAK to be overwritten in case of other
389   *       implementations in user file.
390   * @param  None
391   * @retval None
392   */
LL_SuspendTick(void)393 __WEAK void LL_SuspendTick(void)
394 {
395     /* Disable SysTick Interrupt */
396     CLEAR_BIT(SysTick->CTRL, SysTick_CTRL_TICKINT_Msk);
397 }
398 
399 /**
400   * @brief Resume Tick increment.
401   * @note In the default implementation , SysTick timer is the source of time base. It is
402   *       used to generate interrupts at regular time intervals. Once LL_ResumeTick()
403   *       is called, the SysTick interrupt will be enabled and so Tick increment
404   *       is resumed.
405   * @note This function is declared as __WEAK to be overwritten in case of other
406   *       implementations in user file.
407   * @param  None
408   * @retval None
409   */
LL_ResumeTick(void)410 __WEAK void LL_ResumeTick(void)
411 {
412     /* Enable SysTick Interrupt */
413     SET_BIT(SysTick->CTRL, SysTick_CTRL_TICKINT_Msk);
414 }
415 
416 /**
417   * @brief  Get the LL revision
418   * @param  None
419   * @retval version 0xXYZR (8bits for each decimal, R for RC)
420   */
LL_GetHalVersion(void)421 uint32_t LL_GetHalVersion(void)
422 {
423     return __TAE32F53xx_LL_VERSION;
424 }
425 
426 /**
427   * @brief  Returns words of the device unique identifier (UID based on 128 bits)
428   * @param  UID[] device unique identifier store buffer
429   * @return None
430   */
LL_GetUID(uint32_t * UID[4])431 void LL_GetUID(uint32_t *UID[4])
432 {
433     //:TODO: return the device UID
434 }
435 
436 /**
437   * @}
438   */
439 
440 
441 /** @defgroup TAE32F53xx_LL_Exported_Functions_Group3 TAE32F53xx LL Misc Functions
442   * @brief    TAE32F53xx LL Misc Functions
443   * @{
444   */
445 
446 /**
447   * @brief  LL Show Platform Information
448   * @param  None
449   * @return None
450   */
LL_ShowInfo(void)451 void LL_ShowInfo(void)
452 {
453     DBG_LogRaw("\n======================== Platform Information =======================\n");
454 
455     DBG_LogRaw("Tai-Action TAE32F53xx SDK "SDK_STAGE_STR" V%d.%d.%d "__DATE__" "__TIME__"\n\n", \
456                __TAE32F53xx_LL_VERSION_MAIN, __TAE32F53xx_LL_VERSION_SUB1, __TAE32F53xx_LL_VERSION_SUB2);
457 
458     DBG_LogRaw("CPU  clock %9u Hz\n", LL_SYSCTRL_SysclkGet());
459     DBG_LogRaw("AHB  clock %9u Hz\n", LL_SYSCTRL_AHBClkGet());
460     DBG_LogRaw("APB0 clock %9u Hz\n", LL_SYSCTRL_APB0ClkGet());
461     DBG_LogRaw("APB1 clock %9u Hz\n", LL_SYSCTRL_APB1ClkGet());
462 
463     DBG_LogRaw("HSE  clock %9u Hz\n", HSE_VALUE);
464     DBG_LogRaw("HSI  clock %9u Hz\n", HSI_VALUE);
465     DBG_LogRaw("LSI  clock %9u Hz\n", LSI_VALUE);
466 
467     DBG_LogRaw("=====================================================================\n\n");
468 }
469 
470 /**
471   * @brief  Delay 1ms
472   * @param  ms The time to delay in 1ms Unit
473   * @return None
474   */
delay_ms(uint32_t ms)475 void delay_ms(uint32_t ms)
476 {
477     LL_Delay(ms);
478 }
479 
480 /**
481   * @brief  printf array
482   * @param  ptr printf array pointer
483   * @param  len array len
484   * @retval None
485   */
printf_array(void * ptr,uint32_t len)486 void printf_array(void *ptr, uint32_t len)
487 {
488     uint32_t cnt = 0;
489     uint8_t *p_ptr = (uint8_t *)ptr;
490 
491     while (len--) {
492         DBG_LogRaw("%02x ", *p_ptr);
493         cnt++;
494         p_ptr++;
495 
496         if (!(cnt & 0x0f)) {
497             DBG_LogRaw("\r\n");
498         }
499     }
500 
501     if ((cnt & 0x0f) != 0x0f) {
502         DBG_LogRaw("\r\n");
503     }
504 }
505 
506 /**
507   * @}
508   */
509 
510 /**
511   * @}
512   */
513 
514 /* Private functions ---------------------------------------------------------*/
515 
516 
517 #endif /* LL_MODULE_ENABLED */
518 
519 
520 /**
521   * @}
522   */
523 
524 /**
525   * @}
526   */
527 
528 /************************* (C) COPYRIGHT Tai-Action *****END OF FILE***********/
529 
530