/* p4_1.c UART on SERCOM3 transmit at 9600 Baud * * Send a string "YES" to UART on SERCOM3. * UART3 is connected to EDBG virtual connection to the host PC COM port. * Use TeraTerm to see the message "YES" on a PC. * * 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 UART3_init(void); void UART3_write(char data); void delayMs(int n); unsigned char* ARRAY_PORT_PINCFG0 = (unsigned char*)®_PORT_PINCFG0; unsigned char* ARRAY_PORT_PMUX0 = (unsigned char*)®_PORT_PMUX0; int main (void) { UART3_init(); while (1) { UART3_write('Y'); UART3_write('e'); UART3_write('s'); delayMs(10); /* leave a gap between messages */ } } /* initialize UART3 to transmit at 9600 Baud */ 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]-Tx, BAUD uses fraction, 8x oversampling, internal clock */ REG_SERCOM3_USART_CTRLB = 0x00010000; /* enable Tx, 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_PMUX0[11] = 0x02; /* PA22 = TxD */ } void UART3_write(char data) { while(!(REG_SERCOM3_USART_INTFLAG & 1)) {} /* wait for data register empty */ REG_SERCOM3_USART_DATA = data; /* send a char */ } /* millisecond delay based on 1 MHz system clock */ void delayMs(int n) { int i; for (; n > 0; n--) for (i = 0; i < 199; i++) __asm("nop"); }