LCOV - code coverage report
Current view: top level - src - ds18b20_resolution.c (source / functions) Coverage Total Hit
Test: stm32-async-1wire core coverage Lines: 96.7 % 61 59
Test Date: 2026-11-28 11:52:24 Functions: 100.0 % 3 3
Branches: 97.1 % 35 34

             Branch data     Line data    Source code
       1                 :             : /* DS18B20 resolution-change 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 res_ctx and the
       4                 :             :  * resolution-change state machine (ds18b20_set_resolution{,_poll}). */
       5                 :             : #ifndef DS18B20_DRIVER_BUILD
       6                 :             : #error "ds18b20_resolution.c is an include-only driver part; compile src/ds18b20.c"
       7                 :             : #endif
       8                 :             : 
       9                 :             : /**
      10                 :             :  * @defgroup DS18B20_Resolution_Internal DS18B20 Internal Non-Blocking Resolution Change
      11                 :             :  * @brief Change the temperature conversion resolution (9..12 bit) with the same
      12                 :             :  *        non-blocking discipline as the device search: every state performs
      13                 :             :  *        exactly one hardware-timed operation via the internal bus primitives,
      14                 :             :  *        so a poll call never blocks. The config write is sent with Write
      15                 :             :  *        Scratchpad (0x4E) + TH + TL + CFG; it takes effect immediately and is
      16                 :             :  *        not persisted to the EEPROM (no Copy Scratchpad, which would need a
      17                 :             :  *        strong pull-up under parasitic power).
      18                 :             :  * @{
      19                 :             :  */
      20                 :             : 
      21                 :             : /* res_phase_t, res_ctx_t, DS18B20_RES_FLAG_* and res_config_byte() live in
      22                 :             :  * src/ds18b20_internal.h shared with the other parts; the res_ctx
      23                 :             :  * instance stays here. */
      24                 :             : 
      25                 :             : /** @brief Global resolution context instance */
      26                 :             : static res_ctx_t res_ctx;
      27                 :             : 
      28                 :             : /* Packing guard: 4 bytes, no padding. */
      29                 :             : _Static_assert(sizeof(res_ctx) == 4, "resolution context must stay 4 bytes");
      30                 :             : 
      31                 :             : _Static_assert(DS18B20_RES_DONE <= 255, "res phase enum must fit uint8_t packing");
      32                 :             : 
      33                 :             : /**
      34                 :             :  * @brief Pre-build the resolution config write into phase_pulses.res
      35                 :             :  * @param[in] res Resolution in bits (9..12)
      36                 :             :  * @note Encodes Skip ROM (0xCC) or Match ROM (0x55 + selected ROM) followed by
      37                 :             :  *       Write Scratchpad (0x4E), TH, TL and the config byte. The trailing
      38                 :             :  *       zero-pulse that the 1-Wire layer consumes as the final DMA transfer
      39                 :             :  *       (hardware bus release) is written at the slot index of the mode
      40                 :             :  *       actually used, not always at the end of the buffer.
      41                 :             :  */
      42                 :             : __STATIC_FORCEINLINE void build_res_pulses(uint8_t res) {
      43                 :             :     // In scan mode the config write must reach every sensor, so the Match ROM
      44                 :             :     // address is skipped even if a single-device address is still selected.
      45                 :         534 :     const uint8_t use_match = ((ctx.flags & DS18B20_FLAG_ADDRESS_MODE) != 0u) &&
      46         [ +  + ]:         150 :                               ((ctx.flags & DS18B20_FLAG_SCAN_MODE) == 0u);
      47                 :         534 :     const uint8_t* rom = 0;
      48         [ +  + ]:         534 :     if (use_match) {
      49                 :         125 :         rom = ctx.selected_rom;
      50                 :             :     }
      51                 :         534 :     ow_pulse_t* p = phase_pulses.res;
      52         [ +  + ]:         534 :     if (rom != 0) {
      53                 :             :         ow_encode_match_rom(&p, rom);
      54                 :             :     } else {
      55                 :             :         ow_encode_skip_rom(&p);
      56                 :             :     }
      57                 :         534 :     onewire_encode_byte(p, DS18B20_WRITE_SCRATCHPAD);
      58                 :         534 :     p += DS18B20_BITS_PER_BYTE;
      59                 :         534 :     onewire_encode_byte(p, DS18B20_RES_TH);
      60                 :         534 :     p += DS18B20_BITS_PER_BYTE;
      61                 :         534 :     onewire_encode_byte(p, DS18B20_RES_TL);
      62                 :         534 :     p += DS18B20_BITS_PER_BYTE;
      63                 :        1068 :     onewire_encode_byte(p, res_config_byte(res));
      64         [ +  + ]:         534 :     res_ctx.slots = use_match ? DS18B20_RES_SLOTS_MAX : DS18B20_RES_SLOTS_MIN;
      65                 :         534 :     phase_pulses.res[res_ctx.slots] = ONEWIRE_RELEASE_PULSE;
      66                 :         534 : }
      67                 :             : 
      68                 :             : /**
      69                 :             :  * @brief Start a non-blocking resolution change
      70                 :             :  * @param[in] bits New resolution in bits: DS18B20_RES_MIN (9) .. DS18B20_RES_MAX (12)
      71                 :             :  * @note Out-of-range values are ignored. The change is scheduled only between
      72                 :             :  *       measurement cycles and only while the device search is idle; otherwise
      73                 :             :  *       it is ignored. While running, it owns TIM1/DMA; poll it with
      74                 :             :  *       ds18b20_set_resolution_poll() until it reports completion, then call
      75                 :             :  *       ds18b20_start_measure() to request a measurement with the new resolution.
      76                 :             :  */
      77                 :         734 : void ds18b20_set_resolution(uint8_t bits) {
      78   [ +  +  +  + ]:         734 :     if (bits < DS18B20_RES_MIN || bits > DS18B20_RES_MAX) {
      79                 :         100 :         return; // out of range - ignore
      80                 :             :     }
      81         [ +  + ]:         634 :     if ((res_ctx.flags & DS18B20_RES_FLAG_FINISHED) == 0u) {
      82                 :          25 :         return; // a resolution change is already running
      83                 :             :     }
      84         [ +  + ]:         609 :     if ((txn_ctx.flags & DS18B20_TXN_FLAG_FINISHED) == 0u) {
      85                 :          25 :         return; // a command transaction is running
      86                 :             :     }
      87         [ +  + ]:         584 :     if (onewire_search_active()) {
      88                 :          25 :         return; // the device search owns the timer
      89                 :             :     }
      90         [ +  + ]:         559 :     if (ctx.current_state != DS18B20_ST_IDLE) {
      91                 :          25 :         return; // a measurement cycle is in progress
      92                 :             :     }
      93                 :         534 :     res_ctx.pending_res = bits;
      94                 :             :     /* Fresh change: neither applied nor finished yet. */
      95                 :         534 :     res_ctx.flags = 0u;
      96         [ +  + ]:         534 :     build_res_pulses(bits); // Pre-build the config write for the current address mode
      97                 :         534 :     res_ctx.phase = DS18B20_RES_RESET;
      98                 :         534 :     onewire_reset(ctx.capture); // Schedule the first hardware operation
      99                 :             : }
     100                 :             : 
     101                 :             : /**
     102                 :             :  * @brief Advance the non-blocking resolution change by one hardware operation
     103                 :             :  * @return 1 when the change is finished (successfully or aborted), 0 while running
     104                 :             :  * @note When this returns 1 the next measurement uses the requested resolution
     105                 :             :  *       if (and only if) the config write actually completed; an aborted change
     106                 :             :  *       (e.g. no device present) leaves the resolution unchanged.
     107                 :             :  */
     108                 :        1820 : uint8_t ds18b20_set_resolution_poll(void) {
     109         [ +  + ]:        1820 :     if ((res_ctx.flags & DS18B20_RES_FLAG_FINISHED) != 0u) {
     110                 :         309 :         return 1;
     111                 :             :     }
     112                 :             : 
     113         [ +  + ]:        1511 :     if (res_ctx.phase == DS18B20_RES_DONE) {
     114                 :             :         // The last hardware operation completed (config written or aborted).
     115                 :             :         // The timer stays idle: the next ds18b20_start_measure() picks it up,
     116                 :             :         // so a resolution change never starts a measurement on its own.
     117         [ +  + ]:         384 :         if ((res_ctx.flags & DS18B20_RES_FLAG_APPLIED) != 0u) {
     118                 :         359 :             ctx.resolution = res_ctx.pending_res;
     119                 :             :         }
     120                 :         384 :         res_ctx.flags |= DS18B20_RES_FLAG_FINISHED;
     121                 :         384 :         return 1;
     122                 :             :     }
     123                 :             : 
     124                 :             :     // Wait for the currently scheduled hardware operation to complete.
     125                 :             :     // This is a non-blocking poll, not a busy-wait.
     126                 :        1127 :     uint8_t res_done = ow_op_done();
     127                 :             : 
     128         [ +  + ]:        1127 :     if (!res_done) {
     129                 :         334 :         return 0;
     130                 :             :     }
     131                 :             : 
     132      [ +  +  - ]:         793 :     switch (res_ctx.phase) {
     133                 :         434 :     case DS18B20_RES_RESET:
     134                 :             :         // Reset completed: a presence pulse means at least one device is on
     135                 :             :         // the bus, so send the config write for the requested resolution.
     136                 :             :         {
     137                 :         434 :             uint8_t res_present = ow_has_presence(ctx.capture);
     138         [ +  + ]:         434 :             if (!res_present) {
     139                 :          25 :                 res_ctx.phase = DS18B20_RES_DONE;
     140                 :          25 :                 break;
     141                 :             :             }
     142                 :             :         }
     143                 :         409 :         onewire_write_slots(phase_pulses.res, res_ctx.slots);
     144                 :         409 :         res_ctx.phase = DS18B20_RES_WRITE;
     145                 :         409 :         break;
     146                 :             : 
     147                 :         359 :     case DS18B20_RES_WRITE:
     148                 :             :         // Config write completed: the sensor now uses the new resolution.
     149                 :         359 :         res_ctx.flags |= DS18B20_RES_FLAG_APPLIED;
     150                 :         359 :         res_ctx.phase = DS18B20_RES_DONE;
     151                 :         359 :         break;
     152                 :             : 
     153                 :           0 :     case DS18B20_RES_DONE:
     154                 :             :     default:
     155                 :           0 :         break;
     156                 :             :     }
     157                 :             : 
     158                 :         793 :     return 0;
     159                 :             : }
     160                 :             : 
     161                 :             : /**
     162                 :             :  * @brief Current conversion resolution in bits
     163                 :             :  * @return Resolution in bits (9..12); the default is 12
     164                 :             :  * @note Auto-derived from the last valid scratchpad read (byte 4, R1/R0),
     165                 :             :  *       so it also tracks a resolution changed externally.
     166                 :             :  */
     167                 :         700 : uint8_t ds18b20_get_resolution(void) { return ctx.resolution; }
        

Generated by: LCOV version 2.0-1