1010
1111#if defined(ARDUINO_AVR_UNO_WIFI_REV2) || defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(TARGET_NANO_RP2040_CONNECT)
1212#include " HCINinaSpiTransport.h"
13- #include < WiFiNINA.h>
13+ #include < Arduino_SpiNINA.h>
14+
15+ enum {
16+ BLE_BEGIN = 0x4A ,
17+ BLE_END = 0x4B ,
18+ BLE_AVAILABLE = 0x4C ,
19+ BLE_PEEK = 0x4D ,
20+ BLE_READ = 0x4E ,
21+ BLE_WRITE = 0x4F ,
22+ };
23+
24+ int BleDrv::bleBegin () {
25+ WAIT_FOR_SLAVE_SELECT ();
26+
27+ SpiDrv::sendCmd (BLE_BEGIN, PARAM_NUMS_0);
28+
29+ SpiDrv::spiSlaveDeselect ();
30+ // Wait the reply elaboration
31+ SpiDrv::waitForSlaveReady ();
32+ SpiDrv::spiSlaveSelect ();
33+
34+ uint8_t len = 1 ;
35+ uint8_t result = 0 ;
36+ SpiDrv::waitResponseCmd (BLE_BEGIN, PARAM_NUMS_1, (uint8_t *)&result, &len);
37+ SpiDrv::spiSlaveDeselect ();
38+
39+ return result == 0 ;
40+ }
41+
42+ void BleDrv::bleEnd () {
43+ WAIT_FOR_SLAVE_SELECT ();
44+
45+ SpiDrv::sendCmd (BLE_END, PARAM_NUMS_0);
46+
47+ SpiDrv::spiSlaveDeselect ();
48+ // Wait the reply elaboration
49+ SpiDrv::waitForSlaveReady ();
50+ SpiDrv::spiSlaveSelect ();
51+
52+ uint8_t len = 1 ;
53+ uint8_t result = 0 ;
54+ SpiDrv::waitResponseCmd (BLE_END, PARAM_NUMS_1, (uint8_t *)&result, &len);
55+ SpiDrv::spiSlaveDeselect ();
56+ }
57+
58+ int BleDrv::bleAvailable () {
59+ WAIT_FOR_SLAVE_SELECT ();
60+ uint16_t result = 0 ;
61+
62+ SpiDrv::sendCmd (BLE_AVAILABLE, PARAM_NUMS_0);
63+
64+ SpiDrv::spiSlaveDeselect ();
65+ // Wait the reply elaboration
66+ SpiDrv::waitForSlaveReady ();
67+ SpiDrv::spiSlaveSelect ();
68+
69+ uint8_t len = 2 ;
70+ SpiDrv::waitResponseCmd (BLE_AVAILABLE, PARAM_NUMS_1, (uint8_t *)&result, &len);
71+ SpiDrv::spiSlaveDeselect ();
72+
73+ return result;
74+ }
75+
76+ int BleDrv::bleRead (uint8_t data[], size_t length) {
77+ WAIT_FOR_SLAVE_SELECT ();
78+
79+ SpiDrv::sendCmd (BLE_READ, PARAM_NUMS_1);
80+
81+ int commandSize = 7 ; // 4 for the normal command length + 3 for the parameter
82+ uint16_t param = length; // TODO check length doesn't exceed 2^16
83+ SpiDrv::sendParam ((uint8_t *)¶m, sizeof (param), LAST_PARAM);
84+
85+ // pad to multiple of 4
86+ while (commandSize % 4 != 0 ) {
87+ SpiDrv::readChar ();
88+ commandSize++;
89+ }
90+
91+ SpiDrv::spiSlaveDeselect ();
92+ // Wait the reply elaboration
93+ SpiDrv::waitForSlaveReady ();
94+ SpiDrv::spiSlaveSelect ();
95+
96+ uint16_t res_len = 0 ;
97+ SpiDrv::waitResponseData16 (BLE_READ, data, (uint16_t *)&res_len);
98+
99+ SpiDrv::spiSlaveDeselect ();
100+
101+ return res_len;
102+ }
103+
104+ int BleDrv::blePeek (uint8_t data[], size_t length) {
105+ WAIT_FOR_SLAVE_SELECT ();
106+
107+ SpiDrv::sendCmd (BLE_PEEK, PARAM_NUMS_1);
108+
109+ int commandSize = 7 ; // 4 for the normal command length + 3 for the parameter
110+ uint16_t param = length; // TODO check length doesn't exceed 2^16
111+ SpiDrv::sendParam ((uint8_t *)¶m, sizeof (param), LAST_PARAM);
112+
113+ // pad to multiple of 4
114+ while (commandSize % 4 != 0 ) {
115+ SpiDrv::readChar ();
116+ commandSize++;
117+ }
118+
119+ SpiDrv::spiSlaveDeselect ();
120+ // Wait the reply elaboration
121+ SpiDrv::waitForSlaveReady ();
122+ SpiDrv::spiSlaveSelect ();
123+
124+ uint16_t res_len = 0 ;
125+ SpiDrv::waitResponseData16 (BLE_READ, data, (uint16_t *)&res_len);
126+
127+ SpiDrv::spiSlaveDeselect ();
128+
129+ return res_len;
130+ }
131+
132+ size_t BleDrv::bleWrite (const uint8_t * data, size_t len) {
133+ WAIT_FOR_SLAVE_SELECT ();
134+
135+ int commandSize = 4 ;
136+ SpiDrv::sendCmd (BLE_WRITE, PARAM_NUMS_1);
137+
138+ SpiDrv::sendBuffer ((uint8_t *)data, len, LAST_PARAM);
139+ commandSize += len+2 ;
140+
141+ // pad to multiple of 4
142+ while (commandSize % 4 != 0 ) {
143+ SpiDrv::readChar ();
144+ commandSize++;
145+ }
146+
147+ SpiDrv::spiSlaveDeselect ();
148+ // Wait the reply elaboration
149+ SpiDrv::waitForSlaveReady ();
150+ SpiDrv::spiSlaveSelect ();
151+
152+ uint8_t res_len = 1 ;
153+ uint16_t res = 0 ;
154+ SpiDrv::waitResponseCmd (BLE_WRITE, PARAM_NUMS_1, (uint8_t *)&res, &res_len);
155+ SpiDrv::spiSlaveDeselect ();
156+
157+ return res;
158+ }
14159
15160int HCINinaSpiTransportClass::begin ()
16161{
17- WiFiDrv::wifiDriverInit ();
18- return WiFiDrv ::bleBegin ();
162+ SpiDrv::begin ();
163+ return BleDrv ::bleBegin ();
19164}
20165
21166void HCINinaSpiTransportClass::end ()
22167{
23- WiFiDrv ::bleEnd ();
168+ BleDrv ::bleEnd ();
24169}
25170
26171void HCINinaSpiTransportClass::wait (unsigned long timeout)
@@ -34,28 +179,28 @@ void HCINinaSpiTransportClass::wait(unsigned long timeout)
34179
35180int HCINinaSpiTransportClass::available ()
36181{
37- return WiFiDrv ::bleAvailable ();
182+ return BleDrv ::bleAvailable ();
38183}
39184
40185int HCINinaSpiTransportClass::peek ()
41186{
42187 int res=-1 ;
43- WiFiDrv ::blePeek ((uint8_t *)&res, 1 ); // read a single byte, if nothing is returned we return -1
188+ BleDrv ::blePeek ((uint8_t *)&res, 1 ); // read a single byte, if nothing is returned we return -1
44189
45190 return res;
46191}
47192
48193int HCINinaSpiTransportClass::read ()
49194{
50195 int res=-1 ;
51- WiFiDrv ::bleRead ((uint8_t *)&res, 1 ); // read a single byte, if nothing is returned we return -1
196+ BleDrv ::bleRead ((uint8_t *)&res, 1 ); // read a single byte, if nothing is returned we return -1
52197
53198 return res;
54199}
55200
56201size_t HCINinaSpiTransportClass::write (const uint8_t * data, size_t length)
57202{
58- return WiFiDrv ::bleWrite (data, length);
203+ return BleDrv ::bleWrite (data, length);
59204}
60205
61206HCINinaSpiTransportClass HCINinaSpiTransport;
0 commit comments