forked from gicking/stm8gal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_comm.h
93 lines (67 loc) · 2.72 KB
/
serial_comm.h
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
/**
\file serial_comm.h
\author G. Icking-Konert
\date 2008-11-02
\version 0.1
\brief declaration of RS232 comm port routines
declaration of of routines for RS232 communication using the Win32 or Posix API.
For Win32, see e.g. http://msdn.microsoft.com/en-us/library/default.aspx
For Posix see http://www.easysw.com/~mike/serial/serial.html
*/
// for including file only once
#ifndef _SERIAL_COMM_H_
#define _SERIAL_COMM_H_
// generic ANSI
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
// OS specific: Win32
#if defined(WIN32)
#include <windows.h>
#include <conio.h>
// OS specific: Posix
#elif defined(__APPLE__) || defined(__unix__)
#define HANDLE int // comm port handler is int
#include <fcntl.h> // File control definitions
#include <termios.h> // Posix terminal control definitions
#include <getopt.h>
#include <errno.h> // error number definitions
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#else
#error OS not supported
#endif
/// list all available comm ports
void list_ports(void);
/// init comm port
HANDLE init_port(const char *port, uint32_t baudrate, uint32_t timeout, uint8_t numBits, uint8_t parity, uint8_t numStop, uint8_t RTS, uint8_t DTR);
/// close comm port
void close_port(HANDLE *fpCom);
/// generate low pulse on DTR in [ms] to reset STM8
void pulse_DTR(HANDLE fpCom, uint32_t duration);
/// generate low pulse on RTS in [ms] to reset STM8
void pulse_RTS(HANDLE fpCom, uint32_t duration);
/// generate low pulse on Raspberry pin in [ms] to reset STM8
void pulse_GPIO(int pin, uint32_t duration);
/// get comm port settings
void get_port_attribute(HANDLE fpCom, uint32_t *baudrate, uint32_t *timeout, uint8_t *numBits, uint8_t *parity, uint8_t *numStop, uint8_t *RTS, uint8_t *DTR);
/// modify comm port settings
void set_port_attribute(HANDLE fpCom, uint32_t baudrate, uint32_t timeout, uint8_t numBits, uint8_t parity, uint8_t numStop, uint8_t RTS, uint8_t DTR);
/// modify comm port baudrate
void set_baudrate(HANDLE fpCom, uint32_t Baudrate);
/// modify comm port timeout
void set_timeout(HANDLE fpCom, uint32_t Timeout);
/// modify comm port parity
void set_parity(HANDLE fpCom, uint8_t Parity);
/// send data
uint32_t send_port(HANDLE fpCom, uint8_t uartMode, uint32_t lenTx, char *Tx);
/// receive data
uint32_t receive_port(HANDLE fpCom, uint8_t uartMode, uint32_t lenRx, char *Rx);
/// flush port buffers
void flush_port(HANDLE fpCom);
#endif // _SERIAL_COMM_H_
// end of file