-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
394 lines (351 loc) · 13.5 KB
/
main.c
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*!****************************************************************************
* @file
* main.c
*
* @brief
* "Hello World"-like demo project for the CH32V003 RISC-V MCU.
*
* This project contains a simple set of modules to get the MCU running in a
* minimal configuration:
* - Polling-mode serial I/O on USART1 (connected to WCH-LinkE USART)
* All project files are also available online at:
* https://github.com/islandcontroller/hello-ch32v003
*
* @date 11.02.2022
* @date 14.02.2022 Added core information printout
* @date 17.02.2022 Added clocks, ESig information printout
* @date 17.02.2022 Added LED animation
* @date 23.02.2022 Added remote echo and serial input commands processing
* @date 24.02.2022 Added temperature sensor and Vrefint info printout
* @date 03.03.2022 Modified to used printf(), putchar() and getchar()
* @date 04.03.2022 Added EEPROM demo
* @date 10.03.2022 Added information block readout; Disabled EEPROM demo for
* default configuration
* @date 21.03.2023 Adapted from hello-ch32v103 for hello-ch32v003
* @date 23.03.2023 Added SysTick interrupt demo
* @date 02.10.2023 Moved system time to hw_stk
* @date 02.10.2023 Added LED demo
* @date 09.01.2024 Added EEPROM demo
******************************************************************************/
/*- Header files -------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "ch32v00x.h"
#include "hw_init.h"
#include "hw_stk.h"
#include "syscalls.h"
#include "dbgser.h"
#include "led.h"
#include "eeprom.h"
/*- Macros -------------------------------------------------------------------*/
/*! @brief Enable 24C64 EEPROM demo */
//#define USE_EEPROM_DEMO
/*! @brief Number of bytes to be read for EEPROM hexdump */
#define EEPROM_NUM_BYTES 256
/*! @brief Hexdump items per row */
#define HEXDUMP_ROW_ITEMS 16UL
/*- Private variables --------------------------------------------------------*/
/*! String lookup for XLEN definition field */
const char* const apszMisaMxl[4] = {
"0 - (undefined)",
"1 - 32-bit XLEN",
"2 - 64-bit XLEN",
"3 - 128-bit XLEN"
};
/*! Lookup table for ISA Extensions */
const char* const apszMisaExt[26] = {
"A - Atomic extension",
"B - (reserved) Bit-Manipulation extension",
"C - Compressed extension",
"D - Double-precision floating-point extension",
"E - RV32E base ISA",
"F - Single-precision floating-point extension",
"G - Additional standard extensions present",
"H - Hypervisor extension",
"I - RV32I/RV64I/RV128I base ISA",
"J - (reserved) Dynamically Translated Languages extension",
"K - (reserved)",
"L - (reserved) Decimal Floating-Point extension",
"M - Integer Multiply/Divide extension",
"N - User-level interrupts supported",
"O - (reserved)",
"P - (reserved) Packed-SIMD extension",
"Q - Quad-precision floating-point extension",
"R - (reserved)",
"S - Supervisor mode implemented",
"T - (reserved) Transactional Memory extension",
"U - User mode implemented",
"V - (reserved) Vector extension",
"W - (reserved)",
"X - Non-standard extensions present",
"Y - (reserved)",
"Z - (reserved)"
};
#ifdef USE_EEPROM_DEMO
/*! EEPROM demo data to be programmed */
const char* const pszEepromData = "CH32V003 I2C Demo";
#endif /* USE_EEPROM_DEMO */
/*- Private functions --------------------------------------------------------*/
/*!****************************************************************************
* @brief
* Print plain-text info from MISA
* @date 12.02.2022
* @date 03.03.2022 Modified to use printf()
******************************************************************************/
static void vPrintCoreInfo(void)
{
uint32_t ulMArchId = __get_MARCHID();
uint32_t ulMVendorId = __get_MVENDORID();
uint32_t ulMImpId = __get_MIMPID();
uint32_t ulMISA = __get_MISA();
printf(
"-- Core Information ------------------------------\r\n"
);
/* Print register values */
printf("MARCHID: 0x%08lX\r\n", ulMArchId);
printf("MIMPID: 0x%08lX\r\n", ulMImpId);
printf("MVENDORID: 0x%08lX\r\n", ulMVendorId);
printf("MISA: 0x%08lX\r\n", ulMISA);
/* Print MXL configuration */
unsigned uMxl = (ulMISA >> 30) & 0x3UL;
printf(" MXL:\r\n %s\r\n", apszMisaMxl[uMxl]);
/* Print extensions information */
printf(" Extensions:\r\n");
for (unsigned i = 0; i < 26; ++i)
{
if (ulMISA & (1UL << i)) printf(" %s\r\n", apszMisaExt[i]);
}
}
/*!****************************************************************************
* @brief
* Print plain-text system core clock information
*
* @note
* Run SystemCoreClockUpdate() at least once before calling this function to
* ensure HCLK value recalculation.
*
* @date 14.02.2022
* @date 03.03.2022 Modified to use printf()
******************************************************************************/
static void vPrintSysCoreClk(void)
{
printf(
"-- Clocks ----------------------------------------\r\n"
);
unsigned uKHz = SystemCoreClock / 1000;
unsigned uMHz = uKHz / 1000;
unsigned uKHzRem = uKHz % 1000;
printf("f_HCLK = %d.%03d MHz\r\n", uMHz, uKHzRem);
}
/*!****************************************************************************
* @brief
* Print flash size and device ID information
*
* @date 17.02.2022
* @date 03.03.2022 Modified to use printf()
******************************************************************************/
static void vPrintEsigInfo(void)
{
const volatile uint16_t* puiFlSize = (const void*)0x1FFFF7E0UL;
const volatile uint32_t* pulUID = (const void*)0x1FFFF7E8UL;
printf(
"-- ESIG ------------------------------------------\r\n"
);
printf("FLASH Size: %d KB\r\n", *puiFlSize);
printf("Unique ID: %08lX %08lX %08lX\r\n", pulUID[2], pulUID[1], pulUID[0]);
}
/*!****************************************************************************
* @brief
* Print Hexdump of memory buffer
*
* @param[in] *pBuffer Data buffer
* @param[in] uLen Number of bytes to display
* @param[in] uBaseAdr Base address for row counters
* @date 10.03.2022
******************************************************************************/
static void vPrintHexDump(const uint8_t* pBuffer, unsigned uLen, unsigned uBaseAdr)
{
for (unsigned uRow = 0; uRow < uLen / HEXDUMP_ROW_ITEMS; ++uRow)
{
/* Address or Offset */
unsigned uRowAddr = uRow * HEXDUMP_ROW_ITEMS;
printf("%08x ", uRowAddr + uBaseAdr);
/* Byte columns */
for (unsigned uCol = 0; uCol < HEXDUMP_ROW_ITEMS; ++uCol)
{
unsigned char ucData = pBuffer[uRowAddr + uCol];
printf("%02x ", ucData);
if (uCol == (HEXDUMP_ROW_ITEMS / 2 - 1)) putchar(' ');
}
/* ASCII text representation */
putchar(' ');
for (unsigned uCol = 0; uCol < HEXDUMP_ROW_ITEMS; ++uCol)
{
unsigned char ucData = pBuffer[uRowAddr + uCol];
putchar(isprint(ucData) ? ucData : '.');
}
printf("\r\n");
}
}
#ifdef USE_EEPROM_DEMO
/*!****************************************************************************
* @brief
* Print EEPROM hexdump
*
* @date 04.03.2022
* @date 10.03.2022 Moved hexdump printout into separate routine
******************************************************************************/
static void vPrintEepromData(void)
{
unsigned char aucBuffer[EEPROM_NUM_BYTES];
/* Time readout into buffer */
printf("Reading EEPROM... ");
unsigned uStart = ulHW_STKGetSystemTime();
vReadEeprom(aucBuffer, 0, EEPROM_NUM_BYTES);
unsigned uDuration = (ulHW_STKGetSystemTime() - uStart);
printf("done. Read %d bytes in %d ms.\r\n", EEPROM_NUM_BYTES, uDuration);
/* Hexdump printout */
vPrintHexDump(aucBuffer, EEPROM_NUM_BYTES, 0);
}
#endif /* USE_EEPROM_DEMO */
/*!****************************************************************************
* @brief
* Print User Sel. and Vendor Config. Words
*
* @date 10.03.2022
* @date 21.03.2023 Amended for CH32V003
******************************************************************************/
static void vPrintInfoBlockWords(void)
{
const void* pUserSelWord = (const void*)0x1FFFF800UL;
const void* pVendConfWord = (const void*)0x1FFFF7C0UL;
/* Hexdump printout */
printf("User selection word:\r\n");
vPrintHexDump(pUserSelWord, 64, (unsigned)pUserSelWord);
printf("Vendor configuration word:\r\n");
vPrintHexDump(pVendConfWord, 64, (unsigned)pVendConfWord);
}
/*!****************************************************************************
* @brief
* Serial input processing
*
* @date 23.02.2022
* @date 24.02.2022 Changed PFIC naming convention
* @date 24.02.2022 Added analog inputs readout command; modified help text
* @date 03.03.2022 Modified to use printf(), putchar(), getchar()
* @date 04.03.2022 Added EEPROM readout demo
* @date 10.03.2022 Added information block readout command
* @date 21.03.2023 Removed analog, EEPROM for ch32v003
* @date 23.03.2023 Added system timer value readout
* @date 09.01.2024 Void non-printable characters
* @date 09.01.2024 Added EEPROM
******************************************************************************/
static void vPollSerial(void)
{
/* Early exit, if no data is available */
if (!bIsDbgSerAvailable()) return;
/* Fetch character and print remote echo */
char c = getchar();
if (!isprint(c)) return;
else printf("%c\r\n", c);
/* Process command */
switch (c)
{
case '?':
/* Show available commands */
printf(
"Available Commands:\r\n"
" ? Show this help\r\n"
#ifdef USE_EEPROM_DEMO
" e Read EEPROM\r\n"
#endif /* USE_EEPROM_DEMO */
" i Read information block\r\n"
" r Reboot system\r\n"
" t Print system time\r\n"
);
break;
#ifdef USE_EEPROM_DEMO
case 'e':
/* Print EEPROM hexdump */
vPrintEepromData();
break;
#endif /* USE_EEPROM_DEMO */
case 'i':
/* Read information block */
vPrintInfoBlockWords();
break;
case 'r':
/* Reboot command */
PFIC_SystemReset();
break;
case 't':
/* Print system time */
printf("System time: %lu ms\r\n", ulHW_STKGetSystemTime());
break;
default:
fprintf(stderr, "Unknown command. Press \"?\" to show available commands.\r\n");
}
/* Input prompt */
putchar('>');
fflush(stdout);
}
/*!****************************************************************************
* @brief
* Main program entry point
*
* @date 11.02.2022
* @date 14.02.2022 Added core information printout
* @date 17.02.2022 Added clocks, ESig information printout
* @date 17.02.2022 Added LED animation
* @date 23.02.2022 Added serial input processing
* @date 24.02.2022 Added help prompt
* @date 03.03.2022 Modified to use printf()
* @date 03.03.2022 Moved escape sequence into dbgser macro
* @date 04.03.2022 Added EEPROM programming
* @date 21.03.2023 Adapted for hello-ch32v003; Removed LED, Analog, EEPROM
* @date 02.10.2023 Added LED demo
* @date 09.01.2024 Added EEPROM
******************************************************************************/
int main(void)
{
vInitHW();
/* Init syscalls retargeting */
vInitSyscalls();
/* Init demos */
vInitLed();
/* Print system info */
printf(
VT100_CLEAR_TERM
"--------------------------------------------------\r\n"
" ## \r\n"
"\r\n"
" ## ###### \r\n"
" ## ## \r\n"
" ## ## \r\n"
" ## ###### ###### @islandc_\r\n"
"--------------------------------------------------\r\n"
" MCU: WCH CH32V003F4P6 \r\n"
" Core: RISC-V2A (RV32EC) \r\n"
"--------------------------------------------------\r\n"
"\r\n"
);
vPrintCoreInfo();
printf("\r\n");
vPrintSysCoreClk();
printf("\r\n");
vPrintEsigInfo();
#ifdef USE_EEPROM_DEMO
printf("\r\nWriting EEPROM... ");
vWriteEeprom((const unsigned char*)pszEepromData, 0, strlen(pszEepromData));
printf("done.");
#endif /* USE_EEPROM_DEMO */
printf("\r\nPress \"?\" to show available commands.\r\n>");
fflush(stdout);
/* Main program loop */
while (1)
{
vPollSerial();
vPollLed();
}
}