Skip to content

Commit

Permalink
add support for temperature reading functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dernasherbrezon committed Dec 15, 2023
1 parent ddbd989 commit 124f5d8
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ Where:
* ```transmit_fsk``` - TX in FSK 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.
* ```transmit_fsk_fixed``` - TX in FSK mode. Fixed packet length - 2047 bytes, NRZ encoding, CRC, AFC on.
* ```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.
* ```temperature``` - Constantly read raw temperature value from the internal sensor. Must be calibrated first using well-known temperature. Available in FSK mode.

# Tests

Expand Down
6 changes: 6 additions & 0 deletions examples/temperature/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.5)

set(EXTRA_COMPONENT_DIRS "../../")
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(sx127x_temperature)

1 change: 1 addition & 0 deletions examples/temperature/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
idf_component_register(SRCS "main.c" INCLUDE_DIRS "")
70 changes: 70 additions & 0 deletions examples/temperature/main/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <driver/spi_common.h>
#include <driver/spi_master.h>
#include <esp_log.h>
#include <freertos/task.h>
#include <sx127x.h>
#include <rom/ets_sys.h>

// TTGO lora32 v2.1 1.6.1
#define SCK 5
#define MISO 19
#define MOSI 27
#define SS 18
#define RST 23
#define DIO0 26
// older versions of TTGO require manual wiring of pins below
#define DIO1 33
#define DIO2 32

// Heltec lora32 v2
// #define DIO1 35
// #define DIO2 34

static const char *TAG = "sx127x";

sx127x *device = NULL;

void app_read_temperature() {
ESP_ERROR_CHECK(sx127x_set_opmod(SX127x_MODE_FSRX, SX127x_MODULATION_FSK, device));
//enable temp monitoring
ESP_ERROR_CHECK(sx127x_fsk_ook_set_temp_monitor(true, device));
// a little bit longer for FSRX mode to kick off
ets_delay_us(150);
//disable temp monitoring
ESP_ERROR_CHECK(sx127x_fsk_ook_set_temp_monitor(false, device));
ESP_ERROR_CHECK(sx127x_set_opmod(SX127x_MODE_SLEEP, SX127x_MODULATION_FSK, device));

int8_t raw_temperature;
ESP_ERROR_CHECK(sx127x_fsk_ook_get_raw_temperature(device, &raw_temperature));
ESP_LOGI(TAG, "raw temperature: %d", raw_temperature);
}

void app_main() {
ESP_LOGI(TAG, "starting up");
spi_bus_config_t config = {
.mosi_io_num = MOSI,
.miso_io_num = MISO,
.sclk_io_num = SCK,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 0,
};
ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &config, 1));
spi_device_interface_config_t dev_cfg = {
.clock_speed_hz = 3E6,
.spics_io_num = SS,
.queue_size = 16,
.command_bits = 0,
.address_bits = 8,
.dummy_bits = 0,
.mode = 0};
spi_device_handle_t spi_device;
ESP_ERROR_CHECK(spi_bus_add_device(HSPI_HOST, &dev_cfg, &spi_device));
ESP_ERROR_CHECK(sx127x_create(spi_device, &device));
ESP_ERROR_CHECK(sx127x_set_opmod(SX127x_MODE_SLEEP, SX127x_MODULATION_FSK, device));

while (1) {
app_read_temperature();
vTaskDelay(pdMS_TO_TICKS(5000));
}
}
20 changes: 20 additions & 0 deletions include/sx127x.h
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,26 @@ int sx127x_fsk_ook_rx_set_preamble_detector(bool enable, uint8_t detector_size,
*/
int sx127x_fsk_ook_rx_calibrate(sx127x *device);

/**
* @brief Control temperature monitoring. By default enabled. Should be used to measure the temperature in any mode except Sleep and Standby
* @param enable - Default: enabled
* @param device Pointer to variable to hold the device handle
* @return int
* - SX127X_ERR_INVALID_STATE if current state is not STANDBY
* - SX127X_OK on success
*/
int sx127x_fsk_ook_set_temp_monitor(bool enable, sx127x *device);

/**
* @brief Higher precision requires a calibration procedure at a known temperature.
* @param device Pointer to variable to hold the device handle
* @param raw_temperature raw sensor temperature. Due to process variations, the absolute accuracy of the result is +/- 10 °C.
* @return int
* - SX127X_ERR_INVALID_STATE if current state is not STANDBY
* - SX127X_OK on success
*/
int sx127x_fsk_ook_get_raw_temperature(sx127x *device, int8_t *raw_temperature);

/**
* @brief Disconnect from SPI and release any resources assotiated. After calling this function pointer to device will be unusable.
*
Expand Down
20 changes: 20 additions & 0 deletions src/sx127x.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
#define REG_SYNC_WORD 0x39
#define REG_INVERTIQ2 0x3b
#define REG_IMAGE_CAL 0x3b
#define REG_TEMP 0x3c
#define REG_IRQ_FLAGS_1 0x3e
#define REG_IRQ_FLAGS_2 0x3f
#define REG_DIO_MAPPING_1 0x40
Expand Down Expand Up @@ -1210,6 +1211,25 @@ int sx127x_fsk_ook_rx_calibrate(sx127x *device) {
return SX127X_OK;
}

int sx127x_fsk_ook_get_raw_temperature(sx127x *device, int8_t *raw_temperature) {
CHECK_FSK_OOK_MODULATION(device);
uint8_t value;
ERROR_CHECK(sx127x_read_register(REG_TEMP, device->spi_device, &value));
if ((value & 0x80) == 0x80) {
*raw_temperature = 255 - value;
} else {
*raw_temperature = -1 * value;
}
return SX127X_OK;
}

int sx127x_fsk_ook_set_temp_monitor(bool enable, sx127x *device) {
CHECK_FSK_OOK_MODULATION(device);
// the field is called TempMonitorOff, thus inverted
uint8_t value = (enable ? 0b00000000 : 0b00000001);
return sx127x_append_register(REG_IMAGE_CAL, value, 0b11111110, device->spi_device);
}

void sx127x_destroy(sx127x *device) {
if (device == NULL) {
return;
Expand Down

0 comments on commit 124f5d8

Please sign in to comment.