• AVR Freaks

Helpful ReplyHot!Reading configuration register or bit values programmatically

Author
bodypilllow
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2020/09/16 19:59:19
  • Location: 0
  • Status: offline
2021/02/16 16:18:52 (permalink)
3 (1)

Reading configuration register or bit values programmatically

Hi All,
 
I am writing code to perform a clock switch from the boot clock source to another clock source and I've found a runtime decision that would ideally be made based on the value of a configuration bit:
 
"The clock switch for a given core will not wait for the PLL lock if the (S1)PLLKEN
bit in the Master or Slave Oscillator Configuration register (FOSC<8> and
FS1OSC<8>, respectively) is set to ‘0’." (DS70005255B-page 65, Note 1, #5)



In other words, if the PLLKEN config bit is set, I don't need to poll to make sure the PLL lock is achieved before I move on, but if it is not set, I would need to.  Is there a way I can read this register / bit field in code?
#1
GlennP
Super Member
  • Total Posts : 869
  • Reward points : 0
  • Joined: 2009/03/29 15:04:55
  • Location: El Paso County, CO, USA
  • Status: offline
Re: Reading configuration register or bit values programmatically 2021/02/16 16:30:02 (permalink) ☄ Helpfulby bodypilllow 2021/02/16 16:36:55
5 (1)
What processor?  The manual referenced is a "Family" manual and doesn't tell us what chip you're using.
 
The short answer is you can read the configuration memory but the specifics of the addressing depend on the MCU.
 
You will have to do a read of the code space.
#2
bodypilllow
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2020/09/16 19:59:19
  • Location: 0
  • Status: offline
Re: Reading configuration register or bit values programmatically 2021/02/16 16:34:37 (permalink)
0
Sorry I forgot to add: dsPIC33CK256MP503.
So will I need to make use of the Program Space Visibility feature?  It looks like configuration registers are located in program memory.  I know the address of the register I am trying to read from the device family datasheet (dsPIC33CK256MP508 family).
 
FOSC register for single partition 256k memory: address 0x02BF1C
post edited by bodypilllow - 2021/02/16 16:36:29
#3
GlennP
Super Member
  • Total Posts : 869
  • Reward points : 0
  • Joined: 2009/03/29 15:04:55
  • Location: El Paso County, CO, USA
  • Status: offline
Re: Reading configuration register or bit values programmatically 2021/02/16 16:42:03 (permalink)
4.5 (2)
You can use PSV OR you can use Table Reads.  See section 4.8 of DS70000657J.pdf.  I've only done the latter in assembler, perhaps someone could describe how it's done in C.
 
PS. Is it 503 or 508?
 
Edit 1: Added Manual Reference in Italics.
post edited by GlennP - 2021/02/16 16:47:47
#4
bodypilllow
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2020/09/16 19:59:19
  • Location: 0
  • Status: offline
Re: Reading configuration register or bit values programmatically 2021/02/16 17:07:26 (permalink)
0
OK thank you.  Maybe I can just do it with inline assembly if need be.  It looks like there is also a builtin function for a tableread so that might be easiest.
 
The part I am using is the ..503, but the datasheet for the chip itself is just the ...508 "family" datasheet as far as I know
#5
GlennP
Super Member
  • Total Posts : 869
  • Reward points : 0
  • Joined: 2009/03/29 15:04:55
  • Location: El Paso County, CO, USA
  • Status: offline
Re: Reading configuration register or bit values programmatically 2021/02/16 18:03:03 (permalink)
5 (2)
bodypilllow...
It looks like there is also a builtin function for a tableread so that might be easiest. ...



Personally, if there is a built-in I'd use it.  Especially since performance should not be an issue if you're only doing this on start-up.  Just make sure the built-in can address configuration space.  It should, but I've been bushwhacked enough to be cautious.
#6
bodypillow14
New Member
  • Total Posts : 1
  • Reward points : 0
  • Joined: 2020/09/29 11:33:37
  • Location: 0
  • Status: offline
Re: Reading configuration register or bit values programmatically 2021/02/16 23:05:34 (permalink)
0
I will give it a try and post some results as soon as I can.
#7
NorthGuy
Super Member
  • Total Posts : 6587
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: Reading configuration register or bit values programmatically 2021/02/17 07:45:31 (permalink) ☄ Helpfulby bodypilllow 2021/03/03 22:22:45
0
You can wait for the lock regardless. It won't hurt.
#8
bodypilllow
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2020/09/16 19:59:19
  • Location: 0
  • Status: offline
Re: Reading configuration register or bit values programmatically 2021/02/17 13:55:18 (permalink)
0
NorthGuy
You can wait for the lock regardless. It won't hurt.


That's true.  I guess at the end of the day I may not even save cycles by skipping it given that I have to check first if I'm allowed to.
#9
Aussie Susan
Super Member
  • Total Posts : 3857
  • Reward points : 0
  • Joined: 2008/08/18 22:20:40
  • Location: Melbourne, Australia
  • Status: offline
Re: Reading configuration register or bit values programmatically 2021/02/17 18:34:27 (permalink) ☄ Helpfulby bodypilllow 2021/03/03 22:22:40
5 (1)
I'm getting the feeling that this is 'premature optimisation'.
Typically you set up the oscillator right at the start and then just let it go. Does saving a few cycles (especially when the oscillator may or may not be stable) right at the very start really make that much of a difference?
I'm guessing that, if it does, they you probably have a lot more to worry about getting the MCU started than this.
Susan
#10
bodypilllow
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2020/09/16 19:59:19
  • Location: 0
  • Status: offline
Re: Reading configuration register or bit values programmatically 2021/03/03 22:21:51 (permalink)
0
Aussie Susan
I'm getting the feeling that this is 'premature optimisation'.
Typically you set up the oscillator right at the start and then just let it go. Does saving a few cycles (especially when the oscillator may or may not be stable) right at the very start really make that much of a difference?
I'm guessing that, if it does, they you probably have a lot more to worry about getting the MCU started than this.
Susan


Thanks for your note.  You may very well be right about the first part.  The issue is that at our company, as soon as something gets released and shipped, there is never an opportunity to improve it as we'll get jerked directly to another major development.  So the only time to optimize is basically prematurely, unfortunately.  It is a crappy and broken process but there is not a lot I can do about it.
 
The background reason I am looking at this sequence somewhat closely is that we've had instances where an external oscillator has failed, and the typical Microchip example "while(oscillator != ready);" blocking code has caused non-boots in installation.  I'm not a fan of that and am looking to improve the sequence to make it more robust (i.e. wait for a fixed time for the external oscillator to figure itself out, fallback to backup oscillator, and issue diagnostic messages accordingly), and the PLL lock wait state was another piece I wanted to flesh out while I am inside here.
#11
JPortici
Super Member
  • Total Posts : 1327
  • Reward points : 0
  • Joined: 2012/11/17 06:27:45
  • Location: Grappaland
  • Status: offline
Re: Reading configuration register or bit values programmatically 2021/03/03 23:23:43 (permalink)
4 (1)
bodypilllow
The background reason I am looking at this sequence somewhat closely is that we've had instances where an external oscillator has failed, and the typical Microchip example "while(oscillator != ready);" blocking code has caused non-boots in installation.  I'm not a fan of that and am looking to improve the sequence to make it more robust (i.e. wait for a fixed time for the external oscillator to figure itself out, fallback to backup oscillator, and issue diagnostic messages accordingly), and the PLL lock wait state was another piece I wanted to flesh out while I am inside here.



shouldn't the FSCM kick in and raise an oscillator fail trap in that case?
#12
bodypilllow
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2020/09/16 19:59:19
  • Location: 0
  • Status: offline
Re: Reading configuration register or bit values programmatically 2021/03/04 00:29:55 (permalink)
0
Jack_M
bodypilllow
The background reason I am looking at this sequence somewhat closely is that we've had instances where an external oscillator has failed, and the typical Microchip example "while(oscillator != ready);" blocking code has caused non-boots in installation.  I'm not a fan of that and am looking to improve the sequence to make it more robust (i.e. wait for a fixed time for the external oscillator to figure itself out, fallback to backup oscillator, and issue diagnostic messages accordingly), and the PLL lock wait state was another piece I wanted to flesh out while I am inside here.



shouldn't the FSCM kick in and raise an oscillator fail trap in that case?


If the mess I am cleaning up had it enabled, then yes wink: wink
In all seriousness I haven't tested this feature yet, but I'm sure that you're right that it would make the blocking code unproblematic.  My initial stab was to replace the blocking while with a iteration limited loop with a failure / success branch following.
 
Thanks for your input
#13
JPortici
Super Member
  • Total Posts : 1327
  • Reward points : 0
  • Joined: 2012/11/17 06:27:45
  • Location: Grappaland
  • Status: offline
Re: Reading configuration register or bit values programmatically 2021/03/04 01:23:42 (permalink)
4 (1)
IME i've had success with FSCM. In some projects i deliberately use the clock failure to know when the external clock is powered off so i can either go to failure mode (when not expected) or proceed with sleep (when epxected)
#14
bodypilllow
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2020/09/16 19:59:19
  • Location: 0
  • Status: offline
Re: Reading configuration register or bit values programmatically 2021/03/04 01:26:13 (permalink)
0
Jack_M
IME i've had success with FSCM. In some projects i deliberately use the clock failure to know when the external clock is powered off so i can either go to failure mode (when not expected) or proceed with sleep (when epxected)


That is good to know, thank you.  It looks like it is an old feature so I suppose I should expect it will work as advertised.  Whereas for some of the new dsPIC33C bits and pieces, some more caution seems warranted pink: pink
#15
JPortici
Super Member
  • Total Posts : 1327
  • Reward points : 0
  • Joined: 2012/11/17 06:27:45
  • Location: Grappaland
  • Status: offline
Re: Reading configuration register or bit values programmatically 2021/03/04 01:34:18 (permalink) ☄ Helpfulby bodypilllow 2021/03/04 10:29:47
0
Oh yes, many new things, some good and some bad.
 
More convoluted ideas: run one of the timers form BFRC (if you can, i don't remember off hand if you can) or LPRC or another clock independent from FRC and use that interrupt to clear the WDT (if conditions are met) or decrement counters or measure time (roughly, because accuracy) so you can more or less count time without hard coded delays during the switch and check if it's taking too long.
 
But i'd just use the FSCM first, as i'd be suggesting to do in software what the FSCM does in hardware
#16
Jump to:
© 2021 APG vNext Commercial Version 4.5