Jump to content
 

Using the MPLAB-X Simulator to debug UART code


Orunmila

9,714 views

 Share

One feature of MPLAB-X which is not nearly used enough is the Simulator. It is actually very powerful and useful for testing and debugging code which will run on your board and interact with the outside world via external stimuli. There is more information on the developer help site here http://microchipdeveloper.com/mplabx:scl

This is how you can use the IDE to debug your serial command line interface code using stimulus files and the UART OUTPUT window.

The first trick is to enable the UART output window in the IDE. We are going to start by creating a new project using MCC which drives the UART on the PIC16F18875. Settings should look as follows:

setup.png

Then go on the project settings and enable the UART output window in the simulator 

uart.png

We can now update our main code to do something simple to test the UART output.

#include "mcc_generated_files/mcc.h"
uint8_t buffer[64];

void main(void)
{
    // initialize the device
    SYSTEM_Initialize();

    INTERRUPT_GlobalInterruptEnable();
    INTERRUPT_PeripheralInterruptEnable();

    printf("Hello World\r\n");
    
    

If you run this code in the simulator it will open an additional output window which will show the output from the UART as follows

image.png

Now the next trick is to use a stimulus file to send your test data into the device via the UART to allow you to test your serial processing code.

First we will make a test file, there is more information on the format of these files on the developer help site, but it is fairly easy to understand just by looking at the example.

// Start off by sending 0x00
00

// First string, send the bytes H E L L O (no \0 will be sent)
"HELLO"

// Wait for 100 ms before sending the next bit
wait 100 ms

// Second string
" WORLD"

// Wait for 100 ms before sending the next bit
wait 100 ms

// Send an entirely binary packet
90 03 00 14 55 12

We now simply save this text file so that we can import it into the stimulus system. We will just call it data.txt for now.

Next we have to set up the IDE to send the data in this file. We are going to start by opening the Stimulus editor, this is under the menu "Window | Simulator | Stimulus" in MPLAB-X.

Navigate to the tab labeled "Register Injection" and do the following steps:

  1. To add the file we will enter a new row here. Just type in the empty boxes (or you can use the icons on the left to add a new row) 
  2. Provide any label (we are using TEST)
  3. Select the UART1 Receive Register under Reg/Var - it is called RC1REG
  4. Choose "Message" for the trigger
  5. Click on the filename block to select the file we made above
  6. Set "Wrap" to no - this means it will not repeat
  7. Format should show Pkt and be raed-only

The stimulus is now set up and ready for a test drive.

Screen Shot 2019-01-08 at 7.44.52 PM.png

As a test we are just going to update the program to echo back what it receives and we will be able to see the output in the Simulator Output window as the stimulus is being sent in. Here is the test code:

#include <stdint.h>
#include "mcc_generated_files/mcc.h"

void main(void)
{
    // initialize the device
    SYSTEM_Initialize();

    INTERRUPT_GlobalInterruptEnable();
    INTERRUPT_PeripheralInterruptEnable();
    
    printf("Hello World\r\n");

    while (1)
    {
        EUSART_Write(EUSART_Read());
    }
}

If you now run the simulator the UART output will just show "Hello World" as the start message. When you hit the green run button on the stimulus it will send the data in the file to the program. Hitting the run button a second time will remove the stimulus and if you hit it one more time it will apply the stimulus a second time.

That is a very basic introduction of how to use a stimulus which feeds data from a text file into the UART for testing purposes.

It is a small step from here to feed data into a variable or into a different peripheral, we will not go into that right here, but know that it is easy to also do this. 

When you combine this feature with running MDB from the command line and using a test framework such as Unity http://www.throwtheswitch.org/unity/ you should have all the tools to create automated tests for your serial port code.

As always the project with the code we used is attached.

SimulatorUART.zip

 

 Share

5 Comments


Recommended Comments

  • Member

If you experience strange behavior with MPLAB double check your simulator stimulus files for bad syntax.  I just created a stimulus file for a blog post that will hit tomorrow morning.  I inadvertently placed two sequential quotation marks at the start of a string.  like this:

""lots of stuff to send"

As soon as the simulation was started, MPLAB went into the weeds.  Some stuff could be clicked on, but in the end I had to kill the process and restart.  Only by checking everything with a second pair of eyes was the problem detected.

Link to comment

I followed your instructions step by step. Finally, downloaded SimulatorUART.zip to run.

You say "If you run this code in the simulator it will open an additional output window which will show the output from the UART as follows",

but my screen doesn't prompt out "UART 1 output" window. So I still cannot see printf() result.

My MPLAB X IDE version is 5.35, XC8 is 2.20. And I don't install CCS C Compiler in MPLAB X IDE plugin.

Could you help me to figure out my problem. Thank you!

Link to comment

 

1.     Trying out MPLabX5.5 with Using the MPLAB-X Simulator to debug UART code - What every embedded programmer should know about ... - microforum.cc.html

2.     Extracted the SimulatorUART.zip file,opened the project

2.1. 

2.2.  Upgrade

2.3. 

2.4. 

2.5.  MCC

2.6. 

2.7 Debug

make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf

make[1]: Entering directory 'D:/Software/Microchip/SimulatorAndUART/SimulatorUART-WhatEveryoneShoulKnowEtc/SimulatorUART.X'

make  -f nbproject/Makefile-default.mk dist/default/debug/SimulatorUART.X.debug.elf

make[2]: Entering directory 'D:/Software/Microchip/SimulatorAndUART/SimulatorUART-WhatEveryoneShoulKnowEtc/SimulatorUART.X'

"D:\Programs\xc8\v2.32\bin\xc8-cc.exe"  -mcpu=16F18875 -c  -D__DEBUG=1   -mdfp="D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8"  -fno-short-double -fno-short-float -O0 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default  -msummary=-psect,-class,+mem,-hex,-file  -ginhx32 -Wl,--data-init -mno-keep-startup -mno-osccal -mno-resetbits -mno-save-resetbits -mno-download -mno-stackcall   -std=c99 -gdwarf-3 -mstack=compiled:auto:auto     -o build/default/debug/mcc_generated_files/device_config.p1 mcc_generated_files/device_config.c

"D:\Programs\xc8\v2.32\bin\xc8-cc.exe"  -mcpu=16F18875 -c  -D__DEBUG=1   -mdfp="D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8"  -fno-short-double -fno-short-float -O0 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default  -msummary=-psect,-class,+mem,-hex,-file  -ginhx32 -Wl,--data-init -mno-keep-startup -mno-osccal -mno-resetbits -mno-save-resetbits -mno-download -mno-stackcall   -std=c99 -gdwarf-3 -mstack=compiled:auto:auto     -o build/default/debug/mcc_generated_files/eusart.p1 mcc_generated_files/eusart.c

"D:\Programs\xc8\v2.32\bin\xc8-cc.exe"  -mcpu=16F18875 -c  -D__DEBUG=1   -mdfp="D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8"  -fno-short-double -fno-short-float -O0 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default  -msummary=-psect,-class,+mem,-hex,-file  -ginhx32 -Wl,--data-init -mno-keep-startup -mno-osccal -mno-resetbits -mno-save-resetbits -mno-download -mno-stackcall   -std=c99 -gdwarf-3 -mstack=compiled:auto:auto     -o build/default/debug/mcc_generated_files/interrupt_manager.p1 mcc_generated_files/interrupt_manager.c

"D:\Programs\xc8\v2.32\bin\xc8-cc.exe"  -mcpu=16F18875 -c  -D__DEBUG=1   -mdfp="D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8"  -fno-short-double -fno-short-float -O0 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default  -msummary=-psect,-class,+mem,-hex,-file  -ginhx32 -Wl,--data-init -mno-keep-startup -mno-osccal -mno-resetbits -mno-save-resetbits -mno-download -mno-stackcall   -std=c99 -gdwarf-3 -mstack=compiled:auto:auto     -o build/default/debug/mcc_generated_files/mcc.p1 mcc_generated_files/mcc.c

"D:\Programs\xc8\v2.32\bin\xc8-cc.exe"  -mcpu=16F18875 -c  -D__DEBUG=1   -mdfp="D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8"  -fno-short-double -fno-short-float -O0 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default  -msummary=-psect,-class,+mem,-hex,-file  -ginhx32 -Wl,--data-init -mno-keep-startup -mno-osccal -mno-resetbits -mno-save-resetbits -mno-download -mno-stackcall   -std=c99 -gdwarf-3 -mstack=compiled:auto:auto     -o build/default/debug/mcc_generated_files/pin_manager.p1 mcc_generated_files/pin_manager.c

"D:\Programs\xc8\v2.32\bin\xc8-cc.exe"  -mcpu=16F18875 -c  -D__DEBUG=1   -mdfp="D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8"  -fno-short-double -fno-short-float -O0 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default  -msummary=-psect,-class,+mem,-hex,-file  -ginhx32 -Wl,--data-init -mno-keep-startup -mno-osccal -mno-resetbits -mno-save-resetbits -mno-download -mno-stackcall   -std=c99 -gdwarf-3 -mstack=compiled:auto:auto     -o build/default/debug/main.p1 main.c

Non line specific message::: warning: (1020) unknown attribute "CONFIGPROG" in chipinfo file "D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8\pic\dat\ini\16f18875.ini" at line 186

Non line specific message::: warning: (1020) unknown attribute "IDLOCPROG" in chipinfo file "D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8\pic\dat\ini\16f18875.ini" at line 195

Non line specific message::: warning: (1020) unknown attribute "CONFIGPROG" in chipinfo file "D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8\pic\dat\ini\16f18875.ini" at line 186

Non line specific message::: warning: (1020) unknown attribute "IDLOCPROG" in chipinfo file "D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8\pic\dat\ini\16f18875.ini" at line 195

Non line specific message::: warning: (1020) unknown attribute "CONFIGPROG" in chipinfo file "D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8\pic\dat\ini\16f18875.ini" at line 186

Non line specific message::: warning: (1020) unknown attribute "IDLOCPROG" in chipinfo file "D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8\pic\dat\ini\16f18875.ini" at line 195

Non line specific message::: warning: (1020) unknown attribute "CONFIGPROG" in chipinfo file "D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8\pic\dat\ini\16f18875.ini" at line 186

Non line specific message::: warning: (1020) unknown attribute "IDLOCPROG" in chipinfo file "D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8\pic\dat\ini\16f18875.ini" at line 195

Non line specific message::: warning: (1020) unknown attribute "CONFIGPROG" in chipinfo file "D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8\pic\dat\ini\16f18875.ini" at line 186

Non line specific message::: warning: (1020) unknown attribute "IDLOCPROG" in chipinfo file "D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8\pic\dat\ini\16f18875.ini" at line 195

Non line specific message::: warning: (1020) unknown attribute "CONFIGPROG" in chipinfo file "D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8\pic\dat\ini\16f18875.ini" at line 186

Non line specific message::: warning: (1020) unknown attribute "IDLOCPROG" in chipinfo file "D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8\pic\dat\ini\16f18875.ini" at line 195

"D:\Programs\xc8\v2.32\bin\xc8-cc.exe"  -mcpu=16F18875 -Wl,-Map=dist/default/debug/SimulatorUART.X.debug.map  -D__DEBUG=1  -DXPRJ_default=default  -Wl,--defsym=__MPLAB_BUILD=1   -mdfp="D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8"  -fno-short-double -fno-short-float -O0 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -msummary=-psect,-class,+mem,-hex,-file  -ginhx32 -Wl,--data-init -mno-keep-startup -mno-osccal -mno-resetbits -mno-save-resetbits -mno-download -mno-stackcall -std=c99 -gdwarf-3 -mstack=compiled:auto:auto         -Wl,--memorysummary,dist/default/debug/memoryfile.xml -o dist/default/debug/SimulatorUART.X.debug.elf  build/default/debug/mcc_generated_files/eusart.p1 build/default/debug/mcc_generated_files/device_config.p1 build/default/debug/mcc_generated_files/interrupt_manager.p1 build/default/debug/mcc_generated_files/mcc.p1 build/default/debug/mcc_generated_files/pin_manager.p1 build/default/debug/main.p1    

Non line specific message::: warning: (1020) unknown attribute "CONFIGPROG" in chipinfo file "D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8\pic\dat\ini\16f18875.ini" at line 186

Non line specific message::: warning: (1020) unknown attribute "IDLOCPROG" in chipinfo file "D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8\pic\dat\ini\16f18875.ini" at line 195

Non line specific message::: warning: (1020) unknown attribute "CONFIGPROG" in chipinfo file "D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8\pic\dat\ini\16f18875.ini" at line 186

Non line specific message::: warning: (1020) unknown attribute "IDLOCPROG" in chipinfo file "D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8\pic\dat\ini\16f18875.ini" at line 195

mcc_generated_files/eusart.c:113:: warning: (520) function "_EUSART_is_tx_ready" is never called

mcc_generated_files/eusart.c:118:: warning: (520) function "_EUSART_is_rx_ready" is never called

mcc_generated_files/eusart.c:123:: warning: (520) function "_EUSART_is_tx_done" is never called

mcc_generated_files/eusart.c:171:: warning: (520) function "_getch" is never called

mcc_generated_files/pin_manager.c:124:: warning: (520) function "_PIN_MANAGER_IOC" is never called

Non line specific message::: warning: (1020) unknown attribute "CONFIGPROG" in chipinfo file "D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8\pic\dat\ini\16f18875.ini" at line 186

Non line specific message::: warning: (1020) unknown attribute "IDLOCPROG" in chipinfo file "D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8\pic\dat\ini\16f18875.ini" at line 195

Non line specific message::: warning: (1020) unknown attribute "CONFIGPROG" in chipinfo file "D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8\pic\dat\ini\16f18875.ini" at line 186

Non line specific message::: warning: (1020) unknown attribute "IDLOCPROG" in chipinfo file "D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8\pic\dat\ini\16f18875.ini" at line 195

Non line specific message::: warning: (1020) unknown attribute "CONFIGPROG" in chipinfo file "D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8\pic\dat\ini\16f18875.ini" at line 186

Non line specific message::: warning: (1020) unknown attribute "IDLOCPROG" in chipinfo file "D:/Programs/Microchip/MPLABX/v5.50/packs/Microchip/PIC16F1xxxx_DFP/1.7.146/xc8\pic\dat\ini\16f18875.ini" at line 195

 

Memory Summary:

    Program space        used   27Dh (   637) of  2000h words   (  7.8%)

    Data space           used    94h (   148) of   400h bytes   ( 14.5%)

    EEPROM space         used     0h (     0) of   100h bytes   (  0.0%)

    Configuration bits   used     5h (     5) of     5h words   (100.0%)

    ID Location space    used     0h (     0) of     4h bytes   (  0.0%)

 

make[2]: Leaving directory 'D:/Software/Microchip/SimulatorAndUART/SimulatorUART-WhatEveryoneShoulKnowEtc/SimulatorUART.X'

make[1]: Leaving directory 'D:/Software/Microchip/SimulatorAndUART/SimulatorUART-WhatEveryoneShoulKnowEtc/SimulatorUART.X'

 

BUILD SUCCESSFUL (total time: 6s)

Loading symbols from D:/Software/Microchip/SimulatorAndUART/SimulatorUART-WhatEveryoneShoulKnowEtc/SimulatorUART.X/dist/default/debug/SimulatorUART.X.debug.elf...

Loading code from D:/Software/Microchip/SimulatorAndUART/SimulatorUART-WhatEveryoneShoulKnowEtc/SimulatorUART.X/dist/default/debug/SimulatorUART.X.debug.elf...

Program loaded with pack,PIC16F1xxxx_DFP,1.7.146,Microchip

Loading completed

2.7.  Output-Simulator Tab

W0106-SIM: This device only has partial support for use internal oscillator as timer clock slection peripheral. not implemented

W0106-SIM: This device only has partial support for use internal oscillator as timer clock slection peripheral. not implemented

W0106-SIM: This device only has partial support for use internal oscillator as timer clock slection peripheral. not implemented

Resetting SFRs

Resetting peripherals

Resetting SFRs

Resetting peripherals

Running

2.8. 

2.9.  Stimulus>GreenButton

2.10.                

2.11.                 UART1 output no change

2.12.                 Stimulus-

2.13.                

2.14.                 Applied Stimulus 3 times

2.15.                 Each time “Synchronous Stimulus Applied Successfully” and "Stimulus removed..." message,

2.16.                 But no change in UART1 output.

2.17.                 And Output>Simulator shows

2.18.                 W0106-SIM: This device only has partial support for use internal oscillator as timer clock slection peripheral. not implemented

2.19.                 W0106-SIM: This device only has partial support for use internal oscillator as timer clock slection peripheral. not implemented

2.20.                 W0106-SIM: This device only has partial support for use internal oscillator as timer clock slection peripheral. not implemented

Resetting SFRs

Resetting peripherals

Resetting SFRs

Resetting peripherals

Running

E0108-SIM: Failed simulator operation: java.lang.NullPointerException

   com.microchip.mplab.mdbcore.simulator.pic16.peripherals.UART.RxUART.processPacketInjection(RxUART.java:132)

 

   com.microchip.mplab.mdbcore.simulator.pic16.peripherals.UART.RxUART$UARTRxInjectionListener.notify(RxUART.java:120)

 

   com.microchip.mplab.mdbcore.simulator.scl.RegisterInjectionDefault.addValues(RegisterInjectionDefault.java:123)

 

   com.microchip.mplab.mdbcore.simulator.scl.Instruction.executePacketIn(Instruction.java:1336)

 

   com.microchip.mplab.mdbcore.simulator.scl.Instruction.executeCall(Instruction.java:922)

 

   com.microchip.mplab.mdbcore.simulator.scl.Instruction.execute(Instruction.java:321)

 

   com.microchip.mplab.mdbcore.simulator.scl.Process.run(Process.java:166)

 

   com.microchip.mplab.mdbcore.simulator.scl.SCLDefault.run(SCLDefault.java:275)

 

   com.microchip.mplab.mdbcore.simulator.scl.SCLDefault.run(SCLDefault.java:291)

 

   com.microchip.mplab.mdbcore.simulator.pic16.ProcessorMidRange$RunExecution.stepInstruction(ProcessorMidRange.java:389)

 

   com.microchip.mplab.mdbcore.simulator.pic16.ProcessorMidRange.executeInstruction(ProcessorMidRange.java:262)

 

   com.microchip.mplab.mdbcore.simulator.pic16.ProcessorMidRange.singleStep(ProcessorMidRange.java:448)

 

   com.microchip.mplab.mdbcore.simulator.Simulator.RunTarget(Simulator.java:339)

 

   com.microchip.mplab.mdbcore.debugger.MCDebugger$1.run(MCDebugger.java:576)

 

   java.lang.Thread.run(Thread.java:748)

2.21.                  

3.    

4.     Please let me know where the problem is

5.      

Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...