• AVR Freaks

Hot!Problem using UART within bootloader code.

Author
hala9k
Starting Member
  • Total Posts : 37
  • Reward points : 0
  • Joined: 2011/01/19 10:55:24
  • Location: 0
  • Status: offline
2020/01/23 01:04:14 (permalink)
0

Problem using UART within bootloader code.

Hi all. I have a weird problem that has me baffled. I am migrating a project that uses a pair of PIC32MX695s to a new hardware version using a single PIC32MZ2048EFM144. I have a non-harmony stack (Oryx) running on the new hardware, along with most of my application. I'm using MPLAB v5.05 and XC32 v2.10.
 
The last major step I have is to get the bootloader working. I actually have most of it going, with the bootloader in boot flash and the application in program memory, but flash programming is not there yet. In an effort to debug, I want to send text to a UART (UART4 in this case) from within the bootloader. I am already using UART4 all over the application for that purpose. Using exactly the same code in the bootloader, I can't get squat. As soon as the bootloader jumps to the application it's fine.
 
System setup, clocks, port init, remap pins and UART setup are exactly the same as the application. Here's the serial code:
 
void serial_init(uint32_t brate)
{
   //Configure UART4 (8 bits, no parity, 1 stop bit)
   U4STA = 0;
   //Enable high-speed mode
   U4MODE = _U4MODE_BRGH_MASK;
   //Set baudrate
   U4BRG = ((40000000 / 4) / brate) - 1;

   //Enable UART4
   U4MODESET = _U4MODE_ON_MASK;
   //Enable transmitter and receiver
   U4STASET = _U4STA_UTXEN_MASK | _U4STA_URXEN_MASK;

}

void putb(unsigned char dat)
{
   unsigned int maxwt;
   //Wait for the transmitter to be ready but don't hang
   maxwt = 10000;

   while (!(U4STA & _U4STA_TRMT_MASK) && maxwt--);
   //Send character
   U4TXREG = dat;
   // Wait for the transfer to complete
   maxwt = 10000;
   while(!(U4STA & _U4STA_TRMT_MASK) && maxwt--);
}


Is there something else required to access peripherals from code in the bootloader area? I did run into a similar problem trying to set up SPI4 for the spi flash but I just bit banged that instead (which worked).
 
Thanks, Hal
#1

8 Replies Related Threads

    NKurzman
    A Guy on the Net
    • Total Posts : 18898
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: Problem using UART within bootloader code. 2020/01/23 07:25:28 (permalink)
    5 (1)
    Did you configure the PPS?
    #2
    felipe_ferreira
    New Member
    • Total Posts : 16
    • Reward points : 0
    • Joined: 2019/09/10 07:48:12
    • Location: 0
    • Status: offline
    Re: Problem using UART within bootloader code. 2020/01/23 07:37:02 (permalink)
    0
    Hi! Are you okay?
    Amazingly, your UART problem helped me solve mine.
    link post: https://www.microchip.com/forums/m1125688.aspx
    I was having trouble printing the characters and I verified that I had not given my codigo pause there was no pause in sending the transmitter as you did.
    I simply turned the bit on for sending, and then turned it off by doing
    U2STAbits.UTXEN = 1;
    //IEC1bits.U2TXIE = 1;
    U2TXREG = buffer ;
    U2STAbits.UTXEN = 0;
    could you give me more details of why this break or where did you get it from? I would love to understand
    (while (! (U2STA & _U2STA_TRMT_MASK) && maxwt--);)
    I made the following modification to print a string in my code
     
    void escrever_UART_2_toWorld(char * buffer, int size)
    {
        int i;
        unsigned int maxwt;
       //Wait for the transmitter to be ready but don't hang
        maxwt = 10000;
        while (!(U2STA & _U2STA_TRMT_MASK) && maxwt--);
            for(i = 0; i < size; i++) {
                U2TXREG = buffer[i];
                maxwt = 10000;
                while(!(U2STA & _U2STA_TRMT_MASK) && maxwt--);
            }
        maxwt = 10000;
        while(!(U2STA & _U2STA_TRMT_MASK) && maxwt--);
        U2TXREG = '\n';
    }

    #3
    hala9k
    Starting Member
    • Total Posts : 37
    • Reward points : 0
    • Joined: 2011/01/19 10:55:24
    • Location: 0
    • Status: offline
    Re: Problem using UART within bootloader code. 2020/01/23 09:40:00 (permalink)
    5 (1)
    Well, that proves once again that the stupidest things are the hardest to find. For the bootloader I copied the entire clock, port and PPS setup from the application. However, the PPS setup for the uart is inside a #ifdef USE_DEBUG... #endif. That's defined in the application, but not in the bootloader. It works now, thanks!
     
    Thanks, Hal
    #4
    hala9k
    Starting Member
    • Total Posts : 37
    • Reward points : 0
    • Joined: 2011/01/19 10:55:24
    • Location: 0
    • Status: offline
    Re: Problem using UART within bootloader code. 2020/01/23 10:18:26 (permalink)
    0
    The previous post made me go back and look at my setup code again. Of course this time the problem was obvious, it works now.
    I'm glad that this helped with your problem. This uart code is from the Oryx Cyclone stack. It's part of the debugging function. I have a separate function for sending strings, just didn't include it in my first post.
    while (!(U2STA & _U2STA_TRMT_MASK) && maxwt--) is required to make sure that TXREG is empty (U2STA & _U2STA_TRMT_MASK) before putting another byte in. It takes time for the uart to shift all of the bits out. I added maxwt to make absolutely sure that it couldn't hang waiting for a bit to set, but that's probably overkill. It will work without it.
    Thanks, Hal
     
     
    #5
    ric
    Super Member
    • Total Posts : 28336
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Problem using UART within bootloader code. 2020/01/23 12:32:00 (permalink)
    5 (2)
    That's actually an inefficient way to send.
    TRMT tells you that the transmit shift register is empty, NOT that the TXREG is empty.
    That means you are needlessly wait after the TXREG has been transferred to the shift register.
    (TRMT only useful when you are managing a half duplex connection, and need to wait for the complete transmission before changing the direction.)
     
    Use the UTXBF bit to detect when the TXREG is empty.
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #6
    hala9k
    Starting Member
    • Total Posts : 37
    • Reward points : 0
    • Joined: 2011/01/19 10:55:24
    • Location: 0
    • Status: offline
    Re: Problem using UART within bootloader code. 2020/01/23 21:42:18 (permalink)
    0
    Good point, there's no good reason to wait around if the processor can be doing other things.
     
    Thanks, Hal
    #7
    crosland
    Super Member
    • Total Posts : 2041
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Warks, UK
    • Status: offline
    Re: Problem using UART within bootloader code. 2020/01/24 02:48:54 (permalink)
    0
    Once you have loaded two characters (into the shift register and holding register) you will wait anyway as soon as you try to send another character. It doesn't save a lot of time in the grand scheme of things.
     
    The only way to not "wait around" for the UART is to use interrupts or poll it less often.
    #8
    ric
    Super Member
    • Total Posts : 28336
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Problem using UART within bootloader code. 2020/01/24 03:05:14 (permalink)
    5 (2)
    It will make a small difference, as the code doesn't do the array fetch for the next character until the TRMT bit goes true.
    Using UTXBF means that all happens in parallel with the transfer, and gets loaded into TXBUF ready to commence the moment the previous character finishes.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #9
    Jump to:
    © 2020 APG vNext Commercial Version 4.5