zdevice.c 1.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * File      : zdevice.c
 * the implemention of zmodem protocol.
 * Change Logs:
 * Date           Author       Notes
 * 2011-03-29     itspy       
 */

#include <rtthread.h>
#include <finsh.h>
#include <shell.h>
#include <rtdef.h>
#include <dfs.h>
#include <dfs_file.h>
#include <dfs_posix.h>
#include "zdef.h"

										  
rt_uint32_t Line_left  = 0;		     /* left number of data in the read line buffer*/ 
rt_uint32_t Left_sizes = 0;			 /* left file sizes */
rt_uint32_t Baudrate   = BITRATE; 	 /* console baudrate */
 


I
itspy.wei 已提交
25
rt_uint32_t get_device_baud(void)
26 27 28 29 30 31 32 33 34 35 36 37
{
    return(Baudrate);
}

rt_uint32_t get_sys_time(void)
{
    return(0L);
}

void zsend_byte(rt_uint16_t ch)
{
    rt_device_write(zmodem.device,0,&ch,1);  
I
itspy.wei 已提交
38

39 40 41 42 43 44
    return;
}

void zsend_line(rt_uint16_t c)
{
    rt_uint16_t ch;
I
itspy.wei 已提交
45

46 47
	ch = (c & 0377);
    rt_device_write(zmodem.device,0,&ch,1);   
I
itspy.wei 已提交
48

49
    return;
I
itspy.wei 已提交
50
}
51 52 53 54

rt_int16_t zread_line(rt_uint16_t timeout)
{
	char *str;	 
I
itspy.wei 已提交
55 56
	static char buf[10];

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	if (Line_left > 0)
	{
	    Line_left -= 1;
		return (*str++ & 0377);
	}
	Line_left = 0;
	timeout/=5;
	while (1)
	{
     	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;
I
itspy.wei 已提交
76

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	return (*str++ & 0377);
}

/*
 * send a string to the modem, processing for \336 (sleep 1 sec)
 *   and \335 (break signal)
 */
void zsend_break(char *cmd)
{
   
	while (*cmd++) 
	{
		switch (*cmd) 
		{
		case '\336':
			 continue;
		case '\335':
		     rt_thread_delay(RT_TICK_PER_SECOND);
			 continue;
		default:
			 zsend_line(*cmd);
			 break;
		}
	}
}
/* send cancel string to get the other end to shut up */
void zsend_can(void)
{
I
itspy.wei 已提交
105 106
	static char cmd[] = {24,24,24,24,24,24,24,24,24,24,0};

107 108
	zsend_break(cmd);
	rt_kprintf("\x0d");
I
itspy.wei 已提交
109 110 111
	Line_left=0;	       /* clear Line_left */

	return;
112 113
}

I
itspy.wei 已提交
114
/* end of zdevice.c */