-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserial.cpp
152 lines (130 loc) · 3.69 KB
/
serial.cpp
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
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
141
142
143
144
145
146
147
148
149
150
151
/*
* Raspberry PI / Zero AddOn Board specially for Ham Radio Applications
* ====================================================================
* Author: DJ0ABR
*
* (c) DJ0ABR
* www.dj0abr.de
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
serial.cpp
============
handles the serial interface and the CIV interface
Raspi supporting WLAN (RPI3, 4 and Zero-W) need remapping of serial port
========================================================================
1. Switch-off serial login shell
sudo raspi-config
InterfaceOptions - Serial Port:
login shell: NO
hardware enabled: YES
2. map primary serial port to connector pins
sudo nano /boot/config.txt and add at the end:
# Switch serial ports
dtoverlay=miniuart-bt
3. Reboot and check if ok:
ls -l /dev/serial*
serial0 must map to ttyAMA0
*/
#include "kmfifo.h"
#include "serial.h"
#include "serial_helper.h"
// functions for the primary UART on the Raspi Pins 8 and 10
// =========================================================
int serid = -1;
// exit program on error. Check for above remapping or hardware error
// speed ... use definition Bxxx
void open_serial(int speed)
{
serid = init_serial_interface("/dev/ttyAMA0","", speed); //maybe is /dev/serial0 or /dev/ttyS0
if(serid == -1)
{
printf("connot open primary serial interface\n");
exit(0);
}
}
// returns: -1...write error (write pipe overflow, in this case write slower)
int write_serial(int data)
{
int ret = write_serpipe(serid, data, 'w');
return ret;
}
// returns: -1=write buffer full, 0=ok
int write_serial_free()
{
return check_pipe(serid, 'w');
}
// returns: -1...no data
int read_serial()
{
return read_serpipe(serid,'r');
}
// Functions for the USB-serial converter located on the board
// ===========================================================
int civid = -1;
// speed ... use definition Bxxx
void open_civ(int speed)
{
civid = init_serial_interface("AB0KOTJ8","0403", speed);
if(civid == -1)
{
printf("connot open USB serial interface\n");
exit(0);
}
}
int read_civ()
{
return read_serpipe(civid,'r');
}
int write_civ(int data)
{
int ret = write_serpipe(civid, data, 'w');
return ret;
}
int write_civ_free()
{
return check_pipe(civid, 'w');
}
// Functions for other USB-serial converters
// =========================================
#define MAXUSBSER 10
int serUSBid[MAXUSBSER];
int serUSBnum = 0;
// speed ... use definition Bxxx
int open_serialUSB(int speed, char *idserial, char *idVendor)
{
serUSBid[serUSBnum] = init_serial_interface(idserial,idVendor, speed);
if(serUSBid[serUSBnum] == -1)
{
printf("connot open USB serial interface\n");
exit(0);
}
int ret = serUSBnum;
serUSBnum++;
return ret;
}
int read_serialUSB(int id)
{
return read_serpipe(serUSBid[id],'r');
}
int write_serialUSB(int id, int data)
{
int ret = write_serpipe(serUSBid[id], data, 'w');
return ret;
}
int write_serialUSB_free(int id)
{
return check_pipe(serUSBid[id], 'w');
}