Master Thesis  V1.0
Research and Design of Sensor Node for NMSD Treatment
ble.c
Go to the documentation of this file.
1 /*
2  * ble.c
3  *
4  * Created on: Nov 27, 2019
5  * Author: jonac
6  */
7 
8 /***************************************************************************/
15 #include "ble.h"
16 
17 #include "em_device.h"
18 #include "em_chip.h"
19 #include "em_cmu.h"
20 #include "em_emu.h"
21 #include "em_gpio.h"
22 #include "em_usart.h"
23 #include "em_rtc.h"
24 #include "em_adc.h"
25 #include "em_wdog.h"
26 #include "rtcdriver.h"
27 #include "em_core.h"
28 
29 #include "debug_dbprint.h"
30 #include "delay.h"
31 //#include "ICM20948.h"
32 #include "interrupt.h"
33 #include "adc.h"
34 
35 #include "pinout.h"
36 
37 #include <stdint.h>
38 #include <stdbool.h>
39 
40 #include "uart.h"
41 
42 bool ble_Initialized = false;
43 
44 bool check = false;
45 
46 uint8_t buffer[80];
47 uint8_t BLE_POWER_ON_RETURN[7] = { 0x02, 0x41, 0x02, 0x00, 0x01, 0x01, 0x41 };
48 
49 extern bool bleConnected;
50 
51 
52 /**************************************************************************/
61 void BLE_Init()
62 {
63  uart_Init();
64 
65  /* Set pinmode for reading BLE LED 2 state */
66  GPIO_PinModeSet(BLE_LED2_PORT, BLE_LED2_PIN, gpioModeInputPullFilter, 0);
67 }
68 
69 
70 /**************************************************************************/
84 void BLE_power(bool enable)
85 {
86  /* Initialize VDD pin if not already the case */
87 // if (!ble_Initialized)
88 // {
89 // /* In the case of gpioModePushPull", the last argument directly sets the pin state */
90 // GPIO_PinModeSet(BLE_POWER_PORT, BLE_POWER_PIN, gpioModePushPull, enable);
91 
92  /* Check if the right sequence is returned */
93 // BLE_readData( buffer, BUFFER_SIZE );
94 //
95 // check = Check_Equal(buffer, BLE_POWER_ON_RETURN, 7);
96 // if ( check != true )
97 // {
98 // return BLE_ERROR;
99 // }
100 
101 // ble_Initialized = true;
102 // }
103 // else
104 // {
105  if (enable) GPIO_PinModeSet(BLE_POWER_PORT, BLE_POWER_PIN, gpioModePushPull, 1); /* Enable VDD pin */
106  else {
107  GPIO_PinModeSet(BLE_POWER_PORT, BLE_POWER_PIN, gpioModeDisabled, 0); /* Disable VDD pin */
108  }
109 // }
110 // return 0;
111 }
112 
113 
114 /**************************************************************************/
128 void BLE_rxtx_enable(bool enable)
129 {
130  if(enable)
131  {
132  GPIO_PinModeSet(BLE_PORT, BLE_PIN_RX, gpioModeInput, 0);
133  GPIO_PinModeSet(BLE_PORT, BLE_PIN_TX, gpioModePushPull, 1);
134  }else{
135  /* In ACTION_SLEEP mode the UART is disabled, so the module will not receive or transmit any
136 data. To prevent leakage current, the host shall not pull the UART_RX to LOW level (as the
137 module has an internal pull-up resistor enabled on this pin).*/
138  GPIO_PinModeSet(BLE_PORT, BLE_PIN_RX, gpioModeDisabled, 0);
139  GPIO_PinModeSet(BLE_PORT, BLE_PIN_TX, gpioModeDisabled, 0);
140  }
141 }
142 
143 /**************************************************************************/
153 {
154 // while(!bleConnected)
155 // {
156  /* Check if BLE is connected to device */
157 // BLE_check_connect();
158 
159  //Connect A -> B
160  const char connect[] = { 0x02, 0x06, 0x06, 0x00, 0xBB, 0x04, 0x20, 0xDA, 0x18, 0x00, 0x5F };
161  for (int i=0 ; i < sizeof(connect) ; i++ )
162  {
163  USART_Tx(USART1, connect[i]);
164  }
165 // }
166 
167 }
168 
169 
170 /**************************************************************************/
180 {
181  bleConnected = GPIO_PinInGet(BLE_LED2_PORT, BLE_LED2_PIN);
182 }
183 
184 
185 /**************************************************************************/
196 {
197  //Disconnect A -> B
198  const char disconnect[] = { 0x02, 0x07, 0x00, 0x00, 0x05 };
199  for (int i=0 ; i < sizeof(disconnect) ; i++ )
200  {
201  USART_Tx(USART1, disconnect[i]);
202  }
203 }
204 
205 
206 /**************************************************************************/
212 void BLE_sendData4(uint8_t data_in[])
213 {
214  int d=0;
215 
216  //Preface
217  /* Start signal - command - length - payload - checksum */
218  uint8_t data[9] = {0x02, 0x04, 0x04, 0x00 };
219 
220  //concatenate
221  while (d < (4))
222  {
223  data[4+d] = data_in[d];
224  d++;
225  }
226 
227 
228  //calculate Checksum CS
229  uint8_t checksum = 0x00;
230  for (int j=0;j<(sizeof(data)/sizeof(uint8_t));j++)
231  {
232  checksum = checksum^data[j];
233  }
234 
235  data[8] = checksum;
236 
237 
238  //Send data out
239  for (int i = 0; i < 9; i++)
240  {
241  USART_Tx(USART1, data[i]);
242  }
243 }
244 
245 
246 /**************************************************************************/
261 void BLE_sendData( uint8_t *data, uint8_t *batt, uint8_t length, uint8_t *ble_data ) // max packet length: 255
262 {
263  int ble_packet_length = length + 5;
264  // uint8_t ble_data[ble_packet_length];
265  int d = 0;
266 
267  ble_data[0] = 0x02;
268  ble_data[1] = 0x04;
269  ble_data[2] = length;
270  ble_data[3] = 0x00;
271 
272  while ( d < length)
273  {
274  ble_data[4+d] = data[d];
275  d++;
276  }
277 
278  ble_data[ble_packet_length-2] = batt[0];
279 
280  //calculate Checksum CS
281  uint8_t checksum = 0x00;
282  for (int j=0;j<(ble_packet_length-1);j++)
283  {
284  checksum = checksum^ble_data[j];
285  }
286 
287  ble_data[ble_packet_length-1] = checksum;
288 
289  //Send data out
290 // for (int i = 0; i < ble_packet_length; i++)
291 // {
292 // USART_Tx(USART1, ble_data[i]);
293 // }
294 
295  /* Send data interrupt driven */
296 
297  uartPutData( ble_data, ble_packet_length );
298 
299 }
300 
301 
302 /**************************************************************************/
308 void BLE_readData( uint8_t *readData, uint8_t length )
309 {
310 // while (1)
311 // {
312 // // Read a line from the UART
313 // for (int i = 0; i < BUFFER_SIZE - 1 ; i++ )
314 // {
315 // RX_buffer[i] = USART_Rx(USART1);
316 // if (RX_buffer[i] == '\r')
317 // {
318 // break; // Breaking on CR prevents it from being counted in the number of bytes
319 // }
320 // }
321 // }
322 
323  uartGetData( readData, length );
324 
325 }
326 
327 
328 /**************************************************************************/
334 void BLE_sendIMUData(uint8_t *gyroData, uint8_t *accelData, uint8_t *magnData)
335 {
336  int d=0;
337 
338  //Preface
339  /* Start signal - command - length - payload - checksum */
340  char data[23] = { 0x02, 0x04, 0x06, 0x00 };
341 
342  //concatenate
343  while (d < 6)
344  {
345  data[4+d] = gyroData[d];
346  d++;
347  }
348  d=0;
349  while (d < 6)
350  {
351  data[10+d] = accelData[d];
352  d++;
353  }
354  d=0;
355  while (d < 6)
356  {
357  data[16+d] = magnData[d];
358  d++;
359  }
360 
361  //calculate Checksum CS
362  char checksum = 0x00;
363  for (int j=0;j<(sizeof(data)/sizeof(char));j++)
364  {
365  checksum = checksum^data[j];
366  }
367 
368  data[22] = checksum;
369 
370 
371  //Send data out
372  for (int i = 0; i < 23; i++) //i runs until length of char data[]
373  {
374  USART_Tx(USART1, data[i]);
375  }
376 }
377 
378 
379 /**************************************************************************/
391 void float_to_uint8_t( float *input, uint8_t *out )
392 {
393  union{
394  float in;
395  uint8_t bytes[sizeof(float)];
396  }thing;
397 
398  thing.in = input[0];
399 
400  for( int i=0; i<sizeof(float); i++)
401  {
402  out[i] = thing.bytes[i];
403  }
404 }
405 
406 
407 /**************************************************************************/
419 void float_to_uint8_t_x3( float *in, uint8_t *out ) // Converts euler angles to uint8_t to be transmitted on ble
420 {
421 
422  float_to_uint8_t( &in[0], &out[0] );
423  float_to_uint8_t( &in[1], &out[4] );
424  float_to_uint8_t( &in[2], &out[8] );
425 
426 }
427 
428 
429 /**************************************************************************/
441 void BLE_set_output_power( uint8_t power )
442 {
443  int len = 7;
444  uint8_t data[7];
445 
446  data[0] = 0x02;
447  data[1] = 0x11;
448  data[2] = 0x02;
449  data[3] = 0x00;
450  data[4] = 0x11;
451  data[5] = power;
452 
453  //calculate Checksum CS
454  uint8_t checksum = 0x00;
455  for (int j=0;j<(7-1);j++)
456  {
457  checksum = checksum^data[j];
458  }
459 
460  data[len-1] = checksum;
461 
462  uartPutData( data, len );
463 }
464 
BLE_POWER_ON_RETURN
uint8_t BLE_POWER_ON_RETURN[7]
Definition: ble.c:47
data
MeasurementData_t data
Definition: main.c:122
BLE_sendData
void BLE_sendData(uint8_t *data, uint8_t *batt, uint8_t length, uint8_t *ble_data)
Send all the data over BLE.
Definition: ble.c:261
BLE_PIN_RX
#define BLE_PIN_RX
Definition: ble.h:34
uartPutData
void uartPutData(uint8_t *dataPtr, uint32_t dataLen)
uartPutData function
Definition: uart.c:148
BLE_power
void BLE_power(bool enable)
Turn power on/off for BLE module.
Definition: ble.c:84
buffer
uint8_t buffer[80]
Definition: ble.c:46
adc.h
Read battery voltage.
ble_Initialized
bool ble_Initialized
Definition: ble.c:42
BLE_set_output_power
void BLE_set_output_power(uint8_t power)
Set BLE output power.
Definition: ble.c:441
BLE_rxtx_enable
void BLE_rxtx_enable(bool enable)
Enable disable RX/TX pins from BLE module.
Definition: ble.c:128
uart_Init
void uart_Init()
UART init.
Definition: uart.c:57
bleConnected
bool bleConnected
Definition: main.c:125
BLE_check_connect
void BLE_check_connect()
Check if LED pin on BLE is high to check if the module is connected to a receiver.
Definition: ble.c:179
BLE_POWER_PIN
#define BLE_POWER_PIN
Definition: ble.h:31
uart.h
UART communication with BLE.
interrupt.h
Code to detect and generate interrupts.
BLE_PORT
#define BLE_PORT
Definition: ble.h:35
BLE_connect
void BLE_connect()
Connect sequence BLE.
Definition: ble.c:152
delay.h
Delay functionality.
check
bool check
Definition: ble.c:44
BLE_POWER_PORT
#define BLE_POWER_PORT
Definition: ble.h:32
BLE_sendData4
void BLE_sendData4(uint8_t data_in[])
Test function, not used.
Definition: ble.c:212
pinout.h
File to keep track of most the pins used, pins for BLE are in own file BLE.h.
BLE_sendIMUData
void BLE_sendIMUData(uint8_t *gyroData, uint8_t *accelData, uint8_t *magnData)
Test function, not used.
Definition: ble.c:334
BLE_readData
void BLE_readData(uint8_t *readData, uint8_t length)
Test function, not used.
Definition: ble.c:308
BLE_LED2_PIN
#define BLE_LED2_PIN
Definition: ble.h:44
float_to_uint8_t_x3
void float_to_uint8_t_x3(float *in, uint8_t *out)
Float to uint8_t conversion x3.
Definition: ble.c:419
BLE_PIN_TX
#define BLE_PIN_TX
Definition: ble.h:33
debug_dbprint.h
Enable or disable printing to UART with dbprint.
BLE_disconnect
void BLE_disconnect()
Disconnect BLE from receiver.
Definition: ble.c:195
BLE_Init
void BLE_Init()
BLE chip init.
Definition: ble.c:61
float_to_uint8_t
void float_to_uint8_t(float *input, uint8_t *out)
Float to uint8_t conversion.
Definition: ble.c:391
BLE_LED2_PORT
#define BLE_LED2_PORT
Definition: ble.h:45
ble.h
Send data via BLE wirelessly.
uartGetData
uint32_t uartGetData(uint8_t *dataPtr, uint32_t dataLen)
uartGetData function
Definition: uart.c:185