stm32g0xx/Core/main.cc

157 lines
4.5 KiB
C++
Raw Normal View History

2021-01-08 10:58:59 +00:00
#include <cstring>
#include <cstdint>
#include <cstdio>
2021-01-08 10:58:59 +00:00
#include "delay.h"
2020-12-17 09:39:45 +00:00
#include "platform/stm32g0xx/Gpio.h"
2021-01-08 09:21:58 +00:00
#include "platform/stm32g0xx/Uart.h"
2021-01-08 09:50:38 +00:00
#include "platform/stm32g0xx/IndependentWatchdog.h"
2021-01-08 09:50:38 +00:00
// NOTE! The independent watchdog is clocked by a separate clock. this one
// isn't controlled by JTAG. That's why the independent watchdog needs to
// be disabled during JTAG debug sessions.
#define JTAG_DEBUG
2020-12-17 09:51:20 +00:00
using namespace perinet::platform::stm32g0xx;
2021-05-28 09:06:00 +00:00
Gpio green_led(Gpio::Port::PORT_C, 6, Gpio::Mode::MODE_OUTPUT_PP);
Uart uart1(Uart::UartDevice::UART_1, 115200);
Uart uart2(Uart::UartDevice::UART_2, 115200);
2021-01-08 09:21:58 +00:00
2021-01-08 09:50:38 +00:00
#ifndef JTAG_DEBUG
IndependentWatchdog watchdog(4095, 4095);
#endif
int main(void)
{
unsigned int i = 1, j = 40;
2021-01-08 09:21:58 +00:00
char tx_buf[80];
2021-01-08 09:50:38 +00:00
#ifndef JTAG_DEBUG
watchdog.enable();
#endif
while (1) {
if (j < 100) {
j += 10;
}
else if (j < 200) {
j += 20;
}
else if (j < 400) {
j += 40;
}
if (j > 800) {
j = 800;
}
2021-01-08 09:21:58 +00:00
sprintf(tx_buf, "%u: Hello World\r\n", i++);
2021-05-28 09:06:00 +00:00
uart1.sync_send((const uint8_t *)tx_buf, strlen(tx_buf));
uart2.sync_send((const uint8_t *)tx_buf, strlen(tx_buf));
2020-12-17 09:39:45 +00:00
green_led.toggle();
2021-01-08 10:58:59 +00:00
delay_ms(j);
2021-01-08 09:50:38 +00:00
#ifndef JTAG_DEBUG
watchdog.trigger();
#endif
}
2021-05-28 09:06:00 +00:00
#if 0
// 1:
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
//HAL_Init();
/* Configure Flash prefetch, Instruction cache */
/* Default configuration at reset is: */
/* - Prefetch disabled */
/* - Instruction cache enabled */
#if (INSTRUCTION_CACHE_ENABLE == 0U)
__HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
#endif /* INSTRUCTION_CACHE_ENABLE */
#if (PREFETCH_ENABLE != 0U)
__HAL_FLASH_PREFETCH_BUFFER_ENABLE();
#endif /* PREFETCH_ENABLE */
/* Use SysTick as time base source and configure 1ms tick (default clock after Reset is HSI) */
HAL_InitTick(TICK_INT_PRIORITY);
PWR_PVDTypeDef sConfigPVD = {0};
__HAL_RCC_SYSCFG_CLK_ENABLE();
__HAL_RCC_PWR_CLK_ENABLE();
/* System interrupt init*/
/** PVD Configuration*/
sConfigPVD.Mode = PWR_PVD_MODE_NORMAL;
HAL_PWR_ConfigPVD(&sConfigPVD);
/** Enable the PVD Output*/
HAL_PWR_EnablePVD();
// 2:
/* Configure the system clock */
//SystemClock_Config();
// 3:
/* Initialize all configured peripherals */
//MX_GPIO_Init();
// 4:
//MX_DMA_Init();
/* DMA controller clock enable */
__HAL_RCC_DMA1_CLK_ENABLE();
/* DMA interrupt init */
/* DMA1_Channel1_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
/* DMA1_Channel2_3_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Channel2_3_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel2_3_IRQn);
// 5:
//MX_USART1_UART_Init();
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
{
Error_Handler();
}
//6:
//uint8_t buf[] = "Hello World\r\n";
//HAL_UART_Transmit_DMA(&huart1, buf, strlen((const char *)buf));
//HAL_UART_Receive_DMA(&huart1, buf, strlen((const char *)buf));
#endif
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
}
#endif /* USE_FULL_ASSERT */