Skip to content

Commit bb8a490

Browse files
added commands for ble through spi
1 parent 0788453 commit bb8a490

File tree

3 files changed

+150
-1
lines changed

3 files changed

+150
-1
lines changed

src/utility/wifi_drv.cpp

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ void WiFiDrv::wifiDriverInit()
113113

114114
void WiFiDrv::wifiDriverDeinit()
115115
{
116-
SpiDrv::end();
117116
}
118117

119118
int8_t WiFiDrv::wifiSetNetwork(const char* ssid, uint8_t ssid_len)
@@ -1663,5 +1662,141 @@ PreferenceType WiFiDrv::prefGetType(const char * key) {
16631662
return type;
16641663
}
16651664

1665+
int WiFiDrv::bleBegin() {
1666+
WAIT_FOR_SLAVE_SELECT();
1667+
1668+
SpiDrv::sendCmd(BLE_BEGIN, PARAM_NUMS_0);
1669+
1670+
SpiDrv::spiSlaveDeselect();
1671+
//Wait the reply elaboration
1672+
SpiDrv::waitForSlaveReady();
1673+
SpiDrv::spiSlaveSelect();
1674+
1675+
uint8_t len = 1;
1676+
uint8_t result = 0;
1677+
SpiDrv::waitResponseCmd(BLE_BEGIN, PARAM_NUMS_1, (uint8_t*)&result, &len);
1678+
SpiDrv::spiSlaveDeselect();
1679+
1680+
return result == 0;
1681+
}
1682+
1683+
void WiFiDrv::bleEnd() {
1684+
WAIT_FOR_SLAVE_SELECT();
1685+
1686+
SpiDrv::sendCmd(BLE_END, PARAM_NUMS_0);
1687+
1688+
SpiDrv::spiSlaveDeselect();
1689+
//Wait the reply elaboration
1690+
SpiDrv::waitForSlaveReady();
1691+
SpiDrv::spiSlaveSelect();
1692+
1693+
uint8_t len = 1;
1694+
uint8_t result = 0;
1695+
SpiDrv::waitResponseCmd(BLE_END, PARAM_NUMS_1, (uint8_t*)&result, &len);
1696+
SpiDrv::spiSlaveDeselect();
1697+
}
1698+
1699+
int WiFiDrv::bleAvailable() {
1700+
WAIT_FOR_SLAVE_SELECT();
1701+
uint16_t result = 0;
1702+
1703+
SpiDrv::sendCmd(BLE_AVAILABLE, PARAM_NUMS_0);
1704+
1705+
SpiDrv::spiSlaveDeselect();
1706+
//Wait the reply elaboration
1707+
SpiDrv::waitForSlaveReady();
1708+
SpiDrv::spiSlaveSelect();
1709+
1710+
uint8_t len = 2;
1711+
SpiDrv::waitResponseCmd(BLE_AVAILABLE, PARAM_NUMS_1, (uint8_t*)&result, &len);
1712+
SpiDrv::spiSlaveDeselect();
1713+
1714+
return result;
1715+
}
1716+
1717+
int WiFiDrv::bleRead(uint8_t data[], size_t length) {
1718+
WAIT_FOR_SLAVE_SELECT();
1719+
1720+
SpiDrv::sendCmd(BLE_READ, PARAM_NUMS_1);
1721+
1722+
int commandSize = 7; // 4 for the normal command length + 3 for the parameter
1723+
uint16_t param = length; // TODO check length doesn't exceed 2^16
1724+
SpiDrv::sendParam((uint8_t*)&param, sizeof(param), LAST_PARAM);
1725+
1726+
// pad to multiple of 4
1727+
while (commandSize % 4 != 0) {
1728+
SpiDrv::readChar();
1729+
commandSize++;
1730+
}
1731+
1732+
SpiDrv::spiSlaveDeselect();
1733+
//Wait the reply elaboration
1734+
SpiDrv::waitForSlaveReady();
1735+
SpiDrv::spiSlaveSelect();
1736+
1737+
uint16_t res_len = 0;
1738+
SpiDrv::waitResponseData16(BLE_READ, data, (uint16_t*)&res_len);
1739+
1740+
SpiDrv::spiSlaveDeselect();
1741+
1742+
return res_len;
1743+
}
1744+
1745+
int WiFiDrv::blePeek(uint8_t data[], size_t length) {
1746+
WAIT_FOR_SLAVE_SELECT();
1747+
1748+
SpiDrv::sendCmd(BLE_PEEK, PARAM_NUMS_1);
1749+
1750+
int commandSize = 7; // 4 for the normal command length + 3 for the parameter
1751+
uint16_t param = length; // TODO check length doesn't exceed 2^16
1752+
SpiDrv::sendParam((uint8_t*)&param, sizeof(param), LAST_PARAM);
1753+
1754+
// pad to multiple of 4
1755+
while (commandSize % 4 != 0) {
1756+
SpiDrv::readChar();
1757+
commandSize++;
1758+
}
1759+
1760+
SpiDrv::spiSlaveDeselect();
1761+
//Wait the reply elaboration
1762+
SpiDrv::waitForSlaveReady();
1763+
SpiDrv::spiSlaveSelect();
1764+
1765+
uint16_t res_len = 0;
1766+
SpiDrv::waitResponseData16(BLE_READ, data, (uint16_t*)&res_len);
1767+
1768+
SpiDrv::spiSlaveDeselect();
1769+
1770+
return res_len;
1771+
}
1772+
1773+
size_t WiFiDrv::bleWrite(const uint8_t* data, size_t len) {
1774+
WAIT_FOR_SLAVE_SELECT();
1775+
1776+
int commandSize = 4;
1777+
SpiDrv::sendCmd(BLE_WRITE, PARAM_NUMS_1);
1778+
1779+
SpiDrv::sendBuffer((uint8_t*)data, len, LAST_PARAM);
1780+
commandSize += len+2;
1781+
1782+
// pad to multiple of 4
1783+
while (commandSize % 4 != 0) {
1784+
SpiDrv::readChar();
1785+
commandSize++;
1786+
}
1787+
1788+
SpiDrv::spiSlaveDeselect();
1789+
//Wait the reply elaboration
1790+
SpiDrv::waitForSlaveReady();
1791+
SpiDrv::spiSlaveSelect();
1792+
1793+
uint8_t res_len = 1;
1794+
uint16_t res = 0;
1795+
SpiDrv::waitResponseCmd(BLE_WRITE, PARAM_NUMS_1, (uint8_t*)&res, &res_len);
1796+
SpiDrv::spiSlaveDeselect();
1797+
1798+
return res;
1799+
}
1800+
16661801

16671802
WiFiDrv wiFiDrv;

src/utility/wifi_drv.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,13 @@ class WiFiDrv
336336
static size_t prefGet(const char * key, PreferenceType type, uint8_t value[], size_t len);
337337
static PreferenceType prefGetType(const char * key);
338338

339+
static int bleBegin();
340+
static void bleEnd();
341+
static int bleAvailable();
342+
static int bleRead(uint8_t data[], size_t length);
343+
static int blePeek(uint8_t data[], size_t length);
344+
static size_t bleWrite(const uint8_t* data, size_t length);
345+
339346
static void applyOTA();
340347

341348
friend class WiFiUDP;

src/utility/wifi_spi.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ enum {
9999
GET_DATABUF_TCP_CMD = 0x45,
100100
INSERT_DATABUF_CMD = 0x46,
101101

102+
BLE_BEGIN = 0x4A,
103+
BLE_END = 0x4B,
104+
BLE_AVAILABLE = 0x4C,
105+
BLE_PEEK = 0x4D,
106+
BLE_READ = 0x4E,
107+
BLE_WRITE = 0x4F,
108+
102109
// regular format commands
103110
SET_PIN_MODE = 0x50,
104111
SET_DIGITAL_WRITE = 0x51,

0 commit comments

Comments
 (0)