Master Thesis  V1.0
Research and Design of Sensor Node for NMSD Treatment
ble.h File Reference

Send data via BLE wirelessly. More...

#include <stdint.h>
#include <stdbool.h>
Include dependency graph for ble.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define DIY   1
 
#define BUFFER_SIZE   80
 
#define BLE_ERROR   99
 
#define BLE_POWER_PIN   11
 
#define BLE_POWER_PORT   gpioPortB
 
#define BLE_PIN_TX   0
 
#define BLE_PIN_RX   1
 
#define BLE_PORT   gpioPortC
 
#define BLE_USART   USART1
 
#define BLE_LED2_PIN   10
 
#define BLE_LED2_PORT   gpioPortA
 
#define BLE_OUTPUT_POWER_4DB   0x04
 
#define BLE_OUTPUT_POWER_0DB   0x00
 
#define BLE_OUTPUT_POWER_N4DB   0xFC
 
#define BLE_OUTPUT_POWER_N8DB   0xF8
 
#define BLE_OUTPUT_POWER_N12DB   0xF4
 
#define BLE_OUTPUT_POWER_N16DB   0xF0
 
#define BLE_OUTPUT_POWER_N20DB   0xEC
 
#define BLE_OUTPUT_POWER_N40DB   0xD8
 

Functions

void BLE_power (bool enable)
 Turn power on/off for BLE module. More...
 
void BLE_Init ()
 BLE chip init. More...
 
void BLE_rxtx_enable (bool enable)
 Enable disable RX/TX pins from BLE module. More...
 
void BLE_connect ()
 Connect sequence BLE. More...
 
void BLE_check_connect ()
 Check if LED pin on BLE is high to check if the module is connected to a receiver. More...
 
void BLE_disconnect ()
 Disconnect BLE from receiver. More...
 
void BLE_sendData4 (uint8_t data_in[])
 Test function, not used. More...
 
void BLE_sendData (uint8_t *data, uint8_t *batt, uint8_t length, uint8_t *ble_packet)
 Send all the data over BLE. More...
 
void BLE_readData (uint8_t *readData, uint8_t length)
 Test function, not used. More...
 
void BLE_sendIMUData (uint8_t *gyroData, uint8_t *accelData, uint8_t *magnData)
 Test function, not used. More...
 
void float_to_uint8_t (float *input, uint8_t *out)
 Float to uint8_t conversion. More...
 
void float_to_uint8_t_x3 (float *input, uint8_t *out)
 Float to uint8_t conversion x3. More...
 
void BLE_set_output_power (uint8_t power)
 Set BLE output power. More...
 

Variables

char RX_buffer [BUFFER_SIZE]
 

Detailed Description

Send data via BLE wirelessly.

Version
1.0
Author
Jona Cappelle

Definition in file ble.h.

Macro Definition Documentation

◆ BLE_ERROR

#define BLE_ERROR   99

Definition at line 28 of file ble.h.

◆ BLE_LED2_PIN

#define BLE_LED2_PIN   10

Definition at line 44 of file ble.h.

◆ BLE_LED2_PORT

#define BLE_LED2_PORT   gpioPortA

Definition at line 45 of file ble.h.

◆ BLE_OUTPUT_POWER_0DB

#define BLE_OUTPUT_POWER_0DB   0x00

Definition at line 49 of file ble.h.

◆ BLE_OUTPUT_POWER_4DB

#define BLE_OUTPUT_POWER_4DB   0x04

Definition at line 48 of file ble.h.

◆ BLE_OUTPUT_POWER_N12DB

#define BLE_OUTPUT_POWER_N12DB   0xF4

Definition at line 52 of file ble.h.

◆ BLE_OUTPUT_POWER_N16DB

#define BLE_OUTPUT_POWER_N16DB   0xF0

Definition at line 53 of file ble.h.

◆ BLE_OUTPUT_POWER_N20DB

#define BLE_OUTPUT_POWER_N20DB   0xEC

Definition at line 54 of file ble.h.

◆ BLE_OUTPUT_POWER_N40DB

#define BLE_OUTPUT_POWER_N40DB   0xD8

Definition at line 55 of file ble.h.

◆ BLE_OUTPUT_POWER_N4DB

#define BLE_OUTPUT_POWER_N4DB   0xFC

Definition at line 50 of file ble.h.

◆ BLE_OUTPUT_POWER_N8DB

#define BLE_OUTPUT_POWER_N8DB   0xF8

Definition at line 51 of file ble.h.

◆ BLE_PIN_RX

#define BLE_PIN_RX   1

Definition at line 34 of file ble.h.

◆ BLE_PIN_TX

#define BLE_PIN_TX   0

Definition at line 33 of file ble.h.

◆ BLE_PORT

#define BLE_PORT   gpioPortC

Definition at line 35 of file ble.h.

◆ BLE_POWER_PIN

#define BLE_POWER_PIN   11

Definition at line 31 of file ble.h.

◆ BLE_POWER_PORT

#define BLE_POWER_PORT   gpioPortB

Definition at line 32 of file ble.h.

◆ BLE_USART

#define BLE_USART   USART1

Definition at line 36 of file ble.h.

◆ BUFFER_SIZE

#define BUFFER_SIZE   80

Definition at line 25 of file ble.h.

◆ DIY

#define DIY   1

Definition at line 22 of file ble.h.

Function Documentation

◆ 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.

Note
There is some delay on the actual connection and the pin turning high

Definition at line 179 of file ble.c.

180 {
181  bleConnected = GPIO_PinInGet(BLE_LED2_PORT, BLE_LED2_PIN);
182 }

References BLE_LED2_PIN, BLE_LED2_PORT, and bleConnected.

Referenced by measure_send().

◆ BLE_connect()

void BLE_connect ( )

Connect sequence BLE.

The only function that's not send interrupt based (can be a future improvement)

Definition at line 152 of file ble.c.

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 }

Referenced by measure_send().

◆ BLE_disconnect()

void BLE_disconnect ( )

Disconnect BLE from receiver.

Note
takes approx 1sec

Definition at line 195 of file ble.c.

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 }

◆ BLE_Init()

void BLE_Init ( )

BLE chip init.

Init UART Init BLE power pin

Definition at line 61 of file ble.c.

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 }

References BLE_LED2_PIN, BLE_LED2_PORT, and uart_Init().

◆ BLE_power()

void BLE_power ( bool  enable)

Turn power on/off for BLE module.

Note
Extra 30 µF capacitor needed on VDD line to prevent too much voltage drop
Parameters
[in]enable
  • 'true' - BLE on
  • 'false' - BLE off

Definition at line 84 of file ble.c.

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 }

References BLE_POWER_PIN, and BLE_POWER_PORT.

Referenced by measure_send().

◆ BLE_readData()

void BLE_readData ( uint8_t *  readData,
uint8_t  length 
)

Test function, not used.

Definition at line 308 of file ble.c.

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 }

References uartGetData().

◆ BLE_rxtx_enable()

void BLE_rxtx_enable ( bool  enable)

Enable disable RX/TX pins from BLE module.

Note
Disable when going to sleep, can otherwise draw some unwanted power
Parameters
[in]enable
  • 'true' - enable
  • 'false' - disable

Definition at line 128 of file ble.c.

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 }

References BLE_PIN_RX, BLE_PIN_TX, and BLE_PORT.

◆ BLE_sendData()

void BLE_sendData ( uint8_t *  data,
uint8_t *  batt,
uint8_t  length,
uint8_t *  ble_data 
)

Send all the data over BLE.

Parameters
[in]dataEuler angles
[in]lengthpacket length
[in]battbattery in percentage
[out]ble_datareturn ble_data for debugging purposes

Definition at line 261 of file ble.c.

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 }

References data, and uartPutData().

Referenced by measure_send().

◆ BLE_sendData4()

void BLE_sendData4 ( uint8_t  data_in[])

Test function, not used.

Definition at line 212 of file ble.c.

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 }

References data.

◆ BLE_sendIMUData()

void BLE_sendIMUData ( uint8_t *  gyroData,
uint8_t *  accelData,
uint8_t *  magnData 
)

Test function, not used.

Definition at line 334 of file ble.c.

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 }

References data.

◆ BLE_set_output_power()

void BLE_set_output_power ( uint8_t  power)

Set BLE output power.

Momentarily not used, since the reset value of the BLE is the one used

Parameters
[in]powerpower value (see datasheet for options)

Definition at line 441 of file ble.c.

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 }

References data, and uartPutData().

◆ float_to_uint8_t()

void float_to_uint8_t ( float *  input,
uint8_t *  out 
)

Float to uint8_t conversion.

Parameters
[in]inputfloat
[out]out4 x uint8_t's

Definition at line 391 of file ble.c.

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 }

Referenced by float_to_uint8_t_x3().

◆ float_to_uint8_t_x3()

void float_to_uint8_t_x3 ( float *  in,
uint8_t *  out 
)

Float to uint8_t conversion x3.

Parameters
[in]inputfloat
[out]out4 x uint8_t's

Definition at line 419 of file ble.c.

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 }

References float_to_uint8_t().

Referenced by measure_send().

Variable Documentation

◆ RX_buffer

char RX_buffer[BUFFER_SIZE]

Definition at line 26 of file ble.h.

data
MeasurementData_t data
Definition: main.c:122
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
uart_Init
void uart_Init()
UART init.
Definition: uart.c:57
bleConnected
bool bleConnected
Definition: main.c:125
BLE_POWER_PIN
#define BLE_POWER_PIN
Definition: ble.h:31
BLE_PORT
#define BLE_PORT
Definition: ble.h:35
BLE_POWER_PORT
#define BLE_POWER_PORT
Definition: ble.h:32
BLE_LED2_PIN
#define BLE_LED2_PIN
Definition: ble.h:44
BLE_PIN_TX
#define BLE_PIN_TX
Definition: ble.h:33
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
uartGetData
uint32_t uartGetData(uint8_t *dataPtr, uint32_t dataLen)
uartGetData function
Definition: uart.c:185