samd: move mcu_find_pin_function to header

.. it'll be used in can as well as sdio
This commit is contained in:
Jeff Epler 2020-08-24 09:00:08 -05:00
parent df067b498d
commit 183649aad8
4 changed files with 37 additions and 33 deletions

View File

@ -24,6 +24,8 @@
* THE SOFTWARE.
*/
#include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "atmel_start_pins.h"
@ -256,3 +258,19 @@ void common_hal_mcu_pin_claim(const mcu_pin_obj_t* pin) {
void common_hal_mcu_pin_reset_number(uint8_t pin_no) {
reset_pin_number(pin_no);
}
mcu_pin_function_t *mcu_find_pin_function(mcu_pin_function_t *table, const mcu_pin_obj_t *pin, int instance, uint16_t name) {
if (!pin) {
return NULL;
}
for(; table->obj; table++) {
if (instance != -1 && instance != table->instance) {
continue;
}
if (pin == table->obj) {
return table;
}
}
mp_raise_ValueError_varg(translate("%q pin invalid"), name);
}

View File

@ -47,4 +47,13 @@ void never_reset_pin_number(uint8_t pin_number);
void claim_pin(const mcu_pin_obj_t* pin);
bool pin_number_is_free(uint8_t pin_number);
typedef struct {
const mcu_pin_obj_t *obj;
uint8_t instance;
uint8_t pin;
uint16_t function;
} mcu_pin_function_t;
mcu_pin_function_t *mcu_find_pin_function(mcu_pin_function_t *table, const mcu_pin_obj_t *pin, int instance, uint16_t name);
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_MICROCONTROLLER_PIN_H

View File

@ -58,24 +58,6 @@
static Sdhc *sdhc_insts[] = SDHC_INSTS;
STATIC pin_function_t *find_pin_function(pin_function_t *table, const mcu_pin_obj_t *pin, int instance, uint16_t name) {
DEBUG_PRINT("\n\n[inst=% 2d] %q: ", instance, name);
DEBUG_PRINT_OBJ_NL(pin);
for(; table->obj; table++) {
DEBUG_PRINT("[inst=% 2d] considering table @%p: ");
DEBUG_PRINT_OBJ(table->obj);
DEBUG_PRINT(" %d %d\n", table->instance, table->pin);
if (instance != -1 && instance != table->instance) {
continue;
}
if (pin == table->obj) {
return table;
}
}
mp_raise_ValueError_varg(translate("%q pin invalid"), name);
}
void common_hal_sdioio_sdcard_construct(sdioio_sdcard_obj_t *self,
const mcu_pin_obj_t * clock, const mcu_pin_obj_t * command,
uint8_t num_data, mcu_pin_obj_t ** data, uint32_t frequency) {
@ -98,15 +80,15 @@ CLK PA21 PCC_D? (D32) BROWN
*/
pin_function_t *functions[6] = {};
functions[0] = find_pin_function(sdio_cmd, command, -1, MP_QSTR_command);
mcu_pin_function_t *functions[6] = {};
functions[0] = mcu_find_pin_function(sdio_cmd, command, -1, MP_QSTR_command);
int instance = functions[0]->instance;
functions[1] = find_pin_function(sdio_ck, clock, instance, MP_QSTR_clock);
functions[2] = find_pin_function(sdio_dat0, data[0], instance, MP_QSTR_data0);
functions[1] = mcu_find_pin_function(sdio_ck, clock, instance, MP_QSTR_clock);
functions[2] = mcu_find_pin_function(sdio_dat0, data[0], instance, MP_QSTR_data0);
if(num_data == 4) {
functions[3] = find_pin_function(sdio_dat1, data[1], instance, MP_QSTR_data1);
functions[4] = find_pin_function(sdio_dat2, data[2], instance, MP_QSTR_data2);
functions[5] = find_pin_function(sdio_dat3, data[3], instance, MP_QSTR_data3);
functions[3] = mcu_find_pin_function(sdio_dat1, data[1], instance, MP_QSTR_data1);
functions[4] = mcu_find_pin_function(sdio_dat2, data[2], instance, MP_QSTR_data2);
functions[5] = mcu_find_pin_function(sdio_dat3, data[3], instance, MP_QSTR_data3);
}
// We've verified all pins, now set their special functions
@ -114,7 +96,7 @@ CLK PA21 PCC_D? (D32) BROWN
self->clock_pin = common_hal_mcu_pin_number(functions[1]->obj);
for(int i=0; i<num_data; i++) {
pin_function_t *function = functions[2+i];
mcu_pin_function_t *function = functions[2+i];
if (function) {
self->data_pins[i] = common_hal_mcu_pin_number(function->obj);
} else {

View File

@ -1,7 +1,7 @@
#!/usr/bin/python3
def defines(name, function):
print(f'pin_function_t {name} [] = {{')
print(f'mcu_pin_function_t {name} [] = {{')
for instance in (0, 1):
for port in 'ABCD':
for idx in range(32):
@ -23,13 +23,8 @@ print('''\
#include "mpconfigport.h"
#include "atmel_start_pins.h"
#include "hal/include/hal_gpio.h"
#include "common-hal/microcontroller/Pin.h"
typedef struct {
const mcu_pin_obj_t *obj;
uint8_t instance;
uint8_t pin;
uint16_t function;
} pin_function_t;
''')
defines('sdio_ck', 'SDCK')