November 08, 2024, 12:34:00 AM

PIC32-HMZ144 Uart

Started by hellsmoke, August 29, 2019, 08:33:44 AM

Previous topic - Next topic

hellsmoke

Hi all

I use two PIC32-HMZ144 Rev.C boards.
I wanted to use the UART2 with 115200 baud on the UEXT connector (3 RE8/U2TX and 4 RE9/U2RX).
Connected with an USB FTDI PI Cable (TX, RX, GND) to the PC.
But I get only rubbish on the RX ISR and rubbish on the TX wire.

The source code is from scratch.

#pragma config FPLLIDIV = DIV_3         // 25MHz crystal / 3 = 8MHz
#pragma config FPLLRNG = RANGE_5_10_MHZ 
#pragma config FPLLICLK = PLL_POSC      // crystal input
#pragma config FPLLMULT = MUL_50        // 8MHz x 50 = 400MHz
#pragma config FPLLODIV = DIV_2         // 400MHz / 2 = 200MHz SPLL
#pragma config FNOSC = SPLL             // Selection SPLL 200MHz


Initialization code:

    PPSUnLock();
   
    // PBCLK2: PMP, I2C, UART, SPI
    while (PB2DIVbits.PBDIVRDY == 0);
    PB2DIVbits.PBDIV = 1; // div 2 = 100MHz
    PB2DIVbits.ON = 1;

    // PBCLK3: Timer1
    while (PB3DIVbits.PBDIVRDY == 0);
    PB3DIVbits.PBDIV = 1; // div 2 = 100MHz
    PB3DIVbits.ON = 1;

    // PBCLK4: Ports
    while (PB4DIVbits.PBDIVRDY == 0);
    PB4DIVbits.PBDIV = 1; // div 2 = 100MHz
    PB4DIVbits.ON = 1;

    ANSELEbits.ANSE9 = 0;  // RE9 as digital port
    ANSELEbits.ANSE8 = 0;  // RE8 as digital port
    TRISEbits.TRISE9 = 1;  // RE9 input
    TRISEbits.TRISE8 = 0;  // RE8 ouptut
   
    U2RXRbits.U2RXR = 0xD; // rx pin RPE9
    RPE8Rbits.RPE8R = 0x2; // tx pin RPE8


    U2MODEbits.ON = 0;
    __builtin_disable_interrupts();

    U2MODEbits.SIDL = 0;
    U2MODEbits.IREN = 0;
    U2MODEbits.RTSMD = 0;
    U2MODEbits.UEN = 0;    // TX and RX pins enabled
    U2MODEbits.WAKE = 1;
    U2MODEbits.LPBACK = 0;
    U2MODEbits.ABAUD = 0;
    U2MODEbits.RXINV = 1;
    U2MODEbits.BRGH = 0;
    U2MODEbits.PDSEL = 0;  // 8bit no parity
    U2MODEbits.STSEL = 0;  // 1 stop bit
   
    U2STAbits.ADM_EN = 0;
    U2STAbits.UTXISEL = 0;
    U2STAbits.UTXINV = 1;
    U2STAbits.UTXBRK = 0;
    U2STAbits.URXISEL = 0;
    U2STAbits.ADDEN = 0;
    U2STAbits.OERR = 0;

    IPC36bits.U2RXIP = 7;   // Set priority level = 1
    IPC36bits.U2RXIS = 1;   // Set sub-priority level = 1
   
    IEC4bits.U2RXIE = 1;     // Enable Timer1 interrupt
   
    U2BRG = 53;   // 100MHz / 16 / 115200 - 1 (Baud rate)
   
    U2STAbits.UTXEN = 1;   // TX enable
    U2STAbits.URXEN = 1;   // RX enable

    PPSLock();

    IFS4bits.U2RXIF = 0;     // Clear Interrupt flag
    INTCONbits.MVEC = 1; // Multi vectored interrupts
    __builtin_enable_interrupts();
    U2MODEbits.ON = 1;


ISR:

void __attribute__((vector(_UART2_RX_VECTOR), interrupt(IPL7AUTO), nomips16)) Uart2RxIrqHandler(void)
{
    //....

    IFS4bits.U2RXIF = 0;
}

How I said. Something happens. I get some useless data on RX and TX.
I changed the oscillator inputs to FRC and changed the baud rates. Nothing helped.
It looks like it's always the same wrong.

The cable and the PC software are fine. I use them for many other evaluation boards with 115200 baud.
I tried it with both boards and got the same errors.


Does anybody have an advise?

Regards
Michael


hellsmoke

Hi all

UART works with the settings above after about 10 seconds!!??

Does anybody know why it takes so long? The HMZ-144 pullups are unchanged.

Regards
Michael

LubOlimex

What happens with the demo that we provide, the demo enables the UART at pins #3 and #4 at the UEXT connector. The demo is this one: https://www.olimex.com/Products/PIC/Development/PIC32-HMZ144/resources/PIC32_HMZ144_demo.zip

Technical support and documentation manager at Olimex

hellsmoke

Hi all

Thank you for your answer.

I solved the problem after some blood sweat and tears:


// Set these bits

U2RXRbits.U2RXR = 0xD; // rx pin RPE9
RPE8Rbits.RPE8R = 0x2; // tx pin RPE8


// BEFORE these

TRISEbits.TRISE9 = 1;  // RE9 input
TRISEbits.TRISE8 = 0;  // RE8 output


Regards

Michael