This commit is contained in:
openshwprojects
2022-04-15 10:15:28 +02:00
5 changed files with 249 additions and 212 deletions

View File

@@ -653,7 +653,7 @@ int http_fn_flash_read_tool(http_request_t *request) {
poststr(request,"<label for=\"offset\">offset:</label><br>\
<input type=\"number\" id=\"offset\" name=\"offset\"");
hprintf128(request," value=\"%i\"><br>",ofs);
poststr(request,"<label for=\"lenght\">lenght:</label><br>\
poststr(request,"<label for=\"len\">length:</label><br>\
<input type=\"number\" id=\"len\" name=\"len\" ");
hprintf128(request,"value=\"%i\">",len);
poststr(request,"<br><br>\

View File

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

View File

@@ -42,6 +42,10 @@ typedef struct pinButton_ {
new_btn_callback cb[BTN_number_of_event];
}pinButton_s;
// overall pins enable.
// if zero, all hardware action is disabled.
char g_enable_pins = 0;
#if WINDOWS
@@ -72,6 +76,7 @@ typedef struct item_pins_config
pinsState_t pins;
}ITEM_PINS_CONFIG,*ITEM_PINS_CONFIG_PTR;
void testI2C()
{
bk_i2c_device_t def;
@@ -481,7 +486,7 @@ void PIN_SaveToFlash() {
#endif
}
void PIN_LoadFromFlash() {
int i;
//int i;
#if WINDOWS
#elif PLATFORM_XR809
@@ -514,11 +519,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));
}
@@ -532,6 +544,7 @@ int PIN_GetPinChannel2ForPinIndex(int index) {
return g_pins.channels2[index];
}
void RAW_SetPinValue(int index, int iVal){
if (g_enable_pins) {
#if WINDOWS
#elif PLATFORM_XR809
@@ -542,8 +555,9 @@ void RAW_SetPinValue(int index, int iVal){
HAL_GPIO_WritePin(xr_port, xr_pin, iVal);
#else
bk_gpio_output(index, iVal);
bk_gpio_output(index, iVal);
#endif
}
}
void Button_OnShortClick(int index)
{
@@ -758,189 +772,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, &param);
param.driving = GPIO_DRIVING_LEVEL_1;
param.mode = GPIOx_Pn_F0_INPUT;
param.pull = GPIO_PULL_UP;
HAL_GPIO_Init(xr_port, xr_pin, &param);
}
#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, &param);
}
#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, &param);
#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, &param);
}
#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, &param);
#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;
}

View File

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

View File

@@ -56,6 +56,7 @@ static int g_secondsElapsed = 0;
static int g_openAP = 0;
static int g_connectToWiFi = 0;
int bSafeMode = 0;
// reset in this number of seconds
int g_reset = 0;
@@ -146,7 +147,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;
@@ -181,9 +182,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;
@@ -202,7 +203,7 @@ void run_adc_test(){
// undefined reference to `BkAdcTakeSample'
test = BkAdcTakeSample( adc, &output );
val = output;
ADDLOGF_INFO("BkAdcTakeSample res %i value %i\n", test, val);
ADDLOGF_INFO("BkAdcTakeSample res %i value %i\n", test, val);
}
#endif
static void app_led_timer_handler(void *data)
@@ -285,7 +286,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));
@@ -300,7 +301,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);
@@ -354,16 +355,20 @@ 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);
}
if (bootFailures > 4){
bSafeMode = 1;
ADDLOGF_INFO("###### safe mode activated - boot failures %d", bootFailures);
}
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";
@@ -374,14 +379,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;
@@ -390,48 +387,59 @@ 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);
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 (!bSafeMode){
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,