Skip to content

Commit 6b21c3f

Browse files
committed
cc3200: Refactor UART and I2C object creation.
I2C objects can be freed by the GC and a __del__ method is provided in order to de-init the peripheral prior to being garbage collected. UART objects are now added to a local list and this list is now part of the VM_STATE.
1 parent 7807da2 commit 6b21c3f

12 files changed

Lines changed: 132 additions & 124 deletions

File tree

cc3200/boards/LAUNCHXL/mpconfigboard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@
3434
#define MICROPY_HW_ENABLE_RNG (1)
3535
#define MICROPY_HW_ENABLE_RTC (1)
3636

37-
#define MICROPY_STDIO_UART PYB_UART_1
37+
#define MICROPY_STDIO_UART PYB_UART_0
3838
#define MICROPY_STDIO_UART_BAUD 115200

cc3200/hal/cc3200_hal.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,15 @@ static void hal_EnableSdCard (void);
7070
#endif
7171

7272
/******************************************************************************
73-
DECLARE LOCAL VARIABLES
73+
DECLARE LOCAL DATA
7474
******************************************************************************/
7575
static volatile uint32_t HAL_tickCount;
7676

77+
/******************************************************************************
78+
DECLARE PUBLIC DATA
79+
******************************************************************************/
80+
struct _pyb_uart_obj_t *pyb_stdio_uart;
81+
7782
/******************************************************************************
7883
DECLARE IMPORTED DATA
7984
******************************************************************************/
@@ -134,8 +139,9 @@ void mp_hal_stdout_tx_str(const char *str) {
134139
}
135140

136141
void mp_hal_stdout_tx_strn(const char *str, uint32_t len) {
137-
if (MP_STATE_PORT(pyb_stdio_uart) != NULL) {
138-
uart_tx_strn(MP_STATE_PORT(pyb_stdio_uart), str, len);
142+
// send stdout to UART
143+
if (pyb_stdio_uart != NULL) {
144+
uart_tx_strn(pyb_stdio_uart, str, len);
139145
}
140146
// and also to telnet
141147
if (telnet_is_active()) {
@@ -145,8 +151,8 @@ void mp_hal_stdout_tx_strn(const char *str, uint32_t len) {
145151

146152
void mp_hal_stdout_tx_strn_cooked(const char *str, uint32_t len) {
147153
// send stdout to UART
148-
if (MP_STATE_PORT(pyb_stdio_uart) != NULL) {
149-
uart_tx_strn_cooked(MP_STATE_PORT(pyb_stdio_uart), str, len);
154+
if (pyb_stdio_uart != NULL) {
155+
uart_tx_strn_cooked(pyb_stdio_uart, str, len);
150156
}
151157
// and also to telnet
152158
if (telnet_is_active()) {
@@ -159,8 +165,8 @@ int mp_hal_stdin_rx_chr(void) {
159165
if (telnet_rx_any()) {
160166
return telnet_rx_char();
161167
}
162-
else if (MP_STATE_PORT(pyb_stdio_uart) != NULL && uart_rx_any(MP_STATE_PORT(pyb_stdio_uart))) {
163-
return uart_rx_char(MP_STATE_PORT(pyb_stdio_uart));
168+
else if (pyb_stdio_uart != NULL && uart_rx_any(pyb_stdio_uart)) {
169+
return uart_rx_char(pyb_stdio_uart);
164170
}
165171
HAL_Delay(1);
166172
}

cc3200/hal/cc3200_hal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@
5252
" isb \n"); \
5353
}
5454

55+
/******************************************************************************
56+
DECLARE PUBLIC DATA
57+
******************************************************************************/
58+
extern struct _pyb_uart_obj_t *pyb_stdio_uart;
59+
5560
/******************************************************************************
5661
DECLARE PUBLIC FUNCTIONS
5762
******************************************************************************/

cc3200/mods/modpyb.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,16 +243,16 @@ MP_DEFINE_CONST_FUN_OBJ_0(pyb_standby_obj, pyb_standby);
243243
/// Get or set the UART object that the REPL is repeated on.
244244
STATIC mp_obj_t pyb_repl_uart(uint n_args, const mp_obj_t *args) {
245245
if (n_args == 0) {
246-
if (MP_STATE_PORT(pyb_stdio_uart) == NULL) {
246+
if (pyb_stdio_uart == NULL) {
247247
return mp_const_none;
248248
} else {
249-
return MP_STATE_PORT(pyb_stdio_uart);
249+
return pyb_stdio_uart;
250250
}
251251
} else {
252252
if (args[0] == mp_const_none) {
253-
MP_STATE_PORT(pyb_stdio_uart) = NULL;
253+
pyb_stdio_uart = NULL;
254254
} else if (mp_obj_get_type(args[0]) == &pyb_uart_type) {
255-
MP_STATE_PORT(pyb_stdio_uart) = args[0];
255+
pyb_stdio_uart = args[0];
256256
} else {
257257
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_num_type_invalid_arguments));
258258
}

cc3200/mods/pybadc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
/// Usage:
5757
///
5858
/// adc = pyb.ADC(channel) # create an adc object on the given channel (0 to 3)
59+
/// this automatically configures the pin associated to
60+
/// that analog channel.
5961
/// adc.read() # read channel value
6062
///
6163
/// The sample rate is fixed to 62.5KHz and the resolution to 12 bits.

cc3200/mods/pybi2c.c

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@
9797
/// i2c.mem_write('abc', 0x42, 2, timeout=10)
9898

9999

100+
typedef struct _pyb_i2c_obj_t {
101+
mp_obj_base_t base;
102+
int mode;
103+
union {
104+
uint baudrate;
105+
byte slvaddr;
106+
};
107+
} pyb_i2c_obj_t;
108+
100109
/******************************************************************************
101110
DEFINE CONSTANTS
102111
******************************************************************************/
@@ -117,10 +126,6 @@
117126
/******************************************************************************
118127
DEFINE PUBLIC FUNCTIONS
119128
******************************************************************************/
120-
void i2c_init0(void) {
121-
MP_STATE_PORT(pyb_i2c_obj) = NULL;
122-
}
123-
124129
// only master mode is available for the moment
125130
void i2c_init (uint mode, uint slvaddr, uint baudrate) {
126131
// Enable the I2C Peripheral
@@ -139,14 +144,6 @@ void i2c_deinit(void) {
139144
/******************************************************************************/
140145
/* Micro Python bindings */
141146
/******************************************************************************/
142-
typedef struct _pyb_i2c_obj_t {
143-
mp_obj_base_t base;
144-
int mode;
145-
union {
146-
uint baudrate;
147-
byte slvaddr;
148-
};
149-
} pyb_i2c_obj_t;
150147

151148
STATIC bool pybI2C_transaction(uint cmd, uint timeout) {
152149
// Clear all interrupts
@@ -260,11 +257,15 @@ STATIC bool pybI2C_ScanDevice(byte devAddr, uint timeout) {
260257
STATIC void pyb_i2c_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
261258
pyb_i2c_obj_t *self = self_in;
262259

263-
print(env, "I2C0");
260+
print(env, "<I2C0");
264261
if (self->mode == PYBI2C_MODE_MASTER) {
265-
print(env, ", I2C.MASTER, baudrate=%u)", self->baudrate);
266-
} else if (self->mode == PYBI2C_MODE_SLAVE) {
267-
print(env, ", I2C.SLAVE, addr=0x%02x)", self->slvaddr);
262+
print(env, ", I2C.MASTER, baudrate=%u>)", self->baudrate);
263+
}
264+
else if (self->mode == PYBI2C_MODE_SLAVE) {
265+
print(env, ", I2C.SLAVE, addr=0x%02x>)", self->slvaddr);
266+
}
267+
else {
268+
print(env, ">");
268269
}
269270
}
270271

@@ -310,7 +311,7 @@ STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self_in, mp_uint_t n_args, co
310311

311312
/// \classmethod \constructor(bus, ...)
312313
///
313-
/// Construct an I2C object on the given bus. `bus` can be 1.
314+
/// Construct an I2C object on the given bus. `bus` can only be 0.
314315
/// With no additional parameters, the I2C object is created but not
315316
/// initialised (it has the settings from the last initialisation of
316317
/// the bus, if any). If extra arguments are given, the bus is initialised.
@@ -320,25 +321,17 @@ STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n
320321
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
321322

322323
// get i2c number
323-
mp_int_t i2c_id = mp_obj_get_int(args[0]) - 1;
324+
mp_int_t i2c_id = mp_obj_get_int(args[0]);
324325

325-
// check i2c number
326+
// check the i2c number
326327
if (i2c_id != 0) {
327328
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
328329
}
329330

330-
// setup the object
331-
pyb_i2c_obj_t *self;
332-
if (MP_STATE_PORT(pyb_i2c_obj) == NULL) {
333-
// create a new I2C object
334-
self = m_new_obj(pyb_i2c_obj_t);
335-
self->base.type = &pyb_i2c_type;
336-
self->mode = PYBI2C_MODE_DISABLED;
337-
MP_STATE_PORT(pyb_i2c_obj) = self;
338-
} else {
339-
// reference the existing I2C object
340-
self = MP_STATE_PORT(pyb_i2c_obj);
341-
}
331+
// create and setup the object
332+
pyb_i2c_obj_t *self = m_new_obj(pyb_i2c_obj_t);
333+
self->base.type = &pyb_i2c_type;
334+
self->mode = PYBI2C_MODE_DISABLED;
342335

343336
if (n_args > 1 || n_kw > 0) {
344337
// start the peripheral
@@ -610,6 +603,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_mem_write_obj, 1, pyb_i2c_mem_write);
610603

611604
STATIC const mp_map_elem_t pyb_i2c_locals_dict_table[] = {
612605
// instance methods
606+
{ MP_OBJ_NEW_QSTR(MP_QSTR___del__), (mp_obj_t)&pyb_i2c_deinit_obj },
613607
{ MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&pyb_i2c_init_obj },
614608
{ MP_OBJ_NEW_QSTR(MP_QSTR_deinit), (mp_obj_t)&pyb_i2c_deinit_obj },
615609
{ MP_OBJ_NEW_QSTR(MP_QSTR_is_ready), (mp_obj_t)&pyb_i2c_is_ready_obj },

cc3200/mods/pybi2c.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,5 @@
2727

2828
extern const mp_obj_type_t pyb_i2c_type;
2929

30-
void i2c_init0(void);
3130
void i2c_init (uint mode, uint slvaddr, uint baudrate);
3231
void i2c_deinit(void);

0 commit comments

Comments
 (0)