LCOV - code coverage report
Current view: top level - src - ow_stats.c (source / functions) Coverage Total Hit
Test: stm32-async-1wire core coverage Lines: 96.2 % 131 126
Test Date: 2026-11-28 11:52:24 Functions: 66.7 % 15 10
Branches: 97.1 % 68 66

             Branch data     Line data    Source code
       1                 :             : /**
       2                 :             :  * @file ow_stats.c
       3                 :             :  * @brief 1-Wire signal statistics implementation.
       4                 :             :  * @see ow_stats.h for the public API.
       5                 :             :  */
       6                 :             : 
       7                 :             : #include "ow_stats.h"
       8                 :             : #include "ds18b20.h"
       9                 :             : #include "onewire.h"
      10                 :             : 
      11                 :             : #if OW_STATS_ENABLE
      12                 :             : 
      13                 :             : #include <string.h>
      14                 :             : 
      15                 :             : #if defined(OW_FAMILY_F0)
      16                 :             : #include "stm32f0xx.h"
      17                 :             : #elif defined(OW_FAMILY_F3)
      18                 :             : #include "stm32f3xx.h"
      19                 :             : #elif defined(OW_FAMILY_F4)
      20                 :             : #include "stm32f4xx.h"
      21                 :             : #elif defined(OW_FAMILY_G0)
      22                 :             : #include "stm32g0xx.h"
      23                 :             : #elif defined(OW_FAMILY_G4)
      24                 :             : #include "stm32g4xx.h"
      25                 :             : #elif defined(OW_FAMILY_L4)
      26                 :             : #include "stm32l4xx.h"
      27                 :             : #elif defined(OW_FAMILY_C5)
      28                 :             : #include "stm32c5xx.h"
      29                 :             : #elif defined(OW_FAMILY_H5)
      30                 :             : #include "stm32h5xx.h"
      31                 :             : #elif defined(OW_FAMILY_U5)
      32                 :             : #include "stm32u5xx.h"
      33                 :             : #elif defined(OW_FAMILY_H7)
      34                 :             : #include "stm32h7xx.h"
      35                 :             : #else
      36                 :             : #include "stm32f1xx.h"
      37                 :             : #endif
      38                 :             : 
      39                 :             : /* ---- private state ---- */
      40                 :             : 
      41                 :             : static ow_stats_t st;
      42                 :             : 
      43                 :             : /** Non-blocking dump state. */
      44                 :             : static uint32_t dump_phase; /**< 0 = idle, 1+ = active */
      45                 :             : static uint32_t dump_sensor; /**< current sensor index */
      46                 :             : 
      47                 :             : /* ---- histogram helpers ---- */
      48                 :             : 
      49                 :    14063700 : static uint8_t hist_bucket(uint8_t pulse) {
      50         [ +  + ]:    14063700 :     if (pulse <= 2) return 0;
      51         [ +  + ]:    14063250 :     if (pulse <= 4) return 1;
      52         [ +  + ]:    14063150 :     if (pulse <= 6) return 2;
      53         [ +  + ]:    14036075 :     if (pulse <= 9) return 3;
      54         [ +  + ]:    14035925 :     if (pulse <= 12) return 4;
      55         [ +  + ]:       33000 :     if (pulse <= 14) return 5;
      56         [ +  + ]:       32950 :     if (pulse <= 19) return 6;
      57         [ +  + ]:       32575 :     if (pulse <= 24) return 7;
      58         [ +  + ]:       32325 :     if (pulse <= 29) return 8;
      59         [ +  + ]:       32275 :     if (pulse <= 39) return 9;
      60         [ +  + ]:       32225 :     if (pulse <= 49) return 10;
      61         [ +  + ]:       32175 :     if (pulse <= 59) return 11;
      62                 :       32075 :     return 12;
      63                 :             : }
      64                 :             : 
      65                 :             : /* ---- sensor lookup ---- */
      66                 :             : 
      67                 :     1751825 : static uint8_t sensor_find_or_alloc(const uint8_t* rom) {
      68         [ +  + ]:     1751825 :     if (!rom) return OW_STATS_MAX_SENSORS;
      69         [ +  + ]:     1752275 :     for (uint8_t i = 0; i < st.sensor_count; i++) {
      70         [ +  + ]:     1751300 :         if (memcmp(st.sensors[i].rom, rom, 8) == 0) return i;
      71                 :             :     }
      72         [ +  + ]:         975 :     if (st.sensor_count < OW_STATS_MAX_SENSORS) {
      73                 :         950 :         uint8_t idx = st.sensor_count++;
      74                 :         950 :         memcpy(st.sensors[idx].rom, rom, 8);
      75                 :         950 :         st.sensors[idx].min_pulse = 0xFF;
      76                 :         950 :         st.sensors[idx].max_pulse = 0;
      77                 :         950 :         return idx;
      78                 :             :     }
      79                 :          25 :     return OW_STATS_MAX_SENSORS;
      80                 :             : }
      81                 :             : 
      82                 :             : /* ---- public API ---- */
      83                 :             : 
      84                 :        8894 : void ow_stats_init(void) {
      85                 :        8894 :     memset(&st, 0, sizeof(st));
      86                 :        8894 :     dump_phase = 0;
      87                 :        8894 : }
      88                 :             : 
      89                 :     1751375 : void ow_stats_capture_pulse(const volatile uint8_t* pulse, uint8_t n,
      90                 :             :                             const uint8_t* rom) {
      91                 :     1751375 :     uint8_t si = sensor_find_or_alloc(rom);
      92         [ +  + ]:    15815075 :     for (uint8_t i = 0; i < n; i++) {
      93                 :    14063700 :         uint8_t p = pulse[i];
      94                 :    14063700 :         st.histogram[hist_bucket(p)]++;
      95         [ +  + ]:    14063700 :         if (si < OW_STATS_MAX_SENSORS) {
      96         [ +  + ]:    14028400 :             if (p < st.sensors[si].min_pulse) st.sensors[si].min_pulse = p;
      97         [ +  + ]:    14028400 :             if (p > st.sensors[si].max_pulse) st.sensors[si].max_pulse = p;
      98                 :             :         }
      99                 :             :     }
     100         [ +  + ]:     1751375 :     if (si < OW_STATS_MAX_SENSORS) {
     101                 :     1750750 :         st.sensors[si].count++;
     102                 :             :     }
     103                 :     1751375 : }
     104                 :             : 
     105                 :         450 : void ow_stats_count_error(int16_t error, const uint8_t* rom) {
     106                 :         450 :     uint8_t si = sensor_find_or_alloc(rom);
     107         [ +  + ]:         450 :     if (si < OW_STATS_MAX_SENSORS) {
     108      [ +  +  + ]:         425 :         switch (error) {
     109                 :         325 :         case DS18B20_TEMP_ERROR_CRC_FAIL:
     110                 :         325 :             st.sensors[si].crc_err++;
     111                 :         325 :             break;
     112                 :          75 :         case DS18B20_TEMP_ERROR_NO_SENSOR:
     113                 :          75 :             st.sensors[si].no_presence++;
     114                 :          75 :             break;
     115                 :          25 :         default:
     116                 :          25 :             st.sensors[si].generic_err++;
     117                 :          25 :             break;
     118                 :             :         }
     119                 :             :     }
     120                 :         450 :     st.total_errors++;
     121                 :         450 : }
     122                 :             : 
     123                 :          75 : void ow_stats_capture_overrun(void) {
     124                 :          75 :     st.capture_overruns++;
     125                 :          75 : }
     126                 :             : 
     127                 :          50 : void ow_stats_dump_start(void) {
     128                 :          50 :     dump_phase = 1;
     129                 :          50 :     dump_sensor = 0;
     130                 :          50 : }
     131                 :             : 
     132                 :         125 : uint8_t ow_stats_dump_poll(void) {
     133         [ -  + ]:         125 :     if (dump_phase == 0) return 1;
     134                 :             : 
     135                 :             :     /* Each poll call enqueues one line into the non-blocking UART TX ring
     136                 :             :      * buffer.  The main loop drains the buffer via uart_poll_tx(), so nothing
     137                 :             :      * here blocks on the USART.  UART_TX_BUF_SIZE is chosen per app (default
     138                 :             :      * 128 B, examples 256 B, the 6_statistics example 1024 B) so the longest single line fits
     139                 :             :      * without overflowing; the dump itself streams across poll calls. */
     140   [ +  +  +  +  :         125 :     switch (dump_phase) {
                      - ]
     141                 :          25 :     case 1:
     142                 :          25 :         ow_stats_puts("--- stats [");
     143                 :          25 :         ow_stats_print_int(st.total_cycles);
     144                 :          25 :         ow_stats_puts(" c] ---\r\n");
     145                 :          25 :         dump_phase = 2;
     146                 :          25 :         dump_sensor = 0;
     147                 :          25 :         break;
     148                 :             : 
     149                 :          50 :     case 2:
     150         [ +  + ]:          50 :         if (dump_sensor < st.sensor_count) {
     151                 :          25 :             const ow_stats_sensor_t* s = &st.sensors[dump_sensor];
     152         [ +  + ]:         225 :             for (uint32_t j = 0; j < 8; j++) {
     153                 :         200 :                 ow_stats_print_hex(s->rom[j]);
     154         [ +  + ]:         200 :                 if (j != 7) ow_stats_tx_enqueue(' ');
     155                 :             :             }
     156                 :          25 :             ow_stats_tx_enqueue(':');
     157                 :          25 :             ow_stats_print_int(s->min_pulse);
     158                 :          25 :             ow_stats_tx_enqueue('-');
     159                 :          25 :             ow_stats_print_int(s->max_pulse);
     160                 :          25 :             ow_stats_tx_enqueue(' ');
     161                 :          25 :             ow_stats_tx_enqueue('n');
     162                 :          25 :             ow_stats_print_int(s->count);
     163                 :          25 :             ow_stats_tx_enqueue(' ');
     164                 :          25 :             ow_stats_tx_enqueue('e');
     165                 :          25 :             ow_stats_print_int(s->crc_err + s->no_presence + s->generic_err);
     166                 :          25 :             ow_stats_puts("\r\n");
     167                 :          25 :             dump_sensor++;
     168                 :             :         } else {
     169                 :          25 :             dump_phase = 3;
     170                 :             :         }
     171                 :          50 :         break;
     172                 :             : 
     173                 :          25 :     case 3:
     174                 :          25 :         ow_stats_puts("h:");
     175         [ +  + ]:         425 :         for (uint32_t i = 0; i < OW_STATS_HIST_BUCKETS; i++) {
     176         [ +  + ]:         400 :             if (st.histogram[i]) {
     177                 :          25 :                 ow_stats_print_int(i);
     178                 :          25 :                 ow_stats_tx_enqueue('=');
     179                 :          25 :                 ow_stats_print_int(st.histogram[i]);
     180                 :          25 :                 ow_stats_tx_enqueue(' ');
     181                 :             :             }
     182                 :             :         }
     183                 :          25 :         ow_stats_puts("\r\n");
     184                 :          25 :         dump_phase = 4;
     185                 :          25 :         break;
     186                 :             : 
     187                 :          25 :     case 4:
     188                 :          25 :         ow_stats_puts("t=");
     189                 :          25 :         ow_stats_print_int(st.total_cycles);
     190                 :          25 :         ow_stats_puts("c ");
     191                 :          25 :         ow_stats_print_int(st.total_errors);
     192                 :          25 :         ow_stats_puts("e ");
     193                 :          25 :         ow_stats_print_int(st.capture_overruns);
     194                 :          25 :         ow_stats_puts("o\r\n");
     195                 :          25 :         dump_phase = 0;
     196                 :          25 :         break;
     197                 :             :     }
     198                 :             : 
     199                 :         125 :     return (dump_phase == 0) ? 1 : 0;
     200                 :             : }
     201                 :             : 
     202                 :          25 : void ow_stats_reset(void) {
     203                 :          25 :     uint32_t n = st.sensor_count;
     204                 :             :     uint8_t roms[OW_STATS_MAX_SENSORS][8];
     205         [ +  + ]:          50 :     for (uint32_t i = 0; i < n; i++) {
     206                 :          25 :         memcpy(roms[i], st.sensors[i].rom, 8);
     207                 :             :     }
     208                 :          25 :     memset(&st, 0, sizeof(st));
     209         [ +  + ]:          50 :     for (uint32_t i = 0; i < n; i++) {
     210                 :          25 :         memcpy(st.sensors[i].rom, roms[i], 8);
     211                 :          25 :         st.sensors[i].min_pulse = 0xFF;
     212                 :             :     }
     213                 :          25 :     st.sensor_count = n;
     214                 :          25 : }
     215                 :             : 
     216                 :     1750150 : uint32_t ow_stats_tick(void) {
     217                 :     1750150 :     return ++st.total_cycles;
     218                 :             : }
     219                 :             : 
     220                 :             : /* ---- weak default output callbacks ---- */
     221                 :             : 
     222                 :           0 : __attribute__((weak)) void ow_stats_putchar(char c) { (void)c; }
     223                 :             : 
     224                 :           0 : __attribute__((weak)) void ow_stats_puts(const char* s) { (void)s; }
     225                 :             : 
     226                 :           0 : __attribute__((weak)) void ow_stats_print_int(int32_t v) { (void)v; }
     227                 :             : 
     228                 :           0 : __attribute__((weak)) void ow_stats_print_hex(uint8_t v) { (void)v; }
     229                 :             : 
     230                 :           0 : __attribute__((weak)) void ow_stats_tx_enqueue(char c) { (void)c; }
     231                 :             : 
     232                 :             : #endif /* OW_STATS_ENABLE */
        

Generated by: LCOV version 2.0-1