1+ /* *
2+ * @file AtomCan.ino
3+ * @author Tinyu ([email protected] ) 4+ * @brief M5AtomS3 Atom Can Base Test
5+ * @version 0.1
6+ * @date 2024-06-21
7+ *
8+ *
9+ * @Hardwares: M5AtomS3 + Atom Can Base
10+ * @Platform Version: Arduino M5Stack Board Manager v2.1.0
11+ * @Dependent Library:
12+ * M5GFX: https://github.com/m5stack/M5GFX
13+ * M5Unified: https://github.com/m5stack/M5Unified
14+ * M5AtomS3: https://github.com/m5stack/M5AtomS3
15+ */
16+ #include < M5AtomS3.h>
17+ #include " driver/twai.h"
18+
19+ // Pins used to connect to CAN bus transceiver:
20+ #define RX_PIN 6
21+ #define TX_PIN 5
22+
23+ // Interval:
24+ #define TRANSMIT_RATE_MS 1000
25+
26+ #define POLLING_RATE_MS 1000
27+
28+ #define ARDUINO_USB_CDC_ON_BOOT 1
29+
30+ static bool driver_installed = false ;
31+
32+ unsigned long previousMillis = 0 ; // will store last time a message was send
33+
34+ void setup () {
35+ auto cfg = M5.config ();
36+ AtomS3.begin (cfg);
37+
38+ // Initialize configuration structures using macro initializers
39+ twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT (
40+ (gpio_num_t )TX_PIN, (gpio_num_t )RX_PIN, TWAI_MODE_NORMAL);
41+ twai_timing_config_t t_config =
42+ TWAI_TIMING_CONFIG_500KBITS (); // Look in the api-reference for other
43+ // speed sets.
44+ twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL ();
45+
46+ // Install TWAI driver
47+ if (twai_driver_install (&g_config, &t_config, &f_config) == ESP_OK) {
48+ Serial.println (" Driver installed" );
49+ } else {
50+ Serial.println (" Failed to install driver" );
51+ return ;
52+ }
53+
54+ // Start TWAI driver
55+ if (twai_start () == ESP_OK) {
56+ Serial.println (" Driver started" );
57+ } else {
58+ Serial.println (" Failed to start driver" );
59+ return ;
60+ }
61+
62+ // Reconfigure alerts to detect TX alerts and Bus-Off errors
63+ uint32_t alerts_to_enable = TWAI_ALERT_TX_IDLE | TWAI_ALERT_TX_SUCCESS |
64+ TWAI_ALERT_TX_FAILED | TWAI_ALERT_ERR_PASS |
65+ TWAI_ALERT_BUS_ERROR;
66+ if (twai_reconfigure_alerts (alerts_to_enable, NULL ) == ESP_OK) {
67+ Serial.println (" CAN Alerts reconfigured" );
68+ } else {
69+ Serial.println (" Failed to reconfigure alerts" );
70+ return ;
71+ }
72+
73+ // TWAI driver is now successfully installed and started
74+ driver_installed = true ;
75+ }
76+
77+ static void send_message () {
78+ // Send message
79+
80+ // Configure message to transmit
81+ twai_message_t message;
82+ message.identifier = 0x0F6 ;
83+ message.data_length_code = 4 ;
84+ for (int i = 0 ; i < 4 ; i++) {
85+ message.data [i] = 0 ;
86+ }
87+
88+ // Queue message for transmission
89+ if (twai_transmit (&message, pdMS_TO_TICKS (1000 )) == ESP_OK) {
90+ printf (" Message queued for transmission\n " );
91+ } else {
92+ printf (" Failed to queue message for transmission\n " );
93+ }
94+ }
95+
96+ void loop () {
97+ if (!driver_installed) {
98+ delay (1000 );
99+ return ;
100+ }
101+
102+ uint32_t alerts_triggered;
103+ twai_read_alerts (&alerts_triggered, pdMS_TO_TICKS (POLLING_RATE_MS));
104+ twai_status_info_t twaistatus;
105+ twai_get_status_info (&twaistatus);
106+
107+ if (alerts_triggered & TWAI_ALERT_ERR_PASS) {
108+ Serial.println (" Alert: TWAI controller has become error passive." );
109+ }
110+ if (alerts_triggered & TWAI_ALERT_BUS_ERROR) {
111+ Serial.println (
112+ " Alert: A (Bit, Stuff, CRC, Form, ACK) error has occurred on the "
113+ " bus." );
114+ Serial.printf (" Bus error count: %lu\n " , twaistatus.bus_error_count );
115+ }
116+ if (alerts_triggered & TWAI_ALERT_TX_FAILED) {
117+ Serial.println (" Alert: The Transmission failed." );
118+ Serial.printf (" TX buffered: %lu\t " , twaistatus.msgs_to_tx );
119+ Serial.printf (" TX error: %lu\t " , twaistatus.tx_error_counter );
120+ Serial.printf (" TX failed: %lu\n " , twaistatus.tx_failed_count );
121+ }
122+ if (alerts_triggered & TWAI_ALERT_TX_SUCCESS) {
123+ Serial.println (" Alert: The Transmission was successful." );
124+ Serial.printf (" TX buffered: %lu\t " , twaistatus.msgs_to_tx );
125+ }
126+
127+ unsigned long currentMillis = millis ();
128+ if (currentMillis - previousMillis >= TRANSMIT_RATE_MS) {
129+ previousMillis = currentMillis;
130+ send_message ();
131+ }
132+ }
0 commit comments