Skip to content

Commit 6e6570b

Browse files
authored
Merge pull request #167 from arduino-libraries/rp2040-analog-digital-read-write
Allow access to RGB LED as well as A4-A7 via classic Arduino APIs
2 parents aed56a2 + 6d87c64 commit 6e6570b

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

src/utility/nano_rp2040_support.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
This file is part of the WiFiNINA library.
3+
Copyright (c) 2021 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifdef ARDUINO_NANO_RP2040_CONNECT
21+
22+
/******************************************************************************
23+
* INCLUDE
24+
******************************************************************************/
25+
26+
#include "nina_pins.h" /* variants/NANO_RP2040_CONNECT/ninaPins.h */
27+
#include "wifi_drv.h"
28+
29+
/******************************************************************************
30+
* FUNCTION DEFINITION
31+
******************************************************************************/
32+
33+
uint8_t toAnalogPin(NinaPin pin)
34+
{
35+
if (pin == A4) return 6; /* ADC1 - CH6 */
36+
else if (pin == A5) return 3; /* ADC1 - CH3 */
37+
else if (pin == A6) return 0; /* ADC1 - CH0 */
38+
else if (pin == A7) return 7; /* ADC1 - CH7 */
39+
else return 0xFF;
40+
}
41+
42+
void pinMode(NinaPin pin, PinMode mode)
43+
{
44+
WiFiDrv::pinMode(static_cast<uint8_t>(pin), static_cast<uint8_t>(mode));
45+
}
46+
47+
PinStatus digitalRead(NinaPin pin)
48+
{
49+
return WiFiDrv::digitalRead(static_cast<uint8_t>(pin));
50+
}
51+
52+
void digitalWrite(NinaPin pin, PinStatus value)
53+
{
54+
WiFiDrv::digitalWrite(static_cast<uint8_t>(pin), static_cast<uint8_t>(value));
55+
}
56+
57+
int analogRead(NinaPin pin)
58+
{
59+
uint8_t const adc_channel = toAnalogPin(pin);
60+
61+
if (adc_channel == 0xFF)
62+
return 0;
63+
else
64+
return WiFiDrv::analogRead(adc_channel);
65+
}
66+
67+
void analogWrite(NinaPin pin, int value)
68+
{
69+
WiFiDrv::analogWrite(static_cast<uint8_t>(pin), static_cast<uint8_t>(value));
70+
}
71+
72+
#endif /* ARDUINO_NANO_RP2040_CONNECT */

src/utility/wifi_drv.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,38 @@ void WiFiDrv::pinMode(uint8_t pin, uint8_t mode)
10601060
SpiDrv::spiSlaveDeselect();
10611061
}
10621062

1063+
PinStatus WiFiDrv::digitalRead(uint8_t pin)
1064+
{
1065+
WAIT_FOR_SLAVE_SELECT();
1066+
// Send Command
1067+
SpiDrv::sendCmd(GET_DIGITAL_READ, PARAM_NUMS_1);
1068+
SpiDrv::sendParam((uint8_t*)&pin, 1, LAST_PARAM);
1069+
1070+
// pad to multiple of 4
1071+
SpiDrv::readChar();
1072+
SpiDrv::readChar();
1073+
1074+
SpiDrv::spiSlaveDeselect();
1075+
//Wait the reply elaboration
1076+
SpiDrv::waitForSlaveReady();
1077+
SpiDrv::spiSlaveSelect();
1078+
1079+
// Wait for reply
1080+
uint8_t _data = 0;
1081+
uint8_t _dataLen = 0;
1082+
if (!SpiDrv::waitResponseCmd(GET_DIGITAL_READ, PARAM_NUMS_1, &_data, &_dataLen))
1083+
{
1084+
WARN("error waitResponse");
1085+
_data = WL_FAILURE;
1086+
}
1087+
SpiDrv::spiSlaveDeselect();
1088+
1089+
if (_data == 1)
1090+
return HIGH;
1091+
else
1092+
return LOW;
1093+
}
1094+
10631095
void WiFiDrv::digitalWrite(uint8_t pin, uint8_t value)
10641096
{
10651097
WAIT_FOR_SLAVE_SELECT();
@@ -1087,6 +1119,34 @@ void WiFiDrv::digitalWrite(uint8_t pin, uint8_t value)
10871119
SpiDrv::spiSlaveDeselect();
10881120
}
10891121

1122+
uint16_t WiFiDrv::analogRead(uint8_t adc_channel)
1123+
{
1124+
WAIT_FOR_SLAVE_SELECT();
1125+
// Send Command
1126+
SpiDrv::sendCmd(GET_ANALOG_READ, PARAM_NUMS_1);
1127+
SpiDrv::sendParam((uint8_t*)&adc_channel, 1, LAST_PARAM);
1128+
1129+
// pad to multiple of 4
1130+
SpiDrv::readChar();
1131+
SpiDrv::readChar();
1132+
1133+
SpiDrv::spiSlaveDeselect();
1134+
//Wait the reply elaboration
1135+
SpiDrv::waitForSlaveReady();
1136+
SpiDrv::spiSlaveSelect();
1137+
1138+
// Wait for reply
1139+
uint16_t adc_raw = 0;
1140+
uint8_t adc_raw_len = 0;
1141+
if (!SpiDrv::waitResponseCmd(GET_ANALOG_READ, PARAM_NUMS_1, (uint8_t*)&adc_raw, &adc_raw_len))
1142+
{
1143+
WARN("error waitResponse");
1144+
}
1145+
SpiDrv::spiSlaveDeselect();
1146+
1147+
return adc_raw;
1148+
}
1149+
10901150
void WiFiDrv::analogWrite(uint8_t pin, uint8_t value)
10911151
{
10921152
WAIT_FOR_SLAVE_SELECT();

src/utility/wifi_drv.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,9 @@ class WiFiDrv
290290
static void debug(uint8_t on);
291291
static float getTemperature();
292292
static void pinMode(uint8_t pin, uint8_t mode);
293+
static PinStatus digitalRead(uint8_t pin);
293294
static void digitalWrite(uint8_t pin, uint8_t value);
295+
static uint16_t analogRead(uint8_t adc_channel);
294296
static void analogWrite(uint8_t pin, uint8_t value);
295297

296298
static int8_t downloadFile(const char* url, uint8_t url_len, const char *filename, uint8_t filename_len);

src/utility/wifi_spi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ enum {
102102
SET_PIN_MODE = 0x50,
103103
SET_DIGITAL_WRITE = 0x51,
104104
SET_ANALOG_WRITE = 0x52,
105+
GET_DIGITAL_READ = 0x53,
106+
GET_ANALOG_READ = 0x54,
105107

106108
// regular format commands
107109
WRITE_FILE = 0x60,

0 commit comments

Comments
 (0)