CAN to BLE translator - and back
Dependencies: BLE_API CANnucleo X_NUCLEO_IDB0XA1 mbed
Diff: main.cpp
- Revision:
- 3:5bce2e8d2797
- Parent:
- 2:bfe8810290ac
- Child:
- 4:8c2cd88d2545
--- a/main.cpp Fri Apr 08 07:40:08 2016 +0000
+++ b/main.cpp Fri Apr 08 12:21:07 2016 +0000
@@ -1,18 +1,30 @@
#include "mbed.h"
#include "ble/BLE.h"
#include "CAN.h"
-#define TARGET_NUCLEO_F072RB 1
+#define TARGET_NUCLEO_F072RB 1
#define LED_PIN PA_5
+#define BLE_GATT_CHAR_PROPERTIES_NOTIFY 0x10
+
uint8_t CANId2BLESlot(unsigned int id);
unsigned int BLESlot2CANId(uint8_t id);
void onMsgReceived(void);
void initCAN(void);
-DigitalOut led(LED_PIN);
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params);
+void writeCharCallback(const GattWriteCallbackParams *params);
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params);
+void onBleInitError(BLE &ble, ble_error_t error);
+void initBLE(void);
+DigitalOut led(LED_PIN),CAN_show(PC_12);
+
+
+const static char DEVICE_NAME[] = "STNucleo - RGM - FM";
+static const uint16_t uuid16_list[] = {0xFFFF};
+
//const unsigned int RX_ID = 0x10;
//const unsigned int TX_ID = 0x11;
@@ -25,70 +37,163 @@
uint8_t bleWrPointerB= 255;
uint8_t bleRdPointerB= 255;
-Timer timerA, timerB; //questi due contatori servono solo per dire: appena è passato un po' di tempo -> esegui
+const uint8_t firstBleRdPointer=1;
+const uint8_t lastBleRdPointer=14;
+
+const uint8_t firstCanRdPointer=62;
+const uint8_t lastCanRdPointer=63;
+
+uint8_t canRdLastPointer= 255; // puntatore per sapere che cosa ho mandato per ultimo. a runtime devo andare a modificarlo per sapere al ciclo dopo cosa ho già mandato
+uint8_t bleRdLastPointer= 255; // puntatore per sapere che cosa ho mandato per ultimo. a runtime devo andare a modificarlo per sapere al ciclo dopo cosa ho già mandato
+
+Timer timerA; //questo contatoro serve solo per dire: appena è passato un po' di tempo -> esegui
//CAN can(PB_8, PB_9); // CAN Rx pin name, CAN Tx pin name
-
-CAN can(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name
CANMessage rxMsg;
CANMessage txMsg;
+BLE ble;
+
+uint16_t customServiceUUID = 0xA000; // service UUID
+uint16_t readCharUUID = 0xA001; // read characteristic UUID
+uint16_t writeCharUUID = 0xA002; // write characteristic UUID
+
+static uint8_t readValue[128] = {0};
+
+//static uint8_t readValue[6]="HELLO";
+
+
+ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue, BLE_GATT_CHAR_PROPERTIES_NOTIFY , NULL,0); //aggiunto il BLE_GATT_CHAR_PROPERTIES_NOTIFY => appena arriva lo rimanda
+
+static uint8_t writeValue[128] = {0};
+WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue);
+
+GattCharacteristic *characteristics[] = {&readChar, &writeChar};
+GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
+uint8_t retry=1;
+uint8_t readdata[20]= {};
+char symbol=' ';
volatile bool CANmsgAvailable = false;
-
+volatile bool BLExmit = false;
+float stopTimer=2.0;
int main()
{
- uint8_t j=0;
- printf("\r\nBoard started\r\n");
+ uint8_t j=0,k=firstBleRdPointer;
+ // printf("\r\nBoard started\r\n");
led = 1; // turn LED on
- initCAN();
+ CAN_show=1;
+
+ bleRdLastPointer=lastBleRdPointer;
+ BLE::Instance().init(bleInitComplete);
+ CAN can(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name
+ CAN_show=0;
+
+ //canRdLastPointer=lastCanRdPointer;
+ can.frequency(500000); // set bit rate to 500kbps as S018
+ printf("\r\nCAN started at 500kbps\r\n");
timerA.start();
- timerB.start();
- while(1) {
- if(timerA.read()>=1.0) {
+ can.attach(&onMsgReceived, CAN::RxIrq); // attach 'CAN receive-complete' interrupt handler
+
+ while(true) {
+ if(ble.getGapState().connected) {
+ stopTimer=0.2;
+ symbol='!';
+
+
+ } else {
+ stopTimer=3;
+ ble.waitForEvent();
+ symbol='.';
+ }
+
+ if(timerA.read()>=stopTimer) {
+ BLExmit=ble.getGapState().connected;
timerA.stop();
timerA.reset();
led=!led.read();
+ printf("%c\r\n",symbol);
timerA.start();
- }
- if(timerB.read()>=1.0) {
- timerB.stop();
- timerB.reset();
- //led=!led.read();
- timerB.start();
+
}
if(CANmsgAvailable) {
CANmsgAvailable = false; // reset flag for next use
can.read(rxMsg); // read message into Rx message storage
j=CANId2BLESlot(rxMsg.id);
if(j!=bleRdPointerA && j!=bleRdPointerB) {
- //write if BLE is not reading
- canWrPointer=j;
- printf("CAN message received:\r\n");
- printf(" ID = %#x -> %d \r\n", rxMsg.id, j);
- // printf(" Type = %d\r\n", rxMsg.type);
- // printf(" Format = %d\r\n", rxMsg.format);
- // printf(" Length = %d\r\n", rxMsg.len);
- printf(" Data =");
+
for(int i = 0; i < rxMsg.len; i++) {
shareddata[j][i]=rxMsg.data[i];
- printf(" %x", rxMsg.data[i]);
+ // printf(" %x", rxMsg.data[i]);
}
- printf("\r\n");
- canWrPointer=255;
+ // printf("#");
+
}
- else
- {
- printf("CAN message %#x dropped because BLE read\r\n",rxMsg.id);
+ }
+ if(BLExmit) {
+ BLExmit=false;
+ retry++;
+ if(retry==0xff) ble.gap().startAdvertising();
+ printf ("%x ",retry);
+ printf("@");
+ k++;
+ if(k>lastBleRdPointer)
+ k=firstBleRdPointer;
+ readValue[0]=k;
+ for(int i=1; i<8; i++) {
+ readValue[i]=shareddata[k][i];
}
+ for(int i=0; i<8; i++) {
+ printf ("%x",readValue[i]);
+ }
+ ble.updateCharacteristicValue(readChar.getValueHandle(), readValue,9);
+
+
}
}
}
-void initCAN(void)
+
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
{
- can.frequency(500000); // set bit rate to 500kbps as S018
- can.attach(&onMsgReceived, CAN::RxIrq); // attach 'CAN receive-complete' interrupt handler
- printf("\r\nCAN started at 500kbps\r\n");
+ BLE& ble = params->ble;
+ ble_error_t error = params->error;
+
+ if (error != BLE_ERROR_NONE) {
+ onBleInitError(ble, error);
+ return;
+ }
+
+ if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
+ return;
+ }
+
+ ble.gap().onDisconnection(disconnectionCallback);
+
+ /* Setup primary service. */
+
+ /* Setup advertising. */
+ printf("Setup of Advertising\r\n");
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+ ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+ ble.gap().setAdvertisingInterval(1000);// 1000ms
+ ble.gap().startAdvertising();
+ ble.onDataWritten(writeCharCallback);
+ ble.addService(customService);
+
+ printf("Starting Loop\r\n");
+
+
+}
+void onBleInitError(BLE &ble, ble_error_t error)
+{
+ (void)ble;
+ (void)error;
+ printf(" ### BLE init error ###\r\n");
+
+
+ /* Initialization error handling should go here */
}
void onMsgReceived(void)
{
@@ -158,4 +263,38 @@
break;
}
return retval;
-}
\ No newline at end of file
+}
+
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+{
+ (void)params;
+ printf("\r\nTarget loss... wait for reconnection \r\n");
+
+ BLE::Instance().gap().startAdvertising(); // restart advertising
+}
+
+void writeCharCallback(const GattWriteCallbackParams *params)
+{
+
+ uint8_t j=0;
+
+ // check to see what characteristic was written, by handle
+ if(params->handle == writeChar.getValueHandle()) {
+ BLESlot2CANId(params->data[0]);
+ printf("\n\r Data received: length = %d, data = ",params->len);
+ if(canRdPointer != j && canRdPointer != j+1) {
+ bleWrPointerA=j;
+ bleWrPointerB=j+1;
+ for(int x=0; x < 9; x++) {
+ printf("%c",params->data[x]);
+ shareddata[j][x]=params->data[x];
+ }
+ for(int x=9; x < 18; x++) {
+ printf("%c",params->data[x]);
+ shareddata[j+1][x-9]=params->data[x];
+ }
+ bleWrPointerA=255;
+ bleWrPointerB=255;
+ }
+ }
+}