Skip to content

Commit a13bd0d

Browse files
committed
do the mapping from pin number to ADC channel like it is done for arduino-1.8
1 parent 2148482 commit a13bd0d

3 files changed

Lines changed: 48 additions & 61 deletions

File tree

sduino/hardware/sduino/stm8/cores/sduino/wiring_analog.c

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,6 @@
2727
#include "wiring_private.h"
2828
#include "pins_arduino.h"
2929

30-
const uint8_t digitalPinToAnalogChannelMap[] = {
31-
NO_ANALOG, // PC3, 5
32-
0, // PC4, 6, Ain2 = A0
33-
NO_ANALOG, // PC5, 7
34-
NO_ANALOG, // PC6, 8
35-
NO_ANALOG, // PC7, 9
36-
NO_ANALOG, // PD1, 10
37-
1, // PD2, 11, Ain3 = A1
38-
2, // PD3, 12, Ain4 = A2
39-
NO_ANALOG, // PD4, 13
40-
3, // PD5, 14, Ain5 = A3
41-
4 // PD6, 15, Ain6 = A4
42-
};
43-
4430
/*
4531
uint8_t analog_reference = DEFAULT;
4632
@@ -57,20 +43,26 @@ int analogRead(uint8_t pin)
5743
{
5844
uint8_t low, high;
5945

60-
if (pin>=NUM_DIGITAL_PINS) return 0; // illegal pin number
61-
if (pin>=NUM_ANALOG_INPUTS)
62-
pin = digitalPinToAnalogChannelMap[pin-NUM_ANALOG_INPUTS];
63-
if (pin>=NUM_ANALOG_INPUTS) return 0; // illegal pin number
46+
#if defined(analogPinToChannel)
47+
pin = analogPinToChannel(pin);
48+
#else
49+
// default case: use the last (NUM_ANALOG_INPUTS) pins for analog
50+
if (pin >= NUM_ANALOG_INPUTS) {
51+
pin -= NUM_DIGITAL_INPUTS - NUM_ANALOG_INPUTS;
52+
}
53+
#endif
54+
// note there is no range check anymore
55+
6456
#ifdef USE_SPL
6557
// using spl functions:
66-
ADC1_ConversionConfig(ADC1_CONVERSIONMODE_SINGLE, pin+2, ADC1_ALIGN_RIGHT);
58+
ADC1_ConversionConfig(ADC1_CONVERSIONMODE_SINGLE, pin & 15, ADC1_ALIGN_RIGHT);
6759
ADC1_PrescalerConfig(ADC1_PRESSEL_FCPU_D18);
6860
ADC1_ExternalTriggerConfig(ADC1_EXTTRIG_TIM, DISABLE);
69-
ADC1_SchmittTriggerConfig(pin+2, DISABLE);
61+
ADC1_SchmittTriggerConfig(pin, DISABLE);
7062
#else
7163
// direct register access:
7264
// select channel
73-
ADC1->CSR = pin+2; // arduino channel 0 is Ain2 on STM8S103
65+
ADC1->CSR = pin & 15;
7466
bitSet(ADC1->CR2, 3); // right align
7567
#endif
7668
// first write turns on the ADC

sduino/hardware/sduino/stm8/variants/standard/pins_arduino.h

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -140,25 +140,13 @@ static const uint8_t A4 = PIN_A4;
140140
#define A3 PIN_A3
141141
#define A4 PIN_A4
142142

143-
#define NO_ANALOG 0xff
143+
// Distinguish between ADC channel number and digital pin number.
144+
// Note that for value 6 both ranges overlap and it is used a pin number.
145+
//
146+
// values 0..5: ADC channel number, no conversion
147+
// values 6..15: digital pin numbers, convert to ADC channel number
148+
#define analogPinToChannel(P) ( (P)<6 ? (P) : digitalPinToAnalogChannelMap[(P-6)] )
144149
extern const uint8_t digitalPinToAnalogChannelMap[];
145-
//FIXME: the actual definition is now in wiring_analog.c,
146-
// but it really should be here
147-
/*
148-
= {
149-
NO_ANALOG, // PC3, 5
150-
0, // PC4, 6, Ain2 = A0
151-
NO_ANALOG, // PC5, 7
152-
NO_ANALOG, // PC6, 8
153-
NO_ANALOG, // PC7, 9
154-
NO_ANALOG, // PD1, 10
155-
1, // PD2, 11, Ain3 = A1
156-
2, // PD3, 12, Ain4 = A2
157-
NO_ANALOG, // PD4, 13
158-
3, // PD5, 14, Ain5 = A3
159-
4 // PD6, 15, Ain6 = A4
160-
};
161-
*/
162150

163151
/*FIXME
164152
#define digitalPinToPCICR(p) (((p) >= 0 && (p) <= 21) ? (&PCICR) : ((uint8_t *)0))
@@ -298,7 +286,22 @@ const uint8_t PROGMEM digital_pin_to_timer_PGM[] = {
298286
};
299287

300288

301-
#endif
289+
#define NO_ANALOG 0xff
290+
291+
const uint8_t digitalPinToAnalogChannelMap[] = {
292+
2, // A0 D6 PC4 Ain2
293+
NO_ANALOG, // D7 PC5
294+
NO_ANALOG, // D8 PC6
295+
NO_ANALOG, // D9 PC7
296+
NO_ANALOG, // D10 PD1
297+
3, // A1 D11 PD2 Ain3
298+
4, // A2 D12 PD3 Ain4
299+
NO_ANALOG, // D13 PD4
300+
5, // A3 D14 PD5 Ain5
301+
6 // A4 D15 PD6 Ain6
302+
};
303+
304+
#endif /* ARDUINO_MAIN */
302305

303306
// These serial port names are intended to allow libraries and architecture-neutral
304307
// sketches to automatically default to the correct port name for a particular type

sduino/hardware/sduino/stm8/variants/stm8sdisco/pins_arduino.h

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -162,25 +162,17 @@ static const uint8_t A4 = PIN_A4;
162162
#define A8 PIN_A8
163163
#define A9 PIN_A9
164164

165-
#define NO_ANALOG 0xff
166-
extern const uint8_t digitalPinToAnalogChannelMap[];
167-
//FIXME: the actual definition is now in wiring_analog.c,
168-
// but it really should be here
169-
/*
170-
= {
171-
NO_ANALOG, // PC3, 5
172-
0, // PC4, 6, Ain2 = A0
173-
NO_ANALOG, // PC5, 7
174-
NO_ANALOG, // PC6, 8
175-
NO_ANALOG, // PC7, 9
176-
NO_ANALOG, // PD1, 10
177-
1, // PD2, 11, Ain3 = A1
178-
2, // PD3, 12, Ain4 = A2
179-
NO_ANALOG, // PD4, 13
180-
3, // PD5, 14, Ain5 = A3
181-
4 // PD6, 15, Ain6 = A4
182-
};
183-
*/
165+
//#define NO_ANALOG 0xff
166+
167+
// map the logical pin numbers to the physical ADC channels:
168+
// pin 28,29 -> channel 9,8 (reverse order!)
169+
// pin 30..38 -> channel 0-7
170+
// smaller numbers are not modified but used as channel numbers directly.
171+
#define analogPinToChannel(P) ( (P)>=30 ? (P)-30 : ( \
172+
(P)>=28 ? 37-(P) : \
173+
(P) \
174+
))
175+
184176

185177
/*FIXME
186178
#define digitalPinToPCICR(p) (((p) >= 0 && (p) <= 21) ? (&PCICR) : ((uint8_t *)0))
@@ -219,7 +211,7 @@ extern const uint8_t digitalPinToAnalogChannelMap[];
219211
// 1 PA5 |11 12| PA6 0 26~ PC1 | 2 *1| PE5 27 SS
220212
// +-----+ +-----+
221213
//
222-
// A9 A7 A5 A3 A1
214+
// A8 A6 A4 A2 A0
223215
// Pin - 36 34 32 30 28
224216
// Port VssAPB6 PB4 PB2 PB0 PE6
225217
// +-----------------------+
@@ -228,7 +220,7 @@ extern const uint8_t digitalPinToAnalogChannelMap[];
228220
// +-------- --------+
229221
// Port VddAPB7 PB5 PB3 PB1 PE7
230222
// Pin - 37 35 33 31 29
231-
// A8 A6 A4 A2 A0
223+
// A9 A7 A5 A3 A1
232224

233225
// these arrays map port names (e.g. port B) to the
234226
// appropriate addresses for various functions (e.g. reading

0 commit comments

Comments
 (0)