Skip to content

Commit ea5061e

Browse files
author
Daniel Campora
committed
cc3200: Improve callback API.
Rename "wakes" param to "wake_from" and make "value" an object instead of an integer.
1 parent 4c5bfe2 commit ea5061e

File tree

13 files changed

+46
-38
lines changed

13 files changed

+46
-38
lines changed

cc3200/misc/mpcallback.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ const mp_arg_t mpcallback_init_args[] = {
4646
{ MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
4747
{ MP_QSTR_handler, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
4848
{ MP_QSTR_priority, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
49-
{ MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
50-
{ MP_QSTR_wakes, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = PYB_PWR_MODE_ACTIVE} },
49+
{ MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
50+
{ MP_QSTR_wake_from, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = PYB_PWR_MODE_ACTIVE} },
5151
};
5252

5353
/******************************************************************************
@@ -58,14 +58,14 @@ void mpcallback_init0 (void) {
5858
mp_obj_list_init(&MP_STATE_PORT(mpcallback_obj_list), 0);
5959
}
6060

61-
mp_obj_t mpcallback_new (mp_obj_t parent, mp_obj_t handler, const mp_cb_methods_t *methods) {
61+
mp_obj_t mpcallback_new (mp_obj_t parent, mp_obj_t handler, const mp_cb_methods_t *methods, bool enable) {
6262
mpcallback_obj_t *self = m_new_obj(mpcallback_obj_t);
6363
self->base.type = &pyb_callback_type;
6464
self->handler = handler;
6565
self->parent = parent;
6666
self->methods = (mp_cb_methods_t *)methods;
67-
self->isenabled = true;
68-
// remove any old callback if present
67+
self->isenabled = enable;
68+
// remove it in case it was already registered
6969
mpcallback_remove(self->parent);
7070
mp_obj_list_append(&MP_STATE_PORT(mpcallback_obj_list), self);
7171
return self;
@@ -138,11 +138,11 @@ void mpcallback_handler (mp_obj_t self_in) {
138138
// uncaught exception; disable the callback so that it doesn't run again
139139
self->methods->disable (self->parent);
140140
self->handler = mp_const_none;
141-
// signal the error using the heart beat led and print an
142-
// exception message as well
143-
mperror_signal_error();
141+
// signal the error using the heart beat led and
142+
// by printing a message
144143
printf("Uncaught exception in callback handler\n");
145144
mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
145+
mperror_signal_error();
146146
}
147147
gc_unlock();
148148
}

cc3200/misc/mpcallback.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,11 @@ extern const mp_obj_type_t pyb_callback_type;
6262
DECLARE PUBLIC FUNCTIONS
6363
******************************************************************************/
6464
void mpcallback_init0 (void);
65-
mp_obj_t mpcallback_new (mp_obj_t parent, mp_obj_t handler, const mp_cb_methods_t *methods);
65+
mp_obj_t mpcallback_new (mp_obj_t parent, mp_obj_t handler, const mp_cb_methods_t *methods, bool enable);
6666
mpcallback_obj_t *mpcallback_find (mp_obj_t parent);
6767
void mpcallback_wake_all (void);
6868
void mpcallback_remove (const mp_obj_t parent);
6969
void mpcallback_handler (mp_obj_t self_in);
7070
uint mpcallback_translate_priority (uint priority);
71-
mp_obj_t mpcallback_new (mp_obj_t parent, mp_obj_t handler, const mp_cb_methods_t *methods);
7271

7372
#endif /* MPCALLBACK_H_ */

cc3200/mods/modpyb.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
#include "portable.h"
5757
#include "task.h"
5858
#include "mpexception.h"
59-
#include "mpcallback.h"
6059
#include "random.h"
6160
#include "pybadc.h"
6261
#include "pybi2c.h"

cc3200/mods/modwlan.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,27 +1087,29 @@ STATIC mp_obj_t wlan_scan(mp_obj_t self_in) {
10871087
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wlan_scan_obj, wlan_scan);
10881088

10891089
/// \method callback(handler, pwrmode)
1090-
/// Create a callback object associated with WLAN
1091-
/// min num of arguments is 1 (wakes)
1090+
/// Create a callback object associated with the WLAN subsystem
1091+
/// Only takes one argument (wake_from)
10921092
STATIC mp_obj_t wlan_callback (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
10931093
mp_arg_val_t args[mpcallback_INIT_NUM_ARGS];
10941094
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, mpcallback_INIT_NUM_ARGS, mpcallback_init_args, args);
10951095

10961096
wlan_obj_t *self = pos_args[0];
10971097
mp_obj_t _callback = mpcallback_find(self);
10981098
// check if any parameters were passed
1099-
if (kw_args->used > 0 || !_callback) {
1099+
if (kw_args->used > 0) {
11001100
// check the power mode
11011101
if (args[4].u_int != PYB_PWR_MODE_LPDS) {
11021102
// throw an exception since WLAN only supports LPDS mode
11031103
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
11041104
}
11051105

11061106
// create the callback
1107-
_callback = mpcallback_new (self, args[1].u_obj, &wlan_cb_methods);
1107+
_callback = mpcallback_new (self, args[1].u_obj, &wlan_cb_methods, true);
11081108

11091109
// enable network wakeup
11101110
pybsleep_set_wlan_lpds_callback (_callback);
1111+
} else if (!_callback) {
1112+
_callback = mpcallback_new (self, mp_const_none, &wlan_cb_methods, false);
11111113
}
11121114
return _callback;
11131115
}

cc3200/mods/pybpin.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ STATIC mp_obj_t pin_callback (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map
599599
pin_obj_t *self = pos_args[0];
600600
// check if any parameters were passed
601601
mp_obj_t _callback = mpcallback_find(self);
602-
if (kw_args->used > 0 || !_callback) {
602+
if (kw_args->used > 0) {
603603
// convert the priority to the correct value
604604
uint priority = mpcallback_translate_priority (args[2].u_int);
605605
// verify the interrupt mode
@@ -703,13 +703,15 @@ STATIC mp_obj_t pin_callback (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map
703703
}
704704

705705
// all checks have passed, now we can create the callback
706-
_callback = mpcallback_new (self, args[1].u_obj, &pin_cb_methods);
706+
_callback = mpcallback_new (self, args[1].u_obj, &pin_cb_methods, true);
707707
if (pwrmode & PYB_PWR_MODE_LPDS) {
708708
pybsleep_set_gpio_lpds_callback (_callback);
709709
}
710710

711711
// enable the interrupt just before leaving
712712
pin_extint_enable(self);
713+
} else if (!_callback) {
714+
_callback = mpcallback_new (self, mp_const_none, &pin_cb_methods, false);
713715
}
714716
return _callback;
715717

cc3200/mods/pybrtc.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ STATIC mp_obj_t pyb_rtc_callback (mp_uint_t n_args, const mp_obj_t *pos_args, mp
215215

216216
// check if any parameters were passed
217217
mp_obj_t _callback = mpcallback_find((mp_obj_t)&pyb_rtc_obj);
218-
if (kw_args->used > 0 || !_callback) {
219-
uint32_t f_mseconds = MAX(1, args[3].u_int);
218+
if (kw_args->used > 0) {
219+
uint32_t f_mseconds = MAX(1, mp_obj_get_int(args[3].u_obj));
220220
uint32_t seconds;
221221
uint16_t mseconds;
222222
// get the seconds and the milliseconds from the RTC
@@ -238,7 +238,7 @@ STATIC mp_obj_t pyb_rtc_callback (mp_uint_t n_args, const mp_obj_t *pos_args, mp
238238
pybrtc_data.prwmode = args[4].u_int;
239239

240240
// create the callback
241-
_callback = mpcallback_new ((mp_obj_t)&pyb_rtc_obj, args[1].u_obj, &pybrtc_cb_methods);
241+
_callback = mpcallback_new ((mp_obj_t)&pyb_rtc_obj, args[1].u_obj, &pybrtc_cb_methods, true);
242242

243243
// set the lpds callback
244244
pybsleep_set_timer_lpds_callback(_callback);
@@ -248,6 +248,8 @@ STATIC mp_obj_t pyb_rtc_callback (mp_uint_t n_args, const mp_obj_t *pos_args, mp
248248

249249
// enable the interrupt (the object is not relevant here, the function already knows it)
250250
pyb_rtc_callback_enable(NULL);
251+
} else if (!_callback) {
252+
_callback = mpcallback_new ((mp_obj_t)&pyb_rtc_obj, mp_const_none, &pybrtc_cb_methods, false);
251253
}
252254
return _callback;
253255
}

cc3200/mods/pybtimer.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ STATIC mp_obj_t pyb_timer_channel_callback (mp_uint_t n_args, const mp_obj_t *po
744744

745745
pyb_timer_channel_obj_t *ch = pos_args[0];
746746
mp_obj_t _callback = mpcallback_find(ch);
747-
if (kw_args->used > 0 || !_callback) {
747+
if (kw_args->used > 0) {
748748
// convert the priority to the correct value
749749
uint priority = mpcallback_translate_priority (args[2].u_int);
750750

@@ -755,10 +755,10 @@ STATIC mp_obj_t pyb_timer_channel_callback (mp_uint_t n_args, const mp_obj_t *po
755755
}
756756

757757
uint32_t _config = (ch->channel == TIMER_B) ? ((ch->timer->config & TIMER_B) >> 8) : (ch->timer->config & TIMER_A);
758+
uint32_t c_value = mp_obj_get_int(args[3].u_obj);
758759

759760
// validate and set the value if we are in edge count mode
760761
if (_config == TIMER_CFG_A_CAP_COUNT) {
761-
uint32_t c_value = args[3].u_int;
762762
if (!c_value || c_value > 0xFFFF) {
763763
// zero or exceeds the maximum value of a 16-bit timer
764764
goto invalid_args;
@@ -778,7 +778,7 @@ STATIC mp_obj_t pyb_timer_channel_callback (mp_uint_t n_args, const mp_obj_t *po
778778
case TIMER_CFG_A_CAP_COUNT:
779779
ch->timer->intflags |= TIMER_CAPA_MATCH << shift;
780780
// set the match value and make 1 the minimum
781-
MAP_TimerMatchSet(ch->timer->timer, ch->channel, MAX(1, args[3].u_int));
781+
MAP_TimerMatchSet(ch->timer->timer, ch->channel, MAX(1, c_value));
782782
break;
783783
case TIMER_CFG_A_CAP_TIME:
784784
ch->timer->intflags |= TIMER_CAPA_EVENT << shift;
@@ -841,7 +841,7 @@ STATIC mp_obj_t pyb_timer_channel_callback (mp_uint_t n_args, const mp_obj_t *po
841841
MAP_TimerIntRegister(ch->timer->timer, ch->channel, pfnHandler);
842842

843843
// create the callback
844-
_callback = mpcallback_new (ch, args[1].u_obj, &pyb_timer_channel_cb_methods);
844+
_callback = mpcallback_new (ch, args[1].u_obj, &pyb_timer_channel_cb_methods, true);
845845

846846
// reload the timer
847847
uint32_t period_c;
@@ -851,6 +851,8 @@ STATIC mp_obj_t pyb_timer_channel_callback (mp_uint_t n_args, const mp_obj_t *po
851851

852852
// enable the callback before returning
853853
pyb_timer_channel_callback_enable(ch);
854+
} else if (!_callback) {
855+
_callback = mpcallback_new (ch, mp_const_none, &pyb_timer_channel_cb_methods, false);
854856
}
855857
return _callback;
856858

cc3200/mods/pybuart.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ mp_obj_t uart_callback_new (pyb_uart_obj_t *self, mp_obj_t handler, uint rxbuffe
203203
}
204204

205205
// create the callback
206-
mp_obj_t _callback = mpcallback_new ((mp_obj_t)self, handler, &uart_cb_methods);
206+
mp_obj_t _callback = mpcallback_new ((mp_obj_t)self, handler, &uart_cb_methods, true);
207207

208208
// enable the interrupts now
209209
uart_callback_enable (self);
@@ -520,7 +520,7 @@ STATIC mp_obj_t pyb_uart_callback (mp_uint_t n_args, const mp_obj_t *pos_args, m
520520
// check if any parameters were passed
521521
pyb_uart_obj_t *self = pos_args[0];
522522
mp_obj_t _callback = mpcallback_find((mp_obj_t)self);
523-
if (kw_args->used > 0 || !_callback) {
523+
if (kw_args->used > 0) {
524524

525525
// convert the priority to the correct value
526526
uint priority = mpcallback_translate_priority (args[2].u_int);
@@ -531,7 +531,9 @@ STATIC mp_obj_t pyb_uart_callback (mp_uint_t n_args, const mp_obj_t *pos_args, m
531531
}
532532

533533
// register a new callback
534-
return uart_callback_new (self, args[1].u_obj, args[3].u_int, priority);
534+
return uart_callback_new (self, args[1].u_obj, mp_obj_get_int(args[3].u_obj), priority);
535+
} else if (!_callback) {
536+
_callback = mpcallback_new (self, mp_const_none, &uart_cb_methods, false);
535537
}
536538
return _callback;
537539
}

cc3200/qstrdefsport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ Q(handler)
317317
Q(mode)
318318
Q(value)
319319
Q(priority)
320-
Q(wakes)
320+
Q(wake_from)
321321

322322
// for Sleep class
323323
Q(Sleep)

docs/library/network.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,12 @@ class WLAN
336336
Returns a list of the devices currently connected. Each item in the list is a
337337
tuple of ``(ssid, mac)``.
338338

339-
.. method:: wlan.callback(wakes)
339+
.. method:: wlan.callback(wake_from)
340340

341341
Create a callback to be triggered when a WLAN event occurs during ``pyb.Sleep.SUSPENDED``
342342
mode. Events are triggered by socket activity or by WLAN connection/disconnection.
343343

344-
- ``wakes`` can only be ``pyb.Sleep.SUSPENDED``.
344+
- ``wake_from`` must be ``pyb.Sleep.SUSPENDED``.
345345

346346
Returns a callback object.
347347

0 commit comments

Comments
 (0)