From 7e49cf6a3343dcf86087901b5a3b2a2b8a51c495 Mon Sep 17 00:00:00 2001 From: key Date: Sat, 30 Oct 2021 16:36:28 +0200 Subject: [PATCH] Working inplementation of pin setMode function --- bsl/csl/interfaces/pin.h | 56 ++++++++++++------------- bsl/csl/stm32f042/Src/pin.c | 34 ++++++++++++--- bsl/nucleo_f042k6/bsl_nucleo_f042k6.cpp | 2 +- main.cpp | 6 ++- 4 files changed, 61 insertions(+), 37 deletions(-) diff --git a/bsl/csl/interfaces/pin.h b/bsl/csl/interfaces/pin.h index ba42617..0aa8109 100644 --- a/bsl/csl/interfaces/pin.h +++ b/bsl/csl/interfaces/pin.h @@ -13,34 +13,34 @@ extern "C" { #include "../stm32f042/Drivers/CMSIS/Device/ST/STM32F0xx/Include/stm32f042x6.h" typedef enum { - pinA0 = GPIOA_BASE || 0, - pinA1 = GPIOA_BASE || 1, - pinA2 = GPIOA_BASE || 2, - pinA3 = GPIOA_BASE || 3, - pinA4 = GPIOA_BASE || 4, - pinA5 = GPIOA_BASE || 5, - pinA6 = GPIOA_BASE || 6, - pinA7 = GPIOA_BASE || 7, - pinA8 = GPIOA_BASE || 8, - pinA9 = GPIOA_BASE || 9, - pinA10 = GPIOA_BASE || 10, - pinA11 = GPIOA_BASE || 11, - pinA12 = GPIOA_BASE || 12, - pinA13 = GPIOA_BASE || 13, - pinA14 = GPIOA_BASE || 14, - pinA15 = GPIOA_BASE || 15, - - pinB0 = GPIOB_BASE || 0, - pinB1 = GPIOB_BASE || 1, - pinB3 = GPIOB_BASE || 3, - pinB4 = GPIOB_BASE || 4, - pinB5 = GPIOB_BASE || 5, - pinB6 = GPIOB_BASE || 6, - pinB7 = GPIOB_BASE || 7, - pinB8 = GPIOB_BASE || 8, - - pinF0 = GPIOF_BASE || 0, - pinF1 = GPIOF_BASE || 1 + pinA0 = GPIOA_BASE | 0, + pinA1 = GPIOA_BASE | 1, + pinA2 = GPIOA_BASE | 2, + pinA3 = GPIOA_BASE | 3, + pinA4 = GPIOA_BASE | 4, + pinA5 = GPIOA_BASE | 5, + pinA6 = GPIOA_BASE | 6, + pinA7 = GPIOA_BASE | 7, + pinA8 = GPIOA_BASE | 8, + pinA9 = GPIOA_BASE | 9, + pinA10 = GPIOA_BASE | 10, + pinA11 = GPIOA_BASE | 11, + pinA12 = GPIOA_BASE | 12, + pinA13 = GPIOA_BASE | 13, + pinA14 = GPIOA_BASE | 14, + pinA15 = GPIOA_BASE | 15, + + pinB0 = GPIOB_BASE | 0, + pinB1 = GPIOB_BASE | 1, + pinB3 = GPIOB_BASE | 3, + pinB4 = GPIOB_BASE | 4, + pinB5 = GPIOB_BASE | 5, + pinB6 = GPIOB_BASE | 6, + pinB7 = GPIOB_BASE | 7, + pinB8 = GPIOB_BASE | 8, + + pinF0 = GPIOF_BASE | 0, + pinF1 = GPIOF_BASE | 1 }pinNo_t; #endif diff --git a/bsl/csl/stm32f042/Src/pin.c b/bsl/csl/stm32f042/Src/pin.c index 2caeb97..f6e9d7b 100644 --- a/bsl/csl/stm32f042/Src/pin.c +++ b/bsl/csl/stm32f042/Src/pin.c @@ -1,17 +1,38 @@ #include "pin.h" +#define MODER_IN 0x0UL +#define MODER_OUT 0x1UL +#define MODER_ALTERNATE 0x2UL +#define MODER_ANALOG 0x3UL + +#define OSPEEDR_LOW 0x0UL +#define OSPEEDR_MEDIUM 0x1UL +#define OSPEEDR_HIGH 0x3UL + +#define PUPDR_NO_PULL 0x0UL +#define PUPDR_PULL_UP 0x1UL +#define PUPDR_PULL_DOWN 0x2UL + +const uint32_t moderMode[4] = { + MODER_ANALOG, + MODER_IN, + MODER_OUT, + MODER_ANALOG, + MODER_ALTERNATE +}; void setMode(pinNo_t pinNo, mode mode) { - + init(pinNo); + //Clear entry. + ((GPIO_TypeDef *)(pinNo&~0xFF))->MODER &=~ (MODER_ANALOG << ((pinNo & 0xFF) * 2)); + //Set to corresponding mode. + ((GPIO_TypeDef *)(pinNo&~0xFF))->MODER |= (moderMode[mode] << ((pinNo & 0xFF) * 2)); } void setOutputState(pinNo_t pinNo, state state) { - RCC->AHBENR |= RCC_AHBENR_GPIOBEN; - // Set PIN 3 of port B as Output - GPIOB->MODER |= GPIO_MODER_MODER3_0; - GPIOB->MODER &=~ GPIO_MODER_MODER3_1; + } void setPullUpDonw(pinNo_t pinNo, pullUpDown resistance) @@ -50,9 +71,10 @@ void set(pinNo_t pinNo, uint8_t state) GPIOB->BSRR |= GPIO_BSRR_BR_3; } +//Enable the pin port's clock void init(pinNo_t pinNo) { - + RCC->AHBENR |= RCC_AHBENR_GPIOBEN; } void deInit(pinNo_t pinNo) diff --git a/bsl/nucleo_f042k6/bsl_nucleo_f042k6.cpp b/bsl/nucleo_f042k6/bsl_nucleo_f042k6.cpp index 30fbac0..df3175b 100644 --- a/bsl/nucleo_f042k6/bsl_nucleo_f042k6.cpp +++ b/bsl/nucleo_f042k6/bsl_nucleo_f042k6.cpp @@ -1,5 +1,5 @@ #include "bsl_nucleo_f042k6.hpp" - +//#include"bsl/csl/interfaces/delay.hpp" Nucleo_f042k6::Nucleo_f042k6() { diff --git a/main.cpp b/main.cpp index b7d2c24..9255d20 100644 --- a/main.cpp +++ b/main.cpp @@ -1,13 +1,15 @@ #include"main.hpp" #include"bsl/csl/interfaces/delay.hpp" -//#include"bsl/csl/interfaces/pin.h" +#include"stm32f0xx_csl.h" int main(int argc, char *argv[]) { Delay delay; + stmStart(); + setMode(pinB3,output); - throwError(); + while(1) { set(pinB3,1);