forked from triffid/FiveD_on_Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial_sim.c
116 lines (95 loc) · 2.58 KB
/
serial_sim.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
72
73
74
75
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
105
106
107
108
109
110
111
112
113
114
115
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include "serial.h"
#include "simulation.h"
static int serial_fd;
static bool serial_initialised = false;
void serial_init(void)
{
struct termios options;
// hack to get argv and argc
extern char ** environ;
int argc = 1;
char **argv = environ - 3;
while((int)*argv != argc)
{
++argc;
--argv;
}
argv++;
sim_assert(argc >= 2, "please specify a serial port device name");
sim_info("opening serial port %s", argv[1]);
serial_fd = open(argv[1], O_RDWR | O_NOCTTY | O_NDELAY);
sim_assert(serial_fd != -1, "couldn't open serial port");
sim_assert(isatty(serial_fd), "not a TTY");
sim_info("configuring port");
// Get the current options for the port
if (tcgetattr(serial_fd, &options) != 0)
{
sim_error("tcgetattr");
}
// Set the baud rates
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
// Enable the receiver and set local mode
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
// Set the new options for the port
if (tcsetattr(serial_fd, TCSANOW, &options) != 0)
{
sim_error("tcsetattr");
}
// flush tx and rx buffers
tcflush(serial_fd, TCIOFLUSH);
serial_initialised = true;
}
// return number of characters in the receive buffer
uint8_t serial_rxchars(void)
{
int rx_chars_nb;
sim_assert(serial_initialised, "serial interface not initialised");
ioctl(serial_fd, FIONREAD, &rx_chars_nb);
return rx_chars_nb;
}
// read one character
uint8_t serial_popchar(void)
{
uint8_t c;
ssize_t count;
sim_assert(serial_initialised, "serial interface not initialised");
sim_assert(serial_rxchars() > 0, "no chars to read");
count = read(serial_fd, &c, 1);
sim_assert(count == 1, "no character in serial RX buffer");
return c;
}
// send one character
void serial_writechar(uint8_t data)
{
ssize_t count;
sim_assert(serial_initialised, "serial interface not initialised");
putchar(data);
count = write(serial_fd, &data, 1);
sim_assert(count == 1, "could not write to serial port");
}
// read/write many characters
void serial_writestr(uint8_t *data)
{
ssize_t count;
const char *str = (char *)data;
sim_assert(serial_initialised, "serial interface not initialised");
puts(str);
count = write(serial_fd, str, strlen(str));
sim_assert(count == strlen(str), "could not write to serial port");
}
// write from flash
void serial_writestr_P(PGM_P data)
{
serial_writestr((uint8_t *)data);
}