1 /*******************************************************************************
2 * Copyright (C) 2019, Huada Semiconductor Co.,Ltd All rights reserved.
3 *
4 * This software is owned and published by:
5 * Huada Semiconductor Co.,Ltd ("HDSC").
6 *
7 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
8 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
9 *
10 * This software contains source code for use with HDSC
11 * components. This software is licensed by HDSC to be adapted only
12 * for use in systems utilizing HDSC components. HDSC shall not be
13 * responsible for misuse or illegal use of this software for devices not
14 * supported herein. HDSC is providing this software "AS IS" and will
15 * not be responsible for issues arising from incorrect user implementation
16 * of the software.
17 *
18 * Disclaimer:
19 * HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
20 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
21 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
22 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
23 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
24 * WARRANTY OF NONINFRINGEMENT.
25 * HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
26 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
27 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
28 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
29 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
30 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
31 * SAVINGS OR PROFITS,
32 * EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
34 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
35 * FROM, THE SOFTWARE.
36 *
37 * This software may be replicated in part or whole for the licensed use,
38 * with the restriction that this Disclaimer and Copyright notice must be
39 * included with each copy of this software, whether used in part or whole,
40 * at all times.
41 */
42 /******************************************************************************/
43 /** \file ddl.c
44  **
45  ** Common API of DDL.
46  ** @link ddlGroup Some description @endlink
47  **
48  **   - 2019-03-03
49  **
50  ******************************************************************************/
51 
52 /******************************************************************************/
53 /* Include files                                                              */
54 /******************************************************************************/
55 #include "hc32l196_ddl.h"
56 
57 /**
58  ******************************************************************************
59  ** \addtogroup DDL Common Functions
60  ******************************************************************************/
61 //@{
62 
63 /******************************************************************************/
64 /* Local pre-processor symbols/macros ('#define')                             */
65 /******************************************************************************/
66 
67 /******************************************************************************/
68 /* Global variable definitions (declared in header file with 'extern')        */
69 /******************************************************************************/
70 
71 /******************************************************************************/
72 /* Local type definitions ('typedef')                                         */
73 /******************************************************************************/
74 
75 /******************************************************************************/
76 /* Local variable definitions ('static')                                      */
77 /******************************************************************************/
78 
79 /******************************************************************************/
80 /* Local function prototypes ('static')                                       */
81 /******************************************************************************/
82 
83 /******************************************************************************/
84 /* Function implementation - global ('extern') and local ('static')           */
85 /******************************************************************************/
86 #ifndef __DEBUG
87 #define __DEBUG
88 //#define __CC_ARM
89 #endif
90 
Log2(uint32_t u32Val)91 uint32_t Log2(uint32_t u32Val)
92 {
93     uint32_t u32V1 = 0;
94 
95     if(0u == u32Val)
96     {
97         return 0;
98     }
99 
100     while(u32Val > 1u)
101     {
102         u32V1++;
103         u32Val /=2;
104     }
105 
106     return u32V1;
107 }
108 
109 
110 /**
111  *******************************************************************************
112  ** \brief Memory clear function for DDL_ZERO_STRUCT()
113  ******************************************************************************/
ddl_memclr(void * pu8Address,uint32_t u32Count)114 void ddl_memclr(void *pu8Address, uint32_t u32Count)
115 {
116     uint8_t *pu8Addr = (uint8_t *)pu8Address;
117 
118     if(NULL == pu8Addr)
119     {
120         return;
121     }
122 
123     while (u32Count--)
124     {
125         *pu8Addr++ = 0;
126     }
127 }
128 
129 /**
130  * \brief   delay1ms
131  *          delay approximately 1ms.
132  * \param   [in]  u32Cnt
133  * \retval  void
134  */
delay1ms(uint32_t u32Cnt)135 void delay1ms(uint32_t u32Cnt)
136 {
137     uint32_t u32end;
138 
139     SysTick->LOAD = 0xFFFFFF;
140     SysTick->VAL  = 0;
141     SysTick->CTRL = SysTick_CTRL_ENABLE_Msk | SysTick_CTRL_CLKSOURCE_Msk;
142 
143     while(u32Cnt-- > 0)
144     {
145         SysTick->VAL  = 0;
146         u32end = 0x1000000 - SystemCoreClock/1000;
147         while(SysTick->VAL > u32end)
148         {
149             ;
150         }
151     }
152 
153     SysTick->CTRL = (SysTick->CTRL & (~SysTick_CTRL_ENABLE_Msk));
154 }
155 
156 /**
157  * \brief   delay100us
158  *          delay approximately 100us.
159  * \param   [in]  u32Cnt
160  * \retval  void
161  */
delay100us(uint32_t u32Cnt)162 void delay100us(uint32_t u32Cnt)
163 {
164     uint32_t u32end;
165 
166     SysTick->LOAD = 0xFFFFFF;
167     SysTick->VAL  = 0;
168     SysTick->CTRL = SysTick_CTRL_ENABLE_Msk | SysTick_CTRL_CLKSOURCE_Msk;
169 
170     while(u32Cnt-- > 0)
171     {
172         SysTick->VAL = 0;
173 
174         u32end = 0x1000000 - SystemCoreClock/10000;
175         while(SysTick->VAL > u32end)
176         {
177             ;
178         }
179     }
180 
181     SysTick->CTRL = (SysTick->CTRL & (~SysTick_CTRL_ENABLE_Msk));
182 }
183 
184 /**
185  * \brief   delay10us
186  *          delay approximately 10us.
187  * \param   [in]  u32Cnt
188  * \retval  void
189  */
delay10us(uint32_t u32Cnt)190 void delay10us(uint32_t u32Cnt)
191 {
192     uint32_t u32end;
193 
194     SysTick->LOAD = 0xFFFFFF;
195     SysTick->VAL  = 0;
196     SysTick->CTRL = SysTick_CTRL_ENABLE_Msk | SysTick_CTRL_CLKSOURCE_Msk;
197 
198     while(u32Cnt-- > 0)
199     {
200         SysTick->VAL = 0;
201 
202         u32end = 0x1000000 - SystemCoreClock/100000;
203         while(SysTick->VAL > u32end)
204         {
205             ;
206         }
207     }
208 
209     SysTick->CTRL = (SysTick->CTRL & (~SysTick_CTRL_ENABLE_Msk));
210 }
211 
212 /**
213  * \brief   set register bit
214  *
215  * \param   [in]  addr
216  * \param   [in]  offset
217  * \retval  void
218  */
SetBit(uint32_t addr,uint32_t offset,boolean_t bFlag)219 void SetBit(uint32_t addr, uint32_t offset, boolean_t bFlag)
220 {
221     if(TRUE == bFlag)
222     {
223         *((volatile uint32_t *)(addr)) |= ((1UL)<<(offset));
224     }
225     else
226     {
227         *((volatile uint32_t *)(addr)) &= (~(1UL<<(offset)));
228     }
229 
230 
231 }
232 
233 
234 /**
235  * \brief   get register bit
236  *
237  * \param   [in]  addr
238  * \param   [in]  offset
239  * \retval  void
240  */
GetBit(uint32_t addr,uint32_t offset)241 boolean_t GetBit(uint32_t addr, uint32_t offset)
242 {
243     return ((((*((volatile uint32_t *)(addr))) >> (offset)) & 1u) > 0) ? TRUE : FALSE;
244 }
245 //@} // DDL Functions
246 
247 /******************************************************************************
248  * EOF (not truncated)
249  ******************************************************************************/
250 
251