Skip to content

Commit

Permalink
add MiniCAN example
Browse files Browse the repository at this point in the history
  • Loading branch information
Gitshaoxiang committed Feb 1, 2024
1 parent 8646529 commit d98b0c5
Show file tree
Hide file tree
Showing 13 changed files with 183 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/AtomicBase/AtomicGPS/GPS/GPS.ino
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void setup() {

AtomS3.Display.setTextColor(GREEN);
AtomS3.Display.setTextDatum(middle_center);
AtomS3.Display.setTextFont(&fonts::FreeSerifBold24pt7b);
AtomS3.Display.setFont(&fonts::FreeSerifBold24pt7b);
AtomS3.Display.setTextSize(1);
AtomS3.Display.drawString("GPS", AtomS3.Display.width() / 2,
AtomS3.Display.height() / 2);
Expand Down
2 changes: 1 addition & 1 deletion examples/AtomicBase/AtomicGPS/MicroSD/MicroSD.ino
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ void setup() {

AtomS3.Display.setTextColor(GREEN);
AtomS3.Display.setTextDatum(middle_center);
AtomS3.Display.setTextFont(&fonts::FreeSerifItalic12pt7b);
AtomS3.Display.setFont(&fonts::FreeSerifItalic12pt7b);
AtomS3.Display.setTextSize(1);
AtomS3.Display.drawString("MicroSD", AtomS3.Display.width() / 2, 20);

Expand Down
2 changes: 1 addition & 1 deletion examples/AtomicBase/AtomicHDriver/AtomicHDriver.ino
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void setup() {

AtomS3.Display.setTextColor(GREEN);
AtomS3.Display.setTextDatum(middle_center);
AtomS3.Display.setTextFont(&fonts::Orbitron_Light_24);
AtomS3.Display.setFont(&fonts::Orbitron_Light_24);
AtomS3.Display.setTextSize(1);
AtomS3.Display.drawString("H-Driver", AtomS3.Display.width() / 2,
AtomS3.Display.height() / 2);
Expand Down
2 changes: 1 addition & 1 deletion examples/AtomicBase/AtomicPWM/AtomicPWM.ino
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void setup() {

AtomS3.Display.setTextColor(GREEN);
AtomS3.Display.setTextDatum(middle_center);
AtomS3.Display.setTextFont(&fonts::Orbitron_Light_24);
AtomS3.Display.setFont(&fonts::Orbitron_Light_24);
AtomS3.Display.setTextSize(1);
AtomS3.Display.drawString("PWM", AtomS3.Display.width() / 2,
AtomS3.Display.height() / 2);
Expand Down
2 changes: 1 addition & 1 deletion examples/AtomicBase/AtomicPoE/HTTP/HTTP.ino
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void setup() {

AtomS3.Display.setTextColor(GREEN);
AtomS3.Display.setTextDatum(middle_center);
AtomS3.Display.setTextFont(&fonts::Orbitron_Light_24);
AtomS3.Display.setFont(&fonts::Orbitron_Light_24);
AtomS3.Display.setTextSize(1);

SPI.begin(SCK, MISO, MOSI, -1);
Expand Down
2 changes: 1 addition & 1 deletion examples/AtomicBase/AtomicPoE/LinkStatus/LinkStatus.ino
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void setup() {

AtomS3.Display.setTextColor(GREEN);
AtomS3.Display.setTextDatum(middle_center);
AtomS3.Display.setTextFont(&fonts::Orbitron_Light_24);
AtomS3.Display.setFont(&fonts::Orbitron_Light_24);
AtomS3.Display.setTextSize(1);
AtomS3.Display.drawString("PoE", AtomS3.Display.width() / 2, 20);

Expand Down
2 changes: 1 addition & 1 deletion examples/AtomicBase/AtomicPoE/MQTT/MQTT.ino
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void setup() {

AtomS3.Display.setTextColor(GREEN);
AtomS3.Display.setTextDatum(middle_center);
AtomS3.Display.setTextFont(&fonts::Orbitron_Light_24);
AtomS3.Display.setFont(&fonts::Orbitron_Light_24);
AtomS3.Display.setTextSize(1);

SPI.begin(SCK, MISO, MOSI, -1);
Expand Down
2 changes: 1 addition & 1 deletion examples/AtomicBase/AtomicPoE/WebServer/WebServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void setup() {

AtomS3.Display.setTextColor(GREEN);
AtomS3.Display.setTextDatum(middle_center);
AtomS3.Display.setTextFont(&fonts::Orbitron_Light_24);
AtomS3.Display.setFont(&fonts::Orbitron_Light_24);
AtomS3.Display.setTextSize(1);

SPI.begin(SCK, MISO, MOSI, -1);
Expand Down
2 changes: 1 addition & 1 deletion examples/AtomicBase/AtomicTFCard/AtomicTFCard.ino
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ void setup() {

AtomS3.Display.setTextColor(GREEN);
AtomS3.Display.setTextDatum(middle_center);
AtomS3.Display.setTextFont(&fonts::FreeSerifItalic12pt7b);
AtomS3.Display.setFont(&fonts::FreeSerifItalic12pt7b);
AtomS3.Display.setTextSize(1);
AtomS3.Display.drawString("MicroSD", AtomS3.Display.width() / 2, 20);

Expand Down
2 changes: 1 addition & 1 deletion examples/Basics/button/button.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void setup() {

AtomS3.Display.setTextColor(GREEN);
AtomS3.Display.setTextDatum(middle_center);
AtomS3.Display.setTextFont(&fonts::Orbitron_Light_24);
AtomS3.Display.setFont(&fonts::Orbitron_Light_24);
AtomS3.Display.setTextSize(1);
AtomS3.Display.drawString("Click!", AtomS3.Display.width() / 2,
AtomS3.Display.height() / 2);
Expand Down
2 changes: 1 addition & 1 deletion examples/Basics/ir_nec/ir_nec.ino
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void setup() {
AtomS3.begin(cfg);
AtomS3.Display.setTextColor(GREEN);
AtomS3.Display.setTextDatum(middle_center);
AtomS3.Display.setTextFont(&fonts::FreeMono9pt7b);
AtomS3.Display.setFont(&fonts::FreeMono9pt7b);
AtomS3.Display.setTextSize(1);

IrSender.begin(DISABLE_LED_FEEDBACK); // Start with IR_SEND_PIN as send pin
Expand Down
95 changes: 95 additions & 0 deletions examples/Unit/MiniCAN/Master/Master.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* @file Slave.ino
* @author SeanKwok ([email protected])
* @brief Unit Mini CAN Test Master
* @version 0.1
* @date 2024-02-01
*
*
* @Hardwares: M5AtomS3 + Unit Mini CAN
* @Platform Version: Arduino M5Stack Board Manager v2.0.9
* @Dependent Library:
* M5GFX: https://github.com/m5stack/M5GFX
* M5Unified: https://github.com/m5stack/M5Unified
* M5AtomS3: https://github.com/m5stack/M5AtomS3
*/

#include <stdio.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "esp_err.h"
#include "esp_log.h"
#include "driver/twai.h"
#include "esp_err.h"

#include "M5AtomS3.h"

/* --------------------- Definitions and static variables ------------------ */

#define TX_TASK_PRIO 8 // Receiving task priority
#define TX_GPIO_NUM gpio_num_t(2)
#define RX_GPIO_NUM gpio_num_t(1)

static const twai_general_config_t g_config =
TWAI_GENERAL_CONFIG_DEFAULT(TX_GPIO_NUM, RX_GPIO_NUM, TWAI_MODE_NORMAL);
static const twai_timing_config_t t_config = TWAI_TIMING_CONFIG_25KBITS();
static const twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();

#define ID_SLAVE_1 0x0B1

static const twai_message_t slave_1_on = {.identifier = ID_SLAVE_1,
.data_length_code = 8,
.data = {1, 2, 3, 4, 5, 6, 7, 8}};

static const twai_message_t slave_1_off = {.identifier = ID_SLAVE_1,
.data_length_code = 8,
.data = {0, 0, 0, 0, 0, 0, 0, 0}};

static void twai_transmit_task(void *arg) {
while (1) {
twai_transmit(&slave_1_on, portMAX_DELAY);
Serial.printf("send cmd on to 0x%02X\r\n", ID_SLAVE_1);
Serial.print("send data: ");
for (int i = 0; i < slave_1_on.data_length_code; i++) {
Serial.printf("0x%02X ", slave_1_on.data[i]);
}
Serial.println();

vTaskDelay(pdMS_TO_TICKS(1000));

twai_transmit(&slave_1_off, portMAX_DELAY);
Serial.printf("send cmd off to 0x%02X\r\n", ID_SLAVE_1);
Serial.print("send data: ");
for (int i = 0; i < slave_1_off.data_length_code; i++) {
Serial.printf("0x%02X ", slave_1_off.data[i]);
}
Serial.println();
vTaskDelay(pdMS_TO_TICKS(1000));
}
vTaskDelete(NULL);
}

void setup() {
auto cfg = M5.config();
AtomS3.begin(cfg);

AtomS3.Display.setTextColor(GREEN);
AtomS3.Display.setTextDatum(middle_center);
AtomS3.Display.setFont(&fonts::FreeSansBold9pt7b);
AtomS3.Display.setTextSize(1);

AtomS3.Display.drawString("CAN Master", AtomS3.Display.width() / 2,
AtomS3.Display.height() / 2);

ESP_ERROR_CHECK(twai_driver_install(&g_config, &t_config, &f_config));
Serial.println("Driver installed");
ESP_ERROR_CHECK(twai_start());
xTaskCreatePinnedToCore(twai_transmit_task, "twai_transmit_task", 4096,
NULL, TX_TASK_PRIO, NULL, tskNO_AFFINITY);
}

void loop() {
}
77 changes: 77 additions & 0 deletions examples/Unit/MiniCAN/Slave/Slave.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* @file Slave.ino
* @author SeanKwok ([email protected])
* @brief Unit Mini CAN Test Slave
* @version 0.1
* @date 2024-02-01
*
*
* @Hardwares: M5AtomS3 + Unit Mini CAN
* @Platform Version: Arduino M5Stack Board Manager v2.0.9
* @Dependent Library:
* M5GFX: https://github.com/m5stack/M5GFX
* M5Unified: https://github.com/m5stack/M5Unified
* M5AtomS3: https://github.com/m5stack/M5AtomS3
*/

#include <stdio.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "esp_err.h"
#include "esp_log.h"
#include "driver/twai.h"
#include "esp_err.h"

#include "M5AtomS3.h"

/* --------------------- Definitions and static variables ------------------ */

#define RX_TASK_PRIO 8 // Receiving task priority
#define TX_GPIO_NUM gpio_num_t(2)
#define RX_GPIO_NUM gpio_num_t(1)

static const twai_general_config_t g_config =
TWAI_GENERAL_CONFIG_DEFAULT(TX_GPIO_NUM, RX_GPIO_NUM, TWAI_MODE_NORMAL);
static const twai_timing_config_t t_config = TWAI_TIMING_CONFIG_25KBITS();
static const twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();

static void twai_receive_task(void *arg) {
twai_message_t rx_msg;
while (1) {
twai_receive(&rx_msg, portMAX_DELAY);
Serial.printf("received identifier: 0x%02X\r\n", rx_msg.identifier);
// rx_msg.data
Serial.print("received data: ");
for (int i = 0; i < rx_msg.data_length_code; i++) {
Serial.printf("0x%02X ", rx_msg.data[i]);
}
Serial.println();
vTaskDelay(pdMS_TO_TICKS(100));
}
vTaskDelete(NULL);
}

void setup() {
auto cfg = M5.config();
AtomS3.begin(cfg);

AtomS3.Display.setTextColor(GREEN);
AtomS3.Display.setTextDatum(middle_center);
AtomS3.Display.setFont(&fonts::FreeSansBold9pt7b);
AtomS3.Display.setTextSize(1);

AtomS3.Display.drawString("CAN Slave", AtomS3.Display.width() / 2,
AtomS3.Display.height() / 2);

ESP_ERROR_CHECK(twai_driver_install(&g_config, &t_config, &f_config));
Serial.println("Driver installed");
ESP_ERROR_CHECK(twai_start());
xTaskCreatePinnedToCore(twai_receive_task, "twai_receive_task", 4096, NULL,
RX_TASK_PRIO, NULL, tskNO_AFFINITY);
}

void loop() {
}

0 comments on commit d98b0c5

Please sign in to comment.