#include <stdint.h>
/*==================================== RCC ========================================*/
#define RCC_BASE 0x40021000UL
#define RCC_IOPENR (*(volatile uint32_t*)(RCC_BASE + 0x34U))
#define RCC_APBENR1 (*(volatile uint32_t*)(RCC_BASE + 0x3CU))
#define RCC_IOPENR_GPIOAEN (1U << 0)
#define RCC_APBENR1_TIM3_EN (1U << 1)
/*======================================== GPIO A ====================================*/
#define GPIOA_BASE 0x50000000UL
#define GPIOA_MODER (*(volatile uint32_t*)(GPIOA_BASE + 0x00U))
#define GPIOA_ODR (*(volatile uint32_t*)(GPIOA_BASE + 0x14U))
#define LED_PIN 5U
#define LED_PIN_MASK (1U << LED_PIN)
/*========================================== TIM3 ====================================*/
#define TIM3_BASE 0x40000400UL
#define TIM3_CR1 (*(volatile uint32_t*)(TIM3_BASE + 0x00U))
#define TIM3_SR (*(volatile uint32_t*)(TIM3_BASE + 0x10U))
#define TIM3_EGR (*(volatile uint32_t*)(TIM3_BASE + 0x14U))
#define TIM3_CNT (*(volatile uint32_t*)(TIM3_BASE + 0x24U))
#define TIM3_PSC (*(volatile uint32_t*)(TIM3_BASE + 0x24U))
#define TIM3_ARR (*(volatile uint32_t*)(TIM3_BASE + 0x24U))
#define TIM3_CR1_CEN (1U << 0)
#define TIM3_EGR_UG (1U << 0)
#define TIM3_SR_UIF (1U << 0)
/*================================ GPIOA Initialization ====================================*/
static void GPIOA_init(void)
{
/*Enable GPIOA peripheral clock */
RCC_IOPENR |= RCC_IOPENR_GPIOAEN;
/* PA5: General purpose output mode */
GPIOA_MODER &= ~(3U << (LED_PIN * 2U));
GPIOA_MODER |= (1U << (LED_PIN * 2U));
/* Initially turn LED off */
GPIOA_ODR &= ~LED_PIN_MASK;
}
/*=============================== TIM3 Initialization =======================================*/
static void TIM3_init(void)
{
/* Enable TIM3 peripheral clock */
RCC_APBENR1 |= RCC_APBENR1_TIM3_EN;
/* Stop timer during configuration */
TIM3_CR1 &= ~TIM3_CR1_CEN;
/* Configure timer frequency */
TIM3_PSC = 47999U;
TIM3_ARR = 499U;
/* Reset Counter */
TIM3_CNT = 0U;
/* Load prescaler and auto reload values */
TIM3_EGR = TIM3_EGR_UG;
/* Clear Upadte interrupt flag */
TIM3_SR = 0U;
/* Start timer */
TIM3_CR1 |= TIM3_CR1_CEN;
}
int main (void)
{
GPIOA_init();
TIM3_init();
while(1)
{
/* Wait for timer upadte */
if((TIM3_SR & TIM3_SR_UIF) != 0U)
{
/* Clear update flag */
TIM3_SR = ~TIM3_SR_UIF;
/* Toggle LED */
GPIOA_ODR ^= LED_PIN_MASK;
}
}
}Loading
st-nucleo-c031c6
st-nucleo-c031c6