/***************************************************************************** * * File Name : main.c * * Description: main * * Copyright (c) 2014 Winner Micro Electronic Design Co., Ltd. * All rights reserved. * * Author : dave * * 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 "common.h" tls_os_task_t nano_shell_task_handle = NULL; tls_os_task_t nano_shell_server_task_handle = NULL; extern s16 uart0_rx_callback(u16 len, void *user_data); extern s16 uart1_rx_callback(u16 len, void *user_data); extern int shell_printf(const char *format, ...); #define NANO_SHELL_TASK_STK_SIZE 640 #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) { struct netif *netif = tls_get_netif(); switch(status) { case NETIF_WIFI_JOIN_SUCCESS: shell_printf("Evt : NETIF_WIFI_JOIN_SUCCESS"NEW_LINE); break; case NETIF_WIFI_JOIN_FAILED: shell_printf("Evt : NETIF_WIFI_JOIN_FAILED"NEW_LINE); break; case NETIF_WIFI_DISCONNECTED: shell_printf("Evt : NETIF_WIFI_DISCONNECTED"NEW_LINE); pulse_rate = PULSE_SLOW; break; case NETIF_IP_NET_UP: shell_printf("Evt : NETIF_IP_NET_UP"NEW_LINE"ip addr : %v"NEW_LINE"netmask : %v"NEW_LINE"gateway : %v"NEW_LINE, 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"NEW_LINE); break; case NETIF_WIFI_SOFTAP_FAILED: shell_printf("Evt : NETIF_WIFI_SOFTAP_FAILED"NEW_LINE); break; case NETIF_WIFI_SOFTAP_CLOSED: shell_printf("Evt : NETIF_WIFI_SOFTAP_CLOSED"NEW_LINE); pulse_rate = PULSE_SLOW; break; case NETIF_IP_NET2_UP: shell_printf("Evt : NETIF_IP_NET2_UP"NEW_LINE"ip addr : %v"NEW_LINE"netmask : %v"NEW_LINE"gateway : %v"NEW_LINE, netif->next->ip_addr.addr, netif->next->netmask.addr, netif->next->gw.addr); pulse_rate = PULSE_FAST; break; case NETIF_IPV6_NET_UP: shell_printf("Evt : NETIF_IPV6_NET_UP"NEW_LINE); break; default: shell_printf("Evt : UNKNOWN"NEW_LINE); break; } } void touchsensor_cb(u32 status) { shell_printf("Touch detected : status(%u)"NEW_LINE, status); } void tls_gpio_irq_cb(void *arg) { tls_clr_gpio_irq_status(WM_IO_PB_07); } void user_main(void *param) { u8 pwm_led_duty_cycle = 255; s8 fading_direction = FADE_UP; //We initialize input/output used by the app 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); tls_gpio_isr_register(WM_IO_PB_07, &(tls_gpio_irq_cb), NULL); //We set a a pin as touch sensor : wm_touch_sensor_config(WM_IO_PA_07); tls_touchsensor_threshold_config(1, 120); tls_touchsensor_init_config(1, 16, 16,1); tls_touchsensor_irq_enable(1); tls_touchsensor_irq_register(&(touchsensor_cb)); //We init the uart 1 tls_uart_port_init(TLS_UART_1, NULL, 0); //We create a task for the nano_shell process 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); if(nano_shell_server_task_stack != NULL) { tls_os_status_t status = tls_os_task_create( &nano_shell_server_task_handle, "shll_srv", &(nano_shell_server_task), NULL, (void*) nano_shell_server_task_stack, NANO_SHELL_SERVER_TASK_STK_SIZE * sizeof(u32_t), 62, 0 ); if(status != TLS_OS_SUCCESS) shell_printf("Failed to create nano shell server task."NEW_LINE); } nano_shell_task_stack = tls_mem_alloc(sizeof(u32) * NANO_SHELL_TASK_STK_SIZE); if(nano_shell_task_stack != NULL) { tls_os_task_create( &nano_shell_task_handle, "na_shell", &(nano_shell_loop), NULL, (void*) nano_shell_task_stack, NANO_SHELL_TASK_STK_SIZE * sizeof(u32), 62, 0 ); } shell_printf("Registering netif callback."NEW_LINE); tls_netif_add_status_event(&(tls_netif_status_event_cb)); for(;;) { 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)); } }