From 37fbb642ea7765b11c6cce5c5a973c2de4129a79 Mon Sep 17 00:00:00 2001 From: stevstrong Date: Mon, 26 Oct 2015 11:36:24 +0100 Subject: [PATCH 1/6] added part from SD fat lib which enables SPI 2 device write access taken from SdSpiSTM32F1.cpp - Victor's lib, and adapted to support device selection. To be checked with other libs which are using the SPI lib. --- STM32F1/libraries/SPI/src/SPI.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/STM32F1/libraries/SPI/src/SPI.cpp b/STM32F1/libraries/SPI/src/SPI.cpp index ebb0198..d79b66c 100644 --- a/STM32F1/libraries/SPI/src/SPI.cpp +++ b/STM32F1/libraries/SPI/src/SPI.cpp @@ -322,7 +322,11 @@ void SPIClass::write(uint16 data) { spi_tx_reg(_currentSetting->spi_d, data); // "2. Write the first data item to be transmitted into the SPI_DR register (this clears the TXE flag)." while (spi_is_tx_empty(_currentSetting->spi_d) == 0); // "5. Wait until TXE=1 ..." - while (spi_is_busy(_currentSetting->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI." + while (spi_is_busy(_currentSetting->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI." + // taken from SdSpiSTM32F1.cpp - Victor's lib, and adapted to support device selection + if (spi_is_rx_nonempty(_currentSetting->spi_d)) { + uint8_t b = spi_rx_reg(_currentSetting->spi_d); + } } //void SPIClass::write(uint8 byte) { From f87b4adda8665b58485851b178b40de73ce1ebe6 Mon Sep 17 00:00:00 2001 From: stevstrong Date: Mon, 26 Oct 2015 12:52:45 +0100 Subject: [PATCH 2/6] additional function updateSettings() to replace the call to begin(). removes unwanted glitches before getting SCK active caused by setting parameters after begin(). --- STM32F1/libraries/SPI/src/SPI.cpp | 39 ++++++++++++++++--------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/STM32F1/libraries/SPI/src/SPI.cpp b/STM32F1/libraries/SPI/src/SPI.cpp index d79b66c..e4321e2 100644 --- a/STM32F1/libraries/SPI/src/SPI.cpp +++ b/STM32F1/libraries/SPI/src/SPI.cpp @@ -141,16 +141,19 @@ SPIClass::SPIClass(uint32 spi_num) { /* * Set up/tear down */ - -void SPIClass::begin(void) { - - uint32 flags = ((_currentSetting->bitOrder == MSBFIRST ? SPI_FRAME_MSB : SPI_FRAME_LSB) | SPI_DFF_8_BIT | SPI_SW_SLAVE | SPI_SOFT_SS); - spi_init(_currentSetting->spi_d); - configure_gpios(_currentSetting->spi_d, 1); +void SPIClass::updateSettings(void) { + uint32 flags = ((_currentSetting->bitOrder == MSBFIRST ? SPI_FRAME_MSB : SPI_FRAME_LSB) | SPI_DFF_8_BIT | SPI_SW_SLAVE | SPI_SOFT_SS); #ifdef SPI_DEBUG Serial.print("spi_master_enable("); Serial.print(_currentSetting->clockDivider); Serial.print(","); Serial.print(_currentSetting->dataMode); Serial.print(","); Serial.print(flags); Serial.println(")"); #endif - spi_master_enable(_currentSetting->spi_d, (spi_baud_rate)_currentSetting->clockDivider, (spi_mode)_currentSetting->dataMode, flags); + spi_master_enable(_currentSetting->spi_d, (spi_baud_rate)_currentSetting->clockDivider, (spi_mode)_currentSetting->dataMode, flags); +} + +void SPIClass::begin(void) { + + spi_init(_currentSetting->spi_d); + configure_gpios(_currentSetting->spi_d, 1); + updateSettings(); } void SPIClass::beginSlave(void) { @@ -192,7 +195,7 @@ void SPIClass::setClockDivider(uint32_t clockDivider) Serial.print("Clock divider set to "); Serial.println(clockDivider); #endif _currentSetting->clockDivider = clockDivider; - this->begin(); + updateSettings(); } void SPIClass::setBitOrder(BitOrder bitOrder) @@ -201,7 +204,7 @@ void SPIClass::setBitOrder(BitOrder bitOrder) Serial.print("Bit order set to "); Serial.println(bitOrder); #endif _currentSetting->bitOrder = bitOrder; - this->begin(); + updateSettings(); } /* Victor Perez. Added to test changing datasize from 8 to 16 bit modes on the fly. @@ -250,8 +253,8 @@ If someone finds this is not the case or sees a logic error with this let me kno Serial.print("Data mode set to "); Serial.println(dataMode); #endif _currentSetting->dataMode = dataMode; - this->begin(); -} + updateSettings(); +} void SPIClass::beginTransaction(uint8_t pin, SPISettings settings) @@ -266,7 +269,6 @@ void SPIClass::beginTransaction(uint8_t pin, SPISettings settings) setDataMode(settings.dataMode); setClockDivider(determine_baud_rate(_currentSetting->spi_d, settings.clock)); begin(); - } void SPIClass::endTransaction(void) @@ -322,11 +324,7 @@ void SPIClass::write(uint16 data) { spi_tx_reg(_currentSetting->spi_d, data); // "2. Write the first data item to be transmitted into the SPI_DR register (this clears the TXE flag)." while (spi_is_tx_empty(_currentSetting->spi_d) == 0); // "5. Wait until TXE=1 ..." - while (spi_is_busy(_currentSetting->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI." - // taken from SdSpiSTM32F1.cpp - Victor's lib, and adapted to support device selection - if (spi_is_rx_nonempty(_currentSetting->spi_d)) { - uint8_t b = spi_rx_reg(_currentSetting->spi_d); - } + while (spi_is_busy(_currentSetting->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI." } //void SPIClass::write(uint8 byte) { @@ -350,13 +348,16 @@ void SPIClass::write(const uint8 *data, uint32 length) { } while (spi_is_tx_empty(_currentSetting->spi_d) == 0); // "4. After writing the last data item into the SPI_DR register, wait until TXE=1 ..." while (spi_is_busy(_currentSetting->spi_d) != 0); // "... then wait until BSY=0, this indicates that the transmission of the last data is complete." +- // taken from SdSpiSTM32F1.cpp - Victor's lib, and adapted to support device selection +- if (spi_is_rx_nonempty(_currentSetting->spi_d)) { +- uint8_t b = spi_rx_reg(_currentSetting->spi_d); +- } } uint8 SPIClass::transfer(uint8 byte) const { - uint8 b; spi_tx_reg(_currentSetting->spi_d, byte); // "2. Write the first data item to be transmitted into the SPI_DR register (this clears the TXE flag)." while (spi_is_rx_nonempty(_currentSetting->spi_d) == 0); // "4. Wait until RXNE=1 ..." - b = spi_rx_reg(_currentSetting->spi_d); // "... and read the last received data." + uint8 b = spi_rx_reg(_currentSetting->spi_d); // "... and read the last received data." while (spi_is_tx_empty(_currentSetting->spi_d) == 0); // "5. Wait until TXE=1 ..." while (spi_is_busy(_currentSetting->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI." return b; From 3ef91128700e12208ed6be44aacd25f6b9c942a4 Mon Sep 17 00:00:00 2001 From: stevstrong Date: Mon, 26 Oct 2015 12:58:22 +0100 Subject: [PATCH 3/6] additional function updateSettings() to replace the call to begin(). removes unwanted glitches before getting SCK active caused by setting parameters after begin(). --- STM32F1/libraries/SPI/src/SPI.h | 1 + 1 file changed, 1 insertion(+) diff --git a/STM32F1/libraries/SPI/src/SPI.h b/STM32F1/libraries/SPI/src/SPI.h index 9bfe4f4..7951a80 100644 --- a/STM32F1/libraries/SPI/src/SPI.h +++ b/STM32F1/libraries/SPI/src/SPI.h @@ -375,6 +375,7 @@ private: SPISettings _settings[BOARD_NR_SPI]; SPISettings *_currentSetting; + void updateSettings(void); /* spi_dev *spi_d; uint8_t _SSPin; From 42659ebbb00c96f9c1a58157962dfa180c5380e6 Mon Sep 17 00:00:00 2001 From: stevstrong Date: Fri, 30 Oct 2015 14:12:38 +0100 Subject: [PATCH 4/6] added defines of external event selections for regular and injected groups --- .../libmaple/stm32f1/include/series/adc.h | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/STM32F1/system/libmaple/stm32f1/include/series/adc.h b/STM32F1/system/libmaple/stm32f1/include/series/adc.h index 3e0d1de..2a8a8cc 100644 --- a/STM32F1/system/libmaple/stm32f1/include/series/adc.h +++ b/STM32F1/system/libmaple/stm32f1/include/series/adc.h @@ -86,9 +86,23 @@ extern const struct adc_dev *ADC3; #define ADC_CR2_RSTCAL (1U << ADC_CR2_RSTCAL_BIT) #define ADC_CR2_DMA (1U << ADC_CR2_DMA_BIT) #define ADC_CR2_ALIGN (1U << ADC_CR2_ALIGN_BIT) -#define ADC_CR2_JEXTSEL 0x7000 +#define ADC_CR2_JEXTSEL_TIM1_TRGO (0x0 << 12) +#define ADC_CR2_JEXTSEL_TIM1_CC4 (0x1 << 12) +#define ADC_CR2_JEXTSEL_TIM2_TRGO (0x2 << 12) +#define ADC_CR2_JEXTSEL_TIM2_CC1 (0x3 << 12) +#define ADC_CR2_JEXTSEL_TIM3_CC4 (0x4 << 12) +#define ADC_CR2_JEXTSEL_TIM4_TRGO (0x5 << 12) +#define ADC_CR2_JEXTSEL_EXTI15 (0x6 << 12) +#define ADC_CR2_JEXTSEL_JSWSTART (0x7 << 12) #define ADC_CR2_JEXTTRIG (1U << ADC_CR2_JEXTTRIG_BIT) -#define ADC_CR2_EXTSEL 0xE0000 +#define ADC_CR2_EXTSEL_TIM1_CC1 (0x0 << 17) +#define ADC_CR2_EXTSEL_TIM1_CC2 (0x1 << 17) +#define ADC_CR2_EXTSEL_TIM1_CC3 (0x2 << 17) +#define ADC_CR2_EXTSEL_TIM2_CC2 (0x3 << 17) +#define ADC_CR2_EXTSEL_TIM3_TRGO (0x4 << 17) +#define ADC_CR2_EXTSEL_TIM4_CC4 (0x5 << 17) +#define ADC_CR2_EXTSEL_EXTI11 (0x6 << 17) +#define ADC_CR2_EXTSEL_SWSTART (0x7 << 17) #define ADC_CR2_EXTTRIG (1U << ADC_CR2_EXTTRIG_BIT) #define ADC_CR2_JSWSTART (1U << ADC_CR2_JSWSTART_BIT) #define ADC_CR2_SWSTART (1U << ADC_CR2_SWSTART_BIT) From 796dc93ab912ecd50021eced43a8eb21bc476123 Mon Sep 17 00:00:00 2001 From: stevstrong Date: Tue, 3 Nov 2015 13:43:59 +0100 Subject: [PATCH 5/6] re-added previously deleted EXTSEL defines --- STM32F1/system/libmaple/stm32f1/include/series/adc.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/STM32F1/system/libmaple/stm32f1/include/series/adc.h b/STM32F1/system/libmaple/stm32f1/include/series/adc.h index 2a8a8cc..7e9916d 100644 --- a/STM32F1/system/libmaple/stm32f1/include/series/adc.h +++ b/STM32F1/system/libmaple/stm32f1/include/series/adc.h @@ -86,6 +86,7 @@ extern const struct adc_dev *ADC3; #define ADC_CR2_RSTCAL (1U << ADC_CR2_RSTCAL_BIT) #define ADC_CR2_DMA (1U << ADC_CR2_DMA_BIT) #define ADC_CR2_ALIGN (1U << ADC_CR2_ALIGN_BIT) +#define ADC_CR2_JEXTSEL 0x7000 #define ADC_CR2_JEXTSEL_TIM1_TRGO (0x0 << 12) #define ADC_CR2_JEXTSEL_TIM1_CC4 (0x1 << 12) #define ADC_CR2_JEXTSEL_TIM2_TRGO (0x2 << 12) @@ -95,6 +96,7 @@ extern const struct adc_dev *ADC3; #define ADC_CR2_JEXTSEL_EXTI15 (0x6 << 12) #define ADC_CR2_JEXTSEL_JSWSTART (0x7 << 12) #define ADC_CR2_JEXTTRIG (1U << ADC_CR2_JEXTTRIG_BIT) +#define ADC_CR2_EXTSEL 0xE0000 #define ADC_CR2_EXTSEL_TIM1_CC1 (0x0 << 17) #define ADC_CR2_EXTSEL_TIM1_CC2 (0x1 << 17) #define ADC_CR2_EXTSEL_TIM1_CC3 (0x2 << 17) From b81d7b73346bdc45b87e58afbabee71fb6933cfe Mon Sep 17 00:00:00 2001 From: stevstrong Date: Wed, 4 Nov 2015 08:08:21 +0100 Subject: [PATCH 6/6] removed unwanted dashes which caused compiling error --- STM32F1/libraries/SPI/src/SPI.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/STM32F1/libraries/SPI/src/SPI.cpp b/STM32F1/libraries/SPI/src/SPI.cpp index e4321e2..851a3b1 100644 --- a/STM32F1/libraries/SPI/src/SPI.cpp +++ b/STM32F1/libraries/SPI/src/SPI.cpp @@ -348,10 +348,10 @@ void SPIClass::write(const uint8 *data, uint32 length) { } while (spi_is_tx_empty(_currentSetting->spi_d) == 0); // "4. After writing the last data item into the SPI_DR register, wait until TXE=1 ..." while (spi_is_busy(_currentSetting->spi_d) != 0); // "... then wait until BSY=0, this indicates that the transmission of the last data is complete." -- // taken from SdSpiSTM32F1.cpp - Victor's lib, and adapted to support device selection -- if (spi_is_rx_nonempty(_currentSetting->spi_d)) { -- uint8_t b = spi_rx_reg(_currentSetting->spi_d); -- } + // taken from SdSpiSTM32F1.cpp - Victor's lib, and adapted to support device selection + if (spi_is_rx_nonempty(_currentSetting->spi_d)) { + uint8_t b = spi_rx_reg(_currentSetting->spi_d); + } } uint8 SPIClass::transfer(uint8 byte) const {