mbed library sources
Fork of mbed-src by
Diff: targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/spi_api.c
- Revision:
- 402:09075a3b15e3
- Parent:
- 233:1bbc1451db33
- Child:
- 414:4ec4c5b614b0
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/spi_api.c Fri Nov 14 08:45:06 2014 +0000 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/spi_api.c Fri Nov 14 09:00:07 2014 +0000 @@ -64,7 +64,8 @@ {NC, NC, 0} }; -static void init_spi(spi_t *obj) { +static void init_spi(spi_t *obj) +{ SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); SPI_InitTypeDef SPI_InitStructure; @@ -84,7 +85,8 @@ SPI_Cmd(spi, ENABLE); } -void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) { +void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) +{ // Determine the SPI to use SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI); SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO); @@ -133,7 +135,8 @@ init_spi(obj); } -void spi_free(spi_t *obj) { +void spi_free(spi_t *obj) +{ // Reset SPI and disable clock if (obj->spi == SPI_1) { RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE); @@ -154,7 +157,8 @@ pin_function(obj->pin_ssel, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)); } -void spi_format(spi_t *obj, int bits, int mode, int slave) { +void spi_format(spi_t *obj, int bits, int mode, int slave) +{ // Save new values if (bits == 16) { obj->bits = SPI_DataSize_16b; @@ -192,7 +196,8 @@ init_spi(obj); } -void spi_frequency(spi_t *obj, int hz) { +void spi_frequency(spi_t *obj, int hz) +{ if (obj->spi == SPI_1) { // Values depend of PCLK2: 64 MHz if HSI is used, 72 MHz if HSE is used if (hz < 500000) { @@ -238,7 +243,8 @@ init_spi(obj); } -static inline int ssp_readable(spi_t *obj) { +static inline int ssp_readable(spi_t *obj) +{ int status; SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); // Check if data is received @@ -246,7 +252,8 @@ return status; } -static inline int ssp_writeable(spi_t *obj) { +static inline int ssp_writeable(spi_t *obj) +{ int status; SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); // Check if data is transmitted @@ -254,46 +261,54 @@ return status; } -static inline void ssp_write(spi_t *obj, int value) { +static inline void ssp_write(spi_t *obj, int value) +{ SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); while (!ssp_writeable(obj)); SPI_I2S_SendData(spi, (uint16_t)value); } -static inline int ssp_read(spi_t *obj) { +static inline int ssp_read(spi_t *obj) +{ SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); while (!ssp_readable(obj)); return (int)SPI_I2S_ReceiveData(spi); } -static inline int ssp_busy(spi_t *obj) { +static inline int ssp_busy(spi_t *obj) +{ int status; SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_BSY) != RESET) ? 1 : 0); return status; } -int spi_master_write(spi_t *obj, int value) { +int spi_master_write(spi_t *obj, int value) +{ ssp_write(obj, value); return ssp_read(obj); } -int spi_slave_receive(spi_t *obj) { +int spi_slave_receive(spi_t *obj) +{ return ((ssp_readable(obj) && !ssp_busy(obj)) ? 1 : 0); }; -int spi_slave_read(spi_t *obj) { +int spi_slave_read(spi_t *obj) +{ SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); return (int)SPI_I2S_ReceiveData(spi); } -void spi_slave_write(spi_t *obj, int value) { +void spi_slave_write(spi_t *obj, int value) +{ SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); while (!ssp_writeable(obj)); SPI_I2S_SendData(spi, (uint16_t)value); } -int spi_busy(spi_t *obj) { +int spi_busy(spi_t *obj) +{ return ssp_busy(obj); }