Added support for storing settings in FLASH.

This commit is contained in:
marcel
2022-05-10 21:04:51 +02:00
parent b6eec82d6c
commit cc24f9d531
13 changed files with 30829 additions and 30348 deletions

View File

@@ -60,14 +60,18 @@
const uint8_t SIG_SYNCED = 0x02;
const uint8_t RX_ONGOING = 0x04;
// The size of this struc should be a exactly 256 bytes, which is the FLASH_PAGE_SIZE of the RPi pico's flash memory
struct aprssettings {
uint8_t MyCall[10] = { 'P','E','1','R','X','F','-','5', 0} ;
uint8_t ServerCall[10] = { 'P','E','1','R','X','F','-','3', 0} ;
uint8_t Destination[10] = { 'A','P','Z','M','D','M', 0} ;
uint8_t Path1[10] = { 0,'I','D','E','1','-', '1', 0} ;
uint8_t Path2[10] = { 0,'I','D','E','2','-', '2', 0} ;
uint8_t FirmwareVersion[20] = { 'V','1',',','C','o','n','t','r', 'o','l','l','e','r',' ','0','1', 0} ;
uint8_t ValidFlashData = 0x5A; // Indicates flash contains valid data - 1 bytes
uint8_t MyCall[10] = { 'N','O','C','A','L','L','-','2', 0} ; // 10 bytes
uint8_t ServerCall[10] = { 'N','O','C','A','L','L','-','1', 0} ; // 10 bytes
uint8_t Destination[10] = { 'A','P','Z','M','D','M', 0} ; // 10 bytes
uint8_t Path1[10] = { 0,'I','D','E','1','-', '1', 0} ; // 10 bytes
uint8_t Path2[10] = { 0,'I','D','E','2','-', '2', 0} ; // 10 bytes
uint8_t FirmwareVersion[20] = { 'V','1',',','C','o','n','t','r', 'o','l','l','e','r',' ','0','1', 0} ; // 20 bytes
// 71 bytes total
uint8_t FillerData[256-71];
} AprsSettings;
struct status {

View File

@@ -3,12 +3,13 @@
#include <time.h>
#include "pico/stdlib.h"
#include "pico/binary_info.h"
#include "hardware/flash.h"
#include "LoRa-RP2040.h"
#include "Config.h"
#include "KISS.h"
#include "hardware/claim.h"
bool startRadio();
bool LoadSettings();
void getPacketData(int packetLength);
int compare_strings(uint8_t a[], uint8_t b[]);
bool is_message_for_me (uint8_t data[], uint8_t mycall[]);
@@ -27,14 +28,49 @@ const uint PowerSupply5VControl = 4;
const uint RelayOffControl = 2;
const uint RelayOnControl = 3;
int main() {
// We're going to use a region 256k from the start of flash as non volatile storage for our settings.
// We can access this at XIP_BASE + 256k.
#define FLASH_TARGET_OFFSET (512 * 1024)
const uint8_t *flash_target_contents = (const uint8_t *) (XIP_BASE + FLASH_TARGET_OFFSET);
uint8_t ReadSettingsFromFlash(void)
{
uint16_t ServerCommand = 0;
uint16_t TxDelay = 0;
/* Among others, this initializes the USB-serial port at 115200bps 8N1 */
// If byte zero of flash contains 0x5A we assume the data to be valid, otherwise we fill the flash with default values.
if (flash_target_contents[0] != 0x5A)
{
printf( "No valid data found in FLASH memory.\n" );
memset(AprsSettings.FillerData, 0, sizeof(AprsSettings.FillerData));
uint32_t ints = save_and_disable_interrupts();
// First we erase the FLASH sector we need. After that we can store new values.
// Note that a whole number of sectors must be erased at a time.
// Sector size is 4kB, so this is way bigger than the needed 256 bytes for storing the settings.
printf("Erasing FLASH region...");
flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE);
printf("done\n");
printf("Writing default values to FLASH...");
flash_range_program(FLASH_TARGET_OFFSET, (uint8_t*)&AprsSettings, FLASH_PAGE_SIZE);
printf("done\n");
restore_interrupts (ints);
} else {
// Read settings stored in flash memory
printf("Found valid settings in FLASH memory.\n");
}
memcpy((uint8_t*)&AprsSettings, flash_target_contents, FLASH_PAGE_SIZE);
printf("APRS settings:\n");
printf("My call: %s\n", AprsSettings.MyCall);
printf("Server call: %s\n", AprsSettings.ServerCall);
printf("Firmware: %s\n",AprsSettings.FirmwareVersion);
}
void setup(void)
{
/* Among others, this initializes the USB-serial port at 115200bps 8N1 */
stdio_init_all();
// Buffers
memset(rxBuffer, 0, sizeof(rxBuffer));
memset(txBuffer, 0, sizeof(txBuffer));
@@ -63,12 +99,20 @@ int main() {
gpio_put(RelayOffControl, 0);
gpio_put(RelayOnControl, 0);
Status.ControlRelay = OFF;
sleep_ms(5000);
LoadSettings();
ReadSettingsFromFlash();
startRadio();
}
int main() {
uint16_t ServerCommand = 0;
uint16_t TxDelay = 0;
setup();
while (1) {
int packetSize = LoRa.parsePacket();
@@ -196,16 +240,6 @@ int main() {
return 0;
}
/*
* Load settings from EEPROM
*/
bool LoadSettings()
{
printf("APRS settings:\n");
printf("My call: %s\n", AprsSettings.MyCall);
printf("Server call: %s\n", AprsSettings.ServerCall);
}
/*
* Initializes the LoRa module with the parameters set in config.h
*/