1
+ /* *
2
+ * @file Slave.ino
3
+ * @author SeanKwok ([email protected] )
4
+ * @brief Unit Mini CAN Test Master
5
+ * @version 0.1
6
+ * @date 2024-02-01
7
+ *
8
+ *
9
+ * @Hardwares: M5CoreS3 + Unit Mini CAN
10
+ * @Platform Version: Arduino M5Stack Board Manager v2.0.9
11
+ * @Dependent Library:
12
+ * M5GFX: https://github.com/m5stack/M5GFX
13
+ * M5Unified: https://github.com/m5stack/M5Unified
14
+ * M5CoreS3: https://github.com/m5stack/M5CoreS3
15
+ */
16
+
17
+ #include < stdio.h>
18
+ #include < stdlib.h>
19
+ #include " freertos/FreeRTOS.h"
20
+ #include " freertos/task.h"
21
+ #include " freertos/queue.h"
22
+ #include " freertos/semphr.h"
23
+ #include " esp_err.h"
24
+ #include " esp_log.h"
25
+ #include " driver/twai.h"
26
+ #include " esp_err.h"
27
+
28
+ #include " M5CoreS3.h"
29
+
30
+ M5Canvas canvas (&CoreS3.Display);
31
+
32
+ /* --------------------- Definitions and static variables ------------------ */
33
+
34
+ #define TX_TASK_PRIO 8 // Receiving task priority
35
+ #define TX_GPIO_NUM gpio_num_t (9 )
36
+ #define RX_GPIO_NUM gpio_num_t (8 )
37
+
38
+ static const twai_general_config_t g_config =
39
+ TWAI_GENERAL_CONFIG_DEFAULT(TX_GPIO_NUM, RX_GPIO_NUM, TWAI_MODE_NORMAL);
40
+ static const twai_timing_config_t t_config = TWAI_TIMING_CONFIG_25KBITS();
41
+ static const twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
42
+
43
+ #define ID_SLAVE_1 0x0B1
44
+
45
+ static const twai_message_t slave_1_on = {.identifier = ID_SLAVE_1,
46
+ .data_length_code = 8 ,
47
+ .data = {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 }};
48
+
49
+ static const twai_message_t slave_1_off = {.identifier = ID_SLAVE_1,
50
+ .data_length_code = 8 ,
51
+ .data = {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }};
52
+
53
+ static void twai_transmit_task (void *arg) {
54
+ while (1 ) {
55
+ twai_transmit (&slave_1_on, portMAX_DELAY);
56
+
57
+ Serial.printf (" send cmd on to 0x%02X\r\n " , ID_SLAVE_1);
58
+ Serial.print (" send data: " );
59
+ canvas.printf (" send cmd on to 0x%02X\r\n " , ID_SLAVE_1);
60
+ canvas.print (" send data: " );
61
+ for (int i = 0 ; i < slave_1_on.data_length_code ; i++) {
62
+ Serial.printf (" 0x%02X " , slave_1_on.data [i]);
63
+ canvas.printf (" 0x%02X " , slave_1_on.data [i]);
64
+ }
65
+ Serial.println ();
66
+ canvas.println (" \r\n " );
67
+ canvas.pushSprite (0 , 0 );
68
+ vTaskDelay (pdMS_TO_TICKS (1000 ));
69
+
70
+ twai_transmit (&slave_1_off, portMAX_DELAY);
71
+ Serial.printf (" send cmd off to 0x%02X\r\n " , ID_SLAVE_1);
72
+ Serial.print (" send data: " );
73
+ canvas.printf (" send cmd off to 0x%02X\r\n " , ID_SLAVE_1);
74
+ canvas.print (" send data: " );
75
+ for (int i = 0 ; i < slave_1_off.data_length_code ; i++) {
76
+ Serial.printf (" 0x%02X " , slave_1_off.data [i]);
77
+ canvas.printf (" 0x%02X " , slave_1_on.data [i]);
78
+ }
79
+ Serial.println ();
80
+ canvas.println (" \r\n " );
81
+ canvas.pushSprite (0 , 0 );
82
+ vTaskDelay (pdMS_TO_TICKS (1000 ));
83
+ }
84
+ vTaskDelete (NULL );
85
+ }
86
+
87
+ void setup () {
88
+ auto cfg = M5.config ();
89
+ CoreS3.begin (cfg);
90
+
91
+ canvas.setColorDepth (1 ); // mono color
92
+ canvas.createSprite (CoreS3.Display .width (), CoreS3.Display .height ());
93
+ canvas.setPaletteColor (1 , GREEN);
94
+ canvas.setTextScroll (true );
95
+
96
+ canvas.println (" CAN Master" );
97
+
98
+ canvas.pushSprite (0 , 0 );
99
+
100
+ ESP_ERROR_CHECK (twai_driver_install (&g_config, &t_config, &f_config));
101
+ Serial.println (" Driver installed" );
102
+ ESP_ERROR_CHECK (twai_start ());
103
+ xTaskCreatePinnedToCore (twai_transmit_task, " twai_transmit_task" , 4096 ,
104
+ NULL , TX_TASK_PRIO, NULL , tskNO_AFFINITY);
105
+ }
106
+
107
+ void loop () {
108
+ }
0 commit comments