comit to publish

Dependencies:   CANnucleo CANnucleo_Hello mbed

Fork of CANnucleo_Hello by Zoltan Hudak

Committer:
ser1516
Date:
Mon Nov 28 23:15:14 2016 +0000
Revision:
21:f3a7ffd48020
Parent:
20:eb1a8042605e
comit to publish

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:f3a7ffd48020 4 * Two affordable (less than $3 on ebay) STM32F103C8T6 boards (20kB SRAM, 64kB Flash),
ser1516 21:f3a7ffd48020 5 * (see [https://developer.mbed.org/users/hudakz/code/STM32F103C8T6_Hello/] for more details)
ser1516 21:f3a7ffd48020 6 * are connected to the same CAN bus via transceivers (MCP2551 or TJA1040, or etc.).
ser1516 21:f3a7ffd48020 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:f3a7ffd48020 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:f3a7ffd48020 20 */
hudakz 0:c5e5d0df6f2a 21
ser1516 21:f3a7ffd48020 22 //#define TARGET_STM32F103C8T6 1 // uncomment this line when using STM32F103C8T6 boards!
ser1516 21:f3a7ffd48020 23 //#define BOARD1 1 // comment out this line when compiling for board #2
ser1516 21:f3a7ffd48020 24
hudakz 0:c5e5d0df6f2a 25
ser1516 21:f3a7ffd48020 26 #define start_charge_msg "\x22\x80\x24\x01"
ser1516 21:f3a7ffd48020 27 #define stop_charge_msg "\x22\x80\x24\x02"
ser1516 21:f3a7ffd48020 28 #define charger_startup_msg "\x0\xB\x0\x0\x0\x0\x0\xB" // charger writes this during startup
ser1516 21:f3a7ffd48020 29 #define change_charger_can_bitrate_1Mbs "\x22\xDA\x20\x01\x04"
hudakz 0:c5e5d0df6f2a 30
ser1516 21:f3a7ffd48020 31 const unsigned int RX_ID = 0x100;
ser1516 21:f3a7ffd48020 32 const unsigned int TX_ID = 0x101;
hudakz 6:7ff95ce72f6d 33
hudakz 19:872e304d7e17 34 #include "CANnucleo.h"
hudakz 16:a86f339d1c25 35 #include "mbed.h"
hudakz 16:a86f339d1c25 36
ser1516 21:f3a7ffd48020 37 /*
hudakz 17:18d4d0ff26a6 38 * To avaoid name collision with the CAN and CANMessage classes built into the mbed library
hudakz 17:18d4d0ff26a6 39 * the CANnucleo's CAN and CANMessage classes have been moved into the CANnucleo namespace.
hudakz 20:eb1a8042605e 40 * Remember to qualify them with the CANnucleo namespace.
hudakz 17:18d4d0ff26a6 41 */
hudakz 17:18d4d0ff26a6 42 CANnucleo::CAN can(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name
hudakz 17:18d4d0ff26a6 43 CANnucleo::CANMessage rxMsg;
hudakz 17:18d4d0ff26a6 44 CANnucleo::CANMessage txMsg;
ser1516 21:f3a7ffd48020 45 CANnucleo::CANMessage throttle_txMsg;
ser1516 21:f3a7ffd48020 46
ser1516 21:f3a7ffd48020 47
ser1516 21:f3a7ffd48020 48 DigitalOut led(PA_5);
ser1516 21:f3a7ffd48020 49 DigitalOut controller_key_switch(PA_6); // SS_Rel
ser1516 21:f3a7ffd48020 50
hudakz 18:22977a898fe9 51 Timer timer;
hudakz 17:18d4d0ff26a6 52 int counter = 0;
hudakz 17:18d4d0ff26a6 53 volatile bool msgAvailable = false;
ser1516 21:f3a7ffd48020 54 volatile bool to_send = false;
hudakz 0:c5e5d0df6f2a 55
hudakz 16:a86f339d1c25 56 /**
hudakz 16:a86f339d1c25 57 * @brief 'CAN receive-complete' interrup handler.
hudakz 16:a86f339d1c25 58 * @note Called on arrival of new CAN message.
hudakz 16:a86f339d1c25 59 * Keep it as short as possible.
ser1516 21:f3a7ffd48020 60 * @param
ser1516 21:f3a7ffd48020 61 * @retval
hudakz 16:a86f339d1c25 62 */
ser1516 21:f3a7ffd48020 63 void onMsgReceived()
ser1516 21:f3a7ffd48020 64 {
hudakz 16:a86f339d1c25 65 msgAvailable = true;
hudakz 16:a86f339d1c25 66 }
hudakz 16:a86f339d1c25 67
hudakz 16:a86f339d1c25 68 /**
hudakz 16:a86f339d1c25 69 * @brief Main
hudakz 16:a86f339d1c25 70 * @note
ser1516 21:f3a7ffd48020 71 * @param
hudakz 16:a86f339d1c25 72 * @retval
hudakz 16:a86f339d1c25 73 */
ser1516 21:f3a7ffd48020 74 bool to_charge = 0;
ser1516 21:f3a7ffd48020 75 bool charge = 0;
ser1516 21:f3a7ffd48020 76 void led_error()
ser1516 21:f3a7ffd48020 77 {
ser1516 21:f3a7ffd48020 78
ser1516 21:f3a7ffd48020 79 led = !led;
ser1516 21:f3a7ffd48020 80 wait(0.1);
ser1516 21:f3a7ffd48020 81 led=!led;
ser1516 21:f3a7ffd48020 82 wait(0.1);
ser1516 21:f3a7ffd48020 83 led = !led;
ser1516 21:f3a7ffd48020 84 wait(0.1);
ser1516 21:f3a7ffd48020 85 led = !led;
ser1516 21:f3a7ffd48020 86 wait(0.1);
ser1516 21:f3a7ffd48020 87 led = !led;
ser1516 21:f3a7ffd48020 88
ser1516 21:f3a7ffd48020 89 }
ser1516 21:f3a7ffd48020 90 int build_message(unsigned char * badjoras, const char * command)
ser1516 21:f3a7ffd48020 91 {
ser1516 21:f3a7ffd48020 92 char *src;
ser1516 21:f3a7ffd48020 93 strcpy(src,command);
ser1516 21:f3a7ffd48020 94 strncpy((char *) badjoras,src,sizeof(command)-1);
ser1516 21:f3a7ffd48020 95 return (sizeof(command)-1);
ser1516 21:f3a7ffd48020 96 }
hudakz 0:c5e5d0df6f2a 97
ser1516 21:f3a7ffd48020 98 void flip()
ser1516 21:f3a7ffd48020 99 {
ser1516 21:f3a7ffd48020 100 to_send=1; // append first data item
ser1516 21:f3a7ffd48020 101 to_charge=1;
ser1516 21:f3a7ffd48020 102 //controller_key_switch = !controller_key_switch;
ser1516 21:f3a7ffd48020 103 led = !led;
ser1516 21:f3a7ffd48020 104 charge = !charge;
ser1516 21:f3a7ffd48020 105 }
ser1516 21:f3a7ffd48020 106
ser1516 21:f3a7ffd48020 107 bool controller = 0;//flag for turning on controller_key_switch
ser1516 21:f3a7ffd48020 108 Ticker flipper;
ser1516 21:f3a7ffd48020 109 void change_can_bitrate(){
ser1516 21:f3a7ffd48020 110 to_send= 1;
ser1516 21:f3a7ffd48020 111 }
ser1516 21:f3a7ffd48020 112 InterruptIn button(PC_13);
ser1516 21:f3a7ffd48020 113
ser1516 21:f3a7ffd48020 114 int main()
ser1516 21:f3a7ffd48020 115
ser1516 21:f3a7ffd48020 116 {
ser1516 21:f3a7ffd48020 117 can.frequency(125000); // set bit rate to 1Mbps
ser1516 21:f3a7ffd48020 118 can.attach(&onMsgReceived); // attach 'CAN receive-complete' interrupt handler
ser1516 21:f3a7ffd48020 119 flipper.attach(&change_can_bitrate, 5); //heartbeat
ser1516 21:f3a7ffd48020 120 button.rise(&flip);
ser1516 21:f3a7ffd48020 121 led=0;
ser1516 21:f3a7ffd48020 122 printf("badjoras\n");
ser1516 21:f3a7ffd48020 123
ser1516 21:f3a7ffd48020 124 while(true) {
ser1516 21:f3a7ffd48020 125
hudakz 16:a86f339d1c25 126 if(msgAvailable) {
hudakz 16:a86f339d1c25 127 msgAvailable = false; // reset flag for next use
ser1516 21:f3a7ffd48020 128 int len = can.read(rxMsg);
ser1516 21:f3a7ffd48020 129 printf("Id: %x ", rxMsg.id);
ser1516 21:f3a7ffd48020 130 for(int i = 0; i<len; i++) {
ser1516 21:f3a7ffd48020 131 printf("%x,", rxMsg.data[i]);
ser1516 21:f3a7ffd48020 132 }
ser1516 21:f3a7ffd48020 133 printf("\n\r");
ser1516 21:f3a7ffd48020 134 /*
ser1516 21:f3a7ffd48020 135 rxMsg >> controller;
ser1516 21:f3a7ffd48020 136 led = controller;
ser1516 21:f3a7ffd48020 137 controller_key_switch = controller;*/
ser1516 21:f3a7ffd48020 138 if(rxMsg.id == 0x28B) {
ser1516 21:f3a7ffd48020 139 if (memcmp(rxMsg.data,charger_startup_msg,8)==0) {
ser1516 21:f3a7ffd48020 140 printf(" this is the message\n\r");
ser1516 21:f3a7ffd48020 141 txMsg.clear(); // clear Tx message storage
ser1516 21:f3a7ffd48020 142 txMsg.id = 0x60B;
ser1516 21:f3a7ffd48020 143 build_message(txMsg.data,stop_charge_msg);
ser1516 21:f3a7ffd48020 144 txMsg.len = 4;
ser1516 21:f3a7ffd48020 145 to_send = 1;
ser1516 21:f3a7ffd48020 146 }
ser1516 21:f3a7ffd48020 147 }
ser1516 21:f3a7ffd48020 148 }
ser1516 21:f3a7ffd48020 149 if(to_send) {
ser1516 21:f3a7ffd48020 150 to_send = 0;
ser1516 21:f3a7ffd48020 151 if(to_charge) {
ser1516 21:f3a7ffd48020 152 to_charge = 0;
ser1516 21:f3a7ffd48020 153 if(charge) {
ser1516 21:f3a7ffd48020 154 txMsg.clear(); // clear Tx message storage
ser1516 21:f3a7ffd48020 155 txMsg.id = 0x60B;
ser1516 21:f3a7ffd48020 156 build_message(txMsg.data,start_charge_msg);
ser1516 21:f3a7ffd48020 157 txMsg.len = 4;
ser1516 21:f3a7ffd48020 158 } else {
ser1516 21:f3a7ffd48020 159 txMsg.clear(); // clear Tx message storage
ser1516 21:f3a7ffd48020 160 txMsg.id = 0x60B;
ser1516 21:f3a7ffd48020 161 build_message(txMsg.data,stop_charge_msg);
ser1516 21:f3a7ffd48020 162 txMsg.len = 4;
ser1516 21:f3a7ffd48020 163 }
ser1516 21:f3a7ffd48020 164
ser1516 21:f3a7ffd48020 165 } else {
ser1516 21:f3a7ffd48020 166 txMsg.clear();
ser1516 21:f3a7ffd48020 167 txMsg.id = 0x60B;
ser1516 21:f3a7ffd48020 168 build_message(txMsg.data, change_charger_can_bitrate_1Mbs);
ser1516 21:f3a7ffd48020 169 txMsg.len = 8;
ser1516 21:f3a7ffd48020 170 }
ser1516 21:f3a7ffd48020 171 if(!can.write(txMsg)) {
ser1516 21:f3a7ffd48020 172 led_error();
ser1516 21:f3a7ffd48020 173 } else {
ser1516 21:f3a7ffd48020 174 printf("sent message: ID: %x DATA: %x %x %x %x %x %x %x %x\n\r",txMsg.id,txMsg.data[0],txMsg.data[1],txMsg.data[2],txMsg.data[3],txMsg.data[4],txMsg.data[5],txMsg.data[6],txMsg.data[7]);
hudakz 0:c5e5d0df6f2a 175 }
hudakz 0:c5e5d0df6f2a 176 }
hudakz 0:c5e5d0df6f2a 177 }
hudakz 0:c5e5d0df6f2a 178 }
hudakz 7:2dce8ed51091 179
hudakz 12:e91e44924194 180
hudakz 17:18d4d0ff26a6 181