EE445M RTOS
Taken at the University of Texas Spring 2015
uart.c
Go to the documentation of this file.
1 /* -*- mode: c; c-basic-offset: 4; -*- */
2 #include <stdint.h>
3 
4 #include "uart.h"
5 
6 #include "inc/hw_memmap.h"
7 #include "inc/hw_ints.h"
8 
9 #include "driverlib/pin_map.h"
10 #include "driverlib/gpio.h"
11 #include "driverlib/uart.h"
12 
13 #include "libstd/nexus.h"
14 #include "libnotify/notify.h"
15 
21 #define UART_BUFFER_LEN 128
22 
25 
28 
32 inline
34 
35  uart_active_metadata = metadata;
36 }
37 
38 /* Initializing has the side effect of setting the active channel. */
39 void uart_init(hw_metadata metadata) {
40 
41  uart_set_active_metadata(metadata);
42  GPIOPinConfigure(GPIO_PA0_U0RX);
43  GPIOPinConfigure(GPIO_PA1_U0TX);
44  /* todo: parametrize */
46 
47  /* This is the HF aculprit Thursday February 26, 2015 */
49  metadata.uart.baud_rate,
52 
53  /* Enable the UART interrupt. */
54  IntEnable(metadata.uart.interrupt);
56 }
57 
58 inline
59 void uart_send_char(const char text) {
60 
61  uart_send_char_(uart_active_metadata, text);
62 }
63 
64 inline
65 void uart_send_char_(hw_metadata metadata, const char text) {
66 
67  UARTCharPut(metadata.uart.channel, text);
68 }
69 
70 inline
71 void uart_send_string(const char* text) {
72 
73  uart_send_string_(uart_active_metadata, text);
74 }
75 
76 void uart_send_string_(hw_metadata metadata, const char* text) {
77 
78  uint32_t cnt = ustrlen(text);
79  char* ptr = (char*)text;
80 
81  while(cnt--) {
82  UARTCharPut(metadata.uart.channel, *(ptr++));
83  }
84 }
85 
86 inline
87 char uart_get_char() {
88 
89  return uart_get_char_(uart_active_metadata);
90 }
91 
92 char uart_get_char_(hw_metadata metadata) {
93 
94  uint32_t ui32Status;
95  /* Get the interrrupt status. */
96  ui32Status = UARTIntStatus(metadata.uart.channel, true);
97 
98  /* Clear the asserted interrupts. */
99  UARTIntClear(metadata.uart.channel, ui32Status);
100 
101  char ret = UARTCharGetNonBlocking(metadata.uart.channel);
102  return ret;
103 }
104 
105 inline
106 char* uart_get_string(const long string_length) {
107 
108  return uart_get_string_(uart_active_metadata, string_length);
109 }
110 
112  const long string_length) {
113 
114  uint32_t ui32Status;
115  long remaining_chars = string_length;
116 
117  /* \UART_BUFFER is a null-terminated string */
118  UART_BUFFER[string_length] = 0;
119 
120  /* Get the interrrupt status. */
121  ui32Status = UARTIntStatus(metadata.uart.channel, true);
122 
123  /* Clear the asserted interrupts. */
124  UARTIntClear(metadata.uart.channel, ui32Status);
125 
126  while(UARTCharsAvail(metadata.uart.channel) && remaining_chars > 0) {
127  UART_BUFFER[remaining_chars - string_length] =
128  UARTCharGet(metadata.uart.channel);
129  remaining_chars--;
130  }
131  return UART_BUFFER;
132 }
133 
#define UART_INT_RX
Definition: uart.h:69
uint32_t interrupt
Definition: hardware.h:57
char uart_get_char()
Read a char from the active uart channel.
Definition: uart.c:87
static char UART_BUFFER[128]
Definition: uart.c:24
void uart_send_string(const char *text)
Definition: uart.c:71
bool UARTCharsAvail(uint32_t ui32Base)
Definition: uart.c:1087
void GPIOPinTypeUART(uint32_t ui32Port, uint8_t ui8Pins)
Definition: gpio.c:2031
hw_metadata uart_active_metadata
Definition: uart.c:27
#define GPIO_PIN_0
Definition: gpio.h:60
void uart_send_char_(hw_metadata metadata, const char text)
Definition: uart.c:65
hw_uart_metadata uart
Definition: hardware.h:97
void UARTConfigSetExpClk(uint32_t ui32Base, uint32_t ui32UARTClk, uint32_t ui32Baud, uint32_t ui32Config)
Definition: uart.c:368
memory_address_t channel
Definition: hardware.h:56
char uart_get_char_(hw_metadata metadata)
Read a char from the specified uart channel.
Definition: uart.c:92
void UARTIntClear(uint32_t ui32Base, uint32_t ui32IntFlags)
Definition: uart.c:1581
uint32_t SysCtlClockGet(void)
Definition: sysctl.c:2727
#define GPIO_PIN_1
Definition: gpio.h:61
char * uart_get_string_(hw_metadata metadata, const long string_length)
Read a char array from the active uart channel.
Definition: uart.c:111
#define UART_CONFIG_STOP_ONE
Definition: uart.h:90
#define UART_CONFIG_WLEN_8
Definition: uart.h:85
char * uart_get_string(const long string_length)
Read a char array from the active uart channel.
Definition: uart.c:106
int32_t UARTCharGetNonBlocking(uint32_t ui32Base)
Definition: uart.c:1143
uint32_t ustrlen(const char *s)
Definition: nexus.c:17
void uart_send_char(const char text)
Definition: uart.c:59
#define GPIO_PORTA_BASE
Definition: hw_memmap.h:53
uint32_t UARTIntStatus(uint32_t ui32Base, bool bMasked)
Definition: uart.c:1533
void uart_set_active_metadata(hw_metadata metadata)
Definition: uart.c:33
int32_t UARTCharGet(uint32_t ui32Base)
Definition: uart.c:1184
void GPIOPinConfigure(uint32_t ui32PinConfig)
Definition: gpio.c:2509
void uart_init(hw_metadata metadata)
Definition: uart.c:39
void uart_send_string_(hw_metadata metadata, const char *text)
Definition: uart.c:76
void UARTCharPut(uint32_t ui32Base, unsigned char ucData)
Definition: uart.c:1268
void UARTIntEnable(uint32_t ui32Base, uint32_t ui32IntFlags)
Definition: uart.c:1471
uint32_t baud_rate
Definition: hardware.h:55
#define UART_CONFIG_PAR_NONE
Definition: uart.h:93
void IntEnable(uint32_t ui32Interrupt)
Definition: interrupt.c:610
#define UART_INT_RT
Definition: uart.h:67
#define UART_BUFFER_LEN
Definition: uart.c:21