doc: renamed project

This commit is contained in:
SylvanArnold
2025-04-29 13:52:54 +02:00
committed by Sylvan Arnold
parent 244e516bd8
commit 32618389d1
985 changed files with 1 additions and 1 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,96 @@
/*
* Copyright (c) 2022, Erich Styger
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef MODBUS_MCUHEIDELBERG_H_
#define MODBUS_MCUHEIDELBERG_H_
#include "McuShell.h"
#include <stdint.h>
#include <stdbool.h>
typedef enum McuHeidelberg_Event_e {
McuHeidelberg_Event_HW_State_Changed, /* hardware charger state has changed */
McuHeidelberg_Event_UserChargingMode_Changed, /* user charging mode has changed */
McuHeidelberg_Event_ChargingSetCurrent_Changed, /* current in the charger has changed */
McuHeidelberg_Event_SolarPower_Changed, /* produced solar power has changed */
McuHeidelberg_Event_SitePower_Changed, /* used power by site has changed */
McuHeidelberg_Event_ChargerPower_Changed, /* charging value of car changed */
McuHeidelberg_Event_GridPower_Changed, /* grid power value changed */
McuHeidelberg_Event_BatteryPower_Changed, /* battery power value changed */
} McuHeidelberg_Event_e;
typedef void (*McuHeidelberg_EventCallback)(McuHeidelberg_Event_e);
/*!< Event callback function prototype */
/*!
* \brief Register a callback for events
* \param callback function pointer for the callback
*/
void McuHeidelberg_RegisterEventCallback(McuHeidelberg_EventCallback callback);
/* types of charging strategies and modes */
typedef enum McuHeidelberg_ChargingMode_e {
McuHeidelberg_User_ChargingMode_Stop, /* stop immediately the charging */
McuHeidelberg_User_ChargingMode_OnlyPV, /* charge only with the PV power available */
McuHeidelberg_User_ChargingMode_Slow, /* charge immediately with the minimal power */
McuHeidelberg_User_ChargingMode_SlowPlusPV, /* charge immediately with the minimal power. If PV supports more power, the power level gets increased */
McuHeidelberg_User_ChargingMode_Fast, /* charge immediately with maximum power */
McuHeidelberg_User_ChargingMode_6_Amp,
McuHeidelberg_User_ChargingMode_7_Amp,
McuHeidelberg_User_ChargingMode_8_Amp,
McuHeidelberg_User_ChargingMode_9_Amp,
McuHeidelberg_User_ChargingMode_10_Amp,
McuHeidelberg_User_ChargingMode_11_Amp,
McuHeidelberg_User_ChargingMode_12_Amp,
McuHeidelberg_User_ChargingMode_13_Amp,
McuHeidelberg_User_ChargingMode_14_Amp,
McuHeidelberg_User_ChargingMode_15_Amp,
McuHeidelberg_User_ChargingMode_16_Amp,
McuHeidelberg_User_ChargingMode_NofChargingMode, /* sentinel, must be last in list! */
} McuHeidelberg_UserChargingMode_e;
McuHeidelberg_UserChargingMode_e McuHeidelberg_GetUserChargingMode(void);
void McuHeidelberg_SetUserChargingMode(McuHeidelberg_UserChargingMode_e mode);
/* state of the hardware wallbox */
typedef enum McuHeidelbergChargerState_e {
McuHeidelberg_ChargerState_A1 = 2, /* no vehicle, charging not possible */
McuHeidelberg_ChargerState_A2 = 3, /* no vehicle, charging possible */
McuHeidelberg_ChargerState_B1 = 4, /* vehicle plugged, charging not possible */
McuHeidelberg_ChargerState_B2 = 5, /* vehicle plugged, charging possible */
McuHeidelberg_ChargerState_C1 = 6, /* vehicle charging request, charging not possible */
McuHeidelberg_ChargerState_C2 = 7, /* vehicle charging request, charging possible */
McuHeidelberg_ChargerState_Derating = 8, /* charging current derating due temperature */
McuHeidelberg_ChargerState_E = 9, /* error state */
McuHeidelberg_ChargerState_F = 10, /* wallbox locked or not ready */
McuHeidelberg_ChargerState_Error = 11, /* error state */
} McuHeidelbergChargerState_e;
McuHeidelbergChargerState_e McuHeidelberg_GetHWChargerState(void);
const unsigned char *McuHeidelberg_GetShortHWChargerStateString(McuHeidelbergChargerState_e state);
uint32_t McuHeidelberg_GetSolarPowerWatt(void);
void McuHeidelberg_SetSolarPowerWatt(uint32_t powerW);
uint32_t McuHeidelberg_GetSitePowerWatt(void);
void McuHeidelberg_SetSitePowerWatt(uint32_t powerW);
int32_t McuHeidelberg_GetGridPowerWatt(void);
void McuHeidelberg_SetGridPowerWatt(int32_t powerW);
int32_t McuHeidelberg_GetBatteryPowerWatt(void);
void McuHeidelberg_SetBatteryPowerWatt(int32_t powerW);
uint32_t McuHeidelberg_GetMaxCarPower(void);
uint32_t McuHeidelberg_GetCurrChargerPower(void);
uint8_t McuHeidelberg_ParseCommand(const unsigned char *cmd, bool *handled, const McuShell_StdIOType *io);
void McuHeidelberg_Deinit(void);
void McuHeidelberg_Init(void);
#endif /* MODBUS_MCUHEIDELBERG_H_ */

View File

@@ -0,0 +1,40 @@
/*
* Copyright (c) 2022, Erich Styger
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef MODBUS_MCUHEIDELBERG_CONFIG_H_
#define MODBUS_MCUHEIDELBERG_CONFIG_H_
#ifndef McuHeidelberg_CONFIG_USE_MOCK_WALLBOX
#define McuHeidelberg_CONFIG_USE_MOCK_WALLBOX (0)
/*!< if using testing mocks for wallbox state and phase values instead of real state. Note this cannot be used with a real vehicle! */
#endif
#ifndef McuHeidelberg_CONFIG_USE_MOCK_SOLAR
#define McuHeidelberg_CONFIG_USE_MOCK_SOLAR (0)
/*!< if using testing mocks for solar and site power instead of real power */
#endif
#ifndef McuHeidelberg_CONFIG_USE_WATCHDOG
#define McuHeidelberg_CONFIG_USE_WATCHDOG (0)
/*!< if using MCU watchdog functionality */
#endif
#ifndef McuHeidelberg_CONFIG_HYSTERESIS_POWER
#define McuHeidelberg_CONFIG_HYSTERESIS_POWER (150)
/*! Use this value as hysteresis */
#endif
#ifndef McuHeidelberg_CONFIG_BASE_SITE_POWER
#define McuHeidelberg_CONFIG_BASE_SITE_POWER (250)
/*! Use this value as the site base power */
#endif
#ifndef McuHeidelberg_CONFIG_DEFAULT_CHARGING_MODE
#define McuHeidelberg_CONFIG_DEFAULT_CHARGING_MODE McuHeidelberg_User_ChargingMode_SlowPlusPV
/*!< one of McuHeidelberg_UserChargingMode_e, used at startup */
#endif
#endif /* MODBUS_MCUHEIDELBERG_CONFIG_H_ */

View File

@@ -0,0 +1,207 @@
/*
* Copyright (c) 2022, Erich Styger
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "McuModbus.h"
#if McuModbus_CONFIG_IS_ENABLED
#include <stddef.h>
#include "McuRTOS.h"
#include "McuUtility.h"
#include "McuUart485.h"
#include "McuLog.h"
#define McuModbus_TELEGRAM_SIZE (8)
#if McuModbus_CONFIG_USE_MUTEX
static SemaphoreHandle_t McuModbus_Mutex = NULL; /* Semaphore to protect bus access */
#endif
#if McuModbus_CONFIG_USE_MUTEX
static bool McuModbus_RequestBus(void) {
return xSemaphoreTakeRecursive(McuModbus_Mutex, portMAX_DELAY)==pdPASS;
}
#endif
#if McuModbus_CONFIG_USE_MUTEX
static void McuModbus_ReleaseBus(void) {
(void)xSemaphoreGiveRecursive(McuModbus_Mutex);
}
#endif
static uint16_t McuModbus_CRC(uint8_t *buf, size_t len) {
uint16_t crc = 0xFFFF;
for(int pos=0; pos<len; pos++) {
crc ^= (uint16_t)buf[pos];
for (int i=8; i>0; i--) {
if ((crc&0x1)!=0) {
crc >>= 1;
crc ^= 0xA001;
} else {
crc >>= 1;
}
}
}
return crc;
}
static void McuModbus_CreateReadTelegram(uint8_t telegram[McuModbus_TELEGRAM_SIZE], uint8_t slaveId, McuModbus_Func_Code_e function, uint16_t addr, uint16_t nofCoils) {
telegram[0] = slaveId;
telegram[1] = function;
telegram[2] = addr>>8;
telegram[3] = addr&0xff;
telegram[4] = nofCoils>>8;
telegram[5] = nofCoils&0xff;
uint16_t crc = McuModbus_CRC(telegram, 6);
telegram[6] = crc&0xff;
telegram[7] = crc>>8;
}
static void McuModbus_CreateWriteTelegram(uint8_t telegram[McuModbus_TELEGRAM_SIZE], uint8_t slaveId, McuModbus_Func_Code_e function, uint16_t addr, uint16_t data) {
telegram[0] = slaveId;
telegram[1] = function;
telegram[2] = addr>>8;
telegram[3] = addr&0xff;
telegram[4] = data>>8;
telegram[5] = data&0xff;
uint16_t crc = McuModbus_CRC(telegram, 6);
telegram[6] = crc&0xff;
telegram[7] = crc>>8;
}
static void McuModbus_SendTelegram(uint8_t *telegram, size_t size) {
McuUart485_ClearRxQueue(); /* clear queue just in case */
McuUart485_SendBlock(telegram, size); /* send telegram */
}
static uint8_t McuModbus_ReceiveResponseInputRegister(uint8_t deviceID, McuModbus_Func_Code_e function, uint16_t *regs, uint16_t nofRegs, int timeoutMs) {
unsigned char response[32]; /* minimum of 5 bytes + nofRegs*2 */
int i;
if (5+nofRegs*sizeof(uint16_t)>sizeof(response)) {
return ERR_OVERFLOW; /* response buffer size too small */
}
i = 0;
memset(regs, 0, nofRegs/sizeof(uint16_t)); /* clear response buffer */
while(i<sizeof(response) && i<(5+nofRegs*sizeof(uint16_t)) && timeoutMs>0) {
if (McuUart485_GetRxQueueByte(&response[i], pdMS_TO_TICKS(McuModbus_CONFIG_QUEUE_RX_TIMEOUT_MS))==ERR_OK) {
i++;
} else { /* timeout */
timeoutMs -= McuModbus_CONFIG_QUEUE_RX_TIMEOUT_MS; /* queue waiting time */
if (timeoutMs<0) {
return ERR_IDLE; /* timeout */
}
}
}
/* check response, see https://www.simplymodbus.ca/FC04.htm */
if (i!=5+nofRegs*sizeof(uint16_t)) { /* 1: slave addr, 1: function code, 1: nof bytes, nofData*2, 2: CRC */
return ERR_FAILED; /* response size does not match */
}
if (response[0]!=deviceID) {
return ERR_FAILED; /* response ID does not match */
}
if (response[1]!=function) {
return ERR_FAILED; /* response function code does not match */
}
if (response[2]!=nofRegs*sizeof(uint16_t)) {
return ERR_FAILED; /* number of bytes does not match expectation */
}
uint16_t crc = (response[4+nofRegs*sizeof(uint16_t)]<<8)+response[3+nofRegs*sizeof(uint16_t)];
if (crc!=McuModbus_CRC(response, 3+nofRegs*sizeof(uint16_t))) {
return ERR_CRC; /* CRC does not match */
}
for(int i=0; i<nofRegs; i++) {
regs[i] = (response[3+i*sizeof(uint16_t)]<<8)+response[4+i*sizeof(uint16_t)];
}
return ERR_OK;
}
static uint8_t McuModbus_ReadRegister(uint8_t deviceID, McuModbus_Func_Code_e function, uint16_t addr, uint16_t nofRegs, uint16_t *result) {
uint8_t telegram[McuModbus_TELEGRAM_SIZE];
uint8_t res;
McuModbus_CreateReadTelegram(telegram, deviceID, function, addr, nofRegs);
#if McuModbus_CONFIG_USE_MUTEX
if (McuModbus_RequestBus()) {
#endif
McuModbus_SendTelegram(telegram, sizeof(telegram));
res = McuModbus_ReceiveResponseInputRegister(deviceID, function, result, nofRegs, McuModbus_CONFIG_RX_TIMEOUT_MS);
vTaskDelay(pdMS_TO_TICKS(McuModbus_CONFIG_BUS_WAIT_TIME_MS));
#if McuModbus_CONFIG_USE_MUTEX
McuModbus_ReleaseBus();
}
#endif
return res;
}
static uint8_t McuModbus_WriteRegister(uint8_t deviceID, McuModbus_Func_Code_e function, uint16_t addr, uint16_t value) {
uint8_t telegram[McuModbus_TELEGRAM_SIZE];
McuModbus_CreateWriteTelegram(telegram, deviceID, function, addr, value);
#if McuModbus_CONFIG_USE_MUTEX
if (McuModbus_RequestBus()) {
#endif
McuModbus_SendTelegram(telegram, sizeof(telegram));
vTaskDelay(pdMS_TO_TICKS(McuModbus_CONFIG_BUS_WAIT_TIME_MS));
#if McuModbus_CONFIG_USE_MUTEX
McuModbus_ReleaseBus();
}
#endif
return ERR_OK;
}
uint8_t McuModbus_ReadInputRegisters(uint8_t deviceID, uint16_t addr, uint16_t nofRegs, uint16_t *result) {
return McuModbus_ReadRegister(deviceID, McuModbus_Func_ReadInputRegisters, addr, nofRegs, result);
}
uint8_t McuModbus_ReadHoldingRegisters(uint8_t deviceID, uint16_t addr, uint16_t nofRegs, uint16_t *result) {
return McuModbus_ReadRegister(deviceID, McuModbus_Func_ReadHoldingRegisters, addr, nofRegs, result);
}
uint8_t McuModbus_WriteHoldingRegister(uint8_t deviceID, uint16_t addr, uint16_t value) {
return McuModbus_WriteRegister(deviceID, McuModbus_Func_WriteSingleRegister, addr, value);
}
static uint8_t PrintStatus(const McuShell_StdIOType *io) {
McuShell_SendStatusStr((unsigned char*)"McuModbus", (unsigned char*)"Modbus settings\r\n", io->stdOut);
return ERR_OK;
}
static uint8_t PrintHelp(const McuShell_StdIOType *io) {
McuShell_SendHelpStr((unsigned char*)"McuModbus", (unsigned char*)"Group of McuModbus commands\r\n", io->stdOut);
McuShell_SendHelpStr((unsigned char*)" help|status", (unsigned char*)"Print help or status information\r\n", io->stdOut);
return ERR_OK;
}
uint8_t McuModbus_ParseCommand(const unsigned char *cmd, bool *handled, const McuShell_StdIOType *io) {
if (McuUtility_strcmp((char*)cmd, McuShell_CMD_HELP)==0 || McuUtility_strcmp((char*)cmd, "McuModbus help")==0) {
*handled = true;
return PrintHelp(io);
} else if ((McuUtility_strcmp((char*)cmd, McuShell_CMD_STATUS)==0) || (McuUtility_strcmp((char*)cmd, "McuModbus status")==0)) {
*handled = true;
return PrintStatus(io);
}
return ERR_OK;
}
void McuModbus_Deinit(void) {
#if McuModbus_CONFIG_USE_MUTEX
vSemaphoreDelete(McuModbus_Mutex);
McuModbus_Mutex = NULL;
#endif
}
void McuModbus_Init(void) {
#if McuModbus_CONFIG_USE_MUTEX
McuModbus_Mutex = xSemaphoreCreateRecursiveMutex();
if (McuModbus_Mutex==NULL) { /* semaphore creation failed */
McuLog_fatal("failed creating mutex");
for(;;) {} /* error, not enough memory? */
}
vQueueAddToRegistry(McuModbus_Mutex, "McuModbus_Mutex");
#endif
}
#endif /* McuModbus_CONFIG_IS_ENABLED */

View File

@@ -0,0 +1,39 @@
/*
* Copyright (c) 2022, Erich Styger
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef MCUMODBUS_H_
#define MCUMODBUS_H_
#include "McuModbus_config.h"
#include <stdint.h>
#include <stdbool.h>
#include "McuShell.h"
typedef enum McuModbus_Func_Code_e {
McuModbus_Func_ReadCoils = 0x01,
McuModbus_Func_ReadDiscreteInputs = 0x02,
McuModbus_Func_ReadHoldingRegisters = 0x03,
McuModbus_Func_ReadInputRegisters = 0x04,
McuModbus_Func_WriteSingleCoil = 0x05,
McuModbus_Func_WriteSingleRegister = 0x06,
McuModbus_Func_WriteMultipleCoils = 0x0f,
McuModbus_Func_WriteMultipleRegisters = 0x10,
} McuModbus_Func_Code_e;
uint8_t McuModbus_ParseCommand(const unsigned char *cmd, bool *handled, const McuShell_StdIOType *io);
void McuModbus_Send(uint8_t slaveID, McuModbus_Func_Code_e function, uint16_t addr, uint16_t nofCoils, uint8_t *result, const McuShell_StdIOType *io);
uint8_t McuModbus_ReadInputRegisters(uint8_t deviceID, uint16_t addr, uint16_t nofRegs, uint16_t *result);
uint8_t McuModbus_WriteInputRegister(uint8_t deviceID, uint16_t addr, uint16_t value);
uint8_t McuModbus_ReadHoldingRegisters(uint8_t deviceID, uint16_t addr, uint16_t nofRegs, uint16_t *result);
uint8_t McuModbus_WriteHoldingRegister(uint8_t deviceID, uint16_t addr, uint16_t value);
void McuModbus_Deinit(void);
void McuModbus_Init(void);
#endif /* MCUMODBUS_H_ */

View File

@@ -0,0 +1,35 @@
/*
* Copyright (c) 2022, Erich Styger
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef MODBUS_MCUMODBUS_CONFIG_H_
#define MODBUS_MCUMODBUS_CONFIG_H_
#ifndef McuModbus_CONFIG_IS_ENABLED
#define McuModbus_CONFIG_IS_ENABLED (0)
/*!< 0: disabled; 1: enabled */
#endif
#ifndef McuModbus_CONFIG_RX_TIMEOUT_MS
#define McuModbus_CONFIG_RX_TIMEOUT_MS (500)
/*!< default timeout waiting for an answer from the device */
#endif
#ifndef McuModbus_CONFIG_QUEUE_RX_TIMEOUT_MS
#define McuModbus_CONFIG_QUEUE_RX_TIMEOUT_MS (10)
/*!< default timeout waiting for a queue element from the RX interrupt */
#endif
#ifndef McuModbus_CONFIG_BUS_WAIT_TIME_MS
#define McuModbus_CONFIG_BUS_WAIT_TIME_MS (15)
/*!< forced waiting time after a bus transfer */
#endif
#ifndef McuModbus_CONFIG_USE_MUTEX
#define McuModbus_CONFIG_USE_MUTEX (1)
/*!< 1: use a mutex to access the bus; 0: no mutex used */
#endif
#endif /* MODBUS_MCUMODBUS_CONFIG_H_ */

View File

@@ -0,0 +1,10 @@
ModBus readme.txt
-----------------
See https://en.wikipedia.org/wiki/Modbus
Modbus client on host:
https://sourceforge.net/projects/qmodbus/
Modbus für RP2040:
https://github.com/alejoseb/Modbus-PI-Pico-FreeRTOS