/* p4_5.c C library Console I/O using UART3 at 9600 Baud * * This program demonstrates the use of C library console I/O. * The functions fputc() and fgetc() are implemented using * UART3Tx() and UART3Rx() for character I/O. * In the fgetc(), the received character is echoed and if a '\r' * is received, it is substituted with a '\n' but both characters are * echoed. * In fputc() and fgetc(), the file descripter is not checked. All * file I/O's are directed to the console. * * By default, the clock is running at 1 MHz. * * Tested with Keil MDK-ARM v5.20 Device Family Pack v1.2.0. */ #include "samd21.h" #include void UART3_init(void); unsigned char UART3Rx(void); int UART3Tx(unsigned char c); unsigned char* ARRAY_PORT_PINCFG0 = (unsigned char*)®_PORT_PINCFG0; unsigned char* ARRAY_PORT_PMUX0 = (unsigned char*)®_PORT_PMUX0; int main(void) { int n; char str[80]; UART3_init(); printf("Test stdio library console I/O functions\r\n"); fprintf(stdout, " test for stdout\r\n"); fprintf(stderr, " test for stderr\r\n"); while (1) { printf("please enter a number: "); scanf("%d", &n); printf("the number entered is: %d\r\n", n); printf("please type a string: "); gets(str); printf("the string entered is: "); puts(str); printf("\r\n"); } } /* initialize UART3 to 9600 Baud at 1 MHz core clock */ void UART3_init(void) { REG_PM_APBCMASK |= 0x00000020; /* enable bus clock for SERCOM3 */ REG_GCLK_CLKCTRL = 0x4017; /* GCLK0 to SERCOM3 */ REG_SERCOM3_USART_CTRLA |= 1; /* reset SERCOM3 */ while (REG_SERCOM3_USART_SYNCBUSY & 1) {} /* wait for reset to complete */ REG_SERCOM3_USART_CTRLA = 0x40106004; /* LSB first, async, no parity, PAD[0]-TxD, PAD[1]-RxD BAUD uses fraction, 8x oversampling, internal clock */ REG_SERCOM3_USART_CTRLB = 0x00030000; /* enable Tx/Rx, one stop bit, 8 bit */ REG_SERCOM3_USART_BAUD = 13; /* 1000000/8/9600 = 13.02 */ REG_SERCOM3_USART_CTRLA |= 2; /* enable SERCOM3 */ while (REG_SERCOM3_USART_SYNCBUSY & 2) {} /* wait for enable to complete */ ARRAY_PORT_PINCFG0[22] |= 1; /* allow pmux to set PA22 pin configuration */ ARRAY_PORT_PINCFG0[23] |= 1; /* allow pmux to set PA23 pin configuration */ ARRAY_PORT_PMUX0[11] = 0x22; /* PA22 = TxD, PA23 = RxD */ } /* read a character from UART3 */ unsigned char UART3Rx(void) { char c; while(!(REG_SERCOM3_USART_INTFLAG & 4)) {} /* wait until receive complete */ c = REG_SERCOM3_USART_DATA; /* read the receive char */ return c; } /* write a character to UART */ int UART3Tx(unsigned char c) { while(!(REG_SERCOM3_USART_INTFLAG & 1)) {} /* wait for data register empty */ REG_SERCOM3_USART_DATA = c; /* send a char */ return c; } /* The code below is the interface to the C standard I/O library. * All the I/O are directed to the console, which is UART3. */ struct __FILE { int handle; }; FILE __stdin = {0}; FILE __stdout = {1}; FILE __stderr = {2}; /* Called by C library console/file input * This function echoes the character received. * If the character is '\r', it is substituted by '\n'. */ int fgetc(FILE *f) { int c; c = UART3Rx(); /* read the character from console */ if (c == '\r') { /* if '\r', replace with '\n' */ UART3Tx(c); /* echo */ c = '\n'; } UART3Tx(c); /* echo */ return c; } /* Called by C library console/file output */ int fputc(int c, FILE *f) { return UART3Tx(c); /* write the character to console */ }