# LoRa Driver for Semtech SX1262 on Linux (PineDio USB Adapter) and BL602 (PineDio Stack BL604)
![PineDio LoRa USB Adapter](https://lupyuen.github.io/images/usb-pinedio.jpg)
Read the articles...
- ["Build a Linux Driver for PineDio LoRa SX1262 USB Adapter"](https://lupyuen.github.io/articles/usb)
- ["LoRaWAN on PineDio Stack BL604 RISC-V Board"](https://lupyuen.github.io/articles/lorawan2)
- ["PineCone BL602 Talks LoRaWAN"](https://lupyuen.github.io/articles/lorawan)
- ["The Things Network on PineDio Stack BL604 RISC-V Board"](https://lupyuen.github.io/articles/ttn)
__CAUTION: Sending a LoRa Message on PineDio USB (not BL602) above 29 bytes will cause message corruption!__
__CAUTION: Receiving a LoRa Message on PineDio USB (not BL602) above 28 bytes will cause message corruption!__
(CH341 SPI seems to have trouble transferring a block of 32 bytes)
For BL602 / BL604: To add this driver to an existing BL602 / BL604 project:
```bash
cd bl_iot_sdk/components/3rdparty
git submodule add https://github.com/lupyuen/lora-sx1262
```
Ported from Semtech's Reference Implementation of SX1262 Driver...
https://github.com/Lora-net/LoRaMac-node/tree/master/src/radio/sx126x
# Build PineDio USB Driver
To build PineDio USB Driver on Pinebook Pro Manjaro Arm64...
```bash
## Install DKMS
sudo pacman -Syu dkms base-devel --needed
## Install Kernel Headers for Manjaro: https://linuxconfig.org/manjaro-linux-kernel-headers-installation
uname -r
## Should show "5.14.12-1-MANJARO-ARM" or similar
sudo pacman -S linux-headers
pacman -Q | grep headers
## Should show "linux-headers 5.14.12-1" or similar
## Reboot to be safe
sudo reboot now
## Install CH341 SPI Driver
git clone https://github.com/rogerjames99/spi-ch341-usb.git
pushd spi-ch341-usb
## TODO: Edit Makefile and change...
## KERNEL_DIR = /usr/src/linux-headers-$(KVERSION)/
## To...
## KERNEL_DIR = /lib/modules/$(KVERSION)/build
make
sudo make install
popd
## Unload the module ch341 if it has been automatically loaded
lsmod | grep ch341
sudo rmmod ch341
## Load the new module
sudo modprobe spi-ch341-usb
## Plug in PineDio USB and check that the module has been correctly loaded.
## See dmesg Log below. This needs to be checked every time we reboot
## our computer and when we plug in PineDio USB.
dmesg
## If we see "spi_ch341_usb: loading out-of-tree module taints kernel",
## Unplug PineDio USB, run "sudo rmmod ch341", plug in PineDio USB again
## and recheck dmesg.
## Download PineDio USB Driver
git clone --recursive https://github.com/lupyuen/lora-sx1262
cd lora-sx1262
## TODO: Edit src/main.c and uncomment READ_REGISTERS, SEND_MESSAGE or RECEIVE_MESSAGE.
## See "PineDio USB Operations" below
## Build PineDio USB Driver
make
## Run PineDio USB Driver Demo.
## See Output Log below.
sudo ./lora-sx1262
```
More about PineDio USB and CH341 SPI:
https://wiki.pine64.org/wiki/JF%27s_note_on_PineDio_devices#RAW_LoRa_communication_between_USB_LoRa_adapter_and_PineDio_STACK
# PineDio USB Operations
The PineDio USB Demo supports 3 operations...
1. Read SX1262 Registers:
Edit [`src/main.c`](src/main.c) and uncomment...
```c
#define READ_REGISTERS
```
(See the Read Register Log below)
1. Send LoRa Message:
Edit [`src/main.c`](src/main.c) and uncomment...
```c
#define SEND_MESSAGE
```
(See the Send Message Log below)
1. Receive LoRa Message:
Edit [`src/main.c`](src/main.c) and uncomment...
```c
#define RECEIVE_MESSAGE
```
(See the Receive Message Log below)
Note that the CH341 GPIO programming is incomplete. We need to...
1. Init the GPIO Pins: `SX126xIoInit`
[src/sx126x-linux.c#L65-L77](src/sx126x-linux.c#L65-L77)
1. Register GPIO Interrupt Handler for DIO1: `SX126xIoIrqInit`
[src/sx126x-linux.c#L79-L91](src/sx126x-linux.c#L79-L91)
1. Reset SX1262 via GPIO: `SX126xReset`
(For now we reset SX1262 by manually unplugging PineDio USB)
[src/sx126x-linux.c#L149-L169](src/sx126x-linux.c#L149-L169)
1. Check SX1262 Busy State via GPIO: `SX126xWaitOnBusy`
(For now we sleep 10 milliseconds)
[src/sx126x-linux.c#L171-L182](src/sx126x-linux.c#L171-L182)
1. Get DIO1 Pin State: `SX126xGetDio1PinState`
[src/sx126x-linux.c#L337-L344](src/sx126x-linux.c#L337-L344)
We also need Background Threads to receive LoRa Messages in the background...
[src/main.c#L355-L408](src/main.c#L355-L408)
More about PineDio USB and CH341 GPIO:
https://wiki.pine64.org/wiki/JF%27s_note_on_PineDio_devices#RAW_LoRa_communication_between_USB_LoRa_adapter_and_PineDio_STACK
# PineDio USB Output Log
Output Log from PineDio USB Demo...
## Read Registers
Read SX1262 Registers on PineDio USB:
```text
read_registers
SX126xIoInit
SX126xWakeup
sx126x_hal_write: command_length=1, data_length=1
spi tx: c0 00
spi rx: a2 22
TODO: SX126xWaitOnBusy
TODO: SX126xWaitOnBusy
sx126x_hal_read: command_length=4, data_length=1
spi tx: 1d 00 00 00 00
spi rx: a2 a2 a2 a2 00
TODO: SX126xWaitOnBusy
Register 0x00 = 0x00
TODO: SX126xWaitOnBusy
sx126x_hal_read: command_length=4, data_length=1
spi tx: 1d 00 01 00 00
spi rx: a2 a2 a2 a2 00
TODO: SX126xWaitOnBusy
Register 0x01 = 0x00
TODO: SX126xWaitOnBusy
sx126x_hal_read: command_length=4, data_length=1
spi tx: 1d 00 02 00 00
spi rx: a2 a2 a2 a2 00
TODO: SX126xWaitOnBusy
Register 0x02 = 0x00
TODO: SX126xWaitOnBusy
sx126x_hal_read: command_length=4, data_length=1
spi tx: 1d 00 03 00 00
spi rx: a2 a2 a2 a2 00
TODO: SX126xWaitOnBusy
Register 0x03 = 0x00
TODO: SX126xWaitOnBusy
sx126x_hal_read: command_length=4, data_length=1
spi tx: 1d 00 04 00 00
spi rx: a2 a2 a2 a2 00
TODO: SX126xWaitOnBusy
Register 0x04 = 0x00
TODO: SX126xWaitOnBusy
sx126x_hal_read: command_length=4, data_length=1
spi tx: 1d 00 05 00 00
spi rx: a2 a2 a2 a2 00
TODO: SX126xWaitOnBusy
Register 0x05 = 0x00
TODO: SX126xWaitOnBusy
sx126x_hal_read: command_length=4, data_length=1
spi tx: 1d 00 06 00 00
spi rx: a2 a2 a2 a2 00
TODO: SX126xWaitOnBusy
Register 0x06 = 0x00
TODO: SX126xWaitOnBusy
sx126x_hal_read: command_length=4, data_length=1
spi tx: 1d 00 07 00 00
spi rx: a2 a2 a2 a2 00
TODO: SX126xWaitOnBusy
Register 0x07 = 0x00
TODO: SX126xWaitOnBusy
sx126x_hal_read: command_length=4, data_length=1
spi tx: 1d 00 08 00 00
spi rx: a2 a2 a2 a2 80
TODO: SX126xWaitOnBusy
Register 0x08 = 0x80
TODO: SX126xWaitOnBusy
sx126x_hal_read: command_length=4, data_length=1
spi tx: 1d 00 09 00 00
spi rx: a2 a2 a2 a2 00
TODO: SX126xWaitOnBusy
Register 0x09 = 0x00
TODO: SX126xWaitOnBusy
sx126x_hal_read: command_length=4, data_length=1
spi tx: 1d 00 0a 00 00
spi rx: a2 a2 a2 a2 01
TODO: SX126xWaitOnBusy
Register 0x0a = 0x01
TODO: SX126xWaitOnBusy
sx126x_hal_read: command_length=4, data_length=1
spi tx: 1d 00 0b 00 00
spi rx: a2 a2 a2 a2 00
TODO: SX126xWaitOnBusy
Register 0x0b = 0x00
TODO: SX126xWaitOnBusy
sx126x_hal_read: command_length=4, data_length=1
spi tx: 1d 00 0c 00 00
spi rx: a2 a2 a2 a2 00
TODO: SX126xWaitOnBusy
Register 0x0c = 0x00
TODO: SX126xWaitOnBusy
sx126x_hal_read: command_length=4, data_length=1
spi tx: 1d 00 0d 00 00
spi rx: a2 a2 a2 a2 00
TODO: SX126xWaitOnBusy
Register 0x0d = 0x00
TODO: SX126xWaitOnBusy
sx126x_hal_read: command_length=4, data_length=1
spi tx: 1d 00 0e 00 00
spi rx: a2 a2 a2 a2 00
TODO: SX126xWaitOnBusy
Register 0x0e = 0x00
TODO: SX126xWaitOnBusy
sx126x_hal_read: command_length=4, data_length=1
spi tx: 1d 00 0f 00 00
spi rx: a2 a2 a2 a2 00
TODO: SX126xWaitOnBusy
Register 0x0f = 0x00
Done!
```
See below for the dmesg Read Registers Log.
## Send Message
Transmit 29-byte LoRa Message on PineDio USB:
__CAUTION: Sending a LoRa Message on PineDio USB (not BL602) above 29 bytes will cause message corruption!__
```text
gcc -o lora-sx1262 \
npl/linux/src/os_eventq.cc \
\
src/main.o src/radio.o src/sx126x.o src/sx126x-linux.o npl/linux/src/os_callout.o npl/linux/src/os_sem.o npl/linux/src/os_task.o npl/linux/src/os_atomic.o npl/linux/src/os_time.o npl/linux/src/os_mutex.o \
-g -W
没有合适的资源?快使用搜索试试~ 我知道了~
lora-sx1262-master.zip
共38个文件
c:16个
h:10个
md:3个
需积分: 5 4 下载量 40 浏览量
2023-08-08
16:03:23
上传
评论
收藏 80KB ZIP 举报
温馨提示
lora-sx1262-master.zip
资源推荐
资源详情
资源评论
收起资源包目录
lora-sx1262-master.zip (38个子文件)
lora-sx1262-master
bouffalo.mk 202B
include
radio.h 16KB
sx126x-board.h 7KB
sx126x-utilities.h 1KB
sx126x.h 39KB
.vscode
settings.json 60B
component.mk 187B
.github
FUNDING.yml 743B
Makefile 1KB
src
sx126x-board.c 27KB
sx126x.c 23KB
radio.c 48KB
main.c 13KB
sx126x-linux.c 19KB
LICENSE 1KB
npl
linux
include
nimble
nimble_npl.h 4KB
nimble_npl_os.h 1KB
os_types.h 2KB
console
console.h 1KB
src
os_mutex.c 2KB
os_callout.c 4KB
os_time.c 2KB
os_sem.c 2KB
os_task.c 3KB
wqueue.h 2KB
os_eventq.cc 3KB
os_atomic.c 1KB
test
test_npl_mempool.c 3KB
Makefile 3KB
test_npl_sem.c 4KB
test_npl_task.c 2KB
test_npl_callout.c 3KB
test_npl_eventq.c 4KB
test_util.h 2KB
README.md 138B
README.md 124B
.gitignore 38B
README.md 56KB
共 38 条
- 1
资源评论
Matlab仿真实验室
- 粉丝: 2w+
- 资源: 2179
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功