JM9 XRCU Board 0.1.0
Libraries API Reference
Loading...
Searching...
No Matches
pins_arduino.h
1/*
2 Copyright (c) 2011 Arduino. All right reserved.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 See the GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17*/
18#ifndef _PINS_ARDUINO_H_
19#define _PINS_ARDUINO_H_
20#include <stdbool.h>
21#include <stdlib.h> /* Required for static_assert */
22#include "variant.h"
23#include "PinNames.h"
24
25#include "pins_arduino_analog.h"
26#include "pins_arduino_digital.h"
27
28/*
29 * Pin number mask
30 * allows to retrieve the pin number without ALTx
31 */
32#define PNUM_MASK 0xFF
33
34/* Pin not defined */
35#define PNUM_NOT_DEFINED NUM_DIGITAL_PINS
36
37/* Avoid PortName issue */
38_Static_assert(LastPort <= 0x0F, "PortName must be less than 16");
39
40_Static_assert(NUM_ANALOG_INPUTS <= MAX_ANALOG_INPUTS,
41 "Core NUM_ANALOG_INPUTS limited to MAX_ANALOG_INPUTS");
42
43/* Default for Arduino connector compatibility */
44/* SPI Definitions */
45#ifndef PIN_SPI_SS
46 #define PIN_SPI_SS 10
47#endif
48#ifndef PIN_SPI_SS1
49 #define PIN_SPI_SS1 4
50#endif
51#ifndef PIN_SPI_SS2
52 #define PIN_SPI_SS2 7
53#endif
54#ifndef PIN_SPI_SS3
55 #define PIN_SPI_SS3 8
56#endif
57#ifndef PIN_SPI_MOSI
58 #define PIN_SPI_MOSI 11
59#endif
60#ifndef PIN_SPI_MISO
61 #define PIN_SPI_MISO 12
62#endif
63#ifndef PIN_SPI_SCK
64 #define PIN_SPI_SCK 13
65#endif
66
67static const uint32_t SS = PIN_SPI_SS;
68static const uint32_t SS1 = PIN_SPI_SS1;
69static const uint32_t SS2 = PIN_SPI_SS2;
70static const uint32_t SS3 = PIN_SPI_SS3;
71static const uint32_t MOSI = PIN_SPI_MOSI;
72static const uint32_t MISO = PIN_SPI_MISO;
73static const uint32_t SCK = PIN_SPI_SCK;
74
75/* I2C Definitions */
76#ifndef PIN_WIRE_SDA
77 #define PIN_WIRE_SDA 14
78#endif
79#ifndef PIN_WIRE_SCL
80 #define PIN_WIRE_SCL 15
81#endif
82
83static const uint32_t SDA = PIN_WIRE_SDA;
84static const uint32_t SCL = PIN_WIRE_SCL;
85
86#ifdef __cplusplus
87extern "C" {
88#endif
89extern const PinName digitalPin[];
90extern const uint32_t analogInputPin[];
91
92#define NOT_AN_INTERRUPT (uint32_t)NC
93
94/* Convert a digital pin number Dxx to a PinName PX_n */
95#if NUM_ANALOG_INPUTS > 0
96/* Note: Analog pin is also a digital pin */
97#define digitalPinToPinName(p) ((((uint32_t)(p) & PNUM_MASK) < NUM_DIGITAL_PINS) ? \
98 (PinName)(digitalPin[(uint32_t)(p) & PNUM_MASK] | ((p) & ALTX_MASK)) : \
99 (((uint32_t)(p) & PNUM_ANALOG_BASE) == PNUM_ANALOG_BASE) && \
100 (((uint32_t)(p) & PNUM_MASK) < NUM_ANALOG_INTERNAL_FIRST) ? \
101 (PinName)(digitalPin[analogInputPin[(p) & PNUM_ANALOG_INDEX]] | ((p) & ALTX_MASK)) : NC)
102#else
103#define digitalPinToPinName(p) ((((uint32_t)(p) & PNUM_MASK) < NUM_DIGITAL_PINS) ? \
104 (PinName)(digitalPin[(uint32_t)(p) & PNUM_MASK] | ((p) & ALTX_MASK)) : NC)
105#endif /* NUM_ANALOG_INPUTS > 0 */
106/* Convert a PinName PX_n to a digital pin number */
107uint32_t pinNametoDigitalPin(PinName p);
108
109/* Convert an analog pin number to a digital pin number */
110#if NUM_ANALOG_INPUTS > 0
111/* Used by analogRead api to have A0 == 0 */
112/* Non contiguous analog pins definition in digitalPin array */
113#define analogInputToDigitalPin(p) ((((uint32_t)(p) & PNUM_MASK) < NUM_ANALOG_INPUTS) ? \
114 analogInputPin[(uint32_t)(p) & PNUM_MASK] | ((uint32_t)(p) & ALTX_MASK) : \
115 (((uint32_t)(p) & PNUM_ANALOG_BASE) == PNUM_ANALOG_BASE) && \
116 (((uint32_t)(p) & PNUM_MASK) < NUM_ANALOG_INTERNAL_FIRST) ? \
117 analogInputPin[(p) & PNUM_ANALOG_INDEX] | ((uint32_t)(p) & ALTX_MASK) : (uint32_t)NC)
118#else/* No analog pin defined */
119#define analogInputToDigitalPin(p) (NUM_DIGITAL_PINS)
120#endif /* NUM_ANALOG_INPUTS > 0 */
121
122/* Convert an analog pin number Ax to a PinName PX_n */
123PinName analogInputToPinName(uint32_t pin);
124
125/* All pins could manage EXTI */
126#define digitalPinToInterrupt(p) (digitalPinIsValid(p) ? p : NOT_AN_INTERRUPT)
127
128#define digitalPinHasI2C(p) (pin_in_pinmap(digitalPinToPinName(p), PinMap_I2C_SDA) ||\
129 pin_in_pinmap(digitalPinToPinName(p), PinMap_I2C_SCL))
130#define digitalPinHasPWM(p) (pin_in_pinmap(digitalPinToPinName(p), PinMap_TIM))
131#define digitalPinHasSerial(p) (pin_in_pinmap(digitalPinToPinName(p), PinMap_UART_RX) ||\
132 pin_in_pinmap(digitalPinToPinName(p), PinMap_UART_TX))
133#define digitalPinHasSPI(p) (pin_in_pinmap(digitalPinToPinName(p), PinMap_SPI_MOSI) ||\
134 pin_in_pinmap(digitalPinToPinName(p), PinMap_SPI_MISO) ||\
135 pin_in_pinmap(digitalPinToPinName(p), PinMap_SPI_SCLK) ||\
136 pin_in_pinmap(digitalPinToPinName(p), PinMap_SPI_SSEL))
137
138
139#define digitalPinToPort(p) (get_GPIO_Port(STM_PORT(digitalPinToPinName(p))))
140#define digitalPinToBitMask(p) (STM_GPIO_PIN(digitalPinToPinName(p)))
141
142#define analogInPinToBit(p) (STM_GPIO_PIN(digitalPinToPinName(p)))
143#define portOutputRegister(P) (&(P->ODR))
144#define portInputRegister(P) (&(P->IDR))
145
146#define portSetRegister(P) (&(P->BSRR))
147#if defined(STM32F2xx) || defined(STM32F4xx) || defined(STM32F7xx)
148/* For those series reset are in the high part so << 16U needed */
149#define portClearRegister(P) (&(P->BSRR))
150#else
151#define portClearRegister(P) (&(P->BRR))
152#endif
153
154
155#if defined(STM32F1xx)
156/*
157 * Config registers split in 2 registers:
158 * CRL: pin 0..7
159 * CRH: pin 8..15
160 * Return only CRL
161 */
162#define portModeRegister(P) (&(P->CRL))
163#else
164#define portModeRegister(P) (&(P->MODER))
165#endif
166#define portConfigRegister(P) (portModeRegister(P))
167
168
169#define digitalPinIsValid(p) (digitalPinToPinName(p) != NC)
170
171/* As some pin could be duplicated in digitalPin[] */
172/* return first occurrence of linked PinName (PYx) */
173#define digitalPinFirstOccurence(p) (pinNametoDigitalPin(digitalPinToPinName(p)))
174
175/* Specific for Firmata */
176#if defined(PIN_SERIAL_RX) && defined(PIN_SERIAL_TX)
177#define pinIsSerial(p) ((digitalPinToPinName(p) == \
178 digitalPinToPinName(PIN_SERIAL_RX & PNUM_MASK)) ||\
179 (digitalPinToPinName(p) == \
180 digitalPinToPinName(PIN_SERIAL_TX & PNUM_MASK)))
181#endif
182/* Convenient macro to handle Analog for Firmata */
183#define pinIsAnalogInput digitalpinIsAnalogInput
184bool digitalpinIsAnalogInput(uint32_t pin);
185uint32_t digitalPinToAnalogInput(uint32_t pin);
186
187#ifdef __cplusplus
188}
189#endif
190
191/* Default Definitions, could be redefined in variant.h */
192#ifndef ADC_RESOLUTION
193 #define ADC_RESOLUTION 10
194#endif
195
196#define DACC_RESOLUTION 12
197
198#ifndef PWM_RESOLUTION
199 #define PWM_RESOLUTION 8
200#endif
201
202_Static_assert((ADC_RESOLUTION > 0) &&(ADC_RESOLUTION <= 32),
203 "ADC_RESOLUTION must be 0 < x <= 32!");
204_Static_assert((PWM_RESOLUTION > 0) &&(PWM_RESOLUTION <= 32),
205 "PWM_RESOLUTION must be 0 < x <= 32!");
206
207#ifndef PWM_FREQUENCY
208 #define PWM_FREQUENCY 1000
209#endif
210#ifndef PWM_MAX_DUTY_CYCLE
211 #define PWM_MAX_DUTY_CYCLE 4095
212#endif
213
214#endif /*_PINS_ARDUINO_H_*/