From 32e43fdb02ddb7582866c29dad5e6c87f3315605 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Sun, 21 Nov 2010 13:45:22 +0000 Subject: Fixed bug 3114481. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@2409 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- demos/ARMCM3-STM32F100-DISCOVERY-GCC/halconf.h | 4 +- demos/ARMCM3-STM32F100-DISCOVERY-GCC/main.c | 113 +++++++++++++++++++++++-- demos/ARMCM3-STM32F100-DISCOVERY-GCC/mcuconf.h | 4 +- 3 files changed, 109 insertions(+), 12 deletions(-) (limited to 'demos') diff --git a/demos/ARMCM3-STM32F100-DISCOVERY-GCC/halconf.h b/demos/ARMCM3-STM32F100-DISCOVERY-GCC/halconf.h index 890f3896b..02a280269 100644 --- a/demos/ARMCM3-STM32F100-DISCOVERY-GCC/halconf.h +++ b/demos/ARMCM3-STM32F100-DISCOVERY-GCC/halconf.h @@ -44,7 +44,7 @@ * @brief Enables the ADC subsystem. */ #if !defined(HAL_USE_ADC) || defined(__DOXYGEN__) -#define HAL_USE_ADC FALSE +#define HAL_USE_ADC TRUE #endif /** @@ -79,7 +79,7 @@ * @brief Enables the PWM subsystem. */ #if !defined(HAL_USE_PWM) || defined(__DOXYGEN__) -#define HAL_USE_PWM FALSE +#define HAL_USE_PWM TRUE #endif /** diff --git a/demos/ARMCM3-STM32F100-DISCOVERY-GCC/main.c b/demos/ARMCM3-STM32F100-DISCOVERY-GCC/main.c index 05f0712e4..aea56bab2 100644 --- a/demos/ARMCM3-STM32F100-DISCOVERY-GCC/main.c +++ b/demos/ARMCM3-STM32F100-DISCOVERY-GCC/main.c @@ -21,21 +21,101 @@ #include "hal.h" #include "test.h" +static void pwmpcb(PWMDriver *pwmp); +static void adccb(ADCDriver *adcp, adcsample_t *buffer, size_t n); + +#define ADC_GRP1_NUM_CHANNELS 2 +#define ADC_GRP1_BUF_DEPTH 4 + +/* + * ADC samples buffer. + */ +static adcsample_t samples[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH] = +{ + 2048, 0, + 2048, 0, + 2048, 0, + 2048, 0 +}; + +/* + * ADC conversion group. + * Mode: Linear buffer, 4 samples of 2 channels, SW triggered. + * Channels: IN10, Sensor. + */ +static const ADCConversionGroup adcgrpcfg = { + FALSE, + ADC_GRP1_NUM_CHANNELS, + adccb, + 0, + ADC_CR2_EXTSEL_SWSTART | ADC_CR2_TSVREFE, + 0, + 0, + ADC_SQR1_NUM_CH(ADC_GRP1_NUM_CHANNELS), + 0, + ADC_SQR3_SQ1_N(ADC_CHANNEL_SENSOR) | ADC_SQR3_SQ0_N(ADC_CHANNEL_IN10) +}; + +/* + * ADC configuration structure, empty for STM32, there is nothing to configure. + */ +static const ADCConfig adccfg = { +}; + +/* + * PWM configuration structure. + */ +static PWMConfig pwmcfg = { + pwmpcb, + { + {PWM_OUTPUT_DISABLED, NULL}, + {PWM_OUTPUT_DISABLED, NULL}, + {PWM_OUTPUT_ACTIVE_HIGH, NULL}, + {PWM_OUTPUT_ACTIVE_HIGH, NULL} + }, + PWM_COMPUTE_PSC(STM32_TIMCLK1, 10000), /* 10KHz PWM clock frequency. */ + PWM_COMPUTE_ARR(10000, 1000000000), /* PWM period 1S (in nS). */ + 0 +}; + +/* + * PWM cyclic callback. PWM channels are reprogrammed using a duty cycle + * calculated as average of the last sampling operations. + */ +static void pwmpcb(PWMDriver *pwmp) { + adcsample_t avg_ch1, avg_ch2; + + /* Calculates the average values from the previous ADC sampling + operation.*/ + avg_ch1 = (samples[0] + samples[2] + samples[4] + samples[6]) / 4; + avg_ch2 = (samples[1] + samples[3] + samples[5] + samples[7]) / 4; + chSysLockFromIsr(); + pwmEnableChannelI(pwmp, 2, PWM_FRACTION_TO_WIDTH(pwmp, 4096, avg_ch1)); + pwmEnableChannelI(pwmp, 3, PWM_FRACTION_TO_WIDTH(pwmp, 4096, avg_ch2)); + + /* Starts an asynchronous ADC conversion operation, the conversion + will be executed in parallel to the current PWM cycle and will + terminate before the next PWM cycle.*/ +// adcStartConversionI(&ADCD1, &adcgrpcfg, samples, ADC_GRP1_BUF_DEPTH); + chSysUnlockFromIsr(); +} + /* - * Red LEDs blinker thread, times are in milliseconds. + * ADC end conversion callback. + */ +void adccb(ADCDriver *adcp, adcsample_t *buffer, size_t n) { + + (void)adcp; (void) buffer; (void) n; +} + +/* + * Red and blue LEDs blinker thread, times are in milliseconds. */ static WORKING_AREA(waThread1, 128); static msg_t Thread1(void *arg) { (void)arg; while (TRUE) { - palSetPad(GPIOC, GPIOC_LED4); - chThdSleepMilliseconds(250); - palClearPad(GPIOC, GPIOC_LED4); - chThdSleepMilliseconds(250); - palSetPad(GPIOC, GPIOC_LED3); - chThdSleepMilliseconds(250); - palClearPad(GPIOC, GPIOC_LED3); chThdSleepMilliseconds(250); } return 0; @@ -55,6 +135,23 @@ int main(int argc, char **argv) { */ sdStart(&SD1, NULL); + /* + * Initializes the ADC driver 1. + */ + adcStart(&ADCD1, &adccfg); + + /* + * Initializes the PWM driver 1, re-routes the TIM3 outputs, programs the + * pins as alternate functions and finally enables channels with zero + * initial duty cycle. + * Note, the AFIO access routes the TIM3 output pins on the PC6...PC9 + * where the LEDs are connected. + */ + pwmStart(&PWMD3, &pwmcfg); + AFIO->MAPR |= AFIO_MAPR_TIM3_REMAP_0 | AFIO_MAPR_TIM3_REMAP_1; + palSetGroupMode(GPIOC, PAL_PORT_BIT(GPIOC_LED3) | PAL_PORT_BIT(GPIOC_LED4), + PAL_MODE_STM32_ALTERNATE_PUSHPULL); + /* * Creates the blinker thread. */ diff --git a/demos/ARMCM3-STM32F100-DISCOVERY-GCC/mcuconf.h b/demos/ARMCM3-STM32F100-DISCOVERY-GCC/mcuconf.h index d1ad23373..0ff6793f2 100644 --- a/demos/ARMCM3-STM32F100-DISCOVERY-GCC/mcuconf.h +++ b/demos/ARMCM3-STM32F100-DISCOVERY-GCC/mcuconf.h @@ -61,9 +61,9 @@ /* * PWM driver system settings. */ -#define STM32_PWM_USE_TIM1 TRUE +#define STM32_PWM_USE_TIM1 FALSE #define STM32_PWM_USE_TIM2 FALSE -#define STM32_PWM_USE_TIM3 FALSE +#define STM32_PWM_USE_TIM3 TRUE #define STM32_PWM_USE_TIM4 FALSE #define STM32_PWM_PWM1_IRQ_PRIORITY 7 #define STM32_PWM_PWM2_IRQ_PRIORITY 7 -- cgit v1.2.3