This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetooth.c
305 lines (222 loc) · 6.1 KB
/
bluetooth.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
* @file bluetooth.c
* @brief Bluetooth library for KL46Z and HC-06 module
*/
#include "MKL46Z4.h"
#include "bluetooth.h"
#include <string.h>
#include "motordriver.h"
// Global variables
volatile UART_BUF_t RxBuf;
volatile UART_BUF_t TxBuf;
volatile int16_t string_count = 0;
char c;
/**
@brief Interrupt handler
@details It reacts to byte coming or empty D buffer (in UART module).
Incoming CR character is converted to NULL
(for comfortable usage of terminal e.g. Putty)
*/
#if (UART_MODULE == 0)
void UART0_IRQHandler(void){
#elif (UART_MODULE == 1)
void UART1_IRQHandler(void){
#elif (UART_MODULE == 2)
void UART2_IRQHandler(void){
#endif
__disable_irq();
if(UART(UART_MODULE)->S1 & UART_S1_RDRF_MASK){
char c = UART(UART_MODULE)->D;
#if OVERWRITE==1
if( c == '\0' || c == '\r' || c == '$'){
string_count++;
c = '\0';
}
#endif
if( !buf_full(&RxBuf) ){
#if OVERWRITE==0
if( c == '\0' || c == '\r' || c == '$'){
string_count++;
c = '\0';
}
#endif
to_UART_buffer( c, &RxBuf );
}
#if OVERWRITE==1
else overwrite_UART_buffer( c, &RxBuf );
#endif
}
else if(UART(UART_MODULE)->S1 & UART_S1_TDRE_MASK){
if( buf_empty(&TxBuf) ){
// disable interrupt from transmitter
UART(UART_MODULE)->C2 &= ~UART_C2_TIE_MASK;
TxBuf.tail = TxBuf.head; // force pointers equalization
// Sometimes, when transmitter is hardly loaded,
// tail pointer stops to far. "Size" indicator still works fine.
}
// If Tx buffer isn't empty put in UART next character.
else {
c = from_UART_buffer( &TxBuf );
if ( c != '\0') {
UART(UART_MODULE)->D = c;
}
}
}
NVIC_ClearPendingIRQ(UART0_IRQn);
__enable_irq();
}
void bt_init( uint32_t baud_rate ){
uint32_t divisor;
#if UART_MODULE==0
SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK; // PTA14(TX) PTA15(RX) on mux(3) will be used.
SIM->SOPT2 |= SIM_SOPT2_UART0SRC(2); // 8MHz ext osc
SIM->SCGC4 |= SIM_SCGC4_UART0_MASK;
PORTA->PCR[14] |= PORT_PCR_MUX(3);
PORTA->PCR[15] |= PORT_PCR_MUX(3);
#elif UART_MODULE==1
SIM->SCGC4 |= SIM_SCGC4_UART1_MASK; // 24MHz bus clk
SIM->SCGC5 |= SIM_SCGC5_PORTE_MASK; // PTE0(TX) PTE1(RX) on mux(3) will be used.
PORTE->PCR[0] |= PORT_PCR_MUX(3);
PORTE->PCR[1] |= PORT_PCR_MUX(3);
#elif UART_MODULE==2
SIM->SCGC4 |= SIM_SCGC4_UART2_MASK; // 24MHz bus clk
SIM->SCGC5 |= SIM_SCGC5_PORTE_MASK; // PTE16(TX) PTE17(RX) on mux(3) will be used.
PORTE->PCR[16] |= PORT_PCR_MUX(3);
PORTE->PCR[17] |= PORT_PCR_MUX(3);
#endif
// UART module disable
UART(UART_MODULE)->C2 &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK);
// Setting prescaler value
#if UART_MODULE==0
divisor = (8000000/baud_rate)/16;
#else
divisor = (24000000/baud_rate)/16;
#endif
// Cearing prescaler register
UART(UART_MODULE)->BDH &= ~UART_BDH_SBR_MASK;
UART(UART_MODULE)->BDL &= ~UART_BDL_SBR_MASK;
// Writting prescaler value to right register
UART(UART_MODULE)->BDH |= UART_BDH_SBR(divisor>>8);
UART(UART_MODULE)->BDL |= UART_BDL_SBR(divisor);
// One stop bit
UART(UART_MODULE)->BDH &= ~UART_BDH_SBNS_MASK;
// No parity
UART(UART_MODULE)->C1 &= ~UART_C1_PE_MASK;
// 8-bit mode
UART(UART_MODULE)->C1 &= ~UART_C1_M_MASK;
// Interrupt settings
#if UART_MODULE==0
NVIC_SetPriority(UART0_IRQn, UART_IRQ_PRIORITY);
NVIC_ClearPendingIRQ(UART0_IRQn);
NVIC_EnableIRQ(UART0_IRQn);
#elif UART_MODULE==1
NVIC_SetPriority(UART1_IRQn, UART_IRQ_PRIORITY);
NVIC_ClearPendingIRQ(UART1_IRQn);
NVIC_EnableIRQ(UART1_IRQn);
#elif UART_MODULE==2
NVIC_SetPriority(UART2_IRQn, UART_IRQ_PRIORITY);
NVIC_ClearPendingIRQ(UART2_IRQn);
NVIC_EnableIRQ(UART2_IRQn);
#endif
// Interrupts enable
UART(UART_MODULE)->C2 |= (UART_C2_TIE_MASK | UART_C2_RIE_MASK);
buf_clear(&TxBuf);
buf_clear(&RxBuf);
// UART module enable
UART(UART_MODULE)->C2 |= (UART_C2_TE_MASK | UART_C2_RE_MASK);
}
uint8_t bt_sendChar( char data ){
uint8_t exit = 0; // set failure
if( !buf_full(&TxBuf) ){
to_UART_buffer( data, &TxBuf );
exit = 1; // set success
}
#if OVERWRITE==1
else{
overwrite_UART_buffer( data, &TxBuf );
exit = 1; // set success
}
#endif
if( !(UART(UART_MODULE)->C2 & UART_C2_TIE_MASK) ){
UART(UART_MODULE)->D = from_UART_buffer( &TxBuf );
UART(UART_MODULE)->C2 |= UART_C2_TIE_MASK;
}
return exit;
}
uint8_t bt_sendStr( const char * source ){
uint16_t i = 0;
uint16_t len = strlen(source);
if( len != 0 ){
#if OVERWRITE==0
// If string length is greater than free space of TxBuf
if( ((len)+1) > ((BUFF_SIZE)-(TxBuf.size)) ) return 0; // return failure
#endif
// Send string byte by byte.
for(i=0; i<=len; i++) bt_sendChar( *((source)+i));
}
return 1; // return success
}
char bt_getChar( void ){
// If RxBuf isn't empty...
if( !buf_empty(&RxBuf) ){
char c;
// ... get one character
c = from_UART_buffer( &RxBuf );
if( c == '\0' ){
string_count--;
if(string_count < 0) string_count = 0; // addictional protection
}
return c;
}
else return 0; // If is empty return 0.
}
void bt_getStr( char * destination ){
// If in Rx buffer isn't any string return empty string.
if( string_count == 0 ) *destination = '\0';
else{
uint16_t i = 0;
char c;
// ...if is copy one to user array.
do{
c = bt_getChar();
*((destination)+i) = c;
i++;
}
while( c != '\0');
}
}
void buf_clear( volatile UART_BUF_t * b ){
uint16_t i;
for(i=0; i<BUFF_SIZE; i++) b->buf[i] = 0;
b->head = 0;
b->tail = 0;
b->size = 0;
}
uint8_t buf_empty( const volatile UART_BUF_t * b ){
return (b->size) == 0;
}
uint8_t buf_full( const volatile UART_BUF_t * b ){
return (b->size) == BUFF_SIZE;
}
void to_UART_buffer( const char c, volatile UART_BUF_t * b ){
b->buf[b->tail++] = c;
b->tail %= BUFF_SIZE;
b->size++;
}
char from_UART_buffer( volatile UART_BUF_t * b ){
char c = b->buf[b->head];
b->buf[b->head++] = 0;
b->head %= BUFF_SIZE;
b->size--;
return c;
}
void overwrite_UART_buffer( const char c, volatile UART_BUF_t * b ){
if( (b==&RxBuf) && (b->buf[b->tail] == '\0') ){
string_count--;
if(string_count < 0) string_count = 0;
}
b->head++;
b->head %= BUFF_SIZE;
b->buf[b->tail++] = c;
b->tail %= BUFF_SIZE;
}