Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 3990

C/C++ • Re: library for WiFi NINA

$
0
0
I'm trying to port Arduino's WIFININA library to Raspberry Pi: change the pins numbers, replace the SPI functions with the ones from Raspberry SDK etc. Unfortunately already when sending first SPI command it fails. Here is the code:

wifi_drv.cpp:

Code:

int8_t WiFiDrv::wifiSetPassphrase(const char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len){[size=150][size=200][/size][/size]WAIT_FOR_SLAVE_SELECT();    // Send Command    SpiDrv::sendCmd(SET_PASSPHRASE_CMD, PARAM_NUMS_2);    SpiDrv::sendParam((uint8_t*)ssid, ssid_len, NO_LAST_PARAM);    SpiDrv::sendParam((uint8_t*)passphrase, len, LAST_PARAM);}
spi_drv.h:

Code:

#define WAIT_FOR_SLAVE_SELECT()      \if (!SpiDrv::initialized) {           \SpiDrv::begin();      \}                             \SpiDrv::spiSlaveSelect();class SpiDrv{private:static void getParam(uint8_t* param);static int transfer(uint8_t buf_to_write, uint8_t buf_to_read = 11);public:static bool initialized;    static void begin();    static void spiSlaveSelect();static void spiSlaveDeselect();    static void waitForSlaveReady(bool const feed_watchdog = false);    static int waitSpiChar(unsigned char waitChar);    static int readAndCheckChar(char checkChar, char* readChar);    static char readChar();    static int waitResponseCmd(uint8_t cmd, uint8_t numParam, uint8_t* param, uint8_t* param_len);static void sendParam(uint8_t* param, uint8_t param_len, uint8_t lastParam = NO_LAST_PARAM);    static uint8_t readParamLen8(uint8_t* param_len = NULL);    static void sendParam(uint16_t param, uint8_t lastParam = NO_LAST_PARAM);    static void sendCmd(uint8_t cmd, uint8_t numParam);};
spi_drv.cpp:

Code:

// From NANO_RP2040_CONNECT/pins_arduino.h#define PINS_COUNT      (30u)#define NINA_RESETN(3u)#define NINA_GPIO0(2u)#define SPIWIFI_SS(9u)#define SPIWIFI_ACK(10u)#define SPIWIFI_RESET(NINA_RESETN)#define SPIWIFI spi1#define SPI1_CIPO       (8u)#define SPI1_COPI       (11u)#define SPI1_SCK        (14u)static uint8_t SLAVESELECT = SPIWIFI_SS; // ssstatic uint8_t SLAVEREADY  = SPIWIFI_ACK;  // handshake pinstatic uint8_t SLAVERESET  = SPIWIFI_RESET;  // reset pinstatic bool inverted_reset = false;bool SpiDrv::initialized = false;void SpiDrv::begin(){    gpio_set_dir(SLAVESELECT, GPIO_OUT);    gpio_set_dir(SLAVEREADY, GPIO_IN);    gpio_set_dir(SLAVERESET, GPIO_OUT);    gpio_set_dir(NINA_GPIO0, GPIO_OUT);    gpio_put(NINA_GPIO0, 1);    gpio_put(SLAVESELECT, 1);    gpio_put(SLAVERESET, 0);    sleep_ms(10);    gpio_put(SLAVERESET, 1);    sleep_ms(750);    gpio_put(NINA_GPIO0, 0);    gpio_set_dir(NINA_GPIO0, GPIO_IN);    // initialize SPI    spi_init(SPIWIFI, 8000000);    spi_set_format(SPIWIFI, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);    gpio_set_function(SPI1_COPI, GPIO_FUNC_SPI);    gpio_set_function(SPI1_SCK, GPIO_FUNC_SPI);    gpio_init(SPIWIFI_SS);    gpio_set_dir(SPIWIFI_SS, GPIO_OUT);    gpio_put(SPIWIFI_SS, 1);    sleep_ms(10);    initialized = true;}#define waitSlaveReady() (gpio_get(SLAVEREADY) == 0)void SpiDrv::spiSlaveSelect(){    gpio_put(SLAVESELECT, 0);    // wait for up to 5 ms for the NINA to indicate it is not ready for transfer    // the timeout is only needed for the case when the shield or module is not present    uint32_t start_timestamp = to_ms_since_boot(get_absolute_time());    while(!waitSlaveReady() && (to_ms_since_boot(get_absolute_time()) - start_timestamp) < 5)    {        printf("SpiDrv::spiSlaveSelect - wait for Slave Ready \n");       }}void SpiDrv::spiSlaveDeselect(){    gpio_put(SLAVESELECT, 1);}/* Cmd Struct Message *//* _________________________________________________________________________________  *//*| START CMD | C/R  | CMD  |[TOT LEN]| N.PARAM | PARAM LEN | PARAM  | .. | END CMD | *//*|___________|______|______|_________|_________|___________|________|____|_________| *//*|   8 bit   | 1bit | 7bit |  8bit   |  8bit   |   8bit    | nbytes | .. |   8bit  | *//*|___________|______|______|_________|_________|___________|________|____|_________| */void SpiDrv::sendCmd(uint8_t cmd, uint8_t numParam){    printf("SpiDrv::sendCmd - cmd = %u, numParam = %u \n", cmd, numParam);    uint8_t buf_to_write{START_CMD};    transfer(buf_to_write);    buf_to_write = cmd & ~(REPLY_FLAG);    transfer(buf_to_write);    buf_to_write = numParam;    transfer(buf_to_write);    if(numParam == 0)    {        printf("SpiDrv::sendCmd - END_CMD \n");        buf_to_write = END_CMD;        transfer(buf_to_write);    }}int SpiDrv::transfer(uint8_t buf_to_write, uint8_t buf_to_read){    printf("SpiDrv::transfer - buf_to_write = %u, buf_to_read = %u \n", buf_to_write, buf_to_read);    int response{0};    response = spi_write_read_blocking(SPIWIFI, &buf_to_write, &buf_to_read, 1);    printf("SpiDrv::transfer - response = %u, buf_to_read = %u \n", response, buf_to_read);    return response;}
Here is the result of the print:

Code:

SpiDrv::sendCmd - cmd = 17, numParam = 2 SpiDrv::transfer - buf_to_write = 224, buf_to_read = 11 SpiDrv::transfer - response = 1, buf_to_read = 0 SpiDrv::transfer - buf_to_write = 17, buf_to_read = 11 SpiDrv::transfer - response = 1, buf_to_read = 0 SpiDrv::transfer - buf_to_write = 2, buf_to_read = 11 SpiDrv::transfer - response = 1, buf_to_read = 0 
buf_to_read is always "0", but it should contain some response. When I run the same using Arduino's WIFININA, there is always some response. And for the response to command "17" is always "54". Any suggestions what am I doing wrong?

Statistics: Posted by Dominik P — Thu Mar 28, 2024 4:23 am



Viewing all articles
Browse latest Browse all 3990

Trending Articles