main
John Lauer Bind output-locked temperature diagnostics to selected NTCLE428 sensor d2b6d40
#include <assert.h>
#include <math.h>
#include <stdio.h>
#define ADOM_BRINGUP_OUTPUT_LOCK 1
#include "../firmware/stm32/adc_input.c"
const unsigned short test_factory_cal=1650;
static uint32_t tick;
static int selected=-1;
static unsigned calls,fail_at;
static uint16_t ntc_code=2048;
static bool started;
static HAL_StatusTypeDef step(void){return ++calls==fail_at?HAL_ERROR:HAL_OK;}
uint32_t HAL_GetTick(void){return tick;}
void HAL_Delay(uint32_t t){tick+=t;}
void HAL_GPIO_Init(void *p,GPIO_InitTypeDef *g){assert(p==GPIOA&&g->Pin==3&&g->Mode==GPIO_MODE_ANALOG&&g->Pull==GPIO_NOPULL);}
HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef *a){assert(a->Instance==ADC1&&a->Init.Resolution==12&&a->Init.ClockPrescaler==4);return step();}
HAL_StatusTypeDef HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef *a){(void)a;return step();}
HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef *a,ADC_ChannelConfTypeDef *c){
 (void)a;assert(!started);if(step()!=HAL_OK)return HAL_ERROR;
 if(c->Rank==ADC_RANK_NONE){assert(selected==(int)c->Channel);selected=-1;}
 else{assert(selected==-1);selected=(int)c->Channel;}return HAL_OK;
}
HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef *a){(void)a;assert(selected>=0&&!started);if(step()!=HAL_OK)return HAL_ERROR;started=true;return HAL_OK;}
HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef *a,uint32_t timeout){(void)a;assert(started&&timeout==2);return step();}
uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef *a){(void)a;assert(started);return selected==13?1500:selected==1?564:ntc_code;}
HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef *a){(void)a;started=false;return step();}
static void reset(void){tick=0;calls=fail_at=0;selected=-1;started=false;ntc_code=2048;}
int main(void){
 reset();assert(load_adc_input_init());float v=-1,t=-1;uint32_t s=99;
 assert(!load_adc_input_read(&v,&t,&s));tick=10;load_adc_input_poll();
 assert(load_adc_input_read(&v,&t,&s)&&s==10&&fabsf(v-5)<.005f&&fabsf(t-25)<.03f);
 unsigned sequence_calls=calls;assert(selected==-1);
 tick=61;assert(!load_adc_input_read(&v,&t,&s));
 /* Fail every HAL operation, both initialization and a complete scan. */
 for(unsigned i=1;i<=sequence_calls;i++){
  reset();fail_at=i;bool init_ok=load_adc_input_init();
  if(init_ok){tick=10;load_adc_input_poll();}
  assert(!load_adc_input_read(&v,&t,&s));
  unsigned n=calls;tick=100;load_adc_input_poll();assert(calls==n);
 }
 /* Saturated and electrically plausible codes outside the selected sensor
    table all invalidate diagnostics; later good samples cannot clear it. */
 const uint16_t bad_codes[]={4095,100,4000};
 for(unsigned i=0;i<3;i++){
  reset();assert(load_adc_input_init());ntc_code=bad_codes[i];tick=10;load_adc_input_poll();
  assert(!load_adc_input_read(&v,&t,&s));
  ntc_code=2048;unsigned n=calls;tick=20;load_adc_input_poll();
  assert(calls==n&&!load_adc_input_read(&v,&t,&s));
 }
 reset();tick=UINT32_MAX-20;assert(load_adc_input_init());tick=UINT32_MAX-10;load_adc_input_poll();
 assert(load_adc_input_read(&v,&t,&s));tick=40;assert(!load_adc_input_read(&v,&t,&s));
 puts("ADC adapter fake-HAL: channel isolation, every-operation failure, saturation and age/wrap passed.");
}