From 2bdfa688a33b20cbacc151bc6a0d6dd21a2e7662 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Sat, 28 Nov 2020 12:54:11 +0800 Subject: [PATCH] Board mapping abstraction. --- Makefile | 3 ++- board.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 board.c diff --git a/Makefile b/Makefile index 4908a51..e83b0c8 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,8 @@ PROJECT = f030f4 #SRCS = blink.c #SRCS = ledtick.c #SRCS = cstartup.c -SRCS = startup.c init.c success.c +#SRCS = startup.c init.c success.c +SRCS = startup.c board.c success.c OBJS = $(SRCS:.c=.o) CPU = -mthumb -mcpu=cortex-m0 CFLAGS = $(CPU) -g -Wall -Wextra -Os diff --git a/board.c b/board.c new file mode 100644 index 0000000..906f911 --- /dev/null +++ b/board.c @@ -0,0 +1,56 @@ +/* board.c -- board mapping abstraction +** Copyright (c) 2020 Renaud Fivet +*/ + +#define SYSTICK ((volatile long *) 0xE000E010) +#define SYSTICK_CSR SYSTICK[ 0] +#define SYSTICK_RVR SYSTICK[ 1] +#define SYSTICK_CVR SYSTICK[ 2] + +#define CAT( a, b) a##b +#define HEXA( a) CAT( 0x, a) +#define RCC ((volatile long *) 0x40021000) +#define RCC_AHBENR RCC[ 5] +#define RCC_AHBENR_IOP( h) (1 << (17 + HEXA( h) - 0xA)) + +#define GPIOA ((volatile long *) 0x48000000) +#define GPIOB ((volatile long *) 0x48000400) +#define GPIO( x) CAT( GPIO, x) +#define MODER 0 +#define ODR 5 + +/* user LED ON when PA4 is high */ +#define LED_IOP A +#define LED_PIN 4 +#define LED_ON 1 + +void SysTick_Handler( void) { +#ifdef LED_ON + GPIO( LED_IOP)[ ODR] ^= 1 << LED_PIN ; /* Toggle User LED */ +#endif +} + +int init( void) { +/* By default SYSCLK == HSI [8MHZ] */ + +/* SYSTICK */ + SYSTICK_RVR = 1000000 - 1 ; /* HBA / 8 */ + SYSTICK_CVR = 0 ; + SYSTICK_CSR = 3 ; /* HBA / 8, Interrupt ON, Enable */ + /* SysTick_Handler will execute every 1s from now on */ + +#ifdef LED_ON +/* User LED ON */ + RCC_AHBENR |= RCC_AHBENR_IOP( LED_IOP) ; /* Enable IOPx periph */ + GPIO( LED_IOP)[ MODER] |= 1 << (LED_PIN * 2) ; /* LED_IO Output [01], + ** over default 00 */ + /* OTYPER Push-Pull by default */ + /* Pxn output default LOW at reset */ +# if LED_ON + SysTick_Handler() ; +# endif +#endif + return 0 ; +} + +/* end of board.c */