-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCH56x_bsp.h
368 lines (335 loc) · 11 KB
/
CH56x_bsp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/********************************** (C) COPYRIGHT *******************************
* File Name : CH56x_bsp.h
* Author : bvernoux
* Version : V1.1.1
* Date : 2022/12/11
* Description : This file contains all the functions prototypes for
* Board Support Package(BSP) related to Init/Delays/Timebase
* DisableInterrupts/EnableInterrupts
* Copyright (c) 2022 Benjamin VERNOUX
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#ifndef __CH56x_BSP_H__
#define __CH56x_BSP_H__
#ifdef __cplusplus
extern "C" {
#endif
#include "CH56xSFR.h"
#include "CH56x_gpio.h"
extern uint64_t bsp_us_nbcycles;
extern uint64_t bsp_ms_nbcycles;
extern uint32_t bsp_tick_frequency;
/* Memory mapped structure for lowlevel SysTick access */
typedef struct __attribute__((packed))
{
__IO uint32_t CTLR;
__IO uint32_t CNT_LSB;
__IO uint32_t CNT_MSB;
__IO uint32_t CMP_LSB;
__IO uint32_t CMP_MSB;
__IO uint32_t CNTFG;
}
SysTickU32_Type;
#define SysTickU32 ((SysTickU32_Type *)0xE000F000)
/* Used by bsp_sync2boards() */
#define BSP_SYNCHRO_TIMEOUT (12000000) // About 700ms at 120MHz with -O3 / -Os
/* Definition of GPIO PORTA/PORTB Flag used in e_bsp_PortPinType */
#define BSP_PORTA_FLAG (0x00000000)
#define BSP_PORTB_FLAG (0x80000000)
#define BSP_PINBIT_MASK (0x7FFFFFFF)
/**
* @brief CH56x Dual board type
*/
typedef enum
{
BSP_BOARD1 = 0, /* Primary/main board (usually on Top) */
BSP_BOARD2 = 1 /* Secondary board (usually on Bottom)*/
} e_bsp_TypeDef;
/* WCH CH56x Port Pin definition */
typedef enum
{
PA0 = (BSP_PORTA_FLAG | GPIO_Pin_0 ),
PA1 = (BSP_PORTA_FLAG | GPIO_Pin_1 ),
PA2 = (BSP_PORTA_FLAG | GPIO_Pin_2 ),
PA3 = (BSP_PORTA_FLAG | GPIO_Pin_3 ),
PA4 = (BSP_PORTA_FLAG | GPIO_Pin_4 ),
PA5 = (BSP_PORTA_FLAG | GPIO_Pin_5 ),
PA6 = (BSP_PORTA_FLAG | GPIO_Pin_6 ),
/*
// Reserved for UART1
PA7 = (BSP_PORTA_FLAG | GPIO_Pin_7 ),
PA8 = (BSP_PORTA_FLAG | GPIO_Pin_8 ),
*/
PA9 = (BSP_PORTA_FLAG | GPIO_Pin_9 ),
PA10 = (BSP_PORTA_FLAG | GPIO_Pin_10),
PA11 = (BSP_PORTA_FLAG | GPIO_Pin_11),
PA12 = (BSP_PORTA_FLAG | GPIO_Pin_12),
PA13 = (BSP_PORTA_FLAG | GPIO_Pin_13),
PA14 = (BSP_PORTA_FLAG | GPIO_Pin_14),
PA15 = (BSP_PORTA_FLAG | GPIO_Pin_15),
PA16 = (BSP_PORTA_FLAG | GPIO_Pin_16),
PA17 = (BSP_PORTA_FLAG | GPIO_Pin_17),
PA18 = (BSP_PORTA_FLAG | GPIO_Pin_18),
PA19 = (BSP_PORTA_FLAG | GPIO_Pin_19),
PA20 = (BSP_PORTA_FLAG | GPIO_Pin_20),
PA21 = (BSP_PORTA_FLAG | GPIO_Pin_21),
PA22 = (BSP_PORTA_FLAG | GPIO_Pin_22),
PA23 = (BSP_PORTA_FLAG | GPIO_Pin_23),
PB0 = (BSP_PORTB_FLAG | GPIO_Pin_0 ),
PB1 = (BSP_PORTB_FLAG | GPIO_Pin_1 ),
PB2 = (BSP_PORTB_FLAG | GPIO_Pin_2 ),
PB3 = (BSP_PORTB_FLAG | GPIO_Pin_3 ),
PB4 = (BSP_PORTB_FLAG | GPIO_Pin_4 ),
PB5 = (BSP_PORTB_FLAG | GPIO_Pin_5 ),
PB6 = (BSP_PORTB_FLAG | GPIO_Pin_6 ),
PB7 = (BSP_PORTB_FLAG | GPIO_Pin_7 ),
PB8 = (BSP_PORTB_FLAG | GPIO_Pin_8 ),
PB9 = (BSP_PORTB_FLAG | GPIO_Pin_9 ),
PB10 = (BSP_PORTB_FLAG | GPIO_Pin_10),
PB11 = (BSP_PORTB_FLAG | GPIO_Pin_11),
PB12 = (BSP_PORTB_FLAG | GPIO_Pin_12),
PB13 = (BSP_PORTB_FLAG | GPIO_Pin_13),
PB14 = (BSP_PORTB_FLAG | GPIO_Pin_14),
PB15 = (BSP_PORTB_FLAG | GPIO_Pin_15),
PB16 = (BSP_PORTB_FLAG | GPIO_Pin_16),
PB17 = (BSP_PORTB_FLAG | GPIO_Pin_17),
PB18 = (BSP_PORTB_FLAG | GPIO_Pin_18),
PB19 = (BSP_PORTB_FLAG | GPIO_Pin_19),
PB20 = (BSP_PORTB_FLAG | GPIO_Pin_20),
PB21 = (BSP_PORTB_FLAG | GPIO_Pin_21),
PB22 = (BSP_PORTB_FLAG | GPIO_Pin_22),
PB23 = (BSP_PORTB_FLAG | GPIO_Pin_23)
} e_bsp_PortPinType;
/*******************************************************************************
* @fn bsp_init
*
* @brief Set MCU frequency and SysTick
*
* @return None
**/
void bsp_init(uint32_t systemclck);
/*******************************************************************************
* @fn bsp_disable_interrupt
*
* @brief Disable Global Interrupt (Enter Critical Section)
*
* @return None
**/
#define bsp_disable_interrupt() __asm volatile( "csrci mstatus,0x8" ) // Disable interrupt (mie = 0)
/*******************************************************************************
* @fn bsp_enable_interrupt
*
* @brief Enable Global Interrupt (Exit Critical Section)
*
* @return None
**/
#define bsp_enable_interrupt() __asm volatile ( "csrsi mstatus,0x8" ) // Enable interrupt (mie = 1)
/*******************************************************************************
* @fn bsp_get_tick
*
* @brief Returns the number of system ticks since the system boot
* This function is thread-safe
* Precondition: call to bsp_init
*
* @return Returns the number of system ticks since the system boot
**/
uint64_t bsp_get_tick(void);
/*******************************************************************************
* @fn bsp_wait_us_delay
*
* @brief Delay, wait for N microseconds
* This function is thread-safe
* Precondition: call to bsp_init
*
* @return None
**/
void bsp_wait_us_delay(uint32_t us);
/*******************************************************************************
* @fn bsp_wait_ms_delay
*
* @brief Delay, wait for N milliseconds
* This function is thread-safe
* Precondition: call to bsp_init
*
* @return None
**/
void bsp_wait_ms_delay(uint32_t ms);
/*******************************************************************************
* @fn bsp_wait_nb_cycles
*
* @brief Delay, wait for N MCU cycles
* This function is thread-safe
* Precondition: call to bsp_init
*
* @return None
**/
void bsp_wait_nb_cycles(uint32_t nb_cycles);
/*******************************************************************************
* @fn bsp_get_nbtick_1ms
*
* @brief Returns the Number of tick for 1 microsecond
* This function is thread-safe
* Precondition: call to bsp_init
*
* @return Returns the Number of tick for 1 microsecond
**/
#define bsp_get_nbtick_1us() (bsp_us_nbcycles)
/*******************************************************************************
* @fn bsp_get_nbtick_1ms
*
* @brief Returns the Number of tick for 1 millisecond
* This function is thread-safe
* Precondition: call to bsp_init
*
* @return Returns the Number of tick for 1 millisecond
**/
#define bsp_get_nbtick_1ms() (bsp_ms_nbcycles)
/*******************************************************************************
* @fn bsp_get_tick_frequency
*
* @brief Returns the tick frequency in Hz
* This function is thread-safe
* Precondition: call to bsp_init
*
* @return Returns the tick frequency in Hz
**/
#define bsp_get_tick_frequency() (bsp_tick_frequency)
/*******************************************************************************
* @fn bsp_get_SysTickCNT_LSB
*
* @brief Returns SysTick CNT 64bits
* Potential rollover is managed
* This function will always return correct 64bits CNT
* This function is thread-safe
* Precondition: call to bsp_init
* Note:
* CNT value is decremented at each MCU cycle
* CNT value is one's complement of bsp_get_tick()
*
* @return Returns SysTick CNT 64bits
**/
uint64_t bsp_get_SysTickCNT(void);
/*******************************************************************************
* @fn bsp_get_SysTickCNT_LSB
*
* @brief Returns SysTick CNT 32bits
* Shall be used only safely after startup during <36s
* This function is thread-safe
* Precondition: call to bsp_init
* Note:
* CNT value is decremented at each MCU cycle
* CNT value is one's complement of bsp_get_tick() 32bits only
*
* @return Returns SysTick CNT 32bits
**/
#define bsp_get_SysTickCNT_LSB() (SysTickU32->CNT_LSB)
/*******************************************************************************
* @fn bsp_gpio_cfg
*
* @brief Initializes GPIO Port Pin mode
*
* @param gpioPortPin - GPIO to configure
* mode - GPIO mode
*
* @return None
**/
void bsp_gpio_cfg(e_bsp_PortPinType gpioPortPin, GPIOModeTypeDef mode);
/*******************************************************************************
* @fn bsp_gpio_read
*
* @brief Read GPIO PortPin state
* Precondition: call to bsp_gpio_cfg()
*
* @param gpioPortPin - GPIO state to read
*
* @return 0 (pin low), !0 (pin high)
**/
int bsp_gpio_read(e_bsp_PortPinType gpioPortPin);
/*******************************************************************************
* @fn bsp_gpio_set
*
* @brief Set GPIO PortPin state
* Precondition: call to bsp_gpio_cfg()
*
* @param gpioPortPin - GPIO state to set to '1'
*
* @return None
**/
void bsp_gpio_set(e_bsp_PortPinType gpioPortPin);
/*******************************************************************************
* @fn bsp_gpio_clr
*
* @brief Read GPIO PortPin state
* Precondition: call to bsp_gpio_cfg()
*
* @param gpioPortPin - GPIO state to clear/reset to '0'
*
* @return None
**/
void bsp_gpio_clr(e_bsp_PortPinType gpioPortPin);
/*******************************************************************************
* @fn bsp_sync2boards
*
* @brief Synchronize 2x CH56x boards connected together(one on top of another)
* Precondition: call to bsp_init()
*
* @param gpio1 - 1st GPIO to be used for synchronization
* gpio2 - 2nd GPIO to be used for synchronization
* type - Type of Board
* - BSP_BOARD1 / Main board (usually on Top)
* - BSP_BOARD2 / Secondary board (usually on Bottom)
*
* @return !=0 if success or 0 in case of error(timeout)
**/
int bsp_sync2boards(e_bsp_PortPinType gpio1, e_bsp_PortPinType gpio2, e_bsp_TypeDef type);
/*******************************************************************************
* @fn bsp_gpio_init
*
* @brief Initializes board GPIO (mainly GPIO)
* To be implemented in board/xxx.c (xxx is the board used)
*
* @return None
**/
void bsp_gpio_init(void);
/*******************************************************************************
* @fn bsp_ubtn
*
* @brief Read Board state of button UBTN
* Precondition: call to bsp_init()
* To be implemented in board/xxx.c (xxx is the board used)
*
* @return 0 (pin low), !0 (pin high)
**/
int bsp_ubtn(void);
/*******************************************************************************
* @fn bsp_is_gpio_switch_on
*
* @brief Read GPIO switch state
* Precondition: call to bsp_init()
* To be implemented in board/xxx.c (xxx is the board used)
*
* @return 0 (Switch OFF), 1 (Switch ON)
**/
int bsp_switch(void);
/*******************************************************************************
* @fn bsp_uled_on
*
* @brief Set ULED to ON (Light ON)
* To be implemented in board/xxx.c (xxx is the board used)
*
* @return None
**/
void bsp_uled_on(void);
/*******************************************************************************
* @fn bsp_uled_off
*
* @brief Set ULED to OFF (Light OFF)
* To be implemented in board/xxx.c (xxx is the board used)
*
* @return None
**/
void bsp_uled_off(void);
#ifdef __cplusplus
}
#endif
#endif // __CH56x_BSP_H__