From 0f4fda1f7a56b9b84d0ee2041d569d0fa5d504cd Mon Sep 17 00:00:00 2001 From: key Date: Sat, 23 Oct 2021 17:19:20 +0200 Subject: [PATCH] Implemented pin_impl Branch onto master branch. the pin configuration is made in the bsl/csl/nucleo_f042k6/*.cpp --- bsl/csl/interfaces/pin.hpp | 27 ++-- bsl/csl/stm32f042/Inc/stm_pin.hpp | 181 ++++++++++++++++++++++++ bsl/csl/stm32f042/Src/CMakeLists.txt | 10 -- bsl/csl/stm32f042/Src/gpio.cpp | 82 ----------- bsl/nucleo_f042k6/bslConfig.cmake | 2 +- bsl/nucleo_f042k6/bsl_nucleo_f042k6.cpp | 24 +++- main.cpp | 1 + 7 files changed, 214 insertions(+), 113 deletions(-) create mode 100644 bsl/csl/stm32f042/Inc/stm_pin.hpp delete mode 100644 bsl/csl/stm32f042/Src/gpio.cpp diff --git a/bsl/csl/interfaces/pin.hpp b/bsl/csl/interfaces/pin.hpp index d53381b..fbf4a49 100644 --- a/bsl/csl/interfaces/pin.hpp +++ b/bsl/csl/interfaces/pin.hpp @@ -3,6 +3,7 @@ #include #include +#include class Pin { @@ -69,25 +70,23 @@ class Pin NotGpio }; - Pin(); - ~Pin(); - void setMode(mode mode); - void setOutputState(state state); - void setPullUpDonw(pullUpDown resistance); - void setSpeed(speed speed); - void config(mode mode, state state, pullUpDown resistance, speed speed); + virtual void setMode(mode mode) = 0; + virtual void setOutputState(state state) = 0; + virtual void setPullUpDonw(pullUpDown resistance) = 0; + virtual void setSpeed(speed speed) = 0; + virtual void config(mode mode, state state, pullUpDown resistance, speed speed) = 0; - bool read(); - bool toggle(); - void write(bool state); + virtual bool read() = 0; + virtual bool toggle() = 0; + virtual void write(bool state) = 0; - void init(); - void deInit(); + virtual void init() = 0; + virtual void deInit() = 0; - void hardwareInfo(); + virtual void hardwareInfo() = 0; private: - void throwError(uint16_t line, errors errNo); + virtual void throwError(uint16_t line, errors errNo) = 0; }; #endif // _GPIO_H_ diff --git a/bsl/csl/stm32f042/Inc/stm_pin.hpp b/bsl/csl/stm32f042/Inc/stm_pin.hpp new file mode 100644 index 0000000..da0e81b --- /dev/null +++ b/bsl/csl/stm32f042/Inc/stm_pin.hpp @@ -0,0 +1,181 @@ +#ifndef __STM_PIN_HPP__ +#define __STM_PIN_HPP__ + +#include "../../interfaces/pin.hpp" +#include "../Drivers/CMSIS/Device/ST/STM32F0xx/Include/stm32f042x6.h" +#include +#include + + +// https://accu.org/journals/overload/13/68/goodliffe_281/ + + +enum GPIO_Port { + Port_A_base_address = GPIOA_BASE, + Port_B_base_address = GPIOB_BASE +}; + +enum Pin_no{ + pin_0, + pin_1, + pin_2, + pin_3, + pin_4, + pin_5, + pin_6, + pin_7 +}; + +template +class STM_Pin : Pin +{ + public: + + STM_Pin() { + static_assert(pin_no < 7, "GPIO has only 8 ports!"); + } + + void setMode(mode mode) override + { + reinterpret_cast(port_base_address)->MODER &= ~(0x3UL << (pin_no * 2)); + switch (mode) + { + case undefined: + return; + break; + case input: + return; + case output: + reinterpret_cast(port_base_address)->MODER |= 0x1UL << (pin_no * 2); + return; + case analog: + reinterpret_cast(port_base_address)->MODER |= 0x3UL << (pin_no * 2); + return; + case alternate: + reinterpret_cast(port_base_address)->MODER |= 0x2UL << (pin_no * 2); + return; + default: + break; + }; + } + + void setOutputState(state state) override + { + // TODO: test it! + switch (state) + { + case openDrain: + reinterpret_cast(port_base_address)->OTYPER |= (1UL << pin_no); + return; + break; + case pushPull: + reinterpret_cast(port_base_address)->OTYPER &= ~(1UL << pin_no); + return; + break; + case floating: + break; + default: + break; + }; + } + + void setPullUpDonw(pullUpDown resistance) override + { + + } + + void setSpeed(speed speed) override + { + /* + slow, + normal, + fast, + veryFast + */ + reinterpret_cast(port_base_address)->OSPEEDR &= ~(GPIO_OSPEEDR_OSPEEDR0_Msk << (2 * pin_no)); + switch(speed) { + case slow: + return; + case normal: + return; + case fast: + reinterpret_cast(port_base_address)->OSPEEDR |= (0x01UL << (2 * pin_no)); + return; + case veryFast: + reinterpret_cast(port_base_address)->OSPEEDR |= (0x3UL << (2 * pin_no)); + return; + default: + break; + }; + } + + void config(mode mode, state state, pullUpDown resistance, speed speed) override + { + + } + + bool read() override + { + + } + + bool toggle() override + { + + } + + void write(bool state) override + { + + if(state) { + reinterpret_cast(port_base_address)->BSRR |= (GPIO_BSRR_BS_0 << pin_no); + return; + } + reinterpret_cast(port_base_address)->BSRR |= (GPIO_BSRR_BR_0 << pin_no); + } + + void init() override + { + // TODO: find better way + switch (port_base_address) + { + case GPIOA_BASE: + RCC->AHBENR |= RCC_AHBENR_GPIOAEN; + return; + break; + case GPIOB_BASE: + RCC->AHBENR |= RCC_AHBENR_GPIOBEN; + return; + break; + case GPIOC_BASE: + RCC->AHBENR |= RCC_AHBENR_GPIOCEN; + return; + break; + case GPIOF_BASE: + RCC->AHBENR |= RCC_AHBENR_GPIOFEN; + return; + break; + default: + break; + }; + } + + void deInit() override + { + + } + + void hardwareInfo() override + { + + } + + private: + + void throwError(uint16_t line, errors errNo) override + { + + } +}; + +#endif //__STM_PIN_HPP__ diff --git a/bsl/csl/stm32f042/Src/CMakeLists.txt b/bsl/csl/stm32f042/Src/CMakeLists.txt index 563e4a6..0d30d3f 100644 --- a/bsl/csl/stm32f042/Src/CMakeLists.txt +++ b/bsl/csl/stm32f042/Src/CMakeLists.txt @@ -12,19 +12,9 @@ target_compile_options(stmSources PRIVATE ${C_FLAGS}) target_compile_definitions(stmSources PRIVATE ${C_DEFS}) target_include_directories(stmSources PUBLIC ${STMSRC_INCLUDES}) target_link_libraries(stmSources sub::drivers) - add_library(sub::sources ALIAS stmSources) - -add_library(stmGpio gpio.cpp) - -target_compile_options(stmGpio PRIVATE ${C_FLAGS}) -target_compile_definitions(stmGpio PRIVATE ${C_DEFS}) -target_include_directories(stmGpio PUBLIC ${INTERFACES_DIR} ${CSL_INCLUDES}) -add_library(sub::gpio ALIAS stmGpio) - add_library(stmDelay delay.cpp) - target_compile_options(stmDelay PRIVATE ${C_FLAGS}) target_compile_definitions(stmDelay PRIVATE ${C_DEFS}) target_include_directories(stmDelay PUBLIC ${INTERFACES_DIR} ${CSL_INCLUDES}) diff --git a/bsl/csl/stm32f042/Src/gpio.cpp b/bsl/csl/stm32f042/Src/gpio.cpp deleted file mode 100644 index 2c4d218..0000000 --- a/bsl/csl/stm32f042/Src/gpio.cpp +++ /dev/null @@ -1,82 +0,0 @@ -#include "pin.hpp" -#include "../Drivers/CMSIS/Device/ST/STM32F0xx/Include/stm32f042x6.h" -//#include "stm32f042x6.h" - - -Pin::Pin(){ - 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; -} -Pin::~Pin(){} - - -void Pin::setMode(mode mode) -{ - -} - -void Pin::setOutputState(state state) -{ - -} - -void Pin::setPullUpDonw(pullUpDown resistance) -{ - -} - -void Pin::setSpeed(speed speed) -{ - -} - -void Pin::config(mode mode, state state, pullUpDown resistance, speed speed) -{ - -} - - -bool Pin::read() -{ - return 1; -} - -bool Pin::toggle() -{ - return 1; -} - -void Pin::write(bool state) -{ - if(state == 1) - { - GPIOB->BSRR |= GPIO_BSRR_BS_3; - return; - } - GPIOB->BSRR |= GPIO_BSRR_BR_3; -} - - -void Pin::init() -{ - -} - -void Pin::deInit() -{ - -} - - -void Pin::hardwareInfo() -{ - -} - - -void Pin::throwError(uint16_t line, errors errNo) -{ - -} diff --git a/bsl/nucleo_f042k6/bslConfig.cmake b/bsl/nucleo_f042k6/bslConfig.cmake index 9406819..dbe7bbc 100644 --- a/bsl/nucleo_f042k6/bslConfig.cmake +++ b/bsl/nucleo_f042k6/bslConfig.cmake @@ -74,5 +74,5 @@ set (CPP_DEFS ${C_DEFS}) list(APPEND EXTRA_LIBS sub::startup) list(APPEND EXTRA_LIBS sub::translator) list(APPEND EXTRA_LIBS sub::sources) -list(APPEND EXTRA_LIBS sub::gpio) +#list(APPEND EXTRA_LIBS sub::gpio) # TODO : PLease add this again. list(APPEND EXTRA_LIBS sub::delay) diff --git a/bsl/nucleo_f042k6/bsl_nucleo_f042k6.cpp b/bsl/nucleo_f042k6/bsl_nucleo_f042k6.cpp index bffa639..b58d50f 100644 --- a/bsl/nucleo_f042k6/bsl_nucleo_f042k6.cpp +++ b/bsl/nucleo_f042k6/bsl_nucleo_f042k6.cpp @@ -1,22 +1,34 @@ #include "bsl_nucleo_f042k6.hpp" #include "../csl/interfaces/pin.hpp" #include "../csl/interfaces/delay.hpp" +#include "../csl/stm32f042/Inc/stm_pin.hpp" int startBSL() { stmStart(); - - Pin pin; Delay delay; + + STM_Pin pin; + STM_Pin pin_a0; + + pin.init(); + pin.setMode(Pin::mode::output); + pin.setSpeed(Pin::speed::veryFast); + + pin_a0.init(); + pin_a0.setMode(Pin::mode::output); + pin_a0.setSpeed(Pin::speed::veryFast); + pin_a0.setOutputState(Pin::state::pushPull); while(1) { - delay.ms(50); + delay.ms(100); pin.write(true); - delay.ms(50); + pin_a0.write(true); + delay.ms(100); pin.write(false); - } - + pin_a0.write(false); + } return 1; } diff --git a/main.cpp b/main.cpp index 103300d..1a9ad86 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ #include"main.hpp" + int main(int argc, char *argv[]) { startBSL();