LCOV - code coverage report
Current view: top level - src - ds2413.c (source / functions) Coverage Total Hit
Test: stm32-async-1wire core coverage Lines: 94.7 % 132 125
Test Date: 2026-11-28 11:52:24 Functions: 93.3 % 15 14
Branches: 84.9 % 53 45

             Branch data     Line data    Source code
       1                 :             : /**
       2                 :             :  * @file ds2413.c
       3                 :             :  * @brief Non-blocking DS2413 dual-channel addressable switch driver
       4                 :             :  *
       5                 :             :  * One operation is reset -> presence -> command write -> one-byte read ->
       6                 :             :  * done. The command table is built only after the reset capture has been
       7                 :             :  * consumed, so it can share storage with it, exactly like the DS2431 driver.
       8                 :             :  */
       9                 :             : 
      10                 :             : #include "ds2413.h"
      11                 :             : #include "onewire.h"
      12                 :             : #include "ow_glue.h"
      13                 :             : 
      14                 :             : /* Longest command write: Match ROM prefix (1 + 8) + command + state +
      15                 :             :  * complement = 12 bytes. The read command is shorter. */
      16                 :             : #define DS2413_CMD_MAX_BYTES ((1u + DS2413_ROM_BYTES) + 3u)
      17                 :             : #define DS2413_CMD_SLOTS (DS2413_CMD_MAX_BYTES * ONEWIRE_BITS_PER_BYTE)
      18                 :             : #define DS2413_READ_BYTES 1u
      19                 :             : #define DS2413_READ_SLOTS (DS2413_READ_BYTES * ONEWIRE_BITS_PER_BYTE)
      20                 :             : 
      21                 :             : /** @brief Operation phases */
      22                 :             : typedef enum {
      23                 :             :     DS2413_PH_PRIME = 0,
      24                 :             :     DS2413_PH_RESET,
      25                 :             :     DS2413_PH_CMD,
      26                 :             :     DS2413_PH_READ,
      27                 :             :     DS2413_PH_DONE,
      28                 :             :     DS2413_PH_IDLE
      29                 :             : } ds2413_phase_t;
      30                 :             : 
      31                 :             : /** @brief Driver context */
      32                 :             : typedef struct {
      33                 :             :     uint8_t* out; /**< Read destination (valid until the operation finishes) */
      34                 :             :     union {
      35                 :             :         ow_pulse_t cmd[DS2413_CMD_SLOTS + 1u]; /**< command write table */
      36                 :             :         volatile uint16_t cap[2]; /**< reset + presence capture */
      37                 :             :         volatile uint8_t pulse[DS2413_READ_SLOTS]; /**< reply capture */
      38                 :             :     } u;
      39                 :             :     uint8_t raw[DS2413_READ_BYTES]; /**< decoded reply */
      40                 :             :     uint8_t phase;
      41                 :             :     uint8_t flags;
      42                 :             :     uint8_t command;
      43                 :             :     uint8_t data; /**< PIO state byte (already masked with 0xFC) */
      44                 :             :     uint8_t last_pio;
      45                 :             :     uint8_t last_latch;
      46                 :             :     uint8_t attempts;
      47                 :             :     uint8_t selected_rom[DS2413_ROM_BYTES];
      48                 :             : } ds2413_ctx_t;
      49                 :             : 
      50                 :             : /** @brief Match ROM addressing (0 = Skip ROM broadcast) */
      51                 :             : #define DS2413_FLAG_ADDRESS (1u << 0)
      52                 :             : /** @brief The last operation completed successfully */
      53                 :             : #define DS2413_FLAG_OK (1u << 1)
      54                 :             : /** @brief The operation finished (or nothing was scheduled) */
      55                 :             : #define DS2413_FLAG_FINISHED (1u << 2)
      56                 :             : 
      57                 :             : /** @brief Bus retries, matching the Linux w1_ds2413 driver */
      58                 :             : #define DS2413_MAX_ATTEMPTS 3u
      59                 :             : /** @brief Parasite priming high time before an operation */
      60                 :             : #define DS2413_PRIME_US 1000u
      61                 :             : 
      62                 :             : static ds2413_ctx_t ctx;
      63                 :             : 
      64                 :             : _Static_assert(DS2413_PH_IDLE <= 255, "phase enum must fit uint8_t packing");
      65                 :             : _Static_assert(DS2413_CMD_SLOTS <= ONEWIRE_MAX_SLOTS,
      66                 :             :                "DS2413 command write must fit one RCR window");
      67                 :             : _Static_assert(DS2413_READ_SLOTS <= ONEWIRE_MAX_SLOTS,
      68                 :             :                "DS2413 reply must fit one RCR window");
      69                 :             : _Static_assert(DS2413_READ_BYTES <= ONEWIRE_MAX_READ_BYTES,
      70                 :             :                "DS2413 reply must fit one read window");
      71                 :             : 
      72                 :         126 : static uint8_t ds2413_can_start(void) {
      73                 :         126 :     return (uint8_t)((ctx.phase == DS2413_PH_IDLE ||
      74   [ +  +  -  +  :         234 :                       ctx.phase == DS2413_PH_DONE) &&
                   +  - ]
      75                 :         108 :                      !onewire_search_active());
      76                 :             : }
      77                 :             : 
      78                 :             : /** @brief Build the command table and return its slot count */
      79                 :          99 : static uint8_t ds2413_build(void) {
      80                 :          99 :     ow_pulse_t* p = (ow_pulse_t*)ctx.u.cmd;
      81                 :             :     uint8_t bytes;
      82         [ +  + ]:          99 :     if ((ctx.flags & DS2413_FLAG_ADDRESS) != 0u) {
      83                 :          27 :         bytes = ow_encode_match_rom(&p, ctx.selected_rom);
      84                 :             :     } else {
      85                 :          72 :         bytes = ow_encode_skip_rom(&p);
      86                 :             :     }
      87                 :          99 :     onewire_encode_byte(p, ctx.command);
      88                 :          99 :     p += ONEWIRE_BITS_PER_BYTE;
      89                 :          99 :     bytes++;
      90         [ +  + ]:          99 :     if (ctx.command == DS2413_CMD_WRITE) {
      91                 :          36 :         onewire_encode_byte(p, ctx.data);
      92                 :          36 :         p += ONEWIRE_BITS_PER_BYTE;
      93                 :          36 :         bytes++;
      94                 :          36 :         onewire_encode_byte(p, (uint8_t)~ctx.data);
      95                 :          36 :         p += ONEWIRE_BITS_PER_BYTE;
      96                 :          36 :         bytes++;
      97                 :             :     }
      98                 :          99 :     const uint8_t slots = (uint8_t)(bytes * ONEWIRE_BITS_PER_BYTE);
      99                 :          99 :     ((ow_pulse_t*)ctx.u.cmd)[slots] = ONEWIRE_RELEASE_PULSE;
     100                 :          99 :     return slots;
     101                 :             : }
     102                 :             : 
     103                 :             : /** @brief Schedule one operation; 1 = accepted */
     104                 :         126 : static uint8_t ds2413_start(uint8_t command, uint8_t data, uint8_t* out) {
     105         [ +  + ]:         126 :     if (!ds2413_can_start()) {
     106                 :          18 :         return 0;
     107                 :             :     }
     108                 :         108 :     ctx.command = command;
     109                 :         108 :     ctx.data = data;
     110                 :         108 :     ctx.out = out;
     111                 :         108 :     ctx.flags &= (uint8_t)~(DS2413_FLAG_OK | DS2413_FLAG_FINISHED);
     112                 :         108 :     ctx.attempts = 0;
     113                 :         108 :     onewire_strong_pullup(1);
     114                 :         108 :     ow_start_wait(DS2413_PRIME_US);
     115                 :         108 :     ctx.phase = DS2413_PH_PRIME;
     116                 :         108 :     return 1;
     117                 :             : }
     118                 :             : 
     119                 :             : /** @brief Validate a PIO Access Read reply */
     120                 :          63 : static uint8_t ds2413_reply_valid(uint8_t b) {
     121         [ +  + ]:          63 :     if (b == DS2413_NO_RESPONSE) {
     122                 :           9 :         return 0;
     123                 :             :     }
     124                 :          54 :     return (uint8_t)((b & 0x0Fu) == ((uint8_t)(~b) >> 4));
     125                 :             : }
     126                 :             : 
     127                 :         126 : void ds2413_init(void) {
     128                 :         126 :     onewire_init();
     129                 :         126 :     ctx.flags = DS2413_FLAG_FINISHED;
     130                 :         126 :     ctx.phase = DS2413_PH_IDLE;
     131                 :         126 :     ctx.command = 0;
     132                 :         126 :     ctx.data = 0;
     133                 :         126 :     ctx.last_pio = 0;
     134                 :         126 :     ctx.last_latch = 0;
     135                 :         126 : }
     136                 :             : 
     137                 :          36 : void ds2413_select(const uint8_t* rom) {
     138   [ +  +  +  - ]:          36 :     if (ctx.phase != DS2413_PH_IDLE && ctx.phase != DS2413_PH_DONE) {
     139                 :           9 :         return;
     140                 :             :     }
     141         [ -  + ]:          27 :     if (onewire_search_active()) {
     142                 :           0 :         return;
     143                 :             :     }
     144         [ -  + ]:          27 :     if (rom == 0) {
     145                 :           0 :         ctx.flags &= (uint8_t)~DS2413_FLAG_ADDRESS;
     146                 :           0 :         return;
     147                 :             :     }
     148         [ +  + ]:         243 :     for (uint8_t i = 0; i < DS2413_ROM_BYTES; i++) {
     149                 :         216 :         ctx.selected_rom[i] = rom[i];
     150                 :             :     }
     151                 :          27 :     ctx.flags |= DS2413_FLAG_ADDRESS;
     152                 :             : }
     153                 :             : 
     154                 :          90 : uint8_t ds2413_read_pio(uint8_t* out) {
     155         [ +  + ]:          90 :     if (out == 0) {
     156                 :           9 :         return 0;
     157                 :             :     }
     158                 :          81 :     return ds2413_start(DS2413_CMD_READ, 0, out);
     159                 :             : }
     160                 :             : 
     161                 :          45 : uint8_t ds2413_write_pio(uint8_t states) {
     162                 :             :     /* Bits 2-7 must be 1 in the state byte sent to the device. */
     163                 :          90 :     return ds2413_start(DS2413_CMD_WRITE,
     164                 :          45 :                         (uint8_t)((states & DS2413_PIO_MASK) | 0xFCu), 0);
     165                 :             : }
     166                 :             : 
     167                 :         108 : uint8_t ds2413_last_command_ok(void) {
     168                 :         108 :     return (uint8_t)((ctx.flags & DS2413_FLAG_OK) != 0u);
     169                 :             : }
     170                 :             : 
     171                 :          45 : uint8_t ds2413_last_pio(void) {
     172                 :          45 :     return ctx.last_pio;
     173                 :             : }
     174                 :             : 
     175                 :          36 : uint8_t ds2413_last_latches(void) {
     176                 :          36 :     return ctx.last_latch;
     177                 :             : }
     178                 :             : 
     179                 :           0 : uint8_t ds2413_last_reply(void) {
     180                 :           0 :     return ctx.raw[0];
     181                 :             : }
     182                 :             : 
     183                 :         783 : static uint8_t ds2413_poll(void) {
     184         [ +  + ]:         783 :     if ((ctx.flags & DS2413_FLAG_FINISHED) != 0u) {
     185                 :           9 :         return 1;
     186                 :             :     }
     187         [ +  + ]:         774 :     if (ctx.phase == DS2413_PH_DONE) {
     188                 :         108 :         ctx.flags |= DS2413_FLAG_FINISHED;
     189                 :         108 :         return 1;
     190                 :             :     }
     191         [ +  + ]:         666 :     if (!ow_op_done()) {
     192                 :         108 :         return 0;
     193                 :             :     }
     194                 :             : 
     195   [ +  +  +  +  :         558 :     switch (ctx.phase) {
                      - ]
     196                 :         180 :     case DS2413_PH_PRIME:
     197                 :         180 :         onewire_strong_pullup(0);
     198                 :         180 :         onewire_reset((volatile uint16_t*)ctx.u.cap);
     199                 :         180 :         ctx.phase = DS2413_PH_RESET;
     200                 :         180 :         break;
     201                 :             : 
     202                 :         180 :     case DS2413_PH_RESET:
     203         [ +  + ]:         180 :         if (!ow_has_presence(ctx.u.cap)) {
     204         [ +  + ]:          81 :             if (++ctx.attempts < DS2413_MAX_ATTEMPTS) {
     205                 :          45 :                 onewire_strong_pullup(1);
     206                 :          45 :                 ow_start_wait(DS2413_PRIME_US);
     207                 :          45 :                 ctx.phase = DS2413_PH_PRIME;
     208                 :          45 :                 break;
     209                 :             :             }
     210                 :          36 :             ctx.phase = DS2413_PH_DONE;
     211                 :          36 :             break;
     212                 :             :         }
     213                 :          99 :         onewire_write_slots((const ow_pulse_t*)ctx.u.cmd, ds2413_build());
     214                 :          99 :         ctx.phase = DS2413_PH_CMD;
     215                 :          99 :         break;
     216                 :             : 
     217                 :          99 :     case DS2413_PH_CMD:
     218                 :          99 :         onewire_read_data((volatile uint8_t*)ctx.u.pulse, DS2413_READ_BYTES);
     219                 :          99 :         ctx.phase = DS2413_PH_READ;
     220                 :          99 :         break;
     221                 :             : 
     222                 :          99 :     case DS2413_PH_READ:
     223                 :          99 :         onewire_decode_pulses(ctx.raw, (const volatile uint8_t*)ctx.u.pulse,
     224                 :             :                               DS2413_READ_BYTES);
     225         [ +  + ]:          99 :         if (ctx.command == DS2413_CMD_READ) {
     226         [ +  + ]:          63 :             if (ds2413_reply_valid(ctx.raw[0])) {
     227                 :             :                 /* Read reply low nibble: bit 0 = PIOA pin, bit 1 = PIOA
     228                 :             :                  * latch, bit 2 = PIOB pin, bit 3 = PIOB latch. Normalize each
     229                 :             :                  * half to the write layout (bit 0 = PIOA, bit 1 = PIOB) so
     230                 :             :                  * callers can compare against ds2413_write_pio() directly. */
     231                 :          45 :                 const uint8_t reply = ctx.raw[0];
     232                 :          45 :                 ctx.last_pio =
     233                 :          45 :                     (uint8_t)(((reply & 0x01u) ? DS2413_PIOA : 0u) |
     234                 :             :                               ((reply & 0x04u) ? DS2413_PIOB : 0u));
     235                 :          45 :                 ctx.last_latch =
     236                 :          45 :                     (uint8_t)(((reply & 0x02u) ? DS2413_PIOA : 0u) |
     237                 :             :                               ((reply & 0x08u) ? DS2413_PIOB : 0u));
     238         [ +  - ]:          45 :                 if (ctx.out != 0) {
     239                 :          45 :                     *ctx.out = ctx.last_pio;
     240                 :             :                 }
     241                 :          45 :                 ctx.flags |= DS2413_FLAG_OK;
     242                 :             :             }
     243         [ +  + ]:          36 :         } else if (ctx.raw[0] == DS2413_ACK) {
     244                 :          27 :             ctx.flags |= DS2413_FLAG_OK;
     245                 :             :         }
     246         [ +  + ]:          99 :         if ((ctx.flags & DS2413_FLAG_OK) == 0u &&
     247         [ +  - ]:          27 :             ++ctx.attempts < DS2413_MAX_ATTEMPTS) {
     248                 :          27 :             onewire_strong_pullup(1);
     249                 :          27 :             ow_start_wait(DS2413_PRIME_US);
     250                 :          27 :             ctx.phase = DS2413_PH_PRIME;
     251                 :          27 :             break;
     252                 :             :         }
     253                 :          72 :         ctx.phase = DS2413_PH_DONE;
     254                 :          72 :         break;
     255                 :             : 
     256                 :           0 :     case DS2413_PH_DONE:
     257                 :             :     case DS2413_PH_IDLE:
     258                 :             :     default:
     259                 :           0 :         break;
     260                 :             :     }
     261                 :         558 :     return 0;
     262                 :             : }
     263                 :             : 
     264                 :         531 : uint8_t ds2413_read_pio_poll(void) {
     265                 :         531 :     return ds2413_poll();
     266                 :             : }
     267                 :             : 
     268                 :         252 : uint8_t ds2413_write_pio_poll(void) {
     269                 :         252 :     return ds2413_poll();
     270                 :             : }
        

Generated by: LCOV version 2.0-1