Programming PIC32MX210F016B - Page 1
Forum section: electronics / general

author: vlad777
author_rank: Frequent Contributor
timestamp: December 04, 2019, 11:38:08 pm
content: Hi,I have used the MCC gui to create a small code that blinks a led.I am using mplabx snap.It works when i press "debug main project"but it doesn't work when i press "run main project"(doesn't blink the led)(Everything builds successfully, in all cases.and snap output is : Programming/Verify complete)MPLAB X IDE v5.20xc32 v2.30Microchip PIC32MX210F016BCode:// Configuration bits: selected in the GUI// DEVCFG3#pragma config PMDL1WAY = ON // Peripheral Module Disable Configuration->Allow only one reconfiguration#pragma config IOL1WAY = ON // Peripheral Pin Select Configuration->Allow only one reconfiguration#pragma config FUSBIDIO = OFF // USB USID Selection->Controlled by Port Function#pragma config FVBUSONIO = OFF // USB VBUS ON Selection->Controlled by Port Function// DEVCFG2#pragma config FPLLIDIV = DIV_12 // PLL Input Divider->12x Divider#pragma config FPLLMUL = MUL_24 // PLL Multiplier->24x Multiplier#pragma config UPLLIDIV = DIV_12 // USB PLL Input Divider->12x Divider#pragma config UPLLEN = OFF // USB PLL Enable->Disabled and Bypassed#pragma config FPLLODIV = DIV_256 // System PLL Output Clock Divider->PLL Divide by 256// DEVCFG1#pragma config FNOSC = FRCDIV // Oscillator Selection Bits->Fast RC Osc w/Div-by-N (FRCDIV)#pragma config FSOSCEN = OFF // Secondary Oscillator Enable->Disabled#pragma config IESO = OFF // Internal/External Switch Over->Disabled#pragma config POSCMOD = OFF // Primary Oscillator Configuration->Primary osc disabled#pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin->Disabled#pragma config FPBDIV = DIV_2 // Peripheral Clock Divisor->Pb_Clk is Sys_Clk/2#pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection->Clock Switch Disable, FSCM Disabled#pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler->1:1048576#pragma config WINDIS = OFF // Watchdog Timer Window Enable->Watchdog Timer is in Non-Window Mode#pragma config FWDTEN = OFF // Watchdog Timer Enable->WDT Disabled (SWDTEN Bit Controls)#pragma config FWDTWINSZ = WINSZ_25 // Watchdog Timer Window Size->Window Size is 25%// DEVCFG0#pragma config DEBUG = OFF // Background Debugger Enable->Debugger is Disabled#pragma config JTAGEN = OFF // JTAG Enable->JTAG Disabled#pragma config ICESEL = ICS_PGx1 // ICE/ICD Comm Channel Select->Communicate on PGEC1/PGED1#pragma config PWP = OFF // Program Flash Write Protect->Disable#pragma config BWP = OFF // Boot Flash Write Protect bit->Protection Disabled#pragma config CP = OFF // Code Protect->Protection DisabledCode:#include "mcc_generated_files/mcc.h"void mydelay(){int n=100000;while(n>0)n--;}/*Main application*/int main(void){// initialize the deviceSYSTEM_Initialize();IO_RB8_SetDigitalOutput(); //functions generated by the guiIO_RB5_SetDigitalOutput();while (1){// Add your application codeIO_RB8_Toggle(); //functions generated by the guiIO_RB5_Toggle();mydelay();}return -1;}Many thanks.

author: NorthGuy
author_rank: Super Contributor
timestamp: December 05, 2019, 10:14:57 pm
content: Insert a delay between toggles. Otherwise it's too fast to see.

author: vlad777
author_rank: Frequent Contributor
timestamp: December 05, 2019, 10:49:03 pm
content: Quote from: NorthGuy on December 05, 2019, 10:14:57 pmI do have mydelay(); called.(two toggle calls are for different pins.)The led is full off, in run mode, but blinking normally in debug mode.

author: Howardlong
author_rank: Super Contributor
timestamp: December 05, 2019, 10:57:17 pm
content: Do you have a pull up on MCLR?

author: vlad777
author_rank: Frequent Contributor
timestamp: December 05, 2019, 11:39:22 pm
content: Quote from: Howardlong on December 05, 2019, 10:57:17 pmYes.

author: andersm
author_rank: Super Contributor
timestamp: December 05, 2019, 11:53:49 pm
content: With compiler optimizations enabled, that kind of delay loop will be completely optimized away. The quick hack fix is to make the counter variable volatile, but it's much better to use a timer.

author: vlad777
author_rank: Frequent Contributor
timestamp: December 06, 2019, 12:18:08 am
content: Do I need to worry about the bootloader?It could be missing.I didn't set the config for the bootloader right (for example maybe it uses some peripherial)I need to put the pullup or pulldown on the pin that the bootloader probes at startup.

author: NorthGuy
author_rank: Super Contributor
timestamp: December 06, 2019, 12:21:22 am
content: Quote from: vlad777 on December 05, 2019, 10:49:03 pmEven if delays didn't work LED wuldn't be constantly off.Check the voltage on the MCLR pin. Must be VDD. If 0, remove the programmer and see if LEDs blink.

author: Howardlong
author_rank: Super Contributor
timestamp: December 06, 2019, 12:31:09 am
content: What boot loader?

author: vlad777
author_rank: Frequent Contributor
timestamp: December 06, 2019, 12:39:20 am
content: Quote from: Howardlong on December 06, 2019, 12:31:09 amthis chip has a boot loader flash and I don't know if it is properly programmed:boot flash address: 0x1FC00000program flash: 0x1D000000snap output is:Erasing...The following memory area(s) will be programmed:program memory: start address = 0x1d000000, end address = 0x1d000fffconfiguration memoryboot config memoryProgramming/Verify complete

author: cv007
author_rank: Frequent Contributor
timestamp: December 06, 2019, 03:48:04 am
content: >Do I need to worry about the bootloader?There is no bootloader, unless you put one in, and if you were at that point you would have already jumped through a number of hoops to get there. There is a little chunk of flash that is designated as 'boot flash memory', and you don't really have to deal with it. The linker will place some startup code in there, and when debugging some of that area is used for the debug code.I don't have the answer, but when debugging there is added code that is put into the mcu as already indicated but your app code should be the same and it should be seamless to run debug code or 'release' code (and no separate config for each). If the compiler was optimizing out your delay loop, it would do it in both cases which probably just indicates your optimization level is at a default level and is leaving it alone.I'm not sure why your config bits include the DEBUG pragma, as I think the ide turns that fuse bit on/off depending on whether you are debugging or not.I see you don't use the power pin on the snap (pin 2), even though it does not supply power through that pin it is used to set the two programming pin levels. I would probably hook it up even it if seems happy at the moment without it.You can also disconnect the mclr line from the snap, just to see if for some reason its holding the reset line low. Probably not, but takes 2 seconds to check.

author: vlad777
author_rank: Frequent Contributor
timestamp: December 06, 2019, 10:30:17 pm
content: I have found a solution.This guy didn't touch configuration bits and it works.I tried it and it works for me too, which proves that I have bad config in the program.Edit: The issue was the line: #pragma config DEBUG = OFFI just comment it out and it works.

author: vlad777
author_rank: Frequent Contributor
timestamp: December 06, 2019, 10:37:04 pm
content: Here is a blinker in MIPS assembly :Code:# led on pin 3 , mclr pullup 1k.global main.datatris: .word 0xbf886010lat: .word 0xbf886030.textmain:lw $t1,trislw $t2,lataddi $t3,$zero,0b11111101addi $t4,$zero,2sb $t3,0($t1)sb $t4,0($t2)loopn1:li $t0,300000loop1:addi $t0,$t0,-1bne $t0,$zero,loop1sb $zero,0($t2)loopn2:li $t0,3000000loop2:addi $t0,$t0,-1bne $t0,$zero,loop2sb $t4,0($t2)j loopn1

author: vlad777
author_rank: Frequent Contributor
timestamp: December 07, 2019, 08:25:49 am
content: I want to get to a .hex file, from MIPS assembler file, without using microchip technology.Is there a gcc gnu assembler that I can use with this chip?This maybe ?https://codescape.mips.com/components/toolchain/2019.09-01/index.htmlOr this?https://gcc.gnu.org/What do you recommend?Many thanks.PS:I still just want to blink a led.But now I would have to setup vector table, maybe segments, maybe starting stack address.And I suppose I would burn to boot loader area.

author: Howardlong
author_rank: Super Contributor
timestamp: December 07, 2019, 03:13:32 pm
content: I think you’re trying to run before you can walk.I cannot recommend MCC for a dozen+ reasons, but mostly that the code it generates is bug ridden and also it’s hard to maintain because it doesn’t allow separation of user code from machine generated code.What produced the #pragma config DEBUG=OFF?A blinky for this device should take under ten minutes to code up if you ignore MCC.Not setting any config bits risks the watchdog reseting your device frequently.Why are you trying to avoid the Microchip toolchain? (MCC excepted which is a pile of sh!t).

author: andersm
author_rank: Super Contributor
timestamp: December 07, 2019, 05:56:41 pm
content: The GNU assembler is part ofbinutils, not GCC. It'll work, but making everything from scratch is a lot more involved for PIC32Ms than for most other devices.

author: Howardlong
author_rank: Super Contributor
timestamp: December 07, 2019, 06:58:05 pm
content: I am away in Japan at the moment, without a scope, but I do have a Microstick II with me and a PIC32MX170F256B which is kinda the same family.The following toggles RA0 every second or so. I specified the processor to be PIC32MX210F016B and overrode the processor warning.One further thing, if you're intending to use the USB functionality, I wish you luck fitting a stack inside this chip. You really need the PIC32MX220F032B at a minimum, and even then there's not much left over for application code, let alone a bootloader.Code:// PIC32MX210F016B Configuration Bit Settings// 'C' source line config statements// DEVCFG3#pragma config USERID = 0xFFFF // Enter Hexadecimal value (Enter Hexadecimal value)#pragma config PMDL1WAY = OFF // Peripheral Module Disable Configuration (Allow multiple reconfigurations)#pragma config IOL1WAY = OFF // Peripheral Pin Select Configuration (Allow multiple reconfigurations)#pragma config FUSBIDIO = OFF // USB USID Selection (Controlled by Port Function)#pragma config FVBUSONIO = OFF // USB VBUS ON Selection (Controlled by Port Function)// DEVCFG2#pragma config FPLLIDIV = DIV_12 // PLL Input Divider (12x Divider)#pragma config FPLLMUL = MUL_24 // PLL Multiplier (24x Multiplier)#pragma config UPLLIDIV = DIV_12 // USB PLL Input Divider (12x Divider)#pragma config UPLLEN = OFF // USB PLL Enable (Disabled and Bypassed)#pragma config FPLLODIV = DIV_256 // System PLL Output Clock Divider (PLL Divide by 256)// DEVCFG1#pragma config FNOSC = FRC // Oscillator Selection Bits (Fast RC Osc (FRC))#pragma config FSOSCEN = OFF // Secondary Oscillator Enable (Disabled)#pragma config IESO = OFF // Internal/External Switch Over (Disabled)#pragma config POSCMOD = OFF // Primary Oscillator Configuration (Primary osc disabled)#pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin (Disabled)#pragma config FPBDIV = DIV_1 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/1)#pragma config FCKSM = CSECMD // Clock Switching and Monitor Selection (Clock Switch Enable, FSCM Disabled)#pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576)#pragma config WINDIS = OFF // Watchdog Timer Window Enable (Watchdog Timer is in Non-Window Mode)#pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))#pragma config FWDTWINSZ = WINSZ_25 // Watchdog Timer Window Size (Window Size is 25%)// DEVCFG0#pragma config JTAGEN = OFF // JTAG Enable (JTAG Disabled)#pragma config ICESEL = ICS_PGx1 // ICE/ICD Comm Channel Select (Communicate on PGEC1/PGED1)#pragma config PWP = OFF // Program Flash Write Protect (Disable)#pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled)#pragma config CP = OFF // Code Protect (Protection Disabled)// #pragma config statements should precede project file includes.// Use project enums instead of #define for ON and OFF.#include <xc.h>#include <stdint.h>#define CLOCKFREQ 8000000void MyDelayMS(uint32_t u32){while (u32--){uint32_t u32Inner=CLOCKFREQ/8000;while (u32Inner--){Nop();Nop();Nop();}Nop();}}int main(void){BMXCONbits.BMXWSDRM=0; // Zero RAM wait statesTRISAbits.TRISA0=0; // RA0 digital outputANSELAbits.ANSA0=0;while (1){LATAbits.LATA0=1;MyDelayMS(1000);LATAbits.LATA0=0;MyDelayMS(1000);Nop();}return 0;}

author: vlad777
author_rank: Frequent Contributor
timestamp: December 08, 2019, 12:43:03 am
content: Quote from: Howardlong on December 07, 2019, 03:13:32 pmMCC produced it.

author: vlad777
author_rank: Frequent Contributor
timestamp: December 08, 2019, 12:52:56 am
content: So as I understand ,I am suppose to put the code at the address 0x1fc00000.But if I use the .org directive i get 500MB of zeroes in my file.So it seems in the end I have to use linker scripts, which I was hoping to avoid.What would be the simplest linker script (replacing the .org directive)for the assembly program that I posted above in a previous post.BTW I am using Codescape GNU ToolchainCode:"I:\mips-mti-elf\2019.09-01\bin\mips-mti-elf-as" -march=mips32r2 -EL main01.s -o pic32mx.o"I:\mips-mti-elf\2019.09-01\bin\mips-mti-elf-objcopy" -O ihex pic32mx.o pic32mx.hex"I:\mips-mti-elf\2019.09-01\bin\mips-mti-elf-objdump" -D pic32mx.o >disasm.txtpause

author: andersm
author_rank: Super Contributor
timestamp: December 08, 2019, 01:59:38 am
content: Quote from: vlad777 on December 08, 2019, 12:52:56 amNote that that is the physical address, but the CPU always uses virtual addresses (peripherals, like DMA, or the flash controller, use physical addresses). MIPS CPUs always start in KSEG1, so the code must be compiled to start at address 0xBFC00000.QuoteYou need to define some memory regions, and then define output sections to which you map the input sections.I do strongly recommend examining the linker scripts and startup code that ships with XC32. Try to explain what they do, and why.

author: Howardlong
author_rank: Super Contributor
timestamp: December 08, 2019, 06:06:49 am
content: Quote from: vlad777 on December 08, 2019, 12:43:03 amI rest my case m’lud. MCC is indeed a bug ridden pile of sh!t.

author: Howardlong
author_rank: Super Contributor
timestamp: December 08, 2019, 06:13:30 am
content: Quote from: vlad777 on December 08, 2019, 12:52:56 amYou are much more likely to get community support If you stick to MPLAB X and XC32. (And forget about MCC).I popped into Akizuki Denshi in Akihabara an hour or so ago and picked up your exact part when I was in there. I can help you if you stick to MPLAB X and XC32.

author: vlad777
author_rank: Frequent Contributor
timestamp: December 08, 2019, 07:11:31 am
content: Quote from: Howardlong on December 08, 2019, 06:13:30 amI am grateful that you want to help me. Maybe I should have explained my objective earlier.I don't have a project in mind for this chip. Few days ago I was experimenting with NXP LPC810M021FN8FP part.I won't go very specific with any of them , I am just getting a broad picture of software and hardware that different companies offer.And learning just a little bit of MIPS and ARM assembly languages and comparing them to each other and to x86.EDIT:In the past I played with PWM, timers, UART and USB.Maybe I'll try out ADC with this chip using MPLAB,but frist I want to make a blinker with this other toolchain.

author: vlad777
author_rank: Frequent Contributor
timestamp: December 08, 2019, 11:54:22 am
content: I got the blinker working.I had to use one xc32 command:"I:\Microchip\xc32\v2.30\bin\xc32-bin2hex" -v -i pic32mx.elf-i, --virtual use virtual addressesmips-mti-elf-objcopy doesn't understand virtual addresses.

author: andersm
author_rank: Super Contributor
timestamp: December 08, 2019, 01:41:47 pm
content: You can specify a separate virtual address and load addess for each output section in the linker script. Objcopy also has some options for adjusting section VMA and LMA.