forked from meshack-mbuvi/SPH336_Jan2015
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart.c
83 lines (70 loc) · 2.36 KB
/
uart.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
/*
* uart.c
*
* Created on: Mar 2, 2015
* Author: karibe
*/
#include"uart.h"
void init_uart(UART_MemMapPtr uartch, int sysclk, int baud){
uint16_t ubd, temp, brfa;
/* Make sure that the transmitter and receiver are disabled while we
* change settings.
*/
UART_C2_REG(uartch) &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK );
/* Configure the UART for 8-bit mode, no parity */
/* We need all default settings, so entire register is cleared */
UART_C1_REG(uartch) = 0;
/* Configure the UART for 8-bit mode, no parity */
/* We need all default settings, so entire register is cleared */
UART_C1_REG(uartch) = 0;
/* Calculate baud settings */
ubd = (uint16_t)((sysclk*1000)/(baud * 16));
/* Save off the current value of the UARTx_BDH except for the SBR */
temp = UART_BDH_REG(uartch) & ~(UART_BDH_SBR(0x1F));
UART_BDH_REG(uartch) = temp | UART_BDH_SBR(((ubd & 0x1F00) >> 8));
UART_BDL_REG(uartch) = (uint8_t)(ubd & UART_BDL_SBR_MASK);
/* Determine if a fractional divider is needed to get closer to the baud rate */
brfa = (((sysclk*32000)/(baud * 16)) - (ubd * 32));
/* Save off the current value of the UARTx_C4 register except for the BRFA */
temp = UART_C4_REG(uartch) & ~(UART_C4_BRFA(0x1F));
UART_C4_REG(uartch) = temp | UART_C4_BRFA(brfa);
/* Enable receiver and transmitter */
UART_C2_REG(uartch) |= (UART_C2_TE_MASK | UART_C2_RE_MASK );
//UART_S1_REG(uartch) &= ~UART_S1_TDRE_MASK;
}
void uart_putchar (UART_MemMapPtr channel, uint8_t ch)
{
/* Wait until space is available in the FIFO */
while(!(UART_S1_REG(channel) & UART_S1_TDRE_MASK));
/* Send the character */
UART_D_REG(channel) = (uint8_t)ch;
}
uint8_t uart_getchar (UART_MemMapPtr channel)
{
/* Wait until character has been received */
while (!(UART_S1_REG(channel) & UART_S1_RDRF_MASK));
/* Return the 8-bit data from the receiver */
return (uint8_t)UART_D_REG(channel);
}
uint16_t data_ready (UART_MemMapPtr channel)
{
return (((uint16_t)(UART_S1_REG(channel) & UART_S1_RDRF_MASK)) == UART_S1_RDRF_MASK);
}
char uart_read(void){
return uart_getchar (UART5_BASE_PTR);
}
//send a char to UART5_TX
void uartsend(uint8_t ch){
uart_putchar (UART5_BASE_PTR,ch);
}
//send a string
void puts(uint8_t *s){
while(*s!='\0'){
uartsend(*s++);
}
}
//abstract data_ready function for main, doesnt need to know what chunnel is being used
uint16_t data_available(void)
{
return data_ready(UART5_BASE_PTR);
}