TLMoto

Dependencies:   CANnucleo CANnucleo_Hello mbed

Fork of CANnucleo_Hello by Zoltan Hudak

Committer:
ser1516
Date:
Sat Oct 22 16:35:07 2016 +0000
Revision:
21:13ac27349025
Parent:
20:eb1a8042605e
comit antes de gerardo mexer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:c5e5d0df6f2a 1 /*
hudakz 16:a86f339d1c25 2 * An example showing how to use the CANnucleo library:
hudakz 0:c5e5d0df6f2a 3 *
ser1516 21:13ac27349025 4 * Two affordable (less than $3 on ebay) STM32F103C8T6 boards (20kB SRAM, 64kB Flash),
ser1516 21:13ac27349025 5 * (see [https://developer.mbed.org/users/hudakz/code/STM32F103C8T6_Hello/] for more details)
ser1516 21:13ac27349025 6 * are connected to the same CAN bus via transceivers (MCP2551 or TJA1040, or etc.).
ser1516 21:13ac27349025 7 * CAN transceivers are not part of NUCLEO boards, therefore must be added by you.
hudakz 6:7ff95ce72f6d 8 * Remember also that CAN bus (even a short one) must be terminated with 120 Ohm resitors at both ends.
hudakz 6:7ff95ce72f6d 9 *
hudakz 16:a86f339d1c25 10 * For more details see the wiki page <https://developer.mbed.org/users/hudakz/code/CANnucleo_Hello/>
hudakz 6:7ff95ce72f6d 11 *
hudakz 17:18d4d0ff26a6 12 * NOTE: If you'd like to use the official NUCLEO boards comment out line 22
hudakz 6:7ff95ce72f6d 13 *
hudakz 6:7ff95ce72f6d 14 * The same code is used for both NUCLEO boards, but:
hudakz 0:c5e5d0df6f2a 15 * For board #1 compile the example without any change.
ser1516 21:13ac27349025 16 * For board #2 comment out line 23 before compiling
hudakz 4:ccf4ac2deac8 17 *
hudakz 6:7ff95ce72f6d 18 * Once the binaries have been downloaded to the boards reset board #1.
hudakz 0:c5e5d0df6f2a 19 *
ser1516 21:13ac27349025 20 */
hudakz 0:c5e5d0df6f2a 21
ser1516 21:13ac27349025 22 //#define TARGET_STM32F103C8T6 1 // uncomment this line when using STM32F103C8T6 boards!
ser1516 21:13ac27349025 23 //#define BOARD1 1 // comment out this line when compiling for board #2
hudakz 0:c5e5d0df6f2a 24
ser1516 21:13ac27349025 25
hudakz 0:c5e5d0df6f2a 26
ser1516 21:13ac27349025 27 const unsigned int RX_ID = 0x100;
ser1516 21:13ac27349025 28 const unsigned int TX_ID = 0x101;
hudakz 6:7ff95ce72f6d 29
hudakz 19:872e304d7e17 30 #include "CANnucleo.h"
hudakz 16:a86f339d1c25 31 #include "mbed.h"
hudakz 16:a86f339d1c25 32
ser1516 21:13ac27349025 33 /*
hudakz 17:18d4d0ff26a6 34 * To avaoid name collision with the CAN and CANMessage classes built into the mbed library
hudakz 17:18d4d0ff26a6 35 * the CANnucleo's CAN and CANMessage classes have been moved into the CANnucleo namespace.
hudakz 20:eb1a8042605e 36 * Remember to qualify them with the CANnucleo namespace.
hudakz 17:18d4d0ff26a6 37 */
hudakz 17:18d4d0ff26a6 38 CANnucleo::CAN can(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name
hudakz 17:18d4d0ff26a6 39 CANnucleo::CANMessage rxMsg;
hudakz 17:18d4d0ff26a6 40 CANnucleo::CANMessage txMsg;
ser1516 21:13ac27349025 41 CANnucleo::CANMessage throttle_txMsg;
ser1516 21:13ac27349025 42
ser1516 21:13ac27349025 43
ser1516 21:13ac27349025 44 DigitalOut led(PA_5);
ser1516 21:13ac27349025 45
hudakz 18:22977a898fe9 46 Timer timer;
hudakz 17:18d4d0ff26a6 47 int counter = 0;
hudakz 17:18d4d0ff26a6 48 volatile bool msgAvailable = false;
ser1516 21:13ac27349025 49 volatile bool to_send = false;
hudakz 0:c5e5d0df6f2a 50
hudakz 16:a86f339d1c25 51 /**
hudakz 16:a86f339d1c25 52 * @brief 'CAN receive-complete' interrup handler.
hudakz 16:a86f339d1c25 53 * @note Called on arrival of new CAN message.
hudakz 16:a86f339d1c25 54 * Keep it as short as possible.
ser1516 21:13ac27349025 55 * @param
ser1516 21:13ac27349025 56 * @retval
hudakz 16:a86f339d1c25 57 */
ser1516 21:13ac27349025 58 void onMsgReceived()
ser1516 21:13ac27349025 59 {
hudakz 16:a86f339d1c25 60 msgAvailable = true;
hudakz 16:a86f339d1c25 61 }
hudakz 16:a86f339d1c25 62
hudakz 16:a86f339d1c25 63 /**
hudakz 16:a86f339d1c25 64 * @brief Main
hudakz 16:a86f339d1c25 65 * @note
ser1516 21:13ac27349025 66 * @param
hudakz 16:a86f339d1c25 67 * @retval
hudakz 16:a86f339d1c25 68 */
ser1516 21:13ac27349025 69
ser1516 21:13ac27349025 70 bool key_switch = 0;
ser1516 21:13ac27349025 71
ser1516 21:13ac27349025 72 void flip()
ser1516 21:13ac27349025 73 {
ser1516 21:13ac27349025 74 key_switch = !key_switch;
ser1516 21:13ac27349025 75 led = key_switch;
ser1516 21:13ac27349025 76
ser1516 21:13ac27349025 77 to_send=1;
ser1516 21:13ac27349025 78 //printf("controller switch\r\n");
ser1516 21:13ac27349025 79
ser1516 21:13ac27349025 80
ser1516 21:13ac27349025 81
ser1516 21:13ac27349025 82 // to_send = 1;
ser1516 21:13ac27349025 83 }
ser1516 21:13ac27349025 84
ser1516 21:13ac27349025 85 Ticker flipper;
ser1516 21:13ac27349025 86
ser1516 21:13ac27349025 87 typedef union can_union {
ser1516 21:13ac27349025 88 int i[2];
ser1516 21:13ac27349025 89 char bytes[8];
ser1516 21:13ac27349025 90 float f[2];
ser1516 21:13ac27349025 91 } data;
ser1516 21:13ac27349025 92
ser1516 21:13ac27349025 93 int main()
ser1516 21:13ac27349025 94
ser1516 21:13ac27349025 95 {
hudakz 16:a86f339d1c25 96 can.frequency(1000000); // set bit rate to 1Mbps
hudakz 16:a86f339d1c25 97 can.attach(&onMsgReceived); // attach 'CAN receive-complete' interrupt handler
ser1516 21:13ac27349025 98 flipper.attach(&flip, 30); // turn on or off
ser1516 21:13ac27349025 99 led=key_switch;
hudakz 10:66da8731bdb6 100 timer.start(); // start timer
ser1516 21:13ac27349025 101
ser1516 21:13ac27349025 102 printf("started\r\n");
ser1516 21:13ac27349025 103 while(true) {
ser1516 21:13ac27349025 104
ser1516 21:13ac27349025 105 if(msgAvailable) {
ser1516 21:13ac27349025 106 static int counter = 0;
ser1516 21:13ac27349025 107 data data;
ser1516 21:13ac27349025 108 int len = can.read(rxMsg);
ser1516 21:13ac27349025 109 data.bytes[0] = rxMsg.data[0];
ser1516 21:13ac27349025 110 data.bytes[1] = rxMsg.data[1];
ser1516 21:13ac27349025 111 data.bytes[2] = rxMsg.data[2];
ser1516 21:13ac27349025 112 data.bytes[3] = rxMsg.data[3];
ser1516 21:13ac27349025 113 msgAvailable = false; // reset flag for next use
ser1516 21:13ac27349025 114 printf(" Id: %d, data: %f, counter : %d\n", rxMsg.id, data.f[0],rxMsg.data[4]);
hudakz 0:c5e5d0df6f2a 115
ser1516 21:13ac27349025 116 /*
ser1516 21:13ac27349025 117 printf("\r\nreceived message ID: \t%d\n\r", rxMsg.id);
ser1516 21:13ac27349025 118 for(int i=0; i<len; i++) {
ser1516 21:13ac27349025 119 printf("\t%x",rxMsg.data[i]);
ser1516 21:13ac27349025 120 }*/
ser1516 21:13ac27349025 121 printf("\r\n");
ser1516 21:13ac27349025 122 counter++;
ser1516 21:13ac27349025 123 if(counter == 12) {
ser1516 21:13ac27349025 124
ser1516 21:13ac27349025 125 counter = 0;
ser1516 21:13ac27349025 126 printf("\r\n""""""""""""""""""""""""""""""""""""""""""""""""\r\n");
ser1516 21:13ac27349025 127 }
ser1516 21:13ac27349025 128 // Filtering performed by software:
hudakz 0:c5e5d0df6f2a 129 }
ser1516 21:13ac27349025 130 if(to_send) {
ser1516 21:13ac27349025 131 to_send = 0;
ser1516 21:13ac27349025 132 txMsg.clear();
ser1516 21:13ac27349025 133 txMsg.id = 10;
ser1516 21:13ac27349025 134 txMsg << key_switch;
ser1516 21:13ac27349025 135 if(can.write(txMsg)) {
ser1516 21:13ac27349025 136 printf("sent message\r\n");
ser1516 21:13ac27349025 137 } else {
ser1516 21:13ac27349025 138 static char count = 0;
ser1516 21:13ac27349025 139 count++;
ser1516 21:13ac27349025 140 printf("transmission error\n\r overflow: %x\n\r", count);
ser1516 21:13ac27349025 141 if(count == 3) {
ser1516 21:13ac27349025 142 count = 0;
ser1516 21:13ac27349025 143 NVIC_SystemReset();
ser1516 21:13ac27349025 144 // attach 'CAN receive-complete' interrupt handler
ser1516 21:13ac27349025 145
ser1516 21:13ac27349025 146 }
ser1516 21:13ac27349025 147
hudakz 0:c5e5d0df6f2a 148 }
hudakz 0:c5e5d0df6f2a 149 }
hudakz 0:c5e5d0df6f2a 150 }
hudakz 0:c5e5d0df6f2a 151 }
hudakz 7:2dce8ed51091 152
hudakz 12:e91e44924194 153
hudakz 17:18d4d0ff26a6 154