Arduino与LoRa
自从去年Arduino增加了对于STM32以及Semtech LoRa的支持后,基于Arduino的开发得到了长足的发展。由于Arduino不局限于ARM内核,MCU多样性比较高,至少可以使用ESP8266/ESP32/STM32/AVR/PIC/K20等。而Arduino社交化程度比较高。
在同一硬件中,编程平台、协议栈、应用的复杂度呈现几何级数上升。但不管如何,选择受众多,开发易的平台依然很有用。
SPI 驱动
无论是Arduino/mbed/MicroPython/Lua/JavaScript,驱动LoRa的接口最简单:SPI,四线SPI加上RST/D0中断即可。要调通SPI总线,必须依赖一个工具:逻辑分析仪,淘宝D版。
screenshot_Arduino_9bit_SPI.pngFig 1: Arduino firmware, abnormal SPI operation for leading pulse
Arduino的SPI驱动中有个SPI参数配置:
SPISettings _spiSettings(8E6, MSBFIRST, SPI_MODE0);
一开始我把它放在CS拉低之后,发现其初始化会导致一个毛刺,形成9个脉冲,导致总线出错。参见附图1。
screenshot_Arduino_9bit_SPI_CS_out.pngFig2 : Arduino firmware, normal SPI operation for leading pulse outside of CS period
于是我把参数设置放置在CS拉低之前。
uint8_t singleTransfer(uint8_t address, uint8_t value)
{
uint8_t response;
_spiSettings = SPISettings(FREQ, MSBFIRST, SPI_MODE0);
SPI.beginTransaction(_spiSettings);
digitalWrite(SS, LOW);
SPI.transfer(address);
response = SPI.transfer(value);
SPI.endTransaction();
digitalWrite(SS, HIGH);
return response;
}
这段代码中,先初始化配置,然后拉低CS,然后进行传输,最后要关闭SPI。参见附图,即便有个毛刺,也是在CS周期之外。所以总线读写正常。但是返回二进制却总是0xFF或0x00。
screenshot_mbed_8bit_normal.pngFig3: mbed firmware, normal SPI operation
由于mbed/Arduino共享目标板和STLINK,我立刻下载了一个mbed固件,发现mbed的确总线比较干净,返回值也正确:0x12,是SX127X的返回值。参考附图3。
screenshot_Arduino_8bit_normal.pngFig4: Arduino firmware, normal SPI operation
由于SPI总线硬件无需验证,剩下的就是总线速率和RST引脚,所以将总线速度升到8MHz,POR之后RST对SX127X拉低20ms后拉高。现在Arduino的代码也可以返回0x12了。但是CS的周期很长,比较怪异。
ESP8266/STM32
ESP8266的驱动略有不同。接下来,我会将常见的LoRa PHY驱动集成在SPI之上。Arduino好像不支持printf(),需要打碎成为多个print()函数。略有不便。
代码
#include <SPI.h>
#define LED_BP PC13 // BluePill
#define REG_VERSION 0x42
#define RST A0 //PA0 //2
#define SS D10 //PB6 //15
#define FREQ 8E6
SPISettings _spiSettings(8E6, MSBFIRST, SPI_MODE0);
uint8_t singleTransfer(uint8_t address, uint8_t value)
{
uint8_t response;
_spiSettings = SPISettings(FREQ, MSBFIRST, SPI_MODE0);
SPI.beginTransaction(_spiSettings);
digitalWrite(SS, LOW);
SPI.transfer(address);
response = SPI.transfer(value);
SPI.endTransaction();
digitalWrite(SS, HIGH);
return response;
}
uint8_t readRegister(uint8_t address)
{
return singleTransfer(address & 0x7f, 0x00);
}
void setup() {
pinMode(LED_BP, OUTPUT);
pinMode(SS,OUTPUT);
pinMode(RST,OUTPUT);
digitalWrite(RST, LOW);
delay(20);
digitalWrite(RST, HIGH);
SPI.begin();
//_spiSettings = SPISettings(1E6, MSBFIRST, SPI_MODE0);
//SPI.beginTransaction(_spiSettings);
Serial.begin(115200);
Serial.println("SPI Test");
}
void loop() {
uint8_t res;
res = readRegister(0x42);
//Serial.printf("Register[0x42] = 0x%02X\n\r",res);
Serial.print("Register[0x42]=0x");
Serial.println(res, HEX);
if (res!=0x12){
digitalWrite(LED_BP, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(LED_BP, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
}
}
网友评论