čtvrtek 23. srpna 2018

STM32 HAL - USART & DMA

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
uint8_t RxBuffer[50];
#define MAX_RECEIVE 4
volatile int i = 0;

/* USER CODE END PV */

/* USER CODE BEGIN 0 */

/**
  * @brief Rx Transfer completed callback.
  * @param huart UART handle.
  * @retval None
  */
__weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(huart);

  /* NOTE : This function should not be modified, when the callback is needed,
            the HAL_UART_RxCpltCallback can be implemented in the user file.
   */
  i += 3;
  HAL_UART_Receive_DMA(&huart2, &RxBuffer, MAX_RECEIVE);//sizeof(RxBuffer)); // definate max len of RxBuffer
}

/* USER CODE END 0 */

/* USER CODE BEGIN 2 */

  char Retezec[] = "STM32L476 USART DMA Rx test JFila\r\n";

  HAL_UART_Transmit(&huart2, &Retezec, (uint16_t)sizeof(Retezec), 10);
  HAL_UART_Receive_DMA(&huart2, &RxBuffer, MAX_RECEIVE);//sizeof(RxBuffer)); // definate max len of RxBuffer

  // ETXI0 also interrupt DMA transfer

  /* USER CODE END 2 */

/**
* @brief This function handles EXTI line0 interrupt.
*/
void EXTI0_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI0_IRQn 0 */
  volatile uint8_t RxCount = MAX_RECEIVE - huart2.hdmarx->Instance->CNDTR;
  RxBuffer[RxCount] = 0;
  HAL_UART_AbortReceive_IT(&huart2);
  HAL_UART_Receive_DMA(&huart2, &RxBuffer, MAX_RECEIVE);
  /* USER CODE END EXTI0_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
  /* USER CODE BEGIN EXTI0_IRQn 1 */

  /* USER CODE END EXTI0_IRQn 1 */
}