Newer
Older
/* SAM7Sisp - alternatywny bootloader dla mikrokontrolerw AT91SAM7
(c) Tomasz Wlostowski 2006
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <asm/ioctls.h>
#include <string.h>
#include <termios.h>
#include <fcntl.h>
static int serial_fd = -1;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
void serial_set_dtr(int s) // pin 4
{
unsigned int v;
if (serial_fd<0) return;
ioctl(serial_fd,TIOCMGET,&v);
if (s) v|=TIOCM_DTR; else v&=~TIOCM_DTR;
ioctl(serial_fd,TIOCMSET,&v);
}
int serial_open(char *dev_name, int speed)
{
struct termios t;
int fd;
int spd;
switch(speed)
{
case 115200: spd=B115200; break;
case 57600: spd=B57600; break;
case 38400: spd=B38400; break;
case 19200: spd=B19200; break;
case 9600: spd=B9600; break;
default: return -2;
}
fd = open (dev_name, O_RDWR | O_NONBLOCK);
if(fd<0) return -1;
tcgetattr (fd, &t);
t.c_iflag = IGNBRK | IGNPAR;
t.c_oflag = t.c_lflag = t.c_line = 0;
t.c_cflag = CS8 | CREAD | CLOCAL | HUPCL | spd;
tcsetattr (fd, TCSAFLUSH, &t);
serial_fd = fd;
return 0;
}
void serial_close()
{
close(serial_fd);
}
int serial_write(void *data, int len)
{
// printf("WS: '");
// for(i=0;i<len;i++) printf("%c", data[i]);
// printf("'\n");
return write(serial_fd, data, len);
}
int serial_read(void *data, int len)
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
{
int nbytes=0;
while(len)
{
if(read(serial_fd, data, 1)==1) { len--;data++; nbytes++; }
}
return nbytes;
};
void serial_write_byte(unsigned char b)
{
// printf("WB: '%c'\n", b);
write (serial_fd,&b,1);
}
unsigned char serial_read_byte()
{
unsigned char b;
serial_read(&b,1);
// fprintf(stderr,"%02x ", b);
return b;
}
int serial_data_avail()
{
fd_set set;
struct timeval tv;
FD_ZERO(&set);
FD_SET(serial_fd,&set);
tv.tv_sec = 0;
tv.tv_usec = 0;
return select(serial_fd+1, &set, NULL, NULL, &tv)>0;
}
unsigned int sys_get_clock_usec()
{
struct timezone tz={0,0};
struct timeval tv;
gettimeofday(&tv,&tz);
return tv.tv_usec + tv.tv_sec * 1000000;
}
void sys_delay(int msecs)
{
usleep(msecs*1000);
}
void serial_purge()
{
while(serial_data_avail()) serial_read_byte();