Example #1
0
void HAL_USART_End(HAL_USART_Serial serial)
{
  // wait for transmission of outgoing data
  while (usartMap[serial]->usart_tx_buffer->head != usartMap[serial]->usart_tx_buffer->tail);

  // Disable the USART
  USART_Cmd(usartMap[serial]->usart_peripheral, DISABLE);

  // Switch pins to INPUT
  HAL_Pin_Mode(usartMap[serial]->usart_rx_pin, INPUT);
  HAL_Pin_Mode(usartMap[serial]->usart_tx_pin, INPUT);

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

  // Deinitialise USART
  USART_DeInit(usartMap[serial]->usart_peripheral);

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

  NVIC_InitTypeDef NVIC_InitStructure;

  // Disable the USART Interrupt
  NVIC_InitStructure.NVIC_IRQChannel = usartMap[serial]->usart_int_n;
  NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;

  NVIC_Init(&NVIC_InitStructure);

  // Disable USART Clock
  *usartMap[serial]->usart_apbReg &= ~usartMap[serial]->usart_clock_en;

  // clear any received data
  usartMap[serial]->usart_rx_buffer->head = usartMap[serial]->usart_rx_buffer->tail;

  // Undo any pin re-mapping done for this USART
  GPIO_PinRemapConfig(usartMap[serial]->usart_pin_remap, DISABLE);

  memset(usartMap[serial]->usart_rx_buffer, 0, sizeof(Ring_Buffer));
  memset(usartMap[serial]->usart_tx_buffer, 0, sizeof(Ring_Buffer));

  usartMap[serial]->usart_enabled = false;
  usartMap[serial]->usart_transmitting = false;
}
Example #2
0
/******************************************************************************
 * USART1 Initialization Code Template
******************************************************************************/
void USART1__Init()
{
    USART_InitTypeDef USART_InitStruct;
    
    #if (STRCMP($modeSelect$, USART_MODE_SYNCHRONOUS) == 1)
    //PUT_A_NEW_LINE_HERE
    USART_ClockInitTypeDef USART_ClockInitStruct;
    #endif
    
    #if (!STRCMP($USARTIntSet$, 0))
    NVIC_InitTypeDef NVIC_InitStructure;
	#endif
	
    //PUT_A_NEW_LINE_HERE
    //
    // Enable peripheral clock of USART1 and GPIOA
    //
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
   
    //PUT_A_NEW_LINE_HERE	
    //
    // USART Config
    //	
    USART_InitStruct.USART_BaudRate = $baudRate$;
    USART_InitStruct.USART_WordLength = $dataBits$;
    USART_InitStruct.USART_StopBits = $stopBits$;
    USART_InitStruct.USART_Parity = $parityBits$;
    USART_InitStruct.USART_Mode = $modeSet$;
    USART_InitStruct.USART_HardwareFlowControl = $HardwareFlowControl$;
    USART_Init(USART1, &USART_InitStruct);  
  
    #if ($guardTime$ != 0)
    //PUT_A_NEW_LINE_HERE	
    //
    // Set Guard Time
    //
    USART_SetGuardTime(USART1, $guardTime$);
    #endif
	
    //PUT_A_NEW_LINE_HERE
    USART_WakeUpConfig(USART1, $WakeUpmethod$);
	
    //PUT_A_NEW_LINE_HERE	
    #if (STRCMP($modeSelect$, USART_MODE_NORMAL) == 1)
    
    #elif (STRCMP($modeSelect$, USART_MODE_IRDA) == 1)
    //PUT_A_NEW_LINE_HERE
    //
    //USART IrDA  Mode Config
    //
    USART_IrDAConfig(USART1, $IrDAModeSel$);
    USART_IrDACmd(USART1, ENABLE);
    
    #elif (STRCMP($modeSelect$, USART_MODE_DMA) == 1)
    //PUT_A_NEW_LINE_HERE
    //
    //USART DMA Mode Config
    //
    USART_DMACmd(USART1, $DMAReq$, ENABLE);
	
    #elif (STRCMP($modeSelect$, USART_MODE_SYNCHRONOUS) == 1)
    //PUT_A_NEW_LINE_HERE
    //
    // USART Synchronous Mode Config
    //
    USART_Clock_InitStruct.USART_Clock = USART_CLOCK_ENABLE;
    USART_Clock_InitStruct.USART_CPOL = $synClockPolSet$;
    USART_Clock_InitStruct.USART_CPHA = $synClockPhaseSet$;
    USART_Clock_InitStruct.USART_LastBit = $lastbitenable$;
    USART_ClockInit(USART1, &USART_Clock_InitStruct);
	
    #elif (STRCMP($modeSelect$, USART_MODE_LIN) == 1)
    //PUT_A_NEW_LINE_HERE
    //
    // USART LIN Mode Config
    //
    USART_LINBreakDetectLengthConfig(USART1, $LINBreakDetectLen$);
    USART_LINCmd(USART1, ENABLE);
	
    #elif (STRCMP($modeSelect$, USART_MODE_SMARTCARD) == 1)
    //PUT_A_NEW_LINE_HERE
    //
    // USART SmartCard Mode Config
    //
    USART_SmartCardCmd(USART1, ENABLE);
    #elif (STRCMP($NACKTrans$, ENABLE) == 1)
    //PUT_A_NEW_LINE_HERE
    USART_SmartCardNACKCmd(USART1, ENABLE);
	
    #elif (STRCMP($modeSelect$, USART_MODE_HALFDUPLEX) == 1)
    //PUT_A_NEW_LINE_HERE
    //
    // USART HalfDuplex Mode Config
    //
    USART_HalfDuplexCmd(USART1, ENABLE);	
    #endif
    
    
    #if (!STRCMP($USARTIntSet$, 0))
    //PUT_A_NEW_LINE_HERE
    //
    // Enable the USART Interrupt Function 
    //
    USART_ITConfig(USART1, $USARTIntSet$, ENABLE);
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    #endif
}
Example #3
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;
}