-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathuart.c
260 lines (237 loc) · 7.09 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
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
/*
* uart.c
*
* Created on: Jul 20, 2016
* Author: PDI
*/
#include "../Library-FRDM-KL25Z/uart.h"
/****************************************************************************************
*
*****************************************************************************************/
void (*uart0_task_irq)(void);
void (*uart1_task_irq)(void);
void (*uart2_task_irq)(void);
/****************************************************************************************
*
*****************************************************************************************/
bool uart_Init(UART_MemMapPtr uart, uint8_t alt, uint32_t baud_rate)
{
uint16_t divisor;
uint8_t temp;
if(uart == (UART_MemMapPtr)UART0)
{
SIM_SCGC4 |= SIM_SCGC4_UART0_MASK;
SIM_SOPT2 &= ~SIM_SOPT2_UART0SRC_MASK;
SIM_SOPT2 |= SIM_SOPT2_UART0SRC(1);
divisor = (uint16_t)(SystemCoreClock / baud_rate ) / 16;
switch(alt)
{
case 0:
SIM_SCGC5 |= SIM_SCGC5_PORTA_MASK; // Turn on clock to A module
PORTA_PCR1 = PORT_PCR_MUX(2); // Set PTA1 to mux 2 [RX]
PORTA_PCR2 = PORT_PCR_MUX(2); // Set PTA2 to mux 2 [TX]
break;
case 1:
SIM_SCGC5 |= SIM_SCGC5_PORTA_MASK; // Turn on clock to A module
PORTA_PCR14 = PORT_PCR_MUX(3); // Set PTA14 to mux 3 [TX]
PORTA_PCR15 = PORT_PCR_MUX(3); // Set PTA15 to mux 3 [RX]
break;
case 2:
SIM_SCGC5 |= SIM_SCGC5_PORTB_MASK; // Turn on clock to B module
PORTB_PCR16 = PORT_PCR_MUX(3); // Set PTA14 to mux 3 [RX]
PORTB_PCR17 = PORT_PCR_MUX(3); // Set PTA15 to mux 3 [TX]
break;
case 3:
SIM_SCGC5 |= SIM_SCGC5_PORTD_MASK; // Turn on clock to D module
PORTD_PCR6 = PORT_PCR_MUX(3); // Set PTA14 to mux 3 [RX]
PORTD_PCR7 = PORT_PCR_MUX(3); // Set PTA15 to mux 3 [TX]
break;
case 4:
SIM_SCGC5 |= SIM_SCGC5_PORTE_MASK; // Turn on clock to E module
PORTE_PCR6 = PORT_PCR_MUX(3); // Set PTA14 to mux 3 [RX]
PORTE_PCR7 = PORT_PCR_MUX(3); // Set PTA15 to mux 3 [TX]
break;
default:
return false;
break;
}
}
else if(uart == (UART_MemMapPtr)UART1)
{
SIM_SCGC4 |= SIM_SCGC4_UART1_MASK;
if(baud_rate == 115200)
divisor = (uint16_t)(SystemCoreClock / 128 / baud_rate ) * 66; // 115200
else
divisor = (uint16_t)( SystemCoreClock / (baud_rate * 32) ); // 9600
switch(alt)
{
case 0:
SIM_SCGC5 |= SIM_SCGC5_PORTA_MASK; // Turn on clock to A module
PORTA_PCR18 = PORT_PCR_MUX(3); // Set PTA18 to mux 3 [RX]
PORTA_PCR19 = PORT_PCR_MUX(3); // Set PTA19 to mux 3 [TX]
break;
case 1:
SIM_SCGC5 |= SIM_SCGC5_PORTC_MASK; // Turn on clock to A module
PORTC_PCR3 = PORT_PCR_MUX(3); // Set PTC3 to mux 3 [RX]
PORTC_PCR4 = PORT_PCR_MUX(3); // Set PTC4 to mux 3 [TX]
break;
case 2:
SIM_SCGC5 |= SIM_SCGC5_PORTE_MASK; // Turn on clock to E module
PORTE_PCR0 = PORT_PCR_MUX(3); // Set PTE0 to mux 3 [TX]
PORTE_PCR1 = PORT_PCR_MUX(3); // Set PTE1 to mux 3 [RX]
break;
default:
return false;
break;
}
}
else if(uart == (UART_MemMapPtr)UART2)
{
SIM_SCGC4 |= SIM_SCGC4_UART2_MASK;
if(baud_rate == 115200)
divisor = (uint16_t)(SystemCoreClock / 128 / baud_rate ) * 66; // 115200
else
divisor = (uint16_t)( SystemCoreClock / (baud_rate * 32) ); // 9600
switch(alt)
{
case 0:
SIM_SCGC5 |= SIM_SCGC5_PORTD_MASK; // Turn on clock to A module
PORTD_PCR2 = PORT_PCR_MUX(3); // Set PTA18 to mux 3 [RX]
PORTD_PCR3 = PORT_PCR_MUX(3); // Set PTA19 to mux 3 [TX]
break;
case 1:
SIM_SCGC5 |= SIM_SCGC5_PORTD_MASK; // Turn on clock to A module
PORTD_PCR4 = PORT_PCR_MUX(3); // Set PTC3 to mux 3 [RX]
PORTD_PCR5 = PORT_PCR_MUX(3); // Set PTC4 to mux 3 [TX]
break;
case 2:
SIM_SCGC5 |= SIM_SCGC5_PORTE_MASK; // Turn on clock to E module
PORTE_PCR22 = PORT_PCR_MUX(3); // Set PTE0 to mux 3 [TX]
PORTE_PCR23 = PORT_PCR_MUX(3); // Set PTE1 to mux 3 [RX]
break;
default:
return false;
break;
}
}
else
{
return false;
}
//
uart->C2 &=~ (UART_C2_TE_MASK | UART_C2_RE_MASK);
uart->C1 = 0;
temp = uart->BDH & ~(UART_BDH_SBR(0x1F));
uart->BDH = ( temp | UART_BDH_SBR(((divisor & 0x1F00) >> 8)) );
uart->BDL = (uint16_t)(divisor & UART_BDL_SBR_MASK);
uart->C2 |= (UART_C2_TE_MASK | UART_C2_RE_MASK);
return true;
}
/****************************************************************************************
*
*****************************************************************************************/
void uart_Put(UART_MemMapPtr uart, uint8_t c)
{
while((uart->S1 & UART0_S1_TDRE_MASK) != UART0_S1_TDRE_MASK);
uart->D = c;
}
/****************************************************************************************
*
*****************************************************************************************/
uint8_t uart_Get(UART_MemMapPtr uart)
{
//while(!(UART0_S1 & 0x20));
while((uart->S1 & UART0_S1_TDRE_MASK) != UART0_S1_TDRE_MASK);
return uart->D;
}
/****************************************************************************************
*
*****************************************************************************************/
void uart_String(UART_MemMapPtr uart,char* txt )
{
while(*txt)
{
uart_Put(uart,*txt);
txt++;
}
}
/****************************************************************************************
*
*****************************************************************************************/
void uart_enable_irq(UART_MemMapPtr uart)
{
uart->C2 |= UART_C2_RIE_MASK;
//uart->C3 |= UART_C3_ORIE_MASK;
if(uart == UART1)
{
NVIC_EnableIRQ(UART1_IRQn);
}
else if(uart == UART2)
{
NVIC_EnableIRQ(UART2_IRQn);
}
else
{
NVIC_EnableIRQ(UART0_IRQn);
}
}
/****************************************************************************************
*
*****************************************************************************************/
void uart_add_callback(UART_MemMapPtr uart, void (*task)(void))
{
if(uart == UART1)
{
if(task != NULL)
{
uart1_task_irq = task;
}
}
else if(uart == UART2)
{
if(task != NULL)
{
uart2_task_irq = task;
}
}
else
{
if(task != NULL)
{
uart0_task_irq = task;
}
}
}
/****************************************************************************************
*
*****************************************************************************************/
void UART0_IRQHandler(void)
{
if(uart0_task_irq != NULL)
{
uart0_task_irq();
}
}
/****************************************************************************************
*
*****************************************************************************************/
void UART1_IRQHandler(void)
{
if(uart1_task_irq != NULL)
{
uart1_task_irq();
}
}
/****************************************************************************************
*
*****************************************************************************************/
void UART2_IRQHandler(void)
{
if(uart2_task_irq != NULL)
{
uart2_task_irq();
}
}
/****************************************************************************************
*
*****************************************************************************************/