FireBLE/Uart driver

来自Firefly wiki
跳转至: 导航搜索

Introduction

UART Communication is a very common device communication protocol,its characteristic is simple communication lines,as long as a pair of transmission lines can realize two-way communication.UART often used in data transmission between the simple equipment,is also often used in debugging of the program.

UART Initialization

UART initialization before configuration GPIO.

 //UART io configurate
 uart_io_config();
 void uart_io_config(void)
 {
     // pin mux
     syscon_SetPMCR0(QN_SYSCON, P07_SW_CLK_PIN_CTRL
                              | P06_SW_DAT_PIN_CTRL
                              | P00_UART0_TXD_PIN_CTRL    // P0.0 uart0 tx
                              | P17_UART0_RXD_PIN_CTRL    // P1.7 uart0 rx
                              );
     syscon_SetPMCR1(QN_SYSCON, P21_UART1_TXD_PIN_CTRL    // P2.1 uart1 tx
                                | P20_UART1_RXD_PIN_CTRL    // P2.0 uart1 rx
                              );
 
     // pin select
     syscon_SetPMCR2(QN_SYSCON, SYSCON_MASK_UART1_PIN_SEL);
 
     // pin pull ( 00 : High-Z,  01 : Pull-down,  10 : Pull-up,  11 : Reserved )
     syscon_SetPPCR0(QN_SYSCON, 0xAAAA5AAA);
     syscon_SetPPCR1(QN_SYSCON, 0x2AAAAAAA);
 }

Function syscon_SetPMCR0,syscon_SetPMCR1 respectively configuration GPIO_P00~GPIO_P17 and GPIO_P20~GPIO_P36 GPIO multiplex function,please note that this limit,the configured GPIO_P23 cannot be syscon_SetPMCR0.

     // pin mux
     syscon_SetPMCR0(QN_SYSCON, P07_SW_CLK_PIN_CTRL
                              | P06_SW_DAT_PIN_CTRL
                              | P00_UART0_TXD_PIN_CTRL    // P0.0 uart0 tx
                              | P17_UART0_RXD_PIN_CTRL    // P1.7 uart0 rx
                              );
     syscon_SetPMCR1(QN_SYSCON, P21_UART1_TXD_PIN_CTRL    // P2.1 uart1 tx
                                | P20_UART1_RXD_PIN_CTRL    // P2.0 uart1 rx
                              );

Function syscon_SetPMCR2 choose open function reuse some peripherals multiplex pin,because this case just use UART1,So the configuration for: Other available for selection for open special pin

   // pin select
   syscon_SetPMCR2(QN_SYSCON, SYSCON_MASK_UART1_PIN_SEL);

Other available for selection for open special pin

  • 31 - 27 : TEST_CTRL[4 :0]
  • 27 - 8 : RSVD
  • 7 : CLK_OUT_SEL[1]
  • 6 : CLK_OUT_SEL[0]
  • 5 : UART1_PIN_SEL
  • 4 : I2C_PIN_SEL
  • 3 : ADCT_PIN_SEL
  • 2 : RSVD
  • 1 : SPI0_PIN_SEL
  • 0 : SPI1_PIN_SEL

Also open by SWD need pull-down IO port, so need the following configuration

     // pin pull ( 00 : High-Z,  01 : Pull-down,  10 : Pull-up,  11 : Reserved )
     syscon_SetPPCR0(QN_SYSCON, 0xAAAA5AAA);  //GPIO_P06,GPIO_P07 configured to 5(0101),others GPIO are all A(1010) 
     syscon_SetPPCR1(QN_SYSCON, 0x2AAAAAAA);  //result no GPIO_P37,so the two highest reserves,and GPIO_P36 pull-up,so set to 2(0010)AAAAAAA

Configuration Complete GPIO,the following should be UART initialization,depending on the clock,we can configure different initialization parameter, as follows

 #if __AHB_CLK == 32000UL
     //Initialize uart0 with 1200 baudrate, 8bit data, 1 stop bit, no parity, LSB bitorder, no HW flow control.
     uart_init(QN_UART0, __USART_CLK, UART_1200);
 #else
     //Initialize uart0 with 115200 baudrate, 8bit data, 1 stop bit, no parity, LSB bitorder, no HW flow control.
     uart_init(QN_UART0, __USART_CLK, UART_115200);
     //uart_init(QN_UART0, __USART_CLK, UART_57600);
 #endif
     uart_tx_enable(QN_UART0, MASK_ENABLE);
     uart_rx_enable(QN_UART0, MASK_ENABLE);

UART_init contains three parameters, respectively,the first parameter is a UART module choice,QN902x has two UART in the module,QN_UART0 is most commonly used.the second parameter is choice UART,the third parameter is the baud rate setting,Under the 32k crystals can only adopt 1200 baud rate.others,such as Data width 、stop bit、parity bit 、LSB or MSB first、Flow Control setting of the need to enter the UART_init function changes and set inside.

 uart_init(QN_UART0, __USART_CLK, UART_1200);

Serial transceiver can choose polling,can also choose to interrupt,generally for efficiency consideration,generally for efficiency consideration,so you also need to open the interrupt

     uart_tx_enable(QN_UART0, MASK_ENABLE);
     uart_rx_enable(QN_UART0, MASK_ENABLE);

Initialization is complete!

UART print a string

   //Print out "Hello Quintic!\n" thought uart.
   uart_printf(QN_UART0, (uint8_t *)"Hello Quintic!\n");

Print string by UART_printf function can realize serial port,of course can string and array,you can also be used to print a list of you need to digital transmission.Function argument is simple,the first is the UART port,the second is to send an array pointer.

UART send and receive the sample

   //uart0 receive data
   rx_flag = 1;
   uart_read(QN_UART0, buffer, 10, led_blink_left);
   while (rx_flag == 1);
 
   //uart0 send data
   tx_flag = 1;
   uart_write(QN_UART0, buffer, 10, led_blink_right);
   while (tx_flag == 1);

Example USES UART0 first 10 bytes to the serial port read data stored in buffer within an array,then write just read in the serial port to the serial port through UART0 save 10 bytes of data in the buffer.
uart_read has four parameters,the first serial port selection,the second is the read data storage location,the third parameter is the number of bytes read,the fourth parameter is the callback function set upon the completion of the read data, will call the callback function.

  uart_read(QN_UART0, buffer, 10, led_blink_left);

Before reading the,can be read to set up a sign a rx_flag,execution after reading function into the empty for a while loop operators,at the completion of a read,reset reads the flag bit in the callback function,to complete a read operation.

 void led_blink_left(void)
 {
     rx_flag = 0;
 }

Write and read similar serial port.