AnsweredAssumed Answered

How to set UART RX manipulated data to GPIO

Question asked by Berkovich.nir on Sep 21, 2016
Latest reply on Sep 28, 2016 by Berkovich.nir
Hi,

My name is Nir and I am new to microprocessors coding.
I use Nucleo-64 based on STM32F401RE.

I am trying to take USART data from AT-DEBUG of 6 bytes which are closed by '/n' (0xOD) and parse it to hex (0-F).
After parsing I transfer each bit to GPIO. This way I have 2 lines, 1 CLK and 1 DATA.
Currently I am using bitbang method.
Major cons: Very slow data transfer.

I think I can use DMA to have major improvment to transfer rate.

Is ir correct??
If yes, How can I use it?? should I need both DMA's??
 
Attached my main.c - please help... thank you.

%%%%%%%%%%%%% Code here %%%%%%%%%%%%%%%
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <math.h>


/* USER CODE BEGIN Includes */


/* USER CODE END Includes */


/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart2;


/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/


/* USER CODE END PV */


/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
unsigned char hextobin (unsigned char digit);


/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/





int16_t i=0,counter=0,b=0;
int32_t Select_Bits[4]={0xFF , 0xFF00 , 0xFF0000 , 0xFF000000};
GPIO_PinState PinState[2]={GPIO_PIN_RESET , GPIO_PIN_SET};




//ASCII table
uint8_t mapping_tbl[129]={
          128,128,128,128,128,128,128,128, //start at: 0
          128,128,128,128,128,128,128,128, //8 
          128,128,128,128,128,128,128,128, //16 
          128,128,128,128,128,128,128,128, //24 
          128,128,128,128,128,128,128,128, //32
          128,128,128,128,128,128,128,128, //40
           0,  1,  2,  3,  4,  5,  6,  7,  //48
           8,  9, 128,128,128,128,128,128, //56
          128,10, 11, 12, 13, 14, 15, 128, //64
          128,128,128,128,128,128,128,128, //72
          128,128,128,128,128,128,128,128, //80
          128,128,128,128,128,128,128,128, //88
          128,128,128,128,128,128,128,128, //96
          128,128,128,128,128,128,128,128, //104 
          128,128,128,128,128,128,128,128, //112
          128,128,128,128,128,128,128,128, //120
          128                                                                        //128
};

int32_t offset[4]={0 , 8 , 16 , 24};
uint8_t* bufferRx[2];
int32_t shift=0;
uint8_t bufferRxNot_Clean[4],Bit_buff[32],bit;
int32_t temp;

/* USER CODE END PFP */


/* USER CODE BEGIN 0 */


/* USER CODE END 0 */


int main(void)
{
  /* USER CODE BEGIN 1 */


  /* USER CODE END 1 */


  /* MCU Configuration----------------------------------------------------------*/


  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();


  /* Configure the system clock */
  SystemClock_Config();


  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART2_UART_Init();


  /* USER CODE BEGIN 2 */
     __HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);
     __HAL_UART_ENABLE_IT(&huart2, UART_IT_TC);
  /* USER CODE END 2 */


  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */
          HAL_UART_Receive_IT(&huart2, (uint8_t*)bufferRx, 6);
          if (((int32_t)bufferRx[1] & 0xFF00)==0x0A00)
               {
                    counter=0;
                    for(i=0;i<4;i++)
                    {     
                           temp = ((int32_t)bufferRx[0] & Select_Bits[i]);
                         bufferRxNot_Clean[counter++]= mapping_tbl[temp >> offset[i]];
                    }
                    bufferRx[1]=0;
                    for(i=0;i<counter;i++)
                    {
                         for(b=0;b<4;b++)
                         {
                              bit = (bufferRxNot_Clean[i] >> b) & 0x01;
                              HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, PinState[bit]); //DATA
                              HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET); //CLK High
                              HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET); //CLK low
                              Bit_buff[i*4+b]=bit; 
                         }
                         HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8|GPIO_PIN_9, GPIO_PIN_RESET);
                    }
                    
          }
               
HAL_Delay(200);
  /* USER CODE BEGIN 3 */


  }
  /* USER CODE END 3 */


}


/** System Clock Configuration
*/
void SystemClock_Config(void)
{


  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;


  __HAL_RCC_PWR_CLK_ENABLE();


  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);


  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = 16;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 16;
  RCC_OscInitStruct.PLL.PLLN = 336;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }


  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }


  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);


  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);


  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}


/* USART2 init function */
static void MX_USART2_UART_Init(void)
{


  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;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }


}


/** Configure pins as 
        * Analog 
        * Input 
        * Output
        * EVENT_OUT
        * EXTI
*/
static void MX_GPIO_Init(void)
{


  GPIO_InitTypeDef GPIO_InitStruct;


  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();


  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8|GPIO_PIN_9, GPIO_PIN_RESET);


  /*Configure GPIO pins : PB8 PB9 */
  GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);


}


/* USER CODE BEGIN 4 */


unsigned char hextobin (unsigned char digit)
{
    unsigned char value;


    if ((digit >= 'A') && (digit <= 'F'))
    {
        value = (digit - 'A') + 10;
    }
    else if ((digit >= '0') && (digit <= '9'))
    {
         value = digit - '0';
    }
    else
    {
         value = 255;    // error - not handling this below
    }
    return value;
}




/* USER CODE END 4 */


/**
  * @brief  This function is executed in case of error occurrence.
  * @param  None
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler */
  /* User can add his own implementation to report the HAL error return state */
  while(1) 
  {
  }
  /* USER CODE END Error_Handler */ 
}


#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


/**
  * @}
  */ 


/**
  * @}
*/ 

Outcomes