# sensirion/smartgadget-firmware architecture

> Architecture diagram of the GitHub repository sensirion/smartgadget-firmware, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sensirion/smartgadget-firmware
- Repository: https://github.com/sensirion/smartgadget-firmware

## Overview

##### Project type and purpose
- This is an embedded‐firmware project composed of two independent modules:
– BLE_Module_nRF51822: firmware for the Sensirion Smart Gadget’s nRF51822 BLE SoC. It reads temperature/humidity from an SHT31 sensor, logs data, and exposes it over BLE GATT.
– LCD_Driver_MC9S08LL8: firmware for a Freescale MC9S08LL8 microcontroller that drives the gadget’s LCD display.

##### File structure
- BLE_Module_nRF51822
– BLE_API (common + public): wrapper classes around SoftDevice APIs (Gap, GattService, UUID, etc.)
– mbed + TARGET_NRF51822: mbed OS HAL layer and Nordic SDK / SoftDevice integration
– nRF51822: low‐level BLE stack glue code (btle/*.cpp, Nordic SDK files)
– source/: application code (main.cpp, sensor drivers, FlashCmdQueue, RingBuffer, ServiceDataHandler) and GATT service implementations (TemperatureService, HumidityService, BatteryService, DeviceInformationService, LoggerService)
– project files: Keil uVision project and mbed build scripts
- LCD_Driver_MC9S08LL8
– Project_Settings, .project, .cproject: CodeWarrior IDE setup
– Sources/: LCD.c/h, SPI.c/h, startup and system code, main.c
– bin/, cmd/, prm/: generated binaries, flash scripts, parameter files

##### Key insights from README
– The gadget integrates an SHT31 humidity & temperature sensor, data logging, BLE comms to smartphone, and an LCD display.
– Toolchains: Keil µVision (nRF51822) and Freescale CodeWarrior (MC9S08LL8).

##### Elements
– Firmware stacks (SoftDevice, mbed, BLE_API) in another (blue)
– Application modules (services, logging) in a third (green)
– External entities (smartphone) in a fourth (orange)
– Rectangles for components

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Hardware Layer
    subgraph "Hardware" 
        direction TB
        SHT31["SHT31 Sensor"]:::hardware
        nRF["nRF51822 SoC"]:::hardware
        MC9S08["MC9S08LL8 MCU"]:::hardware
        LCDPanel["LCD Panel"]:::hardware
        Power["Power Management"]:::hardware
    end

    SHT31 -->|I2C| nRF
    MC9S08 -->|SPI| LCDPanel

    %% Flash Storage
    FlashStorage[(Flash Storage)]:::storage
    App -->|Store Data| FlashStorage
    FlashStorage -->|Read Data| App

    %% BLE Firmware Stack
    subgraph "BLE_Module_nRF51822" 
        direction TB
        subgraph "SoftDevice & HAL" 
            direction TB
            SoftDevice["Nordic SoftDevice S110"]:::stack
            mbedHAL["mbed OS HAL"]:::stack
        end

        subgraph "BLE_API Wrapper" 
            direction TB
            BLEAPI_common["Common Implementation"]:::stack
            BLEAPI_public["Public Headers"]:::stack
        end

        subgraph "nRF51822 Glue Code" 
            direction TB
            Glue_btle["btle Glue & GAP/GATT"]:::stack
            Glue_common["Common Utilities"]:::stack
            Glue_nordic["Nordic SDK Sources"]:::stack
        end

        subgraph "Application Layer" 
            direction TB
            subgraph "Drivers & Utils"
                direction TB
                SHT31drv["SHT31 Driver"]:::app
                FlashQ["FlashCmdQueue"]:::app
                RingBuf["RingBuffer"]:::app
                ServData["ServiceDataHandler"]:::app
                LoggerUtil["Logger Utilities"]:::app
            end
            subgraph "GATT Services"
                direction TB
                TempService["TemperatureService"]:::app
                HumService["HumidityService"]:::app
                BattService["BatteryService"]:::app
                DevInfoService["DeviceInformationService"]:::app
                LogService["LoggerService"]:::app
                IService["ISensorValueServiceLogger"]:::app
            end
            MainApp["main.cpp"]:::app
        end
    end

    %% Firmware relationships
    nRF --> SoftDevice
    SoftDevice --> mbedHAL
    mbedHAL --> BLEAPI_common
    mbedHAL --> BLEAPI_public
    BLEAPI_common --> Glue_btle
    BLEAPI_public --> Glue_btle
    Glue_btle --> Glue_common
    Glue_common --> Glue_nordic
    Glue_nordic --> MainApp
    MainApp --> SHT31drv
    MainApp --> FlashQ
    MainApp --> RingBuf
    MainApp --> ServData
    MainApp --> LoggerUtil
    MainApp --> TempService
    MainApp --> HumService
    MainApp --> BattService
    MainApp --> DevInfoService
    MainApp --> LogService
    MainApp --> IService
    MainApp --> SoftDevice -->|GATT| Smartphone

    %% Optional inter-MCU comms
    nRF -->|UART/GPIO| MC9S08

    %% External Smartphone
    Smartphone["BLE Smartphone App"]:::external
    SoftDevice -->|RF Link| Smartphone

    %% LCD Firmware Module
    subgraph "LCD_Driver_MC9S08LL8"
        direction TB
        CodeWarriorProj["CodeWarrior Project"]:::devtools
        subgraph "Sources"
            direction TB
            LCDsrc["LCD.c/h"]:::app
            SPIsrc["SPI.c/h"]:::app
            Startup["Startup & System Code"]:::app
            MainLCD["main.c"]:::app
        end
        binFiles["Generated Binaries"]:::output
        FlashScripts["Flash Scripts"]:::output
        ParamFiles["Parameter Files"]:::output
    end

    MC9S08 --> CodeWarriorProj
    CodeWarriorProj --> LCDsrc
    CodeWarriorProj --> SPIsrc
    CodeWarriorProj --> Startup
    CodeWarriorProj --> MainLCD
    CodeWarriorProj --> binFiles
    CodeWarriorProj --> FlashScripts
    CodeWarriorProj --> ParamFiles

    %% Toolchain Deployment
    SoftDevice -->|Keil µVision| HexOutput[".hex Output"]:::output
    CodeWarriorProj -->|CodeWarrior| binFiles

    %% Click Events
    click BLE_Module_nRF51822/ "https://github.com/sensirion/smartgadget-firmware/tree/master/BLE_Module_nRF51822/"
    click BLEAPI_common "https://github.com/sensirion/smartgadget-firmware/tree/master/BLE_Module_nRF51822/BLE_API/common/"
    click BLEAPI_public "https://github.com/sensirion/smartgadget-firmware/tree/master/BLE_Module_nRF51822/BLE_API/public/"
    click mbedHAL "https://github.com/sensirion/smartgadget-firmware/tree/master/BLE_Module_nRF51822/mbed/"
    click Glue_btle "https://github.com/sensirion/smartgadget-firmware/tree/master/BLE_Module_nRF51822/nRF51822/btle/"
    click Glue_common "https://github.com/sensirion/smartgadget-firmware/tree/master/BLE_Module_nRF51822/nRF51822/common/"
    click Glue_nordic "https://github.com/sensirion/smartgadget-firmware/tree/master/BLE_Module_nRF51822/nRF51822/nordic/"
    click SHT31drv "https://github.com/sensirion/smartgadget-firmware/blob/master/BLE_Module_nRF51822/source/Sht31.cpp"
    click FlashQ "https://github.com/sensirion/smartgadget-firmware/blob/master/BLE_Module_nRF51822/source/FlashCmdQueue.cpp"
    click RingBuf "https://github.com/sensirion/smartgadget-firmware/blob/master/BLE_Module_nRF51822/source/RingBuffer.cpp"
    click ServData "https://github.com/sensirion/smartgadget-firmware/blob/master/BLE_Module_nRF51822/source/ServiceDataHandler.cpp"
    click LoggerUtil "https://github.com/sensirion/smartgadget-firmware/blob/master/BLE_Module_nRF51822/source/Logger.cpp"
    click MainApp "https://github.com/sensirion/smartgadget-firmware/blob/master/BLE_Module_nRF51822/source/main.cpp"
    click TempService "https://github.com/sensirion/smartgadget-firmware/blob/master/BLE_Module_nRF51822/source/services/TemperatureService.cpp"
    click HumService "https://github.com/sensirion/smartgadget-firmware/blob/master/BLE_Module_nRF51822/source/services/HumidityService.cpp"
    click BattService "https://github.com/sensirion/smartgadget-firmware/blob/master/BLE_Module_nRF51822/source/services/BatteryService.h"
    click DevInfoService "https://github.com/sensirion/smartgadget-firmware/blob/master/BLE_Module_nRF51822/source/services/DeviceInformationService.h"
    click LogService "https://github.com/sensirion/smartgadget-firmware/blob/master/BLE_Module_nRF51822/source/services/LoggerService.cpp"
    click IService "https://github.com/sensirion/smartgadget-firmware/blob/master/BLE_Module_nRF51822/source/services/ISensorValueServiceLogger.h"
    click cell SoftDevice "BLE_Module_nRF51822/s110_nrf51822_7.1.0_softdevice.hex"
    click CodeWarriorProj "https://github.com/sensirion/smartgadget-firmware/tree/master/LCD_Driver_MC9S08LL8/"
    click LCDsrc "https://github.com/sensirion/smartgadget-firmware/blob/master/LCD_Driver_MC9S08LL8/Sources/LCD.c"
    click SPIsrc "https://github.com/sensirion/smartgadget-firmware/blob/master/LCD_Driver_MC9S08LL8/Sources/SPI.c"
    click Startup "https://github.com/sensirion/smartgadget-firmware/blob/master/LCD_Driver_MC9S08LL8/Sources/Start08.c"
    click MainLCD "https://github.com/sensirion/smartgadget-firmware/blob/master/LCD_Driver_MC9S08LL8/Sources/main.c"
    click binFiles "https://github.com/sensirion/smartgadget-firmware/tree/master/LCD_Driver_MC9S08LL8/bin/"
    click FlashScripts "https://github.com/sensirion/smartgadget-firmware/tree/master/LCD_Driver_MC9S08LL8/cmd/"
    click ParamFiles "https://github.com/sensirion/smartgadget-firmware/tree/master/LCD_Driver_MC9S08LL8/prm/"

    %% Styles
    classDef hardware fill:#dedede,stroke:#333,stroke-width:1px
    classDef stack fill:#bbdefb,stroke:#1e88e5,stroke-width:1px
    classDef app fill:#c8e6c9,stroke:#43a047,stroke-width:1px
    classDef storage fill:#ffe082,stroke:#ffb300,stroke-width:1px
    classDef external fill:#ffccbc,stroke:#e64a19,stroke-width:1px
    classDef devtools fill:#d1c4e9,stroke:#673ab7,stroke-width:1px
    classDef output fill:#f0f4c3,stroke:#afb42b,stroke-width:1px
```
