From 943006daefeca420749768a4d4e9810a258f2b92 Mon Sep 17 00:00:00 2001 From: Perry Hung Date: Tue, 21 Sep 2010 20:22:17 -0400 Subject: Add PWM_OPEN_DRAIN option to pinMode() Allow for alternate function open drain output mode to be accessed through the wirish interface. This allows for open drain to be used on PWM pins. --- wirish/io.h | 3 ++- wirish/wirish_digital.c | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) (limited to 'wirish') diff --git a/wirish/io.h b/wirish/io.h index e779604..4aa1eef 100644 --- a/wirish/io.h +++ b/wirish/io.h @@ -47,7 +47,8 @@ typedef enum WiringPinMode { INPUT_PULLUP, INPUT_PULLDOWN, INPUT_FLOATING, - PWM + PWM, + PWM_OPEN_DRAIN, } WiringPinMode; diff --git a/wirish/wirish_digital.c b/wirish/wirish_digital.c index c93c786..9dd46ed 100644 --- a/wirish/wirish_digital.c +++ b/wirish/wirish_digital.c @@ -58,6 +58,9 @@ void pinMode(uint8 pin, WiringPinMode mode) { case PWM: outputMode = GPIO_MODE_AF_OUTPUT_PP; break; + case PWM_OPEN_DRAIN: + outputMode = GPIO_MODE_AF_OUTPUT_OD; + break; default: ASSERT(0); return; -- cgit v1.2.3 From c13e850abe053edaa1aad6ef7b928c6bf9288cb3 Mon Sep 17 00:00:00 2001 From: Perry Hung Date: Wed, 22 Sep 2010 03:17:45 -0400 Subject: Enable external interrupts on all Maple GPIOs. Extend the wirish attachInterrupt() and detachInterrupt() interface to work with all GPIOs. Note: The STM32 external interrupt lines are multiplexed between GPIO ports. While any GPIO can be used as an external interrupt, not all of them can be used at the same time. Each EXTI[n] line selects between PA[n], PB[n], PC[n], etc. For example, line EXTI5 can be used with STM32 pins PA5, PB5, or PC5, but not all at the same time. See table: EXTI Line Maple Pin STM32 Pin 0 D2 PA0 0 D27 PB0 0 D15 PC0 1 D3 PA1 1 D28 PB1 1 D16 PC1 2 D1 PA2 2 D17 PC2 2 D25 PD2 3 D0 PA3 3 D18 PC3 4 D10 PA4 4 D19 PC4 5 D13 PA5 5 D4 PB5 5 D20 PC5 6 D12 PA6 6 D5 PB6 6 D35 PC6 7 D11 PA7 7 D9 PB7 7 D36 PC7 8 D6 PA8 8 D14 PB8 8 D37 PC8 9 D7 PA9 9 D24 PB9 9 D38 PC9 (BUT) 10 D8 PA10 10 D29 PB10 10 D26 PC10 11 D30 PB11 12 D31 PB12 13 D32 PB13 13 D21 PC13 14 D33 PB14 14 D22 PC14 15 D34 PB15 15 D23 PC15 --- libmaple/exti.c | 234 ++++++++++++++++------------------------------ libmaple/exti.h | 13 ++- libmaple/libmaple_types.h | 4 + libmaple/nvic.h | 37 +++++--- wirish/boards.h | 102 ++++++++------------ wirish/ext_interrupts.c | 36 +++---- wirish/ext_interrupts.h | 11 +-- 7 files changed, 173 insertions(+), 264 deletions(-) (limited to 'wirish') diff --git a/libmaple/exti.c b/libmaple/exti.c index 5575906..8a54457 100644 --- a/libmaple/exti.c +++ b/libmaple/exti.c @@ -33,10 +33,32 @@ #include "exti.h" #include "nvic.h" -volatile static voidFuncPtr exti_handlers[NR_EXTI_CHANNELS]; +typedef struct ExtIChannel { + void (*handler)(void); + uint32 irq_line; +} ExtIChannel; + +static ExtIChannel exti_channels[] = { + { .handler = NULL, .irq_line = NVIC_EXTI0 }, // EXTI0 + { .handler = NULL, .irq_line = NVIC_EXTI1 }, // EXTI1 + { .handler = NULL, .irq_line = NVIC_EXTI2 }, // EXTI2 + { .handler = NULL, .irq_line = NVIC_EXTI3 }, // EXTI3 + { .handler = NULL, .irq_line = NVIC_EXTI4 }, // EXTI4 + { .handler = NULL, .irq_line = NVIC_EXTI9_5 }, // EXTI5 + { .handler = NULL, .irq_line = NVIC_EXTI9_5 }, // EXTI6 + { .handler = NULL, .irq_line = NVIC_EXTI9_5 }, // EXTI7 + { .handler = NULL, .irq_line = NVIC_EXTI9_5 }, // EXTI8 + { .handler = NULL, .irq_line = NVIC_EXTI9_5 }, // EXTI9 + { .handler = NULL, .irq_line = NVIC_EXTI15_10 }, // EXTI10 + { .handler = NULL, .irq_line = NVIC_EXTI15_10 }, // EXTI11 + { .handler = NULL, .irq_line = NVIC_EXTI15_10 }, // EXTI12 + { .handler = NULL, .irq_line = NVIC_EXTI15_10 }, // EXTI13 + { .handler = NULL, .irq_line = NVIC_EXTI15_10 }, // EXTI14 + { .handler = NULL, .irq_line = NVIC_EXTI15_10 }, // EXTI15 +}; static inline void clear_pending(int bit) { - REG_SET(EXTI_PR, BIT(bit)); + __set_bits(EXTI_PR, BIT(bit)); /* If the pending bit is cleared as the last instruction in an ISR, * it won't actually be cleared in time and the ISR will fire again. * Insert a 2-cycle buffer to allow it to take effect. */ @@ -44,61 +66,39 @@ static inline void clear_pending(int bit) { asm volatile("nop"); } +static inline void dispatch_handler(uint32 channel) { + ASSERT(exti_channels[channel].handler); + if (exti_channels[channel].handler) { + (exti_channels[channel].handler)(); + } +} + /* For EXTI0 through EXTI4, only one handler * is associated with each channel, so we * don't have to keep track of which channel * we came from */ void EXTI0_IRQHandler(void) { - ASSERT(exti_handlers[EXTI0]); - if (exti_handlers[EXTI0]) { - exti_handlers[EXTI0](); - } - - /* Clear pending bit*/ + dispatch_handler(EXTI0); clear_pending(EXTI0); } void EXTI1_IRQHandler(void) { - ASSERT(exti_handlers[EXTI1]); - /* Call registered handler */ - if (exti_handlers[EXTI1]) { - exti_handlers[EXTI1](); - } - - /* Clear pending bit*/ + dispatch_handler(EXTI1); clear_pending(EXTI1); } void EXTI2_IRQHandler(void) { - ASSERT(exti_handlers[EXTI2]); - /* Call registered handler */ - if (exti_handlers[EXTI2]) { - exti_handlers[EXTI2](); - } - - /* Clear pending bit*/ + dispatch_handler(EXTI2); clear_pending(EXTI2); } void EXTI3_IRQHandler(void) { - ASSERT(exti_handlers[EXTI3]); - /* Call registered handler */ - if (exti_handlers[EXTI3]) { - exti_handlers[EXTI3](); - } - - /* Clear pending bit*/ + dispatch_handler(EXTI3); clear_pending(EXTI3); } void EXTI4_IRQHandler(void) { - ASSERT(exti_handlers[EXTI4]); - /* Call registered handler */ - if (exti_handlers[EXTI4]) { - exti_handlers[EXTI4](); - } - - /* Clear pending bit*/ + dispatch_handler(EXTI4); clear_pending(EXTI4); } @@ -112,8 +112,7 @@ void EXTI9_5_IRQHandler(void) { /* Dispatch every handler if the pending bit is set */ for (i = 0; i < 5; i++) { if (pending & 0x1) { - ASSERT(exti_handlers[EXTI5 + i]); - exti_handlers[EXTI5 + i](); + dispatch_handler(EXTI5 + i); clear_pending(EXTI5 + i); } pending >>= 1; @@ -130,8 +129,7 @@ void EXTI15_10_IRQHandler(void) { /* Dispatch every handler if the pending bit is set */ for (i = 0; i < 6; i++) { if (pending & 0x1) { - ASSERT(exti_handlers[EXTI10 + i]); - exti_handlers[EXTI10 + i](); + dispatch_handler(EXTI10 + i); clear_pending(EXTI10 + i); } pending >>= 1; @@ -139,147 +137,73 @@ void EXTI15_10_IRQHandler(void) { } -void exti_attach_interrupt(uint8 channel, uint8 port, voidFuncPtr handler, uint8 mode) { - ASSERT(channel < NR_EXTI_CHANNELS); - ASSERT(port < NR_EXTI_PORTS); - ASSERT(mode < NR_EXTI_MODES); - ASSERT(EXTI0 == 0); - ASSERT(handler); +/** + * @brief Register a handler to run upon external interrupt + * @param port source port of pin (eg EXTI_CONFIG_PORTA) + * @param pin pin number on the source port + * @param handler function handler to execute + * @param mode type of transition to trigger on + */ +void exti_attach_interrupt(uint32 port, + uint32 pin, + voidFuncPtr handler, + uint32 mode) { + static uint32 afio_regs[] = { + AFIO_EXTICR1, // EXT0-3 + AFIO_EXTICR2, // EXT4-7 + AFIO_EXTICR3, // EXT8-11 + AFIO_EXTICR4, // EXT12-15 + }; /* Note: All of the following code assumes that EXTI0 = 0 */ + ASSERT(EXTI0 == 0); + ASSERT(handler); - /* Map port to the correct EXTI channel */ - switch (channel) { - case EXTI0: - case EXTI1: - case EXTI2: - case EXTI3: - REG_SET_MASK(AFIO_EXTICR1, BIT_MASK_SHIFT(port, channel*4)); - break; - - case EXTI4: - case EXTI5: - case EXTI6: - case EXTI7: - REG_SET_MASK(AFIO_EXTICR2, BIT_MASK_SHIFT(port, (channel-4)*4)); - break; - - case EXTI8: - case EXTI9: - case EXTI10: - case EXTI11: - REG_SET_MASK(AFIO_EXTICR3, BIT_MASK_SHIFT(port, (channel-8)*4)); - break; + uint32 channel = pin; - case EXTI12: - case EXTI13: - case EXTI14: - case EXTI15: - REG_SET_MASK(AFIO_EXTICR4, BIT_MASK_SHIFT(port, (channel-12)*4)); - break; - } + /* map port to channel */ + __write(afio_regs[pin/4], (port << ((pin % 4) * 4))); /* Unmask appropriate interrupt line */ - REG_SET_BIT(EXTI_IMR, channel); + __set_bits(EXTI_IMR, BIT(channel)); /* Set trigger mode */ switch (mode) { case EXTI_RISING: - REG_SET_BIT(EXTI_RTSR, channel); + __set_bits(EXTI_RTSR, BIT(channel)); break; case EXTI_FALLING: - REG_SET_BIT(EXTI_FTSR, channel); + __set_bits(EXTI_FTSR, BIT(channel)); break; case EXTI_RISING_FALLING: - REG_SET_BIT(EXTI_RTSR, channel); - REG_SET_BIT(EXTI_FTSR, channel); + __set_bits(EXTI_RTSR, BIT(channel)); + __set_bits(EXTI_FTSR, BIT(channel)); break; } /* Configure the enable interrupt bits for the NVIC */ - switch (channel) { - case EXTI0: - case EXTI1: - case EXTI2: - case EXTI3: - case EXTI4: - REG_SET(NVIC_ISER0, BIT(channel + 6)); - break; - - /* EXTI5-9 map to the same isr */ - case EXTI5: - case EXTI6: - case EXTI7: - case EXTI8: - case EXTI9: - REG_SET(NVIC_ISER0, BIT(23)); - break; - - /* EXTI10-15 map to the same isr */ - case EXTI10: - case EXTI11: - case EXTI12: - case EXTI13: - case EXTI14: - case EXTI15: - REG_SET(NVIC_ISER1, BIT(8)); - break; - } + nvic_irq_enable(exti_channels[channel].irq_line); /* Register the handler */ - exti_handlers[channel] = handler; + exti_channels[channel].handler = handler; } -void exti_detach_interrupt(uint8 channel) { +/** + * @brief Unregister an external interrupt handler + * @param channel channel to disable (eg EXTI0) + */ +void exti_detach_interrupt(uint32 channel) { ASSERT(channel < NR_EXTI_CHANNELS); ASSERT(EXTI0 == 0); - /* Is this interrupt actually on? */ - ASSERT((REG_GET(EXTI_IMR) >> channel) & 0x01); - - /* Clear EXTI_IMR line */ - REG_CLEAR_BIT(EXTI_IMR, channel); - - /* Clear triggers */ - REG_CLEAR_BIT(EXTI_FTSR, channel); - REG_CLEAR_BIT(EXTI_RTSR, channel); - - /* Turn off the associated interrupt */ - switch (channel) { - case EXTI0: - case EXTI1: - case EXTI2: - case EXTI3: - case EXTI4: - REG_SET(NVIC_ICER0, BIT(channel + 6)); - break; - case EXTI5: - case EXTI6: - case EXTI7: - case EXTI8: - case EXTI9: - /* Are there any other channels enabled? - * If so, don't disable the interrupt handler */ - if (GET_BITS(REG_GET(EXTI_IMR), 5, 9) == 0) { - REG_SET(NVIC_ICER0, BIT(23)); - } - break; - case EXTI10: - case EXTI11: - case EXTI12: - case EXTI13: - case EXTI14: - case EXTI15: - /* Are there any other channels enabled? - * If so, don't disable the interrupt handler */ - if (GET_BITS(REG_GET(EXTI_IMR), 10, 15) == 0) { - REG_SET(NVIC_ICER1, BIT(8)); - } - break; - } - /* Clear handler function pointer */ - exti_handlers[channel] = 0; + __clear_bits(EXTI_IMR, BIT(channel)); + __clear_bits(EXTI_FTSR, BIT(channel)); + __clear_bits(EXTI_RTSR, BIT(channel)); + + nvic_irq_disable(exti_channels[channel].irq_line); + + exti_channels[channel].handler = NULL; } diff --git a/libmaple/exti.h b/libmaple/exti.h index 2832e24..97cb4aa 100644 --- a/libmaple/exti.h +++ b/libmaple/exti.h @@ -100,6 +100,10 @@ #define NR_EXTI_CHANNELS 16 #define NR_EXTI_PORTS NR_GPIO_PORTS // board specific +#define EXTI_RISING 0 +#define EXTI_FALLING 1 +#define EXTI_RISING_FALLING 2 + #define EXTI_IMR 0x40010400 // Interrupt mask register #define EXTI_EMR (EXTI_IMR + 0x04) // Event mask register #define EXTI_RTSR (EXTI_IMR + 0x08) // Rising trigger selection register @@ -113,10 +117,6 @@ #define AFIO_EXTICR3 (AFIO_EVCR + 0x10) #define AFIO_EXTICR4 (AFIO_EVCR + 0x14) -#define EXTI_RISING 0 -#define EXTI_FALLING 1 -#define EXTI_RISING_FALLING 2 - #define EXTI0 0 #define EXTI1 1 #define EXTI2 2 @@ -142,13 +142,12 @@ #define EXTI_CONFIG_PORTF 5 // Native only #define EXTI_CONFIG_PORTG 6 // Native only - #ifdef __cplusplus extern "C"{ #endif -void exti_attach_interrupt(uint8, uint8, voidFuncPtr, uint8); -void exti_detach_interrupt(uint8); +void exti_attach_interrupt(uint32, uint32, voidFuncPtr, uint32); +void exti_detach_interrupt(uint32); #ifdef __cplusplus } // extern "C" diff --git a/libmaple/libmaple_types.h b/libmaple/libmaple_types.h index d49f95a..da3c241 100644 --- a/libmaple/libmaple_types.h +++ b/libmaple/libmaple_types.h @@ -45,5 +45,9 @@ typedef void (*voidFuncPtr)(void); #define __io volatile +#ifndef NULL +#define NULL 0 +#endif + #endif diff --git a/libmaple/nvic.h b/libmaple/nvic.h index 3286357..ab56f0e 100644 --- a/libmaple/nvic.h +++ b/libmaple/nvic.h @@ -31,8 +31,6 @@ #define NVIC_INT_USBHP 19 #define NVIC_INT_USBLP 20 -#define NVIC_EXTI1_OFFSET (NVIC_ISER0 + 0x07) -#define NVIC_EXTI9_5_OFFSET (NVIC_ISER0 + 0x17) /* NVIC Interrupt Enable registers */ #define NVIC_ISER0 0xE000E100 @@ -57,19 +55,28 @@ extern "C"{ #endif enum { - NVIC_TIMER1 = 27, - NVIC_TIMER2 = 28, - NVIC_TIMER3 = 29, - NVIC_TIMER4 = 30, - NVIC_TIMER5 = 50, // high density only (Maple Native) - NVIC_TIMER6 = 54, // high density only (Maple Native) - NVIC_TIMER7 = 55, // high density only (Maple Native) - NVIC_TIMER8 = 46, // high density only (Maple Native) - NVIC_USART1 = 37, - NVIC_USART2 = 38, - NVIC_USART3 = 39, - NVIC_USART4 = 52, // high density only (Maple Native) - NVIC_USART5 = 53, // high density only (Maple Native) + NVIC_TIMER1 = 27, + NVIC_TIMER2 = 28, + NVIC_TIMER3 = 29, + NVIC_TIMER4 = 30, + NVIC_TIMER5 = 50, // high density only (Maple Native) + NVIC_TIMER6 = 54, // high density only (Maple Native) + NVIC_TIMER7 = 55, // high density only (Maple Native) + NVIC_TIMER8 = 46, // high density only (Maple Native) + + NVIC_USART1 = 37, + NVIC_USART2 = 38, + NVIC_USART3 = 39, + NVIC_USART4 = 52, // high density only (Maple Native) + NVIC_USART5 = 53, // high density only (Maple Native) + + NVIC_EXTI0 = 6, + NVIC_EXTI1 = 7, + NVIC_EXTI2 = 8, + NVIC_EXTI3 = 9, + NVIC_EXTI4 = 10, + NVIC_EXTI9_5 = 23, + NVIC_EXTI15_10 = 40, }; diff --git a/wirish/boards.h b/wirish/boards.h index 03d0b0e..0e0d159 100644 --- a/wirish/boards.h +++ b/wirish/boards.h @@ -64,13 +64,9 @@ typedef struct PinMapping { uint32 pin; uint32 adc; TimerCCR timer_channel; + uint32 exti_port; } PinMapping; -typedef struct ExtiInfo { - uint8 channel; - uint8 port; -} ExtiInfo; - // LeafLabs Maple rev3, rev4 #ifdef BOARD_maple @@ -78,65 +74,47 @@ typedef struct ExtiInfo { #define MAPLE_RELOAD_VAL 71999 /* takes a cycle to reload */ static __attribute__ ((unused)) PinMapping PIN_MAP[NR_GPIO_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 + {GPIOA_BASE, 3, ADC3, TIMER2_CH4_CCR, EXTI_CONFIG_PORTA}, // D0/PA3 + {GPIOA_BASE, 2, ADC2, TIMER2_CH3_CCR, EXTI_CONFIG_PORTA}, // D1/PA2 + {GPIOA_BASE, 0, ADC0, TIMER2_CH1_CCR, EXTI_CONFIG_PORTA}, // D2/PA0 + {GPIOA_BASE, 1, ADC1, TIMER2_CH2_CCR, EXTI_CONFIG_PORTA}, // D3/PA1 + {GPIOB_BASE, 5, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTB}, // D4/PB5 + {GPIOB_BASE, 6, ADC_INVALID, TIMER4_CH1_CCR, EXTI_CONFIG_PORTB}, // D5/PB6 + {GPIOA_BASE, 8, ADC_INVALID, TIMER1_CH1_CCR, EXTI_CONFIG_PORTA}, // D6/PA8 + {GPIOA_BASE, 9, ADC_INVALID, TIMER1_CH2_CCR, EXTI_CONFIG_PORTA}, // D7/PA9 + {GPIOA_BASE, 10, ADC_INVALID, TIMER1_CH3_CCR, EXTI_CONFIG_PORTA}, // D8/PA10 + {GPIOB_BASE, 7, ADC_INVALID, TIMER4_CH2_CCR, EXTI_CONFIG_PORTB}, // D9/PB7 + {GPIOA_BASE, 4, ADC4, TIMER_INVALID, EXTI_CONFIG_PORTA}, // D10/PA4 + {GPIOA_BASE, 7, ADC7, TIMER3_CH2_CCR, EXTI_CONFIG_PORTA}, // D11/PA7 + {GPIOA_BASE, 6, ADC6, TIMER3_CH1_CCR, EXTI_CONFIG_PORTA}, // D12/PA6 + {GPIOA_BASE, 5, ADC5, TIMER_INVALID, EXTI_CONFIG_PORTA}, // D13/PA5 + {GPIOB_BASE, 8, ADC_INVALID, TIMER4_CH3_CCR, EXTI_CONFIG_PORTB}, // D14/PB8 /* Little header */ - {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 + {GPIOC_BASE, 0, ADC10, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D15/PC0 + {GPIOC_BASE, 1, ADC11, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D16/PC1 + {GPIOC_BASE, 2, ADC12, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D17/PC2 + {GPIOC_BASE, 3, ADC13, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D18/PC3 + {GPIOC_BASE, 4, ADC14, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D19/PC4 + {GPIOC_BASE, 5, ADC15, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D20/PC5 /* External header */ - {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 (BUT) - }; - - static __attribute__ ((unused)) ExtiInfo PIN_TO_EXTI_CHANNEL[NR_GPIO_PINS] = - { - {EXTI3, EXTI_CONFIG_PORTA}, // D0/PA3 - {EXTI2, EXTI_CONFIG_PORTA}, // D1/PA2 - {EXTI0, EXTI_CONFIG_PORTA}, // D2/PA0 - {EXTI1, EXTI_CONFIG_PORTA}, // D3/PA1 - {EXTI5, EXTI_CONFIG_PORTB}, // D4/PB5 - {EXTI6, EXTI_CONFIG_PORTB}, // D5/PB6 - {EXTI8, EXTI_CONFIG_PORTA}, // D6/PA8 - {EXTI9, EXTI_CONFIG_PORTA}, // D7/PA9 - {EXTI10, EXTI_CONFIG_PORTA}, // D8/PA10 - {EXTI7, EXTI_CONFIG_PORTB}, // D9/PB7 - {EXTI4, EXTI_CONFIG_PORTA}, // D10/PA4 - {EXTI7, EXTI_CONFIG_PORTA}, // D11/PA7 - {EXTI6, EXTI_CONFIG_PORTA}, // D12/PA6 - {EXTI5, EXTI_CONFIG_PORTA}, // D13/PA5 + {GPIOC_BASE, 13, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D21/PC13 + {GPIOC_BASE, 14, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D22/PC14 + {GPIOC_BASE, 15, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D23/PC15 + {GPIOB_BASE, 9, ADC_INVALID, TIMER4_CH4_CCR, EXTI_CONFIG_PORTB}, // D24/PB9 + {GPIOD_BASE, 2, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTD}, // D25/PD2 + {GPIOC_BASE, 10, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D26/PC10 + {GPIOB_BASE, 0, ADC8, TIMER3_CH3_CCR, EXTI_CONFIG_PORTB}, // D27/PB0 + {GPIOB_BASE, 1, ADC9, TIMER3_CH4_CCR, EXTI_CONFIG_PORTB}, // D28/PB1 + {GPIOB_BASE, 10, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTB}, // D29/PB10 + {GPIOB_BASE, 11, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTB}, // D30/PB11 + {GPIOB_BASE, 12, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTB}, // D31/PB12 + {GPIOB_BASE, 13, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTB}, // D32/PB13 + {GPIOB_BASE, 14, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTB}, // D33/PB14 + {GPIOB_BASE, 15, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTB}, // D34/PB15 + {GPIOC_BASE, 6, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D35/PC6 + {GPIOC_BASE, 7, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D36/PC7 + {GPIOC_BASE, 8, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D37/PC8 + {GPIOC_BASE, 9, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTC} // D38/PC9 (BUT) }; #endif diff --git a/wirish/ext_interrupts.c b/wirish/ext_interrupts.c index 6ba1d05..5009d75 100644 --- a/wirish/ext_interrupts.c +++ b/wirish/ext_interrupts.c @@ -36,22 +36,21 @@ * @brief Attach an interrupt handler to be triggered on a given * transition on the pin. Runs in interrupt context * - * @param[in] pin Maple pin number - * @param[in] handler Function to run upon external interrupt trigger. - * @param[in] mode Type of transition to trigger on, eg falling, rising, etc. + * @param pin Maple pin number + * @param handler Function to run upon external interrupt trigger. + * @param mode Type of transition to trigger on, eg falling, rising, etc. * * @sideeffect Registers a handler */ -int attachInterrupt(uint8 pin, voidFuncPtr handler, uint32 mode) { +void attachInterrupt(uint8 pin, voidFuncPtr handler, uint32 mode) { uint8 outMode; /* Parameter checking */ if (pin >= NR_GPIO_PINS) { - return EXT_INTERRUPT_INVALID_PIN; + return; } if (!handler) { - ASSERT(0); - return EXT_INTERRUPT_INVALID_FUNCTION; + return; } switch (mode) { @@ -65,22 +64,27 @@ int attachInterrupt(uint8 pin, voidFuncPtr handler, uint32 mode) { outMode = EXTI_RISING_FALLING; break; default: - ASSERT(0); - return EXT_INTERRUPT_INVALID_MODE;; + return; } - exti_attach_interrupt(PIN_TO_EXTI_CHANNEL[pin].channel, - PIN_TO_EXTI_CHANNEL[pin].port, - handler, mode); + exti_attach_interrupt(PIN_MAP[pin].exti_port, + PIN_MAP[pin].pin, + handler, + mode); - return 0; + return; } -int detachInterrupt(uint8 pin) { +/** + * @brief Disable an external interrupt + * @param pin maple pin number + * @sideeffect unregisters external interrupt handler + */ +void detachInterrupt(uint8 pin) { if (!(pin < NR_GPIO_PINS)) { - return EXT_INTERRUPT_INVALID_PIN; + return; } - exti_detach_interrupt(PIN_TO_EXTI_CHANNEL[pin].channel); + exti_detach_interrupt(PIN_MAP[pin].pin); } diff --git a/wirish/ext_interrupts.h b/wirish/ext_interrupts.h index 7449685..62f31bb 100644 --- a/wirish/ext_interrupts.h +++ b/wirish/ext_interrupts.h @@ -37,19 +37,12 @@ enum { CHANGE }; - -enum { - EXT_INTERRUPT_INVALID_PIN = (-1), - EXT_INTERRUPT_INVALID_FUNCTION = (-2), - EXT_INTERRUPT_INVALID_MODE = (-3), -}; - #ifdef __cplusplus extern "C"{ #endif -int attachInterrupt(uint8 pin, voidFuncPtr, uint32 mode); -int detachInterrupt(uint8 pin); +void attachInterrupt(uint8 pin, voidFuncPtr, uint32 mode); +void detachInterrupt(uint8 pin); #ifdef __cplusplus } -- cgit v1.2.3 From 849bc0f8f6abf42567a152cf6e01bf7349902aac Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Sun, 26 Sep 2010 21:47:55 -0400 Subject: wirish reformatted and code-styled --- wirish/HardwareTimer.cpp | 56 ++++++--- wirish/HardwareTimer.h | 62 +++++----- wirish/Print.cpp | 307 +++++++++++++++++++++------------------------ wirish/Print.h | 39 +++--- wirish/WProgram.h | 4 +- wirish/bits.h | 38 +++--- wirish/boards.h | 315 +++++++++++++++++++++++++++++++---------------- wirish/ext_interrupts.c | 13 +- wirish/ext_interrupts.h | 4 +- wirish/io.h | 9 +- wirish/pwm.c | 9 +- wirish/pwm.h | 6 +- wirish/time.c | 20 +-- wirish/time.h | 32 ++--- wirish/usb_serial.cpp | 41 +++--- wirish/usb_serial.h | 36 +++--- wirish/wirish.c | 69 +++++------ wirish/wirish.h | 22 ++-- wirish/wirish_analog.c | 12 +- wirish/wirish_digital.c | 16 ++- wirish/wirish_math.cpp | 72 +++++------ wirish/wirish_math.h | 16 +-- wirish/wirish_shift.c | 54 ++++---- 23 files changed, 685 insertions(+), 567 deletions(-) (limited to 'wirish') diff --git a/wirish/HardwareTimer.cpp b/wirish/HardwareTimer.cpp index 99863c1..6fbad8b 100644 --- a/wirish/HardwareTimer.cpp +++ b/wirish/HardwareTimer.cpp @@ -1,4 +1,4 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 Bryan Newbold. @@ -20,7 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - * ****************************************************************************/ + *****************************************************************************/ /** * @brief wirish timer class to manage the four 16-bit timer peripherals @@ -43,99 +43,127 @@ HardwareTimer::HardwareTimer(uint8 timerNum) { void HardwareTimer::resume(void) { timer_resume(this->timerNum); } + void HardwareTimer::pause(void) { timer_pause(this->timerNum); } + void HardwareTimer::setPrescaleFactor(uint16 factor) { // The prescaler register is zero-indexed timer_set_prescaler(this->timerNum, factor-1); } + void HardwareTimer::setOverflow(uint16 val) { this->overflow = val; timer_set_reload(this->timerNum, val); } -void HardwareTimer::setCount(uint16 val) { - if(val > this->overflow) + +void HardwareTimer::setCount(uint16 val) { + if(val > this->overflow) { val = this->overflow; + } timer_set_count(this->timerNum, val); } + uint16 HardwareTimer::getCount(void) { return timer_get_count(this->timerNum); } -// This function will set the prescaler and overflow to get -// a period of the given length with the most resolution; -// the return value is the overflow value and thus the largest -// value that can be set as a compare. +/* This function will set the prescaler and overflow to get a period + * of the given length with the most resolution; the return value is + * the overflow value and thus the largest value that can be set as a + * compare. */ uint16 HardwareTimer::setPeriod(uint32 microseconds) { // XXX: 72MHz shouldn't be hard coded in here... global define? - + // Not the best way to handle this edge case? if(!microseconds) { setPrescaleFactor(1); setOverflow(1); return this->overflow; } + // With a prescale factor of 1, there are 72counts/ms uint16 ps = ((microseconds*72)/65536) + 1; setPrescaleFactor(ps); + // Finally, this overflow will always be less than 65536 setOverflow(((microseconds*72)/ps) - 1); - return this->overflow; + return this->overflow; } + void HardwareTimer::setChannel1Mode(uint8 mode) { timer_set_mode(this->timerNum,1,mode); } + void HardwareTimer::setChannel2Mode(uint8 mode) { timer_set_mode(this->timerNum,2,mode); } + void HardwareTimer::setChannel3Mode(uint8 mode) { timer_set_mode(this->timerNum,3,mode); } + void HardwareTimer::setChannel4Mode(uint8 mode) { timer_set_mode(this->timerNum,4,mode); } + void HardwareTimer::setCompare1(uint16 val) { - if(val > this->overflow) + if(val > this->overflow) { val = this->overflow; + } timer_set_compare_value(this->timerNum,1,val); } + void HardwareTimer::setCompare2(uint16 val) { - if(val > this->overflow) + if(val > this->overflow) { val = this->overflow; + } timer_set_compare_value(this->timerNum,2,val); } + void HardwareTimer::setCompare3(uint16 val) { - if(val > this->overflow) + if(val > this->overflow) { val = this->overflow; + } timer_set_compare_value(this->timerNum,3,val); } + void HardwareTimer::setCompare4(uint16 val) { - if(val > this->overflow) + if(val > this->overflow) { val = this->overflow; + } timer_set_compare_value(this->timerNum,4,val); } + void HardwareTimer::attachCompare1Interrupt(voidFuncPtr handler) { timer_attach_interrupt(this->timerNum,1,handler); } + void HardwareTimer::attachCompare2Interrupt(voidFuncPtr handler) { timer_attach_interrupt(this->timerNum,2,handler); } + void HardwareTimer::attachCompare3Interrupt(voidFuncPtr handler) { timer_attach_interrupt(this->timerNum,3,handler); } + void HardwareTimer::attachCompare4Interrupt(voidFuncPtr handler) { timer_attach_interrupt(this->timerNum,4,handler); } + void HardwareTimer::detachCompare1Interrupt(void) { timer_detach_interrupt(this->timerNum,1); } + void HardwareTimer::detachCompare2Interrupt(void) { timer_detach_interrupt(this->timerNum,2); } + void HardwareTimer::detachCompare3Interrupt(void) { timer_detach_interrupt(this->timerNum,3); } + void HardwareTimer::detachCompare4Interrupt(void) { timer_detach_interrupt(this->timerNum,4); } diff --git a/wirish/HardwareTimer.h b/wirish/HardwareTimer.h index aa48718..c6e11c8 100644 --- a/wirish/HardwareTimer.h +++ b/wirish/HardwareTimer.h @@ -1,4 +1,4 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 Bryan Newbold. @@ -20,7 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - * ****************************************************************************/ + *****************************************************************************/ /** * @brief wirish timer class to manage the four 16-bit timer peripherals @@ -30,38 +30,38 @@ #define _TIMER_H_ class HardwareTimer { - private: - uint16 overflow; - uint8 timerNum; + private: + uint16 overflow; + uint8 timerNum; - public: - HardwareTimer(uint8 timer_num); + public: + HardwareTimer(uint8 timer_num); - void pause(void); - void resume(void); - void setPrescaleFactor(uint16 factor); - void setOverflow(uint16 val); // truncates to overflow - void setCount(uint16 val); // truncates to overflow - uint16 getCount(void); + void pause(void); + void resume(void); + void setPrescaleFactor(uint16 factor); + void setOverflow(uint16 val); // truncates to overflow + void setCount(uint16 val); // truncates to overflow + uint16 getCount(void); - // tries to set prescaler and overflow wisely; returns overflow - uint16 setPeriod(uint32 microseconds); - void setChannel1Mode(uint8 mode); - void setChannel2Mode(uint8 mode); - void setChannel3Mode(uint8 mode); - void setChannel4Mode(uint8 mode); - void setCompare1(uint16 val); // truncates to overflow - void setCompare2(uint16 val); // truncates to overflow - void setCompare3(uint16 val); // truncates to overflow - void setCompare4(uint16 val); // truncates to overflow - void attachCompare1Interrupt(voidFuncPtr handler); - void attachCompare2Interrupt(voidFuncPtr handler); - void attachCompare3Interrupt(voidFuncPtr handler); - void attachCompare4Interrupt(voidFuncPtr handler); - void detachCompare1Interrupt(void); - void detachCompare2Interrupt(void); - void detachCompare3Interrupt(void); - void detachCompare4Interrupt(void); + // tries to set prescaler and overflow wisely; returns overflow + uint16 setPeriod(uint32 microseconds); + void setChannel1Mode(uint8 mode); + void setChannel2Mode(uint8 mode); + void setChannel3Mode(uint8 mode); + void setChannel4Mode(uint8 mode); + void setCompare1(uint16 val); // truncates to overflow + void setCompare2(uint16 val); // truncates to overflow + void setCompare3(uint16 val); // truncates to overflow + void setCompare4(uint16 val); // truncates to overflow + void attachCompare1Interrupt(voidFuncPtr handler); + void attachCompare2Interrupt(voidFuncPtr handler); + void attachCompare3Interrupt(voidFuncPtr handler); + void attachCompare4Interrupt(voidFuncPtr handler); + void detachCompare1Interrupt(void); + void detachCompare2Interrupt(void); + void detachCompare3Interrupt(void); + void detachCompare4Interrupt(void); }; extern HardwareTimer Timer1; diff --git a/wirish/Print.cpp b/wirish/Print.cpp index 9baa757..c66ca61 100644 --- a/wirish/Print.cpp +++ b/wirish/Print.cpp @@ -1,213 +1,190 @@ /* - Print.cpp - Base class that provides print() and println() - Copyright (c) 2008 David A. Mellis. 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 - - Modified 23 November 2006 by David A. Mellis + * Print.cpp - Base class that provides print() and println() + * Copyright (c) 2008 David A. Mellis. 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 + * + * Modified 23 November 2006 by David A. Mellis */ #include "wirish.h" #include "Print.h" -// Public Methods ////////////////////////////////////////////////////////////// - -/* default implementation: may be overridden */ -void Print::write(const char *str) -{ - while (*str) - write(*str++); -} - -/* default implementation: may be overridden */ -void Print::write(void *buffer, uint32 size) -{ - uint8 *ch = (uint8*)buffer; - while (size--) { - write(*ch++); - } -} +//------------------------------ Public Methods ------------------------------- -void Print::print(uint8 b) -{ - this->write(b); +void Print::write(const char *str) { + while (*str) + write(*str++); } -void Print::print(char c) -{ - print((byte) c); +void Print::write(void *buffer, uint32 size) { + uint8 *ch = (uint8*)buffer; + while (size--) { + write(*ch++); + } } -void Print::print(const char str[]) -{ - write(str); +void Print::print(uint8 b) { + this->write(b); } -void Print::print(int n) -{ - print((long) n); +void Print::print(char c) { + print((byte) c); } -void Print::print(unsigned int n) -{ - print((unsigned long) n); +void Print::print(const char str[]) { + write(str); } -void Print::print(long n) -{ - if (n < 0) { - print('-'); - n = -n; - } - printNumber(n, 10); +void Print::print(int n) { + print((long) n); } -void Print::print(unsigned long n) -{ - printNumber(n, 10); +void Print::print(unsigned int n) { + print((unsigned long) n); } -void Print::print(long n, int base) -{ - if (base == 0) - print((char) n); - else if (base == 10) - print(n); - else - printNumber(n, base); +void Print::print(long n) { + if (n < 0) { + print('-'); + n = -n; + } + printNumber(n, 10); } -void Print::print(double n) -{ - printFloat(n, 2); +void Print::print(unsigned long n) { + printNumber(n, 10); } -void Print::println(void) -{ - print('\r'); - print('\n'); +void Print::print(long n, int base) { + if (base == 0) { + print((char) n); + } else if (base == 10) { + print(n); + } else { + printNumber(n, base); + } } -void Print::println(char c) -{ - print(c); - println(); +void Print::print(double n) { + printFloat(n, 2); } -void Print::println(const char c[]) -{ - print(c); - println(); +void Print::println(void) { + print('\r'); + print('\n'); } -void Print::println(uint8 b) -{ - print(b); - println(); +void Print::println(char c) { + print(c); + println(); } -void Print::println(int n) -{ - print(n); - println(); +void Print::println(const char c[]) { + print(c); + println(); } -void Print::println(unsigned int n) -{ - print(n); - println(); +void Print::println(uint8 b) { + print(b); + println(); } -void Print::println(long n) -{ - print(n); - println(); +void Print::println(int n) { + print(n); + println(); } -void Print::println(unsigned long n) -{ - print(n); - println(); +void Print::println(unsigned int n) { + print(n); + println(); } -void Print::println(long n, int base) -{ - print(n, base); - println(); +void Print::println(long n) { + print(n); + println(); } -void Print::println(double n) -{ - print(n); - println(); +void Print::println(unsigned long n) { + print(n); + println(); } -// Private Methods ///////////////////////////////////////////////////////////// - -void Print::printNumber(unsigned long n, uint8 base) -{ - unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars. - unsigned long i = 0; - - if (n == 0) { - print('0'); - return; - } - - while (n > 0) { - buf[i++] = n % base; - n /= base; - } - - for (; i > 0; i--) - print((char) (buf[i - 1] < 10 ? - '0' + buf[i - 1] : - 'A' + buf[i - 1] - 10)); +void Print::println(long n, int base) { + print(n, base); + println(); } -void Print::printFloat(double number, uint8 digits) -{ - // Handle negative numbers - if (number < 0.0) - { - print('-'); - number = -number; - } - - // Round correctly so that print(1.999, 2) prints as "2.00" - double rounding = 0.5; - for (uint8 i=0; i 0) - print("."); - - // Extract digits from the remainder one at a time - while (digits-- > 0) - { - remainder *= 10.0; - int toPrint = int(remainder); - print(toPrint); - remainder -= toPrint; - } +void Print::println(double n) { + print(n); + println(); +} + +//------------------------------ Private Methods ------------------------------ + +void Print::printNumber(unsigned long n, uint8 base) { + unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars. + unsigned long i = 0; + + if (n == 0) { + print('0'); + return; + } + + while (n > 0) { + buf[i++] = n % base; + n /= base; + } + + for (; i > 0; i--) + print((char) (buf[i - 1] < 10 ? + '0' + buf[i - 1] : + 'A' + buf[i - 1] - 10)); +} + +void Print::printFloat(double number, uint8 digits) { + // Handle negative numbers + if (number < 0.0) { + print('-'); + number = -number; + } + + // Round correctly so that print(1.999, 2) prints as "2.00" + double rounding = 0.5; + for (uint8 i=0; i 0) { + print("."); + } + + // Extract digits from the remainder one at a time + while (digits-- > 0) { + remainder *= 10.0; + int toPrint = int(remainder); + print(toPrint); + remainder -= toPrint; + } } diff --git a/wirish/Print.h b/wirish/Print.h index 9c03978..dc21183 100644 --- a/wirish/Print.h +++ b/wirish/Print.h @@ -1,21 +1,22 @@ /* - Print.h - Base class that provides print() and println() - Copyright (c) 2008 David A. Mellis. 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 -*/ + * Print.h - Base class that provides print() and println() + * Copyright (c) 2008 David A. Mellis. 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. + */ #ifndef Print_h #define Print_h @@ -31,10 +32,10 @@ class Print { - private: + private: void printNumber(unsigned long, uint8); void printFloat(double, uint8); - public: + public: virtual void write(uint8) = 0; virtual void write(const char *str); virtual void write(void *, uint32); diff --git a/wirish/WProgram.h b/wirish/WProgram.h index 9143991..36984ff 100644 --- a/wirish/WProgram.h +++ b/wirish/WProgram.h @@ -1,4 +1,4 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 LeafLabs LLC. @@ -20,7 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - * ****************************************************************************/ + *****************************************************************************/ #include "wirish.h" diff --git a/wirish/bits.h b/wirish/bits.h index 7ebea80..7b51e5e 100644 --- a/wirish/bits.h +++ b/wirish/bits.h @@ -1,23 +1,23 @@ /* - Part of Arduino - http://www.arduino.cc/ - - Copyright (c) 2005-2006 David A. Mellis - - 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., 59 Temple Place, Suite 330, - Boston, MA 02111-1307 USA -*/ + * Part of Arduino - http://www.arduino.cc/ + * + * Copyright (c) 2005-2006 David A. Mellis + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ // BIT DEFINITION diff --git a/wirish/boards.h b/wirish/boards.h index 0e0d159..eed3e26 100644 --- a/wirish/boards.h +++ b/wirish/boards.h @@ -1,4 +1,4 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 Bryan Newbold. @@ -20,12 +20,13 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - * ****************************************************************************/ + *****************************************************************************/ -// This file contains BOARD-specific pin mapping tables. To add a new board -// type, copy the "BOARD_maple" section below and edit it as needed, then -// update your build toolchain with a new "BOARD" type. This must match the -// seperate MCU type (which determines the ../libmaple configuration). +/* This file contains board-specific pin mapping tables. To add a new board + * type, copy the "BOARD_maple" section below and edit it as needed, then + * update your build toolchain with a new "BOARD" type. This must match the + * separate MCU type (which determines the ../libmaple configuration). + */ #ifndef _BOARDS_H_ #define _BOARDS_H_ @@ -36,10 +37,10 @@ #include "exti.h" #ifdef __cplusplus -extern "C"{ +extern "C" { #endif -// Set of all possible digital pin names; not all boards have all these +/* Set of all possible digital pin names; not all boards have all these */ enum { D0, D1, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11, D12, D13, D14, D15, D16, D17, D18, D19, D20, D21, D22, D23, D24, D25, D26, D27, D28, D29, D30, D31, @@ -50,7 +51,7 @@ enum { D92, D93, D94, D95, D96, D97, D98, D99, D100, D101, D102, D103, D104, D105, D106, D107, D108, D109, D110, D111, }; -// Set of all possible analog pin names; not all boards have all these +/* Set of all possible analog pin names; not all boards have all these */ enum { ADC0, ADC1, ADC2, ADC3, ADC4, ADC5, ADC6, ADC7, ADC8, ADC9, ADC10, ADC11, ADC12, ADC13, ADC14, ADC15, ADC16, ADC17, ADC18, ADC19, ADC20, }; @@ -58,7 +59,7 @@ enum { #define ADC_INVALID 0xFFFFFFFF #define TIMER_INVALID (TimerCCR)0xFFFFFFFF -// Types used for the tables below +/* Types used for the tables below */ typedef struct PinMapping { GPIO_Port *port; uint32 pin; @@ -67,59 +68,102 @@ typedef struct PinMapping { uint32 exti_port; } PinMapping; -// LeafLabs Maple rev3, rev4 +/* LeafLabs Maple rev3, rev4 */ #ifdef BOARD_maple #define CYCLES_PER_MICROSECOND 72 #define MAPLE_RELOAD_VAL 71999 /* takes a cycle to reload */ static __attribute__ ((unused)) PinMapping PIN_MAP[NR_GPIO_PINS] = { - {GPIOA_BASE, 3, ADC3, TIMER2_CH4_CCR, EXTI_CONFIG_PORTA}, // D0/PA3 - {GPIOA_BASE, 2, ADC2, TIMER2_CH3_CCR, EXTI_CONFIG_PORTA}, // D1/PA2 - {GPIOA_BASE, 0, ADC0, TIMER2_CH1_CCR, EXTI_CONFIG_PORTA}, // D2/PA0 - {GPIOA_BASE, 1, ADC1, TIMER2_CH2_CCR, EXTI_CONFIG_PORTA}, // D3/PA1 - {GPIOB_BASE, 5, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTB}, // D4/PB5 - {GPIOB_BASE, 6, ADC_INVALID, TIMER4_CH1_CCR, EXTI_CONFIG_PORTB}, // D5/PB6 - {GPIOA_BASE, 8, ADC_INVALID, TIMER1_CH1_CCR, EXTI_CONFIG_PORTA}, // D6/PA8 - {GPIOA_BASE, 9, ADC_INVALID, TIMER1_CH2_CCR, EXTI_CONFIG_PORTA}, // D7/PA9 - {GPIOA_BASE, 10, ADC_INVALID, TIMER1_CH3_CCR, EXTI_CONFIG_PORTA}, // D8/PA10 - {GPIOB_BASE, 7, ADC_INVALID, TIMER4_CH2_CCR, EXTI_CONFIG_PORTB}, // D9/PB7 - {GPIOA_BASE, 4, ADC4, TIMER_INVALID, EXTI_CONFIG_PORTA}, // D10/PA4 - {GPIOA_BASE, 7, ADC7, TIMER3_CH2_CCR, EXTI_CONFIG_PORTA}, // D11/PA7 - {GPIOA_BASE, 6, ADC6, TIMER3_CH1_CCR, EXTI_CONFIG_PORTA}, // D12/PA6 - {GPIOA_BASE, 5, ADC5, TIMER_INVALID, EXTI_CONFIG_PORTA}, // D13/PA5 - {GPIOB_BASE, 8, ADC_INVALID, TIMER4_CH3_CCR, EXTI_CONFIG_PORTB}, // D14/PB8 - /* Little header */ - {GPIOC_BASE, 0, ADC10, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D15/PC0 - {GPIOC_BASE, 1, ADC11, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D16/PC1 - {GPIOC_BASE, 2, ADC12, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D17/PC2 - {GPIOC_BASE, 3, ADC13, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D18/PC3 - {GPIOC_BASE, 4, ADC14, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D19/PC4 - {GPIOC_BASE, 5, ADC15, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D20/PC5 - /* External header */ - {GPIOC_BASE, 13, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D21/PC13 - {GPIOC_BASE, 14, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D22/PC14 - {GPIOC_BASE, 15, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D23/PC15 - {GPIOB_BASE, 9, ADC_INVALID, TIMER4_CH4_CCR, EXTI_CONFIG_PORTB}, // D24/PB9 - {GPIOD_BASE, 2, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTD}, // D25/PD2 - {GPIOC_BASE, 10, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D26/PC10 - {GPIOB_BASE, 0, ADC8, TIMER3_CH3_CCR, EXTI_CONFIG_PORTB}, // D27/PB0 - {GPIOB_BASE, 1, ADC9, TIMER3_CH4_CCR, EXTI_CONFIG_PORTB}, // D28/PB1 - {GPIOB_BASE, 10, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTB}, // D29/PB10 - {GPIOB_BASE, 11, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTB}, // D30/PB11 - {GPIOB_BASE, 12, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTB}, // D31/PB12 - {GPIOB_BASE, 13, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTB}, // D32/PB13 - {GPIOB_BASE, 14, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTB}, // D33/PB14 - {GPIOB_BASE, 15, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTB}, // D34/PB15 - {GPIOC_BASE, 6, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D35/PC6 - {GPIOC_BASE, 7, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D36/PC7 - {GPIOC_BASE, 8, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTC}, // D37/PC8 - {GPIOC_BASE, 9, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTC} // D38/PC9 (BUT) + /* D0/PA3 */ + {GPIOA_BASE, 3, ADC3, TIMER2_CH4_CCR, EXTI_CONFIG_PORTA}, + /* D1/PA2 */ + {GPIOA_BASE, 2, ADC2, TIMER2_CH3_CCR, EXTI_CONFIG_PORTA}, + /* D2/PA0 */ + {GPIOA_BASE, 0, ADC0, TIMER2_CH1_CCR, EXTI_CONFIG_PORTA}, + /* D3/PA1 */ + {GPIOA_BASE, 1, ADC1, TIMER2_CH2_CCR, EXTI_CONFIG_PORTA}, + /* D4/PB5 */ + {GPIOB_BASE, 5, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTB}, + /* D5/PB6 */ + {GPIOB_BASE, 6, ADC_INVALID, TIMER4_CH1_CCR, EXTI_CONFIG_PORTB}, + /* D6/PA8 */ + {GPIOA_BASE, 8, ADC_INVALID, TIMER1_CH1_CCR, EXTI_CONFIG_PORTA}, + /* D7/PA9 */ + {GPIOA_BASE, 9, ADC_INVALID, TIMER1_CH2_CCR, EXTI_CONFIG_PORTA}, + /* D8/PA10 */ + {GPIOA_BASE, 10, ADC_INVALID, TIMER1_CH3_CCR, EXTI_CONFIG_PORTA}, + /* D9/PB7 */ + {GPIOB_BASE, 7, ADC_INVALID, TIMER4_CH2_CCR, EXTI_CONFIG_PORTB}, + /* D10/PA4 */ + {GPIOA_BASE, 4, ADC4, TIMER_INVALID, EXTI_CONFIG_PORTA}, + /* D11/PA7 */ + {GPIOA_BASE, 7, ADC7, TIMER3_CH2_CCR, EXTI_CONFIG_PORTA}, + /* D12/PA6 */ + {GPIOA_BASE, 6, ADC6, TIMER3_CH1_CCR, EXTI_CONFIG_PORTA}, + /* D13/PA5 */ + {GPIOA_BASE, 5, ADC5, TIMER_INVALID, EXTI_CONFIG_PORTA}, + /* D14/PB8 */ + {GPIOB_BASE, 8, ADC_INVALID, TIMER4_CH3_CCR, EXTI_CONFIG_PORTB}, + + /* Little header */ + + /* D15/PC0 */ + {GPIOC_BASE, 0, ADC10, TIMER_INVALID, EXTI_CONFIG_PORTC}, + /* D16/PC1 */ + {GPIOC_BASE, 1, ADC11, TIMER_INVALID, EXTI_CONFIG_PORTC}, + /* D17/PC2 */ + {GPIOC_BASE, 2, ADC12, TIMER_INVALID, EXTI_CONFIG_PORTC}, + /* D18/PC3 */ + {GPIOC_BASE, 3, ADC13, TIMER_INVALID, EXTI_CONFIG_PORTC}, + /* D19/PC4 */ + {GPIOC_BASE, 4, ADC14, TIMER_INVALID, EXTI_CONFIG_PORTC}, + /* D20/PC5 */ + {GPIOC_BASE, 5, ADC15, TIMER_INVALID, EXTI_CONFIG_PORTC}, + + /* External header */ + + /* D21/PC13 */ + {GPIOC_BASE, 13, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTC}, + /* D22/PC14 */ + {GPIOC_BASE, 14, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTC}, + /* D23/PC15 */ + {GPIOC_BASE, 15, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTC}, + /* D24/PB9 */ + {GPIOB_BASE, 9, ADC_INVALID, TIMER4_CH4_CCR, EXTI_CONFIG_PORTB}, + /* D25/PD2 */ + {GPIOD_BASE, 2, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTD}, + /* D26/PC10 */ + {GPIOC_BASE, 10, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTC}, + /* D27/PB0 */ + {GPIOB_BASE, 0, ADC8, TIMER3_CH3_CCR, EXTI_CONFIG_PORTB}, + /* D28/PB1 */ + {GPIOB_BASE, 1, ADC9, TIMER3_CH4_CCR, EXTI_CONFIG_PORTB}, + /* D29/PB10 */ + {GPIOB_BASE, 10, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTB}, + /* D30/PB11 */ + {GPIOB_BASE, 11, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTB}, + /* D31/PB12 */ + {GPIOB_BASE, 12, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTB}, + /* D32/PB13 */ + {GPIOB_BASE, 13, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTB}, + /* D33/PB14 */ + {GPIOB_BASE, 14, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTB}, + /* D34/PB15 */ + {GPIOB_BASE, 15, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTB}, + /* D35/PC6 */ + {GPIOC_BASE, 6, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTC}, + /* D36/PC7 */ + {GPIOC_BASE, 7, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTC}, + /* D37/PC8 */ + {GPIOC_BASE, 8, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTC}, + /* PC9 (BUT) */ + {GPIOC_BASE, 9, ADC_INVALID, TIMER_INVALID, EXTI_CONFIG_PORTC} }; #endif -// LeafLabs Maple Native (prototype) +/* LeafLabs Maple Native (prototype) */ #ifdef BOARD_maple_native #define CYCLES_PER_MICROSECOND 72 @@ -127,65 +171,122 @@ typedef struct PinMapping { // TODO: static __attribute__ ((unused)) PinMapping PIN_MAP[NR_GPIO_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 + /* D0/PA3 */ + {GPIOA_BASE, 3, ADC3, TIMER2_CH4_CCR}, + /* D1/PA2 */ + {GPIOA_BASE, 2, ADC2, TIMER2_CH3_CCR}, + /* D2/PA0 */ + {GPIOA_BASE, 0, ADC0, TIMER2_CH1_CCR}, + /* D3/PA1 */ + {GPIOA_BASE, 1, ADC1, TIMER2_CH2_CCR}, + /* D4/PB5 */ + {GPIOB_BASE, 5, ADC_INVALID, TIMER_INVALID}, + /* D5/PB6 */ + {GPIOB_BASE, 6, ADC_INVALID, TIMER4_CH1_CCR}, + /* D6/PA8 */ + {GPIOA_BASE, 8, ADC_INVALID, TIMER1_CH1_CCR}, + /* D7/PA9 */ + {GPIOA_BASE, 9, ADC_INVALID, TIMER1_CH2_CCR}, + /* D8/PA10 */ + {GPIOA_BASE, 10, ADC_INVALID, TIMER1_CH3_CCR}, + /* D9/PB7 */ + {GPIOB_BASE, 7, ADC_INVALID, TIMER4_CH2_CCR}, + /* D10/PA4 */ + {GPIOA_BASE, 4, ADC4, TIMER_INVALID}, + /* D11/PA7 */ + {GPIOA_BASE, 7, ADC7, TIMER3_CH2_CCR}, + /* D12/PA6 */ + {GPIOA_BASE, 6, ADC6, TIMER3_CH1_CCR}, + /* D13/PA5 */ + {GPIOA_BASE, 5, ADC5, TIMER_INVALID}, + /* D14/PB8 */ + {GPIOB_BASE, 8, ADC_INVALID, TIMER4_CH3_CCR}, + /* Little header */ - {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 + + /* D15/PC0 */ + {GPIOC_BASE, 0, ADC10, TIMER_INVALID}, + /* D16/PC1 */ + {GPIOC_BASE, 1, ADC11, TIMER_INVALID}, + /* D17/PC2 */ + {GPIOC_BASE, 2, ADC12, TIMER_INVALID}, + /* D18/PC3 */ + {GPIOC_BASE, 3, ADC13, TIMER_INVALID}, + /* D19/PC4 */ + {GPIOC_BASE, 4, ADC14, TIMER_INVALID}, + /* D20/PC5 */ + {GPIOC_BASE, 5, ADC15, TIMER_INVALID}, + /* External header */ - {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 (BUT) + + /* D21/PC13 */ + {GPIOC_BASE, 13, ADC_INVALID, TIMER_INVALID}, + /* D22/PC14 */ + {GPIOC_BASE, 14, ADC_INVALID, TIMER_INVALID}, + /* D23/PC15 */ + {GPIOC_BASE, 15, ADC_INVALID, TIMER_INVALID}, + /* D24/PB9 */ + {GPIOB_BASE, 9, ADC_INVALID, TIMER4_CH4_CCR}, + /* D25/PD2 */ + {GPIOD_BASE, 2, ADC_INVALID, TIMER_INVALID}, + /* D26/PC10 */ + {GPIOC_BASE, 10, ADC_INVALID, TIMER_INVALID}, + /* D27/PB0 */ + {GPIOB_BASE, 0, ADC8, TIMER3_CH3_CCR}, + /* D28/PB1 */ + {GPIOB_BASE, 1, ADC9, TIMER3_CH4_CCR}, + /* D29/PB10 */ + {GPIOB_BASE, 10, ADC_INVALID, TIMER_INVALID}, + /* D30/PB11 */ + {GPIOB_BASE, 11, ADC_INVALID, TIMER_INVALID}, + /* D31/PB12 */ + {GPIOB_BASE, 12, ADC_INVALID, TIMER_INVALID}, + /* D32/PB13 */ + {GPIOB_BASE, 13, ADC_INVALID, TIMER_INVALID}, + /* D33/PB14 */ + {GPIOB_BASE, 14, ADC_INVALID, TIMER_INVALID}, + /* D34/PB15 */ + {GPIOB_BASE, 15, ADC_INVALID, TIMER_INVALID}, + /* D35/PC6 */ + {GPIOC_BASE, 6, ADC_INVALID, TIMER_INVALID}, + /* D36/PC7 */ + {GPIOC_BASE, 7, ADC_INVALID, TIMER_INVALID}, + /* D37/PC8 */ + {GPIOC_BASE, 8, ADC_INVALID, TIMER_INVALID}, + /* PC9 (BUT) */ + {GPIOC_BASE, 9, ADC_INVALID, TIMER_INVALID} }; - static __attribute__ ((unused)) ExtiInfo PIN_TO_EXTI_CHANNEL[NR_GPIO_PINS] = + static __attribute__((unused)) ExtiInfo PIN_TO_EXTI_CHANNEL[NR_GPIO_PINS] = { - {EXTI3, EXTI_CONFIG_PORTA}, // D0/PA3 - {EXTI2, EXTI_CONFIG_PORTA}, // D1/PA2 - {EXTI0, EXTI_CONFIG_PORTA}, // D2/PA0 - {EXTI1, EXTI_CONFIG_PORTA}, // D3/PA1 - {EXTI5, EXTI_CONFIG_PORTB}, // D4/PB5 - {EXTI6, EXTI_CONFIG_PORTB}, // D5/PB6 - {EXTI8, EXTI_CONFIG_PORTA}, // D6/PA8 - {EXTI9, EXTI_CONFIG_PORTA}, // D7/PA9 - {EXTI10, EXTI_CONFIG_PORTA}, // D8/PA10 - {EXTI7, EXTI_CONFIG_PORTB}, // D9/PB7 - {EXTI4, EXTI_CONFIG_PORTA}, // D10/PA4 - {EXTI7, EXTI_CONFIG_PORTA}, // D11/PA7 - {EXTI6, EXTI_CONFIG_PORTA}, // D12/PA6 - {EXTI5, EXTI_CONFIG_PORTA}, // D13/PA5 + /* D0/PA3 */ + {EXTI3, EXTI_CONFIG_PORTA}, + /* D1/PA2 */ + {EXTI2, EXTI_CONFIG_PORTA}, + /* D2/PA0 */ + {EXTI0, EXTI_CONFIG_PORTA}, + /* D3/PA1 */ + {EXTI1, EXTI_CONFIG_PORTA}, + /* D4/PB5 */ + {EXTI5, EXTI_CONFIG_PORTB}, + /* D5/PB6 */ + {EXTI6, EXTI_CONFIG_PORTB}, + /* D6/PA8 */ + {EXTI8, EXTI_CONFIG_PORTA}, + /* D7/PA9 */ + {EXTI9, EXTI_CONFIG_PORTA}, + /* D8/PA10 */ + {EXTI10, EXTI_CONFIG_PORTA}, + /* D9/PB7 */ + {EXTI7, EXTI_CONFIG_PORTB}, + /* D10/PA4 */ + {EXTI4, EXTI_CONFIG_PORTA}, + /* D11/PA7 */ + {EXTI7, EXTI_CONFIG_PORTA}, + /* D12/PA6 */ + {EXTI6, EXTI_CONFIG_PORTA}, + /* D13/PA5 */ + {EXTI5, EXTI_CONFIG_PORTA}, }; #endif diff --git a/wirish/ext_interrupts.c b/wirish/ext_interrupts.c index 5009d75..f02cdc5 100644 --- a/wirish/ext_interrupts.c +++ b/wirish/ext_interrupts.c @@ -1,4 +1,4 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 Perry Hung. @@ -20,7 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - * ****************************************************************************/ + *****************************************************************************/ /** * @file ext_interrupts.c @@ -44,7 +44,8 @@ */ void attachInterrupt(uint8 pin, voidFuncPtr handler, uint32 mode) { uint8 outMode; - /* Parameter checking */ + + /* Parameter checking */ if (pin >= NR_GPIO_PINS) { return; } @@ -68,9 +69,9 @@ void attachInterrupt(uint8 pin, voidFuncPtr handler, uint32 mode) { } exti_attach_interrupt(PIN_MAP[pin].exti_port, - PIN_MAP[pin].pin, - handler, - mode); + PIN_MAP[pin].pin, + handler, + mode); return; } diff --git a/wirish/ext_interrupts.h b/wirish/ext_interrupts.h index 62f31bb..fef8c8f 100644 --- a/wirish/ext_interrupts.h +++ b/wirish/ext_interrupts.h @@ -1,4 +1,4 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 Perry Hung. @@ -20,7 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - * ****************************************************************************/ + *****************************************************************************/ /** * @file ext_interrupts.h diff --git a/wirish/io.h b/wirish/io.h index 4aa1eef..647e79c 100644 --- a/wirish/io.h +++ b/wirish/io.h @@ -1,4 +1,4 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 Perry Hung. @@ -20,12 +20,12 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - * ****************************************************************************/ + *****************************************************************************/ /** * @file io.h * - * @brief + * @brief Arduino-compatible digital pin I/O interface. */ #ifndef _IO_H @@ -38,7 +38,6 @@ extern "C"{ #endif - typedef enum WiringPinMode { OUTPUT, OUTPUT_OPEN_DRAIN, @@ -85,7 +84,7 @@ uint32 digitalRead(uint8); * to INPUT_ANALOG * analogRead(pin) * pin -> {A0-A16} - * */ + */ uint32 analogRead(uint8); #ifdef __cplusplus diff --git a/wirish/pwm.c b/wirish/pwm.c index 995e2c7..2f555ab 100644 --- a/wirish/pwm.c +++ b/wirish/pwm.c @@ -1,4 +1,4 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 Perry Hung. @@ -20,10 +20,10 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - * ****************************************************************************/ + *****************************************************************************/ /** - * @brief + * @brief Arduino-compatible PWM implementation. */ #include "wirish.h" @@ -40,8 +40,9 @@ void pwmWrite(uint8 pin, uint16 duty_cycle) { ccr = PIN_MAP[pin].timer_channel; - if (ccr == TIMER_INVALID) + if (ccr == TIMER_INVALID) { return; + } timer_pwm_write_ccr(ccr, duty_cycle); } diff --git a/wirish/pwm.h b/wirish/pwm.h index 927b685..fe170cd 100644 --- a/wirish/pwm.h +++ b/wirish/pwm.h @@ -1,4 +1,4 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 Perry Hung. @@ -20,12 +20,12 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - * ****************************************************************************/ + *****************************************************************************/ /** * @file pwm.h * - * @brief + * @brief Arduino-compatible PWM interface. */ #ifndef _PWM_H diff --git a/wirish/time.c b/wirish/time.c index eaa3c9e..3a48197 100644 --- a/wirish/time.c +++ b/wirish/time.c @@ -1,4 +1,4 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 Perry Hung. @@ -20,30 +20,30 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - * ****************************************************************************/ + *****************************************************************************/ /** - * @brief + * @brief Delay implementation. */ #include "libmaple.h" #include "systick.h" #include "time.h" -void delay(unsigned long ms) -{ - uint32 i; - for (i = 0; i < ms; i++) { - delayMicroseconds(1000); - } +void delay(unsigned long ms) { + uint32 i; + for (i = 0; i < ms; i++) { + delayMicroseconds(1000); + } } void delayMicroseconds(uint32 us) { - // So (2^32)/12 micros max, or less than 6 minutes + /* So (2^32)/12 micros max, or less than 6 minutes */ us *= 12; /* fudge for function call overhead */ us--; + int x = 4; asm volatile(" mov r0, %[us] \n\t" "1: subs r0, #1 \n\t" " bhi 1b \n\t" diff --git a/wirish/time.h b/wirish/time.h index ad39057..fad47a4 100644 --- a/wirish/time.h +++ b/wirish/time.h @@ -1,4 +1,4 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 Perry Hung. @@ -20,10 +20,10 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - * ****************************************************************************/ + *****************************************************************************/ /** - * @brief + * @brief Timing and delay functions. */ #ifndef _TIME_H @@ -43,28 +43,28 @@ extern volatile uint32 systick_timer_millis; /* time in milliseconds since boot */ static inline uint32 millis(void) { - return systick_timer_millis; + return systick_timer_millis; } /* Time in microseconds since boot */ static inline uint32 micros(void) { - uint32 ms; - uint32 cycle_cnt; - uint32 res; + uint32 ms; + uint32 cycle_cnt; + uint32 res; - nvic_globalirq_disable(); + nvic_globalirq_disable(); - cycle_cnt = systick_get_count(); - ms = millis(); + cycle_cnt = systick_get_count(); + ms = millis(); - nvic_globalirq_enable(); + nvic_globalirq_enable(); - /* MAPLE_RELOAD_VAL is 1 less than the number of cycles it actually - takes to complete a systick reload */ - res = (ms * US_PER_MS) + - (MAPLE_RELOAD_VAL + 1 - cycle_cnt)/CYCLES_PER_MICROSECOND; + /* MAPLE_RELOAD_VAL is 1 less than the number of cycles it actually + takes to complete a systick reload */ + res = (ms * US_PER_MS) + + (MAPLE_RELOAD_VAL + 1 - cycle_cnt)/CYCLES_PER_MICROSECOND; - return res; + return res; } void delay(unsigned long ms); diff --git a/wirish/usb_serial.cpp b/wirish/usb_serial.cpp index fafdf49..405220a 100644 --- a/wirish/usb_serial.cpp +++ b/wirish/usb_serial.cpp @@ -1,4 +1,4 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 Perry Hung. @@ -20,10 +20,10 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - * ****************************************************************************/ + *****************************************************************************/ /** - * @brief wirish usb class for easy goin communication, uses libmaple's + * @brief Wirish USB class for easy communication, uses libmaple's * virtual com port implementation */ @@ -49,8 +49,10 @@ void USBSerial::write(uint8 ch) { if(!(usbIsConnected() && usbIsConfigured())) { return; } + uint16 status = 0; uint32 start = millis(); + while(status == 0 && (millis() - start <= USB_TIMEOUT)) { status = usbSendBytes(&ch, 1); } @@ -60,13 +62,15 @@ void USBSerial::write(const char *str) { if(!(usbIsConnected() && usbIsConfigured())) { return; } + uint32 len = strlen(str); uint16 status = 0; uint16 oldstatus = 0; uint32 start = millis(); + while(status < len && (millis() - start < USB_TIMEOUT)) { status += usbSendBytes((uint8*)str+status, len-status); - if(oldstatus != status) + if(oldstatus != status) start = millis(); oldstatus = status; } @@ -76,52 +80,55 @@ void USBSerial::write(void *buf, uint32 size) { if(!(usbIsConnected() && usbIsConfigured())) { return; } + if (!buf) { return; } + uint16 status = 0; uint16 oldstatus = 0; uint32 start = millis(); + while(status < size && (millis() - start < USB_TIMEOUT)) { status += usbSendBytes((uint8*)buf+status, size-status); - if(oldstatus != status) + if(oldstatus != status) start = millis(); oldstatus = status; } } uint32 USBSerial::available(void) { - return usbBytesAvailable(); + return usbBytesAvailable(); } uint32 USBSerial::read(void *buf, uint32 len) { - if (!buf) { - return 0; - } + if (!buf) { + return 0; + } - return usbReceiveBytes((uint8*)buf, len); + return usbReceiveBytes((uint8*)buf, len); } uint8 USBSerial::read(void) { - uint8 ch; - usbReceiveBytes(&ch, 1); - return ch; + uint8 ch; + usbReceiveBytes(&ch, 1); + return ch; } uint8 USBSerial::pending(void) { - return usbGetPending(); + return usbGetPending(); } uint8 USBSerial::getDTR(void) { - return usbGetDTR(); + return usbGetDTR(); } uint8 USBSerial::getRTS(void) { - return usbGetRTS(); + return usbGetRTS(); } uint8 USBSerial::isConnected(void) { - return (usbIsConnected() && usbIsConfigured()); + return (usbIsConnected() && usbIsConfigured()); } USBSerial SerialUSB; diff --git a/wirish/usb_serial.h b/wirish/usb_serial.h index 7c87c04..c228837 100644 --- a/wirish/usb_serial.h +++ b/wirish/usb_serial.h @@ -1,4 +1,4 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 Perry Hung. @@ -20,11 +20,11 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - * ****************************************************************************/ + *****************************************************************************/ /** - * @brief wirish usb class for easy goin communication, uses libmaple's - * virtual com port implementation + * @brief wirish usb class for easy goin communication, uses libmaple's + * virtual com port implementation */ #ifndef _USB_SERIAL_H_ @@ -33,25 +33,25 @@ #include "Print.h" class USBSerial : public Print { - public: - USBSerial(void); +public: + USBSerial(void); - void begin(void); - void end(void); + void begin(void); + void end(void); - uint32 available(void); + uint32 available(void); - uint32 read(void *buf, uint32 len); - uint8 read(void); + uint32 read(void *buf, uint32 len); + uint8 read(void); - void write(uint8); - void write(const char *str); - void write(void *, uint32); + void write(uint8); + void write(const char *str); + void write(void *, uint32); - uint8 getRTS(); - uint8 getDTR(); - uint8 isConnected(); - uint8 pending(); + uint8 getRTS(); + uint8 getDTR(); + uint8 isConnected(); + uint8 pending(); }; extern USBSerial SerialUSB; diff --git a/wirish/wirish.c b/wirish/wirish.c index 5407131..0abec41 100644 --- a/wirish/wirish.c +++ b/wirish/wirish.c @@ -1,4 +1,4 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 Perry Hung. @@ -20,17 +20,16 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - * ****************************************************************************/ + *****************************************************************************/ /** - * @brief generic maple board bring up: + * @brief generic maple board bring up: * - * By default, we bring up all maple boards running on the stm32 to 72mhz, - * clocked off the PLL, driven by the 8MHz external crystal. - * - * AHB and APB2 are clocked at 72MHz - * APB1 is clocked at 36MHz + * By default, we bring up all maple boards running on the stm32 to 72mhz, + * clocked off the PLL, driven by the 8MHz external crystal. * + * AHB and APB2 are clocked at 72MHz + * APB1 is clocked at 36MHz */ #include "wirish.h" @@ -44,35 +43,35 @@ #include "flash.h" void init(void) { - /* make sure the flash is ready before spinning the high speed clock up */ - flash_enable_prefetch(); - flash_set_latency(FLASH_WAIT_STATE_2); + /* make sure the flash is ready before spinning the high speed clock up */ + flash_enable_prefetch(); + flash_set_latency(FLASH_WAIT_STATE_2); - #if NR_FSMC > 0 - fsmc_native_sram_init(); - #endif +#if NR_FSMC > 0 + fsmc_native_sram_init(); +#endif - #if NR_DAC_PINS > 0 - dac_init(); - #endif +#if NR_DAC_PINS > 0 + dac_init(); +#endif - /* initialize clocks */ - rcc_clk_init(RCC_CLKSRC_PLL, RCC_PLLSRC_HSE, RCC_PLLMUL_9); - rcc_set_prescaler(RCC_PRESCALER_AHB, RCC_AHB_SYSCLK_DIV_1); - rcc_set_prescaler(RCC_PRESCALER_APB1, RCC_APB1_HCLK_DIV_2); - rcc_set_prescaler(RCC_PRESCALER_APB2, RCC_APB2_HCLK_DIV_1); + /* initialize clocks */ + rcc_clk_init(RCC_CLKSRC_PLL, RCC_PLLSRC_HSE, RCC_PLLMUL_9); + rcc_set_prescaler(RCC_PRESCALER_AHB, RCC_AHB_SYSCLK_DIV_1); + rcc_set_prescaler(RCC_PRESCALER_APB1, RCC_APB1_HCLK_DIV_2); + rcc_set_prescaler(RCC_PRESCALER_APB2, RCC_APB2_HCLK_DIV_1); - nvic_init(); - systick_init(MAPLE_RELOAD_VAL); - gpio_init(); - adc_init(); - timer_init(TIMER1, 1); - timer_init(TIMER2, 1); - timer_init(TIMER3, 1); - timer_init(TIMER4, 1); - #if NR_TIMERS >= 8 - timer_init(TIMER5, 1); - timer_init(TIMER8, 1); - #endif - setupUSB(); + nvic_init(); + systick_init(MAPLE_RELOAD_VAL); + gpio_init(); + adc_init(); + timer_init(TIMER1, 1); + timer_init(TIMER2, 1); + timer_init(TIMER3, 1); + timer_init(TIMER4, 1); +#if NR_TIMERS >= 8 + timer_init(TIMER5, 1); + timer_init(TIMER8, 1); +#endif + setupUSB(); } diff --git a/wirish/wirish.h b/wirish/wirish.h index 7ede77c..c1c46cb 100644 --- a/wirish/wirish.h +++ b/wirish/wirish.h @@ -1,4 +1,4 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 Perry Hung. @@ -20,14 +20,14 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - * ****************************************************************************/ + *****************************************************************************/ + /** * @brief Main include file for the Wirish core. * * Includes various Arduino wiring macros and bit defines */ - #ifndef _WIRISH_H_ #define _WIRISH_H_ @@ -62,14 +62,14 @@ extern "C"{ #define LSBFIRST 0 #define MSBFIRST 1 -#define lowByte(w) ((w) & 0xff) -#define highByte(w) ((w) >> 8) -#define bitRead(value, bit) (((value) >> (bit)) & 0x01) -#define bitSet(value, bit) ((value) |= (1UL << (bit))) -#define bitClear(value, bit) ((value) &= ~(1UL << (bit))) -#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : \ - bitClear(value, bit)) -#define bit(b) (1UL << (b)) +#define lowByte(w) ((w) & 0xff) +#define highByte(w) ((w) >> 8) +#define bitRead(value, bit) (((value) >> (bit)) & 0x01) +#define bitSet(value, bit) ((value) |= (1UL << (bit))) +#define bitClear(value, bit) ((value) &= ~(1UL << (bit))) +#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : \ + bitClear(value, bit)) +#define bit(b) (1UL << (b)) typedef uint8 boolean; typedef uint8 byte; diff --git a/wirish/wirish_analog.c b/wirish/wirish_analog.c index 1b911bc..3c63342 100644 --- a/wirish/wirish_analog.c +++ b/wirish/wirish_analog.c @@ -1,4 +1,4 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 Perry Hung. @@ -20,22 +20,22 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - * ****************************************************************************/ + *****************************************************************************/ /** - * @brief + * @brief Arduino-compatible ADC implementation. */ #include "libmaple.h" #include "wirish.h" #include "io.h" -/* Assumes that the ADC has been initialized and - * that the pin is set to ANALOG_INPUT */ +/* Assumes that the ADC has been initialized and that the pin is set + * to ANALOG_INPUT */ uint32 analogRead(uint8 pin) { if(PIN_MAP[pin].adc == ADC_INVALID) { return 0; - } + } return adc_read(PIN_MAP[pin].adc); } diff --git a/wirish/wirish_digital.c b/wirish/wirish_digital.c index 9dd46ed..f4868da 100644 --- a/wirish/wirish_digital.c +++ b/wirish/wirish_digital.c @@ -1,4 +1,4 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 Perry Hung. @@ -20,10 +20,10 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - * ****************************************************************************/ + *****************************************************************************/ /** - * @brief + * @brief Arduino-compatible digital I/O implementation. */ #include "wirish.h" @@ -32,8 +32,9 @@ void pinMode(uint8 pin, WiringPinMode mode) { uint8 outputMode; - if (pin >= NR_GPIO_PINS) + if (pin >= NR_GPIO_PINS) { return; + } switch(mode) { case OUTPUT: @@ -71,14 +72,17 @@ void pinMode(uint8 pin, WiringPinMode mode) { uint32 digitalRead(uint8 pin) { - if (pin >= NR_GPIO_PINS) + if (pin >= NR_GPIO_PINS) { return 0; + } + return gpio_read_bit(PIN_MAP[pin].port, PIN_MAP[pin].pin); } void digitalWrite(uint8 pin, uint8 val) { - if (pin >= NR_GPIO_PINS) + if (pin >= NR_GPIO_PINS) { return; + } gpio_write_bit(PIN_MAP[pin].port, PIN_MAP[pin].pin, val); } diff --git a/wirish/wirish_math.cpp b/wirish/wirish_math.cpp index 0d907c4..12a21c3 100644 --- a/wirish/wirish_math.cpp +++ b/wirish/wirish_math.cpp @@ -1,54 +1,54 @@ /* - Modified by LeafLabs, LLC. - - Part of the Wiring project - http://wiring.org.co - Copyright (c) 2004-06 Hernando Barragan - Modified 13 August 2006, David A. Mellis for Arduino - http://www.arduino.cc/ - - 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., 59 Temple Place, Suite 330, - Boston, MA 02111-1307 USA - - $Id$ -*/ + * Modified by LeafLabs, LLC. + * + * Part of the Wiring project - http://wiring.org.co Copyright (c) + * 2004-06 Hernando Barragan Modified 13 August 2006, David A. Mellis + * for Arduino - http://www.arduino.cc/ + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ #include #include "math.h" void randomSeed(unsigned int seed) { - if (seed != 0) { - srand(seed); - } + if (seed != 0) { + srand(seed); + } } long random(long howbig) { - if (howbig == 0) { - return 0; - } - return rand() % howbig; + if (howbig == 0) { + return 0; + } + + return rand() % howbig; } long random(long howsmall, long howbig) { - if (howsmall >= howbig) { - return howsmall; - } - long diff = howbig - howsmall; - return random(diff) + howsmall; + if (howsmall >= howbig) { + return howsmall; + } + + long diff = howbig - howsmall; + return random(diff) + howsmall; } long map(long x, long in_min, long in_max, long out_min, long out_max) { - return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; + return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; } diff --git a/wirish/wirish_math.h b/wirish/wirish_math.h index 8746d01..4543c1b 100644 --- a/wirish/wirish_math.h +++ b/wirish/wirish_math.h @@ -1,4 +1,4 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 Perry Hung. @@ -20,7 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - * ****************************************************************************/ + *****************************************************************************/ #ifndef _WIRING_MATH_H_ #define _WIRING_MATH_H_ @@ -34,11 +34,11 @@ long random(long); long random(long, long); long map(long, long, long, long, long); -#define PI 3.1415926535897932384626433832795 -#define HALF_PI 1.5707963267948966192313216916398 -#define TWO_PI 6.283185307179586476925286766559 -#define DEG_TO_RAD 0.017453292519943295769236907684886 -#define RAD_TO_DEG 57.295779513082320876798154814105 +#define PI 3.1415926535897932384626433832795 +#define HALF_PI 1.5707963267948966192313216916398 +#define TWO_PI 6.283185307179586476925286766559 +#define DEG_TO_RAD 0.017453292519943295769236907684886 +#define RAD_TO_DEG 57.295779513082320876798154814105 #define min(a,b) ((a)<(b)?(a):(b)) #define max(a,b) ((a)>(b)?(a):(b)) @@ -52,7 +52,7 @@ long map(long, long, long, long, long); #ifdef abs #undef abs #endif -#define abs(x) (((x) > 0) ? (x) : -(unsigned)(x)) +#define abs(x) (((x) > 0) ? (x) : -(unsigned)(x)) #endif diff --git a/wirish/wirish_shift.c b/wirish/wirish_shift.c index 884b560..f67364d 100644 --- a/wirish/wirish_shift.c +++ b/wirish/wirish_shift.c @@ -1,38 +1,38 @@ /* - wiring_shift.c - shiftOut() function - Part of Arduino - http://www.arduino.cc/ - - Copyright (c) 2005-2006 David A. Mellis - - 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., 59 Temple Place, Suite 330, - Boston, MA 02111-1307 USA - - $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $ -*/ + * wiring_shift.c - shiftOut() function + * Part of Arduino - http://www.arduino.cc/ + * + * Copyright (c) 2005-2006 David A. Mellis + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + * + * $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $ + */ #include "wirish.h" -void shiftOut(uint8 dataPin, uint8 clockPin, uint8 bitOrder, uint8 val) -{ +void shiftOut(uint8 dataPin, uint8 clockPin, uint8 bitOrder, uint8 val) { int i; - for (i = 0; i < 8; i++) { - if (bitOrder == LSBFIRST) + for (i = 0; i < 8; i++) { + if (bitOrder == LSBFIRST) { digitalWrite(dataPin, !!(val & (1 << i))); - else + } else { digitalWrite(dataPin, !!(val & (1 << (7 - i)))); + } digitalWrite(clockPin, HIGH); digitalWrite(clockPin, LOW); -- cgit v1.2.3 From 994f1f1e36aad7351328822981421d16060ac688 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Mon, 27 Sep 2010 00:51:11 -0400 Subject: cleanups --- wirish/comm/HardwareSPI.cpp | 78 +++++++++++++++++++++--------------------- wirish/comm/HardwareSPI.h | 40 +++++++++++----------- wirish/comm/HardwareSerial.cpp | 28 +++++++-------- wirish/comm/HardwareSerial.h | 54 ++++++++++++++--------------- 4 files changed, 100 insertions(+), 100 deletions(-) (limited to 'wirish') diff --git a/wirish/comm/HardwareSPI.cpp b/wirish/comm/HardwareSPI.cpp index 5f42db7..5d04ddf 100644 --- a/wirish/comm/HardwareSPI.cpp +++ b/wirish/comm/HardwareSPI.cpp @@ -1,4 +1,4 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 Perry Hung. @@ -20,7 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - * ****************************************************************************/ + *****************************************************************************/ /** * @brief HardwareSPI "wiring-like" api for SPI @@ -52,14 +52,14 @@ #include "HardwareSPI.h" static const uint32 prescaleFactors[MAX_SPI_FREQS] = { - SPI_PRESCALE_2, // SPI_18MHZ - SPI_PRESCALE_4, // SPI_9MHZ - SPI_PRESCALE_8, // SPI_4_5MHZ - SPI_PRESCALE_16, // SPI_2_25MHZ - SPI_PRESCALE_32, // SPI_1_125MHZ - SPI_PRESCALE_64, // SPI_562_500KHZ - SPI_PRESCALE_128, // SPI_281_250KHZ - SPI_PRESCALE_256, // SPI_140_625KHZ + SPI_PRESCALE_2, // SPI_18MHZ + SPI_PRESCALE_4, // SPI_9MHZ + SPI_PRESCALE_8, // SPI_4_5MHZ + SPI_PRESCALE_16, // SPI_2_25MHZ + SPI_PRESCALE_32, // SPI_1_125MHZ + SPI_PRESCALE_64, // SPI_562_500KHZ + SPI_PRESCALE_128, // SPI_281_250KHZ + SPI_PRESCALE_256, // SPI_140_625KHZ }; /** @@ -78,37 +78,38 @@ static const uint32 prescaleFactors[MAX_SPI_FREQS] = { * @param mode SPI standard CPOL and CPHA levels */ void HardwareSPI::begin(SPIFrequency freq, uint32 endianness, uint32 mode) { - uint32 spi_num = this->spi_num; - uint32 prescale; - - if ((freq >= MAX_SPI_FREQS) || - !((endianness == LSBFIRST) || - (endianness == MSBFIRST)) || - (mode >= 4)) { - return; - } - - if (spi_num == 1) { - /* SPI1 is too fast for 140625 */ - if (freq == SPI_140_625KHZ) { - return; - } - - /* Turn off PWM on shared pins */ - timer_set_mode(TIMER3, 2, TIMER_DISABLED); - timer_set_mode(TIMER3, 1, TIMER_DISABLED); - } - - endianness = (endianness == LSBFIRST) ? SPI_LSBFIRST : SPI_MSBFIRST; - prescale = (spi_num == 1) ? prescaleFactors[freq + 1] : prescaleFactors[freq]; - - spi_init(spi_num, prescale, endianness, 0); + uint32 spi_num = this->spi_num; + uint32 prescale; + + if ((freq >= MAX_SPI_FREQS) || + !((endianness == LSBFIRST) || + (endianness == MSBFIRST)) || + (mode >= 4)) { + return; + } + + if (spi_num == 1) { + /* SPI1 is too fast for 140625 */ + if (freq == SPI_140_625KHZ) { + return; + } + + /* Turn off PWM on shared pins */ + timer_set_mode(TIMER3, 2, TIMER_DISABLED); + timer_set_mode(TIMER3, 1, TIMER_DISABLED); + } + + endianness = (endianness == LSBFIRST) ? SPI_LSBFIRST : SPI_MSBFIRST; + prescale = (spi_num == 1) ? + prescaleFactors[freq + 1] : + prescaleFactors[freq]; + + spi_init(spi_num, prescale, endianness, 0); } /** - * @brief Initialize a SPI peripheral with a default speed of 1.125 MHZ, MSBFIRST, - * mode 0 - * @param mode SPI standard CPOL and CPHA levels + * @brief Initialize a SPI peripheral with a default speed of 1.125 + * MHZ, MSBFIRST, mode 0 */ void HardwareSPI::begin(void) { begin(SPI_1_125MHZ, MSBFIRST, 0); @@ -126,7 +127,6 @@ uint8 HardwareSPI::send(uint8 *buf, uint32 len) { return spi_tx(this->spi_num, buf, len); } - /** * @brief read a byte from the spi peripheral * @return byte in the buffer diff --git a/wirish/comm/HardwareSPI.h b/wirish/comm/HardwareSPI.h index e606c0c..03d1ea1 100644 --- a/wirish/comm/HardwareSPI.h +++ b/wirish/comm/HardwareSPI.h @@ -1,4 +1,4 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 Perry Hung. @@ -20,7 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - * ****************************************************************************/ + *****************************************************************************/ /** * @brief HardwareSPI definitions @@ -30,28 +30,28 @@ #define _HARDWARESPI_H_ typedef enum SPIFrequency { - SPI_18MHZ = 0, - SPI_9MHZ = 1, - SPI_4_5MHZ = 2, - SPI_2_25MHZ = 3, - SPI_1_125MHZ = 4, - SPI_562_500KHZ = 5, - SPI_281_250KHZ = 6, - SPI_140_625KHZ = 7, - MAX_SPI_FREQS = 8, + SPI_18MHZ = 0, + SPI_9MHZ = 1, + SPI_4_5MHZ = 2, + SPI_2_25MHZ = 3, + SPI_1_125MHZ = 4, + SPI_562_500KHZ = 5, + SPI_281_250KHZ = 6, + SPI_140_625KHZ = 7, + MAX_SPI_FREQS = 8, } SPIFrequency; class HardwareSPI { - private: - uint32 spi_num; + private: + uint32 spi_num; - public: - HardwareSPI(uint32 spi_num); - void begin(void); - void begin(SPIFrequency freq, uint32 endianness, uint32 mode); - uint8 send(uint8 data); - uint8 send(uint8 *data, uint32 length); - uint8 recv(void); + public: + HardwareSPI(uint32 spi_num); + void begin(void); + void begin(SPIFrequency freq, uint32 endianness, uint32 mode); + uint8 send(uint8 data); + uint8 send(uint8 *data, uint32 length); + uint8 recv(void); }; #endif diff --git a/wirish/comm/HardwareSerial.cpp b/wirish/comm/HardwareSerial.cpp index 902b160..425c610 100644 --- a/wirish/comm/HardwareSerial.cpp +++ b/wirish/comm/HardwareSerial.cpp @@ -1,4 +1,4 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 Perry Hung. @@ -20,12 +20,12 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - * ****************************************************************************/ + *****************************************************************************/ /** - * @file HardwareSerial.cpp + * @file HardwareSerial.cpp * - * @brief Wiring-like serial api + * @brief Wiring-like serial api */ #include "wirish.h" @@ -68,18 +68,18 @@ void HardwareSerial::write(unsigned char ch) { } void HardwareSerial::begin(uint32 baud) { - if (baud > max_baud) { - return; - } + if (baud > max_baud) { + return; + } - gpio_set_mode(gpio_port, tx_pin, GPIO_MODE_AF_OUTPUT_PP); - gpio_set_mode(gpio_port, rx_pin, GPIO_MODE_INPUT_FLOATING); + gpio_set_mode(gpio_port, tx_pin, GPIO_MODE_AF_OUTPUT_PP); + gpio_set_mode(gpio_port, rx_pin, GPIO_MODE_INPUT_FLOATING); - if ((usart_num == USART1) || - (usart_num == USART2)) { - /* turn off any pwm if there's a conflict on this usart */ - timer_set_mode(timer_num, compare_num, TIMER_DISABLED); - } + if ((usart_num == USART1) || + (usart_num == USART2)) { + /* turn off any pwm if there's a conflict on this usart */ + timer_set_mode(timer_num, compare_num, TIMER_DISABLED); + } usart_init(usart_num, baud); } diff --git a/wirish/comm/HardwareSerial.h b/wirish/comm/HardwareSerial.h index df8d7bf..17be49f 100644 --- a/wirish/comm/HardwareSerial.h +++ b/wirish/comm/HardwareSerial.h @@ -1,4 +1,4 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 Perry Hung. @@ -20,12 +20,12 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - * ****************************************************************************/ + *****************************************************************************/ /** - * @file HardwareSerial.h + * @file HardwareSerial.h * - * @brief + * @brief Wirish interface to hardware serial communications. */ #ifndef _HARDWARESERIAL_H_ @@ -34,29 +34,29 @@ #include "Print.h" class HardwareSerial : public Print { - private: - uint8 usart_num; - uint32 max_baud; - GPIO_Port *gpio_port; - uint8 tx_pin; - uint8 rx_pin; - uint8 timer_num; - uint8 compare_num; - public: - HardwareSerial(uint8 usart_num, - uint32 max_baud, - GPIO_Port *gpio_port, - uint8 tx_pin, - uint8 rx_pin, - uint8 timer_num, - uint8 compare_num); - void begin(uint32); - void end(void); - uint32 available(void); - uint8 read(void); - void flush(void); - virtual void write(unsigned char); - using Print::write; + private: + uint8 usart_num; + uint32 max_baud; + GPIO_Port *gpio_port; + uint8 tx_pin; + uint8 rx_pin; + uint8 timer_num; + uint8 compare_num; + public: + HardwareSerial(uint8 usart_num, + uint32 max_baud, + GPIO_Port *gpio_port, + uint8 tx_pin, + uint8 rx_pin, + uint8 timer_num, + uint8 compare_num); + void begin(uint32); + void end(void); + uint32 available(void); + uint8 read(void); + void flush(void); + virtual void write(unsigned char); + using Print::write; }; extern HardwareSerial Serial1; extern HardwareSerial Serial2; -- cgit v1.2.3 From adde11b099ff5dad176e410279d21feac39d2c7e Mon Sep 17 00:00:00 2001 From: Perry Hung Date: Tue, 28 Sep 2010 19:21:16 -0400 Subject: Fix HardwareSPI to pass SPI modes for CPOL and CPHA --- wirish/comm/HardwareSPI.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'wirish') diff --git a/wirish/comm/HardwareSPI.cpp b/wirish/comm/HardwareSPI.cpp index 5d04ddf..20090f5 100644 --- a/wirish/comm/HardwareSPI.cpp +++ b/wirish/comm/HardwareSPI.cpp @@ -104,7 +104,7 @@ void HardwareSPI::begin(SPIFrequency freq, uint32 endianness, uint32 mode) { prescaleFactors[freq + 1] : prescaleFactors[freq]; - spi_init(spi_num, prescale, endianness, 0); + spi_init(spi_num, prescale, endianness, mode); } /** -- cgit v1.2.3