From 4f84e43b91fc857719f1d479a835eb7f8c637fec Mon Sep 17 00:00:00 2001 From: polymurph Date: Sun, 17 Jul 2022 14:19:55 +0200 Subject: [PATCH] work on spi implementation --- ked/csl/interfaces/spi.h | 4 ++-- ked/csl/stm32f042/Src/CMakeLists.txt | 6 ++++++ ked/csl/stm32f042/Src/spi.c | 20 ++++++++++++++++++-- ked/csl/stm32f042/config.cmake | 1 + main.c | 3 ++- tmux_workbench.sh | 5 ++++- 6 files changed, 33 insertions(+), 6 deletions(-) diff --git a/ked/csl/interfaces/spi.h b/ked/csl/interfaces/spi.h index 36ce1e9..027f96a 100644 --- a/ked/csl/interfaces/spi.h +++ b/ked/csl/interfaces/spi.h @@ -25,13 +25,13 @@ extern "C" { /** This is the spi hardware channel class*/ -void spi_initMaster(spiCH_t spic_hw_ch); +void spi_initMaster(spiCH_t spi_hw_ch); /*! * @brief Transmits and receives on byte of data * @param spi_hw_ch SPI hardware channel - * @param tx_data 'tx_data' The pyte to be transmitted" + * @param tx_data 'tx_data' The byte to be transmitted" * @return The received data */ uint8_t spi_trx(spiCH_t spi_hw_ch, uint8_t tx_data); diff --git a/ked/csl/stm32f042/Src/CMakeLists.txt b/ked/csl/stm32f042/Src/CMakeLists.txt index b8d9059..2f99735 100644 --- a/ked/csl/stm32f042/Src/CMakeLists.txt +++ b/ked/csl/stm32f042/Src/CMakeLists.txt @@ -29,3 +29,9 @@ target_compile_definitions(stmTimer PRIVATE ${C_DEFS}) target_include_directories(stmTimer PUBLIC ${INTERFACES_DIR} ${CSL_INCLUDES}) add_library(sub::timer ALIAS stmTimer) +add_library(stmSPI spi.c) +target_compile_options(stmSPI PRIVATE ${C_FLAGS}) +target_compile_definitions(stmSPI PRIVATE ${C_DEFS}) +target_include_directories(stmSPI PUBLIC ${INTERFACES_DIR} ${CSL_INCLUDES}) +add_library(sub::spi ALIAS stmSPI) + diff --git a/ked/csl/stm32f042/Src/spi.c b/ked/csl/stm32f042/Src/spi.c index f272ac1..363f04f 100644 --- a/ked/csl/stm32f042/Src/spi.c +++ b/ked/csl/stm32f042/Src/spi.c @@ -4,7 +4,7 @@ // https://controllerstech.com/spi-using-registers-in-stm32/ -void spi_initMater(spiCH_t_spi_hw_ch) +void spi_initMater(spiCH_t spi_hw_ch) { RCC->APB2ENR |= (1<<12); // Enable SPI1 CLock @@ -25,8 +25,23 @@ void spi_initMater(spiCH_t_spi_hw_ch) SPI1->CR2 = 0; } -uint8_t spi_trx(spiCH_t_spi_hw_ch, uint8_t tx_data) +uint8_t spi_trx(spiCH_t spi_hw_ch, uint8_t tx_data) { + uint8_t data; + // example + + + while (((SPI1->SR)&(1<<7))); // wait for BSY bit to Reset -> This will indicate that SPI is not busy in communication + + SPI1->DR = tx_data; // send data + + while (!((SPI1->SR) &(1<<0))); // Wait for RXNE to set -> This will indicate that the Rx buffer is not empty + data = SPI1->DR; + + return data; + +// implementation 2. step +#if 0 // wait for SPY ready while((SPI_BASE->SR) & (1 << 7)); @@ -37,4 +52,5 @@ uint8_t spi_trx(spiCH_t_spi_hw_ch, uint8_t tx_data) while(!((SPI_BASE->SR) & (1<<0)); return SPI_BASE->DR; +#endif } diff --git a/ked/csl/stm32f042/config.cmake b/ked/csl/stm32f042/config.cmake index 65a2c36..b5d895b 100644 --- a/ked/csl/stm32f042/config.cmake +++ b/ked/csl/stm32f042/config.cmake @@ -77,3 +77,4 @@ list(APPEND EXTRA_LIBS sub::pin) list(APPEND EXTRA_LIBS sub::usart) list(APPEND EXTRA_LIBS sub::timer) list(APPEND EXTRA_LIBS sub::init) +list(APPEND EXTRA_LIBS sub::spi) diff --git a/main.c b/main.c index 30688c3..1a6bac8 100644 --- a/main.c +++ b/main.c @@ -77,10 +77,11 @@ int main(int argc, char *argv[]) pinWrite(pinB3,0); spi_initMaster(SPI_CH_1); - while(1) { + for(i = 10; i > 0; i--) { spi_trx(SPI_CH_1, 0xAE); } + pinWrite(pinB3,1); //timer_capture_compare_test(timer_2); //print_Usart(usart2, "All is working fine \r\n"); diff --git a/tmux_workbench.sh b/tmux_workbench.sh index c9e0773..de3dade 100755 --- a/tmux_workbench.sh +++ b/tmux_workbench.sh @@ -6,7 +6,7 @@ echo $dir tmux new -s 'git' -d 'fish' tmux new -s 'compile' -d 'fish' -tmux new -s 'main' -d 'vim main.cpp' +tmux new -s 'main' -d 'fish' cd $dir/ked/csl/stm32f042/Src/ tmux new -s 'src' -d 'fish' @@ -17,4 +17,7 @@ tmux new -s 'device' -d 'fish' cd $dir/ked/csl/interfaces/ tmux new -s 'interface' -d 'fish' +cd $dir/ked/ +tmux new -s 'KED submodule' -d 'fish' + tmux a