Created
July 8, 2018 19:36
-
-
Save lupyuen/24c416b7ad5c792dc2c4ddc0f297e831 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Blink the LED connected to Pin PC13 on an STM32 Blue Pill | |
// From https://satoshinm.github.io/blog/171212_stm32_blue_pill_arm_development_board_first_look_bare_metal_programming.html | |
#define STACK_TOP 0x20002000 | |
#include "stm32f10x.h" | |
// from: stm32f103xb.h | |
#define RCC_APB2ENR_IOPAEN_Pos (2U) | |
#define RCC_APB2ENR_IOPAEN_Msk (0x1U << RCC_APB2ENR_IOPAEN_Pos) /*!< 0x00000004 */ | |
#define RCC_APB2ENR_IOPAEN RCC_APB2ENR_IOPAEN_Msk /*!< I/O port A clock enable */ | |
#define RCC_APB2ENR_IOPCEN_Pos (4U) | |
#define RCC_APB2ENR_IOPCEN_Msk (0x1U << RCC_APB2ENR_IOPCEN_Pos) /*!< 0x00000010 */ | |
#define RCC_APB2ENR_IOPCEN RCC_APB2ENR_IOPCEN_Msk /*!< I/O port C clock enable */ | |
#define GPIO_CRH_MODE13_Pos (20U) | |
#define GPIO_CRH_MODE13_Msk (0x3U << GPIO_CRH_MODE13_Pos) /*!< 0x00300000 */ | |
#define GPIO_CRH_MODE13 GPIO_CRH_MODE13_Msk /*!< MODE13[1:0] bits (Port x mode bits, pin 13) */ | |
#define GPIO_CRH_MODE13_0 (0x1U << GPIO_CRH_MODE13_Pos) /*!< 0x00100000 */ | |
#define GPIO_CRH_MODE13_1 (0x2U << GPIO_CRH_MODE13_Pos) /*!< 0x00200000 */ | |
#define GPIO_CRH_CNF13_Pos (22U) | |
#define GPIO_CRH_CNF13_Msk (0x3U << GPIO_CRH_CNF13_Pos) /*!< 0x00C00000 */ | |
#define GPIO_CRH_CNF13 GPIO_CRH_CNF13_Msk /*!< CNF13[1:0] bits (Port x configuration bits, pin 13) */ | |
#define GPIO_CRH_CNF13_0 (0x1U << GPIO_CRH_CNF13_Pos) /*!< 0x00400000 */ | |
#define GPIO_CRH_CNF13_1 (0x2U << GPIO_CRH_CNF13_Pos) /*!< 0x00800000 */ | |
#define DELAY_COUNT 1000000 | |
void delay (unsigned int ii) { | |
unsigned int jj; | |
for(jj = ii; jj > 0; jj--) | |
asm("nop"); | |
} | |
int main(void){ | |
// I/O port C and A clock enable | |
RCC->APB2ENR = RCC_APB2ENR_IOPCEN | RCC_APB2ENR_IOPAEN; | |
// Set PC_13 to output | |
GPIOC->CRH &= ~(GPIO_CRH_MODE13 | GPIO_CRH_CNF13); | |
GPIOC->CRH |= GPIO_CRH_MODE13; | |
while(1) { | |
GPIOC->BRR |= 1<<13; | |
delay(DELAY_COUNT); | |
GPIOC->BSRR |= 1<<13; | |
delay(DELAY_COUNT); | |
} | |
} | |
/* do nothing functions */ | |
int assert_param (void *v) { return 0; } | |
void handle_nmi (void) { return; } | |
void handle_hardfault (void) { return; } | |
/* vector table */ | |
unsigned int *myvectors[4] __attribute__ ((section("vectors")))= { | |
(unsigned int *) STACK_TOP, // stack pointer | |
(unsigned int *) main, // code entry point | |
(unsigned int *) handle_nmi, // handle non-maskable interrupts | |
(unsigned int *) handle_hardfault // handle hard faults | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment