Francesco Masciari
/
can_multithreadingX_vesctest
A test for vescs
main.cpp@1:9ecda2b969c2, 2019-06-14 (annotated)
- Committer:
- DunkelAmber
- Date:
- Fri Jun 14 15:08:55 2019 +0000
- Revision:
- 1:9ecda2b969c2
- Parent:
- 0:0da0fd430e20
- Child:
- 2:12431dd6475f
can test working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gidiana | 0:0da0fd430e20 | 1 | #include "mbed.h" //5.4.7 (144) |
gidiana | 0:0da0fd430e20 | 2 | |
gidiana | 0:0da0fd430e20 | 3 | const PinName can1rxPins[] = {PA_11}; |
gidiana | 0:0da0fd430e20 | 4 | const PinName can1txPins[] = {PA_12}; |
DunkelAmber | 1:9ecda2b969c2 | 5 | const PinName can2rxPins[] = {PB_5}; |
DunkelAmber | 1:9ecda2b969c2 | 6 | const PinName can2txPins[] = {PB_6}; |
DunkelAmber | 1:9ecda2b969c2 | 7 | |
DunkelAmber | 1:9ecda2b969c2 | 8 | uint64_t counter=0; |
gidiana | 0:0da0fd430e20 | 9 | |
gidiana | 0:0da0fd430e20 | 10 | //CAN1 |
gidiana | 0:0da0fd430e20 | 11 | //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 |
gidiana | 0:0da0fd430e20 | 12 | //CAN2 |
gidiana | 0:0da0fd430e20 | 13 | //0,0=RX_OK 0,1=OK 1,0=RX_OK 1,1=OK |
gidiana | 0:0da0fd430e20 | 14 | |
gidiana | 0:0da0fd430e20 | 15 | CAN can1(can1rxPins[0], can1txPins[0]); |
gidiana | 0:0da0fd430e20 | 16 | CAN can(can2rxPins[0], can2txPins[0]); |
gidiana | 0:0da0fd430e20 | 17 | |
gidiana | 0:0da0fd430e20 | 18 | //UART1, Tx, Rx (Debug) |
gidiana | 0:0da0fd430e20 | 19 | DigitalOut led1(LED1); |
gidiana | 0:0da0fd430e20 | 20 | |
gidiana | 0:0da0fd430e20 | 21 | |
gidiana | 0:0da0fd430e20 | 22 | Thread sendThread(osPriorityAboveNormal, 2048); |
gidiana | 0:0da0fd430e20 | 23 | Thread canrx; |
gidiana | 0:0da0fd430e20 | 24 | CANMessage messageIn; |
gidiana | 0:0da0fd430e20 | 25 | CANMessage messageOut; |
gidiana | 0:0da0fd430e20 | 26 | |
DunkelAmber | 1:9ecda2b969c2 | 27 | uint64_t cantoi(CANMessage message) { |
DunkelAmber | 1:9ecda2b969c2 | 28 | int data=0; |
DunkelAmber | 1:9ecda2b969c2 | 29 | for(int i=message.len-1, k=0; i>=0; i--, k++) { |
DunkelAmber | 1:9ecda2b969c2 | 30 | data += message.data[i] << (8*k); |
DunkelAmber | 1:9ecda2b969c2 | 31 | } |
DunkelAmber | 1:9ecda2b969c2 | 32 | return data; |
DunkelAmber | 1:9ecda2b969c2 | 33 | //pose = message.data[0] + (message.data[1] << 8) + (message.data[2] << 16) + (message.data[3] << 24); |
DunkelAmber | 1:9ecda2b969c2 | 34 | } |
DunkelAmber | 1:9ecda2b969c2 | 35 | |
DunkelAmber | 1:9ecda2b969c2 | 36 | void itocan(uint64_t data, CANMessage& message) { |
DunkelAmber | 1:9ecda2b969c2 | 37 | |
DunkelAmber | 1:9ecda2b969c2 | 38 | for(int i=message.len-1; i>=0; i--) { |
DunkelAmber | 1:9ecda2b969c2 | 39 | //printf("dataconv:\t% 12llu\n\r", data); |
DunkelAmber | 1:9ecda2b969c2 | 40 | message.data[i] = data & 0x00000000000000ff; |
DunkelAmber | 1:9ecda2b969c2 | 41 | data = data>>8; |
DunkelAmber | 1:9ecda2b969c2 | 42 | } |
DunkelAmber | 1:9ecda2b969c2 | 43 | } |
DunkelAmber | 1:9ecda2b969c2 | 44 | |
gidiana | 0:0da0fd430e20 | 45 | void canRxIsr() |
gidiana | 0:0da0fd430e20 | 46 | { |
DunkelAmber | 1:9ecda2b969c2 | 47 | while(1) { |
DunkelAmber | 1:9ecda2b969c2 | 48 | if(can.read(messageIn)) { |
DunkelAmber | 1:9ecda2b969c2 | 49 | led1 = !led1; |
DunkelAmber | 1:9ecda2b969c2 | 50 | printf("received from %lu\t%llu\n\r", messageIn.id, cantoi(messageIn)); |
DunkelAmber | 1:9ecda2b969c2 | 51 | } |
gidiana | 0:0da0fd430e20 | 52 | } |
DunkelAmber | 1:9ecda2b969c2 | 53 | } |
DunkelAmber | 1:9ecda2b969c2 | 54 | |
DunkelAmber | 1:9ecda2b969c2 | 55 | //pose = messageIn.data[0] + (messageIn.data[1] << 8) + (messageIn.data[2] << 16) + (messageIn.data[3] << 24); |
DunkelAmber | 1:9ecda2b969c2 | 56 | |
gidiana | 0:0da0fd430e20 | 57 | void sendMessage() |
gidiana | 0:0da0fd430e20 | 58 | { |
gidiana | 0:0da0fd430e20 | 59 | int status = can.write(messageOut); |
DunkelAmber | 1:9ecda2b969c2 | 60 | printf("Send status: %d\r\n", status); |
gidiana | 0:0da0fd430e20 | 61 | } |
gidiana | 0:0da0fd430e20 | 62 | |
gidiana | 0:0da0fd430e20 | 63 | void sendMessageLoop() |
gidiana | 0:0da0fd430e20 | 64 | { |
DunkelAmber | 1:9ecda2b969c2 | 65 | while (true) { |
DunkelAmber | 1:9ecda2b969c2 | 66 | counter += 1; |
DunkelAmber | 1:9ecda2b969c2 | 67 | |
DunkelAmber | 1:9ecda2b969c2 | 68 | messageOut.id = 1000; |
DunkelAmber | 1:9ecda2b969c2 | 69 | messageOut.format = CANStandard; |
DunkelAmber | 1:9ecda2b969c2 | 70 | messageOut.data[0]=25; |
DunkelAmber | 1:9ecda2b969c2 | 71 | messageOut.len = 8; |
DunkelAmber | 1:9ecda2b969c2 | 72 | |
DunkelAmber | 1:9ecda2b969c2 | 73 | itocan(counter, messageOut); |
DunkelAmber | 1:9ecda2b969c2 | 74 | printf("sending couter: %llu\t| CAN data: %llu\n\r", counter, cantoi(messageOut)); |
gidiana | 0:0da0fd430e20 | 75 | sendMessage(); |
gidiana | 0:0da0fd430e20 | 76 | osDelay(1000); |
gidiana | 0:0da0fd430e20 | 77 | } |
gidiana | 0:0da0fd430e20 | 78 | } |
gidiana | 0:0da0fd430e20 | 79 | |
gidiana | 0:0da0fd430e20 | 80 | int main() |
gidiana | 0:0da0fd430e20 | 81 | { |
gidiana | 0:0da0fd430e20 | 82 | |
gidiana | 0:0da0fd430e20 | 83 | printf("CAN receive / transmit test.\r\n"); |
DunkelAmber | 1:9ecda2b969c2 | 84 | can.frequency(500000); |
gidiana | 0:0da0fd430e20 | 85 | |
DunkelAmber | 1:9ecda2b969c2 | 86 | messageOut.id = 1000; |
DunkelAmber | 1:9ecda2b969c2 | 87 | messageOut.format = CANStandard; |
DunkelAmber | 1:9ecda2b969c2 | 88 | messageOut.data[0]=24; |
DunkelAmber | 1:9ecda2b969c2 | 89 | messageOut.len = 8; |
gidiana | 0:0da0fd430e20 | 90 | |
gidiana | 0:0da0fd430e20 | 91 | canrx.start(canRxIsr); |
gidiana | 0:0da0fd430e20 | 92 | printf("CAN receive / .\r\n"); |
gidiana | 0:0da0fd430e20 | 93 | sendThread.start(&sendMessageLoop); |
gidiana | 0:0da0fd430e20 | 94 | printf("CAN \r\n"); |
gidiana | 0:0da0fd430e20 | 95 | |
gidiana | 0:0da0fd430e20 | 96 | while (true) |
gidiana | 0:0da0fd430e20 | 97 | { |
gidiana | 0:0da0fd430e20 | 98 | |
gidiana | 0:0da0fd430e20 | 99 | led1 = 0; |
gidiana | 0:0da0fd430e20 | 100 | osDelay(1000); |
gidiana | 0:0da0fd430e20 | 101 | } |
gidiana | 0:0da0fd430e20 | 102 | } |