@@ -25,7 +25,6 @@ K_SEM_DEFINE(adc_sem, 0, 1);
2525static ADC_InitSingle_TypeDef initSingle_bv = ADC_INITSINGLE_DEFAULT ;
2626static ADC_InitSingle_TypeDef initSingle_hwid = ADC_INITSINGLE_DEFAULT ;
2727
28- #ifdef HWID_APORT
2928/**
3029 * @brief Set the VBAT_SNS_EN Pin High to enable ADC readings
3130 *
@@ -40,14 +39,19 @@ static void set_vbat_sens_en(bool enable)
4039 GPIO_PinModeSet (VBAT_EN_PORT , VBAT_EN_PIN , gpioModePushPull , enable );
4140#endif /* VBAT_EN_PORT */
4241}
43- #endif /* HWID_APORT */
4442
4543/**
4644 * @brief Initialize the Gecko ADC
4745 *
4846 */
49- void initADC (void )
47+ static void initADC (void )
5048{
49+ static bool lazy_init = false; /* perform the init only once */
50+ /* use lazy init, to remove the call from tmo_shell.c */
51+ if (lazy_init ) {
52+ return ;
53+ }
54+ lazy_init = true;
5155
5256 // Enable ADC0 clock
5357 CMU_ClockEnable (cmuClock_ADC0 , true);
@@ -77,18 +81,14 @@ void initADC(void)
7781 init .timebase = ADC_TimebaseCalc (0 );
7882
7983 ADC_Init (ADC0 , & init );
80-
81- int hwid = read_hwid ();
82-
83- LOG_INF ("HWID = %d\n" , hwid );
8484}
8585
8686/*
8787 * @brief This function writes the amount of battery charge remaining
8888 * (to the nearest 1%) in bv.
8989 * It returns true if successful, or false if there is an issue
9090 */
91- bool millivolts_to_percent (uint32_t millivolts , uint8_t * percent ) {
91+ __weak bool millivolts_to_percent (uint32_t millivolts , uint8_t * percent ) {
9292 float curBv = get_remaining_capacity ((float ) millivolts / 1000 );
9393 * percent = (uint8_t ) (curBv + 0.5 );
9494 return true;
@@ -97,9 +97,11 @@ bool millivolts_to_percent(uint32_t millivolts, uint8_t *percent) {
9797/**
9898 * @brief Main function
9999 */
100- int read_battery_voltage (void )
100+ __weak int read_battery_voltage (void )
101101{
102102#ifdef VBAT_APORT
103+ initADC ();
104+
103105 uint32_t sample ;
104106 uint32_t millivolts ;
105107 float millivolts_f ;
@@ -140,9 +142,11 @@ int read_battery_voltage(void)
140142 *
141143 * @return int Millivolts
142144 */
143- int read_hwid (void )
145+ __weak int read_hwid (void )
144146{
145147#ifdef HWID_APORT
148+ initADC ();
149+
146150 uint32_t sample ;
147151 uint32_t millivolts ;
148152 float millivolts_f ;
0 commit comments