diff --git a/sdk/OpenTR6260 b/sdk/OpenTR6260 index 0fb78980d..bf3773e0e 160000 --- a/sdk/OpenTR6260 +++ b/sdk/OpenTR6260 @@ -1 +1 @@ -Subproject commit 0fb78980d04c195caf08be1ca349973ce8c2885b +Subproject commit bf3773e0ece279cb0650db7176fc56fe61902d01 diff --git a/src/cmnds/cmd_main.c b/src/cmnds/cmd_main.c index 5429785b4..df08b17f3 100644 --- a/src/cmnds/cmd_main.c +++ b/src/cmnds/cmd_main.c @@ -293,7 +293,7 @@ static commandResult_t CMD_ClearAll(const void* context, const char* cmd, const CHANNEL_ClearAllChannels(); CMD_ClearAllHandlers(0, 0, 0, 0); RepeatingEvents_Cmd_ClearRepeatingEvents(0, 0, 0, 0); -#if defined(WINDOWS) || defined(PLATFORM_BL602) || defined(PLATFORM_BEKEN) || defined(PLATFORM_LN882H) +#if defined(WINDOWS) || defined(PLATFORM_BL602) || defined(PLATFORM_BEKEN) || defined(PLATFORM_LN882H) || defined(PLATFORM_TR6260) CMD_resetSVM(0, 0, 0, 0); #endif @@ -855,7 +855,7 @@ void CMD_Init_Early() { CMD_RegisterCommand("IndexRefreshInterval", CMD_IndexRefreshInterval, NULL); -#if (defined WINDOWS) || (defined PLATFORM_BEKEN) || (defined PLATFORM_BL602) || (defined PLATFORM_LN882H) || (defined PLATFORM_ESPIDF) +#if (defined WINDOWS) || (defined PLATFORM_BEKEN) || (defined PLATFORM_BL602) || (defined PLATFORM_LN882H) || (defined PLATFORM_ESPIDF) || defined(PLATFORM_TR6260) CMD_InitScripting(); #endif if (!bSafeMode) { diff --git a/src/driver/drv_ds1820_simple.c b/src/driver/drv_ds1820_simple.c index ea33465df..547684e68 100644 --- a/src/driver/drv_ds1820_simple.c +++ b/src/driver/drv_ds1820_simple.c @@ -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++) { diff --git a/src/hal/tr6260/hal_flashVars_tr6260.c b/src/hal/tr6260/hal_flashVars_tr6260.c index 9c16c8e93..6899ef8d3 100644 --- a/src/hal/tr6260/hal_flashVars_tr6260.c +++ b/src/hal/tr6260/hal_flashVars_tr6260.c @@ -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() @@ -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 diff --git a/src/hal/tr6260/hal_pins_tr6260.c b/src/hal/tr6260/hal_pins_tr6260.c index e17193476..c835a8936 100644 --- a/src/hal/tr6260/hal_pins_tr6260.c +++ b/src/hal/tr6260/hal_pins_tr6260.c @@ -5,6 +5,9 @@ #include "../../new_cfg.h" #include "../../new_pins.h" #include "drv_gpio.h" +#include "drv_pwm.h" + +extern int g_pwmFrequency; typedef struct trPinMapping_s { @@ -18,33 +21,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) @@ -56,7 +75,9 @@ const char* HAL_PIN_GetPinNameAlias(int index) int HAL_PIN_CanThisPinBePWM(int index) { - return 0; + int ch = PIN_GetPWMIndexForPinIndex(index); + if(ch == -1) return 0; + return 1; } void HAL_PIN_SetOutputValue(int index, int iVal) @@ -72,7 +93,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) @@ -83,9 +107,10 @@ void HAL_PIN_Setup_Input_Pullup(int index) DRV_GPIO_CONFIG gpioCfg; gpioCfg.GPIO_Pin = pin->pin; gpioCfg.GPIO_PullEn = DRV_GPIO_PULL_EN; + gpioCfg.GPIO_Dir = DRV_GPIO_DIR_INPUT; gpioCfg.GPIO_PullType = DRV_GPIO_PULL_TYPE_UP; gpio_config(&gpioCfg); - gpio_set_dir(pin->pin, DRV_GPIO_DIR_INPUT); + //gpio_set_dir(pin->pin, DRV_GPIO_DIR_INPUT); } void HAL_PIN_Setup_Input_Pulldown(int index) @@ -96,9 +121,10 @@ void HAL_PIN_Setup_Input_Pulldown(int index) DRV_GPIO_CONFIG gpioCfg; gpioCfg.GPIO_Pin = pin->pin; gpioCfg.GPIO_PullEn = DRV_GPIO_PULL_EN; + gpioCfg.GPIO_Dir = DRV_GPIO_DIR_INPUT; gpioCfg.GPIO_PullType = DRV_GPIO_PULL_TYPE_DOWN; gpio_config(&gpioCfg); - gpio_set_dir(pin->pin, DRV_GPIO_DIR_INPUT); + //gpio_set_dir(pin->pin, DRV_GPIO_DIR_INPUT); } void HAL_PIN_Setup_Input(int index) @@ -109,8 +135,9 @@ void HAL_PIN_Setup_Input(int index) DRV_GPIO_CONFIG gpioCfg; gpioCfg.GPIO_Pin = pin->pin; gpioCfg.GPIO_PullEn = DRV_GPIO_PULL_DIS; + gpioCfg.GPIO_Dir = DRV_GPIO_DIR_INPUT; gpio_config(&gpioCfg); - gpio_set_dir(pin->pin, DRV_GPIO_DIR_INPUT); + //gpio_set_dir(pin->pin, DRV_GPIO_DIR_INPUT); } void HAL_PIN_Setup_Output(int index) @@ -125,18 +152,34 @@ 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_config(ch, g_pwmFrequency, 0); + 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, g_pwmFrequency, (uint32_t)(value * 10)); + pwm_start(ch); } unsigned int HAL_GetGPIOPin(int index) diff --git a/src/hal/tr6260/hal_wifi_tr6260.c b/src/hal/tr6260/hal_wifi_tr6260.c index 6a4b9cbe2..255934dbd 100644 --- a/src/hal/tr6260/hal_wifi_tr6260.c +++ b/src/hal/tr6260/hal_wifi_tr6260.c @@ -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) @@ -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: @@ -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); diff --git a/src/httpserver/http_tcp_server.c b/src/httpserver/http_tcp_server.c index fcdd3c04c..f0eed244a 100644 --- a/src/httpserver/http_tcp_server.c +++ b/src/httpserver/http_tcp_server.c @@ -6,6 +6,7 @@ #include "../logging/logging.h" #include "new_http.h" +#if !NEW_TCP_SERVER #define HTTP_SERVER_PORT 80 #define REPLY_BUFFER_SIZE 2048 #define INCOMING_BUFFER_SIZE 1024 @@ -365,4 +366,4 @@ void HTTPServer_Start() #endif - +#endif diff --git a/src/httpserver/new_tcp_server.c b/src/httpserver/new_tcp_server.c new file mode 100644 index 000000000..9570ba8e6 --- /dev/null +++ b/src/httpserver/new_tcp_server.c @@ -0,0 +1,354 @@ +#include "../new_common.h" +#include "../obk_config.h" + +#if NEW_TCP_SERVER + +#include "lwip/sockets.h" +#include "lwip/ip_addr.h" +#include "lwip/inet.h" +#include "../logging/logging.h" +#include "new_http.h" + +#ifndef MAX_SOCKETS_TCP +#define MAX_SOCKETS_TCP MEMP_NUM_TCP_PCB +#endif + +void HTTPServer_Start(); + +#define HTTP_SERVER_PORT 80 +#define REPLY_BUFFER_SIZE 2048 +#define INCOMING_BUFFER_SIZE 1024 +#define INVALID_SOCK -1 +#if NEW_TCP_USE_TASKS +#define HTTP_CLIENT_STACK_SIZE 8192 / sizeof(StackType_t) +#else +#define HTTP_CLIENT_STACK_SIZE 8192 +#endif + +typedef struct +{ + int fd; + TaskHandle_t thread; + bool isCompleted; +} tcp_thread_t; + +static xTaskHandle g_http_thread = NULL; +static int listen_sock = INVALID_SOCK; +static tcp_thread_t sock[MAX_SOCKETS_TCP - 1] = +{ + [0 ... MAX_SOCKETS_TCP - 2] = { -1, NULL, false }, +}; + +static void tcp_client_thread(tcp_thread_t* arg) +{ + int fd = arg->fd; + char* buf = NULL; + char* reply = NULL; + int replyBufferSize = REPLY_BUFFER_SIZE; + + GLOBAL_INT_DECLARATION(); + GLOBAL_INT_DISABLE(); + reply = (char*)os_malloc(replyBufferSize); + buf = (char*)os_malloc(INCOMING_BUFFER_SIZE); + GLOBAL_INT_RESTORE(); + + if(buf == 0 || reply == 0) + { + ADDLOG_ERROR(LOG_FEATURE_HTTP, "TCP Client failed to malloc buffer"); + goto exit; + } + http_request_t request; + os_memset(&request, 0, sizeof(request)); + + request.fd = fd; + request.received = buf; + request.receivedLenmax = INCOMING_BUFFER_SIZE - 2; + request.responseCode = HTTP_RESPONSE_OK; + request.receivedLen = 0; + while(1) + { + int remaining = request.receivedLenmax - request.receivedLen; + int received = recv(fd, request.received + request.receivedLen, remaining, 0); + if(received <= 0) + { + break; + } + request.receivedLen += received; + if(received < remaining) + { + break; + } + // grow by 1024 + request.receivedLenmax += 1024; + GLOBAL_INT_DISABLE(); + request.received = (char*)realloc(request.received, request.receivedLenmax + 2); + GLOBAL_INT_RESTORE(); + if(request.received == NULL) + { + // no memory + goto exit; + } + } + request.received[request.receivedLen] = 0; + + request.reply = reply; + request.replylen = 0; + reply[0] = '\0'; + + request.replymaxlen = replyBufferSize - 1; + + if(request.receivedLen <= 0) + { + ADDLOG_ERROR(LOG_FEATURE_HTTP, "TCP Client is disconnected, fd: %d", fd); + goto exit; + } + + //addLog( "TCP received string %s\n",buf ); + // returns length to be sent if any + // ADDLOG_DEBUG(LOG_FEATURE_HTTP, "TCP will process packet of len %i\n", request.receivedLen ); + int lenret = HTTP_ProcessPacket(&request); + if(lenret > 0) + { + ADDLOG_DEBUG(LOG_FEATURE_HTTP, "TCP sending reply len %i\n", lenret); + send(fd, reply, lenret, 0); + } + +exit: + GLOBAL_INT_DISABLE(); + if(buf != NULL) + os_free(buf); + if(reply != NULL) + os_free(reply); + GLOBAL_INT_RESTORE(); + + lwip_close(fd); + arg->isCompleted = true; +#if NEW_TCP_USE_TASKS + vTaskSuspend(NULL); +#else + rtos_suspend_thread(NULL); +#endif +} + +static inline char* get_clientaddr(struct sockaddr_storage* source_addr) +{ + static char address_str[128]; + char* res = NULL; + // Convert ip address to string + if(source_addr->ss_family == PF_INET) + { + res = inet_ntoa_r(((struct sockaddr_in*)source_addr)->sin_addr, address_str, sizeof(address_str) - 1); + } +#if LWIP_IPV6 + else if(source_addr->ss_family == PF_INET6) + { + res = inet6_ntoa_r(((struct sockaddr_in6*)source_addr)->sin6_addr, address_str, sizeof(address_str) - 1); + } +#endif + if(!res) + { + address_str[0] = '\0'; + } + return address_str; +} + +void restart_tcp_server(void* arg) +{ + HTTPServer_Start(); + vTaskDelete(NULL); +} + +static void tcp_server_thread(beken_thread_arg_t arg) +{ + OSStatus err = kNoErr; + int reuse = 1; + const size_t max_socks = MAX_SOCKETS_TCP - 1; + + struct sockaddr_in server_addr = + { + .sin_family = AF_INET, + .sin_addr = + { + .s_addr = INADDR_ANY, + }, + .sin_port = htons(HTTP_SERVER_PORT), + }; + + if(listen_sock != INVALID_SOCK) close(listen_sock); + + for(int i = 0; i < max_socks; ++i) + { + if(sock[i].fd != INVALID_SOCK) + { + close(sock[i].fd); + } + if(sock[i].thread != NULL) + { +#if NEW_TCP_USE_TASKS + vTaskDelete(sock[i].thread); +#else + rtos_delete_thread(&sock[i].thread); +#endif + } + sock[i].fd = INVALID_SOCK; + sock[i].thread = NULL; + sock[i].isCompleted = false; + } + + listen_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if(listen_sock < 0) + { + ADDLOG_ERROR(LOG_FEATURE_HTTP, "Unable to create socket"); + goto error; + } + + setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)); + + err = bind(listen_sock, (struct sockaddr*)&server_addr, sizeof(server_addr)); + if(err != 0) + { + ADDLOG_ERROR(LOG_FEATURE_HTTP, "Socket unable to bind"); + goto error; + } + ADDLOG_EXTRADEBUG(LOG_FEATURE_HTTP, "Socket bound on 0.0.0.0:%i", HTTP_SERVER_PORT); + + err = listen(listen_sock, 0); + if(err != 0) + { + ADDLOG_ERROR(LOG_FEATURE_HTTP, "Error occurred during listen"); + goto error; + } + ADDLOG_EXTRADEBUG(LOG_FEATURE_HTTP, "Socket listening"); + while(true) + { + struct sockaddr_storage source_addr; + socklen_t addr_len = sizeof(source_addr); + + int new_idx = 0; + for(new_idx = 0; new_idx < max_socks; ++new_idx) + { + if(sock[new_idx].isCompleted) + { + if(sock[new_idx].thread != NULL) + { +#if NEW_TCP_USE_TASKS + vTaskDelete(sock[new_idx].thread); +#else + rtos_delete_thread(&sock[new_idx].thread); +#endif + sock[new_idx].thread = NULL; + } + sock[new_idx].isCompleted = false; + sock[new_idx].fd = INVALID_SOCK; + } + if(sock[new_idx].fd == INVALID_SOCK) + { + break; + } + } + if(new_idx < max_socks) + { + sock[new_idx].fd = accept(listen_sock, (struct sockaddr*)&source_addr, &addr_len); + + if(sock[new_idx].fd < 0) + { + switch(errno) + { + //case EAGAIN: + case EWOULDBLOCK: break; + case EBADF: goto error; + default: + ADDLOG_ERROR(LOG_FEATURE_HTTP, "[sock=%d]: Error when accepting connection, err: %i", sock[new_idx].fd, errno); + break; + } + } + else + { + ADDLOG_EXTRADEBUG(LOG_FEATURE_HTTP, "[sock=%d]: Connection accepted from IP:%s", sock[new_idx].fd, get_clientaddr(&source_addr)); + + rtos_delay_milliseconds(20); +#if NEW_TCP_USE_TASKS + if(pdPASS != xTaskCreate( + (TaskFunction_t)tcp_client_thread, + "HTTP Client", + HTTP_CLIENT_STACK_SIZE, + (void*)&sock[new_idx], + BEKEN_APPLICATION_PRIORITY, + &sock[new_idx].thread)) +#else + if(kNoErr != rtos_create_thread(&sock[new_idx].thread, + BEKEN_APPLICATION_PRIORITY, + "HTTP Client", + (beken_thread_function_t)tcp_client_thread, + HTTP_CLIENT_STACK_SIZE, + (beken_thread_arg_t)&sock[new_idx])) +#endif + { + ADDLOG_DEBUG(LOG_FEATURE_HTTP, "[sock=%d]: TCP Client thread creation failed!", sock[new_idx].fd); + lwip_close(sock[new_idx].fd); + sock[new_idx].fd = INVALID_SOCK; + goto error; + } + } + } + vTaskDelay(pdMS_TO_TICKS(10)); + } + +error: + ADDLOG_DEBUG(LOG_FEATURE_HTTP, "TCP Server Error"); + if(listen_sock != INVALID_SOCK) + { + close(listen_sock); + } + + for(int i = 0; i < max_socks; ++i) + { + if(sock[i].thread != NULL) + { +#if NEW_TCP_USE_TASKS + vTaskDelete(sock[i].thread); +#else + rtos_delete_thread(&sock[i].thread); +#endif + sock[i].thread = NULL; + } + if(sock[i].fd != INVALID_SOCK) + { + close(sock[i].fd); + sock[i].fd = INVALID_SOCK; + } + } + xTaskCreate( + (TaskFunction_t)restart_tcp_server, + "TCP Restart", + 2048 / sizeof(StackType_t), + NULL, + BEKEN_APPLICATION_PRIORITY, + NULL); +} + +void HTTPServer_Start() +{ + OSStatus err = kNoErr; + + if(g_http_thread != NULL) + { +#if NEW_TCP_USE_TASKS + vTaskDelete(g_http_thread); +#else + rtos_delete_thread(&g_http_thread); +#endif + } + + err = rtos_create_thread(&g_http_thread, BEKEN_APPLICATION_PRIORITY, + "TCP_server", + (beken_thread_function_t)tcp_server_thread, + 0x800, + (beken_thread_arg_t)0); + if(err != kNoErr) + { + ADDLOG_ERROR(LOG_FEATURE_HTTP, "create \"TCP_server\" thread failed with %i!\r\n", err); + } +} + +#endif diff --git a/src/littlefs/our_lfs.c b/src/littlefs/our_lfs.c index 088202e47..2cc44c24a 100644 --- a/src/littlefs/our_lfs.c +++ b/src/littlefs/our_lfs.c @@ -678,6 +678,51 @@ static int lfs_erase(const struct lfs_config* c, lfs_block_t block) return res; } +#elif PLATFORM_TR6260 + +static int lfs_read(const struct lfs_config* c, lfs_block_t block, + lfs_off_t off, void* buffer, lfs_size_t size) +{ + int res; + + unsigned int startAddr = LFS_Start; + startAddr += block * LFS_BLOCK_SIZE; + startAddr += off; + + res = hal_spiflash_read(startAddr, (unsigned char*)buffer, size); + return res; +} + +// Program a region in a block. The block must have previously +// been erased. Negative error codes are propogated to the user. +// May return LFS_ERR_CORRUPT if the block should be considered bad. +static int lfs_write(const struct lfs_config* c, lfs_block_t block, + lfs_off_t off, const void* buffer, lfs_size_t size) +{ + int res; + + unsigned int startAddr = LFS_Start; + startAddr += block * LFS_BLOCK_SIZE; + startAddr += off; + + res = hal_spiflash_write(startAddr, (unsigned char*)buffer, size); + return res; +} + +// Erase a block. A block must be erased before being programmed. +// The state of an erased block is undefined. Negative error codes +// are propogated to the user. +// May return LFS_ERR_CORRUPT if the block should be considered bad. +static int lfs_erase(const struct lfs_config* c, lfs_block_t block) +{ + int res; + + unsigned int startAddr = LFS_Start; + startAddr += block * LFS_BLOCK_SIZE; + + res = hal_spiflash_erase(startAddr, LFS_BLOCK_SIZE); + return res; +} #endif diff --git a/src/littlefs/our_lfs.h b/src/littlefs/our_lfs.h index ed4d88a33..031a3ec73 100644 --- a/src/littlefs/our_lfs.h +++ b/src/littlefs/our_lfs.h @@ -68,6 +68,12 @@ #define LFS_BLOCKS_START_MIN 0x0 #define LFS_BLOCKS_END 0x50000 +#elif PLATFORM_TR6260 + +#define LFS_BLOCKS_START PARTION_LMA_LFS +#define LFS_BLOCKS_START_MIN PARTION_LMA_LFS +#define LFS_BLOCKS_END PARTION_LMA_LFS + 0x20000 + #else // TODO // start 0x1000 after OTA addr diff --git a/src/new_common.h b/src/new_common.h index 2cc7a77e3..2b3475f54 100644 --- a/src/new_common.h +++ b/src/new_common.h @@ -104,7 +104,7 @@ typedef long BaseType_t; #define DEVICENAME_PREFIX_FULL "OpenTR6260" #define DEVICENAME_PREFIX_SHORT "tr6260" #define PLATFORM_MCU_NAME "TR6260" -#define MANUFACTURER "Beijing Eswin Technology" +#define MANUFACTURER "Transa Semi" #else #error "You must define a platform.." This platform is not supported, error! @@ -483,6 +483,10 @@ OSStatus rtos_create_thread(beken_thread_t* thread, beken_thread_function_t function, uint32_t stack_size, beken_thread_arg_t arg); +#define GLOBAL_INT_DECLARATION() ; +#define GLOBAL_INT_DISABLE() ; +#define GLOBAL_INT_RESTORE() ; + #else #include "gw_intf.h" diff --git a/src/obk_config.h b/src/obk_config.h index 37d7e3e1a..fff44d7c3 100644 --- a/src/obk_config.h +++ b/src/obk_config.h @@ -198,7 +198,14 @@ #elif PLATFORM_TR6260 -#define OBK_DISABLE_ALL_DRIVERS 1 +#define ENABLE_LITTLEFS 1 +#define NEW_TCP_SERVER 1 +#define ENABLE_EXPAND_CONSTANT 1 +#define ENABLE_DRIVER_AHT2X 1 +#define ENABLE_DRIVER_BMPI2C 1 +#define ENABLE_DRIVER_DS1820 1 +#define ENABLE_DRIVER_LED 1 +#define ENABLE_DRIVER_WEMO 1 #else diff --git a/src/user_main.c b/src/user_main.c index 2f41d0f19..ee7a873a1 100644 --- a/src/user_main.c +++ b/src/user_main.c @@ -202,7 +202,7 @@ void extended_app_waiting_for_launch2(void) { #endif -#if defined(PLATFORM_LN882H) || defined(PLATFORM_ESPIDF) || defined(PLATFORM_TR6260) +#if defined(PLATFORM_LN882H) || defined(PLATFORM_ESPIDF) int LWIP_GetMaxSockets() { return 0; @@ -246,10 +246,20 @@ OSStatus rtos_create_thread(beken_thread_t* thread, } OSStatus rtos_delete_thread(beken_thread_t* thread) { - vTaskDelete(thread); + if(thread == NULL) vTaskDelete(thread); + else vTaskDelete(*thread); return kNoErr; } + +OSStatus rtos_suspend_thread(beken_thread_t* thread) +{ + if(thread == NULL) vTaskSuspend(thread); + else vTaskSuspend(*thread); + return kNoErr; +} + #endif + void MAIN_ScheduleUnsafeInit(int delSeconds) { g_doUnsafeInitIn = delSeconds; }