Pico Wireless: Driver Rewrite

* Use a single common function prototype for all (but two) commands
* Remove "last param" from every param, in favour of an "end_cmd"
* Rename "send_cmd" to "start_cmd" to better reflect what it does
* Replace hard-coded param counts with macro (sizeof(params) / sizeof(inParam))
* Eliminate common causes of bugs
* Try to document some functions
* Correct some types (wl_enc_type, peek as bool)
* Handle timeouts & sleep states
* Successful command exits sleep
pull/328/head
Phil Howard 2022-03-24 18:06:29 +00:00
rodzic 9926481385
commit 732e59b314
5 zmienionych plików z 766 dodań i 1060 usunięć

Wyświetl plik

@ -6,9 +6,6 @@
#include "spi_drv.hpp"
#include "ip_address.hpp"
#define WARN(message) {}
#define WL_FW_VER_LENGTH 6
#define WIFI_SPI_ACK 1
@ -131,8 +128,8 @@ namespace pimoroni {
//From https://github.com/adafruit/WiFiNINA/blob/master/src/utility/wifi_drv.cpp
//--------------------------------------------------
void get_network_data(uint8_t *ip_out, uint8_t *mask_out, uint8_t *gwip_out);
void get_remote_data(uint8_t sock, uint8_t *ip_out, uint8_t *port_out);
bool get_network_data(uint8_t *ip_out, uint8_t *mask_out, uint8_t *gwip_out);
bool get_remote_data(uint8_t sock, uint8_t *ip_out, uint8_t *port_out);
int8_t wifi_set_network(const std::string ssid);
int8_t wifi_set_passphrase(const std::string ssid, const std::string passphrase);
@ -147,9 +144,9 @@ namespace pimoroni {
uint8_t get_connection_status();
uint8_t* get_mac_address();
void get_ip_address(IPAddress &ip_out);
void get_subnet_mask(IPAddress &mask_out);
void get_gateway_ip(IPAddress &ip_out);
bool get_ip_address(IPAddress &ip_out);
bool get_subnet_mask(IPAddress &mask_out);
bool get_gateway_ip(IPAddress &ip_out);
std::string get_current_ssid();
uint8_t* get_current_bssid();
@ -160,7 +157,7 @@ namespace pimoroni {
uint8_t get_scan_networks();
const char* get_ssid_networks(uint8_t network_item);
uint8_t get_enc_type_networks(uint8_t network_item);
wl_enc_type get_enc_type_networks(uint8_t network_item);
uint8_t* get_bssid_networks(uint8_t network_item, uint8_t* bssid_out);
uint8_t get_channel_networks(uint8_t network_item);
int32_t get_rssi_networks(uint8_t network_item);
@ -200,9 +197,11 @@ namespace pimoroni {
uint8_t get_server_state(uint8_t sock);
uint8_t get_client_state(uint8_t sock);
uint16_t avail_data(uint8_t sock);
uint8_t avail_server(uint8_t sock);
uint8_t avail_server(uint8_t sock); // a weird copy of avail_data that truncates to uint8_t and returns 255 if unavailable...
// see: https://github.com/arduino-libraries/WiFiNINA/blob/e74d115d252bac24267e4b1a504c033f399924f2/src/utility/server_drv.cpp#L228-L288
// and: https://github.com/adafruit/nina-fw/blob/d73fe315cc7f9148a0918490d3b75430c8444bf7/main/CommandHandler.cpp#L437-L498
bool get_data(uint8_t sock, uint8_t *data_out, uint8_t peek);
bool get_data(uint8_t sock, uint8_t *data_out, bool peek);
bool get_data_buf(uint8_t sock, uint8_t *data_out, uint16_t *data_len_out);
bool insert_data_buf(uint8_t sock, const uint8_t *data_in, uint16_t len);
bool send_udp_data(uint8_t sock);
@ -217,6 +216,10 @@ namespace pimoroni {
void wifi_set_ent_username(const std::string username);
void wifi_set_ent_password(const std::string password);
void wifi_set_ent_enable();
void sleep_set_wake_pin(uint8_t wake_pin);
void sleep_light();
void sleep_deep(uint8_t time);
};
}

Wyświetl plik

@ -49,22 +49,38 @@ namespace pimoroni {
return gpio_get(ack);
}
void SpiDrv::wait_for_esp_ack() {
while(!get_esp_ack()) {
bool SpiDrv::wait_for_esp_ack(uint32_t timeout_ms) {
absolute_time_t timeout = make_timeout_time_ms(timeout_ms);
while(!get_esp_ack()) {
tight_loop_contents();
if (absolute_time_diff_us(get_absolute_time(), timeout) <= 0) {
return false;
}
}
return true;
}
void SpiDrv::wait_for_esp_ready() {
bool SpiDrv::wait_for_esp_ready(uint32_t timeout_ms) {
absolute_time_t timeout = make_timeout_time_ms(timeout_ms);
while(!get_esp_ready()) {
tight_loop_contents();
if (absolute_time_diff_us(get_absolute_time(), timeout) <= 0) {
return false;
}
}
return true;
}
void SpiDrv::wait_for_esp_select() {
wait_for_esp_ready();
bool SpiDrv::wait_for_esp_select(uint32_t timeout_ms) {
if(!wait_for_esp_ready(timeout_ms)) {
return false;
}
esp_select();
wait_for_esp_ack();
if(!wait_for_esp_ack(timeout_ms)) {
esp_deselect();
return false;
}
return true;
}
int SpiDrv::wait_for_byte(uint8_t wait_byte) {
@ -73,7 +89,7 @@ namespace pimoroni {
do{
byte_read = read_byte(); //get data byte
if (byte_read == ERR_CMD) {
printf("Err cmd received\n");
WARN("Err cmd received\n");
return -1;
}
} while((timeout-- > 0) && (byte_read != wait_byte));
@ -91,7 +107,7 @@ namespace pimoroni {
return byte_read;
}
bool SpiDrv::wait_response_params(uint8_t cmd, uint8_t num_param, tParam *params_out) {
bool SpiDrv::wait_response_params(uint8_t cmd, uint8_t num_param, outParam *params_out) {
uint8_t data = 0;
int i = 0;
@ -106,12 +122,12 @@ namespace pimoroni {
}
}
else {
printf("Error num_param == 0\n");
WARN("Error num_param == 0\n");
return false;
}
if(num_param != num_param_read) {
printf("Mismatch num_param\n");
WARN("Mismatch num_param\n");
return false;
}
@ -120,7 +136,7 @@ namespace pimoroni {
return true;
}
bool SpiDrv::wait_response_cmd(uint8_t cmd, uint8_t num_param, uint8_t *param_out, uint8_t *param_len_out) {
bool SpiDrv::wait_response_cmd(uint8_t cmd, uint8_t num_param, uint8_t *param_out, uint16_t *param_len_out) {
uint8_t data = 0;
int ii = 0;
@ -132,7 +148,7 @@ namespace pimoroni {
for(ii = 0; ii < (*param_len_out); ++ii) {
get_param(&param_out[ii]);
}
}
}
read_and_check_byte(END_CMD, &data);
}
@ -140,7 +156,7 @@ namespace pimoroni {
return true;
}
bool SpiDrv::wait_response_data8(uint8_t cmd, uint8_t *param_out, uint8_t *param_len_out) {
bool SpiDrv::wait_response_data8(uint8_t cmd, uint8_t *param_out, uint16_t *param_len_out) {
uint8_t data = 0;
IF_CHECK_START_CMD() {
@ -150,7 +166,7 @@ namespace pimoroni {
if(num_param_read != 0) {
read_param_len8(param_len_out);
spi_read_blocking(spi, DUMMY_DATA, param_out, *param_len_out);
}
}
read_and_check_byte(END_CMD, &data);
}
@ -168,7 +184,7 @@ namespace pimoroni {
if(num_param_read != 0) {
read_param_len16(param_len_out);
spi_read_blocking(spi, DUMMY_DATA, param_out, *param_len_out);
}
}
read_and_check_byte(END_CMD, &data);
}
@ -176,7 +192,7 @@ namespace pimoroni {
return false;
}
bool SpiDrv::wait_response(uint8_t cmd, uint8_t *num_param_out, uint8_t **params_out, uint8_t max_num_params) {
bool SpiDrv::wait_response(uint8_t cmd, uint16_t *num_param_out, uint8_t **params_out, uint8_t max_num_params) {
uint8_t data = 0;
int i = 0;
@ -202,7 +218,7 @@ namespace pimoroni {
}
}
else {
printf("Error numParams == 0\n");
WARN("Error numParams == 0\n");
read_and_check_byte(END_CMD, &data);
return false;
}
@ -211,19 +227,16 @@ namespace pimoroni {
return true;
}
void SpiDrv::send_param(const uint8_t *param, uint8_t param_len, lastParam last_param) {
void SpiDrv::send_param(const uint8_t *param, uint8_t param_len) {
send_param_len8(param_len);
spi_write_blocking(spi, param, param_len);
if(last_param) {
uint8_t buf = END_CMD;
spi_write_blocking(spi, &buf, 1);
}
command_length += param_len;
}
void SpiDrv::send_param_len8(uint8_t param_len) {
spi_write_blocking(spi, &param_len, 1);
command_length += 1;
}
void SpiDrv::send_param_len16(uint16_t param_len) {
@ -231,9 +244,10 @@ namespace pimoroni {
buf[0] = (uint8_t)((param_len & 0xff00) >> 8);
buf[1] = (uint8_t)(param_len & 0xff);
spi_write_blocking(spi, buf, 2);
command_length += 2;
}
uint8_t SpiDrv::read_param_len8(uint8_t *param_len_out) {
uint8_t SpiDrv::read_param_len8(uint16_t *param_len_out) {
uint8_t param_len;
get_param(&param_len);
if(param_len_out != nullptr) {
@ -252,53 +266,30 @@ namespace pimoroni {
return param_len;
}
void SpiDrv::send_buffer(const uint8_t* param, uint16_t param_len, lastParam last_param) {
void SpiDrv::send_buffer(const uint8_t* param, uint16_t param_len) {
send_param_len16(param_len);
spi_write_blocking(spi, param, param_len);
if(last_param) {
uint8_t buf = END_CMD;
spi_write_blocking(spi, &buf, 1);
}
}
void SpiDrv::send_param(uint16_t param, lastParam last_param) {
send_param_len8(2);
uint8_t buf[2];
buf[0] = (uint8_t)((param & 0xff00) >> 8);
buf[1] = (uint8_t)(param & 0xff);
spi_write_blocking(spi, buf, 2);
if(last_param) {
uint8_t buf = END_CMD;
spi_write_blocking(spi, &buf, 1);
}
}
void SpiDrv::send_byte_param(uint8_t param, lastParam last_param) {
send_param_len8(1);
spi_write_blocking(spi, &param, 1);
if(last_param) {
uint8_t buf = END_CMD;
spi_write_blocking(spi, &buf, 1);
}
command_length += param_len;
}
void SpiDrv::send_cmd(uint8_t cmd, uint8_t num_param) {
void SpiDrv::start_cmd(uint8_t cmd, uint8_t num_param) {
uint8_t buf[3];
buf[0] = START_CMD;
buf[1] = cmd & ~(REPLY_FLAG);
buf[2] = num_param;
spi_write_blocking(spi, buf, 3);
if(num_param == 0) {
uint8_t buf = END_CMD;
spi_write_blocking(spi, &buf, 1);
}
command_length = 3;
}
void SpiDrv::end_cmd() {
uint8_t buf = END_CMD;
spi_write_blocking(spi, &buf, 1);
command_length += 1;
WARN("Command len: %ld\n", command_length);
pad_to_multiple_of_4(command_length);
command_length = 0;
}
void SpiDrv::pad_to_multiple_of_4(int command_size) {
@ -311,4 +302,96 @@ namespace pimoroni {
void SpiDrv::get_param(uint8_t* param_out) {
spi_read_blocking(spi, DUMMY_DATA, param_out, 1);
}
bool SpiDrv::send_command(uint8_t command, const SpiDrv::inParam *params_in, uint8_t num_in, uint8_t *data, uint16_t *data_len, cmd_response_type response_type) {
if (!wait_for_esp_select()) {
// Timeout waiting for ESP select
// This could be a transport error, or a sleeping EPS32
return false;
}
WARN("\n%s %d\n", commands[command], num_in);
// Send Command
start_cmd(command, num_in);
// Send params
for(uint8_t i = 0; i < num_in; i++) {
SpiDrv::inParam param = params_in[i];
switch(param.type) {
case PARAM_NORMAL:
WARN("param %d\n", param.len);
send_param(param.addr, param.len); // uint8_t length
break;
case PARAM_BUFFER:
WARN("buffer %d\n", param.len);
send_buffer(param.addr, param.len); // uint16_t length
break;
case PARAM_DUMMY:
WARN("dummy\n");
uint8_t dummy = DUMMY_DATA;
send_param(&dummy, 1);
break;
}
}
end_cmd();
esp_deselect();
// Wait for reply
// START_SCAN_NETWORKS is a no-op, and SCAN_NETWORKS will block while the scan is performed
wait_for_esp_select(command == 0x27 ? 30000 : 10000);
*data = -1;
bool status = false;
switch(response_type) {
case RESPONSE_TYPE_NORMAL:
WARN("wait_response\n");
// Currently SCAN_NETWORKS is the only command using "wait_response" so its max_num_params value is hard-coded
status = wait_response(command, data_len, (uint8_t**)data, WL_NETWORKS_LIST_MAXNUM);
break;
case RESPONSE_TYPE_CMD:
WARN("wait_response_cmd\n");
status = wait_response_cmd(command, SpiDrv::PARAM_NUMS_1, data, data_len);
break;
case RESPONSE_TYPE_DATA8:
WARN("wait_response_data8\n");
status = wait_response_data8(command, data, data_len);
break;
case RESPONSE_TYPE_DATA16:
WARN("wait_response_data16\n");
status = wait_response_data16(command, data, data_len);
break;
}
esp_deselect();
if(status) {
// Any successful command should reset sleep status to AWAKE
// a sleeping ESP32 wont respond to commands!
sleep_state = AWAKE;
}
return status;
}
bool SpiDrv::send_command(uint8_t command, SpiDrv::outParam *params_out, SpiDrv::numParams num_out) {
if (!wait_for_esp_select()) {
// Timeout waiting for ESP select!
return false;
}
start_cmd(command, SpiDrv::PARAM_NUMS_1);
uint8_t dummy = DUMMY_DATA;
send_param(&dummy, 1);
end_cmd();
esp_deselect();
wait_for_esp_select();
bool status = wait_response_params(command, num_out, params_out);
esp_deselect();
if(status) {
// Any successful command should reset sleep status to AWAKE
// a sleeping ESP32 wont respond to commands!
sleep_state = AWAKE;
}
return status;
}
}

Wyświetl plik

@ -1,9 +1,15 @@
#pragma once
#include <stdio.h>
#include <string>
#include "pico/stdlib.h"
#include "hardware/spi.h"
//#define WARN(...) {printf(__VA_ARGS__);}
#define WARN(...) {}
#define PARAM_COUNT(params) sizeof(params) / sizeof(SpiDrv::inParam)
// Maximum size of a SSID
#define WL_SSID_MAX_LENGTH 32
// Length of passphrase. Valid lengths are 8-63.
@ -20,7 +26,7 @@
#define IF_CHECK_START_CMD() \
if(!wait_for_byte(START_CMD)) { \
printf("Error waiting START_CMD\n"); \
WARN("Error waiting START_CMD\n"); \
return false; \
} \
else \
@ -28,7 +34,7 @@ else \
#define CHECK_DATA(check, x) \
if(!read_and_check_byte(check, &x)) { \
printf("Reply error\n"); \
WARN("Reply error\n"); \
return false; \
} \
else \
@ -62,7 +68,8 @@ namespace pimoroni {
static const uint8_t CMD_POS = 1; // Position of Command OpCode on SPI stream
static const uint8_t PARAM_LEN_POS = 2; // Position of Param len on SPI stream
static const int BYTE_TIMEOUT = 1000;
static const int BYTE_TIMEOUT = 5000;
static const int SELECT_ACK_TIMEOUT = 5000;
//--------------------------------------------------
@ -78,21 +85,62 @@ namespace pimoroni {
PARAM_NUMS_5
};
enum lastParam : bool {
NO_LAST_PARAM = false,
LAST_PARAM = true,
enum p_type : uint8_t {
PARAM_NORMAL,
PARAM_BUFFER,
PARAM_DUMMY,
};
enum cmd_response_type : uint8_t {
RESPONSE_TYPE_NORMAL,
RESPONSE_TYPE_CMD,
RESPONSE_TYPE_DATA8,
RESPONSE_TYPE_DATA16,
};
enum s_sleep_state : uint8_t {
AWAKE,
LIGHT_SLEEP,
DEEP_SLEEP
};
s_sleep_state sleep_state = AWAKE;
const char* commands[88] = {
// 0x00 -> 0x0f
"NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL",
// 0x10 -> 0x1f
"setNet", "setPassPhrase", "setKey", "NULL", "setIPconfig", "setDNSconfig", "setHostname", "setPowerMode", "setApNet", "setApPassPhrase", "setDebug", "getTemperature", "NULL", "NULL", "NULL", "NULL",
// 0x20 -> 0x2f
"getConnStatus", "getIPaddr", "getMACaddr", "getCurrSSID", "getCurrBSSID", "getCurrRSSI", "getCurrEnct", "scanNetworks", "startServerTcp", "getStateTcp", "dataSentTcp", "availDataTcp", "getDataTcp", "startClientTcp", "stopClientTcp", "getClientStateTcp",
// 0x30 -> 0x3f
"disconnect", "NULL", "getIdxRSSI", "getIdxEnct", "reqHostByName", "getHostByName", "startScanNetworks", "getFwVersion", "NULL", "sendUDPdata", "getRemoteData", "getTime", "getIdxBSSID", "getIdxChannel", "ping", "getSocket",
// 0x40 -> 0x4f
"setClientCert", "setCertKey", "NULL", "NULL", "sendDataTcp", "getDataBufTcp", "insertDataBuf", "NULL", "NULL", "NULL", "wpa2EntSetIdentity", "wpa2EntSetUsername", "wpa2EntSetPassword", "wpa2EntSetCACert", "wpa2EntSetCertKey", "wpa2EntEnable",
// 0x50 -> 0x5f
"setPinMode", "setDigitalWrite", "setAnalogWrite", "setDigitalRead", "setAnalogRead", "setWakePin", "setLightSleep", "setDeepSleep"
};
//--------------------------------------------------
// Substructures
//--------------------------------------------------
public:
struct tParam {
struct outParam {
uint8_t param_len;
uint8_t* param;
};
struct inParam {
const uint8_t *addr = nullptr;
const uint16_t len = 0;
const p_type type = PARAM_NORMAL;
};
//--------------------------------------------------
// Variables
@ -113,6 +161,8 @@ namespace pimoroni {
// Constructors/Destructor
//--------------------------------------------------
public:
uint32_t command_length = 0;
SpiDrv() {}
SpiDrv(spi_inst_t *spi,
@ -137,34 +187,61 @@ namespace pimoroni {
bool get_esp_ready();
bool get_esp_ack();
void wait_for_esp_ack();
void wait_for_esp_ready();
void wait_for_esp_select();
bool wait_for_esp_ack(uint32_t timeout_ms=SELECT_ACK_TIMEOUT);
bool wait_for_esp_ready(uint32_t timeout_ms=SELECT_ACK_TIMEOUT);
bool wait_for_esp_select(uint32_t timeout_ms=SELECT_ACK_TIMEOUT);
int wait_for_byte(uint8_t wait_byte);
bool read_and_check_byte(uint8_t check_byte, uint8_t *byte_out);
uint8_t read_byte();
bool wait_response_params(uint8_t cmd, uint8_t num_param, tParam *params_out);
bool wait_response_cmd(uint8_t cmd, uint8_t num_param, uint8_t *param_out, uint8_t *param_len_out);
bool wait_response_data8(uint8_t cmd, uint8_t *param_out, uint8_t *param_len_out);
bool wait_response_params(uint8_t cmd, uint8_t num_param, outParam *params_out);
bool wait_response_cmd(uint8_t cmd, uint8_t num_param, uint8_t *param_out, uint16_t *param_len_out);
bool wait_response_data8(uint8_t cmd, uint8_t *param_out, uint16_t *param_len_out);
bool wait_response_data16(uint8_t cmd, uint8_t *param_out, uint16_t *param_len_out);
bool wait_response(uint8_t cmd, uint8_t *num_param_out, uint8_t **params_out, uint8_t max_num_params);
bool wait_response(uint8_t cmd, uint16_t *num_param_out, uint8_t **params_out, uint8_t max_num_params);
void send_param(const uint8_t* param, uint8_t param_len, lastParam last_param = NO_LAST_PARAM);
void send_param(const uint8_t* param, uint8_t param_len);
void send_param_len8(uint8_t param_len);
void send_param_len16(uint16_t param_len);
uint8_t read_param_len8(uint8_t *param_len_out = nullptr);
uint8_t read_param_len8(uint16_t *param_len_out = nullptr);
uint16_t read_param_len16(uint16_t *param_len_out = nullptr);
void send_buffer(const uint8_t *param, uint16_t param_len, lastParam last_param = NO_LAST_PARAM);
void send_param(uint16_t param, lastParam last_param = NO_LAST_PARAM);
void send_byte_param(uint8_t param, lastParam last_param = NO_LAST_PARAM);
void send_cmd(uint8_t cmd, uint8_t num_param);
void send_buffer(const uint8_t *param, uint16_t param_len);
void start_cmd(uint8_t cmd, uint8_t num_param);
void end_cmd();
void pad_to_multiple_of_4(int command_size);
static inParam build_param(const std::string *param) {
return inParam{.addr = (const uint8_t *)param->data(), .len = (uint16_t)param->length(), .type = PARAM_NORMAL};
};
static inParam build_param(uint32_t *param) {
// This type is basically just for IP Addresses AFAIK, probably byteswap any uint32_t you want to survive...
return inParam{.addr = (uint8_t *)param, .len = 4, .type = PARAM_NORMAL};
};
static inParam build_param(uint16_t *param) {
// Beware ye who doth not pass a byteswapped uint16_t to this function
return inParam{.addr = (uint8_t *)param, .len = 2, .type = PARAM_NORMAL};
};
static inParam build_param(uint8_t *param) {
// Single bytes are easy, they can't get weird
return inParam{.addr = param, .len = 1, .type = PARAM_NORMAL};
};
static inParam build_param(const uint8_t *buffer, uint16_t len) {
// Normal params are good up to len 255
return inParam{.addr = buffer, .len = len, .type = PARAM_NORMAL};
};
static inParam build_param_buffer(const uint8_t *buffer, uint16_t len) {
// Buffer-type params are for length >255 (usually sending data) but <= 65535 because a uint32_t is too costly for an ESP32 :/
return inParam{.addr = buffer, .len = len, .type = PARAM_BUFFER};
};
static inParam build_param_dummy() {
return inParam{.type = PARAM_DUMMY};
};
bool send_command(uint8_t command, const inParam *params_in, uint8_t num_in, uint8_t *data, uint16_t *data_len, cmd_response_type response_type=RESPONSE_TYPE_CMD);
bool send_command(uint8_t command, SpiDrv::outParam *params_out, SpiDrv::numParams num_out);
private:
void get_param(uint8_t *param_out);
};

Wyświetl plik

@ -340,12 +340,9 @@ mp_obj_t picowireless_get_current_encryption_type() {
}
mp_obj_t picowireless_start_scan_networks() {
if(wireless != nullptr)
return mp_obj_new_int(wireless->start_scan_networks());
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
// This doesn't actually *do* anything, so might as well save a few instructions!
return mp_const_none;
return mp_const_true;
}
mp_obj_t picowireless_get_scan_networks() {