|
| 1 | +#include "py/runtime.h" |
| 2 | +#include "extmod/network_cyw43.h" |
| 3 | +#include "extmod/modnetwork.h" |
| 4 | +#include "lib/cyw43-driver/src/cyw43.h" |
| 5 | +#include "pico/unique_id.h" |
| 6 | + |
| 7 | +void cyw43_irq_deinit(void); |
| 8 | +void cyw43_irq_init(void); |
| 9 | + |
| 10 | +#if CYW43_PIN_WL_DYNAMIC |
| 11 | +// Defined in cyw43_bus_pio_spi.c |
| 12 | +int cyw43_set_pins_wl(uint pins[CYW43_PIN_INDEX_WL_COUNT]); |
| 13 | +#endif |
| 14 | + |
| 15 | +#if CYW43_PIO_CLOCK_DIV_DYNAMIC |
| 16 | +// Defined in cyw43_bus_pio_spi.c |
| 17 | +void cyw43_set_pio_clkdiv_int_frac8(uint32_t clock_div_int, uint8_t clock_div_frac8); |
| 18 | +#endif |
| 19 | + |
| 20 | +// enums for the arguments needed to initialise cyw43 |
| 21 | +#define CYW43_INIT_ARG_ENUM \ |
| 22 | + ARG_pin_on, ARG_pin_out, ARG_pin_in, ARG_pin_wake, ARG_pin_clock, ARG_pin_cs, ARG_pin_dat, ARG_div_int, ARG_div_frac |
| 23 | + |
| 24 | +// Builds an array of pins needed by cyw43_set_pins_wl from function arguments |
| 25 | +// order defined by cyw43_pin_index_t |
| 26 | +#if CYW43_PIN_WL_DYNAMIC |
| 27 | +#define SET_CYW43_PIN_ARG(ARG_ENUM, DEFAULT) args[ARG_ENUM].u_obj != MP_OBJ_NULL ? mp_hal_get_pin_obj(args[ARG_ENUM].u_obj) : (DEFAULT) |
| 28 | +#define CYW43_PIN_ARRAY \ |
| 29 | + SET_CYW43_PIN_ARG(ARG_pin_on, NUM_BANK0_GPIOS), \ |
| 30 | + SET_CYW43_PIN_ARG(ARG_pin_out, SET_CYW43_PIN_ARG(ARG_pin_dat, NUM_BANK0_GPIOS)), \ |
| 31 | + SET_CYW43_PIN_ARG(ARG_pin_in, SET_CYW43_PIN_ARG(ARG_pin_dat, NUM_BANK0_GPIOS)), \ |
| 32 | + SET_CYW43_PIN_ARG(ARG_pin_wake, SET_CYW43_PIN_ARG(ARG_pin_dat, NUM_BANK0_GPIOS)), \ |
| 33 | + SET_CYW43_PIN_ARG(ARG_pin_clock, NUM_BANK0_GPIOS), \ |
| 34 | + SET_CYW43_PIN_ARG(ARG_pin_cs, NUM_BANK0_GPIOS), |
| 35 | +#else |
| 36 | +#define CYW43_PIN_ARRAY 0 |
| 37 | +#endif |
| 38 | + |
| 39 | +static void rp2_cyw43_init(uint *pins, uint32_t clock_div_int, uint8_t clock_div_frac8) { |
| 40 | + static bool cyw43_init_done; |
| 41 | + if (!cyw43_init_done) { |
| 42 | + cyw43_init(&cyw43_state); |
| 43 | + cyw43_irq_init(); |
| 44 | + cyw43_post_poll_hook(); // enable the irq |
| 45 | + cyw43_init_done = true; |
| 46 | + uint8_t buf[8]; |
| 47 | + memcpy(&buf[0], "PICO", 4); |
| 48 | + |
| 49 | + // Use unique id to generate the default AP ssid. |
| 50 | + const char hexchr[16] = "0123456789ABCDEF"; |
| 51 | + pico_unique_board_id_t pid; |
| 52 | + pico_get_unique_board_id(&pid); |
| 53 | + buf[4] = hexchr[pid.id[7] >> 4]; |
| 54 | + buf[5] = hexchr[pid.id[6] & 0xf]; |
| 55 | + buf[6] = hexchr[pid.id[5] >> 4]; |
| 56 | + buf[7] = hexchr[pid.id[4] & 0xf]; |
| 57 | + cyw43_wifi_ap_set_ssid(&cyw43_state, 8, buf); |
| 58 | + cyw43_wifi_ap_set_auth(&cyw43_state, CYW43_AUTH_WPA2_AES_PSK); |
| 59 | + cyw43_wifi_ap_set_password(&cyw43_state, 8, (const uint8_t *)"picoW123"); |
| 60 | + } |
| 61 | + |
| 62 | + #if CYW43_PIN_WL_DYNAMIC |
| 63 | + assert(pins); |
| 64 | + // Set unspecified pins |
| 65 | + for (int i = 0; i < CYW43_PIN_INDEX_WL_COUNT; i++) { |
| 66 | + if (pins[i] == NUM_BANK0_GPIOS) { |
| 67 | + pins[i] = cyw43_get_pin_wl(i); |
| 68 | + } |
| 69 | + } |
| 70 | + // check if the pins have actually changed |
| 71 | + for (int i = 0; i < CYW43_PIN_INDEX_WL_COUNT; i++) { |
| 72 | + if (pins[i] != cyw43_get_pin_wl(i)) { |
| 73 | + // re-initialise cyw43. This can fail if the pins are invalid (gpio base is incompatible) or the pio is in use |
| 74 | + int error = cyw43_set_pins_wl(pins); |
| 75 | + if (error == PICO_ERROR_RESOURCE_IN_USE) { |
| 76 | + mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("wifi in use")); |
| 77 | + } else if (error != PICO_OK) { |
| 78 | + mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("wifi pins invalid")); |
| 79 | + } |
| 80 | + cyw43_irq_deinit(); |
| 81 | + cyw43_irq_init(); |
| 82 | + break; |
| 83 | + } |
| 84 | + } |
| 85 | + #endif // CYW43_PIN_WL_DYNAMIC |
| 86 | + #if CYW43_PIO_CLOCK_DIV_DYNAMIC |
| 87 | + // set the pio clock divisor - has no effect if the pio is in use |
| 88 | + if (clock_div_int > 0) { |
| 89 | + cyw43_set_pio_clkdiv_int_frac8(clock_div_int, clock_div_frac8); |
| 90 | + } |
| 91 | + #endif // CYW43_PIO_CLOCK_DIV_DYNAMIC |
| 92 | +} |
| 93 | + |
| 94 | +static void cyw43_obj_init(mp_arg_val_t *args) { |
| 95 | + assert(args); |
| 96 | + enum { CYW43_INIT_ARG_ENUM }; |
| 97 | + uint pins[] = { |
| 98 | + CYW43_PIN_ARRAY |
| 99 | + }; |
| 100 | + |
| 101 | + uint32_t clock_div_int = 0; |
| 102 | + uint8_t clock_div_frac8 = 0; |
| 103 | + #if CYW43_PIO_CLOCK_DIV_DYNAMIC |
| 104 | + clock_div_int = args[ARG_div_int].u_int; |
| 105 | + clock_div_frac8 = (uint8_t)args[ARG_div_frac].u_int; |
| 106 | + #endif |
| 107 | + |
| 108 | + rp2_cyw43_init(pins, clock_div_int, clock_div_frac8); |
| 109 | +} |
| 110 | + |
| 111 | +void network_cyw43_obj_init(mp_arg_val_t *args) { |
| 112 | + cyw43_obj_init(args); |
| 113 | +} |
| 114 | + |
| 115 | +void bluetooth_ble_obj_init(mp_arg_val_t *args) { |
| 116 | + cyw43_obj_init(args); |
| 117 | +} |
| 118 | + |
| 119 | +// perform extra initialisation in machine_pin_obj_init_helper |
| 120 | +void machine_pin_ext_obj_init(mp_arg_val_t *args) { |
| 121 | + cyw43_obj_init(args); |
| 122 | +} |
0 commit comments