source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/libraries/Firmata/utility/serialUtils.h @ 46

Last change on this file since 46 was 46, checked in by jrpelegrina, 4 years ago

First release to Xenial

File size: 3.2 KB
Line 
1/*
2  serialUtils.h - Definitions and utility functions for the Serial feature.
3  Copyright (c) 2015 Jeff Hoefs.  All rights reserved.
4
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9
10  See file LICENSE.txt for further informations on licensing terms.
11
12  Last updated by Jeff Hoefs: October 3rd, 2015
13*/
14
15#ifndef SERIAL_UTILS_H
16#define SERIAL_UTILS_H
17
18// Serial port Ids
19#define HW_SERIAL0                  0x00
20#define HW_SERIAL1                  0x01
21#define HW_SERIAL2                  0x02
22#define HW_SERIAL3                  0x03
23// extensible up to 0x07
24
25#define SW_SERIAL0                  0x08
26#define SW_SERIAL1                  0x09
27#define SW_SERIAL2                  0x0A
28#define SW_SERIAL3                  0x0B
29// extensible up to 0x0F
30
31#define SERIAL_PORT_ID_MASK         0x0F
32#define MAX_SERIAL_PORTS            8
33#define SERIAL_READ_ARR_LEN         12
34
35// map configuration query response resolution value to serial pin type
36#define RES_RX1                     0x02
37#define RES_TX1                     0x03
38#define RES_RX2                     0x04
39#define RES_TX2                     0x05
40#define RES_RX3                     0x06
41#define RES_TX3                     0x07
42
43// Serial command bytes
44#define SERIAL_CONFIG               0x10
45#define SERIAL_WRITE                0x20
46#define SERIAL_READ                 0x30
47#define SERIAL_REPLY                0x40
48#define SERIAL_CLOSE                0x50
49#define SERIAL_FLUSH                0x60
50#define SERIAL_LISTEN               0x70
51
52// Serial read modes
53#define SERIAL_READ_CONTINUOUSLY    0x00
54#define SERIAL_STOP_READING         0x01
55#define SERIAL_MODE_MASK            0xF0
56
57struct serial_pins {
58  uint8_t rx;
59  uint8_t tx;
60};
61
62/*
63 * Get the serial serial pin type (RX1, TX1, RX2, TX2, etc) for the specified pin.
64 */
65inline uint8_t getSerialPinType(uint8_t pin) {
66#if defined(PIN_SERIAL_RX)
67  // TODO when use of HW_SERIAL0 is enabled
68#endif
69#if defined(PIN_SERIAL1_RX)
70  if (pin == PIN_SERIAL1_RX) return RES_RX1;
71  if (pin == PIN_SERIAL1_TX) return RES_TX1;
72#endif
73#if defined(PIN_SERIAL2_RX)
74  if (pin == PIN_SERIAL2_RX) return RES_RX2;
75  if (pin == PIN_SERIAL2_TX) return RES_TX2;
76#endif
77#if defined(PIN_SERIAL3_RX)
78  if (pin == PIN_SERIAL3_RX) return RES_RX3;
79  if (pin == PIN_SERIAL3_TX) return RES_TX3;
80#endif
81  return 0;
82}
83
84/*
85 * Get the RX and TX pins numbers for the specified HW serial port.
86 */
87inline serial_pins getSerialPinNumbers(uint8_t portId) {
88  serial_pins pins;
89  switch (portId) {
90#if defined(PIN_SERIAL_RX)
91      // case HW_SERIAL0:
92      //   // TODO when use of HW_SERIAL0 is enabled
93      //   break;
94#endif
95#if defined(PIN_SERIAL1_RX)
96    case HW_SERIAL1:
97      pins.rx = PIN_SERIAL1_RX;
98      pins.tx = PIN_SERIAL1_TX;
99      break;
100#endif
101#if defined(PIN_SERIAL2_RX)
102    case HW_SERIAL2:
103      pins.rx = PIN_SERIAL2_RX;
104      pins.tx = PIN_SERIAL2_TX;
105      break;
106#endif
107#if defined(PIN_SERIAL3_RX)
108    case HW_SERIAL3:
109      pins.rx = PIN_SERIAL3_RX;
110      pins.tx = PIN_SERIAL3_TX;
111      break;
112#endif
113    default:
114      pins.rx = 0;
115      pins.tx = 0;
116  }
117  return pins;
118}
119
120#endif /* SERIAL_UTILS_H */
Note: See TracBrowser for help on using the repository browser.