-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathboard_driver_serial.c
104 lines (93 loc) · 3.61 KB
/
board_driver_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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
Copyright (c) 2015 Arduino LLC. All right reserved.
Copyright (c) 2015 Atmel Corporation/Thibaut VIARD. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "board_driver_serial.h"
bool uart_drv_error_flag = false;
void uart_basic_init(Sercom *sercom, uint16_t baud_val, enum uart_pad_settings pad_conf)
{
/* Wait for synchronization */
while(sercom->USART.SYNCBUSY.bit.ENABLE);
/* Disable the SERCOM UART module */
sercom->USART.CTRLA.bit.ENABLE = 0;
/* Wait for synchronization */
while(sercom->USART.SYNCBUSY.bit.SWRST);
/* Perform a software reset */
sercom->USART.CTRLA.bit.SWRST = 1;
/* Wait for synchronization */
while(sercom->USART.CTRLA.bit.SWRST);
/* Wait for synchronization */
while(sercom->USART.SYNCBUSY.bit.SWRST || sercom->USART.SYNCBUSY.bit.ENABLE);
/* Update the UART pad settings, mode and data order settings */
sercom->USART.CTRLA.reg = pad_conf | SERCOM_USART_CTRLA_MODE(1) | SERCOM_USART_CTRLA_DORD;
/* Wait for synchronization */
while(sercom->USART.SYNCBUSY.bit.CTRLB);
/* Enable transmit and receive and set data size to 8 bits */
sercom->USART.CTRLB.reg = SERCOM_USART_CTRLB_RXEN | SERCOM_USART_CTRLB_TXEN | SERCOM_USART_CTRLB_CHSIZE(0);
/* Load the baud value */
sercom->USART.BAUD.reg = baud_val;
/* Wait for synchronization */
while(sercom->USART.SYNCBUSY.bit.ENABLE);
/* Enable SERCOM UART */
sercom->USART.CTRLA.bit.ENABLE = 1;
}
void uart_disable(Sercom *sercom)
{
/* Wait for synchronization */
while(sercom->USART.SYNCBUSY.bit.ENABLE);
/* Disable SERCOM UART */
sercom->USART.CTRLA.bit.ENABLE = 0;
}
void uart_write_byte(Sercom *sercom, uint8_t data)
{
/* Wait for Data Register Empty flag */
while(!sercom->USART.INTFLAG.bit.DRE);
/* Write the data to DATA register */
sercom->USART.DATA.reg = (uint16_t)data;
}
uint8_t uart_read_byte(Sercom *sercom)
{
/* Wait for Receive Complete flag */
while(!sercom->USART.INTFLAG.bit.RXC);
/* Check for errors */
if (sercom->USART.STATUS.bit.PERR || sercom->USART.STATUS.bit.FERR || sercom->USART.STATUS.bit.BUFOVF)
/* Set the error flag */
uart_drv_error_flag = true;
/* Return the read data */
return((uint8_t)sercom->USART.DATA.reg);
}
void uart_write_buffer_polled(Sercom *sercom, uint8_t *ptr, uint16_t length)
{
/* Do the following for specified length */
do {
/* Wait for Data Register Empty flag */
while(!sercom->USART.INTFLAG.bit.DRE);
/* Send data from the buffer */
sercom->USART.DATA.reg = (uint16_t)*ptr++;
} while (length--);
}
void uart_read_buffer_polled(Sercom *sercom, uint8_t *ptr, uint16_t length)
{
/* Do the following for specified length */
do {
/* Wait for Receive Complete flag */
while(!sercom->USART.INTFLAG.bit.RXC);
/* Check for errors */
if (sercom->USART.STATUS.bit.PERR || sercom->USART.STATUS.bit.FERR || sercom->USART.STATUS.bit.BUFOVF)
/* Set the error flag */
uart_drv_error_flag = true;
/* Store the read data to the buffer */
*ptr++ = (uint8_t)sercom->USART.DATA.reg;
} while (length--);
}