aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPerry Hung <iperry@alum.mit.edu>2010-03-23 00:55:40 -0400
committerPerry Hung <iperry@alum.mit.edu>2010-03-23 01:25:00 -0400
commit3639ad2cf027da7425ebe76382842c006acec05a (patch)
tree65682fc68e0625c4ff41f7cdc169482ec6d77672
parent48be688f451e81d2a81c76a85dadf18093e672ab (diff)
downloadlibrambutan-3639ad2cf027da7425ebe76382842c006acec05a.tar.gz
librambutan-3639ad2cf027da7425ebe76382842c006acec05a.zip
Unified analog, digital, and timer pin mappings to implement the mapping
we discussed. There's no such thing as A0-A15 anymore. You should now be able to do something like: unsigned int val; pinMode(15, INPUT_ANALOG); val = analogRead(15);
-rw-r--r--src/main.cpp10
-rw-r--r--src/wiring/io.h29
-rw-r--r--src/wiring/pwm.c25
-rw-r--r--src/wiring/wiring.h1
-rw-r--r--src/wiring/wiring_analog.c24
-rw-r--r--src/wiring/wiring_digital.c121
6 files changed, 85 insertions, 125 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 9c988ec..013ffdf 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -4,18 +4,18 @@
#include "HardwareUsb.h"
#include "usb.h"
-int ledPin = 13;
-HardwareUsb Usb;
+#define TEST_PIN 13
void setup() {
- pinMode(ledPin, OUTPUT);
+ pinMode(TEST_PIN, OUTPUT);
}
int toggle = 1;
+
void loop() {
- digitalWrite(ledPin, toggle);
+ digitalWrite(TEST_PIN, toggle);
toggle ^= 1;
- delay(1000);
+ delay(100);
}
int main(void) {
diff --git a/src/wiring/io.h b/src/wiring/io.h
index a313b2b..9f6aa4a 100644
--- a/src/wiring/io.h
+++ b/src/wiring/io.h
@@ -27,6 +27,8 @@
#define _IO_H
#include <inttypes.h>
+#include "gpio.h"
+#include "adc.h"
#ifdef __cplusplus
extern "C"{
@@ -75,23 +77,6 @@ extern "C"{
#define D38 38
#define D39 39
-#define A0 D14
-#define A1 D15
-#define A2 D16
-#define A3 D17
-#define A4 D18
-#define A5 D19
-#define A6 D0
-#define A7 D1
-#define A8 D2
-#define A9 D3
-#define A10 D10
-#define A11 D11
-#define A12 D12
-#define A13 D13
-#define A14 D26
-#define A15 D11
-
typedef enum WiringPinMode {
OUTPUT,
OUTPUT_OPEN_DRAIN,
@@ -103,6 +88,16 @@ typedef enum WiringPinMode {
PWM
} WiringPinMode;
+typedef struct PinMapping {
+ GPIO_Port *port;
+ uint32 pin;
+ uint32 adc;
+ TimerCCR timer_channel;
+} PinMapping;
+
+#define ADC_INVALID 0xFFFFFFFF
+#define TIMER_INVALID (TimerCCR)0xFFFFFFFF
+
/* Set pin to mode
* pinMode(pin, mode):
* pin -> {0-38, D0-D39, A0-16}
diff --git a/src/wiring/pwm.c b/src/wiring/pwm.c
index e6e100f..2cc815c 100644
--- a/src/wiring/pwm.c
+++ b/src/wiring/pwm.c
@@ -25,27 +25,10 @@
#include "wiring.h"
#include "timers.h"
-#include "gpio.h"
+#include "io.h"
#include "pwm.h"
-#define NOT_A_TIMER 0
-
-static const TimerCCR PIN_TO_TIMER[NR_MAPLE_PINS] = {
- TIMER2_CH4_CCR, // D0/A6
- TIMER2_CH3_CCR, // D1/A7
- TIMER2_CH1_CCR, // D2/A8
- TIMER2_CH2_CCR, // D3/A9
- NOT_A_TIMER, // D4
- TIMER4_CH1_CCR, // D5
- TIMER1_CH1_CCR, // D6
- TIMER1_CH2_CCR, // D7
- TIMER1_CH3_CCR, // D8
- TIMER4_CH2_CCR, // D9
- NOT_A_TIMER, // D10/A10
- TIMER3_CH2_CCR, // D11/A11
- TIMER3_CH1_CCR, // D12/A12
- NOT_A_TIMER, // D13/A13
-};
+extern const PinMapping PIN_MAP[NR_MAPLE_PINS];
void pwmWrite(uint8_t pin, uint16_t duty_cycle) {
TimerCCR ccr;
@@ -54,9 +37,9 @@ void pwmWrite(uint8_t pin, uint16_t duty_cycle) {
return;
}
- ccr = PIN_TO_TIMER[pin];
+ ccr = PIN_MAP[pin].timer_channel;
- if (ccr == NOT_A_TIMER)
+ if (ccr == TIMER_INVALID)
return;
timer_pwm_write_ccr(ccr, duty_cycle);
diff --git a/src/wiring/wiring.h b/src/wiring/wiring.h
index 5333464..bcf6eda 100644
--- a/src/wiring/wiring.h
+++ b/src/wiring/wiring.h
@@ -14,7 +14,6 @@
extern "C"{
#endif
-
#define MAPLE 1
#define NR_MAPLE_PINS 39 // temporary
diff --git a/src/wiring/wiring_analog.c b/src/wiring/wiring_analog.c
index 0426f85..d7d7150 100644
--- a/src/wiring/wiring_analog.c
+++ b/src/wiring/wiring_analog.c
@@ -25,27 +25,9 @@
#include "libmaple.h"
#include "wiring.h"
-#include "adc.h"
+#include "io.h"
-/* Indexed by pins A[0-15] */
-uint32_t PIN_TO_ADC[NR_ANALOG_PINS] = {
- 10, // A0/D14 ADC10
- 11, // A1/D15 ADC11
- 12, // A2/D16 ADC12
- 13, // A3/D17 ADC13
- 14, // A4/D18 ADC14
- 15, // A5/D19 ADC15
- 3, // A6/D0 ADC3
- 2, // A7/D1 ADC2
- 0, // A8/D2 ADC0
- 1, // A9/D3 ADC1
- 4, // A10/D10 ADC4
- 7, // A11/D11 ADC7
- 6, // A12/D12 ADC6
- 5, // A13/D13 ADC5
- 8, // A14/D26 ADC8
- 9, // A15/D11 ADC9
-};
+extern const PinMapping PIN_MAP[NR_MAPLE_PINS];
/* Assumes that the ADC has been initialized and
* that the pin is set to ANALOG_INPUT */
@@ -53,5 +35,5 @@ uint32_t analogRead(uint8_t pin) {
if (pin >= NR_ANALOG_PINS)
return 0;
- return adc_read(PIN_TO_ADC[pin]);
+ return adc_read(PIN_MAP[pin].adc);
}
diff --git a/src/wiring/wiring_digital.c b/src/wiring/wiring_digital.c
index b1be9a5..71dae57 100644
--- a/src/wiring/wiring_digital.c
+++ b/src/wiring/wiring_digital.c
@@ -25,66 +25,67 @@
#include "wiring.h"
#include "io.h"
-#include "gpio.h"
-typedef enum AFMode{
- AF_NONE,
- AF_PWM,
- AF_SERIAL,
- AF_I2C, // unused for now
- AF_SPI, // unusued for now
-} AFMode;
-
-
-typedef struct PinGPIOMapping {
- GPIO_Port *port;
- uint32 pin;
-} PinGPIOMapping;
-
-
-/* Reset state is input floating */
-static const PinGPIOMapping PIN_TO_GPIO[NR_MAPLE_PINS] = {
- {GPIOA_BASE, 3}, // D0/PA3
- {GPIOA_BASE, 2}, // D1/PA2
- {GPIOA_BASE, 0}, // D2/PA0
- {GPIOA_BASE, 1}, // D3/PA1
- {GPIOB_BASE, 5}, // D4/PB5
- {GPIOB_BASE, 6}, // D5/PB6
- {GPIOA_BASE, 8}, // D6/PA8
- {GPIOA_BASE, 9}, // D7/PA9
- {GPIOA_BASE, 10}, // D8/PA10
- {GPIOB_BASE, 7}, // D9/PB7
- {GPIOA_BASE, 4}, // D10/PA4
- {GPIOA_BASE, 7}, // D11/PA7
- {GPIOA_BASE, 6}, // D12/PA6
- {GPIOA_BASE, 5}, // D13/PA5
- {GPIOB_BASE, 8}, // D14/PB8
+#define ADC0 0
+#define ADC1 1
+#define ADC2 2
+#define ADC3 3
+#define ADC4 4
+#define ADC5 5
+#define ADC6 6
+#define ADC7 7
+#define ADC8 8
+#define ADC9 9
+#define ADC10 10
+#define ADC11 11
+#define ADC12 12
+#define ADC13 13
+#define ADC14 14
+#define ADC15 15
+#define ADC16 16
+
+const PinMapping PIN_MAP[NR_MAPLE_PINS] = {
+ {GPIOA_BASE, 3, ADC3, TIMER2_CH4_CCR}, // D0/PA3
+ {GPIOA_BASE, 2, ADC2, TIMER2_CH3_CCR}, // D1/PA2
+ {GPIOA_BASE, 0, ADC0, TIMER2_CH1_CCR}, // D2/PA0
+ {GPIOA_BASE, 1, ADC1, TIMER2_CH2_CCR}, // D3/PA1
+ {GPIOB_BASE, 5, ADC_INVALID, TIMER_INVALID}, // D4/PB5
+ {GPIOB_BASE, 6, ADC_INVALID, TIMER4_CH1_CCR}, // D5/PB6
+ {GPIOA_BASE, 8, ADC_INVALID, TIMER1_CH1_CCR}, // D6/PA8
+ {GPIOA_BASE, 9, ADC_INVALID, TIMER1_CH2_CCR}, // D7/PA9
+ {GPIOA_BASE, 10, ADC_INVALID, TIMER1_CH3_CCR}, // D8/PA10
+ {GPIOB_BASE, 7, ADC_INVALID, TIMER4_CH2_CCR}, // D9/PB7
+ {GPIOA_BASE, 4, ADC4, TIMER_INVALID}, // D10/PA4
+ {GPIOA_BASE, 7, ADC7, TIMER3_CH2_CCR}, // D11/PA7
+ {GPIOA_BASE, 6, ADC6, TIMER3_CH1_CCR}, // D12/PA6
+ {GPIOA_BASE, 5, ADC5, TIMER_INVALID}, // D13/PA5
+ {GPIOB_BASE, 8, ADC_INVALID, TIMER4_CH3_CCR}, // D14/PB8
/* Little header */
- {GPIOC_BASE, 0}, // D15/PC0
- {GPIOC_BASE, 1}, // D16/PC1
- {GPIOC_BASE, 2}, // D17/PC2
- {GPIOC_BASE, 3}, // D18/PC3
- {GPIOC_BASE, 4}, // D19/PC4
- {GPIOC_BASE, 5}, // D20/PC5
+ {GPIOC_BASE, 0, ADC10, TIMER_INVALID}, // D15/PC0
+ {GPIOC_BASE, 1, ADC11, TIMER_INVALID}, // D16/PC1
+ {GPIOC_BASE, 2, ADC12, TIMER_INVALID}, // D17/PC2
+ {GPIOC_BASE, 3, ADC13, TIMER_INVALID}, // D18/PC3
+ {GPIOC_BASE, 4, ADC14, TIMER_INVALID}, // D19/PC4
+ {GPIOC_BASE, 5, ADC15, TIMER_INVALID}, // D20/PC5
/* External header */
- {GPIOC_BASE, 13}, // D21/PC13
- {GPIOC_BASE, 14}, // D22/PC14
- {GPIOC_BASE, 15}, // D23/PC15
- {GPIOB_BASE, 9}, // D24/PB9
- {GPIOD_BASE, 2}, // D25/PD2
- {GPIOC_BASE, 10}, // D26/PC10
- {GPIOB_BASE, 0}, // D27/PB0
- {GPIOB_BASE, 1}, // D28/PB1
- {GPIOB_BASE, 10}, // D29/PB10
- {GPIOB_BASE, 11}, // D30/PB11
- {GPIOB_BASE, 12}, // D31/PB12
- {GPIOB_BASE, 13}, // D32/PB13
- {GPIOB_BASE, 14}, // D33/PB14
- {GPIOB_BASE, 15}, // D34/PB15
- {GPIOC_BASE, 6}, // D35/PC6
- {GPIOC_BASE, 7}, // D36/PC7
- {GPIOC_BASE, 8}, // D37/PC8
- {GPIOC_BASE, 9}, // D38/PC9
+ {GPIOC_BASE, 13, ADC_INVALID, TIMER_INVALID}, // D21/PC13
+ {GPIOC_BASE, 14, ADC_INVALID, TIMER_INVALID}, // D22/PC14
+ {GPIOC_BASE, 15, ADC_INVALID, TIMER_INVALID}, // D23/PC15
+ {GPIOB_BASE, 9, ADC_INVALID, TIMER4_CH4_CCR}, // D24/PB9
+ {GPIOD_BASE, 2, ADC_INVALID, TIMER_INVALID}, // D25/PD2
+ {GPIOC_BASE, 10, ADC_INVALID, TIMER_INVALID}, // D26/PC10
+ {GPIOB_BASE, 0, ADC8, TIMER3_CH3_CCR}, // D27/PB0
+ {GPIOB_BASE, 1, ADC9, TIMER3_CH4_CCR}, // D28/PB1
+ {GPIOB_BASE, 10, ADC_INVALID, TIMER_INVALID}, // D29/PB10
+ {GPIOB_BASE, 11, ADC_INVALID, TIMER_INVALID}, // D30/PB11
+ {GPIOB_BASE, 12, ADC_INVALID, TIMER_INVALID}, // D31/PB12
+ {GPIOB_BASE, 13, ADC_INVALID, TIMER_INVALID}, // D32/PB13
+ {GPIOB_BASE, 14, ADC_INVALID, TIMER_INVALID}, // D33/PB14
+ {GPIOB_BASE, 15, ADC_INVALID, TIMER_INVALID}, // D34/PB15
+ {GPIOC_BASE, 6, ADC_INVALID, TIMER_INVALID}, // D35/PC6
+ {GPIOC_BASE, 7, ADC_INVALID, TIMER_INVALID}, // D36/PC7
+ {GPIOC_BASE, 8, ADC_INVALID, TIMER_INVALID}, // D37/PC8
+ {GPIOC_BASE, 9, ADC_INVALID, TIMER_INVALID} // D38/PC9
};
void pinMode(uint8_t pin, WiringPinMode mode) {
@@ -120,7 +121,7 @@ void pinMode(uint8_t pin, WiringPinMode mode) {
return;
}
- gpio_set_mode(PIN_TO_GPIO[pin].port, PIN_TO_GPIO[pin].pin, outputMode);
+ gpio_set_mode(PIN_MAP[pin].port, PIN_MAP[pin].pin, outputMode);
}
@@ -128,12 +129,12 @@ uint32_t digitalRead(uint8_t pin) {
if (pin >= NR_MAPLE_PINS)
return 0;
- return (PIN_TO_GPIO[pin].port->IDR & BIT(PIN_TO_GPIO[pin].pin)) ? 1 : 0;
+ return (PIN_MAP[pin].port->IDR & BIT(PIN_MAP[pin].pin)) ? 1 : 0;
}
void digitalWrite(uint8_t pin, uint8_t val) {
if (pin >= NR_MAPLE_PINS)
return;
- gpio_write_bit(PIN_TO_GPIO[pin].port, PIN_TO_GPIO[pin].pin, val);
+ gpio_write_bit(PIN_MAP[pin].port, PIN_MAP[pin].pin, val);
}