feat(firmware): add buzzer and refactoring global code
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
# Buzzer
|
||||
|
||||
## Purpose
|
||||
|
||||
This module provides short audible feedback for accepted keypad presses.
|
||||
|
||||
The current hardware uses an active buzzer. The firmware does not generate PWM; it only drives a GPIO high for a short pulse.
|
||||
|
||||
---
|
||||
|
||||
## Hardware
|
||||
|
||||
- Seeed Studio XIAO nRF52840.
|
||||
- Active buzzer.
|
||||
- Buzzer driven through an external transistor.
|
||||
- Control signal: XIAO pin `D0`.
|
||||
|
||||
The GPIO must be used as a control signal only. The buzzer current is supplied through the transistor stage, not directly by the microcontroller pin.
|
||||
|
||||
---
|
||||
|
||||
## Public API
|
||||
|
||||
```c
|
||||
int buzzer_init(void);
|
||||
|
||||
void buzzer_on(void);
|
||||
|
||||
void buzzer_beep_key(void);
|
||||
|
||||
void buzzer_off(void);
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Current Behavior
|
||||
|
||||
- `buzzer_init()` configures the buzzer control GPIO and leaves the buzzer off.
|
||||
- `buzzer_beep_key()` emits a short blocking beep.
|
||||
- The keypad calls `buzzer_beep_key()` only when a new key press is accepted, so holding a key does not produce a continuous beep.
|
||||
|
||||
---
|
||||
|
||||
## Notes
|
||||
|
||||
Because the buzzer is active, tone generation is handled by the buzzer itself.
|
||||
|
||||
Future sound patterns, such as success and error feedback, should be implemented as higher-level sequences built from GPIO on/off pulses.
|
||||
+68
-69
@@ -1,121 +1,120 @@
|
||||
# Firmware OpenParcelBox
|
||||
# OpenParcelBox Firmware
|
||||
|
||||
Ce répertoire contient le firmware de l'OpenParcelBox.
|
||||
This directory contains the OpenParcelBox firmware.
|
||||
|
||||
---
|
||||
|
||||
# Objectifs
|
||||
## Goals
|
||||
|
||||
Le firmware doit permettre :
|
||||
The firmware is responsible for:
|
||||
|
||||
* La gestion du clavier.
|
||||
* La gestion des codes d'accès.
|
||||
* Le contrôle du mécanisme de verrouillage.
|
||||
* La surveillance de la batterie.
|
||||
* La communication Zigbee.
|
||||
* Les mises à jour OTA.
|
||||
* L'intégration Home Assistant.
|
||||
- Keypad management.
|
||||
- Access code handling.
|
||||
- Lock mechanism control.
|
||||
- Battery monitoring.
|
||||
- Zigbee communication.
|
||||
- OTA updates.
|
||||
- Home Assistant integration.
|
||||
- Audible and visual feedback.
|
||||
|
||||
---
|
||||
|
||||
# Plateforme matérielle
|
||||
## Hardware Platform
|
||||
|
||||
## Carte principale
|
||||
Main board:
|
||||
|
||||
* Seeed Studio XIAO BLE nRF52840
|
||||
- Seeed Studio XIAO BLE nRF52840
|
||||
|
||||
## Microcontrôleur
|
||||
Microcontroller:
|
||||
|
||||
* Nordic nRF52840
|
||||
- Nordic nRF52840
|
||||
|
||||
---
|
||||
|
||||
# Technologies utilisées
|
||||
## Technologies
|
||||
|
||||
## Framework
|
||||
Framework:
|
||||
|
||||
Zephyr RTOS
|
||||
- Zephyr RTOS
|
||||
|
||||
## Langage
|
||||
Language:
|
||||
|
||||
C++
|
||||
- C17
|
||||
|
||||
## Outils
|
||||
Core build tools:
|
||||
|
||||
* Zephyr SDK
|
||||
* west
|
||||
* CMake
|
||||
* Ninja
|
||||
* VSCodium
|
||||
- Zephyr SDK
|
||||
- west
|
||||
- CMake
|
||||
- Ninja
|
||||
|
||||
---
|
||||
|
||||
# Architecture logicielle prévue
|
||||
## Current Structure
|
||||
|
||||
```text
|
||||
firmware
|
||||
├── app
|
||||
├── boards
|
||||
├── modules
|
||||
└── tests
|
||||
firmware/
|
||||
+-- app/ Main Zephyr application
|
||||
+-- include/ Board-level configuration headers
|
||||
+-- src/ Firmware modules
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# Modules
|
||||
## Current Modules
|
||||
|
||||
## Core
|
||||
### Core
|
||||
|
||||
Initialisation et gestion du système.
|
||||
System startup and module initialization.
|
||||
|
||||
## Keypad
|
||||
### RGB LED
|
||||
|
||||
Gestion du clavier 2 × 6.
|
||||
On-board status LED driver.
|
||||
|
||||
## Access Control
|
||||
### GPIO Expander
|
||||
|
||||
Validation des codes et gestion des droits.
|
||||
PCF8574 I2C GPIO expander abstraction layer.
|
||||
|
||||
## Lock Controller
|
||||
### Keypad
|
||||
|
||||
Pilotage du verrou.
|
||||
Development keypad matrix scanning through the GPIO expander.
|
||||
|
||||
## Sensor Manager
|
||||
### Buzzer
|
||||
|
||||
Lecture des capteurs.
|
||||
|
||||
## Power Management
|
||||
|
||||
Gestion de l'énergie et des modes basse consommation.
|
||||
|
||||
## Zigbee
|
||||
|
||||
Communication avec Home Assistant.
|
||||
|
||||
## OTA
|
||||
|
||||
Mises à jour à distance.
|
||||
Active buzzer control through a GPIO-driven transistor.
|
||||
|
||||
---
|
||||
|
||||
# Philosophie de développement
|
||||
## Development Philosophy
|
||||
|
||||
Le firmware doit privilégier :
|
||||
The firmware should prioritize:
|
||||
|
||||
* La simplicité.
|
||||
* La robustesse.
|
||||
* La faible consommation.
|
||||
* La maintenabilité.
|
||||
* L'indépendance vis-à-vis des services cloud.
|
||||
- Simplicity.
|
||||
- Robustness.
|
||||
- Low power consumption.
|
||||
- Maintainability.
|
||||
- Independence from cloud services.
|
||||
|
||||
---
|
||||
|
||||
# État actuel
|
||||
## Current Status
|
||||
|
||||
Aucun code de production n'est encore implémenté.
|
||||
The current firmware is a hardware validation application.
|
||||
|
||||
Les travaux actuels concernent :
|
||||
Implemented:
|
||||
|
||||
* Le reverse engineering du matériel d'origine.
|
||||
* La validation de l'architecture logicielle.
|
||||
* La préparation de l'environnement Zephyr.
|
||||
- RGB LED status feedback.
|
||||
- PCF8574 GPIO expander access.
|
||||
- 4x4 development keypad scanning.
|
||||
- Active buzzer feedback on accepted key presses.
|
||||
- Serial debug output.
|
||||
|
||||
In progress or planned:
|
||||
|
||||
- Production keypad mapping.
|
||||
- Access control.
|
||||
- Lock driver.
|
||||
- Battery monitoring.
|
||||
- NFC.
|
||||
- Zigbee.
|
||||
- OTA updates.
|
||||
|
||||
@@ -4,9 +4,14 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||
|
||||
project(OpenParcelBox)
|
||||
|
||||
target_include_directories(app PRIVATE
|
||||
include
|
||||
)
|
||||
|
||||
target_sources(app PRIVATE
|
||||
src/main.c
|
||||
src/led.c
|
||||
src/buzzer.c
|
||||
src/gpio_expander.c
|
||||
src/keypad.c
|
||||
)
|
||||
)
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
# Firmware Constraints
|
||||
# Firmware Application Constraints
|
||||
|
||||
## Technologies
|
||||
|
||||
- Development with Zephyr RTOS.
|
||||
- Programming language: C17.
|
||||
- Target board: Seeed Studio XIAO nRF52840.
|
||||
- Build system: CMake + West.
|
||||
- Build system: CMake + west.
|
||||
- All source code comments must be written in English.
|
||||
- The firmware must compile without warnings whenever possible.
|
||||
|
||||
@@ -14,8 +14,8 @@
|
||||
- Modular architecture.
|
||||
- One module per hardware peripheral or functional block.
|
||||
- Hardware abstraction must be separated from business logic.
|
||||
- All GPIO assignments must be centralized in `board_config.h`.
|
||||
- Hardware initialization must be centralized in the board module.
|
||||
- Board-specific GPIO assignments must be centralized in `board_config.h`.
|
||||
- Hardware initialization should stay close to the module that owns the hardware, unless a shared board module becomes necessary.
|
||||
|
||||
## Hardware
|
||||
|
||||
@@ -23,14 +23,14 @@
|
||||
- Minimize power consumption whenever possible.
|
||||
- GPIO assignments must match the hardware documentation.
|
||||
- DeviceTree must be used for hardware configuration whenever possible.
|
||||
- I²C peripherals must use Zephyr drivers.
|
||||
- I2C peripherals must use Zephyr drivers.
|
||||
|
||||
## Features
|
||||
|
||||
- Matrix keypad management.
|
||||
- Electronic lock control.
|
||||
- Lock state monitoring.
|
||||
- PWM buzzer control.
|
||||
- Active buzzer control through a GPIO-driven transistor.
|
||||
- PCF8574 I/O expander support.
|
||||
- NFC support.
|
||||
- Zigbee communication.
|
||||
@@ -49,4 +49,4 @@
|
||||
|
||||
- Every public function must be documented.
|
||||
- Complex algorithms must include explanatory comments in English.
|
||||
- Keep the firmware synchronized with the project documentation.
|
||||
- Keep the firmware synchronized with the project documentation.
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* OpenParcelBox
|
||||
* Copyright (c) 2026
|
||||
*
|
||||
* Central board configuration.
|
||||
*/
|
||||
|
||||
#ifndef BOARD_CONFIG_H
|
||||
#define BOARD_CONFIG_H
|
||||
|
||||
#include <zephyr/devicetree.h>
|
||||
#include <zephyr/dt-bindings/gpio/gpio.h>
|
||||
|
||||
/* RGB LED */
|
||||
|
||||
#define LED_RED_NODE DT_ALIAS(led0)
|
||||
#define LED_GREEN_NODE DT_ALIAS(led1)
|
||||
#define LED_BLUE_NODE DT_ALIAS(led2)
|
||||
|
||||
/* GPIO expander */
|
||||
|
||||
#define GPIO_EXPANDER_I2C_NODE DT_NODELABEL(i2c1)
|
||||
#define GPIO_EXPANDER_ADDRESS 0x20
|
||||
|
||||
/* Buzzer */
|
||||
|
||||
#define BUZZER_GPIO_NODE DT_NODELABEL(gpio0)
|
||||
#define BUZZER_GPIO_PIN 2
|
||||
#define BUZZER_GPIO_FLAGS GPIO_ACTIVE_HIGH
|
||||
|
||||
#endif /* BOARD_CONFIG_H */
|
||||
|
||||
@@ -4,4 +4,5 @@ CONFIG_I2C_SHELL=y
|
||||
CONFIG_SHELL=y
|
||||
CONFIG_CONSOLE=y
|
||||
CONFIG_SERIAL=y
|
||||
CONFIG_UART_CONSOLE=y
|
||||
CONFIG_UART_CONSOLE=y
|
||||
CONFIG_DEBUG_THREAD_INFO=y
|
||||
@@ -1,26 +0,0 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* OpenParcelBox
|
||||
* Copyright (c) 2026
|
||||
*
|
||||
* Board pin definitions.
|
||||
*/
|
||||
|
||||
#ifndef BOARD_PINS_H
|
||||
#define BOARD_PINS_H
|
||||
|
||||
#include <zephyr/devicetree.h>
|
||||
|
||||
/* RGB LED */
|
||||
|
||||
#define LED_RED_NODE DT_ALIAS(led0)
|
||||
#define LED_GREEN_NODE DT_ALIAS(led1)
|
||||
#define LED_BLUE_NODE DT_ALIAS(led2)
|
||||
|
||||
/* GPIO Expander */
|
||||
|
||||
#define GPIO_EXPANDER_I2C_NODE DT_NODELABEL(i2c1)
|
||||
#define GPIO_EXPANDER_ADDRESS 0x20
|
||||
|
||||
#endif /* BOARD_PINS_H */
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* OpenParcelBox
|
||||
* Copyright (c) 2026
|
||||
*
|
||||
* Buzzer driver.
|
||||
*/
|
||||
|
||||
#include "buzzer.h"
|
||||
|
||||
#include "board_config.h"
|
||||
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/kernel.h>
|
||||
|
||||
#define BUZZER_KEY_BEEP_MS 50
|
||||
|
||||
/* D0 on the Seeed Studio XIAO nRF52840 is mapped to GPIO0 pin 2. */
|
||||
static const struct gpio_dt_spec buzzer_gpio = {
|
||||
.port = DEVICE_DT_GET(BUZZER_GPIO_NODE),
|
||||
.pin = BUZZER_GPIO_PIN,
|
||||
.dt_flags = BUZZER_GPIO_FLAGS,
|
||||
};
|
||||
|
||||
int buzzer_init(void) {
|
||||
int ret;
|
||||
|
||||
if (!gpio_is_ready_dt(&buzzer_gpio)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = gpio_pin_configure_dt(&buzzer_gpio, GPIO_OUTPUT_LOW);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
buzzer_off();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void buzzer_on(void) {
|
||||
/* Active buzzer: drive D0 high. No PWM is generated by the firmware. */
|
||||
gpio_pin_set_raw(buzzer_gpio.port, buzzer_gpio.pin, 1);
|
||||
}
|
||||
|
||||
void buzzer_beep_key(void) {
|
||||
/* Short active pulse used as keypad feedback. */
|
||||
buzzer_on();
|
||||
k_msleep(BUZZER_KEY_BEEP_MS);
|
||||
buzzer_off();
|
||||
}
|
||||
|
||||
void buzzer_off(void) { gpio_pin_set_raw(buzzer_gpio.port, buzzer_gpio.pin, 0); }
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* OpenParcelBox
|
||||
* Copyright (c) 2026
|
||||
*
|
||||
* Buzzer driver.
|
||||
*/
|
||||
|
||||
#ifndef BUZZER_H
|
||||
#define BUZZER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Initialize the buzzer driver.
|
||||
*
|
||||
* Configures the buzzer GPIO and leaves the buzzer off.
|
||||
*
|
||||
* @retval 0 Success.
|
||||
* @retval <0 Initialization failed.
|
||||
*/
|
||||
int buzzer_init(void);
|
||||
|
||||
/**
|
||||
* @brief Turn on the buzzer output.
|
||||
*
|
||||
* Active buzzers generate the tone internally. The firmware only drives D0
|
||||
* high; it does not generate PWM.
|
||||
*/
|
||||
void buzzer_on(void);
|
||||
|
||||
/**
|
||||
* @brief Emit a short keypad feedback beep.
|
||||
*
|
||||
* This function is intentionally short and blocking. Call it only when a new
|
||||
* key press is accepted, not while a key is continuously held.
|
||||
*/
|
||||
void buzzer_beep_key(void);
|
||||
|
||||
/**
|
||||
* @brief Turn off the buzzer output.
|
||||
*/
|
||||
void buzzer_off(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BUZZER_H */
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
#include "gpio_expander.h"
|
||||
|
||||
#include "board_pins.h"
|
||||
#include "board_config.h"
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/i2c.h>
|
||||
@@ -56,11 +56,16 @@ int gpio_expander_read_port(uint8_t *value) {
|
||||
return i2c_read(i2c_dev, value, sizeof(*value), GPIO_EXPANDER_ADDRESS);
|
||||
}
|
||||
|
||||
int gpio_expander_write_port(uint8_t value) {
|
||||
gpio_state = value;
|
||||
int gpio_expander_write_port(uint8_t value)
|
||||
{
|
||||
int ret;
|
||||
|
||||
return i2c_write(i2c_dev, &gpio_state, sizeof(gpio_state),
|
||||
GPIO_EXPANDER_ADDRESS);
|
||||
gpio_state = value;
|
||||
|
||||
ret = i2c_write(i2c_dev, &gpio_state, sizeof(gpio_state),
|
||||
GPIO_EXPANDER_ADDRESS);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool gpio_expander_read_pin(uint8_t pin) {
|
||||
@@ -96,4 +101,4 @@ int gpio_expander_update_port(uint8_t mask, uint8_t value) {
|
||||
gpio_state |= (value & mask);
|
||||
|
||||
return gpio_expander_write_port(gpio_state);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
#include "led.h"
|
||||
|
||||
#include "board_pins.h"
|
||||
#include "board_config.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
@@ -79,4 +79,4 @@ void led_set_cyan(void) { led_set_rgb(false, true, true); }
|
||||
|
||||
void led_set_magenta(void) { led_set_rgb(true, false, true); }
|
||||
|
||||
void led_set_white(void) { led_set_rgb(true, true, true); }
|
||||
void led_set_white(void) { led_set_rgb(true, true, true); }
|
||||
|
||||
+15
-2
@@ -8,10 +8,12 @@
|
||||
*
|
||||
* Current test:
|
||||
* - RGB LED
|
||||
* - Buzzer
|
||||
* - GPIO Expander
|
||||
* - 4x4 Keypad
|
||||
*/
|
||||
|
||||
#include "buzzer.h"
|
||||
#include "keypad.h"
|
||||
#include "led.h"
|
||||
|
||||
@@ -40,6 +42,14 @@ int main(void) {
|
||||
led_set_red();
|
||||
k_msleep(1000);
|
||||
|
||||
if (buzzer_init() < 0) {
|
||||
led_set_magenta();
|
||||
|
||||
while (1) {
|
||||
k_msleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
if (keypad_init() < 0) {
|
||||
led_set_magenta();
|
||||
|
||||
@@ -53,7 +63,7 @@ int main(void) {
|
||||
printf("\n");
|
||||
printf("========================================\n");
|
||||
printf("OpenParcelBox Firmware\n");
|
||||
printf("Hardware Test: RGB LED + Keypad\n");
|
||||
printf("Hardware Test: RGB LED + Buzzer + Keypad\n");
|
||||
printf("========================================\n");
|
||||
|
||||
led_set_green();
|
||||
@@ -66,6 +76,9 @@ int main(void) {
|
||||
|
||||
printf("Key pressed: %c\n", key);
|
||||
|
||||
/* keypad_get_key() returns once per press, so holding a key will not beep continuously. */
|
||||
buzzer_beep_key();
|
||||
|
||||
led_set_blue();
|
||||
|
||||
k_msleep(KEY_PRESS_LED_MS);
|
||||
@@ -77,4 +90,4 @@ int main(void) {
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
# Firmware Constraints
|
||||
|
||||
## Architecture
|
||||
|
||||
- Development is based on Zephyr RTOS.
|
||||
- The code must remain modular.
|
||||
- Business logic, hardware access, and communications must be clearly separated.
|
||||
|
||||
## Power Consumption
|
||||
|
||||
- Low-power modes should be used whenever possible.
|
||||
- Wake-up should happen only on meaningful events.
|
||||
|
||||
## Maintenance
|
||||
|
||||
- Errors should be logged or exposed through diagnostics.
|
||||
- Configuration must be documented.
|
||||
- The update procedure must be documented.
|
||||
|
||||
## Security
|
||||
|
||||
- Commands must be authenticated.
|
||||
- Sensitive data must be stored securely.
|
||||
- The firmware must remain usable without any mandatory cloud service.
|
||||
@@ -1,23 +0,0 @@
|
||||
# Contraintes firmware
|
||||
|
||||
## Architecture
|
||||
|
||||
- Développement sous Zephyr.
|
||||
- Code modulaire.
|
||||
- Séparation claire entre logique métier, matériel et communications.
|
||||
|
||||
## Consommation
|
||||
|
||||
- Exploitation maximale des modes basse consommation.
|
||||
- Réveil uniquement lors d'événements nécessaires.
|
||||
|
||||
## Maintenance
|
||||
|
||||
- Journalisation des erreurs.
|
||||
- Configuration documentée.
|
||||
- Procédure de mise à jour documentée.
|
||||
|
||||
## Sécurité
|
||||
|
||||
- Authentification des commandes.
|
||||
- Stockage sécurisé des données sensibles.
|
||||
Reference in New Issue
Block a user