增加了串口调试和xprintf功能

This commit is contained in:
ShowerXL
2021-10-07 16:13:56 +08:00
parent b94d12c2d5
commit 4b8994464e
59 changed files with 7005 additions and 11160 deletions
@@ -0,0 +1,188 @@
#include <stdio.h>
#include "Mini58Series.h"
#include "myUart.h"
#if USE_UART0
static volatile struct {
uint16_t tri, twi, tct;
uint16_t rri, rwi, rct;
uint8_t tbuf[UART0_TXB];
uint8_t rbuf[UART0_RXB];
} Fifo1;
void UART0_IRQHandler (void)
{
uint8_t d;
int i;
if (UART_GET_INT_FLAG(UART0,UART_INTEN_RDAIEN_Msk)) { /* RXNE is set: Rx ready */
d = UART_READ(UART0); /* Get received byte */
i = Fifo1.rct;
if (i < UART0_RXB) { /* Store it into the rx fifo if not full */
Fifo1.rct = ++i;
i = Fifo1.rwi;
Fifo1.rbuf[i] = d;
Fifo1.rwi = ++i % UART0_RXB;
}
}
if (UART_GET_INT_FLAG(UART0,UART_INTEN_THREIEN_Msk)) { /* TXE is set: Tx ready */
i = Fifo1.tct;
if (i--) { /* There is any data in the tx fifo */
Fifo1.tct = (uint16_t)i;
i = Fifo1.tri;
UART_WRITE(UART0,Fifo1.tbuf[i]);
Fifo1.tri = ++i % UART0_TXB;
} else { /* No data in the tx fifo */
UART_DISABLE_INT(UART0,UART_INTEN_THREIEN_Msk); /* Clear TXEIE - Disable TXE irq */
}
}
}
int uart0_test (void)
{
return Fifo1.rct;
}
uint8_t uart0_getc (void)
{
uint8_t d;
int i;
/* Wait while rx fifo is empty */
while (!Fifo1.rct) ;
i = Fifo1.rri; /* Get a byte from rx fifo */
d = Fifo1.rbuf[i];
Fifo1.rri = ++i % UART0_RXB;
NVIC_DisableIRQ(UART0_IRQn);
Fifo1.rct--;
NVIC_EnableIRQ(UART0_IRQn);
return d;
}
void uart0_putc (uint8_t d)
{
int i;
/* Wait for tx fifo is not full */
while (Fifo1.tct >= UART0_TXB) ;
i = Fifo1.twi; /* Put a byte into Tx fifo */
Fifo1.tbuf[i] = d;
Fifo1.twi = ++i % UART0_TXB;
NVIC_DisableIRQ(UART0_IRQn);
Fifo1.tct++;
UART_ENABLE_INT(UART0,UART_INTEN_THREIEN_Msk); /* Set TXEIE - Enable TXE irq */
NVIC_EnableIRQ(UART0_IRQn);
}
void xprintf_init (uint32_t bps)
{
/* Init uart0 */
CLK_EnableModuleClock(UART0_MODULE);
#ifdef UART_USE_P46_P47
SYS->P4_MFP = SYS_MFP_P46_UART1_RXD | SYS_MFP_P47_UART1_TXD;
GPIO_ENABLE_DIGITAL_PATH(P4,(1<<6));
#else
SYS->P5_MFP = SYS_MFP_P51_UART0_RXD | SYS_MFP_P50_UART0_TXD;
GPIO_ENABLE_DIGITAL_PATH(P5,(1<<1));
#endif
UART_Open(UART0, bps);
UART_ENABLE_INT(UART0, (UART_INTEN_RDAIEN_Msk | UART_INTEN_RXTOIEN_Msk /*| UART_INTEN_THREIEN_Msk*/));
NVIC_EnableIRQ(UART0_IRQn);
/* Clear Tx/Rx fifo */
Fifo1.tri = 0; Fifo1.twi = 0; Fifo1.tct = 0;
Fifo1.rri = 0; Fifo1.rwi = 0; Fifo1.rct = 0;
/* init xprintf with func */
xdev_out(uart0_putc);
xdev_in(uart0_getc);
}
#endif /* USE_UART0 */
#if USE_ERROUT
/* Error_out */
void Error_out (uint8_t rc)
{
const char *str =
"OK\0" "AD0_ERR\0" "AD1\0" "AD2\0" "AD3\0" "NO_PATH\0"
"INVALID_NAME\0" "DENIED\0" "EXIST\0" "INVALID_OBJECT\0" "WRITE_PROTECTED\0"
"INVALID_DRIVE\0" "NOT_ENABLED\0" "NO_FILE_SYSTEM\0" "MKFS_ABORTED\0" "TIMEOUT\0"
"LOCKED\0" "NOT_ENOUGH_CORE\0" "TOO_MANY_OPEN_FILES\0" "INVALID_PARAMETER\0";
uint8_t i;
for (i = 0; i != rc && *str; i++) {
while (*str++) ;
}
xprintf("Error=%u %s", (uint8_t)rc, str);
}
// wait_input
char Line[16]; /* Console input buffer */
void Uart_wait_input(void)
{
char *ptr;
ptr = Line;
switch (*ptr++) {
case '?' : /* Show Command List */
xputs(CmdList);
break;
case 'S' : /* test step */
case 's' :
switch (*ptr++) {
case '0' : /* S0 */
Error_out(0);
break;
case '1' : /* S1 */
Error_out(1);
break;
case '2' : /* S2 */
Error_out(2);
break;
case '3' : /* S3 */
Error_out(3);
break;
case '4' : /* S4 */
Error_out(4);
break;
case '5' : /* S5 */
Error_out(5);
break;
case '6' : /* S6 */
Error_out(6);
break;
case '7' : /* S7 */
Error_out(7);
break;
case '8' : /* S8 */
Error_out(8);
break;
case '9' : /* S9 */
Error_out(9);
break;
default:
Error_out(10);
break;
}
}
}
#endif
@@ -0,0 +1,51 @@
#ifndef _UART_DEFS
#define _UART_DEFS
#include <stdint.h>
#include "xprintf.h"
//#define USE_UART_C 1
#define USE_UART0 1 /* 0:Disable, 1:Enable, 2:Enable w/remap */
#define UART0_RXB 32 /* Size of Rx buffer */
#define UART0_TXB 32 /* Size of Tx buffer */
#if USE_UART0
void xprintf_init(uint32_t bps);
//int uart0_test(void);
//void uart0_putc(uint8_t d);
//uint8_t uart0_getc(void);
#endif
#define USE_ERROUT 1
#if USE_ERROUT
extern char Line[16];
void Uart_wait_input(void);
void Error_out (uint8_t rc);
static
const char Banner[] =
"***********************************************\n"
" ### ####### ### # # ####### # # \n"
" # # # # # # # # \n"
" # # # # # # # # \n"
" # ##### # # # ##### # \n"
" # # # # # # # # \n"
" # # # # # # # # \n"
" ### # ### # ####### # # \n"
"***********************************************\n";
static
const char CmdList[] =
"S0 - ReInit to test\n"
"S1 - Test step one\n"
"S2 - Test step two\n"
"S3 - Test step three\n"
"S4 - Test step four\n"
"S5 - Test step five\n"
"S6 - Test step six\n"
"S7 - Test step seven\n"
"S8 - Test step eight\n"
"S9 - Test step nine\n";
#endif
#endif
@@ -0,0 +1,406 @@
/*------------------------------------------------------------------------/
/ Universal string handler for user console interface
/-------------------------------------------------------------------------/
/
/ Copyright (C) 2011, ChaN, all right reserved.
/
/ * This software is a free software and there is NO WARRANTY.
/ * No restriction on use. You can use, modify and redistribute it for
/ personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
/ * Redistributions of source code must retain the above copyright notice.
/
/-------------------------------------------------------------------------*/
#include "xprintf.h"
#if _USE_XFUNC_OUT
#include <stdarg.h>
void (*xfunc_out)(unsigned char); /* Pointer to the output stream */
static char *outptr;
/*----------------------------------------------*/
/* Put a character */
/*----------------------------------------------*/
void xputc (char c)
{
if (_CR_CRLF && c == '\n') xputc('\r'); /* CR -> CRLF */
if (outptr) {
*outptr++ = (unsigned char)c;
return;
}
if (xfunc_out) xfunc_out((unsigned char)c);
}
/*----------------------------------------------*/
/* Put a null-terminated string */
/*----------------------------------------------*/
void xputs ( /* Put a string to the default device */
const char* str /* Pointer to the string */
)
{
while (*str)
xputc(*str++);
}
void xfputs ( /* Put a string to the specified device */
void(*func)(unsigned char), /* Pointer to the output function */
const char* str /* Pointer to the string */
)
{
void (*pf)(unsigned char);
pf = xfunc_out; /* Save current output device */
xfunc_out = func; /* Switch output to specified device */
while (*str) /* Put the string */
xputc(*str++);
xfunc_out = pf; /* Restore output device */
}
/*----------------------------------------------*/
/* Formatted string output */
/*----------------------------------------------*/
/* xprintf("%d", 1234); "1234"
xprintf("%6d,%3d%%", -200, 5); " -200, 5%"
xprintf("%-6u", 100); "100 "
xprintf("%ld", 12345678L); "12345678"
xprintf("%04x", 0xA3); "00a3"
xprintf("%08LX", 0x123ABC); "00123ABC"
xprintf("%016b", 0x550F); "0101010100001111"
xprintf("%s", "String"); "String"
xprintf("%-4s", "abc"); "abc "
xprintf("%4s", "abc"); " abc"
xprintf("%c", 'a'); "a"
xprintf("%f", 10.0); <xprintf lacks floating point support>
*/
static
void xvprintf (
const char* fmt, /* Pointer to the format string */
va_list arp /* Pointer to arguments */
)
{
unsigned int r, i, j, w, f;
unsigned long v;
char s[16], c, d, *p;
for (;;) {
c = *fmt++; /* Get a char */
if (!c) break; /* End of format? */
if (c != '%') { /* Pass through it if not a % sequense */
xputc(c); continue;
}
f = 0;
c = *fmt++; /* Get first char of the sequense */
if (c == '0') { /* Flag: '0' padded */
f = 1; c = *fmt++;
} else {
if (c == '-') { /* Flag: left justified */
f = 2; c = *fmt++;
}
}
for (w = 0; c >= '0' && c <= '9'; c = *fmt++) /* Minimum width */
w = w * 10 + c - '0';
if (c == 'l' || c == 'L') { /* Prefix: Size is long int */
f |= 4; c = *fmt++;
}
if (!c) break; /* End of format? */
d = c;
if (d >= 'a') d -= 0x20;
switch (d) { /* Type is... */
case 'S' : /* String */
p = va_arg(arp, char*);
for (j = 0; p[j]; j++) ;
while (!(f & 2) && j++ < w) xputc(' ');
xputs(p);
while (j++ < w) xputc(' ');
continue;
case 'C' : /* Character */
xputc((char)va_arg(arp, int)); continue;
case 'B' : /* Binary */
r = 2; break;
case 'O' : /* Octal */
r = 8; break;
case 'D' : /* Signed decimal */
case 'U' : /* Unsigned decimal */
r = 10; break;
case 'X' : /* Hexdecimal */
r = 16; break;
default: /* Unknown type (passthrough) */
xputc(c); continue;
}
/* Get an argument and put it in numeral */
v = (f & 4) ? va_arg(arp, long) : ((d == 'D') ? (long)va_arg(arp, int) : (long)va_arg(arp, unsigned int));
if (d == 'D' && (v & 0x80000000)) {
v = 0 - v;
f |= 8;
}
i = 0;
do {
d = (char)(v % r); v /= r;
if (d > 9) d += (c == 'x') ? 0x27 : 0x07;
s[i++] = d + '0';
} while (v && i < sizeof(s));
if (f & 8) s[i++] = '-';
j = i; d = (f & 1) ? '0' : ' ';
while (!(f & 2) && j++ < w) xputc(d);
do xputc(s[--i]); while(i);
while (j++ < w) xputc(' ');
}
}
void xprintf ( /* Put a formatted string to the default device */
const char* fmt, /* Pointer to the format string */
... /* Optional arguments */
)
{
va_list arp;
va_start(arp, fmt);
xvprintf(fmt, arp);
va_end(arp);
}
void xsprintf ( /* Put a formatted string to the memory */
char* buff, /* Pointer to the output buffer */
const char* fmt, /* Pointer to the format string */
... /* Optional arguments */
)
{
va_list arp;
outptr = buff; /* Switch destination for memory */
va_start(arp, fmt);
xvprintf(fmt, arp);
va_end(arp);
*outptr = 0; /* Terminate output string with a \0 */
outptr = 0; /* Switch destination for device */
}
void xfprintf ( /* Put a formatted string to the specified device */
void(*func)(unsigned char), /* Pointer to the output function */
const char* fmt, /* Pointer to the format string */
... /* Optional arguments */
)
{
va_list arp;
void (*pf)(unsigned char);
pf = xfunc_out; /* Save current output device */
xfunc_out = func; /* Switch output to specified device */
va_start(arp, fmt);
xvprintf(fmt, arp);
va_end(arp);
xfunc_out = pf; /* Restore output device */
}
/*----------------------------------------------*/
/* Dump a line of binary dump */
/*----------------------------------------------*/
void put_dump (
const void* buff, /* Pointer to the array to be dumped */
unsigned long addr, /* Heading address value */
int len, /* Number of items to be dumped */
int width /* Size of the items (DW_CHAR, DW_SHORT, DW_LONG) */
)
{
int i;
const unsigned char *bp;
const unsigned short *sp;
const unsigned long *lp;
xprintf("%08lX:", addr); /* address */
switch (width) {
case DW_CHAR:
bp = buff;
for (i = 0; i < len; i++) /* Hexdecimal dump */
xprintf(" %02X", bp[i]);
xputc(' ');
for (i = 0; i < len; i++) /* ASCII dump */
xputc((bp[i] >= ' ' && bp[i] <= '~') ? bp[i] : '.');
break;
case DW_SHORT:
sp = buff;
do /* Hexdecimal dump */
xprintf(" %04X", *sp++);
while (--len);
break;
case DW_LONG:
lp = buff;
do /* Hexdecimal dump */
xprintf(" %08LX", *lp++);
while (--len);
break;
}
#if !_LF_CRLF
xputc('\r');
#endif
xputc('\n');
}
#endif /* _USE_XFUNC_OUT */
#if _USE_XFUNC_IN
unsigned char (*xfunc_in)(void); /* Pointer to the input stream */
/*----------------------------------------------*/
/* Get a line from the input */
/*----------------------------------------------*/
int xgets ( /* 0:End of stream, 1:A line arrived */
char* buff, /* Pointer to the buffer */
int len /* Buffer length */
)
{
int c, i;
if (!xfunc_in) return 0; /* No input function specified */
i = 0;
for (;;) {
c = xfunc_in(); /* Get a char from the incoming stream */
if (!c) return 0; /* End of stream? */
if (c == '\r') break; /* End of line? */
if (c == '\b' && i) { /* Back space? */
i--;
#if _LINE_ECHO
xputc(c);
#endif
continue;
}
if (c >= ' ' && i < len - 1) { /* Visible chars */
buff[i++] = c;
#if _LINE_ECHO
xputc(c);
#endif
}
}
buff[i] = 0; /* Terminate with a \0 */
#if _LINE_ECHO
#if !_LF_CRLF
xputc('\r');
#endif
xputc('\n');
#endif
return 1;
}
int xfgets ( /* 0:End of stream, 1:A line arrived */
unsigned char (*func)(void), /* Pointer to the input stream function */
char* buff, /* Pointer to the buffer */
int len /* Buffer length */
)
{
unsigned char (*pf)(void);
int n;
pf = xfunc_in; /* Save current input device */
xfunc_in = func; /* Switch input to specified device */
n = xgets(buff, len); /* Get a line */
xfunc_in = pf; /* Restore input device */
return n;
}
/*----------------------------------------------*/
/* Get a value of the string */
/*----------------------------------------------*/
/* "123 -5 0x3ff 0b1111 0377 w "
^ 1st call returns 123 and next ptr
^ 2nd call returns -5 and next ptr
^ 3rd call returns 1023 and next ptr
^ 4th call returns 15 and next ptr
^ 5th call returns 255 and next ptr
^ 6th call fails and returns 0
*/
int xatoi ( /* 0:Failed, 1:Successful */
char **str, /* Pointer to pointer to the string */
long *res /* Pointer to the valiable to store the value */
)
{
unsigned long val;
unsigned char c, r, s = 0;
*res = 0;
while ((c = **str) == ' ') (*str)++; /* Skip leading spaces */
if (c == '-') { /* negative? */
s = 1;
c = *(++(*str));
}
if (c == '0') {
c = *(++(*str));
switch (c) {
case 'x': /* hexdecimal */
r = 16; c = *(++(*str));
break;
case 'b': /* binary */
r = 2; c = *(++(*str));
break;
default:
if (c <= ' ') return 1; /* single zero */
if (c < '0' || c > '9') return 0; /* invalid char */
r = 8; /* octal */
}
} else {
if (c < '0' || c > '9') return 0; /* EOL or invalid char */
r = 10; /* decimal */
}
val = 0;
while (c > ' ') {
if (c >= 'a') c -= 0x20;
c -= '0';
if (c >= 17) {
c -= 7;
if (c <= 9) return 0; /* invalid char */
}
if (c >= r) return 0; /* invalid char for current radix */
val = val * r + c;
c = *(++(*str));
}
if (s) val = 0 - val; /* apply sign if needed */
*res = val;
return 1;
}
#endif /* _USE_XFUNC_IN */
@@ -0,0 +1,38 @@
/*------------------------------------------------------------------------*/
/* Universal string handler for user console interface (C)ChaN, 2011 */
/*------------------------------------------------------------------------*/
#ifndef _STRFUNC
#define _STRFUNC
#define _USE_XFUNC_OUT 1 /* 1: Use output functions */
#define _CR_CRLF 1 /* 1: Convert \n ==> \r\n in the output char */
#define _USE_XFUNC_IN 1 /* 1: Use input function */
#define _LINE_ECHO 1 /* 1: Echo back input chars in xgets function */
#if _USE_XFUNC_OUT
#define xdev_out(func) xfunc_out = (void(*)(unsigned char))(func)
extern void (*xfunc_out)(unsigned char);
void xputc (char c);
void xputs (const char* str);
void xfputs (void (*func)(unsigned char), const char* str);
void xprintf (const char* fmt, ...);
void xsprintf (char* buff, const char* fmt, ...);
void xfprintf (void (*func)(unsigned char), const char* fmt, ...);
void put_dump (const void* buff, unsigned long addr, int len, int width);
#define DW_CHAR sizeof(char)
#define DW_SHORT sizeof(short)
#define DW_LONG sizeof(long)
#endif
#if _USE_XFUNC_IN
#define xdev_in(func) xfunc_in = (unsigned char(*)(void))(func)
extern unsigned char (*xfunc_in)(void);
int xgets (char* buff, int len);
int xfgets (unsigned char (*func)(void), char* buff, int len);
int xatoi (char** str, long* res);
#endif
#endif