Пример #1
0
void HAL_USART_BeginConfig(HAL_USART_Serial serial, uint32_t baud, uint32_t config, void *ptr)
{
  // Verify UART configuration, exit if it's invalid.
  if (!HAL_USART_Validate_Config(config)) {
    usartMap[serial]->usart_enabled = false;
    return;
  }

  usartMap[serial]->usart_enabled = false;

  // AFIO clock enable
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

  // Enable USART Clock
  *usartMap[serial]->usart_apbReg |=  usartMap[serial]->usart_clock_en;

  NVIC_InitTypeDef NVIC_InitStructure;

  // Enable the USART Interrupt
  NVIC_InitStructure.NVIC_IRQChannel = usartMap[serial]->usart_int_n;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 7;//USART2_IRQ_PRIORITY;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

  HAL_USART_Configure_Pin_Modes(serial, config);

  // USART default configuration
  // USART configured as follow:
  // - BaudRate = (set baudRate as 9600 baud)
  // - Word Length = 8 Bits
  // - One Stop Bit
  // - No parity
  // - Hardware flow control disabled (RTS and CTS signals)
  // - Receive and transmit enabled
  // USART configuration
  USART_InitStructure.USART_BaudRate = baud;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  switch (config & SERIAL_FLOW_CONTROL) {
    case SERIAL_FLOW_CONTROL_CTS:
      USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_CTS;
      break;
    case SERIAL_FLOW_CONTROL_RTS:
      USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS;
      break;
    case SERIAL_FLOW_CONTROL_RTS_CTS:
      USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS;
      break;
    case SERIAL_FLOW_CONTROL_NONE:
    default:
      USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
      break;
  }

  if (serial == HAL_USART_SERIAL2) {
    // USART1 supports hardware flow control, but RTS and CTS pins are not exposed
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  }

  switch (USART_InitStructure.USART_HardwareFlowControl) {
    case USART_HardwareFlowControl_CTS:
      HAL_Pin_Mode(usartMap[serial]->usart_cts_pin, AF_OUTPUT_PUSHPULL);
      break;
    case USART_HardwareFlowControl_RTS:
      HAL_Pin_Mode(usartMap[serial]->usart_rts_pin, AF_OUTPUT_PUSHPULL);
      break;
    case USART_HardwareFlowControl_RTS_CTS:
      HAL_Pin_Mode(usartMap[serial]->usart_cts_pin, AF_OUTPUT_PUSHPULL);
      HAL_Pin_Mode(usartMap[serial]->usart_rts_pin, AF_OUTPUT_PUSHPULL);
      break;
    case USART_HardwareFlowControl_None:
    default:
      break;
  }

  // Stop bit configuration.
  switch (config & SERIAL_STOP_BITS) {
    case SERIAL_STOP_BITS_1: // 1 stop bit
      USART_InitStructure.USART_StopBits = USART_StopBits_1;
      break;
    case SERIAL_STOP_BITS_2: // 2 stop bits
      USART_InitStructure.USART_StopBits = USART_StopBits_2;
      break;
    case SERIAL_STOP_BITS_0_5: // 0.5 stop bits
      USART_InitStructure.USART_StopBits = USART_StopBits_0_5;
      break;
    case SERIAL_STOP_BITS_1_5: // 1.5 stop bits
      USART_InitStructure.USART_StopBits = USART_StopBits_1_5;
      break;
  }

  // Data bits configuration
  switch (HAL_USART_Calculate_Word_Length(config, 0)) {
    case 8:
      USART_InitStructure.USART_WordLength = USART_WordLength_8b;
      break;
    case 9:
      USART_InitStructure.USART_WordLength = USART_WordLength_9b;
      break;
  }

  // Parity configuration
  switch (config & SERIAL_PARITY) {
    case SERIAL_PARITY_NO:
      USART_InitStructure.USART_Parity = USART_Parity_No;
      break;
    case SERIAL_PARITY_EVEN:
      USART_InitStructure.USART_Parity = USART_Parity_Even;
      break;
    case SERIAL_PARITY_ODD:
      USART_InitStructure.USART_Parity = USART_Parity_Odd;
      break;
  }

  // Disable LIN mode just in case
  USART_LINCmd(usartMap[serial]->usart_peripheral, DISABLE);

  // Configure USART
  USART_Init(usartMap[serial]->usart_peripheral, &USART_InitStructure);

  // LIN configuration
  if (config & LIN_MODE) {
    // Enable break detection
    switch(config & LIN_BREAK_BITS) {
      case LIN_BREAK_10B:
        USART_LINBreakDetectLengthConfig(usartMap[serial]->usart_peripheral, USART_LINBreakDetectLength_10b);
        break;
      case LIN_BREAK_11B:
        USART_LINBreakDetectLengthConfig(usartMap[serial]->usart_peripheral, USART_LINBreakDetectLength_11b);
        break;
    }
  }


  // Enable USART Receive and Transmit interrupts
  USART_ITConfig(usartMap[serial]->usart_peripheral, USART_IT_RXNE, ENABLE);
  USART_ITConfig(usartMap[serial]->usart_peripheral, USART_IT_TXE, ENABLE);

  usartMap[serial]->usart_config = config;
  if (config & SERIAL_HALF_DUPLEX) {
    HAL_USART_Half_Duplex(serial, ENABLE);
  }

  USART_ITConfig(usartMap[serial]->usart_peripheral, USART_IT_TC, DISABLE);

  // Enable the USART
  USART_Cmd(usartMap[serial]->usart_peripheral, ENABLE);

  if (config & LIN_MODE) {
    USART_LINCmd(usartMap[serial]->usart_peripheral, ENABLE);
  }

  usartMap[serial]->usart_enabled = true;
  usartMap[serial]->usart_transmitting = false;
}
Пример #2
0
void HAL_Core_Execute_Stop_Mode(void)
{
	if((BKP_ReadBackupRegister(BKP_DR9) >> 12) == 0xA)
	{
		uint16_t wakeUpPin = BKP_ReadBackupRegister(BKP_DR9) & 0xFF;
		InterruptMode edgeTriggerMode = (InterruptMode)((BKP_ReadBackupRegister(BKP_DR9) >> 8) & 0x0F);

		/* Clear Stop mode system flag */
		BKP_WriteBackupRegister(BKP_DR9, 0xFFFF);

		if ((wakeUpPin < TOTAL_PINS) && (edgeTriggerMode <= FALLING))
		{
			PinMode wakeUpPinMode = INPUT;

			/* Set required pinMode based on edgeTriggerMode */
			switch(edgeTriggerMode)
			{
			case CHANGE:
				wakeUpPinMode = INPUT;
				break;

			case RISING:
				wakeUpPinMode = INPUT_PULLDOWN;
				break;

			case FALLING:
				wakeUpPinMode = INPUT_PULLUP;
				break;
			}
			HAL_Pin_Mode(wakeUpPin, wakeUpPinMode);

			/* Configure EXTI Interrupt : wake-up from stop mode using pin interrupt */
			HAL_Interrupts_Attach(wakeUpPin, NULL, NULL, edgeTriggerMode, NULL);

			/* Request to enter STOP mode with regulator in low power mode */
			PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);

			/* Detach the Interrupt pin */
	        HAL_Interrupts_Detach(wakeUpPin);

			/* At this stage the system has resumed from STOP mode */
			/* Enable HSE, PLL and select PLL as system clock source after wake-up from STOP */

			/* Enable HSE */
			RCC_HSEConfig(RCC_HSE_ON);

			/* Wait till HSE is ready */
			if(RCC_WaitForHSEStartUp() != SUCCESS)
			{
				/* If HSE startup fails try to recover by system reset */
				NVIC_SystemReset();
			}

			/* Enable PLL */
			RCC_PLLCmd(ENABLE);

			/* Wait till PLL is ready */
			while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);

			/* Select PLL as system clock source */
			RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

			/* Wait till PLL is used as system clock source */
			while(RCC_GetSYSCLKSource() != 0x08);
		}
	}
}