diff --git a/demos/ARMCM3-STM32F103-GCC/Makefile b/demos/ARMCM3-STM32F103-GCC/Makefile index 4919e567e..8f3e902ed 100644 --- a/demos/ARMCM3-STM32F103-GCC/Makefile +++ b/demos/ARMCM3-STM32F103-GCC/Makefile @@ -68,8 +68,10 @@ CSRC = ${PORTSRC} \ ${TESTSRC} \ ${CHIBIOS}/os/io/pal.c \ ${CHIBIOS}/os/io/serial.c \ + ${CHIBIOS}/os/io/spi.c \ ${CHIBIOS}/os/io/platforms/STM32/pal_lld.c \ ${CHIBIOS}/os/io/platforms/STM32/serial_lld.c \ + ${CHIBIOS}/os/io/platforms/STM32/spi_lld.c \ ${CHIBIOS}/os/various/evtimer.c \ board.c main.c diff --git a/demos/ARMCM3-STM32F103-GCC/board.h b/demos/ARMCM3-STM32F103-GCC/board.h index f9364b02b..ac936f2a1 100644 --- a/demos/ARMCM3-STM32F103-GCC/board.h +++ b/demos/ARMCM3-STM32F103-GCC/board.h @@ -24,13 +24,11 @@ * Tricks required to make the TRUE/FALSE declaration inside the library * compatible. */ -#ifndef __STM32F10x_H #undef FALSE #undef TRUE #include #define FALSE 0 #define TRUE (!FALSE) -#endif /* * Uncomment this if you want a 48MHz system clock, else it will be 72MHz. diff --git a/os/io/platforms/STM32/pal_lld.h b/os/io/platforms/STM32/pal_lld.h index 9c234cac3..479b5e856 100644 --- a/os/io/platforms/STM32/pal_lld.h +++ b/os/io/platforms/STM32/pal_lld.h @@ -27,17 +27,11 @@ #ifndef _PAL_LLD_H_ #define _PAL_LLD_H_ -/* - * Tricks required to make the TRUE/FALSE declaration inside the library - * compatible. - */ -#ifndef __STM32F10x_H #undef FALSE #undef TRUE #include #define FALSE 0 #define TRUE (!FALSE) -#endif /*===========================================================================*/ /* I/O Ports Types and constants. */ diff --git a/os/io/platforms/STM32/serial_lld.h b/os/io/platforms/STM32/serial_lld.h index ed80f8b5d..b5d32d605 100644 --- a/os/io/platforms/STM32/serial_lld.h +++ b/os/io/platforms/STM32/serial_lld.h @@ -27,17 +27,11 @@ #ifndef _SERIAL_LLD_H_ #define _SERIAL_LLD_H_ -/* - * Tricks required to make the TRUE/FALSE declaration inside the library - * compatible. - */ -#ifndef __STM32F10x_H #undef FALSE #undef TRUE #include #define FALSE 0 #define TRUE (!FALSE) -#endif /*===========================================================================*/ /* Driver pre-compile time settings. */ diff --git a/os/io/platforms/STM32/spi_lld.c b/os/io/platforms/STM32/spi_lld.c index 4ef99e1ed..5e8ef614e 100644 --- a/os/io/platforms/STM32/spi_lld.c +++ b/os/io/platforms/STM32/spi_lld.c @@ -27,11 +27,41 @@ #include #include +#include "nvic.h" +#include "board.h" + +#if USE_STM32_SPI1 || defined(__DOXYGEN__) +/** @brief SPI1 driver identifier.*/ +SPIDriver SPID1; +#endif + +#if USE_STM32_SPI2 || defined(__DOXYGEN__) +/** @brief SPI2 driver identifier.*/ +SPIDriver SPID2; +#endif + /** * @brief Low level SPI driver initialization. */ void spi_lld_init(void) { +#if USE_STM32_SPI1 + spiObjectInit(&SPID1); + SPID1.spd_spi = SPI1; + SPID1.spd_dmarx = DMA1_Channel2; + SPID1.spd_dmatx = DMA1_Channel3; + RCC->APB2ENR |= RCC_APB2ENR_SPI1EN; + GPIOA->CRH = (GPIOA->CRH & 0x000FFFFF) | 0xB4B00000; +#endif + +#if USE_STM32_SPI2 + spiObjectInit(&SPID2); + SPID2.spd_spi = SPI2; + SPID2.spd_dmarx = DMA1_Channel4; + SPID2.spd_dmatx = DMA1_Channel5; + RCC->APB1ENR |= RCC_APB1ENR_SPI2EN; + GPIOB->CRL = (GPIOB->CRL & 0x000FFFFF) | 0xB4B00000; +#endif } /** diff --git a/os/io/platforms/STM32/spi_lld.h b/os/io/platforms/STM32/spi_lld.h index 26a70d0a4..23193bc0d 100644 --- a/os/io/platforms/STM32/spi_lld.h +++ b/os/io/platforms/STM32/spi_lld.h @@ -27,19 +27,32 @@ #ifndef _SPI_LLD_H_ #define _SPI_LLD_H_ +#undef FALSE +#undef TRUE +#include +#define FALSE 0 +#define TRUE (!FALSE) + /*===========================================================================*/ /* Driver pre-compile time settings. */ /*===========================================================================*/ /** - * @brief Enables the mutual exclusion APIs on the SPI bus. + * @brief SPI1 driver enable switch. + * @details If set to @p TRUE the support for SPI is included. + * @note The default is @p TRUE. */ -#if !defined(SPI_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__) -#define SPI_USE_MUTUAL_EXCLUSION TRUE +#if !defined(USE_STM32_SPI1) || defined(__DOXYGEN__) +#define USE_STM32_SPI1 TRUE #endif -#if SPI_USE_MUTUAL_EXCLUSION && !CH_USE_MUTEXES && !CH_USE_SEMAPHORES -#error "SPI_USE_MUTUAL_EXCLUSION requires CH_USE_MUTEXES and/or CH_USE_SEMAPHORES" +/** + * @brief SPI1 driver enable switch. + * @details If set to @p TRUE the support for SPI is included. + * @note The default is @p TRUE. + */ +#if !defined(USE_STM32_SPI2) || defined(__DOXYGEN__) +#define USE_STM32_SPI2 TRUE #endif /*===========================================================================*/ @@ -50,9 +63,9 @@ * @brief Driver state machine possible states. */ typedef enum { - SPI_UNINIT = 0,//!< SPI_UNINIT - SPI_IDLE = 1, //!< SPI_IDLE - SPI_ACTIVE = 2 //!< SPI_ACTIVE + SPI_UNINIT = 0, + SPI_IDLE = 1, + SPI_ACTIVE = 2 } spistate_t; /** @@ -85,16 +98,28 @@ typedef struct { #endif #endif /* SPI_USE_MUTUAL_EXCLUSION */ /** - * Current configuration data. + * @brief Current configuration data. */ const SPIConfig *spd_config; /* End of the mandatory fields.*/ + SPI_TypeDef *spd_spi; + DMA_Channel_TypeDef *spd_dmarx; + DMA_Channel_TypeDef *spd_dmatx; } SPIDriver; /*===========================================================================*/ /* External declarations. */ /*===========================================================================*/ +/** @cond never*/ +#if USE_STM32_SPI1 +extern SPIDriver SPID1; +#endif + +#if USE_STM32_SPI2 +extern SPIDriver SPID2; +#endif + #ifdef __cplusplus extern "C" { #endif @@ -106,6 +131,7 @@ extern "C" { #ifdef __cplusplus } #endif +/** @endcond*/ #endif /* _SPI_LLD_H_ */ diff --git a/os/io/spi.h b/os/io/spi.h index bb4244944..54409fe76 100644 --- a/os/io/spi.h +++ b/os/io/spi.h @@ -27,6 +27,17 @@ #ifndef _SPI_H_ #define _SPI_H_ +/** + * @brief Enables the mutual exclusion APIs on the SPI bus. + */ +#if !defined(SPI_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__) +#define SPI_USE_MUTUAL_EXCLUSION TRUE +#endif + +#if SPI_USE_MUTUAL_EXCLUSION && !CH_USE_MUTEXES && !CH_USE_SEMAPHORES +#error "SPI_USE_MUTUAL_EXCLUSION requires CH_USE_MUTEXES and/or CH_USE_SEMAPHORES" +#endif + #include "spi_lld.h" #ifdef __cplusplus diff --git a/os/io/templates/spi_lld.h b/os/io/templates/spi_lld.h index 26a70d0a4..37f14781e 100644 --- a/os/io/templates/spi_lld.h +++ b/os/io/templates/spi_lld.h @@ -50,9 +50,9 @@ * @brief Driver state machine possible states. */ typedef enum { - SPI_UNINIT = 0,//!< SPI_UNINIT - SPI_IDLE = 1, //!< SPI_IDLE - SPI_ACTIVE = 2 //!< SPI_ACTIVE + SPI_UNINIT = 0, + SPI_IDLE = 1, + SPI_ACTIVE = 2 } spistate_t; /** @@ -85,7 +85,7 @@ typedef struct { #endif #endif /* SPI_USE_MUTUAL_EXCLUSION */ /** - * Current configuration data. + * @brief Current configuration data. */ const SPIConfig *spd_config; /* End of the mandatory fields.*/