Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migração para IDF 5.0, verificação de inicialização, etc #25

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
build/
sdkconfig.old
.vscode/
61 changes: 33 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
# esp32-lora-library
## What is it
**esp32-lora-library** is a C component to be integrated into ESP32-IDF for sending and receiving data through a LoRa transceiver based on Semtech's SX127_ ICs.

The library itself is based on sandeepmistry's **arduino-LoRa** (https://github.com/sandeepmistry/arduino-LoRa) library for Arduino.
## Which is
** esp32-lora-library ** is a C component to be integrated with ESP32-IDF to send and receive data through a LoRa transceiver based on Semtech's SX127_ ICs.
The library is a fork of the **esp32-lora-library ** (https://github.com/Inteform/esp32-lora-library) library of Inteform for ESP32.

## How to install
Simply clone the repository and copy the ```components/lora``` directory into your ESP-IDF project directory or into the ```components/``` path of your $IDF_PATH (it will be public to all your projects).
You can then simply ```#include "lora.h"``` and use its functions.
Using ```make menuconfig``` there will be LoRa Options to configure (like pin numbers)
Simply clone the repository and copy the `` `components/lora``` directory into your ESP-IDF project directory or into the ` `` components/ `` `path of your $IDF_PATH (it will be public to all your projects) .
You can then just `` `#include" lora.h "` `` and use its functions.
Using `` `make menuconfig``` there will be LoRa options to configure (like pin numbers)

```bash
git clone https://github.com/Inteform/esp32-lora-library
cp -r esp32-lora-library/components /path/to/my/esp32/project
`` `bash
git clone https://github.com/JN513/esp32-lora-library
cp -r esp32-lora-library/components/ path/to/my/esp32/project
cd /path/to/my/esp32/project
make menuconfig
make
I do
#etc
```
`` `

## Basic usage
A simple **sender** program...
Expand Down Expand Up @@ -51,32 +50,38 @@ Meanwhile in the **receiver** program...
#include "freertos/task.h"
#include "lora.h"

uint8_t but[32];
uint8_t buf[255];

void task_rx(void *p)
{
int x;
for(;;) {
lora_receive(); // put into receive mode
while(lora_received()) {
x = lora_receive_packet(buf, sizeof(buf));
buf[x] = 0;
printf("Received: %s\n", buf);
lora_receive();
}
vTaskDelay(1);
}
int x;
for(;;) {
lora_receive(); // put into receive mode
while(lora_received()) {
x = lora_receive_packet(buf, sizeof(buf));
buf[x] = 0;
int rssi = lora_packet_rssi();
printf("Received: %s, RSSI: %d\n", buf, rssi);
lora_receive();
}
vTaskDelay(1);
}
}

void app_main()
{
lora_init();
lora_set_frequency(915e6);
lora_enable_crc();
xTaskCreate(&task_rx, "task_rx", 2048, NULL, 5, NULL);
lora_init();
lora_set_frequency(915e6);
lora_enable_crc();

printf("Lora Initialized: %d\n", lora_initialized());

xTaskCreate(&task_rx, "task_rx", 2048, NULL, 5, NULL);
}
```

More examples in examples:

## Connection with the RF module
By default, the pins used to control the RF transceiver are--

Expand Down
3 changes: 3 additions & 0 deletions components/lora/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
idf_component_register(SRCS "lora.c"
INCLUDE_DIRS "include"
REQUIRES driver)
17 changes: 12 additions & 5 deletions components/lora/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,44 @@ menu "LoRa Configuration"

config CS_GPIO
int "CS GPIO"
range 0 35
range 0 46
default 15
help
Pin Number where the NCS pin of the LoRa module is connected to.

config RST_GPIO
int "RST GPIO"
range 0 35
range 0 46
default 32
help
Pin Number where the NRST pin of the LoRa module is connected to.

config MISO_GPIO
int "MISO GPIO"
range 0 35
range 0 46
default 13
help
Pin Number to be used as the MISO SPI signal.

config MOSI_GPIO
int "MOSI GPIO"
range 0 35
range 0 46
default 12
help
Pin Number to be used as the MOSI SPI signal.

config SCK_GPIO
int "SCK GPIO"
range 0 35
range 0 46
default 14
help
Pin Number to be used as the SCK SPI signal.

config DIO0_GPIO
int "DIO0 GPIO"
range 0 46
default 16
help
Pin Number to be used as the DIO0 signal.

endmenu
79 changes: 78 additions & 1 deletion components/lora/include/lora.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,87 @@
#ifndef __LORA_H__
#define __LORA_H__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/spi_master.h"
#include "driver/gpio.h"
#include "sdkconfig.h"

#ifdef CONFIG_IDF_TARGET_ESP32
#define LORA_HOST HSPI_HOST
#elif defined CONFIG_IDF_TARGET_ESP32S2
#define LORA_HOST SPI2_HOST
#elif defined CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C2
#define LORA_HOST SPI2_HOST
#elif CONFIG_IDF_TARGET_ESP32S3
#define LORA_HOST SPI2_HOST
#endif
/*
* Register definitions
*/
#define REG_FIFO 0x00
#define REG_OP_MODE 0x01
#define REG_FRF_MSB 0x06
#define REG_FRF_MID 0x07
#define REG_FRF_LSB 0x08
#define REG_PA_CONFIG 0x09
#define REG_LNA 0x0c
#define REG_FIFO_ADDR_PTR 0x0d
#define REG_FIFO_TX_BASE_ADDR 0x0e
#define REG_FIFO_RX_BASE_ADDR 0x0f
#define REG_FIFO_RX_CURRENT_ADDR 0x10
#define REG_IRQ_FLAGS 0x12
#define REG_RX_NB_BYTES 0x13
#define REG_PKT_SNR_VALUE 0x19
#define REG_PKT_RSSI_VALUE 0x1a
#define REG_MODEM_CONFIG_1 0x1d
#define REG_MODEM_CONFIG_2 0x1e
#define REG_PREAMBLE_MSB 0x20
#define REG_PREAMBLE_LSB 0x21
#define REG_PAYLOAD_LENGTH 0x22
#define REG_MODEM_CONFIG_3 0x26
#define REG_RSSI_WIDEBAND 0x2c
#define REG_DETECTION_OPTIMIZE 0x31
#define REG_DETECTION_THRESHOLD 0x37
#define REG_SYNC_WORD 0x39
#define REG_DIO_MAPPING_1 0x40
#define REG_VERSION 0x42

/*
* Transceiver modes
*/
#define MODE_LONG_RANGE_MODE 0x80
#define MODE_SLEEP 0x00
#define MODE_STDBY 0x01
#define MODE_TX 0x03
#define MODE_RX_CONTINUOUS 0x05
#define MODE_RX_SINGLE 0x06

/*
* PA configuration
*/
#define PA_BOOST 0x80

/*
* IRQ masks
*/
#define IRQ_TX_DONE_MASK 0x08
#define IRQ_PAYLOAD_CRC_ERROR_MASK 0x20
#define IRQ_RX_DONE_MASK 0x40

#define PA_OUTPUT_RFO_PIN 0
#define PA_OUTPUT_PA_BOOST_PIN 1

#define TIMEOUT_RESET 100

void lora_reset(void);
void lora_explicit_header_mode(void);
void lora_implicit_header_mode(int size);
void lora_idle(void);
void lora_sleep(void);
void lora_sleep(void);
void lora_receive(void);
void lora_set_tx_power(int level);
void lora_set_frequency(long frequency);
Expand All @@ -26,5 +102,6 @@ float lora_packet_snr(void);
void lora_close(void);
int lora_initialized(void);
void lora_dump_registers(void);
int lora_initialized(void);

#endif
Loading