-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjspin.h
205 lines (180 loc) · 4.66 KB
/
jspin.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
* Copyright (C) 2013 Gordon Williams <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* ----------------------------------------------------------------------------
* Utilities and definitions for handling Pins
* ----------------------------------------------------------------------------
*/
#ifndef JSPIN_H
#define JSPIN_H
#include "jsutils.h"
#include "jsvar.h"
#include "PinNames.h"
typedef PinName Pin; ///< for specifying pins for hardware
typedef enum {
JSH_PORT_NONE,
JSH_PORTA=1,
JSH_PORTB,
JSH_PORTC,
JSH_PORTD,
JSH_PORTE,
JSH_PORTF,
JSH_PORTG,
JSH_PORTH,
} PACKED_FLAGS JsvPinInfoPort;
typedef enum {
JSH_PIN0 = 0,
JSH_PIN1,
JSH_PIN2,
JSH_PIN3,
JSH_PIN4,
JSH_PIN5,
JSH_PIN6,
JSH_PIN7,
JSH_PIN8,
JSH_PIN9,
JSH_PIN10,
JSH_PIN11,
JSH_PIN12,
JSH_PIN13,
JSH_PIN14,
JSH_PIN15,
// and there may be more on Linux systems...
} PACKED_FLAGS JsvPinInfoPin;
typedef enum {
JSH_ANALOG_NONE = 0,
JSH_ANALOG1 = 32,
JSH_ANALOG2 = 64,
JSH_ANALOG3 = 128,
JSH_ANALOG4 = 256,
JSH_ANALOG12 = JSH_ANALOG1|JSH_ANALOG2,
JSH_ANALOG123 = JSH_ANALOG1|JSH_ANALOG2|JSH_ANALOG3,
JSH_ANALOG34 = JSH_ANALOG3|JSH_ANALOG4,
JSH_ANALOG_CH0 = 0,
JSH_ANALOG_CH1,
JSH_ANALOG_CH2,
JSH_ANALOG_CH3,
JSH_ANALOG_CH4,
JSH_ANALOG_CH5,
JSH_ANALOG_CH6,
JSH_ANALOG_CH7,
JSH_ANALOG_CH8,
JSH_ANALOG_CH9,
JSH_ANALOG_CH10,
JSH_ANALOG_CH11,
JSH_ANALOG_CH12,
JSH_ANALOG_CH13,
JSH_ANALOG_CH14,
JSH_ANALOG_CH15,
JSH_ANALOG_CH16,
JSH_ANALOG_CH17,
JSH_MASK_ANALOG_CH = 31,
JSH_MASK_ANALOG_ADC = JSH_ANALOG1|JSH_ANALOG2|JSH_ANALOG3|JSH_ANALOG4,
} PACKED_FLAGS JsvPinInfoAnalog;
typedef enum {
// ---------------------------- JSH_MASK_AF
JSH_AF0 = 0,
JSH_AF1,
JSH_AF2,
JSH_AF3,
JSH_AF4,
JSH_AF5,
JSH_AF6,
JSH_AF7,
JSH_AF8,
JSH_AF9,
JSH_AF10,
JSH_AF11,
JSH_AF12,
JSH_AF13,
JSH_AF14,
JSH_AF15,
// ---------------------------- JSH_MASK_TYPE
JSH_NOTHING = 0,
JSH_TIMER1 = 0x0010,
JSH_TIMER2 = 0x0020,
JSH_TIMER3 = 0x0030,
JSH_TIMER4 = 0x0040,
JSH_TIMER5 = 0x0050,
JSH_TIMER6 = 0x0060,
JSH_TIMER7 = 0x0070,
JSH_TIMER8 = 0x0080,
JSH_TIMER9 = 0x0090,
JSH_TIMER10 = 0x00A0,
JSH_TIMER11 = 0x00B0,
JSH_TIMER12 = 0x00C0,
JSH_TIMER13 = 0x00D0,
JSH_TIMER14 = 0x00E0,
JSH_TIMER15 = 0x00F0,
JSH_TIMER16 = 0x0100,
JSH_TIMER17 = 0x0110,
JSH_TIMER18 = 0x0120,
JSH_TIMERMAX = JSH_TIMER18,
JSH_DAC = 0x0180,
JSH_SPI1 = 0x0200,
JSH_SPI2 = 0x0210,
JSH_SPI3 = 0x0220,
JSH_SPIMAX = JSH_SPI3,
JSH_I2C1 = 0x0280,
JSH_I2C2 = 0x0290,
JSH_I2C3 = 0x02A0,
JSH_I2CMAX = JSH_I2C3,
JSH_USART1 = 0x0300,
JSH_USART2 = 0x0310,
JSH_USART3 = 0x0320,
JSH_USART4 = 0x0330,
JSH_USART5 = 0x0340,
JSH_USART6 = 0x0350,
JSH_USARTMAX = JSH_USART6,
// ---------------------------- JSH_MASK_INFO
JSH_TIMER_CH1 = 0x0000,
JSH_TIMER_CH2 = 0x1000,
JSH_TIMER_CH3 = 0x2000,
JSH_TIMER_CH4 = 0x3000,
JSH_MASK_TIMER_CH = 0x7000,
JSH_TIMER_NEGATED = 0x8000,
JSH_USART_RX = 0x0000,
JSH_USART_TX = 0x1000,
JSH_SPI_MISO = 0x0000,
JSH_SPI_MOSI = 0x1000,
JSH_SPI_SCK = 0x2000,
JSH_I2C_SCL = 0x0000,
JSH_I2C_SDA = 0x1000,
JSH_DAC_CH1 = 0x0000,
JSH_DAC_CH2 = 0x1000,
// ---------------------------- Masks
JSH_MASK_AF = 0x000F,
JSH_MASK_TYPE = 0x0FF0,
JSH_MASK_INFO = 0xF000,
} PACKED_FLAGS JshPinFunction;
#define JSH_PINFUNCTION_IS_TIMER(F) ( \
(((F)&JSH_MASK_TYPE)>=JSH_TIMER1) && \
(((F)&JSH_MASK_TYPE)<=JSH_TIMER18))
#define JSH_PINFUNCTION_IS_DAC(F) ( \
(((F)&JSH_MASK_TYPE)==JSH_DAC) || \
0 )
#define JSH_PINFUNCTION_IS_USART(F) ( \
(((F)&JSH_MASK_TYPE)>=JSH_USART1) && \
(((F)&JSH_MASK_TYPE)<=JSH_USART6))
#define JSH_PINFUNCTION_IS_I2C(F) ( \
(((F)&JSH_MASK_TYPE)>=JSH_I2C1) && \
(((F)&JSH_MASK_TYPE)<=JSH_I2CMAX))
#define JSH_PINFUNCTION_IS_SPI(F) ( \
(((F)&JSH_MASK_TYPE)>=JSH_SPI1) && \
(((F)&JSH_MASK_TYPE)<=JSH_SPIMAX))
bool jshIsPinValid(Pin pin); ///< is the specific pin actually valid?
/** Write the pin name to a string. String must have at least 8 characters (to be safe) */
void jshGetPinString(char *result, Pin pin);
/// Given a var, convert it to a pin ID (or -1 if it doesn't exist). safe for undefined!
Pin jshGetPinFromVar(JsVar *pinv);
Pin jshGetPinFromVarAndUnLock(JsVar *pinv);
Pin jshGetPinFromString(const char *s);
bool jshPinInput(Pin pin);
void jshPinOutput(Pin pin, bool value);
#endif //JSPIN_H