-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuart.c
265 lines (224 loc) · 7.26 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
261
262
263
264
265
#include "uart.h"
//#define BAUD_RATE 115200
#define BAUD_RATE 57600
#define UART_RB_SIZE 32
#define UART_MSG_SIZE 6
#define RX_TIMEOUT 2 /* 2 seconds */
#define BT_RESP_SIZE 5
/* Transmit and receive ring buffers */
STATIC RINGBUFF_T rxring, txring;
/* Ring buffer size */
static uint8_t UART_data_RX[UART_RB_SIZE];
static uint8_t UART_data_TX[UART_RB_SIZE];
/* Variables to indicate stale RX */
volatile bool RX_new_data = false;
void UART_init (void)
{
RingBuffer_Init(&rxring, UART_data_RX, 1, UART_RB_SIZE);
RingBuffer_Init(&txring, UART_data_TX, 1, UART_RB_SIZE);
Chip_Clock_SetUARTClockDiv(1);
/* Setup UART */
Chip_UART_Init(LPC_USART0);
Chip_UART_ConfigData(LPC_USART0, UART_CFG_DATALEN_8 | UART_CFG_PARITY_NONE | UART_CFG_STOPLEN_1 );
Chip_Clock_SetUSARTNBaseClockRate((BAUD_RATE * 16), true);
Chip_UART_SetBaud(LPC_USART0, BAUD_RATE);
Chip_UART_Enable(LPC_USART0);
Chip_UART_TXEnable(LPC_USART0);
/* Enable receive data and line status interrupt */
Chip_UART_IntEnable(LPC_USART0, UART_INTEN_RXRDY);
//Chip_UART_IntDisable(LPC_USART0, UART_INTEN_RXRDY);
Chip_UART_IntDisable(LPC_USART0, UART_INTEN_TXRDY); /* May not be needed */
NVIC_EnableIRQ(UART0_IRQn);
}
void UART0_IRQHandler (void)
{
if((Chip_UART_GetStatus(LPC_USART0) & UART_STAT_RXRDY) != 0)
{
RX_new_data = true;
}
Chip_UART_IRQRBHandler(LPC_USART0, &rxring, &txring);
}
bool set_BT_power_save (volatile time_t* curr_time)
{
static BT_states state = CMD;
static BT_states prev_state;
bool setting_done = false;
char rx_data[BT_RESP_SIZE + 1]; /* +1 do add null terminator */
char *tx_data;
int32_t curr_time_stamp;
switch (state)
{
case CMD:
tx_data = "$$$";
Chip_UART_SendRB(LPC_USART0, &txring, tx_data, 3); /* actual size of tx_data is 4, therefore -1 because we don't want to send null terminator */
prev_state = state;
state = WAIT;
break;
case WAIT:
curr_time_stamp = curr_time->seconds + curr_time->minutes*60 + curr_time->hours*360;
if (RingBuffer_GetCount(&rxring) == BT_RESP_SIZE)
{
Chip_UART_IntDisable(LPC_USART0, UART_INTEN_RXRDY); /* disable RX interrupt to protect integrity of rxring buffer during reading */
Chip_UART_ReadRB(LPC_USART0, &rxring, rx_data, BT_RESP_SIZE);
Chip_UART_IntEnable(LPC_USART0, UART_INTEN_RXRDY);
rx_data[BT_RESP_SIZE] = '\0';
if ((strcmp(rx_data, "CMD\r\n") == 0) || (strcmp(rx_data, "AOK\r\n") == 0) || (strcmp(rx_data, "END\r\n") == 0))
{
prev_state++;
state = prev_state;
}
else
{
/* message was not sucessfull */
state = (prev_state == EXIT) ? END : EXIT; /* if previous state was EXIT, then END, otherwise try to exit set mode wit waiting for "END\r\n" sent by BT*/
}
}
if (UART_check_timeout(curr_time_stamp))
{
state = (prev_state == EXIT) ? END : EXIT;
}
break;
case SET_BT_SI:
tx_data = "SI,0012\r"; /* set Discover window to the lowest value */
Chip_UART_SendRB(LPC_USART0, &txring, tx_data, 8);
prev_state = state;
state = WAIT;
break;
case SET_BT_SJ:
tx_data = "SJ,0012\r"; /* set Connect window to the lowest value */
Chip_UART_SendRB(LPC_USART0, &txring, tx_data, 8);
prev_state = state;
state = WAIT;
break;
case EXIT:
tx_data = "---\r\n";
Chip_UART_SendRB(LPC_USART0, &txring, tx_data, 5);
prev_state = state;
state = WAIT;
break;
case END:
setting_done = true;
RingBuffer_Flush(&rxring);
RingBuffer_Flush(&txring);
break;
default:
state = END;
}
return setting_done;
}
void UART_commands_exec(volatile time_t* time_to_set, volatile display_t* user_data_to_set)
{
uint8_t data[UART_MSG_SIZE];
int32_t curr_time_stamp;
/* need to combine ss/mm/hh to adress corner cases when time is 20:59, for example. If I stored only seconds,
timeout difference checked below would be incorrect (01 - 59)*/
curr_time_stamp = time_to_set->seconds + time_to_set->minutes*60 + time_to_set->hours*360;
while (RingBuffer_GetCount(&rxring) >= UART_MSG_SIZE)
{
Chip_UART_IntDisable(LPC_USART0, UART_INTEN_RXRDY); /* disable RX interrupt to protect integrity of rxring buffer during reading */
Chip_UART_ReadRB(LPC_USART0, &rxring, data, UART_MSG_SIZE);
Chip_UART_IntEnable(LPC_USART0, UART_INTEN_RXRDY);
if (data[0] == START_FLAG)
{
switch (data[1])
{
case (SET | UART_TIME):
time_to_set->seconds = data[4];
time_to_set->minutes = data[3];
time_to_set->hours = data[2];
break;
case (SET | UART_DATE):
time_to_set->years = data[4] << 8 | data[5];
time_to_set->months = data[3];
time_to_set->days = data[2];
break;
case (SET | SHOW_INTERVALS):
if (data[2] > 2) /* do not set new setting if less than 3*/
{
time_to_set->show_time = data[2];
}
if (data[3] > 2)
{
time_to_set->show_date = data[3];
}
if (data[4] > 2)
{
time_to_set->show_user_data = data [4];
}
break;
case (PING):
memset(data, 0x0, sizeof(data));
data[0] = START_FLAG;
data[1] = ALIVE;
Chip_UART_SendRB(LPC_USART0, &txring, data, UART_MSG_SIZE);
break;
case (GET | UART_TIME):
data[0] = START_FLAG;
data[1] = UART_TIME;
data[2] = time_to_set->hours;
data[3] = time_to_set->minutes;
data[4] = time_to_set->seconds;
data[5] = 0;
Chip_UART_SendRB(LPC_USART0, &txring, data, UART_MSG_SIZE);
break;
case (GET | UART_DATE):
data[0] = START_FLAG;
data[1] = UART_DATE;
data[2] = time_to_set->days;
data[3] = time_to_set->months;
data[4] = time_to_set->years >> 8;
data[5] = time_to_set->years & 0xFF;
Chip_UART_SendRB(LPC_USART0, &txring, data, UART_MSG_SIZE);
break;
case (GET | SHOW_INTERVALS):
data[0] = START_FLAG;
data[1] = SHOW_INTERVALS;
data[2] = time_to_set->show_time;
data[3] = time_to_set->show_date;
data[4] = time_to_set->show_user_data;
data[5] = 0;
Chip_UART_SendRB(LPC_USART0, &txring, data, UART_MSG_SIZE);
break;
case (DISP):
time_to_set->change_display_timeout = 0;
time_to_set->curr_displayed = USER_DATA;
user_data_to_set->hours = data[2];
user_data_to_set->minutes = data[3];
user_data_to_set->seconds = data[4];
break;
case (TOGGLE):
if (time_to_set->curr_displayed == DATE)
{
time_to_set->curr_displayed = TIME | LOCK;
time_to_set->change_display_timeout = 0;
}
else if (time_to_set->curr_displayed == TIME)
{
time_to_set->curr_displayed = DATE | LOCK;
time_to_set->change_display_timeout = 0;
}
break;
}
}
}
UART_check_timeout(curr_time_stamp);
}
bool UART_check_timeout (int32_t current_time_stamp)
{
static int32_t time_stamp = 0;
if (RX_new_data)
{
time_stamp = current_time_stamp;
RX_new_data = false;
}
else if ((RingBuffer_GetCount(&rxring) > 0) &&
((current_time_stamp - time_stamp) >= RX_TIMEOUT))
{
/*if timeout, flush the incomplete rx ring buffer to have it clean for next incoming messages if connection is established again */
Chip_UART_IntDisable(LPC_USART0, UART_INTEN_RXRDY); /* disable RX interrupt to protect integrity of rxring buffer during flushing */
RingBuffer_Flush(&rxring);
Chip_UART_IntEnable(LPC_USART0, UART_INTEN_RXRDY);
return true;
}
return false;
}