void rt_can_thread_entry(void *parameter) { struct rt_can_msg msg; struct can_app_struct *canpara = (struct can_app_struct *) parameter; rt_device_t candev; rt_uint32_t e; candev = rt_device_find(canpara->name); RT_ASSERT(candev); rt_event_init(&canpara->event, canpara->name, RT_IPC_FLAG_FIFO); rt_device_open(candev, (RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX)); rt_device_control(candev, RT_CAN_CMD_SET_FILTER, canpara->filter); while (1) { if ( rt_event_recv(&canpara->event, ((1 << canpara->filter->items[0].hdr) | (1 << canpara->filter->items[1].hdr) | (1 << canpara->filter->items[2].hdr) | (1 << canpara->filter->items[3].hdr)), canpara->eventopt, RT_WAITING_FOREVER, &e) != RT_EOK ) { continue; } if (e & (1 << canpara->filter->items[0].hdr)) { msg.hdr = canpara->filter->items[0].hdr; while (rt_device_read(candev, 0, &msg, sizeof(msg)) == sizeof(msg)) { rt_device_write(candev, 0, &msg, sizeof(msg)); } } if (e & (1 << canpara->filter->items[1].hdr)) { msg.hdr = canpara->filter->items[1].hdr; while (rt_device_read(candev, 0, &msg, sizeof(msg)) == sizeof(msg)) { rt_device_write(candev, 0, &msg, sizeof(msg)); } } if (e & (1 << canpara->filter->items[2].hdr)) { msg.hdr = canpara->filter->items[2].hdr; while (rt_device_read(candev, 0, &msg, sizeof(msg)) == sizeof(msg)) { rt_device_write(candev, 0, &msg, sizeof(msg)); } } if (e & (1 << canpara->filter->items[3].hdr)) { msg.hdr = canpara->filter->items[3].hdr; while (rt_device_read(candev, 0, &msg, sizeof(msg)) == sizeof(msg)) { rt_device_write(candev, 0, &msg, sizeof(msg)); } } } }
//串口接收数据线程 void uart_thread_entry(void* parameter) { char ch; device = rt_device_find("uart3"); if(device != RT_NULL) { rt_device_open(device, RT_DEVICE_OFLAG_RDWR); rt_kprintf("open device uart3 succeed!\r\n"); rt_sem_init(&rx_sem, "uartrx", 0, 0); rt_device_set_rx_indicate(device, uart_rx_ind); while(1) { if (rt_sem_take(&rx_sem, RT_WAITING_FOREVER) != RT_EOK) //默认情况线程挂起,有数据时,系统会调用uart_rx_ind函数,释放信号量,线程得以执行 continue; while(rt_device_read(device, 0, &ch, 1) == 1) { uartRecvProc(ch); } } } }
/* 监视GPRS串口线程入口*/ void gprswatch_entry(void* parameter) { rt_err_t result = RT_EOK; rt_uint32_t event; char gprs_rx_buffer[512]={0x00}; while(1) { result = rt_event_recv(&rev_event, REV_MASK, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &event); if (result == RT_EOK) { if (event & REV_DATA) { rt_memset(gprs_rx_buffer,0x00,sizeof(gprs_rx_buffer)); rt_thread_delay(RT_TICK_PER_SECOND*2); rt_device_read(gprs_device, 0, gprs_rx_buffer, 512); rt_kprintf(gprs_rx_buffer); } if (event & REV_STOPWATCH) { return; } } } }
rt_int16_t zread_line(rt_uint16_t timeout) { char *str; static char buf[10]; if (Line_left > 0) { Line_left -= 1; return (*str++ & 0377); } Line_left = 0; timeout/=5; while (1) { // if (rt_sem_take(&zmodem.zsem, RT_TICK_PER_SECOND*timeout) != RT_EOK) continue; Line_left = rt_device_read(shell->device, 0, buf, 1); if (Line_left) { Line_left = Line_left; str = buf; break; } } if (Line_left < 1) return TIMEOUT; Line_left -=1; return (*str++ & 0377); }
void rt_rs485_thread_entry(void* parameter) { char ch; while (1) { /* wait receive */ if (rt_sem_take(&uart1_dev_my->rx_sem, RT_WAITING_FOREVER) != RT_EOK) continue; /* read one character from device */ while (rt_device_read(uart1_dev_my->device, 0, &ch, 1) == 1) { #if 0 u8 datatmp; datatmp = ch; rs485_send_data(&datatmp, 1); #endif pelco_rx_isr(ch); } /* end of device read */ } }
int rcu_uart_read(char* buf, int bufLen) { int ret = 0,rlen=0; int flag=0; char ch=0; char* p = buf; if(p == NULL) { return -1; } while(1) { if (rt_sem_take(&rcuReadSem, RT_WAITING_FOREVER) != RT_EOK) return ret; while(rt_device_read(rcuDevice, 0, &ch, 1)==1) { hclog("rcu_uart_read()--ch:0x%02x \n",ch); if(ch == '\r') { flag = 1; //break; } else if(flag==1) { if(ch=='\n') { rlen--; flag=2; break; } else { flag=0; } } if(rlen<bufLen-1) { *p = ch; p++; } rlen++; //if(ch == 0XCE) //{ // flag = 2; // break; //} } if(flag==2) break; } ret = rlen; if(rlen<bufLen) buf[rlen]=0; return ret; }
static rt_bool_t getc(char * ch) { if((rt_sem_take(&remote_sem,RT_TICK_PER_SECOND/2))==RT_EOK) { rt_device_read(uart,0,ch,1); debug("%02X ",*ch); return RT_TRUE; } return RT_FALSE; }
/* start zmodem receive proccess */ void zr_start(char *path) { struct zfile *zf; rt_uint8_t n; char ch,*p,*q; rt_err_t res = -RT_ERROR; zf = rt_malloc(sizeof(struct zfile)); if (zf == RT_NULL) { rt_kprintf("zf: out of memory\r\n"); return; } memset(zf, 0, sizeof(struct zfile)); zf->fname = path; zf->fd = -1; res = zrec_files(zf); p = zf->fname; for (;;) { q = strstr(p,"/"); if (q == RT_NULL) break; p = q+1; } if (res == RT_EOK) { rt_kprintf("\b\b\bfile: %s \r\n",p); rt_kprintf("size: %ld bytes\r\n",zf->bytes_received); rt_kprintf("receive completed.\r\n"); close(zf->fd); rt_free(zf->fname); } else { rt_kprintf("\b\b\bfile: %s \r\n",p); rt_kprintf("size: 0 bytes\r\n"); rt_kprintf("receive failed.\r\n"); if (zf->fd >= 0) { close(zf->fd); unlink(zf->fname); /* remove this file */ rt_free(zf->fname); } } rt_free(zf); /* waiting,clear console buffer */ rt_thread_delay(RT_TICK_PER_SECOND/2); while(1) { n=rt_device_read(shell->device, 0, &ch, 1); if (n == 0) break; } return ; }
u32_t sio_read(sio_fd_t fd, u8_t *buf, u32_t size) { u32_t len; assert(fd != NULL); len = rt_device_read((rt_device_t)fd, 0, buf, size); if (len <= 0) return 0; return len; }
u32_t sio_read(sio_fd_t fd, u8_t *buf, u32_t size) { u32_t len; RT_ASSERT(fd != RT_NULL); len = rt_device_read((rt_device_t)fd, 0, buf, size); if (len <= 0) return 0; return len; }
/* Read Sector(s) */ DRESULT disk_read(BYTE drv, BYTE *buff, DWORD sector, BYTE count) { rt_size_t result; rt_device_t device = disk[drv]; result = rt_device_read(device, sector, buff, count); if (result == count) { return RES_OK; } return RES_ERROR; }
static char finsh_getchar(void) { #ifdef RT_USING_POSIX return getchar(); #else char ch; RT_ASSERT(shell != RT_NULL); while (rt_device_read(shell->device, -1, &ch, 1) != 1) rt_sem_take(&shell->rx_sem, RT_WAITING_FOREVER); return ch; #endif }
static void oled_readData(rt_uint8_t *data, rt_uint8_t size) { rt_uint8_t buf_read[5], ret; /* Build instruction buffer */ buf_read[0] = 0x00; *(rt_uint8_t **)(&buf_read[1]) = data; MINISTM32_OLED_CS_RESET; if ((ret = rt_device_read(spi_dev, 1, buf_read, size)) == 0) { oled_debug("OLED: Read data failed! (%d, %x %x %x %x %x)\n", ret, *data, *(data + 1), *(data + 2), *(data + 3), *(data + 4)); } MINISTM32_OLED_CS_SET; }
int dfs_device_fs_read(struct dfs_fd* file, void *buf, rt_size_t count) { int result; rt_device_t dev_id; RT_ASSERT(file != RT_NULL); /* get device handler */ dev_id = (rt_device_t)file->data; RT_ASSERT(dev_id != RT_NULL); /* read device data */ result = rt_device_read(dev_id, file->pos, buf, count); file->pos += result; return result; }
static void recv_thread_entry(void* parameter) { rt_uint32_t ev =0; rt_err_t ret = RT_EOK; rt_uint16_t recv_len = 0; while(1) { ret = rt_event_recv(cmd.recv_event,0x01,RT_EVENT_FLAG_AND|RT_EVENT_FLAG_CLEAR,RT_WAITING_FOREVER,&ev); if(ret == RT_EOK) { recv_len = rt_device_read(cmd.dev,0,cmd.recv_buf,1024); if (recv_len > 0) { int index =0; while(index <(recv_len-4)) { if((cmd.recv_buf[index]==0xaa)&&(cmd.recv_buf[index+1]==0xaf)&&(cmd.recv_buf[index+2]>0)&&(cmd.recv_buf[index+2]<0xf1)) { if(cmd.recv_buf[index+3]<50) { uint8_t cmd_len = cmd.recv_buf[index+3]; if(cmd_len+index<recv_len) { cmd.Data_Receive_Anl( &cmd.recv_buf[index],cmd_len+5); index+=cmd_len+5; } else { index++; } } else { index+=4; } } else { index++; } } } } } }
void dump_ee(void) { rt_device_t dev; char buf[EE_MEM_SIZE]; int i, j; dev = rt_device_find("eeprom"); rt_device_read(dev, 0, buf, EE_MEM_SIZE ); for (i = 0; i < 16; i++) { for (j = 0; j < 16; j++) { rt_kprintf("0x%02X ", buf[ i*16+ j]); } rt_kprintf("\n"); } }
/* Return: 0: IO Voltage is 0-0.5V 1: IO Voltage is 2.8-3.3V -1: IO Voltage is 0.8-1.8V */ int check_io_voltage(rt_device_t device) { rt_uint32_t data[32], i; rt_uint32_t cnt=8; int ret; rt_device_read(device, 0, data, cnt); rt_kprintf("ADC TEST: \n"); for(i=0; i<cnt; i++) { if((!(i%4)) && i) { rt_kprintf("\n"); } rt_kprintf("%d\t", data[i]); if(data[i] > 2800) { ret=1; break; } else if(data[i]<500) { ret=0; break; } else if(data[i]> 800 && data[i] < 2000) { ret=-1; } else { ret=-2; break; } } rt_kprintf("\n"); rt_kprintf("check_io_voltage ret=%d\n", ret); return ret; }
static void led_thread_entry(void *parameter) { rt_device_t led_dev; rt_device_t vbus_dev; rt_err_t err; rt_led_hw_init(); led_dev = rt_device_find("led"); if (led_dev == RT_NULL) { rt_kprintf("can not find the led device\n"); return; } vbus_dev = rt_device_find("vecho"); if (vbus_dev == RT_NULL) { rt_kprintf("can not find the vbus device\n"); return; } err = rt_device_open(vbus_dev, RT_DEVICE_OFLAG_RDWR); if (err != RT_EOK) { rt_kprintf("open vbus failed: %d\n", err); return; } while (1) { rt_uint8_t led_value; int len; len = rt_device_read(vbus_dev, 0, &led_value, sizeof(led_value)); if (len <= 0) { rt_kprintf("vbus read err: %d, %d\n", len, rt_get_errno()); } led_dev->write(led_dev, 1, &led_value, sizeof(led_value)); } }
void rt_can_thread_entry(void* parameter) { struct rt_can_msg msg; struct can_app_struct* canpara = (struct can_app_struct*) parameter; rt_device_t candev; candev = rt_device_find(canpara->name); RT_ASSERT(candev); rt_sem_init(&canpara->sem, canpara->name, 0, RT_IPC_FLAG_FIFO); rt_device_open(candev, (RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX)); rt_device_control(candev,RT_CAN_CMD_SET_FILTER,canpara->filter); rt_device_set_rx_indicate(candev, lpccanind); while(1) { rt_sem_take(&canpara->sem, RT_WAITING_FOREVER); while (rt_device_read(candev, 0, &msg, sizeof(msg)) == sizeof(msg)) { rt_device_write(candev, 0, &msg, sizeof(msg)); } } }
/* polling */ int gdb_uart_getc() { int ch; #ifdef RT_USING_SERIAL ch = -1; do { ch = gdb_serial->ops->getc(gdb_serial); } while (ch == -1); #else rt_device_read(gdb_dev, 0, &ch, 1); #endif #ifdef RT_GDB_DEBUG rt_kprintf("%c",ch); #endif return ch; }
static Ret ftk_source_input_dispatch(FtkSource* thiz) { char ch; int ret = 0; DECL_PRIV(thiz, priv); while(rt_device_read(priv->device, 0, &ch, 1) == 1) { priv->event.type = (ch & 0x80) ? FTK_EVT_KEY_UP : FTK_EVT_KEY_DOWN; priv->event.u.key.code = s_key_map[ch & 0x7F]; if(priv->on_event != NULL && priv->event.type != FTK_EVT_NOP) { priv->on_event(priv->user_data, &priv->event); priv->event.type = FTK_EVT_NOP; } } return RET_OK; }
DRESULT disk_read_USB ( BYTE drv, /* Physical drive nmuber (0..) */ BYTE *buff, /* Data buffer to store read data */ DWORD sector, /* Sector address (LBA) */ BYTE count /* Number of sectors to read (1..255) */ ) { rt_size_t result; if(!diskinited) { return RES_ERROR; } result = rt_device_read(&mscdev, sector, buff, count); if (result == count) { return RES_OK; } return RES_ERROR; }
void ee_reset(void) { char buf[EE_MEM_SIZE], read[EE_MEM_SIZE]; int i; rt_device_t dev = rt_device_find("eeprom"); for (i = 0; i < EE_MEM_SIZE; i++) { buf[i] = 0xFF; read[i] = 0; } if (rt_device_write(dev, 0, buf, EE_MEM_SIZE ) == EE_MEM_SIZE) rt_kprintf("Write Success\n"); rt_device_read(dev, 0, read, EE_MEM_SIZE ); for (i = 0; i < EE_MEM_SIZE; i++) { if (buf[i] != read[i]) rt_kprintf("EE Failed %X != %X at %d\n", buf[i], read[i], i); } }
/*GPRS串口发送和接收*/ rt_bool_t gprs_send_data_package(char *cmd,char *ack,uint16_t waittime, uint8_t retrytime) { rt_bool_t res = RT_FALSE; rt_err_t result = RT_EOK; rt_uint32_t event; char gprs_rx_buffer[512]={0x00}; rt_thread_t thread; thread = rt_thread_find("gprswatch"); if( thread != RT_NULL) rt_thread_delete(thread); do { rt_device_write(gprs_device, 0, cmd, rt_strlen(cmd)); result = rt_event_recv(&rev_event, REV_MASK, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, waittime*RT_TICK_PER_SECOND, &event); if (result == RT_EOK) { if (event & REV_DATA) { rt_memset(gprs_rx_buffer,0x00,sizeof(gprs_rx_buffer)); rt_thread_delay(RT_TICK_PER_SECOND*2); rt_device_read(gprs_device, 0, gprs_rx_buffer, 512); rt_kprintf(gprs_rx_buffer); if((rt_strstr(gprs_rx_buffer,ack))||(rt_strstr(gprs_rx_buffer,"OK"))) res = RT_TRUE; else res = RT_FALSE; } } retrytime--; }while((!res)&&(retrytime>=1)); gprswatch(); return res; }
void finsh_thread_entry(void* parameter) { char ch; /* normal is echo mode */ shell->echo_mode = 1; finsh_init(&shell->parser); rt_kprintf(FINSH_PROMPT); /* set console device as shell device */ shell->device = rt_console_get_device(); if (shell->device != RT_NULL) { rt_device_open(shell->device, RT_DEVICE_OFLAG_RDWR); rt_device_set_rx_indicate(shell->device, finsh_rx_ind); } while (1) { /* wait receive */ if (rt_sem_take(&shell->rx_sem, RT_WAITING_FOREVER) != RT_EOK) continue; /* read one character from device */ while (rt_device_read(shell->device, 0, &ch, 1) == 1) { /* * handle control key * up key : 0x1b 0x5b 0x41 * down key: 0x1b 0x5b 0x42 * right key:0x1b 0x5b 0x43 * left key: 0x1b 0x5b 0x44 */ if (ch == 0x1b) { shell->stat = WAIT_SPEC_KEY; continue; } else if (shell->stat == WAIT_SPEC_KEY) { if (ch == 0x5b) { shell->stat = WAIT_FUNC_KEY; continue; } shell->stat = WAIT_NORMAL; } else if (shell->stat == WAIT_FUNC_KEY) { shell->stat = WAIT_NORMAL; if (ch == 0x41) /* up key */ { #ifdef FINSH_USING_HISTORY /* prev history */ if (shell->current_history > 0) shell->current_history --; else { shell->current_history = 0; continue; } /* copy the history command */ memcpy(shell->line, &shell->cmd_history[shell->current_history][0], FINSH_CMD_SIZE); shell->line_curpos = shell->line_position = strlen(shell->line); finsh_handle_history(shell); #endif continue; } else if (ch == 0x42) /* down key */ { #ifdef FINSH_USING_HISTORY /* next history */ if (shell->current_history < shell->history_count - 1) shell->current_history ++; else { /* set to the end of history */ if (shell->history_count != 0) shell->current_history = shell->history_count - 1; else continue; } memcpy(shell->line, &shell->cmd_history[shell->current_history][0], FINSH_CMD_SIZE); shell->line_curpos = shell->line_position = strlen(shell->line); finsh_handle_history(shell); #endif continue; } else if (ch == 0x44) /* left key */ { if (shell->line_curpos) { rt_kprintf("\b"); shell->line_curpos --; } continue; } else if (ch == 0x43) /* right key */ { if (shell->line_curpos < shell->line_position) { rt_kprintf("%c", shell->line[shell->line_curpos]); shell->line_curpos ++; } continue; } } /* handle CR key */ if (ch == '\r') { char next; if (rt_device_read(shell->device, 0, &next, 1) == 1) ch = next; else ch = '\r'; } /* handle tab key */ else if (ch == '\t') { int i; /* move the cursor to the beginning of line */ for (i = 0; i < shell->line_curpos; i++) rt_kprintf("\b"); /* auto complete */ finsh_auto_complete(&shell->line[0]); /* re-calculate position */ shell->line_curpos = shell->line_position = strlen(shell->line); continue; } /* handle backspace key */ else if (ch == 0x7f || ch == 0x08) { /* note that shell->line_curpos >= 0 */ if (shell->line_curpos == 0) continue; shell->line_position--; shell->line_curpos--; if (shell->line_position > shell->line_curpos) { int i; rt_memmove(&shell->line[shell->line_curpos], &shell->line[shell->line_curpos + 1], shell->line_position - shell->line_curpos); shell->line[shell->line_position] = 0; rt_kprintf("\b%s \b", &shell->line[shell->line_curpos]); /* move the cursor to the origin position */ for (i = shell->line_curpos; i <= shell->line_position; i++) rt_kprintf("\b"); } else { rt_kprintf("\b \b"); shell->line[shell->line_position] = 0; } continue; } /* handle end of line, break */ if (ch == '\r' || ch == '\n') { #ifdef FINSH_USING_MSH if (msh_is_used() == RT_TRUE && shell->line_position != 0) { rt_kprintf("\n"); msh_exec(shell->line, shell->line_position); #ifdef FINSH_USING_HISTORY finsh_push_history(shell); #endif } else #endif { /* add ';' and run the command line */ shell->line[shell->line_position] = ';'; #ifdef FINSH_USING_HISTORY finsh_push_history(shell); #endif if (shell->line_position != 0) finsh_run_line(&shell->parser, shell->line); else rt_kprintf("\n"); } rt_kprintf(FINSH_PROMPT); memset(shell->line, 0, sizeof(shell->line)); shell->line_curpos = shell->line_position = 0; break; } /* it's a large line, discard it */ if (shell->line_position >= FINSH_CMD_SIZE) shell->line_position = 0; /* normal character */ if (shell->line_curpos < shell->line_position) { int i; rt_memmove(&shell->line[shell->line_curpos + 1], &shell->line[shell->line_curpos], shell->line_position - shell->line_curpos); shell->line[shell->line_curpos] = ch; if (shell->echo_mode) rt_kprintf("%s", &shell->line[shell->line_curpos]); /* move the cursor to new position */ for (i = shell->line_curpos; i < shell->line_position; i++) rt_kprintf("\b"); } else { shell->line[shell->line_position] = ch; rt_kprintf("%c", ch); } ch = 0; shell->line_position ++; shell->line_curpos++; } /* end of device read */ } }
void usbd_msc_read_sect (U32 block, U8 *buf, U32 num_of_blocks) { rt_device_read(dev, block, buf, num_of_blocks); }
void thread_gps_upgrade_uart( void* parameter ) { #define BD_SYNC_40 0 #define BD_SYNC_0D 1 #define BD_SYNC_0A 2 /*定义一个函数指针,用作结果处理 */ void ( *msg )( void *p ); unsigned int resultcode; rt_uint8_t buf[256]; rt_uint8_t info[32]; rt_uint8_t *p; rt_uint16_t count = 0; rt_uint16_t i; rt_size_t len; rt_uint32_t baud = 9600; rt_uint16_t packetnum = 0; rt_uint8_t bd_packet_status = BD_SYNC_40; /*北斗升级报文接收状态*/ rt_uint8_t last_char = 0x0; rt_tick_t last_sendtick = 0; /*北斗更新时记录收到应答的时刻*/ msg = parameter; ptr_mem_packet = rt_malloc( 1200 ); if( ptr_mem_packet == RT_NULL ) { resultcode = BDUPG_RES_RAM; msg( "E内存不足" ); return; } flag_bd_upgrade_uart = 1; dev_vuart.flag &= ~RT_DEVICE_FLAG_STREAM; rt_device_control( &dev_vuart, 0x03, &baud ); p = ptr_mem_packet; while( 1 ) { if( ( last_sendtick > 0 ) && ( rt_tick_get( ) - last_sendtick > RT_TICK_PER_SECOND * 12 ) ) { /*升级程序发送数据,收到应答,再次发送数据。超时10s*/ resultcode = BDUPG_RES_TIMEOUT; msg( "E超时错误" ); goto end_upgrade_uart_memfree; } while( ( len = rt_device_read( &dev_vuart, 0, buf, 256 ) ) > 0 ) { for( i = 0; i < len; i++ ) { switch( bd_packet_status ) { case BD_SYNC_40: if( buf[i] == 0x40 ) { *p++ = 0x40; bd_packet_status = BD_SYNC_0A; count = 1; } break; case BD_SYNC_0A: if( ( buf[i] == 0x0a ) && ( last_char == 0x0d ) ) { *p = 0x0a; count++; dev_gps_write( &dev_gps, 0, ptr_mem_packet, count ); packetnum++; /*显示传递的包数*/ sprintf( info, "I发送第%d包", packetnum ); msg( info ); last_sendtick = rt_tick_get( ); if( memcmp( ptr_mem_packet, "\x40\x41\xc0", 3 ) == 0 ) /*修改波特率*/ { baud = ( *( ptr_mem_packet + 4 ) << 24 ) | ( *( ptr_mem_packet + 5 ) << 16 ) | ( *( ptr_mem_packet + 6 ) << 8 ) | *( ptr_mem_packet + 7 ); gps_baud( baud ); uart1_baud( baud ); } if( memcmp( ptr_mem_packet, "\x40\x34\xc0", 3 ) == 0 ) /*模块软件复位*/ { resultcode = 0; msg( "E更新完成" ); /*通知lcd显示完成*/ goto end_upgrade_uart_memfree; } p = ptr_mem_packet; bd_packet_status = BD_SYNC_40; }else { *p++ = buf[i]; count++; } break; } last_char = buf[i]; } } rt_thread_delay( RT_TICK_PER_SECOND / 50 ); } end_upgrade_uart_memfree: rt_free( ptr_mem_packet ); ptr_mem_packet = RT_NULL; //end_upgrade_uart: baud = 115200; uart1_baud( baud ); flag_bd_upgrade_uart = 0; }
static rt_err_t _block_device_test(rt_device_t device) { rt_err_t result; struct rt_device_blk_geometry geometry; rt_uint8_t * read_buffer = RT_NULL; rt_uint8_t * write_buffer = RT_NULL; rt_kprintf("\r\n"); if( (device->flag & RT_DEVICE_FLAG_RDWR) == RT_DEVICE_FLAG_RDWR ) { // device can read and write. // step 1: open device result = rt_device_open(device,RT_DEVICE_FLAG_RDWR); if( result != RT_EOK ) { return result; } // step 2: get device info rt_memset(&geometry, 0, sizeof(geometry)); result = rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry); if( result != RT_EOK ) { rt_kprintf("device : %s cmd RT_DEVICE_CTRL_BLK_GETGEOME failed.\r\n"); return result; } rt_kprintf("device info:\r\n"); rt_kprintf("sector size : %d byte\r\n", geometry.bytes_per_sector); rt_kprintf("sector count : %d \r\n", geometry.sector_count); rt_kprintf("block size : %d byte\r\n", geometry.block_size); rt_kprintf("\r\n"); read_buffer = rt_malloc(geometry.bytes_per_sector); if( read_buffer == RT_NULL ) { rt_kprintf("no memory for read_buffer!\r\n"); goto __return; } write_buffer = rt_malloc(geometry.bytes_per_sector); if( write_buffer == RT_NULL ) { rt_kprintf("no memory for write_buffer!\r\n"); goto __return; } /* step 3: R/W test */ { rt_uint32_t i,err_count, sector_no; rt_uint8_t * data_point; i = rt_device_read(device, 0, read_buffer, 1); if(i != 1) { rt_kprintf("read device :%s ", device->parent.name); rt_kprintf("the first sector failed.\r\n"); goto __return; } data_point = write_buffer; for(i=0; i<geometry.bytes_per_sector; i++) { *data_point++ = (rt_uint8_t)i; } /* write first sector */ sector_no = 0; data_point = write_buffer; *data_point++ = (rt_uint8_t)sector_no; i = rt_device_write(device, sector_no, write_buffer,1); if( i != 1 ) { rt_kprintf("read the first sector success!\r\n"); rt_kprintf("but write device :%s ", device->parent.name); rt_kprintf("the first sector failed.\r\n"); rt_kprintf("maybe readonly!\r\n"); goto __return; } /* write the second sector */ sector_no = 1; data_point = write_buffer; *data_point++ = (rt_uint8_t)sector_no; i = rt_device_write(device,sector_no,write_buffer,1); if( i != 1 ) { rt_kprintf("write device :%s ",device->parent.name); rt_kprintf("the second sector failed.\r\n"); goto __return; } /* write the end sector */ sector_no = geometry.sector_count-1; data_point = write_buffer; *data_point++ = (rt_uint8_t)sector_no; i = rt_device_write(device,sector_no,write_buffer,1); if( i != 1 ) { rt_kprintf("write device :%s ",device->parent.name); rt_kprintf("the end sector failed.\r\n"); goto __return; } /* verify first sector */ sector_no = 0; i = rt_device_read(device,sector_no,read_buffer,1); if( i != 1 ) { rt_kprintf("read device :%s ",device->parent.name); rt_kprintf("the first sector failed.\r\n"); goto __return; } err_count = 0; data_point = read_buffer; if( (*data_point++) != (rt_uint8_t)sector_no) { err_count++; } for(i=1; i<geometry.bytes_per_sector; i++) { if( (*data_point++) != (rt_uint8_t)i ) { err_count++; } } if( err_count > 0 ) { rt_kprintf("verify device :%s ",device->parent.name); rt_kprintf("the first sector failed.\r\n"); goto __return; } /* verify sector sector */ sector_no = 1; i = rt_device_read(device,sector_no,read_buffer,1); if( i != 1 ) { rt_kprintf("read device :%s ",device->parent.name); rt_kprintf("the second sector failed.\r\n"); goto __return; } err_count = 0; data_point = read_buffer; if( (*data_point++) != (rt_uint8_t)sector_no) { err_count++; } for(i=1; i<geometry.bytes_per_sector; i++) { if( (*data_point++) != (rt_uint8_t)i ) { err_count++; } } if( err_count > 0 ) { rt_kprintf("verify device :%s ",device->parent.name); rt_kprintf("the second sector failed.\r\n"); goto __return; } /* verify the end sector */ sector_no = geometry.sector_count-1; i = rt_device_read(device,sector_no,read_buffer,1); if( i != 1 ) { rt_kprintf("read device :%s ",device->parent.name); rt_kprintf("the end sector failed.\r\n"); goto __return; } err_count = 0; data_point = read_buffer; if( (*data_point++) != (rt_uint8_t)sector_no) { err_count++; } for(i=1; i<geometry.bytes_per_sector; i++) { if( (*data_point++) != (rt_uint8_t)i ) { err_count++; } } if( err_count > 0 ) { rt_kprintf("verify device :%s ",device->parent.name); rt_kprintf("the end sector failed.\r\n"); goto __return; } rt_kprintf("device R/W test pass!\r\n"); } /* step 3: I/O R/W test */ rt_kprintf("\r\nRT_TICK_PER_SECOND:%d\r\n", RT_TICK_PER_SECOND); // step 4: continuous single sector speed test { rt_uint32_t tick_start,tick_end; rt_uint32_t i; rt_kprintf("\r\ncontinuous single sector speed test:\r\n"); if( geometry.sector_count < 10 ) { rt_kprintf("device sector_count < 10, speed test abort!\r\n"); } else { unsigned int sector; // sign sector write rt_kprintf("write: "); sector = 0; tick_start = rt_tick_get(); for(i=0; i<200; i++) { sector += rt_device_write(device, i, read_buffer, 1); if((i != 0) && ((i%4) == 0) ) { if(sector < 4) { rt_kprintf("#"); } else { rt_kprintf("<"); } sector = 0; } } tick_end = rt_tick_get(); rt_kprintf("\r\nwrite 200 sector from %d to %d, ",tick_start,tick_end); calculate_speed_print( (geometry.bytes_per_sector*200UL*RT_TICK_PER_SECOND)/(tick_end-tick_start) ); rt_kprintf("\r\n"); // sign sector read rt_kprintf("read : "); sector = 0; tick_start = rt_tick_get(); for(i=0; i<200; i++) { sector += rt_device_read(device, i, read_buffer, 1); if((i != 0) && ((i%4) == 0) ) { if(sector < 4) { rt_kprintf("#"); } else { rt_kprintf(">"); } sector = 0; } } tick_end = rt_tick_get(); rt_kprintf("\r\nread 200 sector from %d to %d, ",tick_start,tick_end); calculate_speed_print( (geometry.bytes_per_sector*200UL*RT_TICK_PER_SECOND)/(tick_end-tick_start) ); rt_kprintf("\r\n"); } }// step 4: speed test // step 5: random single sector speed test { rt_uint32_t tick_start,tick_end; rt_uint32_t i; rt_kprintf("\r\nrandom single sector speed test:\r\n"); if( geometry.sector_count < 10 ) { rt_kprintf("device sector_count < 10, speed test abort!\r\n"); } else { unsigned int sector; // sign sector write rt_kprintf("write: "); sector = 0; tick_start = rt_tick_get(); for(i=0; i<200; i++) { sector += rt_device_write(device, (geometry.sector_count / 10) * (i%10) + (i%10), read_buffer, 1); if((i != 0) && ((i%4) == 0) ) { if(sector < 4) { rt_kprintf("#"); } else { rt_kprintf("<"); } sector = 0; } } tick_end = rt_tick_get(); rt_kprintf("\r\nwrite 200 sector from %d to %d, ",tick_start,tick_end); calculate_speed_print( (geometry.bytes_per_sector*200UL*RT_TICK_PER_SECOND)/(tick_end-tick_start) ); rt_kprintf("\r\n"); // sign sector read rt_kprintf("read : "); sector = 0; tick_start = rt_tick_get(); for(i=0; i<200; i++) { sector += rt_device_read(device, (geometry.sector_count / 10) * (i%10) + (i%10), read_buffer, 1); if((i != 0) && ((i%4) == 0) ) { if(sector < 4) { rt_kprintf("#"); } else { rt_kprintf(">"); } sector = 0; } } tick_end = rt_tick_get(); rt_kprintf("\r\nread 200 sector from %d to %d, ",tick_start,tick_end); calculate_speed_print( (geometry.bytes_per_sector*200UL*RT_TICK_PER_SECOND)/(tick_end-tick_start) ); rt_kprintf("\r\n"); } }// step 4: speed test /* step 6: multiple sector speed test */ { rt_uint8_t * multiple_buffer; rt_uint8_t * ptr; rt_uint32_t tick_start,tick_end; rt_uint32_t sector,i; rt_kprintf("\r\nmultiple sector speed test\r\n"); for(sector=2; sector<256; sector=sector*2) { multiple_buffer = rt_malloc(geometry.bytes_per_sector * sector); if(multiple_buffer == RT_NULL) { rt_kprintf("no memory for %d sector! multiple sector speed test abort!\r\n", sector); break; } rt_memset(multiple_buffer, sector, geometry.bytes_per_sector * sector); rt_kprintf("write: "); tick_start = rt_tick_get(); for(i=0; i<10; i++) { rt_size_t n; n = rt_device_write(device, 50, multiple_buffer, sector); if(n == sector) { rt_kprintf("<"); } else { rt_kprintf("#"); } } tick_end = rt_tick_get(); rt_kprintf("\r\n"); rt_kprintf("multiple write %d sector speed : ", sector); calculate_speed_print( (geometry.bytes_per_sector * sector * 10 * RT_TICK_PER_SECOND)/(tick_end-tick_start) ); rt_kprintf("\r\n"); rt_memset(multiple_buffer, ~sector, geometry.bytes_per_sector * sector); rt_kprintf("read : "); tick_start = rt_tick_get(); for(i=0; i<10; i++) { rt_size_t n; n = rt_device_read(device, 50, multiple_buffer, sector); if(n == sector) { rt_kprintf(">"); } else { rt_kprintf("#"); } } tick_end = rt_tick_get(); rt_kprintf("\r\n"); rt_kprintf("multiple read %d sector speed : ", sector); calculate_speed_print( (geometry.bytes_per_sector * sector * 10 * RT_TICK_PER_SECOND)/(tick_end-tick_start) ); ptr = multiple_buffer; for(i=0; i<geometry.bytes_per_sector * sector; i++) { if(*ptr != sector) { rt_kprintf(" but data verify fail!"); break; } ptr++; } rt_kprintf("\r\n\r\n"); rt_free(multiple_buffer); } } /* step 5: multiple sector speed test */ return RT_EOK; }// device can read and write. else { // device read only return RT_EOK; }// device read only __return: if( read_buffer != RT_NULL ) { rt_free(read_buffer); } if( write_buffer != RT_NULL ) { rt_free(write_buffer); } return RT_ERROR; }
void finsh_thread_entry(void* parameter) { char ch; /* normal is echo mode */ shell->echo_mode = 1; finsh_init(&shell->parser); rt_kprintf(FINSH_PROMPT); while (1) { /* wait receive */ if (rt_sem_take(&shell->rx_sem, RT_WAITING_FOREVER) != RT_EOK) continue; /* read one character from device */ while (rt_device_read(shell->device, 0, &ch, 1) == 1) { /* handle history key */ #ifdef FINSH_USING_HISTORY if (finsh_handle_history(shell, ch) == RT_TRUE) continue; #endif /* handle CR key */ if (ch == '\r') { char next; if (rt_device_read(shell->device, 0, &next, 1) == 1) ch = next; else ch = '\r'; } /* handle tab key */ else if (ch == '\t') { /* auto complete */ finsh_auto_complete(&shell->line[0]); /* re-calculate position */ shell->line_position = strlen(shell->line); continue; } /* handle backspace key */ else if (ch == 0x7f || ch == 0x08) { if (shell->line_position != 0) { rt_kprintf("%c %c", ch, ch); } if (shell->line_position <= 0) shell->line_position = 0; else shell->line_position --; shell->line[shell->line_position] = 0; continue; } /* handle end of line, break */ if (ch == '\r' || ch == '\n') { /* change to ';' and break */ shell->line[shell->line_position] = ';'; #ifdef FINSH_USING_HISTORY finsh_push_history(shell); #endif if (shell->line_position != 0) finsh_run_line(&shell->parser, shell->line); else rt_kprintf("\n"); rt_kprintf(FINSH_PROMPT); memset(shell->line, 0, sizeof(shell->line)); shell->line_position = 0; break; } /* it's a large line, discard it */ if (shell->line_position >= FINSH_CMD_SIZE) shell->line_position = 0; /* normal character */ shell->line[shell->line_position] = ch; ch = 0; if (shell->echo_mode) rt_kprintf("%c", shell->line[shell->line_position]); shell->line_position ++; shell->use_history = 0; /* it's a new command */ } /* end of device read */ } }
static void proc_rx_byte(struct tcp_pcb * pcb, struct telnetio_dev *teldev, char ch) { #if (!RT_NEED_FINSH_PROC_LOGIN) int num, i; unsigned char ch1; #endif TELNETD_DEBUG(("line:%u, iac_s:%d, state:%d, ch:%c(0x%x)\r\n", __LINE__, teldev->iac_state, teldev->state, ch, ch)); if (0 == teldev->iac_state) { if (TELNET_IAC == ch) { teldev->iac_state = TELS_IAC; return; } if ('\n' != ch) { rb_write(&teldev->rx_rb_buf, &ch, 1); if ('\r' != ch) return; } else { return; } switch (teldev->state) { case TELS_LOGIN_NAME: #if (!RT_NEED_FINSH_PROC_LOGIN) do { if ((0==rb_first_read_byte_pry(&teldev->rx_rb_buf, &ch1)) && '\r'==ch1) rb_first_read_byte_drop(&teldev->rx_rb_buf); else break; } while(1); num = rb_get_used_bytes_num(&teldev->rx_rb_buf) - 1; i = MIN(num, USR_NAME_LEN_MAX); rb_read(&teldev->rx_rb_buf, teldev->usrpw.usr, i); teldev->usrpw.usr[i] = '\0'; rb_cleanup(&teldev->rx_rb_buf); TELNETD_DEBUG(("line:%u, input:%s\r\n", __LINE__, teldev->usrpw.usr)); tcp_write(pcb, PASSWORD, strlen(PASSWORD), TCP_WRITE_FLAG_COPY); //tcp_output(pcb); #else rt_device_read(&teldev->dev, 0, NULL, 0); #endif rt_device_read(&teldev->dev, 0, NULL, 0); teldev->state = TELS_LOGIN_PW; write_iac_cmd2tx_buf(teldev, TELNET_WONT, TELNET_SUPPRESS_GO_AHEAD); write_iac_cmd2tx_buf(teldev, TELNET_WILL, TELNET_ECHO); break; case TELS_LOGIN_PW: #if (!RT_NEED_FINSH_PROC_LOGIN) do { if ((0==rb_first_read_byte_pry(&teldev->rx_rb_buf, &ch1)) && '\r'==ch1) rb_first_read_byte_drop(&teldev->rx_rb_buf); else break; } while(1); num = rb_get_used_bytes_num(&teldev->rx_rb_buf) - 1; i = MIN(num, PW_LEN_MAX); rb_read(&teldev->rx_rb_buf, teldev->usrpw.pw, i); teldev->usrpw.pw[i] = '\0'; rb_cleanup(&teldev->rx_rb_buf); TELNETD_DEBUG(("line:%u, usr:%s, pw:%s\r\n", __LINE__, teldev->usrpw.usr, teldev->usrpw.pw)); if (is_usr_pw_matching(teldev->usrpw.usr, teldev->usrpw.pw)) { teldev->state = TELS_NORMAL; teldev->iac_state = 0; rb_write(&teldev->rx_rb_buf, "\r", 1); rt_device_read(&teldev->dev, 0, NULL, 0); } else { tcp_write(pcb, LOGIN_NAME, strlen(LOGIN_NAME), TCP_WRITE_FLAG_COPY); teldev->state = TELS_LOGIN_NAME; } #else rt_device_read(&teldev->dev, 0, NULL, 0); #endif write_iac_cmd2tx_buf(teldev, TELNET_WONT, TELNET_SUPPRESS_GO_AHEAD); write_iac_cmd2tx_buf(teldev, TELNET_WONT, TELNET_ECHO); break; case TELS_NORMAL : rt_device_read(&teldev->dev, 0, NULL, 0); break; case TELS_CLOSE : case TELS_LOGOUT: default: break; } } else { switch (teldev->iac_state) { case TELS_IAC : switch (ch) { case TELNET_WILL : teldev->iac_state = TELS_WILL; break; case TELNET_WONT : teldev->iac_state = TELS_WONT; break; case TELNET_DO : teldev->iac_state = TELS_DO; break; case TELNET_DONT : teldev->iac_state = TELS_DONT; break; case TELNET_IAC: default : teldev->iac_state = 0;//TELS_NORMAL; break; } break; case TELS_WILL : /* Reply with a DONT */ write_iac_cmd2tx_buf (teldev, TELNET_DONT, ch); teldev->iac_state = 0; break; case TELS_WONT : /* Reply with a DONT */ write_iac_cmd2tx_buf (teldev, TELNET_DONT, ch); teldev->iac_state = 0; break; case TELS_DO : /* Reply with a WONT */ if ((TELS_LOGIN_PW == teldev->state) && TELNET_ECHO==ch) write_iac_cmd2tx_buf (teldev, TELNET_WILL, ch); else write_iac_cmd2tx_buf (teldev, TELNET_WONT, ch); teldev->iac_state = 0; break; case TELS_DONT : /* Reply with a WONT */ write_iac_cmd2tx_buf (teldev, TELNET_WONT, ch); teldev->iac_state = 0; break; default: teldev->iac_state = 0; break; } } }