STM32-LTDC, LCD-TFT, LVGL (MCU3) Lecture 22| Setting up main system clock code implementation part-6

 

Setting up main system clock code implementation part-6

 

SystemClock_Setup code for STM32F407 Discovery Board 

void SystemClock_Setup(void)

{

RCC_TypeDef *pRCC = RCC;
FLASH_TypeDef *pFlash = FLASH;
PWR_TypeDef *pPWR = PWR;

//1. Program flash wait states
REG_SET_VAL(pFlash->ACR,0x5U,0xFU,FLASH_ACR_LATENCY_Pos);

//2. Over drive settings
REG_SET_BIT(pRCC->APB1ENR,RCC_APB1ENR_PWREN_Pos); /*Enable clock for PWR register access*/
REG_SET_BIT(pPWR->CR,PWR_CR_VOS_Pos); /*VOS = 1*/

//3. Setting up main PLL
REG_SET_VAL(pRCC->PLLCFGR,0x8U,0x3FU,RCC_PLLCFGR_PLLM_Pos); /*PLL_M*/
REG_SET_VAL(pRCC->PLLCFGR,168U,0x1FFU,RCC_PLLCFGR_PLLN_Pos); /*PLL_N*/
REG_SET_VAL(pRCC->PLLCFGR,0x00U,0x3U,RCC_PLLCFGR_PLLP_Pos); /*PLL_P*/

//5. Setting up AHB and APBx clocks
REG_SET_VAL(pRCC->CFGR,0U,0xFU,RCC_CFGR_HPRE_Pos); /*AHB prescaler*/
REG_SET_VAL(pRCC->CFGR,0x5U,0x7U,RCC_CFGR_PPRE1_Pos); /*APB1 prescaler*/
REG_SET_VAL(pRCC->CFGR,0x4U,0x7U,RCC_CFGR_PPRE2_Pos); /*APB2 prescaler*/

//6. Turn on PLL and wait for PLLCLK ready
REG_SET_BIT(pRCC->CR,RCC_CR_PLLON_Pos);
while(!REG_READ_BIT(pRCC->CR,RCC_CR_PLLRDY_Pos));

//7. Switch PLLCLK as SYSCLK
REG_SET_VAL(pRCC->CFGR,0x2U,0x3U,RCC_CFGR_SW_Pos);
while(!(REG_READ_VAL(pRCC->CFGR,0x3U,RCC_CFGR_SWS_Pos) == 0x2U));

}

The code is pretty much the same compared to the previous one(SystemClock_Setup code for stm32f429 discovery). In the Overdrive settings, we modified some code. 

In this microcontroller, the maximum speed you can achieve is 168MHz. That’s why, I have modified this line here for the PLL_N entry. 

And apart from that, all these codes remain the same.

 

SystemClock_Setup code for STM32F746 Discovery Board

Below code snippet shows how the system clock setup code looks in the STM32F746 Discovery board. 

void SystemClock_Setup(void)
{
RCC_TypeDef *pRCC = RCC;
FLASH_TypeDef *pFlash = FLASH;
PWR_TypeDef *pPWR = PWR;

//1. Program flash wait states
REG_SET_VAL(pFlash->ACR,0x7U,0xFU,FLASH_ACR_LATENCY_Pos);

//2. Over drive settings
REG_SET_BIT(pRCC->APB1ENR,RCC_APB1ENR_PWREN_Pos); /*Enable clock for PWR register access*/
REG_SET_VAL(pPWR->CR1,0x3,0x3,PWR_CR1_VOS_Pos); /*VOS = 0b11*/
REG_SET_BIT(pPWR->CR1,PWR_CR1_ODEN_Pos); /* Activate over drive mode */
while(! REG_READ_BIT(pPWR->CSR1,PWR_CSR1_ODRDY_Pos)); /* wait for overdrive ready*/
REG_SET_BIT(pPWR->CR1,PWR_CR1_ODSWEN_Pos); /* Over drive switch enable*/

//3. Setting up main PLL
REG_SET_VAL(pRCC->PLLCFGR,0x8U,0x3FU,RCC_PLLCFGR_PLLM_Pos); /*PLL_M*/
REG_SET_VAL(pRCC->PLLCFGR,216U,0x1FFU,RCC_PLLCFGR_PLLN_Pos); /*PLL_N*/
REG_SET_VAL(pRCC->PLLCFGR,0x00U,0x3U,RCC_PLLCFGR_PLLP_Pos); /*PLL_P*/

/////////////////This step is only required if you are using RGB interface ////////////
//4. Setting up LCD_CLK using PLLSAI block
REG_SET_VAL(pRCC->PLLSAICFGR,50U,0x1FFU,RCC_PLLSAICFGR_PLLSAIN_Pos); /*PLLSAI_N*/
REG_SET_VAL(pRCC->PLLSAICFGR,0x02U,0x7U,RCC_PLLSAICFGR_PLLSAIR_Pos); /*PLLSAI_R*/
REG_SET_VAL(pRCC->DCKCFGR1,0x00u,0x3U,RCC_DCKCFGR1_PLLSAIDIVR_Pos); /*DIV*/
REG_SET_BIT(pRCC->CR,RCC_CR_PLLSAION_Pos);
while(!REG_READ_BIT(pRCC->CR,RCC_CR_PLLSAIRDY_Pos));
///////////////////////////////////////////////////////////////////////////////////////

//5. Setting up AHB and APBx clocks
REG_SET_VAL(pRCC->CFGR,0U,0xFU,RCC_CFGR_HPRE_Pos); /*AHB prescaler*/
REG_SET_VAL(pRCC->CFGR,0x5U,0x7U,RCC_CFGR_PPRE1_Pos); /*APB1 prescaler*/
REG_SET_VAL(pRCC->CFGR,0x4U,0x7U,RCC_CFGR_PPRE2_Pos); /*APB2 prescaler*/

//6. Turn on PLL and wait for PLLCLK ready
REG_SET_BIT(pRCC->CR,RCC_CR_PLLON_Pos);
while(!REG_READ_BIT(pRCC->CR,RCC_CR_PLLRDY_Pos));

//7. Switch PLLCLK as SYSCLK
REG_SET_VAL(pRCC->CFGR,0x2U,0x3U,RCC_CFGR_SW_Pos);
while(!(REG_READ_VAL(pRCC->CFGR,0x3U,RCC_CFGR_SWS_Pos) == 0x2U));

}

In this microcontroller, the maximum speed that you can achieve is, 216 MHz(line 217).

And also, we have to produce the LCD_CLK by using the PLLSAI block of the RCC. 

So, please compare your code with this code. And you can also verify the clock by using an oscilloscope. For that, you have to route this clock via some microcontroller pin. So, that feature is available in the microcontroller. And if you just explore the RCC section of the reference manual, they have mentioned how to route the clock via the microcontroller pins for measurement purposes.

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.