PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

108 líneas
2.6KB

  1. /*
  2. * Object Oriented CAN example for Teensy 3.6 with Dual CAN buses
  3. * By Collin Kidder. Based upon the work of Pawelsky and Teachop
  4. *
  5. * Both buses are set to 500k to show things with a faster bus.
  6. * The reception of frames in this example is done via callbacks
  7. * to an object rather than polling. Frames are delivered as they come in.
  8. */
  9. #include <FlexCAN.h>
  10. #ifndef __MK66FX1M0__
  11. #error "Teensy 3.6 with dual CAN bus is required to run this example"
  12. #endif
  13. static CAN_message_t msg0,msg1;
  14. elapsedMillis RXtimer;
  15. elapsedMillis LEDtimer;
  16. uint32_t RXCount = 0;
  17. boolean displayCAN = true;
  18. const uint8_t redLEDpin = 2;
  19. boolean redLEDstate;
  20. class CANClass : public CANListener
  21. {
  22. public:
  23. void printFrame(CAN_message_t &frame, int mailbox);
  24. bool frameHandler(CAN_message_t &frame, int mailbox, uint8_t controller); //overrides the parent version so we can actually do something
  25. };
  26. void CANClass::printFrame(CAN_message_t &frame, int mailbox)
  27. {
  28. if(displayCAN){
  29. Serial.print(mailbox);
  30. Serial.print(" ID: ");
  31. Serial.print(frame.id, HEX);
  32. Serial.print(" Data: ");
  33. for (int c = 0; c < frame.len; c++)
  34. {
  35. Serial.print(frame.buf[c], HEX);
  36. Serial.write(' ');
  37. }
  38. Serial.println();
  39. }
  40. RXCount++;
  41. }
  42. bool CANClass::frameHandler(CAN_message_t &frame, int mailbox, uint8_t controller)
  43. {
  44. printFrame(frame, mailbox);
  45. return true;
  46. }
  47. CANClass CANClass0;
  48. CANClass CANClass1;
  49. // -------------------------------------------------------------
  50. void setup(void)
  51. {
  52. delay(1000);
  53. Serial.println(F("Hello Teensy 3.6 dual CAN Test With Objects."));
  54. pinMode(redLEDpin,OUTPUT);
  55. Can0.begin(1000000);
  56. Can1.begin(1000000);
  57. Can0.attachObj(&CANClass0);
  58. Can1.attachObj(&CANClass1);
  59. CAN_filter_t allPassFilter;
  60. allPassFilter.id=0;
  61. allPassFilter.ext=1;
  62. allPassFilter.rtr=0;
  63. //leave the first 4 mailboxes to use the default filter. Just change the higher ones
  64. for (uint8_t filterNum = 4; filterNum < 16;filterNum++){
  65. Can0.setFilter(allPassFilter,filterNum);
  66. Can1.setFilter(allPassFilter,filterNum);
  67. }
  68. for (uint8_t filterNum = 0; filterNum < 16;filterNum++){
  69. CANClass0.attachMBHandler(filterNum);
  70. CANClass1.attachMBHandler(filterNum);
  71. }
  72. //CANClass0.attachGeneralHandler();
  73. //CANClass1.attachGeneralHandler();
  74. }
  75. // -------------------------------------------------------------
  76. void loop(void)
  77. {
  78. if (RXtimer > 10000){
  79. Serial.println("Total Received Messages in 10 Sec:");
  80. Serial.println(RXCount);
  81. RXtimer = 0;
  82. RXCount=0;
  83. displayCAN = !displayCAN;
  84. }
  85. if (LEDtimer >250){
  86. LEDtimer = 0;
  87. redLEDstate = !redLEDstate;
  88. digitalWrite(redLEDpin, redLEDstate);
  89. }
  90. }