Contiki-Inga 3.x
uart.c
1 #include "drv/uart.h"
2 
3 static FILE mystdout = FDEV_SETUP_STREAM (uart_putchar, NULL, _FDEV_SETUP_WRITE);
4 
5 void uart_init(void)
6 {
7  // Set the TxD pin high - set PORTC DIR register bit 3 to 1
8  PORTC.OUTSET = PIN3_bm;
9 
10  // Set the TxD pin as an output - set PORTC OUT register bit 3 to 1
11  PORTC.DIRSET = PIN3_bm;
12 
13  // Set baud rate & frame format
14 /*
15  2MHz: 0 / 12
16  32 MHz: 0x1011 / 3301
17 */
18  USARTC0.BAUDCTRLB = 0; // BSCALE = 0
19  USARTC0.BAUDCTRLA = 209;
20 
21  // Set mode of operation
22  USARTC0.CTRLA = 0; // no interrupts please
23  USARTC0.CTRLC = 0x03; // async, no parity, 8 bit data, 1 stop bit
24 
25  // Enable transmitter only
26  USARTC0.CTRLB = USART_TXEN_bm;
27 
28  stdout = &mystdout;
29 }
30 
31 int uart_putchar (char c, FILE *stream)
32 {
33  if (c == '\n')
34  uart_putchar('\r', stream);
35 
36  // Wait for the transmit buffer to be empty
37  while ( !( USARTC0.STATUS & USART_DREIF_bm) );
38 
39  // Put our character into the transmit buffer
40  USARTC0.DATA = c;
41 
42  return 0;
43 }