![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
CAN to BLE translator - and back
Dependencies: BLE_API CANnucleo X_NUCLEO_IDB0XA1 mbed
Diff: main.cpp
- Revision:
- 0:345c72cbcd60
- Child:
- 1:43ff0e4d56cc
- Child:
- 2:bfe8810290ac
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Apr 07 14:19:45 2016 +0000 @@ -0,0 +1,159 @@ +#include "mbed.h" +#include "ble/BLE.h" +#include "CAN.h" +#define TARGET_NUCLEO_F072RB 1 + +#define LED_PIN PA_5 + +uint8_t CANId2BLESlot(unsigned int id); +unsigned int BLESlot2CANId(uint8_t id); +void onMsgReceived(void); +void initCAN(void); + +DigitalOut led(LED_PIN); + + +//const unsigned int RX_ID = 0x10; +//const unsigned int TX_ID = 0x11; + +char shareddata[64][8]= { }; + +uint8_t canWrPointer= 255; +uint8_t canRdPointer= 255; +uint8_t bleWrPointerA= 255; +uint8_t bleRdPointerA= 255; +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 + +CAN can(PB_8, PB_9); // CAN Rx pin name, CAN Tx pin name +CANMessage rxMsg; +CANMessage txMsg; + +volatile bool CANmsgAvailable = false; + + +int main() +{ + uint8_t j=0; + printf("\r\nBoard started\r\n"); + led = 1; // turn LED on + initCAN(); + timerA.start(); + timerB.start(); + while(1) { + if(timerA.read()>=1.0) { + timerA.stop(); + timerA.reset(); + led=!led.read(); + 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("\r\n"); + canWrPointer=255; + } + else + { + printf("CAN message %#x dropped because BLE read\r\n",rxMsg.id); + } + } + } +} +void initCAN(void) +{ + 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"); +} +void onMsgReceived(void) +{ + CANmsgAvailable = true; +} +uint8_t CANId2BLESlot(unsigned int id) +{ + uint8_t retval=0; + switch(id) { + case 0x313: + retval=1; + break; + case 0x314: + retval=2; + break; + case 0x310: + retval=3; + break; + case 0x315: + retval=4; + break; + case 0x111: + retval=5; + break; + case 0x112: + retval=6; + break; + case 0x400: + retval=7; + break; + case 0x450: + retval=8; + break; + case 0x451: + retval=9; + break; + case 0x452: + retval=10; + break; + case 0x453: + retval=11; + break; + case 0x454: + retval=12; + break; + case 0x455: + retval=13; + break; + default: + retval=0; + break; + } + return retval; +} +unsigned int BLESlot2CANId(uint8_t id) +{ + unsigned int retval=0; + switch(id) { + case 63: + retval=0x303; + break; + case 62: + retval=0x304; + break; + default: + retval=0x0; + break; + } + return retval; +} \ No newline at end of file