Skip to content

Commit 3f575c2

Browse files
API for low-level register manipulation
+ corresponding example
1 parent b8b3888 commit 3f575c2

File tree

6 files changed

+89
-0
lines changed

6 files changed

+89
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ Where:
131131
* ```transmit_fsk_fixed``` - TX in FSK mode. Fixed packet length - 2047 bytes, NRZ encoding, CRC, AFC on.
132132
* ```transmit_ook``` - TX in OOK mode. Variable packet length, NRZ encoding, CRC, AFC on. Sending several messages: small 2 byte, messages that can fit into FIFO fully, max messages for variable packet type - 255 bytes and same messages, but for node address 0x11 and 0x00.
133133
* ```temperature``` - Constantly read raw temperature value from the internal sensor. Must be calibrated first using well-known temperature. Available in FSK mode.
134+
* ```low_level``` - Example for direct register manipulation.
134135

135136
# Tests
136137

examples/low_level/CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
3+
set(EXTRA_COMPONENT_DIRS "../../")
4+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
5+
project(sx127x_lowlevel)
6+
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
idf_component_register(SRCS "main.c" INCLUDE_DIRS "")

examples/low_level/main/main.c

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <driver/spi_common.h>
2+
#include <driver/spi_master.h>
3+
#include <esp_log.h>
4+
#include <sx127x.h>
5+
#include <sx127x_registers.h>
6+
7+
// TTGO lora32 v2.1 1.6.1
8+
#define SCK 5
9+
#define MISO 19
10+
#define MOSI 27
11+
#define SS 18
12+
#define RST 23
13+
#define DIO0 26
14+
// older versions of TTGO require manual wiring of pins below
15+
#define DIO1 33
16+
#define DIO2 32
17+
18+
// Heltec lora32 v2
19+
// #define DIO1 35
20+
// #define DIO2 34
21+
22+
static const char *TAG = "sx127x";
23+
24+
sx127x device;
25+
26+
void app_main() {
27+
ESP_LOGI(TAG, "starting up");
28+
spi_bus_config_t config = {
29+
.mosi_io_num = MOSI,
30+
.miso_io_num = MISO,
31+
.sclk_io_num = SCK,
32+
.quadwp_io_num = -1,
33+
.quadhd_io_num = -1,
34+
.max_transfer_sz = 0,
35+
};
36+
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &config, 1));
37+
spi_device_interface_config_t dev_cfg = {
38+
.clock_speed_hz = 4E6,
39+
.spics_io_num = SS,
40+
.queue_size = 16,
41+
.command_bits = 0,
42+
.address_bits = 8,
43+
.dummy_bits = 0,
44+
.mode = 0};
45+
spi_device_handle_t spi_device;
46+
ESP_ERROR_CHECK(spi_bus_add_device(SPI2_HOST, &dev_cfg, &spi_device));
47+
ESP_ERROR_CHECK(sx127x_create(spi_device, &device));
48+
49+
uint8_t value = 0b01100111;
50+
ESP_ERROR_CHECK(sx127x_write_register(REGINVERTIQ, value, &device.spi_device));
51+
printf("written: %d\n", value);
52+
uint8_t written = 0;
53+
ESP_ERROR_CHECK(sx127x_read_register(REGINVERTIQ, &device.spi_device, &written));
54+
printf("read: %d\n", written);
55+
}

include/sx127x.h

+22
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,28 @@ int sx127x_fsk_ook_set_temp_monitor(bool enable, sx127x *device);
994994
*/
995995
int sx127x_fsk_ook_get_raw_temperature(sx127x *device, int8_t *raw_temperature);
996996

997+
/**
998+
* Low-level API to read single register. No validation is performed
999+
* @param reg - the register id defined in sx127x_registers.h
1000+
* @param spi_device - wrapper around spi_device. Use device->spi_device.
1001+
* @param result - the result
1002+
* @return int
1003+
* - SX127X_ERR_INVALID_ARG on any SPI transfer errors
1004+
* - SX127X_OK on success
1005+
*/
1006+
int sx127x_read_register(int reg, shadow_spi_device_t *spi_device, uint8_t *result);
1007+
1008+
/**
1009+
* Low-level API to write single register. No validation is performed
1010+
* @param reg - the register id defined in sx127x_registers.h
1011+
* @param value - value to write
1012+
* @param spi_device - wrapper around spi_device. Use device->spi_device.
1013+
* @return int
1014+
* - SX127X_ERR_INVALID_ARG on any SPI transfer errors
1015+
* - SX127X_OK on success
1016+
*/
1017+
int sx127x_write_register(int reg, uint8_t value, shadow_spi_device_t *spi_device);
1018+
9971019
#ifdef __cplusplus
9981020
}
9991021
#endif

src/sx127x.c

+4
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ int sx127x_shadow_spi_write_buffer(int reg, const uint8_t *buffer, size_t buffer
167167
return code;
168168
}
169169

170+
int sx127x_write_register(int reg, uint8_t value, shadow_spi_device_t *spi_device) {
171+
return sx127x_shadow_spi_write_register(reg, &value, 1, spi_device);
172+
}
173+
170174
int sx127x_read_register(int reg, shadow_spi_device_t *spi_device, uint8_t *result) {
171175
#ifdef CONFIG_SX127X_DISABLE_SPI_CACHE
172176
uint32_t value;

0 commit comments

Comments
 (0)