Compare commits
65 Commits
CAN
...
adc-testin
| Author | SHA1 | Date | |
|---|---|---|---|
| 84e7d02db8 | |||
| cc6b4488ee | |||
| 928a176e7c | |||
| 8255b2a672 | |||
| d48281436e | |||
| dcb73c0a25 | |||
| 2c21f1f9cb | |||
| a2afec52e2 | |||
| 2cc258e8e2 | |||
| a77298b3a6 | |||
| 45d011952f | |||
| bb25134b6c | |||
| 9f96384aa5 | |||
| b543579393 | |||
| 69cf7e9511 | |||
| 8df7aef51b | |||
| f6ee0a5122 | |||
| 6c1ff0c4df | |||
| 3f0d5a76c6 | |||
| 10a770de59 | |||
| 1b0519aadf | |||
| e429a0874d | |||
| 3a05c80b25 | |||
| 5208f1370d | |||
| a59e8518cc | |||
| 2a2890b675 | |||
| 38fd3a6aac | |||
| c3df6565b7 | |||
| 140d2baa24 | |||
| 711341f362 | |||
| a5da0a61dd | |||
| b54c73edb1 | |||
| 2418d4e218 | |||
| 2b4890f052 | |||
| 85d493f24a | |||
| f486d4c4ab | |||
| 6cfd4b8b4d | |||
| 0088030d66 | |||
| 4d828b41f1 | |||
| 95f435923f | |||
| 33f2a15cf3 | |||
| c4e87a3125 | |||
| 773027f6b0 | |||
| 461cce7a48 | |||
| 23b88ada83 | |||
| c2916662e2 | |||
| 24087f5622 | |||
| 95fd88e93e | |||
| 21797d8507 | |||
| 6dcb11ae0c | |||
| 38d6dbe95a | |||
| b100a8acf7 | |||
| 269e9e88a1 | |||
| 8cab3eecc1 | |||
| 6a9e4773ea | |||
| b836f9a2f4 | |||
| 032ddf2cc0 | |||
| 1067796df4 | |||
| 6f81e84541 | |||
| 0d3696bf93 | |||
| b005fd5c11 | |||
| 6c15b7021f | |||
| 842b204d36 | |||
| 5ce96a662d | |||
| fbeaa916b9 |
7
.gemini_commit_message.txt
Normal file
7
.gemini_commit_message.txt
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
feat(modbus): Implement persistent and improved reconfiguration for Modbus server
|
||||||
|
|
||||||
|
This commit enhances the Modbus server's configuration handling by:
|
||||||
|
|
||||||
|
- Loading saved baudrate and unit ID settings during initialization, ensuring persistence across reboots.
|
||||||
|
- Providing improved feedback during `modbus_reconfigure`, including logging for successful changes and informing the user when a device restart is required for changes to take effect.
|
||||||
|
- Saving new configuration settings even if immediate reinitialization fails, allowing them to be applied on the next boot.
|
||||||
5
.vscode/settings.json
vendored
Normal file
5
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"fwu.h": "c"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
<img src="./docs/img/logo.png" alt="Logo" width="100"/>
|
||||||
|
|
||||||
🇩🇪 Deutsch | [🇬🇧 English](README.md) | [🇫🇷 Français](README.fr.md) | [🇪🇸 Español](README.es.md)
|
🇩🇪 Deutsch | [🇬🇧 English](README.md) | [🇫🇷 Français](README.fr.md) | [🇪🇸 Español](README.es.md)
|
||||||
|
|
||||||
# Modulares Bewässerungssystem
|
# Modulares Bewässerungssystem
|
||||||
@@ -11,6 +13,8 @@ Die detaillierte Dokumentation befindet sich im Verzeichnis [`docs/`](./docs/):
|
|||||||
* **[Konzept](./docs/concept.de.md)**: Beschreibt die Systemarchitektur, die verwendeten Komponenten und die grundlegenden Design-Entscheidungen.
|
* **[Konzept](./docs/concept.de.md)**: Beschreibt die Systemarchitektur, die verwendeten Komponenten und die grundlegenden Design-Entscheidungen.
|
||||||
* **[MODBUS Register](./docs/modbus-registers.de.md)**: Definiert die Register-Map für die Kommunikation mit den Slave-Nodes.
|
* **[MODBUS Register](./docs/modbus-registers.de.md)**: Definiert die Register-Map für die Kommunikation mit den Slave-Nodes.
|
||||||
* **[Projektplan](./docs/planning.de.md)**: Enthält den Entwicklungs- und Implementierungsplan.
|
* **[Projektplan](./docs/planning.de.md)**: Enthält den Entwicklungs- und Implementierungsplan.
|
||||||
|
* **[Firmware-Handbuch](./docs/firmware-manual.de.md)**: Beschreibt den Funktionsumfang und die Bedienung der Slave-Node-Firmware.
|
||||||
|
* **[Modbus Test-Tool](./software/tools/modbus_tool/README.de.md)**: Anleitung für das Python-basierte Kommandozeilen-Tool zum Testen der Slaves.
|
||||||
|
|
||||||
## Schnellstart
|
## Schnellstart
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
<img src="./docs/img/logo.png" alt="Logo" width="100"/>
|
||||||
|
|
||||||
[🇩🇪 Deutsch](README.de.md) | [🇬🇧 English](README.md) | [🇫🇷 Français](README.fr.md) | 🇪🇸 Español
|
[🇩🇪 Deutsch](README.de.md) | [🇬🇧 English](README.md) | [🇫🇷 Français](README.fr.md) | 🇪🇸 Español
|
||||||
|
|
||||||
# Sistema de riego modular
|
# Sistema de riego modular
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
<img src="./docs/img/logo.png" alt="Logo" width="100"/>
|
||||||
|
|
||||||
[🇩🇪 Deutsch](README.de.md) | [🇬🇧 English](README.md) | 🇫🇷 Français | [🇪🇸 Español](README.es.md)
|
[🇩🇪 Deutsch](README.de.md) | [🇬🇧 English](README.md) | 🇫🇷 Français | [🇪🇸 Español](README.es.md)
|
||||||
|
|
||||||
# Système d'irrigation modulaire
|
# Système d'irrigation modulaire
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
<img src="./docs/img/logo.png" alt="Logo" width="100"/>
|
||||||
|
|
||||||
[🇩🇪 Deutsch](README.de.md) | 🇬🇧 English | [🇫🇷 Français](README.fr.md) | [🇪🇸 Español](README.es.md)
|
[🇩🇪 Deutsch](README.de.md) | 🇬🇧 English | [🇫🇷 Français](README.fr.md) | [🇪🇸 Español](README.es.md)
|
||||||
|
|
||||||
# Modular Irrigation System
|
# Modular Irrigation System
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
<img src="./img/logo.png" alt="Logo" width="100"/>
|
||||||
|
|
||||||
🇩🇪 Deutsch | [🇬🇧 English](concept.en.md) | [🇫🇷 Français](concept.fr.md) | [🇪🇸 Español](concept.es.md)
|
🇩🇪 Deutsch | [🇬🇧 English](concept.en.md) | [🇫🇷 Français](concept.fr.md) | [🇪🇸 Español](concept.es.md)
|
||||||
|
|
||||||
# Konzept: Modulares Bewässerungssystem
|
# Konzept: Modulares Bewässerungssystem
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
<img src="./img/logo.png" alt="Logo" width="100"/>
|
||||||
|
|
||||||
[🇩🇪 Deutsch](concept.de.md) | 🇬🇧 English | [🇫🇷 Français](concept.fr.md) | [🇪🇸 Español](concept.es.md)
|
[🇩🇪 Deutsch](concept.de.md) | 🇬🇧 English | [🇫🇷 Français](concept.fr.md) | [🇪🇸 Español](concept.es.md)
|
||||||
|
|
||||||
# Concept: Modular Irrigation System
|
# Concept: Modular Irrigation System
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
<img src="./img/logo.png" alt="Logo" width="100"/>
|
||||||
|
|
||||||
[🇩🇪 Deutsch](concept.de.md) | [🇬🇧 English](concept.en.md) | [🇫🇷 Français](concept.fr.md) | 🇪🇸 Español
|
[🇩🇪 Deutsch](concept.de.md) | [🇬🇧 English](concept.en.md) | [🇫🇷 Français](concept.fr.md) | 🇪🇸 Español
|
||||||
|
|
||||||
# Concepto: Sistema de riego modular
|
# Concepto: Sistema de riego modular
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
<img src="./img/logo.png" alt="Logo" width="100"/>
|
||||||
|
|
||||||
[🇩🇪 Deutsch](concept.de.md) | [🇬🇧 English](concept.en.md) | 🇫🇷 Français | [🇪🇸 Español](concept.es.md)
|
[🇩🇪 Deutsch](concept.de.md) | [🇬🇧 English](concept.en.md) | 🇫🇷 Français | [🇪🇸 Español](concept.es.md)
|
||||||
|
|
||||||
# Concept : Système d'irrigation modulaire
|
# Concept : Système d'irrigation modulaire
|
||||||
|
|||||||
BIN
docs/img/logo.png
Normal file
BIN
docs/img/logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 272 KiB |
16
docs/img/logo.svg
Normal file
16
docs/img/logo.svg
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<svg width="100" height="120" viewBox="0 0 100 120" xmlns="http://www.w3.org/2000/svg" aria-labelledby="logoTitle">
|
||||||
|
<title id="logoTitle">Logo: Wassertropfen mit integriertem Chip</title>
|
||||||
|
|
||||||
|
<path
|
||||||
|
d="M50 115 C 85 85, 95 65, 95 45 A 45 45 0 1 0 5 45 C 5 65, 15 85, 50 115 Z"
|
||||||
|
fill="#2563EB"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<g fill="#FFFFFF">
|
||||||
|
<rect x="25" y="35" width="50" height="8" rx="2"/>
|
||||||
|
<rect x="25" y="50" width="35" height="8" rx="2"/>
|
||||||
|
<rect x="70" y="50" width="5" height="8" rx="2"/>
|
||||||
|
<rect x="25" y="65" width="50" height="8" rx="2"/>
|
||||||
|
</g>
|
||||||
|
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 568 B |
@@ -1,3 +1,5 @@
|
|||||||
|
<img src="./img/logo.png" alt="Logo" width="100"/>
|
||||||
|
|
||||||
🇩🇪 Deutsch | [🇬🇧 English](modbus-registers.en.md) | [🇫🇷 Français](modbus-registers.fr.md) | [🇪🇸 Español](modbus-registers.es.md)
|
🇩🇪 Deutsch | [🇬🇧 English](modbus-registers.en.md) | [🇫🇷 Français](modbus-registers.fr.md) | [🇪🇸 Español](modbus-registers.es.md)
|
||||||
|
|
||||||
# MODBUS Register Map Definition v1.0
|
# MODBUS Register Map Definition v1.0
|
||||||
@@ -36,6 +38,7 @@ Alle Register sind in einer einzigen, durchgehenden Liste pro Register-Typ (`Inp
|
|||||||
| **0x00F2** | `DEVICE_STATUS` | System | `0`=OK, `1`=Allgemeiner Fehler. |
|
| **0x00F2** | `DEVICE_STATUS` | System | `0`=OK, `1`=Allgemeiner Fehler. |
|
||||||
| **0x00F3** | `UPTIME_SECONDS_LOW` | System | Untere 16 Bit der Uptime in Sekunden. |
|
| **0x00F3** | `UPTIME_SECONDS_LOW` | System | Untere 16 Bit der Uptime in Sekunden. |
|
||||||
| **0x00F4** | `UPTIME_SECONDS_HIGH` | System | Obere 16 Bit der Uptime. |
|
| **0x00F4** | `UPTIME_SECONDS_HIGH` | System | Obere 16 Bit der Uptime. |
|
||||||
|
| **0x00F5** | `SUPPLY_VOLTAGE_MV` | System | Aktuelle Versorgungsspannung in Millivolt (mV). |
|
||||||
| **0x0100** | `FWU_LAST_CHUNK_CRC` | Firmware-Update | Enthält den CRC16 des zuletzt im Puffer empfangenen Daten-Chunks. |
|
| **0x0100** | `FWU_LAST_CHUNK_CRC` | Firmware-Update | Enthält den CRC16 des zuletzt im Puffer empfangenen Daten-Chunks. |
|
||||||
|
|
||||||
## 3. Holding Registers (4xxxx, Read/Write)
|
## 3. Holding Registers (4xxxx, Read/Write)
|
||||||
@@ -47,6 +50,7 @@ Alle Register sind in einer einzigen, durchgehenden Liste pro Register-Typ (`Inp
|
|||||||
| **0x0002** | `MAX_SCHLIESSZEIT_S` | Ventil | Sicherheits-Timeout in Sekunden für den Schliessen-Vorgang. |
|
| **0x0002** | `MAX_SCHLIESSZEIT_S` | Ventil | Sicherheits-Timeout in Sekunden für den Schliessen-Vorgang. |
|
||||||
| **0x0010** | `DIGITAL_AUSGAENGE_ZUSTAND` | Ausgänge | Bitmaske zum Lesen und Schreiben der Ausgänge. Bit 0: Ausgang 1, Bit 1: Ausgang 2. `1`=AN, `0`=AUS. |
|
| **0x0010** | `DIGITAL_AUSGAENGE_ZUSTAND` | Ausgänge | Bitmaske zum Lesen und Schreiben der Ausgänge. Bit 0: Ausgang 1, Bit 1: Ausgang 2. `1`=AN, `0`=AUS. |
|
||||||
| **0x00F0** | `WATCHDOG_TIMEOUT_S` | System | Timeout des Fail-Safe-Watchdogs in Sekunden. `0`=Deaktiviert. |
|
| **0x00F0** | `WATCHDOG_TIMEOUT_S` | System | Timeout des Fail-Safe-Watchdogs in Sekunden. `0`=Deaktiviert. |
|
||||||
|
| **0x00F1** | `DEVICE_RESET` | System | Schreibt `1` um das Gerät neu zu starten. |
|
||||||
| **0x0100** | `FWU_COMMAND` | Firmware-Update | `1`: **Verify Chunk**: Der zuletzt übertragene Chunk wurde vom Client als gültig befunden. Der Slave soll ihn nun ins Flash schreiben. `2`: **Finalize Update**: Alle Chunks sind übertragen. Installation abschliessen und neu starten. |
|
| **0x0100** | `FWU_COMMAND` | Firmware-Update | `1`: **Verify Chunk**: Der zuletzt übertragene Chunk wurde vom Client als gültig befunden. Der Slave soll ihn nun ins Flash schreiben. `2`: **Finalize Update**: Alle Chunks sind übertragen. Installation abschliessen und neu starten. |
|
||||||
| **0x0101** | `FWU_CHUNK_OFFSET_LOW` | Firmware-Update | Untere 16 Bit des 32-Bit-Offsets, an den der nächste Chunk geschrieben werden soll. |
|
| **0x0101** | `FWU_CHUNK_OFFSET_LOW` | Firmware-Update | Untere 16 Bit des 32-Bit-Offsets, an den der nächste Chunk geschrieben werden soll. |
|
||||||
| **0x0102** | `FWU_CHUNK_OFFSET_HIGH` | Firmware-Update | Obere 16 Bit des 32-Bit-Offsets. |
|
| **0x0102** | `FWU_CHUNK_OFFSET_HIGH` | Firmware-Update | Obere 16 Bit des 32-Bit-Offsets. |
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
<img src="./img/logo.png" alt="Logo" width="100"/>
|
||||||
|
|
||||||
[🇩🇪 Deutsch](modbus-registers.de.md) | 🇬🇧 English | [🇫🇷 Français](modbus-registers.fr.md) | [🇪🇸 Español](modbus-registers.es.md)
|
[🇩🇪 Deutsch](modbus-registers.de.md) | 🇬🇧 English | [🇫🇷 Français](modbus-registers.fr.md) | [🇪🇸 Español](modbus-registers.es.md)
|
||||||
|
|
||||||
# MODBUS Register Map Definition v1.0
|
# MODBUS Register Map Definition v1.0
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
<img src="./img/logo.png" alt="Logo" width="100"/>
|
||||||
|
|
||||||
[🇩🇪 Deutsch](modbus-registers.de.md) | [🇬🇧 English](modbus-registers.en.md) | [🇫🇷 Français](modbus-registers.fr.md) | 🇪🇸 Español
|
[🇩🇪 Deutsch](modbus-registers.de.md) | [🇬🇧 English](modbus-registers.en.md) | [🇫🇷 Français](modbus-registers.fr.md) | 🇪🇸 Español
|
||||||
|
|
||||||
# Definición del mapa de registros MODBUS v1.0
|
# Definición del mapa de registros MODBUS v1.0
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
<img src="./img/logo.png" alt="Logo" width="100"/>
|
||||||
|
|
||||||
[🇩🇪 Deutsch](modbus-registers.de.md) | [🇬🇧 English](modbus-registers.en.md) | 🇫🇷 Français | [🇪🇸 Español](modbus-registers.es.md)
|
[🇩🇪 Deutsch](modbus-registers.de.md) | [🇬🇧 English](modbus-registers.en.md) | 🇫🇷 Français | [🇪🇸 Español](modbus-registers.es.md)
|
||||||
|
|
||||||
# Définition de la carte des registres MODBUS v1.0
|
# Définition de la carte des registres MODBUS v1.0
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
<img src="./img/logo.png" alt="Logo" width="100"/>
|
||||||
|
|
||||||
🇩🇪 Deutsch | [🇬🇧 English](planning.en.md) | [🇫🇷 Français](planning.fr.md) | [🇪🇸 Español](planning.es.md)
|
🇩🇪 Deutsch | [🇬🇧 English](planning.en.md) | [🇫🇷 Français](planning.fr.md) | [🇪🇸 Español](planning.es.md)
|
||||||
|
|
||||||
# Projektplan: Modulares Bewässerungssystem
|
# Projektplan: Modulares Bewässerungssystem
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
<img src="./img/logo.png" alt="Logo" width="100"/>
|
||||||
|
|
||||||
[🇩🇪 Deutsch](planning.de.md) | 🇬🇧 English | [🇫🇷 Français](planning.fr.md) | [🇪🇸 Español](planning.es.md)
|
[🇩🇪 Deutsch](planning.de.md) | 🇬🇧 English | [🇫🇷 Français](planning.fr.md) | [🇪🇸 Español](planning.es.md)
|
||||||
|
|
||||||
# Project Plan: Modular Irrigation System
|
# Project Plan: Modular Irrigation System
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
<img src="./img/logo.png" alt="Logo" width="100"/>
|
||||||
|
|
||||||
[🇩🇪 Deutsch](planning.de.md) | [🇬🇧 English](planning.en.md) | [🇫🇷 Français](planning.fr.md) | 🇪🇸 Español
|
[🇩🇪 Deutsch](planning.de.md) | [🇬🇧 English](planning.en.md) | [🇫🇷 Français](planning.fr.md) | 🇪🇸 Español
|
||||||
|
|
||||||
# Plan del proyecto: Sistema de riego modular
|
# Plan del proyecto: Sistema de riego modular
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
<img src="./img/logo.png" alt="Logo" width="100"/>
|
||||||
|
|
||||||
[🇩🇪 Deutsch](planning.de.md) | [🇬🇧 English](planning.en.md) | 🇫🇷 Français | [🇪🇸 Español](planning.es.md)
|
[🇩🇪 Deutsch](planning.de.md) | [🇬🇧 English](planning.en.md) | 🇫🇷 Français | [🇪🇸 Español](planning.es.md)
|
||||||
|
|
||||||
# Plan de projet : Système d'irrigation modulaire
|
# Plan de projet : Système d'irrigation modulaire
|
||||||
|
|||||||
5
software/.vscode/settings.json
vendored
5
software/.vscode/settings.json
vendored
@@ -8,5 +8,10 @@
|
|||||||
|
|
||||||
// File Associations
|
// File Associations
|
||||||
"files.associations": {
|
"files.associations": {
|
||||||
|
"array": "c",
|
||||||
|
"string_view": "c",
|
||||||
|
"initializer_list": "c",
|
||||||
|
"span": "c",
|
||||||
|
"format": "c"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
# SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
cmake_minimum_required(VERSION 3.20.0)
|
|
||||||
|
|
||||||
# This line should ideally be after project() and find_package(Zephyr)
|
|
||||||
# target_include_directories(app PRIVATE ${ZEPHYR_BASE}/include/zephyr/drivers) # <-- WRONG POSITION
|
|
||||||
|
|
||||||
list(APPEND BOARD_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
|
|
||||||
|
|
||||||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
|
||||||
project(valve_node)
|
|
||||||
|
|
||||||
target_include_directories(app PRIVATE ${ZEPHYR_BASE}/include/zephyr/drivers)
|
|
||||||
target_sources(app PRIVATE src/main2.c)
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
# SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
cmake_minimum_required(VERSION 3.20.0)
|
|
||||||
|
|
||||||
list(APPEND BOARD_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
|
|
||||||
|
|
||||||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
|
||||||
project(valve_node)
|
|
||||||
|
|
||||||
target_sources(app PRIVATE src/main.c)
|
|
||||||
target_sources(app PRIVATE lib/canbus.c)
|
|
||||||
|
|
||||||
# source files for modbus waterlevel sensor
|
|
||||||
zephyr_library_sources_ifdef(CONFIG_HAS_MODBUS_WATERLEVEL_SENSOR
|
|
||||||
lib/waterlevel_sensor.c
|
|
||||||
)
|
|
||||||
|
|
||||||
#source files for valve
|
|
||||||
zephyr_library_sources_ifdef(CONFIG_HAS_VALVE
|
|
||||||
lib/valve.c
|
|
||||||
)
|
|
||||||
|
|
||||||
zephyr_include_directories(
|
|
||||||
lib
|
|
||||||
)
|
|
||||||
1
software/Kconfig
Normal file
1
software/Kconfig
Normal file
@@ -0,0 +1 @@
|
|||||||
|
rsource "lib/Kconfig"
|
||||||
8
software/apps/adc_dt/CMakeLists.txt
Normal file
8
software/apps/adc_dt/CMakeLists.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.20.0)
|
||||||
|
|
||||||
|
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||||
|
project(ADC)
|
||||||
|
|
||||||
|
target_sources(app PRIVATE src/main.c)
|
||||||
62
software/apps/adc_dt/README.rst
Normal file
62
software/apps/adc_dt/README.rst
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
.. zephyr:code-sample:: adc_dt
|
||||||
|
:name: Analog-to-Digital Converter (ADC) with devicetree
|
||||||
|
:relevant-api: adc_interface
|
||||||
|
|
||||||
|
Read analog inputs from ADC channels.
|
||||||
|
|
||||||
|
Overview
|
||||||
|
********
|
||||||
|
|
||||||
|
This sample demonstrates how to use the :ref:`ADC driver API <adc_api>`.
|
||||||
|
|
||||||
|
Depending on the target board, it reads ADC samples from one or more channels
|
||||||
|
and prints the readings on the console. If voltage of the used reference can
|
||||||
|
be obtained, the raw readings are converted to millivolts.
|
||||||
|
|
||||||
|
The pins of the ADC channels are board-specific. Please refer to the board
|
||||||
|
or MCU datasheet for further details.
|
||||||
|
|
||||||
|
Building and Running
|
||||||
|
********************
|
||||||
|
|
||||||
|
The ADC peripheral and pinmux is configured in the board's ``.dts`` file. Make
|
||||||
|
sure that the ADC is enabled (``status = "okay";``).
|
||||||
|
|
||||||
|
In addition to that, this sample requires an ADC channel specified in the
|
||||||
|
``io-channels`` property of the ``zephyr,user`` node. This is usually done with
|
||||||
|
a devicetree overlay. The example overlay in the ``boards`` subdirectory for
|
||||||
|
the ``nucleo_l073rz`` board can be easily adjusted for other boards.
|
||||||
|
|
||||||
|
Configuration of channels (settings like gain, reference, or acquisition time)
|
||||||
|
also needs to be specified in devicetree, in ADC controller child nodes. Also
|
||||||
|
the ADC resolution and oversampling setting (if used) need to be specified
|
||||||
|
there. See :zephyr_file:`boards/nrf52840dk_nrf52840.overlay
|
||||||
|
<samples/drivers/adc/adc_dt/boards/nrf52840dk_nrf52840.overlay>` for an example of
|
||||||
|
such setup.
|
||||||
|
|
||||||
|
Building and Running for ST Nucleo L073RZ
|
||||||
|
=========================================
|
||||||
|
|
||||||
|
The sample can be built and executed for the
|
||||||
|
:zephyr:board:`nucleo_l073rz` as follows:
|
||||||
|
|
||||||
|
.. zephyr-app-commands::
|
||||||
|
:zephyr-app: samples/drivers/adc/adc_dt
|
||||||
|
:board: nucleo_l073rz
|
||||||
|
:goals: build flash
|
||||||
|
:compact:
|
||||||
|
|
||||||
|
To build for another board, change "nucleo_l073rz" above to that board's name
|
||||||
|
and provide a corresponding devicetree overlay.
|
||||||
|
|
||||||
|
Sample output
|
||||||
|
=============
|
||||||
|
|
||||||
|
You should get a similar output as below, repeated every second:
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
ADC reading:
|
||||||
|
- ADC_0, channel 7: 36 = 65mV
|
||||||
|
|
||||||
|
.. note:: If the ADC is not supported, the output will be an error message.
|
||||||
38
software/apps/adc_dt/boards/weact_stm32g431_core.overlay
Normal file
38
software/apps/adc_dt/boards/weact_stm32g431_core.overlay
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
/ {
|
||||||
|
vdd_sense: voltage-divider {
|
||||||
|
compatible = "voltage-divider";
|
||||||
|
/*
|
||||||
|
* This reference must provide one argument (the channel number)
|
||||||
|
* because of the "#io-channel-cells = <1>" in the &adc1 node.
|
||||||
|
*/
|
||||||
|
io-channels = <&adc1 1>;
|
||||||
|
output-ohms = <2200>;
|
||||||
|
full-ohms = <3200>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
&adc1 {
|
||||||
|
status = "okay";
|
||||||
|
|
||||||
|
pinctrl-0 = <&adc1_in1_pa0>;
|
||||||
|
pinctrl-names = "default";
|
||||||
|
|
||||||
|
st,adc-clock-source = "SYNC";
|
||||||
|
st,adc-prescaler = <4>;
|
||||||
|
|
||||||
|
#address-cells = <1>;
|
||||||
|
#size-cells = <0>;
|
||||||
|
/*
|
||||||
|
* This line is required by the st,stm32-adc driver binding.
|
||||||
|
* It declares that references to its channels need one extra argument.
|
||||||
|
*/
|
||||||
|
#io-channel-cells = <1>;
|
||||||
|
|
||||||
|
adc_channel_1: channel@1 {
|
||||||
|
reg = <1>;
|
||||||
|
zephyr,gain = "ADC_GAIN_1";
|
||||||
|
zephyr,reference = "ADC_REF_INTERNAL";
|
||||||
|
zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
|
||||||
|
zephyr,resolution = <12>;
|
||||||
|
};
|
||||||
|
};
|
||||||
4
software/apps/adc_dt/prj.conf
Normal file
4
software/apps/adc_dt/prj.conf
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
CONFIG_ADC=y
|
||||||
|
CONFIG_SENSOR=y
|
||||||
|
CONFIG_VOLTAGE_DIVIDER=y
|
||||||
|
CONFIG_LOG=y
|
||||||
53
software/apps/adc_dt/sample.yaml
Normal file
53
software/apps/adc_dt/sample.yaml
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
sample:
|
||||||
|
name: ADC devicetree driver sample
|
||||||
|
tests:
|
||||||
|
sample.drivers.adc.adc_dt:
|
||||||
|
tags:
|
||||||
|
- adc
|
||||||
|
depends_on: adc
|
||||||
|
platform_allow:
|
||||||
|
- nucleo_l073rz
|
||||||
|
- disco_l475_iot1
|
||||||
|
- cc3220sf_launchxl
|
||||||
|
- cc3235sf_launchxl
|
||||||
|
- cy8cproto_063_ble
|
||||||
|
- stm32l496g_disco
|
||||||
|
- stm32h735g_disco
|
||||||
|
- nrf51dk/nrf51822
|
||||||
|
- nrf52840dk/nrf52840
|
||||||
|
- nrf54l15dk/nrf54l15/cpuapp
|
||||||
|
- nrf54h20dk/nrf54h20/cpuapp
|
||||||
|
- ophelia4ev/nrf54l15/cpuapp
|
||||||
|
- mec172xevb_assy6906
|
||||||
|
- gd32f350r_eval
|
||||||
|
- gd32f450i_eval
|
||||||
|
- gd32vf103v_eval
|
||||||
|
- gd32f403z_eval
|
||||||
|
- esp32_devkitc/esp32/procpu
|
||||||
|
- esp32s2_saola
|
||||||
|
- esp32c3_devkitm
|
||||||
|
- gd32l233r_eval
|
||||||
|
- lpcxpresso55s36
|
||||||
|
- mr_canhubk3
|
||||||
|
- longan_nano
|
||||||
|
- longan_nano/gd32vf103/lite
|
||||||
|
- rd_rw612_bga
|
||||||
|
- frdm_mcxn947/mcxn947/cpu0
|
||||||
|
- mcx_n9xx_evk/mcxn947/cpu0
|
||||||
|
- frdm_mcxc242
|
||||||
|
- ucans32k1sic
|
||||||
|
- xg24_rb4187c
|
||||||
|
- xg29_rb4412a
|
||||||
|
- raytac_an54l15q_db/nrf54l15/cpuapp
|
||||||
|
- frdm_mcxa166
|
||||||
|
- frdm_mcxa276
|
||||||
|
integration_platforms:
|
||||||
|
- nucleo_l073rz
|
||||||
|
- nrf52840dk/nrf52840
|
||||||
|
harness: console
|
||||||
|
timeout: 10
|
||||||
|
harness_config:
|
||||||
|
type: multi_line
|
||||||
|
regex:
|
||||||
|
- "ADC reading\\[\\d+\\]:"
|
||||||
|
- "- .+, channel \\d+: -?\\d+"
|
||||||
25
software/apps/adc_dt/socs/esp32_procpu.overlay
Normal file
25
software/apps/adc_dt/socs/esp32_procpu.overlay
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022 Wolter HV <wolterhv@gmx.de>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
/ {
|
||||||
|
zephyr,user {
|
||||||
|
io-channels = <&adc0 0>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
&adc0 {
|
||||||
|
status = "okay";
|
||||||
|
#address-cells = <1>;
|
||||||
|
#size-cells = <0>;
|
||||||
|
|
||||||
|
channel@0 {
|
||||||
|
reg = <0>;
|
||||||
|
zephyr,gain = "ADC_GAIN_1_4";
|
||||||
|
zephyr,reference = "ADC_REF_INTERNAL";
|
||||||
|
zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
|
||||||
|
zephyr,resolution = <12>;
|
||||||
|
};
|
||||||
|
};
|
||||||
25
software/apps/adc_dt/socs/esp32c3.overlay
Normal file
25
software/apps/adc_dt/socs/esp32c3.overlay
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022 Wolter HV <wolterhv@gmx.de>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
/ {
|
||||||
|
zephyr,user {
|
||||||
|
io-channels = <&adc0 0>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
&adc0 {
|
||||||
|
status = "okay";
|
||||||
|
#address-cells = <1>;
|
||||||
|
#size-cells = <0>;
|
||||||
|
|
||||||
|
channel@0 {
|
||||||
|
reg = <0>;
|
||||||
|
zephyr,gain = "ADC_GAIN_1_4";
|
||||||
|
zephyr,reference = "ADC_REF_INTERNAL";
|
||||||
|
zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
|
||||||
|
zephyr,resolution = <12>;
|
||||||
|
};
|
||||||
|
};
|
||||||
25
software/apps/adc_dt/socs/esp32s2.overlay
Normal file
25
software/apps/adc_dt/socs/esp32s2.overlay
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022 Wolter HV <wolterhv@gmx.de>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
/ {
|
||||||
|
zephyr,user {
|
||||||
|
io-channels = <&adc0 0>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
&adc0 {
|
||||||
|
status = "okay";
|
||||||
|
#address-cells = <1>;
|
||||||
|
#size-cells = <0>;
|
||||||
|
|
||||||
|
channel@0 {
|
||||||
|
reg = <0>;
|
||||||
|
zephyr,gain = "ADC_GAIN_1_4";
|
||||||
|
zephyr,reference = "ADC_REF_INTERNAL";
|
||||||
|
zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
|
||||||
|
zephyr,resolution = <12>;
|
||||||
|
};
|
||||||
|
};
|
||||||
25
software/apps/adc_dt/socs/esp32s3_procpu.overlay
Normal file
25
software/apps/adc_dt/socs/esp32s3_procpu.overlay
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022 Wolter HV <wolterhv@gmx.de>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
/ {
|
||||||
|
zephyr,user {
|
||||||
|
io-channels = <&adc0 0>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
&adc0 {
|
||||||
|
status = "okay";
|
||||||
|
#address-cells = <1>;
|
||||||
|
#size-cells = <0>;
|
||||||
|
|
||||||
|
channel@0 {
|
||||||
|
reg = <0>;
|
||||||
|
zephyr,gain = "ADC_GAIN_1_4";
|
||||||
|
zephyr,reference = "ADC_REF_INTERNAL";
|
||||||
|
zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
|
||||||
|
zephyr,resolution = <12>;
|
||||||
|
};
|
||||||
|
};
|
||||||
45
software/apps/adc_dt/src/main.c
Normal file
45
software/apps/adc_dt/src/main.c
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
#include <zephyr/kernel.h>
|
||||||
|
#include <zephyr/device.h>
|
||||||
|
#include <zephyr/devicetree.h>
|
||||||
|
#include <zephyr/drivers/sensor.h>
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(adc_dt_example, LOG_LEVEL_DBG);
|
||||||
|
|
||||||
|
/* Get the voltage divider device */
|
||||||
|
#define VOLTAGE_DIVIDER_NODE DT_NODELABEL(vdd_sense)
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
const struct device *vdd_dev = DEVICE_DT_GET(VOLTAGE_DIVIDER_NODE);
|
||||||
|
struct sensor_value val;
|
||||||
|
int err;
|
||||||
|
|
||||||
|
if (!device_is_ready(vdd_dev)) {
|
||||||
|
LOG_ERR("Voltage divider device not ready");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG_INF("Voltage divider device ready!");
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
err = sensor_sample_fetch(vdd_dev);
|
||||||
|
if (err < 0) {
|
||||||
|
LOG_ERR("Could not fetch sample (%d)", err);
|
||||||
|
k_sleep(K_MSEC(1000));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = sensor_channel_get(vdd_dev, SENSOR_CHAN_VOLTAGE, &val);
|
||||||
|
if (err < 0) {
|
||||||
|
LOG_ERR("Could not get channel (%d)", err);
|
||||||
|
k_sleep(K_MSEC(1000));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG_INF("Voltage reading: %d.%06d V", val.val1, val.val2);
|
||||||
|
|
||||||
|
k_sleep(K_MSEC(1000));
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
6
software/apps/adc_test/CMakeLists.txt
Normal file
6
software/apps/adc_test/CMakeLists.txt
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
|
||||||
|
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||||
|
project(adc_test)
|
||||||
|
|
||||||
|
target_sources(app PRIVATE src/main.c)
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
&adc1 {
|
||||||
|
pinctrl-0 = <&adc1_in1_pa0>;
|
||||||
|
pinctrl-names = "default";
|
||||||
|
status = "okay";
|
||||||
|
|
||||||
|
st,adc-clock-source = "SYNC";
|
||||||
|
st,adc-prescaler = <4>;
|
||||||
|
};
|
||||||
3
software/apps/adc_test/prj.conf
Normal file
3
software/apps/adc_test/prj.conf
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
CONFIG_ADC=y
|
||||||
|
CONFIG_ADC_STM32=y
|
||||||
|
CONFIG_LOG=y
|
||||||
73
software/apps/adc_test/src/main.c
Normal file
73
software/apps/adc_test/src/main.c
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
#include <zephyr/kernel.h>
|
||||||
|
#include <zephyr/drivers/adc.h>
|
||||||
|
#include <zephyr/device.h>
|
||||||
|
#include <zephyr/sys/printk.h>
|
||||||
|
|
||||||
|
// ADC-Knoten holen
|
||||||
|
static const struct device *adc_dev = DEVICE_DT_GET(DT_NODELABEL(adc1));
|
||||||
|
|
||||||
|
// Kanaldefinitionen
|
||||||
|
#define MY_SIGNAL_CHANNEL 1 // PA0
|
||||||
|
#define ADC_VREFINT_CHANNEL 18 // Intern
|
||||||
|
|
||||||
|
// Puffer für ZWEI Messwerte
|
||||||
|
static int16_t sample_buffer[2];
|
||||||
|
|
||||||
|
void main(void)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
// Die VREFINT-Spannung in mV aus dem Datenblatt deines Controllers
|
||||||
|
#define VREFINT_MV 1212
|
||||||
|
|
||||||
|
printk("*** ADC Ratiometric Measurement (Single Sequence) ***\n");
|
||||||
|
|
||||||
|
if (!device_is_ready(adc_dev)) {
|
||||||
|
printk("ADC device not ready!\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Einmaliges Setup der beiden Kanäle ---
|
||||||
|
const struct adc_channel_cfg signal_channel_cfg = {
|
||||||
|
.gain = ADC_GAIN_1,
|
||||||
|
.reference = ADC_REF_INTERNAL,
|
||||||
|
.acquisition_time = ADC_ACQ_TIME_DEFAULT, // Kurz für niederohmige Quellen
|
||||||
|
.channel_id = MY_SIGNAL_CHANNEL,
|
||||||
|
};
|
||||||
|
const struct adc_channel_cfg vrefint_channel_cfg = {
|
||||||
|
.gain = ADC_GAIN_1,
|
||||||
|
.reference = ADC_REF_INTERNAL,
|
||||||
|
.acquisition_time = ADC_ACQ_TIME_MAX, // Lang für VREFINT
|
||||||
|
.channel_id = ADC_VREFINT_CHANNEL,
|
||||||
|
};
|
||||||
|
|
||||||
|
adc_channel_setup(adc_dev, &signal_channel_cfg);
|
||||||
|
adc_channel_setup(adc_dev, &vrefint_channel_cfg);
|
||||||
|
|
||||||
|
// --- EINE Sequenz, die BEIDE Kanäle enthält ---
|
||||||
|
const struct adc_sequence sequence = {
|
||||||
|
.channels = BIT(MY_SIGNAL_CHANNEL) | BIT(ADC_VREFINT_CHANNEL),
|
||||||
|
.buffer = sample_buffer,
|
||||||
|
.buffer_size = sizeof(sample_buffer),
|
||||||
|
.resolution = 12,
|
||||||
|
};
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
err = adc_read(adc_dev, &sequence);
|
||||||
|
if (err != 0) {
|
||||||
|
printk("ADC read failed with code %d\n", err);
|
||||||
|
} else {
|
||||||
|
// Die Ergebnisse sind in der Reihenfolge der Kanalnummern im Puffer
|
||||||
|
// Kanal 1 (MY_SIGNAL_CHANNEL) kommt vor Kanal 18 (ADC_VREFINT_CHANNEL)
|
||||||
|
int16_t signal_raw = sample_buffer[0];
|
||||||
|
int16_t vrefint_raw = sample_buffer[1];
|
||||||
|
|
||||||
|
// Ratiometrische Berechnung
|
||||||
|
int32_t signal_mv = (int32_t)signal_raw * VREFINT_MV / vrefint_raw;
|
||||||
|
|
||||||
|
printk("Signal: raw=%4d | VREFINT: raw=%4d | Calculated Voltage: %d mV\n",
|
||||||
|
signal_raw, vrefint_raw, signal_mv);
|
||||||
|
}
|
||||||
|
|
||||||
|
k_msleep(2000);
|
||||||
|
}
|
||||||
|
}
|
||||||
80
software/apps/adc_test/src/main.c2
Normal file
80
software/apps/adc_test/src/main.c2
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
#include <zephyr/kernel.h>
|
||||||
|
#include <zephyr/drivers/adc.h>
|
||||||
|
#include <zephyr/device.h>
|
||||||
|
|
||||||
|
// Definiere die Kanäle
|
||||||
|
#define ADC_VREFINT_CHANNEL 18 // Muss mit dem DTS übereinstimmen
|
||||||
|
#define MY_SIGNAL_CHANNEL 1 // Muss mit dem pinctrl im DTS übereinstimmen
|
||||||
|
|
||||||
|
// ADC Device
|
||||||
|
static const struct device *adc_dev = DEVICE_DT_GET(DT_NODELABEL(adc1));
|
||||||
|
|
||||||
|
// ADC Kanal Konfigurationen
|
||||||
|
static const struct adc_channel_cfg vrefint_channel_cfg = {
|
||||||
|
.gain = ADC_GAIN_1,
|
||||||
|
.reference = ADC_REF_INTERNAL, // Bedeutet VDDA
|
||||||
|
.acquisition_time = ADC_ACQ_TIME_MAX,
|
||||||
|
.channel_id = ADC_VREFINT_CHANNEL,
|
||||||
|
.differential = 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct adc_channel_cfg signal_channel_cfg = {
|
||||||
|
.gain = ADC_GAIN_1,
|
||||||
|
.reference = ADC_REF_INTERNAL, // Bedeutet VDDA
|
||||||
|
.acquisition_time = ADC_ACQ_TIME_MAX,
|
||||||
|
.channel_id = MY_SIGNAL_CHANNEL,
|
||||||
|
.differential = 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Puffer für die Messwerte
|
||||||
|
#define BUFFER_SIZE 1
|
||||||
|
static int16_t sample_buffer[BUFFER_SIZE];
|
||||||
|
|
||||||
|
// Sequenz für die Messungen
|
||||||
|
struct adc_sequence sequence_vrefint = {
|
||||||
|
.channels = BIT(ADC_VREFINT_CHANNEL),
|
||||||
|
.buffer = sample_buffer,
|
||||||
|
.buffer_size = sizeof(sample_buffer),
|
||||||
|
.resolution = 12, // STM32G4 hat 12-bit
|
||||||
|
};
|
||||||
|
|
||||||
|
struct adc_sequence sequence_signal = {
|
||||||
|
.channels = BIT(MY_SIGNAL_CHANNEL),
|
||||||
|
.buffer = sample_buffer,
|
||||||
|
.buffer_size = sizeof(sample_buffer),
|
||||||
|
.resolution = 12,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
void main(void) {
|
||||||
|
if (!device_is_ready(adc_dev)) {
|
||||||
|
printk("ADC device not found\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Kanäle konfigurieren
|
||||||
|
adc_channel_setup(adc_dev, &vrefint_channel_cfg);
|
||||||
|
adc_channel_setup(adc_dev, &signal_channel_cfg);
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
// 1. VREFINT messen zur Kalibrierung
|
||||||
|
adc_read(adc_dev, &sequence_vrefint);
|
||||||
|
int16_t vrefint_raw = sample_buffer[0];
|
||||||
|
|
||||||
|
// 2. Dein eigentliches Signal messen
|
||||||
|
adc_read(adc_dev, &sequence_signal);
|
||||||
|
int16_t signal_raw = sample_buffer[0];
|
||||||
|
|
||||||
|
// 3. Spannung berechnen
|
||||||
|
// VREFINT Wert für STM32G431 bei 3.0V Vdda ist typ. 1.212V (1212 mV)
|
||||||
|
// Überprüfe den genauen Wert im Datenblatt für deinen Controller!
|
||||||
|
#define VREFINT_MV 1212
|
||||||
|
|
||||||
|
int32_t signal_mv = (int32_t)signal_raw * VREFINT_MV / vrefint_raw;
|
||||||
|
|
||||||
|
printk("VREFINT raw: %d, Signal raw: %d, Calculated Voltage: %d mV\n",
|
||||||
|
vrefint_raw, signal_raw, signal_mv);
|
||||||
|
|
||||||
|
k_msleep(1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
38
software/apps/adc_test/src/main.tabby
Normal file
38
software/apps/adc_test/src/main.tabby
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
#include <zephyr.h>
|
||||||
|
#include <drivers/adc.h>
|
||||||
|
|
||||||
|
#define PA0_PIN 0x04
|
||||||
|
#define ADC_CHANNEL 0x03
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int16_t adc_value = 0;
|
||||||
|
|
||||||
|
// Initialize the ADC
|
||||||
|
adc_config_t adc_config;
|
||||||
|
adc_config.mode = ADC_MODE_SINGLE_SHOT;
|
||||||
|
adc_config.channel = ADC_CHANNEL_PA0;
|
||||||
|
adc_config.sampling_rate = ADC_SAMP_RATE_1MS;
|
||||||
|
|
||||||
|
adc_config.data_rate = ADC_DATA_RATE_4MS;
|
||||||
|
adc_config.aux = ADC_AUX_ALL;
|
||||||
|
|
||||||
|
adc_config.atten = ADC_ATTEN_DB_11;
|
||||||
|
adc_config.ref = ADC_REF_INTERNAL;
|
||||||
|
|
||||||
|
adc_config.cal = ADC_CAL_ALL;
|
||||||
|
|
||||||
|
if (adc_config_data(&adc_config, &adc_context) < 0) {
|
||||||
|
zephyr_printf("Failed to configure ADC\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the analog input value
|
||||||
|
if (adc_read(&adc_context, &adc_value) < 0) {
|
||||||
|
zephyr_printf("Failed to read ADC value\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
zephyr_printf("ADC Value: %d\n", adc_value);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
8
software/apps/firmware_node/CMakeLists.txt
Normal file
8
software/apps/firmware_node/CMakeLists.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
|
||||||
|
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||||
|
|
||||||
|
project(firmware_node LANGUAGES C)
|
||||||
|
zephyr_include_directories(../../include)
|
||||||
|
add_subdirectory(../../lib lib)
|
||||||
|
target_sources(app PRIVATE src/main.c)
|
||||||
34
software/apps/firmware_node/README.md
Normal file
34
software/apps/firmware_node/README.md
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
# Firmware Node Application
|
||||||
|
|
||||||
|
This Zephyr application provides firmware management capabilities for the irrigation system.
|
||||||
|
|
||||||
|
**Tested on Zephyr 4.1.99**
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
### Step 1: Shell with Reset Command
|
||||||
|
- Shell interface with custom "reset" command
|
||||||
|
- Warm reboot functionality
|
||||||
|
|
||||||
|
### Planned Features
|
||||||
|
- MCUboot support with partition manager
|
||||||
|
- Firmware version display
|
||||||
|
- MCUmgr support for OTA updates
|
||||||
|
|
||||||
|
## Building
|
||||||
|
|
||||||
|
```bash
|
||||||
|
west build -p auto -b weact_stm32g431_core apps/firmware_node -- -DBOARD_FLASH_RUNNER=blackmagicprobe
|
||||||
|
```
|
||||||
|
|
||||||
|
## Flashing
|
||||||
|
|
||||||
|
```bash
|
||||||
|
west flash
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
Connect to the device via serial console and use the shell:
|
||||||
|
- `reset` - Reboot the system
|
||||||
|
- `help` - Show available commands
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* Flash partition layout for STM32G431 (128KB total flash)
|
||||||
|
* MCUboot + single application slot configuration
|
||||||
|
*/
|
||||||
|
|
||||||
|
&flash0 {
|
||||||
|
partitions {
|
||||||
|
compatible = "fixed-partitions";
|
||||||
|
#address-cells = <1>;
|
||||||
|
#size-cells = <1>;
|
||||||
|
|
||||||
|
boot_partition: partition@0 {
|
||||||
|
label = "mcuboot";
|
||||||
|
reg = <0x00000000 0x0000A000>; /* 40 KB for MCUboot */
|
||||||
|
read-only;
|
||||||
|
};
|
||||||
|
|
||||||
|
slot0_partition: partition@A000 {
|
||||||
|
label = "image-0";
|
||||||
|
reg = <0x0000A000 0x00016000>; /* 88 KB for application */
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
/ {
|
||||||
|
chosen {
|
||||||
|
zephyr,code-partition = &slot0_partition;
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
# Board specific configuration for weact_stm32g431_core
|
||||||
|
# This file can be used for board-specific overrides if needed
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2021 Nordic Semiconductor ASA
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "flash_partitions_128kb.dtsi"
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
&flash0 {
|
||||||
|
partitions {
|
||||||
|
compatible = "fixed-partitions";
|
||||||
|
#address-cells = <1>;
|
||||||
|
#size-cells = <1>;
|
||||||
|
|
||||||
|
boot_partition: partition@0 {
|
||||||
|
label = "mcuboot";
|
||||||
|
reg = <0x00000000 0x00008000>; /* 32 KB */
|
||||||
|
read-only;
|
||||||
|
};
|
||||||
|
|
||||||
|
slot0_partition: partition@8000 {
|
||||||
|
label = "image-0";
|
||||||
|
reg = <0x00008000 0x00018000>; /* 96 KB */
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
25
software/apps/firmware_node/pm.yml
Normal file
25
software/apps/firmware_node/pm.yml
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
# Partition manager configuration for firmware_node
|
||||||
|
|
||||||
|
# Boot partition (MCUboot)
|
||||||
|
mcuboot_primary:
|
||||||
|
address: 0x00000000
|
||||||
|
size: 0x8000
|
||||||
|
region: flash_primary
|
||||||
|
|
||||||
|
# Application partition (primary slot)
|
||||||
|
mcuboot_primary_app:
|
||||||
|
address: 0x00008000
|
||||||
|
size: 0x18000
|
||||||
|
region: flash_primary
|
||||||
|
|
||||||
|
# Secondary slot for updates
|
||||||
|
mcuboot_secondary:
|
||||||
|
address: 0x00020000
|
||||||
|
size: 0x18000
|
||||||
|
region: flash_primary
|
||||||
|
|
||||||
|
# Settings partition
|
||||||
|
settings_partition:
|
||||||
|
address: 0x00038000
|
||||||
|
size: 0x8000
|
||||||
|
region: flash_primary
|
||||||
21
software/apps/firmware_node/prj.conf
Normal file
21
software/apps/firmware_node/prj.conf
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
# Enable Console and printk for logging
|
||||||
|
CONFIG_CONSOLE=y
|
||||||
|
CONFIG_LOG=y
|
||||||
|
CONFIG_LOG_PROCESS_THREAD=y
|
||||||
|
|
||||||
|
# Enable Shell
|
||||||
|
CONFIG_SHELL=y
|
||||||
|
CONFIG_REBOOT=y
|
||||||
|
|
||||||
|
# Enable the reset command
|
||||||
|
CONFIG_KERNEL_SHELL=y
|
||||||
|
|
||||||
|
# Enable settings for persistent storage
|
||||||
|
CONFIG_SETTINGS=y
|
||||||
|
CONFIG_SETTINGS_NVS=y
|
||||||
|
CONFIG_NVS=y
|
||||||
|
|
||||||
|
# Enable Flash and Flash Map for image trailer manipulation
|
||||||
|
CONFIG_FLASH=y
|
||||||
|
CONFIG_FLASH_MAP=y
|
||||||
|
CONFIG_FLASH_PAGE_LAYOUT=y
|
||||||
167
software/apps/firmware_node/src/main.c
Normal file
167
software/apps/firmware_node/src/main.c
Normal file
@@ -0,0 +1,167 @@
|
|||||||
|
#include <zephyr/kernel.h>
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
#include <zephyr/shell/shell.h>
|
||||||
|
#include <zephyr/sys/reboot.h>
|
||||||
|
#include <zephyr/drivers/flash.h>
|
||||||
|
#include <zephyr/storage/flash_map.h>
|
||||||
|
#include <zephyr/devicetree.h>
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(firmware_node, LOG_LEVEL_INF);
|
||||||
|
|
||||||
|
// Image header magic number (from MCUboot)
|
||||||
|
#define IMAGE_MAGIC 0x96f3b83d
|
||||||
|
#define IMAGE_HEADER_SIZE 32
|
||||||
|
|
||||||
|
// Function to invalidate current image and trigger serial recovery
|
||||||
|
static int invalidate_current_image(void)
|
||||||
|
{
|
||||||
|
const struct flash_area *fa;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
// Get the flash area for the current image slot (slot0_partition)
|
||||||
|
rc = flash_area_open(FIXED_PARTITION_ID(slot0_partition), &fa);
|
||||||
|
if (rc != 0) {
|
||||||
|
LOG_ERR("Failed to open flash area: %d", rc);
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure the flash area is valid
|
||||||
|
if (fa->fa_id != FIXED_PARTITION_ID(slot0_partition)) {
|
||||||
|
LOG_ERR("Invalid flash area ID: %d", fa->fa_id);
|
||||||
|
flash_area_close(fa);
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the flash device associated with this area
|
||||||
|
// This is necessary to perform erase operations
|
||||||
|
|
||||||
|
const struct device *flash_dev = flash_area_get_device(fa);
|
||||||
|
if (flash_dev == NULL) {
|
||||||
|
LOG_ERR("Failed to get flash device for area");
|
||||||
|
flash_area_close(fa);
|
||||||
|
return -ENODEV;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct flash_pages_info page_info;
|
||||||
|
off_t last_block_offset;
|
||||||
|
|
||||||
|
// Find the last block of the flash area
|
||||||
|
rc = flash_get_page_info_by_offs(flash_dev, fa->fa_off + fa->fa_size - 1, &page_info);
|
||||||
|
if (rc != 0) {
|
||||||
|
LOG_ERR("Failed to get page info: %d", rc);
|
||||||
|
flash_area_close(fa);
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate the last block offset
|
||||||
|
rc = flash_get_page_info_by_offs(flash_dev, fa->fa_off + fa->fa_size - 1, &page_info);
|
||||||
|
if (rc != 0) {
|
||||||
|
LOG_ERR("Failed to get page info: %d", rc);
|
||||||
|
flash_area_close(fa);
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
last_block_offset = page_info.start_offset;
|
||||||
|
|
||||||
|
// Convert absolute flash offset to relative offset within the flash area
|
||||||
|
off_t relative_offset = last_block_offset - fa->fa_off;
|
||||||
|
|
||||||
|
// Erase the image trailer/metadata at the end of the partition
|
||||||
|
LOG_INF("Erasing image trailer at absolute offset: %ld, relative offset: %ld, size: %d bytes",
|
||||||
|
last_block_offset, relative_offset, page_info.size);
|
||||||
|
rc = flash_area_erase(fa, relative_offset, page_info.size);
|
||||||
|
if (rc != 0) {
|
||||||
|
LOG_ERR("Failed to erase image trailer: %d", rc);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
LOG_INF("Image trailer erased successfully");
|
||||||
|
}
|
||||||
|
|
||||||
|
flash_area_close(fa);
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
// Custom reset command handler
|
||||||
|
static int cmd_reset(const struct shell *shell, size_t argc, char **argv)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(argc);
|
||||||
|
ARG_UNUSED(argv);
|
||||||
|
|
||||||
|
shell_print(shell, "Resetting system...");
|
||||||
|
k_msleep(100); // Give time for the message to be sent
|
||||||
|
sys_reboot(SYS_REBOOT_COLD);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// MCUboot serial recovery command handler
|
||||||
|
static int cmd_recovery(const struct shell *shell, size_t argc, char **argv)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(argc);
|
||||||
|
ARG_UNUSED(argv);
|
||||||
|
|
||||||
|
shell_print(shell, "Entering MCUboot serial recovery mode...");
|
||||||
|
shell_print(shell, "Corrupting current image magic to trigger recovery...");
|
||||||
|
|
||||||
|
// Invalidate the current image by corrupting its header
|
||||||
|
int rc = invalidate_current_image();
|
||||||
|
if (rc != 0) {
|
||||||
|
shell_error(shell, "Failed to invalidate image: %d", rc);
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
shell_print(shell, "Image magic corrupted. System will reset and MCUboot will detect bad image.");
|
||||||
|
shell_print(shell, "MCUboot should show error and wait for recovery.");
|
||||||
|
k_msleep(100); // Give time for the message to be sent
|
||||||
|
|
||||||
|
// Reset the system - MCUboot will detect invalid image and enter serial recovery
|
||||||
|
// log_process(true);
|
||||||
|
// sys_reboot(SYS_REBOOT_COLD);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command to show firmware info
|
||||||
|
static int cmd_info(const struct shell *shell, size_t argc, char **argv)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(argc);
|
||||||
|
ARG_UNUSED(argv);
|
||||||
|
|
||||||
|
const struct flash_area *fa;
|
||||||
|
int rc = flash_area_open(FIXED_PARTITION_ID(slot0_partition), &fa);
|
||||||
|
|
||||||
|
if (rc != 0) {
|
||||||
|
shell_error(shell, "Failed to open flash area: %d", rc);
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the first few bytes to check the image header
|
||||||
|
uint32_t magic;
|
||||||
|
rc = flash_area_read(fa, 0, &magic, sizeof(magic));
|
||||||
|
if (rc == 0) {
|
||||||
|
shell_print(shell, "Image magic: 0x%08x", magic);
|
||||||
|
if (magic == IMAGE_MAGIC) {
|
||||||
|
shell_print(shell, "Image header is valid");
|
||||||
|
shell_print(shell, "Image starts at flash offset: 0x%lx", (unsigned long)fa->fa_off);
|
||||||
|
shell_print(shell, "Image partition size: %d bytes", fa->fa_size);
|
||||||
|
} else {
|
||||||
|
shell_print(shell, "Image header is INVALID (expected 0x%08x)", IMAGE_MAGIC);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
shell_error(shell, "Failed to read image header: %d", rc);
|
||||||
|
}
|
||||||
|
|
||||||
|
flash_area_close(fa);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
SHELL_CMD_REGISTER(reset, NULL, "Reset the system", cmd_reset);
|
||||||
|
SHELL_CMD_REGISTER(recovery, NULL, "Enter MCUboot serial recovery mode", cmd_recovery);
|
||||||
|
SHELL_CMD_REGISTER(info, NULL, "Show firmware info", cmd_info);
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
LOG_INF("Firmware Node starting up");
|
||||||
|
LOG_INF("Shell with reset command available");
|
||||||
|
LOG_INF("Serial recovery command available");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
4
software/apps/firmware_node/sysbuild.cmake
Normal file
4
software/apps/firmware_node/sysbuild.cmake
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
# Sysbuild configuration for firmware_node with MCUboot
|
||||||
|
|
||||||
|
# Enable MCUboot as bootloader
|
||||||
|
set(SB_CONFIG_BOOTLOADER_MCUBOOT TRUE)
|
||||||
5
software/apps/firmware_node/sysbuild.conf
Normal file
5
software/apps/firmware_node/sysbuild.conf
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# Sysbuild configuration for firmware_node with MCUboot
|
||||||
|
|
||||||
|
# Enable MCUboot as bootloader
|
||||||
|
SB_CONFIG_BOOTLOADER_MCUBOOT=y
|
||||||
|
SB_CONFIG_MCUBOOT_MODE_SINGLE_APP=y
|
||||||
13
software/apps/firmware_node/sysbuild/firmware_node.overlay
Normal file
13
software/apps/firmware_node/sysbuild/firmware_node.overlay
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2021 Nordic Semiconductor ASA
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "../boards/flash_partitions_128kb.dtsi"
|
||||||
|
|
||||||
|
/ {
|
||||||
|
chosen {
|
||||||
|
zephyr,code-partition = &slot0_partition;
|
||||||
|
};
|
||||||
|
};
|
||||||
31
software/apps/firmware_node/sysbuild/mcuboot.conf
Normal file
31
software/apps/firmware_node/sysbuild/mcuboot.conf
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
# MCUboot configuration for firmware_node
|
||||||
|
# Enable basic console and logging for debugging
|
||||||
|
CONFIG_LOG=y
|
||||||
|
CONFIG_BOOT_BANNER=y
|
||||||
|
CONFIG_CONSOLE=y
|
||||||
|
CONFIG_UART_CONSOLE=y
|
||||||
|
CONFIG_PRINTK=y
|
||||||
|
|
||||||
|
# Single slot configuration (no upgrades)
|
||||||
|
CONFIG_SINGLE_APPLICATION_SLOT=y
|
||||||
|
|
||||||
|
# Enable serial recovery mode (temporarily commented out for debugging)
|
||||||
|
# CONFIG_MCUBOOT_SERIAL=y
|
||||||
|
# CONFIG_BOOT_SERIAL_UART=y
|
||||||
|
# CONFIG_BOOT_SERIAL_DETECT_PORT=y
|
||||||
|
|
||||||
|
# Disable signature validation for testing to save space
|
||||||
|
CONFIG_BOOT_SIGNATURE_TYPE_NONE=y
|
||||||
|
|
||||||
|
# Size optimizations to fit in 40KB flash
|
||||||
|
CONFIG_SIZE_OPTIMIZATIONS=y
|
||||||
|
CONFIG_CBPRINTF_NANO=y
|
||||||
|
CONFIG_MINIMAL_LIBC=y
|
||||||
|
CONFIG_ASSERT=n
|
||||||
|
|
||||||
|
# Disable debug features for size
|
||||||
|
CONFIG_DEBUG_INFO=n
|
||||||
|
CONFIG_DEBUG_OPTIMIZATIONS=n
|
||||||
|
|
||||||
|
# Minimal heap for size optimization
|
||||||
|
CONFIG_HEAP_MEM_POOL_SIZE=0
|
||||||
12
software/apps/firmware_node/sysbuild/mcuboot.overlay
Normal file
12
software/apps/firmware_node/sysbuild/mcuboot.overlay
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
/*
|
||||||
|
* MCUboot device tree overlay for firmware_node
|
||||||
|
* Uses shared flash partition layout
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "../boards/flash_partitions_128kb.dtsi"
|
||||||
|
|
||||||
|
/ {
|
||||||
|
chosen {
|
||||||
|
zephyr,code-partition = &boot_partition;
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* MCUboot specific overlay for weact_stm32g431_core
|
||||||
|
* This overlay defines flash partitions for MCUboot
|
||||||
|
*/
|
||||||
|
|
||||||
|
&flash0 {
|
||||||
|
partitions {
|
||||||
|
compatible = "fixed-partitions";
|
||||||
|
#address-cells = <1>;
|
||||||
|
#size-cells = <1>;
|
||||||
|
|
||||||
|
boot_partition: partition@0 {
|
||||||
|
label = "mcuboot";
|
||||||
|
reg = <0x00000000 0x00008000>;
|
||||||
|
};
|
||||||
|
slot0_partition: partition@8000 {
|
||||||
|
label = "image-0";
|
||||||
|
reg = <0x00008000 0x0000E000>;
|
||||||
|
};
|
||||||
|
slot1_partition: partition@16000 {
|
||||||
|
label = "image-1";
|
||||||
|
reg = <0x00016000 0x0000E000>;
|
||||||
|
};
|
||||||
|
storage_partition: partition@24000 {
|
||||||
|
label = "storage";
|
||||||
|
reg = <0x00024000 0x00004000>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
&chosen {
|
||||||
|
zephyr,boot-partition = &boot_partition;
|
||||||
|
};
|
||||||
9
software/apps/gateway/CMakeLists.txt
Normal file
9
software/apps/gateway/CMakeLists.txt
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
|
||||||
|
# Include the main 'software' directory as a module to find boards, libs, etc.
|
||||||
|
list(APPEND ZEPHYR_EXTRA_MODULES ${CMAKE_CURRENT_SOURCE_DIR}/../..)
|
||||||
|
|
||||||
|
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||||
|
project(gateway)
|
||||||
|
|
||||||
|
target_sources(app PRIVATE src/main.c)
|
||||||
2
software/apps/gateway/prj.conf
Normal file
2
software/apps/gateway/prj.conf
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
# Gateway Configuration
|
||||||
|
CONFIG_NETWORKING=y
|
||||||
13
software/apps/gateway/src/main.c
Normal file
13
software/apps/gateway/src/main.c
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2025 Eduard Iten
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <zephyr/kernel.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
printk("Hello from Gateway!\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
8
software/apps/slave_node/CMakeLists.txt
Normal file
8
software/apps/slave_node/CMakeLists.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
|
||||||
|
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||||
|
|
||||||
|
project(slave_node LANGUAGES C)
|
||||||
|
zephyr_include_directories(../../include)
|
||||||
|
add_subdirectory(../../lib lib)
|
||||||
|
target_sources(app PRIVATE src/main.c)
|
||||||
2
software/apps/slave_node/Kconfig
Normal file
2
software/apps/slave_node/Kconfig
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
rsource "../../lib/Kconfig"
|
||||||
|
source "Kconfig.zephyr"
|
||||||
7
software/apps/slave_node/boards/bluepill_f103rb.conf
Normal file
7
software/apps/slave_node/boards/bluepill_f103rb.conf
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# Disable UART console
|
||||||
|
CONFIG_UART_CONSOLE=n
|
||||||
|
|
||||||
|
# Enable RTT console
|
||||||
|
CONFIG_RTT_CONSOLE=y
|
||||||
|
CONFIG_USE_SEGGER_RTT=y
|
||||||
|
CONFIG_SHELL_BACKEND_RTT=y
|
||||||
43
software/apps/slave_node/boards/bluepill_f103rb.overlay
Normal file
43
software/apps/slave_node/boards/bluepill_f103rb.overlay
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
/ {
|
||||||
|
chosen {
|
||||||
|
zephyr,console = &rtt;
|
||||||
|
zephyr,shell = &rtt;
|
||||||
|
zephyr,settings-partition = &storage_partition;
|
||||||
|
};
|
||||||
|
|
||||||
|
rtt: rtt {
|
||||||
|
compatible = "segger,rtt-uart";
|
||||||
|
#address-cells = <1>;
|
||||||
|
#size-cells = <0>;
|
||||||
|
label = "RTT";
|
||||||
|
status = "okay";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
&flash0 {
|
||||||
|
partitions {
|
||||||
|
compatible = "fixed-partitions";
|
||||||
|
#address-cells = <1>;
|
||||||
|
#size-cells = <1>;
|
||||||
|
|
||||||
|
/* Application partition starts at the beginning of flash */
|
||||||
|
slot0_partition: partition@0 {
|
||||||
|
label = "image-0";
|
||||||
|
reg = <0x00000000 DT_SIZE_K(120)>;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Use the last 8K for settings */
|
||||||
|
storage_partition: partition@1E000 {
|
||||||
|
label = "storage";
|
||||||
|
reg = <0x0001E000 DT_SIZE_K(8)>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
&usart1 {
|
||||||
|
modbus0 {
|
||||||
|
compatible = "zephyr,modbus-serial";
|
||||||
|
status = "okay";
|
||||||
|
};
|
||||||
|
status = "okay";
|
||||||
|
};
|
||||||
64
software/apps/slave_node/boards/weact_stm32g431_core.overlay
Normal file
64
software/apps/slave_node/boards/weact_stm32g431_core.overlay
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
/ {
|
||||||
|
vnd7050aj: vnd7050aj {
|
||||||
|
compatible = "vnd7050aj-valve-controller";
|
||||||
|
status = "okay";
|
||||||
|
|
||||||
|
// VND7050AJ GPIO pin definitions
|
||||||
|
in0-gpios = <&gpiob 7 GPIO_ACTIVE_HIGH>; // IN0 (PB7) - Input 0 control signal
|
||||||
|
in1-gpios = <&gpiob 9 GPIO_ACTIVE_HIGH>; // IN1 (PB9) - Input 1 control signal
|
||||||
|
rst-gpios = <&gpiob 3 GPIO_ACTIVE_HIGH>; // RST (PB3) - Reset pin for VND7050AJ
|
||||||
|
sen-gpios = <&gpiob 4 GPIO_ACTIVE_HIGH>; // SEN (PB4) - Sense Enable for current monitoring
|
||||||
|
s0-gpios = <&gpiob 6 GPIO_ACTIVE_HIGH>; // S0 (PB6) - Status/Select 0 output from VND7050AJ
|
||||||
|
s1-gpios = <&gpiob 5 GPIO_ACTIVE_HIGH>; // S1 (PB5) - Status/Select 1 output from VND7050AJ
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
&usart1 {
|
||||||
|
modbus0 {
|
||||||
|
compatible = "zephyr,modbus-serial";
|
||||||
|
status = "okay";
|
||||||
|
};
|
||||||
|
status = "okay";
|
||||||
|
pinctrl-0 = <&usart1_tx_pa9 &usart1_rx_pa10>; // PA9=TX, PA10=RX for Modbus communication
|
||||||
|
pinctrl-names = "default";
|
||||||
|
};
|
||||||
|
|
||||||
|
&adc1 {
|
||||||
|
status = "okay";
|
||||||
|
pinctrl-0 = <&adc1_in1_pa0 &adc1_in15_pb0>;
|
||||||
|
pinctrl-names = "default";
|
||||||
|
st,adc-clock-source = "SYNC";
|
||||||
|
st,adc-prescaler = <1>;
|
||||||
|
#address-cells = <1>;
|
||||||
|
#size-cells = <0>;
|
||||||
|
|
||||||
|
channel@1 {
|
||||||
|
reg = <1>;
|
||||||
|
zephyr,gain = "ADC_GAIN_1";
|
||||||
|
zephyr,reference = "ADC_REF_INTERNAL";
|
||||||
|
zephyr,acquisition-time = <ADC_ACQ_TIME_MAX>; // Use maximum acquisition time for stability
|
||||||
|
zephyr,resolution = <12>;
|
||||||
|
zephyr,vref-mv = <2048>; // STM32G431 VREFBUF at 2.048V
|
||||||
|
};
|
||||||
|
|
||||||
|
channel@15 {
|
||||||
|
reg = <15>;
|
||||||
|
zephyr,gain = "ADC_GAIN_1";
|
||||||
|
zephyr,reference = "ADC_REF_INTERNAL";
|
||||||
|
zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
|
||||||
|
zephyr,resolution = <12>;
|
||||||
|
zephyr,vref-mv = <2048>; // STM32G431 VREFBUF at 2.048V
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
&pinctrl {
|
||||||
|
// Pinmux für PA0 als ADC1_IN1 (Analogmodus)
|
||||||
|
adc1_in1_pa0: adc1_in1_pa0 {
|
||||||
|
pinmux = <STM32_PINMUX('A', 0, ANALOG)>; // PA0 in den Analogmodus setzen
|
||||||
|
};
|
||||||
|
|
||||||
|
// Pinmux für PB0 als ADC1_IN15 (Analogmodus) - for lab supply testing
|
||||||
|
adc1_in15_pb0: adc1_in15_pb0 {
|
||||||
|
pinmux = <STM32_PINMUX('B', 0, ANALOG)>; // PB0 in den Analogmodus setzen
|
||||||
|
};
|
||||||
|
};
|
||||||
14
software/apps/slave_node/cdc-acm.overlay
Normal file
14
software/apps/slave_node/cdc-acm.overlay
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
&zephyr_udc0 {
|
||||||
|
cdc_acm_uart0: cdc_acm_uart0 {
|
||||||
|
compatible = "zephyr,cdc-acm-uart";
|
||||||
|
|
||||||
|
modbus0 {
|
||||||
|
compatible = "zephyr,modbus-serial";
|
||||||
|
status = "okay";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
&usart1 {
|
||||||
|
/delete-node/ modbus0;
|
||||||
|
};
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
# VND7050AJ Valve Controller binding
|
||||||
|
description: VND7050AJ valve controller GPIO configuration
|
||||||
|
|
||||||
|
compatible: "vnd7050aj-valve-controller"
|
||||||
|
|
||||||
|
properties:
|
||||||
|
in0-gpios:
|
||||||
|
type: phandle-array
|
||||||
|
description: GPIO for IN0 control signal
|
||||||
|
required: true
|
||||||
|
|
||||||
|
in1-gpios:
|
||||||
|
type: phandle-array
|
||||||
|
description: GPIO for IN1 control signal
|
||||||
|
required: true
|
||||||
|
|
||||||
|
rst-gpios:
|
||||||
|
type: phandle-array
|
||||||
|
description: GPIO for reset pin
|
||||||
|
required: true
|
||||||
|
|
||||||
|
sen-gpios:
|
||||||
|
type: phandle-array
|
||||||
|
description: GPIO for sense enable pin
|
||||||
|
required: true
|
||||||
|
|
||||||
|
s0-gpios:
|
||||||
|
type: phandle-array
|
||||||
|
description: GPIO for select 0 pin
|
||||||
|
required: true
|
||||||
|
|
||||||
|
s1-gpios:
|
||||||
|
type: phandle-array
|
||||||
|
description: GPIO for select 1 pin
|
||||||
|
required: true
|
||||||
4
software/apps/slave_node/overlay-cdc-acm.conf
Normal file
4
software/apps/slave_node/overlay-cdc-acm.conf
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
CONFIG_USB_DEVICE_STACK=y
|
||||||
|
CONFIG_USB_DEVICE_PRODUCT="Modbus slave node"
|
||||||
|
CONFIG_UART_LINE_CTRL=y
|
||||||
|
CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n
|
||||||
27
software/apps/slave_node/prj.conf
Normal file
27
software/apps/slave_node/prj.conf
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
# Enable Console and printk for logging
|
||||||
|
CONFIG_CONSOLE=y
|
||||||
|
CONFIG_LOG=y
|
||||||
|
|
||||||
|
# Enable Shell
|
||||||
|
CONFIG_SHELL=y
|
||||||
|
CONFIG_REBOOT=y
|
||||||
|
|
||||||
|
# Enable Settings Subsystem
|
||||||
|
CONFIG_SETTINGS=y
|
||||||
|
CONFIG_SETTINGS_NVS=y
|
||||||
|
CONFIG_NVS=y
|
||||||
|
CONFIG_FLASH=y
|
||||||
|
CONFIG_FLASH_MAP=y
|
||||||
|
CONFIG_FLASH_PAGE_LAYOUT=y
|
||||||
|
CONFIG_SETTINGS_LOG_LEVEL_DBG=y
|
||||||
|
|
||||||
|
# Config modbus
|
||||||
|
CONFIG_UART_INTERRUPT_DRIVEN=y
|
||||||
|
CONFIG_MODBUS=y
|
||||||
|
CONFIG_MODBUS_ROLE_SERVER=y
|
||||||
|
CONFIG_MODBUS_BUFFER_SIZE=256
|
||||||
|
|
||||||
|
# Enable ADC driver
|
||||||
|
CONFIG_ADC=y
|
||||||
|
CONFIG_ADC_STM32=y
|
||||||
|
|
||||||
35
software/apps/slave_node/src/main.c
Normal file
35
software/apps/slave_node/src/main.c
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#include <zephyr/kernel.h>
|
||||||
|
#include <zephyr/settings/settings.h>
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
#include <lib/modbus_server.h>
|
||||||
|
#include <lib/valve.h>
|
||||||
|
#include <lib/fwu.h>
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(main, LOG_LEVEL_INF);
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
LOG_INF("Starting Irrigation System Slave Node");
|
||||||
|
|
||||||
|
if (settings_subsys_init() || settings_load()) {
|
||||||
|
LOG_ERR("Settings initialization or loading failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
valve_init();
|
||||||
|
fwu_init();
|
||||||
|
|
||||||
|
if (modbus_server_init()) {
|
||||||
|
LOG_ERR("Modbus RTU server initialization failed");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test supply voltage reading periodically
|
||||||
|
while (1) {
|
||||||
|
uint16_t supply_voltage = valve_get_supply_voltage();
|
||||||
|
LOG_INF("Supply voltage: %u mV", supply_voltage);
|
||||||
|
k_msleep(5000); // Read every 5 seconds
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG_INF("Irrigation System Slave Node started successfully");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
8
software/apps/snippets/bootloader/CMakeLists.txt
Normal file
8
software/apps/snippets/bootloader/CMakeLists.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
|
||||||
|
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||||
|
|
||||||
|
project(bootloader LANGUAGES C)
|
||||||
|
zephyr_include_directories(../../../include)
|
||||||
|
add_subdirectory(../../../lib lib)
|
||||||
|
target_sources(app PRIVATE src/main.c)
|
||||||
34
software/apps/snippets/bootloader/README.md
Normal file
34
software/apps/snippets/bootloader/README.md
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
# Firmware Node Application
|
||||||
|
|
||||||
|
This Zephyr application provides firmware management capabilities for the irrigation system.
|
||||||
|
|
||||||
|
**Tested on Zephyr 4.1.99**
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
### Step 1: Shell with Reset Command
|
||||||
|
- Shell interface with custom "reset" command
|
||||||
|
- Warm reboot functionality
|
||||||
|
|
||||||
|
### Planned Features
|
||||||
|
- MCUboot support with partition manager
|
||||||
|
- Firmware version display
|
||||||
|
- MCUmgr support for OTA updates
|
||||||
|
|
||||||
|
## Building
|
||||||
|
|
||||||
|
```bash
|
||||||
|
west build -p auto -b weact_stm32g431_core apps/firmware_node -- -DBOARD_FLASH_RUNNER=blackmagicprobe
|
||||||
|
```
|
||||||
|
|
||||||
|
## Flashing
|
||||||
|
|
||||||
|
```bash
|
||||||
|
west flash
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
Connect to the device via serial console and use the shell:
|
||||||
|
- `reset` - Reboot the system
|
||||||
|
- `help` - Show available commands
|
||||||
5
software/apps/snippets/bootloader/VERSION
Normal file
5
software/apps/snippets/bootloader/VERSION
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
VERSION_MAJOR = 0
|
||||||
|
VERSION_MINOR = 0
|
||||||
|
PATCHLEVEL = 1
|
||||||
|
VERSION_TWEAK = 0
|
||||||
|
EXTRAVERSION = testing
|
||||||
8
software/apps/snippets/bootloader/erase.sh
Executable file
8
software/apps/snippets/bootloader/erase.sh
Executable file
@@ -0,0 +1,8 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
/home/edi/zephyr-sdk-0.17.1/arm-zephyr-eabi/bin/arm-zephyr-eabi-gdb \
|
||||||
|
-ex 'target extended-remote /dev/ttyACM0' \
|
||||||
|
-ex 'monitor swdp_scan' \
|
||||||
|
-ex 'attach 1' \
|
||||||
|
-ex 'monitor erase_mass' \
|
||||||
|
-ex 'detach' \
|
||||||
|
-ex 'quit' \
|
||||||
16
software/apps/snippets/bootloader/prj.conf
Normal file
16
software/apps/snippets/bootloader/prj.conf
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
CONFIG_SHELL=y
|
||||||
|
CONFIG_REBOOT=y
|
||||||
|
|
||||||
|
# MCUboot support for recovery request function
|
||||||
|
CONFIG_MCUBOOT_BOOTUTIL_LIB=y
|
||||||
|
CONFIG_MCUBOOT_IMG_MANAGER=y
|
||||||
|
CONFIG_IMG_MANAGER=y
|
||||||
|
|
||||||
|
# Flash and Stream Configuration (required for IMG_MANAGER)
|
||||||
|
CONFIG_FLASH=y
|
||||||
|
CONFIG_STREAM_FLASH=y
|
||||||
|
|
||||||
|
# Retention system
|
||||||
|
CONFIG_RETENTION=y
|
||||||
|
CONFIG_RETENTION_BOOT_MODE=y
|
||||||
|
CONFIG_RETAINED_MEM=y
|
||||||
42
software/apps/snippets/bootloader/src/main.c
Normal file
42
software/apps/snippets/bootloader/src/main.c
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#include <zephyr/kernel.h>
|
||||||
|
#include <app_version.h>
|
||||||
|
#include <zephyr/shell/shell.h>
|
||||||
|
#include <zephyr/sys/reboot.h>
|
||||||
|
#include <zephyr/dfu/mcuboot.h>
|
||||||
|
#include <zephyr/retention/bootmode.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/* Shell command handler for "reset" */
|
||||||
|
static int cmd_reset(const struct shell *sh, size_t argc, char **argv)
|
||||||
|
{
|
||||||
|
shell_print(sh, "Rebooting system...");
|
||||||
|
k_sleep(K_MSEC(100)); // Optional delay for user to see the message
|
||||||
|
sys_reboot(SYS_REBOOT_WARM);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cmd_download(const struct shell *sh, size_t argc, char **argv)
|
||||||
|
{
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
/* Set boot mode to serial recovery */
|
||||||
|
rc = bootmode_set(BOOT_MODE_TYPE_BOOTLOADER);
|
||||||
|
if (rc < 0) {
|
||||||
|
shell_error(sh, "Failed to set boot mode: %d", rc);
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
shell_print(sh, "Boot mode set to recovery. Rebooting to bootloader...");
|
||||||
|
k_sleep(K_MSEC(100));
|
||||||
|
sys_reboot(SYS_REBOOT_WARM);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Register the shell command */
|
||||||
|
SHELL_CMD_REGISTER(reset, NULL, "Reboot the system", cmd_reset);
|
||||||
|
SHELL_CMD_REGISTER(download, NULL, "Download firmware", cmd_download);
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
printk("Bootloader test version %s\n", APP_VERSION_EXTENDED_STRING);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
5
software/apps/snippets/bootloader/sysbuild.conf
Normal file
5
software/apps/snippets/bootloader/sysbuild.conf
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# Sysbuild configuration for firmware_node with MCUboot
|
||||||
|
|
||||||
|
# Enable MCUboot as bootloader
|
||||||
|
SB_CONFIG_BOOTLOADER_MCUBOOT=y
|
||||||
|
SB_CONFIG_MCUBOOT_MODE_SINGLE_APP=y
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2021 Nordic Semiconductor ASA
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "flash_partitions_128kb.dtsi"
|
||||||
|
|
||||||
|
/ {
|
||||||
|
chosen {
|
||||||
|
zephyr,code-partition = &slot0_partition;
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* Devicetree Overlay for 128KB Flash
|
||||||
|
* - MCUboot Bootloader (32KB)
|
||||||
|
* - Application Slot (96KB)
|
||||||
|
*/
|
||||||
|
|
||||||
|
&flash0 {
|
||||||
|
/delete-node/ partitions;
|
||||||
|
partitions {
|
||||||
|
compatible = "fixed-partitions";
|
||||||
|
#address-cells = <1>;
|
||||||
|
#size-cells = <1>;
|
||||||
|
|
||||||
|
boot_partition: partition@0 {
|
||||||
|
label = "mcuboot";
|
||||||
|
reg = <0x00000000 DT_SIZE_K(32)>;
|
||||||
|
read-only;
|
||||||
|
};
|
||||||
|
|
||||||
|
slot0_partition: partition@8000 {
|
||||||
|
label = "image-0";
|
||||||
|
reg = <0x00008000 DT_SIZE_K(96)>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Add retention memory to the existing SRAM node */
|
||||||
|
&sram0 {
|
||||||
|
#address-cells = <1>;
|
||||||
|
#size-cells = <1>;
|
||||||
|
|
||||||
|
retainedmem {
|
||||||
|
compatible = "zephyr,retained-ram";
|
||||||
|
status = "okay";
|
||||||
|
#address-cells = <1>;
|
||||||
|
#size-cells = <1>;
|
||||||
|
|
||||||
|
boot_mode: retention@7f00 {
|
||||||
|
compatible = "zephyr,retention";
|
||||||
|
status = "okay";
|
||||||
|
reg = <0x7f00 0x100>;
|
||||||
|
prefix = [08 04];
|
||||||
|
checksum = <1>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
/ {
|
||||||
|
chosen {
|
||||||
|
zephyr,boot-mode = &boot_mode;
|
||||||
|
zephyr,console = &cdc_acm_uart0;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
&zephyr_udc0 {
|
||||||
|
status = "okay";
|
||||||
|
|
||||||
|
cdc_acm_uart0: cdc_acm_uart0 {
|
||||||
|
compatible = "zephyr,cdc-acm-uart";
|
||||||
|
label = "CDC_ACM_0";
|
||||||
|
};
|
||||||
|
};
|
||||||
46
software/apps/snippets/bootloader/sysbuild/mcuboot.conf
Normal file
46
software/apps/snippets/bootloader/sysbuild/mcuboot.conf
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
#
|
||||||
|
# MCUboot Configuration for Serial Recovery over USB-CDC
|
||||||
|
#
|
||||||
|
|
||||||
|
# Enables serial recovery mode in MCUboot.
|
||||||
|
CONFIG_MCUBOOT_SERIAL=y
|
||||||
|
|
||||||
|
# Tell MCUboot to check for a trigger to enter recovery
|
||||||
|
CONFIG_BOOT_SERIAL_BOOT_MODE=y
|
||||||
|
|
||||||
|
# --- USB Stack Configuration ---
|
||||||
|
CONFIG_USB_DEVICE_STACK=y
|
||||||
|
CONFIG_USB_DEVICE_PRODUCT="MCUboot Serial Recovery"
|
||||||
|
|
||||||
|
# Use USB CDC ACM for MCUboot serial recovery (not UART)
|
||||||
|
CONFIG_BOOT_SERIAL_CDC_ACM=y
|
||||||
|
|
||||||
|
# --- Disable Zephyr Console to avoid conflicts ---
|
||||||
|
# MCUboot's serial_adapter doesn't work well with the general console subsystem.
|
||||||
|
CONFIG_UART_CONSOLE=n
|
||||||
|
CONFIG_CONSOLE_HANDLER=n
|
||||||
|
CONFIG_CONSOLE=n
|
||||||
|
|
||||||
|
# --- Flash and Stream Configuration (required for IMG_MANAGER) ---
|
||||||
|
CONFIG_FLASH=y
|
||||||
|
CONFIG_STREAM_FLASH=y
|
||||||
|
|
||||||
|
# --- mcumgr Configuration ---
|
||||||
|
# MCUMGR requires NET_BUF, even for serial transport.
|
||||||
|
CONFIG_NET_BUF=y
|
||||||
|
CONFIG_NET_LOG=n
|
||||||
|
|
||||||
|
# Enables the mcumgr library and necessary command handlers
|
||||||
|
CONFIG_MCUMGR=y
|
||||||
|
CONFIG_IMG_MANAGER=y
|
||||||
|
CONFIG_MCUMGR_GRP_IMG=y
|
||||||
|
CONFIG_MCUMGR_GRP_OS=y
|
||||||
|
|
||||||
|
# --- Retention Configuration ---
|
||||||
|
CONFIG_RETAINED_MEM=y
|
||||||
|
CONFIG_RETENTION=y
|
||||||
|
CONFIG_RETENTION_BOOT_MODE=y
|
||||||
|
|
||||||
|
# --- Optional: Reduce memory usage ---
|
||||||
|
CONFIG_MAIN_STACK_SIZE=2048
|
||||||
|
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=1024
|
||||||
17
software/apps/snippets/bootloader/sysbuild/mcuboot.overlay
Normal file
17
software/apps/snippets/bootloader/sysbuild/mcuboot.overlay
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#include "flash_partitions_128kb.dtsi"
|
||||||
|
|
||||||
|
/ {
|
||||||
|
chosen {
|
||||||
|
zephyr,code-partition = &boot_partition;
|
||||||
|
zephyr,console = &cdc_acm_uart0;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
&zephyr_udc0 {
|
||||||
|
status = "okay";
|
||||||
|
|
||||||
|
cdc_acm_uart0: cdc_acm_uart0 {
|
||||||
|
compatible = "zephyr,cdc-acm-uart";
|
||||||
|
label = "CDC_ACM_0";
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
# Copyright (c) 2025 Eduard Iten
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
config BOARD_BLUEPILL_F103RB
|
||||||
|
select SOC_STM32F103XB
|
||||||
106
software/boards/iten/bluepill_f103rb/bluepill_f103rb.dts
Normal file
106
software/boards/iten/bluepill_f103rb/bluepill_f103rb.dts
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2025 Eduard Iten
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
/dts-v1/;
|
||||||
|
#include <st/f1/stm32f1.dtsi>
|
||||||
|
#include <st/f1/stm32f103Xb.dtsi>
|
||||||
|
#include <st/f1/stm32f103r(8-b)tx-pinctrl.dtsi>
|
||||||
|
#include <zephyr/dt-bindings/input/input-event-codes.h>
|
||||||
|
|
||||||
|
/ {
|
||||||
|
model = "Blue-Pill STM32F103RB";
|
||||||
|
compatible = "iten,bluepill-f103rb";
|
||||||
|
|
||||||
|
chosen {
|
||||||
|
zephyr,console = &usart1;
|
||||||
|
zephyr,shell-uart = &usart1;
|
||||||
|
zephyr,sram = &sram0;
|
||||||
|
zephyr,flash = &flash0;
|
||||||
|
};
|
||||||
|
|
||||||
|
leds {
|
||||||
|
compatible = "gpio-leds";
|
||||||
|
led_status: led_status {
|
||||||
|
gpios = <&gpioc 13 GPIO_ACTIVE_LOW>;
|
||||||
|
label = "User LED";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
aliases {
|
||||||
|
led0 = &led_status;
|
||||||
|
watchdog0 = &iwdg;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
&clk_lsi {
|
||||||
|
status = "okay";
|
||||||
|
};
|
||||||
|
|
||||||
|
&clk_hse {
|
||||||
|
clock-frequency = <DT_FREQ_M(8)>;
|
||||||
|
status = "okay";
|
||||||
|
};
|
||||||
|
|
||||||
|
&pll {
|
||||||
|
mul = <9>;
|
||||||
|
clocks = <&clk_hse>;
|
||||||
|
status = "okay";
|
||||||
|
};
|
||||||
|
|
||||||
|
&rcc {
|
||||||
|
clocks = <&pll>;
|
||||||
|
clock-frequency = <DT_FREQ_M(72)>;
|
||||||
|
ahb-prescaler = <1>;
|
||||||
|
apb1-prescaler = <2>;
|
||||||
|
apb2-prescaler = <1>;
|
||||||
|
adc-prescaler = <6>;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
&usart1 {
|
||||||
|
pinctrl-0 = <&usart1_tx_pa9 &usart1_rx_pa10>;
|
||||||
|
pinctrl-names = "default";
|
||||||
|
current-speed = <115200>;
|
||||||
|
status = "okay";
|
||||||
|
};
|
||||||
|
|
||||||
|
&iwdg {
|
||||||
|
status = "okay";
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
&clk_lsi {
|
||||||
|
status = "okay";
|
||||||
|
};
|
||||||
|
|
||||||
|
&clk_hse {
|
||||||
|
clock-frequency = <DT_FREQ_M(8)>;
|
||||||
|
status = "okay";
|
||||||
|
};
|
||||||
|
|
||||||
|
&pll {
|
||||||
|
mul = <9>;
|
||||||
|
clocks = <&clk_hse>;
|
||||||
|
status = "okay";
|
||||||
|
};
|
||||||
|
|
||||||
|
&rcc {
|
||||||
|
clocks = <&pll>;
|
||||||
|
clock-frequency = <DT_FREQ_M(72)>;
|
||||||
|
ahb-prescaler = <1>;
|
||||||
|
apb1-prescaler = <2>;
|
||||||
|
apb2-prescaler = <1>;
|
||||||
|
adc-prescaler = <6>;
|
||||||
|
};
|
||||||
|
|
||||||
|
&exti {
|
||||||
|
status = "okay";
|
||||||
|
};
|
||||||
|
|
||||||
|
&dma1 {
|
||||||
|
status = "okay";
|
||||||
|
};
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
# Copyright (c) 2025 Eduard Iten
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
CONFIG_SERIAL=y
|
||||||
|
CONFIG_GPIO=y
|
||||||
@@ -1,11 +1,12 @@
|
|||||||
|
# Copyright (c) 2025 Eduard Iten
|
||||||
# SPDX-License-Identifier: Apache-2.0
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
# keep first
|
# keep first
|
||||||
board_runner_args(stm32cubeprogrammer "--port=swd" "--reset-mode=hw")
|
|
||||||
board_runner_args(jlink "--device=STM32F103RB" "--speed=4000")
|
board_runner_args(jlink "--device=STM32F103RB" "--speed=4000")
|
||||||
|
board_runner_args(stm32cubeprogrammer "--port=swd" "--reset-mode=hw")
|
||||||
|
|
||||||
# keep first
|
# keep first
|
||||||
|
include(${ZEPHYR_BASE}/boards/common/jlink.board.cmake)
|
||||||
include(${ZEPHYR_BASE}/boards/common/stm32cubeprogrammer.board.cmake)
|
include(${ZEPHYR_BASE}/boards/common/stm32cubeprogrammer.board.cmake)
|
||||||
include(${ZEPHYR_BASE}/boards/common/openocd-stm32.board.cmake)
|
include(${ZEPHYR_BASE}/boards/common/openocd-stm32.board.cmake)
|
||||||
include(${ZEPHYR_BASE}/boards/common/jlink.board.cmake)
|
|
||||||
include(${ZEPHYR_BASE}/boards/common/blackmagicprobe.board.cmake)
|
include(${ZEPHYR_BASE}/boards/common/blackmagicprobe.board.cmake)
|
||||||
8
software/boards/iten/bluepill_f103rb/board.yml
Normal file
8
software/boards/iten/bluepill_f103rb/board.yml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# Copyright (c) 2025 Eduard Iten
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
board:
|
||||||
|
name: bluepill_f103rb
|
||||||
|
vendor: iten
|
||||||
|
socs:
|
||||||
|
- name: stm32f103xb
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
config BOARD_VALVE_NODE
|
|
||||||
select SOC_STM32F103XB
|
|
||||||
|
|
||||||
mainmenu "APP CAN Settings"
|
|
||||||
config LOOPBACK_MODE
|
|
||||||
bool "Loopback LOOPBACK_MODE"
|
|
||||||
default n
|
|
||||||
help
|
|
||||||
Set the can controller to loopback mode.
|
|
||||||
This allows testing without a second board.
|
|
||||||
|
|
||||||
mainmenu "APP Logging Settings"
|
|
||||||
config LOG_CAN_LEVEL
|
|
||||||
int "Log level for CAN"
|
|
||||||
default 3
|
|
||||||
range 0 7
|
|
||||||
help
|
|
||||||
Set the log level for CAN messages.
|
|
||||||
0 = None, 1 = Error, 2 = Warning, 3 = Info, 4 = Debug, 5 = Trace, 6 = Debug2, 7 = Debug3
|
|
||||||
|
|
||||||
config LOG_SETTINGS_LEVEL
|
|
||||||
int "Log level for settings"
|
|
||||||
default 3
|
|
||||||
range 0 7
|
|
||||||
help
|
|
||||||
Set the log level for CAN messages.
|
|
||||||
0 = None, 1 = Error, 2 = Warning, 3 = Info, 4 = Debug, 5 = Trace, 6 = Debug2, 7 = Debug3
|
|
||||||
|
|
||||||
config LOG_WATERLEVELSENSOR_LEVEL
|
|
||||||
int "Log level for waterlevel sensor"
|
|
||||||
default 3
|
|
||||||
range 0 7
|
|
||||||
help
|
|
||||||
Set the log level for CAN messages.
|
|
||||||
0 = None, 1 = Error, 2 = Warning, 3 = Info, 4 = Debug, 5 = Trace, 6 = Debug2, 7 = Debug3
|
|
||||||
|
|
||||||
config LOG_VALVE_LEVEL
|
|
||||||
int "Log level for valve control"
|
|
||||||
default 3
|
|
||||||
range 0 7
|
|
||||||
help
|
|
||||||
Set the log level for valve control messages.
|
|
||||||
0 = None, 1 = Error, 2 = Warning, 3 = Info, 4 = Debug, 5 = Trace, 6 = Debug2, 7 = Debug3
|
|
||||||
|
|
||||||
mainmenu "Irrigation controller node configuration"
|
|
||||||
config HAS_MODBUS_WATERLEVEL_SENSOR
|
|
||||||
bool "Has modbus water level sensor"
|
|
||||||
default n
|
|
||||||
help
|
|
||||||
Enable modbus water level sensor support.
|
|
||||||
This allows reading the water level from a modbus device.
|
|
||||||
|
|
||||||
config HAS_VALVE
|
|
||||||
bool "Has valve control"
|
|
||||||
default n
|
|
||||||
help
|
|
||||||
Enable valve control support.
|
|
||||||
This allows controlling valves via CAN messages.
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
board:
|
|
||||||
name: valve_node
|
|
||||||
full_name: Irrigation system CANbus valve node
|
|
||||||
socs:
|
|
||||||
- name: stm32f103xb
|
|
||||||
# revision:
|
|
||||||
# format: number
|
|
||||||
# default: "1"
|
|
||||||
# revisions:
|
|
||||||
# -name: "1"
|
|
||||||
@@ -1,206 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2017 Linaro Limited
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
|
||||||
*/
|
|
||||||
|
|
||||||
/dts-v1/;
|
|
||||||
#include <st/f1/stm32f1.dtsi>
|
|
||||||
#include <st/f1/stm32f103Xb.dtsi>
|
|
||||||
#include <st/f1/stm32f103r(8-b)tx-pinctrl.dtsi>
|
|
||||||
#include <zephyr/dt-bindings/input/input-event-codes.h>
|
|
||||||
|
|
||||||
/ {
|
|
||||||
model = "Iten engineering Valve Node";
|
|
||||||
compatible = "st,stm32f103rb";
|
|
||||||
|
|
||||||
can_loopback0: can_loopback0 {
|
|
||||||
status = "disabled";
|
|
||||||
compatible = "zephyr,can-loopback";
|
|
||||||
};
|
|
||||||
|
|
||||||
chosen {
|
|
||||||
zephyr,console = &usart1;
|
|
||||||
zephyr,shell-uart = &usart1;
|
|
||||||
zephyr,sram = &sram0;
|
|
||||||
zephyr,flash = &flash0;
|
|
||||||
zephyr,canbus = &can1;
|
|
||||||
};
|
|
||||||
|
|
||||||
leds: leds {
|
|
||||||
compatible = "gpio-leds";
|
|
||||||
|
|
||||||
green_led_2: led_2 {
|
|
||||||
gpios = <&gpiob 2 GPIO_ACTIVE_HIGH>;
|
|
||||||
label = "User LD2";
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
gpio_keys {
|
|
||||||
compatible = "gpio-keys";
|
|
||||||
|
|
||||||
user_button: button {
|
|
||||||
label = "User";
|
|
||||||
gpios = <&gpioc 13 GPIO_ACTIVE_LOW>;
|
|
||||||
zephyr,code = <INPUT_KEY_0>;
|
|
||||||
};
|
|
||||||
|
|
||||||
endstopopen: endstop_open {
|
|
||||||
gpios = <&gpiob 4 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>;
|
|
||||||
label = "Endstop Open";
|
|
||||||
};
|
|
||||||
|
|
||||||
endstopclose: endstop_closed {
|
|
||||||
gpios = <&gpiob 5 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>;
|
|
||||||
label = "Endstop Close";
|
|
||||||
};
|
|
||||||
|
|
||||||
statusopen: status_open {
|
|
||||||
gpios = <&gpiob 14 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>;
|
|
||||||
label = "Status Open";
|
|
||||||
};
|
|
||||||
|
|
||||||
statusclose: status_close {
|
|
||||||
gpios = <&gpioa 8 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>;
|
|
||||||
label = "Status Close";
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
aliases {
|
|
||||||
led0 = &green_led_2;
|
|
||||||
sw0 = &user_button;
|
|
||||||
watchdog0 = &iwdg;
|
|
||||||
die-temp0 = &die_temp;
|
|
||||||
adc-motor-current = &motor_current_channel;
|
|
||||||
adc-vref = &vref_channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
&clk_lsi {
|
|
||||||
status = "okay";
|
|
||||||
};
|
|
||||||
|
|
||||||
&clk_hse {
|
|
||||||
clock-frequency = <DT_FREQ_M(8)>;
|
|
||||||
status = "okay";
|
|
||||||
};
|
|
||||||
|
|
||||||
&pll {
|
|
||||||
mul = <9>;
|
|
||||||
clocks = <&clk_hse>;
|
|
||||||
status = "okay";
|
|
||||||
};
|
|
||||||
|
|
||||||
&rcc {
|
|
||||||
clocks = <&pll>;
|
|
||||||
clock-frequency = <DT_FREQ_M(72)>;
|
|
||||||
ahb-prescaler = <1>;
|
|
||||||
apb1-prescaler = <2>;
|
|
||||||
apb2-prescaler = <1>;
|
|
||||||
adc-prescaler = <6>;
|
|
||||||
};
|
|
||||||
|
|
||||||
&usart1 {
|
|
||||||
pinctrl-0 = <&usart1_tx_pa9 &usart1_rx_pa10>;
|
|
||||||
pinctrl-names = "default";
|
|
||||||
status = "okay";
|
|
||||||
current-speed = <115200>;
|
|
||||||
};
|
|
||||||
|
|
||||||
&usart2 {
|
|
||||||
pinctrl-0 = <&usart2_tx_pa2 &usart2_rx_pa3>;
|
|
||||||
current-speed = <9600>;
|
|
||||||
pinctrl-names = "default";
|
|
||||||
status = "okay";
|
|
||||||
modbus0 {
|
|
||||||
compatible = "zephyr,modbus-serial";
|
|
||||||
status = "okay";
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
&usart3 {
|
|
||||||
pinctrl-0 = <&usart3_tx_pb10 &usart3_rx_pb11>;
|
|
||||||
current-speed = <115200>;
|
|
||||||
pinctrl-names = "default";
|
|
||||||
};
|
|
||||||
|
|
||||||
&i2c1 {
|
|
||||||
pinctrl-0 = <&i2c1_scl_remap1_pb8 &i2c1_sda_remap1_pb9>;
|
|
||||||
pinctrl-names = "default";
|
|
||||||
status = "okay";
|
|
||||||
clock-frequency = <I2C_BITRATE_FAST>;
|
|
||||||
};
|
|
||||||
|
|
||||||
&iwdg {
|
|
||||||
status = "okay";
|
|
||||||
};
|
|
||||||
|
|
||||||
&rtc {
|
|
||||||
clocks = <&rcc STM32_CLOCK_BUS_APB1 0x10000000>,
|
|
||||||
<&rcc STM32_SRC_LSI RTC_SEL(2)>;
|
|
||||||
status = "okay";
|
|
||||||
};
|
|
||||||
|
|
||||||
&adc1 {
|
|
||||||
pinctrl-0 = <&adc_pb1_pins>;
|
|
||||||
pinctrl-names = "default";
|
|
||||||
status = "okay";
|
|
||||||
|
|
||||||
#address-cells = <1>;
|
|
||||||
#size-cells = <0>;
|
|
||||||
|
|
||||||
motor_current_channel: channel@9 {
|
|
||||||
reg = <0x9>;
|
|
||||||
zephyr,gain = "ADC_GAIN_1";
|
|
||||||
zephyr,reference = "ADC_REF_VDD_1";
|
|
||||||
zephyr,acquisition-time = <49159>;
|
|
||||||
zephyr,resolution = <12>;
|
|
||||||
};
|
|
||||||
|
|
||||||
vref_channel: channel@11 { /* 17 dezimal = 11 hex */
|
|
||||||
reg = <0x11>;
|
|
||||||
zephyr,gain = "ADC_GAIN_1";
|
|
||||||
zephyr,reference = "ADC_REF_VDD_1";
|
|
||||||
zephyr,acquisition-time = <49159>;
|
|
||||||
zephyr,resolution = <12>;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
&die_temp {
|
|
||||||
status = "okay";
|
|
||||||
};
|
|
||||||
|
|
||||||
&dma1 {
|
|
||||||
status = "okay";
|
|
||||||
};
|
|
||||||
|
|
||||||
&flash0 {
|
|
||||||
partitions {
|
|
||||||
compatible = "fixed-partitions";
|
|
||||||
#address-cells = <1>;
|
|
||||||
#size-cells = <1>;
|
|
||||||
|
|
||||||
storage_partition: partition@1f800 {
|
|
||||||
label = "storage";
|
|
||||||
reg = <0x0001f800 DT_SIZE_K(2)>;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
&can1 {
|
|
||||||
pinctrl-0 = <&can_rx_pa11 &can_tx_pa12>;
|
|
||||||
pinctrl-names = "default";
|
|
||||||
status= "okay";
|
|
||||||
bitrate = <125000>;
|
|
||||||
};
|
|
||||||
|
|
||||||
&exti {
|
|
||||||
status = "okay";
|
|
||||||
};
|
|
||||||
|
|
||||||
&pinctrl {
|
|
||||||
adc_pb1_pins: adc_pb1_pins {
|
|
||||||
pinmux = <STM32F1_PINMUX('B', 1, ANALOG, NO_REMAP)>;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
# SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
# enable uart driver
|
|
||||||
CONFIG_SERIAL=y
|
|
||||||
# enable console
|
|
||||||
CONFIG_CONSOLE=y
|
|
||||||
|
|
||||||
# enable GPIO
|
|
||||||
CONFIG_GPIO=y
|
|
||||||
|
|
||||||
# modbus config
|
|
||||||
CONFIG_UART_INTERRUPT_DRIVEN=y
|
|
||||||
CONFIG_UART_LINE_CTRL=n
|
|
||||||
CONFIG_MODBUS=y
|
|
||||||
CONFIG_MODBUS_ROLE_CLIENT=y
|
|
||||||
|
|
||||||
# can config
|
|
||||||
CONFIG_CAN=y
|
|
||||||
CONFIG_CAN_INIT_PRIORITY=80
|
|
||||||
#CONFIG_CAN_MAX_FILTER=5
|
|
||||||
CONFIG_CAN_ACCEPT_RTR=y
|
|
||||||
|
|
||||||
# settings
|
|
||||||
CONFIG_FLASH=y
|
|
||||||
CONFIG_FLASH_MAP=y
|
|
||||||
CONFIG_SETTINGS=y
|
|
||||||
CONFIG_SETTINGS_RUNTIME=y
|
|
||||||
CONFIG_NVS=y
|
|
||||||
CONFIG_SETTINGS_NVS=y
|
|
||||||
CONFIG_HEAP_MEM_POOL_SIZE=256
|
|
||||||
CONFIG_MPU_ALLOW_FLASH_WRITE=y
|
|
||||||
10
software/include/lib/fwu.h
Normal file
10
software/include/lib/fwu.h
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#ifndef FWU_H
|
||||||
|
#define FWU_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
void fwu_init(void);
|
||||||
|
void fwu_handler(uint16_t addr, uint16_t reg);
|
||||||
|
uint16_t fwu_get_last_chunk_crc(void);
|
||||||
|
|
||||||
|
#endif // FWU_H
|
||||||
54
software/include/lib/modbus_server.h
Normal file
54
software/include/lib/modbus_server.h
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
#ifndef MODBUS_SERVER_H
|
||||||
|
#define MODBUS_SERVER_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Modbus Input Register Addresses.
|
||||||
|
*/
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
/* Valve Control & Status */
|
||||||
|
REG_INPUT_VALVE_STATE_MOVEMENT = 0x0000,
|
||||||
|
REG_INPUT_MOTOR_CURRENT_MA = 0x0001,
|
||||||
|
/* Digital Inputs */
|
||||||
|
REG_INPUT_DIGITAL_INPUTS_STATE = 0x0020,
|
||||||
|
REG_INPUT_BUTTON_EVENTS = 0x0021,
|
||||||
|
/* System Config & Status */
|
||||||
|
REG_INPUT_FIRMWARE_VERSION_MAJOR_MINOR = 0x00F0,
|
||||||
|
REG_INPUT_FIRMWARE_VERSION_PATCH = 0x00F1,
|
||||||
|
REG_INPUT_DEVICE_STATUS = 0x00F2,
|
||||||
|
REG_INPUT_UPTIME_SECONDS_LOW = 0x00F3,
|
||||||
|
REG_INPUT_UPTIME_SECONDS_HIGH = 0x00F4,
|
||||||
|
REG_INPUT_SUPPLY_VOLTAGE_MV = 0x00F5,
|
||||||
|
REG_INPUT_FWU_LAST_CHUNK_CRC = 0x0100
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Modbus Holding Register Addresses.
|
||||||
|
*/
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
/* Valve Control */
|
||||||
|
REG_HOLDING_VALVE_COMMAND = 0x0000,
|
||||||
|
REG_HOLDING_MAX_OPENING_TIME_S = 0x0001,
|
||||||
|
REG_HOLDING_MAX_CLOSING_TIME_S = 0x0002,
|
||||||
|
/* Digital Outputs */
|
||||||
|
REG_HOLDING_DIGITAL_OUTPUTS_STATE = 0x0010,
|
||||||
|
/* System Config */
|
||||||
|
REG_HOLDING_WATCHDOG_TIMEOUT_S = 0x00F0,
|
||||||
|
REG_HOLDING_DEVICE_RESET = 0x00F1,
|
||||||
|
/* Firmware Update */
|
||||||
|
REG_HOLDING_FWU_COMMAND = 0x0100,
|
||||||
|
REG_HOLDING_FWU_CHUNK_OFFSET_LOW = 0x0101,
|
||||||
|
REG_HOLDING_FWU_CHUNK_OFFSET_HIGH = 0x0102,
|
||||||
|
REG_HOLDING_FWU_CHUNK_SIZE = 0x0103,
|
||||||
|
REG_HOLDING_FWU_DATA_BUFFER = 0x0180,
|
||||||
|
};
|
||||||
|
|
||||||
|
int modbus_server_init(void);
|
||||||
|
int modbus_reconfigure(uint32_t baudrate, uint8_t unit_id);
|
||||||
|
uint32_t modbus_get_baudrate(void);
|
||||||
|
uint8_t modbus_get_unit_id(void);
|
||||||
|
|
||||||
|
#endif // MODBUS_SERVER_H
|
||||||
37
software/include/lib/valve.h
Normal file
37
software/include/lib/valve.h
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
#ifndef VALVE_H
|
||||||
|
#define VALVE_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <zephyr/drivers/gpio.h>
|
||||||
|
|
||||||
|
struct valve_gpios {
|
||||||
|
const struct gpio_dt_spec in0;
|
||||||
|
const struct gpio_dt_spec in1;
|
||||||
|
const struct gpio_dt_spec rst;
|
||||||
|
const struct gpio_dt_spec sen;
|
||||||
|
const struct gpio_dt_spec s0;
|
||||||
|
const struct gpio_dt_spec s1;
|
||||||
|
};
|
||||||
|
|
||||||
|
enum valve_state {
|
||||||
|
VALVE_STATE_CLOSED,
|
||||||
|
VALVE_STATE_OPEN,
|
||||||
|
};
|
||||||
|
enum valve_movement { VALVE_MOVEMENT_IDLE, VALVE_MOVEMENT_OPENING, VALVE_MOVEMENT_CLOSING, VALVE_MOVEMENT_ERROR };
|
||||||
|
|
||||||
|
void valve_init(void);
|
||||||
|
void valve_open(void);
|
||||||
|
void valve_close(void);
|
||||||
|
void valve_stop(void);
|
||||||
|
|
||||||
|
enum valve_state valve_get_state(void);
|
||||||
|
enum valve_movement valve_get_movement(void);
|
||||||
|
uint16_t valve_get_motor_current(void);
|
||||||
|
uint16_t valve_get_supply_voltage(void);
|
||||||
|
|
||||||
|
void valve_set_max_open_time(uint16_t seconds);
|
||||||
|
void valve_set_max_close_time(uint16_t seconds);
|
||||||
|
uint16_t valve_get_max_open_time(void);
|
||||||
|
uint16_t valve_get_max_close_time(void);
|
||||||
|
|
||||||
|
#endif // VALVE_H
|
||||||
5
software/lib/CMakeLists.txt
Normal file
5
software/lib/CMakeLists.txt
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
add_subdirectory_ifdef(CONFIG_LIB_FWU fwu)
|
||||||
|
add_subdirectory_ifdef(CONFIG_LIB_MODBUS_SERVER modbus_server)
|
||||||
|
add_subdirectory_ifdef(CONFIG_LIB_VALVE valve)
|
||||||
|
add_subdirectory_ifdef(CONFIG_SHELL_SYSTEM shell_system)
|
||||||
|
add_subdirectory_ifdef(CONFIG_SHELL_MODBUS shell_modbus)
|
||||||
8
software/lib/Kconfig
Normal file
8
software/lib/Kconfig
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
menu "Irrigation system software libraries"
|
||||||
|
|
||||||
|
rsource "fwu/Kconfig"
|
||||||
|
rsource "modbus_server/Kconfig"
|
||||||
|
rsource "valve/Kconfig"
|
||||||
|
rsource "shell_system/Kconfig"
|
||||||
|
rsource "shell_modbus/Kconfig"
|
||||||
|
endmenu
|
||||||
@@ -1,286 +0,0 @@
|
|||||||
#include "canbus.h"
|
|
||||||
#include <zephyr/logging/log.h>
|
|
||||||
#include <zephyr/kernel.h>
|
|
||||||
#include <zephyr/sys/byteorder.h>
|
|
||||||
#include <zephyr/drivers/can.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#ifdef CONFIG_HAS_MODBUS_WATERLEVEL_SENSOR
|
|
||||||
#include "waterlevel_sensor.h"
|
|
||||||
#endif // CONFIG_HAS_MODBUS_WATERLEVEL_SENSOR
|
|
||||||
|
|
||||||
#ifdef CONFIG_HAS_VALVE
|
|
||||||
#include "valve.h"
|
|
||||||
#endif // CONFIG_HAS_VALVE
|
|
||||||
|
|
||||||
const struct device *const can_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_canbus));
|
|
||||||
|
|
||||||
LOG_MODULE_REGISTER(canbus, CONFIG_LOG_CAN_LEVEL);
|
|
||||||
|
|
||||||
K_MSGQ_DEFINE(canbus_msgq, sizeof(struct can_frame), CANBUS_RX_MSGQ_SIZE, 4);
|
|
||||||
K_THREAD_STACK_DEFINE(canbus_rx_stack, CANBUS_RX_THREAD_STACK_SIZE);
|
|
||||||
|
|
||||||
uint8_t node_id = 0; // Default node ID, can be set later
|
|
||||||
uint8_t can_msg_id = 0;
|
|
||||||
|
|
||||||
k_tid_t canbus_rx_thread_id;
|
|
||||||
struct k_thread canbus_rx_thread_data;
|
|
||||||
|
|
||||||
int canbus_rx_filter_id = -1;
|
|
||||||
|
|
||||||
void canbus_rx_callback(const struct device *dev, struct can_frame *frame, void *user_data)
|
|
||||||
{
|
|
||||||
int rc;
|
|
||||||
ARG_UNUSED(dev);
|
|
||||||
ARG_UNUSED(user_data);
|
|
||||||
|
|
||||||
if (frame->id >> 8 != node_id) // Check if the frame ID matches the node ID
|
|
||||||
{
|
|
||||||
LOG_DBG("Received CAN frame with ID %d, but it does not match node ID %d", frame->id >> 8, node_id);
|
|
||||||
return; // Ignore frames that do not match the node ID
|
|
||||||
}
|
|
||||||
|
|
||||||
// Process the received CAN frame
|
|
||||||
rc = k_msgq_put(&canbus_msgq, frame, K_NO_WAIT);
|
|
||||||
if (rc != 0)
|
|
||||||
{
|
|
||||||
LOG_ERR("Failed to put CAN frame into message queue: %d", rc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void canbus_thread(void *arg1, void *arg2, void *arg3)
|
|
||||||
{
|
|
||||||
ARG_UNUSED(arg1);
|
|
||||||
ARG_UNUSED(arg2);
|
|
||||||
ARG_UNUSED(arg3);
|
|
||||||
|
|
||||||
LOG_INF("CAN bus thread started");
|
|
||||||
|
|
||||||
// Main loop for CAN bus operations
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
int rc;
|
|
||||||
struct can_frame frame;
|
|
||||||
k_msgq_get(&canbus_msgq, &frame, K_FOREVER); // Wait for a message from the queue
|
|
||||||
LOG_DBG("Received CAN frame with ID: 0x%02x, DLC: %d, RTR: %d",
|
|
||||||
frame.id, frame.dlc, (uint8_t)(frame.flags & CAN_FRAME_RTR));
|
|
||||||
LOG_HEXDUMP_DBG(frame.data, frame.dlc, "CAN frame data");
|
|
||||||
uint8_t reg = frame.id & 0xFF; // Extract register from the frame ID
|
|
||||||
bool is_rtr = (frame.flags & CAN_FRAME_RTR) != 0; // Check if it's a remote transmission request
|
|
||||||
switch (reg)
|
|
||||||
{
|
|
||||||
#ifdef CONFIG_HAS_MODBUS_WATERLEVEL_SENSOR
|
|
||||||
case CANBUS_REG_WATERLEVEL_LEVEL:
|
|
||||||
case CANBUS_REG_WATERLEVEL_ZERO_POINT:
|
|
||||||
case CANBUS_REG_WATERLEVEL_MAX_POINT:
|
|
||||||
waterlevel_command_t command;
|
|
||||||
command.cmd = is_rtr ? WATERLEVEL_CMD_GET : WATERLEVEL_CMD_SET; // Determine command type based on RTR
|
|
||||||
command.reg = reg; // Set the register ID
|
|
||||||
int16_t value = 0; // Initialize value to 0
|
|
||||||
if (!is_rtr) // If it's not a remote request, extract the value from the frame data
|
|
||||||
{
|
|
||||||
if (frame.dlc < sizeof(command.data))
|
|
||||||
{ LOG_ERR("Received frame with insufficient data length: %d", frame.dlc);
|
|
||||||
continue; // Skip processing if data length is insufficient
|
|
||||||
}
|
|
||||||
value = sys_be16_to_cpu(*(uint16_t *)frame.data); // Convert data from big-endian to host byte order
|
|
||||||
command.data = value; // Set the data field
|
|
||||||
LOG_DBG("Setting command data to: %d", value);
|
|
||||||
}
|
|
||||||
extern struct k_msgq waterlevel_sensor_msgq; // Declare the water level sensor message queue
|
|
||||||
k_msgq_put(&waterlevel_sensor_msgq, &command, K_NO_WAIT);
|
|
||||||
break;
|
|
||||||
#endif // CONFIG_HAS_MODBUS_WATERLEVEL_SENSOR
|
|
||||||
#ifdef CONFIG_HAS_VALVE
|
|
||||||
case CANBUS_REG_VALVE_STATUS:
|
|
||||||
case CANBUS_REG_VALVE_OPERATION:
|
|
||||||
if (is_rtr)
|
|
||||||
{
|
|
||||||
LOG_DBG("Received remote request for valve status or operation");
|
|
||||||
if (reg == CANBUS_REG_VALVE_STATUS)
|
|
||||||
{
|
|
||||||
valve_send_status(); // Send the current valve status
|
|
||||||
}
|
|
||||||
else if (reg == CANBUS_REG_VALVE_OPERATION)
|
|
||||||
{
|
|
||||||
valve_send_operation(); // Send the current valve operation state
|
|
||||||
} else {
|
|
||||||
LOG_ERR("Unknown valve register: 0x%02x", reg);
|
|
||||||
continue; // Skip processing if the register is unknown
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
LOG_ERR("Received CAN frame with data for valve status or operation, but RTR is not set");
|
|
||||||
continue; // Skip processing if RTR is not set for valve status or operation
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case CANBUS_REG_VALVE_COMMAND:
|
|
||||||
{
|
|
||||||
if (is_rtr) {
|
|
||||||
LOG_ERR("Received remote request for valve command, but this is not supported");
|
|
||||||
continue; // Skip processing if RTR is set for valve command
|
|
||||||
} else {
|
|
||||||
// Extract the command from the frame data
|
|
||||||
if (frame.dlc < sizeof(uint8_t)) {
|
|
||||||
LOG_ERR("Received frame with insufficient data length for valve command: %d", frame.dlc);
|
|
||||||
continue; // Skip processing if data length is insufficient
|
|
||||||
}
|
|
||||||
uint8_t command = frame.data[0]; // Get the command from the first byte of data
|
|
||||||
LOG_DBG("Received valve command: 0x%02x", command);
|
|
||||||
rc = valve_cmd(command); // Send the command to the valve
|
|
||||||
if (rc < 0) {
|
|
||||||
LOG_ERR("Failed to send valve command: %d", rc);
|
|
||||||
continue; // Skip processing if sending the command failed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
LOG_DBG("Received CAN frame with unknown register: 0x%02x", reg);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
#endif // CONFIG_HAS_VALVE
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int canbus_init(void)
|
|
||||||
{
|
|
||||||
int rc = 0;
|
|
||||||
|
|
||||||
if (!device_is_ready(can_dev))
|
|
||||||
{
|
|
||||||
LOG_ERR("CAN device %s is not ready", can_dev->name);
|
|
||||||
return -ENODEV;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef CONFIG_LOOPBACK_MODE
|
|
||||||
rc = can_set_mode(can_dev, CAN_MODE_LOOPBACK);
|
|
||||||
if (rc != 0)
|
|
||||||
{
|
|
||||||
LOG_ERR("Failed to set CAN loopback mode: %d", rc);
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
rc = can_start(can_dev);
|
|
||||||
if (rc != 0)
|
|
||||||
{
|
|
||||||
printf("Error starting CAN controller [%d]", rc);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
LOG_DBG("CAN device %s is ready", can_dev->name);
|
|
||||||
// Initialize the CAN RX thread
|
|
||||||
canbus_rx_thread_id = k_thread_create(&canbus_rx_thread_data, canbus_rx_stack,
|
|
||||||
K_THREAD_STACK_SIZEOF(canbus_rx_stack), canbus_thread,
|
|
||||||
NULL, NULL, NULL,
|
|
||||||
CANBUS_RX_THREAD_PRIORITY, 0, K_NO_WAIT);
|
|
||||||
k_thread_name_set(canbus_rx_thread_id, "canbus_rx");
|
|
||||||
|
|
||||||
const struct can_filter filter = {
|
|
||||||
.id = node_id << 8, // Standard ID with node ID in the first byte
|
|
||||||
.mask = 0x700, // Mask to match the first byte of the ID
|
|
||||||
.flags = 0, // No special flags
|
|
||||||
};
|
|
||||||
canbus_rx_filter_id = can_add_rx_filter(can_dev, canbus_rx_callback, NULL, &filter);
|
|
||||||
LOG_DBG("CAN RX filter added for node ID %d", canbus_rx_filter_id);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void canbus_tx8_callback(const struct device *dev, int error, void *user_data)
|
|
||||||
{
|
|
||||||
ARG_UNUSED(dev);
|
|
||||||
uint8_t frame_id = *(uint8_t *)user_data; // Retrieve the frame ID from user data
|
|
||||||
|
|
||||||
if (error != 0)
|
|
||||||
{
|
|
||||||
LOG_ERR("CAN transmission error. Error code: %d, Frame ID: %d", error, frame_id);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
LOG_DBG("CAN message with Frame ID %d sent successfully", frame_id);
|
|
||||||
}
|
|
||||||
free(user_data); // Free the allocated memory for frame ID
|
|
||||||
}
|
|
||||||
|
|
||||||
int canbus_send8(uint16_t reg, uint8_t value)
|
|
||||||
{
|
|
||||||
int rc = 0;
|
|
||||||
struct can_frame frame = {
|
|
||||||
.id = (node_id << 8) | reg, // Standard ID with node ID in the first byte
|
|
||||||
.dlc = sizeof(value), // Data Length Code (DLC)
|
|
||||||
};
|
|
||||||
frame.data[0] = value; // Set the value in the first byte of the data
|
|
||||||
uint8_t *frame_id = malloc(sizeof(uint8_t)); // Allocate memory for frame ID
|
|
||||||
LOG_DBG("Preparing to send 8-bit value 0x%02x to register 0x%02x on node %d", value, reg, node_id);
|
|
||||||
if (frame_id == NULL)
|
|
||||||
{
|
|
||||||
LOG_ERR("Failed to allocate memory for frame ID");
|
|
||||||
return -ENOMEM; // Not enough memory
|
|
||||||
}
|
|
||||||
*frame_id = can_msg_id++; // Increment message ID for uniqueness
|
|
||||||
LOG_DBG("Using frame ID: %d", *frame_id);
|
|
||||||
rc = can_send(can_dev, &frame, CANBUS_TX_TIMEOUT, canbus_tx8_callback, frame_id);
|
|
||||||
// Send the CAN frame with a timeout and callback
|
|
||||||
if (rc != 0)
|
|
||||||
{
|
|
||||||
LOG_ERR("Failed to queue CAN frame: %d", rc);
|
|
||||||
free(frame_id); // Free the allocated memory for frame ID
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void canbus_tx16_callback(const struct device *dev, int error, void *user_data)
|
|
||||||
{
|
|
||||||
ARG_UNUSED(dev);
|
|
||||||
uint8_t frame_id = *(uint8_t *)user_data; // Retrieve the frame ID from user data
|
|
||||||
|
|
||||||
if (error != 0)
|
|
||||||
{
|
|
||||||
LOG_ERR("CAN transmission error. Error code: %d, Frame ID: %d", error, frame_id);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
LOG_DBG("CAN message with Frame ID %d sent successfully", frame_id);
|
|
||||||
}
|
|
||||||
free(user_data); // Free the allocated memory for frame ID
|
|
||||||
}
|
|
||||||
|
|
||||||
int canbus_send16(uint16_t reg, uint16_t value)
|
|
||||||
{
|
|
||||||
int rc = 0;
|
|
||||||
union data_type
|
|
||||||
{
|
|
||||||
int16_t value;
|
|
||||||
uint8_t bytes[2];
|
|
||||||
} data;
|
|
||||||
data.value = sys_cpu_to_be16(value); // Convert value to big-endian format
|
|
||||||
|
|
||||||
struct can_frame frame = {
|
|
||||||
.id = (node_id << 8) | reg, // Standard ID with node ID in the first byte
|
|
||||||
.dlc = sizeof(data), // Data Length Code (DLC)
|
|
||||||
};
|
|
||||||
memcpy(frame.data, data.bytes, sizeof(data)); // Copy data into the frame
|
|
||||||
uint8_t *frame_id = malloc(sizeof(uint8_t)); // Allocate memory for frame ID
|
|
||||||
LOG_DBG("Preparing to send 16-bit value 0x%04x to register 0x%02x on node %d", value, reg, node_id);
|
|
||||||
if (frame_id == NULL)
|
|
||||||
{
|
|
||||||
LOG_ERR("Failed to allocate memory for frame ID");
|
|
||||||
return -ENOMEM; // Not enough memory
|
|
||||||
}
|
|
||||||
*frame_id = can_msg_id++; // Increment message ID for uniqueness
|
|
||||||
LOG_DBG("Using frame ID: %d", *frame_id);
|
|
||||||
rc = can_send(can_dev, &frame, CANBUS_TX_TIMEOUT, canbus_tx16_callback, frame_id);
|
|
||||||
// Send the CAN frame with a timeout and callback
|
|
||||||
if (rc != 0)
|
|
||||||
{
|
|
||||||
LOG_ERR("Failed to queue CAN frame: %d", rc);
|
|
||||||
free(frame_id); // Free the allocated memory for frame ID
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_DBG("Queued 16-bit value 0x%04x to register 0x%02x on node %d, frame ID: %d",
|
|
||||||
value, reg, node_id, *frame_id);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
#ifndef __CANBUS_H__
|
|
||||||
#define __CANBUS_H__
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include "canbus_registers.h"
|
|
||||||
|
|
||||||
#define CANBUS_RX_THREAD_STACK_SIZE (512) // Stack size for the CAN RX thread
|
|
||||||
#define CANBUS_RX_THREAD_PRIORITY (5) // Priority for the CAN RX thread
|
|
||||||
#define CANBUS_RX_MSGQ_SIZE (5) // Size of the message queue for CAN RX thread
|
|
||||||
#define CANBUS_TX_TIMEOUT K_MSEC(100) // Timeout for sending CAN messages in milliseconds
|
|
||||||
|
|
||||||
int canbus_init(void);
|
|
||||||
int canbus_send8(uint16_t reg, uint8_t value);
|
|
||||||
int canbus_send16(uint16_t reg, uint16_t value);
|
|
||||||
|
|
||||||
#endif // __CANBUS_H__
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
#ifndef __CANBUS_REGISTERS_H__
|
|
||||||
#define __CANBUS_REGISTERS_H__
|
|
||||||
|
|
||||||
enum canbus_registers {
|
|
||||||
CANBUS_REG_REBOOT = 0x00,
|
|
||||||
CANBUS_REG_STATE = 0x01,
|
|
||||||
CANBUS_REG_ERROR = 0x02,
|
|
||||||
|
|
||||||
CANBUS_REG_VALVE_STATUS = 0x10,
|
|
||||||
CANBUS_REG_VALVE_OPERATION = 0x11,
|
|
||||||
CANBUS_REG_VALVE_COMMAND = 0x12,
|
|
||||||
|
|
||||||
CANBUS_REG_WATERLEVEL_STATE = 0x20,
|
|
||||||
CANBUS_REG_WATERLEVEL_LEVEL = 0x21,
|
|
||||||
CANBUS_REG_WATERLEVEL_ZERO_POINT = 0x22,
|
|
||||||
CANBUS_REG_WATERLEVEL_MAX_POINT = 0x23,
|
|
||||||
};
|
|
||||||
|
|
||||||
enum valve_status {
|
|
||||||
VALVE_STATE_CLOSED = 0x00,
|
|
||||||
VALVE_STATE_OPEN = 0x01,
|
|
||||||
VALVE_STATE_ERROR = 0x02,
|
|
||||||
VALVE_STATE_UNKNOWN = 0x03,
|
|
||||||
};
|
|
||||||
|
|
||||||
enum valve_operation_state {
|
|
||||||
VALVE_OPERATION_IDLE = 0x00,
|
|
||||||
VALVE_OPERATION_OPENING = 0x01,
|
|
||||||
VALVE_OPERATION_CLOSING = 0x02,
|
|
||||||
};
|
|
||||||
|
|
||||||
enum valve_command {
|
|
||||||
VALVE_COMMAND_STOP = 0x00,
|
|
||||||
VALVE_COMMAND_OPEN = 0x01,
|
|
||||||
VALVE_COMMAND_CLOSE = 0x02,
|
|
||||||
};
|
|
||||||
|
|
||||||
enum waterlevel_state {
|
|
||||||
WATERLEVEL_STATE_OK = 0x00,
|
|
||||||
WATERLEVEL_STATE_MODBUS_ERROR = 0x02,
|
|
||||||
};
|
|
||||||
#endif // __CANBUS_REGISTERS_H__
|
|
||||||
1
software/lib/fwu/CMakeLists.txt
Normal file
1
software/lib/fwu/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
zephyr_library_sources(fwu.c)
|
||||||
5
software/lib/fwu/Kconfig
Normal file
5
software/lib/fwu/Kconfig
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
config LIB_FWU
|
||||||
|
bool "Enable Firmware Update Library"
|
||||||
|
default y
|
||||||
|
help
|
||||||
|
Enable the Firmware Update Library.
|
||||||
45
software/lib/fwu/fwu.c
Normal file
45
software/lib/fwu/fwu.c
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
#include <zephyr/kernel.h>
|
||||||
|
#include <zephyr/sys/crc.h>
|
||||||
|
#include <zephyr/sys/byteorder.h>
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
#include <lib/fwu.h>
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(fwu, LOG_LEVEL_INF);
|
||||||
|
|
||||||
|
#define FWU_BUFFER_SIZE 256
|
||||||
|
static uint8_t fwu_buffer[FWU_BUFFER_SIZE];
|
||||||
|
static uint32_t fwu_chunk_offset = 0;
|
||||||
|
static uint16_t fwu_chunk_size = 0;
|
||||||
|
static uint16_t fwu_last_chunk_crc = 0;
|
||||||
|
|
||||||
|
void fwu_init(void) {}
|
||||||
|
|
||||||
|
void fwu_handler(uint16_t addr, uint16_t reg)
|
||||||
|
{
|
||||||
|
// This is a simplified handler. In a real scenario, you would have a proper mapping
|
||||||
|
// between register addresses and actions.
|
||||||
|
if (addr == 0x0100) { // FWU_COMMAND
|
||||||
|
if (reg == 1) { LOG_INF("FWU: Chunk at offset %u (size %u) verified.", fwu_chunk_offset, fwu_chunk_size); }
|
||||||
|
else if (reg == 2) { LOG_INF("FWU: Finalize command received. Rebooting (simulated)."); }
|
||||||
|
} else if (addr == 0x0101) { // FWU_CHUNK_OFFSET_LOW
|
||||||
|
fwu_chunk_offset = (fwu_chunk_offset & 0xFFFF0000) | reg;
|
||||||
|
} else if (addr == 0x0102) { // FWU_CHUNK_OFFSET_HIGH
|
||||||
|
fwu_chunk_offset = (fwu_chunk_offset & 0x0000FFFF) | ((uint32_t)reg << 16);
|
||||||
|
} else if (addr == 0x0103) { // FWU_CHUNK_SIZE
|
||||||
|
fwu_chunk_size = (reg > FWU_BUFFER_SIZE) ? FWU_BUFFER_SIZE : reg;
|
||||||
|
} else if (addr >= 0x0180 && addr < (0x0180 + (FWU_BUFFER_SIZE / 2))) {
|
||||||
|
uint16_t index = (addr - 0x0180) * 2;
|
||||||
|
if (index < sizeof(fwu_buffer)) {
|
||||||
|
sys_put_be16(reg, &fwu_buffer[index]);
|
||||||
|
if (index + 2 >= fwu_chunk_size) {
|
||||||
|
fwu_last_chunk_crc = crc16_ccitt(0xffff, fwu_buffer, fwu_chunk_size);
|
||||||
|
LOG_INF("FWU: Chunk received, CRC is 0x%04X", fwu_last_chunk_crc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t fwu_get_last_chunk_crc(void)
|
||||||
|
{
|
||||||
|
return fwu_last_chunk_crc;
|
||||||
|
}
|
||||||
1
software/lib/modbus_server/CMakeLists.txt
Normal file
1
software/lib/modbus_server/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
zephyr_library_sources(modbus_server.c)
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user