From 26a228e53ef77d9e171bd04836f5aefad14921ce Mon Sep 17 00:00:00 2001 From: niekky Date: Fri, 26 Sep 2025 09:10:13 -0700 Subject: [PATCH 01/14] fix: avoid unint warning --- lora/lora.c | 12 +++--------- lora/lora.h | 2 +- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/lora/lora.c b/lora/lora.c index 82573b2..84c4156 100644 --- a/lora/lora.c +++ b/lora/lora.c @@ -31,12 +31,6 @@ // Debugging purposes // #include "led.h" -/*------------------------------------------------------------------------------ - Frequency calculation helper function -------------------------------------------------------------------------------*/ -uint32_t lora_helper_mhz_to_reg_val( uint32_t mhz_freq ) { - return ( (2^19) * mhz_freq * 10^6 )/( 32 * 10^6 ); -} /*------------------------------------------------------------------------------ Helper functions for various pin functions on the LoRa modem. @@ -236,7 +230,7 @@ LORA_STATUS lora_transmit(uint8_t* buffer_ptr, uint8_t buffer_len){ LORA_STATUS fifo_status = lora_write_register(LORA_REG_SIGNAL_TO_NOISE, buffer_len); // Send byte to byte to the fifo buffer - LORA_STATUS sendbyte_status; + LORA_STATUS sendbyte_status = LORA_OK; for (int i = 0; i /* Operation Mode Register Values */ /* These are just random parts of the operation register that may or not get used To avoid any chance they're in the final binary without being used, From 2fcceb6ff45ba59d886fdcd31de096ed5881355c Mon Sep 17 00:00:00 2001 From: niekky Date: Fri, 26 Sep 2025 09:10:36 -0700 Subject: [PATCH 02/14] fix: added hspi macro --- lora/lora.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lora/lora.c b/lora/lora.c index 84c4156..6b51ac4 100644 --- a/lora/lora.c +++ b/lora/lora.c @@ -28,6 +28,10 @@ #include "lora.h" #include "main.h" +extern SPI_HandleTypeDef hspi1; +#define LORA_SPI hspi1 + + // Debugging purposes // #include "led.h" From c157e9623223a6fc32021e292ddde38acd557846 Mon Sep 17 00:00:00 2001 From: Dexx Stexx Date: Fri, 3 Oct 2025 01:17:42 -0700 Subject: [PATCH 03/14] Add ability to set PA_SELECT to Lora_Config --- lora/lora.c | 12 ++++++++++-- lora/lora.h | 8 ++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lora/lora.c b/lora/lora.c index 6b51ac4..a66d4b7 100644 --- a/lora/lora.c +++ b/lora/lora.c @@ -185,14 +185,22 @@ LORA_STATUS lora_init( LORA_CONFIG *lora_config_ptr ) { uint8_t lora_freq_reg2 = ( frf_reg << 16 ) >> 24; uint8_t lora_freq_reg3 = ( frf_reg << 24 ) >> 24; - // Write the frequncy registers + // Write the frequency registers LORA_STATUS write_status4 = lora_write_register( LORA_REG_FREQ_MSB, lora_freq_reg1 ); LORA_STATUS write_status5 = lora_write_register( LORA_REG_FREQ_MSD, lora_freq_reg2 ); LORA_STATUS write_status6 = lora_write_register( LORA_REG_FREQ_LSB, lora_freq_reg3 ); + // Determine register values for the PA Config Register + uint8_t pa_select_reg; + LORA_STATUS read_status4 = lora_read_register( LORA_REG_PA_CONFIG, &pa_select_reg ); + uint8_t new_pa_select_reg = pa_select_reg | lora_config_ptr->lora_pa_select; + + // Write the PA Config Register + LORA_STATUS write_status7 = lora_write_register( LORA_REG_PA_CONFIG, new_pa_select_reg ); + LORA_STATUS standby_status = lora_set_chip_mode( LORA_STANDBY_MODE ); // Switch it into standby mode, which is what's convenient. - if( set_sleep_status + read_status1 + read_status2 + read_status3 + write_status1 + write_status2 + write_status3 + write_status4 + write_status5 + write_status6 + standby_status == 0 ) { + if( set_sleep_status + read_status1 + read_status2 + read_status3 + read_status4 + write_status1 + write_status2 + write_status3 + write_status4 + write_status5 + write_status6 + write_status7 + standby_status == 0 ) { return LORA_OK; } else { return LORA_FAIL; diff --git a/lora/lora.h b/lora/lora.h index 105b775..8dc16c9 100644 --- a/lora/lora.h +++ b/lora/lora.h @@ -136,6 +136,13 @@ typedef enum LORA_HEADER_MODE {// You can see this on 106 - what this actually m LORA_EXPLICIT_HEADER = 0b0 } LORA_HEADER_MODE; + +// PaConfig options - See datasheet pages 79 and 103 +typedef enum LORA_PA_SELECT { + LORA_RFO = 0x00, + LORA_PA_BOOST = 0x01 +} LORA_PA_SELECT; + /* LORA CONFIG SETTINGS */ typedef struct _LORA_CONFIG { LORA_CHIPMODE lora_mode; // Current LORA Chipmode @@ -143,6 +150,7 @@ typedef struct _LORA_CONFIG { LORA_BANDWIDTH lora_bandwidth; // Signal bandwith LORA_ERROR_CODING lora_ecr; // Data Error coding LORA_HEADER_MODE lora_header_mode; // LORA Header mode + LORA_PA_SELECT lora_pa_select; // Amplifier Selection uint32_t lora_frequency; // The LORA carrier frequency. This is NOT directly in megahertz. (See datasheet page 103) // To convert, use the formula (2^19 * x)/(32 * 10^6) // This library provides a helper function From ed44d4eac9c36e02ed7eb4bea9ea852f6cc636a6 Mon Sep 17 00:00:00 2001 From: Dexx Stexx Date: Fri, 17 Oct 2025 19:09:09 -0700 Subject: [PATCH 04/14] Fix (probably) FIFO value writing --- lora/lora.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lora/lora.c b/lora/lora.c index a66d4b7..1a1bda3 100644 --- a/lora/lora.c +++ b/lora/lora.c @@ -225,14 +225,14 @@ LORA_STATUS lora_transmit(uint8_t* buffer_ptr, uint8_t buffer_len){ // Write data to LoRA FIFO uint8_t fifo_ptr_addr; - LORA_STATUS ptr_status = lora_read_register(LORA_REG_FIFO_SPI_POINTER, &fifo_ptr_addr); // Access LoRA FIFO data buffer pointer - if (ptr_status + standby_status != LORA_OK){ + LORA_STATUS tx_base_status = lora_read_register(LORA_REG_FIFO_TX_BASE_ADDR, &fifo_ptr_addr); // Access LoRA FIFO data buffer pointer + if (tx_base_status + standby_status != LORA_OK){ // Error handler // led_set_color(// led_RED); return LORA_FAIL; } - LORA_STATUS tx_base_status = lora_write_register(LORA_REG_FIFO_TX_BASE_ADDR, fifo_ptr_addr); // Set fifo data pointer to TX base address - if (tx_base_status != LORA_OK){ + LORA_STATUS ptr_status = lora_write_register(LORA_REG_FIFO_SPI_POINTER, fifo_ptr_addr); // Set fifo data pointer to TX base address + if (ptr_status != LORA_OK){ // Error handler // led_set_color(// led_RED); return LORA_FAIL; @@ -334,14 +334,14 @@ LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t* buffer_len_ptr){ // Set lora fifo pointer to the RX base current address uint8_t fifo_ptr_addr; - LORA_STATUS ptr2_status = lora_read_register(LORA_REG_FIFO_SPI_POINTER, &fifo_ptr_addr); // Access LoRA FIFO data buffer pointer - if (ptr2_status != LORA_OK){ + LORA_STATUS base_adr_status = lora_read_register(LORA_REG_FIFO_RX_BASE_CUR_ADDR, &fifo_ptr_addr); // Access LoRA FIFO data buffer pointer + if (base_adr_status != LORA_OK){ // Error handler // led_set_color(// led_RED); return LORA_FAIL; } - LORA_STATUS base_adr_status = lora_write_register(LORA_REG_FIFO_RX_BASE_CUR_ADDR, fifo_ptr_addr); // Set fifo data pointer to TX base address - if (base_adr_status != LORA_OK){ + LORA_STATUS ptr2_status = lora_write_register(LORA_REG_FIFO_SPI_POINTER, fifo_ptr_addr); // Set fifo data pointer to TX base address + if (ptr2_status != LORA_OK){ // Error handler // led_set_color(// led_RED); return LORA_FAIL; From 6da02f90f1e2292e3d662f7791bffa5f0e88b6f4 Mon Sep 17 00:00:00 2001 From: Dexx Stexx Date: Mon, 20 Oct 2025 09:18:25 -0700 Subject: [PATCH 05/14] Get basic continuous receive working --- lora/lora.c | 79 +++++++++++++++++++++++++++++++++++++++++++++-------- lora/lora.h | 3 ++ 2 files changed, 71 insertions(+), 11 deletions(-) diff --git a/lora/lora.c b/lora/lora.c index 1a1bda3..cbd6d00 100644 --- a/lora/lora.c +++ b/lora/lora.c @@ -13,6 +13,16 @@ Standard Includes ------------------------------------------------------------------------------*/ +#include +#include +#include "usb_device.h" +#include "usbd_cdc_if.h" + + +void serial_printlnx(unsigned char* mesg, size_t mesg_len){ + while(CDC_Transmit_FS(mesg, mesg_len)==USBD_BUSY); +} + /*------------------------------------------------------------------------------ MCU Pins ------------------------------------------------------------------------------*/ @@ -198,7 +208,7 @@ LORA_STATUS lora_init( LORA_CONFIG *lora_config_ptr ) { // Write the PA Config Register LORA_STATUS write_status7 = lora_write_register( LORA_REG_PA_CONFIG, new_pa_select_reg ); - LORA_STATUS standby_status = lora_set_chip_mode( LORA_STANDBY_MODE ); // Switch it into standby mode, which is what's convenient. + LORA_STATUS standby_status = lora_set_chip_mode( lora_config_ptr->lora_mode ); // Switch it into standby mode, which is what's convenient. if( set_sleep_status + read_status1 + read_status2 + read_status3 + read_status4 + write_status1 + write_status2 + write_status3 + write_status4 + write_status5 + write_status6 + write_status7 + standby_status == 0 ) { return LORA_OK; @@ -275,6 +285,38 @@ LORA_STATUS lora_transmit(uint8_t* buffer_ptr, uint8_t buffer_len){ return LORA_FAIL; } } +// ============================================================================= +// lora_receive_ready: Check if transmission has been received +// ============================================================================= +LORA_STATUS lora_receive_ready() { + uint8_t mode; + + lora_read_register( LORA_REG_OPERATION_MODE, &mode ); + + mode = mode & 0x07; + + char bufy[255]; + int len = sprintf( bufy, "Mode: %d\n", mode); + serial_printlnx( bufy, len ); + + if( mode == LORA_RX_CONTINUOUS_MODE ) { + uint8_t irq_flag; + lora_read_register(LORA_REG_IRQ_FLAGS, &irq_flag); + uint8_t rx_done = (irq_flag & (1<<6)) >> 6; + + char bufx[255]; + int len = sprintf( bufx, "Register: %d\n", irq_flag); + serial_printlnx( bufx, len ); + + if( rx_done ) { + return LORA_OK; + } else { + return LORA_FAIL; + } + } else { + return LORA_FAIL; + } +} // ============================================================================= // lora_receive: receive a buffer from lora fifo with single mode @@ -284,14 +326,16 @@ LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t* buffer_len_ptr){ uint8_t rx_done; // Mode request STAND-BY - LORA_STATUS standby_status = lora_set_chip_mode(LORA_STANDBY_MODE); + // LORA_STATUS standby_status = lora_set_chip_mode(LORA_STANDBY_MODE); // RX Init TODO + uint8_t reset_irq = 0; + lora_write_register( LORA_REG_IRQ_FLAGS, reset_irq ); // Set lora fifo pointer to the RX base address - uint8_t fifo_ptr_addr; + /* uint8_t fifo_ptr_addr; LORA_STATUS ptr_status = lora_read_register(LORA_REG_FIFO_SPI_POINTER, &fifo_ptr_addr); // Access LoRA FIFO data buffer pointer - if (standby_status + ptr_status != LORA_OK){ + if (ptr_status != LORA_OK){ // Error handler // led_set_color(// led_RED); return LORA_FAIL; @@ -301,27 +345,36 @@ LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t* buffer_len_ptr){ // Error handler // led_set_color(// led_RED); return LORA_FAIL; - } + } */ // Send request for RX Single mode - LORA_STATUS rmode_status = lora_set_chip_mode(LORA_RX_SINGLE_MODE); + // LORA_STATUS rmode_status = lora_set_chip_mode(LORA_RX_SINGLE_MODE); // Wait for LoRA IRQ uint16_t timeout = 0; uint8_t irq_flag; LORA_STATUS irq_status; - while(timeout<10000){ + + + // while(timeout<10000){ irq_status = lora_read_register(LORA_REG_IRQ_FLAGS, &irq_flag); - timeout_flag = (irq_flag & (1<<7)) >> 7; + // timeout_flag = (irq_flag & (1<<7)) >> 7; rx_done = (irq_flag & (1<<6)) >> 6; + + char buf[6]; + int len = sprintf( buf, "Register Value: %d\r\n", irq_flag ); + serial_printlnx( buf, len ); - if (timeout_flag){ + /* if (timeout_flag){ return LORA_TIMEOUT_FAIL; } else if (rx_done) { + char buf2[255]; + len = sprintf( buf2, "Tuh-dah!\r\n" ); + serial_printlnx( buf, len ); break; - } + } */ timeout++; - } + // } if (rx_done){ LORA_STATUS irq_status2 = lora_read_register(LORA_REG_IRQ_FLAGS, &irq_flag); @@ -332,6 +385,10 @@ LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t* buffer_len_ptr){ uint8_t num_bytes; LORA_STATUS fifo2_status = lora_read_register(LORA_REG_FIFO_RX_NUM_BYTES, &num_bytes); + char buf3[255]; + int len = sprintf( buf3, "Numbytes: %d\r\n", num_bytes ); + serial_printlnx( buf3, len ); + // Set lora fifo pointer to the RX base current address uint8_t fifo_ptr_addr; LORA_STATUS base_adr_status = lora_read_register(LORA_REG_FIFO_RX_BASE_CUR_ADDR, &fifo_ptr_addr); // Access LoRA FIFO data buffer pointer diff --git a/lora/lora.h b/lora/lora.h index 8dc16c9..43f7232 100644 --- a/lora/lora.h +++ b/lora/lora.h @@ -175,6 +175,9 @@ LORA_STATUS lora_init(); void lora_reset(); LORA_STATUS lora_transmit(uint8_t* buffer_ptr, uint8_t buffer_len); + +LORA_STATUS lora_receive_ready(); + LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t* buffer_len_ptr); // Convert a human-readable frequency to the unit used internally by the modem From 61d1dbfaefd3aaf103a23290faad8db38bfdeef1 Mon Sep 17 00:00:00 2001 From: Dexx Stexx Date: Mon, 20 Oct 2025 09:48:39 -0700 Subject: [PATCH 06/14] Clean up the code a bit; add new enums --- lora/lora.c | 129 +++++++++++++++++++--------------------------------- lora/lora.h | 4 +- 2 files changed, 49 insertions(+), 84 deletions(-) diff --git a/lora/lora.c b/lora/lora.c index cbd6d00..420cab9 100644 --- a/lora/lora.c +++ b/lora/lora.c @@ -15,13 +15,6 @@ #include #include -#include "usb_device.h" -#include "usbd_cdc_if.h" - - -void serial_printlnx(unsigned char* mesg, size_t mesg_len){ - while(CDC_Transmit_FS(mesg, mesg_len)==USBD_BUSY); -} /*------------------------------------------------------------------------------ MCU Pins @@ -38,13 +31,22 @@ void serial_printlnx(unsigned char* mesg, size_t mesg_len){ #include "lora.h" #include "main.h" +#if defined( STM32F103xB ) // For the LoRa testbeds +#include "usb_device.h" +#include "usbd_cdc_if.h" + extern SPI_HandleTypeDef hspi1; #define LORA_SPI hspi1 +void serial_printlnx(unsigned char* mesg, size_t mesg_len){ + while(CDC_Transmit_FS(mesg, mesg_len)==USBD_BUSY); +} +#endif -// Debugging purposes -// #include "led.h" +#ifndef STM32F103xB // The LoRa testbeds don't support our LED library + #include "led.h" +#endif /*------------------------------------------------------------------------------ Helper functions for various pin functions on the LoRa modem. @@ -141,11 +143,6 @@ LORA_STATUS lora_set_chip_mode( LORA_CHIPMODE chip_mode ) { return LORA_FAIL; } - // // Fail if not in LORA Mode - // if ( !( operation_mode_register & (1<<7) ) ){ - // return LORA_FAIL; - // } - // Change the value of the chip register to set it to the suggested chip mode uint8_t new_opmode_register = (operation_mode_register & ~(0x7)); new_opmode_register = (new_opmode_register | chip_mode); @@ -291,25 +288,32 @@ LORA_STATUS lora_transmit(uint8_t* buffer_ptr, uint8_t buffer_len){ LORA_STATUS lora_receive_ready() { uint8_t mode; - lora_read_register( LORA_REG_OPERATION_MODE, &mode ); - + LORA_STATUS mode_check = lora_read_register( LORA_REG_OPERATION_MODE, &mode ); mode = mode & 0x07; - char bufy[255]; - int len = sprintf( bufy, "Mode: %d\n", mode); - serial_printlnx( bufy, len ); + #if defined( STM32F103xB ) // LoRa testbed test code + char buf[255]; + int len = sprintf( buf, "Mode: %d\n", mode); + serial_printlnx( buf, len ); + #endif - if( mode == LORA_RX_CONTINUOUS_MODE ) { + if( mode_check == LORA_OK && mode == LORA_RX_CONTINUOUS_MODE ) { uint8_t irq_flag; - lora_read_register(LORA_REG_IRQ_FLAGS, &irq_flag); - uint8_t rx_done = (irq_flag & (1<<6)) >> 6; - - char bufx[255]; - int len = sprintf( bufx, "Register: %d\n", irq_flag); - serial_printlnx( bufx, len ); - if( rx_done ) { - return LORA_OK; + LORA_STATUS irq_check = lora_read_register(LORA_REG_IRQ_FLAGS, &irq_flag); + + if( irq_check = LORA_OK ) { + uint8_t rx_done = (irq_flag & (1<<6)) >> 6; + + char bufx[255]; + int len = sprintf( bufx, "Register: %d\n", irq_flag); + serial_printlnx( bufx, len ); + + if( rx_done ) { + return LORA_READY; + } else { + return LORA_WAITING; + } } else { return LORA_FAIL; } @@ -319,64 +323,21 @@ LORA_STATUS lora_receive_ready() { } // ============================================================================= -// lora_receive: receive a buffer from lora fifo with single mode +// lora_receive: receive a buffer from lora fifo with continuous mode // ============================================================================= LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t* buffer_len_ptr){ uint8_t timeout_flag; - uint8_t rx_done; - - // Mode request STAND-BY - // LORA_STATUS standby_status = lora_set_chip_mode(LORA_STANDBY_MODE); - - // RX Init TODO - uint8_t reset_irq = 0; - lora_write_register( LORA_REG_IRQ_FLAGS, reset_irq ); - - // Set lora fifo pointer to the RX base address - /* uint8_t fifo_ptr_addr; - LORA_STATUS ptr_status = lora_read_register(LORA_REG_FIFO_SPI_POINTER, &fifo_ptr_addr); // Access LoRA FIFO data buffer pointer - if (ptr_status != LORA_OK){ - // Error handler - // led_set_color(// led_RED); - return LORA_FAIL; - } - LORA_STATUS fifo_status = lora_write_register(LORA_REG_FIFO_RX_BASE_ADDR, fifo_ptr_addr); // Set fifo data pointer to TX base address - if (fifo_status != LORA_OK){ - // Error handler - // led_set_color(// led_RED); - return LORA_FAIL; - } */ - // Send request for RX Single mode - // LORA_STATUS rmode_status = lora_set_chip_mode(LORA_RX_SINGLE_MODE); + LORA_STATUS rx_done = lora_receive_ready(); // We check if we've received a packet. + // If we haven't received a packet, the output will be similar to lora_receive_ready in that case. - // Wait for LoRA IRQ - uint16_t timeout = 0; - uint8_t irq_flag; - LORA_STATUS irq_status; - - - // while(timeout<10000){ - irq_status = lora_read_register(LORA_REG_IRQ_FLAGS, &irq_flag); - // timeout_flag = (irq_flag & (1<<7)) >> 7; - rx_done = (irq_flag & (1<<6)) >> 6; + if ( rx_done == LORA_READY ){ + // Reset IRQ register. TODO: Not sure if I actually need to this; will investigate. + uint8_t reset_irq = 0; + lora_write_register( LORA_REG_IRQ_FLAGS, reset_irq ); - char buf[6]; - int len = sprintf( buf, "Register Value: %d\r\n", irq_flag ); - serial_printlnx( buf, len ); - - /* if (timeout_flag){ - return LORA_TIMEOUT_FAIL; - } else if (rx_done) { - char buf2[255]; - len = sprintf( buf2, "Tuh-dah!\r\n" ); - serial_printlnx( buf, len ); - break; - } */ - timeout++; - // } + uint8_t irq_flag; - if (rx_done){ LORA_STATUS irq_status2 = lora_read_register(LORA_REG_IRQ_FLAGS, &irq_flag); uint8_t crc_err = (irq_flag & (1<<5)) >> 5; @@ -385,9 +346,11 @@ LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t* buffer_len_ptr){ uint8_t num_bytes; LORA_STATUS fifo2_status = lora_read_register(LORA_REG_FIFO_RX_NUM_BYTES, &num_bytes); - char buf3[255]; - int len = sprintf( buf3, "Numbytes: %d\r\n", num_bytes ); - serial_printlnx( buf3, len ); + #if defined( STM32F103xB ) + char buf[255]; + int len = sprintf( buf, "Numbytes: %d\r\n", num_bytes ); + serial_printlnx( buf, len ); + #endif // Set lora fifo pointer to the RX base current address uint8_t fifo_ptr_addr; @@ -418,6 +381,8 @@ LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t* buffer_len_ptr){ } } return LORA_OK; + } else if( rx_done == LORA_WAITING ) { + return LORA_WAITING; } return LORA_FAIL; } \ No newline at end of file diff --git a/lora/lora.h b/lora/lora.h index 43f7232..6b3950b 100644 --- a/lora/lora.h +++ b/lora/lora.h @@ -47,6 +47,8 @@ typedef enum LORA_STATUS { LORA_TRANSMIT_FAIL, LORA_RECEIVE_FAIL, LORA_TIMEOUT_FAIL, + LORA_READY, + LORA_WAITING } LORA_STATUS; /* Radio register addresses from datasheet (https://www.mouser.com/datasheet/2/975/1463993415RFM95_96_97_98W-1858106.pdf) @@ -183,6 +185,4 @@ LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t* buffer_len_ptr); // Convert a human-readable frequency to the unit used internally by the modem uint32_t lora_helper_mhz_to_reg_val( uint32_t mhz_freq ); -// LORA_STATUS lora_transmit( uint8_t data ); - #endif \ No newline at end of file From 66cfef637c5936ddfc3e2515dfa66fae568c21e7 Mon Sep 17 00:00:00 2001 From: Dexx Stexx Date: Mon, 20 Oct 2025 10:10:25 -0700 Subject: [PATCH 07/14] Clean up header files --- lora/lora.c | 6 +++--- lora/lora.h | 23 +---------------------- 2 files changed, 4 insertions(+), 25 deletions(-) diff --git a/lora/lora.c b/lora/lora.c index 420cab9..3f4d7a5 100644 --- a/lora/lora.c +++ b/lora/lora.c @@ -51,7 +51,7 @@ void serial_printlnx(unsigned char* mesg, size_t mesg_len){ /*------------------------------------------------------------------------------ Helper functions for various pin functions on the LoRa modem. ------------------------------------------------------------------------------*/ -LORA_STATUS LORA_SPI_Receive( uint8_t* read_buffer_ptr ) { +static LORA_STATUS LORA_SPI_Receive( uint8_t* read_buffer_ptr ) { HAL_StatusTypeDef status; /* Takes pointer to the read buffer. and puts output there */ @@ -63,7 +63,7 @@ LORA_STATUS LORA_SPI_Receive( uint8_t* read_buffer_ptr ) { return LORA_FAIL; } -LORA_STATUS LORA_SPI_Transmit_Byte( LORA_REGISTER_ADDR reg ) { +static LORA_STATUS LORA_SPI_Transmit_Byte( LORA_REGISTER_ADDR reg ) { HAL_StatusTypeDef status; /* Takes register and data to write (1 byte) and writes that register. */ @@ -75,7 +75,7 @@ LORA_STATUS LORA_SPI_Transmit_Byte( LORA_REGISTER_ADDR reg ) { } else return LORA_FAIL; } -LORA_STATUS LORA_SPI_Transmit_Data( LORA_REGISTER_ADDR reg, uint8_t data ) { +static LORA_STATUS LORA_SPI_Transmit_Data( LORA_REGISTER_ADDR reg, uint8_t data ) { HAL_StatusTypeDef status; /* Takes register and data to write and writes that register. */ diff --git a/lora/lora.h b/lora/lora.h index 6b3950b..ff5b895 100644 --- a/lora/lora.h +++ b/lora/lora.h @@ -12,20 +12,8 @@ #ifndef LORA_H #define LORA_H -/* Project includes */ +/* Standard includes */ #include -/* Operation Mode Register Values */ -/* These are just random parts of the operation register that may or not get used -To avoid any chance they're in the final binary without being used, -they are commented out for now and will be uncommented as they're needed. - -#define LORA_LORA_MODE 0b1 -#define LORA_LORA_REGISTER_PAGE 0b0 -#define LORA_FSK_REGISTER_PAGE 0b1 -#define LORA_HIGH_FREQ_MODE 0b1 -#define LORA_LOW_FREQ_MODE 0b0 -#define LORA_OPERATION_RESERVED 0b00 -*/ #define LORA_TIMEOUT 2000 @@ -158,12 +146,6 @@ typedef struct _LORA_CONFIG { // This library provides a helper function } LORA_CONFIG; -LORA_STATUS LORA_SPI_Receive( uint8_t* read_buffer_ptr ); - -LORA_STATUS LORA_SPI_Transmit_Buffer( LORA_REGISTER_ADDR reg, uint8_t data ); - -LORA_STATUS LORA_SPI_Transmit_Byte( LORA_REGISTER_ADDR reg ); - LORA_STATUS lora_read_register( LORA_REGISTER_ADDR lora_register, uint8_t* regData); LORA_STATUS lora_write_register( LORA_REGISTER_ADDR lora_register, uint8_t data ); @@ -182,7 +164,4 @@ LORA_STATUS lora_receive_ready(); LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t* buffer_len_ptr); -// Convert a human-readable frequency to the unit used internally by the modem -uint32_t lora_helper_mhz_to_reg_val( uint32_t mhz_freq ); - #endif \ No newline at end of file From cdfe52837b6cf41b2e663a36b8d7c9e22c2ebf2e Mon Sep 17 00:00:00 2001 From: Dexx Stexx Date: Tue, 28 Oct 2025 09:04:09 -0700 Subject: [PATCH 08/14] Switch LoRa frequency unit from megahertz to kilohertz --- lora/lora.c | 2 +- lora/lora.h | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lora/lora.c b/lora/lora.c index 3f4d7a5..6d45f38 100644 --- a/lora/lora.c +++ b/lora/lora.c @@ -186,7 +186,7 @@ LORA_STATUS lora_init( LORA_CONFIG *lora_config_ptr ) { LORA_STATUS write_status3 = lora_write_register( LORA_REG_NUM_RX_BYTES, new_config1_register ); // Determine register values for the frequency registers - uint32_t frf_reg = lora_config_ptr->lora_frequency * 524288 / 32; + uint32_t frf_reg = lora_config_ptr->lora_frequency * 524288 / ( 32 * 1000 ); uint8_t lora_freq_reg1 = ( frf_reg << 8 ) >> 24; uint8_t lora_freq_reg2 = ( frf_reg << 16 ) >> 24; diff --git a/lora/lora.h b/lora/lora.h index ff5b895..59f812d 100644 --- a/lora/lora.h +++ b/lora/lora.h @@ -141,9 +141,8 @@ typedef struct _LORA_CONFIG { LORA_ERROR_CODING lora_ecr; // Data Error coding LORA_HEADER_MODE lora_header_mode; // LORA Header mode LORA_PA_SELECT lora_pa_select; // Amplifier Selection - uint32_t lora_frequency; // The LORA carrier frequency. This is NOT directly in megahertz. (See datasheet page 103) - // To convert, use the formula (2^19 * x)/(32 * 10^6) - // This library provides a helper function + uint32_t lora_frequency; // The LORA carrier frequency, in kilohertz. + // To convert to internal chip unit, use the formula (2^19 * x)/(32 * 10^3) } LORA_CONFIG; LORA_STATUS lora_read_register( LORA_REGISTER_ADDR lora_register, uint8_t* regData); From d3cef338c51f8ac0d2037f81bdd5f195ffbbe467 Mon Sep 17 00:00:00 2001 From: Dexx Stexx Date: Tue, 28 Oct 2025 09:08:59 -0700 Subject: [PATCH 09/14] Update conditional compilation code to be receiver-specific --- lora/lora.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lora/lora.c b/lora/lora.c index 6d45f38..0b8700e 100644 --- a/lora/lora.c +++ b/lora/lora.c @@ -31,18 +31,20 @@ #include "lora.h" #include "main.h" -#if defined( STM32F103xB ) // For the LoRa testbeds +#if defined( TESTRECEIVER ) #include "usb_device.h" #include "usbd_cdc_if.h" -extern SPI_HandleTypeDef hspi1; -#define LORA_SPI hspi1 - void serial_printlnx(unsigned char* mesg, size_t mesg_len){ while(CDC_Transmit_FS(mesg, mesg_len)==USBD_BUSY); } #endif +#if defined( STM32F103xB ) // For the LoRa testbeds +extern SPI_HandleTypeDef hspi1; +#define LORA_SPI hspi1 +#endif + #ifndef STM32F103xB // The LoRa testbeds don't support our LED library #include "led.h" @@ -291,7 +293,7 @@ LORA_STATUS lora_receive_ready() { LORA_STATUS mode_check = lora_read_register( LORA_REG_OPERATION_MODE, &mode ); mode = mode & 0x07; - #if defined( STM32F103xB ) // LoRa testbed test code + #if defined( TESTRECEIVER ) // LoRa testbed test code char buf[255]; int len = sprintf( buf, "Mode: %d\n", mode); serial_printlnx( buf, len ); @@ -346,7 +348,7 @@ LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t* buffer_len_ptr){ uint8_t num_bytes; LORA_STATUS fifo2_status = lora_read_register(LORA_REG_FIFO_RX_NUM_BYTES, &num_bytes); - #if defined( STM32F103xB ) + #if defined( TESTRECEIVER ) char buf[255]; int len = sprintf( buf, "Numbytes: %d\r\n", num_bytes ); serial_printlnx( buf, len ); From 4e2b097a6fc1b6094c9f28407315e814a0b4ad02 Mon Sep 17 00:00:00 2001 From: Dexx Stexx Date: Tue, 28 Oct 2025 09:10:48 -0700 Subject: [PATCH 10/14] Add missing conditional compilation --- lora/lora.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lora/lora.c b/lora/lora.c index 0b8700e..63c1056 100644 --- a/lora/lora.c +++ b/lora/lora.c @@ -307,9 +307,11 @@ LORA_STATUS lora_receive_ready() { if( irq_check = LORA_OK ) { uint8_t rx_done = (irq_flag & (1<<6)) >> 6; + #if defined( TESTRECEIVER ) char bufx[255]; int len = sprintf( bufx, "Register: %d\n", irq_flag); serial_printlnx( bufx, len ); + #endif if( rx_done ) { return LORA_READY; From d14432a2a9998163f8ee186b51787facaff39d8d Mon Sep 17 00:00:00 2001 From: Dexx Stexx Date: Tue, 28 Oct 2025 09:32:31 -0700 Subject: [PATCH 11/14] Check frequency legality --- lora/lora.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- lora/lora.h | 4 ++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/lora/lora.c b/lora/lora.c index 63c1056..7b470ad 100644 --- a/lora/lora.c +++ b/lora/lora.c @@ -158,8 +158,58 @@ LORA_STATUS lora_set_chip_mode( LORA_CHIPMODE chip_mode ) { return LORA_FAIL; } } - +/*------------------------------------------------------------------------------ + Chip initialization function +------------------------------------------------------------------------------*/ LORA_STATUS lora_init( LORA_CONFIG *lora_config_ptr ) { + // Check legality of frequency settings + // We do this first so that nothing gets set if we're on an illegal frequency. + + // Get a version of our bandwidth for legality calculations + uint32_t bandwidth; // Calculations down in hz due to decimal bandwidths + switch( lora_config_ptr->lora_bandwidth ) { + case LORA_BANDWIDTH_7_8_KHZ: + bandwidth = 7800; + break; + case LORA_BANDWIDTH_10_4_KHZ: + bandwidth = 10400; + break; + case LORA_BANDWIDTH_15_6_KHZ: + bandwidth = 15600; + break; + case LORA_BANDWIDTH_20_8_KHZ: + bandwidth = 20800; + break; + case LORA_BANDWIDTH_31_25_KHZ: + bandwidth = 31250; + break; + case LORA_BANDWIDTH_41_7_KHZ: + bandwidth = 41700; + break; + case LORA_BANDWIDTH_62_5_KHZ: + bandwidth = 62500; + break; + case LORA_BANDWIDTH_125_KHZ: + bandwidth = 125000; + break; + case LORA_BANDWIDTH_250_KHZ: + bandwidth = 250000; + break; + case LORA_BANDWIDTH_500_KHZ: + bandwidth = 500000; + break; + default: + // Just in case, even though this is reading an enum + return LORA_FAIL; + } + + // Check legal compliance of frequency: + if( !( lora_config_ptr->lora_frequency * 1000 + ( bandwidth / 2 ) <= ISM_MAX_FREQ * 1000 && + lora_config_ptr->lora_frequency * 1000 - ( bandwidth / 2 ) >= ISM_MAX_FREQ * 1000 ) + ) { + return LORA_FAIL; + } + LORA_STATUS set_sleep_status = lora_set_chip_mode( LORA_SLEEP_MODE ); // Switch to sleep mode to enable LoRa bit (datasheeet page 102) // Get initial value of the operation mode register uint8_t operation_mode_register; diff --git a/lora/lora.h b/lora/lora.h index 59f812d..cdabf3e 100644 --- a/lora/lora.h +++ b/lora/lora.h @@ -17,6 +17,10 @@ #define LORA_TIMEOUT 2000 +/* US ISM band frequencies, used in the code to prevent violating US law */ +#define ISM_MAX_FREQ 928000 +#define ISM_MIN_FREQ 902000 + typedef enum LORA_CHIPMODE { LORA_SLEEP_MODE = 0x00, LORA_STANDBY_MODE = 0x01, From 6b781a5fc4dd551ac525e7e8a15da7d21abb916f Mon Sep 17 00:00:00 2001 From: Dexx Stexx Date: Thu, 30 Oct 2025 14:27:25 -0700 Subject: [PATCH 12/14] Fix frequency setup --- lora/lora.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lora/lora.c b/lora/lora.c index 7b470ad..59988d9 100644 --- a/lora/lora.c +++ b/lora/lora.c @@ -205,7 +205,7 @@ LORA_STATUS lora_init( LORA_CONFIG *lora_config_ptr ) { // Check legal compliance of frequency: if( !( lora_config_ptr->lora_frequency * 1000 + ( bandwidth / 2 ) <= ISM_MAX_FREQ * 1000 && - lora_config_ptr->lora_frequency * 1000 - ( bandwidth / 2 ) >= ISM_MAX_FREQ * 1000 ) + lora_config_ptr->lora_frequency * 1000 - ( bandwidth / 2 ) >= ISM_MIN_FREQ * 1000 ) ) { return LORA_FAIL; } @@ -238,7 +238,11 @@ LORA_STATUS lora_init( LORA_CONFIG *lora_config_ptr ) { LORA_STATUS write_status3 = lora_write_register( LORA_REG_NUM_RX_BYTES, new_config1_register ); // Determine register values for the frequency registers - uint32_t frf_reg = lora_config_ptr->lora_frequency * 524288 / ( 32 * 1000 ); + uint32_t freq_mhz = lora_config_ptr->lora_frequency / 1000; // The megahertz component of our frequency. + uint32_t freq_khz = lora_config_ptr->lora_frequency - freq_mhz * 1000; // The kilohertz component of our frequency. + // The formula for converting khz to chip unit is fruqency * 524288 / ( 32 * 1000 ) + // Straight up doing that causes an integer overflow, though, so I have to do it this way. + uint32_t frf_reg = ( freq_mhz * 524288 / 32 ) + ( freq_khz * 524288 / ( 32 * 1000 ) ); uint8_t lora_freq_reg1 = ( frf_reg << 8 ) >> 24; uint8_t lora_freq_reg2 = ( frf_reg << 16 ) >> 24; From 212423cc45387f4937a159eca1905d1cde10cac2 Mon Sep 17 00:00:00 2001 From: Dexx Stexx Date: Mon, 3 Nov 2025 17:42:59 -0700 Subject: [PATCH 13/14] Fix IRQ check typo --- lora/lora.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lora/lora.c b/lora/lora.c index 59988d9..03c00a8 100644 --- a/lora/lora.c +++ b/lora/lora.c @@ -358,7 +358,7 @@ LORA_STATUS lora_receive_ready() { LORA_STATUS irq_check = lora_read_register(LORA_REG_IRQ_FLAGS, &irq_flag); - if( irq_check = LORA_OK ) { + if( irq_check == LORA_OK ) { uint8_t rx_done = (irq_flag & (1<<6)) >> 6; #if defined( TESTRECEIVER ) From 06fefc66dd55a5f0079e76544770db9dfb254348 Mon Sep 17 00:00:00 2001 From: Dexx Stexx Date: Mon, 17 Nov 2025 17:06:54 -0700 Subject: [PATCH 14/14] Fix basic LoRa continuous receive functionality --- lora/lora.c | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/lora/lora.c b/lora/lora.c index 03c00a8..199ca98 100644 --- a/lora/lora.c +++ b/lora/lora.c @@ -50,6 +50,11 @@ extern SPI_HandleTypeDef hspi1; #include "led.h" #endif +/*------------------------------------------------------------------------------ + Global Variables +------------------------------------------------------------------------------*/ +static LORA_STATUS lora_rx_done = LORA_WAITING; + /*------------------------------------------------------------------------------ Helper functions for various pin functions on the LoRa modem. ------------------------------------------------------------------------------*/ @@ -346,30 +351,37 @@ LORA_STATUS lora_receive_ready() { LORA_STATUS mode_check = lora_read_register( LORA_REG_OPERATION_MODE, &mode ); mode = mode & 0x07; - + /* #if defined( TESTRECEIVER ) // LoRa testbed test code char buf[255]; int len = sprintf( buf, "Mode: %d\n", mode); serial_printlnx( buf, len ); #endif - + */ if( mode_check == LORA_OK && mode == LORA_RX_CONTINUOUS_MODE ) { uint8_t irq_flag; LORA_STATUS irq_check = lora_read_register(LORA_REG_IRQ_FLAGS, &irq_flag); if( irq_check == LORA_OK ) { - uint8_t rx_done = (irq_flag & (1<<6)) >> 6; - + lora_write_register( LORA_REG_IRQ_FLAGS, irq_flag ); + uint8_t rx_done = ( irq_flag & 0x40 ) == 0x40; + // uint8_t rx_done = ( irq_flag & 0x40 ) == 0x00; + /* #if defined( TESTRECEIVER ) char bufx[255]; - int len = sprintf( bufx, "Register: %d\n", irq_flag); + int len = sprintf( bufx, "Register: %x\n", irq_flag); serial_printlnx( bufx, len ); + char bufy[255]; + int len2 = sprintf( bufy, "lora_receive_ready: %x\n", rx_done ); + serial_printlnx( bufy, len2 ); #endif - + */ if( rx_done ) { + lora_rx_done = LORA_READY; return LORA_READY; } else { + lora_rx_done = LORA_WAITING; return LORA_WAITING; } } else { @@ -386,18 +398,20 @@ LORA_STATUS lora_receive_ready() { LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t* buffer_len_ptr){ uint8_t timeout_flag; - LORA_STATUS rx_done = lora_receive_ready(); // We check if we've received a packet. + // LORA_STATUS rx_done = lora_receive_ready(); // We check if we've received a packet. // If we haven't received a packet, the output will be similar to lora_receive_ready in that case. - if ( rx_done == LORA_READY ){ + if ( lora_rx_done == LORA_READY ){ // Reset IRQ register. TODO: Not sure if I actually need to this; will investigate. uint8_t reset_irq = 0; - lora_write_register( LORA_REG_IRQ_FLAGS, reset_irq ); + // lora_write_register( LORA_REG_IRQ_FLAGS, reset_irq ); uint8_t irq_flag; LORA_STATUS irq_status2 = lora_read_register(LORA_REG_IRQ_FLAGS, &irq_flag); - uint8_t crc_err = (irq_flag & (1<<5)) >> 5; + lora_write_register( LORA_REG_IRQ_FLAGS, irq_flag ); + uint8_t crc_err = irq_flag & 0x20 == 0x00; + // uint8_t crc_err = irq_flag & 0x20 == 0x00; if (!crc_err){ // Read received number of bytes @@ -439,7 +453,7 @@ LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t* buffer_len_ptr){ } } return LORA_OK; - } else if( rx_done == LORA_WAITING ) { + } else if( lora_rx_done == LORA_WAITING ) { return LORA_WAITING; } return LORA_FAIL;