Skip to content

Commit 961f668

Browse files
added commands for ble through spi
1 parent c502d45 commit 961f668

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
@@ -1600,5 +1600,141 @@ PreferenceType WiFiDrv::prefGetType(const char * key) {
16001600
return type;
16011601
}
16021602

1603+
int WiFiDrv::bleBegin() {
1604+
WAIT_FOR_SLAVE_SELECT();
1605+
1606+
SpiDrv::sendCmd(BLE_BEGIN, PARAM_NUMS_0);
1607+
1608+
SpiDrv::spiSlaveDeselect();
1609+
//Wait the reply elaboration
1610+
SpiDrv::waitForSlaveReady();
1611+
SpiDrv::spiSlaveSelect();
1612+
1613+
uint8_t len = 1;
1614+
uint8_t result = 0;
1615+
SpiDrv::waitResponseCmd(BLE_BEGIN, PARAM_NUMS_1, (uint8_t*)&result, &len);
1616+
SpiDrv::spiSlaveDeselect();
1617+
1618+
return result == 0;
1619+
}
1620+
1621+
void WiFiDrv::bleEnd() {
1622+
WAIT_FOR_SLAVE_SELECT();
1623+
1624+
SpiDrv::sendCmd(BLE_END, PARAM_NUMS_0);
1625+
1626+
SpiDrv::spiSlaveDeselect();
1627+
//Wait the reply elaboration
1628+
SpiDrv::waitForSlaveReady();
1629+
SpiDrv::spiSlaveSelect();
1630+
1631+
uint8_t len = 1;
1632+
uint8_t result = 0;
1633+
SpiDrv::waitResponseCmd(BLE_END, PARAM_NUMS_1, (uint8_t*)&result, &len);
1634+
SpiDrv::spiSlaveDeselect();
1635+
}
1636+
1637+
int WiFiDrv::bleAvailable() {
1638+
WAIT_FOR_SLAVE_SELECT();
1639+
uint16_t result = 0;
1640+
1641+
SpiDrv::sendCmd(BLE_AVAILABLE, PARAM_NUMS_0);
1642+
1643+
SpiDrv::spiSlaveDeselect();
1644+
//Wait the reply elaboration
1645+
SpiDrv::waitForSlaveReady();
1646+
SpiDrv::spiSlaveSelect();
1647+
1648+
uint8_t len = 2;
1649+
SpiDrv::waitResponseCmd(BLE_AVAILABLE, PARAM_NUMS_1, (uint8_t*)&result, &len);
1650+
SpiDrv::spiSlaveDeselect();
1651+
1652+
return result;
1653+
}
1654+
1655+
int WiFiDrv::bleRead(uint8_t data[], size_t length) {
1656+
WAIT_FOR_SLAVE_SELECT();
1657+
1658+
SpiDrv::sendCmd(BLE_READ, PARAM_NUMS_1);
1659+
1660+
int commandSize = 7; // 4 for the normal command length + 3 for the parameter
1661+
uint16_t param = length; // TODO check length doesn't exceed 2^16
1662+
SpiDrv::sendParam((uint8_t*)&param, sizeof(param), LAST_PARAM);
1663+
1664+
// pad to multiple of 4
1665+
while (commandSize % 4 != 0) {
1666+
SpiDrv::readChar();
1667+
commandSize++;
1668+
}
1669+
1670+
SpiDrv::spiSlaveDeselect();
1671+
//Wait the reply elaboration
1672+
SpiDrv::waitForSlaveReady();
1673+
SpiDrv::spiSlaveSelect();
1674+
1675+
uint16_t res_len = 0;
1676+
SpiDrv::waitResponseData16(BLE_READ, data, (uint16_t*)&res_len);
1677+
1678+
SpiDrv::spiSlaveDeselect();
1679+
1680+
return res_len;
1681+
}
1682+
1683+
int WiFiDrv::blePeek(uint8_t data[], size_t length) {
1684+
WAIT_FOR_SLAVE_SELECT();
1685+
1686+
SpiDrv::sendCmd(BLE_PEEK, PARAM_NUMS_1);
1687+
1688+
int commandSize = 7; // 4 for the normal command length + 3 for the parameter
1689+
uint16_t param = length; // TODO check length doesn't exceed 2^16
1690+
SpiDrv::sendParam((uint8_t*)&param, sizeof(param), LAST_PARAM);
1691+
1692+
// pad to multiple of 4
1693+
while (commandSize % 4 != 0) {
1694+
SpiDrv::readChar();
1695+
commandSize++;
1696+
}
1697+
1698+
SpiDrv::spiSlaveDeselect();
1699+
//Wait the reply elaboration
1700+
SpiDrv::waitForSlaveReady();
1701+
SpiDrv::spiSlaveSelect();
1702+
1703+
uint16_t res_len = 0;
1704+
SpiDrv::waitResponseData16(BLE_READ, data, (uint16_t*)&res_len);
1705+
1706+
SpiDrv::spiSlaveDeselect();
1707+
1708+
return res_len;
1709+
}
1710+
1711+
size_t WiFiDrv::bleWrite(const uint8_t* data, size_t len) {
1712+
WAIT_FOR_SLAVE_SELECT();
1713+
1714+
int commandSize = 4;
1715+
SpiDrv::sendCmd(BLE_WRITE, PARAM_NUMS_1);
1716+
1717+
SpiDrv::sendBuffer((uint8_t*)data, len, LAST_PARAM);
1718+
commandSize += len+2;
1719+
1720+
// pad to multiple of 4
1721+
while (commandSize % 4 != 0) {
1722+
SpiDrv::readChar();
1723+
commandSize++;
1724+
}
1725+
1726+
SpiDrv::spiSlaveDeselect();
1727+
//Wait the reply elaboration
1728+
SpiDrv::waitForSlaveReady();
1729+
SpiDrv::spiSlaveSelect();
1730+
1731+
uint8_t res_len = 1;
1732+
uint16_t res = 0;
1733+
SpiDrv::waitResponseCmd(PREFERENCES_PUT, PARAM_NUMS_1, (uint8_t*)&res, &res_len);
1734+
SpiDrv::spiSlaveDeselect();
1735+
1736+
return res;
1737+
}
1738+
16031739

16041740
WiFiDrv wiFiDrv;

src/utility/wifi_drv.h

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

331+
static int bleBegin();
332+
static void bleEnd();
333+
static int bleAvailable();
334+
static int bleRead(uint8_t data[], size_t length);
335+
static int blePeek(uint8_t data[], size_t length);
336+
static size_t bleWrite(const uint8_t* data, size_t length);
337+
331338
static void applyOTA();
332339

333340
friend class WiFiUDP;

src/utility/wifi_spi.h

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

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

0 commit comments

Comments
 (0)