For-loop exiting when using FLASH_write

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
tysseng
Posts: 89
Joined: 12 Dec 2005 21:01
Location: Oslo, Norway

For-loop exiting when using FLASH_write

#1 Post by tysseng » 30 Dec 2022 14:40

I posted a more ambiguous post about this 13 days ago, now I've stripped everything down to the bare minimum so I'll try asking again:

I now have a program where I write to flash inside a for-loop. But I'm having an issue I don't understand. Here are two versions of the program. The first version exits the loop and/or crashes during the call to FLASH_writeIntBuffer. The second version completes the for-loop and continues into the while-loop in main

What I expect:
16 pulses on portb.F6 then an eternal pulse train on portc.F0

What I see:
1 pulse on portb.F6 then nothing (but previously the pulse train appeared after a single pulse on portb.F6)

Version 1:

Code: Select all

// External functions placed at absolute addresses to make sure other variables
// don't shift around.
void __CC2DW()  org 0x051A;
void KeySequence() org 0x0003;

void FLASH_Write(unsigned, unsigned int*) org 0x005F;
  
// Blocking out space to keep all other variables in their position while
// stripping down the program
unsigned short spacer1[64] absolute 0x0020; // reduce to 63 and everything works
unsigned short spacer2[12] absolute 0x0066;

// variable still in "use". Have tried reducing this to a single byte.
unsigned int flashRow[] = {
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
} absolute 0x0120; // 32 words 14bit values, each takes up two bytes of hex file space

void FLASH_writeIntBuffer(unsigned int buffer, unsigned int address) org 0x114{
  FLASH_write(0x1D00, flashRow);
}

void main() org 0x532 {

  int rowStart = 0 absolute 0x0060;
  
  // Disable all outputs
  GIE_bit = 0;
  PEIE_bit = 0;
  T0EN_bit = 0;
  
  // For debug output
  TRISC.F0 = 0;
  PORTC.F0 = 0;
  
  TRISB.F6 = 0;
  PORTB.F6 = 0;

  for(rowStart = 0; rowStart < 16; rowStart++){
    // 1 - a 5 ms pulse on B6
    PORTB.F6 = 1; delay_ms(5); PORTB.F6 = 0;

    // Writing to a completely different address than what code is at.
    FLASH_writeIntBuffer(0, 0x1D00);

    // 2 - a 15 ms pulse on B6
    PORTB.F6 = 1; delay_ms(15); PORTB.F6 = 0; delay_ms(100);
  }
   
  // These are here just to make sure the compiler doesnt remove them
  spacer1[0] = 1;
  spacer2[0] = 1;

  while(1) {
    // 3 - a 200 ms pulse train on C0
    PORTC.F0 = 1; delay_ms(200);
    PORTC.F0 = 0; delay_ms(200);
  }
}

Version 2 (comments stripped to compress listing):

Code: Select all

void __CC2DW()  org 0x051A;
void KeySequence() org 0x0003;

void FLASH_Write(unsigned, unsigned int*) org 0x005F;
  
unsigned short spacer1[63] absolute 0x0020; // reduce to 63 and everything works
unsigned short spacer2[12] absolute 0x0066;

unsigned int flashRow[] = {
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
} absolute 0x0120; 

void FLASH_writeIntBuffer(unsigned int buffer, unsigned int address) org 0x114{
  FLASH_write(0x1D00, flashRow);
}

void main() org 0x532 {

  int rowStart = 0 absolute 0x0060;
  
  GIE_bit = 0;
  PEIE_bit = 0;
  T0EN_bit = 0;

  TRISC.F0 = 0;
  PORTC.F0 = 0;
  
  TRISB.F6 = 0;
  PORTB.F6 = 0;

  for(rowStart = 0; rowStart < 16; rowStart++){
    PORTB.F6 = 1; delay_ms(5); PORTB.F6 = 0;
    FLASH_writeIntBuffer(0, 0x1D00);
    PORTB.F6 = 1; delay_ms(15); PORTB.F6 = 0; delay_ms(100);
  }
   
  spacer1[0] = 1;
  spacer2[0] = 1;

  while(1) {
    PORTC.F0 = 1; delay_ms(200);
    PORTC.F0 = 0; delay_ms(200);
  }
}

The only difference between them is that in 1), spacer1 takes up 64 bytes and in 2) it takes up 63 bytes. The effect of this is that FLASH_Write_savedINTCON0 moves from 0x00A6 to 0x005F.

Here are the variable placements:

Version 1):
memory_when_failing.png
memory_when_failing.png (131.02 KiB) Viewed 1483 times

Version 2):
memory_when_working.png
memory_when_working.png (139.17 KiB) Viewed 1483 times

Output

The port output is there so I can see what is going on. Here is the output on my logic probes - not working on top, working at the bottom. The single pulse in the non working version is 5ms which is what we would expect the first pulse in the for-loop to be. The 16 first pulses in the working version are actually double, 5 and 15ms, which is exactly what the code says:
combined.png
combined.png (50.37 KiB) Viewed 1483 times


I am at a total loss here. During the process of stripping this down I've seen so much strange (but repeatable) behavior. I had the write within a double loop, and the inner loop would run fine once, then the second time around it would start halfway into the inner loop every time, then continue in main. For some time, with the code very similar to the one above, the loop would exit after the first call to write but CONTINUE in the main loop, which made it quite clear that it isn't just the port output that stops working. Also, moving FLASH_write into main (and removing FLASH_writeIntBuffer) seems to fix things.

For a little more background about the "real" problem - I had a working program but introduced a single global variable and the write loop stopped working.

Details about my setup:
MCU: PIC16F18346
OSC: 32MHz HS crystal oscillator

Project settings:

FEXTOSC External Oscillator mode Selection Bits: -- HS (crystal oscillator above 4MHz)
Power-up default value for COSC bits: -- EXTOSC operating per FEXTOSC bits
Clock Out Enable bit: -- CLKOUT function is disabled
Clock Switch Enable bit: -- Writing to NOSC and NDIV is allowed
Fail-Safe Clock Monitor Enable: -- Fail-Safe Clock Monitor is disabled
Master Clear Enable bit: -- MCLR/VPP pin function is MCLR: Weak pull-up enabled
Power-up Timer Enable bit: -- PWRT disabled
Watchdog Timer Enable bit: -- WDT disabled; SWDTEN is ignored
Low-power BOR enable bit: -- ULPBOR disabled
Brown-out Reset Enable bits: -- Brown-out Reset enabled, SBOREN bit ignored
PPSLOCK bit One-Way Set Enable bit: -- The PPSLOCK bit can be cleared and set only once
Stack Overflow/Underflow Reset Enable bit: -- Stack Overflow or Underflow will cause a Reset
Background Debug: -- Background debugger disabled
User NVM self-write protection bits: -- Write protection off
Low Voltage Programming Enable bit: -- High Voltage on MCLR/VPP must be used for programming.
User NVM Program Memory Code Protection bit: -- User NVM code protection disabled
Data NVM Memory Code Protection bit: -- Data NVM code protection disabled.

Config registers

CONFIG1 : $8007 : 0x0972
CONFIG2 : $8008 : 0x3AE3
CONFIG3 : $8009 : 0x0003
CONFIG4 : $800A : 0x0003

Output settings:
Optimization level: Four
Joakim Tysseng

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: For-loop exiting when using FLASH_write

#2 Post by janni » 30 Dec 2022 18:11

Reducing the code was a good idea :) . There's indeed a compilation error showing in version 1 - after calling FLASH_writeIntBuffer compiler somehow looses track of RAM banking and assumes bank 0 (where the counter rowStart resides) while FLASH_write (and thus FLASH_writeIntBuffer) returns with bank 1 (after using savedINTCON). Adding

Code: Select all

asm movlb 0;
after the call to FLASH_writeIntBuffer will make the code running properly. This proves the point but it's obviously not a general fix to the compiler weakness you discovered :( .

BTW, spacer2 array encroaches on Rx registers, which is of no concern here but could be a problem if present in final code.

tysseng
Posts: 89
Joined: 12 Dec 2005 21:01
Location: Oslo, Norway

Re: For-loop exiting when using FLASH_write

#3 Post by tysseng » 30 Dec 2022 21:55

Janni!

Thank you SO much for confirming this! I thought I was going mad, I've spent every spare moment the last 13days, in between christmas preparations and caring for my sick kids, hunting down this issue. I am so incredibly relieved that we have a solution before the end of the year :-D

I actually logged on now to post a comparison of the listings between the two versions and have highlighted the missing MOVLB 0. The fact that you have come to the same conclusion makes me so happy.

I will have to go back to earlier versions of my program and try the fix there too but at least now I have SOMETHING to try.

I am a bit worried though, when can this weakness show up again? The symptoms were so random... Would reducing the optimization level change anything?

Should I report this as a bug somewhere?


As for the spacers, none of them are there normally, they are just there to make sure nothing moved. But I see what you mean, spacer2 overlaps R0 and R1. Not sure what happened there, I probably just miscalculated its size at one point. But I'll go back and check that as well.

Once again, thank you :D
--
cheers,
Joakim
Joakim Tysseng

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: For-loop exiting when using FLASH_write

#4 Post by janni » 31 Dec 2022 03:38

Hi Joakim,
tysseng wrote:
30 Dec 2022 21:55
I am a bit worried though, when can this weakness show up again? The symptoms were so random... Would reducing the optimization level change anything?
I don't think reducing optimization level would lead to anything better - only level 0 is distinctly different but then you may encounter new quirks (as happened before). I don't know what provokes this error as I could not recreate it without the original FLASH_write library function. Looks like it's a problem within linker when precompiled library is used but it seems to require quite unique circumstances. I never run across it before but then I don't use mE's libraries.
Should I report this as a bug somewhere?
I'm sure these posts will be noticed. Unfortunately, I don't think anyone in mE works on PRO compilers anymore so do not expect a quick fix.

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

Re: For-loop exiting when using FLASH_write

#5 Post by Mince-n-Tatties » 06 Jan 2023 11:14

nicely confirmed janni

1. I would never change the optimizer setting (more hassle than gain), i tend to do the opposite and fight against it with usage of keyword "volatile"
2. janni is also correct on the final point, So i would recommend giving Necto a run. It would be interesting to see if you find the same issue as then it would get mE attention.
Best Regards

Mince

tysseng
Posts: 89
Joined: 12 Dec 2005 21:01
Location: Oslo, Norway

Re: For-loop exiting when using FLASH_write

#6 Post by tysseng » 06 Jan 2023 11:51

Thanks both of you,

I only suggested changing optimization settings because that made stuff work, but I guess that was just because functions were placed on different memory locations.

Anyway - I did realise, as Janni points out, that the MikroC compiler hasn't been updated since 2019. I bought my license way back in 2004 so I guess I shouldn't be too grumpy about it :) The IDE has some sharp edges as well, most importantly that the undo buffer sometimes completely messes up and even makes the program crash...

Necto is definitely worth a look, especially if it is free for non-commercial use. I only use mikroC for a very few old projects as it doesn't run on OSX, and I thought my only option was Microchip's MPLAB XC - which I've used for newer projects.

Thanks again for the suggestions and help :)
Joakim Tysseng

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

Re: For-loop exiting when using FLASH_write

#7 Post by Mince-n-Tatties » 06 Jan 2023 20:18

Hi tysseng

i would really encourage you (everyone) to try Necto, there are some concepts which make for a startup learning curve but it's quick to climb

reasons: you have a good background to help mE and others, the libraries are open source which helps hugely when doing a complete debug being to go through the complete source code instead of having to jump over a library or go into asm! Its free without compromise. Planet debug is amazing, you can test your code live on real kit which you don't have at home. You are not limited to MCU manufacture i.e. you can run ARM, AVR, PIC etc etc without changing compiler.

I can say i was reluctant to move to Necto during beta but i was wrong and should have jumped everything over much sooner.

this an example of a full ARM setup with all the listed Click boards attached, everyone is free to go play on that REAL hardware from the comfort of your own location, remote programming, remote debug etc etc. Proper try before you buy.
NectoPD.jpg
NectoPD.jpg (19.75 KiB) Viewed 1383 times
and that is me connected to the board at mE HQ in Serbia live from my bench in Scotland. amazing!
NectoPD2.jpg
NectoPD2.jpg (217.86 KiB) Viewed 1381 times
Best Regards

Mince

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: For-loop exiting when using FLASH_write

#8 Post by janni » 06 Jan 2023 22:15

Hi Mince,
Mince-n-Tatties wrote:
06 Jan 2023 11:14
So i would recommend giving Necto a run. It would be interesting to see if you find the same issue as then it would get mE attention.
I've already checked whether the AI compiler makes the same error and it doesn't.

I can't say that I share your enthusiasm for Necto, though. It's missing too many features and is quite ungainly even with the simplest things, like changing configuration or switching between projects. Unnervingly, after three years it's still constantly updated and every update ends with the need to painstakingly restore previous settings. Not to mention other surprises.
the libraries are open source which helps hugely when doing a complete debug being to go through the complete source code instead of having to jump over a library or go into asm!
I believe you didn't have to do that lately - otherwise you'd notice how hard it got with the latest update when all c code lines in list file got replaced with 'unresolved line'. Unfortunately, I had to go through the list file to check if the compilation error shows in AI compiler. And libraries like the FLASH one do not come with source files.

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

Re: For-loop exiting when using FLASH_write

#9 Post by Mince-n-Tatties » 11 Jan 2023 23:09

Hi janni
janni wrote:
06 Jan 2023 22:15
I've already checked whether the AI compiler makes the same error and it doesn't.
That's good to know and thanks for the feedback
I can't say that I share your enthusiasm for Necto, though. It's missing too many features and is quite ungainly even with the simplest things, like changing configuration or switching between projects. Unnervingly, after three years it's still constantly updated and every update ends with the need to painstakingly restore previous settings. Not to mention other surprises.
I was not a fan of Necto to start with, it took me a year of poking in and out of it before i forced myself to dig deep and use it properly and now I take the view... for the constant updates: they are constantly working on it. It is a new GUI which takes time to feel your way around and yes the configuration option is a pain until you get use to it and then it becomes a great tool for super easy configuration and platform swap.

Change by it's very nature comes with a new learning curve. What forced me to accept this task was that the new dev kits are not supported in mikroC pro and there are some truly awesome kits now for powerful fast development at low pricing, uni clicker / SiBrain as examples.

I am disappointed with the clunky lack of support for legacy gen 6 and at the very least gen 7 and 7a hardware should have been fully supported as if you had plugged in a gen 8 hardware! I understand it makes sense for mE to want you to buy their new kit, but forcing that is bad business while not giving attention to the pro compiler which (people paid for) works with the previous hardware. If they fix that point for a level of inclusion for legacy kit then that would be a big plus. Given the decision to have a free compiler but have to replace all your dev kits is not a choice most people would opt for. ignore your pro compiler users at your peril!
I believe you didn't have to do that lately - otherwise you'd notice how hard it got with the latest update when all c code lines in list file got replaced with 'unresolved line'. Unfortunately, I had to go through the list file to check if the compilation error shows in AI compiler. And libraries like the FLASH one do not come with source files.
i have not done any debug in version 2.5.0 (not at all since the change to 2.x.x), i will check and see what the status is now.

Planet Debug is awesome though and should be seen to be believed, who else offers a real try before you bye on real hardware on that scale.
Best Regards

Mince

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: For-loop exiting when using FLASH_write

#10 Post by janni » 12 Jan 2023 03:11

Hi Mince,
Mince-n-Tatties wrote:
11 Jan 2023 23:09
What forced me to accept this task was that the new dev kits are not supported in mikroC pro and there are some truly awesome kits now for powerful fast development at low pricing, uni clicker / SiBrain as examples.
Well, that partly explains your cheerful disposition :wink: - Necto was intended to be a tool concentrated on mE boards and no wonder that it fits them well. If one needs to frequently port code to different processors then the HAL layer in SDK is also an advantage - certainly for bigger processors. I just wonder how big a community has such needs. Judging by the number of topics in Necto forums, not nearly as big as mE had for just the 8-bit Microchip processors.
Change by it's very nature comes with a new learning curve.
It's not the learning curve that is a problem. It's actually quite flat for Necto with its simplistic interface and limited features. You probably had in mind the SDK, not the IDE. This might need getting used to, but for 8-bit processors (we're in PIC forum) it's more of an obstacle than help. I've had to use other compilers to be able to work with PIC processors that mE had no time for several years to implement (also for AI compiler) and their learning curves somehow did not deter me. I use still other compilers for various platforms and believe me, none of the compilers constantly change their GUI. That's just unacceptable to present a GUI that needs years to become fully functional. And a GUI which cannot even remember the state of my work so I can close it and reopen next day in the same state is simply nonfunctional.

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

Re: For-loop exiting when using FLASH_write

#11 Post by Mince-n-Tatties » 12 Jan 2023 19:59

janni wrote:
12 Jan 2023 03:11
Hi Mince,
Mince-n-Tatties wrote:
11 Jan 2023 23:09
What forced me to accept this task was that the new dev kits are not supported in mikroC pro and there are some truly awesome kits now for powerful fast development at low pricing, uni clicker / SiBrain as examples.
Well, that partly explains your cheerful disposition :wink: - Necto was intended to be a tool concentrated on mE boards and no wonder that it fits them well. If one needs to frequently port code to different processors then the HAL layer in SDK is also an advantage - certainly for bigger processors. I just wonder how big a community has such needs. Judging by the number of topics in Necto forums, not nearly as big as mE had for just the 8-bit Microchip processors.
Change by it's very nature comes with a new learning curve.
It's not the learning curve that is a problem. It's actually quite flat for Necto with its simplistic interface and limited features. You probably had in mind the SDK, not the IDE. This might need getting used to, but for 8-bit processors (we're in PIC forum) it's more of an obstacle than help. I've had to use other compilers to be able to work with PIC processors that mE had no time for several years to implement (also for AI compiler) and their learning curves somehow did not deter me. I use still other compilers for various platforms and believe me, none of the compilers constantly change their GUI. That's just unacceptable to present a GUI that needs years to become fully functional. And a GUI which cannot even remember the state of my work so I can close it and reopen next day in the same state is simply nonfunctional.
I too use multiple compilers, several of which are now free provided you use the manufacturers MCU's. It's this constant barrage of compilers which adds to reluctance with the task of adding yet another to the list.

MPLAB X (with a choice of compilers), Harmony, STM32Cube, GCC, ARM, KEIL, IAR, Atmel Studio, mikroC pro, Necto, codewarrior, the list goes on ad nauseam and i haven't even added in the PC coding platforms which we constantly have to add yet another with yet another addon or .net framework :cry:
Best Regards

Mince

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: For-loop exiting when using FLASH_write

#12 Post by janni » 12 Jan 2023 23:50

Mince-n-Tatties wrote:
12 Jan 2023 19:59
I too use multiple compilers, several of which are now free provided you use the manufacturers MCU's. It's this constant barrage of compilers which adds to reluctance with the task of adding yet another to the list.
So you know the feeling :) . Still, that's nothing new, only the speed of changes constantly increases. But it's a pity mE made such a gap in support for older hardware and software thus letting the largest community using its compilers to disperse. Maybe their decision makers counted on instant success of Necto. Developing new IDE, better suited to mE's main income source, i.e. hardware boards, must have seemed great idea from their point of view. But the way it went - leaving the community hanging by stopping for years implementation of new processors, and then presenting unfinished product while unceremoniously asking to pay for its beta version :?: :roll: . After three years, it's still an ongoing experiment, with some good ideas but also problems with basic functionality. Though mE's management noticed the lack of popularity, let the 'legacy' option (compatibility with projects for PRO compilers) to be added and lately even dropped AI compilers price to zero, it still hasn't dawned on them that a tool must be functional. Maybe their main target are students, for which it's enough to quickly make a simple project running, get a grade and move on. Otherwise it's really hard to understand present approach to Necto development.

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

Re: For-loop exiting when using FLASH_write

#13 Post by Mince-n-Tatties » 23 Jan 2023 15:39

agreed

I do see the compilers as student/novice target. i have used for that purpose only, teaching kids and young engineers to get a start on the ladder of C while giving them a fast route to some advanced projects by using the vast array of libraries. For the young engineers i expect them to move on to doing their own libraries, which yes they could do in mikroC (Necto) but unfortunately it is not an industry standard and they tend to move on to STM32Cube (free), Atmel-Studio (free), Keil MDK-ARM (£5k yearly).

:MINI RANT ON:
So many free offerings for compilers now, it makes for a tough commercial world for anyone trying to compete. Lets face it free MPLAB and ASM decades ago was without doubt behind the success of pics, MCHP also provided free C environment (albeit stupidly inefficient in certain methods causing bloated slow execution) and played silly games with a certain C provider and the various license levels with release after release and bugs galore while happily charging £1k per year and i am not talking about their on C18 which in itself was costly and prone to numerous updates. They still play the silly game of XC8 (3 versions), XC16 (3 versions), XC32 (3 versions) you can spend upwards of £2.5K a year on a single license and that doesn't buy you any instant help when you find a bug. meanwhile Atmel-Studio (since their takeover) is now free for their AVR and SAM products (ARM) :MINI RANT OFF:

:FULL RANT ON:
I don't see how a "by comparison" SMB with extremely limited resource can take on these giants with almost unlimited resource at this game, in our professional world we pay crazy prices for unfinished product and then we are held ransom to yearly subscription to have half a chance at getting those bugs fixed. There is no such thing as "Code Complete" and so there will always be the cat and mouse game of bug and fix (kindly referred to as "characteristics") combined with product advancement.

Keil MDK-ARM (£5k yearly and that is not for unlimited MCU types). KEIL are in the process of making changes which will render 100's of thousands of JTAG's as door stops, they wont replace these for anyone. Go buy a new updated version is the route and throw your previous up to £400 JTAG in the bin

its everywhere in our world

Altium £10k with yearly sub of £2k
Labview £2.6k yearly (was once £5k with yearly sub of £1k)
you can no longer buy Eagle ECAD without a yearly sub for Fusion 360 (£570 yearly) to AutoCAD and then they throw Eagle premium in for "free", little or no support!

:FULL RANT OFF:
Best Regards

Mince

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: For-loop exiting when using FLASH_write

#14 Post by janni » 23 Jan 2023 16:47

Mince-n-Tatties wrote:
23 Jan 2023 15:39
its everywhere in our world
Yep :( .

Post Reply

Return to “mikroC PRO for PIC General”