Skip to content

Commit 6de1b39

Browse files
committed
cc3200: Make peripheral objects static.
This prevents duplication of objects in the sleep list. Also helps with reducing the code size by ~100 bytes.
1 parent fcf6db0 commit 6de1b39

File tree

7 files changed

+70
-80
lines changed

7 files changed

+70
-80
lines changed

cc3200/hal/prcm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ unsigned char ulRstReg;
159159
#define PRCM_HIB_WAKEUP_CAUSE_GPIO 0x00000004
160160

161161
//*****************************************************************************
162-
// Values that can be passed to PRCMSEnableInterrupt
162+
// Values that can be passed to PRCMIntEnable
163163
//*****************************************************************************
164164
#define PRCM_INT_SLOW_CLK_CTR 0x00004000
165165

cc3200/mods/pybadc.c

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,25 @@
6464
///
6565
/// The sample rate is fixed to 62.5KHz and the resolution to 12 bits.
6666

67-
typedef struct _pyb_obj_adc_t {
67+
68+
/******************************************************************************
69+
DECLARE CONSTANTS
70+
******************************************************************************/
71+
#define PYB_ADC_NUM_CHANNELS 4
72+
73+
/******************************************************************************
74+
DEFINE TYPES
75+
******************************************************************************/
76+
typedef struct {
6877
mp_obj_base_t base;
6978
byte channel;
7079
byte num;
71-
} pyb_obj_adc_t;
80+
} pyb_adc_obj_t;
7281

73-
74-
STATIC void pybadc_init (pyb_obj_adc_t *self) {
82+
/******************************************************************************
83+
DEFINE PUBLIC FUNCTIONS
84+
******************************************************************************/
85+
STATIC void pybadc_init (pyb_adc_obj_t *self) {
7586
// enable the ADC channel
7687
MAP_ADCChannelEnable(ADC_BASE, self->channel);
7788
// enable and configure the timer
@@ -81,11 +92,16 @@ STATIC void pybadc_init (pyb_obj_adc_t *self) {
8192
MAP_ADCEnable(ADC_BASE);
8293
}
8394

95+
/******************************************************************************
96+
DECLARE PRIVATE DATA
97+
******************************************************************************/
98+
STATIC pyb_adc_obj_t pyb_adc_obj[PYB_ADC_NUM_CHANNELS];
99+
84100
/******************************************************************************/
85101
/* Micro Python bindings : adc object */
86102

87103
STATIC void adc_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
88-
pyb_obj_adc_t *self = self_in;
104+
pyb_adc_obj_t *self = self_in;
89105
print(env, "<ADC, channel=%u>", self->num);
90106
}
91107

@@ -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,
123139
}
124140

125141
// disable the callback before re-configuring
126-
pyb_obj_adc_t *self = m_new_obj_with_finaliser(pyb_obj_adc_t);
142+
pyb_adc_obj_t *self = &pyb_adc_obj[channel];
127143
self->base.type = &pyb_adc_type;
128144
self->channel = channel;
129145
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,
144160
/// Read the value on the analog pin and return it. The returned value
145161
/// will be between 0 and 4095.
146162
STATIC mp_obj_t adc_read(mp_obj_t self_in) {
147-
pyb_obj_adc_t *self = self_in;
163+
pyb_adc_obj_t *self = self_in;
148164
uint32_t sample;
149165

150166
// wait until a new value is available
@@ -159,7 +175,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_read_obj, adc_read);
159175
/// \method enable()
160176
/// Enable the adc channel
161177
STATIC mp_obj_t adc_enable(mp_obj_t self_in) {
162-
pyb_obj_adc_t *self = self_in;
178+
pyb_adc_obj_t *self = self_in;
163179

164180
pybadc_init(self);
165181
return mp_const_none;
@@ -169,7 +185,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_enable_obj, adc_enable);
169185
/// \method disable()
170186
/// Disable the adc channel
171187
STATIC mp_obj_t adc_disable(mp_obj_t self_in) {
172-
pyb_obj_adc_t *self = self_in;
188+
pyb_adc_obj_t *self = self_in;
173189

174190
MAP_ADCChannelDisable(ADC_BASE, self->channel);
175191
// unregister it with the sleep module

cc3200/mods/pybi2c.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ typedef struct _pyb_i2c_obj_t {
119119
} \
120120
}
121121

122+
/******************************************************************************
123+
DECLARE PRIVATE DATA
124+
******************************************************************************/
125+
STATIC pyb_i2c_obj_t pyb_i2c_obj;
126+
122127
/******************************************************************************
123128
DEFINE PRIVATE FUNCTIONS
124129
******************************************************************************/
@@ -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
327332
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
328333
}
329334

330-
// create and setup the object
331-
pyb_i2c_obj_t *self = m_new_obj_with_finaliser(pyb_i2c_obj_t);
335+
// setup the object
336+
pyb_i2c_obj_t *self = &pyb_i2c_obj;
332337
self->base.type = &pyb_i2c_type;
333338
self->mode = PYBI2C_MODE_DISABLED;
334339

cc3200/mods/pybuart.c

Lines changed: 32 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -87,25 +87,21 @@
8787
/******************************************************************************
8888
DEFINE CONSTANTS
8989
******************************************************************************/
90-
#define PYBUART_TX_WAIT_MS 1
91-
#define PYBUART_TX_MAX_TIMEOUT_MS 5
90+
#define PYBUART_TX_WAIT_MS 1
91+
#define PYBUART_TX_MAX_TIMEOUT_MS 5
9292

9393
/******************************************************************************
9494
DECLARE PRIVATE FUNCTIONS
9595
******************************************************************************/
9696
STATIC void uart_init (pyb_uart_obj_t *self);
9797
STATIC bool uart_rx_wait (pyb_uart_obj_t *self, uint32_t timeout);
98-
STATIC pyb_uart_obj_t* pyb_uart_add (pyb_uart_id_t uart_id);
99-
STATIC pyb_uart_obj_t* pyb_uart_find (pyb_uart_id_t uart_id);
10098
STATIC void UARTGenericIntHandler(uint32_t uart_id);
10199
STATIC void UART0IntHandler(void);
102100
STATIC void UART1IntHandler(void);
103-
STATIC mp_obj_t pyb_uart_deinit(mp_obj_t self_in);
104101

105102
/******************************************************************************
106103
DEFINE PRIVATE TYPES
107104
******************************************************************************/
108-
109105
struct _pyb_uart_obj_t {
110106
mp_obj_base_t base;
111107
pyb_uart_id_t uart_id;
@@ -122,21 +118,15 @@ struct _pyb_uart_obj_t {
122118
bool enabled;
123119
};
124120

121+
/******************************************************************************
122+
DECLARE PRIVATE DATA
123+
******************************************************************************/
124+
STATIC pyb_uart_obj_t pyb_uart_obj[PYB_NUM_UARTS];
125+
125126
/******************************************************************************
126127
DEFINE PUBLIC FUNCTIONS
127128
******************************************************************************/
128129
void uart_init0 (void) {
129-
mp_obj_list_init(&MP_STATE_PORT(pyb_uart_list), 0);
130-
}
131-
132-
// unregister all interrupt sources
133-
void uart_deinit(void) {
134-
for (int i = PYB_UART_0; i < PYB_NUM_UARTS; i++) {
135-
pyb_uart_obj_t *self;
136-
if ((self = pyb_uart_find (i))) {
137-
pyb_uart_deinit(self);
138-
}
139-
}
140130
}
141131

142132
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) {
255245
}
256246
}
257247

258-
STATIC pyb_uart_obj_t* pyb_uart_add (pyb_uart_id_t uart_id) {
259-
// create a new uart object
260-
pyb_uart_obj_t *self = m_new_obj(pyb_uart_obj_t);
261-
self->base.type = &pyb_uart_type;
262-
self->uart_id = uart_id;
263-
self->read_buf = NULL;
264-
self->enabled = false;
265-
// add it to the list
266-
mp_obj_list_append(&MP_STATE_PORT(pyb_uart_list), self);
267-
return self;
268-
}
269-
270-
STATIC pyb_uart_obj_t* pyb_uart_find (pyb_uart_id_t uart_id) {
271-
for (mp_uint_t i = 0; i < MP_STATE_PORT(pyb_uart_list).len; i++) {
272-
pyb_uart_obj_t *self = (pyb_uart_obj_t *)MP_STATE_PORT(pyb_uart_list).items[i];
273-
if (self->uart_id == uart_id) {
274-
return self;
275-
}
276-
}
277-
return NULL;
278-
}
279-
280248
STATIC void UARTGenericIntHandler(uint32_t uart_id) {
281249
pyb_uart_obj_t *self;
282250
uint32_t status;
283251

284-
if ((self = pyb_uart_find(uart_id))) {
285-
status = MAP_UARTIntStatus(self->reg, true);
286-
// receive interrupt
287-
if (status & (UART_INT_RX | UART_INT_RT)) {
288-
MAP_UARTIntClear(self->reg, UART_INT_RX | UART_INT_RT);
289-
while (UARTCharsAvail(self->reg)) {
290-
int data = MAP_UARTCharGetNonBlocking(self->reg);
291-
if (MICROPY_STDIO_UART == self->uart_id && data == user_interrupt_char) {
292-
// raise exception when interrupts are finished
293-
mpexception_keyboard_nlr_jump();
294-
}
295-
else if (self->read_buf_len != 0) {
296-
uint16_t next_head = (self->read_buf_head + 1) % self->read_buf_len;
297-
if (next_head != self->read_buf_tail) {
298-
// only store data if room in buf
299-
self->read_buf[self->read_buf_head] = data;
300-
self->read_buf_head = next_head;
301-
}
252+
self = &pyb_uart_obj[uart_id];
253+
status = MAP_UARTIntStatus(self->reg, true);
254+
// receive interrupt
255+
if (status & (UART_INT_RX | UART_INT_RT)) {
256+
MAP_UARTIntClear(self->reg, UART_INT_RX | UART_INT_RT);
257+
while (UARTCharsAvail(self->reg)) {
258+
int data = MAP_UARTCharGetNonBlocking(self->reg);
259+
if (MICROPY_STDIO_UART == self->uart_id && data == user_interrupt_char) {
260+
// raise exception when interrupts are finished
261+
mpexception_keyboard_nlr_jump();
262+
}
263+
else if (self->read_buf_len != 0) {
264+
uint16_t next_head = (self->read_buf_head + 1) % self->read_buf_len;
265+
if (next_head != self->read_buf_tail) {
266+
// only store data if room in buf
267+
self->read_buf[self->read_buf_head] = data;
268+
self->read_buf_head = next_head;
302269
}
303270
}
304271
}
@@ -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
469436
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable));
470437
}
471438

472-
// search for an object in the list
473-
pyb_uart_obj_t *self;
474-
if (!(self = pyb_uart_find(uart_id))) {
475-
self = pyb_uart_add(uart_id);
476-
}
477-
439+
// get the correct uart instance
440+
pyb_uart_obj_t *self = &pyb_uart_obj[uart_id];
441+
self->base.type = &pyb_uart_type;
442+
self->uart_id = uart_id;
478443
if (n_args > 1 || n_kw > 0) {
444+
// invalidate the buffer and clear the enabled flag
445+
self->read_buf = NULL;
446+
self->enabled = false;
479447
// start the peripheral
480448
mp_map_t kw_args;
481449
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);
492460

493461
/// \method deinit()
494462
/// Turn off the UART bus.
495-
STATIC mp_obj_t pyb_uart_deinit(mp_obj_t self_in) {
463+
mp_obj_t pyb_uart_deinit(mp_obj_t self_in) {
496464
pyb_uart_obj_t *self = self_in;
497465
uint uartPerh;
498466

cc3200/mods/pybuart.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#define PYBUART_H_
3030

3131
typedef enum {
32-
PYB_UART_NONE = -1,
3332
PYB_UART_0 = 0,
3433
PYB_UART_1 = 1,
3534
PYB_NUM_UARTS
@@ -39,7 +38,7 @@ typedef struct _pyb_uart_obj_t pyb_uart_obj_t;
3938
extern const mp_obj_type_t pyb_uart_type;
4039

4140
void uart_init0(void);
42-
void uart_deinit (void);
41+
mp_obj_t pyb_uart_deinit(mp_obj_t self_in);
4342
bool uart_rx_any(pyb_uart_obj_t *uart_obj);
4443
int uart_rx_char(pyb_uart_obj_t *uart_obj);
4544
bool uart_tx_char(pyb_uart_obj_t *self, int c);

cc3200/mpconfigport.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ extern const struct _mp_obj_module_t mp_module_network;
120120
const char *readline_hist[8]; \
121121
mp_obj_t mp_const_user_interrupt; \
122122
mp_obj_t pyb_config_main; \
123-
mp_obj_list_t pyb_uart_list; \
124123
mp_obj_list_t mod_network_nic_list; \
125124
mp_obj_list_t pybsleep_obj_list; \
126125
mp_obj_list_t mpcallback_obj_list; \

cc3200/mptask.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,10 @@ void TASK_Micropython (void *pvParameters) {
250250
wlan_stop_servers();
251251
wlan_stop();
252252

253-
uart_deinit();
253+
// de-initialize the stdio uart
254+
if (pyb_stdio_uart) {
255+
pyb_uart_deinit(pyb_stdio_uart);
256+
}
254257

255258
goto soft_reset;
256259
}

0 commit comments

Comments
 (0)