You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
5.7 KiB
157 lines
5.7 KiB
/**
|
|
******************************************************************************
|
|
* File Name : USART.c
|
|
* Description : This file provides code for the configuration
|
|
* of the USART instances.
|
|
******************************************************************************
|
|
* 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 "usart.h"
|
|
#include <stdbool.h>
|
|
|
|
#include "gpio.h"
|
|
#include "dma.h"
|
|
|
|
/* USER CODE BEGIN 0 */
|
|
|
|
/* USER CODE END 0 */
|
|
|
|
/* USART1 init function */
|
|
|
|
void MX_USART1_UART_Init(void)
|
|
{
|
|
LL_USART_InitTypeDef USART_InitStruct;
|
|
|
|
LL_GPIO_InitTypeDef GPIO_InitStruct;
|
|
/* Peripheral clock enable */
|
|
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1);
|
|
|
|
/**USART1 GPIO Configuration
|
|
PA9 ------> USART1_TX
|
|
PA10 ------> USART1_RX
|
|
*/
|
|
GPIO_InitStruct.Pin = LL_GPIO_PIN_9;
|
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
|
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
|
|
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
|
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
// we don't use Rx
|
|
// GPIO_InitStruct.Pin = LL_GPIO_PIN_10;
|
|
// GPIO_InitStruct.Mode = LL_GPIO_MODE_FLOATING;
|
|
// LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
/* USART1 DMA Init */
|
|
|
|
/* USART1_TX Init */
|
|
LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_4, LL_DMA_DIRECTION_MEMORY_TO_PERIPH);
|
|
|
|
LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_4, LL_DMA_PRIORITY_LOW);
|
|
|
|
LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_4, LL_DMA_MODE_NORMAL);
|
|
|
|
LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_4, LL_DMA_PERIPH_NOINCREMENT);
|
|
|
|
LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_4, LL_DMA_MEMORY_INCREMENT);
|
|
|
|
LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_4, LL_DMA_PDATAALIGN_BYTE);
|
|
|
|
LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_4, LL_DMA_MDATAALIGN_BYTE);
|
|
|
|
// the USART address
|
|
LL_DMA_SetPeriphAddress(DMA1, LL_DMA_CHANNEL_4, LL_USART_DMA_GetRegAddr(USART1));
|
|
|
|
USART_InitStruct.BaudRate = 115200;
|
|
USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
|
|
USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
|
|
USART_InitStruct.Parity = LL_USART_PARITY_NONE;
|
|
USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX;
|
|
USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
|
|
LL_USART_Init(USART1, &USART_InitStruct);
|
|
|
|
LL_USART_ConfigAsyncMode(USART1);
|
|
|
|
LL_USART_Enable(USART1);
|
|
}
|
|
|
|
|
|
/* USER CODE BEGIN 1 */
|
|
static volatile bool usart_dma_first = true;
|
|
/** Wait for the last DMA request to finish - so the buffer can be re-filled with new content */
|
|
void MX_USART_DmaWaitReady(void)
|
|
{
|
|
if (!usart_dma_first) {
|
|
// wait for the last transfer to finish
|
|
while (!LL_DMA_IsActiveFlag_TC4(DMA1));
|
|
}
|
|
|
|
LL_USART_DisableDMAReq_TX(USART1);
|
|
}
|
|
|
|
/** Send new content using DMA */
|
|
void MX_USART_DmaSend(uint8_t *buffer, uint32_t length)
|
|
{
|
|
usart_dma_first = false;
|
|
|
|
LL_USART_EnableDMAReq_TX(USART1);
|
|
|
|
LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_4);
|
|
LL_DMA_ClearFlag_TC4(DMA1);
|
|
LL_DMA_SetMemoryAddress(DMA1, LL_DMA_CHANNEL_4, (uint32_t) buffer);
|
|
LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_4, length);
|
|
LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_4);
|
|
}
|
|
|
|
/* USER CODE END 1 */
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
|
|