AnsweredHot!PIC32MZ UART printf gets stuck/freezes

Page: 12 > Showing page 1 of 2
Author
marcc70
Starting Member
  • Total Posts : 44
  • Reward points : 0
  • Joined: 2018/12/03 06:23:00
  • Location: 0
  • Status: offline
2019/02/12 04:03:00 (permalink)
0

PIC32MZ UART printf gets stuck/freezes

Hello fellow readers,
 
I am using the PIC32MZ2048EFH064, I have connected the MCP2200 Breakout module to my PIC.
I used a 12MHz oscillator as external, what I read in the microchip documents is that you can use 12MHz or 24 for the usb.
What I want is to print a integer to my terminal on my laptop, so at the moment I connect my pic at my laptop and virtual com-port appears. I can screen the port with a baudrate at my wish. But I cannot see any incoming data because my PIC gets stuck/freezes at the moment i call "printf("");" no matter what I fill in the "".
 
Hope someone can help me out or can give me advise.
 
Thanks in advance.
Cheers,
Marc
 

// PIC32MZ2048EFH064 Configuration Bit Settings

// 'C' source line config statements

// DEVCFG3
#pragma config USERID = 0xFFFF // Enter Hexadecimal value (Enter Hexadecimal value)
#pragma config FMIIEN = ON // Ethernet RMII/MII Enable (MII Enabled)
#pragma config FETHIO = ON // Ethernet I/O Pin Select (Default Ethernet I/O)
#pragma config PGL1WAY = OFF // Permission Group Lock One Way Configuration (Allow only one reconfiguration)
#pragma config PMDL1WAY = OFF // Peripheral Module Disable Configuration (Allow only one reconfiguration)
#pragma config IOL1WAY = OFF // Peripheral Pin Select Configuration (Allow only one reconfiguration)
#pragma config FUSBIDIO = OFF // USB USBID Selection (Controlled by the USB Module)

// DEVCFG2
#pragma config FPLLIDIV = DIV_2 // System PLL Input Divider (8x Divider)
#pragma config FPLLRNG = RANGE_5_10_MHZ// System PLL Input Range (34-68 MHz Input)
#pragma config FPLLICLK = PLL_POSC // System PLL Input Clock Selection (FRC is input to the System PLL)
#pragma config FPLLMULT = MUL_66 // System PLL Multiplier (PLL Multiply by 128)
#pragma config FPLLODIV = DIV_2 // System PLL Output Clock Divider (32x Divider)
#pragma config UPLLFSEL = FREQ_12MHZ // USB PLL Input Frequency Selection (USB PLL input is 24 MHz)

// DEVCFG1
#pragma config FNOSC = SPLL // Oscillator Selection Bits (Fast RC Osc w/Div-by-N (FRCDIV))
#pragma config DMTINTV = WIN_127_128 // DMT Count Window Interval (Window/Interval value is 127/128 counter value)
#pragma config FSOSCEN = OFF // Secondary Oscillator Enable (Enable SOSC)
#pragma config IESO = ON // Internal/External Switch Over (Enabled)
#pragma config POSCMOD = EC // Primary Oscillator Configuration (Primary osc disabled)
#pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin (Disabled)
#pragma config FCKSM = CSECME // Clock Switching and Monitor Selection (Clock Switch Enabled, FSCM Enabled)
#pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576)
#pragma config WDTSPGM = STOP // Watchdog Timer Stop During Flash Programming (WDT stops during Flash programming)
#pragma config WINDIS = NORMAL // Watchdog Timer Window Mode (Watchdog Timer is in non-Window mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Enabled)
#pragma config FWDTWINSZ = WINSZ_25 // Watchdog Timer Window Size (Window size is 25%)
#pragma config DMTCNT = DMT31 // Deadman Timer Count Selection (2^31 (2147483648))
#pragma config FDMTEN = OFF // Deadman Timer Enable (Deadman Timer is enabled)

// DEVCFG0
#pragma config DEBUG = OFF // Background Debugger Enable (Debugger is disabled)
#pragma config JTAGEN = OFF // JTAG Enable (JTAG Port Enabled)
#pragma config ICESEL = ICS_PGx1 // ICE/ICD Comm Channel Select (Communicate on PGEC1/PGED1)
#pragma config TRCEN = OFF // Trace Enable (Trace features in the CPU are enabled)
#pragma config BOOTISA = MIPS32 // Boot ISA Selection (Boot code and Exception code is MIPS32)
#pragma config FECCCON = OFF_UNLOCKED // Dynamic Flash ECC Configuration (ECC and Dynamic ECC are disabled (ECCCON bits are writable))
#pragma config FSLEEP = OFF // Flash Sleep Mode (Flash is powered down when the device is in Sleep mode)
#pragma config DBGPER = PG_ALL // Debug Mode CPU Access Permission (Allow CPU access to all permission regions)
#pragma config SMCLR = MCLR_NORM // Soft Master Clear Enable bit (MCLR pin generates a normal system Reset)
#pragma config SOSCGAIN = GAIN_2X // Secondary Oscillator Gain Control bits (2x gain setting)
#pragma config SOSCBOOST = OFF // Secondary Oscillator Boost Kick Start Enable bit (Boost the kick start of the oscillator)
#pragma config POSCGAIN = GAIN_2X // Primary Oscillator Gain Control bits (2x gain setting)
#pragma config POSCBOOST = OFF // Primary Oscillator Boost Kick Start Enable bit (Boost the kick start of the oscillator)
#pragma config EJTAGBEN = NORMAL // EJTAG Boot (Normal EJTAG functionality)

// DEVCP0
#pragma config CP = OFF // Code Protect (Protection Disabled)

// SEQ3
#pragma config TSEQ = 0xFFFF // Boot Flash True Sequence Number (Enter Hexadecimal value)
#pragma config CSEQ = 0xFFFF // Boot Flash Complement Sequence Number (Enter Hexadecimal value)



// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

/*
 * File: main.c
 * Author:
 *
 * Created on February 1, 2019, 12:36 PM
 */


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <xc.h>
#include <proc/p32mz2048efh064.h>
#define SYS_FREQ 198000000
//Variables
int heartbeat=0;

void set_performance_mode()
{
 unsigned int cp0;
 
    // Unlock Sequence
    asm volatile("di"); // Disable all interrupts
    SYSKEY = 0xAA996655;
    SYSKEY = 0x556699AA;

    // PB1DIV
    // Peripheral Bus 1 cannot be turned off, so there's no need to turn it on
    PB1DIVbits.PBDIV = 1; // Peripheral Bus 1 Clock Divisor Control (PBCLK1 is SYSCLK divided by 2)

    // PB2DIV
    PB2DIVbits.ON = 1; // Peripheral Bus 2 Output Clock Enable (Output clock is enabled)
    PB2DIVbits.PBDIV = 1; // Peripheral Bus 2 Clock Divisor Control (PBCLK2 is SYSCLK divided by 2)

    // PB3DIV
    PB3DIVbits.ON = 1; // Peripheral Bus 2 Output Clock Enable (Output clock is enabled)
    PB3DIVbits.PBDIV = 1; // Peripheral Bus 3 Clock Divisor Control (PBCLK3 is SYSCLK divided by 2)

    // PB4DIV
    PB4DIVbits.ON = 1; // Peripheral Bus 4 Output Clock Enable (Output clock is enabled)
    while (!PB4DIVbits.PBDIVRDY); // Wait until it is ready to write to
    PB4DIVbits.PBDIV = 0; // Peripheral Bus 4 Clock Divisor Control (PBCLK4 is SYSCLK divided by 1)

    // PB5DIV
    PB5DIVbits.ON = 1; // Peripheral Bus 5 Output Clock Enable (Output clock is enabled)
    PB5DIVbits.PBDIV = 1; // Peripheral Bus 5 Clock Divisor Control (PBCLK5 is SYSCLK divided by 2)

    // PB7DIV
    PB7DIVbits.ON = 1; // Peripheral Bus 7 Output Clock Enable (Output clock is enabled)
    PB7DIVbits.PBDIV = 0; // Peripheral Bus 7 Clock Divisor Control (PBCLK7 is SYSCLK divided by 1)

    // PB8DIV
    PB8DIVbits.ON = 1; // Peripheral Bus 8 Output Clock Enable (Output clock is enabled)
    PB8DIVbits.PBDIV = 1; // Peripheral Bus 8 Clock Divisor Control (PBCLK8 is SYSCLK divided by 2)

    // PRECON - Set up prefetch
    PRECONbits.PFMSECEN = 0; // Flash SEC Interrupt Enable (Do not generate an interrupt when the PFMSEC bit is set)
    PRECONbits.PREFEN = 0b11; // Predictive Prefetch Enable (Enable predictive prefetch for any address)
    PRECONbits.PFMWS = 0b010; // PFM Access Time Defined in Terms of SYSCLK Wait States (Two wait states)

    // Set up caching
    cp0 = _mfc0(16, 0);
    cp0 &= ~0x07;
    cp0 |= 0b011; // K0 = Cacheable, non-coherent, write-back, write allocate
    _mtc0(16, 0, cp0);

    // Lock Sequence
    SYSKEY = 0x33333333;
    asm volatile("ei"); // Enable all interrupts
}

void delay_us(unsigned int us)
{
    // Convert microseconds us into how many clock ticks it will take
 us *= SYS_FREQ / 1000000 / 2; // Core Timer updates every 2 ticks
       
    _CP0_SET_COUNT(0); // Set Core Timer count to 0
    
    while (us > _CP0_GET_COUNT()); // Wait until Core Timer count reaches the number we calculated earlier
}

void delay_ms(int ms)
{
    delay_us(ms * 1000);
}



void UART_Init(){

    int pbClk;
    pbClk = SYS_FREQ / 2; // Our PBCLK2 divider was set to 1, so PBCLK2 is exactly half the speed of the system clock, or 100Mhz
//100
    
    U5MODE = 0; // Set UART 5 off prior to setting it up
    U5MODEbits.BRGH = 0; // We want standard speed mode. Not necessary as we just set U5MODE to 0 so this is just for explanation's sake
    U5BRG = pbClk / (16 * 38400) - 1;// This is the formula straight from the datasheet
    U5STA = 0; // Disable the TX and RX pins, clear all flags
    U5STAbits.UTXEN = 1; // Enable the TX pin
    U5STAbits.URXEN = 1; // Enable the RX pin
    U5MODEbits.PDSEL = 0; // PDSEL controls how many data bits and how many parity bits we want, this is the default of 8-bit data, no parity bits that most terminals use
    U5MODEbits.STSEL = 0; // STSEL controls how many stop bits we use, let's use the default of 1
    U5MODEbits.ON = 1; // Turn on the UART 5 peripheral
}

void _mon_putc (char c)
 {
   while (U5STAbits.UTXBF); // Wait til current transmission is complete
   U5TXREG = c;
}

void SPI_init()
{
    SPI2CONbits.ON = 0; // Turn off SPI2 before configuring
    SPI2CONbits.MSTEN = 1; // Enable Master mode
    SPI2CONbits.CKP = 1; // Clock signal is active low, idle state is high
    SPI2CONbits.CKE = 0; // Data is shifted out/in on transition from idle (high) state to active (low) state
    SPI2CONbits.SMP = 1; // Input data is sampled at the end of the clock signal
    SPI2CONbits.MODE16 = 0; // Do not use 16-bit mode
    SPI2CONbits.MODE32 = 0; // Do not use 32-bit mode (combines with the above line to activate 8-bit mode)
    SPI2BRG = 0; // Set Baud Rate Generator to 0 [discussed below]
    SPI2CONbits.ENHBUF = 0; // Disables Enhanced Buffer mode [also discussed below]
 SPI2CONbits.ON = 1; // Configuration is done, turn on SPI2 peripheral
    
    SPI_send(data2);
    SPI2BUF= data2;
    
}


int SPI_send(int data)
{
    data = 245;
 SPI2BUF = data; // Send *data* to SPI2BUF.
 while (SPI2STATbits.SPIRBE); // Which the receive buffer is empty, loop
 return (int)SPI2BUF; // Return whatever is in SPI2BUF
}


void main(){
  
        
 setup();
    
        // Set up PPS for UART 5
    U5RXR = 0b0100; // RPRF1 = U5RX
    RPD3R = 0b0011; // RPD3 = U5TX
    TRISFbits.TRISF1 = 1; // Make RF1 an input
    TRISDbits.TRISD3 = 0; // Make RD3 an output
        // Set max performance mode

    // Set up PPS for SPI2 on RB3 and RB5
 //SDI2R = 0b1000; // RB3 = SDI2
 RPB5R = 0b0110; // RB5 = SDO2
    //RB4=CS EN SCK2=RG6
 //TRISBbits.TRISB3 = 1; // Set RB3 as input because it's SDI2
    
    // Initialise peripheral UART 5
      
 set_performance_mode();
    UART_Init();

    
    SPI_init();
   // __XC_UART=5;

    while(1){

    
    
    heartbeat++;
     if(heartbeat<20000){
            PORTBbits.RB2=1;
            PORTBbits.RB8=1;
            
       // PORTGbits.RG8=1;
            // heartbeat++;
            // PORTGbits.RG9=1; //TEST
        }
        if(heartbeat>=20000 && heartbeat <40000){
            PORTBbits.RB2=0;
            PORTBbits.RB8=0;
         // heartbeat++;
             printf("HELLO.%d\n", heartbeat);
           // delay_ms(3000);
            
    
        }
        if(heartbeat==40000){
            heartbeat=0;
        }
  // setbuf(stdout, NULL);
   
}
}




 
setup(){

    // Set up all ports to be digital and not analog

ANSELB = 0;
ANSELG = 0;

// Set outputs en inputs
    TRISBbits.TRISB12=1;
    TRISBbits.TRISB13=1;
    TRISGbits.TRISG9=0;
    TRISGbits.TRISG7=0;
    
    TRISBbits.TRISB7=0;
    TRISBbits.TRISB8=0;
    TRISBbits.TRISB3=0;
    TRISBbits.TRISB6=0;
  // TRISBbits.TRISB7=;
  // TRISBbits.TRISB8=;
    TRISBbits.TRISB9=1;
    TRISBbits.TRISB2=0;
    
   // TRISFbits.TRISF1=0;
    
    PORTBbits.RB3=1;
    PORTBbits.RB7=1;
    for(delay1=0; delay1<100000; delay1++){
        //delay
    }
    PORTBbits.RB3=0;
    PORTBbits.RB7=0;
    
    PORTBbits.RB6=0;
    PORTGbits.RG7=0;
    
    return;

}

#1
crosland
Super Member
  • Total Posts : 1449
  • Reward points : 0
  • Joined: 2005/05/10 10:55:05
  • Location: Bucks, UK
  • Status: offline
Re: PIC32MZ UART printf gets stuck/freezes 2019/02/12 05:11:31 (permalink)
0
What does the debugger tell you? Where is it stuck?
 
Why are you using your own delay routines? Surely XC32 has some?
 
Why do you include the specific processor .h file?
 
What optimization level are you using and what do you think this does?
 
    for(delay1=0; delay1<100000; delay1++){
        //delay
    }
#2
marcc70
Starting Member
  • Total Posts : 44
  • Reward points : 0
  • Joined: 2018/12/03 06:23:00
  • Location: 0
  • Status: offline
Re: PIC32MZ UART printf gets stuck/freezes 2019/02/12 05:20:14 (permalink)
0
What does the debugger tell you? Where is it stuck?

Well this part of the program will blink a LED (heartbeat):
And it stops when I use the printf.
I use a Pickit3 so I can't realtime debug, unfortunately.
   heartbeat++;
     if(heartbeat<20000){
            PORTBbits.RB2=1;
            PORTBbits.RB8=1;
            
       // PORTGbits.RG8=1;
            // heartbeat++;
            // PORTGbits.RG9=1; //TEST
        }
        if(heartbeat>=20000 && heartbeat <40000){
            PORTBbits.RB2=0;
            PORTBbits.RB8=0;
         // heartbeat++;
             printf("HELLO.%d\n", heartbeat);
           // delay_ms(3000);
            
    
        }
        if(heartbeat==40000){
            heartbeat=0;
        }

 
Why are you using your own delay routines? Surely XC32 has some?
True, but it is in a function what only loop's once and I was not familiar at that time with the delay in XC32. Also that part doesn't really matter atm.
 
Why do you include the specific processor .h file?
Someone advised me to do that, but actually I don't see any use in to that too.
 
What optimization level are you using and what do you think this does?

I do not understand that question. 
 
post edited by marcc70 - 2019/02/12 05:22:11
#3
aidanmocke
Starting Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2018/03/07 02:31:41
  • Location: 0
  • Status: offline
Re: PIC32MZ UART printf gets stuck/freezes 2019/02/12 06:43:09 (permalink)
0
Hi there,

You don't need to include the processor file, including xc.h will automatically include the correct processor file. I see I did that too but I didn't mean to :)

XC32 doesn't have any delay functions that I'm aware of, we have to roll our own.

Why are you using 198MHz, why don't we change that to 200 first (if you're using a 12MHz external clock):

#pragma config FPLLIDIV = DIV_3
#pragma config FPLLMULT = MUL_50
#pragma config FPLLODIV = DIV_1

And then change your SYS_CLK to 200000000.

What crosland means is your "delay" loop likely is being optimised out when you compile and removed entirely so it may not be doing anything.

Don't set PORT bits using PORTBbits, use LATBbits instead, for example LATBbits.LATB2 = 0;

In setup() add ANSELD = 0;

I can't see a reason why that code would crash. By the way you can definitely debug using pickit 3 and mplab x. I am not at my computer but I think you can move to the line you want and press CTRL+F8 to set it as a breakpoint (may be a different hotkey). Do that on the printf line and then press the green play button to start it in Debug mode and see if it gets there.

Furthermore, you don't need to worry about the USB settings at all, you are not using the USB module here. You are using an IC to talk to the computer via USB but you are talking to that IC via a UART port, not USB.
post edited by aidanmocke - 2019/02/12 06:46:12
#4
marcc70
Starting Member
  • Total Posts : 44
  • Reward points : 0
  • Joined: 2018/12/03 06:23:00
  • Location: 0
  • Status: offline
Re: PIC32MZ UART printf gets stuck/freezes 2019/02/12 06:52:15 (permalink)
0
Thank you for your time and answer,
 
Well #pragma config FPLLODIV = DIV_1 can't be set to DIV_1. (this will gives a compile error).
 
When I put ANSELD = 0; in the setup(), it gives this error:
main.c: In function 'setup':
main.c:591:1: error: 'ANSELD' undeclared (first use in this function)
ANSELD = 0;
^



Why use LAT bits instead?
 
I know I will check that out if it really gets stuck in that line, but what I meant I cannot debug 'real time' like a ICD3 or 4.
#5
crosland
Super Member
  • Total Posts : 1449
  • Reward points : 0
  • Joined: 2005/05/10 10:55:05
  • Location: Bucks, UK
  • Status: offline
Re: PIC32MZ UART printf gets stuck/freezes 2019/02/12 06:53:18 (permalink)
0
marcc70
I use a Pickit3 so I can't realtime debug, unfortunately.
 



Why not? Have you read the documentation?
#6
marcc70
Starting Member
  • Total Posts : 44
  • Reward points : 0
  • Joined: 2018/12/03 06:23:00
  • Location: 0
  • Status: offline
Re: PIC32MZ UART printf gets stuck/freezes 2019/02/12 06:57:57 (permalink)
0
Alright, so what I see with debugging is that the program is waiting in 'while' line with the arrow.
It means it is waiting for a confirmation somewhere?
 
void _mon_putc (char c)
{
while (U5STAbits.UTXBF); // Wait til current transmission is complete              <----------
U5TXREG = c;
}
#7
aidanmocke
Starting Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2018/03/07 02:31:41
  • Location: 0
  • Status: offline
Re: PIC32MZ UART printf gets stuck/freezes 2019/02/12 07:02:13 (permalink)
5 (1)
Right, as I say not at a computer :)

Then set MULT to MULT_100 and FPLLODIV = DIV_2 if that works. I don't think that's your issue, though I just wondered why you did it. No ANSELD? Must be because it's a 64-pin chip, I didn't know there was no ANSELD there.

In PIC32 you always read values from the PORT registers and write values to the LAT registers or you will find that you have problems at some point. The reason is valid but long so I don't really want to tap it out on my phone.

I don't know what you mean by real time then? If you set a breakpoint it'll trigger as soon as the condition is met. I suggest trying that first to see where it's actually hanging. Good luck!
#8
marcc70
Starting Member
  • Total Posts : 44
  • Reward points : 0
  • Joined: 2018/12/03 06:23:00
  • Location: 0
  • Status: offline
Re: PIC32MZ UART printf gets stuck/freezes 2019/02/12 07:12:48 (permalink)
0
I did changed it already to MULT_100  AND FPLLODIV = DIV_2 before you even said it, but it gives a confirmation that I did it the right way anyway.
Yes is think because it is a 64-pin chip.
 
Alright then I learned something more about the LAT and PORT registers, thanks for that.
 
Well I discovered as I mentioned above it is stuck here:
 
void _mon_putc (char c)
{
while (U5STAbits.UTXBF); // Wait til current transmission is complete              <----------
U5TXREG = c;
}
#9
aidanmocke
Starting Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2018/03/07 02:31:41
  • Location: 0
  • Status: offline
Re: PIC32MZ UART printf gets stuck/freezes 2019/02/12 07:16:18 (permalink)
0
Hmmm I've only ever seen it hang there when the UART hasn't been initialised or there's a clock problem, but you seem to have done that all correctly.

I'm out of ideas for the moment and it's getting close to midnight here. I'll be able to try that out tomorrow on my board and let you know. Until then, good luck and if you find the problem then please let us know.
#10
marcc70
Starting Member
  • Total Posts : 44
  • Reward points : 0
  • Joined: 2018/12/03 06:23:00
  • Location: 0
  • Status: offline
Re: PIC32MZ UART printf gets stuck/freezes 2019/02/12 07:19:10 (permalink)
0
Alrigth, thanks for your help so far. 
I really do appreciate your help and your time, I will try to find out because I really want to get it work.
 
For now, have a goodnight ;)
#11
marcc70
Starting Member
  • Total Posts : 44
  • Reward points : 0
  • Joined: 2018/12/03 06:23:00
  • Location: 0
  • Status: offline
Re: PIC32MZ UART printf gets stuck/freezes 2019/02/12 07:53:17 (permalink)
0
I changed my code, and now I use only printf when heartbeat hits the 20001.
This don't gives a problem and the program doesn't get stuck right here:
void _mon_putc (char c)
{
while (U5STAbits.UTXBF); // Wait til current transmission is complete              <----------
U5TXREG = c;
}
 
 

heartbeat++;
if(heartbeat<20000){
LATBbits.LATB2=1;
LATBbits.LATB8=1;

// PORTGbits.RG8=1;
// heartbeat++;
// PORTGbits.RG9=1; //TEST
}
if(heartbeat==20001){
printf("HELLO.%d\n", heartbeat);
}
if(heartbeat>=20000 && heartbeat <40000){
LATBbits.LATB2=0;
LATBbits.LATB8=0;
// heartbeat++;
// printf("HELLO.%d\n", heartbeat);
// delay_ms(3000);


}
if(heartbeat==40000){
heartbeat=0;
}

 
I can also see the voltage switching on the 'RX' of the MCP2200 module (so the TX of the PIC32).
What means it is sending data right..
 
Only problem is I don't see it in my terminal, when screening the com port.
 
#12
marcc70
Starting Member
  • Total Posts : 44
  • Reward points : 0
  • Joined: 2018/12/03 06:23:00
  • Location: 0
  • Status: offline
Re: PIC32MZ UART printf gets stuck/freezes 2019/02/12 10:46:00 (permalink)
0
I enabled UART5 in the simulator, and it is serial printing.
But still can't see anything in the terminal somehow.
 
if(heartbeat==20001){
printf("HELLO.%d\n", heartbeat);
}

 
 
 

Attached Image(s)

#13
aidanmocke
Starting Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2018/03/07 02:31:41
  • Location: 0
  • Status: offline
Re: PIC32MZ UART printf gets stuck/freezes 2019/02/12 17:12:29 (permalink)
0
Hi,
 
Just tested the code on my board and it works fine. I cleaned it up and bit but didn't change much. With your terminal app, you may have to ensure you start the terminal app (or re-open the port) after the MCP2200 gets power and you have to ensure the baud rate is the same as you've set in your app, 38400.
 
How are you powering the board and how are you powering the MCP2200? Via the USB VBUS for the MCP2200 and then through a voltage regulator to the PIC32?
 
Anyway, here is the code I used:
 
// PIC32MZ2048EFH064 Configuration Bit Settings

// 'C' source line config statements

// DEVCFG3
#pragma config USERID = 0xFFFF // Enter Hexadecimal value (Enter Hexadecimal value)
#pragma config FMIIEN = ON // Ethernet RMII/MII Enable (MII Enabled)
#pragma config FETHIO = ON // Ethernet I/O Pin Select (Default Ethernet I/O)
#pragma config PGL1WAY = OFF // Permission Group Lock One Way Configuration (Allow only one reconfiguration)
#pragma config PMDL1WAY = OFF // Peripheral Module Disable Configuration (Allow only one reconfiguration)
#pragma config IOL1WAY = OFF // Peripheral Pin Select Configuration (Allow only one reconfiguration)
#pragma config FUSBIDIO = OFF // USB USBID Selection (Controlled by the USB Module)

// DEVCFG2
#pragma config FPLLIDIV = DIV_3 // System PLL Input Divider (8x Divider)
#pragma config FPLLRNG = RANGE_5_10_MHZ// System PLL Input Range (34-68 MHz Input)
#pragma config FPLLICLK = PLL_POSC // System PLL Input Clock Selection (FRC is input to the System PLL)
#pragma config FPLLMULT = MUL_100 // System PLL Multiplier (PLL Multiply by 128)
#pragma config FPLLODIV = DIV_2 // System PLL Output Clock Divider (32x Divider)
#pragma config UPLLFSEL = FREQ_12MHZ // USB PLL Input Frequency Selection (USB PLL input is 24 MHz)

// DEVCFG1
#pragma config FNOSC = SPLL // Oscillator Selection Bits (Fast RC Osc w/Div-by-N (FRCDIV))
#pragma config DMTINTV = WIN_127_128 // DMT Count Window Interval (Window/Interval value is 127/128 counter value)
#pragma config FSOSCEN = OFF // Secondary Oscillator Enable (Enable SOSC)
#pragma config IESO = ON // Internal/External Switch Over (Enabled)
#pragma config POSCMOD = EC // Primary Oscillator Configuration (Primary osc disabled)
#pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin (Disabled)
#pragma config FCKSM = CSECME // Clock Switching and Monitor Selection (Clock Switch Enabled, FSCM Enabled)
#pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576)
#pragma config WDTSPGM = STOP // Watchdog Timer Stop During Flash Programming (WDT stops during Flash programming)
#pragma config WINDIS = NORMAL // Watchdog Timer Window Mode (Watchdog Timer is in non-Window mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Enabled)
#pragma config FWDTWINSZ = WINSZ_25 // Watchdog Timer Window Size (Window size is 25%)
#pragma config DMTCNT = DMT31 // Deadman Timer Count Selection (2^31 (2147483648))
#pragma config FDMTEN = OFF // Deadman Timer Enable (Deadman Timer is enabled)

// DEVCFG0
#pragma config DEBUG = OFF // Background Debugger Enable (Debugger is disabled)
#pragma config JTAGEN = OFF // JTAG Enable (JTAG Port Enabled)
#pragma config ICESEL = ICS_PGx1 // ICE/ICD Comm Channel Select (Communicate on PGEC1/PGED1)
#pragma config TRCEN = OFF // Trace Enable (Trace features in the CPU are enabled)
#pragma config BOOTISA = MIPS32 // Boot ISA Selection (Boot code and Exception code is MIPS32)
#pragma config FECCCON = OFF_UNLOCKED // Dynamic Flash ECC Configuration (ECC and Dynamic ECC are disabled (ECCCON bits are writable))
#pragma config FSLEEP = OFF // Flash Sleep Mode (Flash is powered down when the device is in Sleep mode)
#pragma config DBGPER = PG_ALL // Debug Mode CPU Access Permission (Allow CPU access to all permission regions)
#pragma config SMCLR = MCLR_NORM // Soft Master Clear Enable bit (MCLR pin generates a normal system Reset)
#pragma config SOSCGAIN = GAIN_2X // Secondary Oscillator Gain Control bits (2x gain setting)
#pragma config SOSCBOOST = OFF // Secondary Oscillator Boost Kick Start Enable bit (Boost the kick start of the oscillator)
#pragma config POSCGAIN = GAIN_2X // Primary Oscillator Gain Control bits (2x gain setting)
#pragma config POSCBOOST = OFF // Primary Oscillator Boost Kick Start Enable bit (Boost the kick start of the oscillator)
#pragma config EJTAGBEN = NORMAL // EJTAG Boot (Normal EJTAG functionality)

// DEVCP0
#pragma config CP = OFF // Code Protect (Protection Disabled)

// SEQ3
#pragma config TSEQ = 0xFFFF // Boot Flash True Sequence Number (Enter Hexadecimal value)
#pragma config CSEQ = 0xFFFF // Boot Flash Complement Sequence Number (Enter Hexadecimal value)



// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

/*
 * File: main.c
 * Author:
 *
 * Created on February 1, 2019, 12:36 PM
 */


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <xc.h>
#define SYS_FREQ 200000000
//Variables
int heartbeat=0;

void set_performance_mode()
{
 unsigned int cp0;
 
    // Unlock Sequence
    asm volatile("di"); // Disable all interrupts
    SYSKEY = 0xAA996655;
    SYSKEY = 0x556699AA;

    // PB1DIV
    // Peripheral Bus 1 cannot be turned off, so there's no need to turn it on
    PB1DIVbits.PBDIV = 1; // Peripheral Bus 1 Clock Divisor Control (PBCLK1 is SYSCLK divided by 2)

    // PB2DIV
    PB2DIVbits.ON = 1; // Peripheral Bus 2 Output Clock Enable (Output clock is enabled)
    PB2DIVbits.PBDIV = 1; // Peripheral Bus 2 Clock Divisor Control (PBCLK2 is SYSCLK divided by 2)

    // PB3DIV
    PB3DIVbits.ON = 1; // Peripheral Bus 2 Output Clock Enable (Output clock is enabled)
    PB3DIVbits.PBDIV = 1; // Peripheral Bus 3 Clock Divisor Control (PBCLK3 is SYSCLK divided by 2)

    // PB4DIV
    PB4DIVbits.ON = 1; // Peripheral Bus 4 Output Clock Enable (Output clock is enabled)
    while (!PB4DIVbits.PBDIVRDY); // Wait until it is ready to write to
    PB4DIVbits.PBDIV = 0; // Peripheral Bus 4 Clock Divisor Control (PBCLK4 is SYSCLK divided by 1)

    // PB5DIV
    PB5DIVbits.ON = 1; // Peripheral Bus 5 Output Clock Enable (Output clock is enabled)
    PB5DIVbits.PBDIV = 1; // Peripheral Bus 5 Clock Divisor Control (PBCLK5 is SYSCLK divided by 2)

    // PB7DIV
    PB7DIVbits.ON = 1; // Peripheral Bus 7 Output Clock Enable (Output clock is enabled)
    PB7DIVbits.PBDIV = 0; // Peripheral Bus 7 Clock Divisor Control (PBCLK7 is SYSCLK divided by 1)

    // PB8DIV
    PB8DIVbits.ON = 1; // Peripheral Bus 8 Output Clock Enable (Output clock is enabled)
    PB8DIVbits.PBDIV = 1; // Peripheral Bus 8 Clock Divisor Control (PBCLK8 is SYSCLK divided by 2)

    // PRECON - Set up prefetch
    PRECONbits.PFMSECEN = 0; // Flash SEC Interrupt Enable (Do not generate an interrupt when the PFMSEC bit is set)
    PRECONbits.PREFEN = 0b11; // Predictive Prefetch Enable (Enable predictive prefetch for any address)
    PRECONbits.PFMWS = 0b010; // PFM Access Time Defined in Terms of SYSCLK Wait States (Two wait states)

    // Set up caching
    cp0 = _mfc0(16, 0);
    cp0 &= ~0x07;
    cp0 |= 0b011; // K0 = Cacheable, non-coherent, write-back, write allocate
    _mtc0(16, 0, cp0);

    // Lock Sequence
    SYSKEY = 0x33333333;
    asm volatile("ei"); // Enable all interrupts
}

void delay_us(unsigned int us)
{
    // Convert microseconds us into how many clock ticks it will take
    us *= SYS_FREQ / 1000000 / 2; // Core Timer updates every 2 ticks
       
    _CP0_SET_COUNT(0); // Set Core Timer count to 0
    
    while (us > _CP0_GET_COUNT()); // Wait until Core Timer count reaches the number we calculated earlier
}

void delay_ms(int ms)
{
    delay_us(ms * 1000);
}



void UART_Init(){

    int pbClk;
    pbClk = SYS_FREQ / 2; // Our PBCLK2 divider was set to 1, so PBCLK2 is exactly half the speed of the system clock, or 100Mhz
//100
    
    U5MODE = 0; // Set UART 5 off prior to setting it up
    U5MODEbits.BRGH = 0; // We want standard speed mode. Not necessary as we just set U5MODE to 0 so this is just for explanation's sake
    U5BRG = pbClk / (16 * 38400) - 1;// This is the formula straight from the datasheet
    U5STA = 0; // Disable the TX and RX pins, clear all flags
    U5STAbits.UTXEN = 1; // Enable the TX pin
    U5STAbits.URXEN = 1; // Enable the RX pin
    U5MODEbits.PDSEL = 0; // PDSEL controls how many data bits and how many parity bits we want, this is the default of 8-bit data, no parity bits that most terminals use
    U5MODEbits.STSEL = 0; // STSEL controls how many stop bits we use, let's use the default of 1
    U5MODEbits.ON = 1; // Turn on the UART 5 peripheral
}

void _mon_putc (char c)
 {
   while (U5STAbits.UTXBF); // Wait til current transmission is complete
   U5TXREG = c;
}


 
void setup()
{

    // Set up all ports to be digital and not analog

ANSELB = 0;
ANSELG = 0;

// Set outputs en inputs
    TRISBbits.TRISB12=1;
    TRISBbits.TRISB13=1;
    TRISGbits.TRISG9=0;
    TRISGbits.TRISG7=0;
    
    TRISBbits.TRISB7=0;
    TRISBbits.TRISB8=0;
    TRISBbits.TRISB3=0;
    TRISBbits.TRISB6=0;
  // TRISBbits.TRISB7=;
  // TRISBbits.TRISB8=;
    TRISBbits.TRISB9=1;
    TRISBbits.TRISB2=0;
    
   // TRISFbits.TRISF1=0;
    
    PORTBbits.RB3=1;
    PORTBbits.RB7=1;
    PORTBbits.RB3=0;
    PORTBbits.RB7=0;
    
    PORTBbits.RB6=0;
    PORTGbits.RG7=0;
    
    return;

}

void main()
{
    setup();
    set_performance_mode();
  
    U5RXR = 0b0100; // RPF1R = U5RX
    RPD3R = 0b0011; // RD3 = U5TX
    TRISFbits.TRISF1 = 1; // Make RF1 an input
    TRISDbits.TRISD3 = 0; // Make RD3 an output
      
    UART_Init();

    while(1)
    {
        heartbeat++;
         if(heartbeat<20000)
         {
                PORTBbits.RB2=1;
                PORTBbits.RB8=1;
            }
            if(heartbeat>=20000 && heartbeat <40000)
            {
                PORTBbits.RB2=0;
                PORTBbits.RB8=0;
                printf("HELLO.%d\n", heartbeat);
            }
            if(heartbeat==40000)
            {
                heartbeat=0;
            }
    }
}

 
Attached are my results from the Arduino Serial Monitor.
 
Edit: Changed back to UART5 from UART4 and worked out how to use code tags.
 
 
post edited by aidanmocke - 2019/02/12 17:34:00

Attached Image(s)

#14
marcc70
Starting Member
  • Total Posts : 44
  • Reward points : 0
  • Joined: 2018/12/03 06:23:00
  • Location: 0
  • Status: offline
Re: PIC32MZ UART printf gets stuck/freezes 2019/02/13 00:16:14 (permalink)
0
Alright thanks, I will try out your code also in a bit.
 
My board used to get powered from a regulator, but when I place the MCP2200 breakout module I used the power 3.3V vdd from the breakout module (see picture).
So my MCP2200 gets his power from the usb port on the laptop and the board gets his power from the breakoutmodule.
 

Attached Image(s)

#15
marcc70
Starting Member
  • Total Posts : 44
  • Reward points : 0
  • Joined: 2018/12/03 06:23:00
  • Location: 0
  • Status: offline
Re: PIC32MZ UART printf gets stuck/freezes 2019/02/13 02:23:38 (permalink)
0
So I tried your code. 
But now it is stuck at the while line when I debug, it seems that it is printing to fast or something.
I think there is something wrong with the breakout module from microchip.
What do you use to go from pic to laptop (UART to USB)?
 

 
void _mon_putc (char c)
{
while (U5STAbits.UTXBF); // Wait til current transmission is complete      <------
U5TXREG = c;
}
 

 
Or Do I need a rs232 to UART converter, with a max3232?
 
post edited by marcc70 - 2019/02/13 02:25:56
#16
marcc70
Starting Member
  • Total Posts : 44
  • Reward points : 0
  • Joined: 2018/12/03 06:23:00
  • Location: 0
  • Status: offline
Re: PIC32MZ UART printf gets stuck/freezes 2019/02/13 05:37:17 (permalink)
0
Well I tried also a 24MHz clock at OSC1 and OSC2, but still stuck at that while line.
I also tried a ATEN USB to Serial converter(rs232), also not able to read any data.
 
I am out of ideas..
 
Maybe I do need to do something with the Vbus.
 
Edit: Schematic (if someone sees a problem/mistake)
post edited by marcc70 - 2019/02/13 06:24:52

Attached Image(s)

#17
aidanmocke
Starting Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2018/03/07 02:31:41
  • Location: 0
  • Status: offline
Re: PIC32MZ UART printf gets stuck/freezes 2019/02/13 14:25:52 (permalink)
0
Hi there,

First of all this has nothing to do with Vbus. You are not using the pic32's USB module at all. You are using a USB UART IC via one of the UART peripherals.

I see you're using a crystal there, try changing this line:

#pragma config POSCMOD = EC

To:

#pragma config POSCMOD = HS

Or if that doesn't work

#pragma config POSCMOD = XT

EC is not for crystals.

You do not need any rs232 stuff, I've connected directly to an ft232 chip, a ch340e and others just fine. Will have a look at this again when I go into work.

Edit: I'm still not clear on how the pic32 is powered. Does the breakout board have a 3.3V voltage regulator and a 3.3V output on it?

I presume the breakout board also has some sort of oscillator on it, right?
post edited by aidanmocke - 2019/02/13 17:17:19
#18
marcc70
Starting Member
  • Total Posts : 44
  • Reward points : 0
  • Joined: 2018/12/03 06:23:00
  • Location: 0
  • Status: offline
Re: PIC32MZ UART printf gets stuck/freezes 2019/02/14 00:07:02 (permalink)
0
Hi Aidan,
 
I see that is a mistake I made in coding.
But switching it to HS didn't change anything, the program is still stuck in the while line "while (U5STAbits.UTXBF); // Wait til current transmission is complete"
 
Here is a schematic for the power supply on the pic below.
I am powering with a 24V supply because I am making a industrial product where 24V suplly is used a lot.
It is regulated to 5V and to 3.3.V afterwards.
 
I ordered a "USB to TTL serial adapter bord UART ( CP2102 chip )" (see also a picture below).
I just ordered out of despair, because I am really out of ideas, I see that there is a message being transferred on the TXpin of the PIC with my oscilloscope. But it just gets stuck in the while loop, and I have no idea why and what that  while loops wants, a confirmation??
 
Edit: When I use the simulator, it is printing at my UART5 output very  slow en when I pause, the program is also stuck at the while loop.
 
 
 
 Edit: I think there is something wrong in coding or something problem with the clock or whatever, I connected the mcp2200 breakout module to the TX and RX pin of my Arduino, and I am receiving serial data!
 
So there is something wrong in the code what keeps it stuck in that while loop..
 
Edit: RTs and CTs, what about them? 
I didn't connected them to my PIC and also not on the Arduino, and the Arduino worked without them so..
post edited by marcc70 - 2019/02/14 02:55:54

Attached Image(s)

#19
marcc70
Starting Member
  • Total Posts : 44
  • Reward points : 0
  • Joined: 2018/12/03 06:23:00
  • Location: 0
  • Status: offline
Re: PIC32MZ UART printf gets stuck/freezes 2019/02/14 03:55:57 (permalink)
0
Hi there!
 
Alright I made some progress, I changed it to UART4 and using RB14(rx) and RPG6R(tx). 
I could only set B and G to digital with ANSEL, so maybe it has to do something with that I couldn't set UART5 to digital.
 
Well, still not working as I wanted because this is wat I get (picture below), when I set the baudrate of the monitor lower then the PIC.
 
If I set the same baudrate it keeps being empty.
 
Edit: It looks like there is something wrong with the baudrate, don't know what because I set everything like it should be.
post edited by marcc70 - 2019/02/14 05:23:07

Attached Image(s)

#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2019 APG vNext Commercial Version 4.5