Skip to content

Commit

Permalink
various fixes/features
Browse files Browse the repository at this point in the history
  • Loading branch information
NonPIayerCharacter committed Dec 8, 2024
1 parent f863aca commit afd45c8
Show file tree
Hide file tree
Showing 11 changed files with 597 additions and 30 deletions.
2 changes: 2 additions & 0 deletions src/driver/drv_ds1820_simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ void usleepds(int r)
usleep(5 * r); // "5" seems o.k
#elif PLATFORM_ESPIDF
usleep(r);
#elif PLATFORM_TR6260
usdelay(r);
#else
for(volatile int i = 0; i < r; i++)
{
Expand Down
113 changes: 100 additions & 13 deletions src/hal/tr6260/hal_flashVars_tr6260.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,107 @@

#include "../../new_common.h"
#include "../hal_flashVars.h"
#include "../../logging/logging.h"

FLASH_VARS_STRUCTURE flash_vars;
static int flash_vars_init_flag = 0;
static int g_loaded = 0;

#define KV_KEY_FLASH_VARS "OBK_FLASH_VARS"
#define KV_KEY_FLASH_VARS "OBK_FV"
#define SAVE_CHANGE_IF_REQUIRED_AND_COUNT(target, source, counter) \
if((target) != (source)) { \
(target) = (source); \
counter++; \
}

int flash_vars_store()
static int ReadFlashVars(void* target, int dataLen)
{
return 0;
int readLen;
ADDLOG_DEBUG(LOG_FEATURE_CFG, "ReadFlashVars: will read %d bytes", dataLen);
readLen = ef_get_env_blob(KV_KEY_FLASH_VARS, target, dataLen, NULL);
ADDLOG_DEBUG(LOG_FEATURE_CFG, "ReadFlashVars: really loaded %d bytes", readLen);
g_loaded = 1;
return dataLen;
}

int flash_vars_init()
static int SaveFlashVars(void* src, int dataLen)
{
return 0;
EfErrCode res;

res = ef_set_env_blob(KV_KEY_FLASH_VARS, src, dataLen);
if(res == EF_ENV_INIT_FAILED)
{
ADDLOG_DEBUG(LOG_FEATURE_CFG, "SaveFlashVars: EF_ENV_INIT_FAILED for %d bytes", dataLen);
return 0;
}
if(res == EF_ENV_NAME_ERR)
{
ADDLOG_DEBUG(LOG_FEATURE_CFG, "SaveFlashVars: EF_ENV_ARG_ERR for %d bytes", dataLen);
return 0;
}
ADDLOG_DEBUG(LOG_FEATURE_CFG, "SaveFlashVars: saved %d bytes", dataLen);
return dataLen;
}

// call at startup
void HAL_FlashVars_IncreaseBootCount()
{

memset(&flash_vars, 0, sizeof(flash_vars));
ReadFlashVars(&flash_vars, sizeof(flash_vars));
flash_vars.boot_count++;
SaveFlashVars(&flash_vars, sizeof(flash_vars));
}

void HAL_FlashVars_SaveChannel(int index, int value)
{

if(index < 0 || index >= MAX_RETAIN_CHANNELS)
return;
if(g_loaded == 0)
{
ReadFlashVars(&flash_vars, sizeof(flash_vars));
}
flash_vars.savedValues[index] = value;
// save after increase
SaveFlashVars(&flash_vars, sizeof(flash_vars));
}

void HAL_FlashVars_ReadLED(byte* mode, short* brightness, short* temperature, byte* rgb, byte* bEnableAll)
{

if(g_loaded == 0)
{
ReadFlashVars(&flash_vars, sizeof(flash_vars));
}
*bEnableAll = flash_vars.savedValues[MAX_RETAIN_CHANNELS - 4];
*mode = flash_vars.savedValues[MAX_RETAIN_CHANNELS - 3];
*temperature = flash_vars.savedValues[MAX_RETAIN_CHANNELS - 2];
*brightness = flash_vars.savedValues[MAX_RETAIN_CHANNELS - 1];
rgb[0] = flash_vars.rgb[0];
rgb[1] = flash_vars.rgb[1];
rgb[2] = flash_vars.rgb[2];
}

void HAL_FlashVars_SaveLED(byte mode, short brightness, short temperature, byte r, byte g, byte b, byte bEnableAll)
{

int iChangesCount = 0;

if(g_loaded == 0)
{
ReadFlashVars(&flash_vars, sizeof(flash_vars));
}

SAVE_CHANGE_IF_REQUIRED_AND_COUNT(flash_vars.savedValues[MAX_RETAIN_CHANNELS - 1], brightness, iChangesCount);
SAVE_CHANGE_IF_REQUIRED_AND_COUNT(flash_vars.savedValues[MAX_RETAIN_CHANNELS - 2], temperature, iChangesCount);
SAVE_CHANGE_IF_REQUIRED_AND_COUNT(flash_vars.savedValues[MAX_RETAIN_CHANNELS - 3], mode, iChangesCount);
SAVE_CHANGE_IF_REQUIRED_AND_COUNT(flash_vars.savedValues[MAX_RETAIN_CHANNELS - 4], bEnableAll, iChangesCount);
SAVE_CHANGE_IF_REQUIRED_AND_COUNT(flash_vars.rgb[0], r, iChangesCount);
SAVE_CHANGE_IF_REQUIRED_AND_COUNT(flash_vars.rgb[1], g, iChangesCount);
SAVE_CHANGE_IF_REQUIRED_AND_COUNT(flash_vars.rgb[2], b, iChangesCount);

if(iChangesCount > 0)
{
ADDLOG_INFO(LOG_FEATURE_CFG, "####### Flash Save LED #######");
SaveFlashVars(&flash_vars, sizeof(flash_vars));
}
}

short HAL_FlashVars_ReadUsage()
Expand All @@ -52,39 +118,60 @@ void HAL_FlashVars_SaveTotalUsage(short usage)
// call once started (>30s?)
void HAL_FlashVars_SaveBootComplete()
{

flash_vars.boot_success_count = flash_vars.boot_count;
SaveFlashVars(&flash_vars, sizeof(flash_vars));
}

// call to return the number of boots since a HAL_FlashVars_SaveBootComplete
int HAL_FlashVars_GetBootFailures()
{
int diff = 0;
diff = flash_vars.boot_count - flash_vars.boot_success_count;
return diff;
}

int HAL_FlashVars_GetBootCount()
{

return flash_vars.boot_count;
}

int HAL_FlashVars_GetChannelValue(int ch)
{
return 0;
if(ch < 0 || ch >= MAX_RETAIN_CHANNELS)
return 0;
if(g_loaded == 0)
{
ReadFlashVars(&flash_vars, sizeof(flash_vars));
}
return flash_vars.savedValues[ch];
}

int HAL_GetEnergyMeterStatus(ENERGY_METERING_DATA* data)
{
if(data != NULL)
{
if(g_loaded == 0)
{
ReadFlashVars(&flash_vars, sizeof(flash_vars));
}
memcpy(data, &flash_vars.emetering, sizeof(ENERGY_METERING_DATA));
}
return 0;
}

int HAL_SetEnergyMeterStatus(ENERGY_METERING_DATA* data)
{
if(data != NULL)
{
memcpy(&flash_vars.emetering, data, sizeof(ENERGY_METERING_DATA));
SaveFlashVars(&flash_vars, sizeof(flash_vars));
}
return 0;
}

void HAL_FlashVars_SaveTotalConsumption(float total_consumption)
{

flash_vars.emetering.TotalConsumption = total_consumption;
}

#endif // PLATFORM_TR6260
Expand Down
64 changes: 55 additions & 9 deletions src/hal/tr6260/hal_pins_tr6260.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "../../new_cfg.h"
#include "../../new_pins.h"
#include "drv_gpio.h"
#include "drv_pwm.h"

typedef struct trPinMapping_s
{
Expand All @@ -18,33 +19,49 @@ trPinMapping_t g_pins[] = {
{ "GPIO2", DRV_GPIO_2 },
{ "GPIO3", DRV_GPIO_3 },
{ "GPIO4", DRV_GPIO_4 },
{ "GPIO5", DRV_GPIO_5 },
{ "GPIO6", DRV_GPIO_6 },
{ "GPIO5 (RX0)", DRV_GPIO_5 },
{ "GPIO6 (TX0)", DRV_GPIO_6 },
{ "GPIO7", DRV_GPIO_7 },
{ "GPIO8", DRV_GPIO_8 },
{ "GPIO9", DRV_GPIO_9 },
{ "GPIO10", DRV_GPIO_10 },
{ "GPIO11", DRV_GPIO_11 },
{ "GPIO12", DRV_GPIO_12 },
{ "GPIO13", DRV_GPIO_13 },
{ "GPIO13 (PWM5)", DRV_GPIO_13 },
{ "GPIO14", DRV_GPIO_14 },
{ "GPIO15", DRV_GPIO_15 },
{ "GPIO16", DRV_GPIO_16 },
{ "GPIO17", DRV_GPIO_17 },
{ "GPIO18", DRV_GPIO_18 },
{ "GPIO19", DRV_GPIO_19 },
{ "GPIO20", DRV_GPIO_20 },
{ "GPIO21", DRV_GPIO_21 },
{ "GPIO22", DRV_GPIO_22 },
{ "GPIO20 (PWM0)", DRV_GPIO_20 },
{ "GPIO21 (PWM1)", DRV_GPIO_21 },
{ "GPIO22 (PWM2)", DRV_GPIO_22 },
#if defined (_USR_TR6260)
{ "GPIO23 (PWM3)", DRV_GPIO_23 },
{ "GPIO24 (PWM4)", DRV_GPIO_24 },
#else
{ "GPIO23", DRV_GPIO_23 },
{ "GPIO24", DRV_GPIO_24 },
#endif
};

static int g_numPins = sizeof(g_pins) / sizeof(g_pins[0]);

int PIN_GetPWMIndexForPinIndex(int pin)
{
return -1;
switch(pin)
{
case 13: return PMW_CHANNEL_5;
case 20: return PMW_CHANNEL_0;
case 21: return PMW_CHANNEL_1;
case 22: return PMW_CHANNEL_2;
#if defined (_USR_TR6260)
case 23: return PMW_CHANNEL_3;
case 24: return PMW_CHANNEL_4;
#endif
default: return -1;
}
}

const char* HAL_PIN_GetPinNameAlias(int index)
Expand All @@ -56,7 +73,18 @@ const char* HAL_PIN_GetPinNameAlias(int index)

int HAL_PIN_CanThisPinBePWM(int index)
{
return 0;
switch(index)
{
case 20:
case 21:
case 22:
#if defined (_USR_TR6260)
case 23:
case 24:
#endif
case 13: return 1;
default: return 0;
}
}

void HAL_PIN_SetOutputValue(int index, int iVal)
Expand All @@ -72,7 +100,10 @@ int HAL_PIN_ReadDigitalInput(int index)
if(index >= g_numPins)
return 0;
trPinMapping_t* pin = g_pins + index;
return gpio_read_level(pin->pin);
//return gpio_read_level(pin->pin);
int value = -1;
gpio_read(pin->pin, &value);
return value;
}

void HAL_PIN_Setup_Input_Pullup(int index)
Expand Down Expand Up @@ -125,18 +156,33 @@ void HAL_PIN_PWM_Stop(int index)
{
if(index >= g_numPins)
return;
int ch = PIN_GetPWMIndexForPinIndex(index);
if(ch < 0) return;
//pwm_deinit(ch);
pwm_stop(ch);
}

void HAL_PIN_PWM_Start(int index)
{
if(index >= g_numPins)
return;
int ch = PIN_GetPWMIndexForPinIndex(index);
if(ch < 0) return;
pwm_init(ch);
}

void HAL_PIN_PWM_Update(int index, float value)
{
if(index >= g_numPins)
return;
int ch = PIN_GetPWMIndexForPinIndex(index);
if(ch < 0) return;
if(value < 0)
value = 0;
if(value > 100)
value = 100;
pwm_config(ch, 1000, (uint32_t)(value * 10));
pwm_start(ch);
}

unsigned int HAL_GetGPIOPin(int index)
Expand Down
14 changes: 10 additions & 4 deletions src/hal/tr6260/hal_wifi_tr6260.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ static sys_err_t handle_wifi_event(void* ctx, system_event_t* event)
vif = event->vif;
switch(event->event_id)
{
case SYSTEM_EVENT_STA_START:
{
if(g_wifiStatusCallback != NULL)
{
g_wifiStatusCallback(WIFI_STA_CONNECTING);
}
}
case SYSTEM_EVENT_STA_GOT_IP:
{
if(g_wifiStatusCallback != NULL)
Expand All @@ -100,6 +107,9 @@ static sys_err_t handle_wifi_event(void* ctx, system_event_t* event)
{
g_wifiStatusCallback(WIFI_STA_DISCONNECTED);
}
//HAL_DisconnectFromWifi();
//hal_lmac_reset_all();
HAL_RebootModule();
break;
}
case SYSTEM_EVENT_STA_ASSOC_REJECT:
Expand Down Expand Up @@ -138,10 +148,6 @@ void HAL_ConnectToWiFi(const char* oob_ssid, const char* connect_key, obkStaticI
unsigned int sta_ip = 0, count = 0;
printf("HAL_ConnectToWiFi");
wifi_set_opmode(WIFI_MODE_STA);
if(g_wifiStatusCallback != NULL)
{
g_wifiStatusCallback(WIFI_STA_CONNECTING);
}
wifi_remove_config_all(STATION_IF);
wifi_remove_config_all(SOFTAP_IF);
wifi_add_config(STATION_IF);
Expand Down
Loading

0 comments on commit afd45c8

Please sign in to comment.