From d6164f31326245c4e311cc67448b8db5082bd5fc Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 24 Mar 2018 13:38:43 +0100 Subject: [PATCH] wifi antenna switch implented (LoPy/LoPy4 only) --- README.md | 8 +++++++- platformio.ini | 4 ++-- src/antenna.cpp | 20 +++++++++++--------- src/configmanager.cpp | 24 +++++++++++++++++++++++- src/globals.h | 1 + src/hal/lopy.h | 5 ++--- src/hal/lopy4.h | 5 ++--- src/main.cpp | 9 ++++----- src/main.h | 2 +- src/rcommand.cpp | 17 +++++++++++++++++ 10 files changed, 70 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 9f255cd4..b02dc705 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,11 @@ Note: all settings are stored in NVRAM and will be reloaded when device starts. 0 = disabled [default] 1 = enabled +0x0E set WIFI antenna switch (works on LoPy/LoPy4 only) + + 0 = internal antenna [default] + 1 = external antenna + 0x80 get device configuration device answers with it's current configuration: @@ -162,7 +167,8 @@ Note: all settings are stored in NVRAM and will be reloaded when device starts. byte 10: Wifi channel switch interval in seconds/100 (0..255) byte 11: BLE scan cycle duration in seconds (0..255) byte 12: BLE scan mode (1=on, 0=0ff) - bytes 13-22: Software version (ASCII format) + byte 13: Wifi antenna switch (0=internal, 1=external) + bytes 14-23: Software version (ASCII format) 0x81 get device uptime diff --git a/platformio.ini b/platformio.ini index 9f056e50..e0b37d2e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -10,10 +10,10 @@ ; ---> SELECT TARGET PLATFORM HERE! <--- [platformio] -;env_default = heltec_wifi_lora_32 +env_default = heltec_wifi_lora_32 ;env_default = ttgov1 ;env_default = ttgov2 -env_default = lopy +;env_default = lopy ;env_default = lopy4 [env:heltec_wifi_lora_32] diff --git a/src/antenna.cpp b/src/antenna.cpp index 5485f4fa..8bf94a2c 100644 --- a/src/antenna.cpp +++ b/src/antenna.cpp @@ -1,4 +1,6 @@ -#ifdef LOPY +/* switches wifi antenna, if board has switch to select internal and external antenna */ + +#ifdef HAS_ANTENNA_SWITCH #include @@ -11,7 +13,7 @@ typedef enum { } antenna_type_t; void antenna_init(void) { - gpio_config_t gpioconf = {.pin_bit_mask = 1ull << PIN_ANTENNA_SWITCH, + gpio_config_t gpioconf = {.pin_bit_mask = 1ull << HAS_ANTENNA_SWITCH, .mode = GPIO_MODE_OUTPUT, .pull_up_en = GPIO_PULLUP_DISABLE, .pull_down_en = GPIO_PULLDOWN_DISABLE, @@ -19,21 +21,21 @@ void antenna_init(void) { gpio_config(&gpioconf); } -void antenna_select (antenna_type_t _ant) { - if (PIN_ANTENNA_SWITCH < 32) { +void antenna_select (const int8_t _ant) { + if (HAS_ANTENNA_SWITCH < 32) { if (_ant == ANTENNA_EXT) { - GPIO_REG_WRITE(GPIO_OUT_W1TS_REG, 1 << PIN_ANTENNA_SWITCH); + GPIO_REG_WRITE(GPIO_OUT_W1TS_REG, 1 << HAS_ANTENNA_SWITCH); } else { - GPIO_REG_WRITE(GPIO_OUT_W1TC_REG, 1 << PIN_ANTENNA_SWITCH); + GPIO_REG_WRITE(GPIO_OUT_W1TC_REG, 1 << HAS_ANTENNA_SWITCH); } } else { if (_ant == ANTENNA_EXT) { - GPIO_REG_WRITE(GPIO_OUT1_W1TS_REG, 1 << (PIN_ANTENNA_SWITCH & 31)); + GPIO_REG_WRITE(GPIO_OUT1_W1TS_REG, 1 << (HAS_ANTENNA_SWITCH & 31)); } else { - GPIO_REG_WRITE(GPIO_OUT1_W1TC_REG, 1 << (PIN_ANTENNA_SWITCH & 31)); + GPIO_REG_WRITE(GPIO_OUT1_W1TC_REG, 1 << (HAS_ANTENNA_SWITCH & 31)); } } ESP_LOGI(TAG, "Wifi Antenna switched to %s", _ant ? "external" : "internal"); } -#endif // \ No newline at end of file +#endif \ No newline at end of file diff --git a/src/configmanager.cpp b/src/configmanager.cpp index 5a5d20f3..2194fa39 100644 --- a/src/configmanager.cpp +++ b/src/configmanager.cpp @@ -12,6 +12,11 @@ nvs_handle my_handle; esp_err_t err; +// defined in antenna.cpp +#ifdef HAS_ANTENNA_SWITCH + void antenna_select(const int8_t _ant); +#endif + // populate cfg vars with factory settings void defaultConfig() { cfg.lorasf = LORASFDEFAULT; // 7-12, initial lora spreadfactor defined in main.h @@ -25,6 +30,7 @@ void defaultConfig() { cfg.wifichancycle = WIFI_CHANNEL_SWITCH_INTERVAL; // wifi channel switch cycle [seconds/100] cfg.blescancycle = BLESCANTIME; // BLE scan cycle [seconds] cfg.blescan = 0; // 0=disabled, 1=enabled + cfg.wifiant = 0; // 0=internal, 1=external (for LoPy/LoPy4) strncpy( cfg.version, PROGVERSION, sizeof(cfg.version)-1 ); } @@ -101,7 +107,10 @@ void saveConfig() { nvs_set_i8(my_handle, "blescancycle", cfg.blescancycle); if( nvs_get_i8(my_handle, "blescanmode", &flash8) != ESP_OK || flash8 != cfg.blescan ) - nvs_set_i8(my_handle, "blescanmode", cfg.blescan); + nvs_set_i8(my_handle, "blescanmode", cfg.blescan); + + if( nvs_get_i8(my_handle, "wifiant", &flash8) != ESP_OK || flash8 != cfg.wifiant ) + nvs_set_i8(my_handle, "wifiant", cfg.wifiant); if( nvs_get_i16(my_handle, "rssilimit", &flash16) != ESP_OK || flash16 != cfg.rssilimit ) nvs_set_i16(my_handle, "rssilimit", cfg.rssilimit); @@ -218,6 +227,14 @@ void loadConfig() { ESP_LOGI(TAG, "WIFI channel cycle set to default %i", cfg.wifichancycle); saveConfig(); } + + if( nvs_get_i8(my_handle, "wifiant", &flash8) == ESP_OK ) { + cfg.wifiant = flash8; + ESP_LOGI(TAG, "wifiantenna = %i", flash8); + } else { + ESP_LOGI(TAG, "WIFI antenna switch set to default %i", cfg.wifiant); + saveConfig(); + } if( nvs_get_i8(my_handle, "blescancycle", &flash8) == ESP_OK ) { cfg.blescancycle = flash8; @@ -245,5 +262,10 @@ void loadConfig() { nvs_close(my_handle); ESP_LOGI(TAG, "Done"); + + // put actions to be triggered on loaded config here + #ifdef HAS_ANTENNA_SWITCH + antenna_select(cfg.wifiant); + #endif } } \ No newline at end of file diff --git a/src/globals.h b/src/globals.h index 59367845..8d6ed3fa 100644 --- a/src/globals.h +++ b/src/globals.h @@ -25,6 +25,7 @@ typedef struct { int8_t wifichancycle; // wifi channel switch cycle [seconds/100] int8_t blescancycle; // BLE scan cycle [seconds] int8_t blescan; // 0=disabled, 1=enabled + int8_t wifiant; // 0=internal, 1=external (for LoPy/LoPy4) char version[10]; // Firmware version } configData_t; diff --git a/src/hal/lopy.h b/src/hal/lopy.h index 6995ca9c..cead7c0a 100644 --- a/src/hal/lopy.h +++ b/src/hal/lopy.h @@ -1,6 +1,5 @@ // Hardware related definitions for Pycom LoPy Board (not: LoPy4) -#define LOPY #define CFG_sx1272_radio 1 // Hardware pin definitions for Pycom LoPy board @@ -14,5 +13,5 @@ #define DIO2 23 // workaround // select WIFI antenna (internal = onboard / external = u.fl socket) -#define PIN_ANTENNA_SWITCH 16 -#define WIFI_ANTENNA ANTENNA_INT // can be switced to ANTENNA_EXT +#define HAS_ANTENNA_SWITCH 16 // pin for switching wifi antenna +#define WIFI_ANTENNA 0 // 0 = internal, 1 = external diff --git a/src/hal/lopy4.h b/src/hal/lopy4.h index 0829bf1f..38f49bca 100644 --- a/src/hal/lopy4.h +++ b/src/hal/lopy4.h @@ -1,6 +1,5 @@ // Hardware related definitions for Pycom LoPy Board (not: LoPy4) -#define LOPY #define CFG_sx1276_radio 1 // Hardware pin definitions for Pycom LoPy4 board @@ -14,5 +13,5 @@ #define DIO2 23 // workaround // select WIFI antenna (internal = onboard / external = u.fl socket) -#define PIN_ANTENNA_SWITCH 21 -#define WIFI_ANTENNA ANTENNA_INT // can be switced to ANTENNA_EXT +#define HAS_ANTENNA_SWITCH 21 // pin for switching wifi antenna +#define WIFI_ANTENNA 0 // 0 = internal, 1 = external \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 505dd4c4..416a0db0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -149,10 +149,10 @@ void lorawan_loop(void * pvParameters) { U8X8_NULL u8x8; #endif -#ifdef LOPY +#ifdef HAS_ANTENNA_SWITCH // defined in antenna.cpp - void antenna_init (void); - void antenna_select (antenna_type_t antenna_type); + void antenna_init(); + void antenna_select(const int8_t _ant); #endif #if defined BLECOUNTER @@ -368,9 +368,8 @@ void setup() { #endif // initialize wifi antenna -#ifdef LOPY +#ifdef HAS_ANTENNA_SWITCH antenna_init(); - antenna_select(WIFI_ANTENNA); #endif // initialize display diff --git a/src/main.h b/src/main.h index 3f347f48..4d6434e1 100644 --- a/src/main.h +++ b/src/main.h @@ -1,5 +1,5 @@ // program version -#define PROGVERSION "1.2.33" // use max 10 chars here! +#define PROGVERSION "1.2.4" // use max 10 chars here! #define PROGNAME "PAXCNT" // Verbose enables serial output diff --git a/src/rcommand.cpp b/src/rcommand.cpp index ea63ef28..45d39a67 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -17,6 +17,11 @@ static const char *TAG = "rcommand"; void eraseConfig(void); void saveConfig(void); +// defined in antenna.cpp +#ifdef HAS_ANTENNA_SWITCH + void antenna_select(const int8_t _ant); +#endif + // table of remote commands and assigned functions typedef struct { const int nam; @@ -153,6 +158,17 @@ void set_blescan(int val) { } }; +void set_wifiant(int val) { + ESP_LOGI(TAG, "Remote command: set Wifi antenna to %s", val ? "external" : "internal"); + switch (val) { + case 1: cfg.wifiant = val; break; + default: cfg.wifiant = 0; break; + } + #ifdef HAS_ANTENNA_SWITCH + antenna_select(cfg.wifiant); + #endif +}; + void set_lorapower(int val) { ESP_LOGI(TAG, "Remote command: set LoRa TXPOWER to %i", val); switch_lora(cfg.lorasf, val); @@ -212,6 +228,7 @@ cmd_t table[] = { {0x0b, set_wifichancycle, true}, {0x0c, set_blescancycle, true}, {0x0d, set_blescan, true}, + {0x0e, set_wifiant, true}, {0x80, get_config, false}, {0x81, get_uptime, false}, {0x82, get_cputemp, false}