LCOV - code coverage report
Current view: top level - src - ds18b20_txn.c (source / functions) Coverage Total Hit
Test: stm32-async-1wire core coverage Lines: 98.6 % 138 136
Test Date: 2026-11-28 11:52:24 Functions: 100.0 % 17 17
Branches: 93.8 % 81 76

             Branch data     Line data    Source code
       1                 :             : /* DS18B20 command-transaction module (include-only part of src/ds18b20.c).
       2                 :             :  * Not a translation unit on its own: compile src/ds18b20.c, which
       3                 :             :  * resolves the DS18B20_DRIVER_BUILD gate. This part owns txn_ctx (the
       4                 :             :  * matching static lives in the core file) plus detect_buf, and implements
       5                 :             :  * the non-blocking command transactions (Read ROM, Read/Write Scratchpad,
       6                 :             :  * Copy/Recall EEPROM, alarm thresholds, parasite detection). */
       7                 :             : #ifndef DS18B20_DRIVER_BUILD
       8                 :             : #error "ds18b20_txn.c is an include-only driver part; compile src/ds18b20.c"
       9                 :             : #endif
      10                 :             : 
      11                 :             : /** @brief Receive buffer for the parasite-mode detection answer byte */
      12                 :             : static uint8_t detect_buf;
      13                 :             : 
      14                 :             : /**
      15                 :             :  * @defgroup DS18B20_Command_Impl DS18B20 Non-Blocking Command Transactions
      16                 :             :  * @brief Shared non-blocking engine for the infrequent DS18B20 commands that
      17                 :             :  *        the measurement state machine does not issue: Read ROM (0x33),
      18                 :             :  *        Write Scratchpad thresholds (0x4E), Copy Scratchpad (0x48), Recall
      19                 :             :  *        EEPROM (0xB8), Read Power Supply (0xB4) and raw Read Scratchpad
      20                 :             :  *        (0xBE). Every command runs reset -> presence -> write -> (read |
      21                 :             :  *        timed wait) -> done, one hardware-timed operation per poll call, so
      22                 :             :  *        the same non-blocking discipline as the measurement, search and
      23                 :             :  *        resolution state machines is preserved. Each command owns TIM1/DMA
      24                 :             :  *        while it runs and hands the timer back to ds18b20_poll() when done.
      25                 :             :  * @{
      26                 :             :  */
      27                 :             : 
      28                 :             : /**
      29                 :             :  * @brief Ownership guard shared by every command transaction start
      30                 :             :  * @return 1 when a new transaction may be scheduled
      31                 :             :  */
      32                 :             : __STATIC_FORCEINLINE uint8_t txn_can_start(void) {
      33                 :             :     /* A scan session owns the timer for its whole duration (scan_mode stays 1
      34                 :             :      * until ds18b20_select() clears it): a command transaction started then
      35                 :             :      * would clobber the in-flight measurement/scan cycle, so it must be
      36                 :             :      * rejected. Without this check a command could slip through during the
      37                 :             :      * brief IDLE pause between scan rounds. */
      38                 :        1359 :     return (uint8_t)(ctx.current_state == DS18B20_ST_IDLE &&
      39   [ +  +  +  + ]:        1334 :                      (ctx.flags & DS18B20_FLAG_SCAN_MODE) == 0u && !onewire_search_active() &&
      40         [ +  + ]:        2618 :                      (res_ctx.flags & DS18B20_RES_FLAG_FINISHED) != 0u &&
      41         [ +  + ]:        1259 :                      (txn_ctx.flags & DS18B20_TXN_FLAG_FINISHED) != 0u);
      42                 :             : }
      43                 :             : 
      44                 :             : /**
      45                 :             :  * @brief Build the command pulse sequence into phase_pulses.txn
      46                 :             :  * @note Encodes the addressing prefix (Skip ROM 0xCC, or Match ROM 0x55 +
      47                 :             :  *       selected ROM; none for a bare command such as Read ROM), the function
      48                 :             :  *       command byte and the optional payload (Write Scratchpad TH/TL/CFG).
      49                 :             :  *       The trailing ONEWIRE_RELEASE_PULSE that the 1-Wire layer consumes as
      50                 :             :  *       the final DMA transfer (hardware bus release) is written at the slot
      51                 :             :  *       index of the mode actually used, not always at the end of the buffer.
      52                 :             :  */
      53                 :             : __STATIC_FORCEINLINE void txn_build_pulses(void) {
      54                 :             :     // txn_can_start() rejects scan mode, so a transaction uses Match ROM only
      55                 :             :     // for an explicitly selected device and a command that is not sent bare.
      56                 :        1209 :     const uint8_t use_match = ((ctx.flags & DS18B20_FLAG_ADDRESS_MODE) != 0u) &&
      57         [ +  - ]:          75 :                               (txn_ctx.flags & DS18B20_TXN_FLAG_BARE) == 0u;
      58                 :        1209 :     const uint8_t* rom = 0;
      59         [ +  + ]:        1209 :     if (use_match) {
      60                 :          75 :         rom = ctx.selected_rom;
      61                 :             :     }
      62                 :        1209 :     ow_pulse_t* p = phase_pulses.txn;
      63                 :        1209 :     uint8_t bytes = 0;
      64         [ +  + ]:        1209 :     if ((txn_ctx.flags & DS18B20_TXN_FLAG_BARE) == 0u) {
      65         [ +  + ]:        1818 :         bytes = (rom != 0) ? ow_encode_match_rom(&p, rom) : ow_encode_skip_rom(&p);
      66                 :             :     }
      67                 :        1209 :     onewire_encode_byte(p, txn_ctx.command);
      68                 :        1209 :     p += DS18B20_BITS_PER_BYTE;
      69                 :        1209 :     bytes++;
      70                 :        1209 :     bytes = (uint8_t)(bytes + ow_encode_bytes(&p, txn_ctx.payload, txn_ctx.payload_len));
      71                 :        1209 :     txn_ctx.slots = (uint8_t)(bytes * DS18B20_BITS_PER_BYTE);
      72                 :             :     /* B1: guarantee the trailing ONEWIRE_RELEASE_PULSE that the 1-Wire layer
      73                 :             :      * reads as its final DMA transfer into CCR3, even though the command
      74                 :             :      * write only ever fills slots 0 .. slots - 1 (see build_res_pulses for
      75                 :             :      * the same pattern). */
      76                 :        1209 :     phase_pulses.txn[txn_ctx.slots] = ONEWIRE_RELEASE_PULSE;
      77                 :        1209 : }
      78                 :             : 
      79                 :             : /**
      80                 :             :  * @brief Decode the captured read pulses into txn_ctx.raw
      81                 :             :  * @note Reads ctx.pulse (written by the read DMA), never aliased with raw:
      82                 :             :  *       the union invariant of decode_scratchpad() does not apply here.
      83                 :             :  */
      84                 :             : __STATIC_FORCEINLINE void txn_decode_read(void) {
      85                 :         325 :     onewire_decode_pulses(txn_ctx.raw, ctx.pulse, txn_ctx.read_bytes);
      86                 :         325 : }
      87                 :             : 
      88                 :             : /**
      89                 :             :  * @brief Copy the decoded read result into the user buffer
      90                 :             :  * @param[in] len Number of bytes to copy (txn_ctx.out must hold at least len)
      91                 :             :  */
      92                 :             : __STATIC_FORCEINLINE void txn_copy_out(uint8_t len) {
      93   [ +  +  +  + ]:        2125 :     for (uint8_t i = 0; i < len; i++) {
      94                 :        1900 :         txn_ctx.out[i] = txn_ctx.raw[i];
      95                 :             :     }
      96                 :         225 : }
      97                 :             : 
      98                 :             : /**
      99                 :             :  * @brief Advance the active command transaction by one hardware operation
     100                 :             :  * @return 1 when the transaction finished (successfully or aborted), 0 while
     101                 :             :  *         running
     102                 :             :  */
     103                 :        3795 : static uint8_t txn_poll(void) {
     104         [ +  + ]:        3795 :     if ((txn_ctx.flags & DS18B20_TXN_FLAG_FINISHED) != 0u) {
     105                 :          75 :         return 1;
     106                 :             :     }
     107                 :             : 
     108         [ +  + ]:        3720 :     if (txn_ctx.phase == DS18B20_TXN_DONE) {
     109                 :             :         // The last hardware operation completed (command done or aborted).
     110                 :             :         // The timer is idle from here on: it is picked up again by the next
     111                 :             :         // ds18b20_start_measure() (or by the next search/command), which is
     112                 :             :         // what keeps a command from silently starting a measurement.
     113                 :         859 :         txn_ctx.flags |= DS18B20_TXN_FLAG_FINISHED;
     114                 :         859 :         return 1;
     115                 :             :     }
     116                 :             : 
     117                 :             :     // Wait for the currently scheduled hardware operation to complete.
     118                 :             :     // This is a non-blocking poll, not a busy-wait.
     119                 :        2861 :     uint8_t txn_done = ow_op_done();
     120                 :             : 
     121         [ +  + ]:        2861 :     if (!txn_done) {
     122                 :         784 :         return 0;
     123                 :             :     }
     124                 :             : 
     125   [ +  +  +  +  :        2077 :     switch (txn_ctx.phase) {
                      - ]
     126                 :         859 :     case DS18B20_TXN_RESET:
     127                 :             :         // Reset completed: a presence pulse means at least one device is on
     128                 :             :         // the bus, so send the command for this transaction.
     129                 :             :         {
     130                 :         859 :             uint8_t txn_present = ow_has_presence(ctx.capture);
     131         [ +  + ]:         859 :             if (!txn_present) {
     132                 :         175 :                 txn_ctx.phase = DS18B20_TXN_DONE;
     133                 :         175 :                 break;
     134                 :             :             }
     135                 :             :         }
     136         [ +  + ]:         684 :         if ((ctx.flags & DS18B20_FLAG_PARASITE) != 0u) {
     137                 :         100 :             onewire_strong_pullup(1);
     138                 :             :         }
     139                 :         684 :         onewire_write_slots(phase_pulses.txn, txn_ctx.slots);
     140                 :         684 :         txn_ctx.phase = DS18B20_TXN_WRITE;
     141                 :         684 :         break;
     142                 :             : 
     143                 :         684 :     case DS18B20_TXN_WRITE:
     144                 :             :         // Command write completed: read the response back if the command has
     145                 :             :         // one, otherwise wait the required hold-off or finish immediately.
     146         [ +  + ]:         684 :         if (txn_ctx.read_bytes) {
     147         [ +  + ]:         325 :             if ((ctx.flags & DS18B20_FLAG_PARASITE) != 0u) {
     148                 :          25 :                 onewire_strong_pullup(0);
     149                 :             :             }
     150                 :         325 :             onewire_read_data(ctx.pulse, txn_ctx.read_bytes);
     151                 :         325 :             txn_ctx.phase = DS18B20_TXN_READ;
     152         [ +  + ]:         359 :         } else if (txn_ctx.wait_us) {
     153                 :             :             // Parasite power: Copy Scratchpad / Recall E² draw their supply
     154                 :             :             // from the bus while the EEPROM programs - drive HIGH actively
     155                 :             :             // for the hold-off window.
     156         [ +  + ]:         209 :             if ((ctx.flags & DS18B20_FLAG_PARASITE) != 0u) {
     157                 :          50 :                 onewire_strong_pullup(1);
     158                 :             :             }
     159                 :         209 :             ow_start_wait(txn_ctx.wait_us);
     160                 :         209 :             txn_ctx.phase = DS18B20_TXN_WAIT;
     161                 :             :         } else {
     162         [ +  + ]:         150 :             if ((ctx.flags & DS18B20_FLAG_PARASITE) != 0u) {
     163                 :          25 :                 onewire_strong_pullup(0);
     164                 :             :             }
     165                 :         150 :             txn_ctx.flags |= DS18B20_TXN_FLAG_OK;
     166                 :         150 :             txn_ctx.phase = DS18B20_TXN_DONE;
     167                 :             :         }
     168                 :         684 :         break;
     169                 :             : 
     170                 :         325 :     case DS18B20_TXN_READ:
     171                 :             :         // Data read completed: decode the captured pulse durations.
     172                 :             :         txn_decode_read();
     173                 :         325 :         txn_ctx.flags |= DS18B20_TXN_FLAG_OK;
     174                 :         325 :         txn_ctx.phase = DS18B20_TXN_DONE;
     175                 :         325 :         break;
     176                 :             : 
     177                 :         209 :     case DS18B20_TXN_WAIT:
     178                 :             :         // Hold-off completed (Copy Scratchpad / Recall EEPROM): release the
     179                 :             :         // strong pull-up unconditionally (idempotent) so a parasite flag
     180                 :             :         // cleared mid-window cannot leave the bus actively driven.
     181                 :         209 :         onewire_strong_pullup(0);
     182                 :         209 :         txn_ctx.flags |= DS18B20_TXN_FLAG_OK;
     183                 :         209 :         txn_ctx.phase = DS18B20_TXN_DONE;
     184                 :         209 :         break;
     185                 :             : 
     186                 :           0 :     case DS18B20_TXN_DONE:
     187                 :             :     default:
     188                 :           0 :         break;
     189                 :             :     }
     190                 :             : 
     191                 :        2077 :     return 0;
     192                 :             : }
     193                 :             : 
     194                 :             : /**
     195                 :             :  * @brief Schedule a new non-blocking command transaction
     196                 :             :  * @param[in] command DS18B20 function command byte
     197                 :             :  * @param[in] out User result buffer (may be NULL; only written on success)
     198                 :             :  * @param[in] payload Up to 3 payload bytes (Write Scratchpad TH/TL/CFG)
     199                 :             :  * @param[in] payload_len Number of payload bytes (0..3)
     200                 :             :  * @param[in] read_bytes Bytes to read back after the command (0 = none)
     201                 :             :  * @param[in] wait_us Timed hold-off after the command (0 = none)
     202                 :             :  * @param[in] bare 1 to send the command without an addressing prefix
     203                 :             :  * @note Ignored unless the driver is IDLE, the device search and any
     204                 :             :  *       resolution change are finished, and no transaction is already
     205                 :             :  *       running. The result buffer must stay valid until the transaction
     206                 :             :  *       completes (ds18b20_*_poll() reports 1).
     207                 :             :  */
     208         [ +  + ]:        1359 : static void txn_start(uint8_t command, uint8_t* out, const uint8_t* payload,
     209                 :             :                       uint8_t payload_len, uint8_t read_bytes, uint16_t wait_us,
     210                 :             :                       uint8_t bare) {
     211         [ +  + ]:        1359 :     if (!txn_can_start()) {
     212                 :         150 :         return; // the timer belongs to someone else right now
     213                 :             :     }
     214                 :        1209 :     txn_ctx.command = command;
     215                 :        1209 :     txn_ctx.out = out;
     216                 :        1209 :     txn_ctx.read_bytes = read_bytes;
     217                 :        1209 :     txn_ctx.wait_us = wait_us;
     218                 :        1209 :     txn_ctx.payload_len = payload_len;
     219   [ +  +  +  - ]:        2034 :     for (uint8_t i = 0; i < payload_len && i < sizeof(txn_ctx.payload); i++) {
     220                 :         825 :         txn_ctx.payload[i] = payload[i];
     221                 :             :     }
     222                 :             :     /* Fresh transaction: only the bare bit survives (ok/finished are clear). */
     223   [ +  +  +  + ]:        1209 :     txn_ctx.flags = bare ? DS18B20_TXN_FLAG_BARE : 0u;
     224                 :             :     txn_build_pulses(); // Pre-build the command for the current address mode
     225                 :        1209 :     onewire_strong_pullup(0);
     226                 :        1209 :     txn_ctx.phase = DS18B20_TXN_RESET;
     227                 :        1209 :     onewire_reset(ctx.capture); // Schedule the first hardware operation
     228                 :             : }
     229                 :             : 
     230                 :             : /**
     231                 :             :  * @brief Read the 64-bit ROM of the (only) DS18B20 on the bus
     232                 :             :  * @param[in,out] rom Buffer for the 8-byte ROM (LSB first); written on success
     233                 :             :  * @note Valid only when exactly one device is on the bus (datasheet Read ROM
     234                 :             :  *       0x33). With several devices use the device search (ds18b20_search_*).
     235                 :             :  * @note Result validity: check ds18b20_last_command_ok() or the CRC over the
     236                 :             :  *       7 leading bytes (ds18b20_crc8(rom, 7) == rom[7]).
     237                 :             :  */
     238                 :         400 : void ds18b20_read_rom(uint8_t* rom) {
     239                 :         400 :     txn_start(DS18B20_READ_ROM, rom, 0, 0, DS18B20_ROM_BYTES, 0, 1);
     240                 :         400 : }
     241                 :             : 
     242                 :             : /**
     243                 :             :  * @brief Advance the non-blocking Read ROM transaction
     244                 :             :  * @return 1 when finished (successfully or aborted), 0 while running
     245                 :             :  */
     246                 :         775 : uint8_t ds18b20_read_rom_poll(void) {
     247         [ +  + ]:         775 :     if (!txn_poll()) {
     248                 :         550 :         return 0;
     249                 :             :     }
     250   [ +  +  +  - ]:         225 :     if (((txn_ctx.flags & DS18B20_TXN_FLAG_OK) != 0u) && txn_ctx.out) {
     251                 :             :         txn_copy_out(DS18B20_ROM_BYTES);
     252                 :             :     }
     253                 :         225 :     return 1;
     254                 :             : }
     255                 :             : 
     256                 :             : /**
     257                 :             :  * @brief Configure the alarm trigger thresholds TH and TL
     258                 :             :  * @param[in] th High-alarm trigger value (DS18B20 8-bit threshold code)
     259                 :             :  * @param[in] tl Low-alarm trigger value (DS18B20 8-bit threshold code)
     260                 :             :  * @note Uses the DS18B20 8-bit sign-extended temperature code, the same
     261                 :             :  *       encoding the scratchpad TH/TL bytes use; converting to/from Celsius is
     262                 :             :  *       left to the application. The current conversion resolution (byte 4,
     263                 :             :  *       R1/R0) is written unchanged, so the resolution is not disturbed.
     264                 :             :  * @note Takes effect immediately in the scratchpad; run ds18b20_copy_scratchpad()
     265                 :             :  *       afterwards to persist TH/TL/CFG to the EEPROM.
     266                 :             :  */
     267                 :         325 : void ds18b20_set_alarm_thresholds(uint8_t th, uint8_t tl) {
     268                 :         325 :     const uint8_t payload[3] = {th, tl, res_config_byte(ctx.resolution)};
     269                 :         325 :     txn_start(DS18B20_WRITE_SCRATCHPAD, 0, payload, 3, 0, 0, 0);
     270                 :         325 : }
     271                 :             : 
     272                 :             : /**
     273                 :             :  * @brief Advance the non-blocking alarm threshold write
     274                 :             :  * @return 1 when finished (successfully or aborted), 0 while running
     275                 :             :  */
     276                 :         650 : uint8_t ds18b20_set_alarm_thresholds_poll(void) { return txn_poll(); }
     277                 :             : 
     278                 :             : /**
     279                 :             :  * @brief Read the 9-byte scratchpad (raw; includes TH, TL and the CRC)
     280                 :             :  * @param[in,out] buf Buffer for the 9 scratchpad bytes (byte 0 = temp LSB,
     281                 :             :  *                    bytes 2/3 = TH/TL, byte 8 = CRC); written on success
     282                 :             :  * @note Result validity: check ds18b20_last_command_ok() or the CRC over the
     283                 :             :  *       8 leading bytes (buf[8] == ds18b20_crc8(buf, 8)).
     284                 :             :  */
     285                 :         125 : void ds18b20_read_scratchpad(uint8_t* buf) {
     286                 :         125 :     txn_start(DS18B20_READ_SCRATCHPAD, buf, 0, 0, DS18B20_SCRATCHPAD_LEN, 0, 0);
     287                 :         125 : }
     288                 :             : 
     289                 :             : /**
     290                 :             :  * @brief Advance the non-blocking raw scratchpad read
     291                 :             :  * @return 1 when finished (successfully or aborted), 0 while running
     292                 :             :  * @note On a valid read (CRC byte matches) the conversion resolution is
     293                 :             :  *       auto-derived from the config byte (byte 4), like the measurement path.
     294                 :             :  */
     295                 :         575 : uint8_t ds18b20_read_scratchpad_poll(void) {
     296         [ +  + ]:         575 :     if (!txn_poll()) {
     297                 :         450 :         return 0;
     298                 :             :     }
     299   [ +  +  +  - ]:         125 :     if (((txn_ctx.flags & DS18B20_TXN_FLAG_OK) != 0u) && txn_ctx.out) {
     300                 :             :         txn_copy_out(DS18B20_SCRATCHPAD_LEN);
     301         [ +  + ]:         100 :         if (txn_ctx.raw[DS18B20_SCRATCHPAD_LEN - 1] ==
     302                 :         100 :             ds18b20_crc8(txn_ctx.raw, DS18B20_CRC8_BYTES)) {
     303                 :          75 :             ctx.resolution = DS18B20_RES_MIN + ((txn_ctx.raw[4] >> 5) & 0x3);
     304                 :             :         }
     305                 :             :     }
     306                 :         125 :     return 1;
     307                 :             : }
     308                 :             : 
     309                 :             : /**
     310                 :             :  * @brief Copy the scratchpad into the EEPROM (non-volatile)
     311                 :             :  * @note The copy draws its supply from VDD on externally powered devices;
     312                 :             :  *       parasite-powered devices are supplied by the strong pull-up, which
     313                 :             :  *       the driver engages for the t_COPY hold-off window when
     314                 :             :  *       ds18b20_set_parasite(1) is set. The driver waits the datasheet
     315                 :             :  *       t_COPY hold-off (10ms) before finishing.
     316                 :             :  */
     317                 :         209 : void ds18b20_copy_scratchpad(void) {
     318                 :         209 :     txn_start(DS18B20_COPY_SCRATCHPAD, 0, 0, 0, 0, DS18B20_EEPROM_WAIT_US, 0);
     319                 :         209 : }
     320                 :             : 
     321                 :             : /**
     322                 :             :  * @brief Advance the non-blocking Copy Scratchpad transaction
     323                 :             :  * @return 1 when finished (successfully or aborted), 0 while running
     324                 :             :  */
     325                 :         720 : uint8_t ds18b20_copy_scratchpad_poll(void) { return txn_poll(); }
     326                 :             : 
     327                 :             : /**
     328                 :             :  * @brief Recall the EEPROM contents into the scratchpad
     329                 :             :  * @note Loads the last EEPROM copy (TH/TL/CFG) into the volatile scratchpad.
     330                 :             :  *       The driver waits the datasheet t_RECALL hold-off (10ms) before
     331                 :             :  *       finishing.
     332                 :             :  * @note After recall, the scratchpad holds the EEPROM-stored configuration
     333                 :             :  *       (TH/TL/CFG, including the conversion-resolution bits). The driver's
     334                 :             :  *       tracked ctx.resolution is NOT updated by this call: Recall is a
     335                 :             :  *       write-only command with no data returned. If the EEPROM resolution
     336                 :             :  *       may differ from ctx.resolution, follow this with
     337                 :             :  *       ds18b20_read_scratchpad() / ds18b20_read_scratchpad_poll() to
     338                 :             :  *       resynchronise ctx.resolution before the next conversion.
     339                 :             :  */
     340                 :         125 : void ds18b20_recall_eeprom(void) {
     341                 :         125 :     txn_start(DS18B20_RECALL_EEPROM, 0, 0, 0, 0, DS18B20_EEPROM_WAIT_US, 0);
     342                 :         125 : }
     343                 :             : 
     344                 :             : /**
     345                 :             :  * @brief Advance the non-blocking Recall EEPROM transaction
     346                 :             :  * @return 1 when finished (successfully or aborted), 0 while running
     347                 :             :  * @warning ctx.resolution is NOT updated on success. Recall is a write-only
     348                 :             :  *          command; the device does not return the restored config. To keep
     349                 :             :  *          ctx.resolution in sync with a possibly-different EEPROM resolution,
     350                 :             :  *          call ds18b20_read_scratchpad_poll() after this returns 1 and
     351                 :             :  *          ds18b20_last_command_ok() is set.
     352                 :             :  */
     353                 :         425 : uint8_t ds18b20_recall_eeprom_poll(void) {
     354         [ +  + ]:         425 :     if (!txn_poll()) {
     355                 :         325 :         return 0;
     356                 :             :     }
     357                 :             :     /* Nothing to decode: Recall returns no data, so there is no scratchpad
     358                 :             :      * frame to parse here. The caller is responsible for resynchronising
     359                 :             :      * ctx.resolution via ds18b20_read_scratchpad_poll() if needed. */
     360                 :         100 :     return 1;
     361                 :             : }
     362                 :             : 
     363                 :             : /**
     364                 :             :  * @brief Result of the last completed command transaction
     365                 :             :  * @return 1 when the last ds18b20_*_poll() finished a transaction that found
     366                 :             :  *         a device present (and, for read commands, read its data back),
     367                 :             :  *         0 when it aborted (e.g. no device present) or nothing ran yet
     368                 :             :  */
     369                 :         559 : uint8_t ds18b20_last_command_ok(void) { return (uint8_t)((txn_ctx.flags & DS18B20_TXN_FLAG_OK) != 0u); }
     370                 :             : 
     371                 :             : /**
     372                 :             :  * @brief Declare the bus as parasite-powered
     373                 :             :  * @param[in] parasite 1 = devices are powered over the data line, 0 =
     374                 :             :  *                     external VDD supply (default)
     375                 :             :  * @note In parasite mode the driver engages the strong pull-up (bus pin
     376                 :             :  *       switched to push-pull HIGH) during every temperature conversion wait
     377                 :             :  *       and EEPROM programming hold-off, then releases the line again. The
     378                 :             :  *       flag is read at the start of each window, so call this once after
     379                 :             :  *       ds18b20_init() - or between measurement cycles - and it applies to
     380                 :             :  *       all subsequent operations. The detection helper
     381                 :             :  *       ds18b20_detect_parasite() reports the wiring and stores it back into
     382                 :             :  *       this flag on success; this setter tells the driver how to behave.
     383                 :             :  */
     384                 :         425 : void ds18b20_set_parasite(uint8_t parasite) {
     385         [ +  + ]:         425 :     if (parasite != 0u) {
     386                 :         300 :         ctx.flags |= DS18B20_FLAG_PARASITE;
     387                 :             :     } else {
     388                 :         125 :         ctx.flags &= (uint8_t)~DS18B20_FLAG_PARASITE;
     389                 :             :     }
     390                 :         425 : }
     391                 :             : 
     392                 :             : /**
     393                 :             :  * @brief Current parasite-power configuration of the driver
     394                 :             :  * @return 1 when the strong pull-up will be engaged during conversion and
     395                 :             :  *         EEPROM programming windows, 0 for external VDD supply
     396                 :             :  */
     397                 :         225 : uint8_t ds18b20_parasite_mode(void) { return (uint8_t)((ctx.flags & DS18B20_FLAG_PARASITE) != 0u); }
     398                 :             : 
     399                 :             : /**
     400                 :             :  * @brief Detect the bus wiring and configure parasite mode automatically
     401                 :             :  * @note Issues a Read Power Supply command and stores the decoded answer in
     402                 :             :  *       the parasite flag on success (see ds18b20_detect_parasite_poll()).
     403                 :             :  */
     404                 :         175 : void ds18b20_detect_parasite(void) { txn_start(DS18B20_READ_POWER_SUPPLY, &detect_buf, 0, 0, 1, 0, 0); }
     405                 :             : 
     406                 :         650 : uint8_t ds18b20_detect_parasite_poll(void) {
     407         [ +  + ]:         650 :     if (!txn_poll()) {
     408                 :         500 :         return 0;
     409                 :             :     }
     410         [ +  + ]:         150 :     if ((txn_ctx.flags & DS18B20_TXN_FLAG_OK) != 0u) {
     411                 :             :         // The sensor drives one bit: 0 = parasite power, 1 = external power.
     412         [ +  + ]:         100 :         if ((txn_ctx.raw[0] & 0x01) != 0u) {
     413                 :          50 :             ctx.flags &= (uint8_t)~DS18B20_FLAG_PARASITE;
     414                 :             :         } else {
     415                 :          50 :             ctx.flags |= DS18B20_FLAG_PARASITE;
     416                 :             :         }
     417                 :             :     }
     418                 :         150 :     return 1;
     419                 :             : }
     420                 :             : 
     421                 :             : /**
     422                 :             :  * @}
     423                 :             :  */
        

Generated by: LCOV version 2.0-1