打印
[STM32F4]

【f446re开发板试用】+修改UART+flash模拟EEROM

[复制链接]
2359|8
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
boming|  楼主 | 2015-10-26 07:47 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
关闭发送中断,打开接收中断,待收到数据
HAL_StatusTypeDef HAL_UART_APP_INIT_Receive_IT(UART_HandleTypeDef *huart)
{
PLC_UART_RECEIVE_CNT=0;
    /* Enable the UART Parity Error Interrupt */
    __HAL_UART_ENABLE_IT(huart, UART_IT_PE);

    /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
    __HAL_UART_ENABLE_IT(huart, UART_IT_ERR);

    /* Enable the UART Data Register not empty Interrupt */
    __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
    return HAL_OK;

}


中断函数
/**
  * @brief  This function handles UART interrupt request.
  * @param  huart: pointer to a UART_HandleTypeDef structure that contains
  *                the configuration information for the specified UART module.
  * @retval None
  */
void HAL_UART_APP_IRQHandler(UART_HandleTypeDef *huart)
{
  uint32_t tmp1 = 0, tmp2 = 0;

  tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_PE);
  tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_PE);  
  /* UART parity error interrupt occurred ------------------------------------*/
  if((tmp1 != RESET) && (tmp2 != RESET))
  {
    __HAL_UART_CLEAR_PEFLAG(huart);

    huart->ErrorCode |= HAL_UART_ERROR_PE;
  }

  tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_FE);
  tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR);
  /* UART frame error interrupt occurred -------------------------------------*/
  if((tmp1 != RESET) && (tmp2 != RESET))
  {
    __HAL_UART_CLEAR_FEFLAG(huart);

    huart->ErrorCode |= HAL_UART_ERROR_FE;
  }

  tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_NE);
  tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR);
  /* UART noise error interrupt occurred -------------------------------------*/
  if((tmp1 != RESET) && (tmp2 != RESET))
  {
    __HAL_UART_CLEAR_NEFLAG(huart);

    huart->ErrorCode |= HAL_UART_ERROR_NE;
  }

  tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_ORE);
  tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR);
  /* UART Over-Run interrupt occurred ----------------------------------------*/
  if((tmp1 != RESET) && (tmp2 != RESET))
  {
    __HAL_UART_CLEAR_OREFLAG(huart);

    huart->ErrorCode |= HAL_UART_ERROR_ORE;
  }

  /* UART in mode Receiver ---------------------------------------------------*/
  while((__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET)&&(__HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE)!=RESET))
          {
              
    huart->ErrorCode = HAL_UART_ERROR_NONE;
    /* Check if a transmit process is ongoing or not */
    if(huart->State == HAL_UART_STATE_BUSY_TX)
    {
      huart->State = HAL_UART_STATE_BUSY_TX_RX;
    }
    else
    {
      huart->State = HAL_UART_STATE_BUSY_RX;
    }
    if(huart->Init.WordLength == UART_WORDLENGTH_9B)
    {
      if(huart->Init.Parity == UART_PARITY_NONE)
      {
         PLC_UART_BUFFER[PLC_UART_RECEIVE_CNT]=(uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);
         PLC_UART_RECEIVE_CNT+=1;
          }
      else
      {
         PLC_UART_BUFFER[PLC_UART_RECEIVE_CNT]=(uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);
        PLC_UART_RECEIVE_CNT+=1;
      }
    }
    else
    {
      if(huart->Init.Parity == UART_PARITY_NONE)
      {
         PLC_UART_BUFFER[PLC_UART_RECEIVE_CNT]=(uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);
         PLC_UART_RECEIVE_CNT+=1;
          }
      else
      {
         PLC_UART_BUFFER[PLC_UART_RECEIVE_CNT]=(uint8_t)(huart->Instance->DR & (uint8_t)0x007F);
         PLC_UART_RECEIVE_CNT+=1;
          }
    }
      /* Check if a transmit process is ongoing or not */
      if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
      {
        huart->State = HAL_UART_STATE_BUSY_TX;
      }
      else
      {
        huart->State = HAL_UART_STATE_READY;
      }
          }


  tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_TXE);
  tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE);
  /* UART in mode Transmitter ------------------------------------------------*/
  if((tmp1 != RESET) && (tmp2 != RESET))
  {
    UART_APP_Transmit_IT(huart);
  }

  tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_TC);
  tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_TC);
  /* UART in mode Transmitter end --------------------------------------------*/
  if((tmp1 != RESET) && (tmp2 != RESET))
  {





UART_APP_EndTransmit_IT(huart);













  }

  if(huart->ErrorCode != HAL_UART_ERROR_NONE)
  {
    /* Set the UART state ready to be able to start again the process */
    huart->State = HAL_UART_STATE_READY;

    HAL_UART_ErrorCallback(huart);
  }  
}


主函数
/* USER CODE END 0 */
int main(void)
{
/* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Configure the Systick to generate an interrupt each 1 msec
       - Set NVIC Group Priority to 4
       - Global MSP (MCU Support Package) initialization
     */
  HAL_Init();

  /* Configure the system clock to 180 MHz */
  SystemClock_Config();




    /* Initialize all configured peripherals */
  MX_GPIO_Init();//初始化PLC IO
  MX_USART1_UART_Init();//初始化串口9600 7 evee 1
MX_USART2_UART_Init();//初始化串口9600 7 evee 1

  /* Unlock the Flash to enable the flash control register access *************/
  HAL_FLASH_Unlock();

  /* Erase the user Flash area
    (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/

  /* Get the 1st sector to erase */
  FirstSector = GetSector(FLASH_USER_START_ADDR);
  /* Get the number of sector to erase from 1st sector*/
  NbOfSectors = GetSector(FLASH_USER_END_ADDR) - FirstSector + 1;

  /* Fill EraseInit structure*/
  EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
  EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
  EraseInitStruct.Sector = FirstSector;
  EraseInitStruct.NbSectors = NbOfSectors;
  if(HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK)
  {
    /*
      Error occurred while sector erase.
      User can add here some code to deal with this error.
      SectorError will contain the faulty sector and then to know the code error on this sector,
      user can call function 'HAL_FLASH_GetError()'
    */
      /*
        FLASH_ErrorTypeDef errorcode = HAL_FLASH_GetError();
      */
     while(1)
  {
   HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5, GPIO_PIN_SET);
  }
  }

  /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
     you have to make sure that these data are rewritten before they are accessed during code
     execution. If this cannot be done safely, it is recommended to flush the caches by setting the
     DCRST and ICRST bits in the FLASH_CR register. */
  __HAL_FLASH_DATA_CACHE_DISABLE();
  __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();

  __HAL_FLASH_DATA_CACHE_RESET();
  __HAL_FLASH_INSTRUCTION_CACHE_RESET();

  __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
  __HAL_FLASH_DATA_CACHE_ENABLE();

  /* Program the user Flash area word by word
    (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/

  Address = FLASH_USER_START_ADDR;

  while (Address < FLASH_USER_END_ADDR)
  {
    if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address, DATA_32) == HAL_OK)
    {
      Address = Address + 4;
    }
    else
    {
      /* Error occurred while writing data in Flash memory.
         User can add here some code to deal with this error */
        /*
          FLASH_ErrorTypeDef errorcode = HAL_FLASH_GetError();
        */
    while(1)
  {
   HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5, GPIO_PIN_SET);
  }
    }
  }

  /* Lock the Flash to disable the flash control register access (recommended
     to protect the FLASH memory against possible unwanted operation) *********/
  HAL_FLASH_Lock();

  /* Check if the programmed data is OK
      MemoryProgramStatus = 0: data programmed correctly
      MemoryProgramStatus != 0: number of words not programmed correctly ******/
  Address = FLASH_USER_START_ADDR;
  MemoryProgramStatus = 0x0;

  while (Address < FLASH_USER_END_ADDR)
  {
    data32 = *(__IO uint32_t*)Address;

    if (data32 != DATA_32)
    {
      MemoryProgramStatus++;  
    }

    Address = Address + 4;
  }  

HAL_UART_APP_INIT_Receive_IT(&huart1);
  /* Check if there is an issue to program data */
  if (MemoryProgramStatus == 0)
  {while(1)
  {
for(tem1=0;PLC_UART_RECEIVE_CNT>tem1;tem1++)
        PLC_UART_RECEIVE_CNT=0; HAL_Delay(tem);

HAL_UART_Transmit_IT(&huart1,(uint8_t*)PLC_UART_BUFFER, tem1);

//HAL_UART_Transmit_IT(&huart2,(uint8_t*)aTxBuffer,  (sizeof(aTxBuffer) / sizeof(*(aTxBuffer)))-1);
    HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_5);
  }

  }
  else
  {while(1)
  {
    /* Error detected. Switch on LED4 */
   HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5, GPIO_PIN_SET);
  }         }}


PLC.tar (314 KB)



沙发
boming|  楼主 | 2015-10-26 07:48 | 只看该作者
UART功能是接收待机,定时用中断发送数据。
有哪位能讲解例程中的设备思想吗?????????????
谢谢

使用特权

评论回复
板凳
zhuotuzi| | 2015-10-26 15:37 | 只看该作者
全是代码,来点电路图,或者拓扑图好不好呢。

使用特权

评论回复
地板
734774645| | 2015-10-26 16:11 | 只看该作者
tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_PE);
tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_PE);  
获取标志,获取中断源?

使用特权

评论回复
5
wahahaheihei| | 2015-10-26 18:37 | 只看该作者
/* Lock the Flash to disable the flash control register access (recommended
     to protect the FLASH memory against possible unwanted operation) *********/
  HAL_FLASH_Lock();
好多人问,问什么写不进去,没看到这个闪存锁住了吗

使用特权

评论回复
6
boming|  楼主 | 2015-10-27 09:33 | 只看该作者
734774645 发表于 2015-10-26 16:11
tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_PE);
tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_PE); ...

使用特权

评论回复
7
boming|  楼主 | 2015-10-27 09:34 | 只看该作者
wahahaheihei 发表于 2015-10-26 18:37
/* Lock the Flash to disable the flash control register access (recommended
     to protect the FLAS ...

要解锁才能擦、写

使用特权

评论回复
8
Snow7| | 2015-10-28 22:05 | 只看该作者
HAL_FLASH_Lock(); 这个就是解锁函数吗

使用特权

评论回复
9
boming|  楼主 | 2015-10-29 07:21 | 只看该作者
Snow7 发表于 2015-10-28 22:05
HAL_FLASH_Lock(); 这个就是解锁函数吗

这个是加锁。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

8

主题

169

帖子

3

粉丝