STM32 clock adjustment - clock

I'm using STM32f103 micro controller for a while and today I just confused about clock source and PLL configuration!
I know the clock source is HSI by default when micro starts and startup_stm32f10x_xx.s runs, but I don't know if PLL sets or not!? how can I know whats my micro freq?
thank you

A call to RCC_GetClocksFreq() will tell you the clock frequencies (SYSCLK, HCLK, PCLK1, PCLK2, ADCCLK).

If you are using the CMSIS library for the STM32, it has functions to configure the clock and also functions to tell you at runtime what the clock is.
If you are not, you will have to look to see where the clock source is being set, and if it is the HSE you will need to know what crystal you have. Once you have that info, you can then look at the M, N, and P parameters of the PLL (if used) to calculate your HCLK. You should be able to find all this information in the reference manual for the STM32F103 in the RCC (reset and clock control) section.

Related

what is the best way to design a shift register with stm32

I am using a STM32F031K6, clocked at 40MHz, and I want to design a program which acts as a looping shift register - an external trigger is used to clock it, the values in the shift register left shift every time a rising/falling edge is received. the output is one pin either high or low.
I need to make the time between the clocking edge and the output less than 0.5uS, or failing that as quick as possible. The values of the shift register can be changed and the length can also be changed, but for now I'm just starting with a byte like 11000010 .
I initially thought to implement this with an external interrupt but it was suggested there may be a better way to implement it
any help much appreciated
You might use the SPI peripheral of the STM32F0 for your task. When configured in slave mode, each time an external clock edge is detected on the SCK signal, the MISO will be set to the next bit of a value loaded into an internal shift register via the SPI data register.
Check out the chapter on the Serial peripheral interface (SPI) in STM32F0 reference manual.
Especially have a look at the sections addressing the following keywords:
General description: SPI block diagram
Slave Mode (Master selection: Slave configuration)
Simplex communication: Transmit-only mode (RXONLY=0)
Slave select (NSS) pin management: Software NSS management (SSM = 1)
Data frame format (data size can be set from 4-bit up to 16-bit length)
Configuration of SPI
The SPI unit is highly configurable, e.g. regarding the polarity of clock signal. Since it is an independent hardware unit, it should be able to handle your 0.5us reaction time requirement. The MCU firmware needs to set up the SPI unit and then provide new data to the SPI unit, each time the Tx buffer empty flag (TXE) is set. This can also be done by interrupt (TXEIE) or even using a DMA channel (TXDMAEN) with a circular buffer. In the latter case the "shift register functionality" runs completely independent of the MCU core (after setup).

PIC Clock frequency change by registers and edit project window

I am trying to set MCU clock frequency but I got confused. When I set it by registers (By coding) it seems like it does not change it. But when I change it from "edit project" window it starts to work. So my questions are:
What is the difference between changing MCU Clock by registers and from "edit project" window? Do I need to change both of them? What happens when they are 2 different frequencies?
Is there a difference betweeen oscillator frequency and MCU Clock frequency?
If I need to have 1 mHz clock frequency do I need to set MCU Clock to 1 mHz or to 4 mHz? Because in some websites they say that 1 operation take 4 clock cycles in PIC.
You didn't say which PIC but in modern ones you can change clock frequency in several places. One is configuration bits and this is what can be set in "edit project". Config.bits set the clock source (external,internal) as well as PLL. Another place is registers (OSCCON1 and friends), where you can switch between clock sources. There is also a config bit that allows/disallows clock switching.
The easiest way to make sense of all that is to install Microchip Code Configurator plugin into MplabX and set your clock/peripherals there. It will output chip-specific C code which you can then use in your project.

Declared MCU Clock Frequency vs oscillator frequency

I am quite confused about the relationship between the declared MCU Clock frequency ( which we set from edit project window) and the oscillator frequency (which we set by codding). So for an example let's say I need 31 kHz frequency. Do I also need to set the MCU Clock frequency from edit project window? If yes, in case of I want to change the frequency during the process, how am I supposed to change the clock frequency during run time?( Since I will not be able to change declared clock frequency.)
If you are using MPLAB X, the Harmony Plug-In will create code from your settings in UI. So it's the same and initial frequency on startup. If you want to change the frequency during runtime, you have to set the registers accordingly - but this sounds somehow odd.

What exactly meant in FreeRTOS's configCPU_CLOCK_HZ description?

The configCPU_CLOCK_HZ option explanation starts with this:
Enter the frequency in Hz at which the internal clock that driver the peripheral used to generate the tick interrupt will be executing.
Although I do more or less understand what it means, I need some finer explanation of what exactly is said there. Removing obvious "the peripheral used to generate the tick interrupt" from the middle I'm getting the "Enter the frequency in Hz at which the internal clock that driver will be executing", and this phrase looks a bit uncoordinated to me. What did the autor want to say with this? Some "that" driver, unlike, say, "this"? What "that"? The context doesn't imply any "that" here.
I think 'driver' should be 'drives' in that explanation.
configCPU_CLOCK_HZ is the frequency of the platform dependent timer that generates the tick interrupt. It is used by some ports to program the timer so it generates the correct FreeRTOS tick rate (see configTICK_RATE_HZ).
Example: configCPU_CLOCK_HZ is 1000000 (1 MHz) and configTICK_RATE_HZ is 100, then you configure the timer to generate an interrupt every 1000000/100 = 10000 ticks. That interrupt is your FreeRTOS system tick.
Take a look at an ARM Cortex-M port for one of the most common examples of this that uses the Cortex-M SysTick

GPIO extra interrupt in STM32

I see in STM32F103 series, the GPIO extra interrupt is set to the EXTI. And GPIOx_0 (x=A,B,C...)is set to EXTI0. Take an example, if I want to use PA0 and PB0 as interrupt input,can I set them to EXTI0 at the same time? I mean in the EXTI0_Handler function I read the value of the input register of PA0 and PB0 to judge which one input a electrical level I want to carry different function by using if...else. I use it in STM8 successfully but there seems a little problem in STM32. Can you help me? Thanks.
The answer explains the problem clearly. The picture takes an example that why the four bits will be changed if you set different pins. You can see that the four bit affect by each other status if you config other pins. I ignore this problem before.
If you look into the STM32F103 Reference Manual p. 209, you will see that there is actually a multiplexer that decides if PA0, PB0, ... or PG0 is connected to the EXTI0 signal:
STM32F103 ExtI0 schematic
That means that you cannot connect both PA0 and PB0 to EXTI0. In fact, there are four specific bits in the alternate function input/output register (AFIO) which let you choose which pin is connected to the EXTI0 signal. Here, these bits are located in the control register AFIO_EXTICR1. See the AFIO register map in the same document for further information.
Now I don't know which setup you are using, but as I recall, I had separate functions for different interrupt request routines (for EXTI0, EXTI1 and so on).