diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c
index 90081df55..8873f33a8 100644
--- a/src/httpserver/http_fns.c
+++ b/src/httpserver/http_fns.c
@@ -653,7 +653,7 @@ int http_fn_flash_read_tool(http_request_t *request) {
poststr(request,"
\
",ofs);
- poststr(request,"
\
+ poststr(request,"
\
",len);
poststr(request,"
\
diff --git a/src/mqtt/new_mqtt.c b/src/mqtt/new_mqtt.c
index 4a5a6626e..ac7fbdbd9 100644
--- a/src/mqtt/new_mqtt.c
+++ b/src/mqtt/new_mqtt.c
@@ -55,6 +55,8 @@ static int g_my_reconnect_mqtt_after_time = -1;
ip_addr_t mqtt_ip LWIP_MQTT_EXAMPLE_IPADDR_INIT;
mqtt_client_t* mqtt_client;
+static int mqtt_initialised = 0;
+
typedef struct mqtt_callback_tag {
char *topic;
char *subscriptionTopic;
@@ -595,12 +597,16 @@ void MQTT_init(){
// note: this may REPLACE an existing entry with the same ID. ID 2 !!!
MQTT_RegisterCallback( cbtopicbase, cbtopicsub, 2, tasCmnd);
+ mqtt_initialised = 1;
+
}
// called from user timer.
void MQTT_RunEverySecondUpdate() {
+ if (!mqtt_initialised) return;
+
// if asked to reconnect (e.g. change of topic(s))
if (mqtt_reconnect > 0){
mqtt_reconnect --;
diff --git a/src/new_pins.c b/src/new_pins.c
index e80a89094..c44c7b930 100644
--- a/src/new_pins.c
+++ b/src/new_pins.c
@@ -72,6 +72,10 @@ typedef struct item_pins_config
pinsState_t pins;
}ITEM_PINS_CONFIG,*ITEM_PINS_CONFIG_PTR;
+// overall pins enable.
+// if zero, all hardware action is disabled.
+char g_enable_pins = 0;
+
void testI2C()
{
bk_i2c_device_t def;
@@ -481,7 +485,7 @@ void PIN_SaveToFlash() {
#endif
}
void PIN_LoadFromFlash() {
- int i;
+ //int i;
#if WINDOWS
#elif PLATFORM_XR809
@@ -514,11 +518,18 @@ void PIN_LoadFromFlash() {
os_memcpy(&g_pins, &pins.pins, sizeof(g_pins));
}
#endif
+}
+
+
+// call after PIN_LoadFromFlash() - if you want to actually set them
+void PIN_SetupPins() {
+ int i;
for(i = 0; i < GPIO_MAX; i++) {
PIN_SetPinRoleForPinIndex(i,g_pins.roles[i]);
}
addLogAdv(LOG_INFO, LOG_FEATURE_GENERAL,"PIN_LoadFromFlash pins have been set up.\r\n");
}
+
void PIN_ClearPins() {
memset(&g_pins,0,sizeof(g_pins));
}
@@ -542,7 +553,9 @@ void RAW_SetPinValue(int index, int iVal){
HAL_GPIO_WritePin(xr_port, xr_pin, iVal);
#else
- bk_gpio_output(index, iVal);
+ if (g_enable_pins) {
+ bk_gpio_output(index, iVal);
+ }
#endif
}
void Button_OnShortClick(int index)
@@ -758,189 +771,196 @@ void PIN_SetPinRoleForPinIndex(int index, int role) {
role = IOR_None;
}
#endif
- switch(g_pins.roles[index])
- {
- case IOR_Button:
- case IOR_Button_n:
- case IOR_Button_ToggleAll:
- case IOR_Button_ToggleAll_n:
+
+ if (g_enable_pins) {
+ switch(g_pins.roles[index])
{
- //pinButton_s *bt = &g_buttons[index];
- // TODO: disable button
-#if WINDOWS
-
-#elif PLATFORM_XR809
+ case IOR_Button:
+ case IOR_Button_n:
+ case IOR_Button_ToggleAll:
+ case IOR_Button_ToggleAll_n:
+ {
+ //pinButton_s *bt = &g_buttons[index];
+ // TODO: disable button
+ #if WINDOWS
+
+ #elif PLATFORM_XR809
-#else
-#endif
- }
- break;
- case IOR_LED:
- case IOR_LED_n:
- case IOR_Relay:
- case IOR_Relay_n:
-#if WINDOWS
-
-#elif PLATFORM_XR809
-
-#else
- // TODO: disable?
-#endif
- break;
- // Disable PWM for previous pin role
- case IOR_PWM:
- {
- int pwmIndex;
- //int channelIndex;
-
- pwmIndex = PIN_GetPWMIndexForPinIndex(index);
- // is this pin capable of PWM?
- if(pwmIndex != -1) {
-#if WINDOWS
-
-#elif PLATFORM_XR809
-
-#elif PLATFORM_BK7231N
- bk_pwm_stop(pwmIndex);
-#elif PLATFORM_BK7231T
- bk_pwm_stop(pwmIndex);
-#else
-#error "Unknown platform"
-#endif
+ #else
+ #endif
}
- }
- break;
+ break;
+ case IOR_LED:
+ case IOR_LED_n:
+ case IOR_Relay:
+ case IOR_Relay_n:
+ #if WINDOWS
+
+ #elif PLATFORM_XR809
- default:
- break;
+ #else
+ // TODO: disable?
+ #endif
+ break;
+ // Disable PWM for previous pin role
+ case IOR_PWM:
+ {
+ int pwmIndex;
+ //int channelIndex;
+
+ pwmIndex = PIN_GetPWMIndexForPinIndex(index);
+ // is this pin capable of PWM?
+ if(pwmIndex != -1) {
+ #if WINDOWS
+
+ #elif PLATFORM_XR809
+
+ #elif PLATFORM_BK7231N
+ bk_pwm_stop(pwmIndex);
+ #elif PLATFORM_BK7231T
+ bk_pwm_stop(pwmIndex);
+ #else
+ #error "Unknown platform"
+ #endif
+ }
+ }
+ break;
+
+ default:
+ break;
+ }
}
// set new role
g_pins.roles[index] = role;
- // init new role
- switch(role)
- {
- case IOR_Button:
- case IOR_Button_n:
- case IOR_Button_ToggleAll:
- case IOR_Button_ToggleAll_n:
+
+ if (g_enable_pins) {
+ // init new role
+ switch(role)
{
- pinButton_s *bt = &g_buttons[index];
-#if WINDOWS
-
-#elif PLATFORM_XR809
+ case IOR_Button:
+ case IOR_Button_n:
+ case IOR_Button_ToggleAll:
+ case IOR_Button_ToggleAll_n:
{
- int xr_port; // eg GPIO_PORT_A
- int xr_pin; // eg. GPIO_PIN_20
- GPIO_InitParam param;
+ pinButton_s *bt = &g_buttons[index];
+ #if WINDOWS
+
+ #elif PLATFORM_XR809
+ {
+ int xr_port; // eg GPIO_PORT_A
+ int xr_pin; // eg. GPIO_PIN_20
+ GPIO_InitParam param;
- PIN_XR809_GetPortPinForIndex(index, &xr_port, &xr_pin);
+ PIN_XR809_GetPortPinForIndex(index, &xr_port, &xr_pin);
- param.driving = GPIO_DRIVING_LEVEL_1;
- param.mode = GPIOx_Pn_F0_INPUT;
- param.pull = GPIO_PULL_UP;
- HAL_GPIO_Init(xr_port, xr_pin, ¶m);
+ param.driving = GPIO_DRIVING_LEVEL_1;
+ param.mode = GPIOx_Pn_F0_INPUT;
+ param.pull = GPIO_PULL_UP;
+ HAL_GPIO_Init(xr_port, xr_pin, ¶m);
+ }
+ #else
+ bk_gpio_config_input_pup(index);
+ #endif
+ // init button after initializing pin role
+ NEW_button_init(bt, button_generic_get_gpio_value, 0);
+ /* button_attach(bt, BTN_SINGLE_CLICK, Button_OnShortClick);
+ button_attach(bt, BTN_DOUBLE_CLICK, Button_OnDoubleClick);
+ button_attach(bt, BTN_LONG_PRESS_HOLD, Button_OnLongPressHold);
+ button_start(bt);*/
}
-#else
- bk_gpio_config_input_pup(index);
-#endif
- // init button after initializing pin role
- NEW_button_init(bt, button_generic_get_gpio_value, 0);
- /* button_attach(bt, BTN_SINGLE_CLICK, Button_OnShortClick);
- button_attach(bt, BTN_DOUBLE_CLICK, Button_OnDoubleClick);
- button_attach(bt, BTN_LONG_PRESS_HOLD, Button_OnLongPressHold);
- button_start(bt);*/
- }
- break;
- case IOR_DigitalInput:
- case IOR_DigitalInput_n:
-#if WINDOWS
-
-#elif PLATFORM_XR809
- {
- int xr_port; // eg GPIO_PORT_A
- int xr_pin; // eg. GPIO_PIN_20
- GPIO_InitParam param;
+ break;
+ case IOR_DigitalInput:
+ case IOR_DigitalInput_n:
+ #if WINDOWS
+
+ #elif PLATFORM_XR809
+ {
+ int xr_port; // eg GPIO_PORT_A
+ int xr_pin; // eg. GPIO_PIN_20
+ GPIO_InitParam param;
- PIN_XR809_GetPortPinForIndex(index, &xr_port, &xr_pin);
+ PIN_XR809_GetPortPinForIndex(index, &xr_port, &xr_pin);
- param.driving = GPIO_DRIVING_LEVEL_1;
- param.mode = GPIOx_Pn_F0_INPUT;
- param.pull = GPIO_PULL_UP;
- HAL_GPIO_Init(xr_port, xr_pin, ¶m);
- }
-#else
- bk_gpio_config_input_pup(index);
-#endif
- break;
- case IOR_LED:
- case IOR_LED_n:
- case IOR_Relay:
- case IOR_Relay_n:
- {
-#if WINDOWS
-
-#elif PLATFORM_XR809
- GPIO_InitParam param;
- int xr_port; // eg GPIO_PORT_A
- int xr_pin; // eg. GPIO_PIN_20
-
- PIN_XR809_GetPortPinForIndex(index, &xr_port, &xr_pin);
-
- /*set pin driver capability*/
- param.driving = GPIO_DRIVING_LEVEL_1;
- param.mode = GPIOx_Pn_F1_OUTPUT;
- param.pull = GPIO_PULL_NONE;
- HAL_GPIO_Init(xr_port, xr_pin, ¶m);
-#else
- bk_gpio_config_output(index);
- bk_gpio_output(index, 0);
-#endif
- }
- break;
- case IOR_PWM:
+ param.driving = GPIO_DRIVING_LEVEL_1;
+ param.mode = GPIOx_Pn_F0_INPUT;
+ param.pull = GPIO_PULL_UP;
+ HAL_GPIO_Init(xr_port, xr_pin, ¶m);
+ }
+ #else
+ bk_gpio_config_input_pup(index);
+ #endif
+ break;
+ case IOR_LED:
+ case IOR_LED_n:
+ case IOR_Relay:
+ case IOR_Relay_n:
{
- int pwmIndex;
- int channelIndex;
- float f;
+ #if WINDOWS
+
+ #elif PLATFORM_XR809
+ GPIO_InitParam param;
+ int xr_port; // eg GPIO_PORT_A
+ int xr_pin; // eg. GPIO_PIN_20
- pwmIndex = PIN_GetPWMIndexForPinIndex(index);
- // is this pin capable of PWM?
- if(pwmIndex != -1) {
- channelIndex = PIN_GetPinChannelForPinIndex(index);
-#if WINDOWS
-
-#elif PLATFORM_XR809
+ PIN_XR809_GetPortPinForIndex(index, &xr_port, &xr_pin);
-#elif PLATFORM_BK7231N
- // OSStatus bk_pwm_initialize(bk_pwm_t pwm, uint32_t frequency, uint32_t duty_cycle);
- bk_pwm_initialize(pwmIndex, 1000, 0, 0, 0);
-
- bk_pwm_start(pwmIndex);
- f = g_channelValues[channelIndex] * 0.01f;
- // OSStatus bk_pwm_update_param(bk_pwm_t pwm, uint32_t frequency, uint32_t duty_cycle1, uint32_t duty_cycle2, uint32_t duty_cycle3)
- bk_pwm_update_param(pwmIndex, 1000, f * 1000.0f,0,0);
-
-#else
- // OSStatus bk_pwm_initialize(bk_pwm_t pwm, uint32_t frequency, uint32_t duty_cycle);
- bk_pwm_initialize(pwmIndex, 1000, 0);
-
- bk_pwm_start(pwmIndex);
- // they are using 1kHz PWM
- // See: https://www.elektroda.pl/rtvforum/topic3798114.html
- // bk_pwm_update_param(pwmIndex, 1000, g_channelValues[channelIndex]);
- f = g_channelValues[channelIndex] * 0.01f;
- // OSStatus bk_pwm_update_param(bk_pwm_t pwm, uint32_t frequency, uint32_t duty_cycle)
- bk_pwm_update_param(pwmIndex, 1000, f * 1000.0f);
-
-#endif
- }
+ /*set pin driver capability*/
+ param.driving = GPIO_DRIVING_LEVEL_1;
+ param.mode = GPIOx_Pn_F1_OUTPUT;
+ param.pull = GPIO_PULL_NONE;
+ HAL_GPIO_Init(xr_port, xr_pin, ¶m);
+ #else
+ bk_gpio_config_output(index);
+ bk_gpio_output(index, 0);
+ #endif
}
- break;
+ break;
+ case IOR_PWM:
+ {
+ int pwmIndex;
+ int channelIndex;
+ float f;
- default:
- break;
+ pwmIndex = PIN_GetPWMIndexForPinIndex(index);
+ // is this pin capable of PWM?
+ if(pwmIndex != -1) {
+ channelIndex = PIN_GetPinChannelForPinIndex(index);
+ #if WINDOWS
+
+ #elif PLATFORM_XR809
+
+ #elif PLATFORM_BK7231N
+ // OSStatus bk_pwm_initialize(bk_pwm_t pwm, uint32_t frequency, uint32_t duty_cycle);
+ bk_pwm_initialize(pwmIndex, 1000, 0, 0, 0);
+
+ bk_pwm_start(pwmIndex);
+ f = g_channelValues[channelIndex] * 0.01f;
+ // OSStatus bk_pwm_update_param(bk_pwm_t pwm, uint32_t frequency, uint32_t duty_cycle1, uint32_t duty_cycle2, uint32_t duty_cycle3)
+ bk_pwm_update_param(pwmIndex, 1000, f * 1000.0f,0,0);
+
+ #else
+ // OSStatus bk_pwm_initialize(bk_pwm_t pwm, uint32_t frequency, uint32_t duty_cycle);
+ bk_pwm_initialize(pwmIndex, 1000, 0);
+
+ bk_pwm_start(pwmIndex);
+ // they are using 1kHz PWM
+ // See: https://www.elektroda.pl/rtvforum/topic3798114.html
+ // bk_pwm_update_param(pwmIndex, 1000, g_channelValues[channelIndex]);
+ f = g_channelValues[channelIndex] * 0.01f;
+ // OSStatus bk_pwm_update_param(bk_pwm_t pwm, uint32_t frequency, uint32_t duty_cycle)
+ bk_pwm_update_param(pwmIndex, 1000, f * 1000.0f);
+
+ #endif
+ }
+ }
+ break;
+
+ default:
+ break;
+ }
}
}
+
void PIN_SetPinChannelForPinIndex(int index, int ch) {
g_pins.channels[index] = ch;
}
diff --git a/src/new_pins.h b/src/new_pins.h
index 9f6c11413..ed849ddd4 100644
--- a/src/new_pins.h
+++ b/src/new_pins.h
@@ -53,11 +53,13 @@ typedef struct pinsState_s {
extern pinsState_t g_pins;
+extern char g_enable_pins;
#define GPIO_MAX 27
#define CHANNEL_MAX 32
void PIN_Init(void);
+void PIN_SetupPins();
void PIN_ClearPins();
int PIN_GetPinRoleForPinIndex(int index);
int PIN_GetPinChannelForPinIndex(int index);
diff --git a/src/user_main.c b/src/user_main.c
index 248cb90ff..f473a5967 100644
--- a/src/user_main.c
+++ b/src/user_main.c
@@ -185,7 +185,7 @@ void connect_to_wifi(const char *oob_ssid,const char *connect_key)
os_memset( &wNetConfigAdv, 0x0, sizeof(network_InitTypeDef_adv_st) );
os_strcpy((char*)wNetConfigAdv.ap_info.ssid, oob_ssid);
- hwaddr_aton("48:ee:0c:48:93:12", (u8 *)wNetConfigAdv.ap_info.bssid);
+ hwaddr_aton("48:ee:0c:48:93:12", (unsigned char *)wNetConfigAdv.ap_info.bssid);
wNetConfigAdv.ap_info.security = SECURITY_TYPE_WPA2_MIXED;
wNetConfigAdv.ap_info.channel = 5;
@@ -220,9 +220,9 @@ beken_timer_t led_timer;
// OpenSHWProjects 2022 04 11
// I tried to implement ADC but it seems that BkAdcInitialize and BkAdcTakeSample
// are missing from the precompiled Bekken library...
-int adc_init = 0;
+int adc_init = 0;
#include "../../beken378/func/user_driver/BkDriverADC.h"
-void run_adc_test(){
+void run_adc_test(){
OSStatus test;
int adc;
short res;
@@ -324,7 +324,7 @@ static int setup_wifi_open_access_point(void)
ap_param_t ap_info;
network_InitTypeDef_st wNetConfig;
int len;
- u8 *mac;
+ unsigned char *mac;
os_memset(&general, 0, sizeof(general_param_t));
os_memset(&ap_info, 0, sizeof(ap_param_t));
@@ -339,7 +339,7 @@ static int setup_wifi_open_access_point(void)
ADDLOGF_INFO("no flash configuration, use default\r\n");
- mac = (u8*)&ap_info.bssid.array;
+ mac = (unsigned char*)&ap_info.bssid.array;
// this is MAC for Access Point, it's different than Client one
// see wifi_get_mac_address source
wifi_get_mac_address((char *)mac, CONFIG_ROLE_AP);
@@ -394,16 +394,17 @@ void user_main(void)
// read or initialise the boot count flash area
increment_boot_count();
+ bootFailures = boot_failures();
+ if (bootFailures > 3){
+ bForceOpenAP = 1;
+ ADDLOGF_INFO("###### force AP mode - boot failures %d", bootFailures);
+ } else {
+ }
+
CFG_InitAndLoad();
- DRV_Generic_Init();
- RepeatingEvents_Init();
-
wifi_ssid = CFG_GetWiFiSSID();
wifi_pass = CFG_GetWiFiPass();
- ADDLOGF_INFO("Using SSID [%s]\r\n",wifi_ssid);
- ADDLOGF_INFO("Using Pass [%s]\r\n",wifi_pass);
-
#if 0
// you can use this if you bricked your module by setting wrong access point data
wifi_ssid = "qqqqqqqqqq";
@@ -414,14 +415,6 @@ void user_main(void)
bForceOpenAP = 1;
#endif
-
- bootFailures = boot_failures();
- if (bootFailures > 3){
- bForceOpenAP = 1;
- ADDLOGF_INFO("###### force AP mode - boot failures %d", bootFailures);
- } else {
- }
-
if(*wifi_ssid == 0 || *wifi_pass == 0 || bForceOpenAP) {
// start AP mode in 5 seconds
g_openAP = 5;
@@ -430,49 +423,60 @@ void user_main(void)
g_connectToWiFi = 5;
}
+ ADDLOGF_INFO("Using SSID [%s]\r\n",wifi_ssid);
+ ADDLOGF_INFO("Using Pass [%s]\r\n",wifi_pass);
+
// NOT WORKING, I done it other way, see ethernetif.c
//net_dhcp_hostname_set(g_shortDeviceName);
//demo_start_upd();
start_tcp_http();
ADDLOGF_DEBUG("Started http tcp server\r\n");
-
- PIN_Init();
- ADDLOGF_DEBUG("Initialised pins\r\n");
+ // only initialise certain things if we are not in AP mode
+ if (!g_openAP){
+ g_enable_pins = 1;
+ // this actually sets the pins, moved out so we could avoid if necessary
+ PIN_SetupPins();
- PIN_SetGenericDoubleClickCallback(app_on_generic_dbl_click);
- ADDLOGF_DEBUG("Initialised other callbacks\r\n");
+ DRV_Generic_Init();
+ RepeatingEvents_Init();
+ PIN_Init();
+ ADDLOGF_DEBUG("Initialised pins\r\n");
+
+ // initialise MQTT - just sets up variables.
+ // all MQTT happens in timer thread?
+ MQTT_init();
+
+ PIN_SetGenericDoubleClickCallback(app_on_generic_dbl_click);
+ ADDLOGF_DEBUG("Initialised other callbacks\r\n");
#ifdef BK_LITTLEFS
- // initialise the filesystem, only if present.
- // don't create if it does not mount
- init_lfs(0);
+ // initialise the filesystem, only if present.
+ // don't create if it does not mount
+ // do this for ST mode only, as it may be something in FS which is killing us,
+ // and we may add a command to empty fs just be writing first sector?
+ init_lfs(0);
#endif
- // initialise rest interface
- init_rest();
+ // initialise rest interface
+ init_rest();
- // initialise MQTT - just sets up variables.
- // all MQTT happens in timer thread?
- MQTT_init();
-
- // add some commands...
- taslike_commands_init();
- fortest_commands_init();
- NewLED_InitCommands();
-
- // NOTE: this will try to read autoexec.bat,
- // so ALL commands expected in autoexec.bat should have been registered by now...
- // but DON't run autoexec if we have had 2+ boot failures
- CMD_Init();
-
- if (bootFailures < 2){
- CMD_ExecuteCommand("exec autoexec.bat");
- }
+ // add some commands...
+ taslike_commands_init();
+ fortest_commands_init();
+ NewLED_InitCommands();
+ // NOTE: this will try to read autoexec.bat,
+ // so ALL commands expected in autoexec.bat should have been registered by now...
+ // but DON't run autoexec if we have had 2+ boot failures
+ CMD_Init();
+ if (bootFailures < 2){
+ CMD_ExecuteCommand("exec autoexec.bat");
+ }
+ }
err = rtos_init_timer(&led_timer,
1 * 1000,