-
Notifications
You must be signed in to change notification settings - Fork 21
/
pinSetup.h
401 lines (327 loc) · 15.8 KB
/
pinSetup.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#ifndef PINSETUP_H_INCLUDED
#define PINSETUP_H_INCLUDED
#ifdef __AVR_ATmega2560__
#define outputPin 23
#define INIT_OUTPORT DDRA |= _BV(1) // El pin23 es el bit1 del PORTA
#define WRITE_LOW PORTA &= ~_BV(1) // El pin23 es el bit1 del PORTA
#define WRITE_HIGH PORTA |= _BV(1) // El pin23 es el bit1 del PORTA
#elif defined(__AVR_ATmega4809__)
#define outputPin 9
//#define INIT_OUTPORT DDRB |= _BV(1) // El pin9 es el bit1 del PORTB
//#define INIT_OUTPORT pinMode(outputPin,OUTPUT)
#define INIT_OUTPORT VPORTB.DIR |= _BV(0) // El pin9 es PB0
//#define WRITE_LOW PORTB &= ~_BV(1) // El pin9 es el bit1 del PORTB
//#define WRITE_LOW digitalWrite(outputPin,LOW)
#define WRITE_LOW VPORTB.OUT &= ~_BV(0) // El pin9 es PB0
//#define WRITE_HIGH PORTB |= _BV(1) // El pin9 es el bit1 del PORTB
//#define WRITE_HIGH digitalWrite(outputPin,HIGH)
#define WRITE_HIGH VPORTB.OUT |= _BV(0) // El pin9 es PB0
#elif defined(__AVR_ATmega4808__)
#define outputPin 9
//#define INIT_OUTPORT pinMode(outputPin,OUTPUT)
#define INIT_OUTPORT VPORTA.DIR |= PIN7_bm // El pin9 es PA7
//#define WRITE_LOW digitalWrite(outputPin,LOW)
#define WRITE_LOW VPORTA.OUT &= ~PIN7_bm // El pin9 es PA7
//#define WRITE_HIGH digitalWrite(outputPin,HIGH)
#define WRITE_HIGH VPORTA.OUT |= PIN7_bm // El pin9 es PA7
#elif defined(__arm__) && defined(__STM32F1__)
#define outputPin PA9 // this pin is 5V tolerant and PWM output capable
#define INIT_OUTPORT pinMode(outputPin,OUTPUT)
//#define INIT_OUTPORT pinMode(outputPin,OUTPUT); GPIOA->regs->CRH |= 0x00000030
#define WRITE_LOW digitalWrite(outputPin,LOW)
//#define WRITE_LOW GPIOA->regs->ODR &= ~0b0000001000000000
//#define WRITE_LOW gpio_write_bit(GPIOA, 9, LOW)
#define WRITE_HIGH digitalWrite(outputPin,HIGH)
//#define WRITE_HIGH GPIOA->regs->ODR |= 0b0000001000000000
//#define WRITE_HIGH gpio_write_bit(GPIOA, 9, HIGH)
#elif defined(__AVR_ATmega32U4__)
#define outputPin 7 // this pin is 5V tolerant and PWM output capable
//#define INIT_OUTPORT pinMode(outputPin,OUTPUT)
#define INIT_OUTPORT DDRE |= _BV(6) // El pin PE6 es el bit6 del PORTE
//#define WRITE_LOW digitalWrite(outputPin,LOW)
#define WRITE_LOW PORTE &= ~_BV(6) // El pin PE6 es el bit6 del PORTE
//#define WRITE_HIGH digitalWrite(outputPin,HIGH)
#define WRITE_HIGH PORTE |= _BV(6) // El pin PE6 es el bit6 del PORTE
#elif defined(SEEED_XIAO_M0)
#define outputPin A0
#define INIT_OUTPORT pinMode(outputPin,OUTPUT)
//#define INIT_OUTPORT pinMode(outputPin,OUTPUT); GPIOA->regs->CRH |= 0x00000030
#define WRITE_LOW digitalWrite(outputPin,LOW)
//#define WRITE_LOW GPIOA->regs->ODR &= ~0b0000001000000000
//#define WRITE_LOW gpio_write_bit(GPIOA, 9, LOW)
#define WRITE_HIGH digitalWrite(outputPin,HIGH)
//#define WRITE_HIGH GPIOA->regs->ODR |= 0b0000001000000000
//#define WRITE_HIGH gpio_write_bit(GPIOA, 9, HIGH)
#elif defined(ARDUINO_XIAO_ESP32C3)
#define outputPin D0
#define INIT_OUTPORT pinMode(outputPin,OUTPUT)
#define WRITE_LOW digitalWrite(outputPin,LOW)
#define WRITE_HIGH digitalWrite(outputPin,HIGH)
#elif defined(ARDUINO_ESP8266_WEMOS_D1MINI)
#define outputPin 16 // D0
#define INIT_OUTPORT pinMode(outputPin,OUTPUT)
#define WRITE_LOW digitalWrite(outputPin,LOW)
#define WRITE_HIGH digitalWrite(outputPin,HIGH)
#else //__AVR_ATmega328P__
//#define MINIDUINO_AMPLI // For A.Villena's Miniduino new design
#define outputPin 9
#ifdef MINIDUINO_AMPLI
#define INIT_OUTPORT DDRB |= B00000011 // pin8+ pin9 es el bit0-bit1 del PORTB
#define WRITE_LOW (PORTB &= B11111101) |= B00000001 // pin8+ pin9 , bit0- bit1 del PORTB
#define WRITE_HIGH (PORTB |= B00000010) &= B11111110 // pin8+ pin9 , bit0- bit1 del PORTB
// #define WRITE_LOW PORTB = (PORTB & B11111101) | B00000001 // pin8+ pin9 , bit0- bit1 del PORTB
// #define WRITE_HIGH PORTB = (PORTB | B00000010) & B11111110 // pin8+ pin9 , bit0- bit1 del PORTB
#else
#define INIT_OUTPORT DDRB |= _BV(1) // El pin9 es el bit1 del PORTB
#define WRITE_LOW PORTB &= ~_BV(1) // El pin9 es el bit1 del PORTB
#define WRITE_HIGH PORTB |= _BV(1) // El pin9 es el bit1 del PORTB
#endif
// pin 0-7 PortD0-7, pin 8-13 PortB0-5, pin 14-19 PortC0-5
/*
#ifdef rpolarity
#define WRITE_LOW PORTB &= ~_BV(1) // El pin9 es el bit1 del PORTB
#define WRITE_HIGH PORTB |= _BV(1) // El pin9 es el bit1 del PORTB
// pin 0-7 PortD0-7, pin 8-13 PortB0-5, pin 14-19 PortC0-5
#endif
#ifndef rpolarity
#define WRITE_HIGH PORTB &= ~_BV(1) // El pin9 es el bit1 del PORTB
#define WRITE_LOW PORTB |= _BV(1) // El pin9 es el bit1 del PORTB
// pin 0-7 PortD0-7, pin 8-13 PortB0-5, pin 14-19 PortC0-5
#endif
*/
#endif
/////////////////////////////////////////////////////////////////////////////////////////////
//General Pin settings
//Setup buttons with internal pullup
#ifdef __AVR_ATmega2560__
const byte chipSelect = 53; //Sd card chip select pin
#define btnUp A0 //Up button
#define btnDown A1 //Down button
#define btnPlay A2 //Play Button
#define btnStop A3 //Stop Button
#define btnRoot A4 //Return to SD card root
// #define btnDelete A5 //Not implemented this button is for an optional function
#define btnMotor 6 //Motor Sense (connect pin to gnd to play, NC for pause)
#elif defined(__arm__) && defined(__STM32F1__)
//
// Pin definition for Blue Pill boards
//
#define chipSelect PB12 //Sd card chip select pin
#define btnPlay PA0 //Play Button
#define btnStop PA1 //Stop Button
#define btnUp PA2 //Up button
#define btnDown PA3 //Down button
#define btnMotor PA8 //Motor Sense (connect pin to gnd to play, NC for pause)
#define btnRoot PA4 //Return to SD card root
#elif defined(__AVR_ATmega32U4__)
#define NO_MOTOR
const byte chipSelect = SS; //Sd card chip select pin
#define btnPlay 4 //Play Button
#define btnStop 30 //Stop Button
#define btnUp 6 //Up button
#define btnDown 12 //Down button
#define btnMotor 0 //Motor Sense (connect pin to gnd to play, NC for pause)
#define btnRoot 1 //Return to SD card root
#elif defined(SEEED_XIAO_M0)
//
// Pin definition for Seeeduino Xiao M0 boards
//
#define chipSelect 12 //Sd card chip select pin - map to LED (on assumption that SD CS is actually just tied directly to GND)
#define BUTTON_ADC
#define btnADC A2 // analog input pin for ADC buttons
#define NO_MOTOR // because no spare gpio
#elif defined(ARDUINO_XIAO_ESP32C3)
//
// Pin definition for Seeeduino Xiao ESP32C3 boards
//
#define chipSelect D7
#define BUTTON_ADC
#define btnADC A2 // analog input pin for ADC buttons // CHANGED!!!
#define NO_MOTOR // because no spare gpio
#elif defined(ARDUINO_ESP8266_WEMOS_D1MINI)
//
// Pin definition for Wemos D1 Mini (ESP8266) boards
//
#define chipSelect 15
#define BUTTON_ADC
#define btnADC A0
#define btnMotor 2
#else
const byte chipSelect = 10; //Sd card chip select pin
#define btnPlay 17 //Play Button
#define btnStop 16 //Stop Button
#define btnUp 15 //Up button
#define btnDown 14 //Down button
#define btnMotor 6 //Motor Sense (connect pin to gnd to play, NC for pause)
#define btnRoot 7 //Return to SD card root
#endif
#ifdef __AVR_ATmega2560__
// Analog pin A0
PORTF |= _BV(0);
// Analog pin A1
PORTF |= _BV(1);
// Analog pin A2
PORTF |= _BV(2);
// Analog pin A3
PORTF |= _BV(3);
// Analog pin A4
PORTF |= _BV(4);
// Analog pin A5
//PORTF |= _BV(5);
// Digital pin 6
PORTH |= _BV(3);
#elif defined(__AVR_ATmega4809__)
//pinMode(btnPlay,INPUT_PULLUP); // Not needed, default is INPUT (0)
//digitalWrite(btnPlay,HIGH); // 17 PD0
//PORTD.PIN0CTRL |=0B00001000;
PORTD.PIN0CTRL |=PORT_PULLUPEN_bm; /* Enable the internal pullup */
//VPORTC.DIR |= ~PIN3_bm;
//PORTC.DIR |= ~PIN3_bm; /* Configure PC3 as digital input */
//PORTC.PIN3CTRL = PORT_PULLUPEN_bm; /* Enable the internal pullup */
//digitalWrite(btnPlay,HIGH);
// VPORTC.OUT |= _BV(3);
//PORTC |= _BV(3);
//pinMode(btnStop,INPUT_PULLUP); // Not needed, default is INPUT (0)
//digitalWrite(btnStop,HIGH); // 16 PD1
PORTD.PIN1CTRL |=PORT_PULLUPEN_bm; /* Enable the internal pullup */
//VPORTC.OUT |= _BV(2);
//PORTC |= _BV(2);
//pinMode(btnUp,INPUT_PULLUP); // Not needed, default is INPUT (0)
//digitalWrite(btnUp,HIGH); // 15 PD2
PORTD.PIN2CTRL |=PORT_PULLUPEN_bm; /* Enable the internal pullup */
//VPORTC.OUT |= _BV(1);
//PORTC |= _BV(1);
//pinMode(btnDown,INPUT_PULLUP); // Not needed, default is INPUT (0)
//digitalWrite(btnDown,HIGH); // 14 PD3 also to enbale PULLUP if PINMODE is INPUT
PORTD.PIN3CTRL |=PORT_PULLUPEN_bm; /* Enable the internal pullup */
//VPORTC.OUT |= _BV(0);
//PORTC |= _BV(0);
//pinMode(btnMotor, INPUT_PULLUP); // Not needed, default is INPUT (0)
//digitalWrite(btnMotor,HIGH); // 6 PF4
PORTF.PIN4CTRL |=PORT_PULLUPEN_bm; /* Enable the internal pullup */
//VPORTD.OUT |= _BV(btnMotor);
//PORTD |= _BV(btnMotor);
//pinMode(btnRoot, INPUT_PULLUP); // Not needed, default is INPUT (0)
//digitalWrite(btnRoot, HIGH); // 7 PA1
PORTA.PIN1CTRL |=PORT_PULLUPEN_bm; /* Enable the internal pullup */
//VPORTD.OUT |= _BV(btnRoot);
//PORTD |= _BV(btnRoot);
#elif defined(__AVR_ATmega4808__)
//pinMode(btnPlay,INPUT_PULLUP); // Not needed, default is INPUT (0)
//digitalWrite(btnPlay,HIGH); // 17 PD3
VPORTD.DIR |= ~PIN3_bm;
PORTD.PIN3CTRL |=PORT_PULLUPEN_bm; /* Enable the internal pullup */
VPORTD.OUT |= PIN3_bm;
//pinMode(btnStop,INPUT_PULLUP); // Not needed, default is INPUT (0)
//digitalWrite(btnStop,HIGH); // 16 PD2
VPORTD.DIR |= ~PIN2_bm;
PORTD.PIN2CTRL |=PORT_PULLUPEN_bm; /* Enable the internal pullup */
VPORTD.OUT |= PIN2_bm;
//pinMode(btnUp,INPUT_PULLUP); // Not needed, default is INPUT (0)
//digitalWrite(btnUp,HIGH); // 15 PD1
VPORTD.DIR |= ~PIN1_bm;
PORTD.PIN1CTRL |=PORT_PULLUPEN_bm; /* Enable the internal pullup */
VPORTD.OUT |= PIN1_bm;
//pinMode(btnDown,INPUT_PULLUP); // Not needed, default is INPUT (0)
//digitalWrite(btnDown,HIGH); // 14 PD0 also to enbale PULLUP if PINMODE is INPUT
VPORTD.DIR |= ~PIN0_bm;
PORTD.PIN0CTRL |=PORT_PULLUPEN_bm; /* Enable the internal pullup */
VPORTD.OUT |= PIN0_bm;
//pinMode(btnMotor, INPUT_PULLUP); // Not needed, default is INPUT (0)
//digitalWrite(btnMotor,HIGH); // 6 PA4
VPORTA.DIR |= ~PIN4_bm;
PORTA.PIN4CTRL |=PORT_PULLUPEN_bm; /* Enable the internal pullup */
VPORTA.OUT |= PIN4_bm;
//pinMode(btnRoot, INPUT_PULLUP); // Not needed, default is INPUT (0)
//digitalWrite(btnRoot, HIGH); // 7 PA5
VPORTA.DIR |= ~PIN5_bm;
PORTA.PIN5CTRL |=PORT_PULLUPEN_bm; /* Enable the internal pullup */
VPORTA.OUT |= PIN5_bm;
#elif defined(__arm__) && defined(__STM32F1__)
//General Pin settings
//Setup buttons with internal pullup
pinMode(btnPlay,INPUT_PULLUP);
digitalWrite(btnPlay,HIGH);
pinMode(btnStop,INPUT_PULLUP);
digitalWrite(btnStop,HIGH);
pinMode(btnUp,INPUT_PULLUP);
digitalWrite(btnUp,HIGH);
pinMode(btnDown,INPUT_PULLUP);
digitalWrite(btnDown,HIGH);
pinMode(btnMotor, INPUT_PULLUP);
digitalWrite(btnMotor,HIGH);
pinMode(btnRoot, INPUT_PULLUP);
digitalWrite(btnRoot, HIGH);
#elif defined(__AVR_ATmega32U4__)
// pinMode(btnPlay,INPUT_PULLUP); // Not needed, default is INPUT (0)
// digitalWrite(btnPlay,HIGH); // Wrte for INPUT_PULLUP if input type is only INPUT
PORTD |= _BV(4);
// pinMode(btnStop,INPUT_PULLUP); // Not needed, default is INPUT (0)
// digitalWrite(btnStop,HIGH);
PORTD |= _BV(5);
// pinMode(btnUp,INPUT_PULLUP); // Not needed, default is INPUT (0)
// digitalWrite(btnUp,HIGH);
PORTD |= _BV(7);
// pinMode(btnDown,INPUT_PULLUP); // Not needed, default is INPUT (0)
// digitalWrite(btnDown,HIGH);
PORTD |= _BV(6);
// pinMode(btnMotor, INPUT_PULLUP); // Not needed, default is INPUT (0)
// digitalWrite(btnMotor,HIGH);
PORTD |= _BV(2);
// pinMode(btnRoot, INPUT_PULLUP); // Not needed, default is INPUT (0)
// digitalWrite(btnRoot, HIGH);
PORTD |= _BV(3);
#elif defined(SEEED_XIAO_M0) || defined(ARDUINO_XIAO_ESP32C3) || defined(ARDUINO_ESP8266_WEMOS_D1MINI)
// BUTTON PIN CONFIGURATION
// n.a.
#else //__AVR_ATmega328P__
//pinMode(btnPlay,INPUT_PULLUP); // Not needed, default is INPUT (0)
// digitalWrite(btnPlay,HIGH); // Wrte for INPUT_PULLUP if input type is only INPUT
PORTC |= _BV(3);
//pinMode(btnStop,INPUT_PULLUP); // Not needed, default is INPUT (0)
// digitalWrite(btnStop,HIGH);
PORTC |= _BV(2);
//pinMode(btnUp,INPUT_PULLUP); // Not needed, default is INPUT (0)
// digitalWrite(btnUp,HIGH);
PORTC |= _BV(1);
//pinMode(btnDown,INPUT_PULLUP); // Not needed, default is INPUT (0)
// digitalWrite(btnDown,HIGH);
PORTC |= _BV(0);
//pinMode(btnMotor, INPUT_PULLUP); // Not needed, default is INPUT (0)
// digitalWrite(btnMotor,HIGH);
PORTD |= _BV(btnMotor);
//pinMode(btnRoot, INPUT_PULLUP); // Not needed, default is INPUT (0)
// digitalWrite(btnRoot, HIGH);
PORTD |= _BV(btnRoot);
#endif
#ifdef BUTTON_ADC
// Each button acts as a voltage divider between 10k and the following resistors:
// 0 Ohm i.e. 100%
// 2.2k Ohm i.e. 82% (10 : 12.2)
// 4.7k Ohm i.e. 68% (10 : 14.7)
// 10k Ohm i.e. 50% (10 : 20)
// 20k Ohm i.e. 33% (10 : 30)
// For a 10-bit ADC, each button is calibrated to the band between this value and the next value above
// (or 1023 for upper limit).
// The bands are intentionally set very wide, and far apart
// However note that ESP ADC is nonlinear and not full-scale, so the resistor
// values must be chosen to avoid ranges at the extreme top (100%) end.
// The resistor values and bands chosen here are compatible with ESP devices
#if defined(ESP32) || defined(ESP8266)
// ESP ADC is nonlinear, and also not full scale, so the values are different!
// because not full scale, a 1k:10k voltage divider (i.e. 90%) is undetectable
// and reads as 1023 still, so resistor values have been altered to create better spacing
#define btnADCPlayLow 1020 // 0 ohm reading 1023 due to saturation
#define btnADCStopLow 900 // 2.2k ohm reading around 960
#define btnADCRootLow 700 // 4.7k ohm reading around 800
#define btnADCDownLow 500 // 10k ohm reading around 590
#define btnADCUpLow 200 // 20k ohm reading around 390
#else
#define btnADCPlayLow 950 // 0 ohm reading around 1000, ideally 1023
#define btnADCStopLow 800 // 2.2k ohm reading around 840
#define btnADCRootLow 600 // 4.7k ohm reading around 695
#define btnADCDownLow 420 // 10k ohm reading around 510
#define btnADCUpLow 200 // 20k ohm reading around 340
#endif
#endif // BUTTON_ADC
#endif // #define PINSETUP_H_INCLUDED