Compare commits
73 Commits
CAN
...
3c2235733b
| Author | SHA1 | Date | |
|---|---|---|---|
| 3c2235733b | |||
| a3e8d5c168 | |||
| 5fd904de9e | |||
| 92bb171e85 | |||
| bd8a7a766c | |||
| 8f89713866 | |||
| bf29061db6 | |||
| c1622bb01c | |||
| 222ffea568 | |||
| a9a0626913 | |||
| b11f844415 | |||
| 2e8a86bc54 | |||
| 224adccf6b | |||
| 9b7159d5a4 | |||
| bc327acc41 | |||
| c9b0f38576 | |||
| edf0fb2563 | |||
| 537d76ef5d | |||
| 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
|
||||||
@@ -37,7 +39,7 @@ Die Slave-Nodes sind die Arbeitseinheiten im Feld. Um bei der Fertigung kleiner
|
|||||||
|
|
||||||
* **Mikrocontroller:** Ein `STM32G431PB`. Dieser ist zwar leistungsstark, bietet aber alle nötigen Peripherien (mehrere UARTs, ADCs, CAN) und ermöglicht ein einheitliches Hardware- und Software-Design.
|
* **Mikrocontroller:** Ein `STM32G431PB`. Dieser ist zwar leistungsstark, bietet aber alle nötigen Peripherien (mehrere UARTs, ADCs, CAN) und ermöglicht ein einheitliches Hardware- und Software-Design.
|
||||||
* **Peripherie pro Node:**
|
* **Peripherie pro Node:**
|
||||||
* **Zwei High-Side Ausgänge (+12V):** Realisiert über einen `VND7050AJ`. Perfekt zur Ansteuerung der 12V-Motorventile (`Öffnen`/`Schliessen`). Die `Sense`-Leitung des Treibers wird über einen AD-Wandler ausgelesen, um durch Messung des Motorstroms eine Endlagen-Erkennung ohne physische Endschalter zu realisieren (Motorstrom im Stillstand ≈ 0).
|
* **Zwei High-Side Ausgänge (+12V):** Realisiert über einen `VND7050AJ`. Perfekt zur Ansteuerung der 12V-Motorventile (`Öffnen`/`Schliessen`). Die `Sense`-Leitung des Treibers wird über einen AD-Wandler ausgelesen, um durch Messung des Motorstroms eine Endlagen-Erkennung ohne physische Endschalter zu realisieren (Motorstrom im Stillstand ≈ 0). Zusätzlich können die Temperatur und die Versorgungsspannung des Treibers ausgelesen werden.
|
||||||
* **Zwei Low-Side Ausgänge (0V):** Über N-Kanal-MOSFETs geschaltete Ausgänge. Nutzbar zur Ansteuerung von 12V-LEDs in Tastern oder zum Schalten des Halbleiter-Relais für die Pumpe.
|
* **Zwei Low-Side Ausgänge (0V):** Über N-Kanal-MOSFETs geschaltete Ausgänge. Nutzbar zur Ansteuerung von 12V-LEDs in Tastern oder zum Schalten des Halbleiter-Relais für die Pumpe.
|
||||||
* **Zwei digitale Eingänge:** Direkte, geschützte Eingänge am Controller zum Anschluss von Tastern oder den kapazitiven NPN-Sensoren.
|
* **Zwei digitale Eingänge:** Direkte, geschützte Eingänge am Controller zum Anschluss von Tastern oder den kapazitiven NPN-Sensoren.
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -27,26 +29,33 @@ Alle Register sind in einer einzigen, durchgehenden Liste pro Register-Typ (`Inp
|
|||||||
|
|
||||||
| Adresse (hex) | Name | Zugehörigkeit | Beschreibung |
|
| Adresse (hex) | Name | Zugehörigkeit | Beschreibung |
|
||||||
| :------------ | :----------------------------- | :---------------- | :---------------------------------------------------------------------------------------------------------------------------------------- |
|
| :------------ | :----------------------------- | :---------------- | :---------------------------------------------------------------------------------------------------------------------------------------- |
|
||||||
| **0x0000** | `VENTIL_ZUSTAND_BEWEGUNG` | Ventil | Kombiniertes Status-Register. **High-Byte**: Bewegung (`0`=Idle, `1`=Öffnet, `2`=Schliesst, `3`=Fehler). **Low-Byte**: Zustand (`0`=Geschlossen, `1`=Geöffnet). |
|
| **0x0000** | `VALVE_STATE_MOVEMENT` | Ventil | Kombiniertes Status-Register. **High-Byte**: Bewegung (`0`=Idle, `1`=Öffnet, `2`=Schliesst, `3`=Fehler). **Low-Byte**: Zustand (`0`=Geschlossen, `1`=Geöffnet). |
|
||||||
| **0x0001** | `MOTORSTROM_MA` | Ventil | Aktueller Motorstrom in Milliampere (mA). |
|
| **0x0001** | `MOTORSTROM_OPEN_MA` | Ventil | Motorstrom beim Öffnen in Milliampere (mA). |
|
||||||
| **0x0020** | `DIGITAL_EINGAENGE_ZUSTAND` | Eingänge | Bitmaske der digitalen Eingänge. Bit 0: Eingang 1, Bit 1: Eingang 2. `1`=Aktiv. |
|
| **0x0002** | `MOTORSTROM_CLOSE_MA` | Ventil | Motorstrom beim Schließen in Milliampere (mA). |
|
||||||
| **0x0021** | `TASTER_EVENTS` | Eingänge | Event-Flags für Taster (Clear-on-Read). Bit 0: Taster 1 gedrückt. Bit 1: Taster 2 gedrückt. |
|
| **0x0020** | `DIGITAL_INPUTS_STATE` | Eingänge | Bitmaske der digitalen Eingänge. Bit 0: Eingang 1, Bit 1: Eingang 2. `1`=Aktiv. |
|
||||||
|
| **0x0021** | `BUTTON_EVENTS` | Eingänge | Event-Flags für Taster (Clear-on-Read). Bit 0: Taster 1 gedrückt. Bit 1: Taster 2 gedrückt. |
|
||||||
| **0x00F0** | `FIRMWARE_VERSION_MAJOR_MINOR` | System | z.B. `0x0102` für v1.2. |
|
| **0x00F0** | `FIRMWARE_VERSION_MAJOR_MINOR` | System | z.B. `0x0102` für v1.2. |
|
||||||
| **0x00F1** | `FIRMWARE_VERSION_PATCH` | System | z.B. `3` für v1.2.3. |
|
| **0x00F1** | `FIRMWARE_VERSION_PATCH` | System | z.B. `3` für v1.2.3. |
|
||||||
| **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)
|
||||||
|
|
||||||
| Adresse (hex) | Name | Zugehörigkeit | Beschreibung |
|
| Adresse (hex) | Name | Zugehörigkeit | Beschreibung |
|
||||||
| :------------ | :---------------------------- | :---------------- | :---------------------------------------------------------------------------------------------------------------------------------------- |
|
| :------------ | :---------------------------- | :---------------- | :---------------------------------------------------------------------------------------------------------------------------------------- |
|
||||||
| **0x0000** | `VENTIL_BEFEHL` | Ventil | `1`=Öffnen, `2`=Schliessen, `0`=Bewegung stoppen. |
|
| **0x0000** | `VALVE_COMMAND` | Ventil | `1`=Öffnen, `2`=Schliessen, `0`=Bewegung stoppen. |
|
||||||
| **0x0001** | `MAX_OEFFNUNGSZEIT_S` | Ventil | Sicherheits-Timeout in Sekunden für den Öffnen-Vorgang. |
|
| **0x0001** | `MAX_OPENING_TIME_S` | Ventil | Sicherheits-Timeout in Sekunden für den Öffnen-Vorgang. |
|
||||||
| **0x0002** | `MAX_SCHLIESSZEIT_S` | Ventil | Sicherheits-Timeout in Sekunden für den Schliessen-Vorgang. |
|
| **0x0002** | `MAX_CLOSING_TIME_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. |
|
| **0x0003** | `END_CURRENT_THRESHOLD_OPEN_MA` | Ventil | Minimaler Stromschwellenwert in mA zur Endlagenerkennung beim Öffnen. |
|
||||||
|
| **0x0004** | `END_CURRENT_THRESHOLD_CLOSE_MA` | Ventil | Minimaler Stromschwellenwert in mA zur Endlagenerkennung beim Schliessen. |
|
||||||
|
| **0x0005** | `OBSTACLE_CURRENT_THRESHOLD_OPEN_MA` | Ventil | Stromschwellenwert in mA zur Hinderniserkennung beim Öffnen. |
|
||||||
|
| **0x0006** | `OBSTACLE_CURRENT_THRESHOLD_CLOSE_MA` | Ventil | Stromschwellenwert in mA zur Hinderniserkennung beim Schliessen. |
|
||||||
|
| **0x0010** | `DIGITAL_OUTPUTS_STATE` | 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. |
|
||||||
@@ -76,10 +85,10 @@ Diese Register gehören zum externen Füllstandsensor und können auf dem Bus eb
|
|||||||
|
|
||||||
| Adresse (hex) | Name | R/W | Beschreibung |
|
| Adresse (hex) | Name | R/W | Beschreibung |
|
||||||
| :------------ | :------------------------- | :-- | :---------------------------------------------------------------------------------------------------------------------------------------- |
|
| :------------ | :------------------------- | :-- | :---------------------------------------------------------------------------------------------------------------------------------------- |
|
||||||
| **0x0000** | `NODE_ADRESSE` | R/W | Geräteadresse des Sensors (1-255). |
|
| **0x0000** | `NODE_ADDRESS` | R/W | Geräteadresse des Sensors (1-255). |
|
||||||
| **0x0001** | `BAUDRATE` | R/W | `0`=1200, `1`=2400, `2`=4800, `3`=9600, `4`=19200, `5`=38400, `6`=57600, `7`=115200. |
|
| **0x0001** | `BAUDRATE` | R/W | `0`=1200, `1`=2400, `2`=4800, `3`=9600, `4`=19200, `5`=38400, `6`=57600, `7`=115200. |
|
||||||
| **0x0002** | `EINHEIT` | R/W | `0`=Keine, `1`=cm, `2`=mm, `3`=MPa, `4`=Pa, `5`=kPa. |
|
| **0x0002** | `UNIT` | R/W | `0`=Keine, `1`=cm, `2`=mm, `3`=MPa, `4`=Pa, `5`=kPa. |
|
||||||
| **0x0003** | `NACHKOMMASTELLEN` | R/W | Anzahl der Dezimalstellen für den Messwert (0-3). |
|
| **0x0003** | `DECIMAL_PLACES` | R/W | Anzahl der Dezimalstellen für den Messwert (0-3). |
|
||||||
| **0x0004** | `MESSWERT_AKTUELL` | R | Der skalierte Messwert als vorzeichenbehafteter 16-Bit-Integer. |
|
| **0x0004** | `CURRENT_MEASUREMENT` | R | Der skalierte Messwert als vorzeichenbehafteter 16-Bit-Integer. |
|
||||||
| **0x0005** | `MESSBEREICH_NULLPUNKT` | R/W | Rohwert für den Nullpunkt der Skala. |
|
| **0x0005** | `MEASUREMENT_RANGE_ZERO_POINT` | R/W | Rohwert für den Nullpunkt der Skala. |
|
||||||
| **0x0006** | `MESSBEREICH_ENDPUNKT` | R/W | Rohwert für den Endpunkt der Skala. |
|
| **0x0006** | `MEASUREMENT_RANGE_END_POINT` | R/W | Rohwert für den Endpunkt der Skala. |
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -7,11 +9,13 @@
|
|||||||
| ✅ | **Phase 0: Planung & Definition** | | |
|
| ✅ | **Phase 0: Planung & Definition** | | |
|
||||||
| ✅ | Konzept erstellen und finalisieren | 30.06.2025 | Architektur, Komponenten und grundlegende Architektur sind festgelegt. |
|
| ✅ | Konzept erstellen und finalisieren | 30.06.2025 | Architektur, Komponenten und grundlegende Architektur sind festgelegt. |
|
||||||
| ✅ | MODBUS Register Map definieren | 30.06.2025 | Die "API" der Slaves ist definiert und bildet die Grundlage für die Software-Entwicklung. |
|
| ✅ | MODBUS Register Map definieren | 30.06.2025 | Die "API" der Slaves ist definiert und bildet die Grundlage für die Software-Entwicklung. |
|
||||||
|
| ✅ | Header- und deutsche Dokumentation aktualisiert | 10.07.2025 | Doxygen-Kommentare in Headern und deutsche .md-Dateien auf den neuesten Stand gebracht und übersetzt. |
|
||||||
| ☐ | **Phase 1: Slave-Node Prototyp (STM32 Eval-Board)** | | **Ziel:** Ein einzelner Slave wird auf dem Eval-Board zum Leben erweckt. |
|
| ☐ | **Phase 1: Slave-Node Prototyp (STM32 Eval-Board)** | | **Ziel:** Ein einzelner Slave wird auf dem Eval-Board zum Leben erweckt. |
|
||||||
| ✅ | 1.1 Entwicklungsumgebung für STM32/Zephyr einrichten | 30.06.2025 | Toolchain, VS Code, Zephyr-SDK, MCUBoot etc. installieren und ein "Hello World" zum Laufen bringen. |
|
| ✅ | 1.1 Entwicklungsumgebung für STM32/Zephyr einrichten | 30.06.2025 | Toolchain, VS Code, Zephyr-SDK, MCUBoot etc. installieren und ein "Hello World" zum Laufen bringen. |
|
||||||
| ☐ | 1.2 Basis-Firmware für Slave-Node erstellen | | Hardware-Abstraktion (GPIOs, ADC, UART für RS485) implementieren. |
|
| ✅ | 1.2 Hardware-Abstraktion (VND7050AJ, RS485) | 10.07.2025 | Implementierung der Treiber für den VND7050AJ und die RS485-Kommunikation. |
|
||||||
| ☐ | 1.3 MODBUS-RTU Stack auf dem Slave implementieren | | Basierend auf der definierten Register-Map. Zuerst nur lesende Funktionen (Status, Version). |
|
| ✅ | 1.3 Basis-Firmware für Slave-Node erstellen | 10.07.2025 | Hardware-Abstraktion (GPIOs) implementiert. |
|
||||||
| ☐ | 1.4 Kernlogik implementieren (z.B. Ventilsteuerung) | | Umsetzung der `VENTIL_ZUSTAND_BEWEGUNG` Logik, Strommessung für Endlagen etc. |
|
| ✅ | 1.3 MODBUS-RTU Stack auf dem Slave implementieren | 10.07.2025 | Basierend auf der definierten Register-Map. Zuerst nur lesende Funktionen (Status, Version). |
|
||||||
|
| ✅ | 1.4 Kernlogik implementieren (z.B. Ventilsteuerung) | 10.07.2025 | Umsetzung der `VALVE_STATE_MOVEMENT` Logik, Strommessung für Endlagen etc. |
|
||||||
| ☐ | **Phase 2: Verifikation der Slave-Firmware** | | **Ziel:** Nachweisen, dass der Slave sich exakt an die MODBUS-Spezifikation hält. |
|
| ☐ | **Phase 2: Verifikation der Slave-Firmware** | | **Ziel:** Nachweisen, dass der Slave sich exakt an die MODBUS-Spezifikation hält. |
|
||||||
| ☐ | 2.1 Slave-Node mit PC via USB-MODBUS-Adapter testen | | **Kritischer Meilenstein.** Mit Tools wie "QModMaster" oder einem Python-Skript die Register lesen & schreiben. Die Slave-Firmware wird so unabhängig vom Gateway validiert. |
|
| ☐ | 2.1 Slave-Node mit PC via USB-MODBUS-Adapter testen | | **Kritischer Meilenstein.** Mit Tools wie "QModMaster" oder einem Python-Skript die Register lesen & schreiben. Die Slave-Firmware wird so unabhängig vom Gateway validiert. |
|
||||||
| ☐ | 2.2 Firmware-Update Mechanismus testen | | Den kompletten Update-Prozess (Chunking, CRC-Check) mit einem Skript vom PC aus testen. Der Slave schreibt die Firmware dabei vorerst nur in einen ungenutzten RAM-Bereich. |
|
| ☐ | 2.2 Firmware-Update Mechanismus testen | | Den kompletten Update-Prozess (Chunking, CRC-Check) mit einem Skript vom PC aus testen. Der Slave schreibt die Firmware dabei vorerst nur in einen ungenutzten RAM-Bereich. |
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
56
setup-format-hook.sh
Executable file
56
setup-format-hook.sh
Executable file
@@ -0,0 +1,56 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
# This script sets up a Git pre-commit hook to automatically format C/C++ files
|
||||||
|
# in the 'software/' subdirectory using clang-format.
|
||||||
|
|
||||||
|
# Define the path for the pre-commit hook
|
||||||
|
HOOK_DIR=".git/hooks"
|
||||||
|
HOOK_FILE="$HOOK_DIR/pre-commit"
|
||||||
|
|
||||||
|
# Create the hooks directory if it doesn't exist
|
||||||
|
mkdir -p "$HOOK_DIR"
|
||||||
|
|
||||||
|
# Create the pre-commit hook script using a 'here document'
|
||||||
|
cat > "$HOOK_FILE" << 'EOF'
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
# --- Pre-commit hook for clang-format ---
|
||||||
|
#
|
||||||
|
# This hook formats staged C, C++, and Objective-C files in the 'software/'
|
||||||
|
# subdirectory before a commit is made.
|
||||||
|
# It automatically finds the .clang-format file in the software/ directory.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Directory to be formatted
|
||||||
|
TARGET_DIR="software/"
|
||||||
|
|
||||||
|
# Use git diff to find staged files that are Added (A), Copied (C), or Modified (M).
|
||||||
|
# We filter for files only within the TARGET_DIR.
|
||||||
|
# The grep regex matches common C/C++ and Objective-C file extensions.
|
||||||
|
FILES_TO_FORMAT=$(git diff --cached --name-only --diff-filter=ACM "$TARGET_DIR" | grep -E '\.(c|h|cpp|hpp|cxx|hxx|cc|hh|m|mm)$')
|
||||||
|
|
||||||
|
if [ -z "$FILES_TO_FORMAT" ]; then
|
||||||
|
# No relevant files to format, exit successfully.
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "› Running clang-format on staged files in '$TARGET_DIR'..."
|
||||||
|
|
||||||
|
# Run clang-format in-place on the identified files.
|
||||||
|
# clang-format will automatically find the .clang-format file in the software/ directory
|
||||||
|
# or any of its parent directories.
|
||||||
|
echo "$FILES_TO_FORMAT" | xargs clang-format -i
|
||||||
|
|
||||||
|
# Since clang-format may have changed the files, we need to re-stage them.
|
||||||
|
echo "$FILES_TO_FORMAT" | xargs git add
|
||||||
|
|
||||||
|
echo "› Formatting complete."
|
||||||
|
|
||||||
|
exit 0
|
||||||
|
EOF
|
||||||
|
|
||||||
|
# Make the hook executable
|
||||||
|
chmod +x "$HOOK_FILE"
|
||||||
|
|
||||||
|
echo "✅ Git pre-commit hook has been set up successfully."
|
||||||
|
echo " It will now automatically format files in the '$PWD/software' directory before each commit."
|
||||||
142
software/.clang-format
Normal file
142
software/.clang-format
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
# Zephyr Project .clang-format configuration
|
||||||
|
# Based on Linux kernel style with Zephyr-specific adaptations
|
||||||
|
|
||||||
|
# Use LLVM as the base style and customize from there
|
||||||
|
BasedOnStyle: LLVM
|
||||||
|
|
||||||
|
# Language settings
|
||||||
|
Language: Cpp
|
||||||
|
|
||||||
|
# Indentation settings
|
||||||
|
IndentWidth: 8
|
||||||
|
TabWidth: 8
|
||||||
|
UseTab: ForIndentation
|
||||||
|
|
||||||
|
# Line length
|
||||||
|
ColumnLimit: 100
|
||||||
|
|
||||||
|
# Brace settings
|
||||||
|
BreakBeforeBraces: Linux
|
||||||
|
BraceWrapping:
|
||||||
|
AfterClass: true
|
||||||
|
AfterControlStatement: false
|
||||||
|
AfterEnum: true
|
||||||
|
AfterFunction: true
|
||||||
|
AfterNamespace: true
|
||||||
|
AfterStruct: true
|
||||||
|
AfterUnion: true
|
||||||
|
BeforeCatch: true
|
||||||
|
BeforeElse: false
|
||||||
|
IndentBraces: false
|
||||||
|
SplitEmptyFunction: true
|
||||||
|
SplitEmptyRecord: true
|
||||||
|
SplitEmptyNamespace: true
|
||||||
|
|
||||||
|
# Always add braces for control statements (Zephyr requirement)
|
||||||
|
RemoveBracesLLVM: false
|
||||||
|
|
||||||
|
# Control statement settings
|
||||||
|
SpaceBeforeParens: ControlStatements
|
||||||
|
SpacesInParentheses: false
|
||||||
|
|
||||||
|
# Function settings
|
||||||
|
AllowShortFunctionsOnASingleLine: None
|
||||||
|
AllowShortBlocksOnASingleLine: Empty
|
||||||
|
AllowShortIfStatementsOnASingleLine: Never
|
||||||
|
AllowShortLoopsOnASingleLine: false
|
||||||
|
AllowShortCaseLabelsOnASingleLine: false
|
||||||
|
|
||||||
|
# Pointer and reference alignment
|
||||||
|
PointerAlignment: Right
|
||||||
|
ReferenceAlignment: Right
|
||||||
|
|
||||||
|
# Spacing settings
|
||||||
|
SpaceAfterCStyleCast: false
|
||||||
|
SpaceAfterLogicalNot: false
|
||||||
|
SpaceBeforeAssignmentOperators: true
|
||||||
|
SpaceBeforeCpp11BracedList: false
|
||||||
|
SpaceBeforeCtorInitializerColon: true
|
||||||
|
SpaceBeforeInheritanceColon: true
|
||||||
|
SpaceBeforeRangeBasedForLoopColon: true
|
||||||
|
SpaceInEmptyParentheses: false
|
||||||
|
SpacesBeforeTrailingComments: 1
|
||||||
|
SpacesInAngles: false
|
||||||
|
SpacesInCStyleCastParentheses: false
|
||||||
|
SpacesInContainerLiterals: false
|
||||||
|
SpacesInSquareBrackets: false
|
||||||
|
|
||||||
|
# Alignment settings
|
||||||
|
AlignAfterOpenBracket: DontAlign
|
||||||
|
AlignConsecutiveAssignments: false
|
||||||
|
AlignConsecutiveDeclarations: false
|
||||||
|
AlignEscapedNewlines: Right
|
||||||
|
AlignOperands: false
|
||||||
|
AlignTrailingComments: false
|
||||||
|
|
||||||
|
# Breaking settings
|
||||||
|
AlwaysBreakAfterDefinitionReturnType: None
|
||||||
|
AlwaysBreakAfterReturnType: None
|
||||||
|
AlwaysBreakBeforeMultilineStrings: false
|
||||||
|
AlwaysBreakTemplateDeclarations: false
|
||||||
|
BinPackArguments: false
|
||||||
|
BinPackParameters: false
|
||||||
|
BreakBeforeBinaryOperators: None
|
||||||
|
BreakBeforeTernaryOperators: true
|
||||||
|
BreakConstructorInitializersBeforeComma: false
|
||||||
|
BreakAfterJavaFieldAnnotations: false
|
||||||
|
BreakStringLiterals: true
|
||||||
|
|
||||||
|
# Penalties (used for line breaking decisions)
|
||||||
|
PenaltyBreakAssignment: 2
|
||||||
|
PenaltyBreakBeforeFirstCallParameter: 19
|
||||||
|
PenaltyBreakComment: 300
|
||||||
|
PenaltyBreakFirstLessLess: 120
|
||||||
|
PenaltyBreakString: 1000
|
||||||
|
PenaltyExcessCharacter: 1000000
|
||||||
|
PenaltyReturnTypeOnItsOwnLine: 60
|
||||||
|
|
||||||
|
# Comment settings
|
||||||
|
ReflowComments: true
|
||||||
|
CommentPragmas: '^ IWYU pragma:'
|
||||||
|
|
||||||
|
# Sorting settings
|
||||||
|
SortIncludes: true
|
||||||
|
SortUsingDeclarations: true
|
||||||
|
|
||||||
|
# Preprocessor settings
|
||||||
|
IndentPPDirectives: None
|
||||||
|
MacroBlockBegin: ''
|
||||||
|
MacroBlockEnd: ''
|
||||||
|
|
||||||
|
# Misc settings
|
||||||
|
CompactNamespaces: false
|
||||||
|
ConstructorInitializerAllOnOneLineOrOnePerLine: false
|
||||||
|
ConstructorInitializerIndentWidth: 4
|
||||||
|
ContinuationIndentWidth: 4
|
||||||
|
Cpp11BracedListStyle: true
|
||||||
|
DerivePointerAlignment: false
|
||||||
|
DisableFormat: false
|
||||||
|
ExperimentalAutoDetectBinPacking: false
|
||||||
|
FixNamespaceComments: true
|
||||||
|
ForEachMacros: ['LISTIFY', 'FOR_EACH', 'FOR_EACH_FIXED_ARG', 'FOR_EACH_IDX', 'FOR_EACH_IDX_FIXED_ARG', 'FOR_EACH_NONEMPTY_TERM', 'Z_FOR_EACH', 'Z_FOR_EACH_FIXED_ARG', 'Z_FOR_EACH_IDX', 'Z_FOR_EACH_IDX_FIXED_ARG']
|
||||||
|
IncludeBlocks: Preserve
|
||||||
|
IncludeCategories:
|
||||||
|
- Regex: '^<zephyr/.*\.h>'
|
||||||
|
Priority: 1
|
||||||
|
- Regex: '^<.*\.h>'
|
||||||
|
Priority: 2
|
||||||
|
- Regex: '^<.*'
|
||||||
|
Priority: 3
|
||||||
|
- Regex: '.*'
|
||||||
|
Priority: 4
|
||||||
|
IndentCaseLabels: false
|
||||||
|
IndentWrappedFunctionNames: false
|
||||||
|
JavaScriptQuotes: Leave
|
||||||
|
JavaScriptWrapImports: true
|
||||||
|
KeepEmptyLinesAtTheStartOfBlocks: false
|
||||||
|
MaxEmptyLinesToKeep: 1
|
||||||
|
NamespaceIndentation: None
|
||||||
|
ObjCBinPackProtocolList: Auto
|
||||||
|
ObjCBlockIndentWidth: 2
|
||||||
|
ObjCSpaceAfterProperty: false
|
||||||
|
ObjCSpaceBeforeProtocolList: true
|
||||||
23
software/.vscode/settings.json
vendored
23
software/.vscode/settings.json
vendored
@@ -1,12 +1,15 @@
|
|||||||
{
|
{
|
||||||
// Hush CMake
|
// Hush CMake
|
||||||
"cmake.configureOnOpen": false,
|
"cmake.configureOnOpen": false,
|
||||||
|
// IntelliSense
|
||||||
// IntelliSense
|
"C_Cpp.default.compilerPath": "${userHome}/zephyr-sdk-0.17.1/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc.exe",
|
||||||
"C_Cpp.default.compilerPath": "${userHome}/zephyr-sdk-0.17.1/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc.exe",
|
"C_Cpp.default.compileCommands": "${workspaceFolder}/build/compile_commands.json",
|
||||||
"C_Cpp.default.compileCommands": "${workspaceFolder}/build/compile_commands.json",
|
// File Associations
|
||||||
|
"files.associations": {
|
||||||
// File Associations
|
"app_version.h": "c"
|
||||||
"files.associations": {
|
},
|
||||||
}
|
"C_Cpp.clang_format_style": "file",
|
||||||
|
"nrf-connect.applications": [
|
||||||
|
"${workspaceFolder}/apps/slave_node"
|
||||||
|
],
|
||||||
}
|
}
|
||||||
40
software/.vscode/tasks.json
vendored
40
software/.vscode/tasks.json
vendored
@@ -2,32 +2,20 @@
|
|||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"tasks": [
|
"tasks": [
|
||||||
{
|
{
|
||||||
"label": "West Build",
|
"label": "Format All C/C++ Files",
|
||||||
"type": "shell",
|
"type": "shell",
|
||||||
"group": {
|
"command": "find . -name \"*.c\" -o -name \"*.h\" | xargs clang-format -i",
|
||||||
"kind": "build",
|
"problemMatcher": [],
|
||||||
"isDefault": true
|
"group": {
|
||||||
},
|
"kind": "build",
|
||||||
"linux": {
|
"isDefault": true
|
||||||
"command": "${userHome}/zephyrproject/.venv/bin/west"
|
},
|
||||||
},
|
"presentation": {
|
||||||
"windows": {
|
"reveal": "silent",
|
||||||
"command": "${userHome}/zephyrproject/.venv/Scripts/west.exe"
|
"clear": true,
|
||||||
},
|
"panel": "shared"
|
||||||
"osx": {
|
}
|
||||||
"command": "${userHome}/zephyrproject/.venv/bin/west"
|
},
|
||||||
},
|
|
||||||
"args": [
|
|
||||||
"build",
|
|
||||||
"-p",
|
|
||||||
"auto",
|
|
||||||
"-b",
|
|
||||||
"valve_node"
|
|
||||||
],
|
|
||||||
"problemMatcher": [
|
|
||||||
"$gcc"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"label": "West Configurable Build",
|
"label": "West Configurable Build",
|
||||||
"type": "shell",
|
"type": "shell",
|
||||||
|
|||||||
@@ -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
|
|
||||||
)
|
|
||||||
7
software/Kconfig
Normal file
7
software/Kconfig
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
rsource "lib/Kconfig"
|
||||||
|
rsource "lib/shell_valve/Kconfig"
|
||||||
|
|
||||||
|
config SLAVE_NODE_APP
|
||||||
|
bool "Slave Node Application"
|
||||||
|
default y
|
||||||
|
select SHELL_VALVE
|
||||||
0
software/apps/firmware_node/CMakeLists.txt
Normal file
0
software/apps/firmware_node/CMakeLists.txt
Normal file
0
software/apps/firmware_node/README.md
Normal file
0
software/apps/firmware_node/README.md
Normal file
0
software/apps/firmware_node/pm.yml
Normal file
0
software/apps/firmware_node/pm.yml
Normal file
0
software/apps/firmware_node/prj.conf
Normal file
0
software/apps/firmware_node/prj.conf
Normal file
0
software/apps/firmware_node/src/main.c
Normal file
0
software/apps/firmware_node/src/main.c
Normal file
0
software/apps/firmware_node/sysbuild.cmake
Normal file
0
software/apps/firmware_node/sysbuild.cmake
Normal file
0
software/apps/firmware_node/sysbuild.conf
Normal file
0
software/apps/firmware_node/sysbuild.conf
Normal file
0
software/apps/firmware_node/sysbuild/mcuboot.conf
Normal file
0
software/apps/firmware_node/sysbuild/mcuboot.conf
Normal file
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;
|
||||||
|
}
|
||||||
12
software/apps/slave_node/.vscode/c_cpp_properties.json
vendored
Normal file
12
software/apps/slave_node/.vscode/c_cpp_properties.json
vendored
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Linux",
|
||||||
|
"compileCommands": "${workspaceFolder}/build/compile_commands.json",
|
||||||
|
"cStandard": "c99",
|
||||||
|
"cppStandard": "gnu++17",
|
||||||
|
"intelliSenseMode": "linux-gcc-arm"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"version": 4
|
||||||
|
}
|
||||||
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"
|
||||||
5
software/apps/slave_node/VERSION
Normal file
5
software/apps/slave_node/VERSION
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
VERSION_MAJOR = 0
|
||||||
|
VERSION_MINOR = 0
|
||||||
|
PATCHLEVEL = 1
|
||||||
|
VERSION_TWEAK = 1
|
||||||
|
EXTRAVERSION = devel
|
||||||
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";
|
||||||
|
};
|
||||||
48
software/apps/slave_node/boards/weact_stm32g431_core.overlay
Normal file
48
software/apps/slave_node/boards/weact_stm32g431_core.overlay
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
/ {
|
||||||
|
aliases {
|
||||||
|
vnd7050aj = &vnd7050aj;
|
||||||
|
};
|
||||||
|
|
||||||
|
vnd7050aj: vnd7050aj {
|
||||||
|
compatible = "st,vnd7050aj";
|
||||||
|
status = "okay";
|
||||||
|
|
||||||
|
input0-gpios = <&gpiob 7 GPIO_ACTIVE_HIGH>;
|
||||||
|
input1-gpios = <&gpiob 9 GPIO_ACTIVE_HIGH>;
|
||||||
|
select0-gpios = <&gpiob 5 GPIO_ACTIVE_HIGH>;
|
||||||
|
select1-gpios = <&gpiob 6 GPIO_ACTIVE_HIGH>;
|
||||||
|
sense-enable-gpios = <&gpiob 4 GPIO_ACTIVE_HIGH>;
|
||||||
|
fault-reset-gpios = <&gpiob 3 GPIO_ACTIVE_LOW>;
|
||||||
|
io-channels = <&adc1 1>;
|
||||||
|
r-sense-ohms = <1500>;
|
||||||
|
k-vcc = <4139>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
&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>;
|
||||||
|
|
||||||
|
channel@1 {
|
||||||
|
reg = <1>;
|
||||||
|
zephyr,gain = "ADC_GAIN_1";
|
||||||
|
zephyr,reference = "ADC_REF_INTERNAL";
|
||||||
|
zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
|
||||||
|
zephyr,resolution = <12>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
&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";
|
||||||
|
};
|
||||||
16
software/apps/slave_node/cdc-acm.overlay
Normal file
16
software/apps/slave_node/cdc-acm.overlay
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#include <zephyr/dt-bindings/gpio/gpio.h>
|
||||||
|
|
||||||
|
&zephyr_udc0 {
|
||||||
|
cdc_acm_uart0: cdc_acm_uart0 {
|
||||||
|
compatible = "zephyr,cdc-acm-uart";
|
||||||
|
|
||||||
|
modbus0 {
|
||||||
|
compatible = "zephyr,modbus-serial";
|
||||||
|
status = "okay";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
&usart1 {
|
||||||
|
/delete-node/ modbus0;
|
||||||
|
};
|
||||||
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
|
||||||
29
software/apps/slave_node/prj.conf
Normal file
29
software/apps/slave_node/prj.conf
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
# Enable Console and printk for logging
|
||||||
|
CONFIG_CONSOLE=y
|
||||||
|
CONFIG_LOG=y
|
||||||
|
|
||||||
|
# Enable Shell
|
||||||
|
CONFIG_SHELL=y
|
||||||
|
CONFIG_REBOOT=y
|
||||||
|
CONFIG_SHELL_MODBUS=y
|
||||||
|
CONFIG_SHELL_VALVE=y
|
||||||
|
CONFIG_SHELL_SYSTEM=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 VND7050AJ
|
||||||
|
CONFIG_VND7050AJ=y
|
||||||
|
|
||||||
28
software/apps/slave_node/src/main.c
Normal file
28
software/apps/slave_node/src/main.c
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#include <zephyr/kernel.h>
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
#include <zephyr/settings/settings.h>
|
||||||
|
#include <lib/fwu.h>
|
||||||
|
#include <lib/modbus_server.h>
|
||||||
|
#include <lib/valve.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;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG_INF("Irrigation System Slave Node started successfully");
|
||||||
|
return 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
|
|
||||||
47
software/include/lib/fwu.h
Normal file
47
software/include/lib/fwu.h
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#ifndef FWU_H
|
||||||
|
#define FWU_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file fwu.h
|
||||||
|
* @brief API for the Firmware Update (FWU) library.
|
||||||
|
*
|
||||||
|
* This library provides the core logic for handling the over-the-air firmware
|
||||||
|
* update process via Modbus. It manages the data buffer, processes commands,
|
||||||
|
* and calculates CRC checksums for data verification.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Initializes the firmware update module.
|
||||||
|
*
|
||||||
|
* This function currently does nothing but is a placeholder for future
|
||||||
|
* initialization logic.
|
||||||
|
*/
|
||||||
|
void fwu_init(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Handles incoming Modbus register writes related to firmware updates.
|
||||||
|
*
|
||||||
|
* This function is the main entry point for the FWU process. It parses the
|
||||||
|
* address and value from a Modbus write operation and takes appropriate action,
|
||||||
|
* such as storing metadata (offset, size) or data chunks, and processing
|
||||||
|
* commands (verify, finalize).
|
||||||
|
*
|
||||||
|
* @param addr The Modbus register address being written to.
|
||||||
|
* @param reg The 16-bit value being written to the register.
|
||||||
|
*/
|
||||||
|
void fwu_handler(uint16_t addr, uint16_t reg);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Gets the CRC16-CCITT of the last received firmware chunk.
|
||||||
|
*
|
||||||
|
* After a data chunk is fully received into the buffer, this function can be
|
||||||
|
* called to retrieve the calculated CRC checksum. The master can then compare
|
||||||
|
* this with its own calculated CRC to verify data integrity.
|
||||||
|
*
|
||||||
|
* @return The 16-bit CRC of the last chunk.
|
||||||
|
*/
|
||||||
|
uint16_t fwu_get_last_chunk_crc(void);
|
||||||
|
|
||||||
|
#endif // FWU_H
|
||||||
184
software/include/lib/modbus_server.h
Normal file
184
software/include/lib/modbus_server.h
Normal file
@@ -0,0 +1,184 @@
|
|||||||
|
#ifndef MODBUS_SERVER_H
|
||||||
|
#define MODBUS_SERVER_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file modbus_server.h
|
||||||
|
* @brief API for the Modbus server implementation.
|
||||||
|
*
|
||||||
|
* This file defines the Modbus register map and provides functions to
|
||||||
|
* initialize and manage the Modbus server.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Modbus Input Register Addresses (Read-Only).
|
||||||
|
* @see docs/modbus-registers.de.md
|
||||||
|
*/
|
||||||
|
enum {
|
||||||
|
/**
|
||||||
|
* @brief Combined status register for the valve.
|
||||||
|
* High-Byte: Movement (0=Idle, 1=Opening, 2=Closing, 3=Error).
|
||||||
|
* Low-Byte: State (0=Closed, 1=Open).
|
||||||
|
*/
|
||||||
|
REG_INPUT_VALVE_STATE_MOVEMENT = 0x0000,
|
||||||
|
/**
|
||||||
|
* @brief Motor current during opening in milliamperes (mA).
|
||||||
|
*/
|
||||||
|
REG_INPUT_MOTOR_OPEN_CURRENT_MA = 0x0001,
|
||||||
|
/**
|
||||||
|
* @brief Motor current during closing in milliamperes (mA).
|
||||||
|
*/
|
||||||
|
REG_INPUT_MOTOR_CLOSE_CURRENT_MA = 0x0002,
|
||||||
|
/**
|
||||||
|
* @brief Bitmask of digital inputs. Bit 0: Input 1, Bit 1: Input 2.
|
||||||
|
* 1=Active.
|
||||||
|
*/
|
||||||
|
REG_INPUT_DIGITAL_INPUTS_STATE = 0x0020,
|
||||||
|
/**
|
||||||
|
* @brief Event flags for buttons (Clear-on-Read). Bit 0: Button 1 pressed.
|
||||||
|
* Bit 1: Button 2 pressed.
|
||||||
|
*/
|
||||||
|
REG_INPUT_BUTTON_EVENTS = 0x0021,
|
||||||
|
/**
|
||||||
|
* @brief Firmware version, e.g., 0x0102 for v1.2.
|
||||||
|
*/
|
||||||
|
REG_INPUT_FIRMWARE_VERSION_MAJOR_MINOR = 0x00F0,
|
||||||
|
/**
|
||||||
|
* @brief Firmware version patch level, e.g., 3 for v1.2.3.
|
||||||
|
*/
|
||||||
|
REG_INPUT_FIRMWARE_VERSION_PATCH = 0x00F1,
|
||||||
|
/**
|
||||||
|
* @brief Device status (0=OK, 1=General Error).
|
||||||
|
*/
|
||||||
|
REG_INPUT_DEVICE_STATUS = 0x00F2,
|
||||||
|
/**
|
||||||
|
* @brief Lower 16 bits of uptime in seconds.
|
||||||
|
*/
|
||||||
|
REG_INPUT_UPTIME_SECONDS_LOW = 0x00F3,
|
||||||
|
/**
|
||||||
|
* @brief Upper 16 bits of uptime in seconds.
|
||||||
|
*/
|
||||||
|
REG_INPUT_UPTIME_SECONDS_HIGH = 0x00F4,
|
||||||
|
/**
|
||||||
|
* @brief Current supply voltage in millivolts (mV).
|
||||||
|
*/
|
||||||
|
REG_INPUT_SUPPLY_VOLTAGE_MV = 0x00F5,
|
||||||
|
/**
|
||||||
|
* @brief CRC16 of the last received data chunk in the buffer for firmware
|
||||||
|
* update.
|
||||||
|
*/
|
||||||
|
REG_INPUT_FWU_LAST_CHUNK_CRC = 0x0100
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Modbus Holding Register Addresses (Read/Write).
|
||||||
|
* @see docs/modbus-registers.de.md
|
||||||
|
*/
|
||||||
|
enum {
|
||||||
|
/**
|
||||||
|
* @brief Valve control command (1=Open, 2=Close, 0=Stop movement).
|
||||||
|
*/
|
||||||
|
REG_HOLDING_VALVE_COMMAND = 0x0000,
|
||||||
|
/**
|
||||||
|
* @brief Safety timeout in seconds for the opening process.
|
||||||
|
*/
|
||||||
|
REG_HOLDING_MAX_OPENING_TIME_S = 0x0001,
|
||||||
|
/**
|
||||||
|
* @brief Safety timeout in seconds for the closing process.
|
||||||
|
*/
|
||||||
|
REG_HOLDING_MAX_CLOSING_TIME_S = 0x0002,
|
||||||
|
/**
|
||||||
|
* @brief Minimum current threshold in mA for end-position detection during opening.
|
||||||
|
*/
|
||||||
|
REG_HOLDING_END_CURRENT_THRESHOLD_OPEN_MA = 0x0003,
|
||||||
|
/**
|
||||||
|
* @brief Minimum current threshold in mA for end-position detection during closing.
|
||||||
|
*/
|
||||||
|
REG_HOLDING_END_CURRENT_THRESHOLD_CLOSE_MA = 0x0004,
|
||||||
|
/**
|
||||||
|
* @brief Obstacle current threshold in mA for opening.
|
||||||
|
*/
|
||||||
|
REG_HOLDING_OBSTACLE_CURRENT_THRESHOLD_OPEN_MA = 0x0005,
|
||||||
|
/**
|
||||||
|
* @brief Obstacle current threshold in mA for closing.
|
||||||
|
*/
|
||||||
|
REG_HOLDING_OBSTACLE_CURRENT_THRESHOLD_CLOSE_MA = 0x0006,
|
||||||
|
/**
|
||||||
|
* @brief Bitmask for reading and writing digital outputs. Bit 0: Output 1,
|
||||||
|
* Bit 1: Output 2. 1=ON, 0=OFF.
|
||||||
|
*/
|
||||||
|
REG_HOLDING_DIGITAL_OUTPUTS_STATE = 0x0010,
|
||||||
|
/**
|
||||||
|
* @brief Fail-safe watchdog timeout in seconds. 0=Disabled.
|
||||||
|
*/
|
||||||
|
REG_HOLDING_WATCHDOG_TIMEOUT_S = 0x00F0,
|
||||||
|
/**
|
||||||
|
* @brief Writing 1 restarts the device.
|
||||||
|
*/
|
||||||
|
REG_HOLDING_DEVICE_RESET = 0x00F1,
|
||||||
|
/**
|
||||||
|
* @brief Command for firmware update.
|
||||||
|
* 1: Verify Chunk - Slave writes the last chunk to flash.
|
||||||
|
* 2: Finalize Update - Complete installation and restart.
|
||||||
|
*/
|
||||||
|
REG_HOLDING_FWU_COMMAND = 0x0100,
|
||||||
|
/**
|
||||||
|
* @brief Lower 16 bits of the 32-bit offset for the next firmware update
|
||||||
|
* chunk.
|
||||||
|
*/
|
||||||
|
REG_HOLDING_FWU_CHUNK_OFFSET_LOW = 0x0101,
|
||||||
|
/**
|
||||||
|
* @brief Upper 16 bits of the 32-bit offset for the next firmware update
|
||||||
|
* chunk.
|
||||||
|
*/
|
||||||
|
REG_HOLDING_FWU_CHUNK_OFFSET_HIGH = 0x0102,
|
||||||
|
/**
|
||||||
|
* @brief Size of the next firmware update chunk in bytes (max. 256).
|
||||||
|
*/
|
||||||
|
REG_HOLDING_FWU_CHUNK_SIZE = 0x0103,
|
||||||
|
/**
|
||||||
|
* @brief Start address of the 256-byte buffer for firmware update data.
|
||||||
|
*/
|
||||||
|
REG_HOLDING_FWU_DATA_BUFFER = 0x0180,
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Initializes the Modbus server.
|
||||||
|
*
|
||||||
|
* This function sets up the Modbus RTU server interface, loads saved settings
|
||||||
|
* (baudrate, unit ID), and starts listening for requests.
|
||||||
|
*
|
||||||
|
* @return 0 on success, or a negative error code on failure.
|
||||||
|
*/
|
||||||
|
int modbus_server_init(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Reconfigures the Modbus server at runtime.
|
||||||
|
*
|
||||||
|
* Updates the baudrate and unit ID of the server. If the reconfiguration
|
||||||
|
* fails, the settings are saved and will be applied after a device reset.
|
||||||
|
*
|
||||||
|
* @param baudrate The new baudrate to set.
|
||||||
|
* @param unit_id The new Modbus unit ID (slave address).
|
||||||
|
* @return 0 on success, or a negative error code if immediate reconfiguration
|
||||||
|
* fails. Returns 0 even on failure if settings could be saved for the next
|
||||||
|
* boot.
|
||||||
|
*/
|
||||||
|
int modbus_reconfigure(uint32_t baudrate, uint8_t unit_id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Gets the current baudrate of the Modbus server.
|
||||||
|
*
|
||||||
|
* @return The current baudrate.
|
||||||
|
*/
|
||||||
|
uint32_t modbus_get_baudrate(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Gets the current unit ID of the Modbus server.
|
||||||
|
*
|
||||||
|
* @return The current unit ID.
|
||||||
|
*/
|
||||||
|
uint8_t modbus_get_unit_id(void);
|
||||||
|
|
||||||
|
#endif // MODBUS_SERVER_H
|
||||||
200
software/include/lib/valve.h
Normal file
200
software/include/lib/valve.h
Normal file
@@ -0,0 +1,200 @@
|
|||||||
|
#ifndef VALVE_H
|
||||||
|
#define VALVE_H
|
||||||
|
|
||||||
|
#include <zephyr/drivers/gpio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file valve.h
|
||||||
|
* @brief API for controlling the motorized valve.
|
||||||
|
*
|
||||||
|
* This library provides functions to initialize, open, close, and stop the
|
||||||
|
* valve. It also allows getting the valve's state and movement status, and
|
||||||
|
* configuring the maximum opening and closing times.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define VALVE_CHANNEL_OPEN 0
|
||||||
|
#define VALVE_CHANNEL_CLOSE 1
|
||||||
|
#define VALVE_ENDPOSITION_CHECK_INTERVAL K_MSEC(100)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Represents the static state of the valve (open or closed).
|
||||||
|
*/
|
||||||
|
enum valve_state {
|
||||||
|
VALVE_STATE_CLOSED, /**< The valve is fully closed. */
|
||||||
|
VALVE_STATE_OPEN, /**< The valve is fully open. */
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Represents the dynamic movement status of the valve.
|
||||||
|
*/
|
||||||
|
enum valve_movement {
|
||||||
|
VALVE_MOVEMENT_IDLE, /**< The valve is not moving. */
|
||||||
|
VALVE_MOVEMENT_OPENING, /**< The valve is currently opening. */
|
||||||
|
VALVE_MOVEMENT_CLOSING, /**< The valve is currently closing. */
|
||||||
|
VALVE_MOVEMENT_ERROR, /**< An error occurred during movement. */
|
||||||
|
VALVE_MOVEMENT_OBSTACLE /**< An obstacle was detected during movement. */
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Initializes the valve control system.
|
||||||
|
*
|
||||||
|
* Configures the GPIOs and loads saved settings for timeouts.
|
||||||
|
* This function must be called before any other valve functions.
|
||||||
|
*
|
||||||
|
* @return 0 on success, or a negative error code on failure.
|
||||||
|
*/
|
||||||
|
int valve_init(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Starts opening the valve.
|
||||||
|
*
|
||||||
|
* The valve will open for the configured maximum opening time.
|
||||||
|
*/
|
||||||
|
void valve_open(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Starts closing the valve.
|
||||||
|
*
|
||||||
|
* The valve will close for the configured maximum closing time.
|
||||||
|
*/
|
||||||
|
void valve_close(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Stops any ongoing valve movement immediately.
|
||||||
|
*/
|
||||||
|
void valve_stop(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Gets the current static state of the valve.
|
||||||
|
*
|
||||||
|
* @return The current valve state (VALVE_STATE_CLOSED or VALVE_STATE_OPEN).
|
||||||
|
*/
|
||||||
|
enum valve_state valve_get_state(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Gets the current movement status of the valve.
|
||||||
|
*
|
||||||
|
* @return The current movement status.
|
||||||
|
*/
|
||||||
|
enum valve_movement valve_get_movement(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Gets the motor current.
|
||||||
|
*
|
||||||
|
* @return The motor current in milliamps (currently simulated).
|
||||||
|
*/
|
||||||
|
uint16_t valve_get_motor_current(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Sets the maximum time for the valve to open.
|
||||||
|
*
|
||||||
|
* @param seconds The timeout in seconds.
|
||||||
|
*/
|
||||||
|
void valve_set_max_open_time(uint16_t seconds);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Sets the maximum time for the valve to close.
|
||||||
|
*
|
||||||
|
* @param seconds The timeout in seconds.
|
||||||
|
*/
|
||||||
|
void valve_set_max_close_time(uint16_t seconds);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Sets the current threshold for end-position detection during opening.
|
||||||
|
*
|
||||||
|
* @param current_ma The current threshold in milliamps.
|
||||||
|
*/
|
||||||
|
void valve_set_end_current_threshold_open(uint16_t current_ma);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Sets the current threshold for end-position detection during closing.
|
||||||
|
*
|
||||||
|
* @param current_ma The current threshold in milliamps.
|
||||||
|
*/
|
||||||
|
void valve_set_end_current_threshold_close(uint16_t current_ma);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Sets the current threshold for obstacle detection during opening.
|
||||||
|
*
|
||||||
|
* @param current_ma The current threshold in milliamps.
|
||||||
|
*/
|
||||||
|
void valve_set_obstacle_current_threshold_open(uint16_t current_ma);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Sets the current threshold for obstacle detection during closing.
|
||||||
|
*
|
||||||
|
* @param current_ma The current threshold in milliamps.
|
||||||
|
*/
|
||||||
|
void valve_set_obstacle_current_threshold_close(uint16_t current_ma);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Gets the current threshold for end-position detection during opening.
|
||||||
|
*
|
||||||
|
* @return The current threshold in milliamps.
|
||||||
|
*/
|
||||||
|
uint16_t valve_get_end_current_threshold_open(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Gets the current threshold for end-position detection during closing.
|
||||||
|
*
|
||||||
|
* @return The current threshold in milliamps.
|
||||||
|
*/
|
||||||
|
uint16_t valve_get_end_current_threshold_close(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Gets the configured maximum opening time.
|
||||||
|
*
|
||||||
|
* @return The timeout in seconds.
|
||||||
|
*/
|
||||||
|
uint16_t valve_get_max_open_time(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Gets the configured maximum closing time.
|
||||||
|
*
|
||||||
|
* @return The timeout in seconds.
|
||||||
|
*/
|
||||||
|
uint16_t valve_get_max_close_time(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Gets the current threshold for obstacle detection during opening.
|
||||||
|
*
|
||||||
|
* @return The current threshold in milliamps.
|
||||||
|
*/
|
||||||
|
uint16_t valve_get_obstacle_current_threshold_open(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Gets the current threshold for obstacle detection during closing.
|
||||||
|
*
|
||||||
|
* @return The current threshold in milliamps.
|
||||||
|
*/
|
||||||
|
uint16_t valve_get_obstacle_current_threshold_close(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Gets the current drawn by the valve motor during opening.
|
||||||
|
*
|
||||||
|
* @return The motor current in milliamps.
|
||||||
|
*/
|
||||||
|
int32_t valve_get_opening_current(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Gets the current drawn by the valve motor during closing.
|
||||||
|
*
|
||||||
|
* @return The motor current in milliamps.
|
||||||
|
*/
|
||||||
|
int32_t valve_get_closing_current(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Gets the temperature of the valve motor driver.
|
||||||
|
*
|
||||||
|
* @return The temperature in degrees Celsius.
|
||||||
|
*/
|
||||||
|
int32_t valve_get_vnd_temp(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Gets the voltage supplied to the valve motor driver.
|
||||||
|
*
|
||||||
|
* @return The voltage in millivolts.
|
||||||
|
*/
|
||||||
|
int32_t valve_get_vnd_voltage(void);
|
||||||
|
#endif // VALVE_H
|
||||||
6
software/lib/CMakeLists.txt
Normal file
6
software/lib/CMakeLists.txt
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
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)
|
||||||
|
add_subdirectory_ifdef(CONFIG_SHELL_VALVE shell_valve)
|
||||||
9
software/lib/Kconfig
Normal file
9
software/lib/Kconfig
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
menu "Irrigation system software libraries"
|
||||||
|
|
||||||
|
rsource "fwu/Kconfig"
|
||||||
|
rsource "modbus_server/Kconfig"
|
||||||
|
rsource "valve/Kconfig"
|
||||||
|
rsource "shell_system/Kconfig"
|
||||||
|
rsource "shell_modbus/Kconfig"
|
||||||
|
rsource "shell_valve/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.
|
||||||
64
software/lib/fwu/fwu.c
Normal file
64
software/lib/fwu/fwu.c
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
/**
|
||||||
|
* @file fwu.c
|
||||||
|
* @brief Implementation of the Firmware Update (FWU) library.
|
||||||
|
*
|
||||||
|
* This file implements the logic for receiving a new firmware image in chunks
|
||||||
|
* over Modbus. It maintains a buffer for the incoming data, calculates the CRC
|
||||||
|
* of the received chunk, and handles commands to verify the chunk and finalize
|
||||||
|
* the update process. The actual writing to flash is simulated.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <zephyr/kernel.h>
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
#include <zephyr/sys/byteorder.h>
|
||||||
|
#include <zephyr/sys/crc.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]; // Buffer to store incoming
|
||||||
|
// firmware data chunks
|
||||||
|
static uint32_t fwu_chunk_offset = 0; // Offset for the current firmware chunk in the overall image
|
||||||
|
static uint16_t fwu_chunk_size = 0; // Size of the current firmware chunk
|
||||||
|
static uint16_t fwu_last_chunk_crc = 0; // CRC16 of the last received firmware chunk
|
||||||
|
|
||||||
|
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)
|
||||||
5
software/lib/modbus_server/Kconfig
Normal file
5
software/lib/modbus_server/Kconfig
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
config LIB_MODBUS_SERVER
|
||||||
|
bool "Enable Modbus Server Library"
|
||||||
|
default y
|
||||||
|
help
|
||||||
|
Enable the Modbus Server Library.
|
||||||
271
software/lib/modbus_server/modbus_server.c
Normal file
271
software/lib/modbus_server/modbus_server.c
Normal file
@@ -0,0 +1,271 @@
|
|||||||
|
/**
|
||||||
|
* @file modbus_server.c
|
||||||
|
* @brief Modbus RTU server implementation for the irrigation system slave node.
|
||||||
|
*
|
||||||
|
* This file implements the Modbus server logic, including register callbacks,
|
||||||
|
* watchdog handling, and dynamic reconfiguration. It interfaces with other
|
||||||
|
* libraries like valve control, ADC sensors, and firmware updates.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <zephyr/device.h>
|
||||||
|
#include <zephyr/drivers/misc/vnd7050aj/vnd7050aj.h>
|
||||||
|
#include <zephyr/drivers/uart.h>
|
||||||
|
#include <zephyr/kernel.h>
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
#include <zephyr/modbus/modbus.h>
|
||||||
|
#include <zephyr/settings/settings.h>
|
||||||
|
#include <zephyr/sys/reboot.h>
|
||||||
|
#include <zephyr/usb/usb_device.h>
|
||||||
|
#include <app_version.h>
|
||||||
|
#include <lib/fwu.h>
|
||||||
|
#include <lib/modbus_server.h>
|
||||||
|
#include <lib/valve.h>
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(modbus_server, LOG_LEVEL_INF);
|
||||||
|
|
||||||
|
static int modbus_iface;
|
||||||
|
static struct modbus_iface_param server_param = {
|
||||||
|
.mode = MODBUS_MODE_RTU,
|
||||||
|
.server = {.user_cb = NULL, .unit_id = 1},
|
||||||
|
.serial = {.baud = 19200, .parity = UART_CFG_PARITY_NONE},
|
||||||
|
};
|
||||||
|
|
||||||
|
static uint16_t watchdog_timeout_s = 0;
|
||||||
|
static struct k_timer watchdog_timer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Timer handler for the Modbus watchdog.
|
||||||
|
*
|
||||||
|
* This function is called when the watchdog timer expires, indicating a loss
|
||||||
|
* of communication with the Modbus master. It triggers a fail-safe action,
|
||||||
|
* which is to close the valve.
|
||||||
|
*
|
||||||
|
* @param timer_id Pointer to the timer instance.
|
||||||
|
*/
|
||||||
|
static void watchdog_timer_handler(struct k_timer *timer_id)
|
||||||
|
{
|
||||||
|
LOG_WRN("Modbus watchdog expired! Closing valve as a fail-safe.");
|
||||||
|
valve_close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Resets the Modbus watchdog timer.
|
||||||
|
*
|
||||||
|
* This function should be called upon receiving any valid Modbus request
|
||||||
|
* to prevent the watchdog from expiring.
|
||||||
|
*/
|
||||||
|
static inline void reset_watchdog(void)
|
||||||
|
{
|
||||||
|
if (watchdog_timeout_s > 0) {
|
||||||
|
k_timer_start(&watchdog_timer, K_SECONDS(watchdog_timeout_s), K_NO_WAIT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Callback for reading Modbus holding registers.
|
||||||
|
*
|
||||||
|
* @param addr Register address.
|
||||||
|
* @param reg Pointer to store the read value.
|
||||||
|
* @return 0 on success.
|
||||||
|
*/
|
||||||
|
static int holding_reg_rd(uint16_t addr, uint16_t *reg)
|
||||||
|
{
|
||||||
|
reset_watchdog();
|
||||||
|
switch (addr) {
|
||||||
|
case REG_HOLDING_MAX_OPENING_TIME_S:
|
||||||
|
*reg = valve_get_max_open_time();
|
||||||
|
break;
|
||||||
|
case REG_HOLDING_MAX_CLOSING_TIME_S:
|
||||||
|
*reg = valve_get_max_close_time();
|
||||||
|
break;
|
||||||
|
case REG_HOLDING_WATCHDOG_TIMEOUT_S:
|
||||||
|
*reg = watchdog_timeout_s;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
*reg = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Callback for writing Modbus holding registers.
|
||||||
|
*
|
||||||
|
* @param addr Register address.
|
||||||
|
* @param reg Value to write.
|
||||||
|
* @return 0 on success.
|
||||||
|
*/
|
||||||
|
static int holding_reg_wr(uint16_t addr, uint16_t reg)
|
||||||
|
{
|
||||||
|
reset_watchdog();
|
||||||
|
switch (addr) {
|
||||||
|
case REG_HOLDING_VALVE_COMMAND:
|
||||||
|
if (reg == 1) {
|
||||||
|
valve_open();
|
||||||
|
} else if (reg == 2) {
|
||||||
|
valve_close();
|
||||||
|
} else if (reg == 0) {
|
||||||
|
valve_stop();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case REG_HOLDING_MAX_OPENING_TIME_S:
|
||||||
|
valve_set_max_open_time(reg);
|
||||||
|
break;
|
||||||
|
case REG_HOLDING_MAX_CLOSING_TIME_S:
|
||||||
|
valve_set_max_close_time(reg);
|
||||||
|
break;
|
||||||
|
case REG_HOLDING_WATCHDOG_TIMEOUT_S:
|
||||||
|
watchdog_timeout_s = reg;
|
||||||
|
if (watchdog_timeout_s > 0) {
|
||||||
|
LOG_INF("Watchdog enabled with %u s timeout.", watchdog_timeout_s);
|
||||||
|
reset_watchdog();
|
||||||
|
} else {
|
||||||
|
LOG_INF("Watchdog disabled.");
|
||||||
|
k_timer_stop(&watchdog_timer);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case REG_HOLDING_DEVICE_RESET:
|
||||||
|
if (reg == 1) {
|
||||||
|
LOG_WRN("Modbus reset command received. Rebooting...");
|
||||||
|
sys_reboot(SYS_REBOOT_WARM);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
fwu_handler(addr, reg);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Callback for reading Modbus input registers.
|
||||||
|
*
|
||||||
|
* @param addr Register address.
|
||||||
|
* @param reg Pointer to store the read value.
|
||||||
|
* @return 0 on success.
|
||||||
|
*/
|
||||||
|
static int input_reg_rd(uint16_t addr, uint16_t *reg)
|
||||||
|
{
|
||||||
|
reset_watchdog();
|
||||||
|
uint32_t uptime_s = k_uptime_get_32() / 1000;
|
||||||
|
switch (addr) {
|
||||||
|
case REG_INPUT_VALVE_STATE_MOVEMENT:
|
||||||
|
*reg = (valve_get_movement() << 8) | (valve_get_state() & 0xFF);
|
||||||
|
break;
|
||||||
|
case REG_INPUT_MOTOR_OPEN_CURRENT_MA:
|
||||||
|
*reg = (uint16_t)valve_get_opening_current();
|
||||||
|
break;
|
||||||
|
case REG_INPUT_MOTOR_CLOSE_CURRENT_MA:
|
||||||
|
*reg = (uint16_t)valve_get_closing_current();
|
||||||
|
break;
|
||||||
|
case REG_INPUT_UPTIME_SECONDS_LOW:
|
||||||
|
*reg = (uint16_t)(uptime_s & 0xFFFF);
|
||||||
|
break;
|
||||||
|
case REG_INPUT_UPTIME_SECONDS_HIGH:
|
||||||
|
*reg = (uint16_t)(uptime_s >> 16);
|
||||||
|
break;
|
||||||
|
case REG_INPUT_SUPPLY_VOLTAGE_MV:
|
||||||
|
*reg = (uint16_t)valve_get_vnd_voltage();
|
||||||
|
break;
|
||||||
|
case REG_INPUT_FWU_LAST_CHUNK_CRC:
|
||||||
|
*reg = fwu_get_last_chunk_crc();
|
||||||
|
break;
|
||||||
|
case REG_INPUT_FIRMWARE_VERSION_MAJOR_MINOR:
|
||||||
|
*reg = (APP_VERSION_MAJOR << 8) | APP_VERSION_MINOR;
|
||||||
|
break;
|
||||||
|
case REG_INPUT_FIRMWARE_VERSION_PATCH:
|
||||||
|
*reg = APP_PATCHLEVEL;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
*reg = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct modbus_user_callbacks mbs_cbs = {
|
||||||
|
// Modbus server callback functions
|
||||||
|
.holding_reg_rd = holding_reg_rd,
|
||||||
|
.holding_reg_wr = holding_reg_wr,
|
||||||
|
.input_reg_rd = input_reg_rd,
|
||||||
|
};
|
||||||
|
|
||||||
|
#define MODBUS_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(zephyr_modbus_serial)
|
||||||
|
|
||||||
|
int modbus_server_init(void)
|
||||||
|
{
|
||||||
|
k_timer_init(&watchdog_timer, watchdog_timer_handler, NULL);
|
||||||
|
|
||||||
|
// Load saved settings
|
||||||
|
uint32_t saved_baudrate = 19200;
|
||||||
|
uint8_t saved_unit_id = 1;
|
||||||
|
settings_load_one("modbus/baudrate", &saved_baudrate, sizeof(saved_baudrate));
|
||||||
|
settings_load_one("modbus/unit_id", &saved_unit_id, sizeof(saved_unit_id));
|
||||||
|
|
||||||
|
// Apply loaded settings
|
||||||
|
server_param.serial.baud = saved_baudrate;
|
||||||
|
server_param.server.unit_id = saved_unit_id;
|
||||||
|
|
||||||
|
const char iface_name[] = {DEVICE_DT_NAME(MODBUS_NODE)};
|
||||||
|
#if DT_NODE_HAS_COMPAT(DT_PARENT(MODBUS_NODE), zephyr_cdc_acm_uart)
|
||||||
|
const struct device *const dev = DEVICE_DT_GET(DT_PARENT(MODBUS_NODE));
|
||||||
|
uint32_t dtr = 0;
|
||||||
|
|
||||||
|
if (!device_is_ready(dev) || usb_enable(NULL)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!dtr) {
|
||||||
|
uart_line_ctrl_get(dev, UART_LINE_CTRL_DTR, &dtr);
|
||||||
|
k_sleep(K_MSEC(100));
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG_INF("Client connected to server on %s", dev->name);
|
||||||
|
#endif
|
||||||
|
modbus_iface = modbus_iface_get_by_name(iface_name);
|
||||||
|
if (modbus_iface < 0) {
|
||||||
|
return modbus_iface;
|
||||||
|
}
|
||||||
|
server_param.server.user_cb = &mbs_cbs;
|
||||||
|
|
||||||
|
LOG_INF("Starting Modbus server: baudrate=%u, unit_id=%u", saved_baudrate, saved_unit_id);
|
||||||
|
return modbus_init_server(modbus_iface, server_param);
|
||||||
|
}
|
||||||
|
|
||||||
|
int modbus_reconfigure(uint32_t baudrate, uint8_t unit_id)
|
||||||
|
{
|
||||||
|
// Update parameters
|
||||||
|
server_param.serial.baud = baudrate;
|
||||||
|
server_param.server.unit_id = unit_id;
|
||||||
|
|
||||||
|
// Try to reinitialize - this should work for most cases
|
||||||
|
int ret = modbus_init_server(modbus_iface, server_param);
|
||||||
|
|
||||||
|
if (ret == 0) {
|
||||||
|
settings_save_one("modbus/baudrate", &baudrate, sizeof(baudrate));
|
||||||
|
settings_save_one("modbus/unit_id", &unit_id, sizeof(unit_id));
|
||||||
|
LOG_INF("Modbus reconfigured: baudrate=%u, unit_id=%u", baudrate, unit_id);
|
||||||
|
} else {
|
||||||
|
LOG_ERR("Failed to reconfigure Modbus: %d", ret);
|
||||||
|
LOG_INF("Modbus reconfiguration requires restart to take effect");
|
||||||
|
|
||||||
|
// Save settings for next boot
|
||||||
|
settings_save_one("modbus/baudrate", &baudrate, sizeof(baudrate));
|
||||||
|
settings_save_one("modbus/unit_id", &unit_id, sizeof(unit_id));
|
||||||
|
|
||||||
|
LOG_INF("Settings saved. Type 'reset' to restart the device and apply the "
|
||||||
|
"change.");
|
||||||
|
return 0; // Return success since settings are saved
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t modbus_get_baudrate(void)
|
||||||
|
{
|
||||||
|
return server_param.serial.baud;
|
||||||
|
}
|
||||||
|
uint8_t modbus_get_unit_id(void)
|
||||||
|
{
|
||||||
|
return server_param.server.unit_id;
|
||||||
|
}
|
||||||
1
software/lib/shell_modbus/CMakeLists.txt
Normal file
1
software/lib/shell_modbus/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
zephyr_library_sources(shell_modbus.c)
|
||||||
7
software/lib/shell_modbus/Kconfig
Normal file
7
software/lib/shell_modbus/Kconfig
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
config SHELL_MODBUS
|
||||||
|
bool "Enable Shell Modbus"
|
||||||
|
default n
|
||||||
|
depends on SHELL
|
||||||
|
depends on LIB_MODBUS_SERVER
|
||||||
|
help
|
||||||
|
Enable the modbus shell commands.
|
||||||
119
software/lib/shell_modbus/shell_modbus.c
Normal file
119
software/lib/shell_modbus/shell_modbus.c
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
/**
|
||||||
|
* @file shell_modbus.c
|
||||||
|
* @brief Provides shell commands for Modbus and valve configuration.
|
||||||
|
*
|
||||||
|
* This file implements a set of commands for the Zephyr shell to allow
|
||||||
|
* runtime configuration of the Modbus server (baudrate, slave ID) and the
|
||||||
|
* valve (max opening/closing times). The settings are persisted to non-volatile
|
||||||
|
* storage.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <zephyr/shell/shell.h>
|
||||||
|
#include <lib/modbus_server.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Shell command to set the Modbus baudrate.
|
||||||
|
*
|
||||||
|
* @param sh The shell instance.
|
||||||
|
* @param argc Argument count.
|
||||||
|
* @param argv Argument values.
|
||||||
|
* @return 0 on success, -EINVAL on error.
|
||||||
|
*/
|
||||||
|
static int cmd_modbus_setb(const struct shell *sh, size_t argc, char **argv)
|
||||||
|
{
|
||||||
|
if (argc != 2) {
|
||||||
|
shell_error(sh, "Usage: setb <baudrate>");
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t new_baud = (uint32_t)strtoul(argv[1], NULL, 10);
|
||||||
|
const uint32_t valid_baud_rates[] = {1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
|
||||||
|
bool is_valid = false;
|
||||||
|
|
||||||
|
for (int i = 0; i < ARRAY_SIZE(valid_baud_rates); i++) {
|
||||||
|
if (new_baud == valid_baud_rates[i]) {
|
||||||
|
is_valid = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_valid) {
|
||||||
|
char error_msg[128];
|
||||||
|
int offset =
|
||||||
|
snprintf(error_msg, sizeof(error_msg), "Invalid baudrate. Valid rates are: ");
|
||||||
|
for (int i = 0; i < ARRAY_SIZE(valid_baud_rates); i++) {
|
||||||
|
offset += snprintf(error_msg + offset,
|
||||||
|
sizeof(error_msg) - offset,
|
||||||
|
"%u ",
|
||||||
|
valid_baud_rates[i]);
|
||||||
|
}
|
||||||
|
shell_error(sh, "%s", error_msg);
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (modbus_reconfigure(new_baud, modbus_get_unit_id()) != 0) {
|
||||||
|
shell_error(sh, "Failed to apply new baudrate");
|
||||||
|
} else {
|
||||||
|
shell_print(sh, "Modbus baudrate set to: %u (and saved)", new_baud);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Shell command to set the Modbus slave ID.
|
||||||
|
*
|
||||||
|
* @param sh The shell instance.
|
||||||
|
* @param argc Argument count.
|
||||||
|
* @param argv Argument values.
|
||||||
|
* @return 0 on success, -EINVAL on error.
|
||||||
|
*/
|
||||||
|
static int cmd_modbus_setid(const struct shell *sh, size_t argc, char **argv)
|
||||||
|
{
|
||||||
|
if (argc != 2) {
|
||||||
|
shell_error(sh, "Usage: setid <slave_id>");
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t new_id_u32 = (uint32_t)strtoul(argv[1], NULL, 10);
|
||||||
|
if (new_id_u32 == 0 || new_id_u32 > 247) {
|
||||||
|
shell_error(sh, "Invalid slave ID: %s. Must be between 1 and 247.", argv[1]);
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
uint8_t new_id = (uint8_t)new_id_u32;
|
||||||
|
|
||||||
|
if (modbus_reconfigure(modbus_get_baudrate(), new_id) != 0) {
|
||||||
|
shell_error(sh, "Failed to apply new slave ID");
|
||||||
|
} else {
|
||||||
|
shell_print(sh, "Modbus slave ID set to: %u (and saved)", new_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Shell command to show the current Modbus configuration.
|
||||||
|
*
|
||||||
|
* @param sh The shell instance.
|
||||||
|
* @param argc Argument count.
|
||||||
|
* @param argv Argument values.
|
||||||
|
* @return 0 on success.
|
||||||
|
*/
|
||||||
|
static int cmd_modbus_show(const struct shell *sh, size_t argc, char **argv)
|
||||||
|
{
|
||||||
|
const int label_width = 15;
|
||||||
|
|
||||||
|
shell_print(sh, "Modbus Settings:");
|
||||||
|
shell_print(sh, "%*s %u", label_width, "Baudrate:", modbus_get_baudrate());
|
||||||
|
shell_print(sh, "%*s %u", label_width, "Slave ID:", modbus_get_unit_id());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
SHELL_STATIC_SUBCMD_SET_CREATE(sub_modbus_cmds,
|
||||||
|
SHELL_CMD(setb, NULL, "Set Modbus baudrate", cmd_modbus_setb),
|
||||||
|
SHELL_CMD(setid, NULL, "Set Modbus slave ID", cmd_modbus_setid),
|
||||||
|
SHELL_CMD(show, NULL, "Show Modbus configuration", cmd_modbus_show),
|
||||||
|
SHELL_SUBCMD_SET_END);
|
||||||
|
|
||||||
|
SHELL_CMD_REGISTER(modbus, &sub_modbus_cmds, "Modbus commands", NULL);
|
||||||
1
software/lib/shell_system/CMakeLists.txt
Normal file
1
software/lib/shell_system/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
zephyr_library_sources(shell_system.c)
|
||||||
5
software/lib/shell_system/Kconfig
Normal file
5
software/lib/shell_system/Kconfig
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
config SHELL_SYSTEM
|
||||||
|
bool "Enable Shell System"
|
||||||
|
default n
|
||||||
|
help
|
||||||
|
Enable the system commands.
|
||||||
31
software/lib/shell_system/shell_system.c
Normal file
31
software/lib/shell_system/shell_system.c
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
/**
|
||||||
|
* @file shell_system.c
|
||||||
|
* @brief Provides basic system-level shell commands.
|
||||||
|
*
|
||||||
|
* This file implements essential system commands for the Zephyr shell,
|
||||||
|
* such as rebooting the device.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <zephyr/shell/shell.h>
|
||||||
|
#include <zephyr/sys/reboot.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Shell command to reset the system.
|
||||||
|
*
|
||||||
|
* This command performs a warm reboot of the device after a short delay
|
||||||
|
* to ensure the shell message is printed.
|
||||||
|
*
|
||||||
|
* @param sh The shell instance.
|
||||||
|
* @param argc Argument count.
|
||||||
|
* @param argv Argument values.
|
||||||
|
* @return 0 on success.
|
||||||
|
*/
|
||||||
|
static int cmd_reset(const struct shell *sh, size_t argc, char **argv)
|
||||||
|
{
|
||||||
|
shell_print(sh, "Rebooting system...");
|
||||||
|
k_sleep(K_MSEC(100)); // Allow the shell to print the message
|
||||||
|
sys_reboot(SYS_REBOOT_WARM);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
SHELL_CMD_REGISTER(reset, NULL, "Reboot the system", cmd_reset);
|
||||||
1
software/lib/shell_valve/CMakeLists.txt
Normal file
1
software/lib/shell_valve/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
zephyr_library_sources(shell_valve.c)
|
||||||
7
software/lib/shell_valve/Kconfig
Normal file
7
software/lib/shell_valve/Kconfig
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
config SHELL_VALVE
|
||||||
|
bool "Shell Valve commands"
|
||||||
|
default n
|
||||||
|
depends on SHELL
|
||||||
|
depends on LIB_VALVE
|
||||||
|
help
|
||||||
|
Enable the valve shell commands.
|
||||||
136
software/lib/shell_valve/shell_valve.c
Normal file
136
software/lib/shell_valve/shell_valve.c
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
#include <zephyr/kernel.h>
|
||||||
|
#include <zephyr/shell/shell.h>
|
||||||
|
#include <lib/valve.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static int cmd_valve_set_open_t(const struct shell *sh, size_t argc, char **argv)
|
||||||
|
{
|
||||||
|
if (argc != 2) {
|
||||||
|
shell_print(sh, "Usage: valve set_open_t <seconds>");
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t seconds = (uint16_t)atoi(argv[1]);
|
||||||
|
valve_set_max_open_time(seconds);
|
||||||
|
shell_print(sh, "Max open time set to %u seconds.", seconds);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cmd_valve_set_close_t(const struct shell *sh, size_t argc, char **argv)
|
||||||
|
{
|
||||||
|
if (argc != 2) {
|
||||||
|
shell_print(sh, "Usage: valve set_close_t <seconds>");
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t seconds = (uint16_t)atoi(argv[1]);
|
||||||
|
valve_set_max_close_time(seconds);
|
||||||
|
shell_print(sh, "Max close time set to %u seconds.", seconds);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cmd_valve_set_end_curr_open(const struct shell *sh, size_t argc, char **argv)
|
||||||
|
{
|
||||||
|
if (argc != 2) {
|
||||||
|
shell_print(sh, "Usage: valve set_end_curr_open <milliamps>");
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t current_ma = (uint16_t)atoi(argv[1]);
|
||||||
|
valve_set_end_current_threshold_open(current_ma);
|
||||||
|
shell_print(sh, "End current threshold (open) set to %u mA.", current_ma);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cmd_valve_set_end_curr_close(const struct shell *sh, size_t argc, char **argv)
|
||||||
|
{
|
||||||
|
if (argc != 2) {
|
||||||
|
shell_print(sh, "Usage: valve set_end_curr_close <milliamps>");
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t current_ma = (uint16_t)atoi(argv[1]);
|
||||||
|
valve_set_end_current_threshold_close(current_ma);
|
||||||
|
shell_print(sh, "End current threshold (close) set to %u mA.", current_ma);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cmd_valve_set_obs_curr_open(const struct shell *sh, size_t argc, char **argv)
|
||||||
|
{
|
||||||
|
if (argc != 2) {
|
||||||
|
shell_print(sh, "Usage: valve set_obs_curr_open <milliamps>");
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t current_ma = (uint16_t)atoi(argv[1]);
|
||||||
|
valve_set_obstacle_current_threshold_open(current_ma);
|
||||||
|
shell_print(sh, "Obstacle current threshold (open) set to %u mA.", current_ma);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cmd_valve_set_obs_curr_close(const struct shell *sh, size_t argc, char **argv)
|
||||||
|
{
|
||||||
|
if (argc != 2) {
|
||||||
|
shell_print(sh, "Usage: valve set_obs_curr_close <milliamps>");
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t current_ma = (uint16_t)atoi(argv[1]);
|
||||||
|
valve_set_obstacle_current_threshold_close(current_ma);
|
||||||
|
shell_print(sh, "Obstacle current threshold (close) set to %u mA.", current_ma);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cmd_valve_show(const struct shell *sh, size_t argc, char **argv)
|
||||||
|
{
|
||||||
|
const int label_width = 30;
|
||||||
|
|
||||||
|
shell_print(sh, "Valve Settings:");
|
||||||
|
shell_print(sh, "%*s %u s", label_width, "Max Open Time:", valve_get_max_open_time());
|
||||||
|
shell_print(sh, "%*s %u s", label_width, "Max Close Time:", valve_get_max_close_time());
|
||||||
|
shell_print(sh,
|
||||||
|
"%*s %u mA",
|
||||||
|
label_width,
|
||||||
|
"End Current Threshold (Open):",
|
||||||
|
valve_get_end_current_threshold_open());
|
||||||
|
shell_print(sh,
|
||||||
|
"%*s %u mA",
|
||||||
|
label_width,
|
||||||
|
"End Current Threshold (Close):",
|
||||||
|
valve_get_end_current_threshold_close());
|
||||||
|
shell_print(sh,
|
||||||
|
"%*s %u mA",
|
||||||
|
label_width,
|
||||||
|
"Obstacle Current Threshold (Open):",
|
||||||
|
valve_get_obstacle_current_threshold_open());
|
||||||
|
shell_print(sh,
|
||||||
|
"%*s %u mA",
|
||||||
|
label_width,
|
||||||
|
"Obstacle Current Threshold (Close):",
|
||||||
|
valve_get_obstacle_current_threshold_close());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
SHELL_STATIC_SUBCMD_SET_CREATE(sub_valve_settings,
|
||||||
|
SHELL_CMD(set_open_t, NULL, "Set max open time (seconds)", cmd_valve_set_open_t),
|
||||||
|
SHELL_CMD(set_close_t, NULL, "Set max close time (seconds)", cmd_valve_set_close_t),
|
||||||
|
SHELL_CMD(set_end_curr_open,
|
||||||
|
NULL,
|
||||||
|
"Set end current threshold for opening (mA)",
|
||||||
|
cmd_valve_set_end_curr_open),
|
||||||
|
SHELL_CMD(set_end_curr_close,
|
||||||
|
NULL,
|
||||||
|
"Set end current threshold for closing (mA)",
|
||||||
|
cmd_valve_set_end_curr_close),
|
||||||
|
SHELL_CMD(set_obs_curr_open,
|
||||||
|
NULL,
|
||||||
|
"Set obstacle current threshold for opening (mA)",
|
||||||
|
cmd_valve_set_obs_curr_open),
|
||||||
|
SHELL_CMD(set_obs_curr_close,
|
||||||
|
NULL,
|
||||||
|
"Set obstacle current threshold for closing (mA)",
|
||||||
|
cmd_valve_set_obs_curr_close),
|
||||||
|
SHELL_CMD(show, NULL, "Show valve configuration", cmd_valve_show),
|
||||||
|
SHELL_SUBCMD_SET_END);
|
||||||
|
|
||||||
|
SHELL_CMD_REGISTER(valve, &sub_valve_settings, "Valve commands", NULL);
|
||||||
@@ -1,163 +0,0 @@
|
|||||||
#include <zephyr/kernel.h>
|
|
||||||
#include <zephyr/logging/log.h>
|
|
||||||
|
|
||||||
#include "canbus.h"
|
|
||||||
#include "canbus_registers.h"
|
|
||||||
#include "valve.h"
|
|
||||||
|
|
||||||
LOG_MODULE_REGISTER(valve, CONFIG_LOG_VALVE_LEVEL);
|
|
||||||
|
|
||||||
K_THREAD_STACK_DEFINE(valve_thread_stack, VALVE_THREAD_STACK_SIZE);
|
|
||||||
K_MSGQ_DEFINE(valve_msgq, sizeof(int), VALVE_MSGQ_SIZE, 4);
|
|
||||||
|
|
||||||
k_tid_t valve_thread_id;
|
|
||||||
struct k_thread valve_thread_data;
|
|
||||||
|
|
||||||
valve_status_t valve_status_data = {
|
|
||||||
.valve_state = VALVE_STATE_UNKNOWN,
|
|
||||||
.valve_operation = VALVE_OPERATION_IDLE,
|
|
||||||
};
|
|
||||||
|
|
||||||
int valve_start_thread(void)
|
|
||||||
{
|
|
||||||
int rc;
|
|
||||||
|
|
||||||
// Initialize the valve
|
|
||||||
rc = valve_init();
|
|
||||||
if (rc < 0)
|
|
||||||
{
|
|
||||||
LOG_ERR("Failed to initialize valve: %d", rc);
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the valve thread
|
|
||||||
valve_thread_id = k_thread_create(&valve_thread_data, valve_thread_stack,
|
|
||||||
K_THREAD_STACK_SIZEOF(valve_thread_stack),
|
|
||||||
(k_thread_entry_t)valve_cmd, NULL, NULL, NULL,
|
|
||||||
VALVE_THREAD_PRIORITY, 0, K_NO_WAIT);
|
|
||||||
k_thread_name_set(valve_thread_id, "valve");
|
|
||||||
|
|
||||||
LOG_INF("Valve thread started successfully");
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
// Wait for commands from the message queue
|
|
||||||
int cmd;
|
|
||||||
rc = k_msgq_get(&valve_msgq, &cmd, VALVE_STATE_INTERVAL);
|
|
||||||
if (rc == 0)
|
|
||||||
{
|
|
||||||
// Process the command
|
|
||||||
rc = valve_cmd(cmd);
|
|
||||||
if (rc < 0)
|
|
||||||
{
|
|
||||||
LOG_ERR("Failed to process valve command: %d", rc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
valve_send_status(); // Send current valve status periodically
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
int valve_init(void)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int valve_cmd(int cmd)
|
|
||||||
{
|
|
||||||
switch (cmd)
|
|
||||||
{
|
|
||||||
case VALVE_COMMAND_OPEN:
|
|
||||||
if (valve_status_data.valve_state != VALVE_STATE_OPEN)
|
|
||||||
{
|
|
||||||
valve_status_data.valve_state = VALVE_STATE_OPEN;
|
|
||||||
valve_status_data.valve_operation = VALVE_OPERATION_OPENING;
|
|
||||||
valve_send_status(); // Send updated status before opening
|
|
||||||
valve_send_operation(); // Send updated operation state before opening
|
|
||||||
k_sleep(VALVE_OPENING_TIME); // Simulate opening time
|
|
||||||
valve_status_data.valve_operation = VALVE_OPERATION_IDLE; // Set operation to idle after opening
|
|
||||||
valve_send_status(); // Send updated status after opening
|
|
||||||
valve_send_operation(); // Send updated operation state after opening
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case VALVE_COMMAND_CLOSE:
|
|
||||||
if (valve_status_data.valve_state != VALVE_STATE_CLOSED)
|
|
||||||
{
|
|
||||||
valve_status_data.valve_operation = VALVE_OPERATION_CLOSING;
|
|
||||||
valve_send_operation(); // Send updated operation state before closing
|
|
||||||
k_sleep(VALVE_CLOSING_TIME); // Simulate closing time
|
|
||||||
valve_status_data.valve_state = VALVE_STATE_CLOSED; // Set valve state to closed after closing
|
|
||||||
valve_status_data.valve_operation = VALVE_OPERATION_IDLE; // Set operation to idle after closing
|
|
||||||
valve_send_status(); // Send updated status after closing
|
|
||||||
valve_send_operation(); // Send updated operation state after closing
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case VALVE_COMMAND_STOP:
|
|
||||||
valve_status_data.valve_operation = VALVE_OPERATION_IDLE;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
LOG_ERR("Unknown valve command: %d", cmd);
|
|
||||||
return -EINVAL; // Invalid command
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int valve_send_status(void)
|
|
||||||
{
|
|
||||||
int rc = canbus_send8(CANBUS_REG_VALVE_STATUS, valve_status_data.valve_state);
|
|
||||||
if (rc != 0)
|
|
||||||
{
|
|
||||||
LOG_ERR("Failed to send valve status: %d", rc);
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
char *state_str;
|
|
||||||
switch (valve_status_data.valve_state)
|
|
||||||
{
|
|
||||||
case VALVE_STATE_CLOSED:
|
|
||||||
state_str = "CLOSED";
|
|
||||||
break;
|
|
||||||
case VALVE_STATE_OPEN:
|
|
||||||
state_str = "OPEN";
|
|
||||||
break;
|
|
||||||
case VALVE_STATE_ERROR:
|
|
||||||
state_str = "ERROR";
|
|
||||||
break;
|
|
||||||
case VALVE_STATE_UNKNOWN:
|
|
||||||
state_str = "UNKNOWN";
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
state_str = "INVALID";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
LOG_INF("Valve status sent: %s", state_str);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int valve_send_operation(void)
|
|
||||||
{
|
|
||||||
int rc = canbus_send8(CANBUS_REG_VALVE_OPERATION, valve_status_data.valve_operation);
|
|
||||||
if (rc != 0)
|
|
||||||
{
|
|
||||||
LOG_ERR("Failed to send valve operation: %d", rc);
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
char *operation_str;
|
|
||||||
switch (valve_status_data.valve_operation)
|
|
||||||
{
|
|
||||||
case VALVE_OPERATION_IDLE:
|
|
||||||
operation_str = "IDLE";
|
|
||||||
break;
|
|
||||||
case VALVE_OPERATION_OPENING:
|
|
||||||
operation_str = "OPENING";
|
|
||||||
break;
|
|
||||||
case VALVE_OPERATION_CLOSING:
|
|
||||||
operation_str = "CLOSING";
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
operation_str = "UNKNOWN";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
LOG_INF("Valve operation sent: %s", operation_str);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
#ifndef __VALVE_H__
|
|
||||||
#define __VALVE_H__
|
|
||||||
|
|
||||||
#define VALVE_OPENING_TIME K_MSEC(4500) // Time to open the valve
|
|
||||||
#define VALVE_CLOSING_TIME K_MSEC(4500) // Time to close the valve
|
|
||||||
#define VALVE_MAX_OPENING_TIME K_MSEC(5000) // Maximum time to open the valve
|
|
||||||
#define VALVE_MAX_CLOSING_TIME K_MSEC(5000) // Maximum time to close the valve
|
|
||||||
#define VALVE_STATE_INTERVAL K_SECONDS(5 * 60) // Interval to check the valve state
|
|
||||||
#define VALVE_THREAD_STACK_SIZE (512) // Stack size for the valve thread
|
|
||||||
#define VALVE_THREAD_PRIORITY (2) // Priority for the valve thread
|
|
||||||
#define VALVE_MSGQ_SIZE (5) // Size of the message queue for valve operations
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include "canbus_registers.h"
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint8_t valve_state;
|
|
||||||
uint8_t valve_operation;
|
|
||||||
} valve_status_t;
|
|
||||||
|
|
||||||
int valve_init(void);
|
|
||||||
int valve_cmd(int cmd);
|
|
||||||
int valve_send_status(void);
|
|
||||||
int valve_send_operation(void);
|
|
||||||
|
|
||||||
|
|
||||||
#endif // __VALVE_H__
|
|
||||||
1
software/lib/valve/CMakeLists.txt
Normal file
1
software/lib/valve/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
zephyr_library_sources(valve.c)
|
||||||
5
software/lib/valve/Kconfig
Normal file
5
software/lib/valve/Kconfig
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
config LIB_VALVE
|
||||||
|
bool "Enable Valve Library"
|
||||||
|
default y
|
||||||
|
help
|
||||||
|
Enable the Valve Library.
|
||||||
285
software/lib/valve/valve.c
Normal file
285
software/lib/valve/valve.c
Normal file
@@ -0,0 +1,285 @@
|
|||||||
|
/**
|
||||||
|
* @file valve.c
|
||||||
|
* @brief Implementation of the motorized valve control library.
|
||||||
|
*
|
||||||
|
* This file contains the logic for controlling a motorized valve using a
|
||||||
|
* VND7050AJ high-side driver. It uses a delayed work item to handle the
|
||||||
|
* safety timeouts for opening and closing operations.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <zephyr/device.h>
|
||||||
|
#include <zephyr/drivers/gpio.h>
|
||||||
|
#include <zephyr/drivers/misc/vnd7050aj/vnd7050aj.h>
|
||||||
|
#include <zephyr/kernel.h>
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
#include <zephyr/settings/settings.h>
|
||||||
|
#include <lib/valve.h>
|
||||||
|
|
||||||
|
#define VND_NODE DT_ALIAS(vnd7050aj)
|
||||||
|
#if !DT_NODE_HAS_STATUS(VND_NODE, okay)
|
||||||
|
#error VND7050AJ node is not defined or enabled
|
||||||
|
#endif
|
||||||
|
const struct device *vnd7050aj_dev = DEVICE_DT_GET(VND_NODE);
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(valve, LOG_LEVEL_INF);
|
||||||
|
|
||||||
|
static enum valve_state current_state = VALVE_STATE_OPEN;
|
||||||
|
static enum valve_movement current_movement = VALVE_MOVEMENT_IDLE;
|
||||||
|
static uint16_t max_opening_time_s = 10;
|
||||||
|
static uint16_t max_closing_time_s = 10;
|
||||||
|
static uint16_t end_current_threshold_open_ma = 10; // Default value for open
|
||||||
|
static uint16_t end_current_threshold_close_ma = 10; // Default value for close
|
||||||
|
static uint16_t obstacle_current_threshold_open_ma = 600; // Default value for open
|
||||||
|
static uint16_t obstacle_current_threshold_close_ma = 600; // Default value for close
|
||||||
|
static struct k_work_delayable valve_work; // Work item for scheduling valve movement timeouts
|
||||||
|
static struct k_timer movement_timer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Work handler for end position checks of the valve.
|
||||||
|
*
|
||||||
|
* This function is called periodically to check if the valve has reached its
|
||||||
|
* end position. It reads the current load on the motor and determines if the
|
||||||
|
* valve has reached its target position.
|
||||||
|
*
|
||||||
|
* @param work Pointer to the k_work item.
|
||||||
|
*/
|
||||||
|
static void valve_work_handler(struct k_work *work)
|
||||||
|
{
|
||||||
|
int current_ma = 0;
|
||||||
|
|
||||||
|
if (current_movement == VALVE_MOVEMENT_OPENING) {
|
||||||
|
vnd7050aj_read_load_current(vnd7050aj_dev, VALVE_CHANNEL_OPEN, ¤t_ma);
|
||||||
|
if (current_ma > obstacle_current_threshold_open_ma) {
|
||||||
|
LOG_ERR("Obstacle detected during opening! Current: %d mA", current_ma);
|
||||||
|
current_movement = VALVE_MOVEMENT_OBSTACLE;
|
||||||
|
goto work_handler_finish;
|
||||||
|
}
|
||||||
|
if (current_ma > end_current_threshold_open_ma) {
|
||||||
|
k_work_schedule(&valve_work, VALVE_ENDPOSITION_CHECK_INTERVAL);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
LOG_INF("Valve finished opening");
|
||||||
|
} else if (current_movement == VALVE_MOVEMENT_CLOSING) {
|
||||||
|
vnd7050aj_read_load_current(vnd7050aj_dev, VALVE_CHANNEL_CLOSE, ¤t_ma);
|
||||||
|
if (current_ma > obstacle_current_threshold_close_ma) {
|
||||||
|
LOG_ERR("Obstacle detected during closing! Current: %d mA", current_ma);
|
||||||
|
current_movement = VALVE_MOVEMENT_OBSTACLE;
|
||||||
|
goto work_handler_finish;
|
||||||
|
}
|
||||||
|
if (current_ma > end_current_threshold_close_ma) {
|
||||||
|
k_work_schedule(&valve_work, VALVE_ENDPOSITION_CHECK_INTERVAL);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
current_state = VALVE_STATE_CLOSED;
|
||||||
|
LOG_INF("Valve finished closing");
|
||||||
|
}
|
||||||
|
current_movement = VALVE_MOVEMENT_IDLE;
|
||||||
|
|
||||||
|
work_handler_finish:
|
||||||
|
// Reset the movement timer
|
||||||
|
k_timer_stop(&movement_timer);
|
||||||
|
|
||||||
|
vnd7050aj_set_output_state(vnd7050aj_dev, VALVE_CHANNEL_OPEN, false);
|
||||||
|
vnd7050aj_set_output_state(vnd7050aj_dev, VALVE_CHANNEL_CLOSE, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void movement_timeout_handler(struct k_timer *timer)
|
||||||
|
{
|
||||||
|
// Stop the end position check if the timer expires
|
||||||
|
k_work_cancel_delayable(&valve_work);
|
||||||
|
if (current_movement == VALVE_MOVEMENT_OPENING) {
|
||||||
|
LOG_WRN("Valve opening timeout reached, stopping motor.");
|
||||||
|
current_movement = VALVE_MOVEMENT_ERROR;
|
||||||
|
} else if (current_movement == VALVE_MOVEMENT_CLOSING) {
|
||||||
|
LOG_WRN("Valve closing timeout reached, stopping motor.");
|
||||||
|
current_movement = VALVE_MOVEMENT_ERROR;
|
||||||
|
}
|
||||||
|
vnd7050aj_set_output_state(vnd7050aj_dev, VALVE_CHANNEL_OPEN, false);
|
||||||
|
vnd7050aj_set_output_state(vnd7050aj_dev, VALVE_CHANNEL_CLOSE, false);
|
||||||
|
current_state = VALVE_STATE_CLOSED;
|
||||||
|
}
|
||||||
|
|
||||||
|
int valve_init(void)
|
||||||
|
{
|
||||||
|
if (!device_is_ready(vnd7050aj_dev)) {
|
||||||
|
LOG_ERR("VND7050AJ device is not ready");
|
||||||
|
return -ENODEV;
|
||||||
|
}
|
||||||
|
|
||||||
|
k_work_init_delayable(&valve_work, valve_work_handler);
|
||||||
|
k_timer_init(&movement_timer, movement_timeout_handler, NULL);
|
||||||
|
|
||||||
|
settings_load_one("valve/max_open_time", &max_opening_time_s, sizeof(max_opening_time_s));
|
||||||
|
settings_load_one("valve/max_close_time", &max_closing_time_s, sizeof(max_closing_time_s));
|
||||||
|
settings_load_one("valve/end_current_threshold_open",
|
||||||
|
&end_current_threshold_open_ma,
|
||||||
|
sizeof(end_current_threshold_open_ma));
|
||||||
|
settings_load_one("valve/end_current_threshold_close",
|
||||||
|
&end_current_threshold_close_ma,
|
||||||
|
sizeof(end_current_threshold_close_ma));
|
||||||
|
settings_load_one("valve/obstacle_current_threshold_open",
|
||||||
|
&obstacle_current_threshold_open_ma,
|
||||||
|
sizeof(obstacle_current_threshold_open_ma));
|
||||||
|
settings_load_one("valve/obstacle_current_threshold_close",
|
||||||
|
&obstacle_current_threshold_close_ma,
|
||||||
|
sizeof(obstacle_current_threshold_close_ma));
|
||||||
|
|
||||||
|
LOG_INF("Valve initialized: max_open=%us, max_close=%us, end_curr_open=%umA, "
|
||||||
|
"end_curr_close=%umA, obs_curr_open=%umA, obs_curr_close=%umA",
|
||||||
|
max_opening_time_s,
|
||||||
|
max_closing_time_s,
|
||||||
|
end_current_threshold_open_ma,
|
||||||
|
end_current_threshold_close_ma,
|
||||||
|
obstacle_current_threshold_open_ma,
|
||||||
|
obstacle_current_threshold_close_ma);
|
||||||
|
valve_close();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void valve_open(void)
|
||||||
|
{
|
||||||
|
vnd7050aj_reset_fault(vnd7050aj_dev);
|
||||||
|
vnd7050aj_set_output_state(vnd7050aj_dev, VALVE_CHANNEL_CLOSE, false);
|
||||||
|
vnd7050aj_set_output_state(vnd7050aj_dev, VALVE_CHANNEL_OPEN, true);
|
||||||
|
current_state = VALVE_STATE_OPEN;
|
||||||
|
current_movement = VALVE_MOVEMENT_OPENING; /* Security: assume valve open as
|
||||||
|
soons as it starts opening */
|
||||||
|
if (max_opening_time_s > 0) {
|
||||||
|
k_timer_start(&movement_timer, K_SECONDS(max_opening_time_s), K_NO_WAIT);
|
||||||
|
}
|
||||||
|
k_work_schedule(&valve_work, K_MSEC(100));
|
||||||
|
}
|
||||||
|
|
||||||
|
void valve_close(void)
|
||||||
|
{
|
||||||
|
vnd7050aj_reset_fault(vnd7050aj_dev);
|
||||||
|
vnd7050aj_set_output_state(vnd7050aj_dev, VALVE_CHANNEL_OPEN, false);
|
||||||
|
vnd7050aj_set_output_state(vnd7050aj_dev, VALVE_CHANNEL_CLOSE, true);
|
||||||
|
if (max_closing_time_s > 0) {
|
||||||
|
k_timer_start(&movement_timer, K_SECONDS(max_closing_time_s), K_NO_WAIT);
|
||||||
|
}
|
||||||
|
current_movement = VALVE_MOVEMENT_CLOSING;
|
||||||
|
k_work_schedule(&valve_work, VALVE_ENDPOSITION_CHECK_INTERVAL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void valve_stop(void)
|
||||||
|
{
|
||||||
|
k_work_cancel_delayable(&valve_work);
|
||||||
|
k_timer_stop(&movement_timer);
|
||||||
|
vnd7050aj_set_output_state(vnd7050aj_dev, VALVE_CHANNEL_OPEN, false);
|
||||||
|
vnd7050aj_set_output_state(vnd7050aj_dev, VALVE_CHANNEL_CLOSE, false);
|
||||||
|
current_movement = VALVE_MOVEMENT_IDLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum valve_state valve_get_state(void)
|
||||||
|
{
|
||||||
|
return current_state;
|
||||||
|
}
|
||||||
|
enum valve_movement valve_get_movement(void)
|
||||||
|
{
|
||||||
|
return current_movement;
|
||||||
|
}
|
||||||
|
uint16_t valve_get_motor_current(void)
|
||||||
|
{
|
||||||
|
return (current_movement != VALVE_MOVEMENT_IDLE) ? 150 : 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
void valve_set_max_open_time(uint16_t seconds)
|
||||||
|
{
|
||||||
|
max_opening_time_s = seconds;
|
||||||
|
settings_save_one("valve/max_open_time", &max_opening_time_s, sizeof(max_opening_time_s));
|
||||||
|
}
|
||||||
|
void valve_set_max_close_time(uint16_t seconds)
|
||||||
|
{
|
||||||
|
max_closing_time_s = seconds;
|
||||||
|
settings_save_one("valve/max_close_time", &max_closing_time_s, sizeof(max_closing_time_s));
|
||||||
|
}
|
||||||
|
|
||||||
|
void valve_set_end_current_threshold_open(uint16_t current_ma)
|
||||||
|
{
|
||||||
|
end_current_threshold_open_ma = current_ma;
|
||||||
|
settings_save_one("valve/end_current_threshold_open",
|
||||||
|
&end_current_threshold_open_ma,
|
||||||
|
sizeof(end_current_threshold_open_ma));
|
||||||
|
}
|
||||||
|
|
||||||
|
void valve_set_end_current_threshold_close(uint16_t current_ma)
|
||||||
|
{
|
||||||
|
end_current_threshold_close_ma = current_ma;
|
||||||
|
settings_save_one("valve/end_current_threshold_close",
|
||||||
|
&end_current_threshold_close_ma,
|
||||||
|
sizeof(end_current_threshold_close_ma));
|
||||||
|
}
|
||||||
|
|
||||||
|
void valve_set_obstacle_current_threshold_open(uint16_t current_ma)
|
||||||
|
{
|
||||||
|
obstacle_current_threshold_open_ma = current_ma;
|
||||||
|
settings_save_one("valve/obstacle_current_threshold_open",
|
||||||
|
&obstacle_current_threshold_open_ma,
|
||||||
|
sizeof(obstacle_current_threshold_open_ma));
|
||||||
|
}
|
||||||
|
|
||||||
|
void valve_set_obstacle_current_threshold_close(uint16_t current_ma)
|
||||||
|
{
|
||||||
|
obstacle_current_threshold_close_ma = current_ma;
|
||||||
|
settings_save_one("valve/obstacle_current_threshold_close",
|
||||||
|
&obstacle_current_threshold_close_ma,
|
||||||
|
sizeof(obstacle_current_threshold_close_ma));
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t valve_get_max_open_time(void)
|
||||||
|
{
|
||||||
|
return max_opening_time_s;
|
||||||
|
}
|
||||||
|
uint16_t valve_get_max_close_time(void)
|
||||||
|
{
|
||||||
|
return max_closing_time_s;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t valve_get_end_current_threshold_open(void)
|
||||||
|
{
|
||||||
|
return end_current_threshold_open_ma;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t valve_get_end_current_threshold_close(void)
|
||||||
|
{
|
||||||
|
return end_current_threshold_close_ma;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t valve_get_obstacle_current_threshold_open(void)
|
||||||
|
{
|
||||||
|
return obstacle_current_threshold_open_ma;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t valve_get_obstacle_current_threshold_close(void)
|
||||||
|
{
|
||||||
|
return obstacle_current_threshold_close_ma;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t valve_get_opening_current(void)
|
||||||
|
{
|
||||||
|
int32_t current;
|
||||||
|
vnd7050aj_read_load_current(vnd7050aj_dev, VALVE_CHANNEL_OPEN, ¤t);
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t valve_get_closing_current(void)
|
||||||
|
{
|
||||||
|
int32_t current;
|
||||||
|
vnd7050aj_read_load_current(vnd7050aj_dev, VALVE_CHANNEL_CLOSE, ¤t);
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t valve_get_vnd_temp(void)
|
||||||
|
{
|
||||||
|
int32_t temp_c;
|
||||||
|
vnd7050aj_read_chip_temp(vnd7050aj_dev, &temp_c);
|
||||||
|
return temp_c;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t valve_get_vnd_voltage(void)
|
||||||
|
{
|
||||||
|
int32_t voltage_mv;
|
||||||
|
vnd7050aj_read_supply_voltage(vnd7050aj_dev, &voltage_mv);
|
||||||
|
return voltage_mv;
|
||||||
|
}
|
||||||
@@ -1,383 +0,0 @@
|
|||||||
#include "waterlevel_sensor.h"
|
|
||||||
#include <zephyr/kernel.h>
|
|
||||||
#include <zephyr/logging/log.h>
|
|
||||||
#include <zephyr/modbus/modbus.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include "canbus.h"
|
|
||||||
|
|
||||||
LOG_MODULE_REGISTER(wls, CONFIG_LOG_WATERLEVELSENSOR_LEVEL);
|
|
||||||
|
|
||||||
#define MODBUS_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(zephyr_modbus_serial)
|
|
||||||
|
|
||||||
struct k_thread waterlevel_sensor_thread_data;
|
|
||||||
K_THREAD_STACK_DEFINE(waterlevel_sensor_stack, WATERLEVEL_SENSOR_STACK_SIZE);
|
|
||||||
K_MSGQ_DEFINE(waterlevel_sensor_msgq, sizeof(waterlevel_command_t), WATERLEVEL_MESSAGE_QUEUE_SIZE, 4);
|
|
||||||
|
|
||||||
static int modbus_client_iface;
|
|
||||||
|
|
||||||
volatile static struct
|
|
||||||
{
|
|
||||||
int level; // Water level value
|
|
||||||
int zeropoint; // Minimum value
|
|
||||||
int maxpoint; // Maximum value
|
|
||||||
int factor; // Factor for unit conversion
|
|
||||||
bool factor_set; // Flag to indicate if factor is set
|
|
||||||
} waterlevel_measurement = {
|
|
||||||
.factor_set = false,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct modbus_iface_param client_param = {
|
|
||||||
.mode = MODBUS_MODE_RTU,
|
|
||||||
.rx_timeout = 50000, // Timeout for receiving data in milliseconds
|
|
||||||
.serial = {
|
|
||||||
.baud = 9600,
|
|
||||||
.parity = UART_CFG_PARITY_NONE,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
static int waterlevel_modbus_init() {
|
|
||||||
const char iface_name[] = {DEVICE_DT_NAME(MODBUS_NODE)};
|
|
||||||
modbus_client_iface = modbus_iface_get_by_name(iface_name);
|
|
||||||
if (modbus_client_iface < 0)
|
|
||||||
{
|
|
||||||
LOG_ERR("Failed to get Modbus interface by name: %s", iface_name);
|
|
||||||
return modbus_client_iface;
|
|
||||||
}
|
|
||||||
LOG_DBG("Initializing modbus client interface: %s", iface_name);
|
|
||||||
return modbus_init_client(modbus_client_iface, client_param);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int waterlevel_modbus_read(void) {
|
|
||||||
int rc;
|
|
||||||
union
|
|
||||||
{
|
|
||||||
struct
|
|
||||||
{
|
|
||||||
int16_t unit; // Unit of measurement (e.g., cm, mm)
|
|
||||||
int16_t decimals; // Number of decimal places for the measurement
|
|
||||||
int16_t level; // Water level value
|
|
||||||
int16_t zeropoint; // Zero point for the measurement
|
|
||||||
int16_t maxpoint; // Maximum point for the measurement
|
|
||||||
};
|
|
||||||
int16_t data[5]; // Data array for holding registers
|
|
||||||
} waterlevel_modbus_data;
|
|
||||||
rc = modbus_read_holding_regs(modbus_client_iface, WATERLEVEL_SENSOR_MODBUS_NODE_ID, 0x0002, waterlevel_modbus_data.data, sizeof(waterlevel_modbus_data.data) / sizeof(waterlevel_modbus_data.data[0]));
|
|
||||||
if (rc < 0)
|
|
||||||
{
|
|
||||||
LOG_ERR("Failed to read holding registers, node <%d>, returncode: %d", WATERLEVEL_SENSOR_MODBUS_NODE_ID, rc);
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_DBG("Got values. Unit: %d, Decimals: %d, Level: %d, Zero Point: %d, Max Point: %d",
|
|
||||||
waterlevel_modbus_data.unit,
|
|
||||||
waterlevel_modbus_data.decimals,
|
|
||||||
waterlevel_modbus_data.level,
|
|
||||||
waterlevel_modbus_data.zeropoint,
|
|
||||||
waterlevel_modbus_data.maxpoint);
|
|
||||||
|
|
||||||
LOG_HEXDUMP_DBG(waterlevel_modbus_data.data, sizeof(waterlevel_modbus_data.data), "Waterlevel Sensor Holding Registers Data");
|
|
||||||
|
|
||||||
switch (waterlevel_modbus_data.unit)
|
|
||||||
{
|
|
||||||
case 1: // cm
|
|
||||||
waterlevel_measurement.factor = 10;
|
|
||||||
break;
|
|
||||||
case 2: // mm
|
|
||||||
waterlevel_measurement.factor = 1;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
LOG_ERR("Unknown unit: %d", waterlevel_modbus_data.unit);
|
|
||||||
waterlevel_measurement.factor_set = false;
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
switch (waterlevel_modbus_data.decimals)
|
|
||||||
{
|
|
||||||
case 0: // no decimals
|
|
||||||
waterlevel_measurement.factor /= 1;
|
|
||||||
break;
|
|
||||||
case 1: // one decimal
|
|
||||||
waterlevel_measurement.factor /= 10;
|
|
||||||
break;
|
|
||||||
case 2: // two decimals
|
|
||||||
waterlevel_measurement.factor /= 100;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
LOG_ERR("Unknown decimals: %d", waterlevel_modbus_data.decimals);
|
|
||||||
waterlevel_measurement.factor_set = false;
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
waterlevel_measurement.factor_set = true;
|
|
||||||
waterlevel_measurement.level = waterlevel_modbus_data.level * waterlevel_measurement.factor;
|
|
||||||
waterlevel_measurement.zeropoint = waterlevel_modbus_data.zeropoint * waterlevel_measurement.factor;
|
|
||||||
waterlevel_measurement.maxpoint = waterlevel_modbus_data.maxpoint * waterlevel_measurement.factor;
|
|
||||||
|
|
||||||
LOG_DBG("Water level: %dmm, zero point: %dmm, maximum point: %dmm",
|
|
||||||
waterlevel_measurement.level,
|
|
||||||
waterlevel_measurement.zeropoint,
|
|
||||||
waterlevel_measurement.maxpoint);
|
|
||||||
LOG_HEXDUMP_DBG(waterlevel_modbus_data.data, sizeof(waterlevel_modbus_data.data), "Waterlevel Sensor Holding Registers Data");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int waterlevel_send_level(void) {
|
|
||||||
if (!waterlevel_measurement.factor_set) {
|
|
||||||
LOG_ERR("Factor not set, cannot send water level");
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_INF("Sending water level: %dmm", waterlevel_measurement.level);
|
|
||||||
canbus_send16(CANBUS_REG_WATERLEVEL_LEVEL, waterlevel_measurement.level);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int waterlevel_send_zero_point(void) {
|
|
||||||
if (!waterlevel_measurement.factor_set) {
|
|
||||||
LOG_ERR("Factor not set, cannot send zero point");
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_INF("Sending water zero point: %dmm", waterlevel_measurement.zeropoint);
|
|
||||||
canbus_send16(CANBUS_REG_WATERLEVEL_ZERO_POINT, waterlevel_measurement.zeropoint);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int waterlevel_send_max_point(void) {
|
|
||||||
if (!waterlevel_measurement.factor_set) {
|
|
||||||
LOG_ERR("Factor not set, cannot send maximum point");
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_INF("Sending water maximum point: %dmm", waterlevel_measurement.maxpoint);
|
|
||||||
canbus_send16(CANBUS_REG_WATERLEVEL_MAX_POINT, waterlevel_measurement.maxpoint);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int waterlevel_set_zero_point(int zeropoint) {
|
|
||||||
if (!waterlevel_measurement.factor_set) {
|
|
||||||
LOG_ERR("Factor not set, cannot set zero point");
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
int16_t zeropoint_modbus = zeropoint / waterlevel_measurement.factor;
|
|
||||||
int rc = modbus_write_holding_regs(modbus_client_iface, WATERLEVEL_SENSOR_MODBUS_NODE_ID, 0x0005, &zeropoint_modbus, 1);
|
|
||||||
if (rc < 0) {
|
|
||||||
LOG_ERR("Failed to write zero point: %d", rc);
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
waterlevel_measurement.zeropoint = zeropoint; // Update the local measurement structure
|
|
||||||
LOG_INF("Zero point set to: %dmm", waterlevel_measurement.zeropoint);
|
|
||||||
rc = waterlevel_send_zero_point();
|
|
||||||
if (rc < 0) {
|
|
||||||
LOG_ERR("Failed to send zero point: %d", rc);
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int waterlevel_set_max_point(int maxpoint) {
|
|
||||||
if (!waterlevel_measurement.factor_set) {
|
|
||||||
LOG_ERR("Factor not set, cannot set maximum point");
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
int16_t maxpoint_modbus = maxpoint / waterlevel_measurement.factor;
|
|
||||||
int rc = modbus_write_holding_regs(modbus_client_iface, WATERLEVEL_SENSOR_MODBUS_NODE_ID, 0x0006, &maxpoint_modbus, 1);
|
|
||||||
if (rc < 0) {
|
|
||||||
LOG_ERR("Failed to write maximum point: %d", rc);
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
waterlevel_measurement.maxpoint = maxpoint; // Update the local measurement structure
|
|
||||||
LOG_INF("Maximum point set to: %dmm", waterlevel_measurement.maxpoint);
|
|
||||||
rc = waterlevel_send_max_point();
|
|
||||||
if (rc < 0) {
|
|
||||||
LOG_ERR("Failed to send maximum point: %d", rc);
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void waterlevel_sensor_thread(void *arg1, void *arg2, void *arg3)
|
|
||||||
{
|
|
||||||
ARG_UNUSED(arg1);
|
|
||||||
ARG_UNUSED(arg2);
|
|
||||||
ARG_UNUSED(arg3);
|
|
||||||
|
|
||||||
// Initialize the Modbus client
|
|
||||||
int rc = waterlevel_modbus_init();
|
|
||||||
if (rc < 0)
|
|
||||||
{
|
|
||||||
LOG_ERR("Failed to initialize Modbus client: %d", rc);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = waterlevel_modbus_read();
|
|
||||||
if (rc < 0) {
|
|
||||||
LOG_ERR("Failed to read initial water level: %d", rc);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
waterlevel_send_level();
|
|
||||||
waterlevel_send_zero_point();
|
|
||||||
waterlevel_send_max_point();
|
|
||||||
|
|
||||||
// Initialize the last transmission time and level
|
|
||||||
// Use k_uptime_get_32() to get the current uptime in milliseconds
|
|
||||||
// and store the initial water level measurement.
|
|
||||||
// This will be used to determine when to send updates.
|
|
||||||
|
|
||||||
uint32_t last_transmission_time_ms = k_uptime_get_32();
|
|
||||||
int32_t last_transmission_level = waterlevel_measurement.level;
|
|
||||||
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
uint32_t current_time_ms = k_uptime_get_32();
|
|
||||||
uint32_t delta_time = current_time_ms-last_transmission_time_ms;
|
|
||||||
waterlevel_command_t command;
|
|
||||||
|
|
||||||
rc = waterlevel_modbus_read();
|
|
||||||
if (rc < 0)
|
|
||||||
{
|
|
||||||
LOG_ERR("Failed to read water level: %d", rc);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (delta_time >= WATERLEVEL_SENSOR_MAX_UPDATE_INTERVAL_MS ||
|
|
||||||
abs(waterlevel_measurement.level - last_transmission_level) >= WATERLEVEL_SENSOR_MIN_DELTA) {
|
|
||||||
rc = waterlevel_send_level();
|
|
||||||
if (rc < 0) {
|
|
||||||
LOG_ERR("Failed to send water level: %d", rc);
|
|
||||||
} else {
|
|
||||||
last_transmission_time_ms = current_time_ms;
|
|
||||||
last_transmission_level = waterlevel_measurement.level;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
while (k_msgq_get(&waterlevel_sensor_msgq, &command, K_NO_WAIT) == 0)
|
|
||||||
{
|
|
||||||
switch(command.cmd)
|
|
||||||
{
|
|
||||||
case WATERLEVEL_CMD_SET:
|
|
||||||
switch (command.reg)
|
|
||||||
{
|
|
||||||
case CANBUS_REG_WATERLEVEL_ZERO_POINT: // Set zero point
|
|
||||||
rc = waterlevel_set_zero_point(command.data);
|
|
||||||
if (rc < 0) {
|
|
||||||
LOG_ERR("Failed to set zero point: %d", rc);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case CANBUS_REG_WATERLEVEL_MAX_POINT: // Set maximum point
|
|
||||||
rc = waterlevel_set_max_point(command.data);
|
|
||||||
if (rc < 0) {
|
|
||||||
LOG_ERR("Failed to set maximum point: %d", rc);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
LOG_ERR("Unknown register for SET command: 0x%02X", command.reg);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case WATERLEVEL_CMD_GET:
|
|
||||||
switch (command.reg)
|
|
||||||
{
|
|
||||||
case CANBUS_REG_WATERLEVEL_LEVEL: // Get water level
|
|
||||||
waterlevel_send_level();
|
|
||||||
break;
|
|
||||||
case CANBUS_REG_WATERLEVEL_ZERO_POINT: // Get zero point
|
|
||||||
waterlevel_send_zero_point();
|
|
||||||
break;
|
|
||||||
case CANBUS_REG_WATERLEVEL_MAX_POINT: // Get maximum point
|
|
||||||
waterlevel_send_max_point();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
LOG_ERR("Unknown register for GET command: 0x%02X", command.reg);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
LOG_ERR("Unknown command type: %d", command.cmd);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int waterlevel_sensor_start_thread(void)
|
|
||||||
{
|
|
||||||
k_tid_t waterlevel_sensor_thread_id;
|
|
||||||
|
|
||||||
// Start the thread
|
|
||||||
waterlevel_sensor_thread_id = k_thread_create(&waterlevel_sensor_thread_data, waterlevel_sensor_stack,
|
|
||||||
K_THREAD_STACK_SIZEOF(waterlevel_sensor_stack), waterlevel_sensor_thread,
|
|
||||||
NULL, NULL, NULL,
|
|
||||||
WATERLEVEL_SENSOR_THREAD_PRIORITY, 0, K_NO_WAIT);
|
|
||||||
|
|
||||||
if (waterlevel_sensor_thread_id == NULL)
|
|
||||||
{
|
|
||||||
LOG_ERR("Failed to create water level sensor thread");
|
|
||||||
return -ENOMEM;
|
|
||||||
}
|
|
||||||
k_thread_name_set(waterlevel_sensor_thread_id, "waterlevel_sensor");
|
|
||||||
|
|
||||||
LOG_INF("Water level sensor thread started successfully");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef CONFIG_SHELL
|
|
||||||
#include <zephyr/shell/shell.h>
|
|
||||||
|
|
||||||
void waterlevel_set_zero_point_shell(const struct shell *shell, size_t argc, char **argv) {
|
|
||||||
if (argc != 2) {
|
|
||||||
shell_error(shell, "Usage: waterlevel_sensor set_zero_point <zeropoint>");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int zeropoint = atoi(argv[1]);
|
|
||||||
int rc = waterlevel_set_zero_point(zeropoint);
|
|
||||||
if (rc < 0) {
|
|
||||||
shell_error(shell, "Failed to set zero point: %d", rc);
|
|
||||||
} else {
|
|
||||||
shell_print(shell, "Zero point set to: %dmm", zeropoint);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void waterlevel_set_max_point_shell(const struct shell *shell, size_t argc, char **argv) {
|
|
||||||
if (argc != 2) {
|
|
||||||
shell_error(shell, "Usage: waterlevel_sensor set_max_point <maxpoint>");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int maxpoint = atoi(argv[1]);
|
|
||||||
int rc = waterlevel_set_max_point(maxpoint);
|
|
||||||
if (rc < 0) {
|
|
||||||
shell_error(shell, "Failed to set maximum point: %d", rc);
|
|
||||||
} else {
|
|
||||||
shell_print(shell, "Maximum point set to: %dmm", maxpoint);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void waterlevel_sensor_print_shell(const struct shell *shell, size_t argc, char **argv) {
|
|
||||||
ARG_UNUSED(argc);
|
|
||||||
ARG_UNUSED(argv);
|
|
||||||
|
|
||||||
waterlevel_modbus_read();
|
|
||||||
if (!waterlevel_measurement.factor_set) {
|
|
||||||
shell_error(shell, "Factor not set, cannot print water level");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
shell_print(shell, "Current water level: %4dmm", waterlevel_measurement.level);
|
|
||||||
shell_print(shell, "Zero point: %4dmm", waterlevel_measurement.zeropoint);
|
|
||||||
shell_print(shell, "Maximum point: %4dmm", waterlevel_measurement.maxpoint);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Define the shell commands for the water level sensor
|
|
||||||
SHELL_STATIC_SUBCMD_SET_CREATE(
|
|
||||||
waterlevel_sensor_cmds,
|
|
||||||
SHELL_CMD(print, NULL, "Print the current water level, zero point, and maximum point", waterlevel_sensor_print_shell),
|
|
||||||
SHELL_CMD(setzero, NULL, "Set the zero point for the water level sensor", waterlevel_set_zero_point_shell),
|
|
||||||
SHELL_CMD(setmax, NULL, "Set the maximum point for the water level sensor", waterlevel_set_max_point_shell),
|
|
||||||
SHELL_SUBCMD_SET_END);
|
|
||||||
|
|
||||||
SHELL_CMD_REGISTER(wls, &waterlevel_sensor_cmds, "Water level sensor commands", NULL);
|
|
||||||
#endif // CONFIG_SHELL
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
#ifndef __WATERLEVEL_SENSOR_H__
|
|
||||||
#define __WATERLEVEL_SENSOR_H__
|
|
||||||
|
|
||||||
#define WATERLEVEL_SENSOR_STACK_SIZE (512)
|
|
||||||
#define WATERLEVEL_SENSOR_THREAD_PRIORITY (2)
|
|
||||||
#define WATERLEVEL_MESSAGE_QUEUE_SIZE (5) // Size of the message queue for water level sensor thread
|
|
||||||
#define WATERLEVEL_SENSOR_READ_INTERVAL_MS (5000) // Interval for reading the water level sensor in milliseconds
|
|
||||||
#define WATERLEVEL_SENSOR_MIN_DELTA (2) // Minimum change in water level to trigger an update
|
|
||||||
#define WATERLEVEL_SENSOR_MAX_UPDATE_INTERVAL_MS (600000) // Maximum interval for updating the water level in milliseconds
|
|
||||||
|
|
||||||
#define WATERLEVEL_SENSOR_MODBUS_NODE_ID (0x01) // Modbus node ID for the water level sensor
|
|
||||||
#define WATERLEVEL_SENSOR_MODBUS_BAUD_RATE (9600) // Baud rate for Modbus communication
|
|
||||||
|
|
||||||
#include <inttypes.h>
|
|
||||||
|
|
||||||
int waterlevel_sensor_start_thread(void);
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint8_t reg;
|
|
||||||
enum {
|
|
||||||
WATERLEVEL_CMD_SET,
|
|
||||||
WATERLEVEL_CMD_GET,
|
|
||||||
} cmd;
|
|
||||||
int16_t data; // Data to be set
|
|
||||||
} waterlevel_command_t;
|
|
||||||
|
|
||||||
#endif // __WATERLEVEL_SENSOR_H__
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
CONFIG_HAS_MODBUS_WATERLEVEL_SENSOR=y
|
|
||||||
CONFIG_HAS_VALVE=y
|
|
||||||
|
|
||||||
CONFIG_LOG=y
|
|
||||||
CONFIG_LOG_DEFAULT_LEVEL=3
|
|
||||||
# CONFIG_LOG_CAN_LEVEL=4
|
|
||||||
# CONFIG_LOG_WATERLEVELSENSOR_LEVEL=4
|
|
||||||
# CONFIG_LOG_VALVE_LEVEL=4
|
|
||||||
CONFIG_CBPRINTF_FP_SUPPORT=y
|
|
||||||
CONFIG_UART_CONSOLE=y # Console on USART1
|
|
||||||
|
|
||||||
# CAN loopback mode for testing
|
|
||||||
#CONFIG_LOOPBACK_MODE=y
|
|
||||||
CONFIG_SHELL=y
|
|
||||||
CONFIG_CAN_SHELL=y
|
|
||||||
CONFIG_GPIO_SHELL=y
|
|
||||||
CONFIG_REBOOT=y
|
|
||||||
|
|
||||||
CONFIG_ADC=y
|
|
||||||
CONFIG_ADC_STM32=y
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
#include <zephyr/logging/log.h>
|
|
||||||
#include <zephyr/kernel.h>
|
|
||||||
#include <zephyr/sys/byteorder.h>
|
|
||||||
#include <zephyr/shell/shell.h>
|
|
||||||
#include "canbus.h"
|
|
||||||
#include "canbus_registers.h"
|
|
||||||
|
|
||||||
#ifdef CONFIG_HAS_MODBUS_WATERLEVEL_SENSOR
|
|
||||||
// Include the water level sensor header file when the feature is enabled
|
|
||||||
#include "waterlevel_sensor.h"
|
|
||||||
#endif // CONFIG_HAS_MODBUS_WATERLEVEL_SENSOR
|
|
||||||
|
|
||||||
#ifdef CONFIG_HAS_VALVE
|
|
||||||
#include "valve.h"
|
|
||||||
#endif // CONFIG_HAS_VALVE
|
|
||||||
|
|
||||||
LOG_MODULE_REGISTER(main, CONFIG_LOG_DEFAULT_LEVEL);
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
LOG_INF("Starting main application...");
|
|
||||||
canbus_init();
|
|
||||||
k_sleep(K_MSEC(3000)); // Allow some time for CAN initialization
|
|
||||||
#ifdef CONFIG_HAS_MODBUS_WATERLEVEL_SENSOR
|
|
||||||
int rc = waterlevel_sensor_start_thread();
|
|
||||||
if (rc < 0)
|
|
||||||
{
|
|
||||||
LOG_ERR("Failed to start water level sensor thread: %d", rc);
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
#endif // CONFIG_HAS_MODBUS_WATERLEVEL_SENSOR
|
|
||||||
|
|
||||||
valve_cmd(VALVE_COMMAND_CLOSE); // Ensure the valve is closed at startup
|
|
||||||
|
|
||||||
LOG_INF("Main application started successfully.");
|
|
||||||
return 0; // Return 0 on success
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef CONFIG_SHELL
|
|
||||||
#include <zephyr/shell/shell.h>
|
|
||||||
#include <zephyr/sys/reboot.h>
|
|
||||||
|
|
||||||
static int reboot_shell_cmd(const struct shell *shell, size_t argc, char **argv)
|
|
||||||
{
|
|
||||||
ARG_UNUSED(argc);
|
|
||||||
ARG_UNUSED(argv);
|
|
||||||
shell_print(shell, "Rebooting the node in 1 second...");
|
|
||||||
k_sleep(K_SECONDS(1));
|
|
||||||
sys_reboot(SYS_REBOOT_COLD);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
SHELL_CMD_REGISTER(reboot, NULL, "Reboot the node", reboot_shell_cmd);
|
|
||||||
#endif // CONFIG_SHELL
|
|
||||||
@@ -1,86 +0,0 @@
|
|||||||
#include <zephyr/kernel.h>
|
|
||||||
#include <zephyr/drivers/adc.h>
|
|
||||||
#include <zephyr/logging/log.h>
|
|
||||||
#include <zephyr/device.h>
|
|
||||||
#include <zephyr/devicetree.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
LOG_MODULE_REGISTER(main2, LOG_LEVEL_DBG);
|
|
||||||
|
|
||||||
#define MOTOR_ADC_NODE DT_ALIAS(adc_motor_current)
|
|
||||||
#define VREF_ADC_NODE DT_ALIAS(adc_vref)
|
|
||||||
|
|
||||||
static const struct device * const adc_dev = DEVICE_DT_GET(DT_PARENT(MOTOR_ADC_NODE));
|
|
||||||
static const uint8_t motor_channel_id = DT_REG_ADDR(MOTOR_ADC_NODE);
|
|
||||||
static const uint8_t vref_channel_id = DT_REG_ADDR(VREF_ADC_NODE);
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
int err;
|
|
||||||
int16_t adc_raw_value;
|
|
||||||
|
|
||||||
LOG_INF("Starting ADC test with direct register setup...");
|
|
||||||
|
|
||||||
if (!device_is_ready(adc_dev)) {
|
|
||||||
LOG_ERR("ADC device is not ready");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_INF("Manually setting up ADC registers...");
|
|
||||||
|
|
||||||
uint32_t adc_base = DT_REG_ADDR(DT_NODELABEL(adc1));
|
|
||||||
|
|
||||||
volatile uint32_t *ADC_CR2 = (uint32_t *)(adc_base + 0x08);
|
|
||||||
volatile uint32_t *ADC_SMPR1 = (uint32_t *)(adc_base + 0x0C);
|
|
||||||
volatile uint32_t *ADC_SMPR2 = (uint32_t *)(adc_base + 0x10);
|
|
||||||
|
|
||||||
// Schritt 1: Internen VREFINT-Kanal einschalten
|
|
||||||
const uint32_t ADC_CR2_TSVREFE_BIT = 23;
|
|
||||||
*ADC_CR2 |= (1 << ADC_CR2_TSVREFE_BIT);
|
|
||||||
LOG_INF("VREFINT channel enabled via CR2 register.");
|
|
||||||
|
|
||||||
// Schritt 2: Lange Abtastzeiten für Stabilität setzen
|
|
||||||
*ADC_SMPR2 |= (0b111 << (3 * 9));
|
|
||||||
*ADC_SMPR1 |= (0b111 << (3 * (17 - 10)));
|
|
||||||
LOG_INF("Acquisition times set directly in SMPR registers.");
|
|
||||||
|
|
||||||
|
|
||||||
k_busy_wait(10);
|
|
||||||
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
int32_t motor_raw = 0;
|
|
||||||
int32_t vref_raw = 0;
|
|
||||||
|
|
||||||
struct adc_sequence sequence = {
|
|
||||||
.buffer = &adc_raw_value,
|
|
||||||
.buffer_size = sizeof(adc_raw_value),
|
|
||||||
.resolution = 12,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Motor-Kanal lesen
|
|
||||||
sequence.channels = BIT(motor_channel_id);
|
|
||||||
if (adc_read(adc_dev, &sequence) == 0) {
|
|
||||||
motor_raw = adc_raw_value;
|
|
||||||
}
|
|
||||||
|
|
||||||
// VREF-Kanal lesen
|
|
||||||
sequence.channels = BIT(vref_channel_id);
|
|
||||||
if (adc_read(adc_dev, &sequence) == 0) {
|
|
||||||
vref_raw = adc_raw_value;
|
|
||||||
}
|
|
||||||
|
|
||||||
// VDD-Berechnung mit dem generischen, aber für Sie gut funktionierenden 1200mV-Wert
|
|
||||||
int32_t vdd_mv = (vref_raw > 0) ? (1200 * 4095 / vref_raw) : 0;
|
|
||||||
int32_t motor_mv = 0;
|
|
||||||
|
|
||||||
if (motor_raw > 0 && vdd_mv > 0) {
|
|
||||||
motor_mv = motor_raw;
|
|
||||||
err = adc_raw_to_millivolts(vdd_mv, ADC_GAIN_1, 12, &motor_mv);
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_INF("Motor Raw: %4d, Motor mV: %4d | VDD: %4d mV", motor_raw, motor_mv, vdd_mv);
|
|
||||||
k_sleep(K_MSEC(2000));
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
103
software/tools/modbus_tool/README.de.md
Normal file
103
software/tools/modbus_tool/README.de.md
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
<img src="../../../docs/img/logo.png" alt="Logo" width="100"/>
|
||||||
|
|
||||||
|
# Modbus Tool für Bewässerungssystem-Knoten
|
||||||
|
|
||||||
|
Dieses Python-Skript bietet eine interaktive Kommandozeilen-Benutzeroberfläche (TUI) zur Steuerung und Überwachung eines Ventil-Knotens des Bewässerungssystems über Modbus RTU.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- **Interaktive Benutzeroberfläche:** Eine benutzerfreundliche, auf `curses` basierende Oberfläche, die eine einfache Bedienung ermöglicht.
|
||||||
|
- **Live-Statusanzeige:** Zeigt tabellarisch und in Echtzeit alle wichtigen Register des Slaves an:
|
||||||
|
- Ventilstatus (Zustand, Bewegung, Motorstrom)
|
||||||
|
- Zustand der digitalen Ein- und Ausgänge
|
||||||
|
- "Clear-on-Read" Taster-Events
|
||||||
|
- Systemkonfiguration (Öffnungs-/Schließzeiten, Watchdog-Timeout)
|
||||||
|
- Gerätestatus (Firmware-Version, Uptime)
|
||||||
|
- **Volle Kontrolle:** Ermöglicht das Senden von Befehlen zum Öffnen, Schließen und Stoppen des Ventils sowie zum Umschalten der digitalen Ausgänge.
|
||||||
|
- **Konfiguration zur Laufzeit:** Die maximalen Öffnungs-/Schließzeiten und der Watchdog-Timeout können direkt in der Oberfläche geändert werden.
|
||||||
|
- **Simulierter Firmware-Upload:** Implementiert den vollständigen, in der Dokumentation beschriebenen Firmware-Update-Prozess. Das Tool sendet eine `firmware.bin`-Datei in Chunks an den Slave und folgt dem CRC-Verifizierungs-Protokoll.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
### Voraussetzungen
|
||||||
|
|
||||||
|
- Python 3.x
|
||||||
|
- `pip` (Python Paket-Installer)
|
||||||
|
|
||||||
|
### Installation der Abhängigkeiten
|
||||||
|
|
||||||
|
Die benötigten Python-Pakete sind in der Datei `requirements.txt` aufgeführt. Sie können auf zwei Arten installiert werden: global oder in einer virtuellen Umgebung (empfohlen).
|
||||||
|
|
||||||
|
#### Option 1: Installation mit virtueller Umgebung (empfohlen)
|
||||||
|
|
||||||
|
Eine virtuelle Umgebung isoliert die Projekt-Abhängigkeiten von Ihrem globalen Python-System, was Konflikte vermeidet.
|
||||||
|
|
||||||
|
1. **Virtuelle Umgebung erstellen:**
|
||||||
|
Führen Sie im Verzeichnis `software/tools/modbus_tool` den folgenden Befehl aus, um eine Umgebung im Ordner `.venv` zu erstellen:
|
||||||
|
```bash
|
||||||
|
python3 -m venv .venv
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Umgebung aktivieren:**
|
||||||
|
- **Linux / macOS:**
|
||||||
|
```bash
|
||||||
|
source .venv/bin/activate
|
||||||
|
```
|
||||||
|
Ihre Shell-Anzeige sollte sich ändern und `(.venv)` am Anfang zeigen.
|
||||||
|
- **Windows (cmd.exe):**
|
||||||
|
```bash
|
||||||
|
.venv\Scripts\activate.bat
|
||||||
|
```
|
||||||
|
- **Windows (PowerShell):**
|
||||||
|
```powershell
|
||||||
|
.venv\Scripts\Activate.ps1
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **Abhängigkeiten installieren:**
|
||||||
|
Wenn die Umgebung aktiv ist, installieren Sie die Pakete:
|
||||||
|
```bash
|
||||||
|
pip install -r requirements.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
4. **Umgebung deaktivieren:**
|
||||||
|
Wenn Sie fertig sind, können Sie die Umgebung mit folgendem Befehl wieder verlassen:
|
||||||
|
```bash
|
||||||
|
deactivate
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Option 2: Globale Installation (nicht empfohlen)
|
||||||
|
|
||||||
|
Wenn Sie keine virtuelle Umgebung verwenden möchten, können Sie die Pakete direkt in Ihrem globalen Python-System installieren.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install -r requirements.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
## Verwendung
|
||||||
|
|
||||||
|
Stellen Sie sicher, dass das Skript ausführbar ist:
|
||||||
|
```bash
|
||||||
|
chmod +x modbus_tool.py
|
||||||
|
```
|
||||||
|
|
||||||
|
Starten Sie das Tool, indem Sie den seriellen Port als Argument übergeben:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./modbus_tool.py /dev/ttyACM0
|
||||||
|
```
|
||||||
|
Ersetzen Sie `/dev/ttyACM0` durch den korrekten Port Ihres Geräts.
|
||||||
|
|
||||||
|
### Kommandozeilen-Argumente
|
||||||
|
|
||||||
|
- `port`: (Erforderlich) Der serielle Port (z.B. `/dev/ttyACM0` oder `COM3`).
|
||||||
|
- `--baud`: Die Baudrate (Standard: `19200`).
|
||||||
|
- `--slave-id`: Die Modbus Slave ID des Geräts (Standard: `1`).
|
||||||
|
- `--interval`: Das Abfrageintervall für den Status in Sekunden (Standard: `1.0`).
|
||||||
|
|
||||||
|
### Bedienung der Oberfläche
|
||||||
|
|
||||||
|
- **Navigation:** Verwenden Sie die **Pfeiltasten (↑/↓)**, um zwischen den Menüpunkten zu navigieren.
|
||||||
|
- **Auswählen:** Drücken Sie **Enter**, um den ausgewählten Befehl auszuführen.
|
||||||
|
- **Werte eingeben:** Bei Aktionen wie "Set Watchdog" werden Sie zur Eingabe eines Wertes aufgefordert. Geben Sie den Wert ein und bestätigen Sie mit **Enter**.
|
||||||
|
- **Firmware Update:** Diese Funktion startet den Upload der Datei `firmware.bin` aus dem aktuellen Verzeichnis. Während des Updates wird eine Fortschrittsanzeige dargestellt.
|
||||||
|
- **Beenden:** Wählen Sie den Menüpunkt **"Exit"** und drücken Sie **Enter**.
|
||||||
BIN
software/tools/modbus_tool/firmware.bin
Normal file
BIN
software/tools/modbus_tool/firmware.bin
Normal file
Binary file not shown.
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user