formatting changes

This commit is contained in:
Jake Goodwin 2025-03-08 17:09:08 -08:00
parent 96b0a72533
commit 1f17f8dcdf
3 changed files with 1867 additions and 1691 deletions

384
src/ch32fun.c Executable file → Normal file
View file

@ -85,12 +85,12 @@ void __libc_init_array(void)
#define _SSP_STRING_H_
#define _SSP_STDIO_H_
#include "ch32fun.h"
#include <limits.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <limits.h>
#include <stdint.h>
#include "ch32fun.h"
#define WEAK __attribute__((weak))
@ -179,26 +179,36 @@ typedef void * mbstate_t;
WEAK size_t wcrtomb(char *restrict s, wchar_t wc, mbstate_t *restrict st)
{
if (!s) return 1;
if ((unsigned)wc < 0x80) {
if ((unsigned)wc < 0x80)
{
*s = wc;
return 1;
} else if (MB_CUR_MAX == 1) {
if (!IS_CODEUNIT(wc)) {
}
else if (MB_CUR_MAX == 1)
{
if (!IS_CODEUNIT(wc))
{
errno = 0x02; // EILSEQ
return -1;
}
*s = wc;
return 1;
} else if ((unsigned)wc < 0x800) {
}
else if ((unsigned)wc < 0x800)
{
*s++ = 0xc0 | (wc >> 6);
*s = 0x80 | (wc & 0x3f);
return 2;
} else if ((unsigned)wc < 0xd800 || (unsigned)wc-0xe000 < 0x2000) {
}
else if ((unsigned)wc < 0xd800 || (unsigned)wc - 0xe000 < 0x2000)
{
*s++ = 0xe0 | (wc >> 12);
*s++ = 0x80 | ((wc >> 6) & 0x3f);
*s = 0x80 | (wc & 0x3f);
return 3;
} else if ((unsigned)wc-0x10000 < 0x100000) {
}
else if ((unsigned)wc - 0x10000 < 0x100000)
{
*s++ = 0xf0 | (wc >> 18);
*s++ = 0x80 | ((wc >> 12) & 0x3f);
*s++ = 0x80 | ((wc >> 6) & 0x3f);
@ -217,40 +227,56 @@ WEAK int wctomb(char *s, wchar_t wc)
WEAK size_t strlen(const char *s)
{
const char *a = s;
for (; *s; s++);
for (; *s; s++)
;
return s - a;
}
WEAK size_t strnlen(const char *s, size_t n) { const char *p = memchr(s, 0, n); return p ? (size_t)(p-s) : n;}
WEAK void *memset(void *dest, int c, size_t n) { unsigned char *s = dest; for (; n; n--, s++) *s = c; return dest; }
WEAK size_t strnlen(const char *s, size_t n)
{
const char *p = memchr(s, 0, n);
return p ? (size_t)(p - s) : n;
}
WEAK void *memset(void *dest, int c, size_t n)
{
unsigned char *s = dest;
for (; n; n--, s++)
*s = c;
return dest;
}
WEAK char *strcpy(char *d, const char *s)
{
char *d0 = d;
for (; (*d=*s); s++, d++);
for (; (*d = *s); s++, d++)
;
return d0;
}
WEAK char *strncpy(char *d, const char *s, size_t n)
{
char *d0 = d;
for (; n && (*d=*s); n--, s++, d++);
for (; n && (*d = *s); n--, s++, d++)
;
return d0;
}
WEAK int strcmp(const char *l, const char *r)
{
for (; *l==*r && *l; l++, r++);
for (; *l == *r && *l; l++, r++)
;
return *(unsigned char *)l - *(unsigned char *)r;
}
WEAK int strncmp(const char *_l, const char *_r, size_t n)
{
const unsigned char *l = (void *)_l, *r = (void *)_r;
if (!n--) return 0;
for (; *l && *r && n && *l == *r ; l++, r++, n--);
for (; *l && *r && n && *l == *r; l++, r++, n--)
;
return *l - *r;
}
static char *twobyte_strstr(const unsigned char *h, const unsigned char *n)
{
uint16_t nw = n[0] << 8 | n[1], hw = h[0] << 8 | h[1];
for (h++; *h && hw != nw; hw = hw<<8 | *++h);
for (h++; *h && hw != nw; hw = hw << 8 | *++h)
;
return *h ? (char *)h - 1 : 0;
}
@ -258,7 +284,8 @@ static char *threebyte_strstr(const unsigned char *h, const unsigned char *n)
{
uint32_t nw = (uint32_t)n[0] << 24 | n[1] << 16 | n[2] << 8;
uint32_t hw = (uint32_t)h[0] << 24 | h[1] << 16 | h[2] << 8;
for (h+=2; *h && hw != nw; hw = (hw|*++h)<<8);
for (h += 2; *h && hw != nw; hw = (hw | *++h) << 8)
;
return *h ? (char *)h - 2 : 0;
}
@ -266,7 +293,8 @@ static char *fourbyte_strstr(const unsigned char *h, const unsigned char *n)
{
uint32_t nw = (uint32_t)n[0] << 24 | n[1] << 16 | n[2] << 8 | n[3];
uint32_t hw = (uint32_t)h[0] << 24 | h[1] << 16 | h[2] << 8 | h[3];
for (h+=3; *h && hw != nw; hw = hw<<8 | *++h);
for (h += 3; *h && hw != nw; hw = hw << 8 | *++h)
;
return *h ? (char *)h - 3 : 0;
}
@ -289,18 +317,29 @@ static char *twoway_strstr(const unsigned char *h, const unsigned char *n)
if (n[l]) return 0; /* hit the end of h */
/* Compute maximal suffix */
ip = -1; jp = 0; k = p = 1;
while (jp+k<l) {
if (n[ip+k] == n[jp+k]) {
if (k == p) {
ip = -1;
jp = 0;
k = p = 1;
while (jp + k < l)
{
if (n[ip + k] == n[jp + k])
{
if (k == p)
{
jp += p;
k = 1;
} else k++;
} else if (n[ip+k] > n[jp+k]) {
}
else
k++;
}
else if (n[ip + k] > n[jp + k])
{
jp += k;
k = 1;
p = jp - ip;
} else {
}
else
{
ip = jp++;
k = p = 1;
}
@ -309,72 +348,100 @@ static char *twoway_strstr(const unsigned char *h, const unsigned char *n)
p0 = p;
/* And with the opposite comparison */
ip = -1; jp = 0; k = p = 1;
while (jp+k<l) {
if (n[ip+k] == n[jp+k]) {
if (k == p) {
ip = -1;
jp = 0;
k = p = 1;
while (jp + k < l)
{
if (n[ip + k] == n[jp + k])
{
if (k == p)
{
jp += p;
k = 1;
} else k++;
} else if (n[ip+k] < n[jp+k]) {
}
else
k++;
}
else if (n[ip + k] < n[jp + k])
{
jp += k;
k = 1;
p = jp - ip;
} else {
}
else
{
ip = jp++;
k = p = 1;
}
}
if (ip+1 > ms+1) ms = ip;
else p = p0;
if (ip + 1 > ms + 1)
ms = ip;
else
p = p0;
/* Periodic needle? */
if (memcmp(n, n+p, ms+1)) {
if (memcmp(n, n + p, ms + 1))
{
mem0 = 0;
p = MAX(ms, l - ms - 1) + 1;
} else mem0 = l-p;
}
else
mem0 = l - p;
mem = 0;
/* Initialize incremental end-of-haystack pointer */
z = h;
/* Search loop */
for (;;) {
for (;;)
{
/* Update incremental end-of-haystack pointer */
if ((size_t)(z-h) < l) {
if ((size_t)(z - h) < l)
{
/* Fast estimate for MAX(l,63) */
size_t grow = l | 63;
const unsigned char *z2 = memchr(z, 0, grow);
if (z2) {
if (z2)
{
z = z2;
if ((size_t)(z - h) < l) return 0;
} else z += grow;
}
else
z += grow;
}
/* Check last byte first; advance by shift on mismatch */
if (BITOP(byteset, h[l-1], &)) {
if (BITOP(byteset, h[l - 1], &))
{
k = l - shift[h[l - 1]];
if (k) {
if (k)
{
if (k < mem) k = mem;
h += k;
mem = 0;
continue;
}
} else {
}
else
{
h += l;
mem = 0;
continue;
}
/* Compare right half */
for (k=MAX(ms+1,mem); n[k] && n[k] == h[k]; k++);
if (n[k]) {
for (k = MAX(ms + 1, mem); n[k] && n[k] == h[k]; k++)
;
if (n[k])
{
h += k - ms;
mem = 0;
continue;
}
/* Compare left half */
for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
for (k = ms + 1; k > mem && n[k - 1] == h[k - 1]; k--)
;
if (k <= mem) return (char *)h;
h += p;
mem = mem0;
@ -385,7 +452,8 @@ WEAK char *strchr(const char *s, int c)
{
c = (unsigned char)c;
if (!c) return (char *)s + strlen(s);
for (; *s && *(unsigned char *)s != c; s++);
for (; *s && *(unsigned char *)s != c; s++)
;
return (char *)s;
}
@ -407,12 +475,12 @@ WEAK char *strstr(const char *h, const char *n)
return twoway_strstr((void *)h, (void *)n);
}
WEAK void *__memrchr(const void *m, int c, size_t n)
{
const unsigned char *s = m;
c = (unsigned char)c;
while (n--) if (s[n]==c) return (void *)(s+n);
while (n--)
if (s[n] == c) return (void *)(s + n);
return 0;
}
@ -425,18 +493,19 @@ WEAK void *memcpy(void *dest, const void *src, size_t n)
{
unsigned char *d = dest;
const unsigned char *s = src;
for (; n; n--) *d++ = *s++;
for (; n; n--)
*d++ = *s++;
return dest;
}
WEAK int memcmp(const void *vl, const void *vr, size_t n)
{
const unsigned char *l = vl, *r = vr;
for (; n && *l == *r; n--, l++, r++);
for (; n && *l == *r; n--, l++, r++)
;
return n ? *l - *r : 0;
}
WEAK void *memmove(void *dest, const void *src, size_t n)
{
char *d = dest;
@ -445,10 +514,15 @@ WEAK void *memmove(void *dest, const void *src, size_t n)
if (d == s) return d;
if ((uintptr_t)s - (uintptr_t)d - n <= -2 * n) return memcpy(d, s, n);
if (d<s) {
for (; n; n--) *d++ = *s++;
} else {
while (n) n--, d[n] = s[n];
if (d < s)
{
for (; n; n--)
*d++ = *s++;
}
else
{
while (n)
n--, d[n] = s[n];
}
return dest;
@ -457,7 +531,8 @@ WEAK void *memchr(const void *src, int c, size_t n)
{
const unsigned char *s = src;
c = (unsigned char)c;
for (; n && *s != c; s++, n--);
for (; n && *s != c; s++, n--)
;
return n ? (void *)s : 0;
}
@ -506,13 +581,15 @@ mini_itoa(long value, unsigned int radix, int uppercase, int unsig,
if (radix > 16)
return 0;
if (value < 0 && !unsig) {
if (value < 0 && !unsig)
{
negative = 1;
value = -value;
}
/* This builds the string back to front ... */
do {
do
{
int digit = value % radix;
*(pbuffer++) = (digit < 10 ? '0' + digit : (uppercase ? 'A' : 'a') + digit - 10);
value /= radix;
@ -526,7 +603,8 @@ mini_itoa(long value, unsigned int radix, int uppercase, int unsig,
/* ... now we reverse it (could do it recursively but will
* conserve the stack space) */
len = (pbuffer - buffer);
for (i = 0; i < len / 2; i++) {
for (i = 0; i < len / 2; i++)
{
char j = buffer[i];
buffer[i] = buffer[len - i - 1];
buffer[len - i - 1] = j;
@ -542,26 +620,32 @@ mini_pad(char* ptr, int len, char pad_char, int pad_to, char *buffer)
int overflow = 0;
char *pbuffer = buffer;
if (pad_to == 0) pad_to = len;
if(len > pad_to) {
if (len > pad_to)
{
len = pad_to;
overflow = 1;
}
for(i = pad_to - len; i > 0; i --) {
for (i = pad_to - len; i > 0; i--)
{
*(pbuffer++) = pad_char;
}
for(i = len; i > 0; i --) {
for (i = len; i > 0; i--)
{
*(pbuffer++) = *(ptr++);
}
len = pbuffer - buffer;
if(overflow) {
for (i = 0; i < 3 && pbuffer > buffer; i ++) {
if (overflow)
{
for (i = 0; i < 3 && pbuffer > buffer; i++)
{
*(pbuffer-- - 1) = '*';
}
}
return len;
}
struct mini_buff {
struct mini_buff
{
char *buffer, *pbuffer;
unsigned int buffer_len;
};
@ -574,8 +658,10 @@ _puts(char *s, int len, void *buf)
char *p0 = b->buffer;
int i;
/* Copy to buffer */
for (i = 0; i < len; i++) {
if(b->pbuffer == b->buffer + b->buffer_len - 1) {
for (i = 0; i < len; i++)
{
if (b->pbuffer == b->buffer + b->buffer_len - 1)
{
break;
}
*(b->pbuffer++) = s[i];
@ -600,8 +686,7 @@ void mini_printf_set_handler(
}
#endif
int
mini_vsnprintf(char *buffer, unsigned int buffer_len, const char *fmt, va_list va)
int mini_vsnprintf(char *buffer, unsigned int buffer_len, const char *fmt, va_list va)
{
struct mini_buff b;
b.buffer = buffer;
@ -609,14 +694,14 @@ mini_vsnprintf(char *buffer, unsigned int buffer_len, const char *fmt, va_list v
b.buffer_len = buffer_len;
if (buffer_len == 0) buffer = (void *)0;
int n = mini_vpprintf(_puts, (buffer != (void *)0) ? &b : (void *)0, fmt, va);
if(buffer == (void*) 0) {
if (buffer == (void *)0)
{
return n;
}
return b.pbuffer - b.buffer;
}
int
mini_vpprintf(int (*puts)(char* s, int len, void* buf), void* buf, const char *fmt, va_list va)
int mini_vpprintf(int (*puts)(char *s, int len, void *buf), void *buf, const char *fmt, va_list va)
{
char bf[24];
char bf2[24];
@ -624,17 +709,23 @@ mini_vpprintf(int (*puts)(char* s, int len, void* buf), void* buf, const char *f
#ifdef MINI_PRINTF_ENABLE_OBJECTS
void *obj;
#endif
if(puts == (void*)0) {
if (puts == (void *)0)
{
/* run puts in counting mode. */
puts = _puts; buf = (void*)0;
puts = _puts;
buf = (void *)0;
}
int n = 0;
while ((ch=*(fmt++))) {
while ((ch = *(fmt++)))
{
int len;
if (ch!='%') {
if (ch != '%')
{
len = 1;
len = puts(&ch, len, buf);
} else {
}
else
{
char pad_char = ' ';
int pad_to = 0;
char l = 0;
@ -644,29 +735,39 @@ mini_vpprintf(int (*puts)(char* s, int len, void* buf), void* buf, const char *f
/* Zero padding requested */
if (ch == '0') pad_char = '0';
while (ch >= '0' && ch <= '9') {
while (ch >= '0' && ch <= '9')
{
pad_to = pad_to * 10 + (ch - '0');
ch = *(fmt++);
}
if(pad_to > (signed int) sizeof(bf)) {
if (pad_to > (signed int)sizeof(bf))
{
pad_to = sizeof(bf);
}
if (ch == 'l') {
if (ch == 'l')
{
l = 1;
ch = *(fmt++);
}
switch (ch) {
switch (ch)
{
case 0:
goto end;
case 'u':
case 'd':
if(l) {
if (l)
{
len = mini_itoa(va_arg(va, unsigned long), 10, 0, (ch == 'u'), bf2);
} else {
if(ch == 'u') {
}
else
{
if (ch == 'u')
{
len = mini_itoa((unsigned long)va_arg(va, unsigned int), 10, 0, 1, bf2);
} else {
}
else
{
len = mini_itoa((long)va_arg(va, int), 10, 0, 0, bf2);
}
}
@ -676,9 +777,12 @@ mini_vpprintf(int (*puts)(char* s, int len, void* buf), void* buf, const char *f
case 'x':
case 'X':
if(l) {
if (l)
{
len = mini_itoa(va_arg(va, unsigned long), 16, (ch == 'X'), 1, bf2);
} else {
}
else
{
len = mini_itoa((unsigned long)va_arg(va, unsigned int), 16, (ch == 'X'), 1, bf2);
}
len = mini_pad(bf2, len, pad_char, pad_to, bf);
@ -694,10 +798,13 @@ mini_vpprintf(int (*puts)(char* s, int len, void* buf), void* buf, const char *f
case 's':
ptr = va_arg(va, char *);
len = mini_strlen(ptr);
if (pad_to > 0) {
if (pad_to > 0)
{
len = mini_pad(ptr, len, pad_char, pad_to, bf);
len = puts(bf, len, buf);
} else {
}
else
{
len = puts(ptr, len, buf);
}
break;
@ -706,10 +813,13 @@ mini_vpprintf(int (*puts)(char* s, int len, void* buf), void* buf, const char *f
case 'R': /* Object by representation (e.g. repr)*/
obj = va_arg(va, void *);
len = mini_handler(mini_handler_data, obj, ch, pad_to, &ptr);
if (pad_to > 0) {
if (pad_to > 0)
{
len = mini_pad(ptr, len, pad_char, pad_to, bf);
len = puts(bf, len, buf);
} else {
}
else
{
len = puts(ptr, len, buf);
}
mini_handler_freeor(mini_handler_data, ptr);
@ -727,9 +837,7 @@ end:
return n;
}
int
mini_snprintf(char* buffer, unsigned int buffer_len, const char *fmt, ...)
int mini_snprintf(char *buffer, unsigned int buffer_len, const char *fmt, ...)
{
int ret;
va_list va;
@ -740,8 +848,7 @@ mini_snprintf(char* buffer, unsigned int buffer_len, const char *fmt, ...)
return ret;
}
int
mini_pprintf(int (*puts)(char*s, int len, void* buf), void* buf, const char *fmt, ...)
int mini_pprintf(int (*puts)(char *s, int len, void *buf), void *buf, const char *fmt, ...)
{
int ret;
va_list va;
@ -752,7 +859,6 @@ mini_pprintf(int (*puts)(char*s, int len, void* buf), void* buf, const char *fmt
return ret;
}
/*
C version of CH32V003 Startup .s file from WCH
This file is public domain where possible or the following where not:
@ -777,13 +883,15 @@ extern uint32_t * _edata;
#if FUNCONF_USE_DEBUGPRINTF
static void PrintN(uint32_t n)
{
while( (*DMDATA0) & 0x80 );
while ((*DMDATA0) & 0x80)
;
// Write out character.
*DMDATA0 = 0x78302088; //" 0x"
int shift;
for (shift = 28; shift >= 0; shift -= 4)
{
while( (*DMDATA0) & 0x80 );
while ((*DMDATA0) & 0x80)
;
int s = (n >> shift) & 0xf;
s += (s < 10) ? '0' : ('a' - 10);
*DMDATA0 = 0x85 | (s << 8); //" 0x"
@ -817,9 +925,11 @@ void DefaultIRQHandler( void )
PrintN(__get_MTVAL());
PrintN(__get_MCAUSE());
#if FUNCONF_USE_DEBUGPRINTF
while( (*DMDATA0) & 0x80 );
while ((*DMDATA0) & 0x80)
;
*DMDATA0 = 0x0a85;
while( (*DMDATA0) & 0x80 );
while ((*DMDATA0) & 0x80)
;
*DMDATA0 = 0xaaaaaa83;
#elif FUNCONF_USE_UARTPRINTF
putchar('\n');
@ -967,7 +1077,6 @@ void TIM2_CC_IRQHandler( void ) __attribute__((section(".text.vector_handler")
void TIM2_TRG_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used));
void TIM2_BRK_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used));
void InterruptVector() __attribute__((naked)) __attribute((section(".init"))) __attribute((weak, alias("InterruptVectorDefault"))) __attribute((naked));
void InterruptVectorDefault() __attribute__((naked)) __attribute((section(".init"))) __attribute((naked));
void handle_reset(void) __attribute__((section(".text.handle_reset")));
@ -1103,8 +1212,7 @@ void handle_reset( void )
#else
: :
#endif
: "a0", "a1", "a2", "a3", "memory"
);
: "a0", "a1", "a2", "a3", "memory");
// Setup the interrupt vector, processor status and INTSYSCR.
asm volatile(
@ -1126,8 +1234,7 @@ void handle_reset( void )
" la t0, InterruptVector\n\
ori t0, t0, 3\n\
csrw mtvec, t0\n"
: : [InterruptVector]"r"(InterruptVector) : "t0", "memory"
);
: : [InterruptVector] "r"(InterruptVector) : "t0", "memory");
#if defined(FUNCONF_SYSTICK_USE_HCLK) && FUNCONF_SYSTICK_USE_HCLK && !defined(CH32V10x)
SysTick->CTLR = 5;
@ -1194,8 +1301,7 @@ __attribute__ ((naked)) int setjmp( jmp_buf env )
#endif
" li a0, 0\n"
" ret\n"
);
" ret\n");
}
__attribute__((naked)) void longjmp(jmp_buf env, int val)
@ -1237,8 +1343,7 @@ __attribute__ ((naked)) void longjmp( jmp_buf env, int val )
" seqz a0, a1\n" // a0 = (a1 == 0) ? 1 : 0
" add a0, a0, a1\n"
" ret\n"
);
" ret\n");
__builtin_unreachable(); // Disable warning about no return.
}
@ -1278,8 +1383,10 @@ void SetupUART( int uartBRR )
// For debug writing to the UART.
WEAK int _write(int fd, const char *buf, int size)
{
for(int i = 0; i < size; i++){
while( !(USART1->STATR & USART_FLAG_TC));
for (int i = 0; i < size; i++)
{
while (!(USART1->STATR & USART_FLAG_TC))
;
USART1->DATAR = *buf++;
}
return size;
@ -1288,7 +1395,8 @@ WEAK int _write(int fd, const char *buf, int size)
// single char to UART
WEAK int putchar(int c)
{
while( !(USART1->STATR & USART_FLAG_TC));
while (!(USART1->STATR & USART_FLAG_TC))
;
USART1->DATAR = (const char)c;
return 1;
}
@ -1296,9 +1404,12 @@ WEAK int putchar(int c)
#if defined(FUNCONF_USE_DEBUGPRINTF) && FUNCONF_USE_DEBUGPRINTF
void handle_debug_input(int numbytes, uint8_t *data) __attribute__((weak));
void handle_debug_input( int numbytes, uint8_t * data ) { (void)numbytes; (void)data; }
void handle_debug_input(int numbytes, uint8_t *data)
{
(void)numbytes;
(void)data;
}
static void internal_handle_input(volatile uint32_t *dmdata0)
{
@ -1310,7 +1421,6 @@ static void internal_handle_input( volatile uint32_t * dmdata0 )
}
}
void poll_input(void)
{
volatile uint32_t *dmdata0 = (volatile uint32_t *)DMDATA0;
@ -1321,7 +1431,6 @@ void poll_input( void )
}
}
// MSB .... LSB
// DMDATA0: char3 char2 char1 [status word]
// where [status word] is:
@ -1422,7 +1531,6 @@ void SetupDebugPrintf( void )
int WaitForDebuggerToAttach(int timeout_ms)
{
#if defined(CH32V20x) || defined(CH32V30x)
#define systickcnt_t uint64_t
#define SYSTICKCNT SysTick->CNT
@ -1439,7 +1547,8 @@ int WaitForDebuggerToAttach( int timeout_ms )
const systickcnt_t timeout = timeout_ms * ticks_per_ms;
// Wait for the sentinel to become zero.
while( !DidDebuggerAttach() ) {
while (!DidDebuggerAttach())
{
if (timeout_ms && (SYSTICKCNT - start) > timeout) return 1;
}
@ -1471,13 +1580,16 @@ void DelaySysTick( uint32_t n )
{
#ifdef CH32V003
uint32_t targend = SysTick->CNT + n;
while( ((int32_t)( SysTick->CNT - targend )) < 0 );
while (((int32_t)(SysTick->CNT - targend)) < 0)
;
#elif defined(CH32V20x) || defined(CH32V30x)
uint64_t targend = SysTick->CNT + n;
while( ((int64_t)( SysTick->CNT - targend )) < 0 );
while (((int64_t)(SysTick->CNT - targend)) < 0)
;
#elif defined(CH32V10x) || defined(CH32X03x)
uint32_t targend = SysTick->CNTL + n;
while( ((int32_t)( SysTick->CNTL - targend )) < 0 );
while (((int32_t)(SysTick->CNTL - targend)) < 0)
;
#else
#error DelaySysTick not defined.
#endif
@ -1603,10 +1715,12 @@ void SystemInit( void )
RCC->INTR = 0x009F0000; // Clear PLL, CSSC, HSE, HSI and LSI ready flags.
#if defined(FUNCONF_USE_PLL) && FUNCONF_USE_PLL
while((RCC->CTLR & RCC_PLLRDY) == 0); // Wait till PLL is ready
while ((RCC->CTLR & RCC_PLLRDY) == 0)
; // Wait till PLL is ready
uint32_t tmp32 = RCC->CFGR0 & ~(0x03); // clr the SW
RCC->CFGR0 = tmp32 | RCC_SW_PLL; // Select PLL as system clock source
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08); // Wait till PLL is used as system clock source
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
; // Wait till PLL is used as system clock source
#endif
#if defined(FUNCONF_USE_UARTPRINTF) && FUNCONF_USE_UARTPRINTF
@ -1634,12 +1748,13 @@ void funAnalogInit( void )
// Reset calibration
ADC1->CTLR2 |= ADC_RSTCAL;
while(ADC1->CTLR2 & ADC_RSTCAL);
while (ADC1->CTLR2 & ADC_RSTCAL)
;
// Calibrate
ADC1->CTLR2 |= ADC_CAL;
while(ADC1->CTLR2 & ADC_CAL);
while (ADC1->CTLR2 & ADC_CAL)
;
}
int funAnalogRead(int nAnalogNumber)
@ -1650,7 +1765,8 @@ int funAnalogRead( int nAnalogNumber )
ADC1->CTLR2 |= ADC_SWSTART;
// wait for conversion complete
while(!(ADC1->STATR & ADC_EOC));
while (!(ADC1->STATR & ADC_EOC))
;
// get result
return ADC1->RDATAR;
@ -1660,7 +1776,11 @@ int funAnalogRead( int nAnalogNumber )
#ifdef CPLUSPLUS
// This is required to allow pure virtual functions to be defined.
extern void __cxa_pure_virtual() { while (1); }
extern void __cxa_pure_virtual()
{
while (1)
;
}
// These magic symbols are provided by the linker.
extern void (*__preinit_array_start[])(void) __attribute__((weak));

155
src/ch32fun.h Executable file → Normal file
View file

@ -59,8 +59,6 @@
by setting FUNCONF_DEBUG_HARDFAULT to 0.
*/
/******************************************************************************
* CH32V003 Fun Configs; please define any non-default options in funconfig.h *
@ -142,7 +140,6 @@
#define INTERRUPT_DECORATOR __attribute__((interrupt))
#endif
#if !defined(FUNCONF_USE_CLK_SEC)
#define FUNCONF_USE_CLK_SEC 1 // use clock security system by default
#endif
@ -278,9 +275,9 @@
#endif
#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif
#ifndef __ASSEMBLER__
@ -327,11 +324,23 @@ typedef uint64_t u64;
typedef __IO int64_t vs64;
typedef int64_t s64;
typedef enum {NoREADY = 0, READY = !NoREADY} ErrorStatus;
typedef enum
{
NoREADY = 0,
READY = !NoREADY
} ErrorStatus;
typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;
typedef enum
{
DISABLE = 0,
ENABLE = !DISABLE
} FunctionalState;
typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus;
typedef enum
{
RESET = 0,
SET = !RESET
} FlagStatus, ITStatus;
#define RV_STATIC_INLINE static inline
#endif // __ASSEMBLER__
@ -363,28 +372,37 @@ typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus;
// Enable Global Interrupt
RV_STATIC_INLINE void __enable_irq()
{
uint32_t result; __ASM volatile( ADD_ARCH_ZICSR "csrr %0," "mstatus": "=r"(result) );
result |= 0x88; __ASM volatile( ADD_ARCH_ZICSR "csrw mstatus, %0" : : "r" (result) );
uint32_t result;
__ASM volatile(ADD_ARCH_ZICSR "csrr %0,"
"mstatus" : "=r"(result));
result |= 0x88;
__ASM volatile(ADD_ARCH_ZICSR "csrw mstatus, %0" : : "r"(result));
}
// Disable Global Interrupt
RV_STATIC_INLINE void __disable_irq()
{
uint32_t result; __ASM volatile( ADD_ARCH_ZICSR "csrr %0," "mstatus": "=r"(result) );
result &= ~0x88; __ASM volatile( ADD_ARCH_ZICSR "csrw mstatus, %0" : : "r" (result) );
uint32_t result;
__ASM volatile(ADD_ARCH_ZICSR "csrr %0,"
"mstatus" : "=r"(result));
result &= ~0x88;
__ASM volatile(ADD_ARCH_ZICSR "csrw mstatus, %0" : : "r"(result));
}
// Is Global Interrupt enabled (1 = yes, 0 = no)
RV_STATIC_INLINE uint8_t __isenabled_irq(void)
{
uint32_t result; __ASM volatile( ADD_ARCH_ZICSR "csrr %0," "mstatus": "=r"(result) );
uint32_t result;
__ASM volatile(ADD_ARCH_ZICSR "csrr %0,"
"mstatus" : "=r"(result));
return (result & 0x08) != 0u;
}
// Get stack pointer (returns the stack pointer)
RV_STATIC_INLINE uint32_t __get_cpu_sp(void)
{
uint32_t result; __ASM volatile( ADD_ARCH_ZICSR "mv %0, sp" : "=r"(result));
uint32_t result;
__ASM volatile(ADD_ARCH_ZICSR "mv %0, sp" : "=r"(result));
return result;
}
@ -466,11 +484,13 @@ RV_STATIC_INLINE uint32_t NVIC_get_enabled_IRQs()
RV_STATIC_INLINE void NVIC_clear_all_IRQs_except(uint8_t IRQn_to_keep)
{
if (!(IRQn_to_keep >> 5)) { // IRQn_to_keep < 32
if (!(IRQn_to_keep >> 5))
{ // IRQn_to_keep < 32
NVIC->IRER[0] = (~0) & (~(1 << IRQn_to_keep));
NVIC->IRER[1] = (~0);
}
else {
else
{
IRQn_to_keep = IRQn_to_keep >> 5;
NVIC->IRER[0] = (~0);
NVIC->IRER[1] = (~0) & (~(1 << IRQn_to_keep));
@ -512,7 +532,8 @@ __attribute__( ( always_inline ) ) RV_STATIC_INLINE void __WFE(void)
*
* @return none
*/
RV_STATIC_INLINE void SetVTFIRQ(uint32_t addr, IRQn_Type IRQn, uint8_t num, FunctionalState NewState){
RV_STATIC_INLINE void SetVTFIRQ(uint32_t addr, IRQn_Type IRQn, uint8_t num, FunctionalState NewState)
{
if (num > 1) return;
if (NewState != DISABLE)
@ -520,7 +541,8 @@ RV_STATIC_INLINE void SetVTFIRQ(uint32_t addr, IRQn_Type IRQn, uint8_t num, Func
NVIC->VTFIDR[num] = IRQn;
NVIC->VTFADDR[num] = ((addr & 0xFFFFFFFE) | 0x1);
}
else{
else
{
NVIC->VTFIDR[num] = IRQn;
NVIC->VTFADDR[num] = ((addr & 0xFFFFFFFE) & (~0x1));
}
@ -552,7 +574,8 @@ static inline void __set_INTSYSCR( uint32_t value )
static inline uint32_t __get_FFLAGS(void)
{
uint32_t result;
__ASM volatile( ADD_ARCH_ZICSR "csrr %0," "fflags" : "=r" (result) );
__ASM volatile(ADD_ARCH_ZICSR "csrr %0,"
"fflags" : "=r"(result));
return (result);
}
@ -566,7 +589,8 @@ static inline void __set_FFLAGS(uint32_t value)
static inline uint32_t __get_FRM(void)
{
uint32_t result;
__ASM volatile( ADD_ARCH_ZICSR "csrr %0," "frm" : "=r" (result) );
__ASM volatile(ADD_ARCH_ZICSR "csrr %0,"
"frm" : "=r"(result));
return (result);
}
@ -580,7 +604,8 @@ static inline void __set_FRM(uint32_t value)
static inline uint32_t __get_FCSR(void)
{
uint32_t result;
__ASM volatile( ADD_ARCH_ZICSR "csrr %0," "fcsr" : "=r" (result) );
__ASM volatile(ADD_ARCH_ZICSR "csrr %0,"
"fcsr" : "=r"(result));
return (result);
}
@ -624,7 +649,8 @@ static inline void __set_MISA(uint32_t value)
static inline uint32_t __get_MTVEC(void)
{
uint32_t result;
__ASM volatile( ADD_ARCH_ZICSR "csrr %0," "mtvec": "=r"(result));
__ASM volatile(ADD_ARCH_ZICSR "csrr %0,"
"mtvec" : "=r"(result));
return (result);
}
@ -638,7 +664,8 @@ static inline void __set_MTVEC(uint32_t value)
static inline uint32_t __get_MSCRATCH(void)
{
uint32_t result;
__ASM volatile( ADD_ARCH_ZICSR "csrr %0," "mscratch" : "=r"(result));
__ASM volatile(ADD_ARCH_ZICSR "csrr %0,"
"mscratch" : "=r"(result));
return (result);
}
@ -652,7 +679,8 @@ static inline void __set_MSCRATCH(uint32_t value)
static inline uint32_t __get_MEPC(void)
{
uint32_t result;
__ASM volatile( ADD_ARCH_ZICSR "csrr %0," "mepc" : "=r"(result));
__ASM volatile(ADD_ARCH_ZICSR "csrr %0,"
"mepc" : "=r"(result));
return (result);
}
@ -666,7 +694,8 @@ static inline void __set_MEPC(uint32_t value)
static inline uint32_t __get_MCAUSE(void)
{
uint32_t result;
__ASM volatile( ADD_ARCH_ZICSR "csrr %0," "mcause": "=r"(result));
__ASM volatile(ADD_ARCH_ZICSR "csrr %0,"
"mcause" : "=r"(result));
return (result);
}
@ -680,7 +709,8 @@ static inline void __set_MCAUSE(uint32_t value)
static inline uint32_t __get_MTVAL(void)
{
uint32_t result;
__ASM volatile( ADD_ARCH_ZICSR "csrr %0," "mtval" : "=r" (result) );
__ASM volatile(ADD_ARCH_ZICSR "csrr %0,"
"mtval" : "=r"(result));
return (result);
}
@ -732,7 +762,6 @@ static inline uint32_t __get_DEBUG_CR(void)
return (result);
}
// Set the DBGMCU_CR Register value
static inline void __set_DEBUG_CR(uint32_t value)
{
@ -743,7 +772,8 @@ static inline void __set_DEBUG_CR(uint32_t value)
static inline uint32_t __get_SP(void)
{
uint32_t result;
__ASM volatile( "mv %0,""sp": "=r"(result):);
__ASM volatile("mv %0,"
"sp" : "=r"(result) :);
return (result);
}
#endif // CH32V003
@ -784,13 +814,13 @@ void longjmp( jmp_buf env, int val );
#endif /* __CORE_RISCV_H__ */
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif
/* SYSTICK info
@ -828,26 +858,58 @@ extern "C" {
// For pins, use things like PA8, PB15
// For configuration, use things like GPIO_CFGLR_OUT_10Mhz_PP
#define funDigitalWrite( pin, value ) { GpioOf( pin )->BSHR = 1<<((!(value))*16 + ((pin) & 0xf)); }
#define funDigitalWrite(pin, value) \
{ \
GpioOf(pin)->BSHR = 1 << ((!(value)) * 16 + ((pin) & 0xf)); \
}
#if defined(CH32X03x)
#define funGpioInitAll() { RCC->APB2PCENR |= ( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC ); }
#define funPinMode( pin, mode ) { *((&GpioOf(pin)->CFGLR)+((pin&0x8)>>3)) = ( (*((&GpioOf(pin)->CFGLR)+((pin&0x8)>>3))) & (~(0xf<<(4*((pin)&0x7))))) | ((mode)<<(4*((pin)&0x7))); }
#define funGpioInitAll() \
{ \
RCC->APB2PCENR |= (RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC); \
}
#define funPinMode(pin, mode) \
{ \
*((&GpioOf(pin)->CFGLR) + ((pin & 0x8) >> 3)) = ((*((&GpioOf(pin)->CFGLR) + ((pin & 0x8) >> 3))) & (~(0xf << (4 * ((pin) & 0x7))))) | ((mode) << (4 * ((pin) & 0x7))); \
}
#elif defined(CH32V10x) || defined(CH32V20x) || defined(CH32V30x)
#define funGpioInitAll() { RCC->APB2PCENR |= ( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD ); }
#define funPinMode( pin, mode ) { *((&GpioOf(pin)->CFGLR)+((pin&0x8)>>3)) = ( (*((&GpioOf(pin)->CFGLR)+((pin&0x8)>>3))) & (~(0xf<<(4*((pin)&0x7))))) | ((mode)<<(4*((pin)&0x7))); }
#define funGpioInitB() { RCC->APB2PCENR |= ( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB ); }
#define funGpioInitAll() \
{ \
RCC->APB2PCENR |= (RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD); \
}
#define funPinMode(pin, mode) \
{ \
*((&GpioOf(pin)->CFGLR) + ((pin & 0x8) >> 3)) = ((*((&GpioOf(pin)->CFGLR) + ((pin & 0x8) >> 3))) & (~(0xf << (4 * ((pin) & 0x7))))) | ((mode) << (4 * ((pin) & 0x7))); \
}
#define funGpioInitB() \
{ \
RCC->APB2PCENR |= (RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB); \
}
#else
#define funGpioInitAll() { RCC->APB2PCENR |= ( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD ); }
#define funPinMode( pin, mode ) { GpioOf(pin)->CFGLR = (GpioOf(pin)->CFGLR & (~(0xf<<(4*((pin)&0xf))))) | ((mode)<<(4*((pin)&0xf))); }
#define funGpioInitAll() \
{ \
RCC->APB2PCENR |= (RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD); \
}
#define funPinMode(pin, mode) \
{ \
GpioOf(pin)->CFGLR = (GpioOf(pin)->CFGLR & (~(0xf << (4 * ((pin) & 0xf))))) | ((mode) << (4 * ((pin) & 0xf))); \
}
#endif
#define funGpioInitA() { RCC->APB2PCENR |= ( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA ); }
#define funGpioInitC() { RCC->APB2PCENR |= ( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOC ); }
#define funGpioInitD() { RCC->APB2PCENR |= ( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD ); }
#define funGpioInitA() \
{ \
RCC->APB2PCENR |= (RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA); \
}
#define funGpioInitC() \
{ \
RCC->APB2PCENR |= (RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOC); \
}
#define funGpioInitD() \
{ \
RCC->APB2PCENR |= (RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD); \
}
#define funDigitalRead(pin) ((int)((GpioOf(pin)->INDR >> ((pin) & 0xf)) & 1))
#define ANALOG_0 0
#define ANALOG_1 1
#define ANALOG_2 2
@ -865,7 +927,6 @@ extern "C" {
#if defined(__riscv) || defined(__riscv__) || defined(CH32V003FUN_BASE)
// Stuff that can only be compiled on device (not for the programmer, or other host programs)
// Initialize the ADC calibrate it and set some sane defaults.
@ -884,13 +945,13 @@ void DefaultIRQHandler( void ) __attribute__((section(".text.vector_handler")))
void DelaySysTick(uint32_t n);
// Depending on a LOT of factors, it's about 6 cycles per n.
// **DO NOT send it zero or less.**
#ifndef __MACOSX__
#ifndef __DELAY_TINY_DEFINED__
#define __DELAY_TINY_DEFINED__
static inline void Delay_Tiny( int n ) {
static inline void Delay_Tiny(int n)
{
__ASM volatile("\
mv a5, %[n]\n\
1: \
@ -938,7 +999,6 @@ void poll_input( void );
// Receiving bytes from host. Override if you wish.
void handle_debug_input(int numbytes, uint8_t *data);
// Functions from ch32fun.c
#include <stdarg.h>
@ -949,7 +1009,6 @@ int mini_pprintf(int (*puts)(char*s, int len, void* buf), void* buf, const char
#endif // __ASSEMBLER__
/*
* This file contains various parts of the official WCH EVT Headers which
* were originally under a restrictive license.
@ -990,6 +1049,4 @@ int mini_pprintf(int (*puts)(char*s, int len, void* buf), void* buf, const char
};
#endif
#endif // __CH32FUN_H

View file

@ -4,4 +4,3 @@
#define CH32V003 1
#endif