Implement uart bridge application

This commit is contained in:
Thomas Klaehn 2021-06-09 13:34:48 +02:00
parent 46382e2cf7
commit 103cbe1d6a
3 changed files with 20 additions and 136 deletions

View File

@ -5,152 +5,25 @@
#include "delay.h" #include "delay.h"
#include "platform/stm32g0xx/Gpio.h" #include "platform/stm32g0xx/Gpio.h"
#include "platform/stm32g0xx/Uart.h" #include "platform/stm32g0xx/Uart.h"
#include "platform/stm32g0xx/IndependentWatchdog.h"
// 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
using namespace perinet::platform::stm32g0xx; using namespace perinet::platform::stm32g0xx;
Gpio green_led(Gpio::Port::PORT_C, 6, Gpio::Mode::MODE_OUTPUT_PP);
Uart uart1(Uart::UartDevice::UART_1, 115200); Uart uart1(Uart::UartDevice::UART_1, 115200);
Uart uart2(Uart::UartDevice::UART_2, 115200); Uart uart2(Uart::UartDevice::UART_2, 115200);
#ifndef JTAG_DEBUG
IndependentWatchdog watchdog(4095, 4095);
#endif
int main(void) int main(void)
{ {
unsigned int i = 1, j = 40; char tx_buf[] = "\r\rProgram: UART bridge\r\n\n Receive on uart2 and transmit it on uart 1\r\n";
char tx_buf[80]; uint8_t rec;
#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;
}
sprintf(tx_buf, "%u: Hello World\r\n", i++);
uart1.sync_send((const uint8_t *)tx_buf, strlen(tx_buf)); uart1.sync_send((const uint8_t *)tx_buf, strlen(tx_buf));
uart2.sync_send((const uint8_t *)tx_buf, strlen(tx_buf));
green_led.toggle();
delay_ms(j);
#ifndef JTAG_DEBUG while(true) {
watchdog.trigger(); if(uart2.sync_receive(&rec)) {
#endif uart1.sync_send(&rec, 1);
} }
#if 0 if(uart1.sync_receive(&rec)) {
// 1: uart2.sync_send(&rec, 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 */

View File

@ -72,3 +72,13 @@ void Uart::sync_send(const uint8_t *buffer, uint32_t len)
uart->TDR = buffer[i]; uart->TDR = buffer[i];
} }
} }
bool Uart::sync_receive(uint8_t* res)
{
if((uart->ISR & USART_ISR_RXNE_RXFNE) == 0) {
return false;
}
*res = (uint8_t)(uart->RDR);
return true;
}

View File

@ -18,6 +18,7 @@ public:
Uart(UartDevice, uint32_t); Uart(UartDevice, uint32_t);
void sync_send(const uint8_t *, uint32_t); void sync_send(const uint8_t *, uint32_t);
bool sync_receive(uint8_t*);
private: private:
USART_TypeDef * uart; USART_TypeDef * uart;