LCOV - code coverage report
Current view: top level - src - ds18b20_internal.h Coverage Total Hit
Test: stm32-async-1wire core coverage Lines: 100.0 % 1 1
Test Date: 2026-11-28 11:52:24 Functions: - 0 0

            Line data    Source code
       1              : /**
       2              :  * @file ds18b20_internal.h
       3              :  * @brief Private DS18B20 driver contract (library-internal).
       4              :  *
       5              :  * Owns the driver-private types, flag bits, sizing constants and the one
       6              :  * stateless leaf helper shared across the umbrella translation unit
       7              :  * src/ds18b20.c and its four include-only parts (resolution, txn, search,
       8              :  * measure): measurement/transaction/resolution contexts, phase enums and
       9              :  * the pulse-geometry constants. Not installed, never included by consumers;
      10              :  * the core includes it before the parts, and the host test amalgamation
      11              :  * reaches it through src/ds18b20.c.
      12              :  *
      13              :  * Ownership (instances stay where they are; this header only declares types):
      14              :  *   - src/ds18b20.c: `ctx`, `txn_ctx`, `phase_pulses`, `detect_buf`,
      15              :  *     `build_addr_cmd()`, `wait_conversion()` / `resolution_wait_us()`;
      16              :  *   - src/ds18b20_resolution.c: `res_ctx`;
      17              :  *   - src/ds18b20_search.c: `dev_roms`, `dev_count`, `search_user_sink`;
      18              :  *   - src/ds18b20_measure.c: `build_skip_cmd()` (the CONVERT/REQUEST
      19              :  *     encoders share `phase_pulses.cmd`, addressed writes `phase_pulses.addr`).
      20              :  *
      21              :  * The four parts are peers sharing this state, and the phase_pulses union
      22              :  * aliasing plus the cross-phase ownership guards are only provable inside
      23              :  * one translation unit — that is why the driver stays amalgamated while
      24              :  * DS2431 (strict core/engine hierarchy) is split. The include order in
      25              :  * src/ds18b20.c is load-bearing: every part references declarations from
      26              :  * the parts included above it (txn uses res_config_byte from resolution;
      27              :  * search reads txn_ctx/res_ctx finished flags; measure reads dev_roms).
      28              :  */
      29              : 
      30              : #ifndef DS18B20_INTERNAL_H
      31              : #define DS18B20_INTERNAL_H
      32              : 
      33              : #include <stdint.h>
      34              : 
      35              : #include "ds18b20.h"
      36              : #include "ow_compiler.h"
      37              : 
      38              : /** @brief Total length of DS18B20 scratchpad in bytes */
      39              : #define DS18B20_SCRATCHPAD_LEN 9
      40              : /** @brief Number of bytes to include in the scratchpad CRC calculation */
      41              : #define DS18B20_CRC8_BYTES 8
      42              : /** @brief Total number of bits in DS18B20 scratchpad */
      43              : #define DS18B20_SCRATCHPAD_BITS (DS18B20_SCRATCHPAD_LEN * DS18B20_BITS_PER_BYTE)
      44              : /** @brief Total slots for Match ROM + 8-byte ROM + command */
      45              : #define DS18B20_MATCH_SLOTS ((DS18B20_ROM_BYTES + 2) * DS18B20_BITS_PER_BYTE)
      46              : /** @brief Slots for the invariant Match ROM + 8-byte ROM prefix (rebuilt per addressed operation) */
      47              : #define DS18B20_PREFIX_SLOTS ((DS18B20_ROM_BYTES + 1) * DS18B20_BITS_PER_BYTE)
      48              : /** @brief Number of DMA transfers for command transmission (2 bytes x 8 bits) */
      49              : #define DS18B20_DMA_TRANSFERS (2 * DS18B20_BITS_PER_BYTE)
      50              : #define SCAN_DEVICE_GAP_US 1000 /**< 1ms scheduling bridge between scan-mode device reads (no bus requirement) */
      51              : /** @brief TH byte written together with the config register by the resolution
      52              :  *         state machine (Write Scratchpad requires TH + TL + CFG in one go).
      53              :  *         0 disables the alarm trigger threshold. */
      54              : #define DS18B20_RES_TH 0x00
      55              : /** @brief TL byte written together with the config register by the resolution
      56              :  *         state machine. 0 disables the alarm trigger threshold. */
      57              : #define DS18B20_RES_TL 0x00
      58              : /** @brief Bytes in the resolution config write for Skip ROM mode
      59              :  *         (Skip ROM 0xCC + Write Scratchpad 0x4E + TH + TL + CFG) */
      60              : #define DS18B20_RES_BYTES_MIN (1 + 1 + 3)
      61              : /** @brief Bytes in the resolution config write for Match ROM mode
      62              :  *         (Match ROM 0x55 + 8-byte ROM + 0x4E + TH + TL + CFG) */
      63              : #define DS18B20_RES_BYTES_MAX (1 + DS18B20_ROM_BYTES + 1 + 3)
      64              : /** @brief Slots for the Skip ROM resolution config write */
      65              : #define DS18B20_RES_SLOTS_MIN (DS18B20_RES_BYTES_MIN * DS18B20_BITS_PER_BYTE)
      66              : /** @brief Slots for the Match ROM resolution config write */
      67              : #define DS18B20_RES_SLOTS_MAX (DS18B20_RES_BYTES_MAX * DS18B20_BITS_PER_BYTE)
      68              : /** @brief Wait for Copy Scratchpad (t_COPY) / Recall EEPROM (t_RECALL)
      69              :  *         completion in microseconds (DS18B20 datasheet: 10ms max). */
      70              : #define DS18B20_EEPROM_WAIT_US 10000
      71              : 
      72              : /**
      73              :  * @brief DS18B20 driver context structure using union for memory efficiency
      74              :  * @note Different stages of communication use the same memory for different purposes
      75              :  */
      76              : typedef struct {
      77              :     /**
      78              :      * @brief Union overlay for memory efficiency
      79              :      * @warning CRITICAL INVARIANT: scratchpad[n] aliases pulse[n] (same byte).
      80              :      *          decode_scratchpad() MUST read all 8 bits of pulse[byte*8..byte*8+7]
      81              :      *          BEFORE writing scratchpad[byte]. Reordering loops will corrupt bytes 0-8.
      82              :      */
      83              :     union {
      84              :         volatile uint16_t capture[DS18B20_SCRATCHPAD_BITS / 2]; /**< Captured pulse durations (reset/presence) */
      85              :         volatile uint8_t pulse[DS18B20_SCRATCHPAD_BITS]; /**< Pulse durations for data decoding */
      86              :         uint8_t scratchpad[DS18B20_SCRATCHPAD_LEN]; /**< Sensor scratchpad data */
      87              :     };
      88              :     uint8_t current_state; /**< Current state machine state (ds18b20_state_t values, packed from enum to save RAM) */
      89              :     uint8_t flags; /**< DS18B20_FLAG_* bits (address_mode/scan_mode/parasite) */
      90              :     uint8_t scan_index; /**< Index of the device currently read in scan mode */
      91              :     uint8_t selected_rom[DS18B20_ROM_BYTES]; /**< ROM of the selected device */
      92              :     uint8_t resolution; /**< Conversion resolution in bits (9..12); drives the conversion wait */
      93              : } DS18B20_ctx_t;
      94              : 
      95              : /** @brief Non-zero = Match ROM addressing (0 = Skip ROM broadcast) */
      96              : #define DS18B20_FLAG_ADDRESS_MODE (1u << 0)
      97              : /** @brief 1 = simultaneous multi-device conversion (scan) mode */
      98              : #define DS18B20_FLAG_SCAN_MODE (1u << 1)
      99              : /** @brief 1 = parasite-powered bus: engage the strong pull-up during conversion and EEPROM programming windows (see ds18b20_set_parasite) */
     100              : #define DS18B20_FLAG_PARASITE (1u << 2)
     101              : 
     102              : /**
     103              :  * @brief Non-blocking single-command transaction phases
     104              :  */
     105              : typedef enum {
     106              :     DS18B20_TXN_RESET, /**< reset scheduled; check presence */
     107              :     DS18B20_TXN_WRITE, /**< command (prefix + function + payload) write scheduled */
     108              :     DS18B20_TXN_READ, /**< data read scheduled (read_bytes != 0) */
     109              :     DS18B20_TXN_WAIT, /**< timed wait scheduled (wait_us != 0) */
     110              :     DS18B20_TXN_DONE /**< finished; hand the timer back to the measurement */
     111              : } ds18b20_txn_phase_t;
     112              : 
     113              : /**
     114              :  * @brief Non-blocking single-command transaction context
     115              :  * @note Drives every infrequent DS18B20 command (Read ROM, Write Scratchpad
     116              :  *       thresholds, Copy/Recall EEPROM, Read Power Supply, raw Read
     117              :  *       Scratchpad) with the same reset -> write -> (read | wait) discipline
     118              :  *       as the resolution state machine. The command build lives in the
     119              :  *       shared phase_pulses.txn workspace (see the union in src/ds18b20.c),
     120              :  *       which must stay valid across poll calls because the DMA feeds CCR3
     121              :  *       from it asynchronously.
     122              :  * @note Field order is load-bearing for RAM: out/wait_us first, then bytes,
     123              :  *       so no padding slips between the pointer and the 16-bit wait. The
     124              :  *       one-bit states share flags (explicit masks, not C bitfields — layout
     125              :  *       must stay deterministic across toolchains). There is no ISR in this
     126              :  *       architecture (everything is poll-driven), so read-modify-write on the
     127              :  *       flag bytes is safe.
     128              :  */
     129              : typedef struct {
     130              :     uint8_t* out; /**< User result buffer (valid until the command finishes) */
     131              :     uint16_t wait_us; /**< Timed wait after the command (0 = none) */
     132              :     uint8_t phase; /**< Current phase of the transaction (ds18b20_txn_phase_t values, packed) */
     133              :     uint8_t command; /**< DS18B20 function command byte (0x33/0x4E/0x48/0xB8/0xB4/0xBE) */
     134              :     uint8_t payload[3]; /**< Write Scratchpad payload (TH, TL, CFG) */
     135              :     uint8_t payload_len; /**< 0..3 (payload bytes written after the command) */
     136              :     uint8_t read_bytes; /**< Bytes to read back (0 = no read phase) */
     137              :     uint8_t slots; /**< Bit slots in the built pulses (incl. prefix and payload) */
     138              :     uint8_t raw[DS18B20_SCRATCHPAD_LEN]; /**< Decoded read result */
     139              :     uint8_t flags; /**< DS18B20_TXN_FLAG_* bits (bare/ok/finished) */
     140              : } ds18b20_txn_ctx_t;
     141              : 
     142              : /** @brief Transaction without addressing prefix (Read ROM: single-device bus only) */
     143              : #define DS18B20_TXN_FLAG_BARE (1u << 0)
     144              : /** @brief Set once the transaction completed with a device present / valid read */
     145              : #define DS18B20_TXN_FLAG_OK (1u << 1)
     146              : /** @brief Set once the transaction finished (or aborted) */
     147              : #define DS18B20_TXN_FLAG_FINISHED (1u << 2)
     148              : 
     149              : /** @brief Resolution state machine phases */
     150              : typedef enum {
     151              :     DS18B20_RES_RESET, /**< reset scheduled; check presence */
     152              :     DS18B20_RES_WRITE, /**< config write scheduled (skip/match + 0x4E + TH + TL + CFG) */
     153              :     DS18B20_RES_DONE /**< operation finished; hand the timer back to the measurement */
     154              : } res_phase_t;
     155              : 
     156              : /**
     157              :  * @brief Non-blocking resolution change context
     158              :  * @note The config write is built into the shared phase_pulses.res workspace
     159              :  *       (see the union in src/ds18b20.c), which must stay valid across poll
     160              :  *       calls because the DMA feeds CCR3 from it asynchronously while the
     161              :  *       config write is sent.
     162              :  */
     163              : typedef struct {
     164              :     uint8_t phase; /**< Current phase of the resolution state machine (res_phase_t values, packed) */
     165              :     uint8_t pending_res; /**< Resolution (bits) to apply */
     166              :     uint8_t slots; /**< Bit slots in the built config write (incl. prefix and payload) */
     167              :     uint8_t flags; /**< DS18B20_RES_FLAG_* bits (applied/finished) */
     168              : } res_ctx_t;
     169              : 
     170              : /** @brief Set once the config write completed (resolution actually changed) */
     171              : #define DS18B20_RES_FLAG_APPLIED (1u << 0)
     172              : /** @brief Set once the operation has completed (or aborted) */
     173              : #define DS18B20_RES_FLAG_FINISHED (1u << 1)
     174              : 
     175              : /**
     176              :  * @brief Build the DS18B20 configuration register byte for a resolution
     177              :  * @param[in] res Resolution in bits (9..12)
     178              :  * @return Configuration register byte (R1/R0 bits set, rest at reset value)
     179              :  * @note 9 bit -> 0x1F, 10 bit -> 0x3F, 11 bit -> 0x5F, 12 bit -> 0x7F.
     180              :  *       Stateless leaf shared by the resolution part (config builds) and the
     181              :  *       txn part (threshold writes carry the current CFG).
     182              :  */
     183              : __STATIC_FORCEINLINE uint8_t res_config_byte(uint8_t res) {
     184          859 :     return (uint8_t)(0x1Fu | ((uint8_t)(res - DS18B20_RES_MIN) << 5));
     185              : }
     186              : 
     187              : #endif /* DS18B20_INTERNAL_H */
        

Generated by: LCOV version 2.0-1