Added defines for PCB_revisions

This commit is contained in:
Marcel Peterkau 2022-01-31 09:26:10 +01:00
parent 152a324c9d
commit b029243760
7 changed files with 205 additions and 29 deletions

View File

@ -32,6 +32,7 @@ build_flags =
-DADMIN_PASSWORD=${wifi_cred.admin_password}
-DWIFI_AP_PASSWORD=${wifi_cred.wifi_ap_password}
-DWIFI_AP_IP_GW=10,0,0,1
-DPCB_REVISION=13
board_build.filesystem = littlefs
@ -45,4 +46,6 @@ lib_deps =
joaolopesf/RemoteDebug @ ^2.1.2
fastled/FastLED @ ^3.5.0
sstaub/Ticker @ ^4.2.0
s00500/ESPUI @ ^2.0.0
s00500/ESPUI @ ^2.0.0
coryjfowler/mcp_can @ ^1.5.0
robtillaart/I2C_EEPROM @ ^1.5.2

View File

@ -4,17 +4,18 @@
#define Q(x) #x
#define QUOTE(x) Q(x)
#define GPIO_BUTTON D5
#define GPIO_LED D6
#define GPIO_TRIGGER D4
#define GPIO_PUMP D3
#define GPIO_BUTTON D4
#define GPIO_LED D3
#define GPIO_TRIGGER D6
#define GPIO_PUMP D0
#define GPIO_CS_CAN D8
#ifndef HOST_NAME
#define HOST_NAME "ChainLube_%06X" // Use printf-Formatting - Chip-ID (uin32_t) will be added
#define HOST_NAME "ChainLube_%06X" // Use printf-Formatting - Chip-ID (uin32_t) will be added
#endif
#ifndef OTA_DELAY
#define OTA_DELAY 50 // ticks -> 10ms / tick
#define OTA_DELAY 50 // ticks -> 10ms / tick
#endif
#ifndef ADMIN_PASSWORD
@ -23,11 +24,23 @@
#ifndef WIFI_PASSWORD
#error "You must define an WIFI_PASSWORD for OTA-Update"
#endif
#ifndef WIFI_SSID
#ifndef WIFI_SSID
#error "You must define an WIFI_SSID for OTA-Update"
#endif
#ifndef WIFI_AP_PASSWORD
#error "You must define an WIFI_AP_PASSWORD for Standalone AP-Mode"
#endif
#ifndef PCB_REVISION
#error "You must define PCB_REVISION"
#else
#if PCB_REVISION == 13
#elif PCB_REVISION == 12
#elif PCB_REVISION == 10
#else
#error "Unknown PCB_REVISION defined"
#endif
#endif
#endif

View File

@ -1,29 +1,64 @@
#include "config.h"
#if PCB_REVISION >= 12
I2C_eeprom ee(0x50, EEPROM_SIZE_BYTES);
#endif
LubeConfig_t LubeConfig;
persistenceData_t PersistenceData;
uint16_t eePersistenceMarker = 0;
uint16_t eeVersion = 0; // inc
boolean eeAvailable = false;
const uint16_t startofLubeConfig = sizeof(eePersistenceMarker);
const uint16_t startofPersistence = sizeof(LubeConfig);
#if PCB_REVISION >= 12
void InitEEPROM()
{
ee.begin();
if (ee.isConnected())
{
eeAvailable = true;
Serial.println("EEPROM Initialized...");
}
else
{
Serial.println("ERROR: Can't find eeprom...");
}
}
#endif
void StoreConfig_EEPROM()
{
LubeConfig.checksum = 0;
LubeConfig.checksum = Checksum_EEPROM((uint8_t *)&LubeConfig, sizeof(LubeConfig));
#if PCB_REVISION >= 12
if (eeAvailable)
{
ee.updateBlock(startofLubeConfig, (uint8_t *)&LubeConfig, sizeof(LubeConfig));
}
#else
EEPROM.begin(512);
EEPROM.put(startofLubeConfig, LubeConfig);
EEPROM.commit();
EEPROM.end();
#endif
}
void GetConfig_EEPROM()
{
#if PCB_REVISION >= 12
if (eeAvailable)
{
ee.readBlock(startofLubeConfig, (uint8_t *)&LubeConfig, sizeof(LubeConfig));
}
#else
EEPROM.begin(512);
EEPROM.get(startofLubeConfig, LubeConfig);
EEPROM.end();
#endif
uint32_t checksum = LubeConfig.checksum;
LubeConfig.checksum = 0;
@ -37,37 +72,52 @@ void GetConfig_EEPROM()
void StorePersistence_EEPROM()
{
EEPROM.begin(512);
if (PersistenceData.writeCycleCounter == 0xFFFF)
{
PersistenceData.writeCycleCounter = 0;
eePersistenceMarker += sizeof(PersistenceData);
EEPROM.put(0, eePersistenceMarker);
}
MovePersistencePage_EEPROM(false);
else
{
PersistenceData.writeCycleCounter++;
}
uint16_t PersistenceDataAddress = startofPersistence + eePersistenceMarker;
PersistenceData.checksum = 0;
PersistenceData.checksum = Checksum_EEPROM((uint8_t *)&PersistenceData, sizeof(PersistenceData));
#if PCB_REVISION >= 12
if (eeAvailable)
{
ee.updateBlock(PersistenceDataAddress, (uint8_t *)&PersistenceData, sizeof(PersistenceData));
}
#else
EEPROM.put(PersistenceDataAddress, PersistenceData);
EEPROM.commit();
EEPROM.end();
#endif
}
void GetPersistence_EEPROM()
{
#if PCB_REVISION >= 12
if (eeAvailable)
{
eePersistenceMarker = (ee.readByte(0) << 8) | ee.readByte(1);
Serial.printf("get EE eePersistenceMarker: 0x%04X\n", eePersistenceMarker);
}
#else
EEPROM.begin(512);
EEPROM.get(0, eePersistenceMarker);
uint16_t PersistenceDataAddress = startofPersistence + eePersistenceMarker;
#endif
uint16_t PersistenceDataAddress = startofPersistence + eePersistenceMarker;
Serial.printf("get EE PersistenceDataAddress: 0x%04X\n", PersistenceDataAddress);
#if PCB_REVISION >= 12
if (eeAvailable)
{
ee.readBlock(PersistenceDataAddress, (uint8_t *)&PersistenceData, sizeof(PersistenceData));
}
#else
EEPROM.get(PersistenceDataAddress, PersistenceData);
EEPROM.end();
#endif
uint32_t checksum = PersistenceData.checksum;
PersistenceData.checksum = 0;
@ -99,6 +149,24 @@ void FormatPersistence_EEPROM()
StorePersistence_EEPROM();
}
void MovePersistencePage_EEPROM(boolean reset)
{
eePersistenceMarker = reset ? sizeof(PersistenceData) : eePersistenceMarker + sizeof(PersistenceData);
PersistenceData.writeCycleCounter = 0;
#if PCB_REVISION >= 12
ee.updateByte(0, (uint8_t)(eePersistenceMarker >> 8));
ee.updateByte(1, (uint8_t)(eePersistenceMarker & 0xFF));
#else
EEPROM.begin(512);
EEPROM.put(0, eePersistenceMarker);
#endif
Serial.printf("Moving PDS-Page\n");
Serial.printf("PersistenceData.writeCycleCounter: 0x%04X\n", PersistenceData.writeCycleCounter);
Serial.printf("eePersistenceMarker: 0x%04X\n", eePersistenceMarker);
Serial.printf("sizeof(PersistenceData): 0x%04X\n", sizeof(PersistenceData));
}
uint32_t Checksum_EEPROM(uint8_t const *data, size_t len)
{
if (data == NULL)
@ -111,4 +179,24 @@ uint32_t Checksum_EEPROM(uint8_t const *data, size_t len)
crc = crc & 1 ? (crc >> 1) ^ 0xb2 : crc >> 1;
}
return crc ^ 0xff;
}
void dumpEEPROM(uint16_t memoryAddress, uint16_t length)
{
const int BLOCK_TO_LENGTH = 16;
Serial.print("\nAddr. ");
for (int x = 0; x < BLOCK_TO_LENGTH; x++)
Serial.printf("%3d", x);
memoryAddress = memoryAddress / BLOCK_TO_LENGTH * BLOCK_TO_LENGTH;
length = (length + BLOCK_TO_LENGTH - 1) / BLOCK_TO_LENGTH * BLOCK_TO_LENGTH;
for (unsigned int i = 0; i < length; i++)
{
if (memoryAddress % BLOCK_TO_LENGTH == 0)
Serial.printf("\n0x%05X:", memoryAddress);
Serial.printf(" %02X", ee.readByte(memoryAddress));
memoryAddress++;
}
Serial.println();
}

View File

@ -2,14 +2,23 @@
#define _CONFIG_H_
#include <Arduino.h>
#if PCB_REVISION >= 12
#include <Wire.h>
#include <I2C_eeprom.h>
#else
#include <EEPROM.h>
#endif
#define EEPROM_SIZE_BYTES I2C_DEVICESIZE_24LC256
typedef enum SpeedSource_e
{
SOURCE_TIME,
SOURCE_IMPULSE,
SOURCE_GPS,
#if PCB_REVISION == 13
SOURCE_CAN
#endif
} SpeedSource_t;
typedef enum GPSBaudRate_e
@ -24,6 +33,8 @@ const char GPSBaudRateString[][7] = {
const size_t GPSBaudRateString_Elements = sizeof(GPSBaudRateString) / sizeof(GPSBaudRateString[0]);
#if PCB_REVISION == 13
typedef enum CANSource_e
{
KTM_890_ADV_R_2021
@ -33,12 +44,16 @@ const char CANSourceString[][28] = {
"KTM 890 Adventure R (2021)"};
const char CANSourceString_Elements = sizeof(CANSourceString) / sizeof(CANSourceString[0]);
#endif
const char SpeedSourceString[][8] = {
"Timer",
"Impuls",
"GPS",
"CAN-Bus"};
#if PCB_REVISION == 13
"CAN-Bus"
#endif
};
const size_t SpeedSourceString_Elements = sizeof(SpeedSourceString) / sizeof(SpeedSourceString[0]);
@ -65,10 +80,15 @@ typedef struct
uint8_t BleedingPulses = 25;
SpeedSource_t SpeedSource = SOURCE_IMPULSE;
GPSBaudRate_t GPSBaudRate = BAUD_9600;
#if PCB_REVISION == 13
CANSource_t CANSource = KTM_890_ADV_R_2021;
#endif
uint32_t checksum = 0;
} LubeConfig_t;
#if PCB_REVISION >= 12
void InitEEPROM();
#endif
void StoreConfig_EEPROM();
void GetConfig_EEPROM();
void StorePersistence_EEPROM();
@ -76,6 +96,8 @@ void GetPersistence_EEPROM();
void FormatConfig_EEPROM();
void FormatPersistence_EEPROM();
uint32_t Checksum_EEPROM(uint8_t const *data, size_t len);
void dumpEEPROM(uint16_t memoryAddress, uint16_t length);
void MovePersistencePage_EEPROM(boolean reset);
extern LubeConfig_t LubeConfig;
extern persistenceData_t PersistenceData;

View File

@ -7,6 +7,10 @@
#include <RemoteDebug.h>
#include <FastLED.h>
#include <Ticker.h>
#if PCB_REVISION >= 13
#include <mcp_can.h>
#include <SPI.h>
#endif
#include "common.h"
#include "rmtdbghelp.h"
@ -23,7 +27,10 @@ const char *password = QUOTE(WIFI_PASSWORD);
const uint32_t connectTimeoutMs = 5000;
ESP8266WiFiMulti wifiMulti;
#endif
#if PCB_REVISION >= 13
MCP_CAN CAN0(GPIO_CS_CAN);
#endif
#ifdef DEBUG
@ -58,6 +65,9 @@ void Display_Process();
void Button_Process();
void toggleWiFiAP(boolean shutdown = false);
void SystemShutdown();
#if PCB_REVISION >= 13
void Init_CAN();
#endif
#ifdef WIFI_CLIENT
void wifiMaintainConnectionTicker_callback();
@ -68,9 +78,7 @@ Ticker UpdateWebUITicker(updateWebUITicker_callback, 5000, 0, MILLIS);
void setup()
{
system_update_cpu_freq(SYS_CPU_80MHZ);
snprintf(DeviceName, 32, HOST_NAME, ESP.getChipId());
WiFi.persistent(false);
#ifdef WIFI_CLIENT
@ -88,15 +96,24 @@ void setup()
Serial.println("Souko's ChainLube Mk1");
Serial.println(DeviceName);
#if PCB_REVISION >= 12
InitEEPROM();
#endif
GetConfig_EEPROM();
GetPersistence_EEPROM();
#if PCB_REVISION >= 13
Init_CAN();
#endif
u8x8.begin();
u8x8.setFont(u8x8_font_chroma48medium8_r);
FastLED.addLeds<WS2811, GPIO_LED, GRB>(leds, 1); // GRB ordering is assumed
#if PCB_REVISION <= 13
pinMode(GPIO_TRIGGER, INPUT_PULLUP);
#endif
pinMode(GPIO_BUTTON, INPUT_PULLUP);
pinMode(GPIO_PUMP, OUTPUT);
@ -199,6 +216,12 @@ void processCmdRemoteDebug()
RemoteDebug_formatPersistence();
else if (lastCmd == "checkEE")
RemoteDebug_CheckEEPOM();
else if (lastCmd == "dumpEE1k")
dumpEEPROM(0, 1024);
else if (lastCmd == "dumpEE")
dumpEEPROM(0, EEPROM_SIZE_BYTES);
else if (lastCmd == "resetPageEE")
MovePersistencePage_EEPROM(true);
}
void RemoteDebug_formatCFG()
@ -589,4 +612,26 @@ void SystemShutdown()
{
StoreConfig_EEPROM();
ESP.restart();
}
}
#if PCB_REVISION >= 13
void Init_CAN()
{
// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
if (CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK)
Serial.println("MCP2515 Initialized Successfully!");
else
Serial.println("Error Initializing MCP2515...");
CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
}
void Recieve_CAN()
{
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
}
#endif

View File

@ -1,5 +1,8 @@
const char helpCmd[] = "sysinfo - System Info\r\n"
"netinfo - WiFi Info\r\n"
"formatPDS - Format Persistence EEPROM Data\r\n"
"formatCFG - Format Configuration EEPROM Data\r\n"
"checkEE - Check EEPROM with checksum\r\n";
const char helpCmd[] = "sysinfo - System Info\r\n"
"netinfo - WiFi Info\r\n"
"formatPDS - Format Persistence EEPROM Data\r\n"
"formatCFG - Format Configuration EEPROM Data\r\n"
"checkEE - Check EEPROM with checksum\r\n"
"dumpEE1k - This will dump the first 1kb of EEPROM to Serial\r\n"
"dumpEE - This will dump the whole EPPROM to Serial\r\n"
"resetPageEE - This will Reset the PersistenceData Page\r\n";

View File

@ -133,7 +133,7 @@ void initWebUI()
option_speedsource = ESPUI.addControl(ControlType::Select, "Geschwindigkeit Quelle", "", ControlColor::Wetasphalt, tab_wheel, &speedSourceSelect_callback);
button_changeSource = ESPUI.addControl(ControlType::Button, "neue Quelle übernehmen. ACHTUNG: Gerät startet neu!", "Übernehmen", ControlColor::Wetasphalt, tab_wheel, &speedSourceSelect_callback);
for (int i = 0; i < SpeedSourceString_Elements; i++)
for (int i = 0; i < (int)SpeedSourceString_Elements; i++)
{
ESPUI.addControl(ControlType::Option, SpeedSourceString[i], String(i), ControlColor::Wetasphalt, option_speedsource);
}
@ -155,6 +155,7 @@ void initWebUI()
ESPUI.addControl(ControlType::Option, GPSBaudRateString[i], String(i), ControlColor::Peterriver, option_gps_baudrate);
}
break;
#if PCB_REVISION >= 13
case SOURCE_CAN:
option_can_source = ESPUI.addControl(ControlType::Select, "CAN-Bus Quelle", "", ControlColor::Peterriver, tab_wheel, &SettingChanged_Callback);
for (int i = 0; i < CANSourceString_Elements; i++)
@ -162,6 +163,7 @@ void initWebUI()
ESPUI.addControl(ControlType::Option, CANSourceString[i], String(i), ControlColor::Peterriver, option_can_source);
}
break;
#endif
case SOURCE_TIME:
break;
}