从Bluetooth SIG和TI官网上下载了HCI规格书,参考并定义了LoRa/LoRaWAN的HCI规格。同时在STM32F401上使用MicroPython快速构建了对应的HCI Controller,并与现有的LoRaWAN网关驱动、LoRaWANPktFwd UDP服务器、MQTT代理服务器,以及自己的EPIC IoTHub进行了整合。
从市场营销角度,HCI Controller必须使用C/C++编程,这样可以实现最低成本。比如国产 CH430 + MCU(STM8) + SX127X,真的可以很便宜,还是等我以后来再做这个烂大街组合。但是从快速集成角度看,资源过于受限制的产品组合会导致非常长的周期,我还是顺手拿个MicroPython MCU来快速成型。
HCI Opcode
OpCode | Binary | Comment |
---|---|---|
Write_Addr | FC06 | SYS |
Update_Baudrate | FF36 | SYS |
Power_Mode_Config | FD0C | SYS |
Clock_Set_Timeout | FE24 | SYS |
Write_Codec_Config | FD06 | AVC |
RF_Calibration | FDFB | PHY |
Set_Power_Level | FD82 | PHY |
Set_Test_TX | FDCA | PHY |
Set_Test_RX | FDCB | PHY |
Set_Test_TX_RX | FDCC | PHY |
Set_Features | FF26 | DBG |
Get_System_Status | FE1F | DBG |
Read_RSSI_SNR | FDFC | DBG |
Read_HW_Register | FF00 | MAC |
Write_HW_Register | FF01 | MAC |
Read_Mod_Write_Register | FD09 | MAC |
暂时只是这些草稿,参考的是TI的Vendor Specific Command,和标准OGF/OCF有差异,可能有错误和遗漏,(至少缺乏了Data透传的定义)之后再订正和增补。
Linux BlueZ hcitool至少支持以下命令:
hcitool - HCI Tool ver 4.101
Usage:
hcitool [options] <command> [command parameters]
Options:
--help Display help
-i dev HCI device
Commands:
dev Display local devices
inq Inquire remote devices
scan Scan for remote devices
name Get name from remote device
info Get information from remote device
spinq Start periodic inquiry
epinq Exit periodic inquiry
cmd Submit arbitrary HCI commands
con Display active connections
cc Create connection to remote device
dc Disconnect from remote device
sr Switch master/slave role
cpt Change connection packet type
rssi Display connection RSSI
lq Display link quality
tpl Display transmit power level
afh Display AFH channel map
lp Set/display link policy settings
lst Set/display link supervision timeout
auth Request authentication
enc Set connection encryption
key Change connection link key
clkoff Read clock offset
clock Read local or remote clock
lescan Start LE scan
lewladd Add device to LE White List
lewlrm Remove device from LE White List
lewlsz Read size of LE White List
lewlclr Clear LE White list
lecc Create a LE Connection
ledc Disconnect a LE Connection
lecup LE Connection Update
MicroPython Demo HCI代码
import pyb
import gc
import hci
led = pyb.LED(1)
usb = pyb.USB_VCP()
uart1 = pyb.UART(1, 9600)
def setup():
print("MicroPython for LoRaWAN")
print("Simulator of LoRaWAN HCI, STM32F401CE")
def hciCmdParser(data):
print("HCI Command Parser:{}".format(data))
# if...ifelse...ifelse...else
def loop():
global led, usb, uart1
if usb.any():
usbline = usb.readline()
hciCmdParser(usbline)
setup()
while True:
loop()
因为这是一个Demo,主要就是hciCmdParser(data)来实现乒乓式的HCI收发。在实际工程中,需要考虑SPI驱动、LoRaWAN MAC/LLL、外部中断、定时器中断。
其中SPI/SX127X/LoRaWAN是主线程;USB_VCP已经隐含在MicroPython底层了,不需要使用线程;定时器和外部中断在MicroPython中不算是线程。其设计反而比MicroPython/ESP8266要简单。如果用C语言来做,还需要一些时间的。