LCOV - code coverage report
Current view: top level - src - ds2431.c (source / functions) Coverage Total Hit
Test: stm32-async-1wire core coverage Lines: 100.0 % 86 86
Test Date: 2026-11-28 11:52:24 Functions: 100.0 % 13 13
Branches: 90.0 % 60 54

             Branch data     Line data    Source code
       1                 :             : #include "ds2431.h"
       2                 :             : #include "ds2431_internal.h"
       3                 :             : #include "onewire.h"
       4                 :             : /* The driver schedules the EEPROM hold-off window through the engine; that
       5                 :             :  * bridge is library-internal. See src/ow_glue.h. */
       6                 :             : #include "ow_glue.h"
       7                 :             : 
       8                 :             : /* Private sizing, phases, context layout and flags live in
       9                 :             :  * src/ds2431_internal.h shared with the engine TU. */
      10                 :             : 
      11                 :             : /**
      12                 :             :  * @defgroup DS2431_Private_Variables DS2431 Private Variables
      13                 :             :  * @{
      14                 :             :  */
      15                 :             : 
      16                 :             : /** @brief Global driver context instance */
      17                 :             : static DS2431_ctx_t ctx;
      18                 :             : 
      19                 :             : /* Phase packing guard: ds2431_phase_t values are stored in a uint8_t field. */
      20                 :             : _Static_assert(DS2431_PH_IDLE <= 255, "phase enum must fit uint8_t packing");
      21                 :             : 
      22                 :             : /* RCR guards: every DS2431 pass must fit one 8-bit RCR window (256 slots) and
      23                 :             :  * one read pass must fit the layer's 32-byte read limit. */
      24                 :             : _Static_assert(DS2431_CMD_SLOTS <= ONEWIRE_MAX_SLOTS,
      25                 :             :                "longest DS2431 command write must fit one RCR window");
      26                 :             : _Static_assert(DS2431_FRAME_SLOTS <= ONEWIRE_MAX_SLOTS,
      27                 :             :                "scratchpad frame read must fit one RCR window");
      28                 :             : _Static_assert(DS2431_READ_PASS <= ONEWIRE_MAX_READ_BYTES,
      29                 :             :                "a Read Memory pass must fit one read window");
      30                 :             : _Static_assert(DS2431_SCRATCH_FRAME_CRC <= ONEWIRE_MAX_READ_BYTES,
      31                 :             :                "a scratchpad frame read (incl. CRC) must fit one read window");
      32                 :             : /* B1: the trailing ONEWIRE_RELEASE_PULSE consumed by the CCR3-feed DMA's final
      33                 :             :  * transfer must always be present. Every build writes it at [slots], and slots
      34                 :             :  * never exceeds DS2431_CMD_SLOTS, so [DS2431_CMD_SLOTS] is always in range. */
      35                 :             : 
      36                 :             : /**
      37                 :             :  * @}
      38                 :             :  */
      39                 :             : 
      40                 :             : /**
      41                 :             :  * @defgroup DS2431_Public_Functions DS2431 Public Functions
      42                 :             :  * @{
      43                 :             :  */
      44                 :             : 
      45                 :             : /**
      46                 :             :  * @brief Initialize DS2431 driver - configure clocks and peripherals
      47                 :             :  * @pre Call exactly once after MCU reset, after the system clock is configured
      48                 :             :  *      and before any 1-Wire bus operation or DS2431 driver activity.
      49                 :             :  * @note Calls onewire_init(), which takes exclusive ownership of the shared
      50                 :             :  *       TIM1/DMA/GPIO resources for the lifetime of the driver (until reset),
      51                 :             :  *       and parks the driver at DS2431_PH_IDLE: nothing runs until the
      52                 :             :  *       application schedules a Read Memory, a Write Scratchpad cycle or a
      53                 :             :  *       Copy Scratchpad. The DS18B20, DS2431 and DS2413 drivers share that bus
      54                 :             :  *       ownership, so all three inits may run before the first bus operation.
      55                 :             :  * @warning This function is a bootstrap operation, not a reinitialization hook.
      56                 :             :  *          A second call before any bus operation re-applies the idle setup;
      57                 :             :  *          during activity it does not stop or fully reset the timer, DMA,
      58                 :             :  *          low-power or DS2431 software state.
      59                 :             :  */
      60                 :         630 : void ds2431_init(void) {
      61                 :         630 :     onewire_init();
      62                 :             :     /* No operation running after init; a poll without a scheduled op must
      63                 :             :      * still report "finished" (the FINISHED flag is set like the DS18B20
      64                 :             :      * transaction engine does). External power is the default wiring
      65                 :             :      * assumption; parasite-powered setups opt in via ds2431_set_parasite().
      66                 :             :      * The flag byte starts clear (address/parasite/ok/finished/pending/echo
      67                 :             :      * all zero) before FINISHED is raised. */
      68                 :         630 :     ctx.flags = DS2431_FLAG_FINISHED;
      69                 :         630 :     ctx.phase = DS2431_PH_IDLE;
      70                 :         630 :     ctx.command = 0;
      71                 :         630 :     ctx.len = 0;
      72                 :         630 :     ctx.plen = 0;
      73                 :         630 :     ctx.es = 0;
      74                 :         630 : }
      75                 :             : 
      76                 :             : /**
      77                 :             :  * @brief Select which DS2431 device to address by its ROM address
      78                 :             :  * @param[in] rom Pointer to the 8-byte ROM address (LSB first), or NULL to
      79                 :             :  *                return to Skip ROM (broadcast) addressing
      80                 :             :  * @note Accepted only while the driver is idle (nothing owns the timer);
      81                 :             :  *       mid-operation calls are ignored. Changing the selection clears the
      82                 :             :  *       pending write-cycle state (the latched TA/ES belongs to the addressed
      83                 :             :  *       device), so a select() must not be interleaved into a running
      84                 :             :  *       write_scratchpad -> read_scratchpad -> copy_scratchpad cycle.
      85                 :             :  */
      86                 :         108 : void ds2431_select(const uint8_t* rom) {
      87   [ +  +  +  - ]:         108 :     if (ctx.phase != DS2431_PH_IDLE && ctx.phase != DS2431_PH_DONE) {
      88                 :          18 :         return;
      89                 :             :     }
      90         [ +  + ]:          90 :     if (onewire_search_active()) {
      91                 :             :         // The device search owns the timer - reject to keep its addressing.
      92                 :          18 :         return;
      93                 :             :     }
      94         [ +  + ]:          72 :     if (rom == 0) {
      95                 :          18 :         ctx.flags &= (uint8_t)~(DS2431_FLAG_ADDRESS_MODE | DS2431_FLAG_PENDING);
      96                 :          18 :         return;
      97                 :             :     }
      98         [ +  + ]:         486 :     for (uint8_t i = 0; i < DS2431_ROM_BYTES; i++) {
      99                 :         432 :         ctx.selected_rom[i] = rom[i];
     100                 :             :     }
     101                 :             :     /* The Match ROM table itself is rebuilt from selected_rom on every
     102                 :             :      * operation (ds2431_build), so select only stores the ROM. */
     103                 :          54 :     ctx.flags |= DS2431_FLAG_ADDRESS_MODE;
     104                 :          54 :     ctx.flags &= (uint8_t)~DS2431_FLAG_PENDING;
     105                 :             : }
     106                 :             : 
     107                 :             : /**
     108                 :             :  * @}
     109                 :             :  */
     110                 :             : 
     111                 :             : /* Engine implementation lives in src/ds2431_txn.c and operates on the
     112                 :             :  * core-owned ctx via src/ds2431_internal.h. */
     113                 :             : 
     114                 :             : /**
     115                 :             :  * @brief Read up to DS2431_READ_PASS bytes of EEPROM
     116                 :             :  * @param[in,out] dst Buffer for the read data; written on success
     117                 :             :  * @param[in] addr Start address in EEPROM (0 .. DS2431_EEPROM_SIZE - 1)
     118                 :             :  * @param[in] len Bytes to read, 1 .. DS2431_READ_PASS
     119                 :             :  * @note Out-of-range requests are ignored.
     120                 :             :  */
     121                 :         252 : void ds2431_read_memory(uint8_t* dst, uint16_t addr, uint8_t len) {
     122         [ +  + ]:         252 :     if (dst == 0) {
     123                 :          18 :         return;
     124                 :             :     }
     125   [ +  -  +  +  :         234 :     if (len < 1u || len > DS2431_READ_PASS || addr >= DS2431_EEPROM_SIZE ||
                   +  - ]
     126         [ +  + ]:         216 :         (uint16_t)addr + len > DS2431_EEPROM_SIZE) {
     127                 :          36 :         return;
     128                 :             :     }
     129                 :         198 :     ctx.addr_lo = (uint8_t)(addr & 0xffu);
     130                 :         198 :     ctx.addr_hi = (uint8_t)(addr >> 8);
     131                 :         198 :     ctx.len = len;
     132                 :         198 :     ctx.plen = 0;
     133                 :         198 :     ds2431_start(&ctx, DS2431_READ_MEMORY, dst);
     134                 :             : }
     135                 :             : 
     136                 :             : /**
     137                 :             :  * @brief Advance the non-blocking Read Memory operation
     138                 :             :  * @return 1 when finished (successfully or aborted), 0 while running
     139                 :             :  */
     140                 :        1026 : uint8_t ds2431_read_memory_poll(void) {
     141         [ +  + ]:        1026 :     if (!ds2431_poll(&ctx)) {
     142                 :         756 :         return 0;
     143                 :             :     }
     144   [ +  +  +  - ]:         270 :     if (((ctx.flags & DS2431_FLAG_OK) != 0u) && ctx.out) {
     145         [ +  + ]:         918 :         for (uint8_t i = 0; i < ctx.len; i++) {
     146                 :         738 :             ctx.out[i] = ctx.raw[i];
     147                 :             :         }
     148                 :             :     }
     149                 :         270 :     return 1;
     150                 :             : }
     151                 :             : 
     152                 :             : /**
     153                 :             :  * @brief Write up to 8 bytes into the scratchpad
     154                 :             :  * @param[in] addr Block-aligned target address in EEPROM
     155                 :             :  * @param[in] data Data to write to the scratchpad
     156                 :             :  * @param[in] len Bytes to write, 1 .. DS2431_SCRATCH_SIZE
     157                 :             :  * @return 1 if the write was scheduled, 0 if rejected (NULL data, len out of
     158                 :             :  *         range, addr + len beyond the EEPROM, addr not 8-byte aligned, or
     159                 :             :  *         the driver is busy / a search owns the timer)
     160                 :             :  * @note A misaligned addr is rejected: the EEPROM programs in 8-byte blocks.
     161                 :             :  */
     162                 :         396 : uint8_t ds2431_write_scratchpad(uint16_t addr, const uint8_t* data, uint8_t len) {
     163         [ +  + ]:         396 :     if (data == 0) {
     164                 :          18 :         return 0;
     165                 :             :     }
     166   [ +  +  +  +  :         378 :     if (len < 1u || len > DS2431_SCRATCH_SIZE || addr >= DS2431_EEPROM_SIZE ||
                   +  - ]
     167   [ +  +  +  + ]:         324 :         (uint16_t)addr + len > DS2431_EEPROM_SIZE || (addr & 0x07u) != 0u) {
     168                 :         108 :         return 0;
     169                 :             :     }
     170                 :             :     /* Schedule first: on a busy/search reject nothing is latched, so a
     171                 :             :      * previous write cycle's pending TA/ES is never silently replaced by a
     172                 :             :      * write that never ran. */
     173         [ +  + ]:         270 :     if (!ds2431_start(&ctx, DS2431_WRITE_SCRATCHPAD, 0)) {
     174                 :          18 :         return 0;
     175                 :             :     }
     176                 :         252 :     ctx.addr_lo = (uint8_t)(addr & 0xffu);
     177                 :         252 :     ctx.addr_hi = (uint8_t)(addr >> 8);
     178                 :             :     /* ES = last byte index of the block touched by the write, in the 8-byte
     179                 :             :      * scratchpad window: the device echoes it in Read Scratchpad and requires
     180                 :             :      * it in Copy Scratchpad. */
     181                 :         252 :     ctx.es = (uint8_t)((addr + len - 1u) & 0x07u);
     182                 :         252 :     ctx.len = 0; // no read phase
     183                 :         252 :     ctx.plen = len;
     184         [ +  + ]:        1998 :     for (uint8_t i = 0; i < len; i++) {
     185                 :        1746 :         ctx.payload[i] = data[i];
     186                 :             :     }
     187                 :         252 :     ctx.flags |= DS2431_FLAG_PENDING;
     188                 :         252 :     return 1;
     189                 :             : }
     190                 :             : 
     191                 :             : /**
     192                 :             :  * @brief Advance the non-blocking Write Scratchpad operation
     193                 :             :  * @return 1 when finished (successfully or aborted), 0 while running
     194                 :             :  */
     195                 :        1044 : uint8_t ds2431_write_scratchpad_poll(void) { return ds2431_poll(&ctx); }
     196                 :             : 
     197                 :             : /**
     198                 :             :  * @brief Read the scratchpad frame (TA1, TA2, ES, data..8)
     199                 :             :  * @param[in,out] buf Buffer for the frame; written on success
     200                 :             :  * @note The driver always clocks the maximum wire reply
     201                 :             :  *       (DS2431_SCRATCH_FRAME_CRC bytes, incl. the inverted CRC-16) and
     202                 :             :  *       verifies the CRC over the (E[2:0] - T[2:0] + 1) bytes the device
     203                 :             :  *       actually transmitted; the valid window is copied to @p buf and the
     204                 :             :  *       remaining bytes up to DS2431_SCRATCH_FRAME are zeroed.
     205                 :             :  */
     206                 :         198 : void ds2431_read_scratchpad(uint8_t* buf) {
     207         [ +  + ]:         198 :     if (buf == 0) {
     208                 :          18 :         return;
     209                 :             :     }
     210                 :         180 :     ctx.len = DS2431_SCRATCH_FRAME_CRC;
     211                 :         180 :     ctx.plen = 0;
     212                 :         180 :     ds2431_start(&ctx, DS2431_READ_SCRATCHPAD, buf);
     213                 :         180 :     ctx.flags |= DS2431_FLAG_ECHO;
     214                 :             : }
     215                 :             : 
     216                 :             : /**
     217                 :             :  * @brief Advance the non-blocking scratchpad frame read
     218                 :             :  * @return 1 when finished (successfully or aborted), 0 while running
     219                 :             :  */
     220                 :         918 : uint8_t ds2431_read_scratchpad_poll(void) {
     221         [ +  + ]:         918 :     if (!ds2431_poll(&ctx)) {
     222                 :         720 :         return 0;
     223                 :             :     }
     224   [ +  +  +  - ]:         198 :     if (((ctx.flags & DS2431_FLAG_OK) != 0u) && ctx.out) {
     225                 :             :         // The reply carried (E[2:0] - T[2:0] + 1) data bytes; hand the caller
     226                 :             :         // exactly that window and zero the unused tail so the buffer is fully
     227                 :             :         // defined regardless of the latched end-offset.
     228                 :          90 :         uint8_t valid = (uint8_t)(3u + ds2431_scratchpad_data_len(ctx.raw));
     229         [ +  + ]:        1080 :         for (uint8_t i = 0; i < DS2431_SCRATCH_FRAME; i++) {
     230         [ +  + ]:         990 :             ctx.out[i] = (uint8_t)(i < valid ? ctx.raw[i] : 0u);
     231                 :             :         }
     232                 :             :     }
     233                 :         198 :     return 1;
     234                 :             : }
     235                 :             : 
     236                 :             : /**
     237                 :             :  * @brief Copy the scratchpad into the EEPROM
     238                 :             :  * @note Programs the EEPROM block selected by the last Write Scratchpad
     239                 :             :  *       (target address + ES) and verifies the device's completion pattern
     240                 :             :  *       (0xAA after t_PROG); logic 1 means the device refused the copy (PF
     241                 :             :  *       set, invalid address or copy-protected page) and reports failure.
     242                 :             :  *       Ignored unless a Write Scratchpad completed since the last
     243                 :             :  *       reset/select.
     244                 :             :  */
     245                 :          90 : void ds2431_copy_scratchpad(void) {
     246         [ +  + ]:          90 :     if ((ctx.flags & DS2431_FLAG_PENDING) == 0u) {
     247                 :          18 :         return;
     248                 :             :     }
     249                 :          72 :     ctx.len = 1; // read the copy completion pattern after the hold-off
     250                 :          72 :     ctx.plen = 0; // ES comes from ctx.es; the write payload stays for the echo check
     251                 :          72 :     ds2431_start(&ctx, DS2431_COPY_SCRATCHPAD, 0);
     252                 :             : }
     253                 :             : 
     254                 :             : /**
     255                 :             :  * @brief Advance the non-blocking Copy Scratchpad operation
     256                 :             :  * @return 1 when finished (successfully or aborted), 0 while running
     257                 :             :  */
     258                 :         396 : uint8_t ds2431_copy_scratchpad_poll(void) { return ds2431_poll(&ctx); }
     259                 :             : 
     260                 :             : /**
     261                 :             :  * @brief Declare the bus as parasite-powered
     262                 :             :  * @param[in] parasite 1 = devices are powered over the data line, 0 =
     263                 :             :  *                     external VDD supply (default)
     264                 :             :  */
     265                 :          54 : void ds2431_set_parasite(uint8_t parasite) {
     266         [ +  + ]:          54 :     if (parasite != 0u) {
     267                 :          36 :         ctx.flags |= DS2431_FLAG_PARASITE;
     268                 :             :     } else {
     269                 :          18 :         ctx.flags &= (uint8_t)~DS2431_FLAG_PARASITE;
     270                 :             :     }
     271                 :          54 : }
     272                 :             : 
     273                 :             : /**
     274                 :             :  * @brief Current parasite-power configuration of the driver
     275                 :             :  * @return 1 when the strong pull-up will be engaged during EEPROM programming
     276                 :             :  */
     277                 :          54 : uint8_t ds2431_parasite_mode(void) { return (uint8_t)((ctx.flags & DS2431_FLAG_PARASITE) != 0u); }
     278                 :             : 
     279                 :             : /**
     280                 :             :  * @brief Result of the last completed DS2431 operation
     281                 :             :  * @return 1 when the last ds2431_*_poll() finished an operation that found a
     282                 :             :  *         device present (and, for reads, verified its data - including the
     283                 :             :  *         variable-length scratchpad reply and the Write Scratchpad payload
     284                 :             :  *         read back from the scratchpad - and, for Copy Scratchpad, verified
     285                 :             :  *         the device's completion pattern), 0 when it aborted or nothing ran
     286                 :             :  *         yet
     287                 :             :  */
     288                 :         648 : uint8_t ds2431_last_command_ok(void) { return (uint8_t)((ctx.flags & DS2431_FLAG_OK) != 0u); }
     289                 :             : 
     290                 :             : /**
     291                 :             :  * @}
     292                 :             :  */
        

Generated by: LCOV version 2.0-1