CAN.ino 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. *******************************************************************************
  3. * Copyright (c) 2022 by M5Stack
  4. * Equipped with M5Core sample source code
  5. * 配套 M5Core 示例源代码
  6. * Visit for more information: https://docs.m5stack.com/en/unit/can
  7. * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/can
  8. *
  9. * Describe: CA-IS3050G. CAN transceiver unit
  10. * Date: 2021/11/01
  11. ******************************************** ***********************************
  12. Please connect port B (26, 36), the device will automatically send and receive
  13. messages through the CAN bus If there are phenomena such as failure of normal
  14. communication, please check the communication rate or add a terminal resistance
  15. between the H and L phases Please install library before compiling: ESP32CAN:
  16. https://github.com/miwagner/ESP32-Arduino-CAN
  17. */
  18. #include <CAN_config.h>
  19. #include <ESP32CAN.h>
  20. #include <M5GFX.h>
  21. #include <M5Stack.h>
  22. M5GFX display;
  23. M5Canvas canvas(&display);
  24. CAN_device_t CAN_cfg; // CAN Config
  25. unsigned long previousMillis =
  26. 0; // will store last time a CAN Message was send
  27. const int interval =
  28. 1000; // interval at which send CAN Messages (milliseconds)
  29. const int rx_queue_size = 10; // Receive Queue size
  30. uint8_t count = 0;
  31. void setup() {
  32. M5.begin();
  33. Serial.println("Basic Demo - ESP32-Arduino-CAN");
  34. display.begin();
  35. canvas.setColorDepth(1); // mono color
  36. canvas.createSprite(display.width(), display.height());
  37. canvas.setTextSize(1);
  38. canvas.setPaletteColor(1, GREEN);
  39. canvas.setTextScroll(true);
  40. CAN_cfg.speed = CAN_SPEED_125KBPS;
  41. CAN_cfg.tx_pin_id = GPIO_NUM_26;
  42. CAN_cfg.rx_pin_id = GPIO_NUM_36;
  43. CAN_cfg.rx_queue = xQueueCreate(rx_queue_size, sizeof(CAN_frame_t));
  44. // Init CAN Module
  45. ESP32Can.CANInit();
  46. canvas.println("Init CAN Module.....");
  47. canvas.println("Waiting Frame");
  48. canvas.println(
  49. "If there are phenomena such as failure of normal communication");
  50. canvas.println(
  51. "Please check the communication rate or add a terminal resistance "
  52. "between the H and L phases");
  53. canvas.pushSprite(0, 0);
  54. }
  55. void loop() {
  56. CAN_frame_t rx_frame;
  57. unsigned long currentMillis = millis();
  58. // Receive next CAN frame from queue
  59. if (xQueueReceive(CAN_cfg.rx_queue, &rx_frame, 3 * portTICK_PERIOD_MS) ==
  60. pdTRUE) {
  61. if (rx_frame.FIR.B.FF == CAN_frame_std) {
  62. Serial.println("New standard frame");
  63. canvas.println("New standard frame");
  64. } else {
  65. Serial.println("New extended frame");
  66. canvas.println("New extended frame");
  67. }
  68. if (rx_frame.FIR.B.RTR == CAN_RTR) {
  69. Serial.printf("RTR from 0x%08X, DLC %d\r\n", rx_frame.MsgID,
  70. rx_frame.FIR.B.DLC);
  71. canvas.printf("RTR from 0x%08X, DLC %d\r\n", rx_frame.MsgID,
  72. rx_frame.FIR.B.DLC);
  73. } else {
  74. Serial.printf("from 0x%08X, DLC %d, Data \r\n", rx_frame.MsgID,
  75. rx_frame.FIR.B.DLC);
  76. canvas.printf("from 0x%08X, DLC %d, Data \r\n", rx_frame.MsgID,
  77. rx_frame.FIR.B.DLC);
  78. for (int i = 0; i < rx_frame.FIR.B.DLC; i++) {
  79. Serial.printf("0x%02X ", rx_frame.data.u8[i]);
  80. canvas.printf("0x%02X ", rx_frame.data.u8[i]);
  81. }
  82. Serial.println("\n");
  83. canvas.println("\n");
  84. }
  85. canvas.pushSprite(0, 0);
  86. }
  87. // Send CAN Message
  88. if (currentMillis - previousMillis >= interval) {
  89. previousMillis = currentMillis;
  90. CAN_frame_t tx_frame;
  91. tx_frame.FIR.B.FF = CAN_frame_std;
  92. tx_frame.MsgID = 0x001;
  93. tx_frame.FIR.B.DLC = 8;
  94. tx_frame.data.u8[0] = 0x00;
  95. tx_frame.data.u8[1] = 0x01;
  96. tx_frame.data.u8[2] = 0x02;
  97. tx_frame.data.u8[3] = 0x03;
  98. tx_frame.data.u8[4] = 0x04;
  99. tx_frame.data.u8[5] = 0x05;
  100. tx_frame.data.u8[6] = 0x06;
  101. tx_frame.data.u8[7] = 0x07;
  102. ESP32Can.CANWriteFrame(&tx_frame);
  103. }
  104. delay(1);
  105. }