2727#include <pbdrv/usb.h>
2828#include <pbio/error.h>
2929#include <pbio/util.h>
30+ #include <pbio/os.h>
3031
3132#include "../core.h"
3233#include "charger_mp2639a.h"
3334
3435#define platform pbdrv_charger_mp2639a_platform_data
3536
36- PROCESS (pbdrv_charger_mp2639a_process , "MP2639A" );
37-
3837#if PBDRV_CONFIG_CHARGER_MP2639A_MODE_PWM
3938static pbdrv_pwm_dev_t * mode_pwm ;
4039#endif
@@ -45,11 +44,6 @@ static pbdrv_pwm_dev_t *iset_pwm;
4544static pbdrv_charger_status_t pbdrv_charger_status ;
4645static bool mode_pin_is_low ;
4746
48- void pbdrv_charger_init (void ) {
49- pbdrv_init_busy_up ();
50- process_start (& pbdrv_charger_mp2639a_process );
51- }
52-
5347pbio_error_t pbdrv_charger_get_current_now (uint16_t * current ) {
5448 pbio_error_t err = pbdrv_adc_get_ch (platform .ib_adc_ch , current );
5549 if (err != PBIO_SUCCESS ) {
@@ -182,18 +176,23 @@ static bool read_chg(void) {
182176#define PBDRV_CHARGER_MP2639A_CHARGE_TIMEOUT_MS (60 * 60 * 1000)
183177#define PBDRV_CHARGER_MP2639A_CHARGE_PAUSE_MS (30 * 1000)
184178
185- PROCESS_THREAD (pbdrv_charger_mp2639a_process , ev , data ) {
186- PROCESS_BEGIN ();
179+ static pbio_os_process_t pbdrv_charger_mp2639a_process ;
180+
181+ pbio_error_t pbdrv_charger_mp2639a_process_thread (pbio_os_state_t * state ) {
182+
183+ static pbio_os_timer_t timer ;
184+
185+ ASYNC_BEGIN (state );
187186
188187 #if PBDRV_CONFIG_CHARGER_MP2639A_MODE_PWM
189188 while (pbdrv_pwm_get_dev (platform .mode_pwm_id , & mode_pwm ) != PBIO_SUCCESS ) {
190- PROCESS_PAUSE ( );
189+ AWAIT_MS ( state , & timer , 1 );
191190 }
192191 #endif
193192
194193 #if PBDRV_CONFIG_CHARGER_MP2639A_ISET_PWM
195194 while (pbdrv_pwm_get_dev (platform .iset_pwm_id , & iset_pwm ) != PBIO_SUCCESS ) {
196- PROCESS_PAUSE ( );
195+ AWAIT_MS ( state , & timer , 1 );
197196 }
198197 #endif
199198
@@ -212,12 +211,10 @@ PROCESS_THREAD(pbdrv_charger_mp2639a_process, ev, data) {
212211
213212 static bool chg_samples [7 ];
214213 static uint8_t chg_index = 0 ;
215- static struct etimer timer ;
216214 static uint32_t charge_count = 0 ;
217215
218216 for (;;) {
219- etimer_set (& timer , PBDRV_CHARGER_MP2639A_STATUS_SAMPLE_TIME );
220- PROCESS_WAIT_EVENT_UNTIL (ev == PROCESS_EVENT_TIMER && etimer_expired (& timer ));
217+ AWAIT_MS (state , & timer , PBDRV_CHARGER_MP2639A_STATUS_SAMPLE_TIME );
221218
222219 // Enable charger chip based on USB state. We don't need to disable it
223220 // on charger fault since the chip will automatically disable itself.
@@ -275,13 +272,17 @@ PROCESS_THREAD(pbdrv_charger_mp2639a_process, ev, data) {
275272 if (charge_count > (PBDRV_CHARGER_MP2639A_CHARGE_TIMEOUT_MS / PBDRV_CHARGER_MP2639A_STATUS_SAMPLE_TIME )) {
276273 pbdrv_charger_status = PBDRV_CHARGER_STATUS_DISCHARGE ;
277274 pbdrv_charger_enable (false, PBDRV_CHARGER_LIMIT_NONE );
278- etimer_set (& timer , PBDRV_CHARGER_MP2639A_CHARGE_PAUSE_MS );
279- PROCESS_WAIT_EVENT_UNTIL (etimer_expired (& timer ));
275+ AWAIT_MS (state , & timer , PBDRV_CHARGER_MP2639A_CHARGE_PAUSE_MS );
280276 charge_count = 0 ;
281277 }
282278 }
283279
284- PROCESS_END ();
280+ ASYNC_END (PBIO_SUCCESS );
281+ }
282+
283+ void pbdrv_charger_init (void ) {
284+ pbdrv_init_busy_up ();
285+ pbio_os_start_process (& pbdrv_charger_mp2639a_process , pbdrv_charger_mp2639a_process_thread );
285286}
286287
287288#endif // PBDRV_CONFIG_CHARGER_MP2639A
0 commit comments