forked from stm32duino/Arduino_Core_STM32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpins_arduino.c
120 lines (110 loc) · 2.9 KB
/
pins_arduino.c
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
/*
Copyright (c) 2011 Arduino. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "pins_arduino.h"
#ifdef __cplusplus
extern "C" {
#endif
WEAK uint32_t pinNametoDigitalPin(PinName p)
{
uint32_t i = NUM_DIGITAL_PINS;
if (STM_VALID_PINNAME(p)) {
for (i = 0; i < NUM_DIGITAL_PINS; i++) {
if (digitalPin[i] == (p & PNAME_MASK)) {
i |= ((uint32_t)(p) & ALTX_MASK);
break;
}
}
}
return i;
}
PinName analogInputToPinName(uint32_t pin)
{
PinName pn = digitalPinToPinName(analogInputToDigitalPin(pin));
if (pn == NC) {
switch (pin) {
#if defined(ADC_CHANNEL_TEMPSENSOR) || defined(ADC_CHANNEL_TEMPSENSOR_ADC1)
case ATEMP:
pn = PADC_TEMP;
break;
#endif
#if defined(ADC5) && defined(ADC_CHANNEL_TEMPSENSOR_ADC5)
case ATEMP_ADC5:
pn = PADC_TEMP_ADC5;
break;
#endif
#ifdef AVREF
case AVREF:
pn = PADC_VREF;
break;
#endif
#ifdef AVBAT
case AVBAT:
pn = PADC_VBAT;
break;
#endif
default:
break;
}
}
return pn;
}
/**
* @brief Return true if a digital pin is an analog input
* @param pin Dx, x or PYn
* @retval boolean true if analog or false
*/
bool digitalpinIsAnalogInput(uint32_t pin)
{
bool ret = false;
#if NUM_ANALOG_INPUTS > 0
if ((pin & PNUM_ANALOG_BASE) == PNUM_ANALOG_BASE) {
ret = true;
} else {
for (uint32_t i = 0; i < NUM_ANALOG_INPUTS; i++) {
if (analogInputPin[i] == (pin & PNUM_MASK)) {
ret = true;
break;
}
}
}
#endif /* NUM_ANALOG_INPUTS > 0 */
return ret;
}
/**
* @brief Return the analog input linked to a digital pin
* @param pin Dx, x or PYn
* @retval analogInput valid analog input or NUM_ANALOG_INPUTS
*/
uint32_t digitalPinToAnalogInput(uint32_t pin)
{
uint32_t ret = NUM_ANALOG_INPUTS;
#if NUM_ANALOG_INPUTS > 0
if ((pin & PNUM_ANALOG_BASE) == PNUM_ANALOG_BASE) {
/* PYn = Ax */
ret = (pin & PNUM_ANALOG_INDEX) | (pin & ALTX_MASK);
} else {
for (uint32_t i = 0; i < NUM_ANALOG_INPUTS; i++) {
if (analogInputPin[i] == (pin & PNUM_MASK)) {
ret = i | (pin & ALTX_MASK);
break;
}
}
}
#endif /* NUM_ANALOG_INPUTS > 0 */
return ret;
}
#ifdef __cplusplus
}
#endif