mirror of
https://github.com/dalathegreat/Battery-Emulator.git
synced 2025-10-05 10:49:42 +02:00
add devboard folder
This commit is contained in:
parent
8a1d3c1da5
commit
606f49d548
33 changed files with 32 additions and 32 deletions
|
@ -1,5 +1,5 @@
|
|||
#include "BMW-I3-BATTERY.h"
|
||||
#include "../../ESP32CAN.h"
|
||||
#include "../devboard/can/ESP32CAN.h"
|
||||
#include "../lib/ThomasBarth-ESP32-CAN-Driver/CAN_config.h"
|
||||
|
||||
//TODO before using
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef BMW_I3_BATTERY_H
|
||||
#define BMW_I3_BATTERY_H
|
||||
#include <Arduino.h>
|
||||
#include "../../ESP32CAN.h"
|
||||
#include "../devboard/can/ESP32CAN.h"
|
||||
#include "../../USER_SETTINGS.h"
|
||||
|
||||
#define ABSOLUTE_MAX_VOLTAGE 4040 // 404.4V,if battery voltage goes over this, charging is not possible (goes into forced discharge)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include "CHADEMO-BATTERY.h"
|
||||
#include "../../ESP32CAN.h"
|
||||
#include "../devboard/can/ESP32CAN.h"
|
||||
#include "../lib/ThomasBarth-ESP32-CAN-Driver/CAN_config.h"
|
||||
|
||||
/* Do not change code below unless you are sure what you are doing */
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef CHADEMO_BATTERY_H
|
||||
#define CHADEMO_BATTERY_H
|
||||
#include <Arduino.h>
|
||||
#include "../../ESP32CAN.h"
|
||||
#include "../devboard/can/ESP32CAN.h"
|
||||
#include "../../USER_SETTINGS.h"
|
||||
|
||||
#define ABSOLUTE_MAX_VOLTAGE 4040 // 404.4V,if battery voltage goes over this, charging is not possible (goes into forced discharge)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include "IMIEV-CZERO-ION-BATTERY.h"
|
||||
#include "../../ESP32CAN.h"
|
||||
#include "../devboard/can/ESP32CAN.h"
|
||||
#include "../lib/ThomasBarth-ESP32-CAN-Driver/CAN_config.h"
|
||||
|
||||
//Code still work in progress, TODO:
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef IMIEV_CZERO_ION_BATTERY_H
|
||||
#define IMIEV_CZERO_ION_BATTERY_H
|
||||
#include <Arduino.h>
|
||||
#include "../../ESP32CAN.h"
|
||||
#include "../devboard/can/ESP32CAN.h"
|
||||
#include "../../USER_SETTINGS.h"
|
||||
|
||||
#define ABSOLUTE_MAX_VOLTAGE 4040 // 404.4V,if battery voltage goes over this, charging is not possible (goes into forced discharge)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include "KIA-HYUNDAI-64-BATTERY.h"
|
||||
#include "../../ESP32CAN.h"
|
||||
#include "../devboard/can/ESP32CAN.h"
|
||||
#include "../lib/ThomasBarth-ESP32-CAN-Driver/CAN_config.h"
|
||||
|
||||
/* Do not change code below unless you are sure what you are doing */
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef KIA_HYUNDAI_64_BATTERY_H
|
||||
#define KIA_HYUNDAI_64_BATTERY_H
|
||||
#include <Arduino.h>
|
||||
#include "../../ESP32CAN.h"
|
||||
#include "../devboard/can/ESP32CAN.h"
|
||||
#include "../../USER_SETTINGS.h"
|
||||
|
||||
#define ABSOLUTE_MAX_VOLTAGE 4040 // 404.4V,if battery voltage goes over this, charging is not possible (goes into forced discharge)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include "NISSAN-LEAF-BATTERY.h"
|
||||
#include "../../ESP32CAN.h"
|
||||
#include "../devboard/can/ESP32CAN.h"
|
||||
#include "../lib/ThomasBarth-ESP32-CAN-Driver/CAN_config.h"
|
||||
|
||||
/* Do not change code below unless you are sure what you are doing */
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef NISSAN_LEAF_BATTERY_H
|
||||
#define NISSAN_LEAF_BATTERY_H
|
||||
#include <Arduino.h>
|
||||
#include "../../ESP32CAN.h"
|
||||
#include "../devboard/can/ESP32CAN.h"
|
||||
#include "../../USER_SETTINGS.h"
|
||||
|
||||
#define ABSOLUTE_MAX_VOLTAGE 4040 // 404.4V,if battery voltage goes over this, charging is not possible (goes into forced discharge)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include "RENAULT-ZOE-BATTERY.h"
|
||||
#include "../../ESP32CAN.h"
|
||||
#include "../devboard/can/ESP32CAN.h"
|
||||
#include "../lib/ThomasBarth-ESP32-CAN-Driver/CAN_config.h"
|
||||
|
||||
/* Do not change code below unless you are sure what you are doing */
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef RENAULT_ZOE_BATTERY_H
|
||||
#define RENAULT_ZOE_BATTERY_H
|
||||
#include <Arduino.h>
|
||||
#include "../../ESP32CAN.h"
|
||||
#include "../devboard/can/ESP32CAN.h"
|
||||
#include "../../USER_SETTINGS.h"
|
||||
|
||||
#define ABSOLUTE_MAX_VOLTAGE 4040 // 404.4V,if battery voltage goes over this, charging is not possible (goes into forced discharge)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include "TESLA-MODEL-3-BATTERY.h"
|
||||
#include "../../ESP32CAN.h"
|
||||
#include "../devboard/can/ESP32CAN.h"
|
||||
#include "../lib/ThomasBarth-ESP32-CAN-Driver/CAN_config.h"
|
||||
|
||||
/* Do not change code below unless you are sure what you are doing */
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef TESLA_MODEL_3_BATTERY_H
|
||||
#define TESLA_MODEL_3_BATTERY_H
|
||||
#include <Arduino.h>
|
||||
#include "../../ESP32CAN.h"
|
||||
#include "../devboard/can/ESP32CAN.h"
|
||||
#include "../../USER_SETTINGS.h"
|
||||
|
||||
#define ABSOLUTE_MAX_VOLTAGE 4030 // 403.0V,if battery voltage goes over this, charging is not possible (goes into forced discharge)
|
||||
|
|
38
Software/src/devboard/can/ESP32CAN.cpp
Normal file
38
Software/src/devboard/can/ESP32CAN.cpp
Normal file
|
@ -0,0 +1,38 @@
|
|||
#include "ESP32CAN.h"
|
||||
#include <Arduino.h>
|
||||
|
||||
int ESP32CAN::CANInit()
|
||||
{
|
||||
return CAN_init();
|
||||
}
|
||||
int ESP32CAN::CANWriteFrame(const CAN_frame_t* p_frame)
|
||||
{
|
||||
static unsigned long start_time;
|
||||
int result = -1;
|
||||
if(tx_ok){
|
||||
result = CAN_write_frame(p_frame);
|
||||
tx_ok = (result == 0) ? true : false;
|
||||
if(tx_ok == false){
|
||||
Serial.println("CAN failure! Check wires");
|
||||
LEDcolor = 3;
|
||||
start_time = millis();
|
||||
}
|
||||
}
|
||||
else{
|
||||
if((millis() - start_time) >= 2000){
|
||||
tx_ok = true;
|
||||
LEDcolor = 0;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
int ESP32CAN::CANStop()
|
||||
{
|
||||
return CAN_stop();
|
||||
}
|
||||
int ESP32CAN::CANConfigFilter(const CAN_filter_t* p_filter)
|
||||
{
|
||||
return CAN_config_filter(p_filter);
|
||||
}
|
||||
|
||||
ESP32CAN ESP32Can;
|
20
Software/src/devboard/can/ESP32CAN.h
Normal file
20
Software/src/devboard/can/ESP32CAN.h
Normal file
|
@ -0,0 +1,20 @@
|
|||
#ifndef ESP32CAN_H
|
||||
#define ESP32CAN_H
|
||||
|
||||
#include "../../lib/ThomasBarth-ESP32-CAN-Driver/CAN_config.h"
|
||||
#include "../../lib/ThomasBarth-ESP32-CAN-Driver/CAN.h"
|
||||
extern uint8_t LEDcolor;
|
||||
|
||||
class ESP32CAN
|
||||
{
|
||||
public:
|
||||
bool tx_ok = true;
|
||||
int CANInit();
|
||||
int CANConfigFilter(const CAN_filter_t* p_filter);
|
||||
int CANWriteFrame(const CAN_frame_t* p_frame);
|
||||
int CANStop();
|
||||
void CANSetCfg(CAN_device_t *can_cfg);
|
||||
};
|
||||
|
||||
extern ESP32CAN ESP32Can;
|
||||
#endif
|
35
Software/src/devboard/config.h
Normal file
35
Software/src/devboard/config.h
Normal file
|
@ -0,0 +1,35 @@
|
|||
#ifndef __CONFIG_H__
|
||||
#define __CONFIG_H__
|
||||
|
||||
// PIN mappings, do not change these unless you are adding on extra hardware to the PCB
|
||||
#define PIN_5V_EN 16
|
||||
#define CAN_TX_PIN 27
|
||||
#define CAN_RX_PIN 26
|
||||
#define CAN_SE_PIN 23
|
||||
|
||||
#define RS485_EN_PIN 17 // 17 /RE
|
||||
#define RS485_TX_PIN 22 // 21
|
||||
#define RS485_RX_PIN 21 // 22
|
||||
#define RS485_SE_PIN 19 // 22 /SHDN
|
||||
|
||||
#ifdef DUAL_CAN
|
||||
#define MCP2515_SCK 12 // SCK input of MCP2515
|
||||
#define MCP2515_MOSI 5 // SDI input of MCP2515
|
||||
#define MCP2515_MISO 34 // SDO output of MCP2515 | Pin 34 is input only, without pullup/down resistors
|
||||
#define MCP2515_CS 18 // CS input of MCP2515
|
||||
#define MCP2515_INT 35 // INT output of MCP2515 | | Pin 35 is input only, without pullup/down resistors
|
||||
#endif
|
||||
|
||||
#ifdef CONTACTOR_CONTROL
|
||||
#define POSITIVE_CONTACTOR_PIN 32
|
||||
#define NEGATIVE_CONTACTOR_PIN 33
|
||||
#define PRECHARGE_PIN 25
|
||||
#endif
|
||||
|
||||
#define SD_MISO_PIN 2
|
||||
#define SD_MOSI_PIN 15
|
||||
#define SD_SCLK_PIN 14
|
||||
#define SD_CS_PIN 13
|
||||
#define WS2812_PIN 4
|
||||
|
||||
#endif
|
14
Software/src/devboard/debug/debug.cfg
Normal file
14
Software/src/devboard/debug/debug.cfg
Normal file
|
@ -0,0 +1,14 @@
|
|||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#
|
||||
# Example OpenOCD configuration file for ESP32-WROVER-KIT board.
|
||||
#
|
||||
# For example, OpenOCD can be started for ESP32 debugging on
|
||||
#
|
||||
# openocd -f board/esp32-wrover-kit-3.3v.cfg
|
||||
#
|
||||
|
||||
# Source the JTAG interface configuration file
|
||||
source [find interface/ftdi/esp32_devkitj_v1.cfg]
|
||||
set ESP32_FLASH_VOLTAGE 3.3
|
||||
# Source the ESP32 configuration file
|
||||
source [find target/esp32.cfg]
|
19
Software/src/devboard/debug/debug_custom.json
Normal file
19
Software/src/devboard/debug/debug_custom.json
Normal file
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"name":"Arduino on ESP32",
|
||||
"toolchainPrefix":"xtensa-esp32-elf",
|
||||
"svdFile":"esp32.svd",
|
||||
"request":"attach",
|
||||
"postAttachCommands":[
|
||||
"set remote hardware-watchpoint-limit 2",
|
||||
"monitor reset halt",
|
||||
"monitor gdb_sync",
|
||||
"thb setup",
|
||||
"c"
|
||||
],
|
||||
"overrideRestartCommands":[
|
||||
"monitor reset halt",
|
||||
"monitor gdb_sync",
|
||||
"thb setup",
|
||||
"c"
|
||||
]
|
||||
}
|
178
Software/src/devboard/esp.c
Normal file
178
Software/src/devboard/esp.c
Normal file
|
@ -0,0 +1,178 @@
|
|||
// Implements the RMT peripheral on Espressif SoCs
|
||||
// Copyright (c) 2020 Lucian Copeland for Adafruit Industries
|
||||
|
||||
/* Uses code from Espressif RGB LED Strip demo and drivers
|
||||
* Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#if defined(ESP32)
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "driver/rmt.h"
|
||||
|
||||
#if defined(ESP_IDF_VERSION)
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)
|
||||
#define HAS_ESP_IDF_4
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// This code is adapted from the ESP-IDF v3.4 RMT "led_strip" example, altered
|
||||
// to work with the Arduino version of the ESP-IDF (3.2)
|
||||
|
||||
#define WS2812_T0H_NS (400)
|
||||
#define WS2812_T0L_NS (850)
|
||||
#define WS2812_T1H_NS (800)
|
||||
#define WS2812_T1L_NS (450)
|
||||
|
||||
#define WS2811_T0H_NS (500)
|
||||
#define WS2811_T0L_NS (2000)
|
||||
#define WS2811_T1H_NS (1200)
|
||||
#define WS2811_T1L_NS (1300)
|
||||
|
||||
static uint32_t t0h_ticks = 0;
|
||||
static uint32_t t1h_ticks = 0;
|
||||
static uint32_t t0l_ticks = 0;
|
||||
static uint32_t t1l_ticks = 0;
|
||||
|
||||
// Limit the number of RMT channels available for the Neopixels. Defaults to all
|
||||
// channels (8 on ESP32, 4 on ESP32-S2 and S3). Redefining this value will free
|
||||
// any channels with a higher number for other uses, such as IR send-and-recieve
|
||||
// libraries. Redefine as 1 to restrict Neopixels to only a single channel.
|
||||
#define ADAFRUIT_RMT_CHANNEL_MAX RMT_CHANNEL_MAX
|
||||
|
||||
#define RMT_LL_HW_BASE (&RMT)
|
||||
|
||||
bool rmt_reserved_channels[ADAFRUIT_RMT_CHANNEL_MAX];
|
||||
|
||||
static void IRAM_ATTR ws2812_rmt_adapter(const void *src, rmt_item32_t *dest, size_t src_size,
|
||||
size_t wanted_num, size_t *translated_size, size_t *item_num)
|
||||
{
|
||||
if (src == NULL || dest == NULL) {
|
||||
*translated_size = 0;
|
||||
*item_num = 0;
|
||||
return;
|
||||
}
|
||||
const rmt_item32_t bit0 = {{{ t0h_ticks, 1, t0l_ticks, 0 }}}; //Logical 0
|
||||
const rmt_item32_t bit1 = {{{ t1h_ticks, 1, t1l_ticks, 0 }}}; //Logical 1
|
||||
size_t size = 0;
|
||||
size_t num = 0;
|
||||
uint8_t *psrc = (uint8_t *)src;
|
||||
rmt_item32_t *pdest = dest;
|
||||
while (size < src_size && num < wanted_num) {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
// MSB first
|
||||
if (*psrc & (1 << (7 - i))) {
|
||||
pdest->val = bit1.val;
|
||||
} else {
|
||||
pdest->val = bit0.val;
|
||||
}
|
||||
num++;
|
||||
pdest++;
|
||||
}
|
||||
size++;
|
||||
psrc++;
|
||||
}
|
||||
*translated_size = size;
|
||||
*item_num = num;
|
||||
}
|
||||
|
||||
void espShow(uint8_t pin, uint8_t *pixels, uint32_t numBytes, boolean is800KHz) {
|
||||
// Reserve channel
|
||||
rmt_channel_t channel = ADAFRUIT_RMT_CHANNEL_MAX;
|
||||
for (size_t i = 0; i < ADAFRUIT_RMT_CHANNEL_MAX; i++) {
|
||||
if (!rmt_reserved_channels[i]) {
|
||||
rmt_reserved_channels[i] = true;
|
||||
channel = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (channel == ADAFRUIT_RMT_CHANNEL_MAX) {
|
||||
// Ran out of channels!
|
||||
return;
|
||||
}
|
||||
|
||||
#if defined(HAS_ESP_IDF_4)
|
||||
rmt_config_t config = RMT_DEFAULT_CONFIG_TX(pin, channel);
|
||||
config.clk_div = 2;
|
||||
#else
|
||||
// Match default TX config from ESP-IDF version 3.4
|
||||
rmt_config_t config = {
|
||||
.rmt_mode = RMT_MODE_TX,
|
||||
.channel = channel,
|
||||
.gpio_num = pin,
|
||||
.clk_div = 2,
|
||||
.mem_block_num = 1,
|
||||
.tx_config = {
|
||||
.carrier_freq_hz = 38000,
|
||||
.carrier_level = RMT_CARRIER_LEVEL_HIGH,
|
||||
.idle_level = RMT_IDLE_LEVEL_LOW,
|
||||
.carrier_duty_percent = 33,
|
||||
.carrier_en = false,
|
||||
.loop_en = false,
|
||||
.idle_output_en = true,
|
||||
}
|
||||
};
|
||||
#endif
|
||||
rmt_config(&config);
|
||||
rmt_driver_install(config.channel, 0, 0);
|
||||
|
||||
// Convert NS timings to ticks
|
||||
uint32_t counter_clk_hz = 0;
|
||||
|
||||
#if defined(HAS_ESP_IDF_4)
|
||||
rmt_get_counter_clock(channel, &counter_clk_hz);
|
||||
#else
|
||||
// this emulates the rmt_get_counter_clock() function from ESP-IDF 3.4
|
||||
if (RMT_LL_HW_BASE->conf_ch[config.channel].conf1.ref_always_on == RMT_BASECLK_REF) {
|
||||
uint32_t div_cnt = RMT_LL_HW_BASE->conf_ch[config.channel].conf0.div_cnt;
|
||||
uint32_t div = div_cnt == 0 ? 256 : div_cnt;
|
||||
counter_clk_hz = REF_CLK_FREQ / (div);
|
||||
} else {
|
||||
uint32_t div_cnt = RMT_LL_HW_BASE->conf_ch[config.channel].conf0.div_cnt;
|
||||
uint32_t div = div_cnt == 0 ? 256 : div_cnt;
|
||||
counter_clk_hz = APB_CLK_FREQ / (div);
|
||||
}
|
||||
#endif
|
||||
|
||||
// NS to tick converter
|
||||
float ratio = (float)counter_clk_hz / 1e9;
|
||||
|
||||
if (is800KHz) {
|
||||
t0h_ticks = (uint32_t)(ratio * WS2812_T0H_NS);
|
||||
t0l_ticks = (uint32_t)(ratio * WS2812_T0L_NS);
|
||||
t1h_ticks = (uint32_t)(ratio * WS2812_T1H_NS);
|
||||
t1l_ticks = (uint32_t)(ratio * WS2812_T1L_NS);
|
||||
} else {
|
||||
t0h_ticks = (uint32_t)(ratio * WS2811_T0H_NS);
|
||||
t0l_ticks = (uint32_t)(ratio * WS2811_T0L_NS);
|
||||
t1h_ticks = (uint32_t)(ratio * WS2811_T1H_NS);
|
||||
t1l_ticks = (uint32_t)(ratio * WS2811_T1L_NS);
|
||||
}
|
||||
|
||||
// Initialize automatic timing translator
|
||||
rmt_translator_init(config.channel, ws2812_rmt_adapter);
|
||||
|
||||
// Write and wait to finish
|
||||
rmt_write_sample(config.channel, pixels, (size_t)numBytes, true);
|
||||
rmt_wait_tx_done(config.channel, pdMS_TO_TICKS(100));
|
||||
|
||||
// Free channel again
|
||||
rmt_driver_uninstall(config.channel);
|
||||
rmt_reserved_channels[channel] = false;
|
||||
|
||||
gpio_set_direction(pin, GPIO_MODE_OUTPUT);
|
||||
}
|
||||
|
||||
#endif
|
46087
Software/src/devboard/esp32.svd
Normal file
46087
Software/src/devboard/esp32.svd
Normal file
File diff suppressed because it is too large
Load diff
184
Software/src/devboard/modbus/mbServerFCs.cpp
Normal file
184
Software/src/devboard/modbus/mbServerFCs.cpp
Normal file
|
@ -0,0 +1,184 @@
|
|||
#include "mbServerFCs.h"
|
||||
#include "../../lib/eModbus-eModbus/Logging.h"
|
||||
|
||||
//modbus register memory - declared in main.cpp
|
||||
|
||||
extern uint16_t mbPV[MBPV_MAX];
|
||||
|
||||
// Server function to handle FC 0x03
|
||||
ModbusMessage FC03(ModbusMessage request)
|
||||
{
|
||||
//Serial.println(request);
|
||||
ModbusMessage response; // The Modbus message we are going to give back
|
||||
uint16_t addr = 0; // Start address
|
||||
uint16_t words = 0; // # of words requested
|
||||
request.get(2, addr); // read address from request
|
||||
request.get(4, words); // read # of words from request
|
||||
char debugString[1000];
|
||||
|
||||
LOG_D("FC03 received: read %d words @ %d\r\n", words, addr);
|
||||
|
||||
// Address overflow?
|
||||
if ((addr + words) > MBPV_MAX)
|
||||
{
|
||||
// Yes - send respective error response
|
||||
response.setError(request.getServerID(), request.getFunctionCode(), ILLEGAL_DATA_ADDRESS);
|
||||
LOG_D("ERROR - ILLEGAL DATA ADDRESS\r\n");
|
||||
return response;
|
||||
}
|
||||
|
||||
// Set up response
|
||||
response.add(request.getServerID(), request.getFunctionCode(), (uint8_t)(words * 2));
|
||||
sprintf(debugString, "Read : ");
|
||||
for (uint8_t i = 0; i < words; ++i)
|
||||
{
|
||||
// send increasing data values
|
||||
response.add((uint16_t)(mbPV[addr + i]));
|
||||
sprintf(debugString + strlen(debugString), "%i ", mbPV[addr + i]);
|
||||
}
|
||||
LOG_V("%s\r\n", debugString);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
// Server function to handle FC 0x06
|
||||
ModbusMessage FC06(ModbusMessage request)
|
||||
{
|
||||
//Serial.println(request);
|
||||
ModbusMessage response; // The Modbus message we are going to give back
|
||||
uint16_t addr = 0; // Start address
|
||||
uint16_t val = 0; // value to write
|
||||
request.get(2, addr); // read address from request
|
||||
request.get(4, val); // read # of words from request
|
||||
|
||||
LOG_D("FC06 received: write 1 word @ %d\r\n", addr);
|
||||
|
||||
// Address overflow?
|
||||
if ((addr) > MBPV_MAX)
|
||||
{
|
||||
// Yes - send respective error response
|
||||
response.setError(request.getServerID(), request.getFunctionCode(), ILLEGAL_DATA_ADDRESS);
|
||||
LOG_D("ERROR - ILLEGAL DATA ADDRESS\r\n");
|
||||
return response;
|
||||
}
|
||||
|
||||
// Do the write
|
||||
mbPV[addr] = val;
|
||||
LOG_V("Write : %i", val);
|
||||
|
||||
// Set up response
|
||||
response.add(request.getServerID(), request.getFunctionCode(), mbPV[addr]);
|
||||
return response;
|
||||
}
|
||||
|
||||
// Server function to handle FC 0x10 (FC16)
|
||||
ModbusMessage FC16(ModbusMessage request)
|
||||
{
|
||||
//Serial.println(request);
|
||||
ModbusMessage response; // The Modbus message we are going to give back
|
||||
uint16_t addr = 0; // Start address
|
||||
uint16_t words = 0; // total words to write
|
||||
uint8_t bytes = 0; // # of data bytes in request
|
||||
uint16_t val = 0; // value to be written
|
||||
request.get(2, addr); // read address from request
|
||||
request.get(4, words); // read # of words from request
|
||||
request.get(6, bytes); // read # of data bytes from request (seems redundant with # of words)
|
||||
char debugString[1000];
|
||||
|
||||
LOG_D("FC16 received: write %d words @ %d\r\n", words, addr);
|
||||
|
||||
// # of registers proper?
|
||||
if ((bytes != (words * 2)) // byte count in request must match # of words in request
|
||||
|| (words > 123)) // can't support more than this in request packet
|
||||
{ // Yes - send respective error response
|
||||
response.setError(request.getServerID(), request.getFunctionCode(), ILLEGAL_DATA_VALUE);
|
||||
LOG_D("ERROR - ILLEGAL DATA VALUE\r\n");
|
||||
return response;
|
||||
}
|
||||
// Address overflow?
|
||||
if ((addr + words) > MBPV_MAX)
|
||||
{
|
||||
// Yes - send respective error response
|
||||
response.setError(request.getServerID(), request.getFunctionCode(), ILLEGAL_DATA_ADDRESS);
|
||||
LOG_D("ERROR - ILLEGAL DATA ADDRESS\r\n");
|
||||
return response;
|
||||
}
|
||||
|
||||
// Do the writes
|
||||
sprintf(debugString, "Write : ");
|
||||
for (uint8_t i = 0; i < words; ++i)
|
||||
{
|
||||
request.get(7 + (i * 2), val); //data starts at byte 6 in request packet
|
||||
mbPV[addr + i] = val;
|
||||
sprintf(debugString + strlen(debugString), "%i ", mbPV[addr + i]);
|
||||
}
|
||||
LOG_V("%s\r\n", debugString);
|
||||
|
||||
// Set up response
|
||||
response.add(request.getServerID(), request.getFunctionCode(), addr, words);
|
||||
return response;
|
||||
}
|
||||
|
||||
// Server function to handle FC 0x17 (FC23)
|
||||
ModbusMessage FC23(ModbusMessage request)
|
||||
{
|
||||
//Serial.println(request);
|
||||
ModbusMessage response; // The Modbus message we are going to give back
|
||||
uint16_t read_addr = 0; // Start address for read
|
||||
uint16_t read_words = 0; // # of words requested for read
|
||||
uint16_t write_addr = 0; // Start address for write
|
||||
uint16_t write_words = 0; // total words to write
|
||||
uint8_t write_bytes = 0; // # of data bytes in write request
|
||||
uint16_t write_val = 0; // value to be written
|
||||
request.get(2, read_addr); // read address from request
|
||||
request.get(4, read_words); // read # of words from request
|
||||
request.get(6, write_addr); // read address from request
|
||||
request.get(8, write_words); // read # of words from request
|
||||
request.get(10, write_bytes); // read # of data bytes from request (seems redundant with # of words)
|
||||
char debugString[1000];
|
||||
|
||||
LOG_D("FC23 received: write %d @ %d, read %d @ %d\r\n", write_words, write_addr, read_words, read_addr);
|
||||
|
||||
// ERROR CHECKS
|
||||
// # of registers proper?
|
||||
if ((write_bytes != (write_words * 2)) // byte count in request must match # of words in request
|
||||
|| (write_words > 121) // can't fit more than this in the packet for FC23
|
||||
|| (read_words > 125)) // can't fit more than this in the response packet
|
||||
{ // Yes - send respective error response
|
||||
response.setError(request.getServerID(), request.getFunctionCode(), ILLEGAL_DATA_VALUE);
|
||||
LOG_D("ERROR - ILLEGAL DATA VALUE\r\n");
|
||||
return response;
|
||||
}
|
||||
// Address overflow?
|
||||
if (((write_addr + write_words) > MBPV_MAX) || ((read_addr + read_words) > MBPV_MAX))
|
||||
{ // Yes - send respective error response
|
||||
response.setError(request.getServerID(), request.getFunctionCode(), ILLEGAL_DATA_ADDRESS);
|
||||
LOG_D("ERROR - ILLEGAL DATA ADDRESS\r\n");
|
||||
return response;
|
||||
}
|
||||
|
||||
//WRITE SECTION - write is done before read for FC23
|
||||
// Do the writes
|
||||
sprintf(debugString, "Write: ");
|
||||
for (uint8_t i = 0; i < write_words; ++i)
|
||||
{
|
||||
request.get(11 + (i * 2), write_val); //data starts at byte 6 in request packet
|
||||
mbPV[write_addr + i] = write_val;
|
||||
sprintf(debugString + strlen(debugString), "%i ", mbPV[write_addr + i]);
|
||||
}
|
||||
LOG_V("%s\r\n", debugString);
|
||||
|
||||
// READ SECTION
|
||||
// Set up response
|
||||
sprintf(debugString, "Read: ");
|
||||
response.add(request.getServerID(), request.getFunctionCode(), (uint8_t)(read_words * 2));
|
||||
for (uint8_t i = 0; i < read_words; ++i)
|
||||
{
|
||||
// send increasing data values
|
||||
response.add((uint16_t)(mbPV[read_addr + i]));
|
||||
sprintf(debugString + strlen(debugString), "%i ", mbPV[read_addr + i]);
|
||||
}
|
||||
LOG_V("%s\r\n", debugString);
|
||||
|
||||
return response;
|
||||
}
|
10
Software/src/devboard/modbus/mbServerFCs.h
Normal file
10
Software/src/devboard/modbus/mbServerFCs.h
Normal file
|
@ -0,0 +1,10 @@
|
|||
#include "../../lib/eModbus-eModbus/ModbusServerRTU.h"
|
||||
|
||||
#define MBTCP_ID 21 // modbus TCP server ID
|
||||
#define MBPV_MAX 30000
|
||||
|
||||
|
||||
ModbusMessage FC03(ModbusMessage request);
|
||||
ModbusMessage FC06(ModbusMessage request);
|
||||
ModbusMessage FC16(ModbusMessage request);
|
||||
ModbusMessage FC23(ModbusMessage request);
|
|
@ -1,5 +1,5 @@
|
|||
#include "BYD-CAN.h"
|
||||
#include "../../ESP32CAN.h"
|
||||
#include "../devboard/can/ESP32CAN.h"
|
||||
#include "../lib/ThomasBarth-ESP32-CAN-Driver/CAN_config.h"
|
||||
|
||||
/* Do not change code below unless you are sure what you are doing */
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef BYD_CAN_H
|
||||
#define BYD_CAN_H
|
||||
#include <Arduino.h>
|
||||
#include "../../ESP32CAN.h"
|
||||
#include "../devboard/can/ESP32CAN.h"
|
||||
#include "../../USER_SETTINGS.h"
|
||||
|
||||
extern uint16_t SOC;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include "PYLON-CAN.h"
|
||||
#include "../../ESP32CAN.h"
|
||||
#include "../devboard/can/ESP32CAN.h"
|
||||
#include "../lib/ThomasBarth-ESP32-CAN-Driver/CAN_config.h"
|
||||
|
||||
#define SEND_0 //If defined, the messages will have ID ending with 0 (useful for some inverters)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef PYLON_CAN_H
|
||||
#define PYLON_CAN_H
|
||||
#include <Arduino.h>
|
||||
#include "../../ESP32CAN.h"
|
||||
#include "../devboard/can/ESP32CAN.h"
|
||||
#include "../../USER_SETTINGS.h"
|
||||
|
||||
extern uint16_t SOC;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include "SMA-CAN.h"
|
||||
#include "../../ESP32CAN.h"
|
||||
#include "../devboard/can/ESP32CAN.h"
|
||||
#include "../lib/ThomasBarth-ESP32-CAN-Driver/CAN_config.h"
|
||||
|
||||
//TODO, change CAN sending routine once confirmed that 500ms interval is OK for this battery type
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef SMA_CAN_H
|
||||
#define SMA_CAN_H
|
||||
#include <Arduino.h>
|
||||
#include "../../ESP32CAN.h"
|
||||
#include "../devboard/can/ESP32CAN.h"
|
||||
#include "../../USER_SETTINGS.h"
|
||||
|
||||
extern uint16_t SOC; //SOC%, 0-100.00 (0-10000)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include "SOFAR-CAN.h"
|
||||
#include "../../ESP32CAN.h"
|
||||
#include "../devboard/can/ESP32CAN.h"
|
||||
#include "../lib/ThomasBarth-ESP32-CAN-Driver/CAN_config.h"
|
||||
|
||||
/* This implementation of the SOFAR can protocol is halfway done. What's missing is implementing the inverter replies, all the CAN messages are listed, but the can sending is missing. */
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef SOFAR_CAN_H
|
||||
#define SOFAR_CAN_H
|
||||
#include <Arduino.h>
|
||||
#include "../../ESP32CAN.h"
|
||||
#include "../devboard/can/ESP32CAN.h"
|
||||
#include "../../USER_SETTINGS.h"
|
||||
|
||||
// These parameters need to be mapped for the inverter
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
#ifndef SOLAX_CAN_H
|
||||
#define SOLAX_CAN_H
|
||||
#include <Arduino.h>
|
||||
#include "../../ESP32CAN.h"
|
||||
#include "../../config.h"
|
||||
#include "../devboard/can/ESP32CAN.h"
|
||||
#include "../devboard/config.h"
|
||||
#include "../../USER_SETTINGS.h"
|
||||
|
||||
#include "../../src/lib/pierremolinaro-acan2515/ACAN2515.h"
|
||||
#include "../lib/pierremolinaro-acan2515/ACAN2515.h"
|
||||
extern ACAN2515 can;
|
||||
|
||||
extern uint16_t SOC;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue