24 #include "hal_pin_analog_input.h" 25 #include "hal_nop_delay.h" 27 #include "hal_clocks.h" 29 #include "hal_nop_delay.h" 31 #include "pir_sense.h" 33 #include "profiler_timer.h" 34 #include "simple_adc.h" 35 #include "mcp4012_x.h" 36 #include "uart_printf.h" 37 #include "tinyprintf.h" 38 #include "mcp4012_x.h" 41 #include "hw_testing_app.h" 44 #define TIMER_INIT_VALUE (read_time_us()) 45 #define CALC_TIMER_VAL 1000000 49 #define CALC_OFFSET simple_adc_get_value(SIMPLE_ADC_GAIN1_5, PIN_TO_ANALOG_INPUT(BATT_VOLTAGE_SENSE))/2 56 static uint32_t my_sqrt(uint32_t num);
65 static uint32_t compare_percent(uint32_t data, uint32_t ref,
float per);
72 static uint32_t hw_test_obs_sig();
81 static uint32_t hw_test_obs_freq();
86 MY_LFCLK_SRC_RC = CLOCK_LFCLKSRC_SRC_RC,
87 MY_LFCLK_SRC_Xtal = CLOCK_LFCLKSRC_SRC_Xtal,
88 MY_LFCLK_SRC_Synth = CLOCK_LFCLKSRC_SRC_Synth
95 static uint32_t my_lfclk_init(my_lfclk_src_t my_lfclk_src);
100 void capture_lfclk_test(
void);
103 static uint32_t timer_val_us;
109 uint32_t compare_percent(uint32_t data, uint32_t ref,
float per)
111 if((ref-(ref*per/100))<=data && data<=(ref+(ref*per/100)))
121 static uint32_t my_sqrt(uint32_t num)
123 float xn,xm,flag, temp;
124 xn = 0; xm = 800; flag = 0;
127 xn = (xm + (num/xm))/2;
138 return (uint32_t) temp;
146 hal_gpio_pin_set(LED_RED);
147 log_printf(
"Power_test : 1\n");
148 hal_nop_delay_ms(100);
149 hal_gpio_pin_clear(LED_RED);
155 NRF_POWER->DCDCEN = POWER_DCDCEN_DCDCEN_Enabled << POWER_DCDCEN_DCDCEN_Pos;
156 if(NRF_POWER->DCDCEN == (POWER_DCDCEN_DCDCEN_Enabled << POWER_DCDCEN_DCDCEN_Pos))
158 log_printf(
"DC_DC_test : 1\n");
163 log_printf(
"DC_DC_test : 0\n");
170 for(uint32_t led_cnt; led_cnt < 15; led_cnt ++)
172 hal_gpio_pin_set(LED_GREEN);
173 hal_nop_delay_ms(500);
174 hal_gpio_pin_clear(LED_GREEN);
175 hal_gpio_pin_set(LED_RED);
176 hal_nop_delay_ms(500);
177 hal_gpio_pin_clear(LED_RED);
178 log_printf(
"LED_test : 1\n");
188 static uint32_t my_lfclk_init(my_lfclk_src_t my_lfclk_src)
190 uint32_t my_xtal_flag = 0;
191 if((NRF_CLOCK->LFCLKSTAT &
192 (CLOCK_LFCLKSTAT_STATE_Running << CLOCK_LFCLKSTAT_STATE_Pos)) &&
194 ((NRF_CLOCK->LFCLKSTAT & CLOCK_LFCLKSTAT_SRC_Msk) == my_lfclk_src))
201 NRF_CLOCK->LFCLKSRC = (my_lfclk_src << CLOCK_LFCLKSRC_SRC_Pos);
202 NRF_CLOCK->INTENSET = CLOCK_INTENSET_LFCLKSTARTED_Msk;
203 NVIC_ClearPendingIRQ(POWER_CLOCK_IRQn);
206 SCB->SCR |= SCB_SCR_SEVONPEND_Msk;
208 NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
209 (void)NRF_CLOCK->EVENTS_LFCLKSTARTED;
210 NRF_CLOCK->TASKS_LFCLKSTART = 1;
211 hal_nop_delay_ms(500);
213 if (NRF_CLOCK->EVENTS_LFCLKSTARTED == 1)
218 NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
219 (void)NRF_CLOCK->EVENTS_LFCLKSTARTED;
220 NVIC_ClearPendingIRQ(POWER_CLOCK_IRQn);
221 NRF_CLOCK->INTENCLR = CLOCK_INTENCLR_LFCLKSTARTED_Msk;
230 uint32_t check_status_xtal = 0;
232 check_status_xtal = my_lfclk_init(MY_LFCLK_SRC_Xtal);
235 hal_nop_delay_ms(1020);
236 flag = compare_percent(timer_val_us, CALC_TIMER_VAL, 1);
238 if (check_status_xtal)
240 log_printf(
"Crystal : 1\n");
244 log_printf(
"Crystal : 0\n");
246 if (flag && check_status_xtal)
248 log_printf(
"Crystal_test : 1\n");
253 log_printf(
"Crystal_test : 0\n");
259 void capture_lfclk_test (
void)
262 log_printf(
"Timer_XTAL : %d\n", timer_val_us);
263 log_printf(
"Timer_TIMER : %d\n", CALC_TIMER_VAL);
271 uint32_t obs_offset = 0, avg_offset = 0, i = 0, temp_sum = 0;
277 temp_sum = temp_sum + obs_offset;
280 avg_offset = temp_sum / i;
282 log_printf(
"CALC_OFFSET : %lu\n", CALC_OFFSET);
283 log_printf(
"Avg_Offset : %lu\n", avg_offset);
284 if(compare_percent(avg_offset, CALC_OFFSET, 5))
286 log_printf(
"RC_test : 1\n");
291 log_printf(
"RC_test : 0\n");
299 uint32_t obs_freq = hw_test_obs_freq();
300 if(obs_freq == MAX_FREQ)
302 log_printf(
"Freq_filter_test : 1\n");
307 log_printf(
"Freq_filter_test : 0\n");
312 static uint32_t hw_test_obs_freq()
314 uint32_t temp_signal = 0, cnt_cross = 0, freq = 0;
315 const uint32_t temp_offset = CALC_OFFSET;
319 }
while(!(compare_percent(temp_signal, temp_offset, 0.1) &&((
int)(temp_signal - temp_offset) <= 0)));
325 if(compare_percent(temp_offset,temp_signal,0.2))
333 log_printf(
"Frequency : %d\n", freq);
344 hal_nop_delay_ms(10);
345 read1 = hw_test_obs_sig();
346 log_printf(
"POT_test_Read1 : %d\n", read1);
349 hal_nop_delay_ms(10);
350 read2 = hw_test_obs_sig();
351 log_printf(
"POT_test_Read2 : %d\n", read2);
352 log_printf(
"ratio_Read1_Read2 : %d\n", ((read1*1000)/read2));
354 if(!compare_percent(read1, read2, 100))
356 log_printf(
"POT_test : 1\n");
361 log_printf(
"POT_test : 0\n");
366 static uint32_t hw_test_obs_sig()
368 uint32_t temp_signal, avg_signal, rms, i;
370 temp_signal = 0; avg_signal = 0; temp_sum = 0, i = 0;
371 const uint32_t temp_offset = CALC_OFFSET;
374 }
while((!compare_percent(temp_signal, temp_offset, 0.1)) && (((
int)(temp_signal - temp_offset)) > 0));
379 temp_sum = (temp_sum + (((int)(temp_signal-temp_offset))*((
int)(temp_signal-temp_offset))));
383 avg_signal = temp_sum / i;
384 rms = my_sqrt(avg_signal);
#define PIR_AMP_OFFSET_PIN
void profiler_timer_init(void)
uint32_t simple_adc_get_value(simple_adc_gain_t gain, simple_adc_input_t pin)
This function initializes the SAADC peripheral, gets an ADC value and then deinitializes The function...
void profiler_timer_deinit()
Fully stop the profiling timer to save power. profiler_timer_init needs to be called again before usi...
#define PIR_AMP_SIGNAL_PIN
void ms_timer_init(uint32_t irq_priority)
void mcp4012_set_value(uint32_t value_sent_by_user)
This function is used to set the value of MCP4012 wiper value.
void ms_timer_start(ms_timer_num id, ms_timer_mode mode, uint64_t ticks, void(*handler)(void))
void ms_timer_stop(ms_timer_num id)
One shot call of the timer.
uint32_t read_time_us(void)