Пример #1
0
static void uartReconfigure(uartPort_t *uartPort)
{
    USART_InitTypeDef USART_InitStructure;
    USART_Cmd(uartPort->USARTx, DISABLE);

    USART_InitStructure.USART_BaudRate = uartPort->port.baudRate;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;

    USART_InitStructure.USART_StopBits = (uartPort->port.options & SERIAL_STOPBITS_2) ? USART_StopBits_2 : USART_StopBits_1;
    USART_InitStructure.USART_Parity   = (uartPort->port.options & SERIAL_PARITY_EVEN) ? USART_Parity_Even : USART_Parity_No;

    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = 0;
    if (uartPort->port.mode & MODE_RX)
        USART_InitStructure.USART_Mode |= USART_Mode_Rx;
    if (uartPort->port.mode & MODE_TX)
        USART_InitStructure.USART_Mode |= USART_Mode_Tx;

    USART_Init(uartPort->USARTx, &USART_InitStructure);

    usartConfigurePinInversion(uartPort);

    if(uartPort->port.options & SERIAL_BIDIR)
        USART_HalfDuplexCmd(uartPort->USARTx, ENABLE);
    else
        USART_HalfDuplexCmd(uartPort->USARTx, DISABLE);

    USART_Cmd(uartPort->USARTx, ENABLE);
}
Пример #2
0
/**
 * Hardware initialization.
 */
void OW_Init(void) {
    GPIO_InitTypeDef GPIO_InitStruct;
    USART_InitTypeDef USART_InitStructure;

    /* Enable clock for periphetials */
    OW_GPIO_TX_CLOCK();

#ifndef OW_USE_SINGLE_PIN
    OW_GPIO_RX_CLOCK();
#endif

    OW_USART_CLOCK();

    /* Alternate function config on TX pin */
    GPIO_PinAFConfig(OW_TX_PIN_PORT, OW_TX_PIN_SOURCE, OW_USART_AF);

    /* TX pin configuration */
    GPIO_InitStruct.GPIO_Pin = OW_TX_PIN_PIN;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(OW_TX_PIN_PORT, &GPIO_InitStruct);

#ifndef OW_USE_SINGLE_PIN 
    /* Alternate function config on RX pin */
    GPIO_PinAFConfig(OW_RX_PIN_PORT, OW_RX_PIN_SOURCE, OW_USART_AF);
    GPIO_InitStruct.GPIO_Pin = OW_RX_PIN_PIN;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(OW_RX_PIN_PORT, &GPIO_InitStruct);
#endif

    /* USART configuration */
    USART_InitStructure.USART_BaudRate = 115200;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
    USART_Init(OW_USART, &USART_InitStructure);

    /* BRR register backup for 115200 Baud */
    iUSART115200 = OW_USART->BRR;

    /* BRR register backup for 9600 Baud */
    USART_StructInit(&USART_InitStructure);
    USART_InitStructure.USART_BaudRate = 9600;
    USART_Init(OW_USART, &USART_InitStructure);
    iUSART9600 = OW_USART->BRR;

#ifdef OW_USE_SINGLE_PIN 
    /* Half duplex enable, for single pin communication */
    USART_HalfDuplexCmd(OW_USART, ENABLE);
#endif

    /* USART enable */
    USART_Cmd(OW_USART, ENABLE);
}
Пример #3
0
/**
 * Set RX/TX pin into weak pull-up state.
 */
void OW_WeakPullUp(void) {
#ifdef OW_USE_PARASITE_POWER

    GPIO_SetBits(OW_TX_PIN_PORT, OW_TX_PIN_PIN);
    OW_TX_PIN_PORT->OTYPER |= OW_TX_PIN_PIN;

#ifdef OW_USE_SINGLE_PIN 
    USART_HalfDuplexCmd(OW_USART, ENABLE);
#endif

#endif
}
Пример #4
0
/**
 * Set RX/TX pin into strong pull-up state.
 */
void OW_StrongPullUp(void) {
#ifdef OW_USE_PARASITE_POWER

#ifdef OW_USE_SINGLE_PIN 
    USART_HalfDuplexCmd(OW_USART, DISABLE);
#endif

    GPIO_SetBits(OW_TX_PIN_PORT, OW_TX_PIN_PIN);
    OW_TX_PIN_PORT->OTYPER &= ~(OW_TX_PIN_PIN);

#endif
}
Пример #5
0
void HAL_USART_Half_Duplex(HAL_USART_Serial serial, bool Enable)
{
  if (HAL_USART_Is_Enabled(serial)) {
    USART_Cmd(usartMap[serial]->usart_peripheral, DISABLE);
  }
  if (Enable) {
    usartMap[serial]->usart_config |= SERIAL_HALF_DUPLEX;
  } else {
    usartMap[serial]->usart_config &= ~(SERIAL_HALF_DUPLEX);
  }
  HAL_USART_Configure_Pin_Modes(serial, usartMap[serial]->usart_config);
  // These bits need to be cleared according to the reference manual
  usartMap[serial]->usart_peripheral->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN);
  usartMap[serial]->usart_peripheral->CR3 &= ~(USART_CR3_IREN | USART_CR3_SCEN);
  USART_HalfDuplexCmd(usartMap[serial]->usart_peripheral, Enable ? ENABLE : DISABLE);
  if (HAL_USART_Is_Enabled(serial)) {
    USART_Cmd(usartMap[serial]->usart_peripheral, ENABLE);
  }

}
Пример #6
0
void uart2_init(u32 bound){
    //GPIO端口设置
    GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);	//使能USART2,GPIOA时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
 	USART_DeInit(USART2);  //复位串口2
	 //USART2_TX   PA.2
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.2
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	//复用推挽输出
    GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA2
   
    //USART2_RX	  PA.3
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
    GPIO_Init(GPIOA, &GPIO_InitStructure);  //初始化PA3
#if 1
   //Usart2 NVIC 配置
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//抢占优先级3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;		//子优先级3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ通道使能
	NVIC_Init(&NVIC_InitStructure);	//根据指定的参数初始化VIC寄存器
  
   //USART 初始化设置
	USART_InitStructure.USART_BaudRate = bound;//一般设置为9600;
	USART_InitStructure.USART_WordLength = USART_WordLength_9b;//字长为9位数据格式
	USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
	USART_InitStructure.USART_Parity = USART_Parity_Even;//偶校验位
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;	//收发模式
#endif

    USART_Init(USART2, &USART_InitStructure); //初始化串口
    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启中断
	USART_HalfDuplexCmd(USART2, ENABLE);//半双工
    USART_Cmd(USART2, ENABLE);                    //使能串口 
}
Пример #7
0
serialPort_t *uartOpen(USART_TypeDef *USARTx, serialReceiveCallbackPtr callback, uint32_t baudRate, portMode_t mode, serialInversion_e inversion)
{
    uartPort_t *s = NULL;

    if (USARTx == USART1) {
        s = serialUSART1(baudRate, mode);
#ifdef USE_USART2
    } else if (USARTx == USART2) {
        s = serialUSART2(baudRate, mode);
#endif
#ifdef USE_USART3
    } else if (USARTx == USART3) {
        s = serialUSART3(baudRate, mode);
#endif
    } else {
        return (serialPort_t *)s;
    }
    s->txDMAEmpty = true;

    // common serial initialisation code should move to serialPort::init()
    s->port.rxBufferHead = s->port.rxBufferTail = 0;
    s->port.txBufferHead = s->port.txBufferTail = 0;
    // callback works for IRQ-based RX ONLY
    s->port.callback = callback;
    s->port.mode = mode;
    s->port.baudRate = baudRate;
    s->port.inversion = inversion;

    uartReconfigure(s);

    // Receive DMA or IRQ
    DMA_InitTypeDef DMA_InitStructure;
    if ((mode & MODE_RX) || (mode & MODE_BIDIR)) {
        if (s->rxDMAChannel) {
            DMA_StructInit(&DMA_InitStructure);
            DMA_InitStructure.DMA_PeripheralBaseAddr = s->rxDMAPeripheralBaseAddr;
            DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
            DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
            DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
            DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
            DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
            DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;

            DMA_InitStructure.DMA_BufferSize = s->port.rxBufferSize;
            DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
            DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
            DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)s->port.rxBuffer;
            DMA_DeInit(s->rxDMAChannel);
            DMA_Init(s->rxDMAChannel, &DMA_InitStructure);
            DMA_Cmd(s->rxDMAChannel, ENABLE);
            USART_DMACmd(s->USARTx, USART_DMAReq_Rx, ENABLE);
            s->rxDMAPos = DMA_GetCurrDataCounter(s->rxDMAChannel);
        } else {
            USART_ClearITPendingBit(s->USARTx, USART_IT_RXNE);
            USART_ITConfig(s->USARTx, USART_IT_RXNE, ENABLE);
        }
    }

    // Transmit DMA or IRQ
    if ((mode & MODE_TX) || (mode & MODE_BIDIR)) {
        if (s->txDMAChannel) {
            DMA_StructInit(&DMA_InitStructure);
            DMA_InitStructure.DMA_PeripheralBaseAddr = s->txDMAPeripheralBaseAddr;
            DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
            DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
            DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
            DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
            DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
            DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;

            DMA_InitStructure.DMA_BufferSize = s->port.txBufferSize;
            DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
            DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
            DMA_DeInit(s->txDMAChannel);
            DMA_Init(s->txDMAChannel, &DMA_InitStructure);
            DMA_ITConfig(s->txDMAChannel, DMA_IT_TC, ENABLE);
            DMA_SetCurrDataCounter(s->txDMAChannel, 0);
            s->txDMAChannel->CNDTR = 0;
            USART_DMACmd(s->USARTx, USART_DMAReq_Tx, ENABLE);
        } else {
            USART_ITConfig(s->USARTx, USART_IT_TXE, ENABLE);
        }
    }

    USART_Cmd(s->USARTx, ENABLE);

    if (mode & MODE_BIDIR)
        USART_HalfDuplexCmd(s->USARTx, ENABLE);
    else
        USART_HalfDuplexCmd(s->USARTx, DISABLE);

    return (serialPort_t *)s;
}
Пример #8
0
/**
  * @brief   Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f10x.c file
     */     
       
  /* System Clocks Configuration */
  RCC_Configuration();

  /* Configure the GPIO ports */
  GPIO_Configuration();

/* USARTy and USARTz configuration -------------------------------------------*/
  /* USARTy and USARTz configured as follow:
        - BaudRate = 230400 baud  
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled

  */
  USART_InitStructure.USART_BaudRate = 230400;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  /* Configure USARTy */
  USART_Init(USARTy, &USART_InitStructure);
  /* Configure USARTz */
  USART_Init(USARTz, &USART_InitStructure);
  
  /* Enable the USARTy */
  USART_Cmd(USARTy, ENABLE);
  /* Enable the USARTz */
  USART_Cmd(USARTz, ENABLE);

  /* Enable USARTy Half Duplex Mode*/
  USART_HalfDuplexCmd(USARTy, ENABLE);
  /* Enable USARTz Half Duplex Mode*/
  USART_HalfDuplexCmd(USARTz, ENABLE);

  while(NbrOfDataToRead2--)
  {
    /* Wait until end of transmit */
    while(USART_GetFlagStatus(USARTy, USART_FLAG_TXE) == RESET)
    {
    }
    /* Write one byte in the USARTy Transmit Data Register */
    USART_SendData(USARTy, TxBuffer1[TxCounter1++]);

    /* Wait the byte is entirtly received by USARTz */  
    while(USART_GetFlagStatus(USARTz, USART_FLAG_RXNE) == RESET)
    {
    }
    /* Store the received byte in the RxBuffer2 */
    RxBuffer2[RxCounter2++] = USART_ReceiveData(USARTz);
  }

  /* Clear the USARTy Data Register */
  USART_ReceiveData(USARTy);

  while(NbrOfDataToRead1--)
  { 
    /* Wait until end of transmit */
    while(USART_GetFlagStatus(USARTz, USART_FLAG_TXE)== RESET)
    {
    }
    /* Write one byte in the USARTz Transmit Data Register */
    USART_SendData(USARTz, TxBuffer2[TxCounter2++]);

    /* Wait the byte is entirtly received by USARTy */
    while(USART_GetFlagStatus(USARTy,USART_FLAG_RXNE) == RESET)
    {
    }
    /* Store the received byte in the RxBuffer1 */
    RxBuffer1[RxCounter1++] = USART_ReceiveData(USARTy);
  }
  
  /* Check the received data with the send ones */
  TransferStatus1 = Buffercmp(TxBuffer1, RxBuffer2, TxBufferSize1);
  /* TransferStatus = PASSED, if the data transmitted from USARTy and  
     received by USARTz are the same */
  /* TransferStatus = FAILED, if the data transmitted from USARTy and 
     received by USARTz are different */
  TransferStatus2 = Buffercmp(TxBuffer2, RxBuffer1, TxBufferSize2);
  /* TransferStatus = PASSED, if the data transmitted from USARTz and  
     received by USARTy are the same */
  /* TransferStatus = FAILED, if the data transmitted from USARTz and 
     received by USARTy are different */

  while (1)
  {
  }
}
Пример #9
0
/**
* Initialise a single USART device
*/
int32_t PIOS_USART_Init(uintptr_t * usart_id, const struct pios_usart_cfg * cfg)
{
	PIOS_DEBUG_Assert(usart_id);
	PIOS_DEBUG_Assert(cfg);

	struct pios_usart_dev * usart_dev;

	usart_dev = (struct pios_usart_dev *) PIOS_USART_alloc();
	if (!usart_dev) goto out_fail;

	/* Bind the configuration to the device instance */
	usart_dev->cfg = cfg;

	/* Map pins to USART function */
	if (usart_dev->cfg->remap) {
		if (usart_dev->cfg->rx.gpio != 0)
			GPIO_PinAFConfig(usart_dev->cfg->rx.gpio,
				usart_dev->cfg->rx.pin_source,
				usart_dev->cfg->remap);
		if (usart_dev->cfg->tx.gpio != 0)
			GPIO_PinAFConfig(usart_dev->cfg->tx.gpio,
				usart_dev->cfg->tx.pin_source,
				usart_dev->cfg->remap);
	}

	/* Initialize the USART Rx and Tx pins */
	if (usart_dev->cfg->rx.gpio != 0)
		GPIO_Init(usart_dev->cfg->rx.gpio, (GPIO_InitTypeDef *)&usart_dev->cfg->rx.init);
	if (usart_dev->cfg->tx.gpio != 0)
		GPIO_Init(usart_dev->cfg->tx.gpio, (GPIO_InitTypeDef *)&usart_dev->cfg->tx.init);

	/* Apply inversion and swap settings */
	if (usart_dev->cfg->rx_invert == true)
		USART_InvPinCmd(usart_dev->cfg->regs, USART_InvPin_Rx, ENABLE);
	else
		USART_InvPinCmd(usart_dev->cfg->regs, USART_InvPin_Rx, DISABLE);

	if (usart_dev->cfg->tx_invert == true)
		USART_InvPinCmd(usart_dev->cfg->regs, USART_InvPin_Tx, ENABLE);
	else
		USART_InvPinCmd(usart_dev->cfg->regs, USART_InvPin_Tx, DISABLE);

	if (usart_dev->cfg->rxtx_swap == true)
		USART_SWAPPinCmd(usart_dev->cfg->regs, ENABLE);
	else
		USART_SWAPPinCmd(usart_dev->cfg->regs, DISABLE);

	if (usart_dev->cfg->single_wire == true)
		USART_HalfDuplexCmd(usart_dev->cfg->regs, ENABLE);
	else
		USART_HalfDuplexCmd(usart_dev->cfg->regs, DISABLE);

	/* Configure the USART */
	USART_Init(usart_dev->cfg->regs, (USART_InitTypeDef *)&usart_dev->cfg->init);

	*usart_id = (uintptr_t)usart_dev;

	/* Configure USART Interrupts */
	switch ((uint32_t)usart_dev->cfg->regs) {
	case (uint32_t)USART1:
		PIOS_USART_1_id = (uintptr_t)usart_dev;
		break;
	case (uint32_t)USART2:
		PIOS_USART_2_id = (uintptr_t)usart_dev;
		break;
	case (uint32_t)USART3:
		PIOS_USART_3_id = (uintptr_t)usart_dev;
		break;
	case (uint32_t)UART4:
		PIOS_UART_4_id = (uintptr_t)usart_dev;
		break;
	case (uint32_t)UART5:
		PIOS_UART_5_id = (uintptr_t)usart_dev;
		break;
	}
	NVIC_Init((NVIC_InitTypeDef *)&(usart_dev->cfg->irq.init));
	USART_ITConfig(usart_dev->cfg->regs, USART_IT_RXNE, ENABLE);
	USART_ITConfig(usart_dev->cfg->regs, USART_IT_TXE,  ENABLE);

	// FIXME XXX Clear / reset uart here - sends NUL char else

	/* Enable USART */
	USART_Cmd(usart_dev->cfg->regs, ENABLE);

	return(0);

out_fail:
	return(-1);
}
Пример #10
0
void HAL_USART_Half_Duplex(HAL_USART_Serial serial, bool Enable)
{
    USART_HalfDuplexCmd(usartMap[serial]->usart_peripheral, Enable ? ENABLE : DISABLE);
}
Пример #11
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
}
Пример #12
0
//-----------------------------------------------------------------------------
// инициализирует USART и DMA
//-----------------------------------------------------------------------------
uint8_t OW_Init() {
	GPIO_InitTypeDef GPIO_InitStruct;
	USART_InitTypeDef USART_InitStructure;

	if (OW_USART == USART1) {
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO,
				ENABLE);

		// USART TX
		GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
		GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD;
		GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

		GPIO_Init(GPIOA, &GPIO_InitStruct);

		// USART RX
/*		GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
		GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
		GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

		GPIO_Init(GPIOA, &GPIO_InitStruct);  */

		RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

		RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
	}

	if (OW_USART == USART2) {
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO,
				ENABLE);

		GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
		GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
		GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

		GPIO_Init(GPIOA, &GPIO_InitStruct);

		GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
		GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
		GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

		GPIO_Init(GPIOA, &GPIO_InitStruct);

		RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

		RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
	}

	USART_InitStructure.USART_BaudRate = 115200;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl =
			USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;

	USART_Init(OW_USART, &USART_InitStructure);
	USART_Cmd(OW_USART, ENABLE);

	USART_HalfDuplexCmd(OW_USART, ENABLE);

	return OW_OK;
}