LCOV - code coverage report
Current view: top level - src - ds2431_txn.c (source / functions) Coverage Total Hit
Test: stm32-async-1wire core coverage Lines: 98.4 % 122 120
Test Date: 2026-11-28 11:52:24 Functions: 100.0 % 6 6
Branches: 94.2 % 69 65

             Branch data     Line data    Source code
       1                 :             : /**
       2                 :             :  * @file ds2431_txn.c
       3                 :             :  * @brief DS2431 non-blocking operation engine (library-internal TU).
       4                 :             :  *
       5                 :             :  * Implements the shared reset -> write -> (read | wait) engine plus the
       6                 :             :  * scratchpad helpers on the caller-owned context from
       7                 :             :  * src/ds2431_internal.h. The core (src/ds2431.c) owns the single static
       8                 :             :  * instance and the public start/poll API; this TU never names a global.
       9                 :             :  * Compiled separately and linked with the core.
      10                 :             :  */
      11                 :             : 
      12                 :             : #include "ds2431_internal.h"
      13                 :             : 
      14                 :             : /**
      15                 :             :  * @defgroup DS2431_Operation_Impl DS2431 Non-Blocking Operations
      16                 :             :  * @brief Shared non-blocking engine for every DS2431 operation. Each
      17                 :             :  *        operation runs reset -> presence -> write -> (read | timed wait) ->
      18                 :             :  *        done, one hardware-timed operation per poll call, so the same
      19                 :             :  *        non-blocking discipline as the DS18B20 driver is preserved. Each
      20                 :             :  *        operation owns TIM1/DMA while it runs and leaves the bus idle when
      21                 :             :  *        done.
      22                 :             :  * @{
      23                 :             :  */
      24                 :             : 
      25                 :             : /**
      26                 :             :  * @brief Ownership guard shared by every operation start
      27                 :             :  * @return 1 when a new operation may be scheduled
      28                 :             :  */
      29                 :         720 : uint8_t ds2431_can_start(const DS2431_ctx_t* c) {
      30   [ +  +  +  +  :        1422 :     return (uint8_t)((c->phase == DS2431_PH_IDLE || c->phase == DS2431_PH_DONE) &&
                   +  - ]
      31                 :         702 :                      !onewire_search_active());
      32                 :             : }
      33                 :             : 
      34                 :             : /**
      35                 :             :  * @brief Build the command pulse sequence into c->u.cmd and return its length
      36                 :             :  * @param[in] command DS2431 function command byte
      37                 :             :  * @return Number of bit slots in the built table (incl. prefix and payload)
      38                 :             :  * @note Encodes the addressing prefix (Skip ROM 0xCC, or Match ROM 0x55 +
      39                 :             :  *       selected ROM), the function command byte, the two target-address
      40                 :             :  *       bytes (all commands except the bare Read Scratchpad 0xAA) and the
      41                 :             :  *       optional payload. The trailing ONEWIRE_RELEASE_PULSE that the 1-Wire
      42                 :             :  *       layer consumes as the final DMA transfer into CCR3 is written at the
      43                 :             :  *       slot index of the mode actually used.
      44                 :             :  * @warning Must run only at the RESET -> CMD transition: the table shares
      45                 :             :  *          storage with the reset/read capture union (see DS2431_ctx_t), and
      46                 :             :  *          the reset DMA would have clobbered any earlier build.
      47                 :             :  */
      48                 :         648 : uint8_t ds2431_build(DS2431_ctx_t* c, uint8_t command) {
      49                 :         648 :     const uint8_t* rom = 0;
      50         [ +  + ]:         648 :     if ((c->flags & DS2431_FLAG_ADDRESS_MODE) != 0u) {
      51                 :          36 :         rom = c->selected_rom;
      52                 :             :     }
      53                 :         648 :     ow_pulse_t* p = (ow_pulse_t*)c->u.cmd;
      54         [ +  + ]:        1296 :     uint8_t bytes = (rom != 0) ? ow_encode_match_rom(&p, rom) : ow_encode_skip_rom(&p);
      55                 :         648 :     onewire_encode_byte(p, command);
      56                 :         648 :     p += DS2431_BITS_PER_BYTE;
      57                 :         648 :     bytes++;
      58         [ +  + ]:         648 :     if (command != DS2431_READ_SCRATCHPAD) {
      59                 :         468 :         onewire_encode_byte(p, c->addr_lo);
      60                 :         468 :         p += DS2431_BITS_PER_BYTE;
      61                 :         468 :         bytes++;
      62                 :         468 :         onewire_encode_byte(p, c->addr_hi);
      63                 :         468 :         p += DS2431_BITS_PER_BYTE;
      64                 :         468 :         bytes++;
      65                 :             :     }
      66         [ +  + ]:         648 :     if (command == DS2431_COPY_SCRATCHPAD) {
      67                 :             :         // Copy Scratchpad authorizes with the latched E/S byte. Encode it from
      68                 :             :         // c->es so the last write payload stays intact for the read-back
      69                 :             :         // verification (the scratchpad frame read compares against it).
      70                 :          54 :         onewire_encode_byte(p, c->es);
      71                 :          54 :         bytes++;
      72                 :             :     } else {
      73                 :        1188 :         bytes = (uint8_t)(bytes + ow_encode_bytes(&p, c->payload, c->plen));
      74                 :             :     }
      75                 :         648 :     const uint8_t slots = (uint8_t)(bytes * DS2431_BITS_PER_BYTE);
      76                 :             :     /* B1: guarantee the trailing ONEWIRE_RELEASE_PULSE that the 1-Wire layer
      77                 :             :      * reads as its final DMA transfer into CCR3, even though the command write
      78                 :             :      * only ever fills slots 0 .. slots - 1. */
      79                 :         648 :     ((ow_pulse_t*)c->u.cmd)[slots] = ONEWIRE_RELEASE_PULSE;
      80                 :         648 :     return slots;
      81                 :             : }
      82                 :             : 
      83                 :             : /**
      84                 :             :  * @brief Schedule a new non-blocking DS2431 operation
      85                 :             :  * @param[in] command DS2431 function command byte
      86                 :             :  * @param[in] out User result buffer (may be NULL; only written on success)
      87                 :             :  * @return 1 if the operation was scheduled, 0 if it was rejected (the driver
      88                 :             :  *         is busy or a device search owns the timer)
      89                 :             :  * @note Ignored unless the driver is parked (IDLE/DONE) and the device search
      90                 :             :  *       is finished. The result buffer must stay valid until the operation
      91                 :             :  *       completes (ds2431_*_poll() reports 1).
      92                 :             :  */
      93                 :         720 : uint8_t ds2431_start(DS2431_ctx_t* c, uint8_t command, uint8_t* out) {
      94         [ +  + ]:         720 :     if (!ds2431_can_start(c)) {
      95                 :          18 :         return 0; // the timer belongs to someone else right now
      96                 :             :     }
      97                 :         702 :     c->command = command;
      98                 :         702 :     c->out = out;
      99                 :             :     /* Fresh operation: clear the per-operation success/echo bits; the
     100                 :             :      * address-mode and parasite flags and the pending write-cycle state
     101                 :             :      * survive (they belong to the configuration / to the addressed device). */
     102                 :         702 :     c->flags &= (uint8_t)~(DS2431_FLAG_OK | DS2431_FLAG_FINISHED | DS2431_FLAG_ECHO);
     103                 :         702 :     onewire_strong_pullup(0);
     104                 :         702 :     c->phase = DS2431_PH_RESET;
     105                 :         702 :     onewire_reset((volatile uint16_t*)c->u.cap); // Schedule the first hardware operation
     106                 :         702 :     return 1;
     107                 :             : }
     108                 :             : 
     109                 :             : /**
     110                 :             :  * @brief Data byte count of a Read Scratchpad reply, from its own header
     111                 :             :  * @param[in] frame Decoded reply: TA1, TA2, E/S, data..
     112                 :             :  * @return Number of scratchpad data bytes that follow E/S (1 .. 8), or 0 when
     113                 :             :  *         the header is not a valid window
     114                 :             :  * @note The DS2431 datasheet Read Scratchpad [AAh] returns (E[2:0] - T[2:0] + 1)
     115                 :             :  *       data bytes, so the reply length depends on the latched end-offset and
     116                 :             :  *       the low three address bits. The driver always clocks the maximum window
     117                 :             :  *       (DS2431_SCRATCH_FRAME_CRC bytes) - the datasheet has the device drive
     118                 :             :  *       logic 1 after the CRC - and derives the real length here.
     119                 :             :  */
     120                 :         270 : uint8_t ds2431_scratchpad_data_len(const uint8_t* frame) {
     121                 :         270 :     uint8_t last = (uint8_t)((frame[2] & 0x07u) + 1u); /* E[2:0] + 1, 1 .. 8 */
     122                 :         270 :     uint8_t first = (uint8_t)(frame[0] & 0x07u); /* T[2:0],     0 .. 7 */
     123         [ +  + ]:         270 :     if (last <= first) {
     124                 :          18 :         return 0;
     125                 :             :     }
     126                 :         252 :     return (uint8_t)(last - first);
     127                 :             : }
     128                 :             : 
     129                 :             : /**
     130                 :             :  * @brief Verify the inverted CRC-16 the DS2431 appends to a Read Scratchpad reply
     131                 :             :  * @param[in] frame Decoded reply: TA1, TA2, E/S, data.., CRC_lo, CRC_hi
     132                 :             :  * @param[in] n Number of scratchpad data bytes in this reply (1 .. 8)
     133                 :             :  * @return 1 when the trailing inverted CRC-16 matches the command + frame
     134                 :             :  * @note CRC input is the command (0xAA) followed by the 3-byte header and the
     135                 :             :  *       @p n data bytes the device actually transmitted (datasheet Read
     136                 :             :  *       Scratchpad [AAh]); the device transmits the bitwise complement of the
     137                 :             :  *       Maxim CRC-16, low byte first, right after the last data byte.
     138                 :             :  */
     139                 :         162 : uint8_t ds2431_scratchpad_crc_ok(const uint8_t* frame, uint8_t n) {
     140                 :             :     uint8_t crc_in[1 + 3 + DS2431_SCRATCH_SIZE];
     141                 :         162 :     uint8_t total = (uint8_t)(1u + 3u + n);
     142                 :         162 :     crc_in[0] = DS2431_READ_SCRATCHPAD;
     143         [ +  + ]:        1674 :     for (uint8_t i = 0; i < (uint8_t)(3u + n); i++) {
     144                 :        1512 :         crc_in[i + 1] = frame[i];
     145                 :             :     }
     146                 :         162 :     uint16_t expected = (uint16_t)(~onewire_crc16(crc_in, total));
     147                 :         162 :     uint16_t got = (uint16_t)((uint16_t)frame[3u + n] |
     148                 :         162 :                               ((uint16_t)frame[4u + n] << 8));
     149                 :         162 :     return (uint8_t)(got == expected);
     150                 :             : }
     151                 :             : 
     152                 :             : /**
     153                 :             :  * @brief Advance the active DS2431 operation by one hardware operation
     154                 :             :  * @return 1 when the operation finished (successfully or aborted), 0 while
     155                 :             :  *         running
     156                 :             :  */
     157                 :        3384 : uint8_t ds2431_poll(DS2431_ctx_t* c) {
     158         [ +  + ]:        3384 :     if ((c->flags & DS2431_FLAG_FINISHED) != 0u) {
     159                 :         162 :         return 1;
     160                 :             :     }
     161                 :             : 
     162         [ +  + ]:        3222 :     if (c->phase == DS2431_PH_DONE) {
     163                 :             :         // The last hardware operation completed (command done or aborted).
     164                 :             :         // The timer is idle from here on: it is picked up again by the next
     165                 :             :         // scheduled operation, which is what keeps an operation from silently
     166                 :             :         // running into the next one.
     167                 :         702 :         c->flags |= DS2431_FLAG_FINISHED;
     168                 :         702 :         return 1;
     169                 :             :     }
     170                 :             : 
     171                 :             :     // Wait for the currently scheduled hardware operation to complete.
     172                 :             :     // This is a non-blocking poll, not a busy-wait.
     173                 :        2520 :     uint8_t op_done = ow_op_done();
     174                 :             : 
     175         [ +  + ]:        2520 :     if (!op_done) {
     176                 :         702 :         return 0;
     177                 :             :     }
     178                 :             : 
     179   [ +  +  +  +  :        1818 :     switch (c->phase) {
                      - ]
     180                 :         702 :     case DS2431_PH_RESET:
     181                 :             :         // Reset completed: a presence pulse means at least one device is on
     182                 :             :         // the bus, so build and send the command for this operation.
     183                 :             :         {
     184                 :         702 :             uint8_t present = ow_has_presence(c->u.cap);
     185         [ +  + ]:         702 :             if (!present) {
     186                 :          54 :                 c->phase = DS2431_PH_DONE;
     187                 :          54 :                 break;
     188                 :             :             }
     189                 :             :         }
     190         [ +  + ]:         648 :         if ((c->flags & DS2431_FLAG_PARASITE) != 0u) {
     191                 :          90 :             onewire_strong_pullup(1);
     192                 :             :         }
     193                 :             :         // Build the command table in-place over the consumed reset capture;
     194                 :             :         // it is safe here because the capture is no longer read (the build is
     195                 :             :         // what makes union membership possible, see DS2431_ctx_t).
     196                 :         648 :         onewire_write_slots((const ow_pulse_t*)c->u.cmd, ds2431_build(c, c->command));
     197                 :         648 :         c->phase = DS2431_PH_CMD;
     198                 :         648 :         break;
     199                 :             : 
     200                 :         648 :     case DS2431_PH_CMD:
     201                 :             :         // Command write completed. Copy Scratchpad is timed (the status byte
     202                 :             :         // is read after the t_PROG hold-off); the other commands either read
     203                 :             :         // the answer back (Read Memory, Read Scratchpad) or finish immediately
     204                 :             :         // (Write Scratchpad, whose outcome the echo read verifies later).
     205         [ +  + ]:         648 :         if (c->command == DS2431_COPY_SCRATCHPAD) {
     206                 :             :             // Parasite power: the EEPROM programming draws its supply from the
     207                 :             :             // bus while the block is written - drive HIGH actively for the
     208                 :             :             // t_PROG hold-off window.
     209                 :          54 :             ow_start_wait(DS2431_TPROG_US);
     210                 :          54 :             c->phase = DS2431_PH_WAIT;
     211         [ +  + ]:         594 :         } else if (c->len) {
     212         [ +  + ]:         360 :             if ((c->flags & DS2431_FLAG_PARASITE) != 0u) {
     213                 :          54 :                 onewire_strong_pullup(0);
     214                 :             :             }
     215                 :         360 :             onewire_read_data((volatile uint8_t*)c->u.pulse, c->len);
     216                 :         360 :             c->phase = DS2431_PH_READ;
     217                 :             :         } else {
     218         [ +  + ]:         234 :             if ((c->flags & DS2431_FLAG_PARASITE) != 0u) {
     219                 :          18 :                 onewire_strong_pullup(0);
     220                 :             :             }
     221                 :         234 :             c->flags |= DS2431_FLAG_OK;
     222                 :         234 :             c->phase = DS2431_PH_DONE;
     223                 :             :         }
     224                 :         648 :         break;
     225                 :             : 
     226                 :         414 :     case DS2431_PH_READ:
     227                 :             :         // Data read completed: decode the captured pulse durations.
     228                 :         414 :         onewire_decode_pulses(c->raw, (const volatile uint8_t*)c->u.pulse, c->len);
     229         [ +  + ]:         414 :         if (c->command == DS2431_COPY_SCRATCHPAD) {
     230                 :             :             // The device reports the copy outcome on the bus after t_PROG:
     231                 :             :             // 0xAA when the block was programmed, logic 1 when it refused
     232                 :             :             // (PF set, invalid address or copy-protected page). Only the
     233                 :             :             // completion pattern counts as success.
     234         [ +  + ]:          54 :             if (c->raw[0] == DS2431_COPY_STATUS_OK) {
     235                 :          36 :                 c->flags |= DS2431_FLAG_OK;
     236                 :             :             }
     237                 :          54 :             c->phase = DS2431_PH_DONE;
     238                 :          54 :             break;
     239                 :             :         }
     240         [ +  + ]:         360 :         if (c->command == DS2431_READ_SCRATCHPAD) {
     241                 :             :             // The reply length depends on the latched E/S window; validate the
     242                 :             :             // inverted CRC-16 over the bytes the device actually transmitted.
     243                 :         180 :             uint8_t n = ds2431_scratchpad_data_len(c->raw);
     244   [ +  +  +  + ]:         180 :             if (n == 0u || !ds2431_scratchpad_crc_ok(c->raw, n)) {
     245                 :             :                 // The reply's trailing inverted CRC-16 does not cover the
     246                 :             :                 // frame: the scratchpad read is corrupt. Finish without the
     247                 :             :                 // success flag.
     248                 :          36 :                 c->phase = DS2431_PH_DONE;
     249                 :          36 :                 break;
     250                 :             :             }
     251         [ +  + ]:         144 :             if ((c->flags & (DS2431_FLAG_ECHO | DS2431_FLAG_PENDING)) ==
     252                 :             :                 (DS2431_FLAG_ECHO | DS2431_FLAG_PENDING)) {
     253   [ +  -  +  - ]:         108 :                 if (c->raw[0] != c->addr_lo || c->raw[1] != c->addr_hi ||
     254         [ +  + ]:         108 :                     (c->raw[2] & 0x07u) != c->es) {
     255                 :             :                     // The scratchpad frame echoes neither the latched target
     256                 :             :                     // address nor the expected end-offset status byte: the write
     257                 :             :                     // did not reach this device. Finish without the success flag.
     258                 :          18 :                     c->phase = DS2431_PH_DONE;
     259                 :          18 :                     break;
     260                 :             :                 }
     261         [ +  + ]:          90 :                 if ((c->raw[2] & 0x20u) != 0u) {
     262                 :             :                     // PF: the device received fewer bytes than needed to fill
     263                 :             :                     // the scratchpad (or lost power), so the latched data is
     264                 :             :                     // not valid and Copy Scratchpad would refuse it anyway.
     265                 :          18 :                     c->phase = DS2431_PH_DONE;
     266                 :          18 :                     break;
     267                 :             :                 }
     268                 :             :                 // The reply CRC-16 is computed by the device over what it stored,
     269                 :             :                 // so a byte corrupted on the way to the device passes it. Compare
     270                 :             :                 // the read-back data with the payload that was sent. Writes are
     271                 :             :                 // 8-byte aligned, so the payload starts at scratchpad byte 0 and
     272                 :             :                 // ES is the index of the last written byte.
     273                 :          72 :                 uint8_t i = 0;
     274   [ +  +  +  + ]:         540 :                 while (i < n && c->raw[3u + i] == c->payload[i]) {
     275                 :         468 :                     i++;
     276                 :             :                 }
     277         [ +  + ]:          72 :                 if (i < n) {
     278                 :             :                     // A latched byte differs from the payload: the write was
     279                 :             :                     // corrupted in flight. Finish without the success flag so
     280                 :             :                     // Copy Scratchpad is not authorized from this frame.
     281                 :          18 :                     c->phase = DS2431_PH_DONE;
     282                 :          18 :                     break;
     283                 :             :                 }
     284                 :             :             }
     285                 :             :         }
     286                 :         270 :         c->flags |= DS2431_FLAG_OK;
     287                 :         270 :         c->phase = DS2431_PH_DONE;
     288                 :         270 :         break;
     289                 :             : 
     290                 :          54 :     case DS2431_PH_WAIT:
     291                 :             :         // Hold-off completed (Copy Scratchpad): release the strong pull-up
     292                 :             :         // unconditionally (idempotent) so a parasite flag cleared mid-window
     293                 :             :         // cannot leave the bus actively driven, then sample the device's copy
     294                 :             :         // completion pattern (c->len bytes, one for Copy Scratchpad).
     295                 :          54 :         onewire_strong_pullup(0);
     296                 :          54 :         onewire_read_data((volatile uint8_t*)c->u.pulse, c->len);
     297                 :          54 :         c->phase = DS2431_PH_READ;
     298                 :          54 :         break;
     299                 :             : 
     300                 :           0 :     case DS2431_PH_DONE:
     301                 :             :     case DS2431_PH_IDLE:
     302                 :             :         /* DONE/IDLE: not a running phase. DONE is handled by the finishing
     303                 :             :          * branch above; IDLE only exists between init and the first operation
     304                 :             :          * (its FINISHED bit is set from init, so the top guard returns 1). */
     305                 :             :     default:
     306                 :           0 :         break;
     307                 :             :     }
     308                 :             : 
     309                 :        1818 :     return 0;
     310                 :             : }
     311                 :             : 
     312                 :             : /**
     313                 :             :  * @}
     314                 :             :  */
        

Generated by: LCOV version 2.0-1