#include "stm32f1xx.h"
#include "stm32f1xx_hal.h"
#define PERIPH_BASE ((uint32_t)0x40000000) /*!< Peripheral base address in the alias region */
/** ===== Macros for AHB and RCC ===== BEGIN ===== */
#define AHBPERIPH_BASE (PERIPH_BASE + 0x20000)
#define RCC_BASE (AHBPERIPH_BASE + 0x1000)
/**
typedef struct
{
__IO uint32_t CR; // offset = 0x00
__IO uint32_t CFGR; // offset = 0x04
__IO uint32_t CIR; // offset = 0x08
__IO uint32_t APB2RSTR; // offset = 0x0c
__IO uint32_t APB1RSTR; // offset = 0x10
__IO uint32_t AHBENR; // offset = 0x14
__IO uint32_t APB2ENR; // offset = 0x18
__IO uint32_t APB1ENR;
__IO uint32_t BDCR;
__IO uint32_t CSR;
#ifdef STM32F10X_CL
__IO uint32_t AHBRSTR;
__IO uint32_t CFGR2;
#endif
#if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
uint32_t RESERVED0;
__IO uint32_t CFGR2;
#endif
} RCC_TypeDef;
*/
#define RCC ((RCC_TypeDef *) RCC_BASE)
/** ===== Macros for AHB and RCC ===== END ===== */
/** ===== Macros for APB2 and GPIO C ===== BEGIN ===== */
#define APB2PERIPH_BASE (PERIPH_BASE + 0x10000)
#define GPIOC_BASE (APB2PERIPH_BASE + 0x1000)
/**
typedef struct
{
__IO uint32_t CRL; // offset = 0x00 (4 bytes)
__IO uint32_t CRH; // offset = 0x04
__IO uint32_t IDR; // offset = 0x08
__IO uint32_t ODR; // offset = 0x0c
__IO uint32_t BSRR;
__IO uint32_t BRR;
__IO uint32_t LCKR;
} GPIO_TypeDef;
*/
#define GPIOC ((GPIO_TypeDef *) GPIOC_BASE)
#define RCC_APB2ENR_IOPCEN ((uint32_t)0x00000010) /*!< I/O port C clock enable */
/** ===== Macros for APB2 and GPIO C ===== END ===== */
/** ===== Macros for PIN 13 ===== BEGIN ===== */
#define LED_PIN 13
#define GPIO_CRH_MODE13 ((uint32_t)0x00300000) /*!< MODE13[1:0] bits (Port x mode bits, pin 13) */
#define GPIO_CRH_MODE13_0 ((uint32_t)0x00100000) /*!< Bit 0 */
#define GPIO_CRH_MODE13_1 ((uint32_t)0x00200000) /*!< Bit 1 */
#define GPIO_CRH_CNF13 ((uint32_t)0x00C00000) /*!< CNF13[1:0] bits (Port x configuration bits, pin 13) */
#define GPIO_CRH_CNF13_0 ((uint32_t)0x00400000) /*!< Bit 0 */
#define GPIO_CRH_CNF13_1 ((uint32_t)0x00800000) /*!< Bit 1 */
/** ===== Macros for PIN 13 ===== END ===== */
void SysTick_Handler(void);
int main(void)
{
SysTick_Config(SystemCoreClock / 1000);
// Enable GPIOC clock
RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;
// Configure GPIOC Pin 13 as output
GPIOC->CRH &= ~(GPIO_CRH_MODE13 | GPIO_CRH_CNF13); // Clear mode and config bits
GPIOC->CRH |= (GPIO_CRH_MODE13_1); // Output mode, max speed 2 MHz
GPIOC->CRH |= (GPIO_CRH_CNF13_0); // General purpose output push-pull
while (1)
{
// // Toggle LED
// GPIOC->ODR ^= (1 << LED_PIN);
// // Simple delay
// HAL_Delay(500);
static uint32_t last_check =0 ;
if(HAL_GetTick() - last_check > 500) {
// Toggle LED
GPIOC->ODR ^= (1 << LED_PIN);
last_check = HAL_GetTick();
}
}
}
void SysTick_Handler(void)
{
// Increment system tick
HAL_IncTick();
}Loading
stm32-bluepill
stm32-bluepill