0

HAL_UART_Receive_IT以前在中断模式下开始通过UART接收数据,我对板子编程后似乎工作正常,因为UC进入HAL_UART_RxCpltCallback了,但是在重置板子后它突然停止进入它。有谁知道怎么回事?

#include "main.h"
#include "stm32f0xx_hal.h"

/* USER CODE BEGIN Includes */
#include "st7036i.h"
#include "math.h"
/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc;
DMA_HandleTypeDef hdma_adc;

I2C_HandleTypeDef hi2c1;

TIM_HandleTypeDef htim1;

UART_HandleTypeDef huart1;

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
uint8_t BPG400_out[20]={};//output string for BPG400
uint8_t PCG550_out[20]={};//output string for PCG550


uint8_t BPG400_no_sig;//flag used to determine if there is no signal from BPG400 for more than 200ms
int16_t PCG550_adc;//output from PCG (12bit)
uint8_t BPG400_arr[10];//whole frame of data from BPG
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_ADC_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_I2C1_Init(void);
static void MX_TIM1_Init(void);
static void MX_NVIC_Init(void);

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

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)//interrupt to read adc value of PCG signal every 20ms
{
    static int16_t last_adc=0;//stores value of last redout from adc to deretmine if it's value has significantly changed
    static uint8_t counter=0;
    char PCG550_out_tmp[10];//temporarly stores printf string to get change exponent presision from 3 digits to 2


    if(BPG400_no_sig>10) memcpy(BPG400_out,"BPG400 no signal    ",20); else BPG400_no_sig++;//hasn't there been any data from BPG, send no signal

    if(counter!=11)counter++;
    if(((PCG550_adc>last_adc+10)||(last_adc-10>PCG550_adc))&&counter==11)
    {


        double pressure = pow(10,(double)(0.778*(double)(((double)(PCG550_adc*(double)10.38)/(double)4095)-(double)6.143)));//calculates pressure from ADC redout



        if(pressure<0.0000167)memcpy(PCG550_out,"PCG550 no signal    ",20);//if voltage from probe ~0, assume it's unplugged
        else if(pressure<1)//different probes and correction fctors for different press ranges
        {//pirani
            memcpy (PCG550_out+7 , " mbar Pir. Ar", 13);
            sprintf (PCG550_out_tmp, "%.2E", pressure*1.7);//converts pressure to scientific notation
            PCG550_out_tmp[6]=PCG550_out_tmp[7];
            memcpy (PCG550_out , PCG550_out_tmp, 7);
        }else if(pressure<10)
        {//crossover
            memcpy (PCG550_out+7 , " mbar  Cr. Ar", 13);
            sprintf (PCG550_out_tmp, "%.2E", pressure);
            PCG550_out_tmp[6]=PCG550_out_tmp[7];
            memcpy (PCG550_out , PCG550_out_tmp, 7);
        }else
        {//cap
            memcpy (PCG550_out+7 , " mbar Cap. Ar", 13);
            sprintf (PCG550_out_tmp, "%.2E", pressure);
            PCG550_out_tmp[6]=PCG550_out_tmp[7];
            memcpy (PCG550_out , PCG550_out_tmp, 7);
        }



        last_adc=PCG550_adc;

        counter=0;
    }




    HAL_ADC_Start_DMA(&hadc, (uint32_t *)&PCG550_adc, 1);//stars new ADC redout

}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)//interrupt starts after certain am. of bits had been acquired
{
    static uint8_t flag=0;//used to find valid data frame

    if(BPG400_arr[8]==(0xFF&(BPG400_arr[1]+BPG400_arr[2]+BPG400_arr[3]+BPG400_arr[4]+BPG400_arr[5]+BPG400_arr[6]+BPG400_arr[7])))//checksum
    {
        BPG400_no_sig=0;//resets no signal flag to prevent displaying NO SIGNAL
        char BPG400_val_tmp[10];//temporarly stores printf string to get change exponent presision from 3 digits to 2
        double pressure =  pow(10,((double)((BPG400_arr[4]<<8)+BPG400_arr[5])/(double)4000-12.5));//calculate pressure from input data

        if((pressure>0.01)&&(pressure<1))//correction factors for Ar
            pressure *=1.7;
        else if(pressure<0.001)
            pressure *=0.8;

        sprintf (BPG400_val_tmp, "%.2E",pressure);//converts pressure to scientific notation
        BPG400_val_tmp[6]=BPG400_val_tmp[7];
        memcpy (BPG400_out , BPG400_val_tmp, 7);

        memcpy (BPG400_out+7 , " mbar", 5);
        switch (BPG400_arr[2]&0b11)
        {
        case 0b00:
            memcpy (BPG400_out+12 , " -", 2);
            break;
        case 0b01:
            memcpy (BPG400_out+12 , " e", 2);
            break;
        case 0b10:
            memcpy (BPG400_out+12 , " E", 2);
            break;
        case 0b11:
            memcpy (BPG400_out+12 , " D", 2);
            break;
        }

        switch (BPG400_arr[3]&0b1111)
        {
        case 0b0000:
            memcpy (BPG400_out+14 , " ok Ar", 6);
            break;
        case 0b0101:
            memcpy (BPG400_out, "Pirani adj. poorly  ", 20);
            break;
        case 0b1000:
            memcpy (BPG400_out, "BA error            ", 20);
            break;
        case 0b1001:
            memcpy (BPG400_out, "Pirani error        ", 20);
            break;
        }

        HAL_UART_Receive_IT(&huart1, BPG400_arr, 9);//starts next 9 bit data acquisition

    }else
    {
        if (!flag)//if we start data acquisition in the middle of a frame, try probing 8 bits to shift data frame to correct stating point
        {
            HAL_UART_Receive_IT(&huart1, BPG400_arr, 8);
            flag=1;
        }else
        {
            HAL_UART_Receive_IT(&huart1, BPG400_arr, 9);
            flag=0;
        }
    }


}


/* 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_DMA_Init();
  MX_ADC_Init();
  MX_USART1_UART_Init();
  MX_I2C1_Init();
  MX_TIM1_Init();

  /* Initialize interrupts */
  MX_NVIC_Init();

  /* USER CODE BEGIN 2 */

  HAL_UART_Receive_IT(&huart1, BPG400_arr, 9);//trigger interrupt after 9 bits
  HAL_TIM_Base_Start_IT(&htim1);//start interrupt for PCG
  HAL_ADC_Start_DMA(&hadc, (uint32_t *)&PCG550_adc, 1);//starts ADC DMA
  disp_init();//initialize display
  disp_setpos(1,0);//set dips. cursor to top left
  memcpy(PCG550_out,"PCG550 no signal    ",20);

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

      disp_home();//updating display every 100ms
      disp_puts(BPG400_out);
      disp_setpos(1,0);
      disp_puts(PCG550_out);
      HAL_Delay(100);


  }
  /* USER CODE END 3 */

}

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

  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_PeriphCLKInitTypeDef PeriphClkInit;

    /**Initializes the CPU, AHB and APB busses clocks 
    */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSI14;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSI14State = RCC_HSI14_ON;
  RCC_OscInitStruct.HSICalibrationValue = 16;
  RCC_OscInitStruct.HSI14CalibrationValue = 16;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

    /**Initializes the CPU, AHB and APB busses clocks 
    */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }

  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_I2C1;
  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
  PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }

    /**Configure the Systick interrupt time 
    */
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

    /**Configure the Systick 
    */
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

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

/** NVIC Configuration
*/
static void MX_NVIC_Init(void)
{
  /* USART1_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(USART1_IRQn);
  /* DMA1_Channel1_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  /* TIM1_BRK_UP_TRG_COM_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(TIM1_BRK_UP_TRG_COM_IRQn, 1, 0);
  HAL_NVIC_EnableIRQ(TIM1_BRK_UP_TRG_COM_IRQn);
}

/* ADC init function */
static void MX_ADC_Init(void)
{

  ADC_ChannelConfTypeDef sConfig;

    /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) 
    */
  hadc.Instance = ADC1;
  hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  hadc.Init.Resolution = ADC_RESOLUTION_12B;
  hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
  hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  hadc.Init.LowPowerAutoWait = DISABLE;
  hadc.Init.LowPowerAutoPowerOff = DISABLE;
  hadc.Init.ContinuousConvMode = DISABLE;
  hadc.Init.DiscontinuousConvMode = DISABLE;
  hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc.Init.DMAContinuousRequests = DISABLE;
  hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  if (HAL_ADC_Init(&hadc) != HAL_OK)
  {
    Error_Handler();
  }

    /**Configure for the selected ADC regular channel to be converted. 
    */
  sConfig.Channel = ADC_CHANNEL_0;
  sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
  sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }

}

/* I2C1 init function */
static void MX_I2C1_Init(void)
{

  hi2c1.Instance = I2C1;
  hi2c1.Init.Timing = 0x2000090E;
  hi2c1.Init.OwnAddress1 = 0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  {
    Error_Handler();
  }

    /**Configure Analogue filter 
    */
  if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  {
    Error_Handler();
  }

}

/* TIM1 init function */
static void MX_TIM1_Init(void)
{

  TIM_ClockConfigTypeDef sClockSourceConfig;
  TIM_MasterConfigTypeDef sMasterConfig;

  htim1.Instance = TIM1;
  htim1.Init.Prescaler = 0;
  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim1.Init.Period = 32000;
  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim1.Init.RepetitionCounter = 0;
  htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
  {
    Error_Handler();
  }

  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }

}

/* USART1 init function */
static void MX_USART1_UART_Init(void)
{

  huart1.Instance = USART1;
  huart1.Init.BaudRate = 9600;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }

}

/** 
  * Enable DMA controller clock
  */
static void MX_DMA_Init(void) 
{
  /* DMA controller clock enable */
  __HAL_RCC_DMA1_CLK_ENABLE();

}

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

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

}

/* USER CODE BEGIN 4 */

/* 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

/**
  * @}
  */ 

/**
  * @}
*/ 

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
4

1 回答 1

0

HAL_UART_RxCpltCallback你调用HAL_UART_Receive_IT时,你创建递归,直到堆栈溢出。

注意HAL_UART_RxCpltCallback是在中断线程中运行,不推荐使用 sprintf 等慢速操作。

调用图是这样的: HAL_UART_IRQHandler -> UART_Receive_IT -> HAL_UART_RxCpltCallback

于 2018-02-18T21:40:56.257 回答