Skip to content

Commit 8e062ec

Browse files
added commands for ble through spi
1 parent 95c0b15 commit 8e062ec

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

src/utility/wifi_drv.cpp

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,5 +1633,141 @@ PreferenceType WiFiDrv::prefGetType(const char * key) {
16331633
return type;
16341634
}
16351635

1636+
int WiFiDrv::bleBegin() {
1637+
WAIT_FOR_SLAVE_SELECT();
1638+
1639+
SpiDrv::sendCmd(BLE_BEGIN, PARAM_NUMS_0);
1640+
1641+
SpiDrv::spiSlaveDeselect();
1642+
//Wait the reply elaboration
1643+
SpiDrv::waitForSlaveReady();
1644+
SpiDrv::spiSlaveSelect();
1645+
1646+
uint8_t len = 1;
1647+
uint8_t result = 0;
1648+
SpiDrv::waitResponseCmd(BLE_BEGIN, PARAM_NUMS_1, (uint8_t*)&result, &len);
1649+
SpiDrv::spiSlaveDeselect();
1650+
1651+
return result == 0;
1652+
}
1653+
1654+
void WiFiDrv::bleEnd() {
1655+
WAIT_FOR_SLAVE_SELECT();
1656+
1657+
SpiDrv::sendCmd(BLE_END, PARAM_NUMS_0);
1658+
1659+
SpiDrv::spiSlaveDeselect();
1660+
//Wait the reply elaboration
1661+
SpiDrv::waitForSlaveReady();
1662+
SpiDrv::spiSlaveSelect();
1663+
1664+
uint8_t len = 1;
1665+
uint8_t result = 0;
1666+
SpiDrv::waitResponseCmd(BLE_END, PARAM_NUMS_1, (uint8_t*)&result, &len);
1667+
SpiDrv::spiSlaveDeselect();
1668+
}
1669+
1670+
int WiFiDrv::bleAvailable() {
1671+
WAIT_FOR_SLAVE_SELECT();
1672+
uint16_t result = 0;
1673+
1674+
SpiDrv::sendCmd(BLE_AVAILABLE, PARAM_NUMS_0);
1675+
1676+
SpiDrv::spiSlaveDeselect();
1677+
//Wait the reply elaboration
1678+
SpiDrv::waitForSlaveReady();
1679+
SpiDrv::spiSlaveSelect();
1680+
1681+
uint8_t len = 2;
1682+
SpiDrv::waitResponseCmd(BLE_AVAILABLE, PARAM_NUMS_1, (uint8_t*)&result, &len);
1683+
SpiDrv::spiSlaveDeselect();
1684+
1685+
return result;
1686+
}
1687+
1688+
int WiFiDrv::bleRead(uint8_t data[], size_t length) {
1689+
WAIT_FOR_SLAVE_SELECT();
1690+
1691+
SpiDrv::sendCmd(BLE_READ, PARAM_NUMS_1);
1692+
1693+
int commandSize = 7; // 4 for the normal command length + 3 for the parameter
1694+
uint16_t param = length; // TODO check length doesn't exceed 2^16
1695+
SpiDrv::sendParam((uint8_t*)&param, sizeof(param), LAST_PARAM);
1696+
1697+
// pad to multiple of 4
1698+
while (commandSize % 4 != 0) {
1699+
SpiDrv::readChar();
1700+
commandSize++;
1701+
}
1702+
1703+
SpiDrv::spiSlaveDeselect();
1704+
//Wait the reply elaboration
1705+
SpiDrv::waitForSlaveReady();
1706+
SpiDrv::spiSlaveSelect();
1707+
1708+
uint16_t res_len = 0;
1709+
SpiDrv::waitResponseData16(BLE_READ, data, (uint16_t*)&res_len);
1710+
1711+
SpiDrv::spiSlaveDeselect();
1712+
1713+
return res_len;
1714+
}
1715+
1716+
int WiFiDrv::blePeek(uint8_t data[], size_t length) {
1717+
WAIT_FOR_SLAVE_SELECT();
1718+
1719+
SpiDrv::sendCmd(BLE_PEEK, PARAM_NUMS_1);
1720+
1721+
int commandSize = 7; // 4 for the normal command length + 3 for the parameter
1722+
uint16_t param = length; // TODO check length doesn't exceed 2^16
1723+
SpiDrv::sendParam((uint8_t*)&param, sizeof(param), LAST_PARAM);
1724+
1725+
// pad to multiple of 4
1726+
while (commandSize % 4 != 0) {
1727+
SpiDrv::readChar();
1728+
commandSize++;
1729+
}
1730+
1731+
SpiDrv::spiSlaveDeselect();
1732+
//Wait the reply elaboration
1733+
SpiDrv::waitForSlaveReady();
1734+
SpiDrv::spiSlaveSelect();
1735+
1736+
uint16_t res_len = 0;
1737+
SpiDrv::waitResponseData16(BLE_READ, data, (uint16_t*)&res_len);
1738+
1739+
SpiDrv::spiSlaveDeselect();
1740+
1741+
return res_len;
1742+
}
1743+
1744+
size_t WiFiDrv::bleWrite(const uint8_t* data, size_t len) {
1745+
WAIT_FOR_SLAVE_SELECT();
1746+
1747+
int commandSize = 4;
1748+
SpiDrv::sendCmd(BLE_WRITE, PARAM_NUMS_1);
1749+
1750+
SpiDrv::sendBuffer((uint8_t*)data, len, LAST_PARAM);
1751+
commandSize += len+2;
1752+
1753+
// pad to multiple of 4
1754+
while (commandSize % 4 != 0) {
1755+
SpiDrv::readChar();
1756+
commandSize++;
1757+
}
1758+
1759+
SpiDrv::spiSlaveDeselect();
1760+
//Wait the reply elaboration
1761+
SpiDrv::waitForSlaveReady();
1762+
SpiDrv::spiSlaveSelect();
1763+
1764+
uint8_t res_len = 1;
1765+
uint16_t res = 0;
1766+
SpiDrv::waitResponseCmd(PREFERENCES_PUT, PARAM_NUMS_1, (uint8_t*)&res, &res_len);
1767+
SpiDrv::spiSlaveDeselect();
1768+
1769+
return res;
1770+
}
1771+
16361772

16371773
WiFiDrv wiFiDrv;

src/utility/wifi_drv.h

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

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

340347
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)