Compare commits
54
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5196694e42 | ||
|
|
613e454fed | ||
|
|
1943533360 | ||
|
|
8d8a8f06ad | ||
|
|
fac325d423 | ||
|
|
cc0bdc1004 | ||
|
|
daff69550f | ||
|
|
ede29544d2 | ||
|
|
bc61a29bed | ||
|
|
b4483129c2 | ||
|
|
faf3bde214 | ||
|
|
94dc0d9a3f | ||
|
|
b2707067ac | ||
|
|
d2b4eb7f7e | ||
|
|
3f11266838 | ||
|
|
3b68bc1bcc | ||
|
|
5e02ab2292 | ||
|
|
8c7a18662e | ||
|
|
a06f14b63c | ||
|
|
483e09d1a2 | ||
|
|
c87e5f974a | ||
|
|
2bbba0ba13 | ||
|
|
cfeec975c1 | ||
|
|
98c1daa865 | ||
|
|
f8d7b25648 | ||
|
|
01ca3b4f15 | ||
|
|
f6aeaa79db | ||
|
|
6aa015a140 | ||
|
|
fc71934010 | ||
|
|
c151213325 | ||
|
|
a9b0a278ad | ||
|
|
a5c1d46d63 | ||
|
|
35b390ff24 | ||
|
|
269ce762a0 | ||
|
|
ed29e54d20 | ||
|
|
bdf405fb7b | ||
|
|
1b1ed838eb | ||
|
|
2de110b0b7 | ||
|
|
4eaf89154a | ||
|
|
830315cda0 | ||
|
|
24742fefcb | ||
|
|
fec60aacbb | ||
|
|
a352d6b98c | ||
|
|
941545ea7e | ||
|
|
0f109297e4 | ||
|
|
e63a2db9e5 | ||
|
|
bcf54f6698 | ||
|
|
3653b88d25 | ||
|
|
e5b42ace72 | ||
|
|
9ba540ec8c | ||
|
|
755e555611 | ||
|
|
a2ab1ebd45 | ||
|
|
4f4bdc70ae | ||
|
|
76ee3dc28f |
+3
-1
@@ -10,7 +10,9 @@ env:
|
||||
- PLATFORMIO_CI_SRC=src
|
||||
|
||||
install:
|
||||
- pip install wheel
|
||||
- pip install -U platformio
|
||||
- platformio platform install native
|
||||
- platformio update
|
||||
|
||||
addons:
|
||||
@@ -33,4 +35,4 @@ deploy:
|
||||
github_token: $GH_REPO_TOKEN
|
||||
on:
|
||||
repo: LibreSolar/charge-controller-firmware
|
||||
branch: master
|
||||
branch: main
|
||||
|
||||
@@ -2094,7 +2094,7 @@ PERLMOD_MAKEVAR_PREFIX =
|
||||
# C-preprocessor directives found in the sources and include files.
|
||||
# The default value is: YES.
|
||||
|
||||
ENABLE_PREPROCESSING = YES
|
||||
ENABLE_PREPROCESSING = NO
|
||||
|
||||
# If the MACRO_EXPANSION tag is set to YES, doxygen will expand all macro names
|
||||
# in the source code. If set to NO, only conditional compilation will be
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Libre Solar Charge Controller Firmware
|
||||
|
||||

|
||||

|
||||
|
||||
This repository contains the firmware for the different Libre Solar Charge Controllers based on [Zephyr RTOS](https://www.zephyrproject.org/).
|
||||
|
||||
@@ -8,20 +8,22 @@ Coding style is described [here](https://github.com/LibreSolar/coding-style).
|
||||
|
||||
## Development and release model
|
||||
|
||||
The `master` branch contains the latest release of the firmware plus some cherry-picked bug-fixes. So you can always pull this branch to get a stable and working firmware for your charge controller.
|
||||
The `main` branch contains the latest release of the firmware plus some cherry-picked bug-fixes. So you can always pull this branch to get a stable and working firmware for your charge controller.
|
||||
|
||||
New features are developed in the `develop` branch and merged into master after passing tests with multiple boards. The `develop` branch is changed frequently and may even be rebased to fix previous commits. Use this branch only if you want to try out most recent features and be aware that something might be broken.
|
||||
New features are developed in the `develop` branch and merged into `main` after passing tests with multiple boards. The `develop` branch is changed frequently and may even be rebased to fix previous commits. Use this branch only if you want to try out most recent features and be aware that something might be broken.
|
||||
|
||||
Releases are generated from master after significant changes have been made. The year is used as the major version number. The minor version number starts at zero and is increased for each release in that year.
|
||||
Releases are generated from `main` after significant changes have been made. The year is used as the major version number. The minor version number starts at zero and is increased for each release in that year.
|
||||
|
||||
## Supported devices
|
||||
|
||||
The software is configurable to support different charge controller PCBs with STM32F072, low-power STM32L072 or most recent STM32G431 MCUs:
|
||||
|
||||
- [Libre Solar MPPT 2420 HC](https://github.com/LibreSolar/mppt-2420-hc)
|
||||
- [Libre Solar MPPT 2420 LC](https://github.com/LibreSolar/mppt-2420-lc)
|
||||
- [Libre Solar MPPT 1210 HUS](https://github.com/LibreSolar/mppt-1210-hus)
|
||||
- [Libre Solar PWM 2420 LUS](https://github.com/LibreSolar/pwm-2420-lus)
|
||||
| Board | Default revision | Older revisions |
|
||||
|-----------------------------------------------------|------------------|-----------------|
|
||||
| [Libre Solar MPPT 2420 HC](https://github.com/LibreSolar/mppt-2420-hc) | 0.2 | 0.1 |
|
||||
| [Libre Solar MPPT 2420 LC](https://github.com/LibreSolar/mppt-2420-lc) | 0.10 | |
|
||||
| [Libre Solar MPPT 1210 HUS](https://github.com/LibreSolar/mppt-1210-hus) | 0.7 | 0.4 |
|
||||
| [Libre Solar PWM 2420 LUS](https://github.com/LibreSolar/pwm-2420-lus) | 0.3 | 0.2 |
|
||||
|
||||
## Building and flashing the firmware
|
||||
|
||||
@@ -41,7 +43,7 @@ It is suggested to use Visual Studio Code and PlatformIO for firmware developmen
|
||||
|
||||
2. Adjust configuration in `zephyr/prj.conf` if necessary.
|
||||
|
||||
3. Select the correct board in `platformio.ini` by removing the comment before the board name under `[platformio]` or create a file `custom.ini` with your personal settings.
|
||||
3. Select the correct board in `platformio.ini` by removing the comment before the board name under `[platformio]` or create a file `custom.ini` with your personal settings. PlatformIO is configured for latest revision of the boards (see table above). For older revisions adjust `platformio.ini` and the `.json` files in `boards` folder manually or use native Zephyr environment.
|
||||
|
||||
4. Connect the board via a programmer. See the Libre Solar website for [further project-agnostic instructions](http://libre.solar/docs/flashing).
|
||||
|
||||
@@ -65,9 +67,11 @@ The CMake entry point is in the `zephyr` subfolder, so you need to run `west bui
|
||||
|
||||
cd zephyr
|
||||
|
||||
Initial board selection (see `boards subfolder for correct names):
|
||||
Initial board selection (see `boards` subfolder for correct names):
|
||||
|
||||
west build -b <board-name>
|
||||
west build -b <board-name>@<revision>
|
||||
|
||||
The appended `@<revision>` specifies the board version according to the above table. It can be omitted if only a single board revision is available or if the default (most recent) version should be used. See also [here](https://docs.zephyrproject.org/latest/application/index.html#application-board-version) for more details regarding board revision handling in Zephyr.
|
||||
|
||||
Flash with specific debug probe (runner), e.g. J-Link:
|
||||
|
||||
@@ -82,40 +86,6 @@ Report of used memory (RAM and flash):
|
||||
west build -t rom_report
|
||||
west build -t ram_report
|
||||
|
||||
### Troubleshooting
|
||||
|
||||
#### Errors with STM32L072 MCU using OpenOCD
|
||||
|
||||
The standard OpenOCD settings sometimes fail to flash firmware to this MCU, which is used in the MPPT 1210 HUS and the PWM charge controller.
|
||||
|
||||
Try one of these workarounds:
|
||||
|
||||
1. Change OpenOCD settings to `adapter_khz 500` in line 70 in `~/.platformio/packages/tool-openocd/scripts/target/stm32l0.cfg`.
|
||||
|
||||
2. Use other tools or debug probes such as [STM32CubeProgrammer](https://www.st.com/en/development-tools/stm32cubeprog.html) or [Segger J-Link](https://www.segger.com/products/debug-probes/j-link/).
|
||||
|
||||
#### Connection failures
|
||||
|
||||
If flashing fails like this in PlatformIO:
|
||||
|
||||
```
|
||||
Error: init mode failed (unable to connect to the target)
|
||||
in procedure 'program'
|
||||
** OpenOCD init failed **
|
||||
shutdown command invoked
|
||||
```
|
||||
|
||||
or with ST-Link:
|
||||
|
||||
```bash
|
||||
$ st-flash write .pio/build/mppt-1210-hus-v0.4/firmware.bin 0x08000000
|
||||
st-flash 1.5.1
|
||||
2019-06-21T18:13:03 INFO common.c: Loading device parameters....
|
||||
2019-06-21T18:13:03 WARN common.c: unknown chip id! 0x5fa0004
|
||||
```
|
||||
|
||||
check the connection between the programmer (for example the ST-Link of the Nucleo board) and the charge controller.
|
||||
|
||||
## Firmware customization
|
||||
|
||||
This firmware is developed to allow easy customization for individual use-cases. Below options based on the Zephyr devicetree and Kconfig systems try to allow customization without changing any of the files tracked by git, so that a `git pull` does not lead to conflicts with local changes.
|
||||
@@ -126,17 +96,44 @@ In Zephyr, all hardware-specific configuration is described in the [devicetree](
|
||||
|
||||
The file `boards/arm/board_name/board_name.dts` contains the default devicetree specification (DTS) for a board. It is based on the DTS of the used MCU, which is included from the main Zephyr repository.
|
||||
|
||||
In order to overwrite the default configuration, so-called overlays can be used. An overlay file can be specified via the west command line. If it is stored as `board_name.overlay` in the `zephyr` subfolder, it will be recognized automatically when building the firmware for that board (also with PlatformIO).
|
||||
|
||||
In order to support older board versions (e.g. with changed pins), some overlays with appended version numbers are already existing as a template. Remove the version number (e.g. `_0v4`) to "activate" them.
|
||||
In order to overwrite the default devicetree specification, so-called overlays can be used. An overlay file can be specified via the west command line. If it is stored as `board_name.overlay` in the `zephyr` subfolder, it will be recognized automatically when building the firmware for that board (also with PlatformIO).
|
||||
|
||||
### Application firmware configuration
|
||||
|
||||
For configuration of the application-specific features, Zephyr uses the [Kconfig system](https://docs.zephyrproject.org/latest/guides/kconfig/index.html).
|
||||
|
||||
The configuration can be changed using `west build -t menuconfig` command or manually by changing the prj.conf file.
|
||||
The configuration can be changed using `west build -t menuconfig` command or manually by changing the prj.conf file (see `Kconfig` file for possible options).
|
||||
|
||||
Similar to DTS overlays, the Kconfig can also be customized for each board. Copy the default `prj.conf` to a file `prj_board_name.conf` to use this file instead of `prj.conf`. Please not that, in contrast to overlays, the board-specific Kconfig files are not **added** to the default `prj.conf`, but they **replace** the default configuration instead.
|
||||
Similar to DTS overlays, Kconfig can also be customized per board. Create a folder `zephyr/boards` and a file `board_name.conf` in that folder. The configuration from this file will be merged with the `prj.conf` automatically.
|
||||
|
||||
#### Change the battery type
|
||||
|
||||
By default, the charge controller is configured for maintainance-free VRLA gel battery (`CONFIG_BAT_TYPE_GEL`). Possible other pre-defined options are `CONFIG_BAT_TYPE_FLOODED`, `CONFIG_BAT_TYPE_AGM`, `CONFIG_BAT_TYPE_LFP`, `CONFIG_BAT_TYPE_NMC` and `CONFIG_BAT_TYPE_NMC_HV`.
|
||||
|
||||
The number of cells is automatically selected by Kconfig to get 12V nominal voltage. It can also be manually specified via `CONFIG_BAT_NUM_CELLS`.
|
||||
|
||||
To compile the firmware with default settings for 12V LiFePO4 batteries, add the following to `prj.conf` or the board-specific `.conf` file:
|
||||
|
||||
```
|
||||
CONFIG_BAT_TYPE_LFP=y
|
||||
CONFIG_BAT_NUM_CELLS=4
|
||||
```
|
||||
|
||||
#### Configure serial for ThingSet protocol
|
||||
|
||||
By default, the charge controller uses the serial interface in the UEXT connector for the [ThingSet protocol](https://libre.solar/thingset/). This allows to use WiFi modules with ESP32 without any firmware change.
|
||||
|
||||
Add the following configuration if you prefer to use the serial of the LS.one port or the additional debug RX/TX pins present on most boards:
|
||||
|
||||
```
|
||||
CONFIG_UEXT_SERIAL_THINGSET=n
|
||||
```
|
||||
|
||||
To disable regular data publication in one-second interval on ThingSet serial at startup add the following configuration:
|
||||
|
||||
```
|
||||
CONFIG_THINGSET_SERIAL_PUB_DEFAULT=n
|
||||
```
|
||||
|
||||
### Custom functions (separate C/C++ files)
|
||||
|
||||
@@ -175,3 +172,37 @@ PlatformIO integrates cppcheck and clangtidy. The following command is an exampl
|
||||
- [Charge controller basic concepts](docs/charger-concepts.md)
|
||||
|
||||
- [Firmware module overview](docs/module-overview.md)
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Errors with STM32L072 MCU using OpenOCD
|
||||
|
||||
The standard OpenOCD settings sometimes fail to flash firmware to this MCU, which is used in the MPPT 1210 HUS and the PWM charge controller.
|
||||
|
||||
Try one of these workarounds:
|
||||
|
||||
1. Change OpenOCD settings to `adapter_khz 500` in line 70 in `~/.platformio/packages/tool-openocd/scripts/target/stm32l0.cfg`.
|
||||
|
||||
2. Use other tools or debug probes such as [STM32CubeProgrammer](https://www.st.com/en/development-tools/stm32cubeprog.html) or [Segger J-Link](https://www.segger.com/products/debug-probes/j-link/).
|
||||
|
||||
### Connection failures
|
||||
|
||||
If flashing fails like this in PlatformIO:
|
||||
|
||||
```
|
||||
Error: init mode failed (unable to connect to the target)
|
||||
in procedure 'program'
|
||||
** OpenOCD init failed **
|
||||
shutdown command invoked
|
||||
```
|
||||
|
||||
or with ST-Link:
|
||||
|
||||
```bash
|
||||
$ st-flash write .pio/build/mppt-1210-hus-v0.4/firmware.bin 0x08000000
|
||||
st-flash 1.5.1
|
||||
2019-06-21T18:13:03 INFO common.c: Loading device parameters....
|
||||
2019-06-21T18:13:03 WARN common.c: unknown chip id! 0x5fa0004
|
||||
```
|
||||
|
||||
check the connection between the programmer (for example the ST-Link of the Nucleo board) and the charge controller.
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
if(CONFIG_PINMUX)
|
||||
zephyr_library()
|
||||
zephyr_library_sources(pinmux.c)
|
||||
zephyr_library_include_directories(${ZEPHYR_BASE}/drivers)
|
||||
endif()
|
||||
|
||||
zephyr_include_directories(.)
|
||||
@@ -1,5 +1,5 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# Copyright (c) 2019 Martin Jäger / Libre Solar
|
||||
|
||||
config BOARD_MPPT_1210_HUS
|
||||
bool "Libre Solar MPPT 1210 HUS"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# Copyright (c) 2019 Martin Jäger / Libre Solar
|
||||
|
||||
if BOARD_MPPT_1210_HUS
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
board_runner_args(jlink "--device=STM32L072RZ" "--speed=4000")
|
||||
board_runner_args(jlink "--device=STM32L072RZ" "--speed=4000" "--reset-after-load")
|
||||
|
||||
include(${ZEPHYR_BASE}/boards/common/openocd.board.cmake)
|
||||
include(${ZEPHYR_BASE}/boards/common/jlink.board.cmake)
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* Copyright (c) 2020 Martin Jäger / Libre Solar
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/dts-v1/;
|
||||
#include <st/l0/stm32l072Xz.dtsi>
|
||||
#include <st/l0/stm32l072r(b-z)tx-pinctrl.dtsi>
|
||||
|
||||
/ {
|
||||
model = "Libre Solar MPPT 1210 HUS";
|
||||
@@ -15,19 +16,10 @@
|
||||
compatible = "charge-controller";
|
||||
|
||||
type = "MPPT 1210 HUS";
|
||||
version-str = "v0.7.1";
|
||||
version-num = <7>; // minor version .1 includes no HW change
|
||||
|
||||
hs-voltage-max = <40>;
|
||||
ls-voltage-max = <16>;
|
||||
};
|
||||
|
||||
dcdc {
|
||||
compatible = "dcdc";
|
||||
timer = <&timers3>;
|
||||
pwm-frequency = <50000>;
|
||||
pwm-deadtime = <230>;
|
||||
current-max = <10>;
|
||||
dcdc-current-max = <10>;
|
||||
};
|
||||
|
||||
chosen {
|
||||
@@ -40,27 +32,27 @@
|
||||
leds {
|
||||
compatible = "charlieplexing-leds";
|
||||
gpios = <&gpiob 13 GPIO_ACTIVE_HIGH>, /* Pin A */
|
||||
<&gpiob 2 GPIO_ACTIVE_HIGH>, /* Pin C */
|
||||
<&gpiob 14 GPIO_ACTIVE_HIGH>; /* Pin B */
|
||||
<&gpiob 14 GPIO_ACTIVE_HIGH>, /* Pin B */
|
||||
<&gpiob 2 GPIO_ACTIVE_HIGH>; /* Pin C */
|
||||
|
||||
soc_1 {
|
||||
states = <1 0 2>;
|
||||
states = <1 2 0>; /* LED1 */
|
||||
};
|
||||
|
||||
soc_2 {
|
||||
states = <0 1 2>;
|
||||
states = <0 2 1>; /* LED2 */
|
||||
};
|
||||
|
||||
soc_3 {
|
||||
states = <1 2 0>;
|
||||
};
|
||||
|
||||
rxtx {
|
||||
states = <2 1 0>;
|
||||
states = <1 0 2>; /* LED3 */
|
||||
};
|
||||
|
||||
load {
|
||||
states = <2 0 1>;
|
||||
states = <2 0 1>; /* LED4 */
|
||||
};
|
||||
|
||||
rxtx {
|
||||
states = <2 1 0>; /* LED5 */
|
||||
};
|
||||
};
|
||||
|
||||
@@ -156,21 +148,25 @@
|
||||
};
|
||||
|
||||
&usart1 {
|
||||
pinctrl-0 = <&usart1_tx_pa9 &usart1_rx_pa10>;
|
||||
current-speed = <115200>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&usart2 {
|
||||
pinctrl-0 = <&usart2_tx_pa2 &usart2_rx_pa3>;
|
||||
current-speed = <115200>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&i2c1 {
|
||||
pinctrl-0 = <&i2c1_scl_pb6 &i2c1_sda_pb7>;
|
||||
clock-frequency = <I2C_BITRATE_FAST>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&spi1 {
|
||||
pinctrl-0 = <&spi1_sck_pb3 &spi1_miso_pb4 &spi1_mosi_pb5>;
|
||||
cs-gpios = <&gpioa 15 GPIO_ACTIVE_LOW>;
|
||||
status = "okay";
|
||||
};
|
||||
@@ -179,6 +175,17 @@
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&timers3 {
|
||||
status = "okay";
|
||||
|
||||
halfbridge {
|
||||
compatible = "half-bridge";
|
||||
pinctrl-0 = <&tim3_ch3_pb0 &tim3_ch4_pb1>;
|
||||
frequency = <50000>;
|
||||
deadtime = <230>;
|
||||
};
|
||||
};
|
||||
|
||||
&eeprom {
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# empty (using defaults from _defconfig file)
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* Copyright (c) 2020 Martin Jäger / Libre Solar
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/ {
|
||||
@@ -0,0 +1,3 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# empty (using defaults from _defconfig file)
|
||||
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/ {
|
||||
pcb {
|
||||
version-str = "v0.7.1";
|
||||
version-num = <7>; // minor version .1 includes no HW change
|
||||
};
|
||||
};
|
||||
@@ -1,59 +0,0 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Copyright (c) 2019 Martin Jäger / Libre Solar
|
||||
*/
|
||||
|
||||
#include <kernel.h>
|
||||
#include <device.h>
|
||||
#include <init.h>
|
||||
#include <drivers/pinmux.h>
|
||||
#include <sys/sys_io.h>
|
||||
|
||||
#include <pinmux/stm32/pinmux_stm32.h>
|
||||
|
||||
#define STM32L0_PINMUX_FUNC_PB0_TIM3_CH3 \
|
||||
(STM32_PINMUX_ALT_FUNC_2 | STM32_PUSHPULL_NOPULL)
|
||||
#define STM32L0_PINMUX_FUNC_PB1_TIM3_CH4 \
|
||||
(STM32_PINMUX_ALT_FUNC_2 | STM32_PUSHPULL_NOPULL)
|
||||
|
||||
static const struct pin_config pinconf[] = {
|
||||
/* TIM3_CH3 and TIM3_CH4 */
|
||||
{STM32_PIN_PB0, STM32L0_PINMUX_FUNC_PB0_TIM3_CH3},
|
||||
{STM32_PIN_PB1, STM32L0_PINMUX_FUNC_PB1_TIM3_CH4},
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(usart1), okay) && CONFIG_SERIAL
|
||||
{STM32_PIN_PA9, STM32L0_PINMUX_FUNC_PA9_USART1_TX},
|
||||
{STM32_PIN_PA10, STM32L0_PINMUX_FUNC_PA10_USART1_RX},
|
||||
#endif /* CONFIG_SERIAL || UART_CONSOLE, usart1 */
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(usart2), okay) && CONFIG_SERIAL
|
||||
{STM32_PIN_PA2, STM32L0_PINMUX_FUNC_PA2_USART2_TX},
|
||||
{STM32_PIN_PA3, STM32L0_PINMUX_FUNC_PA3_USART2_RX},
|
||||
#endif /* CONFIG_SERIAL, usart2 */
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c1), okay) && CONFIG_I2C
|
||||
{STM32_PIN_PB6, STM32L0_PINMUX_FUNC_PB6_I2C1_SCL},
|
||||
{STM32_PIN_PB7, STM32L0_PINMUX_FUNC_PB7_I2C1_SDA},
|
||||
#endif /* CONFIG_I2C, i2c1 */
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(spi1), okay) && CONFIG_SPI
|
||||
{STM32_PIN_PB3, STM32L0_PINMUX_FUNC_PB3_SPI1_SCK},
|
||||
{STM32_PIN_PB4, STM32L0_PINMUX_FUNC_PB4_SPI1_MISO},
|
||||
{STM32_PIN_PB5, STM32L0_PINMUX_FUNC_PB5_SPI1_MOSI},
|
||||
#endif /* CONFIG_SPI, spi1 */
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(adc1), okay) && CONFIG_ADC
|
||||
{STM32_PIN_PA0, STM32L0_PINMUX_FUNC_PA0_ADC_IN0},
|
||||
#endif /* CONFIG_ADC, adc1 */
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(dac1), okay) && CONFIG_DAC
|
||||
{STM32_PIN_PA4, STM32L0_PINMUX_FUNC_PA4_DAC_OUT1},
|
||||
#endif /* CONFIG_DAC, dac1 */
|
||||
};
|
||||
|
||||
static int pinmux_stm32_init(const struct device *port)
|
||||
{
|
||||
ARG_UNUSED(port);
|
||||
|
||||
stm32_setup_pins(pinconf, ARRAY_SIZE(pinconf));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
SYS_INIT(pinmux_stm32_init, PRE_KERNEL_1,
|
||||
CONFIG_PINMUX_STM32_DEVICE_INITIALIZATION_PRIORITY);
|
||||
@@ -0,0 +1,6 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
board_check_revision(
|
||||
FORMAT MAJOR.MINOR.PATCH
|
||||
DEFAULT_REVISION 0.7.1
|
||||
)
|
||||
@@ -1,9 +0,0 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
if(CONFIG_PINMUX)
|
||||
zephyr_library()
|
||||
zephyr_library_sources(pinmux.c)
|
||||
zephyr_library_include_directories(${ZEPHYR_BASE}/drivers)
|
||||
endif()
|
||||
|
||||
zephyr_include_directories(.)
|
||||
@@ -1,5 +1,5 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# Copyright (c) 2020 Martin Jäger / Libre Solar
|
||||
|
||||
config BOARD_MPPT_2420_HC
|
||||
bool "Libre Solar MPPT 2420 HC"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# Copyright (c) 2020 Martin Jäger / Libre Solar
|
||||
|
||||
if BOARD_MPPT_2420_HC
|
||||
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* Copyright (c) 2020 Martin Jäger / Libre Solar
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/dts-v1/;
|
||||
#include <st/g4/stm32g431Xb.dtsi>
|
||||
#include <st/g4/stm32g431c(6-8-b)tx-pinctrl.dtsi>
|
||||
|
||||
/ {
|
||||
model = "Libre Solar MPPT 2420 HC";
|
||||
@@ -15,19 +16,10 @@
|
||||
compatible = "charge-controller";
|
||||
|
||||
type = "MPPT 2420 HC";
|
||||
version-str = "v0.1";
|
||||
version-num = <1>;
|
||||
|
||||
hs-voltage-max = <80>;
|
||||
hs-voltage-max = <90>;
|
||||
ls-voltage-max = <32>;
|
||||
};
|
||||
|
||||
dcdc {
|
||||
compatible = "dcdc";
|
||||
timer = <&timers1>;
|
||||
pwm-frequency = <70000>;
|
||||
pwm-deadtime = <300>;
|
||||
current-max = <20>;
|
||||
dcdc-current-max = <20>;
|
||||
};
|
||||
|
||||
chosen {
|
||||
@@ -37,23 +29,20 @@
|
||||
zephyr,flash = &flash0;
|
||||
};
|
||||
|
||||
/* temporary solution w/o using RGB LED */
|
||||
/* temporary solution only using green color LED */
|
||||
leds {
|
||||
compatible = "charlieplexing-leds";
|
||||
gpios = <&gpiob 14 GPIO_ACTIVE_LOW>, /* red */
|
||||
<&gpiob 15 GPIO_ACTIVE_LOW>, /* green */
|
||||
<&gpiob 9 GPIO_ACTIVE_LOW>; /* blue */
|
||||
gpios = <&gpioa 6 GPIO_ACTIVE_LOW>, /* chg red */
|
||||
<&gpioc 14 GPIO_ACTIVE_LOW>, /* chg green */
|
||||
<&gpiob 9 GPIO_ACTIVE_LOW>, /* dis red */
|
||||
<&gpioc 15 GPIO_ACTIVE_LOW>; /* dis green */
|
||||
|
||||
pwr {
|
||||
states = <1 0 0>;
|
||||
states = <0 1 0 0>;
|
||||
};
|
||||
|
||||
load {
|
||||
states = <0 1 0>;
|
||||
};
|
||||
|
||||
rxtx {
|
||||
states = <0 0 1>;
|
||||
states = <0 0 0 1>;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -126,17 +115,6 @@
|
||||
};
|
||||
};
|
||||
|
||||
soc {
|
||||
dac1: dac@50000800 {
|
||||
compatible = "st,stm32-dac";
|
||||
reg = <0x50000800 0x400>;
|
||||
clocks = <&rcc STM32_CLOCK_BUS_AHB2 0x00010000>;
|
||||
status = "disabled";
|
||||
label = "DAC_1";
|
||||
#io-channel-cells = <1>;
|
||||
};
|
||||
};
|
||||
|
||||
aliases {
|
||||
uart-dbg = &usart1;
|
||||
uart-uext = &usart2;
|
||||
@@ -146,78 +124,62 @@
|
||||
};
|
||||
|
||||
&usart1 {
|
||||
pinctrl-0 = <&usart1_tx_pa9 &usart1_rx_pa10>;
|
||||
current-speed = <115200>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&usart2 {
|
||||
pinctrl-0 = <&usart2_tx_pa2 &usart2_rx_pa3>;
|
||||
current-speed = <115200>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&i2c1 {
|
||||
pinctrl-0 = <&i2c1_scl_pa15 &i2c1_sda_pb7>;
|
||||
clock-frequency = <I2C_BITRATE_STANDARD>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&spi1 {
|
||||
pinctrl-0 = <&spi1_sck_pb3 &spi1_miso_pb4 &spi1_mosi_pb5>;
|
||||
cs-gpios = <&gpiob 6 GPIO_ACTIVE_LOW>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&usb {
|
||||
&spi2 {
|
||||
pinctrl-0 = <&spi2_sck_pb13 &spi2_miso_pb14 &spi2_mosi_pb15>;
|
||||
cs-gpios = <&gpioa 5 GPIO_ACTIVE_LOW>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&rtc {
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&flash0 {
|
||||
/*
|
||||
* For more information, see:
|
||||
* http://docs.zephyrproject.org/latest/guides/dts/index.html#flash-partitions
|
||||
*/
|
||||
partitions {
|
||||
compatible = "fixed-partitions";
|
||||
#address-cells = <1>;
|
||||
#size-cells = <1>;
|
||||
};
|
||||
};
|
||||
|
||||
&iwdg {
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
/* TIM8 (CH2) used for charge pump, but is currently not working with original Zephyr driver */
|
||||
&timers1 {
|
||||
status = "okay";
|
||||
|
||||
halfbridge {
|
||||
compatible = "half-bridge";
|
||||
pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pc13>;
|
||||
frequency = <70000>;
|
||||
deadtime = <300>;
|
||||
};
|
||||
};
|
||||
|
||||
/* TIM8 (CH2) used for charge pump */
|
||||
&timers8 {
|
||||
status = "okay";
|
||||
pwm8: pwm {
|
||||
status = "okay";
|
||||
st,prescaler = <149>; /* reduce counter clock frequency to 1 MHz */
|
||||
pinctrl-0 = <&tim8_ch2_pb8>;
|
||||
};
|
||||
};
|
||||
|
||||
&i2c1 {
|
||||
status = "okay";
|
||||
clock-frequency = <I2C_BITRATE_STANDARD>;
|
||||
eeprom@50 {
|
||||
// Microchip 24AA32A
|
||||
compatible = "atmel,at24";
|
||||
reg = <0x50>;
|
||||
label = "EEPROM_0";
|
||||
size = <32768>;
|
||||
pagesize = <32>;
|
||||
address-width = <16>;
|
||||
/*
|
||||
* timeout of 5 ms as suggested in datasheet seems too optimistic
|
||||
* (several write errors occured during testing)
|
||||
*/
|
||||
timeout = <7>;
|
||||
};
|
||||
};
|
||||
|
||||
/*
|
||||
// This requires https://github.com/zephyrproject-rtos/zephyr/pull/22832
|
||||
&can1 {
|
||||
pinctrl-0 = <&fdcan1_rx_pa11 &fdcan1_tx_pa12>;
|
||||
bus-speed = <250000>;
|
||||
bus-speed-data = <250000>;
|
||||
sjw = <1>;
|
||||
@@ -226,4 +188,3 @@
|
||||
sample-point-data = <875>;
|
||||
status = "okay";
|
||||
};
|
||||
*/
|
||||
@@ -0,0 +1,5 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
CONFIG_I2C=y
|
||||
CONFIG_EEPROM=y
|
||||
CONFIG_EEPROM_AT24=y
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/ {
|
||||
pcb {
|
||||
version-str = "v0.1";
|
||||
version-num = <1>;
|
||||
};
|
||||
|
||||
/* temporary solution w/o using RGB LED */
|
||||
leds {
|
||||
compatible = "charlieplexing-leds";
|
||||
gpios = <&gpiob 14 GPIO_ACTIVE_LOW>, /* red */
|
||||
<&gpiob 15 GPIO_ACTIVE_LOW>, /* green */
|
||||
<&gpiob 9 GPIO_ACTIVE_LOW>; /* blue */
|
||||
|
||||
pwr {
|
||||
states = <1 0 0>;
|
||||
};
|
||||
|
||||
load {
|
||||
states = <0 1 0>;
|
||||
};
|
||||
|
||||
rxtx {
|
||||
states = <0 0 1>;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
&i2c1 {
|
||||
eeprom@50 {
|
||||
// Microchip 24AA32A
|
||||
compatible = "atmel,at24";
|
||||
reg = <0x50>;
|
||||
label = "EEPROM_0";
|
||||
size = <32768>;
|
||||
pagesize = <32>;
|
||||
address-width = <16>;
|
||||
/*
|
||||
* timeout of 5 ms as suggested in datasheet seems too optimistic
|
||||
* (several write errors occured during testing)
|
||||
*/
|
||||
timeout = <7>;
|
||||
};
|
||||
};
|
||||
|
||||
&spi1 {
|
||||
pinctrl-0 = <&spi1_sck_pb3 &spi1_miso_pb4 &spi1_mosi_pb5>;
|
||||
cs-gpios = <&gpiob 6 GPIO_ACTIVE_LOW>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&timers1 {
|
||||
halfbridge {
|
||||
pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>;
|
||||
};
|
||||
};
|
||||
|
||||
&spi2 {
|
||||
status = "disabled";
|
||||
|
||||
/delete-node/ pinctrl-0;
|
||||
/delete-node/ cs-gpios;
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
CONFIG_FLASH=y
|
||||
CONFIG_FLASH_PAGE_LAYOUT=y
|
||||
CONFIG_FLASH_LOG_LEVEL_WRN=y
|
||||
#CONFIG_MPU_ALLOW_FLASH_WRITE=y
|
||||
|
||||
CONFIG_SPI=y
|
||||
CONFIG_SPI_NOR=y
|
||||
CONFIG_SPI_NOR_SFDP_RUNTIME=y
|
||||
CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE=4096
|
||||
|
||||
CONFIG_NVS=y
|
||||
CONFIG_NVS_LOG_LEVEL_WRN=y
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/ {
|
||||
pcb {
|
||||
version-str = "v0.2";
|
||||
version-num = <2>;
|
||||
};
|
||||
|
||||
chosen {
|
||||
zephyr,code-partition = &slot0_partition;
|
||||
};
|
||||
};
|
||||
|
||||
&spi2 {
|
||||
w25q80dv: w25q80dv@0 {
|
||||
compatible = "jedec,spi-nor";
|
||||
label = "W25Q80DV";
|
||||
reg = <0>;
|
||||
spi-max-frequency = <4000000>;
|
||||
size = <0x800000>; /* bits */
|
||||
has-dpd;
|
||||
t-enter-dpd = <4000>;
|
||||
t-exit-dpd = <25000>;
|
||||
jedec-id = [ef 40 14];
|
||||
};
|
||||
};
|
||||
|
||||
/*
|
||||
* Below partition layout allows to use MCUboot if CONFIG_BOOTLOADER_MCUBOOT is enabled. Otherwise,
|
||||
* the code will just use the entire available flash of the MCU.
|
||||
*/
|
||||
&flash0 {
|
||||
partitions {
|
||||
compatible = "fixed-partitions";
|
||||
#address-cells = <1>;
|
||||
#size-cells = <1>;
|
||||
|
||||
/* bootloader slot: 32 KiB */
|
||||
boot_partition: partition@0 {
|
||||
label = "mcuboot";
|
||||
reg = <0x00000000 0x8000>;
|
||||
read-only;
|
||||
};
|
||||
|
||||
/* application image slot: 96 KiB */
|
||||
slot0_partition: partition@8000 {
|
||||
label = "image-0";
|
||||
reg = <0x00008000 0x18000>;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
&w25q80dv {
|
||||
partitions {
|
||||
compatible = "fixed-partitions";
|
||||
#address-cells = <1>;
|
||||
#size-cells = <1>;
|
||||
|
||||
/* backup slot: 96 KiB */
|
||||
slot1_partition: partition@0 {
|
||||
label = "image-1";
|
||||
reg = <0x00000000 0x18000>;
|
||||
};
|
||||
|
||||
/* swap slot: 96 KiB */
|
||||
scratch_partition: partition@18000 {
|
||||
label = "image-scratch";
|
||||
reg = <0x00018000 0x00018000>;
|
||||
};
|
||||
|
||||
/* remaining 832 KiB of SPI flash used as storage */
|
||||
storage_partition: partition@30000 {
|
||||
label = "storage";
|
||||
reg = <0x00030000 0x000D0000>;
|
||||
};
|
||||
};
|
||||
};
|
||||
@@ -58,15 +58,10 @@ CONFIG_ARM_MPU=y
|
||||
|
||||
CONFIG_PWM=y
|
||||
|
||||
CONFIG_I2C=y
|
||||
CONFIG_EEPROM=y
|
||||
CONFIG_EEPROM_AT24=y
|
||||
|
||||
# don't enable CAN by default until PR #22832 is merged to upstream Zephyr
|
||||
#CONFIG_CAN=y
|
||||
#CONFIG_THINGSET_CAN=y
|
||||
CONFIG_CAN=y
|
||||
CONFIG_THINGSET_CAN=y
|
||||
|
||||
CONFIG_ISOTP=y
|
||||
CONFIG_ISOTP_RX_BUF_COUNT=1
|
||||
CONFIG_ISOTP_RX_BUF_COUNT=2
|
||||
CONFIG_ISOTP_RX_BUF_SIZE=100
|
||||
CONFIG_ISOTP_RX_SF_FF_BUF_COUNT=2
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Copyright (c) 2020 Martin Jäger / Libre Solar
|
||||
*/
|
||||
|
||||
#include <kernel.h>
|
||||
#include <device.h>
|
||||
#include <init.h>
|
||||
#include <drivers/pinmux.h>
|
||||
#include <sys/sys_io.h>
|
||||
|
||||
#include <pinmux/stm32/pinmux_stm32.h>
|
||||
|
||||
/* this should normally go to drivers/pinmux/stm32/pinmux_stm32g4x.h */
|
||||
#define STM32G4X_PINMUX_FUNC_PB8_PWM8_CH2 \
|
||||
(STM32_PINMUX_ALT_FUNC_10 | STM32_PUSHPULL_NOPULL)
|
||||
|
||||
#define STM32G4X_PINMUX_FUNC_PA8_TIM1_CH1 \
|
||||
(STM32_PINMUX_ALT_FUNC_6 | STM32_PUSHPULL_NOPULL)
|
||||
#define STM32G4X_PINMUX_FUNC_PB13_TIM1_CH1N \
|
||||
(STM32_PINMUX_ALT_FUNC_6 | STM32_PUSHPULL_NOPULL)
|
||||
|
||||
static const struct pin_config pinconf[] = {
|
||||
/* USART1 */
|
||||
{STM32_PIN_PA9, STM32G4X_PINMUX_FUNC_PA9_USART1_TX},
|
||||
{STM32_PIN_PA10, STM32G4X_PINMUX_FUNC_PA10_USART1_RX},
|
||||
/* TIM1_CH1 and TIM1_CH1N */
|
||||
{STM32_PIN_PA8, STM32G4X_PINMUX_FUNC_PA8_TIM1_CH1},
|
||||
{STM32_PIN_PB13, STM32G4X_PINMUX_FUNC_PB13_TIM1_CH1N},
|
||||
/* PWM for charge pump */
|
||||
{STM32_PIN_PB8, STM32G4X_PINMUX_FUNC_PB8_PWM8_CH2},
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(uart2), okay) && CONFIG_SERIAL
|
||||
{STM32_PIN_PA2, STM32G4X_PINMUX_FUNC_PA2_USART2_TX},
|
||||
{STM32_PIN_PA3, STM32G4X_PINMUX_FUNC_PA3_USART2_RX},
|
||||
#endif
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c1), okay) && CONFIG_I2C
|
||||
{STM32_PIN_PA15, STM32G4X_PINMUX_FUNC_PA15_I2C1_SCL},
|
||||
{STM32_PIN_PB7, STM32G4X_PINMUX_FUNC_PB7_I2C1_SDA},
|
||||
#endif
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(spi1), okay) && CONFIG_SPI
|
||||
{STM32_PIN_PB3, STM32G4X_PINMUX_FUNC_PB3_SPI1_SCK},
|
||||
{STM32_PIN_PB4, STM32G4X_PINMUX_FUNC_PB4_SPI1_MISO},
|
||||
{STM32_PIN_PB5, STM32G4X_PINMUX_FUNC_PB5_SPI1_MOSI},
|
||||
#endif
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(can1), okay) && CONFIG_CAN
|
||||
{STM32_PIN_PA11, STM32G4X_PINMUX_FUNC_PA11_FDCAN1_RX},
|
||||
{STM32_PIN_PA12, STM32G4X_PINMUX_FUNC_PA12_FDCAN1_TX},
|
||||
#endif
|
||||
};
|
||||
|
||||
static int pinmux_stm32_init(const struct device *port)
|
||||
{
|
||||
ARG_UNUSED(port);
|
||||
|
||||
stm32_setup_pins(pinconf, ARRAY_SIZE(pinconf));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
SYS_INIT(pinmux_stm32_init, PRE_KERNEL_1,
|
||||
CONFIG_PINMUX_STM32_DEVICE_INITIALIZATION_PRIORITY);
|
||||
@@ -0,0 +1,3 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
board_check_revision(FORMAT MAJOR.MINOR.PATCH)
|
||||
@@ -1,9 +0,0 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
if(CONFIG_PINMUX)
|
||||
zephyr_library()
|
||||
zephyr_library_sources(pinmux.c)
|
||||
zephyr_library_include_directories(${ZEPHYR_BASE}/drivers)
|
||||
endif()
|
||||
|
||||
zephyr_include_directories(.)
|
||||
@@ -1,5 +1,5 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# Copyright (c) 2019 Martin Jäger / Libre Solar
|
||||
|
||||
config BOARD_MPPT_2420_HPX
|
||||
bool "Libre Solar MPPT 2420 HPX"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# Copyright (c) 2019 Martin Jäger / Libre Solar
|
||||
|
||||
if BOARD_MPPT_2420_HPX
|
||||
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* Copyright (c) 2020 Martin Jäger / Libre Solar
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/dts-v1/;
|
||||
#include <st/g4/stm32g431Xb.dtsi>
|
||||
#include <st/g4/stm32g431r(6-8-b)tx-pinctrl.dtsi>
|
||||
|
||||
/ {
|
||||
model = "Libre Solar MPPT 2420 HPX";
|
||||
@@ -20,14 +21,7 @@
|
||||
|
||||
hs-voltage-max = <80>;
|
||||
ls-voltage-max = <32>;
|
||||
};
|
||||
|
||||
dcdc {
|
||||
compatible = "dcdc";
|
||||
timer = <&timers1>;
|
||||
pwm-frequency = <70000>;
|
||||
pwm-deadtime = <300>;
|
||||
current-max = <20>;
|
||||
dcdc-current-max = <20>;
|
||||
};
|
||||
|
||||
chosen {
|
||||
@@ -68,8 +62,8 @@
|
||||
pwms = <&pwm8 1 200000 PWM_POLARITY_NORMAL>; /* 200 us period = 5 kHz */
|
||||
};
|
||||
|
||||
hv-ext-sense {
|
||||
label = "HV external sense and switch";
|
||||
hv-out {
|
||||
label = "HV output sense and enable switch";
|
||||
gpios = <&gpiob 12 GPIO_ACTIVE_LOW>;
|
||||
};
|
||||
};
|
||||
@@ -142,38 +136,6 @@
|
||||
};
|
||||
};
|
||||
|
||||
soc {
|
||||
// ADC nodes not yet provided by Zephyr, but needed for adc-gain compatibles
|
||||
adc1: adc@50000000 {
|
||||
compatible = "st,stm32-adc";
|
||||
reg = <0x50000000 0x400>;
|
||||
clocks = <&rcc STM32_CLOCK_BUS_AHB2 0x00002000>;
|
||||
interrupts = <18 0>;
|
||||
status = "disabled";
|
||||
label = "ADC_1";
|
||||
#io-channel-cells = <1>;
|
||||
};
|
||||
|
||||
adc2: adc@50000100 {
|
||||
compatible = "st,stm32-adc";
|
||||
reg = <0x50000100 0x400>;
|
||||
clocks = <&rcc STM32_CLOCK_BUS_AHB2 0x00002000>;
|
||||
interrupts = <18 0>;
|
||||
status = "disabled";
|
||||
label = "ADC_2";
|
||||
#io-channel-cells = <1>;
|
||||
};
|
||||
|
||||
dac1: dac@50000800 {
|
||||
compatible = "st,stm32-dac";
|
||||
reg = <0x50000800 0x400>;
|
||||
clocks = <&rcc STM32_CLOCK_BUS_AHB2 0x00010000>;
|
||||
status = "disabled";
|
||||
label = "DAC_1";
|
||||
#io-channel-cells = <1>;
|
||||
};
|
||||
};
|
||||
|
||||
aliases {
|
||||
uart-dbg = &usart1;
|
||||
uart-uext = &usart3;
|
||||
@@ -183,28 +145,28 @@
|
||||
};
|
||||
|
||||
&usart1 {
|
||||
pinctrl-0 = <&usart1_tx_pa9 &usart1_rx_pa10>;
|
||||
current-speed = <115200>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&usart3 {
|
||||
pinctrl-0 = <&usart3_tx_pc10 &usart3_rx_pc11>;
|
||||
current-speed = <115200>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&i2c1 {
|
||||
pinctrl-0 = <&i2c1_scl_pa15 &i2c1_sda_pb7>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&spi1 {
|
||||
pinctrl-0 = <&spi1_sck_pb3 &spi1_miso_pb4 &spi1_mosi_pb5>;
|
||||
cs-gpios = <&gpiod 2 GPIO_ACTIVE_LOW>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&usb {
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&rtc {
|
||||
status = "okay";
|
||||
};
|
||||
@@ -231,23 +193,38 @@
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&timers1 {
|
||||
status = "okay";
|
||||
|
||||
halfbridge {
|
||||
compatible = "half-bridge";
|
||||
pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>;
|
||||
frequency = <70000>;
|
||||
deadtime = <300>;
|
||||
};
|
||||
};
|
||||
|
||||
/* TIM3 (CH2) used for PWM power switch */
|
||||
&timers3 {
|
||||
status = "okay";
|
||||
pwm3: pwm {
|
||||
status = "okay";
|
||||
pinctrl-0 = <&tim3_ch2_pc7>;
|
||||
};
|
||||
};
|
||||
|
||||
/* TIM8 (CH1) used for charge pump, but is currently not working with original Zephyr driver */
|
||||
/* TIM8 (CH1) used for charge pump */
|
||||
&timers8 {
|
||||
status = "okay";
|
||||
pwm8: pwm {
|
||||
status = "okay";
|
||||
st,prescaler = <149>; /* reduce counter clock frequency to 1 MHz */
|
||||
pinctrl-0 = <&tim8_ch1_pc6>;
|
||||
};
|
||||
};
|
||||
|
||||
&i2c3 {
|
||||
pinctrl-0 = <&i2c3_scl_pc8 &i2c3_sda_pc9>;
|
||||
status = "okay";
|
||||
clock-frequency = <I2C_BITRATE_STANDARD>;
|
||||
eeprom@50 {
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Copyright (c) 2020 Martin Jäger / Libre Solar
|
||||
*/
|
||||
|
||||
#include <kernel.h>
|
||||
#include <device.h>
|
||||
#include <init.h>
|
||||
#include <drivers/pinmux.h>
|
||||
#include <sys/sys_io.h>
|
||||
|
||||
#include <pinmux/stm32/pinmux_stm32.h>
|
||||
|
||||
/* this should normally go to drivers/pinmux/stm32/pinmux_stm32g4x.h */
|
||||
#define STM32G4X_PINMUX_FUNC_PC6_PWM8_CH1 \
|
||||
(STM32_PINMUX_ALT_FUNC_4 | STM32_PUSHPULL_NOPULL)
|
||||
|
||||
#define STM32G4X_PINMUX_FUNC_PA8_TIM1_CH1 \
|
||||
(STM32_PINMUX_ALT_FUNC_6 | STM32_PUSHPULL_NOPULL)
|
||||
#define STM32G4X_PINMUX_FUNC_PB13_TIM1_CH1N \
|
||||
(STM32_PINMUX_ALT_FUNC_6 | STM32_PUSHPULL_NOPULL)
|
||||
|
||||
static const struct pin_config pinconf[] = {
|
||||
/* TIM1_CH1 and TIM1_CH1N */
|
||||
{STM32_PIN_PA8, STM32G4X_PINMUX_FUNC_PA8_TIM1_CH1},
|
||||
{STM32_PIN_PB13, STM32G4X_PINMUX_FUNC_PB13_TIM1_CH1N},
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(usart1), okay) && CONFIG_SERIAL
|
||||
{STM32_PIN_PA9, STM32G4X_PINMUX_FUNC_PA9_USART1_TX},
|
||||
{STM32_PIN_PA10, STM32G4X_PINMUX_FUNC_PA10_USART1_RX},
|
||||
#endif /* CONFIG_SERIAL || UART_CONSOLE, usart1 */
|
||||
{STM32_PIN_PC6, STM32G4X_PINMUX_FUNC_PC6_PWM8_CH1},
|
||||
{STM32_PIN_PC7, STM32G4X_PINMUX_FUNC_PC7_PWM3_CH2},
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(usart3), okay) && CONFIG_SERIAL
|
||||
{STM32_PIN_PC10, STM32G4X_PINMUX_FUNC_PC10_USART3_TX},
|
||||
{STM32_PIN_PC11, STM32G4X_PINMUX_FUNC_PC11_USART3_RX},
|
||||
#endif /* CONFIG_SERIAL, usart3 */
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c1), okay) && CONFIG_I2C
|
||||
{STM32_PIN_PA15, STM32G4X_PINMUX_FUNC_PA15_I2C1_SCL},
|
||||
{STM32_PIN_PB7, STM32G4X_PINMUX_FUNC_PB7_I2C1_SDA},
|
||||
#endif /* CONFIG_I2C, i2c1 */
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c3), okay) && CONFIG_I2C
|
||||
{STM32_PIN_PC8, STM32G4X_PINMUX_FUNC_PC8_I2C3_SCL},
|
||||
{STM32_PIN_PC9, STM32G4X_PINMUX_FUNC_PC9_I2C3_SDA},
|
||||
#endif /* CONFIG_I2C, i2c3 */
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(spi1), okay) && CONFIG_SPI
|
||||
{STM32_PIN_PB3, STM32G4X_PINMUX_FUNC_PB3_SPI1_SCK},
|
||||
{STM32_PIN_PB4, STM32G4X_PINMUX_FUNC_PB4_SPI1_MISO},
|
||||
{STM32_PIN_PB5, STM32G4X_PINMUX_FUNC_PB5_SPI1_MOSI},
|
||||
#endif /* CONFIG_SPI, spi1 */
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(usb), okay) && CONFIG_USB_DC_STM32
|
||||
{STM32_PIN_PA11, STM32G4X_PINMUX_FUNC_PA11_USB_DM},
|
||||
{STM32_PIN_PA12, STM32G4X_PINMUX_FUNC_PA12_USB_DP},
|
||||
#endif /* CONFIG_USB_DC_STM32 */
|
||||
};
|
||||
|
||||
static int pinmux_stm32_init(const struct device *port)
|
||||
{
|
||||
ARG_UNUSED(port);
|
||||
|
||||
stm32_setup_pins(pinconf, ARRAY_SIZE(pinconf));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
SYS_INIT(pinmux_stm32_init, PRE_KERNEL_1,
|
||||
CONFIG_PINMUX_STM32_DEVICE_INITIALIZATION_PRIORITY);
|
||||
@@ -1,9 +0,0 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
if(CONFIG_PINMUX)
|
||||
zephyr_library()
|
||||
zephyr_library_sources(pinmux.c)
|
||||
zephyr_library_include_directories(${ZEPHYR_BASE}/drivers)
|
||||
endif()
|
||||
|
||||
zephyr_include_directories(.)
|
||||
@@ -1,5 +1,5 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# Copyright (c) 2020 Martin Jäger / Libre Solar
|
||||
|
||||
config BOARD_MPPT_2420_LC
|
||||
bool "Libre Solar MPPT 2420 LC"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# Copyright (c) 2019 Martin Jäger / Libre Solar
|
||||
|
||||
if BOARD_MPPT_2420_LC
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
board_runner_args(jlink "--device=STM32F072CB" "--speed=4000")
|
||||
board_runner_args(jlink "--device=STM32F072CB" "--speed=4000" "--reset-after-load")
|
||||
|
||||
include(${ZEPHYR_BASE}/boards/common/openocd.board.cmake)
|
||||
include(${ZEPHYR_BASE}/boards/common/jlink.board.cmake)
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* Copyright (c) 2020 Martin Jäger / Libre Solar
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/dts-v1/;
|
||||
#include <st/f0/stm32f072Xb.dtsi>
|
||||
#include <st/f0/stm32f072c(8-b)tx-pinctrl.dtsi>
|
||||
|
||||
/ {
|
||||
model = "Libre Solar MPPT 2420 LC";
|
||||
@@ -20,14 +21,7 @@
|
||||
|
||||
hs-voltage-max = <55>;
|
||||
ls-voltage-max = <32>;
|
||||
};
|
||||
|
||||
dcdc {
|
||||
compatible = "dcdc";
|
||||
timer = <&timers1>;
|
||||
pwm-frequency = <70000>;
|
||||
pwm-deadtime = <300>;
|
||||
current-max = <20>;
|
||||
dcdc-current-max = <20>;
|
||||
};
|
||||
|
||||
chosen {
|
||||
@@ -151,22 +145,26 @@
|
||||
};
|
||||
|
||||
&usart1 {
|
||||
pinctrl-0 = <&usart1_tx_pa9 &usart1_rx_pa10>;
|
||||
current-speed = <115200>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&usart2 {
|
||||
pinctrl-0 = <&usart2_tx_pa2 &usart2_rx_pa3>;
|
||||
current-speed = <115200>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&i2c1 {
|
||||
status = "okay";
|
||||
pinctrl-0 = <&i2c1_scl_pb6 &i2c1_sda_pb7>;
|
||||
clock-frequency = <I2C_BITRATE_FAST>;
|
||||
};
|
||||
|
||||
&i2c2 {
|
||||
status = "okay";
|
||||
pinctrl-0 = <&i2c2_scl_pb10 &i2c2_sda_pb11>;
|
||||
clock-frequency = <I2C_BITRATE_STANDARD>;
|
||||
eeprom@50 {
|
||||
// Microchip 24AA32A
|
||||
@@ -185,6 +183,7 @@
|
||||
};
|
||||
|
||||
&spi1 {
|
||||
pinctrl-0 = <&spi1_sck_pb3 &spi1_miso_pb4 &spi1_mosi_pb5>;
|
||||
cs-gpios = <&gpioc 13 GPIO_ACTIVE_LOW>;
|
||||
status = "okay";
|
||||
};
|
||||
@@ -193,7 +192,19 @@
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&timers1 {
|
||||
status = "okay";
|
||||
|
||||
halfbridge {
|
||||
compatible = "half-bridge";
|
||||
pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>;
|
||||
frequency = <70000>;
|
||||
deadtime = <300>;
|
||||
};
|
||||
};
|
||||
|
||||
&can1 {
|
||||
pinctrl-0 = <&can_rx_pb8 &can_tx_pb9>;
|
||||
bus-speed = <250000>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
@@ -30,10 +30,9 @@ CONFIG_CLOCK_STM32_PLL_MULTIPLIER=6
|
||||
CONFIG_CLOCK_STM32_AHB_PRESCALER=1
|
||||
CONFIG_CLOCK_STM32_APB1_PRESCALER=1
|
||||
|
||||
CONFIG_I2C=y
|
||||
|
||||
CONFIG_CAN=y
|
||||
CONFIG_THINGSET_CAN=y
|
||||
|
||||
CONFIG_I2C=y
|
||||
CONFIG_EEPROM=y
|
||||
CONFIG_EEPROM_AT24=y
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Copyright (c) 2020 Martin Jäger / Libre Solar
|
||||
*/
|
||||
|
||||
#include <kernel.h>
|
||||
#include <device.h>
|
||||
#include <init.h>
|
||||
#include <drivers/pinmux.h>
|
||||
#include <sys/sys_io.h>
|
||||
|
||||
#include <pinmux/stm32/pinmux_stm32.h>
|
||||
|
||||
// preliminary fix, as Zephyr doesn't yet include all possible pinmuxes
|
||||
#ifndef STM32F0_PINMUX_FUNC_PB6_I2C1_SCL
|
||||
#define STM32F0_PINMUX_FUNC_PB6_I2C1_SCL \
|
||||
(STM32_PINMUX_ALT_FUNC_1 | STM32_OPENDRAIN_PULLUP)
|
||||
#endif
|
||||
#ifndef STM32F0_PINMUX_FUNC_PB7_I2C1_SDA
|
||||
#define STM32F0_PINMUX_FUNC_PB7_I2C1_SDA \
|
||||
(STM32_PINMUX_ALT_FUNC_1 | STM32_OPENDRAIN_PULLUP)
|
||||
#endif
|
||||
|
||||
#define STM32F0_PINMUX_FUNC_PA8_TIM1_CH1 \
|
||||
(STM32_PINMUX_ALT_FUNC_2 | STM32_PUSHPULL_NOPULL)
|
||||
#define STM32F0_PINMUX_FUNC_PB13_TIM1_CH1N \
|
||||
(STM32_PINMUX_ALT_FUNC_2 | STM32_PUSHPULL_NOPULL)
|
||||
|
||||
static const struct pin_config pinconf[] = {
|
||||
/* TIM1_CH1 and TIM1_CH1N */
|
||||
{STM32_PIN_PA8, STM32F0_PINMUX_FUNC_PA8_TIM1_CH1},
|
||||
{STM32_PIN_PB13, STM32F0_PINMUX_FUNC_PB13_TIM1_CH1N},
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(usart1), okay) && CONFIG_SERIAL
|
||||
{STM32_PIN_PA9, STM32F0_PINMUX_FUNC_PA9_USART1_TX},
|
||||
{STM32_PIN_PA10, STM32F0_PINMUX_FUNC_PA10_USART1_RX},
|
||||
#endif /* CONFIG_SERIAL || UART_CONSOLE, usart1 */
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(usart2), okay) && CONFIG_SERIAL
|
||||
{STM32_PIN_PA2, STM32F0_PINMUX_FUNC_PA2_USART2_TX},
|
||||
{STM32_PIN_PA3, STM32F0_PINMUX_FUNC_PA3_USART2_RX},
|
||||
#endif /* CONFIG_SERIAL, usart2 */
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c1), okay) && CONFIG_I2C
|
||||
{STM32_PIN_PB6, STM32F0_PINMUX_FUNC_PB6_I2C1_SCL},
|
||||
{STM32_PIN_PB7, STM32F0_PINMUX_FUNC_PB7_I2C1_SDA},
|
||||
#endif /* CONFIG_I2C, i2c1 */
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c1), okay) && CONFIG_I2C
|
||||
{STM32_PIN_PB10, STM32F0_PINMUX_FUNC_PB10_I2C2_SCL},
|
||||
{STM32_PIN_PB11, STM32F0_PINMUX_FUNC_PB11_I2C2_SDA},
|
||||
#endif /* CONFIG_I2C, i2c1 */
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(spi1), okay) && CONFIG_SPI
|
||||
{STM32_PIN_PB3, STM32F0_PINMUX_FUNC_PB3_SPI1_SCK},
|
||||
{STM32_PIN_PB4, STM32F0_PINMUX_FUNC_PB4_SPI1_MISO},
|
||||
{STM32_PIN_PB5, STM32F0_PINMUX_FUNC_PB5_SPI1_MOSI},
|
||||
#endif /* CONFIG_SPI, spi1 */
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(can1), okay) && CONFIG_CAN
|
||||
{STM32_PIN_PB8, STM32F0_PINMUX_FUNC_PB8_CAN_RX},
|
||||
{STM32_PIN_PB9, STM32F0_PINMUX_FUNC_PB9_CAN_TX},
|
||||
#endif /* CONFIG_CAN, can1 */
|
||||
};
|
||||
|
||||
static int pinmux_stm32_init(const struct device *port)
|
||||
{
|
||||
ARG_UNUSED(port);
|
||||
|
||||
stm32_setup_pins(pinconf, ARRAY_SIZE(pinconf));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
SYS_INIT(pinmux_stm32_init, PRE_KERNEL_1,
|
||||
CONFIG_PINMUX_STM32_DEVICE_INITIALIZATION_PRIORITY);
|
||||
@@ -1,9 +0,0 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
if(CONFIG_PINMUX)
|
||||
zephyr_library()
|
||||
zephyr_library_sources(pinmux.c)
|
||||
zephyr_library_include_directories(${ZEPHYR_BASE}/drivers)
|
||||
endif()
|
||||
|
||||
zephyr_include_directories(.)
|
||||
@@ -1,5 +1,5 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# Copyright (c) 2020 Martin Jäger / Libre Solar
|
||||
|
||||
config BOARD_MPPT_2420_RC
|
||||
bool "Libre Solar MPPT 2420 RC"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# Copyright (c) 2019 Martin Jäger / Libre Solar
|
||||
|
||||
if BOARD_MPPT_2420_RC
|
||||
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* Copyright (c) 2020 Martin Jäger / Libre Solar
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/dts-v1/;
|
||||
#include <st/g4/stm32g431Xb.dtsi>
|
||||
#include <st/g4/stm32g431c(6-8-b)tx-pinctrl.dtsi>
|
||||
|
||||
/ {
|
||||
model = "Libre Solar MPPT 2420 RC";
|
||||
@@ -20,14 +21,7 @@
|
||||
|
||||
hs-voltage-max = <80>;
|
||||
ls-voltage-max = <32>;
|
||||
};
|
||||
|
||||
dcdc {
|
||||
compatible = "dcdc";
|
||||
timer = <&timers1>;
|
||||
pwm-frequency = <70000>;
|
||||
pwm-deadtime = <300>;
|
||||
current-max = <20>;
|
||||
dcdc-current-max = <20>;
|
||||
};
|
||||
|
||||
chosen {
|
||||
@@ -54,8 +48,8 @@
|
||||
gpios = <&gpiob 14 GPIO_ACTIVE_LOW>;
|
||||
};
|
||||
|
||||
hv-ext-sense {
|
||||
label = "HV external sense and switch";
|
||||
hv-out {
|
||||
label = "HV output sense and enable switch";
|
||||
gpios = <&gpiob 12 GPIO_ACTIVE_LOW>;
|
||||
};
|
||||
};
|
||||
@@ -113,38 +107,6 @@
|
||||
};
|
||||
};
|
||||
|
||||
soc {
|
||||
// ADC nodes not yet provided by Zephyr, but needed for adc-gain compatibles
|
||||
adc1: adc@50000000 {
|
||||
compatible = "st,stm32-adc";
|
||||
reg = <0x50000000 0x400>;
|
||||
clocks = <&rcc STM32_CLOCK_BUS_AHB2 0x00002000>;
|
||||
interrupts = <18 0>;
|
||||
status = "disabled";
|
||||
label = "ADC_1";
|
||||
#io-channel-cells = <1>;
|
||||
};
|
||||
|
||||
adc2: adc@50000100 {
|
||||
compatible = "st,stm32-adc";
|
||||
reg = <0x50000100 0x400>;
|
||||
clocks = <&rcc STM32_CLOCK_BUS_AHB2 0x00002000>;
|
||||
interrupts = <18 0>;
|
||||
status = "disabled";
|
||||
label = "ADC_2";
|
||||
#io-channel-cells = <1>;
|
||||
};
|
||||
|
||||
dac1: dac@50000800 {
|
||||
compatible = "st,stm32-dac";
|
||||
reg = <0x50000800 0x400>;
|
||||
clocks = <&rcc STM32_CLOCK_BUS_AHB2 0x00010000>;
|
||||
status = "disabled";
|
||||
label = "DAC_1";
|
||||
#io-channel-cells = <1>;
|
||||
};
|
||||
};
|
||||
|
||||
aliases {
|
||||
uart-dbg = &usart1;
|
||||
uart-uext = &usart2;
|
||||
@@ -154,22 +116,32 @@
|
||||
};
|
||||
|
||||
&usart1 {
|
||||
pinctrl-0 = <&usart1_tx_pa9 &usart1_rx_pa10>;
|
||||
current-speed = <115200>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&usart2 {
|
||||
pinctrl-0 = <&usart2_tx_pa2 &usart2_rx_pa3>;
|
||||
current-speed = <115200>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&spi1 {
|
||||
pinctrl-0 = <&spi1_sck_pb3 &spi1_miso_pb4 &spi1_mosi_pb5>;
|
||||
cs-gpios = <&gpiob 6 GPIO_ACTIVE_LOW>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&rtc {
|
||||
&timers1 {
|
||||
status = "okay";
|
||||
|
||||
halfbridge {
|
||||
compatible = "half-bridge";
|
||||
pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>;
|
||||
frequency = <70000>;
|
||||
deadtime = <300>;
|
||||
};
|
||||
};
|
||||
|
||||
&flash0 {
|
||||
@@ -197,6 +169,7 @@
|
||||
&i2c1 {
|
||||
/* shared between UEXT and EEPROM */
|
||||
status = "okay";
|
||||
pinctrl-0 = <&i2c1_scl_pa15 &i2c1_sda_pb7>;
|
||||
clock-frequency = <I2C_BITRATE_STANDARD>;
|
||||
eeprom@50 {
|
||||
// Microchip 24AA32A
|
||||
@@ -214,9 +187,8 @@
|
||||
};
|
||||
};
|
||||
|
||||
/*
|
||||
// This requires https://github.com/zephyrproject-rtos/zephyr/pull/22832
|
||||
&can1 {
|
||||
pinctrl-0 = <&fdcan1_rx_pa11 &fdcan1_tx_pa12>;
|
||||
bus-speed = <250000>;
|
||||
bus-speed-data = <250000>;
|
||||
sjw = <1>;
|
||||
@@ -225,4 +197,3 @@
|
||||
sample-point-data = <875>;
|
||||
status = "okay";
|
||||
};
|
||||
*/
|
||||
|
||||
@@ -57,13 +57,11 @@ CONFIG_UART_CONSOLE=y
|
||||
CONFIG_ARM_MPU=y
|
||||
|
||||
CONFIG_I2C=y
|
||||
CONFIG_I2C_1=y
|
||||
CONFIG_EEPROM=y
|
||||
CONFIG_EEPROM_AT24=y
|
||||
|
||||
# don't enable CAN by default until PR #22832 is merged to upstream Zephyr
|
||||
#CONFIG_CAN=y
|
||||
#CONFIG_THINGSET_CAN=y
|
||||
CONFIG_CAN=y
|
||||
CONFIG_THINGSET_CAN=y
|
||||
|
||||
CONFIG_ISOTP=y
|
||||
CONFIG_ISOTP_RX_BUF_COUNT=1
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Copyright (c) 2020 Martin Jäger / Libre Solar
|
||||
*/
|
||||
|
||||
#include <kernel.h>
|
||||
#include <device.h>
|
||||
#include <init.h>
|
||||
#include <drivers/pinmux.h>
|
||||
#include <sys/sys_io.h>
|
||||
|
||||
#include <pinmux/stm32/pinmux_stm32.h>
|
||||
|
||||
#define STM32G4X_PINMUX_FUNC_PA8_TIM1_CH1 \
|
||||
(STM32_PINMUX_ALT_FUNC_6 | STM32_PUSHPULL_NOPULL)
|
||||
#define STM32G4X_PINMUX_FUNC_PB13_TIM1_CH1N \
|
||||
(STM32_PINMUX_ALT_FUNC_6 | STM32_PUSHPULL_NOPULL)
|
||||
|
||||
static const struct pin_config pinconf[] = {
|
||||
/* USART1 */
|
||||
{STM32_PIN_PA9, STM32G4X_PINMUX_FUNC_PA9_USART1_TX},
|
||||
{STM32_PIN_PA10, STM32G4X_PINMUX_FUNC_PA10_USART1_RX},
|
||||
/* USART2 */
|
||||
{STM32_PIN_PA2, STM32G4X_PINMUX_FUNC_PA2_USART2_TX},
|
||||
{STM32_PIN_PA3, STM32G4X_PINMUX_FUNC_PA3_USART2_RX},
|
||||
/* TIM1_CH1 and TIM1_CH1N */
|
||||
{STM32_PIN_PA8, STM32G4X_PINMUX_FUNC_PA8_TIM1_CH1},
|
||||
{STM32_PIN_PB13, STM32G4X_PINMUX_FUNC_PB13_TIM1_CH1N},
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c1), okay) && CONFIG_I2C
|
||||
{STM32_PIN_PA15, STM32G4X_PINMUX_FUNC_PA15_I2C1_SCL},
|
||||
{STM32_PIN_PB7, STM32G4X_PINMUX_FUNC_PB7_I2C1_SDA},
|
||||
#endif /* CONFIG_I2C, i2c1 */
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(spi1), okay) && CONFIG_SPI
|
||||
{STM32_PIN_PB3, STM32G4X_PINMUX_FUNC_PB3_SPI1_SCK},
|
||||
{STM32_PIN_PB4, STM32G4X_PINMUX_FUNC_PB4_SPI1_MISO},
|
||||
{STM32_PIN_PB5, STM32G4X_PINMUX_FUNC_PB5_SPI1_MOSI},
|
||||
#endif /* CONFIG_SPI, spi1 */
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(usb), okay) && CONFIG_USB_DC_STM32
|
||||
{STM32_PIN_PA11, STM32G4X_PINMUX_FUNC_PA11_USB_DM},
|
||||
{STM32_PIN_PA12, STM32G4X_PINMUX_FUNC_PA12_USB_DP},
|
||||
#endif /* CONFIG_USB_DC_STM32 */
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(can1), okay) && CONFIG_CAN
|
||||
{STM32_PIN_PA11, STM32G4X_PINMUX_FUNC_PA11_FDCAN1_RX},
|
||||
{STM32_PIN_PA12, STM32G4X_PINMUX_FUNC_PA12_FDCAN1_TX},
|
||||
#endif
|
||||
#if defined(CONFIG_USB_DC_STM32) && DT_NODE_HAS_STATUS(DT_NODELABEL(can1), okay)
|
||||
#error "PINMUX does not allow concurrent USB and CAN."
|
||||
#endif
|
||||
};
|
||||
|
||||
static int pinmux_stm32_init(const struct device *port)
|
||||
{
|
||||
ARG_UNUSED(port);
|
||||
|
||||
stm32_setup_pins(pinconf, ARRAY_SIZE(pinconf));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
SYS_INIT(pinmux_stm32_init, PRE_KERNEL_1,
|
||||
CONFIG_PINMUX_STM32_DEVICE_INITIALIZATION_PRIORITY);
|
||||
@@ -0,0 +1,8 @@
|
||||
# STM32G474RE Nucleo board configuration
|
||||
|
||||
# Copyright (c) 2019 STMicroelectronics.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
config BOARD_NUCLEO_G474RE
|
||||
bool "Nucleo G474RE Development Board"
|
||||
depends on SOC_STM32G474XX
|
||||
@@ -0,0 +1,14 @@
|
||||
# STM32G474RE Nucleo board configuration
|
||||
|
||||
# Copyright (c) 2019 STMicroelectronics.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
if BOARD_NUCLEO_G474RE
|
||||
|
||||
config BOARD
|
||||
default "nucleo_g474re"
|
||||
|
||||
config LIBRE_SOLAR_TYPE_ID
|
||||
default 0
|
||||
|
||||
endif # BOARD_NUCLEO_G431RB
|
||||
@@ -0,0 +1,10 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# use target=stm32g474rbtx instead of stm32g474retx
|
||||
# to allow board re-flashing (see PR #23230)
|
||||
board_runner_args(pyocd "--target=stm32g474rbtx")
|
||||
|
||||
board_runner_args(jlink "--device=STM32G474RE" "--speed=4000" "--reset-after-load")
|
||||
|
||||
include(${ZEPHYR_BASE}/boards/common/pyocd.board.cmake)
|
||||
include(${ZEPHYR_BASE}/boards/common/openocd.board.cmake)
|
||||
@@ -0,0 +1,255 @@
|
||||
/*
|
||||
* Copyright (c) 2019 STMicroelectronics.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/*
|
||||
* This is a dummy charge controller board for testing new features using the STM32G474 MCU
|
||||
*
|
||||
* IMPORTANT: You have to remove the VREF jumper on the Nucleo board to make it work. Otherwise
|
||||
* the MCU will get stuck while trying to generate a reference voltage at VREF+ pin.
|
||||
*/
|
||||
|
||||
/dts-v1/;
|
||||
#include <st/g4/stm32g474Xe.dtsi>
|
||||
#include <st/g4/stm32g474r(b-c-e)tx-pinctrl.dtsi>
|
||||
|
||||
/ {
|
||||
model = "STMicroelectronics STM32G474RE-NUCLEO board";
|
||||
compatible = "st,stm32g474re-nucleo";
|
||||
|
||||
pcb {
|
||||
compatible = "charge-controller";
|
||||
|
||||
type = "Nucleo G474 dummy charge controller";
|
||||
version-str = "v0.1";
|
||||
version-num = <1>;
|
||||
|
||||
hs-voltage-max = <80>;
|
||||
ls-voltage-max = <32>;
|
||||
dcdc-current-max = <20>;
|
||||
};
|
||||
|
||||
chosen {
|
||||
zephyr,console = &lpuart1;
|
||||
zephyr,shell-uart = &lpuart1;
|
||||
zephyr,sram = &sram0;
|
||||
zephyr,flash = &flash0;
|
||||
zephyr,can-primary = &can1;
|
||||
};
|
||||
|
||||
leds {
|
||||
compatible = "charlieplexing-leds";
|
||||
gpios = <&gpioa 5 GPIO_ACTIVE_HIGH>;
|
||||
|
||||
pwr {
|
||||
states = <1>;
|
||||
};
|
||||
};
|
||||
|
||||
outputs {
|
||||
compatible = "outputs";
|
||||
|
||||
can_en {
|
||||
label = "CAN disable / standby";
|
||||
gpios = <&gpiob 11 GPIO_ACTIVE_LOW>;
|
||||
};
|
||||
};
|
||||
|
||||
adc-inputs {
|
||||
compatible = "adc-inputs";
|
||||
|
||||
v-low {
|
||||
io-channels = <&adc1 12>;
|
||||
multiplier = <105600>;
|
||||
divider = <5600>;
|
||||
};
|
||||
|
||||
v-high {
|
||||
io-channels = <&adc1 15>;
|
||||
multiplier = <102200>;
|
||||
divider = <2200>;
|
||||
};
|
||||
|
||||
vref-mcu {
|
||||
io-channels = <&adc1 18>;
|
||||
multiplier = <1>;
|
||||
divider = <1>;
|
||||
};
|
||||
|
||||
temp-mcu {
|
||||
io-channels = <&adc1 16>;
|
||||
multiplier = <1>;
|
||||
divider = <1>;
|
||||
};
|
||||
|
||||
i-dcdc {
|
||||
io-channels = <&adc2 1>;
|
||||
// amp gain: 25, resistor: 2 mOhm
|
||||
multiplier = <1000>; // 1000
|
||||
divider = <50>; // 2*25
|
||||
};
|
||||
};
|
||||
|
||||
gpio_keys {
|
||||
compatible = "gpio-keys";
|
||||
user_button: button {
|
||||
label = "User";
|
||||
gpios = <&gpioc 13 GPIO_ACTIVE_LOW>;
|
||||
};
|
||||
};
|
||||
|
||||
aliases {
|
||||
sw0 = &user_button;
|
||||
uart-dbg = &lpuart1;
|
||||
uart-uext = &usart1;
|
||||
spi-uext = &spi1;
|
||||
i2c-uext = &i2c1;
|
||||
};
|
||||
|
||||
soc {
|
||||
hrtim1: timers@40016800 {
|
||||
compatible = "st,stm32-timers";
|
||||
reg = <0x40016800 0x400>;
|
||||
clocks = <&rcc STM32_CLOCK_BUS_APB2 0x04000000>;
|
||||
interrupts = <67 0>, <68 0>, <69 0>, <70 0>,
|
||||
<71 0>, <72 0>, <73 0>, <74 0>;
|
||||
interrupt-names = "master", "tima", "timb", "timc",
|
||||
"timd", "time", "flt", "timf";
|
||||
status = "okay";
|
||||
label = "HRTIM_1";
|
||||
};
|
||||
|
||||
/* below nodes are required until following PR is merged:
|
||||
* https://github.com/zephyrproject-rtos/hal_stm32/pull/87
|
||||
*/
|
||||
pinctrl: pin-controller@48000000 {
|
||||
hrtim1_cha1_pa8: hrtim1_cha1_pa8 {
|
||||
pinmux = <STM32_PINMUX('A', 8, AF13)>;
|
||||
};
|
||||
|
||||
hrtim1_cha2_pa9: hrtim1_cha2_pa9 {
|
||||
pinmux = <STM32_PINMUX('A', 9, AF13)>;
|
||||
};
|
||||
|
||||
hrtim1_chb1_pa10: hrtim1_chb1_pa10 {
|
||||
pinmux = <STM32_PINMUX('A', 10, AF13)>;
|
||||
};
|
||||
|
||||
hrtim1_chb2_pa11: hrtim1_chb2_pa11 {
|
||||
pinmux = <STM32_PINMUX('A', 11, AF13)>;
|
||||
};
|
||||
|
||||
hrtim1_chc1_pb12: hrtim1_chc1_pb12 {
|
||||
pinmux = <STM32_PINMUX('B', 12, AF13)>;
|
||||
};
|
||||
|
||||
hrtim1_chc2_pb13: hrtim1_chc2_pb13 {
|
||||
pinmux = <STM32_PINMUX('B', 13, AF13)>;
|
||||
};
|
||||
|
||||
hrtim1_chd1_pb14: hrtim1_chd1_pb14 {
|
||||
pinmux = <STM32_PINMUX('B', 14, AF13)>;
|
||||
};
|
||||
|
||||
hrtim1_chd2_pb15: hrtim1_chd2_pb15 {
|
||||
pinmux = <STM32_PINMUX('B', 15, AF13)>;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
&usart1 {
|
||||
pinctrl-0 = <&usart1_tx_pc4 &usart1_rx_pc5>;
|
||||
current-speed = <115200>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&lpuart1 {
|
||||
pinctrl-0 = <&lpuart1_tx_pa2 &lpuart1_rx_pa3>;
|
||||
current-speed = <115200>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&i2c1 {
|
||||
pinctrl-0 = <&i2c1_scl_pb8 &i2c1_sda_pb9>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&spi1 {
|
||||
pinctrl-0 = <&spi1_nss_pa4 &spi1_sck_pb3
|
||||
&spi1_miso_pa6 &spi1_mosi_pa7>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&hrtim1 {
|
||||
status = "okay";
|
||||
|
||||
halfbridge {
|
||||
compatible = "half-bridge";
|
||||
pinctrl-0 = <&hrtim1_cha1_pa8 &hrtim1_cha2_pa9>;
|
||||
frequency = <70000>;
|
||||
deadtime = <300>;
|
||||
};
|
||||
};
|
||||
|
||||
/*
|
||||
// alternative to HRTIM
|
||||
&timers1 {
|
||||
status = "okay";
|
||||
|
||||
halfbridge {
|
||||
compatible = "half-bridge";
|
||||
pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>;
|
||||
frequency = <70000>;
|
||||
deadtime = <300>;
|
||||
};
|
||||
};
|
||||
*/
|
||||
|
||||
&rtc {
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&flash0 {
|
||||
/*
|
||||
* For more information, see:
|
||||
* http://docs.zephyrproject.org/latest/guides/dts/index.html#flash-partitions
|
||||
*/
|
||||
partitions {
|
||||
compatible = "fixed-partitions";
|
||||
#address-cells = <1>;
|
||||
#size-cells = <1>;
|
||||
|
||||
/* Set 2Kb of storage at the end of the 128Kb of flash */
|
||||
storage_partition: partition@1f800 {
|
||||
label = "storage";
|
||||
reg = <0x0001f800 0x00000800>;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
&iwdg {
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&rng {
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&adc1 {
|
||||
pinctrl-0 = <&adc1_in1_pa0>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&can1 {
|
||||
pinctrl-0 = <&fdcan1_rx_pa11 &fdcan1_tx_pa12>;
|
||||
bus-speed = <250000>;
|
||||
bus-speed-data = <250000>;
|
||||
sjw = <1>;
|
||||
sjw-data = <1>;
|
||||
sample-point = <875>;
|
||||
sample-point-data = <875>;
|
||||
status = "okay";
|
||||
};
|
||||
@@ -0,0 +1,22 @@
|
||||
identifier: nucleo_g474re
|
||||
name: ST Nucleo G474RE
|
||||
type: mcu
|
||||
arch: arm
|
||||
toolchain:
|
||||
- zephyr
|
||||
- gnuarmemb
|
||||
- xtools
|
||||
ram: 128
|
||||
flash: 512
|
||||
supported:
|
||||
- nvs
|
||||
- pwm
|
||||
- i2c
|
||||
- gpio
|
||||
- usb device
|
||||
- counter
|
||||
- spi
|
||||
- watchdog
|
||||
- adc
|
||||
- can
|
||||
- canfd
|
||||
@@ -0,0 +1,68 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
CONFIG_SOC_SERIES_STM32G4X=y
|
||||
CONFIG_SOC_STM32G474XX=y
|
||||
|
||||
# 170MHz system clock only in 'boost power' mode.
|
||||
# RM0440, section 5.1.5 states that the R1MODE bit
|
||||
# must be cleared before system can be 170MHz.
|
||||
CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC=170000000
|
||||
|
||||
# 150MHz system clock in 'normal power' mode
|
||||
# CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC=150000000
|
||||
|
||||
# enable uart driver
|
||||
CONFIG_SERIAL=y
|
||||
|
||||
# enable pinmux
|
||||
CONFIG_PINMUX=y
|
||||
|
||||
# enable GPIO
|
||||
CONFIG_GPIO=y
|
||||
|
||||
# clock configuration
|
||||
CONFIG_CLOCK_CONTROL=y
|
||||
|
||||
# Use PLLCLK for SYSCLK
|
||||
CONFIG_CLOCK_STM32_SYSCLK_SRC_PLL=y
|
||||
|
||||
# Use HSE (24MHz) to feed into PLL
|
||||
CONFIG_CLOCK_STM32_PLL_SRC_HSE=y
|
||||
CONFIG_CLOCK_STM32_HSE_CLOCK=24000000
|
||||
|
||||
CONFIG_CLOCK_STM32_PLL_P_DIVISOR=7
|
||||
CONFIG_CLOCK_STM32_PLL_Q_DIVISOR=2
|
||||
CONFIG_CLOCK_STM32_PLL_R_DIVISOR=2
|
||||
|
||||
# Produce 150MHz clock at PLLCLK output
|
||||
# CONFIG_CLOCK_STM32_PLL_M_DIVISOR=4
|
||||
# CONFIG_CLOCK_STM32_PLL_N_MULTIPLIER=75
|
||||
|
||||
# Produce 170MHz clock at PLLCLK output
|
||||
CONFIG_CLOCK_STM32_PLL_M_DIVISOR=6
|
||||
CONFIG_CLOCK_STM32_PLL_N_MULTIPLIER=85
|
||||
|
||||
# Produce Max (150MHz or 170MHz) HCLK
|
||||
CONFIG_CLOCK_STM32_AHB_PRESCALER=1
|
||||
|
||||
# Produce Max (150MHz or 170MHz) APB1 clocks and APB2 clocks
|
||||
CONFIG_CLOCK_STM32_APB1_PRESCALER=1
|
||||
CONFIG_CLOCK_STM32_APB2_PRESCALER=1
|
||||
|
||||
# Console
|
||||
CONFIG_CONSOLE=y
|
||||
CONFIG_UART_CONSOLE=y
|
||||
|
||||
# Enable MPU
|
||||
CONFIG_ARM_MPU=y
|
||||
|
||||
# Enable HW stack protection
|
||||
CONFIG_HW_STACK_PROTECTION=y
|
||||
|
||||
CONFIG_CAN=y
|
||||
CONFIG_THINGSET_CAN=y
|
||||
|
||||
CONFIG_ISOTP=y
|
||||
CONFIG_ISOTP_RX_BUF_COUNT=2
|
||||
CONFIG_ISOTP_RX_BUF_SIZE=100
|
||||
CONFIG_ISOTP_RX_SF_FF_BUF_COUNT=2
|
||||
@@ -0,0 +1,7 @@
|
||||
source [find interface/stlink.cfg]
|
||||
|
||||
transport select hla_swd
|
||||
|
||||
source [find target/stm32g4x.cfg]
|
||||
|
||||
reset_config srst_only
|
||||
@@ -1,9 +0,0 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
if(CONFIG_PINMUX)
|
||||
zephyr_library()
|
||||
zephyr_library_sources(pinmux.c)
|
||||
zephyr_library_include_directories(${ZEPHYR_BASE}/drivers)
|
||||
endif()
|
||||
|
||||
zephyr_include_directories(.)
|
||||
@@ -1,5 +1,5 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# Copyright (c) 2019 Martin Jäger / Libre Solar
|
||||
|
||||
config BOARD_PWM_2420_LUS
|
||||
bool "Libre Solar PWM 2420 LUS"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# Copyright (c) 2019 Martin Jäger / Libre Solar
|
||||
|
||||
if BOARD_PWM_2420_LUS
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
board_runner_args(jlink "--device=STM32L072RZ" "--speed=4000")
|
||||
board_runner_args(jlink "--device=STM32L072RZ" "--speed=4000" "--reset-after-load")
|
||||
|
||||
include(${ZEPHYR_BASE}/boards/common/openocd.board.cmake)
|
||||
include(${ZEPHYR_BASE}/boards/common/jlink.board.cmake)
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Martin Jäger / Libre Solar
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <kernel.h>
|
||||
#include <device.h>
|
||||
#include <init.h>
|
||||
#include <drivers/pinmux.h>
|
||||
#include <sys/sys_io.h>
|
||||
|
||||
#include <pinmux/stm32/pinmux_stm32.h>
|
||||
|
||||
/* this should normally go to drivers/pinmux/stm32/pinmux_stm32l0.h */
|
||||
#define STM32L0_PINMUX_FUNC_PB1_PWM3_CH4 \
|
||||
(STM32_PINMUX_ALT_FUNC_2 | STM32_PUSHPULL_NOPULL)
|
||||
|
||||
static const struct pin_config pinconf[] = {
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(pwm3), okay)
|
||||
{STM32_PIN_PB1, STM32L0_PINMUX_FUNC_PB1_PWM3_CH4},
|
||||
#endif /* CONFIG_PWM, pwm3 */
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(usart1), okay) && CONFIG_SERIAL
|
||||
{STM32_PIN_PA9, STM32L0_PINMUX_FUNC_PA9_USART1_TX},
|
||||
{STM32_PIN_PA10, STM32L0_PINMUX_FUNC_PA10_USART1_RX},
|
||||
#endif /* CONFIG_SERIAL || UART_CONSOLE, usart1 */
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(usart2), okay) && CONFIG_SERIAL
|
||||
{STM32_PIN_PA2, STM32L0_PINMUX_FUNC_PA2_USART2_TX},
|
||||
{STM32_PIN_PA3, STM32L0_PINMUX_FUNC_PA3_USART2_RX},
|
||||
#endif /* CONFIG_SERIAL, usart2 */
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c1), okay) && CONFIG_I2C
|
||||
{STM32_PIN_PB6, STM32L0_PINMUX_FUNC_PB6_I2C1_SCL},
|
||||
{STM32_PIN_PB7, STM32L0_PINMUX_FUNC_PB7_I2C1_SDA},
|
||||
#endif /* CONFIG_I2C, i2c1 */
|
||||
#if DT_NODE_HAS_STATUS(DT_NODELABEL(spi1), okay) && CONFIG_SPI
|
||||
{STM32_PIN_PB3, STM32L0_PINMUX_FUNC_PB3_SPI1_SCK},
|
||||
{STM32_PIN_PA11, STM32L0_PINMUX_FUNC_PA11_SPI1_MISO},
|
||||
{STM32_PIN_PA12, STM32L0_PINMUX_FUNC_PA12_SPI1_MOSI},
|
||||
#endif /* CONFIG_SPI, spi1 */
|
||||
};
|
||||
|
||||
static int pinmux_stm32_init(const struct device *port)
|
||||
{
|
||||
ARG_UNUSED(port);
|
||||
|
||||
stm32_setup_pins(pinconf, ARRAY_SIZE(pinconf));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
SYS_INIT(pinmux_stm32_init, PRE_KERNEL_1,
|
||||
CONFIG_PINMUX_STM32_DEVICE_INITIALIZATION_PRIORITY);
|
||||
@@ -1,11 +1,12 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* Copyright (c) 2019 Martin Jäger / Libre Solar
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/dts-v1/;
|
||||
#include <st/l0/stm32l072Xz.dtsi>
|
||||
#include <st/l0/stm32l072r(b-z)tx-pinctrl.dtsi>
|
||||
|
||||
/ {
|
||||
model = "Libre Solar PWM 2420 LUS";
|
||||
@@ -15,8 +16,6 @@
|
||||
compatible = "charge-controller";
|
||||
|
||||
type = "PWM 2420 LUS";
|
||||
version-str = "v0.3";
|
||||
version-num = <3>;
|
||||
|
||||
hs-voltage-max = <55>;
|
||||
ls-voltage-max = <32>;
|
||||
@@ -169,21 +168,25 @@
|
||||
};
|
||||
|
||||
&usart1 {
|
||||
pinctrl-0 = <&usart1_tx_pa9 &usart1_rx_pa10>;
|
||||
current-speed = <115200>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&usart2 {
|
||||
pinctrl-0 = <&usart2_tx_pa2 &usart2_rx_pa3>;
|
||||
current-speed = <115200>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&i2c1 {
|
||||
pinctrl-0 = <&i2c1_scl_pb6 &i2c1_sda_pb7>;
|
||||
status = "okay";
|
||||
clock-frequency = <I2C_BITRATE_FAST>;
|
||||
};
|
||||
|
||||
&spi1 {
|
||||
pinctrl-0 = <&spi1_sck_pb3 &spi1_miso_pb4 &spi1_mosi_pb5>;
|
||||
cs-gpios = <&gpioa 15 GPIO_ACTIVE_LOW>;
|
||||
status = "okay";
|
||||
};
|
||||
@@ -192,6 +195,14 @@
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&timers3 {
|
||||
status = "okay";
|
||||
pwm3: pwm {
|
||||
status = "okay";
|
||||
pinctrl-0 = <&tim3_ch4_pb1>;
|
||||
};
|
||||
};
|
||||
|
||||
&eeprom {
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# empty (using defaults from _defconfig file)
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* Copyright (c) 2020 Martin Jäger / Libre Solar
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/ {
|
||||
@@ -0,0 +1,3 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# empty (using defaults from _defconfig file)
|
||||
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/ {
|
||||
pcb {
|
||||
version-str = "v0.3";
|
||||
version-num = <3>;
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,6 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
board_check_revision(
|
||||
FORMAT MAJOR.MINOR.PATCH
|
||||
DEFAULT_REVISION 0.3
|
||||
)
|
||||
@@ -0,0 +1,42 @@
|
||||
{
|
||||
"build": {
|
||||
"core": "stm32",
|
||||
"cpu": "cortex-m4",
|
||||
"extra_flags": "-DSTM32G4xx -DSTM32G474xx",
|
||||
"f_cpu": "170000000L",
|
||||
"mcu": "stm32g474ret6",
|
||||
"product_line": "STM32G474xx",
|
||||
"variant": "NUCLEO_G474RE"
|
||||
},
|
||||
"connectivity": [
|
||||
"can"
|
||||
],
|
||||
"debug": {
|
||||
"jlink_device": "STM32G474RE",
|
||||
"openocd_target": "stm32g4x",
|
||||
"svd_path": "STM32G474xx.svd"
|
||||
},
|
||||
"frameworks": [
|
||||
"arduino",
|
||||
"cmsis",
|
||||
"mbed",
|
||||
"libopencm3",
|
||||
"stm32cube",
|
||||
"zephyr"
|
||||
],
|
||||
"name": "Nucleo G474RE",
|
||||
"upload": {
|
||||
"maximum_ram_size": 131072,
|
||||
"maximum_size": 524288,
|
||||
"protocol": "mbed",
|
||||
"protocols": [
|
||||
"jlink",
|
||||
"stlink",
|
||||
"cmsis-dap",
|
||||
"blackmagic",
|
||||
"mbed"
|
||||
]
|
||||
},
|
||||
"url": "https://www.st.com/en/evaluation-tools/nucleo-g474re.html",
|
||||
"vendor": "ST"
|
||||
}
|
||||
+1
-1
Submodule lib/thingset updated: 4b9434d562...0c39567a0f
+23
-17
@@ -7,13 +7,8 @@
|
||||
|
||||
[platformio]
|
||||
|
||||
# Select the charge controller hardware by uncommenting one of the following lines
|
||||
# Choose one of below charge controller board envs as default, e.g. mppt_2420_hc
|
||||
default_envs =
|
||||
# mppt_2420_lc
|
||||
# mppt_2420_rc
|
||||
# mppt_2420_hpx
|
||||
# mppt_1210_hus
|
||||
# pwm_2420_lus
|
||||
|
||||
# Or create your own custom.ini file to overwrite settings in this file, e.g. for board selection.
|
||||
# For further information see here:
|
||||
@@ -23,13 +18,14 @@ extra_configs =
|
||||
|
||||
[env]
|
||||
|
||||
#platform = https://github.com/platformio/platform-ststm32.git
|
||||
platform = ststm32@~8.1.0
|
||||
|
||||
framework = zephyr
|
||||
|
||||
#platform = https://github.com/platformio/platform-ststm32.git
|
||||
platform = ststm32@~12.0.0
|
||||
|
||||
# Use customized Zephyr branch from Libre Solar repository
|
||||
platform_packages = framework-zephyr@https://github.com/LibreSolar/zephyr#v2.4-branch
|
||||
platform_packages =
|
||||
framework-zephyr@https://github.com/LibreSolar/zephyr#v2.5-branch
|
||||
|
||||
#upload_protocol = mbed
|
||||
upload_protocol = stlink
|
||||
@@ -59,7 +55,13 @@ check_flags =
|
||||
|
||||
#### BOARD DEFINITIONS ########################################################
|
||||
|
||||
# https://github.com/LibreSolar/MPPT-2420-LC/tree/5bc39183c8
|
||||
# You can specify a specific version by adding e.g. @0.2 to the board name
|
||||
# (see https://docs.zephyrproject.org/latest/guides/porting/board_porting.html for details)
|
||||
|
||||
# PlatformIO requires a .json file with the same name in boards/ directory. If not existing for
|
||||
# a particular board revision, just copy it from an existing different revision
|
||||
|
||||
# https://github.com/LibreSolar/mppt-2420-lc
|
||||
[env:mppt_2420_lc]
|
||||
board = mppt_2420_lc
|
||||
|
||||
@@ -69,19 +71,23 @@ board = mppt_2420_rc
|
||||
|
||||
# https://github.com/LibreSolar/mppt-2420-hc
|
||||
[env:mppt_2420_hc]
|
||||
board = mppt_2420_hc
|
||||
board = mppt_2420_hc@0.2
|
||||
|
||||
# https://github.com/LibreSolar/MPPT-1210-HUS
|
||||
# https://github.com/LibreSolar/mppt-1210-hus
|
||||
[env:mppt_1210_hus]
|
||||
board = mppt_1210_hus
|
||||
board = mppt_1210_hus@0.7
|
||||
|
||||
# https://github.com/LibreSolar/mppt-2420-hpx
|
||||
[env:mppt_2420_hpx]
|
||||
board = mppt_2420_hpx
|
||||
|
||||
# https://github.com/LibreSolar/PWM-2420-LUS
|
||||
# https://github.com/LibreSolar/pwm-2420-lus
|
||||
[env:pwm_2420_lus]
|
||||
board = pwm_2420_lus
|
||||
board = pwm_2420_lus@0.3
|
||||
|
||||
# Nucleo board for testing purposes
|
||||
[env:nucleo_g474re]
|
||||
board = nucleo_g474re
|
||||
|
||||
[env:unit_test]
|
||||
platform = native
|
||||
@@ -91,7 +97,7 @@ build_flags =
|
||||
-Wall
|
||||
-D UNIT_TEST
|
||||
-D __STDC_FORMAT_MACROS
|
||||
-D COMMIT_HASH=\"test\"
|
||||
-D FIRMWARE_VERSION_ID=\"v20.0-7-gda01bb3\"
|
||||
-I test
|
||||
# include src directory (otherwise unit-tests will only include lib directory)
|
||||
test_build_project_src = true
|
||||
|
||||
+1
-1
@@ -5,11 +5,11 @@ zephyr_include_directories(.)
|
||||
target_sources(app PRIVATE
|
||||
bat_charger.cpp
|
||||
data_nodes.cpp
|
||||
data_storage.cpp
|
||||
daq.cpp
|
||||
daq_driver.c
|
||||
device_status.cpp
|
||||
dcdc.cpp
|
||||
eeprom.cpp
|
||||
half_bridge.cpp
|
||||
hardware.cpp
|
||||
leds.cpp
|
||||
|
||||
+13
-8
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -10,12 +10,13 @@
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
#include <logging/log.h>
|
||||
LOG_MODULE_REGISTER(bat_charger, CONFIG_LOG_DEFAULT_LEVEL);
|
||||
LOG_MODULE_REGISTER(bat_charger, CONFIG_BAT_LOG_LEVEL);
|
||||
#endif
|
||||
|
||||
#include <math.h> // for fabs function
|
||||
#include <stdio.h>
|
||||
|
||||
#include "board.h"
|
||||
#include "helper.h"
|
||||
#include "device_status.h"
|
||||
|
||||
@@ -23,10 +24,10 @@ extern DeviceStatus dev_stat;
|
||||
extern LoadOutput load;
|
||||
|
||||
// DISCHARGE_CURRENT_MAX used to estimate current-compensation of load disconnect voltage
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
#define DISCHARGE_CURRENT_MAX DT_PROP(DT_CHILD(DT_PATH(outputs), load), current_max)
|
||||
#else
|
||||
#define DISCHARGE_CURRENT_MAX DT_PROP(DT_PATH(dcdc), current_max)
|
||||
#define DISCHARGE_CURRENT_MAX DT_PROP(DT_PATH(pcb), dcdc_current_max)
|
||||
#endif
|
||||
|
||||
void battery_conf_init(BatConf *bat, int type, int num_cells, float nominal_capacity)
|
||||
@@ -148,6 +149,7 @@ void battery_conf_init(BatConf *bat, int type, int num_cells, float nominal_capa
|
||||
break;
|
||||
|
||||
case BAT_TYPE_CUSTOM:
|
||||
#ifdef CONFIG_BAT_TYPE_CUSTOM
|
||||
bat->voltage_absolute_max = 0.001F *
|
||||
static_cast<float>(CONFIG_BAT_NUM_CELLS * CONFIG_CELL_ABS_MAX_VOLTAGE_MV);
|
||||
|
||||
@@ -197,6 +199,9 @@ void battery_conf_init(BatConf *bat, int type, int num_cells, float nominal_capa
|
||||
bat->charge_temp_min = CONFIG_BAT_CHARGE_TEMP_MIN;
|
||||
bat->discharge_temp_max = CONFIG_BAT_DISCHARGE_TEMP_MAX;
|
||||
bat->discharge_temp_min = CONFIG_BAT_DISCHARGE_TEMP_MIN;
|
||||
#else
|
||||
LOG_ERR("Custom battery type cannot be initialized at runtime.");
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -355,8 +360,8 @@ void Charger::enter_state(int next_state)
|
||||
|
||||
void Charger::discharge_control(BatConf *bat_conf)
|
||||
{
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) || \
|
||||
DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr))
|
||||
#if BOARD_HAS_LOAD_OUTPUT || \
|
||||
BOARD_HAS_USB_OUTPUT
|
||||
|
||||
if (!empty) {
|
||||
// as we don't have a proper SOC estimation, we determine an empty battery by the main
|
||||
@@ -489,10 +494,10 @@ void Charger::charge_control(BatConf *bat_conf)
|
||||
port->bus->sink_voltage_intercept = bat_conf->topping_voltage +
|
||||
bat_conf->temperature_compensation * (bat_temperature - 25);
|
||||
|
||||
if (port->bus->voltage >= port->bus->sink_control_voltage() - 0.05F) {
|
||||
if (port->bus->voltage_filtered >= port->bus->sink_control_voltage() - 0.05F) {
|
||||
// battery is full if topping target voltage is still reached (i.e. sufficient
|
||||
// solar power available) and time limit or cut-off current reached
|
||||
if (port->current < bat_conf->topping_current_cutoff ||
|
||||
if (port->current_filtered < bat_conf->topping_current_cutoff ||
|
||||
target_voltage_timer > bat_conf->topping_duration)
|
||||
{
|
||||
full = true;
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef BOARD_H_
|
||||
#define BOARD_H_
|
||||
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* @brief Defines for conditional compiling based on existing board features
|
||||
*/
|
||||
|
||||
#include <zephyr.h>
|
||||
|
||||
#define BOARD_HAS_DCDC DT_HAS_COMPAT_STATUS_OKAY(half_bridge)
|
||||
#define BOARD_HAS_PWM_PORT DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch))
|
||||
#define BOARD_HAS_LOAD_OUTPUT DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#define BOARD_HAS_USB_OUTPUT DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr))
|
||||
|
||||
#endif /* BOARD_H_ */
|
||||
+40
-20
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
#include <logging/log.h>
|
||||
LOG_MODULE_REGISTER(daq, CONFIG_LOG_DEFAULT_LEVEL);
|
||||
LOG_MODULE_REGISTER(daq, CONFIG_DAQ_LOG_LEVEL);
|
||||
#endif
|
||||
|
||||
#include <math.h> // log for thermistor calculation
|
||||
@@ -22,13 +22,19 @@ LOG_MODULE_REGISTER(daq, CONFIG_LOG_DEFAULT_LEVEL);
|
||||
// typical value for Semitec 103AT-5 thermistor: 3435
|
||||
#define NTC_BETA_VALUE 3435
|
||||
|
||||
#if DT_NODE_EXISTS(DT_PATH(dcdc))
|
||||
// filter parameter c for additional battery voltage and current low-pass filter
|
||||
// c = dt / (tau + dt) = 0.1s / (10s + 0.1s)
|
||||
#define LV_BUS_VOLTAGE_FILTER_CONST 0.0099F
|
||||
#define LV_TERMINAL_CURRENT_FILTER_CONST 0.0099F
|
||||
#define PWM_CURRENT_FILTER_CONST 0.0625F // 1.5 seconds
|
||||
|
||||
#if BOARD_HAS_DCDC
|
||||
static uint16_t dcdc_current_offset_raw;
|
||||
#endif
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch))
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
static uint16_t pwm_current_offset_raw;
|
||||
#endif
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
static uint16_t load_current_offset_raw;
|
||||
#endif
|
||||
|
||||
@@ -93,20 +99,20 @@ static inline float ntc_temp(uint32_t channel, int32_t vref, float ntc_series_re
|
||||
|
||||
void calibrate_current_sensors()
|
||||
{
|
||||
#if DT_NODE_EXISTS(DT_PATH(dcdc))
|
||||
#if BOARD_HAS_DCDC
|
||||
dcdc_current_offset_raw = adc_raw_filtered(ADC_POS(i_dcdc));
|
||||
#endif
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch))
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
pwm_current_offset_raw = adc_raw_filtered(ADC_POS(i_pwm));
|
||||
#endif
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
load_current_offset_raw = adc_raw_filtered(ADC_POS(i_load));
|
||||
#endif
|
||||
}
|
||||
|
||||
void adc_update_value(unsigned int pos)
|
||||
{
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch))
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
// only read input voltage and current when switch is on or permanently off
|
||||
if ((pos != ADC_POS(v_pwm) && pos != ADC_POS(i_pwm)) ||
|
||||
pwm_switch.signal_high() || pwm_switch.active() == false)
|
||||
@@ -168,16 +174,25 @@ void daq_update()
|
||||
// calculate lower voltage first, as it is needed for PWM terminal voltage calculation
|
||||
lv_bus.voltage = adc_scaled(ADC_POS(v_low), vref, ADC_GAIN(v_low));
|
||||
|
||||
#if DT_NODE_EXISTS(DT_PATH(dcdc))
|
||||
if (lv_bus.voltage_filtered != 0.0F) {
|
||||
lv_bus.voltage_filtered = LV_BUS_VOLTAGE_FILTER_CONST * lv_bus.voltage +
|
||||
(1.0F - LV_BUS_VOLTAGE_FILTER_CONST) * lv_bus.voltage_filtered;
|
||||
}
|
||||
else {
|
||||
// initialize properly at startup
|
||||
lv_bus.voltage_filtered = lv_bus.voltage;
|
||||
}
|
||||
|
||||
#if BOARD_HAS_DCDC
|
||||
hv_bus.voltage = adc_scaled(ADC_POS(v_high), vref, ADC_GAIN(v_high));
|
||||
#endif
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch))
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
pwm_switch.ext_voltage = lv_bus.voltage -
|
||||
adc_scaled(ADC_POS(v_pwm), vref, ADC_GAIN(v_pwm), ADC_OFFSET(v_pwm));
|
||||
#endif
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
load.current = adc_scaled(ADC_POS(i_load), vref, ADC_GAIN(i_load), load_current_offset_raw);
|
||||
float load_current = load.current;
|
||||
#else
|
||||
@@ -186,18 +201,20 @@ void daq_update()
|
||||
|
||||
float lv_terminal_current = -load_current;
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch))
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
// current multiplied with PWM duty cycle for PWM charger to get avg current for correct power
|
||||
// calculation
|
||||
pwm_switch.current = -pwm_switch.get_duty_cycle() *
|
||||
adc_scaled(ADC_POS(i_pwm), vref, ADC_GAIN(i_pwm), pwm_current_offset_raw);
|
||||
pwm_switch.current_filtered = PWM_CURRENT_FILTER_CONST * pwm_switch.current +
|
||||
(1.0F - PWM_CURRENT_FILTER_CONST) * pwm_switch.current_filtered;
|
||||
|
||||
lv_terminal_current -= pwm_switch.current;
|
||||
|
||||
pwm_switch.power = pwm_switch.bus->voltage * pwm_switch.current;
|
||||
#endif
|
||||
|
||||
#if DT_NODE_EXISTS(DT_PATH(dcdc))
|
||||
#if BOARD_HAS_DCDC
|
||||
dcdc.inductor_current =
|
||||
adc_scaled(ADC_POS(i_dcdc), vref, ADC_GAIN(i_dcdc), dcdc_current_offset_raw);
|
||||
|
||||
@@ -212,7 +229,10 @@ void daq_update()
|
||||
lv_terminal.current = lv_terminal_current;
|
||||
lv_terminal.power = lv_terminal.bus->voltage * lv_terminal.current;
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
lv_terminal.current_filtered = LV_TERMINAL_CURRENT_FILTER_CONST * lv_terminal.current +
|
||||
(1.0F - LV_TERMINAL_CURRENT_FILTER_CONST) * lv_terminal.current_filtered;
|
||||
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
load.power = load.bus->voltage * load.current;
|
||||
#endif
|
||||
|
||||
@@ -255,10 +275,10 @@ void daq_update()
|
||||
void lv_overvoltage_alert()
|
||||
{
|
||||
// disable any sort of input
|
||||
#if DT_NODE_EXISTS(DT_PATH(dcdc))
|
||||
#if BOARD_HAS_DCDC
|
||||
dcdc.stop();
|
||||
#endif
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch))
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
pwm_switch.stop();
|
||||
#endif
|
||||
// do not use enter_state function, as we don't want to wait entire recharge delay
|
||||
@@ -272,7 +292,7 @@ void lv_overvoltage_alert()
|
||||
|
||||
void lv_undervoltage_alert()
|
||||
{
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
// the battery undervoltage must have been caused by a load current peak
|
||||
load.stop(ERR_LOAD_VOLTAGE_DIP);
|
||||
#endif
|
||||
@@ -281,7 +301,7 @@ void lv_undervoltage_alert()
|
||||
adc_readings[ADC_POS(v_low)], adc_alerts_lower[ADC_POS(v_low)].limit);
|
||||
}
|
||||
|
||||
#if DT_NODE_EXISTS(DT_PATH(dcdc))
|
||||
#if BOARD_HAS_DCDC
|
||||
void hv_overvoltage_alert()
|
||||
{
|
||||
dcdc.stop();
|
||||
@@ -324,7 +344,7 @@ void daq_set_lv_limits(float lv_overvoltage, float lv_undervoltage)
|
||||
adc_alerts_lower[ADC_POS(v_low)].callback = lv_undervoltage_alert;
|
||||
}
|
||||
|
||||
#if DT_NODE_EXISTS(DT_PATH(dcdc))
|
||||
#if BOARD_HAS_DCDC
|
||||
void daq_set_hv_limit(float hv_overvoltage)
|
||||
{
|
||||
float scale = (((4096 << 4) * 1000) / ADC_GAIN(v_high)) / (float)VREF;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
+10
-27
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -14,25 +14,11 @@
|
||||
#include <zephyr.h>
|
||||
#include <drivers/gpio.h>
|
||||
|
||||
#if defined(CONFIG_SOC_SERIES_STM32L0X)
|
||||
#include <stm32l0xx_ll_system.h>
|
||||
#include <stm32l0xx_ll_adc.h>
|
||||
#include <stm32l0xx_ll_dac.h>
|
||||
#include <stm32l0xx_ll_dma.h>
|
||||
#include <stm32l0xx_ll_bus.h>
|
||||
#elif defined(CONFIG_SOC_SERIES_STM32F0X)
|
||||
#include <stm32f0xx_ll_system.h>
|
||||
#include <stm32f0xx_ll_adc.h>
|
||||
#include <stm32f0xx_ll_dac.h>
|
||||
#include <stm32f0xx_ll_dma.h>
|
||||
#include <stm32f0xx_ll_bus.h>
|
||||
#elif defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
#include <stm32g4xx_ll_system.h>
|
||||
#include <stm32g4xx_ll_adc.h>
|
||||
#include <stm32g4xx_ll_dac.h>
|
||||
#include <stm32g4xx_ll_dma.h>
|
||||
#include <stm32g4xx_ll_bus.h>
|
||||
#endif
|
||||
#include <stm32_ll_system.h>
|
||||
#include <stm32_ll_adc.h>
|
||||
#include <stm32_ll_dac.h>
|
||||
#include <stm32_ll_dma.h>
|
||||
#include <stm32_ll_bus.h>
|
||||
|
||||
#include "dcdc.h" // for low-level control function called by DMA
|
||||
|
||||
@@ -243,12 +229,6 @@ static void adc_init(ADC_TypeDef *adc)
|
||||
// Enable DMA transfer on ADC and circular mode
|
||||
LL_ADC_REG_SetDMATransfer(adc, LL_ADC_REG_DMA_TRANSFER_UNLIMITED);
|
||||
|
||||
#if defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
if (adc == ADC2) {
|
||||
LL_ADC_REG_SetTriggerEdge(adc, LL_ADC_REG_TRIG_EXT_RISING);
|
||||
LL_ADC_REG_SetTriggerSource(adc, LL_ADC_REG_TRIG_EXT_TIM1_TRGO2);
|
||||
}
|
||||
#endif
|
||||
LL_ADC_Enable(adc);
|
||||
}
|
||||
|
||||
@@ -273,7 +253,9 @@ static inline void adc_trigger_conversion(struct k_timer *timer_id)
|
||||
{
|
||||
LL_ADC_REG_StartConversion(ADC1);
|
||||
|
||||
// ADC2 (if existing) is triggered by the PWM timer
|
||||
#if defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
LL_ADC_REG_StartConversion(ADC2);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void DMA1_Channel1_IRQHandler(void *args)
|
||||
@@ -395,6 +377,7 @@ void daq_setup()
|
||||
k_sleep(K_MSEC(500)); // wait for ADC to collect some measurement values
|
||||
daq_update();
|
||||
calibrate_current_sensors();
|
||||
daq_update();
|
||||
}
|
||||
|
||||
#endif // UNIT_TEST
|
||||
|
||||
+34
-37
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -11,7 +11,7 @@
|
||||
#include <drivers/hwinfo.h>
|
||||
#include <sys/crc.h>
|
||||
#include <logging/log.h>
|
||||
LOG_MODULE_REGISTER(data_nodes, CONFIG_LOG_DEFAULT_LEVEL);
|
||||
LOG_MODULE_REGISTER(data_nodes, CONFIG_DATA_NODES_LOG_LEVEL);
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
@@ -26,13 +26,12 @@ LOG_MODULE_REGISTER(data_nodes, CONFIG_LOG_DEFAULT_LEVEL);
|
||||
#include "thingset.h"
|
||||
#include "hardware.h"
|
||||
#include "dcdc.h"
|
||||
#include "eeprom.h"
|
||||
#include "data_storage.h"
|
||||
|
||||
const char* const manufacturer = "Libre Solar";
|
||||
const char* const device_type = DT_PROP(DT_PATH(pcb), type);
|
||||
const char* const hardware_version = DT_PROP(DT_PATH(pcb), version_str);
|
||||
const char* const firmware_version = "0.1";
|
||||
const char* const firmware_commit = COMMIT_HASH;
|
||||
const char manufacturer[] = "Libre Solar";
|
||||
const char device_type[] = DT_PROP(DT_PATH(pcb), type);
|
||||
const char hardware_version[] = DT_PROP(DT_PATH(pcb), version_str);
|
||||
const char firmware_version[] = FIRMWARE_VERSION_ID;
|
||||
char device_id[9];
|
||||
|
||||
static char auth_password[11];
|
||||
@@ -49,11 +48,11 @@ static char auth_password[11];
|
||||
#define solar_bus lv_bus
|
||||
#endif
|
||||
|
||||
bool pub_serial_enable = false;
|
||||
bool pub_serial_enable = IS_ENABLED(CONFIG_THINGSET_SERIAL_PUB_DEFAULT);
|
||||
|
||||
#if CONFIG_THINGSET_CAN
|
||||
bool pub_can_enable = false;
|
||||
uint16_t ts_can_node_id = CONFIG_THINGSET_CAN_DEFAULT_NODE_ID;
|
||||
bool pub_can_enable = IS_ENABLED(CONFIG_THINGSET_CAN_PUB_DEFAULT);
|
||||
uint16_t can_node_addr = CONFIG_THINGSET_CAN_DEFAULT_NODE_ID;
|
||||
#endif
|
||||
|
||||
/**
|
||||
@@ -86,12 +85,6 @@ static DataNode data_nodes[] = {
|
||||
TS_NODE_STRING(0x1D, "FirmwareVersion", firmware_version, 0,
|
||||
ID_INFO, TS_ANY_R, 0),
|
||||
|
||||
TS_NODE_STRING(0x1E, "FirmwareCommit", firmware_commit, 0,
|
||||
ID_INFO, TS_ANY_R, 0),
|
||||
|
||||
TS_NODE_UINT32(0x20, "Timestamp_s", ×tamp,
|
||||
ID_INFO, TS_ANY_R | TS_ANY_W, PUB_SER | PUB_NVM),
|
||||
|
||||
// CONFIGURATION //////////////////////////////////////////////////////////
|
||||
// using IDs >= 0x30 except for high priority data objects
|
||||
|
||||
@@ -169,7 +162,7 @@ static DataNode data_nodes[] = {
|
||||
ID_CONF, TS_ANY_R | TS_ANY_W, PUB_NVM),
|
||||
|
||||
// load settings
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
TS_NODE_BOOL(0x50, "LoadEnDefault", &load.enable,
|
||||
ID_CONF, TS_ANY_R | TS_ANY_W, PUB_NVM),
|
||||
|
||||
@@ -186,7 +179,7 @@ static DataNode data_nodes[] = {
|
||||
ID_CONF, TS_ANY_R | TS_ANY_W, PUB_NVM),
|
||||
#endif
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr))
|
||||
#if BOARD_HAS_USB_OUTPUT
|
||||
TS_NODE_BOOL(0x55, "UsbEnDefault", &usb_pwr.enable,
|
||||
ID_CONF, TS_ANY_R | TS_ANY_W, PUB_NVM),
|
||||
|
||||
@@ -201,7 +194,7 @@ static DataNode data_nodes[] = {
|
||||
#endif
|
||||
|
||||
#if CONFIG_THINGSET_CAN
|
||||
TS_NODE_UINT16(0x59, "CanNodeId", &ts_can_node_id,
|
||||
TS_NODE_UINT16(0x59, "CanNodeId", &can_node_addr,
|
||||
ID_CONF, TS_ANY_R | TS_ANY_W, PUB_NVM),
|
||||
#endif
|
||||
|
||||
@@ -210,22 +203,22 @@ static DataNode data_nodes[] = {
|
||||
|
||||
TS_NODE_PATH(ID_INPUT, "input", 0, NULL),
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
TS_NODE_BOOL(0x61, "LoadEn", &load.enable,
|
||||
ID_INPUT, TS_ANY_R | TS_ANY_W, 0),
|
||||
#endif
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr))
|
||||
#if BOARD_HAS_USB_OUTPUT
|
||||
TS_NODE_BOOL(0x62, "UsbEn", &usb_pwr.enable,
|
||||
ID_INPUT, TS_ANY_R | TS_ANY_W, 0),
|
||||
#endif
|
||||
|
||||
#if DT_NODE_EXISTS(DT_PATH(dcdc))
|
||||
#if BOARD_HAS_DCDC
|
||||
TS_NODE_BOOL(0x63, "DcdcEn", &dcdc.enable,
|
||||
ID_INPUT, TS_ANY_R | TS_ANY_W, 0),
|
||||
#endif
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch))
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
TS_NODE_BOOL(0x64, "PwmEn", &pwm_switch.enable,
|
||||
ID_INPUT, TS_ANY_R | TS_ANY_W, 0),
|
||||
#endif
|
||||
@@ -243,6 +236,10 @@ static DataNode data_nodes[] = {
|
||||
|
||||
TS_NODE_PATH(ID_OUTPUT, "output", 0, NULL),
|
||||
|
||||
// the timestamp currently reflects the time since last reset and not an actual timestamp
|
||||
TS_NODE_UINT32(0x01, "Uptime_s", ×tamp,
|
||||
ID_OUTPUT, TS_ANY_R, PUB_SER),
|
||||
|
||||
// battery related data objects
|
||||
TS_NODE_FLOAT(0x71, "Bat_V", &bat_bus.voltage, 2,
|
||||
ID_OUTPUT, TS_ANY_R, PUB_SER | PUB_CAN),
|
||||
@@ -282,7 +279,7 @@ static DataNode data_nodes[] = {
|
||||
TS_NODE_UINT32(0x7C, "ChgState", &charger.state,
|
||||
ID_OUTPUT, TS_ANY_R, PUB_SER | PUB_CAN),
|
||||
|
||||
#if DT_NODE_EXISTS(DT_PATH(dcdc))
|
||||
#if BOARD_HAS_DCDC
|
||||
TS_NODE_UINT16(0x7D, "DCDCState", &dcdc.state,
|
||||
ID_OUTPUT, TS_ANY_R, PUB_SER | PUB_CAN),
|
||||
#endif
|
||||
@@ -305,7 +302,7 @@ static DataNode data_nodes[] = {
|
||||
ID_OUTPUT, TS_ANY_R, 0),
|
||||
#endif
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
TS_NODE_FLOAT(0x89, "Load_A", &load.current, 2,
|
||||
ID_OUTPUT, TS_ANY_R, PUB_SER | PUB_CAN),
|
||||
|
||||
@@ -316,7 +313,7 @@ static DataNode data_nodes[] = {
|
||||
ID_OUTPUT, TS_ANY_R, PUB_SER | PUB_CAN),
|
||||
#endif
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr))
|
||||
#if BOARD_HAS_USB_OUTPUT
|
||||
TS_NODE_INT32(0x8C, "UsbInfo", &usb_pwr.info,
|
||||
ID_OUTPUT, TS_ANY_R, PUB_SER | PUB_CAN),
|
||||
#endif
|
||||
@@ -344,7 +341,7 @@ static DataNode data_nodes[] = {
|
||||
TS_NODE_UINT32(0x08, "SolarInTotal_Wh", &dev_stat.solar_in_total_Wh,
|
||||
ID_REC, TS_ANY_R | TS_MKR_W, PUB_NVM),
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
TS_NODE_UINT32(0x09, "LoadOutTotal_Wh", &dev_stat.load_out_total_Wh,
|
||||
ID_REC, TS_ANY_R | TS_MKR_W, PUB_NVM),
|
||||
#endif
|
||||
@@ -375,7 +372,7 @@ static DataNode data_nodes[] = {
|
||||
TS_NODE_UINT16(0x0F, "SolarMaxDay_W", &dev_stat.solar_power_max_day,
|
||||
ID_REC, TS_ANY_R | TS_MKR_W, 0),
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
TS_NODE_UINT16(0x10, "LoadMaxDay_W", &dev_stat.load_power_max_day,
|
||||
ID_REC, TS_ANY_R | TS_MKR_W, 0),
|
||||
#endif
|
||||
@@ -385,7 +382,7 @@ static DataNode data_nodes[] = {
|
||||
ID_REC, TS_ANY_R | TS_MKR_W, PUB_SER | PUB_CAN),
|
||||
#endif
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
TS_NODE_FLOAT(0xA2, "LoadOutDay_Wh", &load.pos_energy_Wh, 2,
|
||||
ID_REC, TS_ANY_R | TS_MKR_W, PUB_SER | PUB_CAN),
|
||||
#endif
|
||||
@@ -409,7 +406,7 @@ static DataNode data_nodes[] = {
|
||||
TS_NODE_UINT16(0xB1, "SolarMaxTotal_W", &dev_stat.solar_power_max_total,
|
||||
ID_REC, TS_ANY_R | TS_MKR_W, PUB_NVM),
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
TS_NODE_UINT16(0xB2, "LoadMaxTotal_W", &dev_stat.load_power_max_total,
|
||||
ID_REC, TS_ANY_R | TS_MKR_W, PUB_NVM),
|
||||
#endif
|
||||
@@ -423,7 +420,7 @@ static DataNode data_nodes[] = {
|
||||
TS_NODE_FLOAT(0xB5, "DcdcMaxTotal_A", &dev_stat.dcdc_current_max, 2,
|
||||
ID_REC, TS_ANY_R | TS_MKR_W, PUB_NVM),
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
TS_NODE_FLOAT(0xB6, "LoadMaxTotal_A", &dev_stat.load_current_max, 2,
|
||||
ID_REC, TS_ANY_R | TS_MKR_W, PUB_NVM),
|
||||
#endif
|
||||
@@ -442,7 +439,7 @@ static DataNode data_nodes[] = {
|
||||
|
||||
TS_NODE_PATH(ID_CAL, "cal", 0, NULL),
|
||||
|
||||
#if DT_NODE_EXISTS(DT_PATH(dcdc))
|
||||
#if BOARD_HAS_DCDC
|
||||
TS_NODE_FLOAT(0xD1, "DcdcMin_W", &dcdc.output_power_min, 1,
|
||||
ID_CAL, TS_ANY_R | TS_MKR_W, PUB_NVM),
|
||||
|
||||
@@ -460,7 +457,7 @@ static DataNode data_nodes[] = {
|
||||
|
||||
TS_NODE_EXEC(0xE1, "reset", &reset_device, ID_EXEC, TS_ANY_RW),
|
||||
TS_NODE_EXEC(0xE2, "bootloader-stm", &start_stm32_bootloader, ID_EXEC, TS_ANY_RW),
|
||||
TS_NODE_EXEC(0xE3, "save-settings", &eeprom_store_data, ID_EXEC, TS_ANY_RW),
|
||||
TS_NODE_EXEC(0xE3, "save-settings", &data_storage_write, ID_EXEC, TS_ANY_RW),
|
||||
|
||||
TS_NODE_EXEC(0xEE, "auth", &thingset_auth, 0, TS_ANY_RW),
|
||||
TS_NODE_STRING(0xEF, "Password", auth_password, sizeof(auth_password), 0xEE, TS_ANY_RW, 0),
|
||||
@@ -489,7 +486,7 @@ void data_nodes_update_conf()
|
||||
if (battery_conf_check(&bat_conf_user)) {
|
||||
LOG_INF("New config valid and activated.");
|
||||
battery_conf_overwrite(&bat_conf_user, &bat_conf, &charger);
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
load.set_voltage_limits(bat_conf.voltage_load_disconnect, bat_conf.voltage_load_reconnect,
|
||||
bat_conf.voltage_absolute_max);
|
||||
#endif
|
||||
@@ -505,7 +502,7 @@ void data_nodes_update_conf()
|
||||
changed = true; // temporary hack
|
||||
|
||||
if (changed) {
|
||||
eeprom_store_data();
|
||||
data_storage_write();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -521,7 +518,7 @@ void data_nodes_init()
|
||||
uint64_to_base32(id64, device_id, sizeof(device_id), alphabet_crockford);
|
||||
#endif
|
||||
|
||||
eeprom_restore_data();
|
||||
data_storage_read();
|
||||
if (battery_conf_check(&bat_conf_user)) {
|
||||
battery_conf_overwrite(&bat_conf_user, &bat_conf, &charger);
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,265 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "data_storage.h"
|
||||
|
||||
#include <zephyr.h>
|
||||
|
||||
#include "mcu.h"
|
||||
#include "thingset.h"
|
||||
#include "data_nodes.h"
|
||||
#include "helper.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
|
||||
#include <device.h>
|
||||
#include <logging/log.h>
|
||||
LOG_MODULE_REGISTER(nvs, CONFIG_DATA_STORAGE_LOG_LEVEL);
|
||||
#include <stm32_ll_bus.h>
|
||||
|
||||
K_MUTEX_DEFINE(data_buf_lock);
|
||||
|
||||
// Buffer used by store and restore functions (must be word-aligned for hardware CRC calculation)
|
||||
static uint8_t buf[512] __aligned(sizeof(uint32_t));
|
||||
|
||||
extern ThingSet ts;
|
||||
|
||||
uint32_t _calc_crc(const uint8_t *buf, size_t len)
|
||||
{
|
||||
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_CRC);
|
||||
|
||||
// we keep standard polynomial 0x04C11DB7 (same for STM32L0 and STM32F0)
|
||||
// and we don't care for endianness here
|
||||
CRC->CR |= CRC_CR_RESET;
|
||||
for (size_t i = 0; i < len; i += 4) {
|
||||
//printf("CRC buf: %.8x, CRC->DR: %.8x\n", *((uint32_t*)&buf[i]), CRC->DR);
|
||||
size_t remaining_bytes = len - i;
|
||||
if (remaining_bytes >= 4) {
|
||||
CRC->DR = *((uint32_t*)&buf[i]);
|
||||
}
|
||||
else {
|
||||
// ignore bytes >= len if len is not a multiple of 4
|
||||
CRC->DR = *((uint32_t*)&buf[i]) & (0xFFFFFFFFU >> (32 - remaining_bytes * 8));
|
||||
}
|
||||
}
|
||||
|
||||
LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_CRC);
|
||||
|
||||
return CRC->DR;
|
||||
}
|
||||
|
||||
#endif /* UNIT_TEST */
|
||||
|
||||
#ifdef CONFIG_EEPROM
|
||||
|
||||
#include <drivers/eeprom.h>
|
||||
|
||||
/*
|
||||
* EEPROM header bytes:
|
||||
* - 0-1: Data nodes version number
|
||||
* - 2-3: Number of data bytes
|
||||
* - 4-7: CRC32
|
||||
*
|
||||
* Data starts from byte 8
|
||||
*/
|
||||
#define EEPROM_HEADER_SIZE 8
|
||||
|
||||
void data_storage_read()
|
||||
{
|
||||
int err;
|
||||
|
||||
const struct device *eeprom_dev = device_get_binding("EEPROM_0");
|
||||
|
||||
uint8_t buf_header[EEPROM_HEADER_SIZE] = {};
|
||||
err = eeprom_read(eeprom_dev, 0, buf_header, EEPROM_HEADER_SIZE);
|
||||
if (err != 0) {
|
||||
LOG_ERR("EEPROM read error %d", err);
|
||||
return;
|
||||
}
|
||||
uint16_t version = *((uint16_t*)&buf_header[0]);
|
||||
uint16_t len = *((uint16_t*)&buf_header[2]);
|
||||
uint32_t crc = *((uint32_t*)&buf_header[4]);
|
||||
|
||||
LOG_DBG("EEPROM header restore: ver %d, len %d, CRC %.8x", version, len, (unsigned int)crc);
|
||||
LOG_DBG("Header: %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x",
|
||||
buf_header[0], buf_header[1], buf_header[2], buf_header[3],
|
||||
buf_header[4], buf_header[5], buf_header[6], buf_header[7]);
|
||||
|
||||
if (version == DATA_NODES_VERSION && len <= sizeof(buf)) {
|
||||
k_mutex_lock(&data_buf_lock, K_FOREVER);
|
||||
err = eeprom_read(eeprom_dev, EEPROM_HEADER_SIZE, buf, len);
|
||||
|
||||
//printf("Data (len=%d): ", len);
|
||||
//for (int i = 0; i < len; i++) printf("%.2x ", buf[i]);
|
||||
|
||||
if (_calc_crc(buf, len) == crc) {
|
||||
int status = ts.bin_sub(buf, sizeof(buf), TS_WRITE_MASK, PUB_NVM);
|
||||
LOG_INF("EEPROM read and data nodes updated, ThingSet result: 0x%x", status);
|
||||
}
|
||||
else {
|
||||
LOG_ERR("EEPROM data CRC invalid, expected 0x%x (data_len = %d)",
|
||||
(unsigned int)crc, len);
|
||||
}
|
||||
k_mutex_unlock(&data_buf_lock);
|
||||
}
|
||||
else {
|
||||
LOG_INF("EEPROM empty or data layout version changed");
|
||||
}
|
||||
}
|
||||
|
||||
void data_storage_write()
|
||||
{
|
||||
int err;
|
||||
|
||||
const struct device *eeprom_dev = device_get_binding("EEPROM_0");
|
||||
|
||||
k_mutex_lock(&data_buf_lock, K_FOREVER);
|
||||
|
||||
int len = ts.bin_pub(buf + EEPROM_HEADER_SIZE, sizeof(buf) - EEPROM_HEADER_SIZE, PUB_NVM);
|
||||
uint32_t crc = _calc_crc(buf + EEPROM_HEADER_SIZE, len);
|
||||
|
||||
*((uint16_t*)&buf[0]) = (uint16_t)DATA_NODES_VERSION;
|
||||
*((uint16_t*)&buf[2]) = (uint16_t)(len);
|
||||
*((uint32_t*)&buf[4]) = crc;
|
||||
|
||||
//printf("Data (len=%d): ", len);
|
||||
//for (int i = 0; i < len; i++) printf("%.2x ", buf[i + EEPROM_HEADER_SIZE]);
|
||||
|
||||
LOG_DBG("Header: %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x",
|
||||
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
|
||||
|
||||
if (len == 0) {
|
||||
LOG_ERR("EEPROM data could not be stored. ThingSet error (len = %d)", len);
|
||||
}
|
||||
else {
|
||||
err = eeprom_write(eeprom_dev, 0, buf, len + EEPROM_HEADER_SIZE);
|
||||
if (err == 0) {
|
||||
LOG_INF("EEPROM data successfully stored");
|
||||
}
|
||||
else {
|
||||
LOG_ERR("EEPROM write error %d", err);
|
||||
}
|
||||
}
|
||||
k_mutex_unlock(&data_buf_lock);
|
||||
}
|
||||
|
||||
#elif defined(CONFIG_NVS)
|
||||
|
||||
#include <drivers/flash.h>
|
||||
#include <storage/flash_map.h>
|
||||
#include <fs/nvs.h>
|
||||
|
||||
/*
|
||||
* NVS header bytes:
|
||||
* - 0-1: Data nodes version number
|
||||
*
|
||||
* Data starts from byte 2
|
||||
*/
|
||||
#define NVS_HEADER_SIZE 2
|
||||
|
||||
#define FLASH_PARTITION_NODE DT_NODE_BY_FIXED_PARTITION_LABEL(storage)
|
||||
#define FLASH_DEVICE_NODE DT_MTD_FROM_FIXED_PARTITION(FLASH_PARTITION_NODE)
|
||||
|
||||
#define THINGSET_DATA_ID 1
|
||||
|
||||
static const struct device *flash_dev = DEVICE_DT_GET(FLASH_DEVICE_NODE);
|
||||
|
||||
static struct nvs_fs fs;
|
||||
static bool nvs_initialized = false;
|
||||
|
||||
static void data_storage_init()
|
||||
{
|
||||
int err;
|
||||
struct flash_pages_info page_info;
|
||||
|
||||
fs.offset = FLASH_AREA_OFFSET(storage);
|
||||
err = flash_get_page_info_by_offs(flash_dev, fs.offset, &page_info);
|
||||
if (err) {
|
||||
LOG_ERR("Unable to get flash page info");
|
||||
}
|
||||
fs.sector_size = page_info.size;
|
||||
fs.sector_count = FLASH_AREA_SIZE(storage) / page_info.size;
|
||||
|
||||
err = nvs_init(&fs, DT_LABEL(FLASH_DEVICE_NODE));
|
||||
if (err) {
|
||||
LOG_ERR("NVS init failed");
|
||||
}
|
||||
|
||||
nvs_initialized = true;
|
||||
}
|
||||
|
||||
void data_storage_read()
|
||||
{
|
||||
if (!nvs_initialized) {
|
||||
data_storage_init();
|
||||
}
|
||||
|
||||
int num_bytes = nvs_read(&fs, THINGSET_DATA_ID, &buf, sizeof(buf));
|
||||
|
||||
if (num_bytes < 0) {
|
||||
LOG_INF("NVS empty (read error %d)", num_bytes);
|
||||
return;
|
||||
}
|
||||
|
||||
uint16_t version = *((uint16_t*)&buf[0]);
|
||||
|
||||
if (version == DATA_NODES_VERSION) {
|
||||
k_mutex_lock(&data_buf_lock, K_FOREVER);
|
||||
|
||||
int status = ts.bin_sub(buf + NVS_HEADER_SIZE, num_bytes - NVS_HEADER_SIZE,
|
||||
TS_WRITE_MASK, PUB_NVM);
|
||||
LOG_INF("NVS read and data nodes updated, ThingSet result: 0x%x", status);
|
||||
|
||||
k_mutex_unlock(&data_buf_lock);
|
||||
}
|
||||
else {
|
||||
LOG_INF("NVS data layout version changed");
|
||||
}
|
||||
}
|
||||
|
||||
void data_storage_write()
|
||||
{
|
||||
if (!nvs_initialized) {
|
||||
data_storage_init();
|
||||
}
|
||||
|
||||
k_mutex_lock(&data_buf_lock, K_FOREVER);
|
||||
|
||||
*((uint16_t*)&buf[0]) = (uint16_t)DATA_NODES_VERSION;
|
||||
|
||||
int len = ts.bin_pub(buf + NVS_HEADER_SIZE, sizeof(buf) - NVS_HEADER_SIZE, PUB_NVM);
|
||||
|
||||
if (len == 0) {
|
||||
LOG_ERR("NVS data could not be stored. ThingSet error (len = %d)", len);
|
||||
}
|
||||
else {
|
||||
int ret = nvs_write(&fs, THINGSET_DATA_ID, &buf, len + NVS_HEADER_SIZE);
|
||||
if (ret == len + NVS_HEADER_SIZE) {
|
||||
LOG_INF("NVS data successfully stored");
|
||||
}
|
||||
else {
|
||||
LOG_ERR("NVS write error %d", ret);
|
||||
}
|
||||
}
|
||||
|
||||
k_mutex_unlock(&data_buf_lock);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
void data_storage_write() {;}
|
||||
void data_storage_read() {;}
|
||||
|
||||
#endif
|
||||
|
||||
void data_storage_update()
|
||||
{
|
||||
if (uptime() % DATA_UPDATE_INTERVAL == 0 && uptime() > 0) {
|
||||
data_storage_write();
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
/*
|
||||
* Copyright (c) 2017 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef EEPROM_H
|
||||
#define EEPROM_H
|
||||
#ifndef DATA_STORAGE_H_
|
||||
#define DATA_STORAGE_H_
|
||||
|
||||
#define DATA_UPDATE_INTERVAL (6*60*60) // update every 6 hours
|
||||
|
||||
/**
|
||||
* @file
|
||||
@@ -16,16 +18,16 @@
|
||||
/**
|
||||
* Store current charge controller data to EEPROM
|
||||
*/
|
||||
void eeprom_store_data();
|
||||
void data_storage_write();
|
||||
|
||||
/**
|
||||
* Restore charge controller data from EEPROM and write to variables in RAM
|
||||
*/
|
||||
void eeprom_restore_data();
|
||||
void data_storage_read();
|
||||
|
||||
/**
|
||||
* Stores data to EEPROM every 6 hours (can be called regularly)
|
||||
*/
|
||||
void eeprom_update();
|
||||
void data_storage_update();
|
||||
|
||||
#endif /* EEPROM_H */
|
||||
#endif /* DATA_STORAGE_H_ */
|
||||
+36
-25
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -9,10 +9,11 @@
|
||||
#include <zephyr.h>
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
#include <device.h>
|
||||
#include <drivers/gpio.h>
|
||||
|
||||
#include <logging/log.h>
|
||||
LOG_MODULE_REGISTER(dcdc, CONFIG_LOG_DEFAULT_LEVEL);
|
||||
LOG_MODULE_REGISTER(dcdc, CONFIG_DCDC_LOG_LEVEL);
|
||||
#endif
|
||||
|
||||
#include <math.h> // for fabs function
|
||||
@@ -22,14 +23,15 @@ LOG_MODULE_REGISTER(dcdc, CONFIG_LOG_DEFAULT_LEVEL);
|
||||
#include "device_status.h"
|
||||
#include "helper.h"
|
||||
#include "half_bridge.h"
|
||||
#include "eeprom.h"
|
||||
#include "data_storage.h"
|
||||
#include "setup.h"
|
||||
|
||||
extern DeviceStatus dev_stat;
|
||||
|
||||
#if DT_NODE_EXISTS(DT_PATH(dcdc))
|
||||
#if BOARD_HAS_DCDC
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), hv_ext_sense))
|
||||
#define HV_EXT_SENSE_GPIO DT_CHILD(DT_PATH(outputs), hv_ext_sense)
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), hv_out))
|
||||
#define HV_OUT_NODE DT_CHILD(DT_PATH(outputs), hv_out)
|
||||
#endif
|
||||
|
||||
Dcdc::Dcdc(DcBus *high, DcBus *low, DcdcOperationMode op_mode)
|
||||
@@ -39,7 +41,7 @@ Dcdc::Dcdc(DcBus *high, DcBus *low, DcdcOperationMode op_mode)
|
||||
mode = op_mode;
|
||||
enable = true;
|
||||
state = DCDC_CONTROL_OFF;
|
||||
inductor_current_max = DT_PROP(DT_PATH(dcdc), current_max);
|
||||
inductor_current_max = DT_PROP(DT_PATH(pcb), dcdc_current_max);
|
||||
hs_voltage_max = DT_PROP(DT_PATH(pcb), hs_voltage_max);
|
||||
ls_voltage_max = DT_PROP(DT_PATH(pcb), ls_voltage_max);
|
||||
ls_voltage_min = 9.0;
|
||||
@@ -49,8 +51,8 @@ Dcdc::Dcdc(DcBus *high, DcBus *low, DcdcOperationMode op_mode)
|
||||
pwm_delta = 1; // start-condition of duty cycle pwr_inc_pwm_direction
|
||||
|
||||
// lower duty limit might have to be adjusted dynamically depending on LS voltage
|
||||
half_bridge_init(DT_PROP(DT_PATH(dcdc), pwm_frequency) / 1000,
|
||||
DT_PROP(DT_PATH(dcdc), pwm_deadtime), 12 / hs_voltage_max, 0.97);
|
||||
half_bridge_init(DT_PROP(DT_INST(0, half_bridge), frequency) / 1000,
|
||||
DT_PROP(DT_INST(0, half_bridge), deadtime), 12 / hs_voltage_max, 0.97);
|
||||
}
|
||||
|
||||
int Dcdc::perturb_observe_controller()
|
||||
@@ -81,7 +83,7 @@ int Dcdc::perturb_observe_controller()
|
||||
|
||||
int pwr_inc_goal = 0; // stores if we want to increase (+1) or decrease (-1) power
|
||||
|
||||
if ((uptime() - power_good_timestamp > 10 || out_power < -1.0) && mode != DCDC_MODE_AUTO) {
|
||||
if ((uptime() - power_good_timestamp > 10 || out_power < -10.0) && mode != DCDC_MODE_AUTO) {
|
||||
// switch off after 10s low power or negative power (if not in nanogrid mode)
|
||||
pwr_inc_goal = 0;
|
||||
}
|
||||
@@ -128,20 +130,25 @@ int Dcdc::perturb_observe_controller()
|
||||
pwr_inc_goal = pwm_delta;
|
||||
}
|
||||
|
||||
#if CONFIG_LOG_DEFAULT_LEVEL == LOG_LEVEL_DBG
|
||||
#if CONFIG_DCDC_LOG_LEVEL == LOG_LEVEL_DBG
|
||||
// workaround as LOG_DBG does not support float printing
|
||||
printf("P: %.2fW (prev %.2fW), in: %.2fV %.2fA (limit %.2fA), "
|
||||
"out: %.2fV (target %.2fV) %.2fA (limit %.2fA), "
|
||||
printf("P: %.2fW (prev %.2fW), ind. current: %.2f, "
|
||||
"in: %.2fV, %.2fA margin, out: %.2fV (target %.2fV), %.2fA margin, "
|
||||
"PWM: %.1f, chg_state: %d, pwr_inc: %d, buck/boost: %d\n",
|
||||
out_power, power_prev, in->voltage, in->current, in->neg_current_limit,
|
||||
out->voltage, out->sink_voltage_intercept, out->current, out->pos_current_limit,
|
||||
out_power, power_prev, inductor_current, in->voltage, in->src_current_margin,
|
||||
out->voltage, out->sink_voltage_intercept, out->sink_current_margin,
|
||||
half_bridge_get_duty_cycle() * 100.0, state, pwr_inc_goal, pwr_inc_pwm_direction);
|
||||
#endif
|
||||
|
||||
power_prev = out_power;
|
||||
|
||||
#ifdef CONFIG_SOC_SERIES_STM32G4X
|
||||
// reduced step size for fast microcontroller
|
||||
half_bridge_set_ccr(half_bridge_get_ccr() + pwr_inc_goal * pwr_inc_pwm_direction * 3);
|
||||
#else
|
||||
// change duty cycle by single minimum step
|
||||
half_bridge_set_ccr(half_bridge_get_ccr() + pwr_inc_goal * pwr_inc_pwm_direction);
|
||||
#endif
|
||||
|
||||
return (pwr_inc_goal == 0);
|
||||
}
|
||||
@@ -322,7 +329,7 @@ void Dcdc::fuse_destruction()
|
||||
|
||||
if (counter > 20) { // wait 20s to be able to send out data
|
||||
LOG_ERR("Charge controller fuse destruction called!\n");
|
||||
eeprom_store_data();
|
||||
data_storage_write();
|
||||
half_bridge_stop();
|
||||
half_bridge_init(50, 0, 0, 0.98); // reset safety limits to allow 0% duty cycle
|
||||
half_bridge_set_duty_cycle(0);
|
||||
@@ -334,19 +341,23 @@ void Dcdc::fuse_destruction()
|
||||
|
||||
void Dcdc::output_hvs_enable()
|
||||
{
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), hv_ext_sense))
|
||||
const struct device *dev_hv_ext = device_get_binding(DT_GPIO_LABEL(HV_EXT_SENSE_GPIO, gpios));
|
||||
gpio_pin_configure(dev_hv_ext, DT_GPIO_PIN(HV_EXT_SENSE_GPIO, gpios),
|
||||
DT_GPIO_FLAGS(HV_EXT_SENSE_GPIO, gpios) | GPIO_OUTPUT_ACTIVE);
|
||||
#ifdef HV_OUT_NODE
|
||||
const struct device *hv_out = DEVICE_DT_GET(DT_GPIO_CTLR(HV_OUT_NODE, gpios));
|
||||
if (device_is_ready(hv_out)) {
|
||||
gpio_pin_configure(hv_out, DT_GPIO_PIN(HV_OUT_NODE, gpios),
|
||||
DT_GPIO_FLAGS(HV_OUT_NODE, gpios) | GPIO_OUTPUT_ACTIVE);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void Dcdc::output_hvs_disable()
|
||||
{
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), hv_ext_sense))
|
||||
const struct device *dev_hv_ext = device_get_binding(DT_GPIO_LABEL(HV_EXT_SENSE_GPIO, gpios));
|
||||
gpio_pin_configure(dev_hv_ext, DT_GPIO_PIN(HV_EXT_SENSE_GPIO, gpios),
|
||||
DT_GPIO_FLAGS(HV_EXT_SENSE_GPIO, gpios) | GPIO_OUTPUT_INACTIVE);
|
||||
#ifdef HV_OUT_NODE
|
||||
const struct device *hv_out = DEVICE_DT_GET(DT_GPIO_CTLR(HV_OUT_NODE, gpios));
|
||||
if (device_is_ready(hv_out)) {
|
||||
gpio_pin_configure(hv_out, DT_GPIO_PIN(HV_OUT_NODE, gpios),
|
||||
DT_GPIO_FLAGS(HV_OUT_NODE, gpios) | GPIO_OUTPUT_INACTIVE);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -354,4 +365,4 @@ void Dcdc::output_hvs_disable()
|
||||
|
||||
Dcdc::Dcdc(DcBus *high, DcBus *low, DcdcOperationMode op_mode) {}
|
||||
|
||||
#endif /* DT_NODE_EXISTS(DT_PATH(dcdc)) */
|
||||
#endif /* BOARD_HAS_DCDC */
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2019 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -68,7 +68,7 @@ void DeviceStatus::update_energy()
|
||||
grid_export_total_Wh_prev = grid_export_total_Wh;
|
||||
#endif
|
||||
solar_terminal.neg_energy_Wh = 0.0;
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
load.pos_energy_Wh = 0.0;
|
||||
#endif
|
||||
bat_terminal.pos_energy_Wh = 0.0;
|
||||
@@ -92,7 +92,7 @@ void DeviceStatus::update_energy()
|
||||
(solar_terminal.neg_energy_Wh > 0 ? solar_terminal.neg_energy_Wh : 0);
|
||||
#endif
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
load_out_total_Wh = load_out_total_Wh_prev +
|
||||
(load.pos_energy_Wh > 0 ? load.pos_energy_Wh : 0);
|
||||
#endif
|
||||
@@ -121,7 +121,7 @@ void DeviceStatus::update_min_max_values()
|
||||
}
|
||||
#endif
|
||||
|
||||
#if DT_NODE_EXISTS(DT_PATH(dcdc))
|
||||
#if BOARD_HAS_DCDC
|
||||
if (dcdc.inductor_current > dcdc_current_max) {
|
||||
dcdc_current_max = dcdc.inductor_current;
|
||||
}
|
||||
@@ -131,7 +131,7 @@ void DeviceStatus::update_min_max_values()
|
||||
}
|
||||
#endif
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
if (load.current > load_current_max) {
|
||||
load_current_max = load.current;
|
||||
}
|
||||
@@ -146,7 +146,7 @@ void DeviceStatus::update_min_max_values()
|
||||
}
|
||||
#endif
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
if (load.power > load_power_max_day) {
|
||||
load_power_max_day = load.power;
|
||||
if (load_power_max_day > load_power_max_total) {
|
||||
|
||||
+9
-3
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2019 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -89,12 +89,18 @@ enum ErrorFlag {
|
||||
*/
|
||||
ERR_DCDC_HS_MOSFET_SHORT = 1U << 14,
|
||||
|
||||
/** Short-circuit in HS MOSFET
|
||||
/** Overvoltage in HS MOSFET
|
||||
*
|
||||
* Set in Dcdc::control() and never cleared
|
||||
* Set in DAQ alert and never cleared
|
||||
*/
|
||||
ERR_DCDC_HS_OVERVOLTAGE = 1U << 15,
|
||||
|
||||
/** Overvoltage caused by PWM switch
|
||||
*
|
||||
* Set and cleared in PwmSwitch::control()
|
||||
*/
|
||||
ERR_PWM_SWITCH_OVERVOLTAGE = 1U << 16,
|
||||
|
||||
/** Mask to catch all error flags (up to 32 errors)
|
||||
*/
|
||||
ERR_ANY_ERROR = UINT32_MAX,
|
||||
|
||||
-159
@@ -1,159 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2017 Martin Jäger / Libre Solar
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "eeprom.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
#include <zephyr.h>
|
||||
#include <device.h>
|
||||
#include <drivers/eeprom.h>
|
||||
#endif
|
||||
|
||||
#include "mcu.h"
|
||||
#include "thingset.h"
|
||||
#include "data_nodes.h"
|
||||
#include "helper.h"
|
||||
|
||||
|
||||
#define EEPROM_HEADER_SIZE 8 // bytes
|
||||
|
||||
#define EEPROM_UPDATE_INTERVAL (6*60*60) // update every 6 hours
|
||||
|
||||
extern ThingSet ts;
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
|
||||
uint32_t _calc_crc(const uint8_t *buf, size_t len)
|
||||
{
|
||||
#if defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
RCC->AHB1ENR |= RCC_AHB1ENR_CRCEN;
|
||||
#else
|
||||
RCC->AHBENR |= RCC_AHBENR_CRCEN;
|
||||
#endif
|
||||
|
||||
// we keep standard polynomial 0x04C11DB7 (same for STM32L0 and STM32F0)
|
||||
// and we don't care for endianness here
|
||||
CRC->CR |= CRC_CR_RESET;
|
||||
for (size_t i = 0; i < len; i += 4) {
|
||||
//printf("CRC buf: %.8x, CRC->DR: %.8x\n", *((uint32_t*)&buf[i]), CRC->DR);
|
||||
size_t remaining_bytes = len - i;
|
||||
if (remaining_bytes >= 4) {
|
||||
CRC->DR = *((uint32_t*)&buf[i]);
|
||||
}
|
||||
else {
|
||||
// ignore bytes >= len if len is not a multiple of 4
|
||||
CRC->DR = *((uint32_t*)&buf[i]) & (0xFFFFFFFFU >> (32 - remaining_bytes * 8));
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
RCC->AHB1ENR &= ~(RCC_AHB1ENR_CRCEN);
|
||||
#else
|
||||
RCC->AHBENR &= ~(RCC_AHBENR_CRCEN);
|
||||
#endif
|
||||
|
||||
return CRC->DR;
|
||||
}
|
||||
|
||||
// EEPROM layout:
|
||||
// bytes 0-1: Version number
|
||||
// bytes 2-3: number of data bytes
|
||||
// bytes 4-7: CRC32
|
||||
// byte 8: start of data
|
||||
|
||||
void eeprom_restore_data()
|
||||
{
|
||||
uint8_t buf_req[300]; // ThingSet request buffer
|
||||
int err;
|
||||
|
||||
const struct device *eeprom_dev = device_get_binding("EEPROM_0");
|
||||
|
||||
// EEPROM header
|
||||
uint8_t buf_header[EEPROM_HEADER_SIZE] = {}; // initialize to avoid compiler warning
|
||||
err = eeprom_read(eeprom_dev, 0, buf_header, EEPROM_HEADER_SIZE);
|
||||
if (err != 0) {
|
||||
printf("EEPROM: read error: %d\n", err);
|
||||
return;
|
||||
}
|
||||
uint16_t version = *((uint16_t*)&buf_header[0]);
|
||||
uint16_t len = *((uint16_t*)&buf_header[2]);
|
||||
uint32_t crc = *((uint32_t*)&buf_header[4]);
|
||||
|
||||
//printf("EEPROM header restore: ver %d, len %d, CRC %.8x\n", version, len, (unsigned int)crc);
|
||||
//printf("Header: %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x\n",
|
||||
// buf_header[0], buf_header[1], buf_header[2], buf_header[3],
|
||||
// buf_header[4], buf_header[5], buf_header[6], buf_header[7]);
|
||||
|
||||
if (version == DATA_NODES_VERSION && len <= sizeof(buf_req)) {
|
||||
|
||||
err = eeprom_read(eeprom_dev, EEPROM_HEADER_SIZE, buf_req, len);
|
||||
|
||||
//printf("Data (len=%d): ", len);
|
||||
//for (int i = 0; i < len; i++) printf("%.2x ", buf_req[i]);
|
||||
|
||||
if (_calc_crc(buf_req, len) == crc) {
|
||||
int status = ts.bin_sub(buf_req, sizeof(buf_req), TS_WRITE_MASK, PUB_NVM);
|
||||
printf("EEPROM: Data objects read and updated, ThingSet result: %x\n", status);
|
||||
}
|
||||
else {
|
||||
printf("EEPROM: CRC of data not correct, expected 0x%x (data_len = %d)\n",
|
||||
(unsigned int)crc, len);
|
||||
}
|
||||
}
|
||||
else {
|
||||
printf("EEPROM: Empty or data layout version changed\n");
|
||||
}
|
||||
}
|
||||
|
||||
void eeprom_store_data()
|
||||
{
|
||||
uint8_t buf[300];
|
||||
|
||||
const struct device *eeprom_dev = device_get_binding("EEPROM_0");
|
||||
|
||||
int len = ts.bin_pub(buf + EEPROM_HEADER_SIZE, sizeof(buf) - EEPROM_HEADER_SIZE, PUB_NVM);
|
||||
uint32_t crc = _calc_crc(buf + EEPROM_HEADER_SIZE, len);
|
||||
|
||||
// store EEPROM_VERSION, number of bytes and CRC
|
||||
*((uint16_t*)&buf[0]) = (uint16_t)DATA_NODES_VERSION;
|
||||
*((uint16_t*)&buf[2]) = (uint16_t)(len); // length of data
|
||||
*((uint32_t*)&buf[4]) = crc;
|
||||
|
||||
//printf("Data (len=%d): ", len);
|
||||
//for (int i = 0; i < len; i++) printf("%.2x ", buf[i + EEPROM_HEADER_SIZE]);
|
||||
|
||||
//printf("Header: %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x\n",
|
||||
// buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
|
||||
|
||||
if (len == 0) {
|
||||
printf("EEPROM: Data could not be stored. ThingSet error (len = %d)\n", len);
|
||||
}
|
||||
else {
|
||||
int err = eeprom_write(eeprom_dev, 0, buf, len + EEPROM_HEADER_SIZE);
|
||||
if (err == 0) {
|
||||
printf("EEPROM: Data successfully stored.\n");
|
||||
}
|
||||
else {
|
||||
printf("EEPROM: Write error.\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
void eeprom_store_data() {;}
|
||||
void eeprom_restore_data() {;}
|
||||
|
||||
#endif
|
||||
|
||||
void eeprom_update()
|
||||
{
|
||||
if (uptime() % EEPROM_UPDATE_INTERVAL == 0 && uptime() > 0) {
|
||||
eeprom_store_data();
|
||||
}
|
||||
}
|
||||
+31
-22
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2018 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -10,13 +10,14 @@
|
||||
#include <device.h>
|
||||
#include <drivers/gpio.h>
|
||||
#include <drivers/can.h>
|
||||
#include <task_wdt/task_wdt.h>
|
||||
|
||||
#ifdef CONFIG_ISOTP
|
||||
#include <canbus/isotp.h>
|
||||
#endif
|
||||
|
||||
#include <logging/log.h>
|
||||
LOG_MODULE_REGISTER(ext_can, CONFIG_LOG_DEFAULT_LEVEL);
|
||||
LOG_MODULE_REGISTER(ext_can, CONFIG_CAN_LOG_LEVEL);
|
||||
|
||||
#include "hardware.h" // for software watchdog
|
||||
|
||||
@@ -28,25 +29,27 @@ LOG_MODULE_REGISTER(ext_can, CONFIG_LOG_DEFAULT_LEVEL);
|
||||
#endif
|
||||
|
||||
extern ThingSet ts;
|
||||
extern uint16_t ts_can_node_id;
|
||||
extern uint16_t can_node_addr;
|
||||
|
||||
const struct device *can_dev;
|
||||
|
||||
#ifdef CONFIG_ISOTP
|
||||
|
||||
#define RX_THREAD_STACK_SIZE 512
|
||||
#define RX_THREAD_STACK_SIZE 1024
|
||||
#define RX_THREAD_PRIORITY 2
|
||||
|
||||
const struct isotp_fc_opts fc_opts = {.bs = 8, .stmin = 0};
|
||||
|
||||
struct isotp_msg_id rx_addr = {
|
||||
.id_type = CAN_EXTENDED_IDENTIFIER,
|
||||
.use_ext_addr = 0 // Normal ISO-TP addressing (using only CAN ID)
|
||||
.use_ext_addr = 0, // Normal ISO-TP addressing (using only CAN ID)
|
||||
.use_fixed_addr = 1, // enable SAE J1939 compatible addressing
|
||||
};
|
||||
|
||||
struct isotp_msg_id tx_addr = {
|
||||
.id_type = CAN_EXTENDED_IDENTIFIER,
|
||||
.use_ext_addr = 0 // Normal ISO-TP addressing (using only CAN ID)
|
||||
.use_ext_addr = 0, // Normal ISO-TP addressing (using only CAN ID)
|
||||
.use_fixed_addr = 1, // enable SAE J1939 compatible addressing
|
||||
};
|
||||
|
||||
struct isotp_recv_ctx recv_ctx;
|
||||
@@ -54,7 +57,7 @@ struct isotp_recv_ctx recv_ctx;
|
||||
void send_complette_cb(int error_nr, void *arg)
|
||||
{
|
||||
ARG_UNUSED(arg);
|
||||
LOG_DBG("TX complete cb [%d]", error_nr);
|
||||
LOG_DBG("TX complete callback, err: %d", error_nr);
|
||||
}
|
||||
|
||||
void can_rx_thread()
|
||||
@@ -62,20 +65,20 @@ void can_rx_thread()
|
||||
int ret, rem_len;
|
||||
unsigned int received_len;
|
||||
struct net_buf *buf;
|
||||
static u8_t rx_buffer[100];
|
||||
static u8_t tx_buffer[500];
|
||||
static uint8_t rx_buffer[100];
|
||||
static uint8_t tx_buffer[500];
|
||||
|
||||
// CAN node ID retrieved from EEPROM --> reset necessary after change via ThingSet serial
|
||||
rx_addr.ext_id = ts_can_node_id << 8;
|
||||
tx_addr.ext_id = ts_can_node_id;
|
||||
|
||||
ret = isotp_bind(&recv_ctx, can_dev, &tx_addr, &rx_addr, &fc_opts, K_FOREVER);
|
||||
if (ret != ISOTP_N_OK) {
|
||||
LOG_DBG("Failed to bind to rx ID %d [%d]", rx_addr.ext_id, ret);
|
||||
return;
|
||||
}
|
||||
rx_addr.ext_id = TS_CAN_BASE_REQRESP | TS_CAN_PRIO_REQRESP | TS_CAN_TARGET_SET(can_node_addr);
|
||||
tx_addr.ext_id = TS_CAN_BASE_REQRESP | TS_CAN_PRIO_REQRESP | TS_CAN_SOURCE_SET(can_node_addr);
|
||||
|
||||
while (1) {
|
||||
ret = isotp_bind(&recv_ctx, can_dev, &rx_addr, &tx_addr, &fc_opts, K_FOREVER);
|
||||
if (ret != ISOTP_N_OK) {
|
||||
LOG_DBG("Failed to bind to rx ID %d [%d]", rx_addr.ext_id, ret);
|
||||
return;
|
||||
}
|
||||
|
||||
received_len = 0;
|
||||
do {
|
||||
rem_len = isotp_recv_net(&recv_ctx, &buf, K_FOREVER);
|
||||
@@ -94,14 +97,19 @@ void can_rx_thread()
|
||||
net_buf_unref(buf);
|
||||
} while (rem_len);
|
||||
|
||||
// we need to unbind the receive ctx so that control frames are received in the send ctx
|
||||
isotp_unbind(&recv_ctx);
|
||||
|
||||
if (received_len > 0) {
|
||||
LOG_DBG("Got %d bytes via ISO-TP. Processing ThingSet message.", received_len);
|
||||
LOG_DBG("RX buf: %x %x %x %x", rx_buffer[0], rx_buffer[1], rx_buffer[2], rx_buffer[3]);
|
||||
int resp_len = ts.process(rx_buffer, received_len, tx_buffer, sizeof(tx_buffer));
|
||||
LOG_DBG("TX buf: %x %x %x %x", tx_buffer[0], tx_buffer[1], tx_buffer[2], tx_buffer[3]);
|
||||
|
||||
if (resp_len > 0) {
|
||||
static struct isotp_send_ctx send_ctx;
|
||||
int ret = isotp_send(&send_ctx, can_dev, tx_buffer, resp_len,
|
||||
&tx_addr, &rx_addr, send_complette_cb, NULL);
|
||||
&recv_ctx.tx_addr, &recv_ctx.rx_addr, send_complette_cb, NULL);
|
||||
if (ret != ISOTP_N_OK) {
|
||||
LOG_DBG("Error while sending data to ID %d [%d]", tx_addr.ext_id, ret);
|
||||
}
|
||||
@@ -122,7 +130,7 @@ void can_pub_isr(uint32_t err_flags, void *arg)
|
||||
|
||||
void can_pub_thread()
|
||||
{
|
||||
int wdt_channel = watchdog_register(1100);
|
||||
int wdt_channel = task_wdt_add(1100, task_wdt_callback, (void *)k_current_get());
|
||||
|
||||
unsigned int can_id;
|
||||
uint8_t can_data[8];
|
||||
@@ -136,18 +144,19 @@ void can_pub_thread()
|
||||
int64_t t_start = k_uptime_get();
|
||||
|
||||
while (true) {
|
||||
watchdog_feed(wdt_channel);
|
||||
|
||||
task_wdt_feed(wdt_channel);
|
||||
|
||||
if (pub_can_enable) {
|
||||
int data_len = 0;
|
||||
int start_pos = 0;
|
||||
while ((data_len = ts.bin_pub_can(start_pos, PUB_CAN, ts_can_node_id, can_id, can_data))
|
||||
while ((data_len = ts.bin_pub_can(start_pos, PUB_CAN, can_node_addr, can_id, can_data))
|
||||
!= -1)
|
||||
{
|
||||
struct zcan_frame frame = {0};
|
||||
frame.id_type = CAN_EXTENDED_IDENTIFIER;
|
||||
frame.rtr = CAN_DATAFRAME;
|
||||
frame.ext_id = can_id;
|
||||
frame.id = can_id;
|
||||
memcpy(frame.data, can_data, 8);
|
||||
|
||||
if (data_len >= 0) {
|
||||
|
||||
+7
-7
@@ -1,10 +1,10 @@
|
||||
/*
|
||||
* Copyright (c) 2019 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#if CONFIG_EXT_OLED_DISPLAY
|
||||
#if CONFIG_UEXT_OLED_DISPLAY
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
@@ -93,7 +93,7 @@ void oled_update()
|
||||
}
|
||||
|
||||
// solar panel data
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch))
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
if (pwm_switch.active()) {
|
||||
#else
|
||||
if (half_bridge_enabled()) {
|
||||
@@ -108,7 +108,7 @@ void oled_update()
|
||||
len = snprintf(buf, sizeof(buf), "n/a");
|
||||
oled.writeString(buf, len);
|
||||
}
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch))
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
if (in_terminal.bus->voltage > bat_terminal.bus->voltage)
|
||||
#endif
|
||||
{
|
||||
@@ -146,7 +146,7 @@ void oled_update()
|
||||
|
||||
oled.setTextCursor(0, 56);
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch))
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
bool pwm_enabled = pwm_switch.active();
|
||||
float duty_cycle = pwm_switch.get_duty_cycle();
|
||||
#else
|
||||
@@ -178,7 +178,7 @@ void oled_thread()
|
||||
DT_GPIO_FLAGS(UEXT_EN_GPIO, gpios) | GPIO_OUTPUT_ACTIVE);
|
||||
#endif
|
||||
|
||||
oled.init(CONFIG_EXT_OLED_BRIGHTNESS);
|
||||
oled.init(CONFIG_UEXT_OLED_BRIGHTNESS);
|
||||
|
||||
while (true) {
|
||||
oled_update();
|
||||
@@ -188,4 +188,4 @@ void oled_thread()
|
||||
|
||||
K_THREAD_DEFINE(oled_thread_id, 1024, oled_thread, NULL, NULL, NULL, 6, 0, 1000);
|
||||
|
||||
#endif /* CONFIG_EXT_OLED_DISPLAY */
|
||||
#endif /* CONFIG_UEXT_OLED_DISPLAY */
|
||||
|
||||
+4
-3
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2018 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <zephyr.h>
|
||||
#include <sys/printk.h>
|
||||
#include <drivers/uart.h>
|
||||
#include <task_wdt/task_wdt.h>
|
||||
|
||||
#include "thingset.h"
|
||||
#include "hardware.h"
|
||||
@@ -114,13 +115,13 @@ void serial_thread()
|
||||
uint32_t last_call = 0;
|
||||
|
||||
// long watchdog timeout needed for ThingSet conf calls which write to slow EEPROM
|
||||
int wdt_channel = watchdog_register(500);
|
||||
int wdt_channel = task_wdt_add(500, task_wdt_callback, (void *)k_current_get());
|
||||
|
||||
uart_irq_callback_user_data_set(uart_dev, process_input, NULL);
|
||||
uart_irq_rx_enable(uart_dev);
|
||||
|
||||
while (true) {
|
||||
watchdog_feed(wdt_channel);
|
||||
task_wdt_feed(wdt_channel);
|
||||
|
||||
uint32_t now = k_uptime_get() / 1000;
|
||||
|
||||
|
||||
+122
-12
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -11,8 +11,20 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "mcu.h"
|
||||
#include "setup.h"
|
||||
|
||||
#if DT_NODE_EXISTS(DT_PATH(dcdc))
|
||||
#ifndef UNIT_TEST
|
||||
#include <soc.h>
|
||||
#include <pinmux/stm32/pinmux_stm32.h>
|
||||
#include <stm32_ll_bus.h>
|
||||
#endif
|
||||
|
||||
#if BOARD_HAS_DCDC
|
||||
|
||||
#define DT_DRV_COMPAT half_bridge
|
||||
|
||||
// Get address of used timer from board dts
|
||||
#define TIMER_ADDR DT_REG_ADDR(DT_PARENT(DT_DRV_INST(0)))
|
||||
|
||||
static uint16_t tim_ccr_min; // capture/compare register min/max
|
||||
static uint16_t tim_ccr_max;
|
||||
@@ -34,15 +46,15 @@ static uint16_t clamp_ccr(uint16_t ccr_target)
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
|
||||
// Get address of used timer from board dts
|
||||
#define PWM_TIMER_ADDR DT_REG_ADDR(DT_PHANDLE(DT_PATH(dcdc), timer))
|
||||
static const struct soc_gpio_pinctrl tim_pinctrl[] = ST_STM32_DT_INST_PINCTRL(0, 0);
|
||||
|
||||
#if PWM_TIMER_ADDR == TIM3_BASE
|
||||
#if TIMER_ADDR == TIM3_BASE
|
||||
|
||||
static void tim_init_registers(int freq_kHz)
|
||||
{
|
||||
// Enable TIM3 clock
|
||||
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
|
||||
stm32_dt_pinctrl_configure(tim_pinctrl, ARRAY_SIZE(tim_pinctrl), TIMER_ADDR);
|
||||
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3);
|
||||
|
||||
// No prescaler --> timer frequency == SystemClock
|
||||
TIM3->PSC = 0;
|
||||
@@ -112,12 +124,17 @@ bool half_bridge_enabled()
|
||||
return TIM3->CCER & TIM_CCER_CC3E;
|
||||
}
|
||||
|
||||
#elif PWM_TIMER_ADDR == TIM1_BASE
|
||||
#elif TIMER_ADDR == TIM1_BASE
|
||||
|
||||
static void tim_init_registers(int freq_kHz)
|
||||
{
|
||||
// Enable TIM1 clock
|
||||
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
|
||||
stm32_dt_pinctrl_configure(tim_pinctrl, ARRAY_SIZE(tim_pinctrl), TIMER_ADDR);
|
||||
|
||||
#ifdef LL_APB2_GRP1_PERIPH_TIM1
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1);
|
||||
#else
|
||||
LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_TIM1);
|
||||
#endif
|
||||
|
||||
// No prescaler --> timer frequency == SystemClock
|
||||
TIM1->PSC = 0;
|
||||
@@ -205,7 +222,100 @@ bool half_bridge_enabled()
|
||||
return TIM1->BDTR & TIM_BDTR_MOE;
|
||||
}
|
||||
|
||||
#endif // TIM1
|
||||
#elif TIMER_ADDR == HRTIM1_BASE
|
||||
|
||||
/*
|
||||
* Below initial HRTIM implementation only works for channel A and it does not make use of the
|
||||
* possible higher resolution yet.
|
||||
*/
|
||||
|
||||
#include <stm32_ll_system.h>
|
||||
#include <stm32_ll_bus.h>
|
||||
|
||||
static void tim_init_registers(int freq_kHz)
|
||||
{
|
||||
stm32_dt_pinctrl_configure(tim_pinctrl, ARRAY_SIZE(tim_pinctrl), TIMER_ADDR);
|
||||
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_HRTIM1);
|
||||
|
||||
// Enable periodic calibration of delay-locked loop (DLL) with lowest calibration period
|
||||
HRTIM1->sCommonRegs.DLLCR |= HRTIM_DLLCR_CALEN | HRTIM_DLLCR_CALRTE_0 | HRTIM_DLLCR_CALRTE_1;
|
||||
|
||||
// Wait for calibration to finish
|
||||
while ((HRTIM1_COMMON->ISR & HRTIM_ISR_DLLRDY) == 0) {;}
|
||||
|
||||
// Prescaler 32 --> SystemClock (same as normal timers like TIM1)
|
||||
HRTIM1_TIMA->TIMxCR |= (5U << HRTIM_TIMCR_CK_PSC_Pos);
|
||||
|
||||
// Prescaler 8 / 2^3 --> same frequency as HRTIM
|
||||
HRTIM1_TIMA->DTxR |= (3U << HRTIM_DTR_DTPRSC_Pos);
|
||||
|
||||
// Continuous mode operation
|
||||
HRTIM1_TIMA->TIMxCR |= HRTIM_TIMCR_CONT;
|
||||
|
||||
// Enable preloading with timer reset update trigger
|
||||
//HRTIM1_TIMA->TIMxCR |= HRTIM_TIMCR_PREEN | HRTIM_TIMCR_TRSTU;
|
||||
|
||||
// Enable preloading with timer E update trigger
|
||||
//HRTIM1_TIMA->TIMxCR |= HRTIM_TIMCR_PREEN | HRTIM_TIMCR_TEU;
|
||||
|
||||
// Timer period register
|
||||
HRTIM1_TIMA->PERxR = SystemCoreClock / (freq_kHz * 1000) + 1;
|
||||
|
||||
HRTIM1_TIMA->SETx1R = HRTIM_SET1R_PER;
|
||||
HRTIM1_TIMA->RSTx1R = HRTIM_SET1R_CMP1;
|
||||
|
||||
HRTIM1_TIMA->OUTxR = HRTIM_OUTR_DTEN;
|
||||
|
||||
// Set deadtime values and lock deadtime signs
|
||||
HRTIM1_TIMA->DTxR |=
|
||||
HRTIM_DTR_DTFSLK | (tim_dt_clocks << 16U) |
|
||||
HRTIM_DTR_DTRSLK | tim_dt_clocks;
|
||||
|
||||
// activate trigger for ADC
|
||||
HRTIM1_COMMON->CR1 = HRTIM_CR1_ADC1USRC_0; // ADC trigger update: Timer A
|
||||
HRTIM1_COMMON->ADC1R = HRTIM_ADC1R_AD1TAC3; // ADC trigger event: Timer A compare 3
|
||||
|
||||
// Start timer A
|
||||
HRTIM1->sMasterRegs.MCR = HRTIM_MCR_TACEN;
|
||||
}
|
||||
|
||||
void half_bridge_start()
|
||||
{
|
||||
HRTIM1->sCommonRegs.OENR = HRTIM_OENR_TA1OEN | HRTIM_OENR_TA2OEN;
|
||||
}
|
||||
|
||||
void half_bridge_stop()
|
||||
{
|
||||
HRTIM1->sCommonRegs.ODISR = HRTIM_ODISR_TA1ODIS | HRTIM_ODISR_TA2ODIS;
|
||||
}
|
||||
|
||||
uint16_t half_bridge_get_arr()
|
||||
{
|
||||
return HRTIM1_TIMA->PERxR;
|
||||
}
|
||||
|
||||
uint16_t half_bridge_get_ccr()
|
||||
{
|
||||
return HRTIM1_TIMA->CMP1xR;
|
||||
}
|
||||
|
||||
void half_bridge_set_ccr(uint16_t ccr)
|
||||
{
|
||||
HRTIM1_TIMA->CMP1xR = clamp_ccr(ccr);
|
||||
|
||||
// Trigger ADC for current measurement in the middle of the cycle.
|
||||
// A negative offset of 80 clocks was found to improve current measurement accuracy and
|
||||
// compensate the ADC delay (no risk of underflow as CCR is around 214 even at 0.1% duty).
|
||||
HRTIM1_TIMA->CMP3xR = HRTIM1_TIMA->CMP1xR / 2 - 80;
|
||||
}
|
||||
|
||||
bool half_bridge_enabled()
|
||||
{
|
||||
return (HRTIM1->sCommonRegs.OENR & (HRTIM_OENR_TA1OEN | HRTIM_OENR_TA2OEN)) > 0;
|
||||
}
|
||||
|
||||
#endif // HRTIM1
|
||||
|
||||
#else // UNIT_TEST
|
||||
|
||||
@@ -286,4 +396,4 @@ float half_bridge_get_duty_cycle()
|
||||
return (float)(half_bridge_get_ccr()) / half_bridge_get_arr();
|
||||
}
|
||||
|
||||
#endif // DT_NODE_EXISTS(DT_PATH(dcdc))
|
||||
#endif // BOARD_HAS_DCDC
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* Copyright (c) 2016 Martin Jäger / Libre Solar
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef HALF_BRIDGE_H_
|
||||
|
||||
+13
-101
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -12,17 +12,6 @@
|
||||
#include "leds.h"
|
||||
#include "setup.h"
|
||||
|
||||
#if defined(CONFIG_SOC_SERIES_STM32F0X)
|
||||
#define SYS_MEM_START 0x1FFFC800
|
||||
#define SRAM_END 0x20003FFF // 16k
|
||||
#elif defined(CONFIG_SOC_SERIES_STM32L0X)
|
||||
#define SYS_MEM_START 0x1FF00000
|
||||
#define SRAM_END 0X20004FFF // 20k
|
||||
#define FLASH_LAST_PAGE 0x0802FF80 // start address of last page (192 kbyte cat 5 devices)
|
||||
#endif
|
||||
|
||||
#define MAGIC_CODE_ADDR (SRAM_END - 0xF) // where the magic code is stored
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), boot0))
|
||||
@@ -31,93 +20,9 @@
|
||||
|
||||
#include <power/reboot.h>
|
||||
#include <drivers/gpio.h>
|
||||
#include <drivers/watchdog.h>
|
||||
|
||||
#include <logging/log.h>
|
||||
LOG_MODULE_REGISTER(hardware, CONFIG_LOG_DEFAULT_LEVEL);
|
||||
|
||||
#define MAX_SW_WDT_CHANNELS 5
|
||||
|
||||
struct sw_wdt_channel {
|
||||
int64_t check_in_time; // most recent check-in/feed-in time of a thread for sw_watchdog
|
||||
uint32_t timeout; // timeout of corresponding thread for sw_watchdog
|
||||
};
|
||||
|
||||
static const struct device *wdt;
|
||||
struct k_timer sw_wdt_timer;
|
||||
struct sw_wdt_channel sw_wdt_channels[MAX_SW_WDT_CHANNELS];
|
||||
|
||||
int sw_wdt_channel_count = 0;
|
||||
int hw_wdt_channel;
|
||||
|
||||
/*
|
||||
* Main software watchdog function called by kernel timer
|
||||
*/
|
||||
static void sw_watchdog(struct k_timer *timer_id);
|
||||
|
||||
void watchdog_init()
|
||||
{
|
||||
wdt = device_get_binding(DT_LABEL(DT_NODELABEL(iwdg)));
|
||||
if (!wdt) {
|
||||
LOG_DBG("Cannot get IWDG device");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
int watchdog_register(uint32_t timeout_ms)
|
||||
{
|
||||
if (sw_wdt_channel_count < MAX_SW_WDT_CHANNELS) {
|
||||
sw_wdt_channels[sw_wdt_channel_count].timeout = timeout_ms;
|
||||
return sw_wdt_channel_count++;
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
void watchdog_start()
|
||||
{
|
||||
struct wdt_timeout_cfg wdt_config;
|
||||
wdt_config.flags = WDT_FLAG_RESET_SOC;
|
||||
wdt_config.window.min = 0U;
|
||||
wdt_config.window.max = 1000U; // initialize with long timeout
|
||||
wdt_config.callback = NULL; // STM32 does not support callbacks
|
||||
|
||||
// look for smallest timeout in software watchdog channels
|
||||
for (int i = 0; i < sw_wdt_channel_count; i++) {
|
||||
if (sw_wdt_channels[i].timeout < wdt_config.window.max) {
|
||||
wdt_config.window.max = sw_wdt_channels[i].timeout;
|
||||
}
|
||||
}
|
||||
|
||||
// start timer for software watchdog
|
||||
k_timer_init(&sw_wdt_timer, sw_watchdog, NULL);
|
||||
k_timer_start(&sw_wdt_timer, K_MSEC(10), K_MSEC(10));
|
||||
|
||||
// finally start hardware watchdog
|
||||
wdt_setup(wdt, 0);
|
||||
hw_wdt_channel = wdt_install_timeout(wdt, &wdt_config);
|
||||
}
|
||||
|
||||
void watchdog_feed(int thread_id)
|
||||
{
|
||||
sw_wdt_channels[thread_id].check_in_time = k_uptime_get();
|
||||
}
|
||||
|
||||
static void sw_watchdog(struct k_timer *timer_id)
|
||||
{
|
||||
// feed also hardware watchdog
|
||||
wdt_feed(wdt, hw_wdt_channel);
|
||||
|
||||
int64_t current_time = k_uptime_get();
|
||||
|
||||
for (int i = 0; i < sw_wdt_channel_count; i++) {
|
||||
if ((current_time - sw_wdt_channels[i].check_in_time) > sw_wdt_channels[i].timeout) {
|
||||
LOG_ERR("Watchdog channel %d triggered!", i);
|
||||
reset_device();
|
||||
}
|
||||
}
|
||||
}
|
||||
LOG_MODULE_REGISTER(hardware, CONFIG_HW_LOG_LEVEL);
|
||||
|
||||
void start_stm32_bootloader()
|
||||
{
|
||||
@@ -130,10 +35,6 @@ void start_stm32_bootloader()
|
||||
|
||||
k_sleep(K_MSEC(100)); // wait for capacitor at BOOT0 pin to charge up
|
||||
reset_device();
|
||||
#elif defined(CONFIG_SOC_SERIES_STM32F0X)
|
||||
// place magic code at end of RAM and initiate reset
|
||||
*((uint32_t *)(MAGIC_CODE_ADDR)) = 0xDEADBEEF;
|
||||
reset_device();
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -142,10 +43,21 @@ void reset_device()
|
||||
sys_reboot(SYS_REBOOT_COLD);
|
||||
}
|
||||
|
||||
void task_wdt_callback(int channel_id, void *user_data)
|
||||
{
|
||||
printk("Task watchdog callback (channel: %d, thread: %s)\n",
|
||||
channel_id, k_thread_name_get((k_tid_t)user_data));
|
||||
|
||||
printk("Resetting device...\n");
|
||||
|
||||
sys_reboot(SYS_REBOOT_COLD);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
// dummy functions for unit tests
|
||||
void start_stm32_bootloader() {}
|
||||
void reset_device() {}
|
||||
void task_wdt_callback(int channel_id, void *user_data) {}
|
||||
|
||||
#endif /* UNIT_TEST */
|
||||
|
||||
+6
-35
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -13,40 +13,6 @@
|
||||
* Hardware-specific functions like timers, watchdog, bootloader
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* Initialization of IWDG
|
||||
*/
|
||||
void watchdog_init();
|
||||
|
||||
/**
|
||||
* Register watchdog for this thread
|
||||
*
|
||||
* @param timeout_ms Timeout in milliseconds
|
||||
*
|
||||
* @returns Assigned channel number or -1 in case of error
|
||||
*/
|
||||
int watchdog_register(uint32_t timeout_ms);
|
||||
|
||||
/**
|
||||
* Must be called after all channels have been registered
|
||||
*/
|
||||
void watchdog_start();
|
||||
|
||||
/**
|
||||
* Feed/Check-in watchdog for given channel
|
||||
*
|
||||
* @param channel Channel number assigned by watchdog_register function
|
||||
*/
|
||||
void watchdog_feed(int channel);
|
||||
|
||||
/** Timer for system control (main DC/DC or PWM control loop) is started
|
||||
*
|
||||
* @param freq_Hz Frequency in Hz (10 Hz suggested)
|
||||
*/
|
||||
void control_timer_start(int freq_Hz);
|
||||
|
||||
/** DC/DC or PWM control loop (implemented in main.cpp)
|
||||
*/
|
||||
void system_control();
|
||||
@@ -60,4 +26,9 @@ void start_stm32_bootloader();
|
||||
*/
|
||||
void reset_device();
|
||||
|
||||
/**
|
||||
* Callback for task watchdogs used in multiple threads
|
||||
*/
|
||||
void task_wdt_callback(int channel_id, void *user_data);
|
||||
|
||||
#endif /* HARDWARE_H */
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
+4
-3
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <zephyr.h>
|
||||
#ifndef UNIT_TEST
|
||||
#include <drivers/gpio.h>
|
||||
#include <task_wdt/task_wdt.h>
|
||||
#endif
|
||||
|
||||
#include "hardware.h"
|
||||
@@ -136,7 +137,7 @@ void leds_update_thread()
|
||||
bool flicker_state = true;
|
||||
const struct device *led_devs[NUM_LED_PINS];
|
||||
|
||||
int wdt_channel = watchdog_register(1000);
|
||||
int wdt_channel = task_wdt_add(1000, task_wdt_callback, (void *)k_current_get());
|
||||
|
||||
for (int pin = 0; pin < NUM_LED_PINS; pin++) {
|
||||
led_devs[pin] = device_get_binding(led_ports[pin]);
|
||||
@@ -146,7 +147,7 @@ void leds_update_thread()
|
||||
|
||||
while (true) {
|
||||
|
||||
watchdog_feed(wdt_channel);
|
||||
task_wdt_feed(wdt_channel);
|
||||
|
||||
// could be increased to value >NUM_LEDS to reduce on-time
|
||||
if (led_count >= NUM_LEDS) {
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
+17
-6
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -7,8 +7,9 @@
|
||||
#include "load.h"
|
||||
|
||||
#include <zephyr.h>
|
||||
#include "board.h"
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) || UNIT_TEST
|
||||
#if BOARD_HAS_LOAD_OUTPUT || UNIT_TEST
|
||||
|
||||
#include "leds.h"
|
||||
#include "device_status.h"
|
||||
@@ -23,14 +24,15 @@
|
||||
|
||||
extern DeviceStatus dev_stat;
|
||||
|
||||
LoadOutput::LoadOutput(DcBus *dc_bus, void (*drv_switch_fn)(bool), void (*drv_init_fn)()) :
|
||||
LoadOutput::LoadOutput(DcBus *dc_bus, void (*switch_fn)(bool), void (*init_fn)(), bool (*pgood_fn)()) :
|
||||
PowerPort(dc_bus),
|
||||
switch_set(drv_switch_fn)
|
||||
switch_set(switch_fn),
|
||||
pgood_check(pgood_fn)
|
||||
{
|
||||
state = LOAD_STATE_OFF;
|
||||
|
||||
// call driver initialization function
|
||||
drv_init_fn();
|
||||
init_fn();
|
||||
|
||||
switch_set(false);
|
||||
junction_temperature = 25; // starting point: 25°C
|
||||
@@ -61,6 +63,11 @@ void LoadOutput::control()
|
||||
oc_timestamp = uptime();
|
||||
}
|
||||
|
||||
if (pgood_check != NULL && !pgood_check()) {
|
||||
flags_set(&error_flags, ERR_LOAD_OVERCURRENT);
|
||||
oc_timestamp = uptime();
|
||||
}
|
||||
|
||||
// negative margin means sourcing current from bus is allowed
|
||||
if (bus->src_current_margin > -0.1F) {
|
||||
flags_set(&error_flags, ERR_LOAD_BUS_SRC_CURRENT);
|
||||
@@ -118,6 +125,10 @@ void LoadOutput::control()
|
||||
flags_clear(&error_flags, ERR_LOAD_OVERVOLTAGE);
|
||||
}
|
||||
|
||||
if (flags_check(&error_flags, ERR_LOAD_BUS_SRC_CURRENT) && bus->src_current_margin < 0) {
|
||||
flags_clear(&error_flags, ERR_LOAD_BUS_SRC_CURRENT);
|
||||
}
|
||||
|
||||
if (flags_check(&error_flags, ERR_LOAD_SHORT_CIRCUIT) && enable == false) {
|
||||
// stay here until the charge controller is reset or load is manually switched off
|
||||
flags_clear(&error_flags, ERR_LOAD_SHORT_CIRCUIT);
|
||||
@@ -165,4 +176,4 @@ void load_short_circuit_stop()
|
||||
load.stop(ERR_LOAD_SHORT_CIRCUIT);
|
||||
}
|
||||
|
||||
#endif /* DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) */
|
||||
#endif /* BOARD_HAS_LOAD_OUTPUT */
|
||||
|
||||
+12
-4
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -96,10 +96,11 @@ public:
|
||||
* Initialize LoadOutput struct and overcurrent / short circuit protection comparator (if existing)
|
||||
*
|
||||
* @param dc_bus DC bus the load is connected to
|
||||
* @param switch_set Pointer to function for enabling/disabling load switch
|
||||
* @param init Pointer to function for load driver initialization
|
||||
* @param switch_fn Pointer to function for enabling/disabling load switch
|
||||
* @param init_fn Pointer to function for load driver initialization
|
||||
* @param pgood_fn Pointer to pgood check
|
||||
*/
|
||||
LoadOutput(DcBus *dc_bus, void (*switch_fn)(bool), void (*init_fn)());
|
||||
LoadOutput(DcBus *dc_bus, void (*switch_fn)(bool), void (*init_fn)(), bool (*pgood_fn)());
|
||||
|
||||
/** Main load control function, should be called by control timer
|
||||
*
|
||||
@@ -158,6 +159,11 @@ private:
|
||||
*/
|
||||
void (*switch_set)(bool);
|
||||
|
||||
/**
|
||||
* Pointer to the function to check the power output status
|
||||
*/
|
||||
bool (*pgood_check)(void);
|
||||
|
||||
/**
|
||||
* Used to prevent switching of because of very short voltage dip
|
||||
*/
|
||||
@@ -183,6 +189,8 @@ void usb_out_set(bool);
|
||||
|
||||
void load_short_circuit_stop();
|
||||
|
||||
bool pgood_check();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
+42
-91
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -8,43 +8,37 @@
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
|
||||
#include <inttypes.h> // for PRIu32 in printf statements
|
||||
|
||||
#include <zephyr.h>
|
||||
#include <device.h>
|
||||
#include <drivers/gpio.h>
|
||||
#include <drivers/pwm.h>
|
||||
|
||||
#if defined(CONFIG_SOC_SERIES_STM32L0X)
|
||||
#include <stm32l0xx_ll_system.h>
|
||||
#elif defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
#include <stm32g4xx_ll_system.h>
|
||||
#endif
|
||||
#include "board.h"
|
||||
#include "hardware.h"
|
||||
#include "leds.h"
|
||||
#include "mcu.h"
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#include <stm32_ll_system.h>
|
||||
#include <stm32_ll_bus.h>
|
||||
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
#define LOAD_GPIO DT_CHILD(DT_PATH(outputs), load)
|
||||
static const struct device *dev_load;
|
||||
#endif
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr))
|
||||
#if BOARD_HAS_USB_OUTPUT
|
||||
#define USB_GPIO DT_CHILD(DT_PATH(outputs), usb_pwr)
|
||||
static const struct device *dev_usb;
|
||||
#endif
|
||||
|
||||
#include "hardware.h"
|
||||
#include "leds.h"
|
||||
#include "mcu.h"
|
||||
|
||||
// short circuit detection comparator only present in PWM 2420 LUS board so far
|
||||
#ifdef CONFIG_BOARD_PWM_2420_LUS
|
||||
|
||||
static void lptim_init()
|
||||
{
|
||||
// Enable peripheral clock of GPIOB
|
||||
RCC->IOPENR |= RCC_IOPENR_IOPBEN;
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_LPTIM1);
|
||||
|
||||
// Enable LPTIM clock
|
||||
RCC->APB1ENR |= RCC_APB1ENR_LPTIM1EN;
|
||||
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
|
||||
|
||||
// Select alternate function mode on PB2 (first bit _1 = 1, second bit _0 = 0)
|
||||
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE2)) | GPIO_MODER_MODE2_1;
|
||||
@@ -101,12 +95,12 @@ void ADC1_COMP_IRQHandler(void *args)
|
||||
|
||||
void short_circuit_comp_init()
|
||||
{
|
||||
// set GPIO pin to analog
|
||||
RCC->IOPENR |= RCC_IOPENR_GPIOBEN;
|
||||
GPIOB->MODER &= ~(GPIO_MODER_MODE4);
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
|
||||
|
||||
// enable SYSCFG clock
|
||||
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
|
||||
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
|
||||
|
||||
// set GPIO pin to analog
|
||||
GPIOB->MODER &= ~(GPIO_MODER_MODE4);
|
||||
|
||||
// enable VREFINT buffer
|
||||
SYSCFG->CFGR3 |= SYSCFG_CFGR3_ENBUFLP_VREFINT_COMP;
|
||||
@@ -150,7 +144,7 @@ void load_out_set(bool status)
|
||||
leds_set(LED_POS(load), status, LED_TIMEOUT_INFINITE);
|
||||
#endif
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
gpio_pin_configure(dev_load, DT_GPIO_PIN(LOAD_GPIO, gpios),
|
||||
DT_GPIO_FLAGS(LOAD_GPIO, gpios) | GPIO_OUTPUT_INACTIVE);
|
||||
if (status == true) {
|
||||
@@ -168,11 +162,15 @@ void load_out_set(bool status)
|
||||
|
||||
void usb_out_set(bool status)
|
||||
{
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr))
|
||||
#if BOARD_HAS_USB_OUTPUT
|
||||
gpio_pin_configure(dev_usb, DT_GPIO_PIN(USB_GPIO, gpios),
|
||||
DT_GPIO_FLAGS(USB_GPIO, gpios) | GPIO_OUTPUT_INACTIVE);
|
||||
if (status == true) {
|
||||
gpio_pin_set(dev_usb, DT_GPIO_PIN(USB_GPIO, gpios), 1);
|
||||
#if DT_PROP(DT_CHILD(DT_PATH(outputs), usb_pwr), latching_pgood)
|
||||
k_sleep(K_MSEC(50));
|
||||
gpio_pin_configure(dev_usb, DT_GPIO_PIN(USB_GPIO, gpios), GPIO_INPUT);
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
gpio_pin_set(dev_usb, DT_GPIO_PIN(USB_GPIO, gpios), 0);
|
||||
@@ -182,82 +180,24 @@ void usb_out_set(bool status)
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), charge_pump))
|
||||
|
||||
#if defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
#include <stm32g4xx_ll_tim.h>
|
||||
#include <stm32g4xx_ll_rcc.h>
|
||||
#include <stm32g4xx_ll_bus.h>
|
||||
#endif
|
||||
|
||||
#define CP_PWM_CONTROLLER (DT_PWMS_LABEL(DT_CHILD(DT_PATH(outputs), charge_pump)))
|
||||
#define CP_PWM_PERIOD (DT_PHA(DT_CHILD(DT_PATH(outputs), charge_pump), pwms, period))
|
||||
#define CP_PWM_CHANNEL (DT_PHA(DT_CHILD(DT_PATH(outputs), charge_pump), pwms, channel))
|
||||
|
||||
#if CP_PWM_CHANNEL == 1
|
||||
#define LL_TIM_CHANNEL LL_TIM_CHANNEL_CH1
|
||||
#define LL_TIM_OC_SetCompare LL_TIM_OC_SetCompareCH1
|
||||
#elif CP_PWM_CHANNEL == 2
|
||||
#define LL_TIM_CHANNEL LL_TIM_CHANNEL_CH2
|
||||
#define LL_TIM_OC_SetCompare LL_TIM_OC_SetCompareCH2
|
||||
#elif CP_PWM_CHANNEL == 3
|
||||
#define LL_TIM_CHANNEL LL_TIM_CHANNEL_CH3
|
||||
#define LL_TIM_OC_SetCompare LL_TIM_OC_SetCompareCH3
|
||||
#elif CP_PWM_CHANNEL == 4
|
||||
#define LL_TIM_CHANNEL LL_TIM_CHANNEL_CH4
|
||||
#define LL_TIM_OC_SetCompare LL_TIM_OC_SetCompareCH4
|
||||
#endif
|
||||
|
||||
/* Currently hard-coded for TIM8 as Zephyr driver doesn't work with this timer at the moment */
|
||||
void load_cp_enable()
|
||||
{
|
||||
int freq_Hz = 1000*1000*1000 / CP_PWM_PERIOD;
|
||||
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM8);
|
||||
|
||||
// Set timer clock to 100 kHz
|
||||
LL_TIM_SetPrescaler(TIM8, SystemCoreClock / 100000 - 1);
|
||||
|
||||
LL_TIM_OC_SetMode(TIM8, LL_TIM_CHANNEL, LL_TIM_OCMODE_PWM1);
|
||||
LL_TIM_OC_EnablePreload(TIM8, LL_TIM_CHANNEL);
|
||||
LL_TIM_OC_SetPolarity(TIM8, LL_TIM_CHANNEL, LL_TIM_OCPOLARITY_HIGH);
|
||||
|
||||
// Interrupt on timer update
|
||||
LL_TIM_EnableIT_UPDATE(TIM8);
|
||||
|
||||
// Force update generation (UG = 1)
|
||||
LL_TIM_GenerateEvent_UPDATE(TIM8);
|
||||
|
||||
// set PWM frequency and resolution
|
||||
int _pwm_resolution = 100000 / freq_Hz;
|
||||
|
||||
// Period goes from 0 to ARR (including ARR value), so substract 1 clock cycle
|
||||
LL_TIM_SetAutoReload(TIM8, _pwm_resolution - 1);
|
||||
|
||||
LL_TIM_EnableCounter(TIM8);
|
||||
|
||||
LL_TIM_OC_SetCompare(TIM8, _pwm_resolution / 2); // 50% duty
|
||||
|
||||
LL_TIM_CC_EnableChannel(TIM8, LL_TIM_CHANNEL);
|
||||
|
||||
LL_TIM_EnableAllOutputs(TIM8);
|
||||
|
||||
/*
|
||||
This should do the same using the Zephyr driver, but there seems to be a bug in Zephyr v2.2
|
||||
so that it doesn't work with advanced timer as TIM8. Keep it here until Zephyr bug was fixed.
|
||||
|
||||
const struct device *pwm_dev;
|
||||
pwm_dev = device_get_binding(DT_OUTPUTS_CHARGE_PUMP_PWMS_CONTROLLER);
|
||||
if (!pwm_dev) {
|
||||
LOG_ERR("Cannot find %s!\n", DT_OUTPUTS_CHARGE_PUMP_PWMS_CONTROLLER);
|
||||
return;
|
||||
const struct device *pwm_dev = device_get_binding(CP_PWM_CONTROLLER);
|
||||
if (pwm_dev) {
|
||||
// set to 50% duty cycle
|
||||
pwm_pin_set_nsec(pwm_dev, CP_PWM_CHANNEL, CP_PWM_PERIOD, CP_PWM_PERIOD / 2, 0);
|
||||
}
|
||||
// set to 50% duty cycle
|
||||
pwm_pin_set_nsec(pwm_dev, CP_PWM_CHANNEL, CP_PWM_PERIOD, CP_PWM_PERIOD / 2, 0);
|
||||
*/
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void load_out_init()
|
||||
{
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
dev_load = device_get_binding(DT_GPIO_LABEL(LOAD_GPIO, gpios));
|
||||
#endif
|
||||
|
||||
@@ -274,11 +214,20 @@ void load_out_init()
|
||||
|
||||
void usb_out_init()
|
||||
{
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr))
|
||||
#if BOARD_HAS_USB_OUTPUT
|
||||
dev_usb = device_get_binding(DT_GPIO_LABEL(USB_GPIO, gpios));
|
||||
#endif
|
||||
}
|
||||
|
||||
bool pgood_check()
|
||||
{
|
||||
#if BOARD_HAS_USB_OUTPUT
|
||||
return gpio_pin_get(dev_usb, DT_GPIO_PIN(USB_GPIO, gpios));
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
#else // UNIT_TEST
|
||||
|
||||
void load_out_init() {;}
|
||||
@@ -287,4 +236,6 @@ void usb_out_init() {;}
|
||||
void load_out_set(bool value) {;}
|
||||
void usb_out_set(bool value) {;}
|
||||
|
||||
bool pgood_check() { return false; }
|
||||
|
||||
#endif
|
||||
|
||||
+22
-20
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2019 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <stdio.h>
|
||||
#include <device.h>
|
||||
#include <drivers/gpio.h>
|
||||
#include <task_wdt/task_wdt.h>
|
||||
|
||||
#include "thingset.h" // handles access to internal data via communication interfaces
|
||||
#include "setup.h"
|
||||
@@ -21,7 +22,7 @@
|
||||
#include "pwm_switch.h" // PWM charge controller
|
||||
#include "bat_charger.h" // battery settings and charger state machine
|
||||
#include "daq.h" // ADC using DMA and conversion to measurement values
|
||||
#include "eeprom.h" // external I2C EEPROM
|
||||
#include "data_storage.h" // non-volatile data storage (e.g. EEPROM)
|
||||
#include "load.h" // load and USB output management
|
||||
#include "leds.h" // LED switching using charlieplexing
|
||||
#include "device_status.h" // log data (error memory, min/max measurements, etc.)
|
||||
@@ -29,16 +30,18 @@
|
||||
|
||||
void main(void)
|
||||
{
|
||||
printf("Libre Solar Charge Controller: %s\n", CONFIG_BOARD);
|
||||
printf("Hardware: Libre Solar %s (%s)\n",
|
||||
DT_PROP(DT_PATH(pcb), type), DT_PROP(DT_PATH(pcb), version_str));
|
||||
printf("Firmware: %s\n", FIRMWARE_VERSION_ID);
|
||||
|
||||
watchdog_init();
|
||||
task_wdt_init(device_get_binding(DT_LABEL(DT_NODELABEL(iwdg))));
|
||||
|
||||
setup();
|
||||
|
||||
battery_conf_init(&bat_conf, CONFIG_BAT_TYPE, CONFIG_BAT_NUM_CELLS, CONFIG_BAT_CAPACITY_AH);
|
||||
battery_conf_overwrite(&bat_conf, &bat_conf_user); // initialize conf_user with same values
|
||||
|
||||
#if DT_NODE_EXISTS(DT_PATH(dcdc))
|
||||
#if BOARD_HAS_DCDC
|
||||
daq_set_hv_limit(DT_PROP(DT_PATH(pcb), hs_voltage_max));
|
||||
#endif
|
||||
|
||||
@@ -59,19 +62,18 @@ void main(void)
|
||||
charger.detect_num_batteries(&bat_conf); // check if we have 24V instead of 12V system
|
||||
charger.init_terminal(&bat_conf);
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
load.set_voltage_limits(bat_conf.voltage_load_disconnect, bat_conf.voltage_load_reconnect,
|
||||
bat_conf.voltage_absolute_max);
|
||||
#endif
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr))
|
||||
#if BOARD_HAS_USB_OUTPUT
|
||||
usb_pwr.set_voltage_limits(bat_conf.voltage_load_disconnect - 0.1, // keep on longer than load
|
||||
bat_conf.voltage_load_reconnect, bat_conf.voltage_absolute_max);
|
||||
#endif
|
||||
|
||||
// wait until all threads are spawned before activating the watchdog
|
||||
k_sleep(K_MSEC(2500));
|
||||
watchdog_start();
|
||||
|
||||
int64_t t_start = k_uptime_get();
|
||||
|
||||
@@ -82,13 +84,13 @@ void main(void)
|
||||
charger.charge_control(&bat_conf);
|
||||
|
||||
// energy + soc calculation must be called exactly once per second
|
||||
#if DT_NODE_EXISTS(DT_PATH(dcdc))
|
||||
#if BOARD_HAS_DCDC
|
||||
if (dcdc.state != DCDC_CONTROL_OFF) {
|
||||
hv_terminal.energy_balance();
|
||||
}
|
||||
#endif
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch))
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
if (pwm_switch.active() == 1) {
|
||||
pwm_switch.energy_balance();
|
||||
}
|
||||
@@ -96,7 +98,7 @@ void main(void)
|
||||
|
||||
lv_terminal.energy_balance();
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
if (load.state == 1) {
|
||||
load.energy_balance();
|
||||
}
|
||||
@@ -106,7 +108,7 @@ void main(void)
|
||||
dev_stat.update_min_max_values();
|
||||
charger.update_soc(&bat_conf);
|
||||
|
||||
#if CONFIG_HS_MOSFET_FAIL_SAFE_PROTECTION && DT_NODE_EXISTS(DT_PATH(dcdc))
|
||||
#if CONFIG_HS_MOSFET_FAIL_SAFE_PROTECTION && BOARD_HAS_DCDC
|
||||
if (dev_stat.has_error(ERR_DCDC_HS_MOSFET_SHORT)) {
|
||||
dcdc.fuse_destruction();
|
||||
}
|
||||
@@ -114,13 +116,13 @@ void main(void)
|
||||
|
||||
leds_update_1s();
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
leds_update_soc(charger.soc, flags_check(&load.error_flags, ERR_LOAD_SHEDDING));
|
||||
#else
|
||||
leds_update_soc(charger.soc, false);
|
||||
#endif
|
||||
|
||||
eeprom_update();
|
||||
data_storage_update();
|
||||
|
||||
t_start += 1000;
|
||||
k_sleep(K_TIMEOUT_ABS_MS(t_start));
|
||||
@@ -129,14 +131,14 @@ void main(void)
|
||||
|
||||
void control_thread()
|
||||
{
|
||||
int wdt_channel = watchdog_register(200);
|
||||
int wdt_channel = task_wdt_add(200, task_wdt_callback, (void *)k_current_get());
|
||||
|
||||
while (true) {
|
||||
// control loop runs at approx. 10 Hz
|
||||
|
||||
bool charging = false;
|
||||
|
||||
watchdog_feed(wdt_channel);
|
||||
task_wdt_feed(wdt_channel);
|
||||
|
||||
// convert ADC readings to meaningful measurement values
|
||||
daq_update();
|
||||
@@ -146,12 +148,12 @@ void control_thread()
|
||||
|
||||
lv_terminal.update_bus_current_margins();
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch))
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
pwm_switch.control();
|
||||
charging |= pwm_switch.active();
|
||||
#endif
|
||||
|
||||
#if DT_NODE_EXISTS(DT_PATH(dcdc))
|
||||
#if BOARD_HAS_DCDC
|
||||
hv_terminal.update_bus_current_margins();
|
||||
dcdc.control(); // control of DC/DC including MPPT algorithm
|
||||
charging |= half_bridge_enabled();
|
||||
@@ -159,11 +161,11 @@ void control_thread()
|
||||
|
||||
leds_set_charging(charging);
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
load.control();
|
||||
#endif
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr))
|
||||
#if BOARD_HAS_USB_OUTPUT
|
||||
usb_pwr.control();
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2019 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2017 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
+21
-1
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2017 Martin Jäger / Libre Solar
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -30,6 +30,14 @@ public:
|
||||
*/
|
||||
float voltage;
|
||||
|
||||
/**
|
||||
* Filtered bus voltage
|
||||
*
|
||||
* Suitable filter time constants depend on type of DC bus. This filtering is applied in
|
||||
* addition to the raw ADC value filtering.
|
||||
*/
|
||||
float voltage_filtered = 0;
|
||||
|
||||
/**
|
||||
* Multiplier for series connection of batteries
|
||||
*
|
||||
@@ -84,6 +92,10 @@ public:
|
||||
/**
|
||||
* Available additional current from the DC bus until limits of the connected ports are reached
|
||||
* (has a negative sign)
|
||||
*
|
||||
* If for example a battery connected to the bus is not allowed to be discharged anymore, but
|
||||
* we still get power via solar, current up to the amount of solar input can be diverted into
|
||||
* the load (until the margin gets 0 and the battery would be discharged).
|
||||
*/
|
||||
float src_current_margin;
|
||||
|
||||
@@ -178,6 +190,14 @@ public:
|
||||
*/
|
||||
float current = 0;
|
||||
|
||||
/**
|
||||
* Filtered port current
|
||||
*
|
||||
* Suitable filter time constants depend on type of port. This filtering is applied in
|
||||
* addition to the raw ADC value filtering.
|
||||
*/
|
||||
float current_filtered = 0;
|
||||
|
||||
/**
|
||||
* Product of port current and bus voltage
|
||||
*/
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user