static s32 usart1_irq_send(u8* buff, u32 buff_len) { u8 BoxErr = 0; //邮箱接收错误标志 u8 tx_data = 0; if(buff_len == 0) { return 0; } kfifo_put(&tx_fifo,buff,buff_len); //有可能再次调用此函数的时候,发送fifo中有未发送完毕的数据 while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); //如果连续调用此函数,等待发送数据寄存器为空,也就是等待上一次发送的数据已经发送完毕 //while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); //如果连续调用此函数,等待发送数据寄存器为空,也就是等待上一次发送的数据已经发送完毕 if( 0 == kfifo_getc( &tx_fifo,&tx_data ) ) { USART1_PUTCHAR(tx_data); USART_ITConfig(USART1, USART_IT_TXE, ENABLE); //开启发送中断 } SysMboxPend(tx_event,tx_timeout_ticks,&BoxErr); if(BoxErr != 0) { return 0; } return buff_len; }
int getc(void) { unsigned char ch; uint64_t start; /* * For 100us we read the characters from the serial driver * into a kfifo. This helps us not to lose characters * in small hardware fifos. */ start = get_time_ns(); while (1) { if (tstc()) { kfifo_putc(console_input_buffer, getc_raw()); start = get_time_ns(); } if (is_timeout(start, 100 * USECOND) && kfifo_len(console_input_buffer)) break; } kfifo_getc(console_input_buffer, &ch); return ch; }
int console_register(struct console_device *newcdev) { struct device_d *dev = &newcdev->class_dev; int first = 0; char ch; strcpy(dev->name, "cs"); dev->type_data = newcdev; register_device(dev); if (newcdev->setbrg) { newcdev->baudrate_param.set = console_baudrate_set; newcdev->baudrate_param.name = "baudrate"; sprintf(newcdev->baudrate_string, "%d", CONFIG_BAUDRATE); console_baudrate_set(dev, &newcdev->baudrate_param, newcdev->baudrate_string); newcdev->baudrate_param.value = newcdev->baudrate_string; dev_add_param(dev, &newcdev->baudrate_param); } newcdev->active_param.set = console_std_set; newcdev->active_param.name = "active"; newcdev->active_param.value = newcdev->active; dev_add_param(dev, &newcdev->active_param); initialized = CONSOLE_INIT_FULL; #ifdef CONFIG_CONSOLE_ACTIVATE_ALL console_std_set(dev, &newcdev->active_param, "ioe"); #endif #ifdef CONFIG_CONSOLE_ACTIVATE_FIRST if (list_empty(&console_list)) { first = 1; console_std_set(dev, &newcdev->active_param, "ioe"); } #endif list_add_tail(&newcdev->list, &console_list); if (console_output_buffer) { while (kfifo_getc(console_output_buffer, &ch) == 0) console_putc(CONSOLE_STDOUT, ch); kfifo_free(console_output_buffer); console_output_buffer = NULL; } #ifndef CONFIG_HAS_EARLY_INIT if (first) display_banner(); #endif return 0; }
int register_console(struct TSerialDevice* dev) { console_dev = dev; if(console_fifo && console_dev){ u8 ch; while(0==kfifo_getc(console_fifo,&ch)){ console_putc(ch); } } return 1; }
static int nc_getc(struct console_device *cdev) { struct nc_priv *priv = container_of(cdev, struct nc_priv, cdev); unsigned char c; while (!kfifo_len(priv->fifo)) net_poll(); kfifo_getc(priv->fifo, &c); return c; }
static int console_std_set(struct device_d *dev, struct param_d *param, const char *val) { struct console_device *cdev = to_console_dev(dev); char active[4]; unsigned int flag = 0, i = 0; if (val) { if (strchr(val, 'i') && cdev->getc) { active[i++] = 'i'; flag |= CONSOLE_STDIN; } if (cdev->putc) { if (strchr(val, 'o')) { active[i++] = 'o'; flag |= CONSOLE_STDOUT; } if (strchr(val, 'e')) { active[i++] = 'e'; flag |= CONSOLE_STDERR; } } } if (flag && !cdev->f_active) { /* The device is being activated, set its baudrate */ if (cdev->setbrg) cdev->setbrg(cdev, cdev->baudrate); } active[i] = 0; cdev->f_active = flag; dev_param_set_generic(dev, param, active); if (initialized < CONSOLE_INIT_FULL) { char ch; initialized = CONSOLE_INIT_FULL; puts_ll("Switch to console ["); puts_ll(dev_name(dev)); puts_ll("]\n"); barebox_banner(); while (kfifo_getc(console_output_fifo, &ch) == 0) console_putc(CONSOLE_STDOUT, ch); } return 0; }
static int console_std_set(struct device_d *dev, struct param_d *param, const char *val) { struct console_device *cdev = to_console_dev(dev); char active[4]; unsigned int flag = 0, i = 0; if (!val) dev_param_set_generic(dev, param, NULL); if (strchr(val, 'i') && cdev->f_caps & CONSOLE_STDIN) { active[i++] = 'i'; flag |= CONSOLE_STDIN; } if (strchr(val, 'o') && cdev->f_caps & CONSOLE_STDOUT) { active[i++] = 'o'; flag |= CONSOLE_STDOUT; } if (strchr(val, 'e') && cdev->f_caps & CONSOLE_STDERR) { active[i++] = 'e'; flag |= CONSOLE_STDERR; } active[i] = 0; cdev->f_active = flag; dev_param_set_generic(dev, param, active); if (initialized < CONSOLE_INIT_FULL) { char ch; initialized = CONSOLE_INIT_FULL; PUTS_LL("Switch to console ["); PUTS_LL(dev_name(dev)); PUTS_LL("]\n"); barebox_banner(); while (kfifo_getc(console_output_fifo, &ch) == 0) console_putc(CONSOLE_STDOUT, ch); } return 0; }
int console_register(struct console_device *newcdev) { struct device_d *dev = &newcdev->class_dev; int first = 0; char ch; if (initialized == CONSOLE_UNINITIALIZED) console_init_early(); dev->id = DEVICE_ID_DYNAMIC; strcpy(dev->name, "cs"); if (newcdev->dev) dev_add_child(newcdev->dev, dev); register_device(dev); if (newcdev->setbrg) { dev_add_param(dev, "baudrate", console_baudrate_set, NULL, 0); dev_set_param(dev, "baudrate", __stringify(CONFIG_BAUDRATE)); } dev_add_param(dev, "active", console_std_set, NULL, 0); initialized = CONSOLE_INIT_FULL; #ifdef CONFIG_CONSOLE_ACTIVATE_ALL dev_set_param(dev, "active", "ioe"); #endif #ifdef CONFIG_CONSOLE_ACTIVATE_FIRST if (list_empty(&console_list)) { first = 1; dev_set_param(dev, "active", "ioe"); } #endif list_add_tail(&newcdev->list, &console_list); while (kfifo_getc(console_output_fifo, &ch) == 0) console_putc(CONSOLE_STDOUT, ch); if (first) barebox_banner(); return 0; }
void USART1_IRQHandler(void) { SysIntEnter(); //任务优先级调度保护 //接收中断触发 if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET) { //数据接收动作 rx_char = USART1_GETCHAR(); #if 0 if( check_data(rx_char) != 0) { rx_char = rx_char; } #endif kfifo_putc( &rx_fifo, rx_char); #if(USART1_RX_MODE==RX_PACKET_MODE) //包数据检测机制的话,复位超时计数器,等待下一个接收的字符 { TIM3->CCR1 = TIM3->CNT + TimeVal; //重新增加超时量 //接收第一个数据时,开启超时中断,接收完成该中断会有定时中断程序关闭 if((TIM3->DIER & TIM_FLAG_CC1) == 0) //如果比较通道使能为关闭 { TIM3->SR = (uint16_t)~TIM_FLAG_CC1; //清空比较通道1的中断标志 TIM3->DIER |= TIM_IT_CC1; //比较通道打开等待下一次中断 } } #elif(USART1_RX_MODE==RX_LENGTH_MODE) { int len = kfifo_len(&rx_fifo); if( len == rx_length ) { rx_length = 0; SysMboxPost(rx_event, (void *)(&rx_char)); //发送激活等待的邮箱信号 } else if(len >= MAX_RX_BUFF_SIZE) { SysMboxPost(rx_event, (void *)(&rx_char)); //发送激活等待的邮箱信号 } } #elif(USART1_RX_MODE==RX_KEY_MODE) { if(rx_char == key_char) { SysMboxPost(rx_event, (void *)&rx_char); //发送激活等待的邮箱信号 } } #endif } //当接收移位寄存器中收完了数据,准备将数据往DR寄存器中放的时候,发现RNXE标志没有被清空,就会触发此中断 else if(USART_GetFlagStatus(USART1, USART_IT_ORE) == SET) //检测是否有接收溢出 { USART1_GETCHAR(); //清接收溢出标志,只能用读数据的方式来清溢出标志 } //发送中断触发 if(USART_GetITStatus(USART1, USART_IT_TXE) == SET) //发送中断 { if(-1 == kfifo_getc(&tx_fifo,&tx_char)) //fifo中没有数据了 { //禁止发送中断 USART_ITConfig(USART1, USART_IT_TXE, DISABLE); if(tx_event) { SysMboxPost(tx_event, (void *)&tx_char); //发送激活等待的邮箱信号 } } else { //还有数据,继续发送fifo中的数据 USART1_PUTCHAR(tx_char); } } SysIntExit(); //任务优先级调度保护解除 }
void USART1_IRQHandler(void) { u16 state; SysIntEnter(); //任务优先级调度保护 //接收中断触发 state = USART1->SR; if((state&0x20) != 0) { //数据接收动作 rx_char = USART1_GETCHAR(); #if 0 if( check_data(rx_char) != 0) { rx_char = rx_char; } #endif kfifo_putc( &rx_fifo, rx_char); #if(RX_MODE==RX_PACKET_MODE) //包数据检测机制的话,复位超时计数器,等待下一个接收的字符 { TIM3->CCR1 = TIM3->CNT + TimeVal; //重新增加超时量 //接收第一个数据时,开启超时中断,接收完成该中断会有定时中断程序关闭 if((TIM3->DIER & TIM_FLAG_CC1) == 0) //如果比较通道使能为关闭 { TIM3->SR = (uint16_t)~TIM_FLAG_CC1; //清空比较通道1的中断标志 TIM3->DIER |= TIM_IT_CC1; //比较通道打开等待下一次中断 } } #elif(USART1_RX_MODE==RX_LENGTH_MODE) { int len = kfifo_len(&rx_fifo); if( len == rx_length ) { rx_length = 0; SysMboxPost(rx_event, (void *)(&rx_char)); //发送激活等待的邮箱信号 } else if(len >= MAX_RX_BUFF_SIZE) { SysMboxPost(rx_event, (void *)(&rx_char)); //发送激活等待的邮箱信号 } } #elif(USART1_RX_MODE==RX_KEY_MODE) { if(rx_char == key_char) { SysMboxPost(rx_event, (void *)&rx_char); //发送激活等待的邮箱信号 } } #endif } else { state = USART1->SR; if( (state&0x08) != 0 ) { USART1_GETCHAR(); //清接收溢出标志,只能用读数据的方式来清溢出标志 } } //发送中断触发 state = USART1->SR; if(( state&0x80 ) != 0 ) { if(-1 == kfifo_getc(&tx_fifo,&ch)) //fifo中没有数据了 { //禁止发送中断 USART_ITConfig(USART1, USART_IT_TXE, DISABLE); if(tx_event) { SysMboxPost(tx_event, (void *)&tx_char); //发送激活等待的邮箱信号 } } else { //还有数据,继续发送fifo中的数据 USART1_PUTCHAR(ch); } } SysIntExit(); //任务优先级调度保护解除 }