A test for vescs

Committer:
DunkelAmber
Date:
Sat Jun 15 09:31:22 2019 +0000
Revision:
2:12431dd6475f
Parent:
1:9ecda2b969c2
feat: itocan supports template

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DunkelAmber 2:12431dd6475f 1 #include "mbed.h" //5.4.7 (144)
DunkelAmber 2:12431dd6475f 2 #include "canid.hpp"
DunkelAmber 2:12431dd6475f 3
DunkelAmber 2:12431dd6475f 4 #define BIG_ENDIAN
gidiana 0:0da0fd430e20 5
gidiana 0:0da0fd430e20 6 const PinName can1rxPins[] = {PA_11};
gidiana 0:0da0fd430e20 7 const PinName can1txPins[] = {PA_12};
DunkelAmber 1:9ecda2b969c2 8 const PinName can2rxPins[] = {PB_5};
DunkelAmber 1:9ecda2b969c2 9 const PinName can2txPins[] = {PB_6};
DunkelAmber 1:9ecda2b969c2 10
DunkelAmber 1:9ecda2b969c2 11 uint64_t counter=0;
gidiana 0:0da0fd430e20 12
gidiana 0:0da0fd430e20 13 //CAN1
gidiana 0:0da0fd430e20 14 //0,0=OK 0,1=OK 0,2=OK 1,0=OK 1,1=OK 1,2=OK 2,0=OK 2,1=OK 2,2=OK
DunkelAmber 2:12431dd6475f 15 //CAN"
gidiana 0:0da0fd430e20 16 //0,0=RX_OK 0,1=OK 1,0=RX_OK 1,1=OK
gidiana 0:0da0fd430e20 17
gidiana 0:0da0fd430e20 18 CAN can1(can1rxPins[0], can1txPins[0]);
gidiana 0:0da0fd430e20 19 CAN can(can2rxPins[0], can2txPins[0]);
gidiana 0:0da0fd430e20 20
gidiana 0:0da0fd430e20 21 //UART1, Tx, Rx (Debug)
gidiana 0:0da0fd430e20 22 DigitalOut led1(LED1);
gidiana 0:0da0fd430e20 23
gidiana 0:0da0fd430e20 24
gidiana 0:0da0fd430e20 25 Thread sendThread(osPriorityAboveNormal, 2048);
gidiana 0:0da0fd430e20 26 Thread canrx;
gidiana 0:0da0fd430e20 27 CANMessage messageIn;
gidiana 0:0da0fd430e20 28 CANMessage messageOut;
gidiana 0:0da0fd430e20 29
DunkelAmber 2:12431dd6475f 30 void setID (CANMessage& message, uint32_t device_id, uint32_t content_id) {
DunkelAmber 2:12431dd6475f 31 uint32_t id = device_id;
DunkelAmber 2:12431dd6475f 32 id |= (uint32_t)content_id << 8;
DunkelAmber 2:12431dd6475f 33 id |= 0x80000000; // Set EFF(Extended Frame Format) flag true
DunkelAmber 2:12431dd6475f 34
DunkelAmber 2:12431dd6475f 35 message.id = id;
DunkelAmber 2:12431dd6475f 36
DunkelAmber 2:12431dd6475f 37 }
DunkelAmber 2:12431dd6475f 38
DunkelAmber 1:9ecda2b969c2 39 uint64_t cantoi(CANMessage message) {
DunkelAmber 2:12431dd6475f 40 uint64_t ret=0;
DunkelAmber 2:12431dd6475f 41
DunkelAmber 2:12431dd6475f 42 #ifdef BIG_ENDIAN
DunkelAmber 1:9ecda2b969c2 43 for(int i=message.len-1, k=0; i>=0; i--, k++) {
DunkelAmber 2:12431dd6475f 44 ret += message.data[i] << (8*k);
DunkelAmber 1:9ecda2b969c2 45 }
DunkelAmber 2:12431dd6475f 46 #else //LITTLE ENDIAN
DunkelAmber 2:12431dd6475f 47 for(int i=0, k=0; i>=message.len-1; i++, k++) {
DunkelAmber 2:12431dd6475f 48 ret += message.data[i] << (8*k);
DunkelAmber 2:12431dd6475f 49 }
DunkelAmber 2:12431dd6475f 50 #endif
DunkelAmber 2:12431dd6475f 51
DunkelAmber 2:12431dd6475f 52
DunkelAmber 2:12431dd6475f 53 return ret;
DunkelAmber 1:9ecda2b969c2 54 //pose = message.data[0] + (message.data[1] << 8) + (message.data[2] << 16) + (message.data[3] << 24);
DunkelAmber 1:9ecda2b969c2 55 }
DunkelAmber 1:9ecda2b969c2 56
DunkelAmber 2:12431dd6475f 57 template <class data_t> void xtocan(data_t _data, unsigned int n_bytes, CANMessage& message) {
DunkelAmber 2:12431dd6475f 58 data_t data = 0;
DunkelAmber 2:12431dd6475f 59 message.len = n_bytes;
DunkelAmber 2:12431dd6475f 60 int mask = 0xff; // last 8 bits
DunkelAmber 2:12431dd6475f 61
DunkelAmber 2:12431dd6475f 62 #ifdef BIG_ENDIAN
DunkelAmber 2:12431dd6475f 63 for(int i=message.len-1; i>=0; i--) {
DunkelAmber 2:12431dd6475f 64 message.data[i] = data & mask;
DunkelAmber 2:12431dd6475f 65 data = data>>8;
DunkelAmber 2:12431dd6475f 66 }
DunkelAmber 2:12431dd6475f 67 #else //LITTLE ENDIAN
DunkelAmber 2:12431dd6475f 68 for(int i=0; i<message.len; i++) {
DunkelAmber 2:12431dd6475f 69 message.data[i] = data & maks;
DunkelAmber 2:12431dd6475f 70 data = data>>8;
DunkelAmber 2:12431dd6475f 71 }
DunkelAmber 2:12431dd6475f 72 #endif
DunkelAmber 2:12431dd6475f 73
DunkelAmber 2:12431dd6475f 74 return;
DunkelAmber 2:12431dd6475f 75 }
DunkelAmber 2:12431dd6475f 76
DunkelAmber 2:12431dd6475f 77 /* DEPRECATED
DunkelAmber 2:12431dd6475f 78 void ui64tocan(uint64_t data, CANMessage& message) {
DunkelAmber 1:9ecda2b969c2 79
DunkelAmber 1:9ecda2b969c2 80 for(int i=message.len-1; i>=0; i--) {
DunkelAmber 1:9ecda2b969c2 81 //printf("dataconv:\t% 12llu\n\r", data);
DunkelAmber 1:9ecda2b969c2 82 message.data[i] = data & 0x00000000000000ff;
DunkelAmber 1:9ecda2b969c2 83 data = data>>8;
DunkelAmber 1:9ecda2b969c2 84 }
DunkelAmber 2:12431dd6475f 85 }*/
DunkelAmber 1:9ecda2b969c2 86
gidiana 0:0da0fd430e20 87 void canRxIsr()
gidiana 0:0da0fd430e20 88 {
DunkelAmber 1:9ecda2b969c2 89 while(1) {
DunkelAmber 1:9ecda2b969c2 90 if(can.read(messageIn)) {
DunkelAmber 1:9ecda2b969c2 91 led1 = !led1;
DunkelAmber 1:9ecda2b969c2 92 printf("received from %lu\t%llu\n\r", messageIn.id, cantoi(messageIn));
DunkelAmber 1:9ecda2b969c2 93 }
gidiana 0:0da0fd430e20 94 }
DunkelAmber 1:9ecda2b969c2 95 }
DunkelAmber 1:9ecda2b969c2 96
DunkelAmber 1:9ecda2b969c2 97 //pose = messageIn.data[0] + (messageIn.data[1] << 8) + (messageIn.data[2] << 16) + (messageIn.data[3] << 24);
DunkelAmber 1:9ecda2b969c2 98
gidiana 0:0da0fd430e20 99 void sendMessage()
gidiana 0:0da0fd430e20 100 {
gidiana 0:0da0fd430e20 101 int status = can.write(messageOut);
DunkelAmber 1:9ecda2b969c2 102 printf("Send status: %d\r\n", status);
gidiana 0:0da0fd430e20 103 }
gidiana 0:0da0fd430e20 104
gidiana 0:0da0fd430e20 105 void sendMessageLoop()
gidiana 0:0da0fd430e20 106 {
DunkelAmber 1:9ecda2b969c2 107 while (true) {
DunkelAmber 1:9ecda2b969c2 108 counter += 1;
DunkelAmber 1:9ecda2b969c2 109
DunkelAmber 2:12431dd6475f 110 messageOut.id = 1000; // DELETEME
DunkelAmber 2:12431dd6475f 111 setID(messageOut, 1, BLDC_SET_RPM);
DunkelAmber 2:12431dd6475f 112 messageOut.format = CANExtended;
DunkelAmber 1:9ecda2b969c2 113 messageOut.data[0]=25;
DunkelAmber 1:9ecda2b969c2 114 messageOut.len = 8;
DunkelAmber 1:9ecda2b969c2 115
DunkelAmber 2:12431dd6475f 116 xtocan<uint64_t>(counter, 8, messageOut);
DunkelAmber 2:12431dd6475f 117 //ui64tocan(counter, messageOut);
DunkelAmber 1:9ecda2b969c2 118 printf("sending couter: %llu\t| CAN data: %llu\n\r", counter, cantoi(messageOut));
gidiana 0:0da0fd430e20 119 sendMessage();
gidiana 0:0da0fd430e20 120 osDelay(1000);
gidiana 0:0da0fd430e20 121 }
gidiana 0:0da0fd430e20 122 }
gidiana 0:0da0fd430e20 123
gidiana 0:0da0fd430e20 124 int main()
gidiana 0:0da0fd430e20 125 {
gidiana 0:0da0fd430e20 126
gidiana 0:0da0fd430e20 127 printf("CAN receive / transmit test.\r\n");
DunkelAmber 1:9ecda2b969c2 128 can.frequency(500000);
gidiana 0:0da0fd430e20 129
DunkelAmber 1:9ecda2b969c2 130 messageOut.id = 1000;
DunkelAmber 2:12431dd6475f 131 messageOut.format = CANExtended;
DunkelAmber 1:9ecda2b969c2 132 messageOut.data[0]=24;
DunkelAmber 1:9ecda2b969c2 133 messageOut.len = 8;
gidiana 0:0da0fd430e20 134
gidiana 0:0da0fd430e20 135 canrx.start(canRxIsr);
gidiana 0:0da0fd430e20 136 printf("CAN receive / .\r\n");
gidiana 0:0da0fd430e20 137 sendThread.start(&sendMessageLoop);
gidiana 0:0da0fd430e20 138 printf("CAN \r\n");
gidiana 0:0da0fd430e20 139
gidiana 0:0da0fd430e20 140 while (true)
gidiana 0:0da0fd430e20 141 {
gidiana 0:0da0fd430e20 142
gidiana 0:0da0fd430e20 143 led1 = 0;
gidiana 0:0da0fd430e20 144 osDelay(1000);
gidiana 0:0da0fd430e20 145 }
gidiana 0:0da0fd430e20 146 }