1 /**
2   ****************************************************************************************************
3   * @file    fm33lc0xx_fl.c
4   * @author  FMSH Application Team
5   * @brief   Source file of FL Driver Library
6   ****************************************************************************************************
7   * @attention
8   *
9   * Copyright (c) [2021] [Fudan Microelectronics]
10   * THIS SOFTWARE is licensed under Mulan PSL v2.
11   * You can use this software according to the terms and conditions of the Mulan PSL v2.
12   * You may obtain a copy of Mulan PSL v2 at:
13   *          http://license.coscl.org.cn/MulanPSL2
14   * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
15   * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
16   * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
17   * See the Mulan PSL v2 for more details.
18   *
19   ****************************************************************************************************
20   */
21 
22 
23 /* Includes ----------------------------------------------------------------------------------------*/
24 #include "fm33lc0xx_fl.h"
25 
26 /** @addtogroup FL_EF_DELAY
27   * @{
28   */
29 
30 /**
31   * @brief  Initialize the timer(default is Systick) used as delay timer.
32   * @note   The function is declared as __WEAK to be overwritten in case of other
33   *         implementation in user file.
34   * @param  None
35   * @retval None
36   */
FL_DelayInit(void)37 __WEAK void FL_DelayInit(void)
38 {
39     SysTick->CTRL |= SysTick_CTRL_CLKSOURCE_Msk;
40     SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
41 }
42 
43 /**
44   * @brief  Provide block delay in microseconds.
45   * @note   The function is declared as __WEAK to be overwritten in case of other
46   *         implementation in user file.
47   * @param  count   specifies the delay count in microseconds.
48   * @retval None
49   */
FL_DelayUs(uint32_t count)50 __WEAK void FL_DelayUs(uint32_t count)
51 {
52     count = FL_DELAY_US * count;
53     count = count > 16777216 ? 16777216 : count;
54     SysTick->LOAD = count - 1;
55     SysTick->VAL = 0;
56     while(!((SysTick->CTRL >> 16) & 0x1));
57 }
58 
59 /**
60   * @brief  Provide blocking delay in milliseconds.
61   * @note   The function is declared as __WEAK to be overwritten in case of other
62   *         implementation in user file.
63   * @param  count   specifies the delay count in milliseconds.
64   * @retval None
65   */
FL_DelayMs(uint32_t count)66 __WEAK void FL_DelayMs(uint32_t count)
67 {
68     while(count--)
69     {
70         FL_DelayUs(1000);
71     }
72 }
73 
74 /**
75   * @brief  Provide no-blocking delay initialization in microseconds.
76   * @note   Should be follow By while(!FL_DelayEnd()){ ** user code ** } immediately.
77   *         The function is declared as __WEAK to be overwritten in case of other
78   *         implementation in user file.
79   * @param  count   specifies the delay count in microseconds.
80   * @retval None
81   */
FL_DelayUsStart(uint32_t count)82 __WEAK void FL_DelayUsStart(uint32_t count)
83 {
84     count = FL_DELAY_US * count;
85     count = count > 16777216 ? 16777216 : count;
86     SysTick->LOAD = count - 1;
87     SysTick->VAL = 0;
88 }
89 
90 /**
91   * @brief  Provide no-blocking delay initialization in milliseconds.
92   * @note   Should be followed By while(!FL_DelayEnd()){ ** user code ** }.
93   *         The function is declared as __WEAK to be overwritten in case of other
94   *         implementation in user file.
95   * @param  count   specifies the delay count in milliseconds.
96   * @retval None
97   */
FL_DelayMsStart(uint32_t count)98 __WEAK void FL_DelayMsStart(uint32_t count)
99 {
100     FL_DelayUsStart(1000 * count);
101 }
102 
103 /**
104   * @brief  Showing if the no-blocking delay has ended.
105   * @note   Should be used with FL_DelayMs/UsStart() function.
106   *         The function is declared as __WEAK to be overwritten in case of other
107   *         implementation in user file.
108   * @param  count   specifies the delay count in milliseconds.
109   * @retval true  - delay has ended
110   *         false - delay is in progress
111   */
FL_DelayEnd(void)112 __WEAK bool FL_DelayEnd(void)
113 {
114     return (((SysTick->CTRL >> 16) & 0x1) == 0x1);
115 }
116 
117 /**
118   * @}
119   */
120 
121 /** @addtogroup FL_EF_DELAY
122   * @{
123   */
124 
FL_Init(void)125 void FL_Init(void)
126 {
127     /* Init delay support function */
128     FL_DelayInit();
129 }
130 
131 /**
132   * @}
133   */
134 
135 /** @addtogroup FL_EF_NVIC
136   * @{
137   */
138 
139 /**
140   * @brief  Configure NVIC for specified Interrupt.
141   * @param  configStruct    NVIC configuration.
142   * @param  irq             Interrupt number.
143   * @retval None
144   */
FL_NVIC_Init(FL_NVIC_ConfigTypeDef * configStruct,IRQn_Type irq)145 void FL_NVIC_Init(FL_NVIC_ConfigTypeDef *configStruct, IRQn_Type irq)
146 {
147     /* Check parameter */
148     if(configStruct->preemptPriority > 3)
149     {
150         configStruct->preemptPriority = 3;
151     }
152     NVIC_DisableIRQ(irq);
153     NVIC_SetPriority(irq, configStruct->preemptPriority);
154     NVIC_EnableIRQ(irq);
155 }
156 
157 /**
158   * @}
159   */
160 
161 /********************** (C) COPYRIGHT Fudan Microelectronics **** END OF FILE ***********************/
162 
163 
164 
165