Ejemplo n.º 1
0
static uint8_t USBD_CDC_DeInit (USBD_HandleTypeDef *pdev, uint8_t cfgidx)
{
  USBD_CDC_HandleTypeDef *hcdc = context;
  unsigned index;

  for (index = 0; index < NUM_OF_CDC_UARTS; index++,hcdc++)
  {
    /* Close EP IN */
    USBD_LL_CloseEP(pdev, parameters[index].data_in_ep);
  
    /* Close EP OUT */
    USBD_LL_CloseEP(pdev, parameters[index].data_out_ep);
  
    /* Close Command IN EP */
    USBD_LL_CloseEP(pdev, parameters[index].command_ep);

    /* DeInitialize the UART peripheral */
    if (hcdc->UartHandle.Instance)
      if(HAL_UART_DeInit(&hcdc->UartHandle) != HAL_OK)
      {
        /* Initialization Error */
        Error_Handler();
      }
  }
  
  return USBD_OK;
}
Ejemplo n.º 2
0
/*#####################################################*/
bool _uart_close(Uart_t *UartSettings)
{
	HAL_UART_DeInit((UART_HandleTypeDef*)UartSettings->udata);
	if(UartSettings->udata)
		free(UartSettings->udata);
	return true;
}
Ejemplo n.º 3
0
 void traceUartDeInit(void)
 {
     if(HAL_UART_DeInit(&g_uartHandle) != HAL_OK)
     {
         ERROR_UART();
     }
 }
Ejemplo n.º 4
0
void uart_deinit(pyb_uart_obj_t *uart_obj) {
    uart_obj->is_enabled = false;
    UART_HandleTypeDef *uart = &uart_obj->uart;
    HAL_UART_DeInit(uart);
    if (uart->Instance == USART1) {
        __USART1_FORCE_RESET();
        __USART1_RELEASE_RESET();
        __USART1_CLK_DISABLE();
    } else if (uart->Instance == USART2) {
        __USART2_FORCE_RESET();
        __USART2_RELEASE_RESET();
        __USART2_CLK_DISABLE();
    } else if (uart->Instance == USART3) {
        __USART3_FORCE_RESET();
        __USART3_RELEASE_RESET();
        __USART3_CLK_DISABLE();
    } else if (uart->Instance == UART4) {
        __UART4_FORCE_RESET();
        __UART4_RELEASE_RESET();
        __UART4_CLK_DISABLE();
    } else if (uart->Instance == USART6) {
        __USART6_FORCE_RESET();
        __USART6_RELEASE_RESET();
        __USART6_CLK_DISABLE();
    }
}
Ejemplo n.º 5
0
void SleepMode(void)
{
  GPIO_InitTypeDef GPIO_InitStruct;

  /* Disable all GPIOs to reduce power */
  MX_GPIO_Deinit();

  /* Configure User push-button as external interrupt generator */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  GPIO_InitStruct.Pin = B1_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);

  HAL_UART_DeInit(&huart2);

  /* Suspend Tick increment to prevent wakeup by Systick interrupt.
     Otherwise the Systick interrupt will wake up the device within 1ms (HAL time base) */
  HAL_SuspendTick();

  __HAL_RCC_PWR_CLK_ENABLE();
  /* Request to enter SLEEP mode */
  HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI);

  /* Resume Tick interrupt if disabled prior to sleep mode entry*/
  HAL_ResumeTick();

  /* Reinitialize GPIOs */
  MX_GPIO_Init();

  /* Reinitialize UART2 */
  MX_USART2_UART_Init();
}
/**
  * @brief  CDC_Itf_DeInit
  *         DeInitializes the CDC media low layer
  * @param  None
  * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  */
static int8_t CDC_Itf_DeInit(void)
{
  /* DeInitialize the UART peripheral */
  if(HAL_UART_DeInit(&UartHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
  return (USBD_OK);
}
void Debug_UARTConfig() { 
    UARTHandle.Instance = USART2;
    UARTHandle.Init.BaudRate = UART_BAUDRATE;
    UARTHandle.Init.WordLength = UART_WORDLENGTH_8B;
    UARTHandle.Init.StopBits = UART_STOPBITS_1;
    UARTHandle.Init.Parity = UART_PARITY_NONE;
    UARTHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    UARTHandle.Init.Mode = UART_MODE_TX_RX;
    UARTHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

    HAL_UART_DeInit(&UARTHandle);
    HAL_UART_Init(&UARTHandle);
}
Ejemplo n.º 8
0
/**
 * @brief	Disables the RS232 interface
 * @param	None
 * @retval	None
 */
static void prvDisableRs232Interface()
{
	HAL_NVIC_DisableIRQ(UART4_IRQn);
	HAL_UART_DeInit(&UART_Handle);
	__UART4_CLK_DISABLE();
	xSemaphoreGive(xSemaphore);

	prvRxBuffer1CurrentIndex = 0;
	prvRxBuffer1Count = 0;
	prvRxBuffer1State = BUFFERState_Writing;
	prvRxBuffer2CurrentIndex = 0;
	prvRxBuffer2Count = 0;
	prvRxBuffer2State = BUFFERState_Writing;
}
Ejemplo n.º 9
0
void HardwareSerial::begin(uint32_t baud) {

	HAL_StatusTypeDef status;
	if ( huart6.Init.BaudRate != baud ) {
		HAL_UART_DeInit(&huart6);
		huart6.Init.BaudRate = baud;
		HAL_UART_Init(&huart6);
	}
	this->tx_in_progress = false;
	this->rx_buffer_bytes_available = 0;
	this->rx_buffer_index = 0;
	receive_request_pending = false;
	status = HAL_UART_Receive_IT(&huart6, &this->rx_buffer[this->rx_buffer_index], 1);

}
Ejemplo n.º 10
0
/**
 * @brief	Disables the UART1 interface
 * @param	None
 * @retval	None
 */
static void prvDisableUart1Interface()
{
	HAL_NVIC_DisableIRQ(USART1_IRQn);
	HAL_UART_DeInit(&UART_Handle);
	__USART1_CLK_DISABLE();
	xSemaphoreGive(xSemaphore);

	prvRxBuffer1CurrentIndex = 0;
	prvRxBuffer1Count = 0;
	prvRxBuffer1State = BUFFERState_Writing;
	prvRxBuffer2CurrentIndex = 0;
	prvRxBuffer2Count = 0;
	prvRxBuffer2State = BUFFERState_Writing;

	prvChannelIsEnabled = false;
}
Ejemplo n.º 11
0
    void traceUartInit(void)
    {
        g_uartReady = RESET;

        g_uartHandle.Instance        = TRACE_UART;
        g_uartHandle.Init.BaudRate   = 115200;
        g_uartHandle.Init.WordLength = UART_WORDLENGTH_8B;
        g_uartHandle.Init.StopBits   = UART_STOPBITS_1;
        g_uartHandle.Init.Parity     = UART_PARITY_NONE;
        g_uartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
        g_uartHandle.Init.Mode       = UART_MODE_TX_RX;

        if(HAL_UART_DeInit(&g_uartHandle) != HAL_OK)
        {
            ERROR_UART();
        }
        if(HAL_UART_Init(&g_uartHandle) != HAL_OK)
        {
            ERROR_UART();
        }
    }
Ejemplo n.º 12
0
 void PX4FlowPortInit(void)
 {
	 //*******************************************************
	 //配置USART发送与接受
		__USART1_CLK_ENABLE();
		PX4_UartHandle.Instance        = USART1;

		PX4_UartHandle.Init.BaudRate   = 115200;
		PX4_UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
		PX4_UartHandle.Init.StopBits   = UART_STOPBITS_1;
		PX4_UartHandle.Init.Parity     = UART_PARITY_NONE;
		PX4_UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
		PX4_UartHandle.Init.Mode       = UART_MODE_TX_RX;
		PX4_UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_RXOVERRUNDISABLE_INIT; 
		PX4_UartHandle.AdvancedInit.OverrunDisable=UART_ADVFEATURE_OVERRUN_DISABLE;//关闭OverRun
		HAL_UART_DeInit(&PX4_UartHandle);//卸载USART1
		HAL_UART_Init(&PX4_UartHandle);//装载USART1
	 		   /* NVIC for USART, to catch the RX complete */
  
  /*##-4- Put UART peripheral in reception process ###########################*/ 
    /* Enable the UART Data Register not empty Interrupt */
    __HAL_UART_ENABLE_IT(&PX4_UartHandle, UART_IT_RXNE);	
 }
Ejemplo n.º 13
0
static int uart_init_peripheral(void)
{
  __HAL_RCC_USART2_CLK_ENABLE();
  __HAL_RCC_DMA1_CLK_ENABLE();

  UARThandle.Instance                   = UART_INSTANCE;
  UARThandle.Init.BaudRate              = UART_BAUDRATE;
  UARThandle.Init.WordLength            = UART_WORDLENGTH_8B;
  UARThandle.Init.StopBits              = UART_STOPBITS_1;
  UARThandle.Init.Parity                = UART_PARITY_NONE;
  UARThandle.Init.HwFlowCtl             = UART_HWCONTROL_NONE;
  UARThandle.Init.OverSampling          = UART_OVERSAMPLING_8;
  UARThandle.Init.Mode                  = UART_MODE_TX_RX;

  DMAhandle_TX.Instance                 = DMA1_Stream6;
  DMAhandle_TX.Init.Channel             = DMA_CHANNEL_4;
  DMAhandle_TX.Init.Direction           = DMA_MEMORY_TO_PERIPH;
  DMAhandle_TX.Init.FIFOMode            = DMA_FIFOMODE_DISABLE;
  DMAhandle_TX.Init.MemBurst            = DMA_MBURST_SINGLE;
  DMAhandle_TX.Init.MemDataAlignment    = DMA_MDATAALIGN_BYTE;
  DMAhandle_TX.Init.MemInc              = DMA_MINC_ENABLE;
  DMAhandle_TX.Init.Mode                = DMA_NORMAL;
  DMAhandle_TX.Init.PeriphBurst         = DMA_PBURST_SINGLE;
  DMAhandle_TX.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  DMAhandle_TX.Init.PeriphInc           = DMA_PINC_DISABLE;
  DMAhandle_TX.Init.Priority            = DMA_PRIORITY_LOW;

  DMAhandle_RX                          = DMAhandle_TX;
  DMAhandle_RX.Instance                 = DMA1_Stream5;
  DMAhandle_RX.Init.Channel             = DMA_CHANNEL_4;
  DMAhandle_RX.Init.Direction           = DMA_PERIPH_TO_MEMORY;
  DMAhandle_RX.Init.Mode                = DMA_CIRCULAR;

  UARThandle.hdmatx = &DMAhandle_TX;
  UARThandle.hdmarx = &DMAhandle_RX;
  HAL_UART_DeInit(&UARThandle);
  HAL_UART_Init(&UARThandle);

  UART_INSTANCE->BRR = (2u << 4u) | (5u); /* Baudrate = 2000000 with SYSCLK=168MHz,
                                           * HAL is not very good at figuring this number out... */

  HAL_DMA_DeInit(&DMAhandle_TX);
  HAL_DMA_Init(&DMAhandle_TX);
  HAL_DMA_DeInit(&DMAhandle_RX);
  HAL_DMA_Init(&DMAhandle_RX);

  NVIC_SetPriority(DMA1_Stream6_IRQn, UART_DMA_TX_IRQ_PRIO);
  HAL_NVIC_EnableIRQ(DMA1_Stream6_IRQn);
  NVIC_SetPriority(DMA1_Stream5_IRQn, UART_DMA_RX_IRQ_PRIO);
  HAL_NVIC_EnableIRQ(DMA1_Stream5_IRQn);

  /* Use uart data register as peripheral destination for TX */
  DMAhandle_TX.Instance->PAR = (uint32_t) &(UARThandle.Instance->DR);

  /* Set source and destination address and buffer length */
  DMAhandle_RX.Instance->NDTR = RX_BUF_LEN;
  DMAhandle_RX.Instance->PAR  = (uint32_t) &(UARThandle.Instance->DR);
  DMAhandle_RX.Instance->M0AR = (uint32_t) dma_buffer_rx;

  /* Enable UART as DMA enabled receiver */
  UARThandle.Instance->CR3 |= USART_CR3_DMAR;

  /* Enable transfer complete interrupt */
  __HAL_DMA_ENABLE_IT(&DMAhandle_RX, DMA_IT_TC);

  __HAL_DMA_ENABLE(&DMAhandle_RX);

  return 0;
}
Ejemplo n.º 14
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
#ifndef BOARD_IN_STOP_MODE
  GPIO_InitTypeDef  GPIO_InitStruct;
#endif
  /* STM32F0xx HAL library initialization:
       - Configure the Flash prefetch
       - Systick timer is configured by default as source of time base, but user 
             can eventually implement his proper time base source (a general purpose 
             timer for example or other time source), keeping in mind that Time base 
             duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
             handled in milliseconds basis.
       - Low Level Initialization
     */
  HAL_Init();

  /* Configure the system clock to 48 MHz */
  SystemClock_Config();
  
  /* Configure LED3 */
  BSP_LED_Init(LED3);



#ifdef BOARD_IN_STOP_MODE  
  /* HSI must be UART clock source to be able to wake up the MCU */
  __HAL_RCC_USART1_CONFIG(RCC_USART1CLKSOURCE_HSI);
#endif
  
  /*##-1- Configure the UART peripheral ######################################*/
  /* Put the USART peripheral in the Asynchronous mode (UART Mode) */
  /* UART configured as follows:
      - Word Length = 8 Bits
      - Stop Bit = One Stop bit
      - Parity = None
      - BaudRate = 9600 baud
      - Hardware flow control disabled (RTS and CTS signals) */
   
  UartHandle.Instance        = USARTx;
  HAL_UART_DeInit(&UartHandle); 

  UartHandle.Init.BaudRate   = 9600;
  UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  UartHandle.Init.StopBits   = UART_STOPBITS_1;
  UartHandle.Init.Parity     = UART_PARITY_NONE;
  UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
  UartHandle.Init.Mode       = UART_MODE_TX_RX;
  UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  
  
  if(HAL_UART_Init(&UartHandle) != HAL_OK)
  {
    Error_Handler();
  }
  
#ifdef BOARD_IN_STOP_MODE
  
    BSP_LED_On(LED3);
    /* wait for two seconds before test start */
    HAL_Delay(2000);
  
   /* make sure that no UART transfer is on-going */ 
   while(__HAL_UART_GET_FLAG(&UartHandle, USART_ISR_BUSY) == SET);
   /* make sure that UART is ready to receive
   * (test carried out again later in HAL_UARTEx_StopModeWakeUpSourceConfig) */   
   while(__HAL_UART_GET_FLAG(&UartHandle, USART_ISR_REACK) == RESET);

  /* set the wake-up event:
   * specify wake-up on RXNE flag */
  WakeUpSelection.WakeUpEvent = UART_WAKEUP_ON_READDATA_NONEMPTY;
  if (HAL_UARTEx_StopModeWakeUpSourceConfig(&UartHandle, WakeUpSelection)!= HAL_OK)
  {
    Error_Handler(); 
  }
 
  /* Enable the UART Wake UP from stop mode Interrupt */
  __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_WUF);
  
  /* about to enter stop mode: switch off LED */
  BSP_LED_Off(LED3);
  /* enable MCU wake-up by UART */
  HAL_UARTEx_EnableStopMode(&UartHandle); 
  /* enter stop mode */
  HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

  /* ... STOP mode ... */  
  
  
  SystemClock_Config_fromSTOP();
  /* at that point, MCU has been awoken: the LED has been turned back on */
  /* Wake Up based on RXNE flag successful */ 
  HAL_UARTEx_DisableStopMode(&UartHandle);

  /* wait for some delay */
  HAL_Delay(100);
  /* Inform other board that wake up is successful */
  if (HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer1, COUNTOF(aTxBuffer1)-1, 5000)!= HAL_OK)  
  {
    Error_Handler();
  }  
  
  /*##-2- Wake Up second step  ###############################################*/
  /* make sure that no UART transfer is on-going */ 
  while(__HAL_UART_GET_FLAG(&UartHandle, USART_ISR_BUSY) == SET);
  /* make sure that UART is ready to receive 
   * (test carried out again later in HAL_UARTEx_StopModeWakeUpSourceConfig) */    
  while(__HAL_UART_GET_FLAG(&UartHandle, USART_ISR_REACK) == RESET);
  
  /* set the wake-up event:
   * specify wake-up on start-bit detection */
  WakeUpSelection.WakeUpEvent = UART_WAKEUP_ON_STARTBIT;
  if (HAL_UARTEx_StopModeWakeUpSourceConfig(&UartHandle, WakeUpSelection)!= HAL_OK)
  {
    Error_Handler(); 
  }

  /* Enable the UART Wake UP from stop mode Interrupt */
  __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_WUF);
  
  /* about to enter stop mode: switch off LED */
  BSP_LED_Off(LED3);
  /* enable MCU wake-up by UART */
  HAL_UARTEx_EnableStopMode(&UartHandle); 
  /* enter stop mode */
  HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

  /* ... STOP mode ... */
   
  SystemClock_Config_fromSTOP();
  /* at that point, MCU has been awoken: the LED has been turned back on */
  /* Wake Up on start bit detection successful */ 
  HAL_UARTEx_DisableStopMode(&UartHandle);
  /* wait for some delay */
  HAL_Delay(100);
  /* Inform other board that wake up is successful */
  if (HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer2, COUNTOF(aTxBuffer2)-1, 5000)!= HAL_OK)  
  {
    Error_Handler();
  }   
  
  
  /*##-3- Wake Up third step  ################################################*/
 /* make sure that no UART transfer is on-going */ 
  while(__HAL_UART_GET_FLAG(&UartHandle, USART_ISR_BUSY) == SET);
  /* make sure that UART is ready to receive
   * (test carried out again later in HAL_UARTEx_StopModeWakeUpSourceConfig) */       
  while(__HAL_UART_GET_FLAG(&UartHandle, USART_ISR_REACK) == RESET);
     
  /* set the wake-up event:  
   * specify address-to-match type. 
   * The address is 0x29, meaning the character triggering the 
   * address match is 0xA9 */
  WakeUpSelection.WakeUpEvent = UART_WAKEUP_ON_ADDRESS;
  WakeUpSelection.AddressLength = UART_ADDRESS_DETECT_7B; 
  WakeUpSelection.Address = 0x29;  
  if (HAL_UARTEx_StopModeWakeUpSourceConfig(&UartHandle, WakeUpSelection)!= HAL_OK)
  {
    Error_Handler(); 
  }

  /* Enable the UART Wake UP from stop mode Interrupt */
  __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_WUF);
  
  /* about to enter stop mode: switch off LED */
  BSP_LED_Off(LED3);
  /* enable MCU wake-up by UART */
  HAL_UARTEx_EnableStopMode(&UartHandle); 
  /* enter stop mode */
  HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

  /* ... STOP mode ... */
  SystemClock_Config_fromSTOP();
  /* at that point, MCU has been awoken: the LED has been turned back on */
  /* Wake Up on 7-bit address detection successful */ 
  HAL_UARTEx_DisableStopMode(&UartHandle);
  /* wait for some delay */
  HAL_Delay(100);
  /* Inform other board that wake up is successful */
  if (HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer3, COUNTOF(aTxBuffer3)-1, 5000)!= HAL_OK)  
  {
    Error_Handler();
  } 
  

  /*##-4- Wake Up fourth step  ###############################################*/   
 /* make sure that no UART transfer is on-going */ 
  while(__HAL_UART_GET_FLAG(&UartHandle, USART_ISR_BUSY) == SET);
  /* make sure that UART is ready to receive
   * (test carried out again later in HAL_UARTEx_StopModeWakeUpSourceConfig) */      
  while(__HAL_UART_GET_FLAG(&UartHandle, USART_ISR_REACK) == RESET);
    
  /* set the wake-up event:  
   * specify address-to-match type. 
   * The address is 0x2, meaning the character triggering the 
   * address match is 0x82 */
  WakeUpSelection.WakeUpEvent = UART_WAKEUP_ON_ADDRESS;
  WakeUpSelection.AddressLength = UART_ADDRESS_DETECT_4B; 
  WakeUpSelection.Address = 0x2;  
  if (HAL_UARTEx_StopModeWakeUpSourceConfig(&UartHandle, WakeUpSelection)!= HAL_OK)
  {
    Error_Handler(); 
  }

  /* Enable the UART Wake UP from stop mode Interrupt */
  __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_WUF);
  
  /* about to enter stop mode: switch off LED */
  BSP_LED_Off(LED3);
  /* enable MCU wake-up by UART */
  HAL_UARTEx_EnableStopMode(&UartHandle); 
  /* enter stop mode */
  HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

  /* ... STOP mode ... */
  SystemClock_Config_fromSTOP();
  /* at that point, MCU has been awoken: the LED has been turned back on */
  /* Wake Up on 4-bit address detection successful */ 
  /* wait for some delay */
  HAL_Delay(100);
  /* Inform other board that wake up is successful */
  if (HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer4, COUNTOF(aTxBuffer4)-1, 5000)!= HAL_OK)  
  {
    Error_Handler();
  } 

  
#else

  /* Configure PA.12 (Arduino D2) as input with External interrupt */
  GPIO_InitStruct.Pin = GPIO_PIN_12;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;

  /* Enable GPIOA clock */
  __HAL_RCC_GPIOA_CLK_ENABLE();

  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* Enable and set PA.12 (Arduino D2) EXTI Interrupt to the lowest priority */
  NVIC_SetPriority((IRQn_Type)(EXTI4_15_IRQn), 0x03);
  HAL_NVIC_EnableIRQ((IRQn_Type)(EXTI4_15_IRQn));
  /* Wait for the user to set GPIOA to GND before starting the Communication.
     In the meantime, LED3 is blinking */
  while(VirtualUserButtonStatus == 0)
  {
      /* Toggle LED3*/
      BSP_LED_Toggle(LED3);
      HAL_Delay(100);
  }

  
  
  /*##-2- Send the wake-up from stop mode first trigger ######################*/
  /*      (RXNE flag setting)                                                 */
  BSP_LED_On(LED3);
  if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aWakeUpTrigger1, COUNTOF(aWakeUpTrigger1)-1, 5000)!= HAL_OK)
  {
    Error_Handler();
  }
  
  /* Put UART peripheral in reception process to wait for other board
     wake up confirmation */  
  if(HAL_UART_Receive(&UartHandle, (uint8_t *)aRxBuffer, COUNTOF(aTxBuffer1)-1, 10000) != HAL_OK)
  {
    Error_Handler();
  } 
  BSP_LED_Off(LED3);
   
  /* Compare the expected and received buffers */
  if(Buffercmp((uint8_t*)aTxBuffer1,(uint8_t*)aRxBuffer,COUNTOF(aTxBuffer1)-1))
  {
    Error_Handler();
  } 

  /* wait for two seconds before test second step */
  HAL_Delay(2000);
  
  /*##-3- Send the wake-up from stop mode second trigger #####################*/
  /*      (start Bit detection)                                               */
  BSP_LED_On(LED3);  
  if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aWakeUpTrigger2, COUNTOF(aWakeUpTrigger2)-1, 5000)!= HAL_OK)
  {
    Error_Handler();
  }

  /* Put UART peripheral in reception process to wait for other board
     wake up confirmation */  
  if(HAL_UART_Receive(&UartHandle, (uint8_t *)aRxBuffer, COUNTOF(aTxBuffer2)-1, 10000) != HAL_OK)
  {
    Error_Handler();
  } 
  BSP_LED_Off(LED3);
   
  /* Compare the expected and received buffers */
  if(Buffercmp((uint8_t*)aTxBuffer2,(uint8_t*)aRxBuffer,COUNTOF(aTxBuffer2)-1))
  {
    Error_Handler();
  } 

  /* wait for two seconds before test third step */
  HAL_Delay(2000);


  /*##-4- Send the wake-up from stop mode third trigger ######################*/
  /*      (7-bit address match)                                               */ 
  BSP_LED_On(LED3);  
  if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aWakeUpTrigger3, 1, 5000)!= HAL_OK)
  {
    Error_Handler();
  }
 
  /* Put UART peripheral in reception process to wait for other board
     wake up confirmation */  
  if(HAL_UART_Receive(&UartHandle, (uint8_t *)aRxBuffer, COUNTOF(aTxBuffer3)-1, 10000) != HAL_OK)
  {
    Error_Handler();
  } 
  BSP_LED_Off(LED3);
   
  /* Compare the expected and received buffers */
  if(Buffercmp((uint8_t*)aTxBuffer3,(uint8_t*)aRxBuffer,COUNTOF(aTxBuffer3)-1))
  {
    Error_Handler();
  } 

  /* wait for two seconds before test fourth and last step */
  HAL_Delay(2000);


  /*##-5- Send the wake-up from stop mode fourth trigger #####################*/
  /*      (4-bit address match)                                               */  
  BSP_LED_On(LED3); 
  if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aWakeUpTrigger4, 1, 5000)!= HAL_OK)
  {
    Error_Handler();
  }
 
  
  /* Put UART peripheral in reception process to wait for other board
     wake up confirmation */  
  if(HAL_UART_Receive(&UartHandle, (uint8_t *)aRxBuffer, COUNTOF(aTxBuffer4)-1, 10000) != HAL_OK)
  {
    Error_Handler();
  } 
  BSP_LED_Off(LED3);
   
  /* Compare the expected and received buffers */
  if(Buffercmp((uint8_t*)aTxBuffer4,(uint8_t*)aRxBuffer,COUNTOF(aTxBuffer4)-1))
  {
    Error_Handler();
  } 

  HAL_Delay(2000);

#endif /* BOARD_IN_STOP_MODE */


  
  /* Turn on LED3 if test passes then enter infinite loop */
  BSP_LED_On(LED3); 
  while (1)
  {
  }
}
Ejemplo n.º 15
0
static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{
    struct stm32_uart *uart;

    RT_ASSERT(serial != RT_NULL);
    RT_ASSERT(cfg != RT_NULL);

    uart = (struct stm32_uart *)serial->parent.user_data;

    uart->UartHandle.Init.BaudRate   = cfg->baud_rate;
    uart->UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
    uart->UartHandle.Init.Mode       = UART_MODE_TX_RX;
    uart->UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

    switch (cfg->data_bits)
    {
    case DATA_BITS_7:
        uart->UartHandle.Init.WordLength = UART_WORDLENGTH_7B;
        break;
    case DATA_BITS_8:
        uart->UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
        break;
    case DATA_BITS_9:
        uart->UartHandle.Init.WordLength = UART_WORDLENGTH_9B;
        break;
    default:
        uart->UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
        break;
    }
    switch (cfg->stop_bits)
    {
    case STOP_BITS_1:
        uart->UartHandle.Init.StopBits   = UART_STOPBITS_1;
        break;
    case STOP_BITS_2:
        uart->UartHandle.Init.StopBits   = UART_STOPBITS_2;
        break;
    default:
        uart->UartHandle.Init.StopBits   = UART_STOPBITS_1;
        break;
    }
    switch (cfg->parity)
    {
    case PARITY_NONE:
        uart->UartHandle.Init.Parity     = UART_PARITY_NONE;
        break;
    case PARITY_ODD:
        uart->UartHandle.Init.Parity     = UART_PARITY_ODD;
        break;
    case PARITY_EVEN:
        uart->UartHandle.Init.Parity     = UART_PARITY_EVEN;
        break;
    default:
        uart->UartHandle.Init.Parity     = UART_PARITY_NONE;
        break;
    }
    if (HAL_UART_DeInit(&uart->UartHandle) != HAL_OK)
    {
        return RT_ERROR;
    }
    if (HAL_UART_Init(&uart->UartHandle) != HAL_OK)
    {
        return RT_ERROR;
    }

    return RT_EOK;
}
Ejemplo n.º 16
0
/**
 * @brief	Uart De-Initialization Function.
 * @param	numUart: numero della periferica UART da de-inizializzare.
 * 			numUart può assumere uno dei seguenti valori:
 * 			@arg HELPER_UART1: periferica UART 1;
 * 			@arg HELPER_UART2: periferica UART 2.
 * 			@arg HELPER_UART3: periferica UART 3;
 * 			@arg HELPER_UART4: periferica UART 4.
 * 			@arg HELPER_UART5: periferica UART 5;
 * 			@arg HELPER_UART6: periferica UART 6.
 *
 * @retval	None
 */
HAL_StatusTypeDef HELPER_UART_DeInit(HELPER_UART_TypeDef numUart){
	return HAL_UART_DeInit(HELPER_UART_GetHandle(numUart));
}
Ejemplo n.º 17
0
void uartReconfigure(uartPort_t *uartPort)
{
    /*RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;
    RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_USART3|
            RCC_PERIPHCLK_UART4|RCC_PERIPHCLK_UART5|RCC_PERIPHCLK_USART6|RCC_PERIPHCLK_UART7|RCC_PERIPHCLK_UART8;
    RCC_PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_SYSCLK;
    RCC_PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_SYSCLK;
    RCC_PeriphClkInit.Usart3ClockSelection = RCC_USART3CLKSOURCE_SYSCLK;
    RCC_PeriphClkInit.Uart4ClockSelection = RCC_UART4CLKSOURCE_SYSCLK;
    RCC_PeriphClkInit.Uart5ClockSelection = RCC_UART5CLKSOURCE_SYSCLK;
    RCC_PeriphClkInit.Usart6ClockSelection = RCC_USART6CLKSOURCE_SYSCLK;
    RCC_PeriphClkInit.Uart7ClockSelection = RCC_UART7CLKSOURCE_SYSCLK;
    RCC_PeriphClkInit.Uart8ClockSelection = RCC_UART8CLKSOURCE_SYSCLK;
    HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit);*/

    HAL_UART_DeInit(&uartPort->Handle);
    uartPort->Handle.Init.BaudRate = uartPort->port.baudRate;
    // according to the stm32 documentation wordlen has to be 9 for parity bits
    // this does not seem to matter for rx but will give bad data on tx!
    uartPort->Handle.Init.WordLength = (uartPort->port.options & SERIAL_PARITY_EVEN) ? UART_WORDLENGTH_9B : UART_WORDLENGTH_8B;
    uartPort->Handle.Init.StopBits = (uartPort->port.options & SERIAL_STOPBITS_2) ? USART_STOPBITS_2 : USART_STOPBITS_1;
    uartPort->Handle.Init.Parity = (uartPort->port.options & SERIAL_PARITY_EVEN) ? USART_PARITY_EVEN : USART_PARITY_NONE;
    uartPort->Handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    uartPort->Handle.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
    uartPort->Handle.Init.Mode = 0;

    if (uartPort->port.mode & MODE_RX)
        uartPort->Handle.Init.Mode |= UART_MODE_RX;
    if (uartPort->port.mode & MODE_TX)
        uartPort->Handle.Init.Mode |= UART_MODE_TX;


    usartConfigurePinInversion(uartPort);

#ifdef TARGET_USART_CONFIG
    usartTargetConfigure(uartPort);
#endif

    if (uartPort->port.options & SERIAL_BIDIR)
    {
        HAL_HalfDuplex_Init(&uartPort->Handle);
    }
    else
    {
        HAL_UART_Init(&uartPort->Handle);
    }

    // Receive DMA or IRQ
    if (uartPort->port.mode & MODE_RX)
    {
        if (uartPort->rxDMAStream)
        {
            uartPort->rxDMAHandle.Instance = uartPort->rxDMAStream;
            uartPort->rxDMAHandle.Init.Channel = uartPort->rxDMAChannel;
            uartPort->rxDMAHandle.Init.Direction = DMA_PERIPH_TO_MEMORY;
            uartPort->rxDMAHandle.Init.PeriphInc = DMA_PINC_DISABLE;
            uartPort->rxDMAHandle.Init.MemInc = DMA_MINC_ENABLE;
            uartPort->rxDMAHandle.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
            uartPort->rxDMAHandle.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
            uartPort->rxDMAHandle.Init.Mode = DMA_CIRCULAR;
            uartPort->rxDMAHandle.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
            uartPort->rxDMAHandle.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_1QUARTERFULL;
            uartPort->rxDMAHandle.Init.PeriphBurst = DMA_PBURST_SINGLE;
            uartPort->rxDMAHandle.Init.MemBurst = DMA_MBURST_SINGLE;
            uartPort->rxDMAHandle.Init.Priority = DMA_PRIORITY_MEDIUM;


            HAL_DMA_DeInit(&uartPort->rxDMAHandle);
            HAL_DMA_Init(&uartPort->rxDMAHandle);
            /* Associate the initialized DMA handle to the UART handle */
            __HAL_LINKDMA(&uartPort->Handle, hdmarx, uartPort->rxDMAHandle);

            HAL_UART_Receive_DMA(&uartPort->Handle, (uint8_t*)uartPort->port.rxBuffer, uartPort->port.rxBufferSize);

            uartPort->rxDMAPos = __HAL_DMA_GET_COUNTER(&uartPort->rxDMAHandle);

        }
        else
        {
            /* Enable the UART Parity Error Interrupt */
            SET_BIT(uartPort->USARTx->CR1, USART_CR1_PEIE);

            /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
            SET_BIT(uartPort->USARTx->CR3, USART_CR3_EIE);

            /* Enable the UART Data Register not empty Interrupt */
            SET_BIT(uartPort->USARTx->CR1, USART_CR1_RXNEIE);
        }
    }

    // Transmit DMA or IRQ
    if (uartPort->port.mode & MODE_TX) {

        if (uartPort->txDMAStream) {
            uartPort->txDMAHandle.Instance = uartPort->txDMAStream;
            uartPort->txDMAHandle.Init.Channel = uartPort->txDMAChannel;
            uartPort->txDMAHandle.Init.Direction = DMA_MEMORY_TO_PERIPH;
            uartPort->txDMAHandle.Init.PeriphInc = DMA_PINC_DISABLE;
            uartPort->txDMAHandle.Init.MemInc = DMA_MINC_ENABLE;
            uartPort->txDMAHandle.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
            uartPort->txDMAHandle.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
            uartPort->txDMAHandle.Init.Mode = DMA_NORMAL;
            uartPort->txDMAHandle.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
            uartPort->txDMAHandle.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_1QUARTERFULL;
            uartPort->txDMAHandle.Init.PeriphBurst = DMA_PBURST_SINGLE;
            uartPort->txDMAHandle.Init.MemBurst = DMA_MBURST_SINGLE;
            uartPort->txDMAHandle.Init.Priority = DMA_PRIORITY_MEDIUM;


            HAL_DMA_DeInit(&uartPort->txDMAHandle);
            HAL_StatusTypeDef status = HAL_DMA_Init(&uartPort->txDMAHandle);
            if (status != HAL_OK)
            {
                while (1);
            }
            /* Associate the initialized DMA handle to the UART handle */
            __HAL_LINKDMA(&uartPort->Handle, hdmatx, uartPort->txDMAHandle);

            __HAL_DMA_SET_COUNTER(&uartPort->txDMAHandle, 0);
        } else {
            /* Enable the UART Transmit Data Register Empty Interrupt */
            SET_BIT(uartPort->USARTx->CR1, USART_CR1_TXEIE);
        }
    }
    return;
}
Ejemplo n.º 18
0
int armPortClose(void* port)
{
	UART_HandleTypeDef* huart = (UART_HandleTypeDef*)port;
	HAL_UART_DeInit(&huart);
	return 0;
}
Ejemplo n.º 19
0
 /* STM32F3xx HAL library initialization:
       - Configure the Flash prefetch
       - Systick timer is configured by default as source of time base, but user 
         can eventually implement his proper time base source (a general purpose 
         timer for example or other time source), keeping in mind that Time base 
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
         handled in milliseconds basis.
       - Set NVIC Group Priority to 4
       - Low Level Initialization
			 */
void com_DMA()
{

  HAL_Init();
	
	  /* Configure the system clock to 168 Mhz */
  SystemClock_Config();
	  /*##-1- Configure the UART peripheral ######################################*/
  /* Put the USART peripheral in the Asynchronous mode (UART Mode) */
  /* UART configured as follows:
      - Word Length = 8 Bits
      - Stop Bit = One Stop bit
      - Parity = None
      - BaudRate = 9600 baud
      - Hardware flow control disabled (RTS and CTS signals) */

  UartHandle.USART_BaudRate   = 38400;
  UartHandle.USART_WordLength = USART_WordLength_8b;
  UartHandle.USART_StopBits   = USART_StopBits_1;
  UartHandle.USART_Parity     = USART_Parity_No;
  UartHandle.USART_HardwareFlowControl  = USART_HardwareFlowControl_None;
  UartHandle.USART_Mode       = USART_Mode_Rx|USART_Mode_Tx;
  
  if(HAL_UART_DeInit(&UartHandle) != HAL_OK)
  {
    Error_Handler();
  } 
  if(HAL_UART_Init(&UartHandle) != HAL_OK)
  {
    Error_Handler();
  }
  
#ifdef TRANSMITTER_BOARD

  /* Configure User push-button */
  BSP_PB_Init(BUTTON_USER, BUTTON_MODE_GPIO);
  
  /* Wait for User push-button press before starting the Communication */
  while (BSP_PB_GetState(BUTTON_USER) == RESET)
  {
  }
  
  /* The board sends the message and expects to receive it back */
  
  /*##-2- Start the transmission process #####################################*/  
  /* While the UART in reception process, user can transmit data through 
     "aTxBuffer" buffer */
  if(HAL_UART_Transmit_DMA(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK)
  {
    Error_Handler();
  }
  
  /*##-3- Wait for the end of the transfer ###################################*/  
  while (UartReady != SET)
  {
  }
  
  /* Reset transmission flag */
  UartReady = RESET;
  
  /*##-4- Put UART peripheral in reception process ###########################*/  
  if(HAL_UART_DeInit(&UartHandle) != HAL_OK)
  {
    Error_Handler();
  } 
  if(HAL_UART_Init(&UartHandle) != HAL_OK)
  {
    Error_Handler();
  }  
  if(HAL_UART_Receive_DMA(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)
  {
    Error_Handler();
  }

#else
  
  /* The board receives the message and sends it back */

  /*##-2- Put UART peripheral in reception process ###########################*/  
  if(HAL_UART_Receive_DMA(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)
  {
    Error_Handler();
  }
  
  /*##-3- Wait for the end of the transfer ###################################*/  
  while (UartReady != SET)
  {
  }

  /* Reset transmission flag */
  UartReady = RESET;
  
  /*##-4- Start the transmission process #####################################*/  
  /* While the UART in reception process, user can transmit data through 
     "aTxBuffer" buffer */
  if(HAL_UART_DeInit(&UartHandle) != HAL_OK)
  {
    Error_Handler();
  } 
  if(HAL_UART_Init(&UartHandle) != HAL_OK)
  {
    Error_Handler();
  }     
  if(HAL_UART_Transmit_DMA(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK)
  {
    Error_Handler();
  }
  
#endif /* TRANSMITTER_BOARD */
  
  /*##-5- Wait for the end of the transfer ###################################*/  
  while (UartReady != SET)
  {
  }

  /* Reset transmission flag */
  UartReady = RESET;

  /*##-6- Compare the sent and received buffers ##############################*/
  if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,RXBUFFERSIZE))
  {
    Error_Handler();
  }
   
  /* Infinite loop */
  while (1)
  {
  }
}
Ejemplo n.º 20
0
void HardwareSerial::end(void) {
	HAL_UART_DeInit(&huart6);
}
Ejemplo n.º 21
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F0xx HAL library initialization:
       - Configure the Flash prefetch
       - Systick timer is configured by default as source of time base, but user 
         can eventually implement his proper time base source (a general purpose 
         timer for example or other time source), keeping in mind that Time base 
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
         handled in milliseconds basis.
       - Low Level Initialization
     */
  HAL_Init();
  
  /* Configure the system clock to 48 MHz */
  SystemClock_Config();

  /* Configure LED3 and LED4 */
  BSP_LED_Init(LED3);
  BSP_LED_Init(LED4);
  /*##-1- Configure the UART peripheral ######################################*/
  /* Put the USART peripheral in the Asynchronous mode (UART Mode) */
  /* UART configured as follows:
      - Word Length = 8 Bits
      - Stop Bit = One Stop bit
      - Parity = None
      - BaudRate = 9600 baud
      - Hardware flow control disabled (RTS and CTS signals) */
  UartHandle.Instance        = USARTx;

  UartHandle.Init.BaudRate   = 9600;
  UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  UartHandle.Init.StopBits   = UART_STOPBITS_1;
  UartHandle.Init.Parity     = UART_PARITY_NONE;
  UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
  UartHandle.Init.Mode       = UART_MODE_TX_RX;
  UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if(HAL_UART_DeInit(&UartHandle) != HAL_OK)
  {
    Error_Handler();
  }  
  if(HAL_UART_Init(&UartHandle) != HAL_OK)
  {
    Error_Handler();
  }
  
  /*##-1- Configure the I2C peripheral ######################################*/
  I2cHandle.Instance             = I2Cx;
  
  I2cHandle.Init.Timing          = I2C_TIMING;
  I2cHandle.Init.OwnAddress1     = I2C_ADDRESS;
  I2cHandle.Init.AddressingMode  = I2C_ADDRESSINGMODE_7BIT;
  I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  I2cHandle.Init.OwnAddress2     = 0xFF;
  I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  I2cHandle.Init.NoStretchMode   = I2C_NOSTRETCH_DISABLE;  
  
  if(HAL_I2C_Init(&I2cHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();    
  }

  /* Enable the Analog I2C Filter */
  HAL_I2CEx_ConfigAnalogFilter(&I2cHandle,I2C_ANALOGFILTER_ENABLE);

  
  /* Configure User push-button Button*/
  BSP_PB_Init(BUTTON_USER, BUTTON_MODE_GPIO);

	do{
		/* Wait for User push-button Button press before starting the Communication */
		while (BSP_PB_GetState(BUTTON_USER) != GPIO_PIN_RESET)
		{
		}
		
		/* Wait for User push-button Button release before starting the Communication */
		while (BSP_PB_GetState(BUTTON_USER) != GPIO_PIN_SET)
		{
		}
				
		void ndef_test(void);
		float get_adc(void);
		
		ndef_test();
		//get_adc();
	}while(1);

}
Ejemplo n.º 22
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Systick timer is configured by default as source of time base, but user
         can eventually implement his proper time base source (a general purpose
         timer for example or other time source), keeping in mind that Time base
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
         handled in milliseconds basis.
       - Set NVIC Group Priority to 4
       - Low Level Initialization: global MSP (MCU Support Package) initialization
     */
  HAL_Init();

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

  /* Configure LED2 */
  BSP_LED_Init(LED2);

  /*##-1- Configure the UART peripheral ######################################*/
  /* Put the USART peripheral in the Asynchronous mode (UART Mode) */
  /* UART configured as follows:
      - Word Length = 8 Bits
      - Stop Bit = One Stop bit
      - Parity = None
      - BaudRate = 9600 baud
      - Hardware flow control disabled (RTS and CTS signals) */
  UartHandle.Instance        = USARTx;

  UartHandle.Init.BaudRate     = 115200;
  UartHandle.Init.WordLength   = UART_WORDLENGTH_8B;
  UartHandle.Init.StopBits     = UART_STOPBITS_1;
  UartHandle.Init.Parity       = UART_PARITY_NONE;
  UartHandle.Init.HwFlowCtl    = UART_HWCONTROL_NONE;
  UartHandle.Init.Mode         = UART_MODE_TX_RX;
  UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;

  if(HAL_UART_DeInit(&UartHandle) != HAL_OK)
  {
    Error_Handler();
  }
  if(HAL_UART_Init(&UartHandle) != HAL_OK)
  {
    Error_Handler();
  }

#ifdef TRANSMITTER_BOARD

  /* Configure User push-button in Interrupt mode */
  BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_EXTI);

  /* Wait for User push-button press before starting the Communication.
     In the meantime, LED2 is blinking */
  while(UserButtonStatus == 0)
  {
      /* Toggle LED2*/
      BSP_LED_Toggle(LED2);
      HAL_Delay(100);
  }

  BSP_LED_Off(LED2);

  /* The board sends the message and expects to receive it back */

  /*##-2- Start the transmission process #####################################*/
  /* While the UART in reception process, user can transmit data through
     "aTxBuffer" buffer */
  if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE, 5000)!= HAL_OK)
  {
    Error_Handler();
  }

  /* Turn LED2 on: Transfer in transmission process is correct */
  BSP_LED_On(LED2);

  /*##-3- Put UART peripheral in reception process ###########################*/
  if(HAL_UART_Receive(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE, 5000) != HAL_OK)
  {
    Error_Handler();
  }

  /* Turn LED2 on: Transfer in reception process is correct */
  BSP_LED_On(LED2);

#else

  /* The board receives the message and sends it back */

  /*##-2- Put UART peripheral in reception process ###########################*/
  if(HAL_UART_Receive(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE, 0x1FFFFFF) != HAL_OK)
  {
    Error_Handler();
  }

  /* Turn LED2 on: Transfer in reception process is correct */
  BSP_LED_On(LED2);

  /*##-3- Start the transmission process #####################################*/
  /* While the UART in reception process, user can transmit data through
     "aTxBuffer" buffer */
  if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE, 5000)!= HAL_OK)
  {
    Error_Handler();
  }

  /* Turn LED2 on: Transfer in transmission process is correct */
  BSP_LED_On(LED2);

#endif /* TRANSMITTER_BOARD */

  /*##-4- Compare the sent and received buffers ##############################*/
  if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,RXBUFFERSIZE))
  {
    Error_Handler();
  }

  /* Infinite loop */
  while (1)
  {
  }
}
Ejemplo n.º 23
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F3xx HAL library initialization:
       - Configure the Flash prefetch
       - Configure the Systick to generate an interrupt each 1 msec
       - Set NVIC Group Priority to 4
       - Low Level Initialization
     */
  HAL_Init();

  /* Configure the system clock to 64 MHz */
  SystemClock_Config();
  
  /* Configure LED1, LED2 and LED3 */
  BSP_LED_Init(LED1);
  BSP_LED_Init(LED2);
  BSP_LED_Init(LED3);

  /*##-1- Configure the UART peripheral ######################################*/
  /* Put the USART peripheral in the Asynchronous mode (UART Mode) */
  /* UART configured as follows:
      - Word Length = 8 Bits
      - Stop Bit = One Stop bit
      - Parity = None
      - BaudRate = 9600 baud
      - Hardware flow control disabled (RTS and CTS signals) */
  UartHandle.Instance        = USARTx;

  UartHandle.Init.BaudRate     = 9600;
  UartHandle.Init.WordLength   = UART_WORDLENGTH_8B;
  UartHandle.Init.StopBits     = UART_STOPBITS_1;
  UartHandle.Init.Parity       = UART_PARITY_NONE;
  UartHandle.Init.HwFlowCtl    = UART_HWCONTROL_NONE;
  UartHandle.Init.Mode         = UART_MODE_TX_RX;
  UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
  UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

  if(HAL_UART_DeInit(&UartHandle) != HAL_OK)
  {
    Error_Handler();
  }  
  if(HAL_UART_Init(&UartHandle) != HAL_OK)
  {
    Error_Handler();
  }
  
#ifdef TRANSMITTER_BOARD

  /* Configure User push-button in Interrupt mode */
  BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_EXTI);
  
  /* Wait for User push-button press before starting the Communication.
     In the meantime, LED3 is blinking */
  while(UserButtonStatus == 0)
  {
      /* Toggle LED3*/
      BSP_LED_Toggle(LED3); 
      HAL_Delay(100);
  }
  
  BSP_LED_Off(LED3); 
  /* The board sends the message and expects to receive it back */
  
  /*##-2- Start the transmission process #####################################*/  
  /* While the UART in reception process, user can transmit data through 
     "aTxBuffer" buffer */
  if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE, 5000)!= HAL_OK)
  {
    Error_Handler();   
  }
  
  /* Turn LED1 on: Transfer in transmission process is correct */
  BSP_LED_On(LED1);
  
  /*##-3- Put UART peripheral in reception process ###########################*/  
  if(HAL_UART_Receive(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE, 5000) != HAL_OK)
  {
    Error_Handler();  
  }
   
 
#else
  
  /* The board receives the message and sends it back */

  /*##-2- Put UART peripheral in reception process ###########################*/
  if(HAL_UART_Receive(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE, 0x1FFFFFF) != HAL_OK)
  {
    Error_Handler();
  }
 
  /* Turn LED1 on: Transfer in reception process is correct */
  BSP_LED_On(LED1);
  
  /*##-3- Start the transmission process #####################################*/  
  /* While the UART in reception process, user can transmit data through 
     "aTxBuffer" buffer */
  if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE, 5000)!= HAL_OK)
  {
    Error_Handler();
  }
  
  
#endif /* TRANSMITTER_BOARD */
  
  /*##-4- Compare the sent and received buffers ##############################*/
  if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,RXBUFFERSIZE))
  {
    Error_Handler();
  }
   
  /* Turn on LED2 if test passes then enter infinite loop */
  BSP_LED_On(LED2); 
  /* Infinite loop */
  while (1)
  {
  }
}
Ejemplo n.º 24
0
Archivo: main.c Proyecto: shjere/common
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32L0xx HAL library initialization:
       - Configure the Flash prefetch
       - Configure the Systick to generate an interrupt each 1 msec
       - Low Level Initialization */
  HAL_Init();

  /* Configure LED3 */
  BSP_LED_Init(LED3);
  BSP_LED_Init(LED4);

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

  /*##-1- Configure the LPUART peripheral ####################################*/
  /* Put the USART peripheral in the Asynchronous mode (UART Mode) */
  /* LPUART configured as follows:
      - Word Length = 8 Bits
      - Stop Bit = One Stop bit
      - Parity = None
      - BaudRate = 9600 baud
      - Hardware flow control disabled (RTS and CTS signals) */

  UartHandle.Instance        = USARTx;
  HAL_UART_DeInit(&UartHandle);

  UartHandle.Init.BaudRate   = 9600;
  UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  UartHandle.Init.StopBits   = UART_STOPBITS_1;
  UartHandle.Init.Parity     = UART_PARITY_NONE;
  UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
  UartHandle.Init.Mode       = UART_MODE_TX_RX;
  UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if(HAL_UART_Init(&UartHandle) != HAL_OK)
  {
    Error_Handler();
  }

#ifdef BOARD_IN_STOP_MODE
  
  BSP_LED_On(LED3);
  /* wait for two seconds before test start */
  HAL_Delay(2000);
  
  /* make sure that no LPUART transfer is on-going */ 
  while(__HAL_UART_GET_FLAG(&UartHandle, USART_ISR_BUSY) == SET);
  /* make sure that UART is ready to receive
  * (test carried out again later in HAL_UARTEx_StopModeWakeUpSourceConfig) */
  while(__HAL_UART_GET_FLAG(&UartHandle, USART_ISR_REACK) == RESET);

  /* set the wake-up event:
   * specify wake-up on RXNE flag */
  WakeUpSelection.WakeUpEvent = UART_WAKEUP_ON_READDATA_NONEMPTY;
  if (HAL_UARTEx_StopModeWakeUpSourceConfig(&UartHandle, WakeUpSelection)!= HAL_OK)
  {
    Error_Handler(); 
  }

  /* Enable the LPUART Wake UP from stop mode Interrupt */
  __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_WUF);

  /* about to enter stop mode: switch off LED */
  BSP_LED_Off(LED3);
  /* enable MCU wake-up by LPUART */
  HAL_UARTEx_EnableStopMode(&UartHandle);

  /* enter stop mode */
  HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

  /* ... STOP mode ... */  

  SystemClock_Config_fromSTOP();
  /* at that point, MCU has been awoken: the LED has been turned back on */
  /* Wake Up based on RXNE flag successful */ 
  HAL_UARTEx_DisableStopMode(&UartHandle);

  /* wait for some delay */
  HAL_Delay(100);
  
  /* Inform other board that wake up is successful */
  if (HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer1, COUNTOF(aTxBuffer1)-1, 5000)!= HAL_OK)  
  {
    Error_Handler();
  }
  
  /*##-2- Wake Up second step  ###############################################*/
  /* make sure that no UART transfer is on-going */ 
  
  BSP_LED_On(LED3);
  /* wait for two seconds before test start */
  HAL_Delay(2000);
  
  while(__HAL_UART_GET_FLAG(&UartHandle, USART_ISR_BUSY) == SET);
  /* make sure that LPUART is ready to receive 
   * (test carried out again later in HAL_UARTEx_StopModeWakeUpSourceConfig) */    
  while(__HAL_UART_GET_FLAG(&UartHandle, USART_ISR_REACK) == RESET);
  
  /* set the wake-up event:
   * specify wake-up on start-bit detection */
  WakeUpSelection.WakeUpEvent = UART_WAKEUP_ON_STARTBIT;
  if (HAL_UARTEx_StopModeWakeUpSourceConfig(&UartHandle, WakeUpSelection)!= HAL_OK)
  {
    Error_Handler(); 
  }

  /* Enable the LPUART Wake UP from stop mode Interrupt */
  __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_WUF);
  
  /* about to enter stop mode: switch off LED */
  BSP_LED_Off(LED3);
  
  /* enable MCU wake-up by LPUART */
  HAL_UARTEx_EnableStopMode(&UartHandle); 
  /* enter stop mode */
  HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

  /* ... STOP mode ... */
   
  SystemClock_Config_fromSTOP();  
  /* at that point, MCU has been awoken: the LED has been turned back on */
  /* Wake Up on start bit detection successful */ 
  HAL_UARTEx_DisableStopMode(&UartHandle);
  
  /* wait for some delay */
  HAL_Delay(100);
  
  /* Inform other board that wake up is successful */
  if (HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer2, COUNTOF(aTxBuffer2)-1, 5000)!= HAL_OK)  
  {
    Error_Handler();
  }
  
  /*##-3- Wake Up third step  ################################################*/
 /* make sure that no LPUART transfer is on-going */ 
  while(__HAL_UART_GET_FLAG(&UartHandle, USART_ISR_BUSY) == SET);
  /* make sure that UART is ready to receive
   * (test carried out again later in HAL_UARTEx_StopModeWakeUpSourceConfig) */
  while(__HAL_UART_GET_FLAG(&UartHandle, USART_ISR_REACK) == RESET);
     
  /* set the wake-up event:  
   * specify address-to-match type. 
   * The address is 0x29, meaning the character triggering the 
   * address match is 0xA9 */
  WakeUpSelection.WakeUpEvent = UART_WAKEUP_ON_ADDRESS;
  WakeUpSelection.AddressLength = UART_ADDRESS_DETECT_7B; 
  WakeUpSelection.Address = 0x29;  
  if (HAL_UARTEx_StopModeWakeUpSourceConfig(&UartHandle, WakeUpSelection)!= HAL_OK)
  {
    Error_Handler(); 
  }

  /* Enable the LPUART Wake UP from stop mode Interrupt */
  __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_WUF);
  
  /* about to enter stop mode: switch off LED */
  BSP_LED_Off(LED3);
  /* enable MCU wake-up by LPUART */
  HAL_UARTEx_EnableStopMode(&UartHandle); 
  /* enter stop mode */
  HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

  /* ... STOP mode ... */
   
  SystemClock_Config_fromSTOP();  
  /* at that point, MCU has been awoken: the LED has been turned back on */
  /* Wake Up on 7-bit address detection successful */ 
  HAL_UARTEx_DisableStopMode(&UartHandle);
  /* wait for some delay */
  HAL_Delay(100);
  /* Inform other board that wake up is successful */
  if (HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer3, COUNTOF(aTxBuffer3)-1, 5000)!= HAL_OK)  
  {
    Error_Handler();
  } 
  
  /*##-4- Wake Up fourth step  ###############################################*/   
 /* make sure that no LPUART transfer is on-going */ 
  while(__HAL_UART_GET_FLAG(&UartHandle, USART_ISR_BUSY) == SET);
  /* make sure that UART is ready to receive
   * (test carried out again later in HAL_UARTEx_StopModeWakeUpSourceConfig) */      
  while(__HAL_UART_GET_FLAG(&UartHandle, USART_ISR_REACK) == RESET);
    
  /* set the wake-up event:  
   * specify address-to-match type. 
   * The address is 0x2, meaning the character triggering the 
   * address match is 0x82 */
  WakeUpSelection.WakeUpEvent = UART_WAKEUP_ON_ADDRESS;
  WakeUpSelection.AddressLength = UART_ADDRESS_DETECT_4B; 
  WakeUpSelection.Address = 0x2;  
  if (HAL_UARTEx_StopModeWakeUpSourceConfig(&UartHandle, WakeUpSelection)!= HAL_OK)
  {
    Error_Handler(); 
  }

  /* Enable the LPUART Wake UP from stop mode Interrupt */
  __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_WUF);
  
  /* about to enter stop mode: switch off LED */
  BSP_LED_Off(LED3);
  /* enable MCU wake-up by LPUART */
  HAL_UARTEx_EnableStopMode(&UartHandle); 
  /* enter stop mode */
  HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

  /* ... STOP mode ... */
  
  SystemClock_Config_fromSTOP();
  /* at that point, MCU has been awoken: the LED has been turned back on */
  /* Wake Up on 4-bit address detection successful */ 
  /* wait for some delay */
  HAL_Delay(100);
  /* Inform other board that wake up is successful */
  if (HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer4, COUNTOF(aTxBuffer4)-1, 5000)!= HAL_OK)  
  {
    Error_Handler();
  } 
#else

/* initialize the User push-button in Interrupt mode */
  BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_EXTI);
  
  /* Wait for User push-button press before starting the test.
     In the meantime, LED3 is blinking */
  while(UserButtonStatus == 0)
  {
    /* Toggle LED3 */
    BSP_LED_Toggle(LED3);
    HAL_Delay(100);
  }

  /*##-2- Send the wake-up from stop mode first trigger ######################*/
  /*      (RXNE flag setting)                                                 */
  BSP_LED_On(LED3);
  if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aWakeUpTrigger1, COUNTOF(aWakeUpTrigger1)-1, 5000)!= HAL_OK)
  {
    Error_Handler();
  }
  
  /* Put LPUART peripheral in reception process to wait for other board
     wake up confirmation */  
  if(HAL_UART_Receive(&UartHandle, (uint8_t *)aRxBuffer, COUNTOF(aTxBuffer1)-1, 10000) != HAL_OK)
  {
    Error_Handler();
  } 
  BSP_LED_Off(LED3);
   
  /* Compare the expected and received buffers */
  if(Buffercmp((uint8_t*)aTxBuffer1,(uint8_t*)aRxBuffer,COUNTOF(aTxBuffer1)-1))
  {
    Error_Handler();
  } 

  /* wait for two seconds before test second step */
  HAL_Delay(2000);
  
  /*##-3- Send the wake-up from stop mode second trigger #####################*/
  /*      (start Bit detection)                                               */
  BSP_LED_On(LED3);  
  if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aWakeUpTrigger2, COUNTOF(aWakeUpTrigger2)-1, 5000)!= HAL_OK)
  {
    Error_Handler();
  }

  /* Put LPUART peripheral in reception process to wait for other board
     wake up confirmation */  
  if(HAL_UART_Receive(&UartHandle, (uint8_t *)aRxBuffer, COUNTOF(aTxBuffer2)-1, 10000) != HAL_OK)
  {
    Error_Handler();
  } 
  BSP_LED_Off(LED3);
   
  /* Compare the expected and received buffers */
  if(Buffercmp((uint8_t*)aTxBuffer2,(uint8_t*)aRxBuffer,COUNTOF(aTxBuffer2)-1))
  {
    Error_Handler();
  } 

  /* wait for two seconds before test third step */
  HAL_Delay(2000);
  

  /*##-4- Send the wake-up from stop mode third trigger ######################*/
  /*      (7-bit address match)                                               */ 
  BSP_LED_On(LED3);  
  if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aWakeUpTrigger3, 1, 5000)!= HAL_OK)
  {
    Error_Handler();
  }
 
  /* Put LPUART peripheral in reception process to wait for other board
     wake up confirmation */  
  if(HAL_UART_Receive(&UartHandle, (uint8_t *)aRxBuffer, COUNTOF(aTxBuffer3)-1, 10000) != HAL_OK)
  {
    Error_Handler();
  } 
  BSP_LED_Off(LED3);
   
  /* Compare the expected and received buffers */
  if(Buffercmp((uint8_t*)aTxBuffer3,(uint8_t*)aRxBuffer,COUNTOF(aTxBuffer3)-1))
  {
    Error_Handler();
  } 

  /* wait for two seconds before test fourth and last step */
  HAL_Delay(2000);


  /*##-5- Send the wake-up from stop mode fourth trigger #####################*/
  /*      (4-bit address match)                                               */  
  BSP_LED_On(LED3); 
  if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aWakeUpTrigger4, 1, 5000)!= HAL_OK)
  {
    Error_Handler();
  }

  /* Put LPUART peripheral in reception process to wait for other board
     wake up confirmation */  
  if(HAL_UART_Receive(&UartHandle, (uint8_t *)aRxBuffer, COUNTOF(aTxBuffer4)-1, 10000) != HAL_OK)
  {
    Error_Handler();
  } 
  BSP_LED_Off(LED3);
   
  /* Compare the expected and received buffers */
  if(Buffercmp((uint8_t*)aTxBuffer4,(uint8_t*)aRxBuffer,COUNTOF(aTxBuffer4)-1))
  {
    Error_Handler();
  } 

  HAL_Delay(2000);

#endif /* BOARD_IN_STOP_MODE */
 
  /* Turn on LED3 & LED4 if test passes then enter infinite loop */
  BSP_LED_On(LED3); 
  BSP_LED_On(LED4); 
  while (1)
  {
  }
}
Ejemplo n.º 25
0
Archivo: main.c Proyecto: salinraj/NIBP
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F103xB HAL library initialization:
       - Configure the Flash prefetch
       - Systick timer is configured by default as source of time base, but user 
         can eventually implement his proper time base source (a general purpose 
         timer for example or other time source), keeping in mind that Time base 
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
         handled in milliseconds basis.
       - Set NVIC Group Priority to 4
       - Low Level Initialization
     */
  HAL_Init();
  
  /* Configure the system clock to 64 MHz */
  SystemClock_Config();
  
  /*## Configure peripherals #################################################*/
  
  /* Initialize LED on board */
  BSP_LED_Init(LED2);
  
  /* Configure User push-button in Interrupt mode */
  BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);
  
  /* Configure the ADC peripheral */
  //ADC_Config();
  /*uart configuration*/
	
	
	 UartHandle.Instance        = USARTx;

  UartHandle.Init.BaudRate   = 9600;
  UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  UartHandle.Init.StopBits   = UART_STOPBITS_1;
  UartHandle.Init.Parity     = UART_PARITY_NONE;
  UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
  UartHandle.Init.Mode       = UART_MODE_TX_RX;
  if(HAL_UART_DeInit(&UartHandle) != HAL_OK)
  {
    Error_Handler();
  }  
  if(HAL_UART_Init(&UartHandle) != HAL_OK)
  {
     Error_Handler();
  }
	
// 	
// 	
//   /* Run the ADC calibration */  
//   if (HAL_ADCEx_Calibration_Start(&AdcHandle) != HAL_OK)
//   {
//     /* Calibration Error */
//     Error_Handler();
//   }

// #if defined(ADC_TRIGGER_FROM_TIMER)
//   /* Configure the TIM peripheral */
//   TIM_Config();
// #endif /* ADC_TRIGGER_FROM_TIMER */

//   /* Note: This example, on some other STM32 boards, is performing            */
//   /*       DAC configuration here.                                            */
//   /*       On STM32F103RB-Nucleo, the device has no DAC available,            */
//   /*       therefore analog signal must be supplied externally.               */

//   /*## Enable peripherals ####################################################*/
// #if defined(ADC_TRIGGER_FROM_TIMER)
//   /* Timer enable */
//   if (HAL_TIM_Base_Start(&TimHandle) != HAL_OK)
//   {
//     /* Counter Enable Error */
//     Error_Handler();
//   }
// #endif /* ADC_TRIGGER_FROM_TIMER */

//   /* Note: This example, on some other STM32 boards, is performing            */
//   /*       DAC signal generation here.                                        */
//   /*       On STM32F103RB-Nucleo, the device has no DAC available,            */
//   /*       therefore analog signal must be supplied externally.               */

//   /*## Start ADC conversions #################################################*/
//   
//   /* Start ADC conversion on regular group with transfer by DMA */
//   if (HAL_ADC_Start_DMA(&AdcHandle,
//                         (uint32_t *)aADCxConvertedValues,
//                         ADCCONVERTEDVALUES_BUFFER_SIZE
//                        ) != HAL_OK)
//   {
//     /* Start Error */
//     Error_Handler();
//   }
//   
//     if(HAL_UART_Receive_IT(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)
//   {
//     Error_Handler();
//   }
//   

Motor_IO_Init();
MOTOR_OFF();
Valve_Open();
  /* Infinite loop */
  while (1)
  {
		
	  MOTOR_ON();
		Valve_Close();
//  		HAL_Delay(4100);
//  		Valve_Open();
		memset(PressureAmplitude , 0, ADCCONVERTEDVALUES_BUFFER_SIZE);
		memset(PulseAmplitude , 0, ADCCONVERTEDVALUES_BUFFER_SIZE);
		while((PressureSensValue=ADC_Get_Pressure()* 0.052)<140)
		{
	PressureSensValue2=ADC_Get_Pulse();
	
	PressureAmplitude[CurrentPos] = PressureSensValue2 * 0.052; 
	PulseAmplitude[CurrentPos] = PressureSensValue;
	SendDecimal(PressureSensValue2);
			Sendspace();
	SendDecimal(PressureSensValue);		
			Sendnewline();
			
	
		
	HAL_Delay(100);
	MOTOR_OFF();
		HAL_Delay(20);
	MOTOR_ON();
		}
		PressureOldValue=PressureSensValue;
		MOTOR_OFF();
		
		
		
		////////////////////////////////////////reducing pressure 5mmhg per second
		
		
		while((PressureSensValue=ADC_Get_Pressure()* 0.052)>50)
		{
		if(PressureSensValue>(PressureOldValue-4))
		{
		Valve_Open();
		}
		else
		{
			Valve_Close();
			PressureOldValue=PressureSensValue;
			PressureAmplitude[CurrentPos]= PressureSensValue;                //storing pressure amplitude to the buffer
			PressureSensValue2=ADC_Get_Pulse();
			PulseAmplitude[CurrentPos] = PressureSensValue2;
			
// 			SendDecimal(PressureSensValue2);
// 			Sendspace();
// 			SendDecimal(PressureSensValue);		
// 			Sendnewline();
			HAL_Delay(200);
						PressureSensValue2=ADC_Get_Pulse();
					if((PulseAmplitude[CurrentPos])<PressureSensValue2)
						{
							PulseAmplitude[CurrentPos] = PressureSensValue2;
						}
// 			SendDecimal(PressureSensValue2);
// 			Sendspace();
// 			SendDecimal(PressureSensValue);		
// 			Sendnewline();
			HAL_Delay(200);
						PressureSensValue2=ADC_Get_Pulse();
					if((PulseAmplitude[CurrentPos])<PressureSensValue2)
						{
							PulseAmplitude[CurrentPos] = PressureSensValue2;
						}
// 			SendDecimal(PressureSensValue2);
// 			Sendspace();
// 			SendDecimal(PressureSensValue);		
// 			Sendnewline();
						
						
// 			HAL_Delay(200);
// 						PressureSensValue2=ADC_Get_Pulse();
// 					if((PulseAmplitude[CurrentPos])<PressureSensValue2)
// 						{
// 							PulseAmplitude[CurrentPos] = PressureSensValue2;
// 						}
						
						
// 			SendDecimal(PressureSensValue2);
// 			Sendspace();
// 	 		SendDecimal(PressureSensValue);		
// 			Sendnewline();
			HAL_Delay(200);
						PressureSensValue2=ADC_Get_Pulse();
					if((PulseAmplitude[CurrentPos])<PressureSensValue2)
						{
							PulseAmplitude[CurrentPos] = PressureSensValue2;
						}
// 			SendDecimal(PressureSensValue2);
// 			Sendspace();
// 			SendDecimal(PressureSensValue);		
// 			Sendnewline();
			HAL_Delay(200);
						PressureSensValue2=ADC_Get_Pulse();
					if((PulseAmplitude[CurrentPos])<PressureSensValue2)
						{
							PulseAmplitude[CurrentPos] = PressureSensValue2;
						}
				SendDecimal(PulseAmplitude[CurrentPos]);
				Sendspace();
				SendDecimal(PressureSensValue);		
				Sendnewline();
			HAL_Delay(200);
				CurrentPos++;
				if(CurrentPos>ADCCONVERTEDVALUES_BUFFER_SIZE)
				{
					CurrentPos=0;
				
				}


						
						
						
		}
	}
		Last_Position = CurrentPos;
	MaxPulseAmplitude=0;
	
		Sendnewline();
		Sendnewline();
	
// 	 	for(CurrentPos=0 ; CurrentPos<Last_Position ; CurrentPos++)
// 	{
// 		SendDecimal(PulseAmplitude[CurrentPos]);
// 		Sendspace();
// 		SendDecimal(PressureAmplitude[CurrentPos]);	
// 		Sendnewline();
// 	}
// 	
// 	
	
	
	
	
	
	
	
	///**********   Finding Mean Pressure    ********************
	
 		for(CurrentPos=2 ; CurrentPos<Last_Position ; CurrentPos++)
	{
		
		if(MaxPulseAmplitude<PulseAmplitude[CurrentPos])
		{	
			MaxPulseAmplitude=PulseAmplitude[CurrentPos];
			MeanPressure=PressureAmplitude[CurrentPos];
			MAP_Position = CurrentPos;
		}
	}
	
	CurrentPos=0;	

	Sendstring_mean();
					Sendspace();
				SendDecimal(MeanPressure);		
				Sendnewline();
	
	
	
	///********      Finding Systolic Pressure    ************
	SysPulse = MaxPulseAmplitude *0.54;
	Pulse_Difference_Old=0xffff;
		for(CurrentPos=2 ; CurrentPos < MAP_Position ; CurrentPos++)	
			{
			if(PulseAmplitude[CurrentPos]<SysPulse)
			{
				Pulse_Difference= SysPulse - PulseAmplitude[CurrentPos];
			}
			else
			{
				Pulse_Difference= PulseAmplitude[CurrentPos] - SysPulse;
			}
			
			if(Pulse_Difference_Old>=Pulse_Difference)
			{
				Pulse_Difference_Old=Pulse_Difference;
				SysPressure = PressureAmplitude[CurrentPos];
				
			}
			
			}	
			Sendstring_Sys();
					Sendspace();
				SendDecimal(SysPressure);		
				Sendnewline();
		
			
			
		///********      Finding Diastolic Pressure    ************		
			
				DiaPulse = MaxPulseAmplitude *0.72;
	Pulse_Difference_Old=0xffff;
		for(CurrentPos=MAP_Position ; CurrentPos < Last_Position ; CurrentPos++)	
			{
			if(PulseAmplitude[CurrentPos]<DiaPulse)
			{
				Pulse_Difference= DiaPulse - PulseAmplitude[CurrentPos];
			}
			else
			{
				Pulse_Difference= PulseAmplitude[CurrentPos] - DiaPulse;
			}
			
			if(Pulse_Difference_Old>=Pulse_Difference)
			{
				Pulse_Difference_Old=Pulse_Difference;
				DiaPressure = PressureAmplitude[CurrentPos];
				
			}
			
			}
			
				Sendstring_Dia();
					Sendspace();
				SendDecimal(DiaPressure);		
				Sendnewline();
		

			
			
			
			
		
		Valve_Open();
	HAL_Delay(10000);
		
		
		
    /* Turn-on/off LED2 in function of ADC conversion result */
    /*  - Turn-off if voltage is into AWD window */ 
    /*  - Turn-on if voltage is out of AWD window */

    /* Variable of analog watchdog status is set into analog watchdog         */
    /* interrupt callback         
		*/
memset(CommandToStart,0,10);
// while(1)
// {
// uint8_t i;
// //	uint16_t PressureSensValuefinal=0;
// HAL_Delay(1000);
// PressureSensValue=0;	
// if(CommandToStart[0]=='s' && CommandToStart[1]=='t' && CommandToStart[2]=='a' && CommandToStart[3]=='r' && CommandToStart[4]=='t')
// {
// memset(CommandToStart,0,10);
// 	for( i=0;i<25;i++)
// {
//  PressureSensValue += aADCxConvertedValues[i];
// }	
// PressureSensValue=PressureSensValue/25;	
// SendDecimal(PressureSensValue);
// //while(1);
// }

// }

    /* Note: This example, on some other STM32 boards, is performing          */
    /*       DAC signal generation here.                                      */
    /*       On STM32F103RB-Nucleo, the device has no DAC available,          */
    /*       therefore analog signal must be supplied externally.             */
// 		memset(PressureAmplitude , 0, ADCCONVERTEDVALUES_BUFFER_SIZE);
// 		memset(PulseAmplitude , 0, ADCCONVERTEDVALUES_BUFFER_SIZE);
// 		

// 	for(CurrentPos=0 ; CurrentPos<ADCCONVERTEDVALUES_BUFFER_SIZE ; CurrentPos++)
// 	{
// 	PressureSensValue=ADC_Get_Pressure();	
// 	PressureSensValue2=ADC_Get_Pulse();
// 	
// 	PressureAmplitude[CurrentPos] = PressureSensValue2 * 0.052; 
// 	PulseAmplitude[CurrentPos] = PressureSensValue;
// 	
// 	HAL_Delay(100);
// 		
// 	} 
// 	
// 		for(CurrentPos=120 ; CurrentPos<ADCCONVERTEDVALUES_BUFFER_SIZE ; CurrentPos++)
// 	{
// 		
// 		if(MaxPulseAmplitude<PulseAmplitude[CurrentPos])
// 		{	
// 			MaxPulseAmplitude=PulseAmplitude[CurrentPos];
// 			MeanPressure=PressureAmplitude[CurrentPos];
// 		}
// 	}

// 		
// 	for(CurrentPos=0 ; CurrentPos<ADCCONVERTEDVALUES_BUFFER_SIZE ; CurrentPos++)
// 	{
// 		SendDecimal(PulseAmplitude[CurrentPos]);
// 		Sendspace();
// 		SendDecimal(PressureAmplitude[CurrentPos]);	
// 		Sendnewline();
// 	}
// 	
// 	Sendstring_mean();
// 	Sendspace();
// 	SendDecimal(MeanPressure);
// 	
// 	
	
	
	
// 	//  calibration starts
// 	while(1)
// 	{
// 		PressureSensValue=ADC_Get_Pressure();	
// 	PressureSensValue2=ADC_Get_Pulse();
// 		
// 	PressureSensValue2= PressureSensValue2 * 0.056 ;
// 	SendDecimal(PressureSensValue);	
// 	Sendspace();
// 	SendDecimal(PressureSensValue2);	
// 	Sendnewline();
// 	
// 		HAL_Delay(100);
// 		
// 	}
// 	
// 		
// 	//  calibration ends
	
	
	
	
// 		for(CurrentPos=0 ; CurrentPos<ADCCONVERTEDVALUES_BUFFER_SIZE ; CurrentPos++)
// 	
// 	{
// 	SendDecimal(PulseAmplitude[CurrentPos]);		
// 	}
		
//HAL_Delay(3000);	
		
  }
}
Ejemplo n.º 26
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  uint8_t index;
  uint8_t *ptr;
  uint32_t retr;

  
  /* STM32F0xx HAL library initialization:
  - Configure the Flash prefetch
  - Systick timer is configured by default as source of time base, but user 
  can eventually implement his proper time base source (a general purpose 
  timer for example or other time source), keeping in mind that Time base 
  duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
  handled in milliseconds basis.
  - Low Level Initialization
  */
  HAL_Init();
  
  /* Configure LED3, LED4, LED5 and LED6 */
  BSP_LED_Init(LED1);
  BSP_LED_Init(LED2);
  BSP_LED_Init(LED3);
  BSP_LED_Init(LED4);
  
  /* Configure the system clock to 168 Mhz */
  SystemClock_Config();
  
  /* Set the uart instance */
  UartHandle[USART1_INDEX].Instance        = USARTx(1);
  UartHandle[USART2_INDEX].Instance        = USARTx(2);
  UartHandle[USART3_INDEX].Instance        = USARTx(3);
  UartHandle[USART4_INDEX].Instance        = USARTx(4);
  UartHandle[USART5_INDEX].Instance        = USARTx(5);
  UartHandle[USART6_INDEX].Instance        = USARTx(6);
  UartHandle[USART7_INDEX].Instance        = USARTx(7);
  UartHandle[USART8_INDEX].Instance        = USARTx(8);
  
  for (index = 0; index < USART__INDEX_MAX; index++)
  {
    /*##-1- Configure the UARTs peripheral ######################################*/
    /* Put the USART peripheral in the Asynchronous mode (UART Mode) */
    /* UART configured as follows:
    - Word Length = 8 Bits
    - Stop Bit = One Stop bit
    - Parity = None
    - BaudRate = 9600 baud
    - Hardware flow control disabled (RTS and CTS signals) */
    UartHandle[index].Init.BaudRate   = 9600;
    UartHandle[index].Init.WordLength = UART_WORDLENGTH_8B;
    UartHandle[index].Init.StopBits   = UART_STOPBITS_1;
    UartHandle[index].Init.Parity     = UART_PARITY_NONE;
    UartHandle[index].Init.HwFlowCtl  = UART_HWCONTROL_NONE;
    UartHandle[index].Init.Mode       = UART_MODE_TX_RX;
    UartHandle[index].AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; 
    if(HAL_UART_DeInit(&UartHandle[index]) != HAL_OK)
    {
      Error_Handler();
    }  
    if(HAL_UART_Init(&UartHandle[index]) != HAL_OK)
    {
      Error_Handler();
    }
  }
  
  do {
    
    /* counter for the number of transfert complete */
    count++;
    
    /* initialize the global variable to handle the data transfert */
    TransferComplete = FALSE;
    uart_tx_transfert = 0;
    memset(aRxBuffer, 0x0, BUFFER_SIZE * USART__INDEX_MAX);
    memset(UartTransfertState, 0x00, sizeof(DEMO_UART_TransfertSate)*USART__INDEX_MAX);
    
    /* Prepare all uart to receive a data packet */
    for (index = 0; index < USART__INDEX_MAX; index++)
    {  
      if(HAL_UART_Receive_IT(&UartHandle[index], (uint8_t*)aRxBuffer[index], PACKET_SIZE)!= HAL_OK)
      {
        Error_Handler();
      }
    }
    
    /* Send the first packet from UART1 to initiate the loop transfer */
    if(HAL_UART_Transmit(&UartHandle[USART1_INDEX], (uint8_t*)aTxBuffer, PACKET_SIZE, 2000)!= HAL_OK)
    {
      Error_Handler();
    }
    UartTransfertState[USART1_INDEX].tx_counter++;
    
    /* Loop tranfert handling, when a data has been transfert start the next transfert */ 
    do 
    {
      /* Delay can be added for the demo : to let time to see counter incrementation on the live watch */
      /*HAL_Delay(200);  */

      /* check uart_tx_transfert bit field to know wich tranfert must be restart */
      for(index = 0; index < USART__INDEX_MAX; index++)
      {
        if((uart_tx_transfert & (1 << index)) != 0 )
        {
            /* Protection done to avoid conflict for the variable update */
            HAL_NVIC_DisableIRQ(USART3_8_IRQn);
            HAL_NVIC_DisableIRQ(USART2_IRQn);
            HAL_NVIC_DisableIRQ(USART1_IRQn);
            uart_tx_transfert &= ~(1 << index);
            HAL_NVIC_EnableIRQ(USART1_IRQn);
            HAL_NVIC_EnableIRQ(USART2_IRQn);
            HAL_NVIC_EnableIRQ(USART3_8_IRQn);    
            
            /* Control if we already send all the data packet */
            if (UartTransfertState[index].tx_counter < NB_PACKET)
            {
              /* Set the data ptr to use for the transfer */
              if ( index == 0)
              {
                ptr = aTxBuffer;
              }
              else
              {
                ptr = aRxBuffer[index];
              } 
              
              retr = HAL_UART_Transmit(&UartHandle[index], (uint8_t*)(ptr+UartTransfertState[index].tx_counter*PACKET_SIZE), PACKET_SIZE, 2000);
              if(  retr != HAL_OK)
              {
                Error_Handler();
              }
              UartTransfertState[index].tx_counter++;
            }
        }
      }
      
      BSP_LED_Toggle(LED1);
      BSP_LED_Toggle(LED4);
      if (UartTransfertState[USART1_INDEX].rx_counter == NB_PACKET)
      {
        /* Transfer status complete because the last packet has been recieved on USART1 */
        TransferComplete = TRUE;
      }
    } while(TransferComplete == FALSE);
    
    /* Compare the sent buffer sent on UART1 with the buffer receive on UART1 */
    if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer[USART1_INDEX],BUFFER_SIZE))
    {
      Error_Handler();
    }
  } while (1);
}
Ejemplo n.º 27
0
/**
  * @brief  ComPort_Config
  *         Configure the COM Port with the parameters received from host.
  * @param  None.
  * @retval None
  * @note   When a configuration is not supported, a default value is used.
  */
static void ComPort_Config(void)
{
#if 0
  if(HAL_UART_DeInit(&UartHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
  
  /* set the Stop bit */
  switch (LineCoding.format)
  {
  case 0:
    UartHandle.Init.StopBits = UART_STOPBITS_1;
    break;
  case 2:
    UartHandle.Init.StopBits = UART_STOPBITS_2;
    break;
  default :
    UartHandle.Init.StopBits = UART_STOPBITS_1;
    break;
  }
  
  /* set the parity bit*/
  switch (LineCoding.paritytype)
  {
  case 0:
    UartHandle.Init.Parity = UART_PARITY_NONE;
    break;
  case 1:
    UartHandle.Init.Parity = UART_PARITY_ODD;
    break;
  case 2:
    UartHandle.Init.Parity = UART_PARITY_EVEN;
    break;
  default :
    UartHandle.Init.Parity = UART_PARITY_NONE;
    break;
  }
  
  /*set the data type : only 8bits and 9bits is supported */
  switch (LineCoding.datatype)
  {
  case 0x07:
    /* With this configuration a parity (Even or Odd) must be set */
    UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
    break;
  case 0x08:
    if(UartHandle.Init.Parity == UART_PARITY_NONE)
    {
      UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
    }
    else 
    {
      UartHandle.Init.WordLength = UART_WORDLENGTH_9B;
    }
    
    break;
  default :
    UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
    break;
  }
  
  UartHandle.Init.BaudRate     = LineCoding.bitrate;
  UartHandle.Init.HwFlowCtl    = UART_HWCONTROL_NONE;
  UartHandle.Init.Mode         = UART_MODE_TX_RX;
  UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
  
  if(HAL_UART_Init(&UartHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Start reception: provide the buffer pointer with offset and the buffer size */
  HAL_UART_Receive_IT(&UartHandle, (uint8_t *)(UserTxBuffer + UserTxBufPtrIn), 1);
#endif
}
Ejemplo n.º 28
0
static void ComPort_Config(USBD_CDC_HandleTypeDef *hcdc)
{
  if (hcdc->UartHandle.State != HAL_UART_STATE_RESET)
    if (HAL_UART_DeInit(&hcdc->UartHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler();
    }
  
  /* set the Stop bit */
  switch (hcdc->LineCoding.format)
  {
  case 0:
    hcdc->UartHandle.Init.StopBits = UART_STOPBITS_1;
    break;
  case 2:
    hcdc->UartHandle.Init.StopBits = UART_STOPBITS_2;
    break;
  default:
    hcdc->UartHandle.Init.StopBits = UART_STOPBITS_1;
    break;
  }
  
  /* set the parity bit*/
  switch (hcdc->LineCoding.paritytype)
  {
  case 0:
    hcdc->UartHandle.Init.Parity = UART_PARITY_NONE;
    break;
  case 1:
    hcdc->UartHandle.Init.Parity = UART_PARITY_ODD;
    break;
  case 2:
    hcdc->UartHandle.Init.Parity = UART_PARITY_EVEN;
    break;
  default:
    hcdc->UartHandle.Init.Parity = UART_PARITY_NONE;
    break;
  }
  
  /*set the data type : only 8bits and 9bits is supported */
  switch (hcdc->LineCoding.datatype)
  {
  case 0x07:
    /* With this configuration a parity (Even or Odd) must be set */
    hcdc->UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
    break;
  case 0x08:
    if(hcdc->UartHandle.Init.Parity == UART_PARITY_NONE)
    {
      hcdc->UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
    }
    else 
    {
      hcdc->UartHandle.Init.WordLength = UART_WORDLENGTH_9B;
    }
    
    break;
  default:
    hcdc->UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
    break;
  }
  
  hcdc->UartHandle.Init.BaudRate = hcdc->LineCoding.bitrate;
  hcdc->UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
  hcdc->UartHandle.Init.Mode       = UART_MODE_TX_RX;
  
  if(HAL_UART_Init(&hcdc->UartHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Start reception */
  HAL_UART_Receive_DMA(&hcdc->UartHandle, (uint8_t *)(hcdc->InboundBuffer), INBOUND_BUFFER_SIZE);
}
Ejemplo n.º 29
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{

  /* STM32F0xx HAL library initialization:
       - Configure the Flash prefetch
       - Systick timer is configured by default as source of time base, but user 
         can eventually implement his proper time base source (a general purpose 
         timer for example or other time source), keeping in mind that Time base 
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
         handled in milliseconds basis.
       - Low Level Initialization
     */
  HAL_Init();
  
  /* Configure LED2 */
  BSP_LED_Init(LED2);


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

  /*##-1- Configure the UART peripheral ######################################*/
  /* Put the USART peripheral in the Asynchronous mode (UART Mode) */
  /* UART configured as follows:
      - Word Length = 8 Bits
      - Stop Bit = One Stop bit
      - Parity = None
      - BaudRate = 9600 baud
      - Hardware flow control disabled (RTS and CTS signals) */
  UartHandle.Instance        = USARTx;

  UartHandle.Init.BaudRate   = 9600;
  UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  UartHandle.Init.StopBits   = UART_STOPBITS_1;
  UartHandle.Init.Parity     = UART_PARITY_NONE;
  UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
  UartHandle.Init.Mode       = UART_MODE_TX_RX;
  UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; 
  if(HAL_UART_DeInit(&UartHandle) != HAL_OK)
  {
    Error_Handler();
  }  
  if(HAL_UART_Init(&UartHandle) != HAL_OK)
  {
    Error_Handler();
  }
  
#ifdef TRANSMITTER_BOARD

  /* Configure User push-button in Interrupt mode */
  BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);
  
  /* Wait for User push-button press before starting the Communication.
     In the meantime, LED2 is blinking */
  while(UserButtonStatus == 0)
  {
      /* Toggle LED2*/
      BSP_LED_Toggle(LED2); 
      HAL_Delay(100);
  }
  
  BSP_LED_Off(LED2); 
  
  
  /* The board sends the message and expects to receive it back */
  
  /*##-2- Start the transmission process #####################################*/  
  /* While the UART in reception process, user can transmit data through 
     "aTxBuffer" buffer */
  if(HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK)
  {
    Error_Handler();
  }
  
  /*##-3- Wait for the end of the transfer ###################################*/   
  while (UartReady != SET)
  {
  }
  
  /* Reset transmission flag */
  UartReady = RESET;
  
  /*##-4- Put UART peripheral in reception process ###########################*/  
  if(HAL_UART_Receive_IT(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)
  {
    Error_Handler();
  }

#else
  
  /* The board receives the message and sends it back */

  /*##-2- Put UART peripheral in reception process ###########################*/  
  if(HAL_UART_Receive_IT(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)
  {
    Error_Handler();
  }
  
  /*##-3- Wait for the end of the transfer ###################################*/   
  /* While waiting for message to come from the other board, LED2 is
     blinking according to the following pattern: a double flash every half-second */  
  while (UartReady != SET)
  {
      BSP_LED_On(LED2); 
      HAL_Delay(100);
      BSP_LED_Off(LED2); 
      HAL_Delay(100);
      BSP_LED_On(LED2); 
      HAL_Delay(100);
      BSP_LED_Off(LED2); 
      HAL_Delay(500); 
  } 
  
  /* Reset transmission flag */
  UartReady = RESET;
  BSP_LED_Off(LED2); 
  
  /*##-4- Start the transmission process #####################################*/  
  /* While the UART in reception process, user can transmit data through 
     "aTxBuffer" buffer */
  if(HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK)
  {
    Error_Handler();
  }
  
#endif /* TRANSMITTER_BOARD */
  
  /*##-5- Wait for the end of the transfer ###################################*/   
  while (UartReady != SET)
  {
  } 
  
  /* Reset transmission flag */
  UartReady = RESET;

  /*##-6- Compare the sent and received buffers ##############################*/
  if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,RXBUFFERSIZE))
  {
    Error_Handler();
  }
  
  /* Turn on LED2 if test passes then enter infinite loop */
  BSP_LED_On(LED2); 
  /* Infinite loop */
  while (1)
  {
  }
}
Ejemplo n.º 30
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
#ifdef TRANSMITTER_BOARD
  GPIO_InitTypeDef  GPIO_InitStruct;
#endif
  /* STM32F0xx HAL library initialization:
       - Configure the Flash prefetch
       - Systick timer is configured by default as source of time base, but user 
             can eventually implement his proper time base source (a general purpose 
             timer for example or other time source), keeping in mind that Time base 
             duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
             handled in milliseconds basis.
       - Low Level Initialization
     */
  HAL_Init();

  /* Configure the system clock to 48 MHz */
  SystemClock_Config();
  
  /* Configure LED3 */
  BSP_LED_Init(LED3);

#ifdef TRANSMITTER_BOARD
  /* Configure PA.12 (Arduino D2) as input with External interrupt */
  GPIO_InitStruct.Pin = GPIO_PIN_12;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;

  /* Enable GPIOA clock */
  __HAL_RCC_GPIOA_CLK_ENABLE();

  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* Enable and set PA.12 (Arduino D2) EXTI Interrupt to the lowest priority */
  NVIC_SetPriority((IRQn_Type)(EXTI4_15_IRQn), 0x03);
  HAL_NVIC_EnableIRQ((IRQn_Type)(EXTI4_15_IRQn));
  /* Wait for the user to set GPIOA to GND before starting the Communication.
     In the meantime, LED3 is blinking */
  while(VirtualUserButtonStatus == 0)
  {
      /* Toggle LED3*/
      BSP_LED_Toggle(LED3);
      HAL_Delay(100);
  }

  BSP_LED_Off(LED3);
#endif

  /*##-1- Configure the UART peripheral ######################################*/
  /* Put the USART peripheral in the Asynchronous mode (UART Mode) */
  /* UART configured as follows:
      - Word Length = 8 Bits
      - Stop Bit = One Stop bit
      - Parity = None
      - BaudRate = 9600 baud
      - Hardware flow control disabled (RTS and CTS signals) */
  UartHandle.Instance        = USARTx;

  UartHandle.Init.BaudRate   = 9600;
  UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  UartHandle.Init.StopBits   = UART_STOPBITS_1;
  UartHandle.Init.Parity     = UART_PARITY_NONE;
  UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
  UartHandle.Init.Mode       = UART_MODE_TX_RX;
  UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if(HAL_UART_DeInit(&UartHandle) != HAL_OK)
  {
    Error_Handler();
  }  
  if(HAL_UART_Init(&UartHandle) != HAL_OK)
  {
    Error_Handler();
  }
  
#ifdef TRANSMITTER_BOARD
  
  /* The board sends the message and expects to receive it back */
  /* DMA is programmed for reception before starting the transmission, in order to
     be sure DMA Rx is ready when board 2 will start transmitting */

  /*##-2- Program the Reception process #####################################*/  
  if(HAL_UART_Receive_DMA(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)
  {
    Error_Handler();
  }

  /*##-3- Start the transmission process #####################################*/  
  /* While the UART in reception process, user can transmit data through 
     "aTxBuffer" buffer */
  if(HAL_UART_Transmit_DMA(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK)
  {
    Error_Handler();
  }
  
  /*##-4- Wait for the end of the transfer ###################################*/  
  while (UartReady != SET)
  {
  }

  /* Reset transmission flag */
  UartReady = RESET;
  
#else
  
  /* The board receives the message and sends it back */

  /*##-2- Put UART peripheral in reception process ###########################*/  
  if(HAL_UART_Receive_DMA(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)
  {
    Error_Handler();
  }

  /*##-3- Wait for the end of the transfer ###################################*/
  /* While waiting for message to come from the other board, LED3 is
     blinking according to the following pattern: a double flash every half-second */  
  while (UartReady != SET)
  {
      BSP_LED_On(LED3); 
      HAL_Delay(100);
      BSP_LED_Off(LED3); 
      HAL_Delay(100);
      BSP_LED_On(LED3); 
      HAL_Delay(100);
      BSP_LED_Off(LED3); 
      HAL_Delay(500); 
  }

  /* Reset transmission flag */
  UartReady = RESET;
  BSP_LED_Off(LED3); 
  
  /*##-4- Start the transmission process #####################################*/  
  /* While the UART in reception process, user can transmit data through 
     "aTxBuffer" buffer */
  if(HAL_UART_Transmit_DMA(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK)
  {
    Error_Handler();
  }
  
#endif /* TRANSMITTER_BOARD */
  
  /*##-5- Wait for the end of the transfer ###################################*/  
  while (UartReady != SET)
  {
  }

  /* Reset transmission flag */
  UartReady = RESET;

  /*##-6- Compare the sent and received buffers ##############################*/
  if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,RXBUFFERSIZE))
  {
    Error_Handler();
  }
   
  /* Turn on LED3 if test passes then enter infinite loop */
  BSP_LED_On(LED3); 
  /* Infinite loop */
  while (1)
  {
  }
}