RTLib
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
gpio.h
Go to the documentation of this file.
1 /*
2  * This file is part of RTLib.
3  *
4  * Copyright (C) 2018 Derppening <david.18.19.21@gmail.com>
5  *
6  * RTLib is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * RTLib is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with RTLib. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef RTLIB_CORE_STM32F1_GPIO_H_
21 #define RTLIB_CORE_STM32F1_GPIO_H_
22 
23 #if defined(STM32F1)
24 
25 #include <cstdint>
26 #include <utility>
27 
28 #include <libopencm3/stm32/gpio.h>
29 
30 #include "core/util.h"
31 
32 namespace core {
33 namespace stm32f1 {
34 
41 class GPIO final {
42  public:
46  using AltFnMaps = uint32_t;
47 
55  enum struct Configuration : uint8_t {
61  kInputAnalog = 0x0,
79  kOutputPushPull = 0x0,
98  };
99 
107  enum struct Mode : uint8_t {
113  kInput,
119  kOutput10MHz,
125  kOutput2MHz,
132  };
133 
141  enum struct JTAGDisables : uint32_t {
147  kNoDisable = AFIO_MAPR_SWJ_CFG_FULL_SWJ,
153  kNoJNTRST = AFIO_MAPR_SWJ_CFG_FULL_SWJ_NO_JNTRST,
159  kDisableJTAG = AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON,
165  kDisableJTAGSW = AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_OFF
166  };
167 
183  enum struct PriRemap : uint32_t {
194  kADC2ExtTrigReg = AFIO_MAPR_ADC2_ETRGREG_REMAP,
205  kADC2ExtTrigInj = AFIO_MAPR_ADC2_ETRGINJ_REMAP,
216  kADC1ExtTrigReg = AFIO_MAPR_ADC1_ETRGREG_REMAP,
227  kADC1ExtTrigInj = AFIO_MAPR_ADC1_ETRGINJ_REMAP,
238  kTIM5Ch4 = AFIO_MAPR_TIM5CH4_IREMAP,
249  kPD01 = AFIO_MAPR_PD01_REMAP,
259  kTIM4 = AFIO_MAPR_TIM4_REMAP,
267  kUSART2 = AFIO_MAPR_USART2_REMAP,
275  kUSART1 = AFIO_MAPR_USART1_REMAP,
283  kI2C1 = AFIO_MAPR_I2C1_REMAP,
291  kSPI1 = AFIO_MAPR_SPI1_REMAP,
301  kCAN1PA = AFIO_MAPR_CAN1_REMAP_PORTA,
312  kCAN1PB = AFIO_MAPR_CAN1_REMAP_PORTB,
322  kCAN1PD = AFIO_MAPR_CAN1_REMAP_PORTD,
330  kTIM3NoRemap = AFIO_MAPR_TIM3_REMAP_NO_REMAP,
340  kTIM3PartRemap = AFIO_MAPR_TIM3_REMAP_PARTIAL_REMAP,
350  kTIM3FullRemap = AFIO_MAPR_TIM3_REMAP_FULL_REMAP,
358  kTIM2NoRemap = AFIO_MAPR_TIM2_REMAP_NO_REMAP,
366  kTIM2PartRemap1 = AFIO_MAPR_TIM2_REMAP_PARTIAL_REMAP1,
374  kTIM2PartRemap2 = AFIO_MAPR_TIM2_REMAP_PARTIAL_REMAP2,
382  kTIM2FullRemap = AFIO_MAPR_TIM2_REMAP_FULL_REMAP,
390  kTIM1NoRemap = AFIO_MAPR_TIM1_REMAP_NO_REMAP,
398  kTIM1PartRemap = AFIO_MAPR_TIM1_REMAP_PARTIAL_REMAP,
406  kTIM1FullRemap = AFIO_MAPR_TIM1_REMAP_FULL_REMAP,
414  kUSART3NoRemap = AFIO_MAPR_USART3_REMAP_NO_REMAP,
422  kUSART3PartRemap = AFIO_MAPR_USART3_REMAP_PARTIAL_REMAP,
430  kUSART3FullRemap = AFIO_MAPR_USART3_REMAP_FULL_REMAP,
440  kPTPPPS = AFIO_MAPR_PTP_PPS_REMAP,
451  kTIM2IntTrig1 = AFIO_MAPR_TIM2ITR1_IREMAP,
461  kSPI3 = AFIO_MAPR_SPI3_REMAP,
472  kMIIOrRMIISelect = AFIO_MAPR_MII_RMII_SEL,
482  kCAN2 = AFIO_MAPR_CAN2_REMAP,
493  kEthMAC = AFIO_MAPR_ETH_REMAP
494  };
495 
503  enum struct SecRemap : uint32_t {
515  kMisc = AFIO_MAPR2_MISC_REMAP,
525  kTIM12 = AFIO_MAPR2_TIM12_REMAP,
536  kTIM76DMA = AFIO_MAPR2_TIM76_DAC_DMA_REMAPE,
547  kDisconnectNADV = AFIO_MAPR2_FSMC_NADV_DISCONNECT,
559  kTIM14 = AFIO_MAPR2_TIM14_REMAP,
571  kTIM13 = AFIO_MAPR2_TIM13_REMAP,
581  kTIM11 = AFIO_MAPR2_TIM11_REMAP,
591  kTIM10 = AFIO_MAPR2_TIM10_REMAP,
601  kTIM9 = AFIO_MAPR2_TIM9_REMAP,
612  kTIM1DMA = AFIO_MAPR2_TIM1_DMA_REMAP,
622  kCEC = AFIO_MAPR2_CEC_REMAP,
632  kTIM17 = AFIO_MAPR2_TIM17_REMAP,
642  kTIM16 = AFIO_MAPR2_TIM16_REMAP,
652  kTIM15 = AFIO_MAPR1_TIM16_REMAP
653  };
654 
658  struct Config {
664  Pinout pin = {};
665 
672 
679  };
680 
686  explicit GPIO(const Config& config);
687 
698  GPIO(Pinout pin, Configuration cnf, Mode mode);
699 
703  ~GPIO() = default;
704 
710  GPIO(GPIO&& other) noexcept = default;
717  GPIO& operator=(GPIO&& other) noexcept = default;
718 
725  GPIO(const GPIO&) = delete;
732  GPIO& operator=(const GPIO&) = delete;
733 
739  bool Read() const;
745  void Set(bool state) const;
749  void Toggle() const;
750 
754  void Reset() const;
755 
770  static void SetPriAltFn(JTAGDisables swj_state, AltFnMaps maps);
771 
786  static void SetSecAltFn(AltFnMaps maps);
787 
798  static void SetPriAltFn(JTAGDisables swj_state, PriRemap map) { SetPriAltFn(swj_state, static_cast<uint32_t>(map)); }
799 
808  static void SetSecAltFn(SecRemap map) { SetSecAltFn(static_cast<uint32_t>(map)); }
809 
810  private:
819  void Init(Configuration cnf, Mode mode) const;
820 
828  void InitRcc(Port port) const;
829 
837  const Pinout& GetPinout() const { return pin_; }
841  Pinout pin_;
842 };
843 
844 } // namespace stm32f1
845 } // namespace core
846 
847 #endif // defined(STM32F1)
848 
849 #endif // RTLIB_CORE_STM32F1_GPIO_H_
unsigned Port
Type definition for MCU Port.
Definition: util.h:41
Ethernet PTP PPS Remapping.
ADC1 External Trigger Injected Conversion Remapping.
CAN1 RX/TX Port D Remapping.
TIM2 ETR/CH1-CH4 Partial Remapping (2).
SPI1 NSS/SCK/MISO/MOSI Remapping.
TIM6-TIM7 DAC to DMA Remapping.
TIM3 CH1-CH2 Partial Remapping.
Output Mode at 10MHz max.
std::pair< Port, Pin > Pinout
Type definition for MCU Pinout.
Definition: util.h:49
Output Mode at 50MHz max.
bool Read() const
Reads the current logic state of the managed GPIO.
Definition: gpio.cpp:78
TIM3 No Remapping. (Default)
Use this GPIO as digital open-drain output.
uint32_t AltFnMaps
Type definition for alternate function selection.
Definition: gpio.h:46
ADC2 External Trigger Injected Conversion Remapping.
TIM5 Channel 4 Internal Remapping.
USART3 No Remapping. (Default)
Mode mode
GPIO Output Speed.
Definition: gpio.h:678
PriRemap
Enumeration for the primary set of different alternate function remaps.
Definition: gpio.h:183
SPI3 NSS/SCK/MISO/MOSI and I2S3 WS/CK/SD Remapping.
JTAGDisables
Enumeration for different Serial Wire JTAG states.
Definition: gpio.h:141
TIM1 BKIN/CH1N-CH3N Partial Remapping.
static void SetPriAltFn(JTAGDisables swj_state, PriRemap map)
Configures one primary remap functionality.
Definition: gpio.h:798
Use full JTAG capability with JNTRST.
Use this GPIO as floating input.
GPIO(const Config &config)
Conversion constructor.
Definition: gpio.cpp:32
I2C1 SCL/SDA Remapping.
SecRemap
Enumeration for the secondary set of different alternate function remaps.
Definition: gpio.h:503
Pinout pin
GPIO Pin to enable and initialize.
Definition: gpio.h:664
CAN1 RX/TX Port A Remapping.
Use this GPIO as digital push-pull output.
Ethernet MAC DV/RXD0-RXD3 Remapping.
TIM2 Internal Trigger 1 Remapping.
CAN1 RX/TX Port B Remapping.
Utility helpers for core library.
STM32F1xx-specific HAL implementation for GPIO pins.
Definition: gpio.h:41
void Reset() const
Resets the GPIO to its original configuration, i.e. when the RESET button is first pushed...
Definition: gpio.cpp:94
static void SetPriAltFn(JTAGDisables swj_state, AltFnMaps maps)
Configures the primary remap functionality of all GPIOs.
Definition: gpio.cpp:98
Use this GPIO as alternate function open-drain output.
TIM4 CH1/CH2/CH3/CH4 Remapping.
USART2 CTS/RTS/TX/RX/CK Remapping.
void Set(bool state) const
Sets new GPIO state.
Definition: gpio.cpp:82
ADC1 External Trigger Regulator Conversion Remapping.
ADC2 External Trigger Regulator Conversion Remapping.
TIM12 CH1-CH2 Remapping.
TIM2 ETR/CH1-CH2 Partial Remapping (1).
USART3 TX/RX/CK/CTS/RTS Full Remapping.
Mode
Enumeration for different GPIO modes.
Definition: gpio.h:107
USART3 TX/RX/CK Partial Remapping.
TIM1 ETR/CH1-CH4/BKIN/CH1N-CH3N Full Remapping.
void Toggle() const
Toggles GPIO state. Logic High -> Logic Low and vice versa.
Definition: gpio.cpp:90
Configuration cnf
GPIO Mode.
Definition: gpio.h:671
TIM15 CH1-CH2 Remapping.
TIM9 CH1-CH2 Remapping.
TIM2 No Remapping. (Default)
TIM2 ETR/CH1-CH4 Full Remapping.
static void SetSecAltFn(AltFnMaps maps)
Configures the secondary remap functionality of all GPIOs.
Definition: gpio.cpp:102
Use this GPIO as alternate function push-pull output.
Use this GPIO as pull-up/pull-down input.
TIM1 No Remapping. (Default)
Configuration for GPIO.
Definition: gpio.h:658
static void SetSecAltFn(SecRemap map)
Configures one secondary remap functionality.
Definition: gpio.h:808
Miscellaneous Features Remapping.
Configuration
Enumeration for different GPIO configurations.
Definition: gpio.h:55