TLMoto

Dependencies:   CANnucleo CANnucleo_Hello mbed

Fork of CANnucleo_Hello by Zoltan Hudak

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * An example showing how to use the CANnucleo library:
00003  *
00004  * Two affordable (less than $3 on ebay) STM32F103C8T6 boards (20kB SRAM, 64kB Flash),
00005  * (see [https://developer.mbed.org/users/hudakz/code/STM32F103C8T6_Hello/] for more details)
00006  * are connected to the same CAN bus via transceivers (MCP2551 or TJA1040, or etc.).
00007  * CAN transceivers are not part of NUCLEO boards, therefore must be added by you.
00008  * Remember also that CAN bus (even a short one) must be terminated with 120 Ohm resitors at both ends.
00009  *
00010  * For more details see the wiki page <https://developer.mbed.org/users/hudakz/code/CANnucleo_Hello/>
00011  *
00012  * NOTE: If you'd like to use the official NUCLEO boards comment out line 22
00013  *
00014  * The same code is used for both NUCLEO boards, but:
00015  *      For board #1 compile the example without any change.
00016  *      For board #2 comment out line 23 before compiling
00017  *
00018  * Once the binaries have been downloaded to the boards reset board #1.
00019  *
00020  */
00021 
00022 //#define TARGET_STM32F103C8T6  1     // uncomment this line when using STM32F103C8T6 boards!
00023 //#define BOARD1                1     // comment out this line when compiling for board #2
00024 
00025 
00026 
00027 const unsigned int RX_ID = 0x100;
00028 const unsigned int TX_ID = 0x101;
00029 
00030 #include "CANnucleo.h"
00031 #include "mbed.h"
00032 
00033 /*
00034  * To avaoid name collision with the CAN and CANMessage classes built into the mbed library
00035  * the CANnucleo's CAN and CANMessage classes have been moved into the CANnucleo namespace.
00036  * Remember to qualify them with the CANnucleo namespace.
00037  */
00038 CANnucleo::CAN          can(PA_11, PA_12);  // CAN Rx pin name, CAN Tx pin name
00039 CANnucleo::CANMessage   rxMsg;
00040 CANnucleo::CANMessage   txMsg;
00041 CANnucleo::CANMessage   throttle_txMsg;
00042 
00043 
00044 DigitalOut              led(PA_5);
00045 
00046 Timer                   timer;
00047 int                     counter = 0;
00048 volatile bool           msgAvailable = false;
00049 volatile bool           to_send = false;
00050 
00051 /**
00052  * @brief   'CAN receive-complete' interrup handler.
00053  * @note    Called on arrival of new CAN message.
00054  *          Keep it as short as possible.
00055  * @param
00056  * @retval
00057  */
00058 void onMsgReceived()
00059 {
00060     msgAvailable = true;
00061 }
00062 
00063 /**
00064  * @brief   Main
00065  * @note
00066  * @param
00067  * @retval
00068  */
00069 
00070 bool key_switch = 0;
00071 
00072 void flip()
00073 {
00074     key_switch = !key_switch;
00075     led = key_switch;
00076 
00077     to_send=1;
00078     //printf("controller switch\r\n");
00079 
00080 
00081 
00082     // to_send = 1;
00083 }
00084 
00085 Ticker flipper;
00086 
00087 typedef union can_union {
00088     int i[2];
00089     char bytes[8];
00090     float f[2];
00091 } data;
00092 
00093 int main()
00094 
00095 {
00096     can.frequency(1000000);                     // set bit rate to 1Mbps
00097     can.attach(&onMsgReceived);                 // attach 'CAN receive-complete' interrupt handler
00098     flipper.attach(&flip, 30);                 // turn on or off
00099     led=key_switch;
00100     timer.start();  // start timer
00101 
00102     printf("started\r\n");
00103     while(true) {
00104 
00105         if(msgAvailable) {
00106             static int counter = 0;
00107             data data;
00108             int len = can.read(rxMsg);
00109             data.bytes[0] = rxMsg.data[0];
00110             data.bytes[1] = rxMsg.data[1];
00111             data.bytes[2] = rxMsg.data[2];
00112             data.bytes[3] = rxMsg.data[3];
00113             msgAvailable = false;               // reset flag for next use
00114             printf(" Id: %d, data: %f, counter : %d\n", rxMsg.id, data.f[0],rxMsg.data[4]);
00115 
00116             /*
00117             printf("\r\nreceived message ID: \t%d\n\r", rxMsg.id);
00118             for(int i=0; i<len; i++) {
00119                 printf("\t%x",rxMsg.data[i]);
00120             }*/
00121             printf("\r\n");
00122             counter++;
00123             if(counter == 12) {
00124 
00125                 counter = 0;
00126                 printf("\r\n""""""""""""""""""""""""""""""""""""""""""""""""\r\n");
00127             }
00128             // Filtering performed by software:
00129         }
00130         if(to_send) {
00131             to_send = 0;
00132             txMsg.clear();
00133             txMsg.id = 10;
00134             txMsg << key_switch;
00135             if(can.write(txMsg)) {
00136                 printf("sent message\r\n");
00137             } else {
00138                 static char count = 0;
00139                 count++;
00140                 printf("transmission error\n\r overflow: %x\n\r", count);
00141                 if(count == 3) {
00142                     count = 0;
00143                     NVIC_SystemReset();
00144                     // attach 'CAN receive-complete' interrupt handler
00145 
00146                 }
00147 
00148             }
00149         }
00150     }
00151 }
00152 
00153 
00154