Skip to content

Commit 5090245

Browse files
committed
Use SpiNINA library
1 parent 999e9d7 commit 5090245

File tree

4 files changed

+195
-8
lines changed

4 files changed

+195
-8
lines changed

src/utility/HCINinaSpiTransport.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@
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 "spi_drv.h"
14+
#include "ble_drv.h"
1415

1516
int HCINinaSpiTransportClass::begin()
1617
{
17-
WiFiDrv::wifiDriverInit();
18-
return WiFiDrv::bleBegin();
18+
SpiDrv::begin();
19+
return BleDrv::bleBegin();
1920
}
2021

2122
void HCINinaSpiTransportClass::end()
2223
{
23-
WiFiDrv::bleEnd();
24+
BleDrv::bleEnd();
2425
}
2526

2627
void HCINinaSpiTransportClass::wait(unsigned long timeout)
@@ -34,28 +35,28 @@ void HCINinaSpiTransportClass::wait(unsigned long timeout)
3435

3536
int HCINinaSpiTransportClass::available()
3637
{
37-
return WiFiDrv::bleAvailable();
38+
return BleDrv::bleAvailable();
3839
}
3940

4041
int HCINinaSpiTransportClass::peek()
4142
{
4243
int res=-1;
43-
WiFiDrv::blePeek((uint8_t*)&res, 1); // read a single byte, if nothing is returned we return -1
44+
BleDrv::blePeek((uint8_t*)&res, 1); // read a single byte, if nothing is returned we return -1
4445

4546
return res;
4647
}
4748

4849
int HCINinaSpiTransportClass::read()
4950
{
5051
int res=-1;
51-
WiFiDrv::bleRead((uint8_t*)&res, 1); // read a single byte, if nothing is returned we return -1
52+
BleDrv::bleRead((uint8_t*)&res, 1); // read a single byte, if nothing is returned we return -1
5253

5354
return res;
5455
}
5556

5657
size_t HCINinaSpiTransportClass::write(const uint8_t* data, size_t length)
5758
{
58-
return WiFiDrv::bleWrite(data, length);
59+
return BleDrv::bleWrite(data, length);
5960
}
6061

6162
HCINinaSpiTransportClass HCINinaSpiTransport;

src/utility/ble_drv.cpp

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
2+
#include <stdio.h>
3+
#include <string.h>
4+
#include <stdint.h>
5+
6+
#include "Arduino.h"
7+
#include <spi_drv.h>
8+
#include "utility/ble_drv.h"
9+
10+
11+
extern "C" {
12+
#include "utility/ble_spi.h"
13+
}
14+
15+
16+
int BleDrv::bleBegin() {
17+
WAIT_FOR_SLAVE_SELECT();
18+
19+
SpiDrv::sendCmd(BLE_BEGIN, PARAM_NUMS_0);
20+
21+
SpiDrv::spiSlaveDeselect();
22+
//Wait the reply elaboration
23+
SpiDrv::waitForSlaveReady();
24+
SpiDrv::spiSlaveSelect();
25+
26+
uint8_t len = 1;
27+
uint8_t result = 0;
28+
SpiDrv::waitResponseCmd(BLE_BEGIN, PARAM_NUMS_1, (uint8_t*)&result, &len);
29+
SpiDrv::spiSlaveDeselect();
30+
31+
return result == 0;
32+
}
33+
34+
void BleDrv::bleEnd() {
35+
WAIT_FOR_SLAVE_SELECT();
36+
37+
SpiDrv::sendCmd(BLE_END, PARAM_NUMS_0);
38+
39+
SpiDrv::spiSlaveDeselect();
40+
//Wait the reply elaboration
41+
SpiDrv::waitForSlaveReady();
42+
SpiDrv::spiSlaveSelect();
43+
44+
uint8_t len = 1;
45+
uint8_t result = 0;
46+
SpiDrv::waitResponseCmd(BLE_END, PARAM_NUMS_1, (uint8_t*)&result, &len);
47+
SpiDrv::spiSlaveDeselect();
48+
}
49+
50+
int BleDrv::bleAvailable() {
51+
WAIT_FOR_SLAVE_SELECT();
52+
uint16_t result = 0;
53+
54+
SpiDrv::sendCmd(BLE_AVAILABLE, PARAM_NUMS_0);
55+
56+
SpiDrv::spiSlaveDeselect();
57+
//Wait the reply elaboration
58+
SpiDrv::waitForSlaveReady();
59+
SpiDrv::spiSlaveSelect();
60+
61+
uint8_t len = 2;
62+
SpiDrv::waitResponseCmd(BLE_AVAILABLE, PARAM_NUMS_1, (uint8_t*)&result, &len);
63+
SpiDrv::spiSlaveDeselect();
64+
65+
return result;
66+
}
67+
68+
int BleDrv::bleRead(uint8_t data[], size_t length) {
69+
WAIT_FOR_SLAVE_SELECT();
70+
71+
SpiDrv::sendCmd(BLE_READ, PARAM_NUMS_1);
72+
73+
int commandSize = 7; // 4 for the normal command length + 3 for the parameter
74+
uint16_t param = length; // TODO check length doesn't exceed 2^16
75+
SpiDrv::sendParam((uint8_t*)&param, sizeof(param), LAST_PARAM);
76+
77+
// pad to multiple of 4
78+
while (commandSize % 4 != 0) {
79+
SpiDrv::readChar();
80+
commandSize++;
81+
}
82+
83+
SpiDrv::spiSlaveDeselect();
84+
//Wait the reply elaboration
85+
SpiDrv::waitForSlaveReady();
86+
SpiDrv::spiSlaveSelect();
87+
88+
uint16_t res_len = 0;
89+
SpiDrv::waitResponseData16(BLE_READ, data, (uint16_t*)&res_len);
90+
91+
SpiDrv::spiSlaveDeselect();
92+
93+
return res_len;
94+
}
95+
96+
int BleDrv::blePeek(uint8_t data[], size_t length) {
97+
WAIT_FOR_SLAVE_SELECT();
98+
99+
SpiDrv::sendCmd(BLE_PEEK, PARAM_NUMS_1);
100+
101+
int commandSize = 7; // 4 for the normal command length + 3 for the parameter
102+
uint16_t param = length; // TODO check length doesn't exceed 2^16
103+
SpiDrv::sendParam((uint8_t*)&param, sizeof(param), LAST_PARAM);
104+
105+
// pad to multiple of 4
106+
while (commandSize % 4 != 0) {
107+
SpiDrv::readChar();
108+
commandSize++;
109+
}
110+
111+
SpiDrv::spiSlaveDeselect();
112+
//Wait the reply elaboration
113+
SpiDrv::waitForSlaveReady();
114+
SpiDrv::spiSlaveSelect();
115+
116+
uint16_t res_len = 0;
117+
SpiDrv::waitResponseData16(BLE_READ, data, (uint16_t*)&res_len);
118+
119+
SpiDrv::spiSlaveDeselect();
120+
121+
return res_len;
122+
}
123+
124+
size_t BleDrv::bleWrite(const uint8_t* data, size_t len) {
125+
WAIT_FOR_SLAVE_SELECT();
126+
127+
int commandSize = 4;
128+
SpiDrv::sendCmd(BLE_WRITE, PARAM_NUMS_1);
129+
130+
SpiDrv::sendBuffer((uint8_t*)data, len, LAST_PARAM);
131+
commandSize += len+2;
132+
133+
// pad to multiple of 4
134+
while (commandSize % 4 != 0) {
135+
SpiDrv::readChar();
136+
commandSize++;
137+
}
138+
139+
SpiDrv::spiSlaveDeselect();
140+
//Wait the reply elaboration
141+
SpiDrv::waitForSlaveReady();
142+
SpiDrv::spiSlaveSelect();
143+
144+
uint8_t res_len = 1;
145+
uint16_t res = 0;
146+
SpiDrv::waitResponseCmd(BLE_WRITE, PARAM_NUMS_1, (uint8_t*)&res, &res_len);
147+
SpiDrv::spiSlaveDeselect();
148+
149+
return res;
150+
}
151+
152+
153+
BleDrv bleDrv;

src/utility/ble_drv.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
#pragma once
3+
4+
#include <inttypes.h>
5+
6+
7+
8+
class BleDrv
9+
{
10+
public:
11+
static int bleBegin();
12+
static void bleEnd();
13+
static int bleAvailable();
14+
static int bleRead(uint8_t data[], size_t length);
15+
static int blePeek(uint8_t data[], size_t length);
16+
static size_t bleWrite(const uint8_t* data, size_t length);
17+
18+
};
19+
20+
extern BleDrv bleDrv;

src/utility/ble_spi.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
enum {
4+
5+
6+
BLE_BEGIN = 0x4A,
7+
BLE_END = 0x4B,
8+
BLE_AVAILABLE = 0x4C,
9+
BLE_PEEK = 0x4D,
10+
BLE_READ = 0x4E,
11+
BLE_WRITE = 0x4F,
12+
13+
};

0 commit comments

Comments
 (0)