Initial commit

This commit is contained in:
Martin Linkwitz - NUC
2026-04-02 14:25:39 +02:00
parent ea3b0f29ea
commit c7adbcd1c7
110 changed files with 189727 additions and 33726 deletions

View File

@@ -1,61 +1,45 @@
#include <iterator>
#include "esp32-hal-gpio.h"
#include "MeyCan.h";
#include <SPI.h>;
#include <mcp2515.h>;
#include "driver/twai.h"
PinState *MeyPin = NULL;
CanInterface *CanBusses = NULL;
uint16_t myDeviceId;
uint16_t myDeviceId = 0;
byte _deviceTypeId = 0;
byte _majorHardwareVersion = 0;
byte _minorHardwareVersion = 0;
void SetupMeyPin(PinState *state)
{
pinMode(state->pin_id, OUTPUT);
digitalWrite(state->pin_id, LOW);
state->pin_state = false;
}
void InitCanInterface(MCP2515 *interface, can_frame *frame) // can_frame is NULL. Reuse ForEach Method for smaller footprint
{
interface->reset();
interface->setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
interface->setNormalMode();
SendVersionPackage(interface);
}
void ForEachCanInterface(void (*handle)(MCP2515 *canInterace))
{
CanBusses->ForEach(handle);
}
void SetupMeyCan()
{
CalculateMyDeviceId();
if (MeyPin != NULL);
MeyPin->ForEach(SetupMeyPin);
if (CanBusses != NULL)
{
CanBusses->ForEach(InitCanInterface, NULL);
void SetupMeyPin(PinState *state) {
if (state->is_input) {
pinMode(state->pin_id, INPUT_PULLUP);
state->pin_state = digitalRead(state->pin_id);
} else {
pinMode(state->pin_id, OUTPUT);
digitalWrite(state->pin_id, LOW);
state->pin_state = true;
}
}
void SetCanInterface(byte index, byte pinId)
{
MCP2515* newCanInterface = new MCP2515(pinId);
CanInterface* canInterface = new CanInterface();
if (CanBusses == NULL)
CanBusses = canInterface;
else
CanBusses->AddCanInterface(canInterface);
void SetupMeyCan(byte majorHardwareVersion, byte minorHardwareVersion, byte deviceTypeId) {
_majorHardwareVersion = majorHardwareVersion;
_minorHardwareVersion = minorHardwareVersion;
_deviceTypeId = deviceTypeId;
if (MeyPin != NULL)
MeyPin->ForEach(SetupMeyPin);
}
void SetMeyPin(byte index, byte meyPinId, byte pinId)
{
PinState* newState = new PinState();
newState->Init(pinId, (byte) meyPinId);
void SetDevicedId(byte high, byte low) {
myDeviceId = (high << 8) | low;
}
void SetMeyPin(byte meyPinId, byte pinId) {
PinState *newState = new PinState();
newState->Init(pinId, (byte)meyPinId);
if (MeyPin == NULL)
MeyPin = newState;
@@ -64,66 +48,69 @@ void SetMeyPin(byte index, byte meyPinId, byte pinId)
}
bool CheckPinStatus(PinState * state)
{
bool ReadPin(PinState *state) {
return digitalRead(state->pin_id);
}
void SendSwitchedTriggeredCanPackage(byte pinId, int state) {
twai_message_t message;
message.extd = 1;
message.rtr = 0;
message.ss = 0;
message.self = 0;
message.dlc_non_comp = 0;
message.identifier = CreateCanId(SWITCH_TRIGGERED_CAN_ID);
message.data_length_code = 2;
message.data[0] = pinId;
message.data[1] = state;
DoSendCanPkg(&message);
}
void CheckPinStatus(PinState *state) {
if (!state->is_input)
return false;
return;
bool newValue = ReadPin(state);
if (newValue != state->pin_state)
{
if (newValue != state->pin_state) {
delay(10);
newValue = ReadPin(state);
if (newValue != state->pin_state)
{
if (newValue != state->pin_state) {
state->pin_state = newValue;
SendSwitchedTriggeredCanPackage(state->meyPinId, state->pin_state);
}
}
return false;
return;
}
void CheckMeyPinsTriggered()
{
void CheckMeyPinsTriggered() {
MeyPin->ForEach(CheckPinStatus);
}
bool ReadPin(PinState * state)
{
if (state->pin_id == PIN_PD2)
return digitalReadFast(PIN_PD2);
else if (state->pin_id == PIN_PC7)
return digitalReadFast(PIN_PC7);
else if (state->pin_id == PIN_PD1)
return digitalReadFast(PIN_PD1);
else if (state->pin_id == PIN_PD0)
return digitalReadFast(PIN_PD0);
else if (state->pin_id == PIN_PD6)
return digitalReadFast(PIN_PD6);
else if (state->pin_id == PIN_PD5)
return digitalReadFast(PIN_PD5);
else if (state->pin_id == PIN_PD4)
return digitalReadFast(PIN_PD4);
else if (state->pin_id == PIN_PD3)
return digitalReadFast(PIN_PD3);
else
return digitalRead(state->pin_id);
uint16_t GetDeviceId(uint32_t canFrameId) {
return canFrameId & 0xFFFF;
}
void HandleFrame(can_frame *frame, MCP2515 *source)
{
CanBusses->ForEach(DoSendCanPkg, frame, source);
HandleTriggerMeypinCanPackage(frame);
uint16_t GetPackageType(uint32_t canFrameId) {
return (canFrameId / 0x10000) & 0xFFF;
}
void HandleTriggerMeypinCanPackage(can_frame *frame)
{
if (GetPackageType(frame->can_id) == TRIGGER_SWITCH_CAN_ID)
{
uint16_t adressedDeviceId = ((uint16_t )frame->data[0] << 8) | frame->data[1] ;
uint32_t CreateCanId(uint16_t commandId) {
return ((((uint32_t)commandId) & 0xFFF) * 0x10000) | myDeviceId;
}
void HandleTriggerMeypinCanPackage(twai_message_t *frame) {
if (GetPackageType(frame->identifier) == TRIGGER_SWITCH_CAN_ID) {
uint16_t adressedDeviceId = ((uint16_t)frame->data[0] << 8) | frame->data[1];
if (adressedDeviceId != myDeviceId) return;
@@ -132,10 +119,8 @@ void HandleTriggerMeypinCanPackage(can_frame *frame)
PinState *adressedPin = MeyPin->Find(meyPinId);
if (adressedPin != NULL)
{
if (adressedPin->is_input == true)
{
if (adressedPin != NULL) {
if (adressedPin->is_input == true) {
pinMode(adressedPin->pin_id, OUTPUT);
adressedPin->is_input = false;
}
@@ -147,104 +132,17 @@ void HandleTriggerMeypinCanPackage(can_frame *frame)
digitalWrite(adressedPin->pin_id, state);
SendSwitchedTriggeredCanPackage(adressedPin->meyPinId, state);
}
}
}
}
byte CircularShift(byte b)
{
return (b << 1) | (b >> 7 & 1);
void HandleFrame(twai_message_t *frame) {
HandleTriggerMeypinCanPackage(frame);
}
uint16_t GetDeviceId(uint32_t canFrameId)
{
return canFrameId & 0xFFFF;
}
uint16_t GetPackageType(uint32_t canFrameId)
{
return (canFrameId / 0x10000) & 0xFFF;
}
byte GetDeviceIdLow() {
return (SIGROW.SERNUM0 ^
CircularShift(SIGROW.SERNUM2) << 1 ^
CircularShift( CircularShift(SIGROW.SERNUM4)) ^
CircularShift( CircularShift( CircularShift(SIGROW.SERNUM6))) ^
CircularShift( CircularShift( CircularShift( CircularShift(SIGROW.SERNUM8)))));
}
byte GetDeviceIdHigh() {
return (SIGROW.SERNUM1 ^
CircularShift(SIGROW.SERNUM3) << 1 ^
CircularShift( CircularShift(SIGROW.SERNUM5)) ^
CircularShift( CircularShift( CircularShift(SIGROW.SERNUM7))) ^
CircularShift( CircularShift( CircularShift( CircularShift(SIGROW.SERNUM9)))));
}
uint32_t CreateCanId(uint16_t commandId)
{
return ((((uint32_t)commandId) & 0xFFF) * 0x10000) | myDeviceId | CAN_EFF_FLAG;
}
void SendVersionPackage(MCP2515 *interface)
{
can_frame toSend;
toSend.can_id = CreateCanId(HELP_PACKAGE_CAN_ID);
toSend.can_dlc = 6;
toSend.data[0] = SOFTWARE_VERSION_HIGH;
toSend.data[1] = SOFTWARE_VERSION_LOW;
toSend.data[2] = HARDWARE_VERSION_HIGH;
toSend.data[3] = HARDWARE_VERSION_LOW;
toSend.data[4] = (myDeviceId >> 8) & 0xFF;
toSend.data[5] = myDeviceId & 0xFF;
DoSendCanPkg(interface, &toSend);
}
void BroadcastTriggerMeyPinCanPackage(uint16_t targetCanId, byte pinId, byte state)
{
can_frame toSend;
toSend.can_id = CreateCanId(TRIGGER_SWITCH_CAN_ID);
toSend.can_dlc = 4;
toSend.data[0] = (targetCanId & 0xFF00) >> 8;
toSend.data[1] = targetCanId & 0xFF;
toSend.data[2] = pinId;
toSend.data[3] = state;
HandleFrame(&toSend, NULL);
}
void SendSwitchedTriggeredCanPackage(byte pinId, int state)
{
can_frame toSend;
toSend.can_id = CreateCanId(SWITCH_TRIGGERED_CAN_ID);
toSend.can_dlc = 2;
toSend.data[0] = pinId;
toSend.data[1] = state;
DoSendCanPkg(&toSend);
}
void DoSendCanPkg(can_frame *frame)
{
if (CanBusses != NULL)
CanBusses->ForEach(DoSendCanPkg, frame);
}
void DoSendCanPkg(MCP2515 *interface, can_frame *frame)
{
byte cnt = 0;
while (interface->sendMessage(frame))
{
if (++cnt > 10) return;
}
}
void CalculateMyDeviceId()
{
myDeviceId = (GetDeviceIdHigh() << 8) | GetDeviceIdLow();
void DoSendCanPkg(twai_message_t *frame) {
esp_err_t ret = twai_transmit(frame, pdMS_TO_TICKS(1000));
}

View File

@@ -1,128 +1,80 @@
#ifndef MEYCAN_H
#define MEYCAN_H
#include <SPI.h>
#include <mcp2515.h>
typedef struct CanInterface
{
MCP2515 *interface;
CanInterface *next = NULL;
void ForEach(void (*handle)( MCP2515 *interface))
{
if (this->interface != NULL)
handle(this->interface);
if (this->next != NULL)
this->next->ForEach(handle);
}
void ForEach(void (*handle)( MCP2515 *interface, can_frame *frame), can_frame *frame, MCP2515* exclude = NULL)
{
if (this->interface != NULL && this->interface != exclude)
handle(this->interface, frame);
if (this->next != NULL)
this->next->ForEach(handle, frame);
}
void AddCanInterface(CanInterface *newCanInterface)
{
if (next == NULL)
{
this->next = newCanInterface;
newCanInterface->next = NULL;
} else {
next->AddCanInterface(newCanInterface);
}
}
};
typedef struct PinState
{
int pin_id;
bool pin_state;
bool is_input;
byte meyPinId;
PinState *next = NULL;
PinState() {}
void Init(int pin_id, byte meyPinId)
{
this->pin_id = pin_id;
this->pin_state = true;
this->is_input = true;
this->meyPinId = meyPinId;
}
void AddPinState(PinState *nextPinState)
{
if (next == NULL)
{
this->next = nextPinState;
nextPinState->next = NULL;
} else {
next->AddPinState(nextPinState);
}
}
PinState* Find(byte meyPinId)
{
if (this->meyPinId == meyPinId)
return this;
if (this->next != NULL)
return this->next->Find(meyPinId);
return NULL;
}
void ForEach(void (*handle)(PinState *theState))
{
handle(this);
if (this->next != NULL)
this->next->ForEach(handle);
}
};
const byte SOFTWARE_VERSION_HIGH = 5;
const byte SOFTWARE_VERSION_LOW = 0;
const byte HARDWARE_VERSION_HIGH = 7;
const byte HARDWARE_VERSION_LOW = 0;
const uint16_t HELP_PACKAGE_CAN_ID = 0x0FFFUL;
const uint16_t SWITCH_TRIGGERED_CAN_ID = 0x0050;
const uint16_t TRIGGER_SWITCH_CAN_ID = 0x0055;
void SetCanInterface(byte index, byte pinId);
void SetMeyPin(byte index, byte meyPinId, byte pinId);
void SetupMeyCan();
void ForEachCanInterface(void (*handle)(MCP2515 *canInterace));
bool ReadPin(PinState *state);
bool CheckPinStatus(PinState * state);
void CheckMeyPinsTriggered(); /* checks weather a meypin triggered and sends a can pkg is neccessary */
void HandleFrame(can_frame *frame, MCP2515 *source);
void HandleTriggerMeypinCanPackage(can_frame *frame);
byte CircularShift(byte b);
uint16_t GetDeviceId(uint32_t canFrameId);
uint16_t GetPackageType(uint32_t canFrameId);
byte GetDeviceIdLow();
byte GetDeviceIdHigh();
uint32_t CreateCanId(uint16_t commandId);
void SendVersionPackage(MCP2515 *interface);
void SendSwitchedTriggeredCanPackage(byte pinId, int state);
void BroadcastTriggerMeyPinCanPackage(uint16_t targetCanId, byte pinId, byte state);
void DoSendCanPkg(can_frame *frame);
void DoSendCanPkg(MCP2515 *interface, can_frame *frame);
void CalculateMyDeviceId();
#endif
#ifndef MEYCAN_H
#define MEYCAN_H
#include <Arduino.h>
#include <SPI.h>
#include "driver/twai.h"
typedef struct PinState
{
int pin_id;
bool pin_state;
bool is_input;
byte meyPinId;
PinState *next = NULL;
PinState() {}
void Init(int pin_id, byte meyPinId)
{
this->pin_id = pin_id;
this->pin_state = true;
this->is_input = true;
this->meyPinId = meyPinId;
}
void AddPinState(PinState *nextPinState)
{
if (next == NULL)
{
this->next = nextPinState;
nextPinState->next = NULL;
} else {
next->AddPinState(nextPinState);
}
}
PinState* Find(byte meyPinId)
{
if (this->meyPinId == meyPinId)
return this;
if (this->next != NULL)
return this->next->Find(meyPinId);
return NULL;
}
void ForEach(void (*handle)(PinState *theState))
{
handle(this);
if (this->next != NULL)
this->next->ForEach(handle);
}
};
const byte SOFTWARE_VERSION_HIGH = 6;
const byte SOFTWARE_VERSION_LOW = 0;
const byte HARDWARE_VERSION_HIGH = 8;
const byte HARDWARE_VERSION_LOW = 1;
const uint16_t HELP_PACKAGE_CAN_ID = 0x0FFFUL;
const uint16_t SWITCH_TRIGGERED_CAN_ID = 0x0050;
const uint16_t TRIGGER_SWITCH_CAN_ID = 0x0055;
void SetupMeyCan(byte majorHardwareVersion, byte minorHardwareVersion, byte deviceTypeId);
uint32_t CreateCanId(uint16_t commandId);
void SetDevicedId(byte high, byte low);
void SetMeyPin(byte meyPinId, byte pinId);
uint16_t GetDeviceId(uint32_t canFrameId);
uint16_t GetPackageType(uint32_t canFrameId);
void SendSwitchedTriggeredCanPackage(byte pinId, int state);
void HandleFrame(twai_message_t *frame);
void CheckPinStatus(PinState *state);
void CheckMeyPinsTriggered(); /* checks weather a meypin triggered and sends a can pkg is neccessary */
void DoSendCanPkg(twai_message_t *frame) ;
#endif

View File

@@ -1,32 +1,82 @@
#include <SPI.h>;
#include <mcp2515.h>;
#include "MeyCan.h";
void setup() {
return;
SPI.begin();
_PROTECTED_WRITE(CLKCTRL.MCLKCTRLA, CLKCTRL.MCLKCTRLA | 1 << 7);
delay(10); // a bit delay for mcp2515 to get the clock
SetMeyPin(0, 1, PIN_PD0);
SetMeyPin(1, 2, PIN_PC7);
SetMeyPin(2, 3, PIN_PD2);
SetMeyPin(3, 4, PIN_PD1);
SetMeyPin(4, 5, PIN_PD5);
SetMeyPin(5, 6, PIN_PD3);
SetMeyPin(6, 7, PIN_PD7);
SetMeyPin(7, 8, PIN_PD6);
delay(20);
SetCanInterface(0, PIN_PD4);
SetupMeyCan();
}
void loop()
{
CheckMeyPinsTriggered();
}
#include <Arduino.h>
#include "driver/twai.h"
#include "MeyCan.h"
// https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/TWAI/TWAIreceive/TWAIreceive.ino
#define RX_PIN 2
#define TX_PIN 3
void DebugBlink(int d) {
pinMode(20, OUTPUT);
while (true) {
digitalWrite(20, HIGH);
delay(d);
digitalWrite(20, LOW);
delay(d);
}
}
bool driver_installed = false;
void setup() {
Serial.begin(9600);
SPI.begin();
// Explicit GND for LED and Input
pinMode(21, OUTPUT);
digitalWrite(21, LOW);
// Initialize configuration structures using macro initializers
twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t)TX_PIN, (gpio_num_t)RX_PIN, TWAI_MODE_NORMAL);
twai_timing_config_t t_config = TWAI_TIMING_CONFIG_1MBITS(); //Look in the api-reference for other speed sets.
twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) {
Serial.println("Driver installed");
} else {
DebugBlink(100);
return;
}
esp_err_t e = twai_start();
// Start TWAI driver
if (e == ESP_OK) {
driver_installed = true;
} else {
DebugBlink(500);
return;
}
SetDevicedId(0x05, 0x1F);
SetMeyPin(1, 5);
SetMeyPin(2, 6);
SetMeyPin(3, 7);
SetMeyPin(4, 8);
SetMeyPin(5, 9);
SetMeyPin(6, 10);
SetMeyPin(7, 20);
SetupMeyCan(8, 1, 3);
}
twai_message_t frame;
void loop() {
if (!driver_installed) {
// Driver not installed
DebugBlink(2000);
return;
}
CheckMeyPinsTriggered();
if (twai_receive(&frame, 0) == ESP_OK) {
HandleFrame(&frame);
}
}