Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
6 years, 5 months ago.
SPI SCLK frequency could not be changed on NUCLEO-F103RB
Hello, I tried to test SPI interface on NUCLEO-F103RB with mbed OS. And I faced strange behavior of the board something like the frequency of the SCLK pin was completely different from what I specify in the code, 100kHz. I confirmed that the SCLK frequency from the pin was 281kHz when the code was run on NUCLEO-F103RB.
I just specify the SCLK frequency other than 100kHz, like 1kHz, 1MHz and also default. but it did not work, NO effect on the frequency of the SCLK pin. that was stuck on 281kHz.
Does any one have ideas to solve this problem?
Thanks,
The code i tested on the board
#include "mbed.h" SPI device_1(D4, D5, D3); DigitalOut device_1_ss(PB_6); int main() { device_1_ss = 0; device_1.frequency(100000); device_1.format(16,1); device_1_ss = 1; device_1.write(0x1111); device_1_ss = 0; }
1 Answer
6 years, 5 months ago.
Hi
The file you are mentioning is very old. Please use latest version of mbed-dev library instead (mbed-src is deprecated).
Now the spi_frequency function calculates the prescaler:
void spi_frequency(spi_t *obj, int hz) { struct spi_s *spiobj = SPI_S(obj); int spi_hz = 0; uint8_t prescaler_rank = 0; uint8_t last_index = (sizeof(baudrate_prescaler_table)/sizeof(baudrate_prescaler_table[0])) - 1; SPI_HandleTypeDef *handle = &(spiobj->handle); /* Calculate the spi clock for prescaler_rank 0: SPI_BAUDRATEPRESCALER_2 */ spi_hz = spi_get_clock_freq(obj) / 2; /* Define pre-scaler in order to get highest available frequency below requested frequency */ while ((spi_hz > hz) && (prescaler_rank < last_index)) { spi_hz = spi_hz / 2; prescaler_rank++; } /* Use the best fit pre-scaler */ handle->Init.BaudRatePrescaler = baudrate_prescaler_table[prescaler_rank]; /* In case maximum pre-scaler still gives too high freq, raise an error */ if (spi_hz > hz) { DEBUG_PRINTF("WARNING: lowest SPI freq (%d) higher than requested (%d)\r\n", spi_hz, hz); } DEBUG_PRINTF("spi_frequency, request:%d, select:%d\r\n", hz, spi_hz); init_spi(obj); }
This is a comment to myself.
I found the lowest frequency of the SPIs on the F103RB board was 250 - - 281kHz.
https://os.mbed.com/users/mbed_official/code/mbed-src/file/a11c0372f0ba/targets/hal/TARGET_STM/TARGET_STM32F1/spi_api.c
targets-hal-TARGET_STM-TARGET_STM32F1-spi_api.c
Detailed frequency is depend on the MCO speed, but on this board, the MCO frequency is 8MHz which coming from STLINK part and internal clock frequency would be 72MHz. It clearly describes the situation of this SPI SCLK problem.
I will try to check out that the more higher frequency works.
thanks,
posted by Masa Miyamoto 13 Nov 2017