Commit 6de1b393 authored by danicampora's avatar danicampora
Browse files

cc3200: Make peripheral objects static.

This prevents duplication of objects in the sleep list. Also helps
with reducing the code size by ~100 bytes.
parent fcf6db06
...@@ -159,7 +159,7 @@ unsigned char ulRstReg; ...@@ -159,7 +159,7 @@ unsigned char ulRstReg;
#define PRCM_HIB_WAKEUP_CAUSE_GPIO 0x00000004 #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 #define PRCM_INT_SLOW_CLK_CTR 0x00004000
......
...@@ -64,14 +64,25 @@ ...@@ -64,14 +64,25 @@
/// ///
/// The sample rate is fixed to 62.5KHz and the resolution to 12 bits. /// 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; mp_obj_base_t base;
byte channel; byte channel;
byte num; 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 // enable the ADC channel
MAP_ADCChannelEnable(ADC_BASE, self->channel); MAP_ADCChannelEnable(ADC_BASE, self->channel);
// enable and configure the timer // enable and configure the timer
...@@ -81,11 +92,16 @@ STATIC void pybadc_init (pyb_obj_adc_t *self) { ...@@ -81,11 +92,16 @@ STATIC void pybadc_init (pyb_obj_adc_t *self) {
MAP_ADCEnable(ADC_BASE); MAP_ADCEnable(ADC_BASE);
} }
/******************************************************************************
DECLARE PRIVATE DATA
******************************************************************************/
STATIC pyb_adc_obj_t pyb_adc_obj[PYB_ADC_NUM_CHANNELS];
/******************************************************************************/ /******************************************************************************/
/* Micro Python bindings : adc object */ /* 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) { 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); 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, ...@@ -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 // 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->base.type = &pyb_adc_type;
self->channel = channel; self->channel = channel;
self->num = num; 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, ...@@ -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 /// Read the value on the analog pin and return it. The returned value
/// will be between 0 and 4095. /// will be between 0 and 4095.
STATIC mp_obj_t adc_read(mp_obj_t self_in) { 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; uint32_t sample;
// wait until a new value is available // wait until a new value is available
...@@ -159,7 +175,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_read_obj, adc_read); ...@@ -159,7 +175,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_read_obj, adc_read);
/// \method enable() /// \method enable()
/// Enable the adc channel /// Enable the adc channel
STATIC mp_obj_t adc_enable(mp_obj_t self_in) { 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); pybadc_init(self);
return mp_const_none; return mp_const_none;
...@@ -169,7 +185,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_enable_obj, adc_enable); ...@@ -169,7 +185,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_enable_obj, adc_enable);
/// \method disable() /// \method disable()
/// Disable the adc channel /// Disable the adc channel
STATIC mp_obj_t adc_disable(mp_obj_t self_in) { 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); MAP_ADCChannelDisable(ADC_BASE, self->channel);
// unregister it with the sleep module // unregister it with the sleep module
......
...@@ -119,6 +119,11 @@ typedef struct _pyb_i2c_obj_t { ...@@ -119,6 +119,11 @@ typedef struct _pyb_i2c_obj_t {
} \ } \
} }
/******************************************************************************
DECLARE PRIVATE DATA
******************************************************************************/
STATIC pyb_i2c_obj_t pyb_i2c_obj;
/****************************************************************************** /******************************************************************************
DEFINE PRIVATE FUNCTIONS DEFINE PRIVATE FUNCTIONS
******************************************************************************/ ******************************************************************************/
...@@ -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 ...@@ -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)); nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
} }
// create and setup the object // setup the object
pyb_i2c_obj_t *self = m_new_obj_with_finaliser(pyb_i2c_obj_t); pyb_i2c_obj_t *self = &pyb_i2c_obj;
self->base.type = &pyb_i2c_type; self->base.type = &pyb_i2c_type;
self->mode = PYBI2C_MODE_DISABLED; self->mode = PYBI2C_MODE_DISABLED;
......
...@@ -87,25 +87,21 @@ ...@@ -87,25 +87,21 @@
/****************************************************************************** /******************************************************************************
DEFINE CONSTANTS DEFINE CONSTANTS
******************************************************************************/ ******************************************************************************/
#define PYBUART_TX_WAIT_MS 1 #define PYBUART_TX_WAIT_MS 1
#define PYBUART_TX_MAX_TIMEOUT_MS 5 #define PYBUART_TX_MAX_TIMEOUT_MS 5
/****************************************************************************** /******************************************************************************
DECLARE PRIVATE FUNCTIONS DECLARE PRIVATE FUNCTIONS
******************************************************************************/ ******************************************************************************/
STATIC void uart_init (pyb_uart_obj_t *self); STATIC void uart_init (pyb_uart_obj_t *self);
STATIC bool uart_rx_wait (pyb_uart_obj_t *self, uint32_t timeout); 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 UARTGenericIntHandler(uint32_t uart_id);
STATIC void UART0IntHandler(void); STATIC void UART0IntHandler(void);
STATIC void UART1IntHandler(void); STATIC void UART1IntHandler(void);
STATIC mp_obj_t pyb_uart_deinit(mp_obj_t self_in);
/****************************************************************************** /******************************************************************************
DEFINE PRIVATE TYPES DEFINE PRIVATE TYPES
******************************************************************************/ ******************************************************************************/
struct _pyb_uart_obj_t { struct _pyb_uart_obj_t {
mp_obj_base_t base; mp_obj_base_t base;
pyb_uart_id_t uart_id; pyb_uart_id_t uart_id;
...@@ -122,21 +118,15 @@ struct _pyb_uart_obj_t { ...@@ -122,21 +118,15 @@ struct _pyb_uart_obj_t {
bool enabled; bool enabled;
}; };
/******************************************************************************
DECLARE PRIVATE DATA
******************************************************************************/
STATIC pyb_uart_obj_t pyb_uart_obj[PYB_NUM_UARTS];
/****************************************************************************** /******************************************************************************
DEFINE PUBLIC FUNCTIONS DEFINE PUBLIC FUNCTIONS
******************************************************************************/ ******************************************************************************/
void uart_init0 (void) { 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) { 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) { ...@@ -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) { STATIC void UARTGenericIntHandler(uint32_t uart_id) {
pyb_uart_obj_t *self; pyb_uart_obj_t *self;
uint32_t status; uint32_t status;
if ((self = pyb_uart_find(uart_id))) { self = &pyb_uart_obj[uart_id];
status = MAP_UARTIntStatus(self->reg, true); status = MAP_UARTIntStatus(self->reg, true);
// receive interrupt // receive interrupt
if (status & (UART_INT_RX | UART_INT_RT)) { if (status & (UART_INT_RX | UART_INT_RT)) {
MAP_UARTIntClear(self->reg, UART_INT_RX | UART_INT_RT); MAP_UARTIntClear(self->reg, UART_INT_RX | UART_INT_RT);
while (UARTCharsAvail(self->reg)) { while (UARTCharsAvail(self->reg)) {
int data = MAP_UARTCharGetNonBlocking(self->reg); int data = MAP_UARTCharGetNonBlocking(self->reg);
if (MICROPY_STDIO_UART == self->uart_id && data == user_interrupt_char) { if (MICROPY_STDIO_UART == self->uart_id && data == user_interrupt_char) {
// raise exception when interrupts are finished // raise exception when interrupts are finished
mpexception_keyboard_nlr_jump(); mpexception_keyboard_nlr_jump();
} }
else if (self->read_buf_len != 0) { else if (self->read_buf_len != 0) {
uint16_t next_head = (self->read_buf_head + 1) % self->read_buf_len; uint16_t next_head = (self->read_buf_head + 1) % self->read_buf_len;
if (next_head != self->read_buf_tail) { if (next_head != self->read_buf_tail) {
// only store data if room in buf // only store data if room in buf
self->read_buf[self->read_buf_head] = data; self->read_buf[self->read_buf_head] = data;
self->read_buf_head = next_head; 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 ...@@ -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)); nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable));
} }
// search for an object in the list // get the correct uart instance
pyb_uart_obj_t *self; pyb_uart_obj_t *self = &pyb_uart_obj[uart_id];
if (!(self = pyb_uart_find(uart_id))) { self->base.type = &pyb_uart_type;
self = pyb_uart_add(uart_id); self->uart_id = uart_id;
}
if (n_args > 1 || n_kw > 0) { 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 // start the peripheral
mp_map_t kw_args; mp_map_t kw_args;
mp_map_init_fixed_table(&kw_args, n_kw, args + n_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); ...@@ -492,7 +460,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_uart_init_obj, 1, pyb_uart_init);
/// \method deinit() /// \method deinit()
/// Turn off the UART bus. /// 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; pyb_uart_obj_t *self = self_in;
uint uartPerh; uint uartPerh;
......
...@@ -29,7 +29,6 @@ ...@@ -29,7 +29,6 @@
#define PYBUART_H_ #define PYBUART_H_
typedef enum { typedef enum {
PYB_UART_NONE = -1,
PYB_UART_0 = 0, PYB_UART_0 = 0,
PYB_UART_1 = 1, PYB_UART_1 = 1,
PYB_NUM_UARTS PYB_NUM_UARTS
...@@ -39,7 +38,7 @@ typedef struct _pyb_uart_obj_t pyb_uart_obj_t; ...@@ -39,7 +38,7 @@ typedef struct _pyb_uart_obj_t pyb_uart_obj_t;
extern const mp_obj_type_t pyb_uart_type; extern const mp_obj_type_t pyb_uart_type;
void uart_init0(void); 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); bool uart_rx_any(pyb_uart_obj_t *uart_obj);
int uart_rx_char(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); bool uart_tx_char(pyb_uart_obj_t *self, int c);
......
...@@ -120,7 +120,6 @@ extern const struct _mp_obj_module_t mp_module_network; ...@@ -120,7 +120,6 @@ extern const struct _mp_obj_module_t mp_module_network;
const char *readline_hist[8]; \ const char *readline_hist[8]; \
mp_obj_t mp_const_user_interrupt; \ mp_obj_t mp_const_user_interrupt; \
mp_obj_t pyb_config_main; \ 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 mod_network_nic_list; \
mp_obj_list_t pybsleep_obj_list; \ mp_obj_list_t pybsleep_obj_list; \
mp_obj_list_t mpcallback_obj_list; \ mp_obj_list_t mpcallback_obj_list; \
......
...@@ -250,7 +250,10 @@ soft_reset_exit: ...@@ -250,7 +250,10 @@ soft_reset_exit:
wlan_stop_servers(); wlan_stop_servers();
wlan_stop(); wlan_stop();
uart_deinit(); // de-initialize the stdio uart
if (pyb_stdio_uart) {
pyb_uart_deinit(pyb_stdio_uart);
}
goto soft_reset; goto soft_reset;
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment