Advertisement
Guest User

Untitled

a guest
Apr 10th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include "kinetis_flexcan.h"
  2. #include <FlexCAN.h> //https://github.com/pawelsky/FlexCAN_Library
  3.  
  4. // the setup function runs once when you press reset or power the
  5. FlexCAN can_ptr = FlexCAN(125000, 0,0,0);
  6. CAN_message_t enable_periodic_status_1;
  7.  
  8. uint32_t spark_max_device_type_bits = 0x02;
  9. uint32_t spark_max_manufacturer_id_bits = 0x05;
  10.  
  11. uint8_t msg_byte = 0;
  12. void setup() {
  13.     can_ptr.begin(); //500kpbs
  14.    
  15.     enable_periodic_status_1.ext = 1;
  16.     enable_periodic_status_1.id =
  17.         spark_max_device_type_bits << 24 //[28:24] device type
  18.         | spark_max_manufacturer_id_bits << 16 //[23:16] manufacturer id
  19.         | (0x061 | 0) << 6// [15:6] CMD_API_STAT1
  20.         | 2; //[5:0] device ID
  21.  
  22.     for (size_t i = 0; i < 8; i++)
  23.     {
  24.         enable_periodic_status_1.buf[i] = 0x00;
  25.     }
  26.  
  27.     enable_periodic_status_1.buf[0] = 0x0F;
  28.     enable_periodic_status_1.buf[1] = 0x0F;
  29.     enable_periodic_status_1.len = 2;
  30.     enable_periodic_status_1.timeout = 100; //100ms to send msg before timeout
  31.     Serial.begin(9600);
  32.     Serial.println("Starting CAN listener");
  33. }
  34.  
  35.  
  36. // the loop function runs over and over again until power down or reset
  37. void loop() {
  38.  
  39.     if (Serial.available() > 0)
  40.     {
  41.         Serial.read();
  42.         Serial.flush();
  43.         Serial.println("Sending can msg");
  44.         Serial.print("CAN msg ID: ");
  45.         Serial.println(enable_periodic_status_1.id, HEX);
  46.  
  47.         int ret_val = can_ptr.write(enable_periodic_status_1);
  48.  
  49.         Serial.print("ret val: ");
  50.         Serial.println(ret_val);
  51.         delay(1000);
  52.        
  53.         uint32_t can_err = CAN0_ESR1; //
  54.         if (can_err != 0) {
  55.             Serial.print("CAN0_ESR1: 0x");
  56.             Serial.println(can_err, HEX);
  57.         }
  58.     }
  59.    
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement