summaryrefslogtreecommitdiffstatshomepage
path: root/cc3200/misc
diff options
context:
space:
mode:
Diffstat (limited to 'cc3200/misc')
-rw-r--r--cc3200/misc/mpcallback.c205
-rw-r--r--cc3200/misc/mpcallback.h70
-rw-r--r--cc3200/misc/mperror.c6
-rw-r--r--cc3200/misc/pin_defs_cc3200.c25
-rw-r--r--cc3200/misc/pin_named_pins.c6
5 files changed, 288 insertions, 24 deletions
diff --git a/cc3200/misc/mpcallback.c b/cc3200/misc/mpcallback.c
new file mode 100644
index 0000000000..3a5611f5df
--- /dev/null
+++ b/cc3200/misc/mpcallback.c
@@ -0,0 +1,205 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2015 Daniel Campora
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "py/mpconfig.h"
+#include MICROPY_HAL_H
+#include "py/obj.h"
+#include "py/runtime.h"
+#include "py/gc.h"
+#include "inc/hw_types.h"
+#include "interrupt.h"
+#include "pybsleep.h"
+#include "mpcallback.h"
+#include "mpexception.h"
+#include "mperror.h"
+
+
+/******************************************************************************
+ DECLARE PRIVATE FUNCTIONS
+ ******************************************************************************/
+STATIC mpcallback_obj_t *mpcallback_find (mp_obj_t parent);
+
+/******************************************************************************
+ DEFINE PUBLIC DATA
+ ******************************************************************************/
+const mp_arg_t mpcallback_init_args[] = {
+ { MP_QSTR_intmode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
+ { MP_QSTR_handler, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
+ { MP_QSTR_priority, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
+ { MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
+ { MP_QSTR_wake, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = PYB_PWR_MODE_ACTIVE_IDLE } },
+};
+
+/******************************************************************************
+ DEFINE PUBLIC FUNCTIONS
+ ******************************************************************************/
+void mpcallback_init0 (void) {
+ // initialize the callback objects list
+ mp_obj_list_init(&MP_STATE_PORT(mpcallback_obj_list), 0);
+}
+
+mp_obj_t mpcallback_new (mp_obj_t parent, mp_obj_t handler, const mp_cb_methods_t *methods) {
+ mpcallback_obj_t *self = m_new_obj(mpcallback_obj_t);
+ self->base.type = &pyb_callback_type;
+ self->handler = handler;
+ self->parent = parent;
+ self->methods = (mp_cb_methods_t *)methods;
+ // remove any old callback if present
+ mpcallback_remove(self->parent);
+ mp_obj_list_append(&MP_STATE_PORT(mpcallback_obj_list), self);
+ return self;
+}
+
+void mpcallback_remove (const mp_obj_t parent) {
+ mpcallback_obj_t *callback_obj;
+ if ((callback_obj = mpcallback_find(parent))) {
+ mp_obj_list_remove(&MP_STATE_PORT(mpcallback_obj_list), callback_obj);
+ }
+}
+
+uint mpcallback_translate_priority (uint priority) {
+ if (priority < 1 || priority > 7) {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
+ }
+
+ switch (priority) {
+ case 1:
+ return INT_PRIORITY_LVL_7;
+ case 2:
+ return INT_PRIORITY_LVL_6;
+ case 3:
+ return INT_PRIORITY_LVL_5;
+ case 4:
+ return INT_PRIORITY_LVL_4;
+ case 5:
+ return INT_PRIORITY_LVL_3;
+ case 6:
+ return INT_PRIORITY_LVL_2;
+ case 7:
+ return INT_PRIORITY_LVL_1;
+ default:
+ return INT_PRIORITY_LVL_7;
+ }
+}
+
+void mpcallback_handler (mp_obj_t self_in) {
+ mpcallback_obj_t *self = self_in;
+ if (self->handler != mp_const_none) {
+ // disable interrupts to avoid nesting
+ uint primsk = disable_irq();
+ // when executing code within a handler we must lock the GC to prevent
+ // any memory allocations. We must also catch any exceptions.
+ gc_lock();
+ nlr_buf_t nlr;
+ if (nlr_push(&nlr) == 0) {
+ mp_call_function_1(self->handler, self->parent);
+ nlr_pop();
+ }
+ else {
+ // uncaught exception; disable the callback so that it doesn't run again
+ self->methods->disable (self->parent);
+ self->handler = mp_const_none;
+ // printing an exception here will cause a stack overflow that will end up in
+ // a hard fault, so is better to signal the uncaught (probably non-recoverable)
+ // exception by blinking the system led instead.
+ mperror_signal_error();
+ }
+ gc_unlock();
+ enable_irq(primsk);
+ }
+}
+
+/******************************************************************************
+ DEFINE PRIVATE FUNCTIONS
+ ******************************************************************************/
+STATIC mpcallback_obj_t *mpcallback_find (mp_obj_t parent) {
+ for (mp_uint_t i = 0; i < MP_STATE_PORT(mpcallback_obj_list).len; i++) {
+ // search for the object and then remove it
+ mpcallback_obj_t *callback_obj = ((mpcallback_obj_t *)(MP_STATE_PORT(mpcallback_obj_list).items[i]));
+ if (callback_obj->parent == parent) {
+ return callback_obj;
+ }
+ }
+ return NULL;
+}
+
+/******************************************************************************/
+// Micro Python bindings
+
+/// \method init()
+/// Initializes the interrupt callback. With no parameters passed, everything will default
+/// to the values assigned to mpcallback_init_args[].
+STATIC mp_obj_t callback_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+ mpcallback_obj_t *self = pos_args[0];
+ // this is a bit of a hack, but it let us reuse the callback_create method from our parent
+ ((mp_obj_t *)pos_args)[0] = self->parent;
+ self->methods->init (n_args, pos_args, kw_args);
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_KW(callback_init_obj, 1, callback_init);
+
+/// \method enable()
+/// Enables the interrupt callback
+STATIC mp_obj_t callback_enable (mp_obj_t self_in) {
+ mpcallback_obj_t *self = self_in;
+ self->methods->enable(self->parent);
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(callback_enable_obj, callback_enable);
+
+/// \method disable()
+/// Disables the interrupt callback
+STATIC mp_obj_t callback_disable (mp_obj_t self_in) {
+ mpcallback_obj_t *self = self_in;
+ self->methods->disable(self->parent);
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(callback_disable_obj, callback_disable);
+
+/// \method \call()
+/// Triggers the interrupt callback
+STATIC mp_obj_t callback_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
+ mp_arg_check_num(n_args, n_kw, 0, 0, false);
+ mpcallback_handler (self_in);
+ return mp_const_none;
+}
+
+STATIC const mp_map_elem_t callback_locals_dict_table[] = {
+ // instance methods
+ { MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&callback_init_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_enable), (mp_obj_t)&callback_enable_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_disable), (mp_obj_t)&callback_disable_obj },
+};
+
+STATIC MP_DEFINE_CONST_DICT(callback_locals_dict, callback_locals_dict_table);
+
+const mp_obj_type_t pyb_callback_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_callback,
+ .call = callback_call,
+ .locals_dict = (mp_obj_t)&callback_locals_dict,
+};
+
diff --git a/cc3200/misc/mpcallback.h b/cc3200/misc/mpcallback.h
new file mode 100644
index 0000000000..e0a9fc6a92
--- /dev/null
+++ b/cc3200/misc/mpcallback.h
@@ -0,0 +1,70 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2015 Daniel Campora
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MPCALLBACK_H_
+#define MPCALLBACK_H_
+
+/******************************************************************************
+ DEFINE CONSTANTS
+ ******************************************************************************/
+#define mpcallback_INIT_NUM_ARGS 5
+
+/******************************************************************************
+ DEFINE TYPES
+ ******************************************************************************/
+typedef void (*mp_cb_method_t) (mp_obj_t self);
+typedef mp_obj_t (*mp_cb_init_t) (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
+
+typedef struct {
+ mp_cb_init_t init;
+ mp_cb_method_t enable;
+ mp_cb_method_t disable;
+} mp_cb_methods_t;
+
+typedef struct {
+ mp_obj_base_t base;
+ mp_obj_t parent;
+ mp_obj_t handler;
+ mp_cb_methods_t *methods;
+} mpcallback_obj_t;
+
+/******************************************************************************
+ DECLARE EXPORTED DATA
+ ******************************************************************************/
+extern const mp_arg_t mpcallback_init_args[];
+extern const mp_obj_type_t pyb_callback_type;
+
+/******************************************************************************
+ DECLARE PUBLIC FUNCTIONS
+ ******************************************************************************/
+void mpcallback_init0 (void);
+mp_obj_t mpcallback_new (mp_obj_t parent, mp_obj_t handler, const mp_cb_methods_t *methods);
+void mpcallback_remove (const mp_obj_t parent);
+void mpcallback_handler (mp_obj_t self_in);
+uint mpcallback_translate_priority (uint priority);
+mp_obj_t mpcallback_new (mp_obj_t parent, mp_obj_t handler, const mp_cb_methods_t *methods);
+
+#endif /* MPCALLBACK_H_ */
diff --git a/cc3200/misc/mperror.c b/cc3200/misc/mperror.c
index 522e76396f..32e04ba00c 100644
--- a/cc3200/misc/mperror.c
+++ b/cc3200/misc/mperror.c
@@ -51,8 +51,8 @@
/******************************************************************************
DEFINE CONSTANTS
******************************************************************************/
-#define MPERROR_TOOGLE_MS (200)
-#define MPERROR_SIGNAL_ERROR_MS (2000)
+#define MPERROR_TOOGLE_MS (40)
+#define MPERROR_SIGNAL_ERROR_MS (1000)
#define MPERROR_HEARTBEAT_ON_MS (80)
#define MPERROR_HEARTBEAT_OFF_MS (2920)
@@ -113,7 +113,7 @@ void mperror_deinit_sfe_pin (void) {
void mperror_signal_error (void) {
uint32_t count = 0;
- while ((MPERROR_TOOGLE_MS * count++) > MPERROR_SIGNAL_ERROR_MS) {
+ while ((MPERROR_TOOGLE_MS * count++) < MPERROR_SIGNAL_ERROR_MS) {
// toogle the led
MAP_GPIOPinWrite(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN, ~MAP_GPIOPinRead(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN));
UtilsDelay(UTILS_DELAY_US_TO_COUNT(MPERROR_TOOGLE_MS * 1000));
diff --git a/cc3200/misc/pin_defs_cc3200.c b/cc3200/misc/pin_defs_cc3200.c
index e585161bc0..a9b9413a12 100644
--- a/cc3200/misc/pin_defs_cc3200.c
+++ b/cc3200/misc/pin_defs_cc3200.c
@@ -26,6 +26,7 @@
*/
#include "py/mpconfig.h"
+#include MICROPY_HAL_H
#include "py/obj.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
@@ -35,32 +36,20 @@
#include "gpio.h"
#include "pin.h"
#include "pybpin.h"
-#include MICROPY_HAL_H
+
// Returns the pin mode. This value returned by this macro should be one of:
// GPIO_DIR_MODE_IN or GPIO_DIR_MODE_OUT
-uint32_t pin_get_mode(const pin_obj_t *self) {
- return MAP_GPIODirModeGet(self->port, self->bit);
+uint32_t pin_get_mode (const pin_obj_t *self) {
+ return self->mode;
}
-uint32_t pin_get_type(const pin_obj_t *self) {
-
- uint32_t strenght;
- uint32_t type;
-
- MAP_PinConfigGet(self->pin_num, &strenght, &type);
-
- return type;
+uint32_t pin_get_type (const pin_obj_t *self) {
+ return self->type;
}
uint32_t pin_get_strenght (const pin_obj_t *self) {
-
- uint32_t strenght;
- uint32_t type;
-
- MAP_PinConfigGet(self->pin_num, &strenght, &type);
-
- return strenght;
+ return self->strength;
}
diff --git a/cc3200/misc/pin_named_pins.c b/cc3200/misc/pin_named_pins.c
index 67f75c3302..590e013d44 100644
--- a/cc3200/misc/pin_named_pins.c
+++ b/cc3200/misc/pin_named_pins.c
@@ -49,7 +49,7 @@ const mp_obj_type_t pin_cpu_pins_obj_type = {
.locals_dict = (mp_obj_t)&pin_cpu_pins_locals_dict,
};
-const pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t name) {
+pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t name) {
mp_map_t *named_map = mp_obj_dict_get_map((mp_obj_t)named_pins);
mp_map_elem_t *named_elem = mp_map_lookup(named_map, name, MP_MAP_LOOKUP);
if (named_elem != NULL && named_elem->value != NULL) {
@@ -58,7 +58,7 @@ const pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t na
return NULL;
}
-const pin_obj_t *pin_find_pin(const mp_obj_dict_t *named_pins, uint pin_num) {
+pin_obj_t *pin_find_pin(const mp_obj_dict_t *named_pins, uint pin_num) {
mp_map_t *named_map = mp_obj_dict_get_map((mp_obj_t)named_pins);
for (uint i = 0; i < named_map->used; i++) {
if (((pin_obj_t *)named_map->table[i].value)->pin_num == pin_num) {
@@ -68,7 +68,7 @@ const pin_obj_t *pin_find_pin(const mp_obj_dict_t *named_pins, uint pin_num) {
return NULL;
}
-const pin_obj_t *pin_find_pin_by_port_bit (const mp_obj_dict_t *named_pins, uint port, uint bit) {
+pin_obj_t *pin_find_pin_by_port_bit (const mp_obj_dict_t *named_pins, uint port, uint bit) {
mp_map_t *named_map = mp_obj_dict_get_map((mp_obj_t)named_pins);
for (uint i = 0; i < named_map->used; i++) {
if ((((pin_obj_t *)named_map->table[i].value)->port == port) &&