Re: Another gotcha- @ and ! can break interrupts

Samuel Tardieu <[email protected]> 14 Nov 2004 20:43:47 +0100
Newsgroups gmane.comp.lang.forth.picforth
Organization Avian Carrier & Friends
Message-ID <[email protected]>
>>>>> "Alex" == Alex Holden <[email protected]> writes:

Alex> These files define words called any-@ and any-! which are
Alex> sometimes called when you use '@' and '!'. any-@ and any-! use
Alex> suspend-interrupts and restore-interrupts to prevent interrupts
Alex> from occuring in the middle of a fetch or store.

Could you try the following picisr.fs version and tell me whether this
fixes your problem? It allows you to nest as many as 256
suspend-interrupts/restore-interrupts. The drawback are:

  - the code is a little longer (not much) than before for initial
    (non-nested) calls (and faster for nested calls)

  - it takes one more byte in memory

The generated code looks like:

        ; name: suspend-interrupts
        ; max return-stack depth: 0
0x000D  0AA5    incf    0x25,f
0x000E  0B25    decfsz  0x25,w
0x000F  0008    return  
0x0010  080B    movf    0x0B,w
0x0011  00A4    movwf   0x24
0x0012  138B    bcf     0x0B,7
0x0013  0008    return  
        ; name: restore-interrupts
        ; max return-stack depth: 0
0x0014  0B25    decfsz  0x25,f
0x0015  0008    return  
0x0016  1BA4    btfsc   0x24,7
0x0017  178B    bsf     0x0B,7
0x0018  0008    return  

  Sam
-- 
Samuel Tardieu -- [email protected] -- http://www.rfc1149.net/sam

_______________________________________________
PicForth mailing list
[email protected]
http://lists.rfc1149.net/mailman/listinfo/picforth
picisr.fs (text/x-forth, 1.4 KB)
\
\ PicForth library file
\
\ This library file has been written by Samuel Tardieu <[email protected]>.
\ It belongs to the public domain. Do whatever you want with it.
\
\ Code for handling interrupt for the PIC. This is quite convoluted as we
\ have to restrict the bank 0 from using address $7f, which must be reserved
\ in all the banks (see PIC datasheet for an explanation).

host

current-area @
meta> bank0
$7e data-change-high

target
$7f constant w_temp
variable status_temp
variable pclath_temp

host
current-area !

target

4 org    \ Vector for isr is 4

code (isr-save) ( -- )
    w_temp movwf
    status ,w swapf
    status clrf
    status_temp movwf
    pclath ,w movf
    pclath_temp movwf
end-code

meta

create isr-origin tcshere ,
3 +org

: isr-restore-return ( -- )
    pclath_temp ,w movf
    pclath movwf
    status_temp ,w swapf
    status movwf
    w_temp ,f swapf
    w_temp ,w swapf
    retfie unreachable
;

: enable-interrupts ( -- ) gie bsf ;
: disable-interrupts ( -- ) gie bcf ;

: isr ( -- ) isr-origin @ set-vector ;

target

variable isr-enabled
create isr-depth 0 ,

code suspend-interrupts
    isr-depth ,f incf
    isr-depth ,w decfsz
    return
    gie drop ,w movf
    isr-enabled movwf
    disable-interrupts
    return
end-code

code restore-interrupts
    isr-depth ,f decfsz
    return
    isr-enabled gie nip btfsc
    enable-interrupts
    return
end-code