diff options
author | cn <[email protected]> | 2019-09-15 21:47:30 +0200 |
---|---|---|
committer | Ron Evans <[email protected]> | 2019-09-16 11:08:59 +0200 |
commit | 500ee8067d633c666562ccae57bd8a621f3b5f34 (patch) | |
tree | 0f8ac6e159ad4c96b31854a81b2c56e7567f5d07 | |
parent | 8cd2c7502e7b7c3d4bbe14407fd7adc6aa882a95 (diff) | |
download | tinygo-500ee8067d633c666562ccae57bd8a621f3b5f34.tar.gz tinygo-500ee8067d633c666562ccae57bd8a621f3b5f34.zip |
machine/stm32f103xx: fix SPI frequency selection
The SPI frequency is broken since https://github.com/tinygo-org/tinygo/commit/b8c326d710a88877413e550b06100d9b79373dcd
added I2C interface and changed the unrelated `PCLK2 = HCLK/4` initialization
to `PCLK2 = HCLK/1` (but I2C uses PCLK1 anyways).
This commit changes all baud rate prescalers to be /4 compared to before.
Note: it is not possible to find an equivalent for 125 KHz SPI speed,
it will be too fast (`f = 72 MHz / 256`)
-rw-r--r-- | src/machine/machine_stm32f103xx.go | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/src/machine/machine_stm32f103xx.go b/src/machine/machine_stm32f103xx.go index 40566a74d..552e540b6 100644 --- a/src/machine/machine_stm32f103xx.go +++ b/src/machine/machine_stm32f103xx.go @@ -202,24 +202,25 @@ func (spi SPI) Configure(config SPIConfig) { var conf uint32 - // set frequency + // set frequency dependent on PCLK2 prescaler (div 1) switch config.Frequency { case 125000: - conf |= stm32.SPI_BaudRatePrescaler_128 + // Note: impossible to achieve lower frequency with current PCLK2! + conf |= stm32.SPI_BaudRatePrescaler_256 case 250000: - conf |= stm32.SPI_BaudRatePrescaler_64 + conf |= stm32.SPI_BaudRatePrescaler_256 case 500000: - conf |= stm32.SPI_BaudRatePrescaler_32 + conf |= stm32.SPI_BaudRatePrescaler_128 case 1000000: - conf |= stm32.SPI_BaudRatePrescaler_16 + conf |= stm32.SPI_BaudRatePrescaler_64 case 2000000: - conf |= stm32.SPI_BaudRatePrescaler_8 + conf |= stm32.SPI_BaudRatePrescaler_32 case 4000000: - conf |= stm32.SPI_BaudRatePrescaler_4 + conf |= stm32.SPI_BaudRatePrescaler_16 case 8000000: - conf |= stm32.SPI_BaudRatePrescaler_2 + conf |= stm32.SPI_BaudRatePrescaler_8 default: - conf |= stm32.SPI_BaudRatePrescaler_128 + conf |= stm32.SPI_BaudRatePrescaler_256 } // set bit transfer order @@ -345,11 +346,11 @@ func (i2c I2C) Configure(config I2CConfig) { // Disable the selected I2C peripheral to configure i2c.Bus.CR1.ClearBits(stm32.I2C_CR1_PE) - // pclk1 clock speed is main frequency divided by PCK1 prescaler (div 2) + // pclk1 clock speed is main frequency divided by PCLK1 prescaler (div 2) pclk1 := uint32(CPU_FREQUENCY / 2) - // set freqency range to pclk1 clock speed in Mhz. - // aka setting the value 36 means to use 36MhZ clock. + // set freqency range to PCLK1 clock speed in MHz + // aka setting the value 36 means to use 36 MHz clock pclk1Mhz := pclk1 / 1000000 i2c.Bus.CR2.SetBits(pclk1Mhz) |