UoS³ Flight Computer Firmware
 All Data Structures Files Functions Groups Pages
uart.c
Go to the documentation of this file.
1 
11 #include "board.h"
12 #include "../uart.h"
13 
14 #include "inc/tm4c123gh6pm.h"
15 #include "inc/hw_memmap.h"
16 
17 #include "driverlib/gpio.h"
18 #include "driverlib/uart.h"
19 #include "driverlib/sysctl.h"
20 #include "driverlib/pin_map.h"
21 
22 /* UART description struct */
23 typedef struct UART {
24  uint32_t peripheral_gpio; // TI Driver GPIO Peripheral Reference
25  uint32_t peripheral_uart; // TI Driver UART Peripheral Reference
26  uint32_t base_gpio; // TI Driver GPIO Base Reference
27  uint32_t base_uart; // TI Driver UART Base Reference
28  uint32_t pin_rx_function; // TI Driver UART RX Pin Reference
29  uint32_t pin_tx_function; // TI Driver UART TX Pin Reference
30  uint8_t pin_rx; // TI Driver RX Pin Reference
31  uint8_t pin_tx; // TI Driver TX Pin Reference
32  bool initialised;
33 } UART;
34 
35 /* Array of enabled UARTs */
36 static UART UART_uarts[3] =
37  {
38  {
39  SYSCTL_PERIPH_GPIOA,
40  SYSCTL_PERIPH_UART0,
41  GPIO_PORTA_BASE,
42  UART0_BASE,
43  GPIO_PA0_U0RX,
44  GPIO_PA1_U0TX,
45  GPIO_PIN_0,
46  GPIO_PIN_1,
47  false
48  },
49  {
50  SYSCTL_PERIPH_GPIOC,
51  SYSCTL_PERIPH_UART3,
52  GPIO_PORTC_BASE,
53  UART3_BASE,
54  GPIO_PC6_U3RX,
55  GPIO_PC7_U3TX,
56  GPIO_PIN_6,
57  GPIO_PIN_7,
58  false
59  },
60  {
61  SYSCTL_PERIPH_GPIOE,
62  SYSCTL_PERIPH_UART7,
63  GPIO_PORTE_BASE,
64  UART7_BASE,
65  GPIO_PE0_U7RX,
66  GPIO_PE1_U7TX,
67  GPIO_PIN_0,
68  GPIO_PIN_1,
69  false
70  }
71  };
72 #define NUMBER_OF_UARTS ( sizeof(UART_uarts) / sizeof(UART) )
73 
74 #define check_uart_num(x, y) if(x >= NUMBER_OF_UARTS) { return y; }
75 
78 void UART_init(uint8_t uart_num, uint32_t baudrate)
79 {
80  check_uart_num(uart_num,);
81  UART *uart = &UART_uarts[uart_num];
82 
83  /* Check UART is initialised */
84  if(!uart->initialised)
85  {
86  /* Initialise UART Peripheral if not already initialised */
87  if(!SysCtlPeripheralReady(uart->peripheral_uart))
88  {
89  SysCtlPeripheralEnable(uart->peripheral_uart);
90  while(!SysCtlPeripheralReady(uart->peripheral_uart)) { };
91  }
92 
93  /* Initialise GPIO Peripheral if not already initialised */
94  if(!SysCtlPeripheralReady(uart->peripheral_gpio))
95  {
96  SysCtlPeripheralEnable(uart->peripheral_gpio);
97  while(!SysCtlPeripheralReady(uart->peripheral_gpio)) { };
98  }
99 
100  GPIOPinConfigure(uart->pin_rx_function);
101  GPIOPinConfigure(uart->pin_tx_function);
102  GPIOPinTypeUART(uart->base_gpio, uart->pin_rx | uart->pin_tx);
103 
104  UARTClockSourceSet(uart->base_uart, UART_CLOCK_SYSTEM);
105 
106  uart->initialised = true;
107  }
108 
109  UARTConfigSetExpClk(uart->base_uart, SysCtlClockGet(), baudrate,
110  (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE));
111 }
112 
113 char UART_getc(uint8_t uart_num)
114 {
115  check_uart_num(uart_num, '\0');
116  UART *uart = &UART_uarts[uart_num];
117 
118  return (char)UARTCharGet(uart->base_uart);
119 }
120 
121 void UART_putc(uint8_t uart_num, char c)
122 {
123  check_uart_num(uart_num,);
124  UART *uart = &UART_uarts[uart_num];
125 
126  UARTCharPut(uart->base_uart, c);
127 }
128 
129 void UART_puts(uint8_t uart_num, char *str)
130 {
131  while(*str != '\0')
132  {
133  UART_putc(uart_num, *str++);
134  }
135 }
136 
137 void UART_putb(uint8_t uart_num, char *str, uint32_t len)
138 {
139  while(len--)
140  {
141  UART_putc(uart_num, *str++);
142  }
143 }
144 
145 bool UART_getc_nonblocking(uint8_t uart_num, char *c)
146 {
147  check_uart_num(uart_num, false);
148  UART *uart = &UART_uarts[uart_num];
149 
150  int32_t value = UARTCharGetNonBlocking(uart->base_uart);
151 
152  if(value >= 0)
153  {
154  *c = (char)value;
155  return true;
156  }
157  else
158  {
159  return false;
160  }
161 }
162 
163 bool UART_putc_nonblocking(uint8_t uart_num, char c)
164 {
165  check_uart_num(uart_num, false);
166  UART *uart = &UART_uarts[uart_num];
167 
168  return UARTCharPutNonBlocking(uart->base_uart, c);
169 }
170 
171 bool UART_puts_nonblocking(uint8_t uart_num, char *str)
172 {
173  while(*str != '\0')
174  {
175  if(!UART_putc_nonblocking(uart_num, *str++))
176  {
177  return false;
178  }
179  }
180  return true;
181 }
182 
183 bool UART_putb_nonblocking(uint8_t uart_num, char *str, uint32_t len)
184 {
185  while(len--)
186  {
187  if(!UART_putc_nonblocking(uart_num, *str++))
188  {
189  return false;
190  }
191  }
192  return true;
193 }
194 
195 bool UART_busy(uint8_t uart_num)
196 {
197  check_uart_num(uart_num, false);
198  UART *uart = &UART_uarts[uart_num];
199 
200  return UARTBusy(uart->base_uart);
201 }
202 
203 bool UART_charsAvail(uint8_t uart_num)
204 {
205  check_uart_num(uart_num, false);
206  UART *uart = &UART_uarts[uart_num];
207 
208  return UARTCharsAvail(uart->base_uart);
209 }
210 
void UART_init(uint8_t uart_num, uint32_t baudrate)
Definition: uart.c:78
bool UART_getc_nonblocking(uint8_t uart_num, char *c)
Definition: uart.c:145
char UART_getc(uint8_t uart_num)
Definition: uart.c:113
bool UART_putb_nonblocking(uint8_t uart_num, char *str, uint32_t len)
Definition: uart.c:183
Definition: uart.c:23
void UART_putb(uint8_t uart_num, char *str, uint32_t len)
Definition: uart.c:137
bool UART_charsAvail(uint8_t uart_num)
Definition: uart.c:203
bool UART_putc_nonblocking(uint8_t uart_num, char c)
Definition: uart.c:163
void UART_puts(uint8_t uart_num, char *str)
Definition: uart.c:129
bool UART_busy(uint8_t uart_num)
Definition: uart.c:195
void UART_putc(uint8_t uart_num, char c)
Definition: uart.c:121
bool UART_puts_nonblocking(uint8_t uart_num, char *str)
Definition: uart.c:171