diff --git a/app/main.c b/app/main.c index f115378..734d96d 100644 --- a/app/main.c +++ b/app/main.c @@ -12,13 +12,13 @@ * Date : 2014-6-14 *****************************************************************************/ #include +#include "nano_shell_server_task.h" #include "wm_include.h" #include "wm_gpio_afsel.h" #include "nano_shell.h" #include "lwip/netif.h" #include "FreeRTOS.h" #include "FreeRTOSConfig.h" -#include "rtostimers.h" tls_os_task_t nano_shell_task_handle = NULL; extern s16 uart0_rx_callback(u16 len, void *user_data); @@ -26,7 +26,18 @@ extern s16 uart1_rx_callback(u16 len, void *user_data); extern int shell_printf(const char *format, ...); #define NANO_SHELL_TASK_STK_SIZE 1024 +#define NANO_SHELL_SERVER_TASK_STK_SIZE 640 #define STATUS_LED WM_IO_PB_18 +#define PWM_STATUS_LED WM_IO_PB_25 +#define FADE_DOWN 1 +#define FADE_UP -1 +#define FADE_LOW_THRESHOLD 255 +#define FADE_HIGH_THRESHOLD 200 +#define PULSE_FAST 3 +#define PULSE_SLOW 12 + + +u8 pulse_rate = PULSE_SLOW; void tls_netif_status_event_cb(u8 status) { @@ -41,11 +52,13 @@ void tls_netif_status_event_cb(u8 status) break; case NETIF_WIFI_DISCONNECTED: shell_printf("Evt : NETIF_WIFI_DISCONNECTED\n"); + pulse_rate = PULSE_SLOW; break; case NETIF_IP_NET_UP: shell_printf("Evt : NETIF_IP_NET_UP\nip addr : %v\nnetmask : %v\ngateway : %v\n", netif->ip_addr.addr, netif->netmask.addr, netif->gw.addr); + pulse_rate = PULSE_FAST; break; case NETIF_WIFI_SOFTAP_SUCCESS: shell_printf("Evt : NETIF_WIFI_SOFTAP_SUCCESS\n"); @@ -82,8 +95,12 @@ void tls_gpio_irq_cb(void *arg) void user_main(void *param) { + u8 pwm_led_duty_cycle = 255; + s8 fading_direction = FADE_UP; + //We initialize input/output used by the app - tls_gpio_cfg(STATUS_LED, WM_GPIO_DIR_OUTPUT, WM_GPIO_ATTR_FLOATING); + wm_pwm3_config(PWM_STATUS_LED); + tls_pwm_init(3, 1000, 0, 0); wm_uart1_tx_config(WM_IO_PB_06); wm_uart1_rx_config(WM_IO_PB_07); tls_gpio_irq_enable(WM_IO_PB_07, WM_GPIO_IRQ_TRIG_DOUBLE_EDGE); @@ -100,11 +117,13 @@ void user_main(void *param) //We create a task for the nano_shell process - u8 *nano_shell_task_stack = NULL; + u8 *nano_shell_task_stack = NULL, *nano_shell_server_task_stack = NULL; tls_uart_rx_callback_register(TLS_UART_0, &(uart0_rx_callback), NULL); tls_uart_rx_callback_register(TLS_UART_1, &(uart1_rx_callback), NULL); + nano_shell_server_task_stack = tls_mem_alloc(sizeof(u32) * NANO_SHELL_SERVER_TASK_STK_SIZE); + nano_shell_task_stack = tls_mem_alloc(sizeof(u32) * NANO_SHELL_TASK_STK_SIZE); if(nano_shell_task_stack != NULL) { @@ -139,9 +158,21 @@ void user_main(void *param) for(;;) { - //shell_printf("Waiting for clients\n"); - tls_gpio_write(STATUS_LED, !tls_gpio_read(STATUS_LED)); - tls_os_time_delay(pdMS_TO_TICKS(1000)); + tls_pwm_duty_set(3, pwm_led_duty_cycle); + if(pwm_led_duty_cycle == FADE_LOW_THRESHOLD) + { + fading_direction = FADE_UP; + tls_os_time_delay(pdMS_TO_TICKS(pulse_rate == PULSE_SLOW ? 500 : 100)); + } + else if(pwm_led_duty_cycle == FADE_HIGH_THRESHOLD) + { + fading_direction = FADE_DOWN; + tls_os_time_delay(pdMS_TO_TICKS(pulse_rate == PULSE_SLOW ? 500 : 100)); + } + + pwm_led_duty_cycle+=fading_direction; + + tls_os_time_delay(pdMS_TO_TICKS(pulse_rate)); /*client_socket = accept(listening_socket, (struct sockaddr *)&client, &socklent); shell_printf("Client got accepted\n"); while(recv(client_socket, buffer, 99, 0) > 0) diff --git a/app/nano_shell_command.c b/app/nano_shell_command.c index cbc3c98..11a52d5 100644 --- a/app/nano_shell_command.c +++ b/app/nano_shell_command.c @@ -9,6 +9,7 @@ extern int shell_printf(const char *format, ...); extern int wm_printf(const char *fmt,...); +extern u32 tls_mem_get_avail_heapsize(void); extern int demo_bt_enable(); extern int demo_bt_destroy(); @@ -106,7 +107,7 @@ int _system(const shell_cmd_t *pcmd, int argc, char *const argv[]) } else if(strcmp(argv[1], "ram_usage") == 0) { - shell_printf("Free heap : %u byte(s)\n", xPortGetFreeHeapSize()); + shell_printf("Free OS heap : %u/%u byte(s)\ntls heap size : %u\n", xPortGetFreeHeapSize(), configTOTAL_HEAP_SIZE, tls_mem_get_avail_heapsize()); } else { @@ -177,7 +178,7 @@ int _soft_ap(const shell_cmd_t *pcmd, int argc, char *const argv[]) } else { - shell_printf("Unknown soft_ap action\n"); + shell_printf("Unknown %s action\n", argv[0]); } } else @@ -228,7 +229,7 @@ int _station(const shell_cmd_t *pcmd, int argc, char *const argv[]) } else { - shell_printf("Unknown station action\n"); + shell_printf("Unknown %s action\n", argv[0]); } } else @@ -299,7 +300,7 @@ int _wifi(const shell_cmd_t *pcmd, int argc, char *const argv[]) } else { - shell_printf("Unknown wifi action\n"); + shell_printf("Unknown %s action\n", argv[0]); } } else @@ -325,7 +326,7 @@ int _wifi_sleep(const shell_cmd_t *pcmd, int argc, char *const argv[]) } else { - shell_printf("Unknown wifi_sleep action\n"); + shell_printf("Unknown %s action\n", argv[0]); } } @@ -360,7 +361,7 @@ int _pmu(const shell_cmd_t *pcmd, int argc, char *const argv[]) } else { - shell_printf("Unknown pmu action\n"); + shell_printf("Unknown %s action\n", argv[0]); } } else @@ -432,7 +433,7 @@ int _rtc(const shell_cmd_t *pcmd, int argc, char *const argv[]) } else { - shell_printf("Unknown rtc action\n"); + shell_printf("Unknown %s action\n", argv[0]); } } else @@ -448,11 +449,13 @@ int _bluetooth(const shell_cmd_t *pcmd, int argc, char *const argv[]) { if(strcmp(argv[1], "enable") == 0) { - shell_printf("Enabling bluetooth : %d\n", demo_bt_enable()); + //shell_printf("Enabling bluetooth : %d\n", demo_bt_enable()); + shell_printf("Enabling bluetooth test\n"); } else if(strcmp(argv[1], "disable") == 0) { - shell_printf("Disabling bluetooth : %d\n", demo_bt_destroy()); + //shell_printf("Disabling bluetooth : %d\n", demo_bt_destroy()); + shell_printf("Disabling bluetooth test\n"); } else if(strcmp(argv[1], "start_demo") == 0) { @@ -464,7 +467,7 @@ int _bluetooth(const shell_cmd_t *pcmd, int argc, char *const argv[]) } else { - shell_printf("Unknown bluetooth action\n"); + shell_printf("Unknown %s action\n", argv[0]); } } else diff --git a/app/nano_shell_port.c b/app/nano_shell_port.c index be47f0f..6ab5d48 100644 --- a/app/nano_shell_port.c +++ b/app/nano_shell_port.c @@ -3,10 +3,14 @@ static_fifo_declare(uart_char_fifo, 256, unsigned char, char); extern int sendchar(int ch); +extern void network_write(char c); extern tls_os_task_t nano_shell_task_handle; s16 uart0_rx_callback(u16 len, void *user_data) { + (void)len; + (void)user_data; + u8 buff[256] = ""; int data_len = tls_uart_read(TLS_UART_0, (u8 *) buff, 256); for(int i = 0; i < data_len; i++) @@ -14,12 +18,15 @@ s16 uart0_rx_callback(u16 len, void *user_data) fifo_push(uart_char_fifo, buff[i]); } - tls_os_task_resume_from_isr(nano_shell_task_handle); + (void)tls_os_task_resume_from_isr(nano_shell_task_handle); return 0; } s16 uart1_rx_callback(u16 len, void *user_data) { + (void)len; + (void)user_data; + u8 buff[256] = ""; int data_len = tls_uart_read(TLS_UART_1, (u8 *) buff, 256); for(int i = 0; i < data_len; i++) @@ -27,10 +34,22 @@ s16 uart1_rx_callback(u16 len, void *user_data) fifo_push(uart_char_fifo, buff[i]); } - tls_os_task_resume_from_isr(nano_shell_task_handle); + (void)tls_os_task_resume_from_isr(nano_shell_task_handle); return 0; } +void network_rx_callback(u16 len, char *data) +{ + if(!len)return; + + for(int i = 0; i < len; i++) + { + fifo_push(uart_char_fifo, data[i]); + } + + (void)tls_os_task_resume(nano_shell_task_handle); +} + int shell_getc(char *ch) { if(is_fifo_empty(uart_char_fifo)) @@ -49,4 +68,5 @@ void low_level_write_char(char ch) { (void)sendchar((int)ch); (void)tls_uart_write(TLS_UART_1, &ch, 1); + network_write(ch); } diff --git a/app/nano_shell_server_task.c b/app/nano_shell_server_task.c new file mode 100644 index 0000000..6bb5c31 --- /dev/null +++ b/app/nano_shell_server_task.c @@ -0,0 +1,6 @@ +#include "nano_shell_server_task.h" + +void network_write(char c) +{ + +} \ No newline at end of file diff --git a/app/nano_shell_server_task.h b/app/nano_shell_server_task.h new file mode 100644 index 0000000..e69de29 diff --git a/platform/sys/wm_main.c b/platform/sys/wm_main.c index 030acef..e8911bf 100644 --- a/platform/sys/wm_main.c +++ b/platform/sys/wm_main.c @@ -453,7 +453,7 @@ void task_start (void *data) if (tststarthdl) { tls_os_task_del_by_task_handle(tststarthdl,task_start_free); - } + } tls_os_time_delay(0x10000000); #else #if 1 diff --git a/src/os/rtos/include/FreeRTOSConfig.h b/src/os/rtos/include/FreeRTOSConfig.h index c8a89a2..e018803 100644 --- a/src/os/rtos/include/FreeRTOSConfig.h +++ b/src/os/rtos/include/FreeRTOSConfig.h @@ -77,7 +77,7 @@ #define configTICK_RATE_HZ ( ( portTickType ) 500u ) //ʱ��Ƭ�жϵ�Ƶ�� #define configMAX_PRIORITIES ( ( unsigned portBASE_TYPE ) 63) //Ӧ�ó����п������ȼ�����Ŀ #define configMINIMAL_STACK_SIZE ( ( unsigned short ) 90 ) //��������ʹ�õĶ�ջ��С -#define configTOTAL_HEAP_SIZE ( ( size_t ) 12 * 1024 ) //�ں��п��õ�RAM����,heap2ʹ�� +#define configTOTAL_HEAP_SIZE ( ( size_t ) 20 * 1024 ) //�ں��п��õ�RAM����,heap2ʹ�� #define configMAX_TASK_NAME_LEN ( 8 ) //����������������������� #define configUSE_TRACE_FACILITY 1 //�Ƿ�ʹ�ÿ��ӻ�׷�� #define configUSE_STATS_FORMATTING_FUNCTIONS 1 diff --git a/src/os/rtos/include/task.h b/src/os/rtos/include/task.h index 1aafebf..4447446 100644 --- a/src/os/rtos/include/task.h +++ b/src/os/rtos/include/task.h @@ -487,7 +487,7 @@ void vTaskAllocateMPURegions( TaskHandle_t xTask, const MemoryRegion_t * const p * \ingroup Tasks */ void vTaskDelete( TaskHandle_t xTaskToDelete ) PRIVILEGED_FUNCTION; - +void vTaskDeleteByHandle( TaskHandle_t xTaskToDelete, void (*freeStackfunc)(void)); /*----------------------------------------------------------- * TASK CONTROL API *----------------------------------------------------------*/ diff --git a/src/os/rtos/source/tasks.c b/src/os/rtos/source/tasks.c index 5a5ca24..59affca 100644 --- a/src/os/rtos/source/tasks.c +++ b/src/os/rtos/source/tasks.c @@ -169,6 +169,7 @@ typedef struct tskTaskControlBlock implements a system-wide malloc() that must be provided with locks. */ struct _reent xNewLib_reent; #endif + void (*freeStackfunc)(void); } tskTCB; @@ -768,6 +769,80 @@ TCB_t * pxNewTCB; } } + void vTaskDeleteByHandle( TaskHandle_t xTaskToDelete, void (*freeStackfunc)(void)) + { + TCB_t *pxTCB; + + taskENTER_CRITICAL(); + { + /* If null is passed in here then it is the calling task that is + being deleted. */ + pxTCB = prvGetTCBFromHandle( xTaskToDelete ); + pxTCB->freeStackfunc = freeStackfunc; + + /* Remove task from the ready list and place in the termination list. + This will stop the task from be scheduled. The idle task will check + the termination list and free up any memory allocated by the + scheduler for the TCB and stack. */ + if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 ) + { + taskRESET_READY_PRIORITY( pxTCB->uxPriority ); + } + else + { + mtCOVERAGE_TEST_MARKER(); + } + + /* Is the task waiting on an event also? */ + if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL ) + { + ( void ) uxListRemove( &( pxTCB->xEventListItem ) ); + } + else + { + mtCOVERAGE_TEST_MARKER(); + } + + vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xGenericListItem ) ); + + /* Increment the ucTasksDeleted variable so the idle task knows + there is a task that has been deleted and that it should therefore + check the xTasksWaitingTermination list. */ + ++uxTasksDeleted; + + /* Increment the uxTaskNumberVariable also so kernel aware debuggers + can detect that the task lists need re-generating. */ + uxTaskNumber++; + + traceTASK_DELETE( pxTCB ); + } + taskEXIT_CRITICAL(); + + /* Force a reschedule if it is the currently running task that has just + been deleted. */ + if( xSchedulerRunning != pdFALSE ) + { + if( pxTCB == pxCurrentTCB ) + { + configASSERT( uxSchedulerSuspended == 0 ); + + /* The pre-delete hook is primarily for the Windows simulator, + in which Windows specific clean up operations are performed, + after which it is not possible to yield away from this task - + hence xYieldPending is used to latch that a context switch is + required. */ + portPRE_TASK_DELETE_HOOK( pxTCB, &xYieldPending ); + portYIELD_WITHIN_API(); + } + else + { + /* Reset the next expected unblock time in case it referred to + the task that has just been deleted. */ + prvResetNextTaskUnblockTime(); + } + } + } + #endif /* INCLUDE_vTaskDelete */ /*-----------------------------------------------------------*/ @@ -2857,6 +2932,11 @@ static void prvCheckTasksWaitingTermination( void ) ( void ) uxListRemove( &( pxTCB->xGenericListItem ) ); --uxCurrentNumberOfTasks; --uxTasksDeleted; + + if(pxTCB->freeStackfunc) + { + pxTCB->freeStackfunc(); + } } taskEXIT_CRITICAL(); @@ -2926,6 +3006,7 @@ TCB_t *pxNewTCB; else { pxNewTCB->stacksize = ( StackType_t ) usStackDepth * sizeof(StackType_t); + pxNewTCB->freeStackfunc = NULL; /* Avoid dependency on memset() if it is not required. */ #if( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) ) { @@ -3084,7 +3165,11 @@ TCB_t *pxNewTCB; /* Free up the memory allocated by the scheduler for the task. It is up to the task to free any memory allocated at the application level. */ - vPortFreeAligned( pxTCB->pxStack ); + + //Only if we did not provide a custom free function, we free it using freeRTOS func + if(!pxTCB->freeStackfunc) + vPortFreeAligned( pxTCB->pxStack ); + vPortFree( pxTCB ); } diff --git a/src/os/rtos/wm_osal_rtos.c b/src/os/rtos/wm_osal_rtos.c index a5e2c35..feac0a6 100644 --- a/src/os/rtos/wm_osal_rtos.c +++ b/src/os/rtos/wm_osal_rtos.c @@ -145,10 +145,7 @@ tls_os_status_t tls_os_task_del(u8 prio,void (*freefun)(void)) tls_os_status_t tls_os_task_del_by_task_handle(void *handle, void (*freefun)(void)) { - //vTaskDeleteByHandle(handle, freefun); - vTaskDelete(handle); - if(freefun != NULL) - freefun(); + vTaskDeleteByHandle(handle, freefun); return TLS_OS_SUCCESS; } #endif @@ -220,7 +217,8 @@ tls_os_status_t tls_os_task_resume(tls_os_task_t task) tls_os_status_t tls_os_task_resume_from_isr(tls_os_task_t task) { - return (tls_os_status_t)xTaskResumeFromISR(task); + (void)xTaskResumeFromISR(task); + return TLS_OS_SUCCESS; } #endif