From c852dcb3c960198f49c5fdd8619a6d5d581d9136 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Thu, 25 Nov 2010 18:32:45 +0000 Subject: Improved ADC and SPI driver models. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@2426 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- demos/ARMCM3-STM32F100-DISCOVERY-GCC/main.c | 18 +++++++++--------- os/hal/include/adc.h | 16 ++++++---------- os/hal/include/spi.h | 19 ++++++++----------- os/hal/platforms/AT91SAM7/spi_lld.h | 3 --- os/hal/platforms/LPC11xx/spi_lld.h | 3 --- os/hal/platforms/LPC13xx/spi_lld.h | 3 --- os/hal/platforms/LPC214x/spi_lld.h | 3 --- os/hal/platforms/STM32/adc_lld.h | 3 --- os/hal/platforms/STM32/spi_lld.h | 3 --- os/hal/platforms/STM8S/spi_lld.h | 3 --- os/hal/src/spi.c | 8 ++++---- os/hal/templates/adc_lld.h | 3 --- os/hal/templates/spi_lld.h | 3 --- readme.txt | 2 ++ 14 files changed, 29 insertions(+), 61 deletions(-) diff --git a/demos/ARMCM3-STM32F100-DISCOVERY-GCC/main.c b/demos/ARMCM3-STM32F100-DISCOVERY-GCC/main.c index b1bc37c29..445872f00 100644 --- a/demos/ARMCM3-STM32F100-DISCOVERY-GCC/main.c +++ b/demos/ARMCM3-STM32F100-DISCOVERY-GCC/main.c @@ -187,17 +187,9 @@ int main(int argc, char **argv) { */ spiStart(&SPID1, &spicfg); - /* - * Initializes the ADC driver 1. - * The pin PC0 on the port GPIOC is programmed as analog input. - */ - adcStart(&ADCD1, &adccfg); - palSetGroupMode(GPIOC, PAL_PORT_BIT(0), PAL_MODE_INPUT_ANALOG); - /* * 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. + * pins as alternate functions. * Note, the AFIO access routes the TIM3 output pins on the PC6...PC9 * where the LEDs are connected. */ @@ -206,6 +198,14 @@ int main(int argc, char **argv) { palSetGroupMode(GPIOC, PAL_PORT_BIT(GPIOC_LED3) | PAL_PORT_BIT(GPIOC_LED4), PAL_MODE_STM32_ALTERNATE_PUSHPULL); + /* + * Initializes the ADC driver 1 and performs a conversion. + * The pin PC0 on the port GPIOC is programmed as analog input. + */ + adcStart(&ADCD1, &adccfg); + palSetGroupMode(GPIOC, PAL_PORT_BIT(0), PAL_MODE_INPUT_ANALOG); + adcConvert(&ADCD1, &adcgrpcfg, samples, ADC_GRP1_BUF_DEPTH); + /* * Creates the example thread. */ diff --git a/os/hal/include/adc.h b/os/hal/include/adc.h index 48f9d9ff6..0b55d0d15 100644 --- a/os/hal/include/adc.h +++ b/os/hal/include/adc.h @@ -122,22 +122,21 @@ typedef enum { * * @notapi */ -#define _adc_wakeup_i(adcp) { \ - chSysLockFromIsr(); \ - (adcp)->ad_grpp = NULL; \ +#define _adc_wakeup_isr(adcp) { \ if ((adcp)->ad_thread != NULL) { \ Thread *tp = (adcp)->ad_thread; \ (adcp)->ad_thread = NULL; \ + chSysLockFromIsr(); \ tp->p_u.rdymsg = RDY_OK; \ chSchReadyI(tp); \ + chSysUnlockFromIsr(); \ } \ - chSysUnlockFromIsr(); \ } #else /* !ADC_USE_WAIT */ #define _adc_reset_i(adcp) #define _adc_reset_s(adcp) -#define _adc_wakeup(adcp) +#define _adc_wakeup_isr(adcp) #endif /* !ADC_USE_WAIT */ /** @@ -192,11 +191,7 @@ typedef enum { else { \ /* End conversion.*/ \ adc_lld_stop_conversion(adcp); \ - if ((adcp)->ad_grpp->acg_endcb == NULL) { \ - (adcp)->ad_state = ADC_READY; \ - _adc_wakeup_i(adcp); \ - } \ - else { \ + if ((adcp)->ad_grpp->acg_endcb != NULL) { \ (adcp)->ad_state = ADC_COMPLETE; \ if ((adcp)->ad_depth > 1) { \ /* Invokes the callback passing the 2nd half of the buffer.*/ \ @@ -212,6 +207,7 @@ typedef enum { (adcp)->ad_state = ADC_READY; \ } \ (adcp)->ad_grpp = NULL; \ + _adc_wakeup_isr(adcp); \ } \ } diff --git a/os/hal/include/spi.h b/os/hal/include/spi.h index 93aba4980..a563b262c 100644 --- a/os/hal/include/spi.h +++ b/os/hal/include/spi.h @@ -214,7 +214,7 @@ typedef enum { * * @notapi */ -#define _spi_wait(spip) { \ +#define _spi_wait_s(spip) { \ chDbgAssert((spip)->spd_thread == NULL, \ "_spi_wait(), #1", "already waiting"); \ (spip)->spd_thread = chThdSelf(); \ @@ -228,18 +228,18 @@ typedef enum { * * @notapi */ -#define _spi_wakeup(spip) { \ - chSysLockFromIsr(); \ +#define _spi_wakeup_isr(spip) { \ if ((spip)->spd_thread != NULL) { \ Thread *tp = (spip)->spd_thread; \ (spip)->spd_thread = NULL; \ + chSysLockFromIsr(); \ chSchReadyI(tp); \ + chSysUnlockFromIsr(); \ } \ - chSysUnlockFromIsr(); \ } #else /* !SPI_USE_WAIT */ -#define _spi_wakeup(spip) -#define _spi_wait(spip) +#define _spi_wait_s(spip) +#define _spi_wakeup_isr(spip) #endif /* !SPI_USE_WAIT */ /** @@ -261,11 +261,8 @@ typedef enum { (spip)->spd_state = SPI_COMPLETE; \ (spip)->spd_config->spc_endcb(spip); \ if ((spip)->spd_state == SPI_COMPLETE) \ - (spip)->spd_state = SPI_READY; \ - } \ - else { \ - (spip)->spd_state = SPI_READY; \ - _spi_wakeup(spip); \ + (spip)->spd_state = SPI_READY; \ + _spi_wakeup_isr(spip); \ } \ } diff --git a/os/hal/platforms/AT91SAM7/spi_lld.h b/os/hal/platforms/AT91SAM7/spi_lld.h index 6243e66fa..3b0bfaaba 100644 --- a/os/hal/platforms/AT91SAM7/spi_lld.h +++ b/os/hal/platforms/AT91SAM7/spi_lld.h @@ -126,9 +126,6 @@ typedef void (*spicallback_t)(SPIDriver *spip); typedef struct { /** * @brief Operation complete callback or @p NULL. - * @note In order to use synchronous functions this field must be set to - * @p NULL, callbacks and synchronous operations are mutually - * exclusive. */ spicallback_t spc_endcb; /* End of the mandatory fields.*/ diff --git a/os/hal/platforms/LPC11xx/spi_lld.h b/os/hal/platforms/LPC11xx/spi_lld.h index c7af31697..64f87c679 100644 --- a/os/hal/platforms/LPC11xx/spi_lld.h +++ b/os/hal/platforms/LPC11xx/spi_lld.h @@ -227,9 +227,6 @@ typedef void (*spicallback_t)(SPIDriver *spip); typedef struct { /** * @brief Operation complete callback or @p NULL. - * @note In order to use synchronous functions this field must be set to - * @p NULL, callbacks and synchronous operations are mutually - * exclusive. */ spicallback_t spc_endcb; /* End of the mandatory fields.*/ diff --git a/os/hal/platforms/LPC13xx/spi_lld.h b/os/hal/platforms/LPC13xx/spi_lld.h index bf94a824e..63a233f18 100644 --- a/os/hal/platforms/LPC13xx/spi_lld.h +++ b/os/hal/platforms/LPC13xx/spi_lld.h @@ -227,9 +227,6 @@ typedef void (*spicallback_t)(SPIDriver *spip); typedef struct { /** * @brief Operation complete callback or @p NULL. - * @note In order to use synchronous functions this field must be set to - * @p NULL, callbacks and synchronous operations are mutually - * exclusive. */ spicallback_t spc_endcb; /* End of the mandatory fields.*/ diff --git a/os/hal/platforms/LPC214x/spi_lld.h b/os/hal/platforms/LPC214x/spi_lld.h index 5f5d45823..50226cf4a 100644 --- a/os/hal/platforms/LPC214x/spi_lld.h +++ b/os/hal/platforms/LPC214x/spi_lld.h @@ -98,9 +98,6 @@ typedef void (*spicallback_t)(SPIDriver *spip); typedef struct { /** * @brief Operation complete callback or @p NULL. - * @note In order to use synchronous functions this field must be set to - * @p NULL, callbacks and synchronous operations are mutually - * exclusive. */ spicallback_t spc_endcb; /* End of the mandatory fields.*/ diff --git a/os/hal/platforms/STM32/adc_lld.h b/os/hal/platforms/STM32/adc_lld.h index c6ba26c05..0850b62f4 100644 --- a/os/hal/platforms/STM32/adc_lld.h +++ b/os/hal/platforms/STM32/adc_lld.h @@ -161,9 +161,6 @@ typedef struct { adc_channels_num_t acg_num_channels; /** * @brief Callback function associated to the group or @p NULL. - * @note In order to use synchronous functions this field must be set to - * @p NULL, callbacks and synchronous operations are mutually - * exclusive. */ adccallback_t acg_endcb; /* End of the mandatory fields.*/ diff --git a/os/hal/platforms/STM32/spi_lld.h b/os/hal/platforms/STM32/spi_lld.h index d46035ec0..9e29c5d68 100644 --- a/os/hal/platforms/STM32/spi_lld.h +++ b/os/hal/platforms/STM32/spi_lld.h @@ -186,9 +186,6 @@ typedef void (*spicallback_t)(SPIDriver *spip); typedef struct { /** * @brief Operation complete callback or @p NULL. - * @note In order to use synchronous functions this field must be set to - * @p NULL, callbacks and synchronous operations are mutually - * exclusive. */ spicallback_t spc_endcb; /* End of the mandatory fields.*/ diff --git a/os/hal/platforms/STM8S/spi_lld.h b/os/hal/platforms/STM8S/spi_lld.h index e4a963d67..cbcd2fd62 100644 --- a/os/hal/platforms/STM8S/spi_lld.h +++ b/os/hal/platforms/STM8S/spi_lld.h @@ -86,9 +86,6 @@ typedef void (*spicallback_t)(SPIDriver *spip); typedef struct { /** * @brief Operation complete callback or @p NULL. - * @note In order to use synchronous functions this field must be set to - * @p NULL, callbacks and synchronous operations are mutually - * exclusive. */ spicallback_t spc_endcb; /* End of the mandatory fields.*/ diff --git a/os/hal/src/spi.c b/os/hal/src/spi.c index 4d9749962..d1102cc6e 100644 --- a/os/hal/src/spi.c +++ b/os/hal/src/spi.c @@ -295,7 +295,7 @@ void spiIgnore(SPIDriver *spip, size_t n) { chDbgAssert(spip->spd_config->spc_endcb == NULL, "spiIgnore(), #2", "has callback"); spiStartIgnoreI(spip, n); - _spi_wait(spip); + _spi_wait_s(spip); chSysUnlock(); } @@ -329,7 +329,7 @@ void spiExchange(SPIDriver *spip, size_t n, chDbgAssert(spip->spd_config->spc_endcb == NULL, "spiExchange(), #2", "has callback"); spiStartExchangeI(spip, n, txbuf, rxbuf); - _spi_wait(spip); + _spi_wait_s(spip); chSysUnlock(); } @@ -360,7 +360,7 @@ void spiSend(SPIDriver *spip, size_t n, const void *txbuf) { chDbgAssert(spip->spd_config->spc_endcb == NULL, "spiSend(), #2", "has callback"); spiStartSendI(spip, n, txbuf); - _spi_wait(spip); + _spi_wait_s(spip); chSysUnlock(); } @@ -391,7 +391,7 @@ void spiReceive(SPIDriver *spip, size_t n, void *rxbuf) { chDbgAssert(spip->spd_config->spc_endcb == NULL, "spiReceive(), #2", "has callback"); spiStartReceiveI(spip, n, rxbuf); - _spi_wait(spip); + _spi_wait_s(spip); chSysUnlock(); } #endif /* SPI_USE_WAIT */ diff --git a/os/hal/templates/adc_lld.h b/os/hal/templates/adc_lld.h index c85a1bd77..54d78fd44 100644 --- a/os/hal/templates/adc_lld.h +++ b/os/hal/templates/adc_lld.h @@ -93,9 +93,6 @@ typedef struct { adc_channels_num_t acg_num_channels; /** * @brief Callback function associated to the group or @p NULL. - * @note In order to use synchronous functions this field must be set to - * @p NULL, callbacks and synchronous operations are mutually - * exclusive. */ adccallback_t acg_endcb; /* End of the mandatory fields.*/ diff --git a/os/hal/templates/spi_lld.h b/os/hal/templates/spi_lld.h index 4e7d7b81b..e985aa727 100644 --- a/os/hal/templates/spi_lld.h +++ b/os/hal/templates/spi_lld.h @@ -67,9 +67,6 @@ typedef void (*spicallback_t)(SPIDriver *spip); typedef struct { /** * @brief Operation complete callback. - * @note In order to use synchronous functions this field must be set to - * @p NULL, callbacks and synchronous operations are mutually - * exclusive. */ spicallback_t spc_endcb; /* End of the mandatory fields.*/ diff --git a/readme.txt b/readme.txt index c708996d0..e44efdf49 100644 --- a/readme.txt +++ b/readme.txt @@ -78,6 +78,8 @@ - FIX: Fixed typo in board name (bug 3113574)(backported to 2.0.7). - FIX: Fixed defective event wait functions with timeout (bug 3113443) (backported to 2.0.7). +- NEW: More improvements to the ADC and SPI drivers, now synchronous + operations can also have callbacks, optimized ISR code paths. - NEW: Added to the STM32 ADC driver the macros for easy handling of the sampling time for each channel. - NEW: Greatly simplified the STM32 PWM driver implementation. -- cgit v1.2.3