#include "kt.h" #include "asm/uart_dev.h" #include "system/event.h" #include "music_player.h" #include "key_event_deal.h" #include "audio_config.h" u8 kt_sys_mute = 0; tRxPacket rx_pack; uart_bus_t *uart_bus; static u8 uart_cbuf[256] __attribute__((aligned(4))); static void uart_isr_hook(void *arg, u32 status) { const uart_bus_t *ubus = arg; struct sys_event e; if (status == UT_RX) { //printf("uart_rx_isr\n"); e.type = SYS_DEVICE_EVENT; e.arg = (void *)DEVICE_EVENT_FROM_UART_RX_OVERFLOW; e.u.dev.event = DEVICE_EVENT_CHANGE; e.u.dev.value = (int)ubus; sys_event_notify(&e); } if (status == UT_RX_OT) { //printf("uart_rx_ot_isr\n"); e.type = SYS_DEVICE_EVENT; e.arg = (void *)DEVICE_EVENT_FROM_UART_RX_OUTTIME; e.u.dev.event = DEVICE_EVENT_CHANGE; e.u.dev.value = (int)ubus; sys_event_notify(&e); } } void kt_uart_init(void) { struct uart_platform_data_t u_arg = {0}; u_arg.tx_pin = KT_UART_TX_PORT; u_arg.rx_pin = KT_UART_RX_PORT; u_arg.rx_cbuf = uart_cbuf; u_arg.rx_cbuf_size = 256; u_arg.frame_length = 128; u_arg.rx_timeout = 50; u_arg.isr_cbfun = uart_isr_hook; u_arg.baud = KT_UART_BAUDRATE; u_arg.is_9bit = 0; uart_bus = uart_dev_open(&u_arg); if (uart_bus != NULL) { printf("uart_dev_open() success\n"); } } static void uart_ack(u8 cmd, u8 err) { if (uart_bus == NULL) { return; } printf("tx_data cmd:%x err:%d \r\n", cmd, err); u8 tx_buf[32] = {0}; int index = 0; tx_buf[index++] = 0x02; tx_buf[index++] = 0x04; tx_buf[index++] = cmd; tx_buf[index++] = err; u8 x = tx_buf[1]; x = x ^ tx_buf[2]; x = x ^ tx_buf[3]; tx_buf[index++] = x; tx_buf[index++] = 0x03; uart_bus->write(tx_buf,index); } char str_play_name[32]; char str_play_name_full[32]; static void uart_process(tRxPacket *pack) { if ((pack->buf[0] != 0x02) || (pack->buf[pack->leng - 1] != 0x03)) { return; } if (pack->buf[1] != pack->leng - 2) { uart_ack(pack->buf[2], KT_ERR_LENGTH); return; } u8 xor = pack->buf[1]; if (pack->buf[pack->leng - 2] != 0xFF) { for (int i = 0; i < pack->buf[1] - 2; i++) { xor = xor ^ pack->buf[2 + i]; } if (xor != pack->buf[pack->leng - 2]) { printf("data err xor = %d x = %d \n", pack->buf[pack->leng - 2], xor); uart_ack(pack->buf[2], KT_ERR_XOR); //err = 7 校验错误 return; } } if (pack->buf[2] == KT_CMD_PLAY) { int sta = music_player_get_play_status(); if (sta == FILE_DEC_STATUS_STOP) { app_task_put_key_msg(KEY_MUSIC_PLAYER_START,0); } else if ((sta == FILE_DEC_STATUS_PAUSE) || (sta == FILE_DEC_STATUS_WAIT_PAUSE) || (sta == FILE_DEC_STATUS_PAUSE_SUCCESS)) { app_task_put_key_msg(KEY_MUSIC_PP,0); } else { } uart_ack(pack->buf[2], KT_OK); } else if (pack->buf[2] == KT_CMD_PAUSE) { int sta = music_player_get_play_status(); if ((sta == FILE_DEC_STATUS_PLAY) || (sta == FILE_DEC_STATUS_WAIT_PLAY)) { app_task_put_key_msg(KEY_MUSIC_PP,0); } uart_ack(pack->buf[2], KT_OK); } else if (pack->buf[2] == KT_CMD_PP) { int sta = music_player_get_play_status(); if (sta == FILE_DEC_STATUS_STOP) { app_task_put_key_msg(KEY_MUSIC_PLAYER_START,0); } else { app_task_put_key_msg(KEY_MUSIC_PP,0); } uart_ack(pack->buf[2], KT_OK); } else if (pack->buf[2] == KT_CMD_NEXT) { app_task_put_key_msg(KEY_MUSIC_NEXT,0); uart_ack(pack->buf[2], KT_OK); } else if (pack->buf[2] == KT_CMD_PREV) { app_task_put_key_msg(KEY_MUSIC_PREV,0); uart_ack(pack->buf[2], KT_OK); } else if (pack->buf[2] == KT_CMD_STOP) { app_task_put_key_msg(KEY_MUSIC_PLAYER_END,0); uart_ack(pack->buf[2], KT_OK); } else if (pack->buf[2] == KT_CMD_VOL_UP) { app_task_put_key_msg(KEY_VOL_UP,0); uart_ack(pack->buf[2], KT_OK); } else if (pack->buf[2] == KT_CMD_VOL_DOWN) { app_task_put_key_msg(KEY_VOL_DOWN,0); uart_ack(pack->buf[2], KT_OK); } else if (pack->buf[2] == KT_CMD_VOL_SET) { app_audio_set_volume(APP_AUDIO_STATE_MUSIC, (s8)(pack->buf[3]), 1); uart_ack(pack->buf[2], KT_OK); } else if (pack->buf[2] == KT_CMD_VOL_MUTE) { printf("KT_CMD_VOL_MUTE \n"); //KT_PA_MUTE(); kt_sys_mute = 1; //KT_PA_MUTE(); uart_ack(pack->buf[2], KT_OK); } else if (pack->buf[2] == KT_CMD_VOL_UNMUTE) { printf("KT_CMD_VOL_UNMUTE \n"); kt_sys_mute = 0; //KT_PA_UNMUTE(); uart_ack(pack->buf[2], KT_OK); } else if (pack->buf[2] == KT_CMD_PLAY_NAME) //0xAB { if (pack->buf[3] == '$') { //char temp[32]; memset(str_play_name,0,32); int i = 0; while (1) { if (pack->buf[4+i] != '$') { str_play_name[i] = pack->buf[4+i]; } else { break; } i++; } printf("%s\n",str_play_name); if (strlen(str_play_name) < 12) { app_task_put_key_msg(KEY_USER_PLAY_NAME, (int)str_play_name); //test demo uart_ack(pack->buf[2], KT_OK); } else { uart_ack(pack->buf[2], KT_ERR_PARA); } } else { uart_ack(pack->buf[2], KT_ERR_PARA); } } else if (pack->buf[2] == KT_CMD_PLAY_FULL_NAME) //0xAC { if (pack->buf[3] == '$') { //char temp[32]; memset(str_play_name_full,0,32); int i = 0; while (1) { if (pack->buf[4+i] != '$') { str_play_name_full[i] = pack->buf[4+i]; } else { break; } i++; } printf("%s\n",str_play_name_full); //023.mp3 if (strlen(str_play_name_full) < 12) { app_task_put_key_msg(KEY_USER_PLAY_FULL_NAME, (int)str_play_name_full); //test demo uart_ack(pack->buf[2], KT_OK); } else { uart_ack(pack->buf[2], KT_ERR_PARA); } } else { uart_ack(pack->buf[2], KT_ERR_PARA); } } else if (pack->buf[2] == KT_CMD_PLAY_NUM) //0xAD { u16 num = 0; num = pack->buf[3]; num = num << 8; num |= pack->buf[4]; app_task_put_key_msg(KEY_USER_PLAY_NUM, num); //test demo uart_ack(pack->buf[2], KT_OK); } else { uart_ack(pack->buf[2], KT_ERR_UNCMD); } } void kt_uart_event(void) { if (uart_bus != NULL) { rx_pack.leng = uart_bus->read(rx_pack.buf, KT_UART_RX_BUF_SIZE, 10); if (rx_pack.leng > 4) { uart_process(&rx_pack); } } }