This commit is contained in:
Meydin87
2022-09-04 07:23:59 +02:00
parent 204d7580c3
commit 3e44b2acac

View File

@@ -1,15 +1,18 @@
#include <SPI.h>;
#include <mcp2515.h>;
struct can_frame _frame;
MCP2515 mcp2515(PIN_PA3);
const byte SoftwareVersionHigh = 1;
const byte SoftwareVersionLow = 0;
const byte HardwareVersionHigh = 2;
const byte HardwareVersionLow = 0;
typedef struct
{
int pin_id;
bool pin_state;
short pin_wait;
bool pin_send_wait;
bool is_input;
byte meyPinId;
@@ -18,7 +21,6 @@ typedef struct
{
this->pin_id = pin_id;
this->pin_state = true;
this->pin_wait = 0;
this->is_input = true;
this->meyPinId = meyPinId;
}
@@ -26,6 +28,7 @@ typedef struct
int PinCount = 8;
PinState PinPD2[8];
byte DeviceId[2];
@@ -34,16 +37,16 @@ void setup() {
PinPD2[0] = PinState();
PinPD2[0].Init(PIN_PD2, (byte) 4);
PinPD2[0].Init(PIN_PC7, (byte) 1);
PinPD2[1] = PinState();
PinPD2[1].Init(PIN_PC7, (byte) 1);
PinPD2[1].Init(PIN_PD0, (byte) 2);
PinPD2[2] = PinState();
PinPD2[2].Init(PIN_PD1, (byte) 3);
PinPD2[3] = PinState();
PinPD2[3].Init(PIN_PD0, (byte) 2);
PinPD2[3].Init(PIN_PD2, (byte) 4);
PinPD2[4] = PinState();
PinPD2[4].Init(PIN_PD6, (byte) 5);
@@ -57,6 +60,8 @@ void setup() {
PinPD2[7] = PinState();
PinPD2[7].Init(PIN_PD3, (byte) 8);
_PROTECTED_WRITE(CLKCTRL.MCLKCTRLA, CLKCTRL.MCLKCTRLA | 1 << 7);
mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
@@ -65,28 +70,113 @@ void setup() {
for (int i = 0; i <= PinCount - 1; i++)
{
pinMode(PinPD2[i].pin_id, INPUT_PULLUP);
}
PinPD2[i].pin_state = ReadPin(&PinPD2[i]);
}
DeviceId[0] = (SIGROW.SERNUM0 ^
CircularShift(SIGROW.SERNUM2) << 1 ^
CircularShift( CircularShift(SIGROW.SERNUM4)) ^
CircularShift( CircularShift( CircularShift(SIGROW.SERNUM6))) ^
CircularShift( CircularShift( CircularShift( CircularShift(SIGROW.SERNUM8)))));
DeviceId[1] = (SIGROW.SERNUM1 ^
CircularShift(SIGROW.SERNUM3) << 1 ^
CircularShift( CircularShift(SIGROW.SERNUM5)) ^
CircularShift( CircularShift( CircularShift(SIGROW.SERNUM7))) ^
CircularShift( CircularShift( CircularShift( CircularShift(SIGROW.SERNUM9)))));
SendSerialPackage();
}
byte CircularShift(byte b)
{
return (b << 1) | (b >> 7 & 1);
}
uint32_t GetDeviceId(uint32_t canFrameId)
{
return canFrameId & 0xFFFF;
}
uint32_t CreateCanId(uint32_t commandId)
{
return ((commandId & 0xFFF) * 0x10000) | ( DeviceId[0] << 8) | (DeviceId[1]) | CAN_EFF_FLAG;
}
bool debugFlag = false;
void loop()
{
for (int i = 0; i <= PinCount - 1; i++)
{
if (CheckPinStatus(&PinPD2[i]))
SendCanPackage(&PinPD2[i]);
}
SendSwitchedTriggeredCanPackage(&PinPD2[i]);
}
void SendCanPackage(PinState *state)
if (mcp2515.readMessage(&_frame) == MCP2515::ERROR_OK)
{
_frame.can_id = 0x001;
debugFlag ^= true;
int meyPinId = _frame.data[0];
bool state = _frame.data[1] > 0;
PinState *adressedPin;
for (int i = 0; i <= PinCount - 1; i++)
if (PinPD2[i].meyPinId == meyPinId)
{
adressedPin = &PinPD2[i];
break;
}
if (adressedPin != NULL)
{
if (adressedPin->is_input == true)
{
pinMode(adressedPin->pin_id, OUTPUT);
adressedPin->is_input = false;
}
adressedPin->pin_state = state;
digitalWrite(adressedPin->pin_id, state);
}
}
delay(20);
}
void SendSerialPackage()
{
_frame.can_id = CreateCanId(0xFFFF);
_frame.can_dlc = 4;
_frame.data[0] = SoftwareVersionHigh;
_frame.data[1] = SoftwareVersionLow;
_frame.data[2] = HardwareVersionHigh;
_frame.data[3] = HardwareVersionLow;
mcp2515.sendMessage(MCP2515::TXB1, &_frame);
}
void SendSwitchedTriggeredCanPackage(PinState * state)
{
_frame.can_id = CreateCanId(0x050);
_frame.can_dlc = 2;
_frame.data[0] = state->meyPinId;
_frame.data[1] = state->pin_state;
_frame.data[2] = 0x00;
_frame.data[3] = 0x00;
mcp2515.sendMessage(&_frame);
mcp2515.sendMessage(MCP2515::TXB1, &_frame);
}
void SendDoTriggerSwitchCanPackage(uint32_t targetCanId, byte pinId, byte state)
{
_frame.can_id = CreateCanId(0x050);
_frame.can_dlc = 4;
_frame.data[0] = targetCanId & 0xFF;
_frame.data[1] = (targetCanId & 0xFF00) >> 8;
_frame.data[2] = pinId;
_frame.data[3] = state;
mcp2515.sendMessage(MCP2515::TXB1, &_frame);
}
bool ReadPin(PinState * state)
@@ -109,7 +199,6 @@ bool ReadPin(PinState *state)
return digitalReadFast(PIN_PD3);
else
return digitalRead(state->pin_id);
}
bool CheckPinStatus(PinState * state)
@@ -127,11 +216,10 @@ bool CheckPinStatus(PinState *state)
if (newValue != state->pin_state)
{
state->pin_state = newValue;
state->pin_wait = 2048; // block this pin for 16 loops (debouncer)
state->pin_send_wait = true;
return true;
}
}
return false;
}