Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : Prevent HW Error from stopping Program
Author | Message | ||||
atmega8 Guru Joined: 19/11/2013 Location: GermanyPosts: 712 |
Hello, how can i prevent a Hardware error, for example broken cable of RTC, from stopping the Programm? RTC Gettime Error: RTC not responding THX, Atmega8 |
||||
atmega8 Guru Joined: 19/11/2013 Location: GermanyPosts: 712 |
Hi Forum, may be this is really no problem and i'am wrong ?! |
||||
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 3676 |
Generally you can't and should not expect to. As there are far more of them it's more likely you'll have badly soldered power or grounds. The fix is to solder properly. If you can't then buy boards from someone who can. Or, use multiple boards and vote. Much as the US moon rockets did. (They did use pretty reliable hardware, too.) John |
||||
Geoffg Guru Joined: 06/06/2011 Location: AustraliaPosts: 3165 |
You can set the watchdog timer which will restart the processor... but then it will fall over again when it tries to read the RTC. Other than that there is nothing that you can do as MMBasic treats a failed RTC as a fatal error. Geoff Graham - http://geoffg.net |
||||
twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1139 |
I think this could be done with a extended version of "ON ERROR CONTINUE". But this wold make MMBasic (source) more komplex ... |
||||
srnet Senior Member Joined: 08/08/2014 Location: United KingdomPosts: 164 |
Perhaps a specific function that would just return an error if there was no device found on the I2C bus would be an idea. At last then the program could be written so that it could, at startup for instance, test for the devices and flag and report an error in some way. $50SAT is Silent but probably still working. For information on LoRa visit http://www.loratracker.uk/ |
||||
twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1139 |
Yes, if the concern is only to the RTC error intercept you can write your own RTC procedure. Something like this (not tested yet!): Sub SaveRTC DIM temp, i2caddr, RTCavail i2caddr = &h68 ' Your RTC address for DS3231 I2C open 100, 1000 ' i2c enable 100kHz, 1000ms timeout I2C read i2caddr, 0, 1, temp If MM.I2C = 0 Then RTCavail=1 else RTCavail=0 Print "RTC Error "; i2caddr EndIf I2C close ' i2c disable if RTCavail then RTC GETTIME End sub Michael |
||||
atmega8 Guru Joined: 19/11/2013 Location: GermanyPosts: 712 |
I think, that in general, a malefunktion of a peripheral Hardware device, should not stop the program execution. Instead it should set an error flag, like MM.errno. So the software programmer is responsable for the correct error Handling. What do you think? Should not be to hard, to implement?! |
||||
twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1139 |
I agree, in many cases it could be done easily. But there are limits (execution speed, memory usage, development cycle time). And for a one man project seems MMBasic pretty well made. Please remember: If you need you can take the original sources and build your own (better/safer) CFunctions ... and share here. On the other hand, I think perhaps it would be important to remember not only as many functions as possible to have but also a good error handling. Just my 2c. |
||||
Positron Newbie Joined: 09/08/2015 Location: ArgentinaPosts: 15 |
Just elaborating on Geoff's idea of using the Watchdog timer to reset the CPU, if you combine this with using a persistent variable to indicate if a hardware operation is pending, you could recover easilly. Some quick and dirty pseudocode to implement this idea (not tested...): 1. Outside your program (command line) use RTC SETREG to set a Flag that indicates a pending HW operation initially =0 (only once or after you fixed the faulty hardware). 2. In your program check that Flag with RTC GETREG, if =0 you set it to =1 and perform the HW operation. 3. If it hangs up, the watchdog will reset the CPU and your Flag will be =1 indicating that the previous operation failed, so on the next execution of your routine it knows you have a faulty HW and perform whatever recovery or ignore sequence you like. 4. If the HW operation succeeds, you reset the Flag to =0 and execute your normal code. I think this could work for you, let me now. PD: You could also use VAR SAVE/ RESTORE if you are not abusing the flash with writes every minute... |
||||
atmega8 Guru Joined: 19/11/2013 Location: GermanyPosts: 712 |
You can not check the proper function of an object with the object himself ( itself?) ! This results in a Perpetuum Mobile. All in all it sounds a Little Bit abstrus. Blut what about the MMBasic command "continue"? |
||||
Positron Newbie Joined: 09/08/2015 Location: ArgentinaPosts: 15 |
Sorry atmega8, perhaps I didn't expalain it well with my pseudocode, but I think this method will work because once that the hw function fails, the watchdog resets and next time you effectively know that it failed BEFORE executing the hw function again ! So you are not trapped in a loop. I will write a small program to prove this tomorrow. |
||||
robert.rozee Guru Joined: 31/12/2012 Location: New ZealandPosts: 2294 |
with delphi, there are quite complicated structures to handle errors. TRY / EXCEPT / END is the simplest one that could be useful in MMbasic - if it were practical to add in, of course. the (basic-ized) syntax is: TRY
RTC GETTIME print "time set OK" EXCEPT print "enter time (H, M, S)"; input H, M, S time$ = str$(H) +":"+ str$(M) +":"+ str$(S) ENDTRY print "code carries on here" geoff: how difficult would it be to add this sort of error/exception handling to MMbasic? in an ideal world, this sort of structure could be used to catch division by zero and other maths errors, even as an alternative to the current DS18B20, DISTANCE, and DHT22 error handling. cheers, rob :-) |
||||
atmega8 Guru Joined: 19/11/2013 Location: GermanyPosts: 712 |
Hi Rob, a mailfunction of a ds1820 does Not stop the Program. It returns a value of 1000 andrehe program resumes operation. If you check for 1000 you have an Error Handling. Atmega8 |
||||
robert.rozee Guru Joined: 31/12/2012 Location: New ZealandPosts: 2294 |
i'm thinking it could be available as a secondary method of handling the situation, perhaps providing a cleaner syntax. the structure could perhaps also be used to handle I2C and ONEWIRE failure conditions - again, even though the errors are not not fatal, the syntax may prove to be easier to read. cheers, rob :-) |
||||
Geoffg Guru Joined: 06/06/2011 Location: AustraliaPosts: 3165 |
Implementing an ON ERROR function is not that easy. Currently the interpreter simply clears the stack and all status (ie, FOR loops, GOSUB returns, etc) and returns to the command prompt. This makes it easy, on every error it simply goes back to the start. But ON ERROR requires the interpreter to intelligently exit whatever command was being executed and keep the program's status. There are lots of errors that can occur so that would require a lot of extra code (and flash). Geoff Geoff Graham - http://geoffg.net |
||||
G8JCF Guru Joined: 15/05/2014 Location: United KingdomPosts: 676 |
Hi Robert Way back when, I once wrote an interpreter for a language called CESIL, "Customer Engineering Systems Integration Language", for Thorn EMI, which was an interpreter for process control of HVAC equipment. In that interpreter we had a global var called ERRNO which was normally 0. When the interpreter encountered an error, it would set ERRNO to a non zero value, ie the error code. The program could then check after executing a command which might give rise to an error for a non-zero ERRNO value and if ERRNO was non-zero then decide what to do based on the value of ERRNO. If I recall the syntax was something like [code] ;Set ERRNO to 0 before we start the operation ERRNO=0 I=ANALOGUE(20) ;Capture the state of ERRNO J=ERRNO ERRNO=0 ;Error Handling block IF J <> 0 THEN ;If conversion out of range, then try again IF J=1 I=ANALOGUE(20) ENDIF ;Check if another error condition IF J=2 do something ENDIF ENDIF ;Reset ERRNO to 0 ERRNO=0 .... continue with rest of program [/code] Since this was an embedded control system, aborting to the console was not an option, and restarting was also deemed too drastic, hence the ERRNO concept. (restart being reserved for watchdog timeout). In many ways ERRNO is similar to the "RESUME NEXT" of Visual Basic, leaving the error checking and handling up to the programmer. When it comes to MMBasic, I would suggest that perhaps a new OPTION, OPTION ERROR RESUME | BREAK could be added to MMBasic, ie when OPTION ERROR RESUME is in force MMBasic simply updates a variable, eg MM.ERRNO with a non-zero code reflecting the error condition and then continues onto the next MMBasic statement, whereas with OPTION ERROR BREAK in force, MMBasic breaks out to the command prompt as it does now. I don't know if MMBasic would permit a statement such as MM.ERRNO=0 to be issued, but that's surely a minor detail. Peter The only Konstant is Change |
||||
Print this page |