-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsp.c
263 lines (200 loc) · 7.17 KB
/
bsp.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* Board Support Package (BSP) for the EK-TM4C123GXL board */
#include "bsp.h"
extern QueueHandle_t uart_transfer_queue;
#define TRANSFER_R_FROM_UART0 0
#define TRANSFER_R_FROM_UART3 1
static const TransferRequest transfer_requests[2] = { { UART0, UART3 }, /* Used by UART0_IRQHandler. */
{ UART3, UART0 } /* Used by UART3_IRQHandler. */
};
void BSP_init(void)
{
SystemCoreClockUpdate();
BSP_initLeds();
BSP_initUART0();
BSP_initUART3();
BSP_initUARTInterrupts();
}
void BSP_initLeds(void)
{
/* Enable clock for GPIOF. */
SYSCTL->RCGC2 |= (1U << 5);
/* Enable Advanced High-Performance Bus for GPIOF. */
SYSCTL->GPIOHBCTL |= (1U << 5);
/* Set GPIO Pin Direction as output. */
GPIOF_AHB->DIR |= LED_GREEN;
/* Set Digital Enable. */
GPIOF_AHB->DEN |= LED_GREEN;
}
void BSP_initUARTInterrupts(void)
{
/* Set the interrupt priorities:
* A higher-urgency interrupt (lower priority number) can preempt
* a lower-urgency interrupt (higher priority number).
* PendSV has the highest value. */
NVIC_SetPriority(UART0_IRQn, configLIBRARY_LOWEST_INTERRUPT_PRIORITY);
NVIC_SetPriority(UART3_IRQn, configLIBRARY_LOWEST_INTERRUPT_PRIORITY);
/* Enable IRQs in NVIC. */
NVIC_EnableIRQ(UART0_IRQn);
NVIC_EnableIRQ(UART3_IRQn);
/* Enable interrupts only on receiving. */
UART0->IM = (1u << 4);
UART3->IM = (1u << 4);
/* Clear interrupt receiver source. */
UART0->ICR = 0xFFFU;
UART3->ICR = 0xFFFU;
}
void BSP_initUART0(void) {
/* Enable the UART module 0 by providing a clock. */
SYSCTL->RCGCUART |= 1u << 0;
/* Enable and provide a clock to GPIO Port A. */
SYSCTL->RCGCGPIO |= 1u << 0;
/* The associated pin of PA0 and PA1 functions as a peripheral signal
* and is controlled by the alternate hardware function. */
GPIOA->AFSEL |= (1u << 1) | (1u << 0);
/* GPIO Port Control. Configure the PMC0 and PMC1 in the GPIOPCTL
* register to assign the UART signals to the appropriate pins. */
GPIOA->PCTL |= (1 << 4) | (1 << 0);
/* Enable the digital functions for the pins PD6 and PD7. GPIODEN. */
GPIOA->DEN |= (1u << 1) | (1u << 0);
BSP_configUART(UART0);
}
void BSP_initUART3(void) {
/* On pin 14 (Receiver) = PC6 and pin 13 (Transmitter) = PC7. */
/* Enable the UART module 3 by providing a clock. */
SYSCTL->RCGCUART |= 1u << 3;
/* Enable and provide a clock to GPIO Port C. */
SYSCTL->RCGCGPIO |= 1u << 2;
/* The associated pin of PC6 and PC7 functions as a peripheral signal
* and is controlled by the alternate hardware function. */
GPIOC->AFSEL |= (1u << 7) | (1u << 6);
/* GPIO Port Control. Configure the PMC6 and PMC7 in the GPIOPCTL
* register to assign the UART signals to the appropriate pins. */
GPIOC->PCTL |= (1 << 28) | (1 << 24);
/* Enable the digital functions for the pins PC6 and PC7. GPIODEN. */
GPIOC->DEN |= (1u << 7) | (1u << 6);
BSP_configUART(UART3);
}
#define UART_BAUD_RATE 115200
void BSP_configUART(UART0_Type* uart) {
/* Disable UART during setting by clearing
* the UARTEN bit in the UARTCTL register. */
uart->CTL &= ~(1u << 0);
/* UART Clock Configuration.
* Use the PIOSC clock in the project, so the clock is independent
* from the value of the system clock (PLL).
* 0x0: System clock (based on clock source and divisor factor).
* 0x5: PIOSC = 16 MHz. */
uart->CC = 0x5;
/* Find the Baud-Rate Divider BRD.
* UARTSysClk = 16MHz, ClkDiv = 16 and Baud Rate = 115200
* BRD = IBRD + FBRD = UARTSysClk / (ClkDiv * Baud Rate)
*
* IBRD = Integer part of BRD = UARTSysclk/clkdiv*115200
* = 8
*
* FNRD = Integer part of: Fractional portion of the BRD * 64 + 0.5
* = int(0.6805 * 64 + 0.5) = 44
*/
uart->IBRD = 8;
uart->FBRD = 44;
/* UART Line Control. Enable FIFO and set UART Word length = 8 bits
* UART0->LCRH |= ((1 << 6) | (1 << 5) | (1 << 4));
* 8-bit, no parity, 1-stop bit. */
uart->LCRH = (0x3 << 5);
/* Enable UART, transmission and reception
* by setting the UARTEN bit in the UARTCTL. */
uart->CTL |= (1u << 0) | (1u << 8) | (1u << 9);
}
/***** IRQ Handlers *****/
void UART0_IRQHandler(void) {
/* Debug: to monitor the needed size for the uart_transfer_queue. */
static unsigned nb_UART0errQueueFull = 0;
/* If context switch is required, if will get set to pdTRUE
* inside xQueueSendToBackFromISR. */
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
/* Raw Interrupt Status: check interrupt caused by receiving status. */
if ((UART0->RIS & (1u << 4)) != 0u)
{
BaseType_t status;
status = xQueueSendToBackFromISR(uart_transfer_queue,
&transfer_requests[TRANSFER_R_FROM_UART0],
&xHigherPriorityTaskWoken);
if(status == errQUEUE_FULL) {
nb_UART0errQueueFull++;
}
}
/* Clear interrupt sources. */
UART0->ICR = 0xFFFu;
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
void UART3_IRQHandler(void) {
/* Debug: to monitor the needed size for the uart_transfer_queue. */
static unsigned nb_UART3errQueueFull = 0;
/* If context switch is required, if will get set to pdTRUE
* inside xQueueSendToBackFromISR. */
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
/* Raw Interrupt Status: check interrupt caused by receiving status. */
if ((UART3->RIS & (1u << 4)) != 0u)
{
BaseType_t status;
status = xQueueSendToBackFromISR(uart_transfer_queue,
&transfer_requests[TRANSFER_R_FROM_UART3],
&xHigherPriorityTaskWoken);
if(status == errQUEUE_FULL) {
nb_UART3errQueueFull++;
}
}
/* Clear interrupt sources. */
UART3->ICR = 0xFFFu;
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
/**** UART data handlers *****/
void BSP_sendChar(UART0_Type* uart, unsigned char c)
{
/* Wait that any previous transmission has completed. */
while ((uart->FR & (1u << 5)) != 0);
uart->DR = c;
}
unsigned char BSP_readChar(UART0_Type* uart)
{
unsigned char c;
/* Check that the data register is not empty. */
if((uart->FR & (1u << 4)) != 0) {
return CHAR_NULL;
}
c = uart->DR;
return c;
}
void BSP_readStr(UART0_Type* uart, unsigned char *buffer) {
uint8_t volatile i;
uint8_t len = sizeof(buffer)/sizeof(unsigned char);
/* Wait that the data register is not empty. */
while ((uart->FR & (1u << 4)) != 0);
for(i = 0; (i < len) && ((uart->FR & (1u << 4)) == 0); i++) {
buffer[i] = uart->DR;
}
}
void BSP_sendStr(UART0_Type* uart, unsigned char *buffer)
{
while (*buffer != 0)
{
BSP_sendChar(uart, *buffer);
buffer++;
}
}
void BSP_transferData(UART0_Type* from, UART0_Type* to)
{
BSP_ledGreenToggle();
// while ((from->FR & (1u << 4)) == 0)
// {
char c = BSP_readChar(from);
if(c != CHAR_NULL) {
BSP_sendChar(to, c);
}
// }
}
/***** LED handlers *****/
void BSP_ledGreenToggle(void)
{
GPIOF_AHB->DATA ^= LED_GREEN;
}