+ Post New Thread
Results 1 to 3 of 3
  1. #1
    Newbie level 6
    Points: 121, Level: 1

    Join Date
    Jun 2015
    Posts
    14
    Helped
    0 / 0
    Points
    121
    Level
    1

    UART code for flowcontrol enable

    Hi
    I am using PI32MX795f512l .. I want to integrate flowcontrol in my uart communication .. As of now i am using only TX and RX pins for data transmit and receive ...
    In Continous transmission ,in bluetooth module i am getting overwritten buffer...Pls Help me to come out of this.. I am new one to this.... How to enable flowcontrol in my uart code. Here i have attached my code below

    Code C - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    
    #pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
    #pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1
    #define SYS_FREQ (80000000L)
    #pragma config ICESEL = ICS_PGx1
     
    void init_uart()
    {
        UARTConfigure(UART_MODULE_ID, UART_ENABLE_PINS_TX_RX_ONLY);
        UARTSetFifoMode(UART_MODULE_ID, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
        UARTSetLineControl(UART_MODULE_ID, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
        UARTSetDataRate(UART_MODULE_ID, GetPeripheralClock(), DESIRED_BAUDRATE);
        UARTEnable(UART_MODULE_ID, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));
    }
    void Uart_Interrupt(){
    INTEnable(INT_SOURCE_UART_RX(UART_MODULE_ID), INT_ENABLED);
        INTSetVectorPriority(INT_VECTOR_UART(UART_MODULE_ID), INT_PRIORITY_LEVEL_2);
        INTSetVectorSubPriority(INT_VECTOR_UART(UART_MODULE_ID), INT_SUB_PRIORITY_LEVEL_0);
     
        // Enable multi-vector interrupts
        INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
        INTEnableInterrupts();
    }
    main()
    {
       init_uart();
       Uart_Interrupt();
       while(1){
        if(flag ==1){ // some code}
       }
       
    }
    void __ISR(_UART2_VECTOR,IPL2) IntUart2Handler(void)
    {
        unsigned char Receivedchar;
        if(INTGetFlag(INT_SOURCE_UART_RX(UART_MODULE_ID)))
        {
            Receivedchar= UARTGetDataByte(UART_MODULE_ID);
            ReceivedByte[buf]=Receivedchar;
            buf++;
            count++;
            flag=1;
            INTClearFlag(INT_SOURCE_UART_RX(UART_MODULE_ID));
     
        }
            
       // We don't care about TX interrupt
        if ( INTGetFlag(INT_SOURCE_UART_TX(UART_MODULE_ID)) )
        {
            INTClearFlag(INT_SOURCE_UART_TX(UART_MODULE_ID));
        }
    }
    Last edited by BradtheRad; 13th July 2015 at 05:47. Reason: Added code formatting

    •   AltAdvertisment

        
       

  2. #2
    Super Moderator
    Points: 70,762, Level: 64
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    14,473
    Helped
    3292 / 3292
    Points
    70,762
    Level
    64

    Re: UART code for flowcontrol enable

    Hi,

    did you read documents about UART software flow control?

    ***
    Check: (to avoid software flow control)
    * if reducing / increasing the baud rate may solve your problem
    * if the receiver software could be optimized to avoid buffer overflow. (latency times, buffer size)

    Klaus



    •   AltAdvertisment

        
       

  3. #3
    Member level 1
    Points: 2,140, Level: 10

    Join Date
    Apr 2005
    Posts
    39
    Helped
    4 / 4
    Points
    2,140
    Level
    10

    Re: UART code for flowcontrol enable

    To start with, flowcontrol from a microcontroller is not really complex. There are two flow control models usually: hardware and software.

    For hardware, the sender asserts RTS (request to send). The receiver responds with CTS (clear to send). In principle, you could use a pair of GPIO pins for RTS/CTS signals. I did not see the uC datasheet myself. But, in ideal scenario, the RTS should have peripheral input interrupt enabled on it. Here is a rough algoirthm:

    =============================================
    On RTS Assert (or Interrupt):
    if (buffer full):
    continue # The device holds the data in its buffer.
    else
    assert CTS
    get a byte

    On receive byte (or Interrupt):
    if (buffer full):
    de-assert CTS
    =========================================

    The software flow control uses a pair of control characters - XON/XOFF. Search around for software flow control in, say, Google.

    I would start with datasheets. If the baudrate could be reduced, that'll be a very good place to start. The primary requirement in any case is: the sending device must support flow control - either software or hardware.
    There is no spoon.



--[[ ]]--