Add C++ Gpio driver

This commit is contained in:
Thomas Klaehn
2020-12-17 10:51:20 +01:00
parent 75dda2e6eb
commit 4dd55be05c
3 changed files with 181 additions and 49 deletions

View File

@@ -11,7 +11,6 @@ IWDG_HandleTypeDef hiwdg;
UART_HandleTypeDef huart2;
static void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
// static void MX_IWDG_Init(void);
@@ -24,6 +23,8 @@ class My
~My() {printf("Destructor\r\n");};
};
using namespace perinet::platform::stm32g0xx;
int main(void)
{
unsigned int i = 1, j = 40;
@@ -44,13 +45,14 @@ int main(void)
MODIFY_REG(SYSCFG->CFGR1, (SYSCFG_CFGR1_UCPD1_STROBE | SYSCFG_CFGR1_UCPD2_STROBE), SYSCFG_CFGR1_UCPD1_STROBE | SYSCFG_CFGR1_UCPD2_STROBE);
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
My* my = new My();
delete(my);
Gpio green_led(Gpio::Port::PORT_A, 5, Gpio::Mode::MODE_OUTPUT_PP);
// MX_IWDG_Init();
while (1) {
if (j < 100) {
@@ -66,11 +68,7 @@ int main(void)
j = 800;
}
printf("%u: Hello World\r\n", i++);
<<<<<<< HEAD
HAL_GPIO_TogglePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin);
=======
green_led.toggle();
>>>>>>> 133286e (fix: format)
HAL_Delay(j);
// HAL_IWDG_Refresh(&hiwdg);
}
@@ -172,49 +170,6 @@ static void MX_USART2_UART_Init(void)
}
}
static void MX_GPIO_Init(void)
{
/* Enable PORT C clock*/
SET_BIT(RCC->IOPENR, RCC_IOPENR_GPIOCEN);
/* Delay after an RCC peripheral clock enabling */
READ_BIT(RCC->IOPENR, RCC_IOPENR_GPIOCEN);
/* Enable PORT F clock*/
SET_BIT(RCC->IOPENR, RCC_IOPENR_GPIOFEN);
/* Delay after an RCC peripheral clock enabling */
READ_BIT(RCC->IOPENR, RCC_IOPENR_GPIOFEN);
/* Enable PORT A clock*/
SET_BIT(RCC->IOPENR, RCC_IOPENR_GPIOAEN);
/* Delay after an RCC peripheral clock enabling */
READ_BIT(RCC->IOPENR, RCC_IOPENR_GPIOAEN);
GPIOA->BSRR = 1 << 5; // PORTA PIN5 (green LED)
/* Configure the IO Speed */
uint32_t temp = GPIOA->OSPEEDR;
temp &= ~(GPIO_OSPEEDR_OSPEED0 << (5 * 2u));
temp |= (GPIO_SPEED_FREQ_HIGH << (5 * 2u));
GPIOA->OSPEEDR = temp;
/* Configure the IO Output Type */
temp = GPIOA->OTYPER;
temp &= ~(GPIO_OTYPER_OT0 << 5);
temp |= (((GPIO_MODE_OUTPUT_PP & 0x00000010u) >> 4u) << 5);
GPIOA->OTYPER = temp;
/* Activate the Pull-up or Pull down resistor for the current IO */
temp = GPIOA->PUPDR;
temp &= ~(GPIO_PUPDR_PUPD0 << (5 * 2u));
temp |= ((GPIO_NOPULL) << (5 * 2u));
GPIOA->PUPDR = temp;
/* Configure IO Direction mode (Input, Output, Alternate or Analog) */
temp = GPIOA->MODER;
temp &= ~(GPIO_MODER_MODE0 << (5 * 2u));
temp |= ((GPIO_MODE_OUTPUT_PP & 0x00000003u) << (5 * 2u));
GPIOA->MODER = temp;
}
void Error_Handler(void)
{