summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authordanicampora <danicampora@gmail.com>2015-03-17 11:05:59 +0100
committerdanicampora <danicampora@gmail.com>2015-03-17 13:26:07 +0100
commit6de1b39368efc27d7a90d64f28f99b9e46422f5b (patch)
treecfb27f5e12cde99857e795ed031aed6db51a6e7c
parentfcf6db06950c00d83d7d02b6ab0a23de52f27b01 (diff)
downloadmicropython-6de1b39368efc27d7a90d64f28f99b9e46422f5b.tar.gz
micropython-6de1b39368efc27d7a90d64f28f99b9e46422f5b.zip
cc3200: Make peripheral objects static.
This prevents duplication of objects in the sleep list. Also helps with reducing the code size by ~100 bytes.
-rw-r--r--cc3200/hal/prcm.h2
-rw-r--r--cc3200/mods/pybadc.c34
-rw-r--r--cc3200/mods/pybi2c.c9
-rw-r--r--cc3200/mods/pybuart.c96
-rw-r--r--cc3200/mods/pybuart.h3
-rw-r--r--cc3200/mpconfigport.h1
-rw-r--r--cc3200/mptask.c5
7 files changed, 70 insertions, 80 deletions
diff --git a/cc3200/hal/prcm.h b/cc3200/hal/prcm.h
index 58163d7cd1..a00bf9d774 100644
--- a/cc3200/hal/prcm.h
+++ b/cc3200/hal/prcm.h
@@ -159,7 +159,7 @@ unsigned char ulRstReg;
#define PRCM_HIB_WAKEUP_CAUSE_GPIO 0x00000004
//*****************************************************************************
-// Values that can be passed to PRCMSEnableInterrupt
+// Values that can be passed to PRCMIntEnable
//*****************************************************************************
#define PRCM_INT_SLOW_CLK_CTR 0x00004000
diff --git a/cc3200/mods/pybadc.c b/cc3200/mods/pybadc.c
index a619362601..446fb40033 100644
--- a/cc3200/mods/pybadc.c
+++ b/cc3200/mods/pybadc.c
@@ -64,14 +64,25 @@
///
/// The sample rate is fixed to 62.5KHz and the resolution to 12 bits.
-typedef struct _pyb_obj_adc_t {
+
+/******************************************************************************
+ DECLARE CONSTANTS
+ ******************************************************************************/
+#define PYB_ADC_NUM_CHANNELS 4
+
+/******************************************************************************
+ DEFINE TYPES
+ ******************************************************************************/
+typedef struct {
mp_obj_base_t base;
byte channel;
byte num;
-} pyb_obj_adc_t;
+} pyb_adc_obj_t;
-
-STATIC void pybadc_init (pyb_obj_adc_t *self) {
+/******************************************************************************
+ DEFINE PUBLIC FUNCTIONS
+ ******************************************************************************/
+STATIC void pybadc_init (pyb_adc_obj_t *self) {
// enable the ADC channel
MAP_ADCChannelEnable(ADC_BASE, self->channel);
// enable and configure the timer
@@ -81,11 +92,16 @@ STATIC void pybadc_init (pyb_obj_adc_t *self) {
MAP_ADCEnable(ADC_BASE);
}
+/******************************************************************************
+ DECLARE PRIVATE DATA
+ ******************************************************************************/
+STATIC pyb_adc_obj_t pyb_adc_obj[PYB_ADC_NUM_CHANNELS];
+
/******************************************************************************/
/* Micro Python bindings : adc object */
STATIC void adc_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
- pyb_obj_adc_t *self = self_in;
+ pyb_adc_obj_t *self = self_in;
print(env, "<ADC, channel=%u>", self->num);
}
@@ -123,7 +139,7 @@ STATIC mp_obj_t adc_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw,
}
// disable the callback before re-configuring
- pyb_obj_adc_t *self = m_new_obj_with_finaliser(pyb_obj_adc_t);
+ pyb_adc_obj_t *self = &pyb_adc_obj[channel];
self->base.type = &pyb_adc_type;
self->channel = channel;
self->num = num;
@@ -144,7 +160,7 @@ STATIC mp_obj_t adc_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw,
/// Read the value on the analog pin and return it. The returned value
/// will be between 0 and 4095.
STATIC mp_obj_t adc_read(mp_obj_t self_in) {
- pyb_obj_adc_t *self = self_in;
+ pyb_adc_obj_t *self = self_in;
uint32_t sample;
// wait until a new value is available
@@ -159,7 +175,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_read_obj, adc_read);
/// \method enable()
/// Enable the adc channel
STATIC mp_obj_t adc_enable(mp_obj_t self_in) {
- pyb_obj_adc_t *self = self_in;
+ pyb_adc_obj_t *self = self_in;
pybadc_init(self);
return mp_const_none;
@@ -169,7 +185,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_enable_obj, adc_enable);
/// \method disable()
/// Disable the adc channel
STATIC mp_obj_t adc_disable(mp_obj_t self_in) {
- pyb_obj_adc_t *self = self_in;
+ pyb_adc_obj_t *self = self_in;
MAP_ADCChannelDisable(ADC_BASE, self->channel);
// unregister it with the sleep module
diff --git a/cc3200/mods/pybi2c.c b/cc3200/mods/pybi2c.c
index aa4401ee18..ce7bc0e74d 100644
--- a/cc3200/mods/pybi2c.c
+++ b/cc3200/mods/pybi2c.c
@@ -120,6 +120,11 @@ typedef struct _pyb_i2c_obj_t {
}
/******************************************************************************
+ DECLARE PRIVATE DATA
+ ******************************************************************************/
+STATIC pyb_i2c_obj_t pyb_i2c_obj;
+
+/******************************************************************************
DEFINE PRIVATE FUNCTIONS
******************************************************************************/
// only master mode is available for the moment
@@ -327,8 +332,8 @@ STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
}
- // create and setup the object
- pyb_i2c_obj_t *self = m_new_obj_with_finaliser(pyb_i2c_obj_t);
+ // setup the object
+ pyb_i2c_obj_t *self = &pyb_i2c_obj;
self->base.type = &pyb_i2c_type;
self->mode = PYBI2C_MODE_DISABLED;
diff --git a/cc3200/mods/pybuart.c b/cc3200/mods/pybuart.c
index 0387f68e42..06854b79b6 100644
--- a/cc3200/mods/pybuart.c
+++ b/cc3200/mods/pybuart.c
@@ -87,25 +87,21 @@
/******************************************************************************
DEFINE CONSTANTS
******************************************************************************/
-#define PYBUART_TX_WAIT_MS 1
-#define PYBUART_TX_MAX_TIMEOUT_MS 5
+#define PYBUART_TX_WAIT_MS 1
+#define PYBUART_TX_MAX_TIMEOUT_MS 5
/******************************************************************************
DECLARE PRIVATE FUNCTIONS
******************************************************************************/
STATIC void uart_init (pyb_uart_obj_t *self);
STATIC bool uart_rx_wait (pyb_uart_obj_t *self, uint32_t timeout);
-STATIC pyb_uart_obj_t* pyb_uart_add (pyb_uart_id_t uart_id);
-STATIC pyb_uart_obj_t* pyb_uart_find (pyb_uart_id_t uart_id);
STATIC void UARTGenericIntHandler(uint32_t uart_id);
STATIC void UART0IntHandler(void);
STATIC void UART1IntHandler(void);
-STATIC mp_obj_t pyb_uart_deinit(mp_obj_t self_in);
/******************************************************************************
DEFINE PRIVATE TYPES
******************************************************************************/
-
struct _pyb_uart_obj_t {
mp_obj_base_t base;
pyb_uart_id_t uart_id;
@@ -123,20 +119,14 @@ struct _pyb_uart_obj_t {
};
/******************************************************************************
+ DECLARE PRIVATE DATA
+ ******************************************************************************/
+STATIC pyb_uart_obj_t pyb_uart_obj[PYB_NUM_UARTS];
+
+/******************************************************************************
DEFINE PUBLIC FUNCTIONS
******************************************************************************/
void uart_init0 (void) {
- mp_obj_list_init(&MP_STATE_PORT(pyb_uart_list), 0);
-}
-
-// unregister all interrupt sources
-void uart_deinit(void) {
- for (int i = PYB_UART_0; i < PYB_NUM_UARTS; i++) {
- pyb_uart_obj_t *self;
- if ((self = pyb_uart_find (i))) {
- pyb_uart_deinit(self);
- }
- }
}
bool uart_rx_any(pyb_uart_obj_t *self) {
@@ -255,50 +245,27 @@ STATIC bool uart_rx_wait (pyb_uart_obj_t *self, uint32_t timeout) {
}
}
-STATIC pyb_uart_obj_t* pyb_uart_add (pyb_uart_id_t uart_id) {
- // create a new uart object
- pyb_uart_obj_t *self = m_new_obj(pyb_uart_obj_t);
- self->base.type = &pyb_uart_type;
- self->uart_id = uart_id;
- self->read_buf = NULL;
- self->enabled = false;
- // add it to the list
- mp_obj_list_append(&MP_STATE_PORT(pyb_uart_list), self);
- return self;
-}
-
-STATIC pyb_uart_obj_t* pyb_uart_find (pyb_uart_id_t uart_id) {
- for (mp_uint_t i = 0; i < MP_STATE_PORT(pyb_uart_list).len; i++) {
- pyb_uart_obj_t *self = (pyb_uart_obj_t *)MP_STATE_PORT(pyb_uart_list).items[i];
- if (self->uart_id == uart_id) {
- return self;
- }
- }
- return NULL;
-}
-
STATIC void UARTGenericIntHandler(uint32_t uart_id) {
pyb_uart_obj_t *self;
uint32_t status;
- if ((self = pyb_uart_find(uart_id))) {
- status = MAP_UARTIntStatus(self->reg, true);
- // receive interrupt
- if (status & (UART_INT_RX | UART_INT_RT)) {
- MAP_UARTIntClear(self->reg, UART_INT_RX | UART_INT_RT);
- while (UARTCharsAvail(self->reg)) {
- int data = MAP_UARTCharGetNonBlocking(self->reg);
- if (MICROPY_STDIO_UART == self->uart_id && data == user_interrupt_char) {
- // raise exception when interrupts are finished
- mpexception_keyboard_nlr_jump();
- }
- else if (self->read_buf_len != 0) {
- uint16_t next_head = (self->read_buf_head + 1) % self->read_buf_len;
- if (next_head != self->read_buf_tail) {
- // only store data if room in buf
- self->read_buf[self->read_buf_head] = data;
- self->read_buf_head = next_head;
- }
+ self = &pyb_uart_obj[uart_id];
+ status = MAP_UARTIntStatus(self->reg, true);
+ // receive interrupt
+ if (status & (UART_INT_RX | UART_INT_RT)) {
+ MAP_UARTIntClear(self->reg, UART_INT_RX | UART_INT_RT);
+ while (UARTCharsAvail(self->reg)) {
+ int data = MAP_UARTCharGetNonBlocking(self->reg);
+ if (MICROPY_STDIO_UART == self->uart_id && data == user_interrupt_char) {
+ // raise exception when interrupts are finished
+ mpexception_keyboard_nlr_jump();
+ }
+ else if (self->read_buf_len != 0) {
+ uint16_t next_head = (self->read_buf_head + 1) % self->read_buf_len;
+ if (next_head != self->read_buf_tail) {
+ // only store data if room in buf
+ self->read_buf[self->read_buf_head] = data;
+ self->read_buf_head = next_head;
}
}
}
@@ -469,13 +436,14 @@ STATIC mp_obj_t pyb_uart_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable));
}
- // search for an object in the list
- pyb_uart_obj_t *self;
- if (!(self = pyb_uart_find(uart_id))) {
- self = pyb_uart_add(uart_id);
- }
-
+ // get the correct uart instance
+ pyb_uart_obj_t *self = &pyb_uart_obj[uart_id];
+ self->base.type = &pyb_uart_type;
+ self->uart_id = uart_id;
if (n_args > 1 || n_kw > 0) {
+ // invalidate the buffer and clear the enabled flag
+ self->read_buf = NULL;
+ self->enabled = false;
// start the peripheral
mp_map_t kw_args;
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
@@ -492,7 +460,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_uart_init_obj, 1, pyb_uart_init);
/// \method deinit()
/// Turn off the UART bus.
-STATIC mp_obj_t pyb_uart_deinit(mp_obj_t self_in) {
+mp_obj_t pyb_uart_deinit(mp_obj_t self_in) {
pyb_uart_obj_t *self = self_in;
uint uartPerh;
diff --git a/cc3200/mods/pybuart.h b/cc3200/mods/pybuart.h
index 44660aabe4..73eab476e2 100644
--- a/cc3200/mods/pybuart.h
+++ b/cc3200/mods/pybuart.h
@@ -29,7 +29,6 @@
#define PYBUART_H_
typedef enum {
- PYB_UART_NONE = -1,
PYB_UART_0 = 0,
PYB_UART_1 = 1,
PYB_NUM_UARTS
@@ -39,7 +38,7 @@ typedef struct _pyb_uart_obj_t pyb_uart_obj_t;
extern const mp_obj_type_t pyb_uart_type;
void uart_init0(void);
-void uart_deinit (void);
+mp_obj_t pyb_uart_deinit(mp_obj_t self_in);
bool uart_rx_any(pyb_uart_obj_t *uart_obj);
int uart_rx_char(pyb_uart_obj_t *uart_obj);
bool uart_tx_char(pyb_uart_obj_t *self, int c);
diff --git a/cc3200/mpconfigport.h b/cc3200/mpconfigport.h
index 52bbf530dd..4da01fa5b2 100644
--- a/cc3200/mpconfigport.h
+++ b/cc3200/mpconfigport.h
@@ -120,7 +120,6 @@ extern const struct _mp_obj_module_t mp_module_network;
const char *readline_hist[8]; \
mp_obj_t mp_const_user_interrupt; \
mp_obj_t pyb_config_main; \
- mp_obj_list_t pyb_uart_list; \
mp_obj_list_t mod_network_nic_list; \
mp_obj_list_t pybsleep_obj_list; \
mp_obj_list_t mpcallback_obj_list; \
diff --git a/cc3200/mptask.c b/cc3200/mptask.c
index bd86da2aa4..8697301e18 100644
--- a/cc3200/mptask.c
+++ b/cc3200/mptask.c
@@ -250,7 +250,10 @@ soft_reset_exit:
wlan_stop_servers();
wlan_stop();
- uart_deinit();
+ // de-initialize the stdio uart
+ if (pyb_stdio_uart) {
+ pyb_uart_deinit(pyb_stdio_uart);
+ }
goto soft_reset;
}