1 /*******************************************************************************
2 * Copyright (C) 2018, 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  **   - 2018-04-15
49  **
50  ******************************************************************************/
51 
52 /******************************************************************************/
53 /* Include files                                                              */
54 /******************************************************************************/
55 #include "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  *****************************************************************************
131  ** \brief Hook function, which is called in polling loops
132  *****************************************************************************/
DDL_WAIT_LOOP_HOOK(void)133 void DDL_WAIT_LOOP_HOOK(void)
134 {
135     // Place code for triggering Watchdog counters here, if needed
136 }
137 
138 /**
139  *****************************************************************************
140  ** \brief debug printf function.
141  *****************************************************************************/
Debug_UartInit(void)142 void Debug_UartInit(void)
143 {
144 
145 }
146 
Debug_Output(uint8_t u8Data)147 void Debug_Output(uint8_t u8Data)
148 {
149 
150 }
151 
152 extern void Debug_UartInit(void);
153 extern void Debug_Output(uint8_t u8Data);
154 
155 #if defined (__CC_ARM)          //KEIL
156 #pragma import(__use_no_semihosting)
_sys_exit(int x)157 void _sys_exit(int x)
158 {
159     (void)x;
160 }
161 struct __FILE
162 {
163     int handle;
164     /* Whatever you require here. If the only file you are using is */
165     /* standard output using printf() for debugging, no file handling */
166 /* is required. */
167 };
168 /* FILE is typedef?d in stdio.h. */
169 FILE __stdout;
170 
171 #endif
172 
173 #ifdef __DEBUG
174 /**
175  ******************************************************************************
176  ** \brief  Re-target putchar function
177  ******************************************************************************/
fputc(int ch,FILE * f)178 int fputc(int ch, FILE *f)
179 {
180 
181     if (((uint8_t)ch) == '\n')
182     {
183         Debug_Output('\r');
184     }
185     Debug_Output(ch);
186 
187     return ch;
188 }
189 #endif
190 
191 
__backspace(void)192 int __backspace(void)
193 {
194     return 0;
195 }
196 
197 
198 
199 /**
200  * \brief   delay1ms
201  *          delay approximately 1ms.
202  * \param   [in]  u32Cnt
203  * \retval  void
204  */
delay1ms(uint32_t u32Cnt)205 void delay1ms(uint32_t u32Cnt)
206 {
207     uint32_t u32end;
208 
209     SysTick->LOAD = 0xFFFFFF;
210     SysTick->VAL  = 0;
211     SysTick->CTRL = SysTick_CTRL_ENABLE_Msk | SysTick_CTRL_CLKSOURCE_Msk;
212 
213     while(u32Cnt-- > 0)
214     {
215         SysTick->VAL  = 0;
216         u32end = 0x1000000 - SystemCoreClock/1000;
217         while(SysTick->VAL > u32end)
218         {
219             ;
220         }
221     }
222 
223     SysTick->CTRL = (SysTick->CTRL & (~SysTick_CTRL_ENABLE_Msk));
224 }
225 
226 /**
227  * \brief   delay100us
228  *          delay approximately 100us.
229  * \param   [in]  u32Cnt
230  * \retval  void
231  */
delay100us(uint32_t u32Cnt)232 void delay100us(uint32_t u32Cnt)
233 {
234     uint32_t u32end;
235 
236     SysTick->LOAD = 0xFFFFFF;
237     SysTick->VAL  = 0;
238     SysTick->CTRL = SysTick_CTRL_ENABLE_Msk | SysTick_CTRL_CLKSOURCE_Msk;
239 
240     while(u32Cnt-- > 0)
241     {
242         SysTick->VAL = 0;
243 
244         u32end = 0x1000000 - SystemCoreClock/10000;
245         while(SysTick->VAL > u32end)
246         {
247             ;
248         }
249     }
250 
251     SysTick->CTRL = (SysTick->CTRL & (~SysTick_CTRL_ENABLE_Msk));
252 }
253 
254 /**
255  * \brief   delay10us
256  *          delay approximately 10us.
257  * \param   [in]  u32Cnt
258  * \retval  void
259  */
delay10us(uint32_t u32Cnt)260 void delay10us(uint32_t u32Cnt)
261 {
262     uint32_t u32end;
263 
264     SysTick->LOAD = 0xFFFFFF;
265     SysTick->VAL  = 0;
266     SysTick->CTRL = SysTick_CTRL_ENABLE_Msk | SysTick_CTRL_CLKSOURCE_Msk;
267 
268     while(u32Cnt-- > 0)
269     {
270         SysTick->VAL = 0;
271 
272         u32end = 0x1000000 - SystemCoreClock/100000;
273         while(SysTick->VAL > u32end)
274         {
275             ;
276         }
277     }
278 
279     SysTick->CTRL = (SysTick->CTRL & (~SysTick_CTRL_ENABLE_Msk));
280 }
281 
282 
283 //@} // DDL Functions
284 
285 /******************************************************************************
286  * EOF (not truncated)
287  ******************************************************************************/
288 
289