/* p4_4.c UART on SERCOM4 echo at 9600 Baud * * This program receives a character from UART on SERCOM4 receiver * then sends it back through UART on SERCOM4 transmitter. * UART on SERCOM4 is connected to PB08-Tx and PB09-Rx. * A 3.3V signal level to USB cable is used to connect PB08/PB09 * to the host PC COM port. * Use TeraTerm on the host PC to send keystrokes and observe the display * of the characters echoed. * * By default, the clock is running at 1 MHz. * * Tested with Atmel Studio 7 v7.0.1006 * and Keil MDK-ARM v5.20 Device Family Pack v1.2.0. */ #include "samd21.h" void UART4_init(void); void UART4_write(char data); char UART4_read(void); unsigned char* ARRAY_PORT_PINCFG1 = (unsigned char*)®_PORT_PINCFG1; unsigned char* ARRAY_PORT_PMUX1 = (unsigned char*)®_PORT_PMUX1; int main (void) { char c; UART4_init(); while (1) { c = UART4_read(); UART4_write(c); } } /* initialize UART4 to transmit at 9600 Baud */ void UART4_init(void) { REG_PM_APBCMASK |= 0x00000040; /* enable bus clock for SERCOM4 */ REG_GCLK_CLKCTRL = 0x4018; /* GCLK0 to SERCOM4 */ REG_SERCOM4_USART_CTRLA |= 1; /* reset SERCOM4 */ while (REG_SERCOM4_USART_SYNCBUSY & 1) {} /* wait for reset to complete */ REG_SERCOM4_USART_CTRLA = 0x40106004; /* LSB first, async, no parity, PAD[1]-Rx, PAD[0]-Tx, BAUD uses fraction, 8x oversampling, internal clock */ REG_SERCOM4_USART_CTRLB = 0x00030000; /* enable Tx, Rx, one stop bit, 8 bit */ REG_SERCOM4_USART_BAUD = 13; /* 1000000 / 8 / 9600 = 13.02 */ REG_SERCOM4_USART_CTRLA |= 2; /* enable SERCOM4 */ while (REG_SERCOM4_USART_SYNCBUSY & 2) {} /* wait for enable to complete */ ARRAY_PORT_PINCFG1[8] |= 1; /* allow pmux to set PB08 pin configuration */ ARRAY_PORT_PINCFG1[9] |= 1; /* allow pmux to set PB09 pin configuration */ ARRAY_PORT_PMUX1[4] = 0x33; /* PB08 = TxD, PB09 = RxD */ } void UART4_write(char data) { while(!(REG_SERCOM4_USART_INTFLAG & 1)) {} /* wait for data register empty */ REG_SERCOM4_USART_DATA = data; /* send a char */ } char UART4_read(void) { while(!(REG_SERCOM4_USART_INTFLAG & 4)) {} /* wait until receive complete */ return REG_SERCOM4_USART_DATA; /* read the receive char and return it */ }