-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserial.c
49 lines (40 loc) · 983 Bytes
/
serial.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* serial.c
*
* This code manages sending data over the serial port (USART0). This code
* moves USART0 to its alternative pins as the default pins are already in
* use managing the blade's LEDs.
*
* default USART0 pins:
* 11 PB3 RXD
* 12 PB4 TXD
*
* alt USART0 pins:
* 20 PA1 TXD
* 1 PA2 RXD
*/
#include <avr/io.h>
#include <string.h>
#include "serial.h"
char serial_buf[SERIAL_BUF_LEN];
void serial_setup( void ) {
// move USART0 to alternative pins
PORTMUX.CTRLB |= PORTMUX_USART0_ALTERNATE_gc;
// set ALT USART0 TX (PA1) to output
PORTA.DIRSET = PIN1_bm;
// set USART0 baud rate
USART0.BAUD = (uint16_t)USART0_BAUD_RATE(SERIAL_BAUD_RATE);
// enable TX for USART0
USART0.CTRLB |= USART_TXEN_bm;
}
void USART0_sendChar(char c) {
while (!(USART0.STATUS & USART_DREIF_bm)) {
;
}
USART0.TXDATAL = c;
}
void serial_sendString(char *str)
{
for(size_t i = 0; i < strlen(str); i++) {
USART0_sendChar(str[i]);
}
}