| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- #include <rtthread.h>
- #include <rtdevice.h>
- #include <board.h>
- #include <stdio.h>
- #include "string.h"
- #include <rt_fota.h>
- #include "sys_config.h"
- #define LOG_TAG "hmi.f" // 该模块对应的标签。不定义时,默认:NO_TAG
- #define LOG_LVL LOG_LVL_DBG // LOG_LVL_DBG // 该模块对应的日志输出级别。不定义时,默认:调试级别
- #include <ulog.h> // 必须在 LOG_TAG 与 LOG_LVL 下面
- #define TOUCH_P GET_PIN(A, 15)
- #define HIM_POWER_PIN GET_PIN(C, 8)
- /* 定时器的控制块 */
- rt_timer_t HIM_POWER_PIN_TIMER;
- int HIM_POWER_CNT = 0;
- static char dis_buf[100]; /*显示缓冲区*/
- static uint8_t GET_PAGE_ID[]="sendme"; /*获取页面ID*/
- /* 消息队列控制块 */
- static struct rt_messagequeue fota_status;
- static struct rt_messagequeue fota_progress_bar;
- /* 消息队列中用到的放置消息的内存池 */
- static rt_uint8_t msg_pool[512];
- static rt_uint8_t msg_pool_1[256];
- /* 事件控制块 */
- struct rt_event hmi_event;
- /* HMI显示功能函数 */
- extern rt_err_t HMI_Display(void *Data, rt_size_t length);
- extern rt_err_t HMI_Display_urgent(void *Data, rt_size_t length);
- extern void usb_ota(uint8_t argc, char **argv);
- extern uint8_t CH376_check(void);
- #define app_timeout 66
- uint16_t inter_app_timeout_cnt = app_timeout;//超时进入应用计数,30ms时基延时,20s超时时间
- /*HMI整形显示功能函数*/
- void Display_fun(const char *position,uint16_t value1,uint16_t value2)
- {
- rt_memset(dis_buf,'\0',100);
- if(value1 == NULL)
- {
- rt_sprintf(dis_buf,position,NULL);
- }
- else
- rt_sprintf(dis_buf,position,value1);
- if(value2 == 1)
- HMI_Display_urgent(dis_buf,rt_strlen(dis_buf));//存入队列
- else if(value2 == NULL)
- HMI_Display(dis_buf,rt_strlen(dis_buf));//存入队列
- }
- /*HMI字符串显示功能函数*/
- void Display_fun_str(const char *position,char *str)
- {
- rt_memset(dis_buf,'\0',100);
- if(str == NULL)
- {
- rt_sprintf(dis_buf,position,NULL);
- }
- else
- rt_sprintf(dis_buf,position,str);
- HMI_Display(dis_buf,rt_strlen(dis_buf));//存入队列
- }
- extern unsigned char Read_CMD[256]; //一帧信息的缓冲区
- extern unsigned char Check_CMD(void);
- //HMI返回数据解码
- uint8_t HMI_Decode(void)//(unsigned char *Read_CMD)
- {
- static uint32_t i;
- char buf[3];
- extern uint8_t firstime_flag;
- if(Check_CMD() == 1)
- {
- switch(Read_CMD[0])
- {
- case 0x12://12 00 FF FF FF // bootloader
- switch(Read_CMD[1])
- {
- case 0x00:
- fota_status_send("即将从U盘写入固件", sizeof("即将从U盘写入固件"));
- rt_thread_mdelay(1000);
- if(CH376_check() == 1)
- usb_ota(RT_NULL,RT_NULL);
- break;
- case 0x01:
- fota_status_send("正在恢复出厂固件", sizeof("正在恢复出厂固件"));
- rt_thread_mdelay(1000);
- upgrade_df();
- break;
- case 0x02:
- fota_status_send("即将进入系统", sizeof("即将进入系统"));
- rt_thread_mdelay(1000);
- rt_fota_start_application();
- break;
- case 0x03:
- fota_status_send("正在重启", sizeof("正在重启"));
- rt_hw_cpu_reset();
- break;
- case 0x04:
- firstime_flag = 0;//强制标志位置零
- firstime_clone_df();//覆盖出厂固件
- break;
- default: break;
- }
- break;
- case 0x66://66 12 00 FF FF FF // bootloader
- if(Read_CMD[1] == 0x12)
- {
- char cache[60] = {0};
- sprintf(cache , "bootloader.t3.txt=\"%s %s\"" ,IsReady , Version);
- Display_fun(cache,NULL,NULL);
- Display_fun("t100.txt=\"0\"",NULL,NULL);
- Display_fun("j100.val=0",NULL,NULL);
- }
- break;
- default: break;
- }
- inter_app_timeout_cnt = app_timeout;
- }
- }
- void fota_status_send(const char *data,uint8_t len)
- {
- int result;
- /* 发送消息到消息队列中 */
- //rt_kprintf("rt_mq_send MARK A\n");
- result = rt_mq_send(&fota_status,data, len);
- if (result != RT_EOK)
- {
- rt_kprintf("fota_status_send ERR\n");
- }
- }
- void fota_progress_bar_send(unsigned char data)
- {
- int result;
- /* 发送消息到消息队列中 */
- //rt_kprintf("rt_mq_send MARK B\n");
- result = rt_mq_send(&fota_progress_bar,&data, 1);
- if (result != RT_EOK)
- {
- rt_kprintf("fota_progress_bar_send ERR\n");
- }
- }
- /*HMI协议解码线程*/
- static void Hmi_decord_thread_entry(void *parameter)
- {
- while(1)
- {
- HMI_Decode();
- inter_app_timeout_cnt --;
- Display_fun("bootloader.t9.txt=\"%d\"",(inter_app_timeout_cnt / 2),NULL);
- if((inter_app_timeout_cnt == 0) || (inter_app_timeout_cnt > app_timeout)) //超出设置范围进入应用模式
- {
- rt_kprintf("rt_fota_start_application\r\n");
- rt_fota_start_application();
- }
- rt_thread_mdelay(500);
- }
- }
- /*HMI刷新线程*/
- static void Hmi_flash_thread_entry(void *parameter)
- {
- rt_uint32_t e;
- char *mq_buf = rt_malloc(100); /*显示缓冲区*/
- uint8_t buf = 0;
- char buff[3] = "0";
- while(1)
- {
- if(rt_event_recv(&hmi_event, SWITCH_HMI,RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,0, &e) == RT_EOK)
- {
- rt_pin_write(HIM_POWER_PIN, PIN_HIGH);//保证屏幕上电状态
- rt_thread_mdelay(100);
- Display_fun("sleep=0",NULL,1);//如果休眠则唤醒
- rt_thread_mdelay(30);
- Display_fun("page bootloader",NULL,1);
- Display_fun("sendme",NULL,NULL);
- }
- /* 从消息队列中接收消息 */
- if(rt_mq_recv(&fota_status, mq_buf, 100, 0) == RT_EOK)
- {
- Display_fun_str("bootloader.t3.txt=\"%.20s\"","\0");
- Display_fun_str("bootloader.t3.txt=\"%s\"",mq_buf);
- rt_memset(mq_buf,'\0',100);
- }
- /* 从消息队列中接收消息 */
- if(rt_mq_recv(&fota_progress_bar, &buf, 1, 0) == RT_EOK)
- {
- sprintf(buff,"%d",buf);
- Display_fun_str("t100.txt=\"%s\"",buff);
- Display_fun("j100.val=%d",buf,NULL);
- }
- rt_thread_mdelay(10);
- }
- }
- // HMI页面刷新线程
- static int HMI_thread(int argc, char *argv[])
- {
- rt_err_t ret = RT_EOK;
- /* 初始化事件对象 */
- ret = rt_event_init(&hmi_event, "event", RT_IPC_FLAG_FIFO);
- if (ret != RT_EOK)
- {
- rt_kprintf("init event failed.\n");
- return -1;
- }
- /* 初始化消息队列 */
- ret = rt_mq_init(&fota_status,
- "fota_status",
- &msg_pool[0], /* 内存池指向 msg_pool */
- 100, /* 每个消息的大小是 20 字节 */
- sizeof(msg_pool), /* 内存池的大小是 msg_pool 的大小 */
- RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
- if (ret != RT_EOK)
- {
- rt_kprintf("init message queue failed.\n");
- return -1;
- }
- ret = rt_mq_init(&fota_progress_bar,
- "fota_progress_bar",
- &msg_pool_1[0], /* 内存池指向 msg_pool */
- 1, /* 每个消息的大小是 20 字节 */
- sizeof(msg_pool_1), /* 内存池的大小是 msg_pool 的大小 */
- RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
- if (ret != RT_EOK)
- {
- rt_kprintf("init message queue failed.\n");
- return -1;
- }
- /* 创建 serial 线程 */
- rt_thread_t thread1 = rt_thread_create("Hmi_decord", Hmi_decord_thread_entry, RT_NULL, 2048, 21, 10);//栈空间不能过小,重启需要载入参数受影响
- /* 创建成功则启动线程 */
- if (thread1 != RT_NULL)
- {
- rt_thread_startup(thread1);
- }
- else
- {
- ret = RT_ERROR;
- }
- rt_thread_t thread2 = rt_thread_create("Hmi_flash", Hmi_flash_thread_entry, RT_NULL, 768, 20, 10);
- /* 创建成功则启动线程 */
- if (thread2 != RT_NULL)
- {
- rt_thread_startup(thread2);
- }
- else
- {
- ret = RT_ERROR;
- }
- return ret;
- }
- /* 定时器 1 超时函数 */
- static void HIM_POWER_CTL(void *parameter)
- {
- if(rt_pin_read(HIM_POWER_PIN) == 1)//如果电源控制脚为高表示屏幕上电则开始倒计时
- {
- HIM_POWER_CNT --;
- if(HIM_POWER_CNT == 0)
- {
- rt_pin_write(HIM_POWER_PIN, PIN_LOW);//计时时间到,关闭屏幕电源
- rt_timer_stop(HIM_POWER_PIN_TIMER);
- }
- }
- }
- void touch_p(void *args)
- {
- rt_pin_write(HIM_POWER_PIN, PIN_HIGH);
- log_w("touch_p testing............");
- HIM_POWER_CNT = 60;//给计数变量赋值60s
- rt_timer_start(HIM_POWER_PIN_TIMER);
- }
- static void touchpad_irq(void)
- {
- /* 触摸引脚为输出模式 */
- rt_pin_mode(15, PIN_MODE_INPUT_PULLUP);
- /* 默认高电平 */
- rt_pin_write(15, PIN_HIGH);
- /* 绑定中断,下降沿模式,回调函数名为touch_p */
- rt_pin_attach_irq(15, PIN_IRQ_MODE_FALLING, touch_p, RT_NULL);
- /* 使能中断 */
- rt_pin_irq_enable(15, PIN_IRQ_ENABLE);
- }
- int hmi_power_timer(void)
- {
- rt_pin_mode(HIM_POWER_PIN, PIN_MODE_OUTPUT);
- rt_pin_write(HIM_POWER_PIN, PIN_HIGH);//上电开机开启屏幕
- HIM_POWER_CNT = 60;//给计数变量赋值60s
- /* 创建定时器 1 周期定时器 */
- HIM_POWER_PIN_TIMER = rt_timer_create("hmi_p_timer", HIM_POWER_CTL,
- RT_NULL, 1000,
- RT_TIMER_FLAG_PERIODIC);
- /* 启动定时器 1 */
- if (HIM_POWER_PIN_TIMER != RT_NULL) rt_timer_start(HIM_POWER_PIN_TIMER);
- return 0;
- }
- INIT_COMPONENT_EXPORT(HMI_thread);
- INIT_COMPONENT_EXPORT(touchpad_irq);
- INIT_COMPONENT_EXPORT(hmi_power_timer);
|