Commit 95fa931b authored by Sergi Hernandez's avatar Sergi Hernandez
Browse files

Used the UART data structgures instead of the USART ones.

parent 04dadcb6
#ifndef USART1_F1_H
#define USART1_F1_H
#ifndef UART1_F1_H
#define UART1_F1_H
#include "stm32f1xx_hal.h"
#include "usart_common.h"
#include "comm.h"
/* public functions */
void usart1_init(TComm *comm_dev,USART_InitTypeDef *conf,TUSART_IRQ_Priorities *priorities);
void usart1_config(TComm *comm_dev,USART_InitTypeDef *conf);
void usart1_set_priorities(TComm *comm_dev,TUSART_IRQ_Priorities *priorities);
void usart1_init(TComm *comm_dev,UART_InitTypeDef *conf,TUART_IRQ_Priorities *priorities);
void usart1_config(TComm *comm_dev,UART_InitTypeDef *conf);
void usart1_set_priorities(TComm *comm_dev,TUART_IRQ_Priorities *priorities);
void usart1_set_baudrate(TComm *comm_dev,unsigned int baudrate);
/* IRQ functions */
unsigned char usart1_send_irq(unsigned char first_byte);
......
#ifndef USART2_F1_H
#define USART2_F1_H
#ifndef UART2_F1_H
#define UART2_F1_H
#include "stm32f1xx_hal.h"
#include "usart_common.h"
#include "comm.h"
/* public functions */
void usart2_init(TComm *comm_dev,USART_InitTypeDef *conf,TUSART_IRQ_Priorities *priorities);
void usart2_config(TComm *comm_dev,USART_InitTypeDef *conf);
void usart2_set_priorities(TComm *comm_dev,TUSART_IRQ_Priorities *priorities);
void usart2_init(TComm *comm_dev,UART_InitTypeDef *conf,TUART_IRQ_Priorities *priorities);
void usart2_config(TComm *comm_dev,UART_InitTypeDef *conf);
void usart2_set_priorities(TComm *comm_dev,TUART_IRQ_Priorities *priorities);
void usart2_set_baudrate(TComm *comm_dev,unsigned int baudrate);
/* IRQ functions */
unsigned char usart2_send_irq(unsigned char first_byte);
......
#ifndef USART3_F1_H
#define USART3_F1_H
#ifndef UART3_F1_H
#define UART3_F1_H
#ifdef __cplusplus
extern "C" {
......@@ -11,22 +11,20 @@ extern "C" {
/* public functions */
void usart3_init(TComm *comm_dev,USART_InitTypeDef *conf,TUSART_IRQ_Priorities *priorities);
void usart3_config(TComm *comm_dev,USART_InitTypeDef *conf);
void usart3_set_priorities(TComm *comm_dev,TUSART_IRQ_Priorities *priorities);
void usart3_init(TComm *comm_dev,UART_InitTypeDef *conf,TUART_IRQ_Priorities *priorities);
void usart3_config(TComm *comm_dev,UART_InitTypeDef *conf);
void usart3_set_priorities(TComm *comm_dev,TUART_IRQ_Priorities *priorities);
void usart3_set_baudrate(TComm *comm_dev,unsigned int baudrate);
/* IRQ functions */
unsigned char usart3_send_irq(unsigned char first_byte);
unsigned char usart3_enable_tx_irq(void);
unsigned char usart3_receive_irq(void);
unsigned char usart3_cancel_receive_irq(void);
void USART_IRQHandler(void);
void UART_IRQHandler(void);
/* DMA functions */
unsigned char usart3_send_dma(unsigned char *data,unsigned short int length);
unsigned char usart3_receive_dma(unsigned char *data,unsigned short int length);
unsigned char usart3_cancel_receive_dma(void);
void USART_DMA_TX_IRQHandler(void);
void USART_DMA_RX_IRQHandler(void);
#ifdef __cplusplus
}
......
#ifndef USART_COMMON_F1_H
#define USART_COMMON_F1_H
#ifndef UART_COMMON_F1_H
#define UART_COMMON_F1_H
typedef struct
{
......@@ -9,6 +9,6 @@ typedef struct
unsigned char dma_rx_subpriority;
unsigned char dma_tx_priority;
unsigned char dma_tx_subpriority;
}TUSART_IRQ_Priorities;
}TUART_IRQ_Priorities;
#endif
#include "usart1.h"
#define USART USART1
#define USART_ENABLE_CLK __HAL_RCC_USART1_CLK_ENABLE()
#define USART_IRQn USART1_IRQn
#define USART_IRQHandler USART1_IRQHandler
#define UART USART1
#define UART_ENABLE_CLK __HAL_RCC_USART1_CLK_ENABLE()
#define UART_IRQn USART1_IRQn
#define UART_IRQHandler USART1_IRQHandler
#define USART_TX_PIN GPIO_PIN_9
#define USART_TX_GPIO_PORT GPIOA
#define USART_ENABLE_TX_GPIO_CLK __HAL_RCC_GPIOA_CLK_ENABLE()
#define UART_TX_PIN GPIO_PIN_9
#define UART_TX_GPIO_PORT GPIOA
#define UART_ENABLE_TX_GPIO_CLK __HAL_RCC_GPIOA_CLK_ENABLE()
#define USART_RX_PIN GPIO_PIN_10
#define USART_RX_GPIO_PORT GPIOA
#define USART_ENABLE_RX_GPIO_CLK __HAL_RCC_GPIOA_CLK_ENABLE()
#define UART_RX_PIN GPIO_PIN_10
#define UART_RX_GPIO_PORT GPIOA
#define UART_ENABLE_RX_GPIO_CLK __HAL_RCC_GPIOA_CLK_ENABLE()
/* DMA configuration */
#define USART_DMA DMA1
#define USART_ENABLE_DMA_CLK __HAL_RCC_DMA1_CLK_ENABLE()
#define UART_DMA DMA1
#define UART_ENABLE_DMA_CLK __HAL_RCC_DMA1_CLK_ENABLE()
#define USART_TX_DMA_CHANNEL DMA1_Channel4
#define USART_RX_DMA_CHANNEL DMA1_Channel5
#define UART_TX_DMA_CHANNEL DMA1_Channel4
#define UART_RX_DMA_CHANNEL DMA1_Channel5
#define USART_DMA_TX_IRQn DMA1_Channel4_IRQn
#define USART_DMA_RX_IRQn DMA1_Channel5_IRQn
#define USART_DMA_TX_IRQHandler DMA1_Channel4_IRQHandler
#define USART_DMA_RX_IRQHandler DMA1_Channel5_IRQHandler
#define UART_DMA_TX_IRQn DMA1_Channel4_IRQn
#define UART_DMA_RX_IRQn DMA1_Channel5_IRQn
#define UART_DMA_TX_IRQHandler DMA1_Channel4_IRQHandler
#define UART_DMA_RX_IRQHandler DMA1_Channel5_IRQHandler
// private variables
USART_HandleTypeDef Uart1Handle;
UART_HandleTypeDef Uart1Handle;
DMA_HandleTypeDef usart1_hdma_tx;
DMA_HandleTypeDef usart1_hdma_rx;
TComm *usart1_comm_dev;
// interrupt handlers
void USART_IRQHandler(void)
void UART_IRQHandler(void)
{
unsigned char data,ret;
uint32_t source;
if(__HAL_USART_GET_FLAG(&Uart1Handle, USART_FLAG_RXNE) != RESET)
if(__HAL_UART_GET_FLAG(&Uart1Handle, UART_FLAG_RXNE) != RESET)
{
if(__HAL_USART_GET_IT_SOURCE(&Uart1Handle, USART_IT_RXNE) !=RESET)
if(__HAL_UART_GET_IT_SOURCE(&Uart1Handle, UART_IT_RXNE) !=RESET)
{
__HAL_USART_CLEAR_FLAG(&Uart1Handle,USART_FLAG_RXNE);
__HAL_UART_CLEAR_FLAG(&Uart1Handle,UART_FLAG_RXNE);
data=(uint8_t)(Uart1Handle.Instance->DR & (uint8_t)0x00FF);
// call the reception function
if(!comm_do_irq_receive(usart1_comm_dev,data))
__HAL_USART_DISABLE_IT(&Uart1Handle, USART_IT_RXNE);
__HAL_UART_DISABLE_IT(&Uart1Handle, UART_IT_RXNE);
}
}
if(__HAL_USART_GET_FLAG(&Uart1Handle, USART_FLAG_TC) != RESET)
if(__HAL_UART_GET_FLAG(&Uart1Handle, UART_FLAG_TC) != RESET)
{
if(__HAL_USART_GET_IT_SOURCE(&Uart1Handle, USART_IT_TC) !=RESET)
if(__HAL_UART_GET_IT_SOURCE(&Uart1Handle, UART_IT_TC) !=RESET)
{
__HAL_USART_CLEAR_FLAG(&Uart1Handle,USART_FLAG_TC);
__HAL_UART_CLEAR_FLAG(&Uart1Handle,UART_FLAG_TC);
ret=comm_do_irq_send(usart1_comm_dev,&data);
if(ret==0x01)
Uart1Handle.Instance->DR=data;
else if(ret==0x00)
__HAL_USART_DISABLE_IT(&Uart1Handle, USART_IT_TC);
__HAL_UART_DISABLE_IT(&Uart1Handle, UART_IT_TC);
}
}
if(__HAL_USART_GET_FLAG(&Uart1Handle, USART_FLAG_PE) != RESET)// parity error
if(__HAL_UART_GET_FLAG(&Uart1Handle, UART_FLAG_PE) != RESET)// parity error
{
if(__HAL_USART_GET_IT_SOURCE(&Uart1Handle, USART_IT_PE) !=RESET)
if(__HAL_UART_GET_IT_SOURCE(&Uart1Handle, UART_IT_PE) !=RESET)
{
__HAL_USART_CLEAR_PEFLAG(&Uart1Handle);
__HAL_UART_CLEAR_PEFLAG(&Uart1Handle);
}
}
source=__HAL_USART_GET_IT_SOURCE(&Uart1Handle, USART_IT_ERR);
if(__HAL_USART_GET_FLAG(&Uart1Handle, USART_FLAG_FE) != RESET)// frame error
source=__HAL_UART_GET_IT_SOURCE(&Uart1Handle, UART_IT_ERR);
if(__HAL_UART_GET_FLAG(&Uart1Handle, UART_FLAG_FE) != RESET)// frame error
{
if(source !=RESET)
{
__HAL_USART_CLEAR_FEFLAG(&Uart1Handle);
__HAL_UART_CLEAR_FEFLAG(&Uart1Handle);
}
}
if(__HAL_USART_GET_FLAG(&Uart1Handle, USART_FLAG_NE) != RESET)// noise error
if(__HAL_UART_GET_FLAG(&Uart1Handle, UART_FLAG_NE) != RESET)// noise error
{
if(source !=RESET)
{
__HAL_USART_CLEAR_NEFLAG(&Uart1Handle);
__HAL_UART_CLEAR_NEFLAG(&Uart1Handle);
}
}
if(__HAL_USART_GET_FLAG(&Uart1Handle, USART_FLAG_ORE) != RESET)// overrun error
if(__HAL_UART_GET_FLAG(&Uart1Handle, UART_FLAG_ORE) != RESET)// overrun error
{
if(source !=RESET)
{
__HAL_USART_CLEAR_OREFLAG(&Uart1Handle);
__HAL_UART_CLEAR_OREFLAG(&Uart1Handle);
}
}
}
void USART_DMA_TX_IRQHandler(void)
void UART_DMA_TX_IRQHandler(void)
{
if(__HAL_DMA_GET_FLAG(Uart1Handle.hdmatx,__HAL_DMA_GET_TE_FLAG_INDEX(Uart1Handle.hdmatx)) != RESET)
{
......@@ -129,7 +129,7 @@ void USART_DMA_TX_IRQHandler(void)
}
}
void USART_DMA_RX_IRQHandler(void)
void UART_DMA_RX_IRQHandler(void)
{
if(__HAL_DMA_GET_FLAG(Uart1Handle.hdmarx,__HAL_DMA_GET_TE_FLAG_INDEX(Uart1Handle.hdmarx)) != RESET)
{
......@@ -168,36 +168,36 @@ void USART_DMA_RX_IRQHandler(void)
}
/* public functions*/
void usart1_init(TComm *comm_dev,USART_InitTypeDef *conf,TUSART_IRQ_Priorities *priorities)
void usart1_init(TComm *comm_dev,UART_InitTypeDef *conf,TUART_IRQ_Priorities *priorities)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIO clock */
USART_ENABLE_TX_GPIO_CLK;
USART_ENABLE_RX_GPIO_CLK;
USART_ENABLE_DMA_CLK;
UART_ENABLE_TX_GPIO_CLK;
UART_ENABLE_RX_GPIO_CLK;
UART_ENABLE_DMA_CLK;
// configure the GPIO pins
USART_ENABLE_CLK;
UART_ENABLE_CLK;
/* Configure USART Tx and Rx as alternate function push-pull */
GPIO_InitStructure.Pin = USART_TX_PIN;
/* Configure UART Tx and Rx as alternate function push-pull */
GPIO_InitStructure.Pin = UART_TX_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(USART_TX_GPIO_PORT, &GPIO_InitStructure);
HAL_GPIO_Init(UART_TX_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.Pin = USART_RX_PIN;
GPIO_InitStructure.Pin = UART_RX_PIN;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
HAL_GPIO_Init(USART_RX_GPIO_PORT, &GPIO_InitStructure);
HAL_GPIO_Init(UART_RX_GPIO_PORT, &GPIO_InitStructure);
Uart1Handle.Instance = USART;
Uart1Handle.Instance = UART;
usart1_config(comm_dev,conf);
if(comm_dev->use_dma)
{
// configure the DMA channels
usart1_hdma_tx.Instance = USART_TX_DMA_CHANNEL;
usart1_hdma_tx.Instance = UART_TX_DMA_CHANNEL;
usart1_hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
usart1_hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE;
usart1_hdma_tx.Init.MemInc = DMA_MINC_ENABLE;
......@@ -208,11 +208,11 @@ void usart1_init(TComm *comm_dev,USART_InitTypeDef *conf,TUSART_IRQ_Priorities *
HAL_DMA_Init(&usart1_hdma_tx);
/* Associate the initialized DMA handle to the USART handle */
/* Associate the initialized DMA handle to the UART handle */
__HAL_LINKDMA(&Uart1Handle, hdmatx, usart1_hdma_tx);
/* Configure the DMA handler for reception process */
usart1_hdma_rx.Instance = USART_RX_DMA_CHANNEL;
usart1_hdma_rx.Instance = UART_RX_DMA_CHANNEL;
usart1_hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
usart1_hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE;
usart1_hdma_rx.Init.MemInc = DMA_MINC_ENABLE;
......@@ -223,7 +223,7 @@ void usart1_init(TComm *comm_dev,USART_InitTypeDef *conf,TUSART_IRQ_Priorities *
HAL_DMA_Init(&usart1_hdma_rx);
/* Associate the initialized DMA handle to the the USART handle */
/* Associate the initialized DMA handle to the the UART handle */
__HAL_LINKDMA(&Uart1Handle, hdmarx, usart1_hdma_rx);
}
usart1_set_priorities(comm_dev,priorities);
......@@ -248,43 +248,42 @@ void usart1_init(TComm *comm_dev,USART_InitTypeDef *conf,TUSART_IRQ_Priorities *
usart1_comm_dev=comm_dev;
}
void usart1_config(TComm *comm_dev,USART_InitTypeDef *conf)
void usart1_config(TComm *comm_dev,UART_InitTypeDef *conf)
{
Uart1Handle.Init.BaudRate = conf->BaudRate;
Uart1Handle.Init.WordLength = conf->WordLength;
Uart1Handle.Init.StopBits = conf->StopBits;
Uart1Handle.Init.Parity = conf->Parity;
Uart1Handle.Init.Mode = conf->Mode;
Uart1Handle.Init.CLKPolarity = conf->CLKPolarity;
Uart1Handle.Init.CLKPhase = conf->CLKPhase;
Uart1Handle.Init.CLKLastBit = conf->CLKLastBit;
HAL_USART_Init(&Uart1Handle);
Uart1Handle.Init.HwFlowCtl = conf->HwFlowCtl;
Uart1Handle.Init.OverSampling = conf->OverSampling;
HAL_UART_Init(&Uart1Handle);
}
void usart1_set_priorities(TComm *comm_dev,TUSART_IRQ_Priorities *priorities)
void usart1_set_priorities(TComm *comm_dev,TUART_IRQ_Priorities *priorities)
{
HAL_NVIC_SetPriority(USART_IRQn, priorities->irq_priority,priorities->irq_subpriority);
HAL_NVIC_EnableIRQ(USART_IRQn);
HAL_NVIC_SetPriority(UART_IRQn, priorities->irq_priority,priorities->irq_subpriority);
HAL_NVIC_EnableIRQ(UART_IRQn);
if(comm_dev->use_dma)
{
HAL_NVIC_SetPriority(USART_DMA_TX_IRQn, priorities->dma_tx_priority,priorities->dma_tx_subpriority);
HAL_NVIC_EnableIRQ(USART_DMA_TX_IRQn);
HAL_NVIC_SetPriority(USART_DMA_RX_IRQn, priorities->dma_rx_priority,priorities->dma_rx_subpriority);
HAL_NVIC_EnableIRQ(USART_DMA_RX_IRQn);
HAL_NVIC_SetPriority(UART_DMA_TX_IRQn, priorities->dma_tx_priority,priorities->dma_tx_subpriority);
HAL_NVIC_EnableIRQ(UART_DMA_TX_IRQn);
HAL_NVIC_SetPriority(UART_DMA_RX_IRQn, priorities->dma_rx_priority,priorities->dma_rx_subpriority);
HAL_NVIC_EnableIRQ(UART_DMA_RX_IRQn);
}
}
void usart1_set_baudrate(TComm *comm_dev,unsigned int baudrate)
{
Uart1Handle.Init.BaudRate = baudrate;
HAL_USART_Init(&Uart1Handle);
HAL_UART_Init(&Uart1Handle);
}
/* IRQ functions */
unsigned char usart1_send_irq(unsigned char first_byte)
{
__HAL_USART_CLEAR_FLAG(&Uart1Handle,USART_FLAG_TC);
__HAL_USART_ENABLE_IT(&Uart1Handle, USART_IT_TC);
__HAL_UART_CLEAR_FLAG(&Uart1Handle,UART_FLAG_TC);
__HAL_UART_ENABLE_IT(&Uart1Handle, UART_IT_TC);
Uart1Handle.Instance->DR=first_byte;
return 0x00;
......@@ -292,8 +291,8 @@ unsigned char usart1_send_irq(unsigned char first_byte)
unsigned char usart1_enable_tx_irq(void)
{
__HAL_USART_CLEAR_FLAG(&Uart1Handle,USART_FLAG_TC);
__HAL_USART_ENABLE_IT(&Uart1Handle, USART_IT_TC);
__HAL_UART_CLEAR_FLAG(&Uart1Handle,UART_FLAG_TC);
__HAL_UART_ENABLE_IT(&Uart1Handle, UART_IT_TC);
return 0x00;
}
......@@ -301,7 +300,7 @@ unsigned char usart1_enable_tx_irq(void)
unsigned char usart1_receive_irq(void)
{
/* enable the rx interrupt */
__HAL_USART_ENABLE_IT(&Uart1Handle, USART_IT_RXNE);
__HAL_UART_ENABLE_IT(&Uart1Handle, UART_IT_RXNE);
return 0x00;
}
......@@ -309,7 +308,7 @@ unsigned char usart1_receive_irq(void)
unsigned char usart1_cancel_receive_irq(void)
{
/* disable the rx interrupt */
__HAL_USART_DISABLE_IT(&Uart1Handle, USART_IT_RXNE);
__HAL_UART_DISABLE_IT(&Uart1Handle, UART_IT_RXNE);
return 0x00;
}
......@@ -319,9 +318,9 @@ unsigned char usart1_send_dma(unsigned char *data,unsigned short int length)
{
HAL_DMA_Start_IT(Uart1Handle.hdmatx,(uint32_t)data,(uint32_t)&Uart1Handle.Instance->DR,length);
/* Clear the TC flag in the SR register by writing 0 to it */
__HAL_USART_CLEAR_FLAG(&Uart1Handle,USART_FLAG_TC);
__HAL_UART_CLEAR_FLAG(&Uart1Handle,UART_FLAG_TC);
/* Enable the DMA transfer for transmit request by setting the DMAT bit
in the USART CR3 register */
in the UART CR3 register */
SET_BIT(Uart1Handle.Instance->CR3, USART_CR3_DMAT);
return 0x00;
......
#include "usart2.h"
#define USART USART2
#define USART_ENABLE_CLK __HAL_RCC_USART2_CLK_ENABLE()
#define USART_IRQn USART2_IRQn
#define USART_IRQHandler USART2_IRQHandler
#define UART USART2
#define UART_ENABLE_CLK __HAL_RCC_USART2_CLK_ENABLE()
#define UART_IRQn USART2_IRQn
#define UART_IRQHandler USART2_IRQHandler
#define USART_TX_PIN GPIO_PIN_2
#define USART_TX_GPIO_PORT GPIOA
#define USART_ENABLE_TX_GPIO_CLK __HAL_RCC_GPIOA_CLK_ENABLE()
#define UART_TX_PIN GPIO_PIN_2
#define UART_TX_GPIO_PORT GPIOA
#define UART_ENABLE_TX_GPIO_CLK __HAL_RCC_GPIOA_CLK_ENABLE()
#define USART_RX_PIN GPIO_PIN_3
#define USART_RX_GPIO_PORT GPIOA
#define USART_ENABLE_RX_GPIO_CLK __HAL_RCC_GPIOA_CLK_ENABLE()
#define UART_RX_PIN GPIO_PIN_3
#define UART_RX_GPIO_PORT GPIOA
#define UART_ENABLE_RX_GPIO_CLK __HAL_RCC_GPIOA_CLK_ENABLE()
/* DMA configuration */
#define USART_DMA DMA1
#define USART_ENABLE_DMA_CLK __HAL_RCC_DMA1_CLK_ENABLE()
#define UART_DMA DMA1
#define UART_ENABLE_DMA_CLK __HAL_RCC_DMA1_CLK_ENABLE()
#define USART_TX_DMA_CHANNEL DMA1_Channel7
#define USART_RX_DMA_CHANNEL DMA1_Channel6
#define UART_TX_DMA_CHANNEL DMA1_Channel7
#define UART_RX_DMA_CHANNEL DMA1_Channel6
#define USART_DMA_TX_IRQn DMA1_Channel7_IRQn
#define USART_DMA_RX_IRQn DMA1_Channel6_IRQn
#define USART_DMA_TX_IRQHandler DMA1_Channel7_IRQHandler
#define USART_DMA_RX_IRQHandler DMA1_Channel6_IRQHandler
#define UART_DMA_TX_IRQn DMA1_Channel7_IRQn
#define UART_DMA_RX_IRQn DMA1_Channel6_IRQn
#define UART_DMA_TX_IRQHandler DMA1_Channel7_IRQHandler
#define UART_DMA_RX_IRQHandler DMA1_Channel6_IRQHandler
// private variables
USART_HandleTypeDef Uart2Handle;
UART_HandleTypeDef Uart2Handle;
DMA_HandleTypeDef usart2_hdma_tx;
DMA_HandleTypeDef usart2_hdma_rx;
TComm *usart2_comm_dev;
// interrupt handlers
void USART_IRQHandler(void)
void UART_IRQHandler(void)
{
unsigned char data,ret;
uint32_t source;
if(__HAL_USART_GET_FLAG(&Uart2Handle, USART_FLAG_RXNE) != RESET)
if(__HAL_UART_GET_FLAG(&Uart2Handle, UART_FLAG_RXNE) != RESET)
{
if(__HAL_USART_GET_IT_SOURCE(&Uart2Handle, USART_IT_RXNE) !=RESET)
if(__HAL_UART_GET_IT_SOURCE(&Uart2Handle, UART_IT_RXNE) !=RESET)
{
__HAL_USART_CLEAR_FLAG(&Uart2Handle,USART_FLAG_RXNE);
__HAL_UART_CLEAR_FLAG(&Uart2Handle,UART_FLAG_RXNE);
data=(uint8_t)(Uart2Handle.Instance->DR & (uint8_t)0x00FF);
// call the reception function
if(!comm_do_irq_receive(usart2_comm_dev,data))
__HAL_USART_DISABLE_IT(&Uart2Handle, USART_IT_RXNE);
__HAL_UART_DISABLE_IT(&Uart2Handle, UART_IT_RXNE);
}
}
if(__HAL_USART_GET_FLAG(&Uart2Handle, USART_FLAG_TC) != RESET)
if(__HAL_UART_GET_FLAG(&Uart2Handle, UART_FLAG_TC) != RESET)
{
if(__HAL_USART_GET_IT_SOURCE(&Uart2Handle, USART_IT_TC) !=RESET)
if(__HAL_UART_GET_IT_SOURCE(&Uart2Handle, UART_IT_TC) !=RESET)
{
__HAL_USART_CLEAR_FLAG(&Uart2Handle,USART_FLAG_TC);
__HAL_UART_CLEAR_FLAG(&Uart2Handle,UART_FLAG_TC);
ret=comm_do_irq_send(usart2_comm_dev,&data);
if(ret==0x01)
Uart2Handle.Instance->DR=data;
else if(ret==0x00)
__HAL_USART_DISABLE_IT(&Uart2Handle, USART_IT_TC);
__HAL_UART_DISABLE_IT(&Uart2Handle, UART_IT_TC);
}
}
if(__HAL_USART_GET_FLAG(&Uart2Handle, USART_FLAG_PE) != RESET)// parity error
if(__HAL_UART_GET_FLAG(&Uart2Handle, UART_FLAG_PE) != RESET)// parity error
{
if(__HAL_USART_GET_IT_SOURCE(&Uart2Handle, USART_IT_PE) !=RESET)
if(__HAL_UART_GET_IT_SOURCE(&Uart2Handle, UART_IT_PE) !=RESET)
{
__HAL_USART_CLEAR_PEFLAG(&Uart2Handle);
__HAL_UART_CLEAR_PEFLAG(&Uart2Handle);
}
}
source=__HAL_USART_GET_IT_SOURCE(&Uart2Handle, USART_IT_ERR);
if(__HAL_USART_GET_FLAG(&Uart2Handle, USART_FLAG_FE) != RESET)// frame error
source=__HAL_UART_GET_IT_SOURCE(&Uart2Handle, UART_IT_ERR);
if(__HAL_UART_GET_FLAG(&Uart2Handle, UART_FLAG_FE) != RESET)// frame error
{
if(source !=RESET)
{
__HAL_USART_CLEAR_FEFLAG(&Uart2Handle);
__HAL_UART_CLEAR_FEFLAG(&Uart2Handle);
}
}
if(__HAL_USART_GET_FLAG(&Uart2Handle, USART_FLAG_NE) != RESET)// noise error
if(__HAL_UART_GET_FLAG(&Uart2Handle, UART_FLAG_NE) != RESET)// noise error
{
if(source !=RESET)
{
__HAL_USART_CLEAR_NEFLAG(&Uart2Handle);
__HAL_UART_CLEAR_NEFLAG(&Uart2Handle);
}
}
if(__HAL_USART_GET_FLAG(&Uart2Handle, USART_FLAG_ORE) != RESET)// overrun error
if(__HAL_UART_GET_FLAG(&Uart2Handle, UART_FLAG_ORE) != RESET)// overrun error
{
if(source !=RESET)
{
__HAL_USART_CLEAR_OREFLAG(&Uart2Handle);
__HAL_UART_CLEAR_OREFLAG(&Uart2Handle);
}
}
}
void USART_DMA_TX_IRQHandler(void)
void UART_DMA_TX_IRQHandler(void)
{
if(__HAL_DMA_GET_FLAG(Uart2Handle.hdmatx,__HAL_DMA_GET_TE_FLAG_INDEX(Uart2Handle.hdmatx)) != RESET)
{
......@@ -129,7 +129,7 @@ void USART_DMA_TX_IRQHandler(void)
}
}
void USART_DMA_RX_IRQHandler(void)
void UART_DMA_RX_IRQHandler(void)
{
if(__HAL_DMA_GET_FLAG(Uart2Handle.hdmarx,__HAL_DMA_GET_TE_FLAG_INDEX(Uart2Handle.hdmarx)) != RESET)
{
......@@ -168,36 +168,36 @@ void USART_DMA_RX_IRQHandler(void)
}
/* public functions*/
void usart2_init(TComm *comm_dev,USART_InitTypeDef *conf,TUSART_IRQ_Priorities *priorities)
void usart2_init(TComm *comm_dev,UART_InitTypeDef *conf,TUART_IRQ_Priorities *priorities)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIO clock */
USART_ENABLE_TX_GPIO_CLK;
USART_ENABLE_RX_GPIO_CLK;
USART_ENABLE_DMA_CLK;
UART_ENABLE_TX_GPIO_CLK;
UART_ENABLE_RX_GPIO_CLK;
UART_ENABLE_DMA_CLK;
// configure the GPIO pins
/* Configure USART Tx and Rx as alternate function push-pull */
GPIO_InitStructure.Pin = USART_TX_PIN;
/* Configure UART Tx and Rx as alternate function push-pull */
GPIO_InitStructure.Pin = UART_TX_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.Pull = GPIO_PULLUP;
GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(USART_TX_GPIO_PORT, &GPIO_InitStructure);
HAL_GPIO_Init(UART_TX_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.Pin = USART_RX_PIN;
GPIO_InitStructure.Pin = UART_RX_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
HAL_GPIO_Init(USART_RX_GPIO_PORT, &GPIO_InitStructure);
HAL_GPIO_Init(UART_RX_GPIO_PORT, &GPIO_InitStructure);
USART_ENABLE_CLK;
UART_ENABLE_CLK;
Uart2Handle.Instance = USART;
Uart2Handle.Instance = UART;
usart2_config(comm_dev,conf);
if(comm_dev->use_dma)
{
// configure the DMA channels
usart2_hdma_tx.Instance = USART_TX_DMA_CHANNEL;
usart2_hdma_tx.Instance = UART_TX_DMA_CHANNEL;
usart2_hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
usart2_hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE;
usart2_hdma_tx.Init.MemInc = DMA_MINC_ENABLE;
......@@ -208,11 +208,11 @@ void usart2_init(TComm *comm_dev,USART_InitTypeDef *conf,TUSART_IRQ_Priorities *
HAL_DMA_Init(&usart2_hdma_tx);
/* Associate the initialized DMA handle to the USART handle */
/* Associate the initialized DMA handle to the UART handle */
__HAL_LINKDMA(&Uart2Handle, hdmatx, usart2_hdma_tx);
/* Configure the DMA handler for reception process */
usart2_hdma_rx.Instance = USART_RX_DMA_CHANNEL;
usart2_hdma_rx.Instance = UART_RX_DMA_CHANNEL;
usart2_hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
usart2_hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE;
usart2_hdma_rx.Init.MemInc = DMA_MINC_ENABLE;
......@@ -223,7 +223,7 @@ void usart2_init(TComm *comm_dev,USART_InitTypeDef *conf,TUSART_IRQ_Priorities *
HAL_DMA_Init(&usart2_hdma_rx);
/* Associate the initialized DMA handle to the the USART handle */
/* Associate the initialized DMA handle to the the UART handle */
__HAL_LINKDMA(&Uart2Handle, hdmarx, usart2_hdma_rx);
}
usart2_set_priorities(comm_dev,priorities);
...