diff --git a/src/ch32fun.c b/src/ch32fun.c old mode 100755 new mode 100644 index 216b775..1469639 --- a/src/ch32fun.c +++ b/src/ch32fun.c @@ -32,7 +32,7 @@ void *memmove(void *dest, const void *src, size_t n) void *memchr(const void *src, int c, size_t n) int puts(const char *s) int mini_itoa(long value, unsigned int radix, int uppercase, int unsig, - char *buffer) + char *buffer) int mini_vsnprintf(char *buffer, unsigned int buffer_len, 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) int mini_snprintf(char* buffer, unsigned int buffer_len, const char *fmt, ...) @@ -85,55 +85,55 @@ void __libc_init_array(void) #define _SSP_STRING_H_ #define _SSP_STDIO_H_ +#include "ch32fun.h" +#include +#include +#include #include #include -#include -#include -#include -#include "ch32fun.h" #define WEAK __attribute__((weak)) -WEAK int errno; +WEAK int errno; -static int __puts_uart( char *s, int len, void *buf ) +static int __puts_uart(char *s, int len, void *buf) { - (void)buf; + (void)buf; - _write( 0, s, len ); - return len; + _write(0, s, len); + return len; } -WEAK int printf( const char* format, ... ) +WEAK int printf(const char *format, ...) { - va_list args; - va_start( args, format ); - int ret_status = mini_vpprintf(__puts_uart, 0, format, args); - va_end( args ); - return ret_status; + va_list args; + va_start(args, format); + int ret_status = mini_vpprintf(__puts_uart, 0, format, args); + va_end(args); + return ret_status; } -WEAK int vprintf(const char* format, va_list args) +WEAK int vprintf(const char *format, va_list args) { - return mini_vpprintf(__puts_uart, 0, format, args); + return mini_vpprintf(__puts_uart, 0, format, args); } -WEAK int snprintf( char * buffer, unsigned int buffer_len, const char* format, ... ) +WEAK int snprintf(char *buffer, unsigned int buffer_len, const char *format, ...) { - va_list args; - va_start( args, format ); - int ret = mini_vsnprintf( buffer, buffer_len, format, args ); - va_end( args ); - return ret; + va_list args; + va_start(args, format); + int ret = mini_vsnprintf(buffer, buffer_len, format, args); + va_end(args); + return ret; } -WEAK int sprintf( char * buffer, const char * format, ... ) +WEAK int sprintf(char *buffer, const char *format, ...) { - va_list args; - va_start( args, format ); - int ret = mini_vsnprintf( buffer, INT_MAX, format, args ); - va_end( args ); - return ret; + va_list args; + va_start(args, format); + int ret = mini_vsnprintf(buffer, INT_MAX, format, args); + va_end(args); + return ret; } /* Some stuff from MUSL @@ -173,307 +173,382 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #define IS_CODEUNIT(c) ((unsigned)(c)-0xdf80 < 0x80) #define MB_CUR_MAX (CURRENT_UTF8 ? 4 : 1) -typedef void * mbstate_t; +typedef void *mbstate_t; #ifdef UNICODE WEAK size_t wcrtomb(char *restrict s, wchar_t wc, mbstate_t *restrict st) { - if (!s) return 1; - if ((unsigned)wc < 0x80) { - *s = wc; - return 1; - } else if (MB_CUR_MAX == 1) { - if (!IS_CODEUNIT(wc)) { - errno = 0x02; // EILSEQ - return -1; - } - *s = wc; - return 1; - } else if ((unsigned)wc < 0x800) { - *s++ = 0xc0 | (wc>>6); - *s = 0x80 | (wc&0x3f); - return 2; - } 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) { - *s++ = 0xf0 | (wc>>18); - *s++ = 0x80 | ((wc>>12)&0x3f); - *s++ = 0x80 | ((wc>>6)&0x3f); - *s = 0x80 | (wc&0x3f); - return 4; - } - errno = 0x02;//EILSEQ; - return -1; + if (!s) return 1; + if ((unsigned)wc < 0x80) + { + *s = wc; + return 1; + } + else if (MB_CUR_MAX == 1) + { + if (!IS_CODEUNIT(wc)) + { + errno = 0x02; // EILSEQ + return -1; + } + *s = wc; + return 1; + } + else if ((unsigned)wc < 0x800) + { + *s++ = 0xc0 | (wc >> 6); + *s = 0x80 | (wc & 0x3f); + return 2; + } + 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) + { + *s++ = 0xf0 | (wc >> 18); + *s++ = 0x80 | ((wc >> 12) & 0x3f); + *s++ = 0x80 | ((wc >> 6) & 0x3f); + *s = 0x80 | (wc & 0x3f); + return 4; + } + errno = 0x02; // EILSEQ; + return -1; } WEAK int wctomb(char *s, wchar_t wc) { - if (!s) return 0; - return wcrtomb(s, wc, 0); + if (!s) return 0; + return wcrtomb(s, wc, 0); } #endif WEAK size_t strlen(const char *s) { - const char *a = s; - for (; *s; s++); - return s-a; + const char *a = 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++); - return d0; + char *d0 = 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++); - return d0; + char *d0 = 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++); - return *(unsigned char *)l - *(unsigned char *)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--); - return *l - *r; + const unsigned char *l = (void *)_l, *r = (void *)_r; + if (!n--) return 0; + 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); - return *h ? (char *)h-1 : 0; + uint16_t nw = n[0] << 8 | n[1], hw = h[0] << 8 | h[1]; + for (h++; *h && hw != nw; hw = hw << 8 | *++h) + ; + return *h ? (char *)h - 1 : 0; } 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); - return *h ? (char *)h-2 : 0; + 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) + ; + return *h ? (char *)h - 2 : 0; } 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); - return *h ? (char *)h-3 : 0; + 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) + ; + return *h ? (char *)h - 3 : 0; } -#define MAX(a,b) ((a)>(b)?(a):(b)) -#define MIN(a,b) ((a)<(b)?(a):(b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) -#define BITOP(a,b,op) \ - ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a)))) +#define BITOP(a, b, op) \ + ((a)[(size_t)(b) / (8 * sizeof *(a))] op(size_t) 1 << ((size_t)(b) % (8 * sizeof *(a)))) static char *twoway_strstr(const unsigned char *h, const unsigned char *n) { - const unsigned char *z; - size_t l, ip, jp, k, p, ms, p0, mem, mem0; - size_t byteset[32 / sizeof(size_t)] = { 0 }; - size_t shift[256]; + const unsigned char *z; + size_t l, ip, jp, k, p, ms, p0, mem, mem0; + size_t byteset[32 / sizeof(size_t)] = {0}; + size_t shift[256]; - /* Computing length of needle and fill shift table */ - for (l=0; n[l] && h[l]; l++) - BITOP(byteset, n[l], |=), shift[n[l]] = l+1; - if (n[l]) return 0; /* hit the end of h */ + /* Computing length of needle and fill shift table */ + for (l = 0; n[l] && h[l]; l++) + BITOP(byteset, n[l], |=), shift[n[l]] = l + 1; + if (n[l]) return 0; /* hit the end of h */ - /* Compute maximal suffix */ - ip = -1; jp = 0; k = p = 1; - while (jp+k n[jp+k]) { - jp += k; - k = 1; - p = jp - ip; - } else { - ip = jp++; - k = p = 1; - } - } - ms = ip; - p0 = p; + /* Compute maximal suffix */ + 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]) + { + jp += k; + k = 1; + p = jp - ip; + } + else + { + ip = jp++; + k = p = 1; + } + } + ms = ip; + p0 = p; - /* And with the opposite comparison */ - ip = -1; jp = 0; k = p = 1; - while (jp+k ms+1) ms = ip; - else p = p0; + /* 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) + { + jp += p; + k = 1; + } + else + k++; + } + else if (n[ip + k] < n[jp + k]) + { + jp += k; + k = 1; + p = jp - ip; + } + else + { + ip = jp++; + k = p = 1; + } + } + if (ip + 1 > ms + 1) + ms = ip; + else + p = p0; - /* Periodic needle? */ - if (memcmp(n, n+p, ms+1)) { - mem0 = 0; - p = MAX(ms, l-ms-1) + 1; - } else mem0 = l-p; - mem = 0; + /* Periodic needle? */ + if (memcmp(n, n + p, ms + 1)) + { + mem0 = 0; + p = MAX(ms, l - ms - 1) + 1; + } + else + mem0 = l - p; + mem = 0; - /* Initialize incremental end-of-haystack pointer */ - z = h; + /* Initialize incremental end-of-haystack pointer */ + z = h; - /* Search loop */ - for (;;) { - /* Update incremental end-of-haystack pointer */ - 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) { - z = z2; - if ((size_t)(z-h) < l) return 0; - } else z += grow; - } + /* Search loop */ + for (;;) + { + /* Update incremental end-of-haystack pointer */ + 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) + { + z = z2; + if ((size_t)(z - h) < l) return 0; + } + else + z += grow; + } - /* Check last byte first; advance by shift on mismatch */ - if (BITOP(byteset, h[l-1], &)) { - k = l-shift[h[l-1]]; - if (k) { - if (k < mem) k = mem; - h += k; - mem = 0; - continue; - } - } else { - h += l; - mem = 0; - continue; - } + /* Check last byte first; advance by shift on mismatch */ + if (BITOP(byteset, h[l - 1], &)) + { + k = l - shift[h[l - 1]]; + if (k) + { + if (k < mem) k = mem; + h += k; + mem = 0; + continue; + } + } + 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]) { - h += k-ms; - mem = 0; - continue; - } - /* Compare left half */ - for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--); - if (k <= mem) return (char *)h; - h += p; - mem = mem0; - } + /* Compare right half */ + 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--) + ; + if (k <= mem) return (char *)h; + h += p; + mem = mem0; + } } 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++); - return (char *)s; + c = (unsigned char)c; + if (!c) return (char *)s + strlen(s); + for (; *s && *(unsigned char *)s != c; s++) + ; + return (char *)s; } WEAK char *strstr(const char *h, const char *n) { - /* Return immediately on empty needle */ - if (!n[0]) return (char *)h; + /* Return immediately on empty needle */ + if (!n[0]) return (char *)h; - /* Use faster algorithms for short needles */ - h = strchr(h, *n); - if (!h || !n[1]) return (char *)h; - if (!h[1]) return 0; - if (!n[2]) return twobyte_strstr((void *)h, (void *)n); - if (!h[2]) return 0; - if (!n[3]) return threebyte_strstr((void *)h, (void *)n); - if (!h[3]) return 0; - if (!n[4]) return fourbyte_strstr((void *)h, (void *)n); + /* Use faster algorithms for short needles */ + h = strchr(h, *n); + if (!h || !n[1]) return (char *)h; + if (!h[1]) return 0; + if (!n[2]) return twobyte_strstr((void *)h, (void *)n); + if (!h[2]) return 0; + if (!n[3]) return threebyte_strstr((void *)h, (void *)n); + if (!h[3]) return 0; + if (!n[4]) return fourbyte_strstr((void *)h, (void *)n); - return twoway_strstr((void *)h, (void *)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); - return 0; + const unsigned char *s = m; + c = (unsigned char)c; + while (n--) + if (s[n] == c) return (void *)(s + n); + return 0; } WEAK char *strrchr(const char *s, int c) { - return __memrchr(s, c, strlen(s) + 1); + return __memrchr(s, c, strlen(s) + 1); } 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++; - return dest; + unsigned char *d = dest; + const unsigned char *s = src; + 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++); - return n ? *l-*r : 0; + const unsigned char *l = vl, *r = vr; + 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; - const char *s = src; + char *d = dest; + const char *s = src; - if (d==s) return d; - if ((uintptr_t)s-(uintptr_t)d-n <= -2*n) return memcpy(d, s, n); + if (d == s) return d; + if ((uintptr_t)s - (uintptr_t)d - n <= -2 * n) return memcpy(d, s, n); - if (d - * + * * Permission granted on 2024-07-13 for optional relicense under MIT license. * https://github.com/mludvig/mini-printf/issues/16 * @@ -496,267 +571,298 @@ WEAK int puts(const char *s) static int mini_itoa(long value, unsigned int radix, int uppercase, int unsig, - char *buffer) + char *buffer) { - char *pbuffer = buffer; - int negative = 0; - int i, len; + char *pbuffer = buffer; + int negative = 0; + int i, len; - /* No support for unusual radixes. */ - if (radix > 16) - return 0; + /* No support for unusual radixes. */ + if (radix > 16) + return 0; - if (value < 0 && !unsig) { - negative = 1; - value = -value; - } + if (value < 0 && !unsig) + { + negative = 1; + value = -value; + } - /* This builds the string back to front ... */ - do { - int digit = value % radix; - *(pbuffer++) = (digit < 10 ? '0' + digit : (uppercase ? 'A' : 'a') + digit - 10); - value /= radix; - } while (value > 0); + /* This builds the string back to front ... */ + do + { + int digit = value % radix; + *(pbuffer++) = (digit < 10 ? '0' + digit : (uppercase ? 'A' : 'a') + digit - 10); + value /= radix; + } while (value > 0); - if (negative) - *(pbuffer++) = '-'; + if (negative) + *(pbuffer++) = '-'; - *(pbuffer) = '\0'; + *(pbuffer) = '\0'; - /* ... now we reverse it (could do it recursively but will - * conserve the stack space) */ - len = (pbuffer - buffer); - for (i = 0; i < len / 2; i++) { - char j = buffer[i]; - buffer[i] = buffer[len-i-1]; - buffer[len-i-1] = j; - } + /* ... now we reverse it (could do it recursively but will + * conserve the stack space) */ + len = (pbuffer - buffer); + for (i = 0; i < len / 2; i++) + { + char j = buffer[i]; + buffer[i] = buffer[len - i - 1]; + buffer[len - i - 1] = j; + } - return len; + return len; } static int -mini_pad(char* ptr, int len, char pad_char, int pad_to, char *buffer) +mini_pad(char *ptr, int len, char pad_char, int pad_to, char *buffer) { - int i; - int overflow = 0; - char * pbuffer = buffer; - if(pad_to == 0) pad_to = len; - if(len > pad_to) { - len = pad_to; - overflow = 1; - } - for(i = pad_to - len; i > 0; i --) { - *(pbuffer++) = pad_char; - } - for(i = len; i > 0; i --) { - *(pbuffer++) = *(ptr++); - } - len = pbuffer - buffer; - if(overflow) { - for (i = 0; i < 3 && pbuffer > buffer; i ++) { - *(pbuffer-- - 1) = '*'; - } - } - return len; + int i; + int overflow = 0; + char *pbuffer = buffer; + if (pad_to == 0) pad_to = len; + if (len > pad_to) + { + len = pad_to; + overflow = 1; + } + for (i = pad_to - len; i > 0; i--) + { + *(pbuffer++) = pad_char; + } + for (i = len; i > 0; i--) + { + *(pbuffer++) = *(ptr++); + } + len = pbuffer - buffer; + if (overflow) + { + for (i = 0; i < 3 && pbuffer > buffer; i++) + { + *(pbuffer-- - 1) = '*'; + } + } + return len; } -struct mini_buff { - char *buffer, *pbuffer; - unsigned int buffer_len; +struct mini_buff +{ + char *buffer, *pbuffer; + unsigned int buffer_len; }; static int _puts(char *s, int len, void *buf) { - if(!buf) return len; - struct mini_buff *b = 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) { - break; - } - *(b->pbuffer ++) = s[i]; - } - *(b->pbuffer) = 0; - return b->pbuffer - p0; + if (!buf) return len; + struct mini_buff *b = 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) + { + break; + } + *(b->pbuffer++) = s[i]; + } + *(b->pbuffer) = 0; + return b->pbuffer - p0; } #ifdef MINI_PRINTF_ENABLE_OBJECTS -static int (*mini_handler) (void* data, void* obj, int ch, int lhint, char** bf) = 0; -static void (*mini_handler_freeor)(void* data, void*) = 0; -static void * mini_handler_data = 0; +static int (*mini_handler)(void *data, void *obj, int ch, int lhint, char **bf) = 0; +static void (*mini_handler_freeor)(void *data, void *) = 0; +static void *mini_handler_data = 0; -void mini_printf_set_handler( - void* data, - int (*handler)(void* data, void* obj, int ch, int len_hint, char** buf), - void (*freeor)(void* data, void* buf)) +void mini_printf_set_handler( + void *data, + int (*handler)(void *data, void *obj, int ch, int len_hint, char **buf), + void (*freeor)(void *data, void *buf)) { - mini_handler = handler; - mini_handler_freeor = freeor; - mini_handler_data = data; + mini_handler = handler; + mini_handler_freeor = freeor; + mini_handler_data = data; } #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; - b.pbuffer = buffer; - 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) { - return n; - } - return b.pbuffer - b.buffer; + struct mini_buff b; + b.buffer = buffer; + b.pbuffer = buffer; + 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) + { + 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]; - char ch; + char bf[24]; + char bf2[24]; + char ch; #ifdef MINI_PRINTF_ENABLE_OBJECTS - void* obj; + void *obj; #endif - if(puts == (void*)0) { - /* run puts in counting mode. */ - puts = _puts; buf = (void*)0; - } - int n = 0; - while ((ch=*(fmt++))) { - int len; - if (ch!='%') { - len = 1; - len = puts(&ch, len, buf); - } else { - char pad_char = ' '; - int pad_to = 0; - char l = 0; - char *ptr; + if (puts == (void *)0) + { + /* run puts in counting mode. */ + puts = _puts; + buf = (void *)0; + } + int n = 0; + while ((ch = *(fmt++))) + { + int len; + if (ch != '%') + { + len = 1; + len = puts(&ch, len, buf); + } + else + { + char pad_char = ' '; + int pad_to = 0; + char l = 0; + char *ptr; - ch=*(fmt++); + ch = *(fmt++); - /* Zero padding requested */ - if (ch == '0') pad_char = '0'; - while (ch >= '0' && ch <= '9') { - pad_to = pad_to * 10 + (ch - '0'); - ch=*(fmt++); - } - if(pad_to > (signed int) sizeof(bf)) { - pad_to = sizeof(bf); - } - if (ch == 'l') { - l = 1; - ch=*(fmt++); - } + /* Zero padding requested */ + if (ch == '0') pad_char = '0'; + while (ch >= '0' && ch <= '9') + { + pad_to = pad_to * 10 + (ch - '0'); + ch = *(fmt++); + } + if (pad_to > (signed int)sizeof(bf)) + { + pad_to = sizeof(bf); + } + if (ch == 'l') + { + l = 1; + ch = *(fmt++); + } - switch (ch) { - case 0: - goto end; - case 'u': - case 'd': - if(l) { - len = mini_itoa(va_arg(va, unsigned long), 10, 0, (ch=='u'), bf2); - } else { - if(ch == 'u') { - len = mini_itoa((unsigned long) va_arg(va, unsigned int), 10, 0, 1, bf2); - } else { - len = mini_itoa((long) va_arg(va, int), 10, 0, 0, bf2); - } - } - len = mini_pad(bf2, len, pad_char, pad_to, bf); - len = puts(bf, len, buf); - break; + switch (ch) + { + case 0: + goto end; + case 'u': + case 'd': + if (l) + { + len = mini_itoa(va_arg(va, unsigned long), 10, 0, (ch == 'u'), bf2); + } + else + { + if (ch == 'u') + { + len = mini_itoa((unsigned long)va_arg(va, unsigned int), 10, 0, 1, bf2); + } + else + { + len = mini_itoa((long)va_arg(va, int), 10, 0, 0, bf2); + } + } + len = mini_pad(bf2, len, pad_char, pad_to, bf); + len = puts(bf, len, buf); + break; - case 'x': - case 'X': - if(l) { - len = mini_itoa(va_arg(va, unsigned long), 16, (ch=='X'), 1, bf2); - } 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); - len = puts(bf, len, buf); - break; + case 'x': + case 'X': + if (l) + { + len = mini_itoa(va_arg(va, unsigned long), 16, (ch == 'X'), 1, bf2); + } + 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); + len = puts(bf, len, buf); + break; - case 'c' : - ch = (char)(va_arg(va, int)); - len = mini_pad(&ch, 1, pad_char, pad_to, bf); - len = puts(bf, len, buf); - break; + case 'c': + ch = (char)(va_arg(va, int)); + len = mini_pad(&ch, 1, pad_char, pad_to, bf); + len = puts(bf, len, buf); + break; - case 's' : - ptr = va_arg(va, char*); - len = mini_strlen(ptr); - if (pad_to > 0) { - len = mini_pad(ptr, len, pad_char, pad_to, bf); - len = puts(bf, len, buf); - } else { - len = puts(ptr, len, buf); - } - break; + case 's': + ptr = va_arg(va, char *); + len = mini_strlen(ptr); + if (pad_to > 0) + { + len = mini_pad(ptr, len, pad_char, pad_to, bf); + len = puts(bf, len, buf); + } + else + { + len = puts(ptr, len, buf); + } + break; #ifdef MINI_PRINTF_ENABLE_OBJECTS - case 'O' : /* Object by content (e.g. str) */ - 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) { - len = mini_pad(ptr, len, pad_char, pad_to, bf); - len = puts(bf, len, buf); - } else { - len = puts(ptr, len, buf); - } - mini_handler_freeor(mini_handler_data, ptr); - break; + case 'O': /* Object by content (e.g. str) */ + 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) + { + len = mini_pad(ptr, len, pad_char, pad_to, bf); + len = puts(bf, len, buf); + } + else + { + len = puts(ptr, len, buf); + } + mini_handler_freeor(mini_handler_data, ptr); + break; #endif - default: - len = 1; - len = puts(&ch, len, buf); - break; - } - } - n = n + len; - } + default: + len = 1; + len = puts(&ch, len, buf); + break; + } + } + n = n + len; + } end: - return n; + 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; - va_start(va, fmt); - ret = mini_vsnprintf(buffer, buffer_len, fmt, va); - va_end(va); + int ret; + va_list va; + va_start(va, fmt); + ret = mini_vsnprintf(buffer, buffer_len, fmt, va); + va_end(va); - return ret; + 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; - va_start(va, fmt); - ret = mini_vpprintf(puts, buf, fmt, va); - va_end(va); + int ret; + va_list va; + va_start(va, fmt); + ret = mini_vpprintf(puts, buf, fmt, va); + va_end(va); - return ret; + return ret; } - /* - C version of CH32V003 Startup .s file from WCH - This file is public domain where possible or the following where not: - Copyright 2023 Charles Lohr, under the MIT-x11 or NewBSD licenses, you choose. + C version of CH32V003 Startup .s file from WCH + This file is public domain where possible or the following where not: + Copyright 2023 Charles Lohr, under the MIT-x11 or NewBSD licenses, you choose. */ #ifdef CPLUSPLUS @@ -764,70 +870,74 @@ mini_pprintf(int (*puts)(char*s, int len, void* buf), void* buf, const char *fmt void __libc_init_array(void); #endif -int main() __attribute__((used)); -void SystemInit( void ) __attribute__((used)); +int main() __attribute__((used)); +void SystemInit(void) __attribute__((used)); -extern uint32_t * _sbss; -extern uint32_t * _ebss; -extern uint32_t * _data_lma; -extern uint32_t * _data_vma; -extern uint32_t * _edata; +extern uint32_t *_sbss; +extern uint32_t *_ebss; +extern uint32_t *_data_lma; +extern uint32_t *_data_vma; +extern uint32_t *_edata; #if FUNCONF_DEBUG_HARDFAULT #if FUNCONF_USE_DEBUGPRINTF -static void PrintN( uint32_t n ) +static void PrintN(uint32_t n) { - while( (*DMDATA0) & 0x80 ); - // Write out character. - *DMDATA0 = 0x78302088; //" 0x" - int shift; - for( shift = 28; shift >= 0; shift -= 4 ) - { - while( (*DMDATA0) & 0x80 ); - int s = (n>>shift) & 0xf; - s += ( s < 10 ) ? '0' : ('a' - 10); - *DMDATA0 = 0x85 | (s<<8); //" 0x" - } + while ((*DMDATA0) & 0x80) + ; + // Write out character. + *DMDATA0 = 0x78302088; //" 0x" + int shift; + for (shift = 28; shift >= 0; shift -= 4) + { + while ((*DMDATA0) & 0x80) + ; + int s = (n >> shift) & 0xf; + s += (s < 10) ? '0' : ('a' - 10); + *DMDATA0 = 0x85 | (s << 8); //" 0x" + } } #elif FUNCONF_USE_UARTPRINTF -static void PrintN( uint32_t n ) +static void PrintN(uint32_t n) { - putchar( ' ' ); - putchar( '0' ); - putchar( 'x' ); - int shift; - for( shift = 28; shift >= 0; shift -= 4 ) - { - int s = (n>>shift) & 0xf; - s += ( s < 10 ) ? '0' : ('a' - 10); - putchar( s ); - } + putchar(' '); + putchar('0'); + putchar('x'); + int shift; + for (shift = 28; shift >= 0; shift -= 4) + { + int s = (n >> shift) & 0xf; + s += (s < 10) ? '0' : ('a' - 10); + putchar(s); + } } #endif #endif // If you don't override a specific handler, it will just spin forever. -void DefaultIRQHandler( void ) +void DefaultIRQHandler(void) { -#if FUNCONF_DEBUG_HARDFAULT && ( FUNCONF_USE_DEBUGPRINTF || FUNCONF_USE_UARTPRINTF ) - //This is kind of like a crash handler. - //printf( "DEAD MSTATUS:%08x MTVAL:%08x MCAUSE:%08x MEPC:%08x\n", (int)__get_MSTATUS(), (int)__get_MTVAL(), (int)__get_MCAUSE(), (int)__get_MEPC() ); - PrintN( __get_MEPC() ); // "addr2line -e debugprintfdemo.elf 0x000007e6" ---> debugprintfdemo.c:45 - PrintN( __get_MSTATUS() ); - PrintN( __get_MTVAL() ); - PrintN( __get_MCAUSE() ); +#if FUNCONF_DEBUG_HARDFAULT && (FUNCONF_USE_DEBUGPRINTF || FUNCONF_USE_UARTPRINTF) + // This is kind of like a crash handler. + // printf( "DEAD MSTATUS:%08x MTVAL:%08x MCAUSE:%08x MEPC:%08x\n", (int)__get_MSTATUS(), (int)__get_MTVAL(), (int)__get_MCAUSE(), (int)__get_MEPC() ); + PrintN(__get_MEPC()); // "addr2line -e debugprintfdemo.elf 0x000007e6" ---> debugprintfdemo.c:45 + PrintN(__get_MSTATUS()); + PrintN(__get_MTVAL()); + PrintN(__get_MCAUSE()); #if FUNCONF_USE_DEBUGPRINTF - while( (*DMDATA0) & 0x80 ); - *DMDATA0 = 0x0a85; - while( (*DMDATA0) & 0x80 ); - *DMDATA0 = 0xaaaaaa83; + while ((*DMDATA0) & 0x80) + ; + *DMDATA0 = 0x0a85; + while ((*DMDATA0) & 0x80) + ; + *DMDATA0 = 0xaaaaaa83; #elif FUNCONF_USE_UARTPRINTF - putchar( '\n' ); + putchar('\n'); #endif #endif - //printf( "DEAD MSTATUS:%08x MTVAL:%08x MCAUSE:%08x MEPC:%08x\n", (int)__get_MSTATUS(), (int)__get_MTVAL(), (int)__get_MCAUSE(), (int)__get_MEPC() ); - // Infinite Loop - asm volatile( "1: j 1b" ); + // printf( "DEAD MSTATUS:%08x MTVAL:%08x MCAUSE:%08x MEPC:%08x\n", (int)__get_MSTATUS(), (int)__get_MTVAL(), (int)__get_MCAUSE(), (int)__get_MEPC() ); + // Infinite Loop + asm volatile("1: j 1b"); } // This makes it so that all of the interrupt handlers just alias to @@ -841,185 +951,184 @@ void DefaultIRQHandler( void ) * The sys clock is switched to HSI. * Clears the CSSF flag in RCC->INTR */ -void NMI_RCC_CSS_IRQHandler( void ) +void NMI_RCC_CSS_IRQHandler(void) { - RCC->INTR |= RCC_CSSC; // clear the clock security int flag + RCC->INTR |= RCC_CSSC; // clear the clock security int flag } -void NMI_Handler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("NMI_RCC_CSS_IRQHandler"))) __attribute__((used)); +void NMI_Handler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("NMI_RCC_CSS_IRQHandler"))) __attribute__((used)); #else -void NMI_Handler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); +void NMI_Handler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); #endif -void HardFault_Handler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void Ecall_M_Mode_Handler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void Ecall_U_Mode_Handler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void Break_Point_Handler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void SysTick_Handler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void SW_Handler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void WWDG_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void PVD_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void TAMPER_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void RTC_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void FLASH_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void RCC_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void EXTI7_0_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void AWU_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void EXTI0_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void EXTI1_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void EXTI2_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void EXTI3_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void EXTI4_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void DMA1_Channel1_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void DMA1_Channel2_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void DMA1_Channel3_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void DMA1_Channel4_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void DMA1_Channel5_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void DMA1_Channel6_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void DMA1_Channel7_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void ADC1_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void ADC1_2_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void USBFS_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void USBFSWakeUp_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void USB_HP_CAN1_TX_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void USB_LP_CAN1_RX0_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void CAN1_RX1_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void CAN1_SCE_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void EXTI9_5_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void TIM1_BRK_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void TIM1_UP_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void TIM1_TRG_COM_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void TIM1_CC_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void TIM2_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void TIM3_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void TIM4_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void I2C1_EV_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void I2C1_ER_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void SPI1_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void I2C2_EV_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void I2C2_ER_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void SPI2_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void USART1_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void USART2_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void USART3_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void EXTI15_10_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void RTCAlarm_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void USBWakeUp_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void ETH_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void ETHWakeUp_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void OSC32KCal_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void OSCWakeUp_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void DMA1_Channel8_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void BB_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void LLE_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); +void HardFault_Handler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void Ecall_M_Mode_Handler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void Ecall_U_Mode_Handler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void Break_Point_Handler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void SysTick_Handler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void SW_Handler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void WWDG_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void PVD_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void TAMPER_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void RTC_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void FLASH_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void RCC_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void EXTI7_0_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void AWU_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void EXTI0_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void EXTI1_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void EXTI2_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void EXTI3_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void EXTI4_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void DMA1_Channel1_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void DMA1_Channel2_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void DMA1_Channel3_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void DMA1_Channel4_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void DMA1_Channel5_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void DMA1_Channel6_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void DMA1_Channel7_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void ADC1_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void ADC1_2_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void USBFS_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void USBFSWakeUp_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void USB_HP_CAN1_TX_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void USB_LP_CAN1_RX0_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void CAN1_RX1_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void CAN1_SCE_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void EXTI9_5_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void TIM1_BRK_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void TIM1_UP_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void TIM1_TRG_COM_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void TIM1_CC_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void TIM2_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void TIM3_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void TIM4_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void I2C1_EV_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void I2C1_ER_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void SPI1_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void I2C2_EV_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void I2C2_ER_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void SPI2_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void USART1_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void USART2_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void USART3_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void EXTI15_10_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void RTCAlarm_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void USBWakeUp_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void ETH_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void ETHWakeUp_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void OSC32KCal_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void OSCWakeUp_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void DMA1_Channel8_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void BB_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void LLE_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); // This appears to be masked to USBHD -void TIM8_BRK_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void TIM8_UP_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void TIM8_TRG_COM_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void TIM8_CC_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void RNG_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void FSMC_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void SDIO_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void TIM5_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void SPI3_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void UART4_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void UART5_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void TIM6_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void TIM7_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void DMA2_Channel1_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void DMA2_Channel2_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void DMA2_Channel3_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void DMA2_Channel4_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void DMA2_Channel5_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void OTG_FS_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void USBHSWakeUp_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void USBHS_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void DVP_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void UART6_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void UART7_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void UART8_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void TIM9_BRK_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void TIM9_UP_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void TIM9_TRG_COM_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void TIM9_CC_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void TIM10_BRK_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void TIM10_UP_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void TIM10_TRG_COM_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void TIM10_CC_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void DMA2_Channel6_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void DMA2_Channel7_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void DMA2_Channel8_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void DMA2_Channel9_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void DMA2_Channel10_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void DMA2_Channel11_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void CAN2_TX_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void CAN2_RX0_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void CAN2_RX1_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void CAN2_SCE_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void EXTI15_8_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void EXTI25_16_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void USART4_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void USBFS_WakeUp_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void PIOC_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void OPA_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void USBPD_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void USBPD_WKUP_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -void TIM2_CC_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used)); -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 TIM8_BRK_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void TIM8_UP_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void TIM8_TRG_COM_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void TIM8_CC_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void RNG_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void FSMC_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void SDIO_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void TIM5_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void SPI3_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void UART4_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void UART5_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void TIM6_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void TIM7_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void DMA2_Channel1_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void DMA2_Channel2_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void DMA2_Channel3_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void DMA2_Channel4_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void DMA2_Channel5_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void OTG_FS_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void USBHSWakeUp_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void USBHS_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void DVP_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void UART6_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void UART7_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void UART8_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void TIM9_BRK_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void TIM9_UP_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void TIM9_TRG_COM_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void TIM9_CC_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void TIM10_BRK_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void TIM10_UP_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void TIM10_TRG_COM_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void TIM10_CC_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void DMA2_Channel6_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void DMA2_Channel7_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void DMA2_Channel8_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void DMA2_Channel9_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void DMA2_Channel10_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void DMA2_Channel11_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void CAN2_TX_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void CAN2_RX0_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void CAN2_RX1_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void CAN2_SCE_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void EXTI15_8_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void EXTI25_16_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void USART4_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void USBFS_WakeUp_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void PIOC_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void OPA_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void USBPD_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void USBPD_WKUP_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +void TIM2_CC_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute((weak, alias("DefaultIRQHandler"))) __attribute__((used)); +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"))); -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"))); - -void InterruptVectorDefault( void ) +void InterruptVectorDefault(void) { #if !defined(FUNCONF_TINYVECTOR) || !FUNCONF_TINYVECTOR - asm volatile( DEFAULT_INTERRUPT_VECTOR_CONTENTS ); + asm volatile(DEFAULT_INTERRUPT_VECTOR_CONTENTS); #else - asm volatile( "\n\ + asm volatile("\n\ .align 2\n\ .option push;\n\ .option norvc;\n\ j handle_reset\n\ - .option pop;\n" ); + .option pop;\n"); #endif } -#if defined( CH32V003 ) || defined( CH32X03x ) +#if defined(CH32V003) || defined(CH32X03x) -void handle_reset( void ) +void handle_reset(void) { - asm volatile( "\n\ + asm volatile("\n\ .option push\n\ .option norelax\n\ la gp, __global_pointer$\n\ .option pop\n\ la sp, _eusrstack\n" #if __GNUC__ > 10 -".option arch, +zicsr\n" + ".option arch, +zicsr\n" #endif - // Setup the interrupt vector, processor status and INTSYSCR. + // Setup the interrupt vector, processor status and INTSYSCR. -#if FUNCONF_ENABLE_HPE // Enabled nested and hardware (HPE) stack, since it's really good on the x035. -" li t0, 0x88\n\ +#if FUNCONF_ENABLE_HPE // Enabled nested and hardware (HPE) stack, since it's really good on the x035. + " li t0, 0x88\n\ csrs mstatus, t0\n" -" li t0, 0x0b\n\ + " li t0, 0x0b\n\ csrw 0x804, t0\n" #else -" li a0, 0x80\n\ + " li a0, 0x80\n\ csrw mstatus, a0\n" #endif -" li a3, 0x3\n\ + " li a3, 0x3\n\ la a0, InterruptVector\n\ or a0, a0, a3\n\ - csrw mtvec, a0\n" - : : : "a0", "a3", "memory"); + csrw mtvec, a0\n" + : : : "a0", "a3", "memory"); - // Careful: Use registers to prevent overwriting of self-data. - // This clears out BSS. -asm volatile( -" la a0, _sbss\n\ + // Careful: Use registers to prevent overwriting of self-data. + // This clears out BSS. + asm volatile( + " la a0, _sbss\n\ la a1, _ebss\n\ li a2, 0\n\ bge a0, a1, 2f\n\ @@ -1027,8 +1136,8 @@ asm volatile( addi a0, a0, 4\n\ blt a0, a1, 1b\n\ 2:" - // This loads DATA from FLASH to RAM. -" la a0, _data_lma\n\ + // This loads DATA from FLASH to RAM. + " la a0, _data_lma\n\ la a1, _data_vma\n\ la a2, _edata\n\ 1: beq a1, a2, 2f\n\ @@ -1039,54 +1148,54 @@ asm volatile( bne a1, a2, 1b\n\ 2:\n" #ifdef CPLUSPLUS - // Call __libc_init_array function -" call %0 \n\t" -: : "i" (__libc_init_array) -: "a0", "a1", "a2", "a3", "a4", "a5", "t0", "t1", "t2", "memory" + // Call __libc_init_array function + " call %0 \n\t" + : : "i"(__libc_init_array) + : "a0", "a1", "a2", "a3", "a4", "a5", "t0", "t1", "t2", "memory" #else -: : : "a0", "a1", "a2", "a3", "memory" + : : : "a0", "a1", "a2", "a3", "memory" #endif -); + ); -#if defined( FUNCONF_SYSTICK_USE_HCLK ) && FUNCONF_SYSTICK_USE_HCLK - SysTick->CTLR = 5; +#if defined(FUNCONF_SYSTICK_USE_HCLK) && FUNCONF_SYSTICK_USE_HCLK + SysTick->CTLR = 5; #else - SysTick->CTLR = 1; + SysTick->CTLR = 1; #endif - // set mepc to be main as the root app. -asm volatile( -" csrw mepc, %[main]\n" -" mret\n" : : [main]"r"(main) ); + // set mepc to be main as the root app. + asm volatile( + " csrw mepc, %[main]\n" + " mret\n" : : [main] "r"(main)); } #elif defined(CH32V10x) || defined(CH32V20x) || defined(CH32V30x) -void handle_reset( void ) +void handle_reset(void) { - asm volatile( "\n\ + asm volatile("\n\ .option push\n\ .option norelax\n\ la gp, __global_pointer$\n\ .option pop\n\ la sp, _eusrstack\n" #if __GNUC__ > 10 - ".option arch, +zicsr\n" + ".option arch, +zicsr\n" #endif - ); + ); - // Careful: Use registers to prevent overwriting of self-data. - // This clears out BSS. - asm volatile( -" la a0, _sbss\n\ + // Careful: Use registers to prevent overwriting of self-data. + // This clears out BSS. + asm volatile( + " la a0, _sbss\n\ la a1, _ebss\n\ bgeu a0, a1, 2f\n\ 1: sw zero, 0(a0)\n\ addi a0, a0, 4\n\ bltu a0, a1, 1b\n\ 2:" - // This loads DATA from FLASH to RAM. -" la a0, _data_lma\n\ + // This loads DATA from FLASH to RAM. + " la a0, _data_lma\n\ la a1, _data_vma\n\ la a2, _edata\n\ beq a1, a2, 2f\n\ @@ -1097,230 +1206,230 @@ void handle_reset( void ) bltu a1, a2, 1b\n\ 2:\n" #ifdef CPLUSPLUS - // Call __libc_init_array function -" call %0 \n\t" -: : "i" (__libc_init_array) + // Call __libc_init_array function + " call %0 \n\t" + : : "i"(__libc_init_array) #else -: : + : : #endif -: "a0", "a1", "a2", "a3", "memory" -); + : "a0", "a1", "a2", "a3", "memory"); - // Setup the interrupt vector, processor status and INTSYSCR. - asm volatile( -" li t0, 0x1f\n\ + // Setup the interrupt vector, processor status and INTSYSCR. + asm volatile( + " li t0, 0x1f\n\ csrw 0xbc0, t0\n" -#if defined(CH32V30x) && !defined( DISABLED_FLOAT ) -" li t0, 0x6088\n\ +#if defined(CH32V30x) && !defined(DISABLED_FLOAT) + " li t0, 0x6088\n\ csrs mstatus, t0\n" #else -" li t0, 0x88\n\ + " li t0, 0x88\n\ csrs mstatus, t0\n" #endif -#if FUNCONF_ENABLE_HPE // Enabled nested and hardware (HPE) stack, since it's really good on the x035. -" li t0, 0x0b\n\ +#if FUNCONF_ENABLE_HPE // Enabled nested and hardware (HPE) stack, since it's really good on the x035. + " li t0, 0x0b\n\ csrw 0x804, t0\n" #endif -" la t0, InterruptVector\n\ + " 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; +#if defined(FUNCONF_SYSTICK_USE_HCLK) && FUNCONF_SYSTICK_USE_HCLK && !defined(CH32V10x) + SysTick->CTLR = 5; #else - SysTick->CTLR = 1; + SysTick->CTLR = 1; #endif - // set mepc to be main as the root app. - asm volatile( -" csrw mepc, %[main]\n" -" mret\n" : : [main]"r"(main) ); + // set mepc to be main as the root app. + asm volatile( + " csrw mepc, %[main]\n" + " mret\n" : : [main] "r"(main)); } #endif -#if defined( __riscv_float_abi_double ) -#define FLOAD( src, offset, dst ) \ -" fld " #src ", " #offset "*8(" #dst ")\n" -#define FSTORE( dst, offset, src ) \ -" fsd " #dst ", " #offset "*8(" #src ")\n" -#elif defined( __riscv_float_abi_single ) -#define FLOAD( src, offset, dst ) \ -" flw " #src ", " #offset "*4(" #dst ")\n" -#define FSTORE( dst, offset, src ) \ -" fsw " #dst ", " #offset "*4(" #src ")\n" +#if defined(__riscv_float_abi_double) +#define FLOAD(src, offset, dst) \ + " fld " #src ", " #offset "*8(" #dst ")\n" +#define FSTORE(dst, offset, src) \ + " fsd " #dst ", " #offset "*8(" #src ")\n" +#elif defined(__riscv_float_abi_single) +#define FLOAD(src, offset, dst) \ + " flw " #src ", " #offset "*4(" #dst ")\n" +#define FSTORE(dst, offset, src) \ + " fsw " #dst ", " #offset "*4(" #src ")\n" #else // Soft float #endif -__attribute__ ((naked)) int setjmp( jmp_buf env ) -{ - asm volatile( - // Common registers -" sw ra, 0*4(a0)\n" -" sw s0, 1*4(a0)\n" -" sw s1, 2*4(a0)\n" -" sw sp, 3*4(a0)\n" - - // RV32I only registers -#if !defined( __riscv_abi_rve ) -" sw s2, 4*4(a0)\n" -" sw s3, 5*4(a0)\n" -" sw s4, 6*4(a0)\n" -" sw s5, 7*4(a0)\n" -" sw s6, 8*4(a0)\n" -" sw s7, 9*4(a0)\n" -" sw s8, 10*4(a0)\n" -" sw s9, 11*4(a0)\n" -" sw s10, 12*4(a0)\n" -" sw s11, 13*4(a0)\n" -#endif - - // FPU registers -#if defined( FSTORE ) - FSTORE(fs2, 14, a0) - FSTORE(fs3, 15, a0) - FSTORE(fs4, 16, a0) - FSTORE(fs5, 17, a0) - FSTORE(fs6, 18, a0) - FSTORE(fs7, 19, a0) - FSTORE(fs8, 20, a0) - FSTORE(fs9, 21, a0) - FSTORE(fs10, 22, a0) - FSTORE(fs11, 23, a0) -#endif - -" li a0, 0\n" -" ret\n" - ); -} - -__attribute__ ((naked)) void longjmp( jmp_buf env, int val ) +__attribute__((naked)) int setjmp(jmp_buf env) { asm volatile( - // Common registers -" lw ra, 0*4(a0)\n" -" lw s0, 1*4(a0)\n" -" lw s1, 2*4(a0)\n" -" lw sp, 3*4(a0)\n" + // Common registers + " sw ra, 0*4(a0)\n" + " sw s0, 1*4(a0)\n" + " sw s1, 2*4(a0)\n" + " sw sp, 3*4(a0)\n" // RV32I only registers -#if !defined( __riscv_abi_rve ) -" lw s2, 4*4(a0)\n" -" lw s3, 5*4(a0)\n" -" lw s4, 6*4(a0)\n" -" lw s5, 7*4(a0)\n" -" lw s6, 8*4(a0)\n" -" lw s7, 9*4(a0)\n" -" lw s8, 10*4(a0)\n" -" lw s9, 11*4(a0)\n" -" lw s10, 12*4(a0)\n" -" lw s11, 13*4(a0)\n" +#if !defined(__riscv_abi_rve) + " sw s2, 4*4(a0)\n" + " sw s3, 5*4(a0)\n" + " sw s4, 6*4(a0)\n" + " sw s5, 7*4(a0)\n" + " sw s6, 8*4(a0)\n" + " sw s7, 9*4(a0)\n" + " sw s8, 10*4(a0)\n" + " sw s9, 11*4(a0)\n" + " sw s10, 12*4(a0)\n" + " sw s11, 13*4(a0)\n" #endif - // FPU registers -#if defined( FLOAD ) - FLOAD(fs2, 14, a0) - FLOAD(fs3, 15, a0) - FLOAD(fs4, 16, a0) - FLOAD(fs5, 17, a0) - FLOAD(fs6, 18, a0) - FLOAD(fs7, 19, a0) - FLOAD(fs8, 20, a0) - FLOAD(fs9, 21, a0) - FLOAD(fs10, 22, a0) - FLOAD(fs11, 23, a0) + // FPU registers +#if defined(FSTORE) + FSTORE(fs2, 14, a0) + FSTORE(fs3, 15, a0) + FSTORE(fs4, 16, a0) + FSTORE(fs5, 17, a0) + FSTORE(fs6, 18, a0) + FSTORE(fs7, 19, a0) + FSTORE(fs8, 20, a0) + FSTORE(fs9, 21, a0) + FSTORE(fs10, 22, a0) + FSTORE(fs11, 23, a0) #endif -" seqz a0, a1\n" // a0 = (a1 == 0) ? 1 : 0 -" add a0, a0, a1\n" -" ret\n" - ); - __builtin_unreachable(); // Disable warning about no return. + " li a0, 0\n" + " ret\n"); } -#if defined( FUNCONF_USE_UARTPRINTF ) && FUNCONF_USE_UARTPRINTF -void SetupUART( int uartBRR ) +__attribute__((naked)) void longjmp(jmp_buf env, int val) { -#ifdef CH32V003 - // Enable GPIOD and UART. - RCC->APB2PCENR |= RCC_APB2Periph_GPIOD | RCC_APB2Periph_USART1; + asm volatile( + // Common registers + " lw ra, 0*4(a0)\n" + " lw s0, 1*4(a0)\n" + " lw s1, 2*4(a0)\n" + " lw sp, 3*4(a0)\n" - // Push-Pull, 10MHz Output, GPIO D5, with AutoFunction - GPIOD->CFGLR &= ~(0xf<<(4*5)); - GPIOD->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP_AF)<<(4*5); -#elif defined(CH32X03x) - RCC->APB2PCENR |= RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1; - - // Push-Pull, 10MHz Output, GPIO A9, with AutoFunction - GPIOB->CFGHR &= ~(0xf<<(4*2)); - GPIOB->CFGHR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP_AF)<<(4*2); -#else - RCC->APB2PCENR |= RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1; - - // Push-Pull, 10MHz Output, GPIO A9, with AutoFunction - GPIOA->CFGHR &= ~(0xf<<(4*1)); - GPIOA->CFGHR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP_AF)<<(4*1); + // RV32I only registers +#if !defined(__riscv_abi_rve) + " lw s2, 4*4(a0)\n" + " lw s3, 5*4(a0)\n" + " lw s4, 6*4(a0)\n" + " lw s5, 7*4(a0)\n" + " lw s6, 8*4(a0)\n" + " lw s7, 9*4(a0)\n" + " lw s8, 10*4(a0)\n" + " lw s9, 11*4(a0)\n" + " lw s10, 12*4(a0)\n" + " lw s11, 13*4(a0)\n" #endif - // 115200, 8n1. Note if you don't specify a mode, UART remains off even when UE_Set. - USART1->CTLR1 = USART_WordLength_8b | USART_Parity_No | USART_Mode_Tx; - USART1->CTLR2 = USART_StopBits_1; - USART1->CTLR3 = USART_HardwareFlowControl_None; + // FPU registers +#if defined(FLOAD) + FLOAD(fs2, 14, a0) + FLOAD(fs3, 15, a0) + FLOAD(fs4, 16, a0) + FLOAD(fs5, 17, a0) + FLOAD(fs6, 18, a0) + FLOAD(fs7, 19, a0) + FLOAD(fs8, 20, a0) + FLOAD(fs9, 21, a0) + FLOAD(fs10, 22, a0) + FLOAD(fs11, 23, a0) +#endif - USART1->BRR = uartBRR; - USART1->CTLR1 |= CTLR1_UE_Set; + " seqz a0, a1\n" // a0 = (a1 == 0) ? 1 : 0 + " add a0, a0, a1\n" + " ret\n"); + __builtin_unreachable(); // Disable warning about no return. +} + +#if defined(FUNCONF_USE_UARTPRINTF) && FUNCONF_USE_UARTPRINTF +void SetupUART(int uartBRR) +{ +#ifdef CH32V003 + // Enable GPIOD and UART. + RCC->APB2PCENR |= RCC_APB2Periph_GPIOD | RCC_APB2Periph_USART1; + + // Push-Pull, 10MHz Output, GPIO D5, with AutoFunction + GPIOD->CFGLR &= ~(0xf << (4 * 5)); + GPIOD->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP_AF) << (4 * 5); +#elif defined(CH32X03x) + RCC->APB2PCENR |= RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1; + + // Push-Pull, 10MHz Output, GPIO A9, with AutoFunction + GPIOB->CFGHR &= ~(0xf << (4 * 2)); + GPIOB->CFGHR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP_AF) << (4 * 2); +#else + RCC->APB2PCENR |= RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1; + + // Push-Pull, 10MHz Output, GPIO A9, with AutoFunction + GPIOA->CFGHR &= ~(0xf << (4 * 1)); + GPIOA->CFGHR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP_AF) << (4 * 1); +#endif + + // 115200, 8n1. Note if you don't specify a mode, UART remains off even when UE_Set. + USART1->CTLR1 = USART_WordLength_8b | USART_Parity_No | USART_Mode_Tx; + USART1->CTLR2 = USART_StopBits_1; + USART1->CTLR3 = USART_HardwareFlowControl_None; + + USART1->BRR = uartBRR; + USART1->CTLR1 |= CTLR1_UE_Set; } // 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)); - USART1->DATAR = *buf++; - } - return size; + for (int i = 0; i < size; i++) + { + while (!(USART1->STATR & USART_FLAG_TC)) + ; + USART1->DATAR = *buf++; + } + return size; } // single char to UART WEAK int putchar(int c) { - while( !(USART1->STATR & USART_FLAG_TC)); - USART1->DATAR = (const char)c; - return 1; + while (!(USART1->STATR & USART_FLAG_TC)) + ; + USART1->DATAR = (const char)c; + return 1; } #endif -#if defined( FUNCONF_USE_DEBUGPRINTF ) && FUNCONF_USE_DEBUGPRINTF +#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; } - -static void internal_handle_input( volatile uint32_t * dmdata0 ) +void handle_debug_input(int numbytes, uint8_t *data) __attribute__((weak)); +void handle_debug_input(int numbytes, uint8_t *data) { - uint32_t dmd0 = *dmdata0; - int bytes = (dmd0 & 0x3f) - 4; - if( bytes > 0 && bytes < 16 ) - { - handle_debug_input( bytes, ((uint8_t*)dmdata0) + 1 ); - } + (void)numbytes; + (void)data; } - -void poll_input( void ) +static void internal_handle_input(volatile uint32_t *dmdata0) { - volatile uint32_t * dmdata0 = (volatile uint32_t *)DMDATA0; - if( ((*dmdata0) & 0x80) == 0 ) - { - internal_handle_input( dmdata0 ); - *dmdata0 = 0x84; - } + uint32_t dmd0 = *dmdata0; + int bytes = (dmd0 & 0x3f) - 4; + if (bytes > 0 && bytes < 16) + { + handle_debug_input(bytes, ((uint8_t *)dmdata0) + 1); + } } +void poll_input(void) +{ + volatile uint32_t *dmdata0 = (volatile uint32_t *)DMDATA0; + if (((*dmdata0) & 0x80) == 0) + { + internal_handle_input(dmdata0); + *dmdata0 = 0x84; + } +} // MSB .... LSB // DMDATA0: char3 char2 char1 [status word] @@ -1335,115 +1444,115 @@ void poll_input( void ) // declare as weak to allow overriding. WEAK int _write(int fd, const char *buf, int size) { - (void)fd; - if( ( *DMDATA0 & 0xc0 ) == 0xc0 ) return 0; + (void)fd; + if ((*DMDATA0 & 0xc0) == 0xc0) return 0; - char buffer[4] = { 0 }; - int place = 0; - uint32_t lastdmd; - uint32_t timeout = FUNCONF_DEBUGPRINTF_TIMEOUT; // Give up after ~120ms + char buffer[4] = {0}; + int place = 0; + uint32_t lastdmd; + uint32_t timeout = FUNCONF_DEBUGPRINTF_TIMEOUT; // Give up after ~120ms - if( size == 0 ) - { - lastdmd = (*DMDATA0); - if( lastdmd && !(lastdmd&0x80) ) internal_handle_input( (uint32_t*)DMDATA0 ); - } - while( place < size ) - { - int tosend = size - place; - if( tosend > 7 ) tosend = 7; + if (size == 0) + { + lastdmd = (*DMDATA0); + if (lastdmd && !(lastdmd & 0x80)) internal_handle_input((uint32_t *)DMDATA0); + } + while (place < size) + { + int tosend = size - place; + if (tosend > 7) tosend = 7; - while( ( lastdmd = (*DMDATA0) ) & 0x80 ) - { - if( timeout-- == 0 ) - { - *DMDATA0 |= 0xc0; - return 0; - } - } + while ((lastdmd = (*DMDATA0)) & 0x80) + { + if (timeout-- == 0) + { + *DMDATA0 |= 0xc0; + return 0; + } + } - if( lastdmd ) internal_handle_input( (uint32_t*)DMDATA0 ); + if (lastdmd) internal_handle_input((uint32_t *)DMDATA0); - timeout = FUNCONF_DEBUGPRINTF_TIMEOUT; + timeout = FUNCONF_DEBUGPRINTF_TIMEOUT; - int t = 3; - while( t < tosend ) - { - buffer[t-3] = buf[t+place]; - t++; - } - *DMDATA1 = *(uint32_t*)&(buffer[0]); - t = 0; - while( t < tosend && t < 3 ) - { - buffer[t+1] = buf[t+place]; - t++; - } - buffer[0] = 0x80 | (tosend + 4); - *DMDATA0 = *(uint32_t*)&(buffer[0]); + int t = 3; + while (t < tosend) + { + buffer[t - 3] = buf[t + place]; + t++; + } + *DMDATA1 = *(uint32_t *)&(buffer[0]); + t = 0; + while (t < tosend && t < 3) + { + buffer[t + 1] = buf[t + place]; + t++; + } + buffer[0] = 0x80 | (tosend + 4); + *DMDATA0 = *(uint32_t *)&(buffer[0]); - //buf += tosend; - place += tosend; - } - return size; + // buf += tosend; + place += tosend; + } + return size; } // single to debug intf WEAK int putchar(int c) { - if( ( *DMDATA0 & 0xc0 ) == 0xc0 ) return 0; + if ((*DMDATA0 & 0xc0) == 0xc0) return 0; - int timeout = FUNCONF_DEBUGPRINTF_TIMEOUT; - uint32_t lastdmd = 0; + int timeout = FUNCONF_DEBUGPRINTF_TIMEOUT; + uint32_t lastdmd = 0; - while( ( lastdmd = (*DMDATA0) ) & 0x80 ) - { - if( timeout-- == 0 ) - { - *DMDATA0 |= 0xc0; - return 0; - } - } + while ((lastdmd = (*DMDATA0)) & 0x80) + { + if (timeout-- == 0) + { + *DMDATA0 |= 0xc0; + return 0; + } + } - // Simply seeking input. - if( lastdmd ) internal_handle_input( (uint32_t*)DMDATA0 ); + // Simply seeking input. + if (lastdmd) internal_handle_input((uint32_t *)DMDATA0); - // Write out character. - *DMDATA0 = 0x85 | ((const char)c<<8); - return 1; + // Write out character. + *DMDATA0 = 0x85 | ((const char)c << 8); + return 1; } -void SetupDebugPrintf( void ) +void SetupDebugPrintf(void) { - // Clear out the sending flag. - *DMDATA1 = 0x00; - *DMDATA0 = 0x80; + // Clear out the sending flag. + *DMDATA1 = 0x00; + *DMDATA0 = 0x80; } -int WaitForDebuggerToAttach( int timeout_ms ) +int WaitForDebuggerToAttach(int timeout_ms) { - #if defined(CH32V20x) || defined(CH32V30x) - #define systickcnt_t uint64_t - #define SYSTICKCNT SysTick->CNT +#define systickcnt_t uint64_t +#define SYSTICKCNT SysTick->CNT #elif defined(CH32V10x) || defined(CH32X03x) - #define systickcnt_t uint32_t - #define SYSTICKCNT SysTick->CNTL +#define systickcnt_t uint32_t +#define SYSTICKCNT SysTick->CNTL #else - #define systickcnt_t uint32_t - #define SYSTICKCNT SysTick->CNT +#define systickcnt_t uint32_t +#define SYSTICKCNT SysTick->CNT #endif - const systickcnt_t start = SYSTICKCNT; - const systickcnt_t ticks_per_ms = (FUNCONF_SYSTEM_CORE_CLOCK / 1000); - const systickcnt_t timeout = timeout_ms * ticks_per_ms; + const systickcnt_t start = SYSTICKCNT; + const systickcnt_t ticks_per_ms = (FUNCONF_SYSTEM_CORE_CLOCK / 1000); + const systickcnt_t timeout = timeout_ms * ticks_per_ms; - // Wait for the sentinel to become zero. - while( !DidDebuggerAttach() ) { - if( timeout_ms && (SYSTICKCNT - start) > timeout ) return 1; - } + // Wait for the sentinel to become zero. + while (!DidDebuggerAttach()) + { + if (timeout_ms && (SYSTICKCNT - start) > timeout) return 1; + } - return 0; + return 0; #undef systickcnt_t #undef SYSTICKCNT @@ -1451,234 +1560,245 @@ int WaitForDebuggerToAttach( int timeout_ms ) #endif -#if (defined( FUNCONF_USE_DEBUGPRINTF ) && !FUNCONF_USE_DEBUGPRINTF) && \ - (defined( FUNCONF_USE_UARTPRINTF ) && !FUNCONF_USE_UARTPRINTF) && \ - (defined( FUNCONF_NULL_PRINTF ) && FUNCONF_NULL_PRINTF) +#if (defined(FUNCONF_USE_DEBUGPRINTF) && !FUNCONF_USE_DEBUGPRINTF) && \ + (defined(FUNCONF_USE_UARTPRINTF) && !FUNCONF_USE_UARTPRINTF) && \ + (defined(FUNCONF_NULL_PRINTF) && FUNCONF_NULL_PRINTF) WEAK int _write(int fd, const char *buf, int size) { - return size; + return size; } // single to debug intf WEAK int putchar(int c) { - return 1; + return 1; } #endif -void DelaySysTick( uint32_t n ) +void DelaySysTick(uint32_t n) { #ifdef CH32V003 - uint32_t targend = SysTick->CNT + n; - while( ((int32_t)( SysTick->CNT - targend )) < 0 ); + uint32_t targend = SysTick->CNT + n; + 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 ); + uint64_t targend = SysTick->CNT + n; + 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 ); + uint32_t targend = SysTick->CNTL + n; + while (((int32_t)(SysTick->CNTL - targend)) < 0) + ; #else - #error DelaySysTick not defined. +#error DelaySysTick not defined. #endif } -void SystemInit( void ) +void SystemInit(void) { #if defined(CH32V30x) && defined(TARGET_MCU_MEMORY_SPLIT) - FLASH->OBR = TARGET_MCU_MEMORY_SPLIT<<8; + FLASH->OBR = TARGET_MCU_MEMORY_SPLIT << 8; #endif #if FUNCONF_HSE_BYPASS - #define HSEBYP (1<<18) +#define HSEBYP (1 << 18) #else - #define HSEBYP 0 +#define HSEBYP 0 #endif #if defined(FUNCONF_USE_CLK_SEC) && FUNCONF_USE_CLK_SEC -#define RCC_CSS RCC_CSSON // Enable clock security system +#define RCC_CSS RCC_CSSON // Enable clock security system #else #define RCC_CSS 0 #endif #if defined(FUNCONF_USE_PLL) && FUNCONF_USE_PLL - #if defined(CH32V003) - #define BASE_CFGR0 RCC_HPRE_DIV1 | RCC_PLLSRC_HSI_Mul2 // HCLK = SYSCLK = APB1 And, enable PLL - #elif defined(CH32V20x_D8W) - #define BASE_CFGR0 RCC_HPRE_DIV1 | RCC_PPRE2_DIV1 | RCC_PPRE1_DIV1 | PLL_MULTIPLICATION - #else - #define BASE_CFGR0 RCC_HPRE_DIV1 | RCC_PPRE2_DIV1 | RCC_PPRE1_DIV2 | PLL_MULTIPLICATION - #endif +#if defined(CH32V003) +#define BASE_CFGR0 RCC_HPRE_DIV1 | RCC_PLLSRC_HSI_Mul2 // HCLK = SYSCLK = APB1 And, enable PLL +#elif defined(CH32V20x_D8W) +#define BASE_CFGR0 RCC_HPRE_DIV1 | RCC_PPRE2_DIV1 | RCC_PPRE1_DIV1 | PLL_MULTIPLICATION #else - #if defined(CH32V003) || defined(CH32X03x) - #define BASE_CFGR0 RCC_HPRE_DIV1 // HCLK = SYSCLK = APB1 And, no pll. - #else - #define BASE_CFGR0 RCC_HPRE_DIV1 | RCC_PPRE2_DIV1 | RCC_PPRE1_DIV1 - #endif +#define BASE_CFGR0 RCC_HPRE_DIV1 | RCC_PPRE2_DIV1 | RCC_PPRE1_DIV2 | PLL_MULTIPLICATION +#endif +#else +#if defined(CH32V003) || defined(CH32X03x) +#define BASE_CFGR0 RCC_HPRE_DIV1 // HCLK = SYSCLK = APB1 And, no pll. +#else +#define BASE_CFGR0 RCC_HPRE_DIV1 | RCC_PPRE2_DIV1 | RCC_PPRE1_DIV1 +#endif #endif // HSI always ON - needed for the Debug subsystem -#define BASE_CTLR (((FUNCONF_HSITRIM) << 3) | RCC_HSION | HSEBYP | RCC_CSS) -//#define BASE_CTLR (((FUNCONF_HSITRIM) << 3) | HSEBYP | RCC_CSS) // disable HSI in HSE modes +#define BASE_CTLR (((FUNCONF_HSITRIM) << 3) | RCC_HSION | HSEBYP | RCC_CSS) + // #define BASE_CTLR (((FUNCONF_HSITRIM) << 3) | HSEBYP | RCC_CSS) // disable HSI in HSE modes - // Flash latency settings. + // Flash latency settings. #if defined(CH32V00x) - // Per TRM - #if FUNCONF_SYSTEM_CORE_CLOCK > 25000000 - FLASH->ACTLR = FLASH_ACTLR_LATENCY_2; - #elif FUNCONF_SYSTEM_CORE_CLOCK > 15000000 - FLASH->ACTLR = FLASH_ACTLR_LATENCY_1; - #else - FLASH->ACTLR = FLASH_ACTLR_LATENCY_0; - #endif +// Per TRM +#if FUNCONF_SYSTEM_CORE_CLOCK > 25000000 + FLASH->ACTLR = FLASH_ACTLR_LATENCY_2; +#elif FUNCONF_SYSTEM_CORE_CLOCK > 15000000 + FLASH->ACTLR = FLASH_ACTLR_LATENCY_1; +#else + FLASH->ACTLR = FLASH_ACTLR_LATENCY_0; +#endif #elif defined(CH32X03x) - FLASH->ACTLR = FLASH_ACTLR_LATENCY_2; // +2 Cycle Latency (Recommended per TRM) + FLASH->ACTLR = FLASH_ACTLR_LATENCY_2; // +2 Cycle Latency (Recommended per TRM) #elif defined(CH32V003) - #if FUNCONF_SYSTEM_CORE_CLOCK > 25000000 - FLASH->ACTLR = FLASH_ACTLR_LATENCY_1; // +1 Cycle Latency - #else - FLASH->ACTLR = FLASH_ACTLR_LATENCY_0; // +0 Cycle Latency - #endif +#if FUNCONF_SYSTEM_CORE_CLOCK > 25000000 + FLASH->ACTLR = FLASH_ACTLR_LATENCY_1; // +1 Cycle Latency +#else + FLASH->ACTLR = FLASH_ACTLR_LATENCY_0; // +0 Cycle Latency +#endif #endif #if defined(FUNCONF_USE_HSI) && FUNCONF_USE_HSI - #if defined(CH32V30x) || defined(CH32V20x) || defined(CH32V10x) - EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE; - #endif - #if defined(FUNCONF_USE_PLL) && FUNCONF_USE_PLL - RCC->CFGR0 = BASE_CFGR0; - RCC->CTLR = BASE_CTLR | RCC_HSION | RCC_PLLON; // Use HSI, enable PLL. - #else - RCC->CFGR0 = RCC_HPRE_DIV1; // PLLCLK = HCLK = SYSCLK = APB1 - RCC->CTLR = BASE_CTLR | RCC_HSION; // Use HSI, Only. - #endif +#if defined(CH32V30x) || defined(CH32V20x) || defined(CH32V10x) + EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE; +#endif +#if defined(FUNCONF_USE_PLL) && FUNCONF_USE_PLL + RCC->CFGR0 = BASE_CFGR0; + RCC->CTLR = BASE_CTLR | RCC_HSION | RCC_PLLON; // Use HSI, enable PLL. +#else + RCC->CFGR0 = RCC_HPRE_DIV1; // PLLCLK = HCLK = SYSCLK = APB1 + RCC->CTLR = BASE_CTLR | RCC_HSION; // Use HSI, Only. +#endif #elif defined(FUNCONF_USE_HSE) && FUNCONF_USE_HSE - #if defined(CH32V003) - RCC->CTLR = BASE_CTLR | RCC_HSION | RCC_HSEON ; // Keep HSI on while turning on HSE - #else - RCC->CTLR = RCC_HSEON; // Only turn on HSE. - #endif - - // Values lifted from the EVT. There is little to no documentation on what this does. - while(!(RCC->CTLR&RCC_HSERDY)) {}; - - #if defined(CH32V003) - RCC->CFGR0 = RCC_PLLSRC_HSE_Mul2 | RCC_SW_HSE; - #else - RCC->CFGR0 = BASE_CFGR0 | RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE; - #endif - - #if defined(FUNCONF_USE_PLL) && FUNCONF_USE_PLL - RCC->CTLR = BASE_CTLR | RCC_HSEON | RCC_PLLON; // Turn off HSI. - #else - RCC->CTLR = RCC_HSEON | HSEBYP; // Turn off PLL and HSI. - #endif +#if defined(CH32V003) + RCC->CTLR = BASE_CTLR | RCC_HSION | RCC_HSEON; // Keep HSI on while turning on HSE +#else + RCC->CTLR = RCC_HSEON; // Only turn on HSE. #endif - // CH32V10x flash prefetch buffer -#if defined(CH32V10x) - // Enable Prefetch Buffer - FLASH->ACTLR |= FLASH_ACTLR_PRFTBE; -#endif + // Values lifted from the EVT. There is little to no documentation on what this does. + while (!(RCC->CTLR & RCC_HSERDY)) {}; - // CH32V10x flash latency -#if defined(CH32V10x) - #if defined(FUNCONF_USE_HSE) && FUNCONF_USE_HSE - #if !defined(FUNCONF_USE_PLL) || !FUNCONF_USE_PLL - FLASH->ACTLR = FLASH_ACTLR_LATENCY_0; // +0 Cycle Latency - #else - #if FUNCONF_SYSTEM_CORE_CLOCK < 56000000 - FLASH->ACTLR = FLASH_ACTLR_LATENCY_1; // +1 Cycle Latency - #else - FLASH->ACTLR = FLASH_ACTLR_LATENCY_2; // +2 Cycle Latency - #endif - #endif - #else - FLASH->ACTLR = FLASH_ACTLR_LATENCY_1; // +1 Cycle Latency - #endif +#if defined(CH32V003) + RCC->CFGR0 = RCC_PLLSRC_HSE_Mul2 | RCC_SW_HSE; +#else + RCC->CFGR0 = BASE_CFGR0 | RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE; #endif - 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 - 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 + RCC->CTLR = BASE_CTLR | RCC_HSEON | RCC_PLLON; // Turn off HSI. +#else + RCC->CTLR = RCC_HSEON | HSEBYP; // Turn off PLL and HSI. +#endif #endif -#if defined( FUNCONF_USE_UARTPRINTF ) && FUNCONF_USE_UARTPRINTF - SetupUART( UART_BRR ); + // CH32V10x flash prefetch buffer +#if defined(CH32V10x) + // Enable Prefetch Buffer + FLASH->ACTLR |= FLASH_ACTLR_PRFTBE; #endif -#if defined( FUNCONF_USE_DEBUGPRINTF ) && FUNCONF_USE_DEBUGPRINTF - SetupDebugPrintf(); + + // CH32V10x flash latency +#if defined(CH32V10x) +#if defined(FUNCONF_USE_HSE) && FUNCONF_USE_HSE +#if !defined(FUNCONF_USE_PLL) || !FUNCONF_USE_PLL + FLASH->ACTLR = FLASH_ACTLR_LATENCY_0; // +0 Cycle Latency +#else +#if FUNCONF_SYSTEM_CORE_CLOCK < 56000000 + FLASH->ACTLR = FLASH_ACTLR_LATENCY_1; // +1 Cycle Latency +#else + FLASH->ACTLR = FLASH_ACTLR_LATENCY_2; // +2 Cycle Latency +#endif +#endif +#else + FLASH->ACTLR = FLASH_ACTLR_LATENCY_1; // +1 Cycle Latency +#endif +#endif + + 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 + 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 +#endif + +#if defined(FUNCONF_USE_UARTPRINTF) && FUNCONF_USE_UARTPRINTF + SetupUART(UART_BRR); +#endif +#if defined(FUNCONF_USE_DEBUGPRINTF) && FUNCONF_USE_DEBUGPRINTF + SetupDebugPrintf(); #endif } -void funAnalogInit( void ) +void funAnalogInit(void) { - //RCC->CFGR0 &= ~(0x1F<<11); // Assume ADCPRE = 0 - RCC->APB2PCENR |= RCC_APB2Periph_ADC1; + // RCC->CFGR0 &= ~(0x1F<<11); // Assume ADCPRE = 0 + RCC->APB2PCENR |= RCC_APB2Periph_ADC1; - // Reset ADC. - RCC->APB2PRSTR |= RCC_APB2Periph_ADC1; - RCC->APB2PRSTR &= ~RCC_APB2Periph_ADC1; + // Reset ADC. + RCC->APB2PRSTR |= RCC_APB2Periph_ADC1; + RCC->APB2PRSTR &= ~RCC_APB2Periph_ADC1; - // set sampling time for all channels to 15 (A good middleground) ADC_SMP0_1. - ADC1->SAMPTR2 = (ADC_SMP0_1<<(3*0)) | (ADC_SMP0_1<<(3*1)) | (ADC_SMP0_1<<(3*2)) | (ADC_SMP0_1<<(3*3)) | (ADC_SMP0_1<<(3*4)) | (ADC_SMP0_1<<(3*5)) | (ADC_SMP0_1<<(3*6)) | (ADC_SMP0_1<<(3*7)) | (ADC_SMP0_1<<(3*8)) | (ADC_SMP0_1<<(3*9)); - ADC1->SAMPTR1 = (ADC_SMP0_1<<(3*0)) | (ADC_SMP0_1<<(3*1)) | (ADC_SMP0_1<<(3*2)) | (ADC_SMP0_1<<(3*3)) | (ADC_SMP0_1<<(3*4)) | (ADC_SMP0_1<<(3*5)); + // set sampling time for all channels to 15 (A good middleground) ADC_SMP0_1. + ADC1->SAMPTR2 = (ADC_SMP0_1 << (3 * 0)) | (ADC_SMP0_1 << (3 * 1)) | (ADC_SMP0_1 << (3 * 2)) | (ADC_SMP0_1 << (3 * 3)) | (ADC_SMP0_1 << (3 * 4)) | (ADC_SMP0_1 << (3 * 5)) | (ADC_SMP0_1 << (3 * 6)) | (ADC_SMP0_1 << (3 * 7)) | (ADC_SMP0_1 << (3 * 8)) | (ADC_SMP0_1 << (3 * 9)); + ADC1->SAMPTR1 = (ADC_SMP0_1 << (3 * 0)) | (ADC_SMP0_1 << (3 * 1)) | (ADC_SMP0_1 << (3 * 2)) | (ADC_SMP0_1 << (3 * 3)) | (ADC_SMP0_1 << (3 * 4)) | (ADC_SMP0_1 << (3 * 5)); - ADC1->CTLR2 |= ADC_ADON | ADC_EXTSEL; // turn on ADC and set rule group to sw trig + ADC1->CTLR2 |= ADC_ADON | ADC_EXTSEL; // turn on ADC and set rule group to sw trig - // Reset calibration - ADC1->CTLR2 |= ADC_RSTCAL; - while(ADC1->CTLR2 & ADC_RSTCAL); - - // Calibrate - ADC1->CTLR2 |= ADC_CAL; - while(ADC1->CTLR2 & ADC_CAL); + // Reset calibration + ADC1->CTLR2 |= ADC_RSTCAL; + while (ADC1->CTLR2 & ADC_RSTCAL) + ; + // Calibrate + ADC1->CTLR2 |= ADC_CAL; + while (ADC1->CTLR2 & ADC_CAL) + ; } -int funAnalogRead( int nAnalogNumber ) +int funAnalogRead(int nAnalogNumber) { - ADC1->RSQR3 = nAnalogNumber; + ADC1->RSQR3 = nAnalogNumber; - // start sw conversion (auto clears) - ADC1->CTLR2 |= ADC_SWSTART; - - // wait for conversion complete - while(!(ADC1->STATR & ADC_EOC)); - - // get result - return ADC1->RDATAR; + // start sw conversion (auto clears) + ADC1->CTLR2 |= ADC_SWSTART; + + // wait for conversion complete + while (!(ADC1->STATR & ADC_EOC)) + ; + + // get result + return ADC1->RDATAR; } // C++ Support #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)); -extern void (*__preinit_array_end[]) (void) __attribute__((weak)); -extern void (*__init_array_start[]) (void) __attribute__((weak)); -extern void (*__init_array_end[]) (void) __attribute__((weak)); +extern void (*__preinit_array_start[])(void) __attribute__((weak)); +extern void (*__preinit_array_end[])(void) __attribute__((weak)); +extern void (*__init_array_start[])(void) __attribute__((weak)); +extern void (*__init_array_end[])(void) __attribute__((weak)); void __libc_init_array(void) { - size_t count; - size_t i; + size_t count; + size_t i; - count = __preinit_array_end - __preinit_array_start; - for (i = 0; i < count; i++) - __preinit_array_start[i](); + count = __preinit_array_end - __preinit_array_start; + for (i = 0; i < count; i++) + __preinit_array_start[i](); - count = __init_array_end - __init_array_start; - for (i = 0; i < count; i++) - __init_array_start[i](); + count = __init_array_end - __init_array_start; + for (i = 0; i < count; i++) + __init_array_start[i](); } #endif diff --git a/src/ch32fun.h b/src/ch32fun.h old mode 100755 new mode 100644 index 3dc9409..e44018d --- a/src/ch32fun.h +++ b/src/ch32fun.h @@ -7,45 +7,45 @@ #include "funconfig.h" /***************************************************************************** - CH32V003 BASICS + CH32V003 BASICS - 1. Be sure to see configuration section below! + 1. Be sure to see configuration section below! - 2. Backend Initialization - SystemInit(); + 2. Backend Initialization + SystemInit(); - 3. Arduino-like I/O - funGpioInitAll(); - funPinMode( PA2, GPIO_CFGLR_OUT_10Mhz_PP ); - funDigitalWrite( PA2, FUN_HIGH ); - funDigitalWrite( PA2, FUN_LOW ); - funAnalogRead( 0 ); // Not Pin number, but rather analog number. + 3. Arduino-like I/O + funGpioInitAll(); + funPinMode( PA2, GPIO_CFGLR_OUT_10Mhz_PP ); + funDigitalWrite( PA2, FUN_HIGH ); + funDigitalWrite( PA2, FUN_LOW ); + funAnalogRead( 0 ); // Not Pin number, but rather analog number. - 4. Delays - Delay_Us(n) - Delay_Ms(n) - DelaySysTick( uint32_t n ); + 4. Delays + Delay_Us(n) + Delay_Ms(n) + DelaySysTick( uint32_t n ); - 5. printf - printf, _write may be semihosted, or printed to UART. + 5. printf + printf, _write may be semihosted, or printed to UART. - poll_input, handle_debug_input may be used with semihsoting to accept input from host. + poll_input, handle_debug_input may be used with semihsoting to accept input from host. - For UART printf, on: - CH32V003, Port D5, 115200 8n1 - CH32V203, Port A9, 115200 8n1 + For UART printf, on: + CH32V003, Port D5, 115200 8n1 + CH32V203, Port A9, 115200 8n1 - Modifications can be made to SetupUart, or your own version as desired. + Modifications can be made to SetupUart, or your own version as desired. 6. ISR Control Routines - __enable_irq(); // For global interrupt enable - __disable_irq(); // For global interrupt disable - __isenabled_irq(); // For seeing if interrupts are enabled. - NVIC_EnableIRQ(IRQn_Type IRQn) // To enable a specific interrupt + __enable_irq(); // For global interrupt enable + __disable_irq(); // For global interrupt disable + __isenabled_irq(); // For seeing if interrupts are enabled. + NVIC_EnableIRQ(IRQn_Type IRQn) // To enable a specific interrupt - 7. Hardware MMIO structs, i.e. - SysTick->CNT = current system tick counter (can be Hclk or Hclk/8) - TIM2->CH1CVR = direct control over a PWM output + 7. Hardware MMIO structs, i.e. + SysTick->CNT = current system tick counter (can be Hclk or Hclk/8) + TIM2->CH1CVR = direct control over a PWM output 8. Default debug behavior, when semihosting: a. You get access to DidDebuggerAttach() - so you can see if a debugger has attached. @@ -59,12 +59,10 @@ by setting FUNCONF_DEBUG_HARDFAULT to 0. */ - - /****************************************************************************** * CH32V003 Fun Configs; please define any non-default options in funconfig.h * -#define FUNCONF_USE_PLL 1 // Use built-in 2x PLL +#define FUNCONF_USE_PLL 1 // Use built-in 2x PLL #define FUNCONF_USE_HSI 1 // Use HSI Internal Oscillator #define FUNCONF_USE_HSE 0 // Use External Oscillator #define FUNCONF_HSITRIM 0x10 // Use factory calibration on HSI Trim. @@ -78,7 +76,7 @@ #define FUNCONF_TINYVECTOR 0 // If enabled, Does not allow normal interrupts. #define FUNCONF_UART_PRINTF_BAUD 115200 // Only used if FUNCONF_USE_UARTPRINTF is set. #define FUNCONF_DEBUGPRINTF_TIMEOUT 0x80000 // Arbitrary time units, this is around 120ms. -#define FUNCONF_ENABLE_HPE 1 // Enable hardware interrupt stack. Very good on QingKeV4, i.e. x035, v10x, v20x, v30x, but questionable on 003. +#define FUNCONF_ENABLE_HPE 1 // Enable hardware interrupt stack. Very good on QingKeV4, i.e. x035, v10x, v20x, v30x, but questionable on 003. // If you are using that, consider using INTERRUPT_DECORATOR as an attribute to your interrupt handlers. #define FUNCONF_USE_5V_VDD 0 // Enable this if you plan to use your part at 5V - affects USB and PD configration on the x035. #define FUNCONF_DEBUG_HARDFAULT 1 // Log fatal errors with "printf" @@ -86,173 +84,172 @@ // Sanity check for when porting old code. #if defined(CH32V10x) || defined(CH32V20x) || defined(CH32V30x) || defined(CH32X03x) - #if defined(CH32V003) - #error Cannot define CH32V003 and another arch. - #endif +#if defined(CH32V003) +#error Cannot define CH32V003 and another arch. +#endif #endif #if !defined(FUNCONF_USE_DEBUGPRINTF) && !defined(FUNCONF_USE_UARTPRINTF) - #define FUNCONF_USE_DEBUGPRINTF 1 +#define FUNCONF_USE_DEBUGPRINTF 1 #endif #if defined(FUNCONF_USE_UARTPRINTF) && FUNCONF_USE_UARTPRINTF && !defined(FUNCONF_UART_PRINTF_BAUD) - #define FUNCONF_UART_PRINTF_BAUD 115200 +#define FUNCONF_UART_PRINTF_BAUD 115200 #endif #if defined(FUNCONF_USE_DEBUGPRINTF) && FUNCONF_USE_DEBUGPRINTF && !defined(FUNCONF_DEBUGPRINTF_TIMEOUT) - #define FUNCONF_DEBUGPRINTF_TIMEOUT 0x80000 +#define FUNCONF_DEBUGPRINTF_TIMEOUT 0x80000 #endif #if defined(FUNCONF_USE_HSI) && defined(FUNCONF_USE_HSE) && FUNCONF_USE_HSI && FUNCONF_USE_HSE - #error FUNCONF_USE_HSI and FUNCONF_USE_HSE cannot both be set +#error FUNCONF_USE_HSI and FUNCONF_USE_HSE cannot both be set #endif -#if !defined( FUNCONF_USE_HSI ) && !defined( FUNCONF_USE_HSE ) - #define FUNCONF_USE_HSI 1 // Default to use HSI - #define FUNCONF_USE_HSE 0 +#if !defined(FUNCONF_USE_HSI) && !defined(FUNCONF_USE_HSE) +#define FUNCONF_USE_HSI 1 // Default to use HSI +#define FUNCONF_USE_HSE 0 #endif -#if defined( CH32X03x ) && FUNCONF_USE_HSE - #error No HSE in CH32X03x +#if defined(CH32X03x) && FUNCONF_USE_HSE +#error No HSE in CH32X03x #endif -#if !defined( FUNCONF_USE_PLL ) - #if defined( CH32X03x ) - #define FUNCONF_USE_PLL 0 // No PLL on X03x - #else - #define FUNCONF_USE_PLL 1 // Default to use PLL - #endif +#if !defined(FUNCONF_USE_PLL) +#if defined(CH32X03x) +#define FUNCONF_USE_PLL 0 // No PLL on X03x +#else +#define FUNCONF_USE_PLL 1 // Default to use PLL +#endif #endif -#if !defined( FUNCONF_DEBUG_HARDFAULT ) - #define FUNCONF_DEBUG_HARDFAULT 1 +#if !defined(FUNCONF_DEBUG_HARDFAULT) +#define FUNCONF_DEBUG_HARDFAULT 1 #endif -#if defined( CH32X03x ) && FUNCONF_USE_PLL - #error No PLL on the X03x +#if defined(CH32X03x) && FUNCONF_USE_PLL +#error No PLL on the X03x #endif #ifndef FUNCONF_ENABLE_HPE - #define FUNCONF_ENABLE_HPE 0 +#define FUNCONF_ENABLE_HPE 0 #endif #if FUNCONF_ENABLE_HPE == 1 - #define INTERRUPT_DECORATOR __attribute__((interrupt("WCH-Interrupt-fast"))) +#define INTERRUPT_DECORATOR __attribute__((interrupt("WCH-Interrupt-fast"))) #else - #define INTERRUPT_DECORATOR __attribute__((interrupt)) +#define INTERRUPT_DECORATOR __attribute__((interrupt)) #endif - -#if !defined( FUNCONF_USE_CLK_SEC ) - #define FUNCONF_USE_CLK_SEC 1// use clock security system by default +#if !defined(FUNCONF_USE_CLK_SEC) +#define FUNCONF_USE_CLK_SEC 1 // use clock security system by default #endif #ifndef HSE_VALUE - #if defined(CH32V003) - #define HSE_VALUE (24000000) // Value of the External oscillator in Hz, default - #elif defined(CH32V10x) - #define HSE_VALUE (8000000) - #elif defined(CH32V20x) - #if defined(CH32V20x_D8) || defined(CH32V20x_D8W) - #define HSE_VALUE (32000000) - #else - #define HSE_VALUE (8000000) - #endif - #elif defined(CH32V30x) - #define HSE_VALUE (8000000) - #endif +#if defined(CH32V003) +#define HSE_VALUE (24000000) // Value of the External oscillator in Hz, default +#elif defined(CH32V10x) +#define HSE_VALUE (8000000) +#elif defined(CH32V20x) +#if defined(CH32V20x_D8) || defined(CH32V20x_D8W) +#define HSE_VALUE (32000000) +#else +#define HSE_VALUE (8000000) +#endif +#elif defined(CH32V30x) +#define HSE_VALUE (8000000) +#endif #endif #ifndef HSI_VALUE - #if defined(CH32V003) - #define HSI_VALUE (24000000) // Value of the Internal oscillator in Hz, default. - #elif defined(CH32X03x) - #define HSI_VALUE (48000000) - #elif defined(CH32V10x) - #define HSI_VALUE (8000000) - #elif defined(CH32V20x) - #define HSI_VALUE (8000000) - #elif defined(CH32V30x) - #define HSI_VALUE (8000000) - #endif +#if defined(CH32V003) +#define HSI_VALUE (24000000) // Value of the Internal oscillator in Hz, default. +#elif defined(CH32X03x) +#define HSI_VALUE (48000000) +#elif defined(CH32V10x) +#define HSI_VALUE (8000000) +#elif defined(CH32V20x) +#define HSI_VALUE (8000000) +#elif defined(CH32V30x) +#define HSI_VALUE (8000000) +#endif #endif #ifndef FUNCONF_HSITRIM - #define FUNCONF_HSITRIM 0x10 // Default (Chip default) +#define FUNCONF_HSITRIM 0x10 // Default (Chip default) #endif #ifndef FUNCONF_USE_PLL - #define FUNCONF_USE_PLL 1 // Default, Use PLL. +#define FUNCONF_USE_PLL 1 // Default, Use PLL. #endif -#if !defined( FUNCONF_PLL_MULTIPLIER ) - #if defined(FUNCONF_USE_PLL) && FUNCONF_USE_PLL - #if defined(CH32V10x) - #define FUNCONF_PLL_MULTIPLIER 10 // Default: 8 * 10 = 80 MHz - #elif defined(CH32V20x) - #define FUNCONF_PLL_MULTIPLIER 18 // Default: 8 * 18 = 144 MHz - #elif defined(CH32V30x) - #define FUNCONF_PLL_MULTIPLIER 18 // Default: 8 * 18 = 144 MHz - #else // CH32V003 - #define FUNCONF_PLL_MULTIPLIER 2 // Default: 24 * 2 = 48 MHz - #endif - #else - #define FUNCONF_PLL_MULTIPLIER 1 - #endif +#if !defined(FUNCONF_PLL_MULTIPLIER) +#if defined(FUNCONF_USE_PLL) && FUNCONF_USE_PLL +#if defined(CH32V10x) +#define FUNCONF_PLL_MULTIPLIER 10 // Default: 8 * 10 = 80 MHz +#elif defined(CH32V20x) +#define FUNCONF_PLL_MULTIPLIER 18 // Default: 8 * 18 = 144 MHz +#elif defined(CH32V30x) +#define FUNCONF_PLL_MULTIPLIER 18 // Default: 8 * 18 = 144 MHz +#else // CH32V003 +#define FUNCONF_PLL_MULTIPLIER 2 // Default: 24 * 2 = 48 MHz +#endif +#else +#define FUNCONF_PLL_MULTIPLIER 1 +#endif #endif #ifndef FUNCONF_SYSTEM_CORE_CLOCK - #if defined(FUNCONF_USE_HSI) && FUNCONF_USE_HSI - #define FUNCONF_SYSTEM_CORE_CLOCK ((HSI_VALUE)*(FUNCONF_PLL_MULTIPLIER)) - #elif defined(FUNCONF_USE_HSE) && FUNCONF_USE_HSE - #define FUNCONF_SYSTEM_CORE_CLOCK ((HSE_VALUE)*(FUNCONF_PLL_MULTIPLIER)) - #else - #error Must define either FUNCONF_USE_HSI or FUNCONF_USE_HSE to be 1. - #endif +#if defined(FUNCONF_USE_HSI) && FUNCONF_USE_HSI +#define FUNCONF_SYSTEM_CORE_CLOCK ((HSI_VALUE) * (FUNCONF_PLL_MULTIPLIER)) +#elif defined(FUNCONF_USE_HSE) && FUNCONF_USE_HSE +#define FUNCONF_SYSTEM_CORE_CLOCK ((HSE_VALUE) * (FUNCONF_PLL_MULTIPLIER)) +#else +#error Must define either FUNCONF_USE_HSI or FUNCONF_USE_HSE to be 1. +#endif #endif #ifndef FUNCONF_USE_5V_VDD - #define FUNCONF_USE_5V_VDD 0 +#define FUNCONF_USE_5V_VDD 0 #endif // Default package for CH32V20x #if defined(CH32V20x) #if !defined(CH32V20x_D8W) && !defined(CH32V20x_D8) && !defined(CH32V20x_D6) - #define CH32V20x_D6 /* CH32V203F6-CH32V203F8-CH32V203G6-CH32V203G8-CH32V203K6-CH32V203K8-CH32V203C6-CH32V203C8 */ - //#define CH32V20x_D8 /* CH32V203RBT6 */ - //#define CH32V20x_D8W /* CH32V208 */ - #endif +#define CH32V20x_D6 /* CH32V203F6-CH32V203F8-CH32V203G6-CH32V203G8-CH32V203K6-CH32V203K8-CH32V203C6-CH32V203C8 */ +// #define CH32V20x_D8 /* CH32V203RBT6 */ +// #define CH32V20x_D8W /* CH32V208 */ +#endif #endif // Default package for CH32V30x #if defined(CH32V30x) #if !defined(CH32V30x_D8) && !defined(CH32V30x_D8C) - //#define CH32V30x_D8 /* CH32V303x */ - #define CH32V30x_D8C /* CH32V307x-CH32V305x */ - #endif +// #define CH32V30x_D8 /* CH32V303x */ +#define CH32V30x_D8C /* CH32V307x-CH32V305x */ +#endif #endif ///////////////////////////////////////////////////////////////////////////////////////////////// // Legacy, for EVT, CMSIS -#define __MPU_PRESENT 0 /* Other CH32 devices does not provide an MPU */ -#define __Vendor_SysTickConfig 0 /* Set to 1 if different SysTick Config is used */ +#define __MPU_PRESENT 0 /* Other CH32 devices does not provide an MPU */ +#define __Vendor_SysTickConfig 0 /* Set to 1 if different SysTick Config is used */ -#ifndef __ASSEMBLER__ // Things before this can be used in assembly. +#ifndef __ASSEMBLER__ // Things before this can be used in assembly. #include #ifdef __cplusplus - #define __I volatile /*!< defines 'read only' permissions */ +#define __I volatile /*!< defines 'read only' permissions */ #else - #define __I volatile const /*!< defines 'read only' permissions */ +#define __I volatile const /*!< defines 'read only' permissions */ #endif -#define __O volatile /*!< defines 'write only' permissions */ -#define __IO volatile /*!< defines 'read / write' permissions */ +#define __O volatile /*!< defines 'write only' permissions */ +#define __IO volatile /*!< defines 'read / write' permissions */ #endif // __ASSEMBLER__ - -/////////////////////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////// // Code in this section was originally from __CORE_RISCV_H__ @@ -261,491 +258,524 @@ /* define compiler specific symbols */ #if defined(__CC_ARM) - #define __ASM __asm /*!< asm keyword for ARM Compiler */ - #define __INLINE __inline /*!< inline keyword for ARM Compiler */ +#define __ASM __asm /*!< asm keyword for ARM Compiler */ +#define __INLINE __inline /*!< inline keyword for ARM Compiler */ #elif defined(__ICCARM__) - #define __ASM __asm /*!< asm keyword for IAR Compiler */ - #define __INLINE inline /*!< inline keyword for IAR Compiler. Only avaiable in High optimization mode! */ +#define __ASM __asm /*!< asm keyword for IAR Compiler */ +#define __INLINE inline /*!< inline keyword for IAR Compiler. Only avaiable in High optimization mode! */ #elif defined(__GNUC__) - #define __ASM __asm /*!< asm keyword for GNU Compiler */ - #define __INLINE inline /*!< inline keyword for GNU Compiler */ +#define __ASM __asm /*!< asm keyword for GNU Compiler */ +#define __INLINE inline /*!< inline keyword for GNU Compiler */ #elif defined(__TASKING__) - #define __ASM __asm /*!< asm keyword for TASKING Compiler */ - #define __INLINE inline /*!< inline keyword for TASKING Compiler */ +#define __ASM __asm /*!< asm keyword for TASKING Compiler */ +#define __INLINE inline /*!< inline keyword for TASKING Compiler */ #endif - #ifdef __cplusplus - extern "C" { +extern "C" +{ #endif - + #ifndef __ASSEMBLER__ -/* Standard Peripheral Library old types (maintained for legacy purpose) */ -typedef __I uint32_t vuc32; /* Read Only */ -typedef __I uint16_t vuc16; /* Read Only */ -typedef __I uint8_t vuc8; /* Read Only */ + /* Standard Peripheral Library old types (maintained for legacy purpose) */ + typedef __I uint32_t vuc32; /* Read Only */ + typedef __I uint16_t vuc16; /* Read Only */ + typedef __I uint8_t vuc8; /* Read Only */ -typedef const uint32_t uc32; /* Read Only */ -typedef const uint16_t uc16; /* Read Only */ -typedef const uint8_t uc8; /* Read Only */ + typedef const uint32_t uc32; /* Read Only */ + typedef const uint16_t uc16; /* Read Only */ + typedef const uint8_t uc8; /* Read Only */ -typedef __I int32_t vsc32; /* Read Only */ -typedef __I int16_t vsc16; /* Read Only */ -typedef __I int8_t vsc8; /* Read Only */ + typedef __I int32_t vsc32; /* Read Only */ + typedef __I int16_t vsc16; /* Read Only */ + typedef __I int8_t vsc8; /* Read Only */ -typedef const int32_t sc32; /* Read Only */ -typedef const int16_t sc16; /* Read Only */ -typedef const int8_t sc8; /* Read Only */ + typedef const int32_t sc32; /* Read Only */ + typedef const int16_t sc16; /* Read Only */ + typedef const int8_t sc8; /* Read Only */ -typedef __IO uint32_t vu32; -typedef __IO uint16_t vu16; -typedef __IO uint8_t vu8; + typedef __IO uint32_t vu32; + typedef __IO uint16_t vu16; + typedef __IO uint8_t vu8; -typedef uint32_t u32; -typedef uint16_t u16; -typedef uint8_t u8; + typedef uint32_t u32; + typedef uint16_t u16; + typedef uint8_t u8; -typedef __IO int32_t vs32; -typedef __IO int16_t vs16; -typedef __IO int8_t vs8; + typedef __IO int32_t vs32; + typedef __IO int16_t vs16; + typedef __IO int8_t vs8; -typedef int32_t s32; -typedef int16_t s16; -typedef int8_t s8; + typedef int32_t s32; + typedef int16_t s16; + typedef int8_t s8; -typedef __I uint64_t vuc64; /* Read Only */ -typedef const uint64_t uc64; /* Read Only */ -typedef __I int64_t vsc64; /* Read Only */ -typedef const int64_t sc64; /* Read Only */ -typedef __IO uint64_t vu64; -typedef uint64_t u64; -typedef __IO int64_t vs64; -typedef int64_t s64; + typedef __I uint64_t vuc64; /* Read Only */ + typedef const uint64_t uc64; /* Read Only */ + typedef __I int64_t vsc64; /* Read Only */ + typedef const int64_t sc64; /* Read Only */ + typedef __IO uint64_t vu64; + 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 +#define RV_STATIC_INLINE static inline #endif // __ASSEMBLER__ #ifdef CH32V003 - #include "ch32v003hw.h" -#elif defined( CH32X03x ) - #include "ch32x03xhw.h" -#elif defined( CH32X03x ) - #include "ch32x03xhw.h" -#elif defined( CH32V10x ) - #include "ch32v10xhw.h" -#elif defined( CH32V20x ) - #include "ch32v20xhw.h" -#elif defined( CH32V30x ) - #include "ch32v30xhw.h" +#include "ch32v003hw.h" +#elif defined(CH32X03x) +#include "ch32x03xhw.h" +#elif defined(CH32X03x) +#include "ch32x03xhw.h" +#elif defined(CH32V10x) +#include "ch32v10xhw.h" +#elif defined(CH32V20x) +#include "ch32v20xhw.h" +#elif defined(CH32V30x) +#include "ch32v30xhw.h" #endif -#if defined(__riscv) || defined(__riscv__) || defined( CH32V003FUN_BASE ) +#if defined(__riscv) || defined(__riscv__) || defined(CH32V003FUN_BASE) #if __GNUC__ > 10 - #define ADD_ARCH_ZICSR ".option arch, +zicsr\n" +#define ADD_ARCH_ZICSR ".option arch, +zicsr\n" #else - #define ADD_ARCH_ZICSR +#define ADD_ARCH_ZICSR #endif #ifndef __ASSEMBLER__ -// 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) ); -} + // 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)); + } -// 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) ); -} + // 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)); + } -// 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) ); - return (result & 0x08) != 0u; -} + // 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)); + 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)); - return result; -} + // 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)); + return result; + } -// nop -RV_STATIC_INLINE void __NOP() -{ - __ASM volatile( "nop" ); -} + // nop + RV_STATIC_INLINE void __NOP() + { + __ASM volatile("nop"); + } -// Enable Interrupt (by interrupt number) -RV_STATIC_INLINE void NVIC_EnableIRQ(IRQn_Type IRQn) -{ - NVIC->IENR[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); -} + // Enable Interrupt (by interrupt number) + RV_STATIC_INLINE void NVIC_EnableIRQ(IRQn_Type IRQn) + { + NVIC->IENR[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); + } -// Disable Interrupt (by interrupt number) -RV_STATIC_INLINE void NVIC_DisableIRQ(IRQn_Type IRQn) -{ - NVIC->IRER[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); -} + // Disable Interrupt (by interrupt number) + RV_STATIC_INLINE void NVIC_DisableIRQ(IRQn_Type IRQn) + { + NVIC->IRER[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); + } -// Get Interrupt Enable State, (by number), 1 = Triggered 0 = Not triggered -RV_STATIC_INLINE uint32_t NVIC_GetStatusIRQ(IRQn_Type IRQn) -{ - return((uint32_t) ((NVIC->ISR[(uint32_t)(IRQn) >> 5] & (1 << ((uint32_t)(IRQn) & 0x1F)))?1:0)); -} + // Get Interrupt Enable State, (by number), 1 = Triggered 0 = Not triggered + RV_STATIC_INLINE uint32_t NVIC_GetStatusIRQ(IRQn_Type IRQn) + { + return ((uint32_t)((NVIC->ISR[(uint32_t)(IRQn) >> 5] & (1 << ((uint32_t)(IRQn) & 0x1F))) ? 1 : 0)); + } -// Get Interrupt Pending State, (by number), 1 = Pending 0 = Not pending -RV_STATIC_INLINE uint32_t NVIC_GetPendingIRQ(IRQn_Type IRQn) -{ - return((uint32_t) ((NVIC->IPR[(uint32_t)(IRQn) >> 5] & (1 << ((uint32_t)(IRQn) & 0x1F)))?1:0)); -} + // Get Interrupt Pending State, (by number), 1 = Pending 0 = Not pending + RV_STATIC_INLINE uint32_t NVIC_GetPendingIRQ(IRQn_Type IRQn) + { + return ((uint32_t)((NVIC->IPR[(uint32_t)(IRQn) >> 5] & (1 << ((uint32_t)(IRQn) & 0x1F))) ? 1 : 0)); + } -// "current number break hang" -RV_STATIC_INLINE void NVIC_SetPendingIRQ(IRQn_Type IRQn) -{ - NVIC->IPSR[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); -} + // "current number break hang" + RV_STATIC_INLINE void NVIC_SetPendingIRQ(IRQn_Type IRQn) + { + NVIC->IPSR[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); + } -// Clear Interrupt Pending -RV_STATIC_INLINE void NVIC_ClearPendingIRQ(IRQn_Type IRQn) -{ - NVIC->IPRR[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); -} + // Clear Interrupt Pending + RV_STATIC_INLINE void NVIC_ClearPendingIRQ(IRQn_Type IRQn) + { + NVIC->IPRR[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); + } -// Get Interrupt Active State (returns 1 if active) -RV_STATIC_INLINE uint32_t NVIC_GetActive(IRQn_Type IRQn) -{ - return((uint32_t)((NVIC->IACTR[(uint32_t)(IRQn) >> 5] & (1 << ((uint32_t)(IRQn) & 0x1F)))?1:0)); -} + // Get Interrupt Active State (returns 1 if active) + RV_STATIC_INLINE uint32_t NVIC_GetActive(IRQn_Type IRQn) + { + return ((uint32_t)((NVIC->IACTR[(uint32_t)(IRQn) >> 5] & (1 << ((uint32_t)(IRQn) & 0x1F))) ? 1 : 0)); + } -// Set Interrupt Priority (priority: bit7: pre-emption priority, bit6: subpriority, bit[5-0]: reserved -RV_STATIC_INLINE void NVIC_SetPriority(IRQn_Type IRQn, uint8_t priority) -{ - NVIC->IPRIOR[(uint32_t)(IRQn)] = priority; -} + // Set Interrupt Priority (priority: bit7: pre-emption priority, bit6: subpriority, bit[5-0]: reserved + RV_STATIC_INLINE void NVIC_SetPriority(IRQn_Type IRQn, uint8_t priority) + { + NVIC->IPRIOR[(uint32_t)(IRQn)] = priority; + } -/********************************************************************* - * SUSPEND ALL INTERRUPTS EXCEPT - * The following 3 functions serve to suspend all interrupts, except for the one you momentarily need. - * The purpose of this is to not disturb the one interrupt of interest and let it run unimpeded. - * procedure: - * 1. save the enabled IRQs: uint32_t IRQ_backup = NVIC_get_enabled_IRQs(); - * 2. disable all IRQs: NVIC_clear_all_IRQs_except(IRQ_of_interest); - * 3. restore the previously enabled IRQs: NVIC_restore_IRQs(IRQ_backup); - * - * bit layout of the IRQ backup - * bit 0 | 1 | 2 | 3 | 4 | 5 | 6 .. 22 | 23 .. 28 - * IRQn 2 | 3 | 12 | res | 14 | res | 16 .. 31 | 32 .. 38 - * IRQn 2 and 3 aren't actually user-settable (see RM). - * - * Specifying an invalid IRQn_to_keep like 0 will disable all interrupts. - */ + /********************************************************************* + * SUSPEND ALL INTERRUPTS EXCEPT + * The following 3 functions serve to suspend all interrupts, except for the one you momentarily need. + * The purpose of this is to not disturb the one interrupt of interest and let it run unimpeded. + * procedure: + * 1. save the enabled IRQs: uint32_t IRQ_backup = NVIC_get_enabled_IRQs(); + * 2. disable all IRQs: NVIC_clear_all_IRQs_except(IRQ_of_interest); + * 3. restore the previously enabled IRQs: NVIC_restore_IRQs(IRQ_backup); + * + * bit layout of the IRQ backup + * bit 0 | 1 | 2 | 3 | 4 | 5 | 6 .. 22 | 23 .. 28 + * IRQn 2 | 3 | 12 | res | 14 | res | 16 .. 31 | 32 .. 38 + * IRQn 2 and 3 aren't actually user-settable (see RM). + * + * Specifying an invalid IRQn_to_keep like 0 will disable all interrupts. + */ -RV_STATIC_INLINE uint32_t NVIC_get_enabled_IRQs() -{ - return ( ((NVIC->ISR[0] >> 2) & 0b11) | ((NVIC->ISR[0] >> 12) << 2) | ((NVIC->ISR[1] & 0b1111111) << 23) ); -} + RV_STATIC_INLINE uint32_t NVIC_get_enabled_IRQs() + { + return (((NVIC->ISR[0] >> 2) & 0b11) | ((NVIC->ISR[0] >> 12) << 2) | ((NVIC->ISR[1] & 0b1111111) << 23)); + } -RV_STATIC_INLINE void NVIC_clear_all_IRQs_except(uint8_t IRQn_to_keep) -{ - if (!(IRQn_to_keep >> 5)) { // IRQn_to_keep < 32 - NVIC->IRER[0] = (~0) & (~(1 << IRQn_to_keep)); - NVIC->IRER[1] = (~0); - } - else { - IRQn_to_keep = IRQn_to_keep >> 5; - NVIC->IRER[0] = (~0); - NVIC->IRER[1] = (~0) & (~(1 << IRQn_to_keep)); - } -} + RV_STATIC_INLINE void NVIC_clear_all_IRQs_except(uint8_t IRQn_to_keep) + { + if (!(IRQn_to_keep >> 5)) + { // IRQn_to_keep < 32 + NVIC->IRER[0] = (~0) & (~(1 << IRQn_to_keep)); + NVIC->IRER[1] = (~0); + } + else + { + IRQn_to_keep = IRQn_to_keep >> 5; + NVIC->IRER[0] = (~0); + NVIC->IRER[1] = (~0) & (~(1 << IRQn_to_keep)); + } + } -RV_STATIC_INLINE void NVIC_restore_IRQs(uint32_t old_state) -{ - NVIC->IENR[0] = (old_state >> 2) << 12; - NVIC->IENR[1] = old_state >> 23; -} + RV_STATIC_INLINE void NVIC_restore_IRQs(uint32_t old_state) + { + NVIC->IENR[0] = (old_state >> 2) << 12; + NVIC->IENR[1] = old_state >> 23; + } -// WFI - wait for interrupt (like a light sleep) -__attribute__( ( always_inline ) ) RV_STATIC_INLINE void __WFI(void) -{ - NVIC->SCTLR &= ~(1<<3); // wfi - __ASM volatile ("wfi"); -} + // WFI - wait for interrupt (like a light sleep) + __attribute__((always_inline)) RV_STATIC_INLINE void __WFI(void) + { + NVIC->SCTLR &= ~(1 << 3); // wfi + __ASM volatile("wfi"); + } -// WFE - wait for events (more like a deeper sleep) -__attribute__( ( always_inline ) ) RV_STATIC_INLINE void __WFE(void) -{ - uint32_t t; + // WFE - wait for events (more like a deeper sleep) + __attribute__((always_inline)) RV_STATIC_INLINE void __WFE(void) + { + uint32_t t; - t = NVIC->SCTLR; - NVIC->SCTLR |= (1<<3)|(1<<5); // (wfi->wfe)+(__sev) - NVIC->SCTLR = (NVIC->SCTLR & ~(1<<5)) | ( t & (1<<5)); - __ASM volatile ("wfi"); - __ASM volatile ("wfi"); -} + t = NVIC->SCTLR; + NVIC->SCTLR |= (1 << 3) | (1 << 5); // (wfi->wfe)+(__sev) + NVIC->SCTLR = (NVIC->SCTLR & ~(1 << 5)) | (t & (1 << 5)); + __ASM volatile("wfi"); + __ASM volatile("wfi"); + } -/********************************************************************* - * @fn SetVTFIRQ - * @brief Set VTF Interrupt - * @param addr - VTF interrupt service function base address. - * IRQn - Interrupt Numbers - * num - VTF Interrupt Numbers - * NewState - DISABLE or ENABLE - * - * @return none - */ -RV_STATIC_INLINE void SetVTFIRQ(uint32_t addr, IRQn_Type IRQn, uint8_t num, FunctionalState NewState){ - if(num > 1) return ; + /********************************************************************* + * @fn SetVTFIRQ + * @brief Set VTF Interrupt + * @param addr - VTF interrupt service function base address. + * IRQn - Interrupt Numbers + * num - VTF Interrupt Numbers + * NewState - DISABLE or ENABLE + * + * @return none + */ + RV_STATIC_INLINE void SetVTFIRQ(uint32_t addr, IRQn_Type IRQn, uint8_t num, FunctionalState NewState) + { + if (num > 1) return; - if (NewState != DISABLE) - { - NVIC->VTFIDR[num] = IRQn; - NVIC->VTFADDR[num] = ((addr&0xFFFFFFFE)|0x1); - } - else{ - NVIC->VTFIDR[num] = IRQn; - NVIC->VTFADDR[num] = ((addr&0xFFFFFFFE)&(~0x1)); - } -} + if (NewState != DISABLE) + { + NVIC->VTFIDR[num] = IRQn; + NVIC->VTFADDR[num] = ((addr & 0xFFFFFFFE) | 0x1); + } + else + { + NVIC->VTFIDR[num] = IRQn; + NVIC->VTFADDR[num] = ((addr & 0xFFFFFFFE) & (~0x1)); + } + } -// Initiate a system reset request -RV_STATIC_INLINE void NVIC_SystemReset(void) -{ - NVIC->CFGR = NVIC_KEY3|(1<<7); -} + // Initiate a system reset request + RV_STATIC_INLINE void NVIC_SystemReset(void) + { + NVIC->CFGR = NVIC_KEY3 | (1 << 7); + } -// For reading INTSYSCR, for interrupt nesting + hardware stack enable. -static inline uint32_t __get_INTSYSCR(void) -{ - uint32_t result; - __ASM volatile( ADD_ARCH_ZICSR "csrr %0, 0x804": "=r"(result)); - return result; -} + // For reading INTSYSCR, for interrupt nesting + hardware stack enable. + static inline uint32_t __get_INTSYSCR(void) + { + uint32_t result; + __ASM volatile(ADD_ARCH_ZICSR "csrr %0, 0x804" : "=r"(result)); + return result; + } -// For setting INTSYSCR, for interrupt nesting + hardware stack enable. -static inline void __set_INTSYSCR( uint32_t value ) -{ - __ASM volatile( ADD_ARCH_ZICSR "csrw 0x804, %0" : : "r"(value)); -} + // For setting INTSYSCR, for interrupt nesting + hardware stack enable. + static inline void __set_INTSYSCR(uint32_t value) + { + __ASM volatile(ADD_ARCH_ZICSR "csrw 0x804, %0" : : "r"(value)); + } #if defined(CH32V30x) -// Return the Floating-Point Accrued Exceptions -static inline uint32_t __get_FFLAGS(void) -{ - uint32_t result; - __ASM volatile( ADD_ARCH_ZICSR "csrr %0," "fflags" : "=r" (result) ); - return (result); -} + // Return the Floating-Point Accrued Exceptions + static inline uint32_t __get_FFLAGS(void) + { + uint32_t result; + __ASM volatile(ADD_ARCH_ZICSR "csrr %0," + "fflags" : "=r"(result)); + return (result); + } -// Set the Floating-Point Accrued Exceptions -static inline void __set_FFLAGS(uint32_t value) -{ - __ASM volatile( ADD_ARCH_ZICSR "csrw fflags, %0" : : "r" (value) ); -} + // Set the Floating-Point Accrued Exceptions + static inline void __set_FFLAGS(uint32_t value) + { + __ASM volatile(ADD_ARCH_ZICSR "csrw fflags, %0" : : "r"(value)); + } -// Return the Floating-Point Dynamic Rounding Mode -static inline uint32_t __get_FRM(void) -{ - uint32_t result; - __ASM volatile( ADD_ARCH_ZICSR "csrr %0," "frm" : "=r" (result) ); - return (result); -} + // Return the Floating-Point Dynamic Rounding Mode + static inline uint32_t __get_FRM(void) + { + uint32_t result; + __ASM volatile(ADD_ARCH_ZICSR "csrr %0," + "frm" : "=r"(result)); + return (result); + } -// Set the Floating-Point Dynamic Rounding Mode -static inline void __set_FRM(uint32_t value) -{ - __ASM volatile( ADD_ARCH_ZICSR "csrw frm, %0" : : "r" (value) ); -} + // Set the Floating-Point Dynamic Rounding Mode + static inline void __set_FRM(uint32_t value) + { + __ASM volatile(ADD_ARCH_ZICSR "csrw frm, %0" : : "r"(value)); + } -// Return the Floating-Point Control and Status Register -static inline uint32_t __get_FCSR(void) -{ - uint32_t result; - __ASM volatile( ADD_ARCH_ZICSR "csrr %0," "fcsr" : "=r" (result) ); - return (result); -} + // Return the Floating-Point Control and Status Register + static inline uint32_t __get_FCSR(void) + { + uint32_t result; + __ASM volatile(ADD_ARCH_ZICSR "csrr %0," + "fcsr" : "=r"(result)); + return (result); + } -// Set the Floating-Point Dynamic Rounding Mode -static inline void __set_FCSR(uint32_t value) -{ - __ASM volatile( ADD_ARCH_ZICSR "csrw fcsr, %0" : : "r" (value) ); -} + // Set the Floating-Point Dynamic Rounding Mode + static inline void __set_FCSR(uint32_t value) + { + __ASM volatile(ADD_ARCH_ZICSR "csrw fcsr, %0" : : "r"(value)); + } #endif // CH32V30x -// Return the Machine Status Register (MSTATUS) -static inline uint32_t __get_MSTATUS(void) -{ - uint32_t result; - __ASM volatile( ADD_ARCH_ZICSR "csrr %0, mstatus": "=r"(result) ); - return (result); -} + // Return the Machine Status Register (MSTATUS) + static inline uint32_t __get_MSTATUS(void) + { + uint32_t result; + __ASM volatile(ADD_ARCH_ZICSR "csrr %0, mstatus" : "=r"(result)); + return (result); + } -// Set the Machine Status Register (MSTATUS) -static inline void __set_MSTATUS(uint32_t value) -{ - __ASM volatile( ADD_ARCH_ZICSR "csrw mstatus, %0" : : "r"(value) ); -} + // Set the Machine Status Register (MSTATUS) + static inline void __set_MSTATUS(uint32_t value) + { + __ASM volatile(ADD_ARCH_ZICSR "csrw mstatus, %0" : : "r"(value)); + } -// Return the Machine ISA Register (MISA) -static inline uint32_t __get_MISA(void) -{ - uint32_t result; - __ASM volatile( ADD_ARCH_ZICSR "csrr %0, misa" : "=r"(result)); - return (result); -} + // Return the Machine ISA Register (MISA) + static inline uint32_t __get_MISA(void) + { + uint32_t result; + __ASM volatile(ADD_ARCH_ZICSR "csrr %0, misa" : "=r"(result)); + return (result); + } -// Set the Machine ISA Register (MISA) -static inline void __set_MISA(uint32_t value) -{ - __ASM volatile( ADD_ARCH_ZICSR "csrw misa, %0" : : "r"(value)); -} + // Set the Machine ISA Register (MISA) + static inline void __set_MISA(uint32_t value) + { + __ASM volatile(ADD_ARCH_ZICSR "csrw misa, %0" : : "r"(value)); + } -// Return the Machine Trap-Vector Base-Address Register (MTVEC) -static inline uint32_t __get_MTVEC(void) -{ - uint32_t result; - __ASM volatile( ADD_ARCH_ZICSR "csrr %0," "mtvec": "=r"(result)); - return (result); -} + // Return the Machine Trap-Vector Base-Address Register (MTVEC) + static inline uint32_t __get_MTVEC(void) + { + uint32_t result; + __ASM volatile(ADD_ARCH_ZICSR "csrr %0," + "mtvec" : "=r"(result)); + return (result); + } -// * @brief Set the Machine Trap-Vector Base-Address Register (MTVEC) -static inline void __set_MTVEC(uint32_t value) -{ - __ASM volatile( ADD_ARCH_ZICSR "csrw mtvec, %0":: "r"(value)); -} + // * @brief Set the Machine Trap-Vector Base-Address Register (MTVEC) + static inline void __set_MTVEC(uint32_t value) + { + __ASM volatile(ADD_ARCH_ZICSR "csrw mtvec, %0" ::"r"(value)); + } -// Return the Machine Seratch Register (MSCRATCH) -static inline uint32_t __get_MSCRATCH(void) -{ - uint32_t result; - __ASM volatile( ADD_ARCH_ZICSR "csrr %0," "mscratch" : "=r"(result)); - return (result); -} + // Return the Machine Seratch Register (MSCRATCH) + static inline uint32_t __get_MSCRATCH(void) + { + uint32_t result; + __ASM volatile(ADD_ARCH_ZICSR "csrr %0," + "mscratch" : "=r"(result)); + return (result); + } -// Set the Machine Seratch Register (MSRATCH) -static inline void __set_MSCRATCH(uint32_t value) -{ - __ASM volatile( ADD_ARCH_ZICSR "csrw mscratch, %0" : : "r"(value)); -} + // Set the Machine Seratch Register (MSRATCH) + static inline void __set_MSCRATCH(uint32_t value) + { + __ASM volatile(ADD_ARCH_ZICSR "csrw mscratch, %0" : : "r"(value)); + } -// Return the Machine Exception Program Register (MEPC) -static inline uint32_t __get_MEPC(void) -{ - uint32_t result; - __ASM volatile( ADD_ARCH_ZICSR "csrr %0," "mepc" : "=r"(result)); - return (result); -} + // Return the Machine Exception Program Register (MEPC) + static inline uint32_t __get_MEPC(void) + { + uint32_t result; + __ASM volatile(ADD_ARCH_ZICSR "csrr %0," + "mepc" : "=r"(result)); + return (result); + } -// Set the Machine Exception Program Register (MEPC) -static inline void __set_MEPC(uint32_t value) -{ - __ASM volatile( ADD_ARCH_ZICSR "csrw mepc, %0" : : "r"(value)); -} + // Set the Machine Exception Program Register (MEPC) + static inline void __set_MEPC(uint32_t value) + { + __ASM volatile(ADD_ARCH_ZICSR "csrw mepc, %0" : : "r"(value)); + } -// Return the Machine Cause Register (MCAUSE) -static inline uint32_t __get_MCAUSE(void) -{ - uint32_t result; - __ASM volatile( ADD_ARCH_ZICSR "csrr %0," "mcause": "=r"(result)); - return (result); -} + // Return the Machine Cause Register (MCAUSE) + static inline uint32_t __get_MCAUSE(void) + { + uint32_t result; + __ASM volatile(ADD_ARCH_ZICSR "csrr %0," + "mcause" : "=r"(result)); + return (result); + } -// Set the Machine Cause Register (MCAUSE) -static inline void __set_MCAUSE(uint32_t value) -{ - __ASM volatile( ADD_ARCH_ZICSR "csrw mcause, %0":: "r"(value)); -} + // Set the Machine Cause Register (MCAUSE) + static inline void __set_MCAUSE(uint32_t value) + { + __ASM volatile(ADD_ARCH_ZICSR "csrw mcause, %0" ::"r"(value)); + } -// Return the Machine Trap Value Register (MTVAL) -static inline uint32_t __get_MTVAL(void) -{ - uint32_t result; - __ASM volatile( ADD_ARCH_ZICSR "csrr %0," "mtval" : "=r" (result) ); - return (result); -} + // Return the Machine Trap Value Register (MTVAL) + static inline uint32_t __get_MTVAL(void) + { + uint32_t result; + __ASM volatile(ADD_ARCH_ZICSR "csrr %0," + "mtval" : "=r"(result)); + return (result); + } -// Set the Machine Trap Value Register (MTVAL) -static inline void __set_MTVAL(uint32_t value) -{ - __ASM volatile ( ADD_ARCH_ZICSR "csrw mtval, %0" : : "r" (value) ); -} + // Set the Machine Trap Value Register (MTVAL) + static inline void __set_MTVAL(uint32_t value) + { + __ASM volatile(ADD_ARCH_ZICSR "csrw mtval, %0" : : "r"(value)); + } -// Return Vendor ID Register (MVENDORID) -static inline uint32_t __get_MVENDORID(void) -{ - uint32_t result; - __ASM volatile( ADD_ARCH_ZICSR "csrr %0, mvendorid": "=r"(result)); - return (result); -} + // Return Vendor ID Register (MVENDORID) + static inline uint32_t __get_MVENDORID(void) + { + uint32_t result; + __ASM volatile(ADD_ARCH_ZICSR "csrr %0, mvendorid" : "=r"(result)); + return (result); + } -// Return Machine Architecture ID Register (MARCHID) -static inline uint32_t __get_MARCHID(void) -{ - uint32_t result; - __ASM volatile( ADD_ARCH_ZICSR "csrr %0, marchid": "=r"(result)); - return (result); -} + // Return Machine Architecture ID Register (MARCHID) + static inline uint32_t __get_MARCHID(void) + { + uint32_t result; + __ASM volatile(ADD_ARCH_ZICSR "csrr %0, marchid" : "=r"(result)); + return (result); + } -// Return Machine Implementation ID Register (MIPID) -static inline uint32_t __get_MIMPID(void) -{ - uint32_t result; - __ASM volatile( ADD_ARCH_ZICSR "csrr %0, mimpid": "=r"(result)); - return (result); -} + // Return Machine Implementation ID Register (MIPID) + static inline uint32_t __get_MIMPID(void) + { + uint32_t result; + __ASM volatile(ADD_ARCH_ZICSR "csrr %0, mimpid" : "=r"(result)); + return (result); + } -// Return Hart ID Register MHARTID -static inline uint32_t __get_MHARTID(void) -{ - uint32_t result; - __ASM volatile( ADD_ARCH_ZICSR "csrr %0, mhartid": "=r"(result)); - return (result); -} + // Return Hart ID Register MHARTID + static inline uint32_t __get_MHARTID(void) + { + uint32_t result; + __ASM volatile(ADD_ARCH_ZICSR "csrr %0, mhartid" : "=r"(result)); + return (result); + } #if defined(CH32V003) && CH32V003 -// Return DBGMCU_CR Register value -static inline uint32_t __get_DEBUG_CR(void) -{ - uint32_t result; - __ASM volatile( ADD_ARCH_ZICSR "csrr %0, 0x7C0" : "=r" (result) ); - return (result); -} + // Return DBGMCU_CR Register value + static inline uint32_t __get_DEBUG_CR(void) + { + uint32_t result; + __ASM volatile(ADD_ARCH_ZICSR "csrr %0, 0x7C0" : "=r"(result)); + return (result); + } + // Set the DBGMCU_CR Register value + static inline void __set_DEBUG_CR(uint32_t value) + { + __ASM volatile(ADD_ARCH_ZICSR "csrw 0x7C0, %0" : : "r"(value)); + } -// Set the DBGMCU_CR Register value -static inline void __set_DEBUG_CR(uint32_t value) -{ - __ASM volatile( ADD_ARCH_ZICSR "csrw 0x7C0, %0" : : "r" (value) ); -} - -// Return stack pointer register (SP) -static inline uint32_t __get_SP(void) -{ - uint32_t result; - __ASM volatile( "mv %0,""sp": "=r"(result):); - return (result); -} + // Return stack pointer register (SP) + static inline uint32_t __get_SP(void) + { + uint32_t result; + __ASM volatile("mv %0," + "sp" : "=r"(result) :); + return (result); + } #endif // CH32V003 #endif // !assembler @@ -753,27 +783,27 @@ static inline uint32_t __get_SP(void) // _JBTYPE using long long to make sure the alignment is align to 8 byte, // otherwise in rv32imafd, store/restore FPR may mis-align. #define _JBTYPE long long -#if defined( __riscv_abi_rve ) -#define _JBLEN ((4*sizeof(long))/sizeof(long)) -#elif defined( __riscv_float_abi_double ) -#define _JBLEN ((14*sizeof(long) + 12*sizeof(double))/sizeof(long)) -#elif defined( __riscv_float_abi_single ) -#define _JBLEN ((14*sizeof(long) + 12*sizeof(float))/sizeof(long)) +#if defined(__riscv_abi_rve) +#define _JBLEN ((4 * sizeof(long)) / sizeof(long)) +#elif defined(__riscv_float_abi_double) +#define _JBLEN ((14 * sizeof(long) + 12 * sizeof(double)) / sizeof(long)) +#elif defined(__riscv_float_abi_single) +#define _JBLEN ((14 * sizeof(long) + 12 * sizeof(float)) / sizeof(long)) #else -#define _JBLEN ((14*sizeof(long))/sizeof(long)) +#define _JBLEN ((14 * sizeof(long)) / sizeof(long)) #endif #ifndef __ASSEMBLER__ #ifdef _JBLEN #ifdef _JBTYPE -typedef _JBTYPE jmp_buf[_JBLEN]; + typedef _JBTYPE jmp_buf[_JBLEN]; #else -typedef int jmp_buf[_JBLEN]; + typedef int jmp_buf[_JBLEN]; #endif // _JBTYPE #endif // _JBLEN -int setjmp( jmp_buf env ); -void longjmp( jmp_buf env, int val ); + int setjmp(jmp_buf env); + void longjmp(jmp_buf env, int val); #endif #endif // defined(__riscv) || defined(__riscv__) || defined( CH32V003FUN_BASE ) @@ -782,71 +812,103 @@ void longjmp( jmp_buf env, int val ); } #endif -#endif/* __CORE_RISCV_H__ */ - +#endif /* __CORE_RISCV_H__ */ /////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif -/* SYSTICK info - * time on the ch32v003 is kept by the SysTick counter (32bit) - * by default, it will operate at (FUNCONF_SYSTEM_CORE_CLOCK / 8) = 6MHz - * more info at https://github.com/cnlohr/ch32v003fun/wiki/Time -*/ + /* SYSTICK info + * time on the ch32v003 is kept by the SysTick counter (32bit) + * by default, it will operate at (FUNCONF_SYSTEM_CORE_CLOCK / 8) = 6MHz + * more info at https://github.com/cnlohr/ch32v003fun/wiki/Time + */ -#if defined( FUNCONF_SYSTICK_USE_HCLK ) && FUNCONF_SYSTICK_USE_HCLK && !defined(CH32V10x) -#define DELAY_US_TIME ((FUNCONF_SYSTEM_CORE_CLOCK)/1000000) -#define DELAY_MS_TIME ((FUNCONF_SYSTEM_CORE_CLOCK)/1000) +#if defined(FUNCONF_SYSTICK_USE_HCLK) && FUNCONF_SYSTICK_USE_HCLK && !defined(CH32V10x) +#define DELAY_US_TIME ((FUNCONF_SYSTEM_CORE_CLOCK) / 1000000) +#define DELAY_MS_TIME ((FUNCONF_SYSTEM_CORE_CLOCK) / 1000) #else // Use systick = hclk/8 -#define DELAY_US_TIME ((FUNCONF_SYSTEM_CORE_CLOCK)/8000000) -#define DELAY_MS_TIME ((FUNCONF_SYSTEM_CORE_CLOCK)/8000) +#define DELAY_US_TIME ((FUNCONF_SYSTEM_CORE_CLOCK) / 8000000) +#define DELAY_MS_TIME ((FUNCONF_SYSTEM_CORE_CLOCK) / 8000) #endif -#define Delay_Us(n) DelaySysTick( (n) * DELAY_US_TIME ) -#define Delay_Ms(n) DelaySysTick( (n) * DELAY_MS_TIME ) +#define Delay_Us(n) DelaySysTick((n) * DELAY_US_TIME) +#define Delay_Ms(n) DelaySysTick((n) * DELAY_MS_TIME) -#define Ticks_from_Us(n) (n * DELAY_US_TIME) -#define Ticks_from_Ms(n) (n * DELAY_MS_TIME) +#define Ticks_from_Us(n) (n * DELAY_US_TIME) +#define Ticks_from_Ms(n) (n * DELAY_MS_TIME) // Add a certain number of nops. Note: These are usually executed in pairs // and take two cycles, so you typically would use 0, 2, 4, etc. -#define ADD_N_NOPS( n ) asm volatile( ".rept " #n "\nc.nop\n.endr" ); +#define ADD_N_NOPS(n) asm volatile(".rept " #n "\nc.nop\n.endr"); // Arduino-like GPIO Functionality -#define GpioOf( pin ) ((GPIO_TypeDef *)(GPIOA_BASE + 0x400 * ((pin)>>4))) +#define GpioOf(pin) ((GPIO_TypeDef *)(GPIOA_BASE + 0x400 * ((pin) >> 4))) #define FUN_HIGH 0x1 #define FUN_LOW 0x0 #define FUN_OUTPUT (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP) #define FUN_INPUT (GPIO_CNF_IN_FLOATING) -// For pins, use things like PA8, PB15 -// For configuration, use things like GPIO_CFGLR_OUT_10Mhz_PP + // 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 funDigitalRead( pin ) ((int)((GpioOf(pin)->INDR >> ((pin)&0xf)) & 1)) - +#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 @@ -863,65 +925,64 @@ extern "C" { #ifndef __ASSEMBLER__ -#if defined(__riscv) || defined(__riscv__) || defined( CH32V003FUN_BASE ) +#if defined(__riscv) || defined(__riscv__) || defined(CH32V003FUN_BASE) + // Stuff that can only be compiled on device (not for the programmer, or other host programs) -// 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. + void funAnalogInit(void); -// Initialize the ADC calibrate it and set some sane defaults. -void funAnalogInit( void ); + // Read an analog input (not a GPIO pin number) + // Be sure to call funAnalogInit first. + int funAnalogRead(int nAnalogNumber); -// Read an analog input (not a GPIO pin number) -// Be sure to call funAnalogInit first. -int funAnalogRead( int nAnalogNumber ); - -void handle_reset() __attribute__((naked)) __attribute((section(".text.handle_reset"))) __attribute__((used)); -void DefaultIRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute__((naked)) __attribute__((used)); + void handle_reset() __attribute__((naked)) __attribute((section(".text.handle_reset"))) __attribute__((used)); + void DefaultIRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute__((naked)) __attribute__((used)); // used to clear the CSS flag in case of clock fail switch #if defined(FUNCONF_USE_CLK_SEC) && FUNCONF_USE_CLK_SEC - void NMI_RCC_CSS_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute__((naked)) __attribute__((used)); + void NMI_RCC_CSS_IRQHandler(void) __attribute__((section(".text.vector_handler"))) __attribute__((naked)) __attribute__((used)); #endif -void DelaySysTick( uint32_t n ); - + 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 ) { - __ASM volatile( "\ + static inline void Delay_Tiny(int n) + { + __ASM volatile("\ mv a5, %[n]\n\ 1: \ c.addi a5, -1\n\ - c.bnez a5, 1b" : : [n]"r"(n) : "a5" ); -} + c.bnez a5, 1b" : : [n] "r"(n) : "a5"); + } #endif #endif -#endif //defined(__riscv) || defined(__riscv__) || defined( CH32V003FUN_BASE ) +#endif // defined(__riscv) || defined(__riscv__) || defined( CH32V003FUN_BASE ) -// Tricky: We need to make sure main and SystemInit() are preserved. -int main() __attribute__((used)); -void SystemInit(void); + // Tricky: We need to make sure main and SystemInit() are preserved. + int main() __attribute__((used)); + void SystemInit(void); #ifdef FUNCONF_UART_PRINTF_BAUD - #define UART_BAUD_RATE FUNCONF_UART_PRINTF_BAUD +#define UART_BAUD_RATE FUNCONF_UART_PRINTF_BAUD #else - #define UART_BAUD_RATE 115200 +#define UART_BAUD_RATE 115200 #endif // Debug UART baud rate register calculation. Works assuming HCLK prescaler is off. // Computes UART_BRR = CORE_CLOCK / BAUD_RATE with rounding to closest integer -#define UART_BRR (((FUNCONF_SYSTEM_CORE_CLOCK) + (UART_BAUD_RATE)/2) / (UART_BAUD_RATE)) -// Put an output debug UART on Pin D5. -// You can write to this with printf(...) or puts(...) +#define UART_BRR (((FUNCONF_SYSTEM_CORE_CLOCK) + (UART_BAUD_RATE) / 2) / (UART_BAUD_RATE)) + // Put an output debug UART on Pin D5. + // You can write to this with printf(...) or puts(...) -void SetupUART( int uartBRR ); + void SetupUART(int uartBRR); -// Returns 1 if timeout reached, 0 otherwise. -// If timeout_ms == 0, wait indefinitely. -// Use DidDebuggerAttach() For a zero-wait way of seeing if it attached. -int WaitForDebuggerToAttach( int timeout_ms ); + // Returns 1 if timeout reached, 0 otherwise. + // If timeout_ms == 0, wait indefinitely. + // Use DidDebuggerAttach() For a zero-wait way of seeing if it attached. + int WaitForDebuggerToAttach(int timeout_ms); // Returns 1 if a debugger has activated the debug module. #define DidDebuggerAttach() (!*DMSTATUS_SENTINEL) @@ -929,67 +990,63 @@ int WaitForDebuggerToAttach( int timeout_ms ); // Returns 1 if a debugger has activated the debug module. #define DebugPrintfBufferFree() (!(*DMDATA0 & 0x80)) -// Just a definition to the internal _write function. -int _write(int fd, const char *buf, int size); + // Just a definition to the internal _write function. + int _write(int fd, const char *buf, int size); -// Call this to busy-wait the polling of input. -void poll_input( void ); - -// Receiving bytes from host. Override if you wish. -void handle_debug_input( int numbytes, uint8_t * data ); + // Call this to busy-wait the polling of input. + 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 -int mini_vsnprintf( char *buffer, unsigned int buffer_len, 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 ); -int mini_snprintf(char* buffer, unsigned int buffer_len, const char *fmt, ...); -int mini_pprintf(int (*puts)(char*s, int len, void* buf), void* buf, const char *fmt, ...); + int mini_vsnprintf(char *buffer, unsigned int buffer_len, 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); + int mini_snprintf(char *buffer, unsigned int buffer_len, const char *fmt, ...); + int mini_pprintf(int (*puts)(char *s, int len, void *buf), void *buf, const char *fmt, ...); #endif // __ASSEMBLER__ - -/* - * This file contains various parts of the official WCH EVT Headers which - * were originally under a restrictive license. - * - * The collection of this file was generated by - * cnlohr, 2023-02-18 and - * AlexanderMandera, 2023-06-23 - * It was significantly reworked into several files cnlohr, 2025-01-29 - * - * While originally under a restrictive copyright, WCH has approved use - * under MIT-licensed use, because of inclusion in Zephyr, as well as other - * open-source licensed projects. - * - * These copies of the headers from WCH are available now under: - * - * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the “Software”), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ + /* + * This file contains various parts of the official WCH EVT Headers which + * were originally under a restrictive license. + * + * The collection of this file was generated by + * cnlohr, 2023-02-18 and + * AlexanderMandera, 2023-06-23 + * It was significantly reworked into several files cnlohr, 2025-01-29 + * + * While originally under a restrictive copyright, WCH has approved use + * under MIT-licensed use, because of inclusion in Zephyr, as well as other + * open-source licensed projects. + * + * These copies of the headers from WCH are available now under: + * + * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the “Software”), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ #ifdef __cplusplus }; #endif - #endif // __CH32FUN_H - diff --git a/src/funconfig.h b/src/funconfig.h index 998cf76..dd78e96 100644 --- a/src/funconfig.h +++ b/src/funconfig.h @@ -1,7 +1,6 @@ #ifndef _FUNCONFIG_H #define _FUNCONFIG_H -#define CH32V003 1 +#define CH32V003 1 #endif -