Exemple #1
0
void setup_usart(void) {
    // enable clocks for usart2
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
    
    // Setup Alternate Functions to get usart2 out on PA2/PA3...
    GPIO_InitTypeDef usart_af;
    usart_af.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
    usart_af.GPIO_Mode = GPIO_Mode_AF;
    GPIO_Init(GPIOA, &usart_af);
    
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
    
    
    USART_ClockInitTypeDef usart_clocks;
    USART_ClockStructInit(&usart_clocks);
    usart_clocks.USART_Clock = USART_Clock_Enable;
    USART_ClockInit(USART2, &usart_clocks);
    
    USART_InitTypeDef usart_init;
    usart_init.USART_BaudRate = 57600;
    usart_init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    usart_init.USART_StopBits = USART_StopBits_1;
    usart_init.USART_Parity = USART_Parity_No;
    usart_init.USART_WordLength = USART_WordLength_8b;
    usart_init.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(USART2, &usart_init);
    USART_Cmd(USART2, ENABLE);
}
Exemple #2
0
void init_SCI(USART_TypeDef* USARTx,
              uint32_t USART_BaudRate,
              uint16_t USART_WordLength,
              uint16_t USART_StopBits,
              uint16_t USART_Parity,
              uint16_t USART_Mode,
              uint16_t USART_HardwareFlowControl)
{
   static uint16_t usartDeInitFlag=0;

   USART_InitTypeDef USART_InitStructure; //setup UART_InitStructure
   USART_ClockInitTypeDef USART_ClockInitStructure; //setup UART_ClockInitStructure
   USART_ClockStructInit(&USART_ClockInitStructure);

   if (usartDeInitFlag==0)
   {
      USART_DeInit(USARTx); //Deinit USARTx
      usartDeInitFlag=1;
   }

   USART_InitStructure.USART_BaudRate = USART_BaudRate;
   USART_InitStructure.USART_WordLength = USART_WordLength;
   USART_InitStructure.USART_StopBits = USART_StopBits;
   USART_InitStructure.USART_Parity = USART_Parity;
   USART_InitStructure.USART_Mode = USART_Mode;
   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl;

   USART_Init(USARTx, &USART_InitStructure);

   USART_ClockInit(USARTx, &USART_ClockInitStructure);

   USART_Cmd(USARTx, ENABLE);
}
Exemple #3
0
void Usart4Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    USART_ClockInitTypeDef USART_ClockInitStructure;

    //Set USART2 Tx (PA.02) as AF push-pull
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
    //Set USART2 Rx (PA.03) as input floating
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOC, &GPIO_InitStructure);


    USART_ClockStructInit(&USART_ClockInitStructure);
    USART_ClockInit(UART4, &USART_ClockInitStructure);
    USART_InitStructure.USART_BaudRate = 9600;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No ;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    //Write USART2 parameters
    USART_Init(UART4, &USART_InitStructure);
    //Enable UART4 Receive interrupt
    USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
    //Enable USART2
    USART_Cmd(UART4, ENABLE);
}
Exemple #4
0
void BoardConsoleInit() {
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

    GPIO_InitTypeDef gpiodef;
    gpiodef.GPIO_Pin = GPIO_Pin_9;
    gpiodef.GPIO_Mode = GPIO_Mode_AF_PP;
    gpiodef.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &gpiodef);
    gpiodef.GPIO_Pin = GPIO_Pin_10;
    gpiodef.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &gpiodef);

    USART_InitTypeDef usartdef;

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

    USART_Init(USART1, &usartdef);

    USART_ClockInitTypeDef clockdef;
    USART_ClockStructInit(&clockdef);
    USART_ClockInit(USART1, &clockdef);

    USART_Cmd(USART1, ENABLE);
}
Exemple #5
0
void USART3_Config(void) {
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

	USART_ClockInitTypeDef USART_ClockInitStructure;
	GPIO_InitTypeDef UARTTX;
	UARTTX.GPIO_Pin = GPIO_Pin_10;
	UARTTX.GPIO_Mode = GPIO_Mode_AF_PP;
	UARTTX.GPIO_Speed = GPIO_Speed_50MHz;

	GPIO_InitTypeDef UARTRX;
	UARTRX.GPIO_Pin = GPIO_Pin_11;
	UARTRX.GPIO_Mode = GPIO_Mode_IPU;
	UARTRX.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &UARTTX);
	GPIO_Init(GPIOB, &UARTRX);

	USART_ClockStructInit(&USART_ClockInitStructure);
	USART_ClockInit(USART3, &USART_ClockInitStructure);

	USART_InitTypeDef UART3;
	UART3.USART_BaudRate = 9600;
	UART3.USART_WordLength = USART_WordLength_8b;
	UART3.USART_StopBits = USART_StopBits_1;
	UART3.USART_Parity = USART_Parity_No;
	UART3.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	UART3.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

	USART_Init(USART3, &UART3);
	USART_Cmd(USART3, ENABLE);

	USART3_NVIC_Configuration();
}
Exemple #6
0
void UsartInit(USART_TypeDef *USART) {
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	USART_ClockInitTypeDef USART_ClockInitStructure;

	if (USART == USART1) {
		//Enable bus clocks
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
		//Set USART1 Tx (PA.09) as AF push-pull
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOA, &GPIO_InitStructure);
		//Set USART1 Rx (PA.10) as input floating
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
		GPIO_Init(GPIOA, &GPIO_InitStructure);
	} else if (USART == USART2) {
		//Enable bus clocks
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
		//Set USART2 Tx (PA.02) as AF push-pull
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOA, &GPIO_InitStructure);
		//Set USART1 Rx (PA.3) as input floating
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
		GPIO_Init(GPIOA, &GPIO_InitStructure);
	} else {
		//Enable bus clocks
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
		//Set USART1 Tx (PB.10) as AF push-pull
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOA, &GPIO_InitStructure);
		//Set USART1 Rx (PB.11) as input floating
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
		GPIO_Init(GPIOA, &GPIO_InitStructure);
	}

	//Set USART paramenhs
	USART_ClockStructInit(&USART_ClockInitStructure);
	USART_InitStructure.USART_BaudRate = 9600;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No ;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_ClockInit(USART1, &USART_ClockInitStructure);
	//Write USART1 parameters
	USART_Init(USART1, &USART_InitStructure);
	//Enable USART1
	USART_Cmd(USART1, ENABLE);
}
Exemple #7
0
void usart_setup(const usart_dev *dev, uint32_t baudRate, uint16_t wordLength,
	uint16_t stopBits, uint16_t parity, uint16_t mode, uint16_t hardwareFlowControl)
{
    /* Check the parameters */
    assert_param(IS_USART_ALL_PERIPH(dev->USARTx));
    assert_param(IS_USART_BAUDRATE(baud));
    assert_param(IS_USART_STOPBITS(stopbits));
    assert_param(IS_USART_PARITY(parity));
    assert_param(IS_USART_WORD_LENGTH(wordLength));
    assert_param(IS_USART_MODE(mode));
    assert_param(IS_USART_HARDWARE_FLOW_CONTROL(hardwareFlowControl));

    memset(dev->state, 0, sizeof(*dev->state));

    dev->state->txbusy = 0;
    dev->state->callback = 0;

    /* Disable USARTx */
    usart_disable(dev);

    rb_init(dev->txrb, USART_TX_BUF_SIZE, dev->state->tx_buf);
    rb_init(dev->rxrb, USART_RX_BUF_SIZE, dev->state->rx_buf);

    /* Enables the USART's 8x oversampling mode. */
    USART_OverSampling8Cmd(dev->USARTx, ENABLE);

    USART_ClockInitTypeDef USART_InitClock;
    USART_ClockStructInit(&USART_InitClock);
    USART_ClockInit(dev->USARTx, &USART_InitClock);

    USART_InitTypeDef USART_config;
    USART_StructInit(&USART_config);
    USART_config.USART_BaudRate = baudRate;
    USART_config.USART_WordLength = wordLength;
    USART_config.USART_StopBits = stopBits;
    USART_config.USART_Parity = parity;
    USART_config.USART_Mode = mode;
    USART_config.USART_HardwareFlowControl = hardwareFlowControl;

    USART_Init(dev->USARTx, &USART_config);

    dev->USARTx->CR1 &= ~(USART_MASK_IDLEIE | USART_MASK_RXNEIE | USART_MASK_TCEIE | USART_MASK_TXEIE | USART_MASK_PEIE);
    dev->USARTx->CR2 &= ~(USART_MASK2_LBDIE);
    dev->USARTx->CR3 &= ~(USART_MASK3_CTSIE | USART_MASK3_EIE);
    
    if(mode & USART_Mode_Rx) { /* Enable Rx request */
        USART_ClearFlag(dev->USARTx, USART_FLAG_RXNE);
        dev->USARTx->CR1 |= USART_MASK_RXNEIE;
    }

    if(mode & USART_Mode_Tx) {
        USART_ClearFlag(dev->USARTx, USART_FLAG_TC);
    }    

    enable_nvic_irq(dev->irq, UART_INT_PRIORITY);
}
Exemple #8
0
void usart1_init()
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
	GPIO_Init(GPIOA, (GPIO_InitTypeDef*) &usart_tx);
	GPIO_Init(GPIOA, (GPIO_InitTypeDef*) &usart_rx);
	USART_ClockStructInit(&USART_ClockInitStructure);
	USART_ClockInit(USART1, &USART_ClockInitStructure);
	USART_Init(USART1, &usart1);
	USART_Cmd(USART1, ENABLE);
}
int main(void) {
    int i = 10;

    GPIO_InitTypeDef gpio_init;
    USART_InitTypeDef usart_init;
    USART_ClockInitTypeDef usart_clk_init;

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

    // PA9 = Tx, PA10 = Rx
    gpio_init.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
    gpio_init.GPIO_Mode = GPIO_Mode_AF;
    gpio_init.GPIO_Speed = GPIO_Speed_40MHz;
    gpio_init.GPIO_OType = GPIO_OType_PP;
    gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &gpio_init);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);

    USART_ClockStructInit(&usart_clk_init);
    USART_ClockInit(USART1, &usart_clk_init);

    usart_init.USART_BaudRate =            9600;
    usart_init.USART_WordLength =          USART_WordLength_8b;
    usart_init.USART_StopBits =            USART_StopBits_1;
    usart_init.USART_Parity =              USART_Parity_No ;
    usart_init.USART_Mode =                USART_Mode_Rx | USART_Mode_Tx;
    usart_init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_Init(USART1, &usart_init);
    USART_Cmd(USART1,ENABLE);

    while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}

    while (1) {

        if ( usart_available() ) // data available
        {
            usart_print( "Data Available: " );
            uint8_t ch = usart_read();
            usart_write(ch);
            usart_print( "\r\n" );
        }

    }


    return 0;
}
int main(void)
{
  RCC_GetClocksFreq(&RCC_Clocks);
  SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);
    
  GPIO_InitTypeDef gpio_init;
  USART_InitTypeDef usart_init;
  USART_ClockInitTypeDef usart_clk_init;
 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
 
  // PA2 = Tx, PA3 = Rx
  gpio_init.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
  gpio_init.GPIO_Mode = GPIO_Mode_AF_PP;
  gpio_init.GPIO_Speed = GPIO_Speed_10MHz;
  GPIO_Init(GPIOA, &gpio_init);

  gpio_init.GPIO_Pin = GPIO_Pin_5;
  gpio_init.GPIO_Mode = GPIO_Mode_Out_PP;
  gpio_init.GPIO_Speed = GPIO_Speed_10MHz;
  GPIO_Init(GPIOA, &gpio_init);

  GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
  GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
 
  USART_ClockStructInit(&usart_clk_init);
  USART_ClockInit(USART2, &usart_clk_init);
 
  usart_init.USART_BaudRate =            9600;
  usart_init.USART_WordLength =          USART_WordLength_8b;
  usart_init.USART_StopBits =            USART_StopBits_1;
  usart_init.USART_Parity =              USART_Parity_No ;
  usart_init.USART_Mode =                USART_Mode_Rx | USART_Mode_Tx;
  usart_init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_Init(USART2, &usart_init);
  USART_Cmd(USART2,ENABLE);    
     
  while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET) {}        
 
  while (1)
  {
    usart_print( "UART Example" );

    // toggle led
    GPIOA->ODR ^= GPIO_Pin_5;
    Delay(500);
  }
}
Exemple #11
0
////////////////////////////////////////////////////硬件平台初始化
void UART1_HardwareInit(u32 baudRate)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    USART_ClockInitTypeDef USART_ClockInitStruct;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
    RCC_APB2PeriphClockCmd(UART1_RCC_APB2Periph, ENABLE);    //使能GPIO

    GPIO_InitStructure.GPIO_Pin = UART1_Tx_Pin;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(UART1_Port, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = UART1_Rx_Pin;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(UART1_Port, &GPIO_InitStructure);

    USART_InitStructure.USART_BaudRate = baudRate;//波特率设置
    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;
    USART_Init(USART1, &USART_InitStructure);

    USART_ClockStructInit(&USART_ClockInitStruct);    //之前没有填入缺省值,是不行的
    USART_ClockInit(USART1, &USART_ClockInitStruct);

    // Configure one bit for preemption priority
    NVIC_PriorityGroupConfig(Devices_PriorityGroup);
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = UART1_PreemptionPriority;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = UART1_SubPriority;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);                            //使能中断

    USART_Cmd(USART1, ENABLE);

    //装载发送中断
    UART1_SetTxISRFunc(UART1_TxDequeue);	//存入发送队列

	//装载接收中断
	UART1_SetRxISRFunc(UART1_RxData);	//存入接收队列
}
Exemple #12
0
// ============================================================================
void vUSART2_Init( void ) {
    USART_ClockInitTypeDef USART_ClockInitStruct;
    USART_InitTypeDef USART_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;

    // Enable needed clocks for uart.
    RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2, ENABLE );
    RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA, ENABLE );

    // Make sure you use 'GPIO_PinSource2' and NOT 'GPIO_Pin_2'.  Using the
    // latter will not work!
    GPIO_PinAFConfig( GPIOA, GPIO_PinSource2, GPIO_AF_USART2 );
    GPIO_PinAFConfig( GPIOA, GPIO_PinSource3, GPIO_AF_USART2 );

    // Setup Tx / Rx pins.
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;			// Tx Pin
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_Init( GPIOA, &GPIO_InitStructure );
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;			// Rx Pin
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init( GPIOA, &GPIO_InitStructure );

    // Make sure syncro clock is turned off.
    USART_ClockStructInit( &USART_ClockInitStruct );
    USART_ClockInit( USART2, &USART_ClockInitStruct  );

    // Setup transmit complete irq.
    USART_ITConfig( USART2, USART_IT_TC, ENABLE );

    // Use defaults (except baud rate).
    USART_StructInit( &USART_InitStructure );
    USART_InitStructure.USART_BaudRate = 115200;
    USART_Init( USART2, &USART_InitStructure );
    USART_Cmd( USART2, ENABLE );

    // Enable USART2 interrupt.
    /*
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    */
}
Exemple #13
0
void Usart1Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
//enable bus clocks
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
//Set USART1 Tx (PA.09) as AF push-pull
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
 
//Set USART1 Rx (PA.10) as input floating
 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
 
GPIO_Init(GPIOA, &GPIO_InitStructure);
 
USART_ClockStructInit(&USART_ClockInitStructure);
 
USART_ClockInit(USART1, &USART_ClockInitStructure);
 
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_Mode = USART_Mode_Rx | USART_Mode_Tx;
 
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
 
//Write USART1 parameters
 
USART_Init(USART1, &USART_InitStructure);
 
//Enable USART1
 
USART_Cmd(USART1, ENABLE);
 
}
Exemple #14
0
/*******************************************************************************************************************************
 * 函数名 :DC_USART_Conf
 * 参数   :void 
 * 返回值 :void 
 * 功能   :配置USART6,并初始化
 *******************************************************************************************************************************/
void DC_USART_Conf(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	USART_ClockInitTypeDef USART_ClockInitStruct;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE); //开启USART6时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); //开启GPIOC时钟
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);//
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);// 
	
	/*配置GPIOC*/
	/*配置GPIOC_Pin7为TX输入*/
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOC,&GPIO_InitStructure);
	
	/*配置GPIOC_Pin6为RX输出*/
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOC,&GPIO_InitStructure);
	
	/*配置USART6*/
	USART_StructInit(&USART_InitStructure);
	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_Rx | USART_Mode_Tx;
	USART_Init(USART6, &USART_InitStructure);
	
	USART_ClockStructInit(&USART_ClockInitStruct);
	USART_ClockInit(USART6, &USART_ClockInitStruct);
//	USART_ITConfig(USART6, USART_IT_RXNE, ENABLE); //使能 USART6中断
	USART_Cmd(USART6, ENABLE); //使能 USART6 
	
	USART_ClearFlag(USART6, USART_FLAG_TC);     /* 清发送完成标志,Transmission Complete flag */   
}
Exemple #15
0
void USART2_Init( void ) {
    USART_ClockInitTypeDef USART_ClockInitStruct;
    USART_InitTypeDef USART_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2, ENABLE );
    RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA, ENABLE );

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;            // Rx Pin
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // Make sure you use 'GPIO_PinSource2' and NOT 'GPIO_Pin_2'.  Using the
    // latter will not work!
    GPIO_PinAFConfig( GPIOA, GPIO_PinSource2, GPIO_AF_USART2 );
    GPIO_PinAFConfig( GPIOA, GPIO_PinSource3, GPIO_AF_USART2 );

    // Make sure syncro clock is turned off.
    USART_ClockStructInit( &USART_ClockInitStruct );
    USART_ClockInit( USART2, &USART_ClockInitStruct  );

    USART_StructInit( &USART_InitStructure );
    // Initialize USART
    USART_InitStructure.USART_BaudRate = 256000*8;
    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_Init( USART2, &USART_InitStructure );
    //USART2->BRR = 364;
    USART_Cmd( USART2, ENABLE );
}
Exemple #16
0
/*******************************************************************************
* Function Name  : UART3_Configuration
* Description    : UART3 配置(用于485通信,可根据需要修改波特率)
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void USART3_Configuration(void)
{
    USART_InitTypeDef USART_InitStructure;  
    USART_ClockInitTypeDef USART_ClockInitStructure;
    
    USART_InitStructure.USART_BaudRate = 9600;
    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 the USART3 */
    USART_Init(USART3, &USART_InitStructure);
    
    USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
    USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
    USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
    USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
    USART_ClockStructInit(&USART_ClockInitStructure);
}
Exemple #17
0
int uart_init(USART_TypeDef* USARTx, uint32_t baud, uint32_t flags) {
	GPIO_InitTypeDef GPIO_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

	if (USARTx == USART1) {
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

		GPIO_StructInit(&GPIO_InitStructure);
		if (flags & USART_Mode_Tx) {
			// UART1_Tx
			GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
			GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
			GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
			GPIO_Init(GPIOA, &GPIO_InitStructure);
		}
		if (flags & USART_Mode_Rx) {
			// UART1_Rx
			GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
			GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
			GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
			GPIO_Init(GPIOA, &GPIO_InitStructure);
		}
	}

	USART_InitTypeDef USART_InitStructure;
	USART_ClockInitTypeDef USART_ClockInitStructure;

	USART_StructInit(&USART_InitStructure);
	USART_ClockStructInit(&USART_ClockInitStructure);
	USART_ClockInit(USARTx, &USART_ClockInitStructure);
	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_BaudRate = baud;
	USART_InitStructure.USART_Mode = flags;
	USART_Init(USARTx, &USART_InitStructure);
	USART_Cmd(USARTx, ENABLE);
}
Exemple #18
0
void Usart4Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    USART_ClockInitTypeDef USART_ClockInitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);

    //Set USART4 Tx (PC10) as AF push-pull
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    //Set USART4 Rx (PC11) as input pull down
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
    GPIO_Init(GPIOC, &GPIO_InitStructure);


    USART_ClockStructInit(&USART_ClockInitStructure);
    USART_ClockInit(UART4, &USART_ClockInitStructure);
    //USART_InitStructure.USART_BaudRate = 9600;
    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_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_Init(UART4, &USART_InitStructure);

    //Enable UART4 Receive interrupt
    USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
    //Enable USART4
    USART_Cmd(UART4, ENABLE);

    InitUart4Buffer();
    InitUart4BufferIRQ();
}
Exemple #19
0
void usart_setup(usart_dev *dev, uint32_t baudRate, uint16_t wordLength,
	uint16_t stopBits, uint16_t parity, uint16_t mode,
	uint16_t hardwareFlowControl, uint32_t tx_timeout)
    {
    /* Check the parameters */
    assert_param(IS_USART_ALL_PERIPH(dev->USARTx));
    assert_param(IS_USART_BAUDRATE(baud));
    assert_param(IS_USART_STOPBITS(stopbits));
    assert_param(IS_USART_PARITY(parity));
    assert_param(IS_USART_WORD_LENGTH(wordLength));
    assert_param(IS_USART_MODE(mode));
    assert_param(IS_USART_HARDWARE_FLOW_CONTROL(hardwareFlowControl));

    dev->tx_timeout = tx_timeout;
    dev->txbusy = 0;
    dev->usetxrb = 1;
    dev->use_timeout = 0;

    /* Disable USARTx */
    USART_Cmd(dev->USARTx, DISABLE);

    /* Enables the USART's 8x oversampling mode. */
    USART_OverSampling8Cmd(dev->USARTx, ENABLE);

    USART_ClockInitTypeDef USART_InitClock;
    USART_ClockStructInit(&USART_InitClock);
    USART_ClockInit(dev->USARTx, &USART_InitClock);

    USART_InitTypeDef USART_config;
    USART_StructInit(&USART_config);
    USART_config.USART_BaudRate = baudRate;
    USART_config.USART_WordLength = wordLength;
    USART_config.USART_StopBits = stopBits;
    USART_config.USART_Parity = parity;
    USART_config.USART_Mode = mode;
    USART_config.USART_HardwareFlowControl = hardwareFlowControl;

    USART_Init(dev->USARTx, &USART_config);

    NVIC_InitTypeDef NVIC_InitStructure;
    /* Configure the NVIC Preemption Priority Bits */
    //NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);  
    /* Enable the USART Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = dev->irq;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    USART_ITConfig(dev->USARTx, USART_IT_PE, DISABLE);
    USART_ITConfig(dev->USARTx, USART_IT_IDLE, DISABLE);
    USART_ITConfig(dev->USARTx, USART_IT_LBD, DISABLE);
    if (IS_USART_1236_PERIPH(dev->USARTx))
	USART_ITConfig(dev->USARTx, USART_IT_CTS, DISABLE);
    USART_ITConfig(dev->USARTx, USART_IT_ERR, DISABLE);

    /* Enable USART2 Rx request */
    USART_ITConfig(dev->USARTx, USART_IT_RXNE, ENABLE);
    USART_ClearFlag(dev->USARTx, USART_FLAG_RXNE);

    USART_ITConfig(dev->USARTx, USART_IT_TC, DISABLE);
    USART_ITConfig(dev->USARTx, USART_IT_TXE, ENABLE);
    USART_ClearFlag(dev->USARTx, USART_FLAG_TC);

    /*
     USART_ITConfig(dev->USARTx, USART_IT_RXNE,  ENABLE);
     USART_ITConfig(dev->USARTx, USART_IT_PE,    ENABLE);
     USART_ITConfig(dev->USARTx, USART_IT_ERR,   ENABLE);
     */
    }
void UsartInit ()
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    USART_ClockInitTypeDef USART_ClockInitStructure;
 
        // Enable APB2 bus clock
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
    
    DelayUSART(2);
    
    //Set USART1 Tx (PB.10) as AF push-pull --> PC10
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    //Set USART1 Rx (PB.11) as input floating --> PC11
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
    
    USART_ClockStructInit(&USART_ClockInitStructure);
    USART_ClockInit(USART3, &USART_ClockInitStructure);
    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_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    
    //Write USART3 parameters
    USART_Init(USART3, &USART_InitStructure);
    //Enable USART3
    USART_Cmd(USART3, ENABLE);
    
        //configure NVIC
    NVIC_InitTypeDef NVIC_InitStructure;
    //select NVIC channel to configure
    NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
    //set priority to lowest
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
    //set subpriority to lowest
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
    //enable IRQ channel
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    //update NVIC registers
    NVIC_Init(&NVIC_InitStructure);
    //disable Transmit Data Register empty interrupt
  // USART_ITConfig(USART3, USART_IT_TXE, DISABLE);  // proveri!
    //enable Receive Data register not empty interrupt
    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
    USART_ITConfig(USART3, USART_IT_TC, ENABLE);
    
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_12;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;            // konfiguracija za alternativnu funkciju - Push Pull
    GPIO_Init( GPIOC, &GPIO_InitStructure );
    
    GPIO_ResetBits(GPIOC,GPIO_Pin_12); 
 
    GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE);

}
Exemple #21
0
void initUart() {
  RCC_APB2PeriphClockCmd(
      RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO,
      ENABLE);

  GPIO_InitTypeDef GPIO_InitStructure;
  GPIO_StructInit(&GPIO_InitStructure);

  // Transmit pin.
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9,
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  // Receive pin.
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  // Default clock structure works for us.
  USART_ClockInitTypeDef USART_ClockInitStructure;
  USART_ClockStructInit(&USART_ClockInitStructure);
  USART_ClockInit(USART1, &USART_ClockInitStructure);

  // Default USART structure works for us.
  USART_InitTypeDef USART_InitStructure;
  USART_StructInit(&USART_InitStructure);
  USART_Init(USART1, &USART_InitStructure);

  USART_Cmd(USART1, ENABLE);

//  // Disable data pushes from GPS.
//  usartTx("$PUBX,40,GGA,0,0,0,0*5A\r\n");
//  usartTx("$PUBX,40,GGA,0,0,0,0*5A\r\n");  // Repeat first; timing work around.
//  usartTx("$PUBX,40,GLL,0,0,0,0*5C\r\n");
//  usartTx("$PUBX,40,GSA,0,0,0,0*4E\r\n");
//  usartTx("$PUBX,40,GSV,0,0,0,0*59\r\n");
//  usartTx("$PUBX,40,RMC,0,0,0,0*47\r\n");
//  usartTx("$PUBX,40,VTG,0,0,0,0*5E\r\n");
//  // Keep just the recommended minimum flowing, slowly.
//  usartTx("$PUBX,40,GGA,0,15,0,0*6E\r\n");
//  usartTx("$PUBX,40,GGA,0,15,0,0*6E\r\n");  // Repeat first; timing work around.
//  usartTx("$PUBX,40,GLL,0,15,0,0*68\r\n");
//  usartTx("$PUBX,40,GSA,0,15,0,0*7A\r\n");
//  usartTx("$PUBX,40,GSV,0,15,0,0*6D\r\n");
//  usartTx("$PUBX,40,RMC,0,15,0,0*73\r\n");
//  usartTx("$PUBX,40,VTG,0,15,0,0*6A\r\n");

  // The first character is sometimes (always?) dropped, so send a few
  // buffer characters that can be safely discarded.
  gpsTxChar('\r'); gpsTxChar('\n');
  gpsTxChar('\r'); gpsTxChar('\n');

  // Disable all data pushes from GPS.
  gpsSetPushFreq("DTM", 0);
  gpsSetPushFreq("GBS", 0);
  gpsSetPushFreq("GGA", 0);
  gpsSetPushFreq("GLL", 0);
  gpsSetPushFreq("GPQ", 0);
  gpsSetPushFreq("GRS", 0);
  gpsSetPushFreq("GSA", 0);
  gpsSetPushFreq("GST", 0);
  gpsSetPushFreq("GSV", 0);
  gpsSetPushFreq("RMC", 0);
  gpsSetPushFreq("THS", 0);
  gpsSetPushFreq("VTG", 0);
  // Except the one which tells us what time it is.
  gpsSetPushFreq("ZDA", 2);

  // Now that it won't be noisy, enable the RX interrupt.
  NVIC_InitTypeDef NVIC_InitStructure = {
    .NVIC_IRQChannel = USART1_IRQn,
    .NVIC_IRQChannelPreemptionPriority = 1,
    .NVIC_IRQChannelSubPriority = 1,
    .NVIC_IRQChannelCmd = ENABLE,
  };
  NVIC_Init(&NVIC_InitStructure);
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
}

// \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ //

void RTC_IRQHandler(void) {
  if (RTC_GetFlagStatus(RTC_FLAG_SEC) != RESET) {
    RTC_ClearFlag(RTC_FLAG_SEC);
    gSecondFlag = 1;
    gSeconds = RTC_GetCounter();
  }
}


void SysTick_Handler(void) {
  gBlinkTick++;
  if (gBlinkTick > 125) {
    gBlinkTick = 0;
    gBlinkStatus = !gBlinkStatus;
  }

  uint8_t btnPressed = 0;
  uint16_t btns = GPIO_ReadInputData(BTN_PORT) & BTN_ALL_PINS;
  if ((btns & BTN_MAPLE_PIN) == BTN_MAPLE_PIN) btnPressed = BTN_MAPLE;
  else if ((btns & BTN_DIM_PIN) == 0) btnPressed = BTN_DIM;
  else if ((btns & BTN_DN_PIN) == 0) btnPressed = BTN_DOWN;
  else if ((btns & BTN_UP_PIN) == 0) btnPressed = BTN_UP;
  else if ((btns & BTN_SET_PIN) == 0) btnPressed = BTN_SET;
  if (btnPressed && gButtonPending == btnPressed) {
    if (gButtonDebounce > 10) {
      // Button already activated, still held, do nothing.
    } else if (gButtonDebounce == 10) {
      // Transition!  Set pressed button.
      gButtonPressed = btnPressed;
      gButtonDebounce++;
    } else {
      gButtonDebounce++;
    }
  } else {
    gButtonDebounce = 0;
    gButtonPending = btnPressed;
  }

  if (gDpTick > 0) {
    gDpTick--;
    if (gDpTick == 0) {
      GPIO_WriteBit(DP_PORT, DP_PIN, RESET);
    }
  }
}
Exemple #22
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_OCInitTypeDef  TIM_OCInitStructure;
  
  BufferInit(&TxRingBuf);
  BufferInit(&RxRingBuf);


  /*!< At this stage the microcontroller clock setting is already configured, 
  this is done through SystemInit() function which is called from startup
  file (startup_stm32f401xx.s) before to branch to application main.
  To reconfigure the default setting of SystemInit() function, refer to
  system_stm32f4xx.c file
  */
  
  /* TIM Configuration */
  //TIM_Config();
  
  /* -----------------------------------------------------------------------
  TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles.
  
  In this example TIM3 input clock (TIM3CLK) is set to 2 * APB1 clock (PCLK1), 
  since APB1 prescaler is different from 1.   
  TIM3CLK = 2 * PCLK1  
  PCLK1 = HCLK / 4 
  => TIM3CLK = HCLK / 2 = SystemCoreClock /2
  
  To get TIM3 counter clock at 14 MHz, the prescaler is computed as follows:
  Prescaler = (TIM3CLK / TIM3 counter clock) - 1
  Prescaler = ((SystemCoreClock /2) /14 MHz) - 1
  
  To get TIM3 output clock at 21 KHz, the period (ARR)) is computed as follows:
  ARR = (TIM3 counter clock / TIM3 output clock) - 1
  = 665
  
  TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
  TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
  TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
  TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
  
  Note: 
  SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f4xx.c file.
  Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()
  function to update SystemCoreClock variable value. Otherwise, any configuration
  based on this variable will be incorrect.    
  ----------------------------------------------------------------------- */  
  //===========================================================
  //UART config
  //===========================================================

  USART_InitTypeDef UartHandle;
  GPIO_InitTypeDef GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  USART_ClockInitTypeDef USART_ClockInitStruct;
  RCC_ClocksTypeDef RCC_Clocks;
  
  RCC_GetClocksFreq(&RCC_Clocks);
  SysTick_Config(RCC_Clocks.HCLK_Frequency / 100);

  USART_StructInit(&UartHandle);
  USART_ClockStructInit(&USART_ClockInitStruct);

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

  GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2);



  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOD, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOD, &GPIO_InitStructure);

  USART_Init(USART2,&UartHandle);

  USART_ClockInit(USART2,&USART_ClockInitStruct);
  USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
  USART_ITConfig(USART2, USART_IT_TC,ENABLE);


  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  USART_Cmd(USART2, ENABLE);
  Delay(40);




  Sevenseg_Setup();
  Delay(40);



  USART_SendData(USART2,0xaaaa);
  //GPIO_SetBits(GPIOB,GPIO_Pin_0 | GPIO_Pin_1);
  GPIO_SetBits(GPIOD,GPIO_Pin_0);
  //======================================================
  //----------------------------------------------------
  //========================================================






//  /* Compute the prescaler value */
//  PrescalerValue = (uint16_t) (SystemCoreClock / 1000000) - 1;
//
//  /* Time base configuration */
//  TIM_TimeBaseStructure.TIM_Period = 5000;
//  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
//  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
//  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
//
//  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
//
//  /* PWM1 Mode configuration: Channel1 */
//  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
//  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
//  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
//  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
//
//  TIM_OC1Init(TIM3, &TIM_OCInitStructure);
//
//  TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
//
//
//  /* PWM1 Mode configuration: Channel2 */
//  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
//  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
//
//  TIM_OC2Init(TIM3, &TIM_OCInitStructure);
//
//  TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
//
//  /* PWM1 Mode configuration: Channel3 */
//  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
//  TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
//
//  TIM_OC3Init(TIM3, &TIM_OCInitStructure);
//
//  TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
//
//  /* PWM1 Mode configuration: Channel4 */
//  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
//  TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
//
//  TIM_OC4Init(TIM3, &TIM_OCInitStructure);
//
//  TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
//
//  TIM_ARRPreloadConfig(TIM3, ENABLE);
//
//  /* TIM3 enable counter */
//  TIM_Cmd(TIM3, ENABLE);
  int bts = 0;



  Sevenseg_Send(0x33);

  while (1)
  {

	if(RxRingBuf.length > 10) {
		int i;
		int ntosend = RxRingBuf.length;
		for(i = 0; i < ntosend;i++)
		{
			bts = BufferRead(&RxRingBuf);
			//if(bts == -1) break;
			USART2_SendByte(bts);
		}
		/*
		while(TxRingBuf.length > 0)
		{
			if((TxRingBuf.length > 0) && (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == SET)) {
				BufferSend(&TxRingBuf);
			}
		}
		*/
	}
	  /*
	  GPIO_ResetBits(GPIOB,GPIO_Pin_0 | GPIO_Pin_1);
	  GPIO_ResetBits(GPIOA,GPIO_Pin_0);


	  //GPIO_ToggleBits(GPIOB, GPIO_Pin_0);
	  GPIO_SetBits(GPIOB,GPIO_Pin_0 | GPIO_Pin_1);
	  GPIO_SetBits(GPIOA,GPIO_Pin_0);
	  */
	  //USART_SendData(USART2,0x0051);
	  //USART_SendData(USART2,0x003b);
  }
}
Exemple #23
0
/*
*********************************************************************************************************
*	函 数 名: USART6_Init
*	功能说明: 初始化CPU的USART6串口硬件设备。预留
*	形    参:无
*	返 回 值: 无
*********************************************************************************************************
*/
void USART6_Init(_UART_BAUD BaudRate)
{
	USART_InitTypeDef USART_InitStructure;   
	GPIO_InitTypeDef GPIO_InitStructure;
	NVIC_InitTypeDef   NVIC_InitStructure;
	USART_ClockInitTypeDef USART_ClockInitStruct;
	
	RCC_AHB1PeriphClockCmd(USART6_TX_PORT_CLK | USART6_RX_PORT_CLK , ENABLE);
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
	
	/* 配置发送管脚*/
  GPIO_PinAFConfig(USART6_TX_PORT, USART6_TX_SOURCE, GPIO_AF_USART6);

  /*配置接收管脚*/
  GPIO_PinAFConfig(USART6_RX_PORT, USART6_RX_SOURCE, GPIO_AF_USART6);

  /* Configure USART Tx as alternate function  */
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_SPEED;
  GPIO_InitStructure.GPIO_Pin = USART6_TX_PIN;  
  GPIO_Init(USART6_TX_PORT, &GPIO_InitStructure);

  /* Configure USART Rx as alternate function  */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Pin = USART6_RX_PIN;
  GPIO_Init(USART6_RX_PORT, &GPIO_InitStructure);
	

       
  /* USARTx configured as follow:
        - BaudRate = 115200 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 = BaudRate;
  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;
	USART_Init(USART6, &USART_InitStructure);
	USART_ClockStructInit(&USART_ClockInitStruct);    //之前没有填入缺省值,是不行的
  USART_ClockInit(USART6, &USART_ClockInitStruct);
	
	NVIC_InitStructure.NVIC_IRQChannel =USART6_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  
	#if UART6_DMA_RX_ENABLE
	/*空闲中断*/
  USART_ITConfig(USART6, USART_IT_IDLE , ENABLE);
  #else
	USART_ITConfig(USART6, USART_IT_RXNE | USART_IT_IDLE , ENABLE);
	#endif
	/* Enable USART */
  USART_Cmd(USART6, ENABLE);
  
  /* 
		CPU的小缺陷:串口配置好,如果直接Send,则第1个字节发送不出去
		如下语句解决第1个字节无法正确发送出去的问题:
	 	清发送完成标志,Transmission Complete flag 
	*/
	USART_ClearFlag(USART6, USART_FLAG_TC); 
	
	memset((u8*)&UART6_Str,0x00,sizeof(UART6_Str));
	#if UART6_DMA_RX_ENABLE
	  USART6_RX_DMA();
	#endif
	#if UART6_DMA_TX_ENABLE
		USART6_TX_DMA();
		UART6_Str.Send_Finish = 1;
	#endif

}