diff --git a/ked/csl/interfaces/spi.h b/ked/csl/interfaces/spi.h index 6c06638..3236ebe 100644 --- a/ked/csl/interfaces/spi.h +++ b/ked/csl/interfaces/spi.h @@ -42,6 +42,8 @@ typedef enum{ typedef struct{ pinNo_t pin; /*!< pin number */ spiCH_t spi; /*!< spi hardware channel number */ + spi_framef_t format; /*!< spi format */ + spi_clkPol_t clkPol; /*!< spi clock polarity*/ }spi_ch_t; typedef uint8_t (*readReg_t) (uint8_t); @@ -109,6 +111,55 @@ void spiWriteBlock(spi_ch_t *spi_ch, const uint8_t *data, uint8_t data_len); + +/** + * \brief write 8 bits + * \param bits 8 bits + */ +void spiWrite8bit(spi_ch_t *spi_ch, + uint8_t bits); + +/** + * \brief read and write simultainously 8 bits + * \param bits 8 bits + * \return 8 bits + */ +uint8_t spiReadWrite8bit(spi_ch_t *spi_ch, + uint8_t bits); + +/** + * \brief write 16 bits + * \param bits 16 bits + */ +void spiWrite16bit(spi_ch_t *spi_ch, + uint16_t bits); + +/** + * \brief read and write simultainously 16 bits + * \param bits 16 bits + * \return 16 bits + */ +uint16_t spiReadWrite16bit(spi_ch_t *spi_ch, + uint16_t bits); + +/** + * \brief write 32 bits + * \param bits 32 bits + */ +void spiWrite32bit(spi_ch_t *spi_ch, + uint32_t bits); + +/** + * \brief read and write simultainously 32 bits + * \param bits 32 bits + * \return 32 bits + */ +uint32_t spiReadWrite32bit(spi_ch_t *spi_ch, + uint8_t bits); + + + + //implementation /** diff --git a/ked/csl/stm32f042/Src/imp_spi.c b/ked/csl/stm32f042/Src/imp_spi.c index 5541741..b0960ba 100644 --- a/ked/csl/stm32f042/Src/imp_spi.c +++ b/ked/csl/stm32f042/Src/imp_spi.c @@ -51,6 +51,7 @@ void spiReset(spiCH_t spi_hw_ch) void spiEnable(spiCH_t spi_hw_ch) { //TODO void spi_enable(spiCH_t spi_hw_ch); + SPI_BASE->CR1 |= SPI_CR1_SPE; } void spiSetPolarity(spiCH_t spi_hw_ch, spi_clkPol_t clkPol) diff --git a/ked/drivers/spi.c b/ked/drivers/spi.c index 3030aa6..96ba4b9 100644 --- a/ked/drivers/spi.c +++ b/ked/drivers/spi.c @@ -86,3 +86,95 @@ void spiWriteBlock(spi_ch_t *spi_ch, pinWrite(spi_ch->pin,1); } +void spiWrite8bit(spi_ch_t *spi_ch, + uint8_t bits) +{ + pinWrite(spi_ch->pin,0); + spiTrx(spi_ch->spi,bits); + pinWrite(spi_ch->pin,1); +} + +uint8_t spiReadWrite8bit(spi_ch_t *spi_ch, + uint8_t bits) +{ + uint8_t buf; + pinWrite(spi_ch->pin,0); + buf = spiTrx(spi_ch->spi,bits); + pinWrite(spi_ch->pin,1); + return buf; +} + +void spiWrite16bit(spi_ch_t *spi_ch, + uint16_t bits) +{ + pinWrite(spi_ch->pin,0); + if(spi_ch->format == LSB_FIRST) { + spiTrx(spi_ch->spi,(uint8_t)(bits >> 8)); + spiTrx(spi_ch->spi,(uint8_t)(bits)); + } else { + spiTrx(spi_ch->spi,(uint8_t)(bits)); + spiTrx(spi_ch->spi,(uint8_t)(bits >> 8)); + } + pinWrite(spi_ch->pin,1); +} + +uint16_t spiReadWrite16bit(spi_ch_t *spi_ch, + uint16_t bits) +{ + uint16_t buf; + + pinWrite(spi_ch->pin,0); + if(spi_ch->format == LSB_FIRST) { + buf = spiTrx(spi_ch->spi,(uint8_t)(bits >> 8)) << 8; + buf |= spiTrx(spi_ch->spi,(uint8_t)(bits)); + } else { + buf = spiTrx(spi_ch->spi,(uint8_t)(bits)); + buf |= spiTrx(spi_ch->spi,(uint8_t)(bits >> 8)) << 8; + } + pinWrite(spi_ch->pin,1); + + return buf; +} + +void spiWrite32bit(spi_ch_t *spi_ch, + uint32_t bits) +{ + pinWrite(spi_ch->pin,0); + if(spi_ch->format == LSB_FIRST) { + spiTrx(spi_ch->spi,(uint8_t)(bits >> 32)); + spiTrx(spi_ch->spi,(uint8_t)(bits >> 16)); + spiTrx(spi_ch->spi,(uint8_t)(bits >> 8)); + spiTrx(spi_ch->spi,(uint8_t)(bits)); + } else { + spiTrx(spi_ch->spi,(uint8_t)(bits)); + spiTrx(spi_ch->spi,(uint8_t)(bits >> 8)); + spiTrx(spi_ch->spi,(uint8_t)(bits >> 16)); + spiTrx(spi_ch->spi,(uint8_t)(bits >> 32)); + } + pinWrite(spi_ch->pin,1); +} + +uint32_t spiReadWrite32bit(spi_ch_t *spi_ch, + uint8_t bits) +{ + uint32_t buf; + + pinWrite(spi_ch->pin,0); + if(spi_ch->format == LSB_FIRST) { + buf = spiTrx(spi_ch->spi,(uint8_t)(bits >> 24)) << 24; + buf |= spiTrx(spi_ch->spi,(uint8_t)(bits >> 16)) << 16; + buf |= spiTrx(spi_ch->spi,(uint8_t)(bits >> 8)) << 8; + buf |= spiTrx(spi_ch->spi,(uint8_t)(bits)); + } else { + buf = spiTrx(spi_ch->spi,(uint8_t)(bits)); + buf |= spiTrx(spi_ch->spi,(uint8_t)(bits >> 8)) >> 8; + buf |= spiTrx(spi_ch->spi,(uint8_t)(bits >> 16)) >> 16; + buf |= spiTrx(spi_ch->spi,(uint8_t)(bits >> 24)) >> 24; + } + pinWrite(spi_ch->pin,1); + + return buf; +} + + + diff --git a/main.c b/main.c index 205a637..c0501e6 100644 --- a/main.c +++ b/main.c @@ -62,25 +62,34 @@ int main(int argc, char *argv[]) pinSetAlternate(pinA7, 0); // SPI1_MOSI spiInit(SPI_CH_1); + + spiSetupCH(&spi_test_channel, SPI_CH_1, pinA4); + spi_test_channel.format = MSB_FIRST; + + + spiEnable(SPI_CH_1); for(i = 0 ; i < 20 ; i++) { delayMs(50); pinToggle(pinB3); delayMs(50); } - for(i = 10; i > 0; i--) { - - spiWriteReg(&spi_test_channel,0xAE,0xAE); - //spi_trx(SPI_CH_1, 0xAE); + for(i = 100; i > 0; i--) { + //spiWriteReg(&spi_test_channel,0xAE,0xAE); + spiTrx(SPI_CH_1, 0xAE); } + spiWrite16bit(&spi_test_channel, 0x00F0); + for(i = 0 ; i < 100 ; i++) { pinWrite(pinB3, 1); + //pinWrite(pinA4,1); delayMs(100); pinWrite(pinB3, 0); + //pinWrite(pinA4,0); delayMs(900); }