From 794a79a151b03d78d17c66474f4a327d90320594 Mon Sep 17 00:00:00 2001 From: Roger Clark Date: Wed, 3 Dec 2014 21:32:00 +1100 Subject: [PATCH] Added defined for __STM32F1XX__ to board.txt in preparation for changes to OneWire library. Also added analogWrite function to replicate what the Arduino API does, and fix the issue with needing to call pinMode with type PWM. Fade example now works, as long as a PWM compatible pin is used e.g. PA0 --- STM32F1XX/boards.txt | 10 +++++----- STM32F1XX/cores/maple/pwm.cpp | 13 +++++++++++++ STM32F1XX/cores/maple/pwm.h | 21 ++++++++++++--------- STM32F1XX/cores/maple/wirish_types.h | 1 + 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/STM32F1XX/boards.txt b/STM32F1XX/boards.txt index e8ebdc4..b4fb747 100644 --- a/STM32F1XX/boards.txt +++ b/STM32F1XX/boards.txt @@ -17,7 +17,7 @@ maple.upload.auto_reset=true maple.build.mcu=cortex-m3 maple.build.f_cpu=72000000L maple.build.core=maple -maple.build.extra_flags=-DMCU_STM32F103RB -mthumb -DSTM32_MEDIUM_DENSITY -DBOOTLOADER_maple -march=armv7-m +maple.build.extra_flags=-DMCU_STM32F103RB -mthumb -DSTM32_MEDIUM_DENSITY -DBOOTLOADER_maple -march=armv7-m -D__STM32F1XX__ maple.build.ldscript=ld/flash.ld maple.build.variant=maple maple.build.variant_system_lib=libmaple.a @@ -43,7 +43,7 @@ mapleRAM.upload.auto_reset=true mapleRAM.build.mcu=cortex-m3 mapleRAM.build.f_cpu=72000000L mapleRAM.build.core=maple -mapleRAM.build.extra_flags=-DMCU_STM32F103RB -mthumb -MD -DSTM32_MEDIUM_DENSITY -DBOOTLOADER_maple -march=armv7-m +mapleRAM.build.extra_flags=-DMCU_STM32F103RB -mthumb -MD -DSTM32_MEDIUM_DENSITY -DBOOTLOADER_maple -march=armv7-m -D__STM32F1XX__ mapleRAM.build.ldscript=ld/ram.ld mapleRAM.build.variant=maple mapleRAM.build.variant_system_lib=libmaple.a @@ -70,7 +70,7 @@ maple_mini.upload.auto_reset=true maple_mini.build.mcu=cortex-m3 maple_mini.build.f_cpu=72000000L maple_mini.build.core=maple -maple_mini.build.extra_flags=-DMCU_STM32F103CB -mthumb -MD -DSTM32_MEDIUM_DENSITY -DBOOTLOADER_maple -march=armv7-m +maple_mini.build.extra_flags=-DMCU_STM32F103CB -mthumb -MD -DSTM32_MEDIUM_DENSITY -DBOOTLOADER_maple -march=armv7-m -D__STM32F1XX__ maple_mini.build.ldscript=ld/flash.ld maple_mini.build.variant=maple_mini maple_mini.build.variant_system_lib=libmaple.a @@ -96,7 +96,7 @@ mapleRAM.upload.auto_reset=true maple_miniRAM.build.mcu=cortex-m3 maple_miniRAM.build.f_cpu=72000000L maple_miniRAM.build.core=maple -mapleRAM.build.extra_flags=-DMCU_STM32F103CB -mthumb -MD -DSTM32_MEDIUM_DENSITY -DBOOTLOADER_maple -march=armv7-m +mapleRAM.build.extra_flags=-DMCU_STM32F103CB -mthumb -MD -DSTM32_MEDIUM_DENSITY -DBOOTLOADER_maple -march=armv7-m -D__STM32F1XX__ mapleRAM.build.ldscript=ld/ram.ld mapleRAM.build.variant=maple_mini mapleRAM.build.variant_system_lib=libmaple.a @@ -123,7 +123,7 @@ maple_STM32.upload.auto_reset=true maple_STM32.build.mcu=cortex-m3 maple_STM32.build.f_cpu=72000000L maple_STM32.build.core=maple -maple_STM32.build.extra_flags=-DMCU_STM32F103CB -mthumb -MD -DSTM32_MEDIUM_DENSITY -march=armv7-m +maple_STM32.build.extra_flags=-DMCU_STM32F103CB -mthumb -MD -DSTM32_MEDIUM_DENSITY -march=armv7-m -D__STM32F1XX__ maple_STM32.build.ldscript=ld/jtag.ld maple_STM32.build.variant=maple_mini maple_STM32.build.variant_system_lib=libmaple.a diff --git a/STM32F1XX/cores/maple/pwm.cpp b/STM32F1XX/cores/maple/pwm.cpp index cba1832..6977fd6 100644 --- a/STM32F1XX/cores/maple/pwm.cpp +++ b/STM32F1XX/cores/maple/pwm.cpp @@ -36,6 +36,7 @@ #include #include "boards.h" +#include "io.h" void pwmWrite(uint8 pin, uint16 duty_cycle) { if (pin >= BOARD_NR_GPIO_PINS) { @@ -46,3 +47,15 @@ void pwmWrite(uint8 pin, uint16 duty_cycle) { ASSERT(dev && cc_channel); timer_set_compare(dev, cc_channel, duty_cycle); } + +/* + * Roger Clark. Added new function to replicate more closely what the Arduino API does + * Note. This implementation is currently slower than it could be, + * because pinMode needs to be called to set the special (new) mode of PWM + * Some optimisation may be possible with pinMode or even in this function + */ +void analogWrite(uint8 pin, int duty_cycle8) +{ + pinMode(pin,PWM); + pwmWrite(pin,duty_cycle8 * 257);// 257 maps 255 to 65535 (i.e 255*257 = 65535) +} diff --git a/STM32F1XX/cores/maple/pwm.h b/STM32F1XX/cores/maple/pwm.h index 6631d42..7775102 100644 --- a/STM32F1XX/cores/maple/pwm.h +++ b/STM32F1XX/cores/maple/pwm.h @@ -34,13 +34,6 @@ #include -/** - * As a convenience, analogWrite is an alias of pwmWrite to ease - * porting Arduino code. However, period and duty will have to be - * recalibrated. - */ -#define analogWrite pwmWrite - /** * Set the PWM duty on the given pin. * @@ -48,9 +41,19 @@ * (based on the configured period). * * @param pin PWM output pin - * @param duty_cycle Duty cycle to set. + * @param duty_cycle Duty cycle to set. (Range is 0 to 65535) */ -void pwmWrite(uint8 pin, uint16 duty_cycle); +void pwmWrite(uint8 pin, uint16 duty_cycle16); +/** + * Roger Clark. 20140103 + * Added function to replicate the Arduino PWM functionality or range 0 to 255 + * User code is expected to determine and honor the maximum value + * (based on the configured period). + * + * @param pin PWM output pin + * @param duty_cycle Duty cycle to set. (Range is 0 to 255) + */ +void analogWrite(uint8 pin, int duty_cycle8); #endif diff --git a/STM32F1XX/cores/maple/wirish_types.h b/STM32F1XX/cores/maple/wirish_types.h index fce895e..9a60fb3 100644 --- a/STM32F1XX/cores/maple/wirish_types.h +++ b/STM32F1XX/cores/maple/wirish_types.h @@ -55,6 +55,7 @@ typedef struct stm32_pin_info { uint8 gpio_bit; /**< Pin's GPIO port bit. */ uint8 timer_channel; /**< Timer channel, or 0 if none. */ uint8 adc_channel; /**< Pin ADC channel, or ADCx if none. */ + uint8 pinMode; /**< mode specific by pinMode call (Roger Clark added to optimize compatibility with Arduino API*/ } stm32_pin_info; /**