Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Not exiting Powersave reliably

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
sentinel

Bascom Member



Joined: 02 Feb 2009
Posts: 89
Location: Tasmania

australia.gif
PostPosted: Fri Oct 01, 2021 3:53 am    Post subject: Not exiting Powersave reliably Reply with quote

When I run this (stripped-down) code, the firmware often does not exit Powersave regardless of the state of the power supply.
Pin C.5 (alias Power_on) is high when the circuit is powered and triggers entry into Powersave mode when the power drops out.
The problem is that it sometimes sticks in the power down state, and of course, the watchdog is never triggered.
Grateful for any suggestions Very Happy

Code:

$regfile = "m168def.dat"
'Oscillator Is 8 Mhz not divided By 8 Internally.
$crystal = 8000000
Baud = 9600
$baud = 9600
$hwstack = 60
$swstack = 60
$framesize = 60

Dim Date_saved as Eram string * 8
Dim Time_saved as Eram string * 8

Dim Timerflag As Bit
Dim Uart As Bit
Dim Messageflag as Byte

Config Serialin = Buffered , Size = 40 , Bytematch = 10
Config Serialout = Buffered , Size = 120
Config Clock = Soft , Gosub = Sectic
Config Date = Dmy , Separator = /

Ddrb = &B110111                                             'Configure inputs and outputs
Ddrc = &B0011110                                             'Configure inputs and outputs
Ddrd = &B11111100                                           'Configure inputs and outputs

Power_on Alias Pinc.5

Const On = 1
Const Off = 0

Portc.0 = 1
Portb.3 = 1

Config Adc = Single , Prescaler = Auto , Reference = INTERNAL_1.1

Config Watchdog = 8192                                'Set the watchdog to time out at 8 seconds
On Wdt Wdt_isr Nosave
Enable Wdt
Start Watchdog
Enable Interrupts

'===========================================================================
'Main program
'===========================================================================
Do
   If Power_on = 1 Then
      If Uart = Off Then
         Ucsr0b.txen0 = 1
         Ucsr0b.rxen0 = 1
         Uart = On
         Start Adc
         Start Watchdog
      End If

         'Main loop tasks here

   Else
      If Uart = On Then
         Ucsr0b.txen0 = 0                                'Disable USART0 transmit
         Ucsr0b.rxen0 = 0                                'Disable USART0 receive
         Uart = Off
         Stop Adc
         Stop Watchdog
      End If
      Powersave
   End If
   Reset Watchdog
Loop

'===========================================================================
'Timer interrupt subroutine
'===========================================================================
Sectic:
   Timerflag = 1
   OCR2B = 0                                                   'Wait for the write cycle of Timer2.
   Do
   Loop Until ASSR.OCR2BUB = 0
Return

Wdt_isr:
   Date_saved = Date$ : Time_saved = Time$
   !jmp $0000
Return

'=========================================
' Serial input interrupt routine
' ========================================
Serial0charmatch:
   Incr Messageflag
Return

End
 


(BASCOM-AVR version : 2.0.8.2 , Latest : 2.0.8.4 )


Last edited by sentinel on Sat Oct 02, 2021 2:04 am; edited 1 time in total
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 757

poland.gif
PostPosted: Fri Oct 01, 2021 7:42 pm    Post subject: Reply with quote

Do you understand what Nosave argument means?
Or in another way - how you think it work?
Back to top
View user's profile Visit poster's website
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 757

poland.gif
PostPosted: Fri Oct 01, 2021 7:51 pm    Post subject: Reply with quote

Nosave argument means that you know what registers will be used and they should be saved on the stack and after your operation is done they should be restored by your code. When you use Nosave argument you should save and restore registers that was handy during ISR yourself because you tell to the Bascom - I care of them myself.
So..
Back to top
View user's profile Visit poster's website
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1548
Location: Basel, Switzerland

switzerland.gif
PostPosted: Fri Oct 01, 2021 8:23 pm    Post subject: Reply with quote

Hi,

I can't see where your actually enabling interrupts with "Enable Interrupts"

Regards
Ian Dobson

_________________
Home of AVRTimer, the Bascom timer value calculator.
http://www.planet-ian.com

Walking on water and writing software to specification is easy if they're frozen.
Back to top
View user's profile Visit poster's website
sentinel

Bascom Member



Joined: 02 Feb 2009
Posts: 89
Location: Tasmania

australia.gif
PostPosted: Sat Oct 02, 2021 12:16 am    Post subject: Reply with quote

EDC wrote:
Do you understand what Nosave argument means?
Or in another way - how you think it work?


As you suspect, not well enough to be able to justify why I included it in my code. It was lifted wholesale from someone else's example.
I'll get rid of the Nosave and try again.

Thanks for the input.
Back to top
View user's profile
sentinel

Bascom Member



Joined: 02 Feb 2009
Posts: 89
Location: Tasmania

australia.gif
PostPosted: Sat Oct 02, 2021 12:19 am    Post subject: Reply with quote

i.dobson wrote:
Hi,

I can't see where your actually enabling interrupts with "Enable Interrupts"

Regards
Ian Dobson


Hi Ian, yes, you're right, I "stripped down" the code too much and deleted it from the original code in error.

"Enable Interrupts" is in the original problematic code.

I've added it to the sample code above.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1985

blank.gif
PostPosted: Sat Oct 02, 2021 8:54 am    Post subject: Reply with quote

EDC wrote:
When you use Nosave argument you should save and restore registers that was handy during ISR yourself because you tell to the Bascom - I care of them myself.
So..

So what?
You was drifting off target and discussing non-issues, which you could have avoided if you had checked what the NOSAVE-ISR actually does: Calls EEPROM-functions and then restarts.
What registers you want toi save for a restart?
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1985

blank.gif
PostPosted: Sat Oct 02, 2021 10:26 am    Post subject: Re: Not exiting Powersave reliably Reply with quote

sentinel wrote:
The problem is that it sometimes sticks in the power down state, and of course, the watchdog is never triggered.

If UART is on and power is off this sequence executes:
Code:
Stop Adc
Stop Watchdog
Powersave

After in your code, only async timer2 can pull the controller from sleep by executing timer2 ISR, which again calls the SECTIC stub.
In this matter I wonder about the purpose of this code construct, as more as the comment does not match exactly what it does.
Code:
Sectic:
   Timerflag = 1
   OCR2B = 0                                                   'Wait for the write cycle of Timer2.
   Do
   Loop Until ASSR.OCR2BUB = 0
Return

It does not exactly wait till a write cycle is enabled, in contrary it shoots first and asks later.
Also I wonder about the (non-)sense of triggering a wait for write enable, as more OCR2B seems not be used elsewhere.
Would you explain?
Back to top
View user's profile
sentinel

Bascom Member



Joined: 02 Feb 2009
Posts: 89
Location: Tasmania

australia.gif
PostPosted: Mon Oct 04, 2021 12:35 am    Post subject: Reply with quote

I would explain, except I can't.

This is fossil code from another project that I left in through laziness. As you say, there is no apparent need for it in this application, so out it goes. Rigorous testing will ensue.

Thank you for casting your critical eye over this effort.
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum