vp-digi/Core/Src/main.c

408 wiersze
10 KiB
C
Czysty Zwykły widok Historia

2021-09-10 09:30:51 +00:00
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
2021-09-10 09:30:51 +00:00
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
2021-09-10 09:30:51 +00:00
*
******************************************************************************
*/
/*
Copyright 2020-2023 Piotr Wilkon
2021-09-10 09:30:51 +00:00
This file is part of VP-Digi.
VP-Digi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
VP-Digi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with VP-Digi. If not, see <http://www.gnu.org/licenses/>.
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usb_device.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdint.h>
2023-09-08 08:51:54 +00:00
#include "systick.h"
#include "modem.h"
2021-09-10 09:30:51 +00:00
#include "ax25.h"
#include "digipeater.h"
#include "common.h"
#include "drivers/watchdog.h"
#include "beacon.h"
#include "terminal.h"
#include "config.h"
2023-09-08 08:51:54 +00:00
#include "uart.h"
#include "drivers/usb.h"
2023-09-12 09:16:22 +00:00
#include "kiss.h"
2023-08-25 09:56:06 +00:00
#ifdef ENABLE_FX25
#include "fx25.h"
#endif
2021-09-10 09:30:51 +00:00
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
2021-09-10 09:30:51 +00:00
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/**
2023-08-17 12:57:29 +00:00
* @brief Handle received frame
2021-09-10 09:30:51 +00:00
*/
2023-08-17 12:57:29 +00:00
static void handleFrame(void)
2021-09-10 09:30:51 +00:00
{
2023-08-17 12:57:29 +00:00
uint8_t modemBitmap = Ax25GetReceivedFrameBitmap(); //store states
Ax25ClearReceivedFrameBitmap();
2021-09-10 09:30:51 +00:00
2023-08-18 10:50:33 +00:00
uint8_t *buf;
2023-08-17 12:57:29 +00:00
uint16_t size = 0;
2023-08-30 14:26:23 +00:00
int8_t peak = 0;
int8_t valley = 0;
uint8_t signalLevel = 0;
2023-08-30 14:25:37 +00:00
uint8_t fixed = 0;
2021-09-10 09:30:51 +00:00
2023-08-30 14:26:23 +00:00
while(Ax25ReadNextRxFrame(&buf, &size, &peak, &valley, &signalLevel, &fixed))
2021-09-10 09:30:51 +00:00
{
2023-08-17 12:57:29 +00:00
TermSendToAll(MODE_KISS, buf, size);
2021-09-10 09:30:51 +00:00
2023-08-17 12:57:29 +00:00
if(((UartUsb.mode == MODE_MONITOR) || (Uart1.mode == MODE_MONITOR) || (Uart2.mode == MODE_MONITOR)))
2021-09-10 09:30:51 +00:00
{
2023-08-30 14:26:23 +00:00
if(signalLevel > 70)
2023-08-17 12:57:29 +00:00
{
2023-08-30 14:26:23 +00:00
TermSendToAll(MODE_MONITOR, (uint8_t*)"\r\nInput level too high! Please reduce so most stations are around 30-50%.\r\n", 0);
2023-08-17 12:57:29 +00:00
}
2023-08-30 14:26:23 +00:00
else if(signalLevel < 5)
2023-08-17 12:57:29 +00:00
{
2023-08-30 14:26:23 +00:00
TermSendToAll(MODE_MONITOR, (uint8_t*)"\r\nInput level too low! Please increase so most stations are around 30-50%.\r\n", 0);
2023-08-17 12:57:29 +00:00
}
2023-08-30 14:25:37 +00:00
TermSendToAll(MODE_MONITOR, (uint8_t*)"(AX.25) Frame received [", 0);
2023-08-22 11:32:07 +00:00
for(uint8_t i = 0; i < ModemGetDemodulatorCount(); i++)
2023-08-17 12:57:29 +00:00
{
if(modemBitmap & (1 << i))
{
2023-08-22 11:32:07 +00:00
enum ModemPrefilter m = ModemGetFilterType(i);
2023-08-17 12:57:29 +00:00
switch(m)
{
2023-08-22 11:32:07 +00:00
case PREFILTER_PREEMPHASIS:
2023-08-17 12:57:29 +00:00
TermSendToAll(MODE_MONITOR, (uint8_t*)"P", 1);
break;
2023-08-22 11:32:07 +00:00
case PREFILTER_DEEMPHASIS:
2023-08-17 12:57:29 +00:00
TermSendToAll(MODE_MONITOR, (uint8_t*)"D", 1);
break;
2023-08-22 11:32:07 +00:00
case PREFILTER_FLAT:
TermSendToAll(MODE_MONITOR, (uint8_t*)"F", 1);
2023-08-18 10:50:33 +00:00
break;
2023-08-30 14:26:23 +00:00
case PREFILTER_NONE:
2023-08-31 16:55:03 +00:00
TermSendToAll(MODE_MONITOR, (uint8_t*)"N", 1);
2023-08-17 12:57:29 +00:00
}
}
else
TermSendToAll(MODE_MONITOR, (uint8_t*)"_", 1);
}
2023-08-30 14:26:23 +00:00
TermSendToAll(MODE_MONITOR, (uint8_t*)"], ", 0);
if(fixed != AX25_NOT_FX25)
{
TermSendNumberToAll(MODE_MONITOR, fixed);
TermSendToAll(MODE_MONITOR, (uint8_t*)" bytes fixed, ", 0);
}
TermSendToAll(MODE_MONITOR, (uint8_t*)"signal level ", 0);
2023-08-17 12:57:29 +00:00
TermSendNumberToAll(MODE_MONITOR, signalLevel);
2023-08-30 14:26:23 +00:00
TermSendToAll(MODE_MONITOR, (uint8_t*)"% (", 0);
TermSendNumberToAll(MODE_MONITOR, peak);
TermSendToAll(MODE_MONITOR, (uint8_t*)"%/", 0);
TermSendNumberToAll(MODE_MONITOR, valley);
TermSendToAll(MODE_MONITOR, (uint8_t*)"%): ", 0);
2023-08-17 12:57:29 +00:00
SendTNC2(buf, size);
TermSendToAll(MODE_MONITOR, (uint8_t*)"\r\n", 0);
2021-09-10 09:30:51 +00:00
}
2023-08-17 12:57:29 +00:00
DigiDigipeat(buf, size);
2021-09-10 09:30:51 +00:00
}
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
2023-08-17 12:57:29 +00:00
SysTickInit();
2021-09-10 09:30:51 +00:00
2023-09-08 08:51:54 +00:00
USB_FORCE_REENUMERATION();
2021-09-10 09:30:51 +00:00
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USB_DEVICE_Init();
/* USER CODE BEGIN 2 */
2023-09-11 08:25:08 +00:00
WdogInit(); //initialize watchdog
2021-09-10 09:30:51 +00:00
2023-08-17 12:57:29 +00:00
memset(&beacon, 0, sizeof(beacon));
memset(&Ax25Config, 0, sizeof(Ax25Config));
memset(&ModemConfig, 0, sizeof(ModemConfig));
memset(&DigiConfig, 0, sizeof(DigiConfig));
2021-09-10 09:30:51 +00:00
//set some initial values in case there is no configuration saved in memory
2023-08-17 12:57:29 +00:00
Uart1.baudrate = 9600;
2023-09-08 08:51:54 +00:00
Uart1.defaultMode = MODE_KISS;
2023-08-17 12:57:29 +00:00
Uart2.baudrate = 9600;
2023-09-08 08:51:54 +00:00
Uart2.defaultMode = MODE_KISS;
UartUsb.defaultMode = MODE_KISS;
2023-08-30 20:52:16 +00:00
ModemConfig.usePWM = 1; //use PWM by default
2023-08-17 12:57:29 +00:00
ModemConfig.flatAudioIn = 0;
Ax25Config.quietTime = 300;
Ax25Config.txDelayLength = 300;
Ax25Config.txTailLength = 30;
2023-08-30 14:25:37 +00:00
Ax25Config.fx25 = 0;
2023-08-17 12:57:29 +00:00
DigiConfig.dupeTime = 30;
2021-09-10 09:30:51 +00:00
2023-08-17 12:57:29 +00:00
ConfigRead();
2021-09-10 09:30:51 +00:00
2023-08-30 14:52:27 +00:00
ModemInit();
2023-08-17 12:57:29 +00:00
Ax25Init();
2023-08-25 09:56:06 +00:00
#ifdef ENABLE_FX25
Fx25Init();
#endif
2021-09-10 09:30:51 +00:00
2023-09-08 08:51:54 +00:00
UartInit(&Uart1, UART_LL_UART1_STRUCTURE, Uart1.baudrate);
UartInit(&Uart2, UART_LL_UART2_STRUCTURE, Uart2.baudrate);
2023-08-17 12:57:29 +00:00
UartInit(&UartUsb, NULL, 1);
2021-09-10 09:30:51 +00:00
2023-08-17 12:57:29 +00:00
UartConfig(&Uart1, 1);
UartConfig(&Uart2, 1);
UartConfig(&UartUsb, 1);
2021-09-10 09:30:51 +00:00
2023-08-17 12:57:29 +00:00
BeaconInit();
2021-09-10 09:30:51 +00:00
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
2022-08-12 13:27:44 +00:00
/* USER CODE BEGIN 3 */
2023-09-11 08:25:08 +00:00
WdogReset();
2021-09-10 09:30:51 +00:00
2023-08-17 12:57:29 +00:00
if(Ax25GetReceivedFrameBitmap())
2021-09-10 09:30:51 +00:00
handleFrame();
2023-08-17 12:57:29 +00:00
DigiViscousRefresh(); //refresh viscous-delay buffers
Ax25TransmitBuffer(); //transmit buffer (will return if nothing to be transmitted)
Ax25TransmitCheck(); //check for pending transmission request
if(UartUsb.rxType != DATA_NOTHING)
2021-09-10 09:30:51 +00:00
{
2023-08-18 10:50:33 +00:00
TermHandleSpecial(&UartUsb);
2023-09-12 09:16:22 +00:00
if(UartUsb.rxType == DATA_KISS)
2023-08-18 10:50:33 +00:00
{
2023-09-12 09:16:22 +00:00
KissProcess(&UartUsb);
}
else if(UartUsb.rxType != DATA_USB)
{
2023-08-18 10:50:33 +00:00
TermParse(&UartUsb);
UartClearRx(&UartUsb);
}
UartUsb.rxType = DATA_NOTHING;
2021-09-10 09:30:51 +00:00
}
2023-08-17 12:57:29 +00:00
if(Uart1.rxType != DATA_NOTHING)
2021-09-10 09:30:51 +00:00
{
2023-09-12 09:16:22 +00:00
if(Uart1.rxType == DATA_KISS)
{
KissProcess(&Uart1);
}
else
{
TermParse(&Uart1);
UartClearRx(&Uart1);
}
2021-09-10 09:30:51 +00:00
}
2023-08-17 12:57:29 +00:00
if(Uart2.rxType != DATA_NOTHING)
2021-09-10 09:30:51 +00:00
{
2023-09-12 09:16:22 +00:00
if(Uart2.rxType == DATA_KISS)
{
KissProcess(&Uart2);
}
else
{
TermParse(&Uart2);
UartClearRx(&Uart2);
}
2021-09-10 09:30:51 +00:00
}
2023-08-17 12:57:29 +00:00
BeaconCheck(); //check beacons
2021-09-10 09:30:51 +00:00
2023-08-25 09:56:06 +00:00
if(SysTickGet() > 0xFFFFF000) //going to wrap around soon - hard reset the device
2021-09-10 09:30:51 +00:00
NVIC_SystemReset();
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
2021-09-10 09:30:51 +00:00
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
2021-09-10 09:30:51 +00:00
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
2021-09-10 09:30:51 +00:00
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
2021-09-10 09:30:51 +00:00
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
2021-09-10 09:30:51 +00:00
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
2022-08-12 13:27:44 +00:00
{
2021-09-10 09:30:51 +00:00
/* USER CODE BEGIN 6 */
/* 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) */
2021-09-10 09:30:51 +00:00
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */