Skip to content

Commit 359b4e9

Browse files
author
Daniel Campora
committed
cc3200: Refactor pin af assigment functions.
1 parent 1d399c3 commit 359b4e9

3 files changed

Lines changed: 44 additions & 49 deletions

File tree

cc3200/mods/pybpin.c

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ DECLARE PRIVATE FUNCTIONS
6161
STATIC pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t name);
6262
STATIC pin_obj_t *pin_find_pin_by_port_bit (const mp_obj_dict_t *named_pins, uint port, uint bit);
6363
STATIC int8_t pin_obj_find_af (const pin_obj_t* pin, uint8_t fn, uint8_t unit, uint8_t type);
64+
STATIC int8_t pin_find_af_index (const pin_obj_t* pin, uint8_t fn, uint8_t unit, uint8_t type);
65+
STATIC void pin_free_af_from_pins (uint8_t fn, uint8_t unit, uint8_t type);
6466
STATIC void pin_deassign (pin_obj_t* pin);
6567
STATIC void pin_obj_configure (const pin_obj_t *self);
6668
STATIC void pin_get_hibernate_pin_and_idx (const pin_obj_t *self, uint *wake_pin, uint *idx);
@@ -162,26 +164,12 @@ void pin_config (pin_obj_t *self, int af, uint mode, uint pull, int value, uint
162164
pybsleep_add ((const mp_obj_t)self, (WakeUpCB_t)pin_obj_configure);
163165
}
164166

165-
int8_t pin_find_af_index (const pin_obj_t* pin, uint8_t fn, uint8_t unit, uint8_t type) {
166-
int8_t af = pin_obj_find_af(pin, fn, unit, type);
167-
if (af < 0) {
168-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
169-
}
170-
return af;
171-
}
172-
173-
void pin_free_af_from_pins (uint8_t fn, uint8_t unit, uint8_t type) {
174-
mp_map_t *named_map = mp_obj_dict_get_map((mp_obj_t)&pin_board_pins_locals_dict);
175-
for (uint i = 0; i < named_map->used - 1; i++) {
176-
pin_obj_t * pin = (pin_obj_t *)named_map->table[i].value;
177-
// af is different than GPIO
178-
if (pin->af > PIN_MODE_0) {
179-
// check if the pin supports the target af
180-
int af = pin_obj_find_af(pin, fn, unit, type);
181-
if (af > 0 && af == pin->af) {
182-
// the pin is assigned to the target af, de-assign it
183-
pin_deassign (pin);
184-
}
167+
void pin_assign_pins_af (mp_obj_t *pins, uint32_t n_pins, uint32_t pull, uint32_t fn, uint32_t unit) {
168+
for (int i = 0; i < n_pins; i++) {
169+
pin_free_af_from_pins(fn, unit, i);
170+
if (pins[i] != mp_const_none) {
171+
pin_obj_t *pin = pin_find(pins[i]);
172+
pin_config (pin, pin_find_af_index(pin, fn, unit, i), 0, pull, -1, PIN_STRENGTH_2MA);
185173
}
186174
}
187175
}
@@ -218,6 +206,30 @@ STATIC int8_t pin_obj_find_af (const pin_obj_t* pin, uint8_t fn, uint8_t unit, u
218206
return -1;
219207
}
220208

209+
STATIC int8_t pin_find_af_index (const pin_obj_t* pin, uint8_t fn, uint8_t unit, uint8_t type) {
210+
int8_t af = pin_obj_find_af(pin, fn, unit, type);
211+
if (af < 0) {
212+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
213+
}
214+
return af;
215+
}
216+
217+
STATIC void pin_free_af_from_pins (uint8_t fn, uint8_t unit, uint8_t type) {
218+
mp_map_t *named_map = mp_obj_dict_get_map((mp_obj_t)&pin_board_pins_locals_dict);
219+
for (uint i = 0; i < named_map->used - 1; i++) {
220+
pin_obj_t * pin = (pin_obj_t *)named_map->table[i].value;
221+
// af is different than GPIO
222+
if (pin->af > PIN_MODE_0) {
223+
// check if the pin supports the target af
224+
int af = pin_obj_find_af(pin, fn, unit, type);
225+
if (af > 0 && af == pin->af) {
226+
// the pin is assigned to the target af, de-assign it
227+
pin_deassign (pin);
228+
}
229+
}
230+
}
231+
}
232+
221233
STATIC void pin_deassign (pin_obj_t* pin) {
222234
pin_config (pin, PIN_MODE_0, GPIO_DIR_MODE_IN, PIN_TYPE_STD, -1, PIN_STRENGTH_4MA);
223235
pin->used = false;

cc3200/mods/pybpin.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,6 @@ typedef struct {
115115
uint8_t used : 1;
116116
} pin_obj_t;
117117

118-
typedef struct {
119-
pin_obj_t *pin;
120-
uint8_t af_idx;
121-
} pin_fn_t;
122-
123118
extern const mp_obj_type_t pin_type;
124119

125120
typedef struct {
@@ -139,7 +134,6 @@ extern const mp_obj_dict_t pin_board_pins_locals_dict;
139134
void pin_init0(void);
140135
void pin_config(pin_obj_t *self, int af, uint mode, uint type, int value, uint strength);
141136
pin_obj_t *pin_find(mp_obj_t user_obj);
142-
int8_t pin_find_af_index(const pin_obj_t* pin, uint8_t fn, uint8_t unit, uint8_t type);
143-
void pin_free_af_from_pins (uint8_t fn, uint8_t unit, uint8_t type);
137+
void pin_assign_pins_af (mp_obj_t *pins, uint32_t n_pins, uint32_t pull, uint32_t fn, uint32_t unit);
144138

145139
#endif // PYBPIN_H_

cc3200/mods/pybuart.c

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ STATIC pyb_uart_obj_t pyb_uart_obj[PYB_NUM_UARTS] = { {.reg = UARTA0_BASE, .baud
115115
{.reg = UARTA1_BASE, .baudrate = 0, .read_buf = NULL, .peripheral = PRCM_UARTA1} };
116116
STATIC const mp_cb_methods_t uart_cb_methods;
117117

118-
STATIC const pin_fn_t pyb_uart_def_pin[PYB_NUM_UARTS][2] = { {{.pin = &pin_GP1, .af_idx = 3}, {.pin = &pin_GP2, .af_idx = 3}},
119-
{{.pin = &pin_GP3, .af_idx = 6}, {.pin = &pin_GP4, .af_idx = 6}} };
118+
STATIC const mp_obj_t pyb_uart_def_pin[PYB_NUM_UARTS][2] = { {&pin_GP1, &pin_GP2}, {&pin_GP3, &pin_GP4} };
120119

121120
/******************************************************************************
122121
DEFINE PUBLIC FUNCTIONS
@@ -397,46 +396,36 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, con
397396
// stop bits
398397
config |= (args[3].u_int == 1 ? UART_CONFIG_STOP_ONE : UART_CONFIG_STOP_TWO);
399398

399+
// assign the pins
400400
mp_obj_t pins_o = args[4].u_obj;
401401
uint flowcontrol = UART_FLOWCONTROL_NONE;
402402
if (pins_o != mp_const_none) {
403+
mp_obj_t *pins;
404+
mp_uint_t n_pins;
403405
if (pins_o == MP_OBJ_NULL) {
404406
// use the default pins
405-
pin_config (pyb_uart_def_pin[self->uart_id][PIN_TYPE_UART_TX].pin, pyb_uart_def_pin[self->uart_id][PIN_TYPE_UART_TX].af_idx,
406-
0, PIN_TYPE_STD_PU, -1, PIN_STRENGTH_2MA);
407-
pin_config (pyb_uart_def_pin[self->uart_id][PIN_TYPE_UART_RX].pin, pyb_uart_def_pin[self->uart_id][PIN_TYPE_UART_RX].af_idx,
408-
0, PIN_TYPE_STD_PU, -1, PIN_STRENGTH_2MA);
407+
pins = (mp_obj_t *)pyb_uart_def_pin[self->uart_id];
409408
} else {
410-
mp_obj_t *pins_t;
411-
mp_uint_t n_pins;
412-
mp_obj_get_array(pins_o, &n_pins, &pins_t);
409+
mp_obj_get_array(pins_o, &n_pins, &pins);
413410
if (n_pins != 2 && n_pins != 4) {
414411
goto error;
415412
}
416413
if (n_pins == 4) {
417-
if (pins_t[PIN_TYPE_UART_RTS] != mp_const_none && pins_t[PIN_TYPE_UART_RX] == mp_const_none) {
414+
if (pins[PIN_TYPE_UART_RTS] != mp_const_none && pins[PIN_TYPE_UART_RX] == mp_const_none) {
418415
goto error; // RTS pin given in TX only mode
419-
} else if (pins_t[PIN_TYPE_UART_CTS] != mp_const_none && pins_t[PIN_TYPE_UART_TX] == mp_const_none) {
416+
} else if (pins[PIN_TYPE_UART_CTS] != mp_const_none && pins[PIN_TYPE_UART_TX] == mp_const_none) {
420417
goto error; // CTS pin given in RX only mode
421418
} else {
422-
if (pins_t[PIN_TYPE_UART_RTS] != mp_const_none) {
419+
if (pins[PIN_TYPE_UART_RTS] != mp_const_none) {
423420
flowcontrol |= UART_FLOWCONTROL_RX;
424421
}
425-
if (pins_t[PIN_TYPE_UART_CTS] != mp_const_none) {
422+
if (pins[PIN_TYPE_UART_CTS] != mp_const_none) {
426423
flowcontrol |= UART_FLOWCONTROL_TX;
427424
}
428425
}
429426
}
430-
// the pins tuple passed looks good so far
431-
for (int i = 0; i < n_pins; i++) {
432-
pin_free_af_from_pins(PIN_FN_UART, self->uart_id, i);
433-
if (pins_t[i] != mp_const_none) {
434-
pin_obj_t *pin = pin_find(pins_t[i]);
435-
pin_config (pin, pin_find_af_index(pin, PIN_FN_UART, self->uart_id, i),
436-
0, PIN_TYPE_STD_PU, -1, PIN_STRENGTH_2MA);
437-
}
438-
}
439427
}
428+
pin_assign_pins_af (pins, n_pins, PIN_TYPE_STD_PU, PIN_FN_UART, self->uart_id);
440429
}
441430

442431
self->baudrate = baudrate;

0 commit comments

Comments
 (0)