wifi antenna switch implented (LoPy/LoPy4 only)

This commit is contained in:
Klaus K Wilting 2018-03-24 13:38:43 +01:00
parent 62325e9d40
commit d6164f3132
10 changed files with 70 additions and 25 deletions

View File

@ -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

View File

@ -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]

View File

@ -1,4 +1,6 @@
#ifdef LOPY
/* switches wifi antenna, if board has switch to select internal and external antenna */
#ifdef HAS_ANTENNA_SWITCH
#include <Arduino.h>
@ -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 //
#endif

View File

@ -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
}
}

View File

@ -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;

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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}