Skip to content

Commit

Permalink
ver4 dma start rx tx, tx act like polling
Browse files Browse the repository at this point in the history
  • Loading branch information
nquantum committed Jan 2, 2018
1 parent 5d76e91 commit 74d1d2b
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 11 deletions.
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#include "std_msgs/String.h"


//__IO ITStatus TxReady = RESET;
__IO ITStatus TxReady = RESET;
__IO ITStatus RxReady = RESET;


Expand Down
63 changes: 61 additions & 2 deletions stm32f4xx_hal_msp.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ void HAL_MspInit(void)

void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
static DMA_HandleTypeDef hdma_tx;
static DMA_HandleTypeDef hdma_rx;

/* uart3 = usb, uart6 = gpio */

Expand All @@ -89,6 +91,9 @@ void HAL_UART_MspInit(UART_HandleTypeDef* huart)

/* Peripheral clock enable */
__HAL_RCC_USART6_CLK_ENABLE();

/* Enable DMA clock */
__HAL_RCC_DMA2_CLK_ENABLE();

/**USART6 GPIO Configuration
PG14 ------> USART6_TX
Expand All @@ -101,8 +106,62 @@ void HAL_UART_MspInit(UART_HandleTypeDef* huart)
GPIO_InitStruct.Alternate = GPIO_AF8_USART6;
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);

/* USART6 interrupt Init */
HAL_NVIC_SetPriority(USART6_IRQn, 0, 0);
// /* USART6 interrupt Init */
// HAL_NVIC_SetPriority(USART6_IRQn, 0, 0);
// HAL_NVIC_EnableIRQ(USART6_IRQn);

/*##-3- Configure the DMA ##################################################*/
/* Configure the DMA handler for Transmission process */
hdma_tx.Instance = DMA2_Stream6;
hdma_tx.Init.Channel = DMA_CHANNEL_5;
hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_tx.Init.MemInc = DMA_MINC_ENABLE;
hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_tx.Init.Mode = DMA_NORMAL;
hdma_tx.Init.Priority = DMA_PRIORITY_LOW;
hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_tx.Init.MemBurst = DMA_MBURST_INC4;
hdma_tx.Init.PeriphBurst = DMA_PBURST_INC4;

HAL_DMA_Init(&hdma_tx);

/* Associate the initialized DMA handle to the UART handle */
__HAL_LINKDMA(huart, hdmatx, hdma_tx);

/* Configure the DMA handler for reception process */
hdma_rx.Instance = DMA2_Stream2;
hdma_rx.Init.Channel = DMA_CHANNEL_5;
hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_rx.Init.Mode = DMA_NORMAL;
hdma_rx.Init.Priority = DMA_PRIORITY_HIGH;
hdma_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_rx.Init.MemBurst = DMA_MBURST_INC4;
hdma_rx.Init.PeriphBurst = DMA_PBURST_INC4;

HAL_DMA_Init(&hdma_rx);

/* Associate the initialized DMA handle to the the UART handle */
__HAL_LINKDMA(huart, hdmarx, hdma_rx);

/*##-4- Configure the NVIC for DMA #########################################*/
/* NVIC configuration for DMA transfer complete interrupt (USART6_TX) */
HAL_NVIC_SetPriority(DMA2_Stream6_IRQn, 0, 1);
HAL_NVIC_EnableIRQ(DMA2_Stream6_IRQn);

/* NVIC configuration for DMA transfer complete interrupt (USART6_RX) */
HAL_NVIC_SetPriority(DMA2_Stream2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA2_Stream2_IRQn);

/* NVIC for USART, to catch the TX complete */
HAL_NVIC_SetPriority(USART6_IRQn, 0, 1);
HAL_NVIC_EnableIRQ(USART6_IRQn);

}
Expand Down
40 changes: 32 additions & 8 deletions stm32f4xx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ extern TIM_HandleTypeDef htim1;

extern UART_HandleTypeDef UartHandle;

//extern __IO ITStatus TxReady;
extern __IO ITStatus TxReady;
extern __IO ITStatus RxReady;

/******************************************************************************/
Expand Down Expand Up @@ -198,6 +198,30 @@ void TIM1_UP_TIM10_IRQHandler(void)
/* USER CODE END TIM1_UP_TIM10_IRQn 1 */
}

/**
* @brief This function handles DMA interrupt request.
* @param None
* @retval None
* @Note This function is redefined in "main.h" and related to DMA
* used for USART data transmission
*/
void DMA2_Stream2_IRQHandler(void)
{
HAL_DMA_IRQHandler(UartHandle.hdmarx);
}

/**
* @brief This function handles DMA interrupt request.
* @param None
* @retval None
* @Note This function is redefined in "main.h" and related to DMA
* used for USART data reception
*/
void DMA2_Stream6_IRQHandler(void)
{
HAL_DMA_IRQHandler(UartHandle.hdmatx);
}

/**
* @brief This function handles USART6 global interrupt.
*/
Expand All @@ -218,13 +242,13 @@ void USART6_IRQHandler(void)
//
//}

//void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
//{
//
//// HAL_GPIO_TogglePin(GREEN_GPIO_Port, GREEN_Pin);
// TxReady = SET;
//
//}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{

// HAL_GPIO_TogglePin(GREEN_GPIO_Port, GREEN_Pin);
TxReady = SET;

}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
Expand Down
2 changes: 2 additions & 0 deletions stm32f4xx_it.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ void DebugMon_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);
void TIM1_UP_TIM10_IRQHandler(void);
void DMA2_Stream2_IRQHandler(void);
void DMA2_Stream6_IRQHandler(void);
void USART6_IRQHandler(void);

#ifdef __cplusplus
Expand Down

0 comments on commit 74d1d2b

Please sign in to comment.