Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* This notice applies to any and all portions of this file
* that are not between comment pairs USER CODE BEGIN and
* USER CODE END. Other portions of this file, whether
* inserted by the user or by software development tools
* are owned by their respective copyright owners.
*
* Copyright (c) 2018 STMicroelectronics International N.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted, provided that the following conditions are met:
*
* 1. Redistribution of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of other
* contributors to this software may be used to endorse or promote products
* derived from this software without specific written permission.
* 4. This software, including modifications and/or derivative works of this
* software, must execute solely and exclusively on microcontroller or
- * microprocessor devices manufactured by or for STMicroelectronics.
* 5. Redistribution and use of this software other than as permitted under
* this license is void and will automatically terminate your rights under
* this license.
*
* THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
* RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
* SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f0xx_hal.h"
#include "usb_device.h"
/* USER CODE BEGIN Includes */
#include "slcan.h"
#include "lin_slcan.h"
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
void MX_USART1_UART_Init(void);
void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
const uint32_t *uid = (uint32_t *)(UID_BASE);
uint32_t serialNumber = 0;
uint8_t Uart2RxFifo;
uint32_t lin_baund_rate = 19200;
#define UART_RX_FIFO_SIZE 1
/* USER CODE END 0 */
/**
* @brief The application entry point.
*
* @retval None
*/
void bootloaderSwitcher();
//#define EUART_TX_PORT GPIOA
//#define EUART_TX_PIN GPIO_PIN_8
//
//void emulated_uart_init()
//{
// HAL_GPIO_WritePin(EUART_TX_PORT,EUART_TX_PIN,1);
//}
//#define DELAY_EMUL ;
//#define DELAY_START_BIT ;
//#define DELAY_STOP_BIT ;
//void emulated_uart_send_byte(uint8_t b)
//{
// HAL_GPIO_WritePin(EUART_TX_PORT,EUART_TX_PIN,0);
// DELAY_START_BIT;
// HAL_GPIO_WritePin(EUART_TX_PORT,EUART_TX_PIN,b & 0x01);
// DELAY_EMUL;
// HAL_GPIO_WritePin(EUART_TX_PORT,EUART_TX_PIN,b & 0x02);
// DELAY_EMUL;
// HAL_GPIO_WritePin(EUART_TX_PORT,EUART_TX_PIN,b & 0x04);
// DELAY_EMUL;
// HAL_GPIO_WritePin(EUART_TX_PORT,EUART_TX_PIN,b & 0x08);
// DELAY_EMUL;
// HAL_GPIO_WritePin(EUART_TX_PORT,EUART_TX_PIN,b & 0x10);
// DELAY_EMUL;
// HAL_GPIO_WritePin(EUART_TX_PORT,EUART_TX_PIN,b & 0x20);
// DELAY_EMUL;
// HAL_GPIO_WritePin(EUART_TX_PORT,EUART_TX_PIN,b & 0x40);
// DELAY_EMUL;
// HAL_GPIO_WritePin(EUART_TX_PORT,EUART_TX_PIN,b & 0x80);
// DELAY_EMUL;
//
// HAL_GPIO_WritePin(EUART_TX_PORT,EUART_TX_PIN,1);
// DELAY_STOP_BIT;
//}
//
//void emulated_uart_send_buffer(uint8_t* buff, uint8_t len)
//{
// for (uint8_t i = 0; i < len; i++)
// {
// emulated_uart_send_byte(buff[i]);
// }
//}
//uint8_t test[] = {"lolo\r\n"};
int main(void)
{
serialNumber = uid[0] ^ uid[1] ^ uid[2];
/* USER CODE BEGIN 1 */
bootloaderSwitcher();
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all periph
* erals, 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 */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
MX_USB_DEVICE_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&huart1, &Uart2RxFifo, UART_RX_FIFO_SIZE);
// HAL_NVIC_SetPriority(USART1_IRQn, 2, 0);
NVIC_EnableIRQ(USART1_IRQn);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
lin_type = LIN_MONITOR;
slcan_state = SLCAN_STATE_OPEN;
open_lin_hw_reset();
lin_slcan_reset();
while (1)
{
slCanCheckCommand();
/* USER CODE END WHILE */
// if (sendbytes_len > 0)
{
// emulated_uart_send_buffer(sendbuff,sendbytes_len);
// HAL_UART_Transmit(&huart2,test,sizeof(test),1000);
// HAL_UART_Transmit(&huart2,sendbuff,sendbytes_len,1000);
// HAL_UART_Transmit(&huart2,sl_frame,sendbytes_len,1000);
// sendbytes_len = 0;
}
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit;
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48;
RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
// _Error_Handler(__FILE__, __LINE__);
}
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI48;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
// _Error_Handler(__FILE__, __LINE__);
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB|RCC_PERIPHCLK_USART1;
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
// _Error_Handler(__FILE__, __LINE__);
}
/**Configure the Systick interrupt time
*/
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
/**Configure the Systick
*/
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
/* USART1 init function */
void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = lin_baund_rate;
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.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_LIN_Init(&huart1, UART_LINBREAKDETECTLENGTH_11B) != HAL_OK)
{
// _Error_Handler(__FILE__, __LINE__);
}
}
void MX_USART2_UART_Init(void)
{
/* USER CODE BEGIN USART2_Init 0 */
/* USER CODE END USART2_Init 0 */
/* USER CODE BEGIN USART2_Init 1 */
/* USER CODE END USART2_Init 1 */
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART2_Init 2 */
/* USER CODE END USART2_Init 2 */
}
/** Configure pins as
* Analog
* Input
* Output
* EVENT_OUT
* EXTI
*/
static void MX_GPIO_Init(void)
{
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
// GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
// __HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin : PA13 */
// GPIO_InitStruct.Pin = GPIO_PIN_13;
// GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
// GPIO_InitStruct.Pull = GPIO_PULLUP;
// HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
extern LinType_t lin_type;
uint8_t rbyte = Uart2RxFifo;
if (slcan_state == SLCAN_STATE_OPEN)
{
switch (lin_type)
{
case LIN_MASTER:
open_lin_master_dl_rx(rbyte);
break;
case LIN_SLAVE:
open_lin_slave_rx_header(rbyte);
default: /* Monitor */
lin_slcan_rx(rbyte);
break;
}
}
HAL_UART_Receive_IT(huart, &Uart2RxFifo, 1);
}
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
{
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @param file: The file name as string.
* @param line: The line in file as a number.
* @retval None
*/
void _Error_Handler(char *file, int line)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
while(1)
{
}
/* 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)
{
/* 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) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
49eaee9a5This file is pinned to the selected release. Download the original to open it in your design tool.