Author Topic: PIC24EP problem, double rmw to io register fails  (Read 5642 times)

0 Members and 1 Guest are viewing this topic.

Offline fourtytwo42Topic starter

  • Super Contributor
  • ***
  • Posts: 1184
  • Country: gb
  • Interested in all things green/ECO NOT political
PIC24EP problem, double rmw to io register fails
« on: February 25, 2018, 04:50:39 pm »
For example if you use bset twice to the same io register (different bits) sometimes it fails and not always the second! In my particular case this is to a register within the HSPWM so may be specific to that peripheral. Adding a nop or two between sucsessive rmw's doesnt work so the only alternative when bit twiddling multiple bits is do it in a shadow register and load it in a single mov, that works :)

I hope this helps others rather than attract unwarented comments. This is not in the Errata.
 

Offline kony

  • Regular Contributor
  • *
  • Posts: 242
  • Country: cz
Re: PIC24EP problem, double rmw to io register fails
« Reply #1 on: February 25, 2018, 05:14:50 pm »
Are you sure you are witing to the output shadow register and not the port I/O one? Similar thing happens with 18F families, write to PORTx sometimes works, but as it is R/M/W with the actual values present on the I/O port, it can go corrupt if something pulls or glitches the output value at the time of execution of the instruction. Hence why shadow output LATx register set is present (which gets automatically copied to PORTx enabled outputs).
 

Offline fourtytwo42Topic starter

  • Super Contributor
  • ***
  • Posts: 1184
  • Country: gb
  • Interested in all things green/ECO NOT political
Re: PIC24EP problem, double rmw to io register fails
« Reply #2 on: February 25, 2018, 05:41:46 pm »
Sorry I did clearly state "register within the HSPWM" nothing to do with port i/o. It is however an io register with respect to the cpu, i.e. not a cpu core register, hope that clarifies things :)
 

Offline Benta

  • Super Contributor
  • ***
  • Posts: 5869
  • Country: de
Re: PIC24EP problem, double rmw to io register fails
« Reply #3 on: February 25, 2018, 08:06:51 pm »
Back in the early 80s when I learned coding (assembler in those days), I got burned so bad using RMW instructions on peripherals, that I made myself a promise never to do that again.
If I use an RMW, then only on RAM, and only on single-CPU systems.

One issue is, that just reading values from a peripheral can change the value. As an example, reading an interrupt register clears the interrupt, because the peripheral interprets this as the interrupt being serviced (which is correct from the peripheral's point of view).

I suggest you do atomic reads, modifies and writes, because only in that way are you in control of what happens.

Compilers usually do the same, by the way.

 

Offline fourtytwo42Topic starter

  • Super Contributor
  • ***
  • Posts: 1184
  • Country: gb
  • Interested in all things green/ECO NOT political
Re: PIC24EP problem, double rmw to io register fails
« Reply #4 on: February 26, 2018, 07:46:50 pm »
Thank you for your insight, as an assembler programmer I tend to make full use of the offered instruction set and get a bit peeved when it doesnt work! However in the case of this processor the peripherals run at half the core clock speed so I am not entirely suprised at the defect.  Given Microchip seem to have abandoned assembly programmers (see peoples comments about MPLAB-X etc) I would expect all there testing is performed using C and as you pointed out the compiler avoids rmw. rmw is a heavely used construct particuarly in multiprocessing for indivisable lock flags so given C compilers apparently avoid this at all times some programming problems must prove interesting :)
 

Offline andersm

  • Super Contributor
  • ***
  • Posts: 1198
  • Country: fi
Re: PIC24EP problem, double rmw to io register fails
« Reply #5 on: February 26, 2018, 08:12:05 pm »
Does it help if you read back the register after writing (or at least before the next write)? That functions as a workaround for bus synchronization issues on some devices.

Offline Benta

  • Super Contributor
  • ***
  • Posts: 5869
  • Country: de
Re: PIC24EP problem, double rmw to io register fails
« Reply #6 on: February 26, 2018, 08:28:39 pm »
rmw is a heavely used construct particuarly in multiprocessing for indivisable lock flags so given C compilers apparently avoid this at all times some programming problems must prove interesting :)

This is not quite true. CPU architects are aware of this issue and have evolved special RMW instructions for multiprocessor systems.
For example, the M68k architecture provides the TAS instruction, which is an indivisible RMW, meaning it locks the bus until it completes. This is in contrast to normal RMWs (like BSET), where bus arbitration may happen during instruction execution (and subsequent catastrophic results).
 

Offline fourtytwo42Topic starter

  • Super Contributor
  • ***
  • Posts: 1184
  • Country: gb
  • Interested in all things green/ECO NOT political
Re: PIC24EP problem, double rmw to io register fails
« Reply #7 on: February 26, 2018, 08:39:54 pm »
I must confess as the indivisability of the rmw was not an issue in this application I didnt spend a lot of time investigating it further. I can say by observation a 3rd rmw some long time after the first two adgacent ones flushes the missing write of whichever failed, very mysterious indeed but not having a description of there synchroniser it's difficult to imagine the circumstances. The failure rate is also quite random, it is however very typical of a metastable condition oops!!
If you wish to try testing this try using bset & bclr on pairs of bits in the PWM IOCONx register specifically the OVRDAT bits, you need a scope on the PWMx outputs to see the problem, I am running the cpu at ~40Mips.
 

Offline fourtytwo42Topic starter

  • Super Contributor
  • ***
  • Posts: 1184
  • Country: gb
  • Interested in all things green/ECO NOT political
Re: PIC24EP problem, double rmw to io register fails
« Reply #8 on: February 26, 2018, 08:55:45 pm »
rmw is a heavely used construct particuarly in multiprocessing for indivisable lock flags so given C compilers apparently avoid this at all times some programming problems must prove interesting :)

This is not quite true. CPU architects are aware of this issue and have evolved special RMW instructions for multiprocessor systems.
For example, the M68k architecture provides the TAS instruction, which is an indivisible RMW, meaning it locks the bus until it completes. This is in contrast to normal RMWs (like BSET), where bus arbitration may happen during instruction execution (and subsequent catastrophic results).
Yes well Microchip are long past the point where they provide such detailed information to there users, however in this case where only a single processor is in use and there is no other active bus master the rmw has clearly failed due to the intracasies of there synchronisation logic nececiated by the core running at twice the peripheral frequency. To muddy the waters with what compilers do or not and other processor architectures does not help others to be warned about the simple point I have outlined.
 

Offline Benta

  • Super Contributor
  • ***
  • Posts: 5869
  • Country: de
Re: PIC24EP problem, double rmw to io register fails
« Reply #9 on: February 26, 2018, 10:04:36 pm »
42, if you are pissed off at the PICs, I understand you. In my personal opinion they were always crap.
But the best parts don't always win. PICs are popular because Microchip marketing catered to hobbyists and students, the big players ignored them. Students turn into engineers, though. And suddenly PICs are mainstream.

I still think they are not well designed and would never use one myself.

Feel better? Probably not, but you now know my opinion.
« Last Edit: February 26, 2018, 10:46:20 pm by Benta »
 

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 627
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: PIC24EP problem, double rmw to io register fails
« Reply #10 on: February 26, 2018, 10:18:41 pm »
For example if you use bset twice to the same io register (different bits) sometimes it fails and not always the second! In my particular case this is to a register within the HSPWM
Which PIC24EP, and can you show us the code that fails? How often does it fail?

 
The following users thanked this post: JPortici

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5317
  • Country: gb
Re: PIC24EP problem, double rmw to io register fails
« Reply #11 on: February 26, 2018, 11:12:01 pm »
Happy to try it here, I have both PIC24EP256GP202 and PIC24EP256MC202 in stock if that helps. I have EP series with a larger numbers of pins in stock but they're less easy to breadboard.

Are you saying the RMW just is not working, or that the peripheral takes no notice of the setting?

I am wary of setting out on writing my own PWM code on this device as this specific PWM peripheral is very complex: I did some work on a dsPIC33 with a similar PWM peripheral a couple of years ago, it takes a lot of time to understand and get right. Happy to try your example code.

What I do note, if it's a PIC24EPXXXMC20X, that there's a config bit PWMLOCK that locks you out of changing the IOCONx and FCLCONx registers unless you write 0xABCD and 0x4321 to the PWMKEY register first. Section 16.1.2 of the datasheet, DS70000657H, page 226.

The code below I tested on a PIC24EP256MC202 and it won't set the bit on the register unless I compile the code with #pragma config PWMLOCK = OFF

Code: [Select]

// PIC24EP256MC202 Configuration Bit Settings

// 'C' source line config statements

// FICD
#pragma config ICS = PGD3               // ICD Communication Channel Select bits (Communicate on PGEC3 and PGED3)
#pragma config JTAGEN = OFF             // JTAG Enable bit (JTAG is disabled)

// FPOR
#pragma config ALTI2C1 = OFF            // Alternate I2C1 pins (I2C1 mapped to SDA1/SCL1 pins)
#pragma config ALTI2C2 = OFF            // Alternate I2C2 pins (I2C2 mapped to SDA2/SCL2 pins)
#pragma config WDTWIN = WIN25           // Watchdog Window Select bits (WDT Window is 25% of WDT period)

// FWDT
#pragma config WDTPOST = PS32768        // Watchdog Timer Postscaler bits (1:32,768)
#pragma config WDTPRE = PR128           // Watchdog Timer Prescaler bit (1:128)
#pragma config PLLKEN = OFF             // PLL Lock Enable bit (Clock switch will not wait for the PLL lock signal.)
#pragma config WINDIS = OFF             // Watchdog Timer Window Enable bit (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = OFF             // Watchdog Timer Enable bit (Watchdog timer enabled/disabled by user software)

// FOSC
#pragma config POSCMD = NONE            // Primary Oscillator Mode Select bits (Primary Oscillator disabled)
//#pragma config OSCIOFNC = ON            // OSC2 Pin Function bit (OSC2 is general purpose digital I/O pin)
#pragma config OSCIOFNC = OFF           // OSC2 Pin Function bit (OSC2 is clock output)
#pragma config IOL1WAY = OFF            // Peripheral pin select configuration (Allow multiple reconfigurations)
#pragma config FCKSM = CSECMD           // Clock Switching Mode bits (Clock switching is enabled,Fail-safe Clock Monitor is disabled)

// FOSCSEL
#pragma config FNOSC = FRC              // Oscillator Source Selection (Internal Fast RC (FRC))
//#pragma config PWMLOCK = OFF            // PWM Lock Enable bit (PWM registers may be written without key sequence)
#pragma config PWMLOCK = ON             // PWM Lock Enable bit (Certain PWM registers may only be written after key sequence)
#pragma config IESO = OFF               // Two-speed Oscillator Start-up Enable bit (Start up with user-selected oscillator source)

// FGS
#pragma config GWRP = OFF               // General Segment Write-Protect bit (General Segment may be written)
#pragma config GCP = OFF                // General Segment Code-Protect bit (General Segment Code protect is Disabled)

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

#include <xc.h>

int main(void)
{
    TRISBbits.TRISB5=0;
   
    while (1)
    {
        Nop();
        LATBbits.LATB5=1;
        LATBbits.LATB5=0;

        IOCON1bits.OSYNC=1;
        Nop();
        IOCON1bits.OSYNC=0;
        Nop();
    }
    return 0;
}

Code: [Select]
385 00300 FA0000 main LNK #0x0
386 00302 A9AE10 BCLR TRISB, #5
387 00304 000000 NOP
388 00306 A8AE14 BSET LATB, #5
389 00308 A9AE14 BCLR LATB, #5
390 0030A A80C22 BSET IOCON1, #0
391 0030C 000000 NOP
392 0030E A90C22 BCLR IOCON1, #0
393 00310 000000 NOP
394 00312 37FFF8 BRA 0x304


Edit: Note that I get the same behaviour with the OVRDAT0 bit.
« Last Edit: February 26, 2018, 11:21:21 pm by Howardlong »
 

Online JPortici

  • Super Contributor
  • ***
  • Posts: 3461
  • Country: it
Re: PIC24EP problem, double rmw to io register fails
« Reply #12 on: February 27, 2018, 07:08:34 am »
For example if you use bset twice to the same io register (different bits) sometimes it fails and not always the second! In my particular case this is to a register within the HSPWM
Which PIC24EP, and can you show us the code that fails? How often does it fail?



i remember him complaining about MPLABX, that it was extremely slow but omitting the completely irrelevant information that he was using an ancient PC with no SSD  ::)

I also want to know the part and the code so i can crosscheck, the HSPWM is a relatively newer and complex peripheral and there are caveats in how and when to write to certain register.
It's very possible that in some of the register you can't perform two RMW instructions in a row, and that it is stated in either the datasheet or the reference manual or both.

Regarding unlocking, as with other simillar operations the compiler provides builtins that guarantees the correct excecution of the unlocking sequence. they are described in the compiler manual and used in the examples provided in the reference manual
 

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 627
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: PIC24EP problem, double rmw to io register fails
« Reply #13 on: February 27, 2018, 11:00:19 am »
i remember him complaining about MPLABX, that it was extremely slow but omitting the completely irrelevant information that he was using an ancient PC with no SSD  ::)
Yeah we know, if you don't have the very latest top-of-the line hardware it's your fault if the bloatware runs slow.  ::)

Quote
I also want to know the part and the code so i can crosscheck, the HSPWM is a relatively newer and complex peripheral and there are caveats in how and when to write to certain register.
Or it's just buggy, but once again that's your fault for not having the latest chip revision (or if a bug-free version hasn't been released yet, expecting it to work...).

They tell me that any PIC with less than 5 digits is deprecated and I'm a criminal for running Windows XP, but I don't care. MPlab8 and PicKit2 may not do the latest PICs, but they work better and I don't need the latest PICs for my projects. My hobby is retro computing - and now my MCUs and development tools are too!

Quote from: fourtytwo42
Microchip are long past the point where they provide such detailed information to there users
which is a pity because that was their strong suit, making up their mediocre performance compared to other brands.

Anyway as an assembler programmer myself I am curious to see your code, as I have a few PIC24F's that I might want to use some day.

 
 
The following users thanked this post: fourtytwo42

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5317
  • Country: gb
Re: PIC24EP problem, double rmw to io register fails
« Reply #14 on: February 27, 2018, 12:19:21 pm »
 “Latest top of the range hardware”... like the ICD4, which I discovered doesn’t debug the PIC24EP (along with quite a few others, at least not even slightly reliably).
 
The following users thanked this post: fourtytwo42

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 21658
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: PIC24EP problem, double rmw to io register fails
« Reply #15 on: February 27, 2018, 02:01:37 pm »
PIC24 bad enough, but PIC24E?  :scared:

I've worked on a project using that... the software guys didn't have much hair left.

As the hardware guy, I was in and out of that project in a couple months; the software guys put several years into it!  (Not that that's necessarily an implication against the CPU; you don't go that long without having concerns about project management or feature creep.)

It should be no surprise that the new-design boards for that customer used STM32s...  (The PIC24E persisted for two reasons: 1. adapting and improving existing customer design; 2. existing customer codebase.)

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 
The following users thanked this post: fourtytwo42

Offline fourtytwo42Topic starter

  • Super Contributor
  • ***
  • Posts: 1184
  • Country: gb
  • Interested in all things green/ECO NOT political
Re: PIC24EP problem, double rmw to io register fails
« Reply #16 on: February 27, 2018, 02:20:06 pm »
i remember him complaining about MPLABX, that it was extremely slow but omitting the completely irrelevant information that he was using an ancient PC with no SSD  ::)
If you wish to troll people go find someone else I can do without the likes of you!
 

Offline fourtytwo42Topic starter

  • Super Contributor
  • ***
  • Posts: 1184
  • Country: gb
  • Interested in all things green/ECO NOT political
Re: PIC24EP problem, double rmw to io register fails
« Reply #17 on: February 27, 2018, 02:29:40 pm »
Thank you for your reply's first of all some code
   mov      #iocon2init,w0
   mov      w0,IOCON2
;   bset   IOCON2,#OVRDAT1
;   bset   IOCON2,#OVRDAT0
The commented out stuff doesnt work, the single mov works :)

So let me see, well I dont hate pics in fact I have been using them for years, I only made this post in case some other poor sod is trying to figure out why his/her code isn't working.

Yes tim I like the STM32 also but I dont use it because I am still building cpu's without pcb's in a small space, this does admitedly cause me problems with restricted numbers of io pins. For the present project a gti I can just about manage with this part (PIC24EP256MC202) but it's a close thing! I do pcb's sometimes for example audio dsp's where dip simply isnt available but I like to keep my costs down. Also when handling high power devices it's essential IMOP to have absolute control and do things fast hence assembly preferance. I have/do write tons of C for BIG processors but I dont consider a PIC in that way, its just an itsy bitsy machine control engine :)
 

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5317
  • Country: gb
Re: PIC24EP problem, double rmw to io register fails
« Reply #18 on: February 27, 2018, 02:54:45 pm »
Can you please give more context?

I may have missed it, but I couldn't see any reference regarding your PWMLOCK config bit setting.
 

Offline fourtytwo42Topic starter

  • Super Contributor
  • ***
  • Posts: 1184
  • Country: gb
  • Interested in all things green/ECO NOT political
Re: PIC24EP problem, double rmw to io register fails
« Reply #19 on: February 27, 2018, 03:29:01 pm »
I am sorry you may have missed it because I dont dump irrelevent information here, if it were wrong (that it isn't) then how would one peice of code work and the other not ?
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 21658
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: PIC24EP problem, double rmw to io register fails
« Reply #20 on: February 27, 2018, 03:56:03 pm »
Yes tim I like the STM32 also but I dont use it because I am still building cpu's without pcb's in a small space, this does admitedly cause me problems with restricted numbers of io pins. For the present project a gti I can just about manage with this part (PIC24EP256MC202) but it's a close thing! I do pcb's sometimes for example audio dsp's where dip simply isnt available but I like to keep my costs down. Also when handling high power devices it's essential IMOP to have absolute control and do things fast hence assembly preferance. I have/do write tons of C for BIG processors but I dont consider a PIC in that way, its just an itsy bitsy machine control engine :)

Speaking of C, the aforementioned project is all in C, just to fill that in..

I imagine, between libraries and early consternations, they solved low level register problems long ago.  Though I think there were still a few hangups here and there?  So, eh.. if you want to re-invent the wheel, you're welcome to, and you'll re-discover along the way many of the catches that happen, like the above.

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Online JPortici

  • Super Contributor
  • ***
  • Posts: 3461
  • Country: it
Re: PIC24EP problem, double rmw to io register fails
« Reply #21 on: February 27, 2018, 04:37:45 pm »
Yeah we know, if you don't have the very latest top-of-the line hardware it's your fault if the bloatware runs slow.  ::)

I referenced that thread because apparently this was some "irrelevant information" when complaining about MPLABX.
It was not. My 2010 macbook with an SSD was fast enough, sure it took like 30 seconds to finish loading MPLABX compared to 5 in my current PC. And my current PC is a 7th gen NUC, nothing really fancy.

Quote
Quote
I also want to know the part and the code so i can crosscheck, the HSPWM is a relatively newer and complex peripheral and there are caveats in how and when to write to certain register.
Or it's just buggy, but once again that's your fault for not having the latest chip revision (or if a bug-free version hasn't been released yet, expecting it to work...).

perhaps, but if the documentation states that you can't do successive RMW instructions in certain register, it's not a bug.

Quote
They tell me that any PIC with less than 5 digits is deprecated and I'm a criminal for running Windows XP, but I don't care. MPlab8 and PicKit2 may not do the latest PICs, but they work better and I don't need the latest PICs for my projects. My hobby is retro computing - and now my MCUs and development tools are too!

not at all :) you are free to do what you want and i'm free to always suggest moving to modern/better/cheaper parts for work. I also take different choices in my hobby projects :)

Quote
Quote from: fourtytwo42
Microchip are long past the point where they provide such detailed information to there users
which is a pity because that was their strong suit, making up their mediocre performance compared to other brands.
[/quote]

or maybe the information is there somewhere you haven't looked. which is why asking for the part number is not asking to provide "irrelevant information".
documentation quality has gone down the drain compared to like 5 years ago, but i still think it's one of the best against say ST or ATMEL, or even NXP(Kinetis)

sorry for you, but i'm not trolling. i'm really trying to help.
 

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 825
Re: PIC24EP problem, double rmw to io register fails
« Reply #22 on: February 27, 2018, 08:12:36 pm »
Quote
I am sorry you may have missed it because I dont dump irrelevent information here, if it were wrong (that it isn't) then how would one peice of code work and the other not ?
Its relevant for this reason- IF you have PWMLOCK on, and you are not showing the 'irrelevant' code previous which does the unlock, then it could explain a problem-

your code (this is all we get to see)-
   mov      #iocon2init,w0
   mov      w0,IOCON2
;   bset   IOCON2,#OVRDAT1
;   bset   IOCON2,#OVRDAT0

maybe you are doing the unlock before this (we don't know), which could explain why the mov works and the bset does not.

According to the datasheet, the PWMUNLOCK sequence requires that the first sfr access following has to be a WRITE, so if using bset a read is thrown in there first which may prevent the write taking place since the unlock just failed. The mov would work as a WRITE is the first sfr access after the unlock code.

This may not be the case, but it certainly is a possibility. If you have PWMLOCK off, then never mind (but again, how would we know).
 

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5317
  • Country: gb
Re: PIC24EP problem, double rmw to io register fails
« Reply #23 on: February 27, 2018, 08:54:30 pm »
Quote
I am sorry you may have missed it because I dont dump irrelevent information here, if it were wrong (that it isn't) then how would one peice of code work and the other not ?
Its relevant for this reason- IF you have PWMLOCK on, and you are not showing the 'irrelevant' code previous which does the unlock, then it could explain a problem-

your code (this is all we get to see)-
   mov      #iocon2init,w0
   mov      w0,IOCON2
;   bset   IOCON2,#OVRDAT1
;   bset   IOCON2,#OVRDAT0

maybe you are doing the unlock before this (we don't know), which could explain why the mov works and the bset does not.

According to the datasheet, the PWMUNLOCK sequence requires that the first sfr access following has to be a WRITE, so if using bset a read is thrown in there first which may prevent the write taking place since the unlock just failed. The mov would work as a WRITE is the first sfr access after the unlock code.

This may not be the case, but it certainly is a possibility. If you have PWMLOCK off, then never mind (but again, how would we know).

Precisely.

I'm now not sure I should've spent the hour or so I've burned trying to reproduce the OPs problem in real hardware. At the moment, I am unable to reproduce it, unless the PWMLOCK config bit is set. Therefore, I don't see it as a bug, it is by design, unless I can be shown the precise context, including any unlock sequences and the PWMLOCK config bit.
 

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 627
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: PIC24EP problem, double rmw to io register fails
« Reply #24 on: February 28, 2018, 12:50:07 am »
Quote from: JPortici
I referenced that thread because apparently this was some "irrelevant information" when complaining about MPLABX.
Fair enough, but the 'irrelevant' reference to MPLabx was a bit obtuse so next time you might try being more direct (and BTW you shouldn't need an SSD to get reasonable performance).

I agree that the OP should provide sufficient code to reproduce the problem. That means including all the config bits and initialization code, preferably as part of a Minimal, Complete, and Verifiable Example that still fails with all (truly) irrelevant bits stripped out.

Thank you for your reply's first of all some code
   mov      #iocon2init,w0
   mov      w0,IOCON2
;   bset   IOCON2,#OVRDAT1
;   bset   IOCON2,#OVRDAT0
The commented out stuff doesnt work, the single mov works :)
You said "if you use bset twice to the same io register (different bits) sometimes it fails and not always the second". So does the code above fail sometimes, or always? Can you show an example of similar code that doesn't fail?
 





 

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5317
  • Country: gb
Re: PIC24EP problem, double rmw to io register fails
« Reply #25 on: February 28, 2018, 08:45:09 am »
FWIW the following code segment reproduces the alleged problem if you have the PMWLOCK config bit set. If the PMWLOCK config bit is OFF, the OVRDAT bits do set. But this behaviour to me is as designed and documented.

Code: [Select]
        asm("mov #0xabcd,w10 ; Load first unlock key to w10 register");
        asm("mov #0x4321,w11 ; Load second unlock key to w11 register");
        asm("mov #0xF000,w0 ; Load desired value of IOCON2 register in w0");
        asm("mov w10, PWMKEY ; Write first unlock key to PWMKEY register");
        asm("mov w11, PWMKEY ; Write second unlock key to PWMKEY register");
        asm("mov w0,IOCON2 ; Write desired value to IOCON2 register");
        asm("bset   IOCON2,#7 ; #OVRDAT1");
        asm("bset   IOCON2,#6 ; #OVRDAT0");
 

Online JPortici

  • Super Contributor
  • ***
  • Posts: 3461
  • Country: it
Re: PIC24EP problem, double rmw to io register fails
« Reply #26 on: February 28, 2018, 10:12:38 am »
Thank you for your reply's first of all some code
   mov      #iocon2init,w0
   mov      w0,IOCON2
;   bset   IOCON2,#OVRDAT1
;   bset   IOCON2,#OVRDAT0
The commented out stuff doesnt work, the single mov works :)

And that's what is supposed to happen! Current datasheet for PIC24EP with HSPWM: http://ww1.microchip.com/downloads/en/DeviceDoc/70000657H.pdf

paragraph 16.1.2 Write Protected registers

Quote
On dsPIC33EPXXXMC20X/50X and PIC24EPXXXMC20X devices, write protection is implemented for the IOCONx and FCLCONx registers.
The write protection feature prevents any inadvertent writes to these registers.
This protection feature can be controlled by the PWMLOCK Configuration bit (FOSCSEL<6>). The default state of the write protection feature is enabled (PWMLOCK = 1).
The write protection feature can be disabled by configuring, PWMLOCK = 0.

To gain write access to these locked registers, the user application must write two consecutive values of (0xABCD and 0x4321) to the PWMKEY register to perform the unlock operation.
The write access to the IOCONx or FCLCONx registers must be the next SFR access following the unlock process. There can be no other SFR accesses during the unlock process and subsequent write access.
To write to both the IOCONx and FCLCONx registers requires two unlock operations.

it was written there the whole time. you can perform only one operation after the unlock sequence, so it was not a bug. It's implemented this way and the code example immediately below uses a mask register

Code: [Select]
; FLT32 pin must be pulled low externally in order to clear and disable the fault
; Writing to FCLCON1 register requires unlock sequence
mov #0xabcd,w10 ; Load first unlock key to w10 register
mov #0x4321,w11 ; Load second unlock key to w11 register
mov #0x0000,w0 ; Load desired value of FCLCON1 register in w0
mov w10, PWMKEY ; Write first unlock key to PWMKEY register
mov w11, PWMKEY ; Write second unlock key to PWMKEY register
mov w0,FCLCON1 ; Write desired value to FCLCON1 register

; Set PWM ownership and polarity using the IOCON1 register
; Writing to IOCON1 register requires unlock sequence
mov #0xabcd,w10 ; Load first unlock key to w10 register
mov #0x4321,w11 ; Load second unlock key to w11 register
mov #0xF000,w0 ; Load desired value of IOCON1 register in w0
mov w10, PWMKEY ; Write first unlock key to PWMKEY register
mov w11, PWMKEY ; Write second unlock key to PWMKEY register
mov w0,IOCON1 ; Write desired value to IOCON1 register

other peripherals have the same unlock mechanism: For example NVM erase/write operation and clock switching. in each case if you want to program in C and be sure that you perform the correct operations there are builtin functions, documentation at the end of the XC16 compiler manual.
Hope this helped.
« Last Edit: February 28, 2018, 10:17:08 am by JPortici »
 

Online JPortici

  • Super Contributor
  • ***
  • Posts: 3461
  • Country: it
Re: PIC24EP problem, double rmw to io register fails
« Reply #27 on: February 28, 2018, 10:30:34 am »
Side note, a reason to state why partnumber is NEVER irrelevant:
Reference manual tells what the complete peripheral is and how it behaves. The datasheet tells what is the actual implementation that may have differences from the ref manual.

For example the ADC2 in the dsPIC33EP MU series is slightly different than ADC1 and the differences are only in the datasheet.

Or there are different versions of the same peripheral, like 3 kinds of ADC (the 10 bit only, the 10/12 bit and multi-core the 12 bit used in the GS series which i think is pulled from the PIC32 MZ) and 2 different I2C, one is simillar to (or the same as) PIC32 MX.
Which one to use? Check different manuals? Better check the datasheet to see which one it uses first.

And different chips in the same family can have different errata documents.

(For example pic18f24/25K42 has or used to have different errata documents than 26/27K42)
« Last Edit: February 28, 2018, 10:34:22 am by JPortici »
 

Offline fourtytwo42Topic starter

  • Super Contributor
  • ***
  • Posts: 1184
  • Country: gb
  • Interested in all things green/ECO NOT political
Re: PIC24EP problem, double rmw to io register fails
« Reply #28 on: February 28, 2018, 10:58:03 am »
This really is tiresome, another code snippet

config  __FOSCSEL,FNOSC_FRCPLL & PWMLOCK_OFF

If you READ the post you will find I state that it fails perodically, this is NOT a STATIC problem.

Now perhaps you will leave this thread alone and stop obscuring it with vast quanteties of information that is not applicable to this problem. In any case I don't require any help, the post was for information only.
 

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5317
  • Country: gb
Re: PIC24EP problem, double rmw to io register fails
« Reply #29 on: February 28, 2018, 02:51:30 pm »
Give us a full, concise reproducible example and we'll be happy to help. I have been unable to reproduce the problem on real hardware.
 

Offline fourtytwo42Topic starter

  • Super Contributor
  • ***
  • Posts: 1184
  • Country: gb
  • Interested in all things green/ECO NOT political
Re: PIC24EP problem, double rmw to io register fails
« Reply #30 on: February 28, 2018, 05:49:51 pm »
Of course it might be rev/id specific I should have mentioned mine are 4003 that is mask A3.
I am sorry I am not prepared to disclose my code on any public forum but I can say the circumstances are periodic timer driven setting and resetting of these bits. The failure is not continious and is typically corrected by a subsiquent rmw some time later. The failure can only realistically be observed using a scope on the output pins.
 

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5317
  • Country: gb
Re: PIC24EP problem, double rmw to io register fails
« Reply #31 on: February 28, 2018, 09:21:05 pm »
You don’t have to disclose your entire application.

Just a compact example of a reproducible scenario. That is -the- standard way to analyse and disclose bugs.

Without that it’ll remain nothing more than internet heresay.
 

Offline fourtytwo42Topic starter

  • Super Contributor
  • ***
  • Posts: 1184
  • Country: gb
  • Interested in all things green/ECO NOT political
Re: PIC24EP problem, double rmw to io register fails
« Reply #32 on: March 13, 2018, 08:25:33 am »
After no more problems for several weeks another one has arisen again involving register writes of the HSPWM module but this time a simple mov instruction! The code snippet is
   mov      #66,w0   
   mov      w0,PHASE3
And the watch window after a breakpoint just afterwards
PHASE3   0x1200   4608   00010010 00000000
If however I single step (F7) the same code it works  |O

So far I have changed the chip, same result, they are both mask A3, nothing apparent in errata
I will see if enhancing the decoupling makes any differance, added extra 100nF every pwr pin pair, no change.
In previous versions of code this particular code worked fine, makes me very worried indeed  :-//
BTW this is during initialisation so interrupts are disabled.

I should mention this is my first use of PIC24EP I have never experienced anything like this with 18F's etc, my tools are PK3 & MPLAB 8.92
« Last Edit: March 13, 2018, 09:23:12 am by fourtytwo42 »
 

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5317
  • Country: gb
Re: PIC24EP problem, double rmw to io register fails
« Reply #33 on: March 13, 2018, 09:35:11 am »
Give us a complete, concise, reproducible example of this happening.
 

Offline fourtytwo42Topic starter

  • Super Contributor
  • ***
  • Posts: 1184
  • Country: gb
  • Interested in all things green/ECO NOT political
Re: PIC24EP problem, double rmw to io register fails
« Reply #34 on: March 13, 2018, 09:47:31 am »
I knew you would say that and your right, I am going to have to make a new progect just to try and isolate these problems and I have a funny feeling when I do they will evaporate, in other words it's some nasty combinitorial problem potentially code location sensative :( BUT I either have to find out what's wrong or abandon this chip as I cannot have random register uncertainty everytime I rebuild the code for some reason :(
 

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5317
  • Country: gb
Re: PIC24EP problem, double rmw to io register fails
« Reply #35 on: March 13, 2018, 10:29:18 am »
I either have to find out what's wrong or abandon this chip as I cannot have random register uncertainty everytime I rebuild the code for some reason :(

Which is precisely why you need a concise reproducible example. I’m not saying it’s easy, especially as those newer PWM peripherals are bitches to configure.
 

Offline fourtytwo42Topic starter

  • Super Contributor
  • ***
  • Posts: 1184
  • Country: gb
  • Interested in all things green/ECO NOT political
Re: PIC24EP problem, double rmw to io register fails
« Reply #36 on: March 13, 2018, 12:09:23 pm »
OMG I find this hard to beleive BUT it seems somebody at Microchip thought it would be a good idea to ENABLE INTERRUPTS BY DEFAULT >:D Thats a completely new one on me and I cannot think of a processor I have used in 40ish years where the designer thought, I know let's do something weird and enable interrupts by default, wowww good thinking guys thank you soooooooooooooooooooooooo much :palm:
 

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5317
  • Country: gb
Re: PIC24EP problem, double rmw to io register fails
« Reply #37 on: March 13, 2018, 01:49:28 pm »
The interrupt sources aren’t enabled by default, you have to explicitly do that yourself.

The PiC24 and dsPIC families use the DISI instruction to disable global interrupts for a given finite number of cycles. If you want to permanently disable interrupts, you’ll need to run at IPL 7 by setting the IPL bits in the SR register. That’s not something I’m recommending for productiion code, but if you’re trying to figure something out it might make sense temporarily.
 

Offline fourtytwo42Topic starter

  • Super Contributor
  • ***
  • Posts: 1184
  • Country: gb
  • Interested in all things green/ECO NOT political
Re: PIC24EP problem, double rmw to io register fails
« Reply #38 on: March 13, 2018, 03:47:44 pm »
I do apologise I should have been clear, I meant the GIE bit in INTCON2.
To add to my woes I have now discovered the PicKit3/MPASM suite can adversly affect internal operation if in debug mode, even with no breakpoints set certain register writes fail possibly because they are in a watch list though I have not confirmed that yet (but I thought those were only updated when the cpu halted). This is certainly proving to be an unstable platform compared to what I am used but that may also be because it's the first time I have used it.
EDIT
After an hour or so of trying different combinations of with/without PK3, program/debug mode, reset or not, pwr cycle or not, guess what, the write problem on that particular register went away!! perhaps it's now cropped up somewhere else I have yet to discover..........
« Last Edit: March 13, 2018, 06:58:36 pm by fourtytwo42 »
 

Offline bson

  • Supporter
  • ****
  • Posts: 2269
  • Country: us
Re: PIC24EP problem, double rmw to io register fails
« Reply #39 on: March 16, 2018, 12:40:28 am »
If you READ the post you will find I state that it fails perodically, this is NOT a STATIC problem.
Do you have interrupts disabled?  An interrupt at the wrong time would "steal" your unlock cycle.

Edit: never mind
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf