![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
CAN to BLE translator - and back
Dependencies: BLE_API CANnucleo X_NUCLEO_IDB0XA1 mbed
main.cpp@2:bfe8810290ac, 2016-04-08 (annotated)
- Committer:
- filippomontano
- Date:
- Fri Apr 08 07:40:08 2016 +0000
- Revision:
- 2:bfe8810290ac
- Parent:
- 0:345c72cbcd60
- Child:
- 3:5bce2e8d2797
0B: CanBus su PA11-12;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
filippomontano | 0:345c72cbcd60 | 1 | #include "mbed.h" |
filippomontano | 0:345c72cbcd60 | 2 | #include "ble/BLE.h" |
filippomontano | 0:345c72cbcd60 | 3 | #include "CAN.h" |
filippomontano | 0:345c72cbcd60 | 4 | #define TARGET_NUCLEO_F072RB 1 |
filippomontano | 0:345c72cbcd60 | 5 | |
filippomontano | 0:345c72cbcd60 | 6 | #define LED_PIN PA_5 |
filippomontano | 0:345c72cbcd60 | 7 | |
filippomontano | 0:345c72cbcd60 | 8 | uint8_t CANId2BLESlot(unsigned int id); |
filippomontano | 0:345c72cbcd60 | 9 | unsigned int BLESlot2CANId(uint8_t id); |
filippomontano | 0:345c72cbcd60 | 10 | void onMsgReceived(void); |
filippomontano | 0:345c72cbcd60 | 11 | void initCAN(void); |
filippomontano | 0:345c72cbcd60 | 12 | |
filippomontano | 0:345c72cbcd60 | 13 | DigitalOut led(LED_PIN); |
filippomontano | 0:345c72cbcd60 | 14 | |
filippomontano | 0:345c72cbcd60 | 15 | |
filippomontano | 0:345c72cbcd60 | 16 | //const unsigned int RX_ID = 0x10; |
filippomontano | 0:345c72cbcd60 | 17 | //const unsigned int TX_ID = 0x11; |
filippomontano | 0:345c72cbcd60 | 18 | |
filippomontano | 0:345c72cbcd60 | 19 | char shareddata[64][8]= { }; |
filippomontano | 0:345c72cbcd60 | 20 | |
filippomontano | 0:345c72cbcd60 | 21 | uint8_t canWrPointer= 255; |
filippomontano | 0:345c72cbcd60 | 22 | uint8_t canRdPointer= 255; |
filippomontano | 0:345c72cbcd60 | 23 | uint8_t bleWrPointerA= 255; |
filippomontano | 0:345c72cbcd60 | 24 | uint8_t bleRdPointerA= 255; |
filippomontano | 0:345c72cbcd60 | 25 | uint8_t bleWrPointerB= 255; |
filippomontano | 0:345c72cbcd60 | 26 | uint8_t bleRdPointerB= 255; |
filippomontano | 0:345c72cbcd60 | 27 | |
filippomontano | 0:345c72cbcd60 | 28 | Timer timerA, timerB; //questi due contatori servono solo per dire: appena è passato un po' di tempo -> esegui |
filippomontano | 0:345c72cbcd60 | 29 | |
filippomontano | 2:bfe8810290ac | 30 | //CAN can(PB_8, PB_9); // CAN Rx pin name, CAN Tx pin name |
filippomontano | 2:bfe8810290ac | 31 | |
filippomontano | 2:bfe8810290ac | 32 | CAN can(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name |
filippomontano | 0:345c72cbcd60 | 33 | CANMessage rxMsg; |
filippomontano | 0:345c72cbcd60 | 34 | CANMessage txMsg; |
filippomontano | 0:345c72cbcd60 | 35 | |
filippomontano | 0:345c72cbcd60 | 36 | volatile bool CANmsgAvailable = false; |
filippomontano | 0:345c72cbcd60 | 37 | |
filippomontano | 0:345c72cbcd60 | 38 | |
filippomontano | 0:345c72cbcd60 | 39 | int main() |
filippomontano | 0:345c72cbcd60 | 40 | { |
filippomontano | 0:345c72cbcd60 | 41 | uint8_t j=0; |
filippomontano | 0:345c72cbcd60 | 42 | printf("\r\nBoard started\r\n"); |
filippomontano | 0:345c72cbcd60 | 43 | led = 1; // turn LED on |
filippomontano | 0:345c72cbcd60 | 44 | initCAN(); |
filippomontano | 0:345c72cbcd60 | 45 | timerA.start(); |
filippomontano | 0:345c72cbcd60 | 46 | timerB.start(); |
filippomontano | 0:345c72cbcd60 | 47 | while(1) { |
filippomontano | 0:345c72cbcd60 | 48 | if(timerA.read()>=1.0) { |
filippomontano | 0:345c72cbcd60 | 49 | timerA.stop(); |
filippomontano | 0:345c72cbcd60 | 50 | timerA.reset(); |
filippomontano | 0:345c72cbcd60 | 51 | led=!led.read(); |
filippomontano | 0:345c72cbcd60 | 52 | timerA.start(); |
filippomontano | 0:345c72cbcd60 | 53 | } |
filippomontano | 0:345c72cbcd60 | 54 | if(timerB.read()>=1.0) { |
filippomontano | 0:345c72cbcd60 | 55 | timerB.stop(); |
filippomontano | 0:345c72cbcd60 | 56 | timerB.reset(); |
filippomontano | 0:345c72cbcd60 | 57 | //led=!led.read(); |
filippomontano | 0:345c72cbcd60 | 58 | timerB.start(); |
filippomontano | 0:345c72cbcd60 | 59 | } |
filippomontano | 0:345c72cbcd60 | 60 | if(CANmsgAvailable) { |
filippomontano | 0:345c72cbcd60 | 61 | CANmsgAvailable = false; // reset flag for next use |
filippomontano | 0:345c72cbcd60 | 62 | can.read(rxMsg); // read message into Rx message storage |
filippomontano | 0:345c72cbcd60 | 63 | j=CANId2BLESlot(rxMsg.id); |
filippomontano | 0:345c72cbcd60 | 64 | if(j!=bleRdPointerA && j!=bleRdPointerB) { |
filippomontano | 0:345c72cbcd60 | 65 | //write if BLE is not reading |
filippomontano | 0:345c72cbcd60 | 66 | canWrPointer=j; |
filippomontano | 0:345c72cbcd60 | 67 | printf("CAN message received:\r\n"); |
filippomontano | 0:345c72cbcd60 | 68 | printf(" ID = %#x -> %d \r\n", rxMsg.id, j); |
filippomontano | 0:345c72cbcd60 | 69 | // printf(" Type = %d\r\n", rxMsg.type); |
filippomontano | 0:345c72cbcd60 | 70 | // printf(" Format = %d\r\n", rxMsg.format); |
filippomontano | 0:345c72cbcd60 | 71 | // printf(" Length = %d\r\n", rxMsg.len); |
filippomontano | 0:345c72cbcd60 | 72 | printf(" Data ="); |
filippomontano | 0:345c72cbcd60 | 73 | for(int i = 0; i < rxMsg.len; i++) { |
filippomontano | 0:345c72cbcd60 | 74 | shareddata[j][i]=rxMsg.data[i]; |
filippomontano | 0:345c72cbcd60 | 75 | printf(" %x", rxMsg.data[i]); |
filippomontano | 0:345c72cbcd60 | 76 | } |
filippomontano | 0:345c72cbcd60 | 77 | printf("\r\n"); |
filippomontano | 0:345c72cbcd60 | 78 | canWrPointer=255; |
filippomontano | 0:345c72cbcd60 | 79 | } |
filippomontano | 0:345c72cbcd60 | 80 | else |
filippomontano | 0:345c72cbcd60 | 81 | { |
filippomontano | 0:345c72cbcd60 | 82 | printf("CAN message %#x dropped because BLE read\r\n",rxMsg.id); |
filippomontano | 0:345c72cbcd60 | 83 | } |
filippomontano | 0:345c72cbcd60 | 84 | } |
filippomontano | 0:345c72cbcd60 | 85 | } |
filippomontano | 0:345c72cbcd60 | 86 | } |
filippomontano | 0:345c72cbcd60 | 87 | void initCAN(void) |
filippomontano | 0:345c72cbcd60 | 88 | { |
filippomontano | 0:345c72cbcd60 | 89 | can.frequency(500000); // set bit rate to 500kbps as S018 |
filippomontano | 0:345c72cbcd60 | 90 | can.attach(&onMsgReceived, CAN::RxIrq); // attach 'CAN receive-complete' interrupt handler |
filippomontano | 0:345c72cbcd60 | 91 | printf("\r\nCAN started at 500kbps\r\n"); |
filippomontano | 0:345c72cbcd60 | 92 | } |
filippomontano | 0:345c72cbcd60 | 93 | void onMsgReceived(void) |
filippomontano | 0:345c72cbcd60 | 94 | { |
filippomontano | 0:345c72cbcd60 | 95 | CANmsgAvailable = true; |
filippomontano | 0:345c72cbcd60 | 96 | } |
filippomontano | 0:345c72cbcd60 | 97 | uint8_t CANId2BLESlot(unsigned int id) |
filippomontano | 0:345c72cbcd60 | 98 | { |
filippomontano | 0:345c72cbcd60 | 99 | uint8_t retval=0; |
filippomontano | 0:345c72cbcd60 | 100 | switch(id) { |
filippomontano | 0:345c72cbcd60 | 101 | case 0x313: |
filippomontano | 0:345c72cbcd60 | 102 | retval=1; |
filippomontano | 0:345c72cbcd60 | 103 | break; |
filippomontano | 0:345c72cbcd60 | 104 | case 0x314: |
filippomontano | 0:345c72cbcd60 | 105 | retval=2; |
filippomontano | 0:345c72cbcd60 | 106 | break; |
filippomontano | 0:345c72cbcd60 | 107 | case 0x310: |
filippomontano | 0:345c72cbcd60 | 108 | retval=3; |
filippomontano | 0:345c72cbcd60 | 109 | break; |
filippomontano | 0:345c72cbcd60 | 110 | case 0x315: |
filippomontano | 0:345c72cbcd60 | 111 | retval=4; |
filippomontano | 0:345c72cbcd60 | 112 | break; |
filippomontano | 0:345c72cbcd60 | 113 | case 0x111: |
filippomontano | 0:345c72cbcd60 | 114 | retval=5; |
filippomontano | 0:345c72cbcd60 | 115 | break; |
filippomontano | 0:345c72cbcd60 | 116 | case 0x112: |
filippomontano | 0:345c72cbcd60 | 117 | retval=6; |
filippomontano | 0:345c72cbcd60 | 118 | break; |
filippomontano | 0:345c72cbcd60 | 119 | case 0x400: |
filippomontano | 0:345c72cbcd60 | 120 | retval=7; |
filippomontano | 0:345c72cbcd60 | 121 | break; |
filippomontano | 0:345c72cbcd60 | 122 | case 0x450: |
filippomontano | 0:345c72cbcd60 | 123 | retval=8; |
filippomontano | 0:345c72cbcd60 | 124 | break; |
filippomontano | 0:345c72cbcd60 | 125 | case 0x451: |
filippomontano | 0:345c72cbcd60 | 126 | retval=9; |
filippomontano | 0:345c72cbcd60 | 127 | break; |
filippomontano | 0:345c72cbcd60 | 128 | case 0x452: |
filippomontano | 0:345c72cbcd60 | 129 | retval=10; |
filippomontano | 0:345c72cbcd60 | 130 | break; |
filippomontano | 0:345c72cbcd60 | 131 | case 0x453: |
filippomontano | 0:345c72cbcd60 | 132 | retval=11; |
filippomontano | 0:345c72cbcd60 | 133 | break; |
filippomontano | 0:345c72cbcd60 | 134 | case 0x454: |
filippomontano | 0:345c72cbcd60 | 135 | retval=12; |
filippomontano | 0:345c72cbcd60 | 136 | break; |
filippomontano | 0:345c72cbcd60 | 137 | case 0x455: |
filippomontano | 0:345c72cbcd60 | 138 | retval=13; |
filippomontano | 0:345c72cbcd60 | 139 | break; |
filippomontano | 0:345c72cbcd60 | 140 | default: |
filippomontano | 0:345c72cbcd60 | 141 | retval=0; |
filippomontano | 0:345c72cbcd60 | 142 | break; |
filippomontano | 0:345c72cbcd60 | 143 | } |
filippomontano | 0:345c72cbcd60 | 144 | return retval; |
filippomontano | 0:345c72cbcd60 | 145 | } |
filippomontano | 0:345c72cbcd60 | 146 | unsigned int BLESlot2CANId(uint8_t id) |
filippomontano | 0:345c72cbcd60 | 147 | { |
filippomontano | 0:345c72cbcd60 | 148 | unsigned int retval=0; |
filippomontano | 0:345c72cbcd60 | 149 | switch(id) { |
filippomontano | 0:345c72cbcd60 | 150 | case 63: |
filippomontano | 0:345c72cbcd60 | 151 | retval=0x303; |
filippomontano | 0:345c72cbcd60 | 152 | break; |
filippomontano | 0:345c72cbcd60 | 153 | case 62: |
filippomontano | 0:345c72cbcd60 | 154 | retval=0x304; |
filippomontano | 0:345c72cbcd60 | 155 | break; |
filippomontano | 0:345c72cbcd60 | 156 | default: |
filippomontano | 0:345c72cbcd60 | 157 | retval=0x0; |
filippomontano | 0:345c72cbcd60 | 158 | break; |
filippomontano | 0:345c72cbcd60 | 159 | } |
filippomontano | 0:345c72cbcd60 | 160 | return retval; |
filippomontano | 0:345c72cbcd60 | 161 | } |