ICS 发表于 2024-11-27 18:26

【Follow me第二季第4期】任务一:搭建环境并点亮RGBLED&串口输出

(https://www.eeworld.com.cn/huodong/digikey_follow_me_2024_04/?sid=102)



---

### 任务准备

1. 硬件准备
   1. Arduino® Nano RP2040 Connect开发板
   2. USB-Micro B 数据线
2. 软件准备
   1. 安装Arduino IDE(确保版本支持RP2040芯片)或者 PlatformIO
   2. 安装RP2040的Board支持包

### 从零搭建环境(以PlatformIO为例)

1. 安装 VS Code

   访问(https://code.visualstudio.com/Download)下载**System Installer**的安装包,根据步骤安装即可。

2. 安装 PlatformIO IDE

   1. 在VS Code中点击左边栏的拓展(Ctrl+Shift+X)
   2. 点击搜索框,搜索`PlatformIO IDE`
   3. 点击安装

3. 安装RP2040支持包

   1. 在VS Code中点击左边栏的PlatformIO
   2. 点击Open->New Project
   3. 在Board中选择Arduino Nano RP2040 Connect
   4. 点击Finsh,等待非常长一段时间(示网络情况而定)

### Blink三色LED / 串口打印Hello DigiKey & EEWorld!

一开始以为,串口打印和Blink应该是很简单的事情,结果事实上并不简单。从上面的框图和介绍中可知,RGB LED连接的并不是 RP2040,而是一个叫NINA-W102的东西,一看介绍:240MHz 32位双核Xtensa LX6处理器!!!这不是ESP32嘛!!!并没有直接可以通过RP2040操作RGB LED的引脚,必须先通过SPI与ESP32进行通讯,然后再由ESP 32对RGB LED进行操作。不过官方文档给出了如何操作NINA的库和例程:(https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-web-server-rgb/),例程如下:

```C++
#include <Arduino.h>
#include <WiFiNINA.h># 需要在Platform IO 中安装 WiFi NINA

void fadeLED(NinaPin ledPin)
{
for (int brightness = 255; brightness >= 0; brightness--)
{
    analogWrite(ledPin, brightness);
    delay(10);
}
for (int brightness = 0; brightness <= 255; brightness++)
{
    analogWrite(ledPin, brightness);
    delay(10);
}
}

void setup()
{
Serial.begin(115200);
pinMode(LEDR, OUTPUT);
pinMode(LEDG, OUTPUT);
pinMode(LEDB, OUTPUT);
digitalWrite(LEDR, LOW);
digitalWrite(LEDG, LOW);
digitalWrite(LEDB, LOW);
}

void loop()
{
Serial.println("Hello DigiKey & EEWorld!");
fadeLED(LEDR);
fadeLED(LEDG);
fadeLED(LEDB);
}
```

在`wifi_drv.cpp`可以看到具体实现如下:

```c++
……
void WiFiDrv::pinMode(uint8_t pin, uint8_t mode)
{
    WAIT_FOR_SLAVE_SELECT();
    // Send Command
    SpiDrv::sendCmd(SET_PIN_MODE, PARAM_NUMS_2);
    SpiDrv::sendParam((uint8_t*)&pin, 1, NO_LAST_PARAM);
    SpiDrv::sendParam((uint8_t*)&mode, 1, LAST_PARAM);

    // pad to multiple of 4
    SpiDrv::readChar();

    SpiDrv::spiSlaveDeselect();
    //Wait the reply elaboration
    SpiDrv::waitForSlaveReady();
    SpiDrv::spiSlaveSelect();

    // Wait for reply
    uint8_t _data = 0;
    uint8_t _dataLen = 0;
    if (!SpiDrv::waitResponseCmd(SET_PIN_MODE, PARAM_NUMS_1, &_data, &_dataLen))
    {
      WARN("error waitResponse");
      _data = WL_FAILURE;
    }
    SpiDrv::spiSlaveDeselect();
}

PinStatus WiFiDrv::digitalRead(uint8_t pin)
{
    WAIT_FOR_SLAVE_SELECT();
    // Send Command
    SpiDrv::sendCmd(GET_DIGITAL_READ, PARAM_NUMS_1);
    SpiDrv::sendParam((uint8_t*)&pin, 1, LAST_PARAM);

    // pad to multiple of 4
    SpiDrv::readChar();
    SpiDrv::readChar();

    SpiDrv::spiSlaveDeselect();
    //Wait the reply elaboration
    SpiDrv::waitForSlaveReady();
    SpiDrv::spiSlaveSelect();

    // Wait for reply
    uint8_t _data = 0;
    uint8_t _dataLen = 0;
    if (!SpiDrv::waitResponseCmd(GET_DIGITAL_READ, PARAM_NUMS_1, &_data, &_dataLen))
    {
      WARN("error waitResponse");
      _data = WL_FAILURE;
    }
    SpiDrv::spiSlaveDeselect();

    if (_data == 1)
      return HIGH;
    else
      return LOW;
}

void WiFiDrv::digitalWrite(uint8_t pin, uint8_t value)
{
    WAIT_FOR_SLAVE_SELECT();
    // Send Command
    SpiDrv::sendCmd(SET_DIGITAL_WRITE, PARAM_NUMS_2);
    SpiDrv::sendParam((uint8_t*)&pin, 1, NO_LAST_PARAM);
    SpiDrv::sendParam((uint8_t*)&value, 1, LAST_PARAM);

    // pad to multiple of 4
    SpiDrv::readChar();

    SpiDrv::spiSlaveDeselect();
    //Wait the reply elaboration
    SpiDrv::waitForSlaveReady();
    SpiDrv::spiSlaveSelect();

    // Wait for reply
    uint8_t _data = 0;
    uint8_t _dataLen = 0;
    if (!SpiDrv::waitResponseCmd(SET_DIGITAL_WRITE, PARAM_NUMS_1, &_data, &_dataLen))
    {
      WARN("error waitResponse");
      _data = WL_FAILURE;
    }
    SpiDrv::spiSlaveDeselect();
}

uint16_t WiFiDrv::analogRead(uint8_t adc_channel)
{
    WAIT_FOR_SLAVE_SELECT();
    // Send Command
    SpiDrv::sendCmd(GET_ANALOG_READ, PARAM_NUMS_1);
    SpiDrv::sendParam((uint8_t*)&adc_channel, 1, LAST_PARAM);

    // pad to multiple of 4
    SpiDrv::readChar();
    SpiDrv::readChar();

    SpiDrv::spiSlaveDeselect();
    //Wait the reply elaboration
    SpiDrv::waitForSlaveReady();
    SpiDrv::spiSlaveSelect();

    // Wait for reply
    uint16_t adc_raw = 0;
    uint8_t adc_raw_len = 0;
    if (!SpiDrv::waitResponseCmd(GET_ANALOG_READ, PARAM_NUMS_1, (uint8_t*)&adc_raw, &adc_raw_len))
    {
      WARN("error waitResponse");
    }
    SpiDrv::spiSlaveDeselect();

    return adc_raw;
}

void WiFiDrv::analogWrite(uint8_t pin, uint8_t value)
{
    WAIT_FOR_SLAVE_SELECT();
    // Send Command
    SpiDrv::sendCmd(SET_ANALOG_WRITE, PARAM_NUMS_2);
    SpiDrv::sendParam((uint8_t*)&pin, 1, NO_LAST_PARAM);
    SpiDrv::sendParam((uint8_t*)&value, 1, LAST_PARAM);

    // pad to multiple of 4
    SpiDrv::readChar();

    SpiDrv::spiSlaveDeselect();
    //Wait the reply elaboration
    SpiDrv::waitForSlaveReady();
    SpiDrv::spiSlaveSelect();

    // Wait for reply
    uint8_t _data = 0;
    uint8_t _dataLen = 0;
    if (!SpiDrv::waitResponseCmd(SET_ANALOG_WRITE, PARAM_NUMS_1, &_data, &_dataLen))
    {
      WARN("error waitResponse");
      _data = WL_FAILURE;
    }
    SpiDrv::spiSlaveDeselect();
}
……
```

### 如何烧录

短接REC到GND,双击按键即可进入烧录模式,将编译好的.uf2拖入即可。

### 项目文件及固件




### 实验截图




### 参考资料


(https://docs.arduino.cc/hardware/nano-rp2040-connect/)

(https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-web-server-rgb/)

(https://docs.arduino.cc/resources/datasheets/ABX00053-datasheet.pdf)
页: [1]
查看完整版本: 【Follow me第二季第4期】任务一:搭建环境并点亮RGBLED&串口输出