forked from cvra/nastya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_io.c
106 lines (87 loc) · 2.23 KB
/
serial_io.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
#include <stdio.h>
#include <lwip/ip.h>
#include <netif/slipif.h>
#include "lwip/sio.h"
/** Enables activity LEDs. */
#define ACTIVITY_LED_TOGGLE
#ifdef ACTIVITY_LED_TOGGLE
/* Altera files, to toggle LEDs */
#include <os_cfg.h>
#include <io.h>
#include <os_cpu.h>
#define ACTIVITY_LED_TX 0
#define ACTIVITY_LED_RX 1
#endif
FILE *in, *out;
/**
* Sends a single character to the serial device.
*
* @param c character to send
* @param fd serial device handle
*
* @note This function will block until the character can be sent.
*/
void sio_send(u8_t c, sio_fd_t fd)
{
int32_t led_val;
/* Toggles Tx led. */
#ifdef ACTIVITY_LED_TOGGLE
OS_CPU_SR cpu_sr;
OS_ENTER_CRITICAL();
led_val = IORD(LED_BASE, 0);
led_val ^= (1 << ACTIVITY_LED_TX);
IOWR(LED_BASE, 0, led_val);
OS_EXIT_CRITICAL();
#endif
fputc((int)c, out);
}
/**
* Reads from the serial device.
*
* @param fd serial device handle
* @param data pointer to data buffer for receiving
* @param len maximum length (in bytes) of data to receive
* @return number of bytes actually received - may be 0 if aborted by sio_read_abort
*
*/
u32_t sio_read(sio_fd_t fd, u8_t *data, u32_t len)
{
int32_t led_val;
/* Toggles Rx led */
#ifdef ACTIVITY_LED_TOGGLE
OS_CPU_SR cpu_sr;
OS_ENTER_CRITICAL();
led_val = IORD(LED_BASE, 0);
led_val ^= (1 << ACTIVITY_LED_RX);
IOWR(LED_BASE, 0, led_val);
OS_EXIT_CRITICAL();
#endif
return (u32_t)fread((void *)data, 1, (size_t)len, in);
}
/**
* Tries to read from the serial device. Same as sio_read but returns
* immediately if no data is available and never blocks.
*
* @param fd serial device handle
* @param data pointer to data buffer for receiving
* @param len maximum length (in bytes) of data to receive
* @return number of bytes actually received
*/
u32_t sio_tryread(sio_fd_t fd, u8_t *data, u32_t len)
{
return sio_read(fd, data, len);
}
/**
* Opens a serial device for communication.
*
* @param devnum device number
* @return handle to serial device if successful, NULL otherwise
*/
sio_fd_t sio_open(u8_t devnum)
{
in = fopen("/dev/comPC", "r");
out = fopen("/dev/comPC", "w");
if (in == NULL || out == NULL)
return NULL;
return 1;
}