From d573d5f308f2df192be15c82890d04db13217987 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Sun, 21 Nov 2010 14:21:11 +0000 Subject: Improved STM32VL-Discovery demo. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@2410 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- demos/ARMCM3-STM32F100-DISCOVERY-GCC/halconf.h | 2 +- demos/ARMCM3-STM32F100-DISCOVERY-GCC/main.c | 74 ++++++++++++++++++++----- demos/ARMCM3-STM32F100-DISCOVERY-GCC/readme.txt | 8 ++- 3 files changed, 65 insertions(+), 19 deletions(-) (limited to 'demos/ARMCM3-STM32F100-DISCOVERY-GCC') diff --git a/demos/ARMCM3-STM32F100-DISCOVERY-GCC/halconf.h b/demos/ARMCM3-STM32F100-DISCOVERY-GCC/halconf.h index 02a280269..10c78a7ba 100644 --- a/demos/ARMCM3-STM32F100-DISCOVERY-GCC/halconf.h +++ b/demos/ARMCM3-STM32F100-DISCOVERY-GCC/halconf.h @@ -93,7 +93,7 @@ * @brief Enables the SPI subsystem. */ #if !defined(HAL_USE_SPI) || defined(__DOXYGEN__) -#define HAL_USE_SPI FALSE +#define HAL_USE_SPI TRUE #endif /** diff --git a/demos/ARMCM3-STM32F100-DISCOVERY-GCC/main.c b/demos/ARMCM3-STM32F100-DISCOVERY-GCC/main.c index aea56bab2..0dbcc8354 100644 --- a/demos/ARMCM3-STM32F100-DISCOVERY-GCC/main.c +++ b/demos/ARMCM3-STM32F100-DISCOVERY-GCC/main.c @@ -23,20 +23,18 @@ static void pwmpcb(PWMDriver *pwmp); static void adccb(ADCDriver *adcp, adcsample_t *buffer, size_t n); +static void spicb(SPIDriver *spip); +/* Total number of channels to be sampled by a single ADC operation.*/ #define ADC_GRP1_NUM_CHANNELS 2 + +/* Depth of the conversion buffer, channels are sampled four times each.*/ #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 -}; +static adcsample_t samples[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH]; /* * ADC conversion group. @@ -53,7 +51,7 @@ static const ADCConversionGroup adcgrpcfg = { 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_SQR3_SQ1_N(ADC_CHANNEL_IN10) | ADC_SQR3_SQ0_N(ADC_CHANNEL_SENSOR) }; /* @@ -63,7 +61,9 @@ static const ADCConfig adccfg = { }; /* - * PWM configuration structure. + * PWM configuration structure. + * Cyclic callback enabled, channels 3 and 4 enabled without callbacks, + * the active state is a logic one. */ static PWMConfig pwmcfg = { pwmpcb, @@ -78,6 +78,18 @@ static PWMConfig pwmcfg = { 0 }; +/* + * SPI configuration structure. + * Maximum speed (12MHz), CPHA=0, CPOL=0, 16bits frames, MSb transmitted first. + * The slave select line is the pin GPIOA_SPI1NSS on the port GPIOA. + */ +static const SPIConfig spicfg = { + spicb, + GPIOA, + GPIOA_SPI1NSS, + SPI_CR1_DFF +}; + /* * PWM cyclic callback. PWM channels are reprogrammed using a duty cycle * calculated as average of the last sampling operations. @@ -90,33 +102,60 @@ static void pwmpcb(PWMDriver *pwmp) { avg_ch1 = (samples[0] + samples[2] + samples[4] + samples[6]) / 4; avg_ch2 = (samples[1] + samples[3] + samples[5] + samples[7]) / 4; chSysLockFromIsr(); + + /* Changes the channels pulse width, the change will be effective + starting from the next cycle.*/ 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); + adcStartConversionI(&ADCD1, &adcgrpcfg, samples, ADC_GRP1_BUF_DEPTH); chSysUnlockFromIsr(); } /* - * ADC end conversion callback. + * ADC end conversion callback. + * The latest samples are transmitted into a single SPI transaction. */ void adccb(ADCDriver *adcp, adcsample_t *buffer, size_t n) { - (void)adcp; (void) buffer; (void) n; + (void) buffer; (void) n; + /* Note, only in the ADC_COMPLETE state because the ADC driver fires an + intermediate callback when the buffer is half full.*/ + if (adcp->ad_state == ADC_COMPLETE) { + /* SPI slave selection and transmission start.*/ + chSysLockFromIsr(); + spiSelectI(&SPID1); + spiStartSendI(&SPID1, ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH, samples); + chSysUnlockFromIsr(); + } +} + +/* + * SPI end transfer callback. + */ +static void spicb(SPIDriver *spip) { + + /* On transfer end just releases the slave select line.*/ + chSysLockFromIsr(); + spiUnselectI(spip); + chSysUnlockFromIsr(); } /* - * Red and blue LEDs blinker thread, times are in milliseconds. + * This is a periodic thread that does absolutely nothing except sleeping and + * increase a counter. */ static WORKING_AREA(waThread1, 128); static msg_t Thread1(void *arg) { + static uint32_t seconds_counter; (void)arg; while (TRUE) { - chThdSleepMilliseconds(250); + chThdSleepMilliseconds(1000); + seconds_counter++; } return 0; } @@ -135,6 +174,11 @@ int main(int argc, char **argv) { */ sdStart(&SD1, NULL); + /* + * Initializes the SPI driver 1. + */ + spiStart(&SPID1, &spicfg); + /* * Initializes the ADC driver 1. */ @@ -153,7 +197,7 @@ int main(int argc, char **argv) { PAL_MODE_STM32_ALTERNATE_PUSHPULL); /* - * Creates the blinker thread. + * Creates the example thread. */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); diff --git a/demos/ARMCM3-STM32F100-DISCOVERY-GCC/readme.txt b/demos/ARMCM3-STM32F100-DISCOVERY-GCC/readme.txt index f2f69f7c6..8423d1394 100644 --- a/demos/ARMCM3-STM32F100-DISCOVERY-GCC/readme.txt +++ b/demos/ARMCM3-STM32F100-DISCOVERY-GCC/readme.txt @@ -8,9 +8,11 @@ The demo runs on an ST STM32VL-Discovery board. ** The Demo ** -The demo flashes the board LEDs using a thread, by pressing the button located -on the board the test procedure is activated with output on the serial port -COM1 (USART1). +The demo shows how to use the ADC, PWM and SPI drivers using asynchronous +APIs. The ADC samples two channels and modulates the PWM using the sample +values. The sample data is also transmitted on the SPI port 1. +By pressing the button located on the board the test procedure is activated +with output on the serial port COM1 (USART1). ** Build Procedure ** -- cgit v1.2.3