View previous topic :: View next topic |
Author |
Message |
O-Family
Joined: 23 May 2010 Posts: 320 Location: Japan
|
Posted: Fri Aug 19, 2016 12:47 pm Post subject: |
|
|
Hi EDC
Yes, the code does not work.
As I have pointed out the mistake of BASCOM, because Watchdog is initialized after the $initmicro instruction.
So the ASM code has no effect.
Let's wait for the correction of the MCS-Mark. |
|
Back to top |
|
|
laborratte
Joined: 27 Jul 2005 Posts: 299 Location: Berlin
|
Posted: Fri Aug 19, 2016 2:50 pm Post subject: |
|
|
This is not a bug.
$initmicro only adds a call to a subroutine _init_micro at the earliest possible stage (after setting up the stacks) to allow port configuration (i.e. setting low and high states where needed in hardware). After returning from _init_micro all normal init is done: saving and clearing mcusr, setting watchdog register and clearing memory.
So any watchdog settings in _init_micro are overwritten.
According to the datasheet the initial setting of watchdog register in mandatory, even the watchdog isn't used at all. That's why BASCOM is doing that.
If you are setting the watchdog fuse and you are angry about the time needed for clearing the memory, use the $noramclear option. So you can setup your watchdogtime and, when needed, clear afterwards the memory by yourself. |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Fri Aug 19, 2016 3:02 pm Post subject: |
|
|
As I said if BOOTRST is disabled this code work with m2560 .
Led flashes.
Code: | $regfile = "m2560def.dat"
$crystal = 16000000
$hwstack = 64
$swstack = 32
$framesize = 128
Config Watchdog = 1024
Config Portb.7 = Output : Led Alias Portb.7 'Arduino Mega LED
Do
Toggle Led
Wait 1
Waitms 50 'value 60 causes Reset
Reset Watchdog
Loop
End |
|
|
Back to top |
|
|
laborratte
Joined: 27 Jul 2005 Posts: 299 Location: Berlin
|
Posted: Fri Aug 19, 2016 3:20 pm Post subject: |
|
|
BOOTRST has nothing to do with watchdog. It points programm counter to the bootloader section after reset, when programmed. |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Fri Aug 19, 2016 3:27 pm Post subject: |
|
|
I mean Arduino boards. If one use them for own project then bootloader also should take care to set WD time if WDTON is set.
If Arduino bootloader is cleared by flashing micro via ISP then BOOTRST should be disabled I think
..and I test this..are you too?
If BOOTRST is set then led is lit, no flash |
|
Back to top |
|
|
O-Family
Joined: 23 May 2010 Posts: 320 Location: Japan
|
Posted: Fri Aug 19, 2016 3:34 pm Post subject: |
|
|
Hi laborratte
When the reset is performed, after setting the stack, user code of $initmicro is called.
However, followed by the initialization WDTCSR register of Watchdog.
If the AVR clock is low, this causes a problem.
BASCOM is after the initialization of WDTCSR register, it should call the $initmicro routine.
$noramclear in first aid, not a solution.
This is the assembler code output of BASCOM.
Code: |
00072 : EF8F RESET: LDI R24,$FF
00073 : BF8D OUT SPL,R24
00074 : EEC0 LDI YL,$E0
00075 : EEE8 LDI ZL,$E8
00076 : 2E4E MOV R4,ZL
00077 : E281 LDI R24,$21
00078 : BF8E OUT SPH,R24
00079 : E2D1 LDI YH,$21
0007A : E2F1 LDI ZH,$21
0007B : 2E5F MOV R5,ZH
0007C : 940E 00AA CALL L000AA ;$initmicro
0007E : 95A8 WDR ;Watchdog of Reset
0007F : B784 IN R24,MCUSR
00080 : 2E08 MOV R0,R24
00081 : 7F87 ANDI R24,$F7
00082 : BF84 OUT MCUSR,R24
00083 : E188 LDI R24,$18
00084 : 2799 CLR R25
00085 : 9380 0060 STS WDTCSR,R24
00087 : 9390 0060 STS WDTCSR,R25 ;### Watchdog => 16ms ###
00089 : EFEE LDI ZL,LOW(S1FFE)
0008A : E1FF LDI ZH,HIGH(S1FFE)
0008B : E0A0 LDI XL,LOW(S0200)
0008C : E0B2 LDI XH,HIGH(S0200)
0008D : 2788 CLR R24 ;All clear of SRAM
0008E : 938D L0008E: ST X+,R24
0008F : 9731 SBIW ZH:ZL,1
00090 : F7E9 BRNE L0008E
|
|
|
Back to top |
|
|
laborratte
Joined: 27 Jul 2005 Posts: 299 Location: Berlin
|
Posted: Fri Aug 19, 2016 3:48 pm Post subject: |
|
|
The purpose of $initmicro is port setting as soon as possible, not watchdog setting for CPU with huge memory and low clock and fused "always wachdog".
IMO the port setting is more important than the rare second situation.
Edit: The assembler output shows "Watchdog off", not "Watchdog 16ms", if the fuse WDTON is not programmed. IMO this is OK. |
|
Back to top |
|
|
O-Family
Joined: 23 May 2010 Posts: 320 Location: Japan
|
Posted: Fri Aug 19, 2016 4:14 pm Post subject: |
|
|
Yes, to control the Watchdog in the fuse bit [WDTON] is a special example.
Therefore, I think this problem has not been found so far.
By performing the call of $initmicro after initialization of WDTCSR, the user can set the Watchdog freely. |
|
Back to top |
|
|
Pzx
Joined: 05 May 2015 Posts: 39
|
Posted: Sun Aug 21, 2016 11:36 pm Post subject: |
|
|
Thanks Bartek, you are very helpful, as usual
You are right, your first code works, I tested it, and it was a surprise for me, but in my code I had one small additional thing that I didint mentioned before. I thought it was not important, but it is.
I want to use LCD in my program so I put CLS before the main loop.
My program look actually like this (ful version):
$regfile = "m2560def.dat"
$crystal = 16000000
$hwstack = 64
$swstack = 32
$framesize = 128
Config Watchdog = 1024
Reset Watchdog
Config Lcd = 16 * 4
Config Lcdpin = Port , Port = Portc , E = Porta.5 , Rs = Porta.7
Led Alias Portk.0
Config Led = Output
Cls 'This instruction is a problem in my code !!!!!!!!!
Lcd "Hello World" 'This instruction is also a problem, even without Cls before !!!!!!!!!
Do
Toggle Led
Waitms 1
Reset Watchdog
Loop
End
Without Cls instruction the code works perfectly, but with Cls or other LCD instruction like Lcd "Hello Word" The code doesnt work. This strange behaviour is only when Watchdog always on fusebit is activated.
If I activate Watchdog controlled by software fusebit all LCD instructions works ok.
Does anyone know how to use LCD with Watchdog always on fusebit activated? |
|
Back to top |
|
|
O-Family
Joined: 23 May 2010 Posts: 320 Location: Japan
|
Posted: Mon Aug 22, 2016 12:25 am Post subject: |
|
|
You showed only a part of the program source.
If you are using the LCD, initialization routine of the LCD after the clearing of the SRAM are put forcibly.
The initialization of this LCD takes time.
Thus, the $noramclear instruction is ineffective, too.
There are no ways until you correct BASCOM by MCS-Mark.
Do not use a fuse bit [WDTON] until the modification is carried out, and please be available on Config Watchdog instruction. |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Mon Aug 22, 2016 8:31 am Post subject: |
|
|
Please don`t start coding in monday without cup of coffe Hahaha
If you insert Initlcd statement then LCD will be initialized at this point
Code below work fine
Code: | $regfile = "m2560def.dat"
$crystal = 16000000
$hwstack = 64
$swstack = 32
$framesize = 128
Config Watchdog = 1024
Config Portb.7 = Output : Led Alias Portb.7 'Arduino Mega LED
Config Lcdpin = Port , Port = Portc , E = Porta.5 , Rs = Porta.7
Initlcd 'the LCD will be initialized here, after Watchdog config
Cls
Reset Watchdog
Do
Toggle Led 'LED flash like a charm
Wait 1
Reset Watchdog
Loop
End |
Have a nice day |
|
Back to top |
|
|
Pzx
Joined: 05 May 2015 Posts: 39
|
Posted: Mon Aug 22, 2016 2:54 pm Post subject: |
|
|
EDC's code solved the problem.
The issue was to properly initialize LCD in my program.
Many thanks, Bartek |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Mon Sep 05, 2016 9:01 pm Post subject: |
|
|
While i can change the sequence so initmicro is called after the WD is turned off. (it is indeed turned off, not set to 16 ms), this will have effect on the timing.
I think a few cycles extra till the user can set the port levels will not matter much? on the other hand, it might be simpler to add an option to skip the reset of the WD.
Something like $NOWD that will remove the init code for the WD. That way the user can make use of the WD always on option.
Please comment. _________________ Mark |
|
Back to top |
|
|
Arera
Joined: 23 Sep 2007 Posts: 386 Location: Wuppertal, Germany
|
Posted: Fri Sep 09, 2016 12:10 pm Post subject: |
|
|
It seems to me, we are running into an dead end here.
Actually it is only about a watchdog, it is not THAT tricky, I think.
I always use the WD, and I never needed to worry about ASM-code and stuff.
First question:
What are you aiming for?
Answer:
In case the main-routine "hangs" for any reason, it cannot reset the WD anymore, resulting in a WD-triggered reset.
That brings us to the crutial point in any programming:
The main routine shold be looped fast and regulary.
FAST means: Do not WAIT for to long. A 1000ms WAIT to flash a led is not a good idea...
Do not loop inside the main loop, unless you are shure the loop will be left within an acceptable time.
REGULARY means: Do not get stuck in subroutines, that themselfs take unexpected (or expected) time.
The WD-timeout should have surplus time to the main-loop execution time. This is to prevent the WD from accidently triggering a reset.
If your main loop takes 3s-5s (witch is looooooong), set the WD to 8s. There is no closer timeout avaliable and nessecary in most cases.
My conclusion:
Setting the WD to one second, and then WAIT for one second in the main-loop, can lead to problems. Obviously it did in in the TO's case.
Creating code, that takes all my hints into account, requires a well structured algorythm. I know this is difficult and often needs more code.
It took me a while to learn how to do this. But in the end it will be faster and more universal.
On the other hand, if your code is simpler (blink a led every 12s using a WAIT in an loop), it can work and be just fine for you, no problem.
But in that case the use of a WD will not succeed.
BTW: resetting the WD in every loop you have will kill the idea of the WD. The WD should be resetted only once in the code, right at the beginning of the main loop. |
|
Back to top |
|
|
O-Family
Joined: 23 May 2010 Posts: 320 Location: Japan
|
Posted: Fri Sep 09, 2016 2:44 pm Post subject: |
|
|
Hi Mark
Like the setting of the stack pointer, the initialization of Watchdog is possible by the assembler order of a short step.
Therefore when initializing Watchdog before $initmicro processing, setting of Watchdog is the user's concerned street.
Since this problem is a special case with the configuration of the fuse, additional new instruction ($NOWD) I think that there is no need.
_init_micro:
Config Watchdog = 1024
Return |
|
Back to top |
|
|
|
|
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
|
|