Improved ADC and SPI driver models.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@2426 35acf78f-673a-0410-8e92-d51de3d6d3f4
This commit is contained in:
gdisirio 2010-11-25 18:32:45 +00:00
parent d973c3f25a
commit c852dcb3c9
14 changed files with 29 additions and 61 deletions

View File

@ -187,17 +187,9 @@ int main(int argc, char **argv) {
*/ */
spiStart(&SPID1, &spicfg); 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 * Initializes the PWM driver 1, re-routes the TIM3 outputs, programs the
* pins as alternate functions and finally enables channels with zero * pins as alternate functions.
* initial duty cycle.
* Note, the AFIO access routes the TIM3 output pins on the PC6...PC9 * Note, the AFIO access routes the TIM3 output pins on the PC6...PC9
* where the LEDs are connected. * 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), palSetGroupMode(GPIOC, PAL_PORT_BIT(GPIOC_LED3) | PAL_PORT_BIT(GPIOC_LED4),
PAL_MODE_STM32_ALTERNATE_PUSHPULL); 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. * Creates the example thread.
*/ */

View File

@ -122,22 +122,21 @@ typedef enum {
* *
* @notapi * @notapi
*/ */
#define _adc_wakeup_i(adcp) { \ #define _adc_wakeup_isr(adcp) { \
chSysLockFromIsr(); \
(adcp)->ad_grpp = NULL; \
if ((adcp)->ad_thread != NULL) { \ if ((adcp)->ad_thread != NULL) { \
Thread *tp = (adcp)->ad_thread; \ Thread *tp = (adcp)->ad_thread; \
(adcp)->ad_thread = NULL; \ (adcp)->ad_thread = NULL; \
chSysLockFromIsr(); \
tp->p_u.rdymsg = RDY_OK; \ tp->p_u.rdymsg = RDY_OK; \
chSchReadyI(tp); \ chSchReadyI(tp); \
chSysUnlockFromIsr(); \
} \ } \
chSysUnlockFromIsr(); \
} }
#else /* !ADC_USE_WAIT */ #else /* !ADC_USE_WAIT */
#define _adc_reset_i(adcp) #define _adc_reset_i(adcp)
#define _adc_reset_s(adcp) #define _adc_reset_s(adcp)
#define _adc_wakeup(adcp) #define _adc_wakeup_isr(adcp)
#endif /* !ADC_USE_WAIT */ #endif /* !ADC_USE_WAIT */
/** /**
@ -192,11 +191,7 @@ typedef enum {
else { \ else { \
/* End conversion.*/ \ /* End conversion.*/ \
adc_lld_stop_conversion(adcp); \ adc_lld_stop_conversion(adcp); \
if ((adcp)->ad_grpp->acg_endcb == NULL) { \ if ((adcp)->ad_grpp->acg_endcb != NULL) { \
(adcp)->ad_state = ADC_READY; \
_adc_wakeup_i(adcp); \
} \
else { \
(adcp)->ad_state = ADC_COMPLETE; \ (adcp)->ad_state = ADC_COMPLETE; \
if ((adcp)->ad_depth > 1) { \ if ((adcp)->ad_depth > 1) { \
/* Invokes the callback passing the 2nd half of the buffer.*/ \ /* Invokes the callback passing the 2nd half of the buffer.*/ \
@ -212,6 +207,7 @@ typedef enum {
(adcp)->ad_state = ADC_READY; \ (adcp)->ad_state = ADC_READY; \
} \ } \
(adcp)->ad_grpp = NULL; \ (adcp)->ad_grpp = NULL; \
_adc_wakeup_isr(adcp); \
} \ } \
} }

View File

@ -214,7 +214,7 @@ typedef enum {
* *
* @notapi * @notapi
*/ */
#define _spi_wait(spip) { \ #define _spi_wait_s(spip) { \
chDbgAssert((spip)->spd_thread == NULL, \ chDbgAssert((spip)->spd_thread == NULL, \
"_spi_wait(), #1", "already waiting"); \ "_spi_wait(), #1", "already waiting"); \
(spip)->spd_thread = chThdSelf(); \ (spip)->spd_thread = chThdSelf(); \
@ -228,18 +228,18 @@ typedef enum {
* *
* @notapi * @notapi
*/ */
#define _spi_wakeup(spip) { \ #define _spi_wakeup_isr(spip) { \
chSysLockFromIsr(); \
if ((spip)->spd_thread != NULL) { \ if ((spip)->spd_thread != NULL) { \
Thread *tp = (spip)->spd_thread; \ Thread *tp = (spip)->spd_thread; \
(spip)->spd_thread = NULL; \ (spip)->spd_thread = NULL; \
chSysLockFromIsr(); \
chSchReadyI(tp); \ chSchReadyI(tp); \
chSysUnlockFromIsr(); \
} \ } \
chSysUnlockFromIsr(); \
} }
#else /* !SPI_USE_WAIT */ #else /* !SPI_USE_WAIT */
#define _spi_wakeup(spip) #define _spi_wait_s(spip)
#define _spi_wait(spip) #define _spi_wakeup_isr(spip)
#endif /* !SPI_USE_WAIT */ #endif /* !SPI_USE_WAIT */
/** /**
@ -261,11 +261,8 @@ typedef enum {
(spip)->spd_state = SPI_COMPLETE; \ (spip)->spd_state = SPI_COMPLETE; \
(spip)->spd_config->spc_endcb(spip); \ (spip)->spd_config->spc_endcb(spip); \
if ((spip)->spd_state == SPI_COMPLETE) \ if ((spip)->spd_state == SPI_COMPLETE) \
(spip)->spd_state = SPI_READY; \ (spip)->spd_state = SPI_READY; \
} \ _spi_wakeup_isr(spip); \
else { \
(spip)->spd_state = SPI_READY; \
_spi_wakeup(spip); \
} \ } \
} }

View File

@ -126,9 +126,6 @@ typedef void (*spicallback_t)(SPIDriver *spip);
typedef struct { typedef struct {
/** /**
* @brief Operation complete callback or @p NULL. * @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; spicallback_t spc_endcb;
/* End of the mandatory fields.*/ /* End of the mandatory fields.*/

View File

@ -227,9 +227,6 @@ typedef void (*spicallback_t)(SPIDriver *spip);
typedef struct { typedef struct {
/** /**
* @brief Operation complete callback or @p NULL. * @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; spicallback_t spc_endcb;
/* End of the mandatory fields.*/ /* End of the mandatory fields.*/

View File

@ -227,9 +227,6 @@ typedef void (*spicallback_t)(SPIDriver *spip);
typedef struct { typedef struct {
/** /**
* @brief Operation complete callback or @p NULL. * @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; spicallback_t spc_endcb;
/* End of the mandatory fields.*/ /* End of the mandatory fields.*/

View File

@ -98,9 +98,6 @@ typedef void (*spicallback_t)(SPIDriver *spip);
typedef struct { typedef struct {
/** /**
* @brief Operation complete callback or @p NULL. * @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; spicallback_t spc_endcb;
/* End of the mandatory fields.*/ /* End of the mandatory fields.*/

View File

@ -161,9 +161,6 @@ typedef struct {
adc_channels_num_t acg_num_channels; adc_channels_num_t acg_num_channels;
/** /**
* @brief Callback function associated to the group or @p NULL. * @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; adccallback_t acg_endcb;
/* End of the mandatory fields.*/ /* End of the mandatory fields.*/

View File

@ -186,9 +186,6 @@ typedef void (*spicallback_t)(SPIDriver *spip);
typedef struct { typedef struct {
/** /**
* @brief Operation complete callback or @p NULL. * @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; spicallback_t spc_endcb;
/* End of the mandatory fields.*/ /* End of the mandatory fields.*/

View File

@ -86,9 +86,6 @@ typedef void (*spicallback_t)(SPIDriver *spip);
typedef struct { typedef struct {
/** /**
* @brief Operation complete callback or @p NULL. * @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; spicallback_t spc_endcb;
/* End of the mandatory fields.*/ /* End of the mandatory fields.*/

View File

@ -295,7 +295,7 @@ void spiIgnore(SPIDriver *spip, size_t n) {
chDbgAssert(spip->spd_config->spc_endcb == NULL, chDbgAssert(spip->spd_config->spc_endcb == NULL,
"spiIgnore(), #2", "has callback"); "spiIgnore(), #2", "has callback");
spiStartIgnoreI(spip, n); spiStartIgnoreI(spip, n);
_spi_wait(spip); _spi_wait_s(spip);
chSysUnlock(); chSysUnlock();
} }
@ -329,7 +329,7 @@ void spiExchange(SPIDriver *spip, size_t n,
chDbgAssert(spip->spd_config->spc_endcb == NULL, chDbgAssert(spip->spd_config->spc_endcb == NULL,
"spiExchange(), #2", "has callback"); "spiExchange(), #2", "has callback");
spiStartExchangeI(spip, n, txbuf, rxbuf); spiStartExchangeI(spip, n, txbuf, rxbuf);
_spi_wait(spip); _spi_wait_s(spip);
chSysUnlock(); chSysUnlock();
} }
@ -360,7 +360,7 @@ void spiSend(SPIDriver *spip, size_t n, const void *txbuf) {
chDbgAssert(spip->spd_config->spc_endcb == NULL, chDbgAssert(spip->spd_config->spc_endcb == NULL,
"spiSend(), #2", "has callback"); "spiSend(), #2", "has callback");
spiStartSendI(spip, n, txbuf); spiStartSendI(spip, n, txbuf);
_spi_wait(spip); _spi_wait_s(spip);
chSysUnlock(); chSysUnlock();
} }
@ -391,7 +391,7 @@ void spiReceive(SPIDriver *spip, size_t n, void *rxbuf) {
chDbgAssert(spip->spd_config->spc_endcb == NULL, chDbgAssert(spip->spd_config->spc_endcb == NULL,
"spiReceive(), #2", "has callback"); "spiReceive(), #2", "has callback");
spiStartReceiveI(spip, n, rxbuf); spiStartReceiveI(spip, n, rxbuf);
_spi_wait(spip); _spi_wait_s(spip);
chSysUnlock(); chSysUnlock();
} }
#endif /* SPI_USE_WAIT */ #endif /* SPI_USE_WAIT */

View File

@ -93,9 +93,6 @@ typedef struct {
adc_channels_num_t acg_num_channels; adc_channels_num_t acg_num_channels;
/** /**
* @brief Callback function associated to the group or @p NULL. * @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; adccallback_t acg_endcb;
/* End of the mandatory fields.*/ /* End of the mandatory fields.*/

View File

@ -67,9 +67,6 @@ typedef void (*spicallback_t)(SPIDriver *spip);
typedef struct { typedef struct {
/** /**
* @brief Operation complete callback. * @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; spicallback_t spc_endcb;
/* End of the mandatory fields.*/ /* End of the mandatory fields.*/

View File

@ -78,6 +78,8 @@
- FIX: Fixed typo in board name (bug 3113574)(backported to 2.0.7). - FIX: Fixed typo in board name (bug 3113574)(backported to 2.0.7).
- FIX: Fixed defective event wait functions with timeout (bug 3113443) - FIX: Fixed defective event wait functions with timeout (bug 3113443)
(backported to 2.0.7). (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 - NEW: Added to the STM32 ADC driver the macros for easy handling of the
sampling time for each channel. sampling time for each channel.
- NEW: Greatly simplified the STM32 PWM driver implementation. - NEW: Greatly simplified the STM32 PWM driver implementation.