Problem with outputting LSE clock on PA8 pin as MCO
Hi!
I'm trying to output the LSE clock on the PA8 pin on my Nucleo64 STM32L476 board.
The code I have written is the following:
#include<stdint.h>
#define RCC_BASE_ADDR 0x40021000UL
#define RCC_CFGR_REG_OFFSET 0x08UL
#define RCC_CFGR_REG_ADDR (RCC_BASE_ADDR + RCC_CFGR_REG_OFFSET )
#define GPIOA_BASE_ADDR 0x48000000UL
int main(void)
{
uint32_t *pRccCfgrReg = (uint32_t*) RCC_CFGR_REG_ADDR;
//1. Configure the RCC_CFGR MCOSEL bit fields to select LSE as clock source: 0111: LSE clock selected
*pRccCfgrReg &=~(0U << 27);
*pRccCfgrReg |= (1U << 26);
*pRccCfgrReg |= (1U << 25);
*pRccCfgrReg |= (1U << 24);
//2. Configure PA8 to AF0 mode to behave as MCO signal
//a ) Enable the peripheral clock for GPIOA peripheral
// RCC_AHB2ENR
uint32_t *pRCCAhb2Enr = (uint32_t*)(RCC_BASE_ADDR + 0x4C);
*pRCCAhb2Enr |= (1U << 0); //Enable GPIOA peripheral clock
//b ) Configure the mode of GPIOA pin 8 as alternate function mode
// GPIOx_MODER
uint32_t *pGPIOAModeReg = (uint32_t*)(GPIOA_BASE_ADDR + 0x00UL);
*pGPIOAModeReg &=~(1U << 16); // clear bit 16
*pGPIOAModeReg |= (1U << 17); // set bit 17
//c ) Configure the alternation function register to set the mode 0 for PA8
// GPIOx_AFRH
uint32_t *pGPIOAAltFunHighReg = (uint32_t*)(GPIOA_BASE_ADDR + 0x24);
*pGPIOAAltFunHighReg &=~(1U << 0);
*pGPIOAAltFunHighReg &=~(1U << 1);
*pGPIOAAltFunHighReg &=~(1U << 2);
*pGPIOAAltFunHighReg &=~(1U << 3);
uint32_t *pRCC_BDCR = (uint32_t*)(RCC_BASE_ADDR + 0x90);
// Turn on LSE clock (LSEON)
*pRCC_BDCR |= (1U << 0);
for (;;);
}Can anyone see an obvious mistake? Is there something I have forgotten? I have read that the LSE clock might take some time to start, how can I improve my code to get around this?
An observation I have made is that the LSERDY bit is never set to 1, and I suspect this to be the issue. What can cause this bit to always stay 0?
Sincerely,
Marcus