• AVR Freaks

Hot!Bad to call function inside interrupt?

Author
Number_two
Starting Member
  • Total Posts : 35
  • Reward points : 0
  • Joined: 2015/01/24 20:18:41
  • Location: 0
  • Status: offline
2019/11/30 23:47:07 (permalink)
0

Bad to call function inside interrupt?

Programming a pic16lf18345 in assembly. The interrupts are giving me trouble. Something keeps either turning off all interrupt enable bits randomly (mostly GIE and PEIE), or it will get stuck in the interrupt loop and not be able to exit. This happens in spite of debug mode saying all interrupt flags are low.
 
ISR CODE 0x0004 
INTERRUPTS:
    banksel PIR1
    pagesel RECEIVE_INT
    btfsc PIR1,RCIF
    call RECEIVE_INT
    
    banksel PIR1
    pagesel TIMEOUT
    btfsc PIR1,TMR2IF
    call TIMEOUT
    
    banksel IOCCF
    pagesel GDO2_F
    btfsc IOCCF,7
    call GDO2_F
    

; This was my attempt at trying to disable everything...
    banksel PIR0
    clrf PIR0
    banksel PIR1
    bcf PIR1,TMR1IF
    bcf PIR1,SSP1IF
    banksel PIR2
    bcf PIR2,NVMIF
    retfie
       
RECEIVE_INT:
    BANKSEL STATE
    ;movlf STATE, 0x1 ; Set state to RECIEVING
    movlw 0x1
    banksel STATE
    movwf STATE
    
    banksel RC1REG
    movfw RC1REG
    banksel DATBUF
    movwf DATBUF
    pagesel FPUSH_FIFO
    call FPUSH_FIFO ; <----- COULD THIS BE CAUSING THE ISSUE?
    banksel PIR1
    bcf PIR1,RCIF ; Clear RCIF flag
    return
    
TIMEOUT:
    pagesel TMR2_STOP
    call TMR2_STOP ; Stop timer
    ;movlf STATE, 0x2 ; Set state to DECODE
    movlw 0x2
    banksel STATE
    movwf STATE
    
    banksel PIR1
    bcf PIR1,TMR2IF ; Clear TMR2IF flag
    return

GDO2_F:
    banksel PIR0
    BCF PIR0,IOCIF ; Clear IOCIF
    banksel IOCCF
    bcf IOCCF,7

    movlw 0x3
    banksel STATE
    movwf STATE
    

    return
    
    END

 
post edited by Number_two - 2019/12/01 08:54:00
#1

12 Replies Related Threads

    ric
    Super Member
    • Total Posts : 24638
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Bad to call function inside interrupt? 2019/12/01 01:09:13 (permalink)
    +1 (1)
    You ask if calling FPUSH_FIFO could cause a problem, but don't show the code in that subroutine.
     
    Note, your RECEIVE_INT code does "banksel state" twice in a row, and attempts to clear RCIF, which is a read-only flag (it's automatically cleared when you read RCREG).
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    NorthGuy
    Super Member
    • Total Posts : 5817
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Bad to call function inside interrupt? 2019/12/01 07:24:33 (permalink)
    +1 (1)
    This part:
     

        banksel RC1REG
        movfw   RC1REG
        banksel DATBUF
        movwf   DATBUF

     
    Perhaps you should change it to:
     

        banksel RC1REG
        movf   RC1REG,w
        banksel DATBUF
        movwf   DATBUF

     
    ?
    #3
    Number_two
    Starting Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2015/01/24 20:18:41
    • Location: 0
    • Status: offline
    Re: Bad to call function inside interrupt? 2019/12/01 08:50:34 (permalink)
    0
    Oops. There was a return using a goto instead of call... pink: pink
     
    I knew the counter was getting corrupted somewhere. Weird thing is, it works most of the time, except on very long receptions where it uses the whole FIFO.
     
    Also, forum question; Is there a reason it keeps saying "ACCESS DENIED" when I try to do anything other than quick post?
     
    Edit: Actually this can't be the problem. It doesn't call FPUSH_FIFO when the issue occurs.

    FPUSH_FIFO:
        ;ifltf FIFO_BYTES, FIFO_WM, goto, pfifo_nfull
        banksel FIFO_WM
        movfw FIFO_WM
        banksel FIFO_BYTES
        subwf FIFO_BYTES,w
        pagesel pfifo_nfull
        btfss STATUS,C
        goto pfifo_nfull <--- This.
        return
        
    pfifo_nfull:
       ; warr FIFO_DATA, HEAD, DATBUF
        banksel FIFO_DATA
        movlw HIGH(FIFO_DATA)
        banksel FSR0
        movwf FSR0H
        banksel FIFO_DATA
        movlw LOW(FIFO_DATA)
        banksel FSR0
        movwf FSR0L

        banksel HEAD
        movfw HEAD
        banksel FSR0
        addwf FSR0

        banksel DATBUF
        movfw DATBUF
        banksel INDF0
        movwf INDF0
        
        banksel HEAD
        incf HEAD

        banksel FIFO_WM
        movfw FIFO_WM
        incf WREG
        banksel HEAD
        subwf HEAD,w
        pagesel hnz
        btfss STATUS,C
        goto hnz
        
        banksel HEAD
        clrf HEAD
    hnz:
        banksel FIFO_BYTES
        incf FIFO_BYTES
        return
        global FPUSH_FIFO

    post edited by Number_two - 2019/12/01 09:16:29
    #4
    NorthGuy
    Super Member
    • Total Posts : 5817
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Bad to call function inside interrupt? 2019/12/01 10:56:00 (permalink)
    +1 (1)
    You still return from your call at the end, so having gotos in between won't hurt.
     
    Is your FSR aligned? Otherwise adding HEAD may overflow FSR0 or go out of the bank?
     
    BTW: You don't need banksels for FSRs and INFs because they're accessible from all banks. Also, you can use bra instead of goto and avoid most pagesels.
     
     
    #5
    ric
    Super Member
    • Total Posts : 24638
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Bad to call function inside interrupt? 2019/12/01 12:15:25 (permalink)
    +1 (1)
    NorthGuy
    BTW: You don't need banksels for FSRs and INFs because they're accessible from all banks. Also, you can use bra instead of goto and avoid most pagesels.

    You're thinking in PIC18 mode. This is a PIC16 chip. PIC16LF18435
    So, BRA is available, but BANKSELs are still often needed.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #6
    NorthGuy
    Super Member
    • Total Posts : 5817
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Bad to call function inside interrupt? 2019/12/01 14:31:16 (permalink)
    0
    ric
    So, BRA is available, but BANKSELs are still often needed.



    Banksels are certainly needed, but not for FSRs and INDFs because these (along with WREG, PCLATH and other important registers) are located in the common area (satarting from 0x0) which is always accessible regardless of the value of the BSR.
    #7
    ric
    Super Member
    • Total Posts : 24638
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Bad to call function inside interrupt? 2019/12/01 14:55:49 (permalink)
    +1 (1)
    Ahh, I was misreading your "FSRs" as "SFRs", and "INF" rather than "INDF" threw me.
    Yes, as you say, the "core" registers are always available and don't need banksels in a PIC16
     
    I also have a big dislike of the "movfw" pseudo op, its way too easy to mistake for "movwf".
    Far preferable to use the real instruction "mov reg,w"
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #8
    NorthGuy
    Super Member
    • Total Posts : 5817
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Bad to call function inside interrupt? 2019/12/01 15:40:34 (permalink)
    0
    ric
    Ahh, I was misreading your "FSRs" as "SFRs", and "INF" rather than "INDF" threw me.



    My typing has always been bad. The compiler always corrects me, but the spellchecker only makes things worse :)
    #9
    ric
    Super Member
    • Total Posts : 24638
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Bad to call function inside interrupt? 2019/12/01 16:23:43 (permalink)
    +1 (1)
    Also, are these two routines really right next to each other?
    If yes, you don't need a GOTO at all
        btfss STATUS,C
        goto pfifo_nfull <--- This.
        return
        
    pfifo_nfull:
       ; warr FIFO_DATA, HEAD, DATBUF

    could be:
        btfsc STATUS,C
        return
        
    pfifo_nfull:
       ; warr FIFO_DATA, HEAD, DATBUF


    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #10
    Number_two
    Starting Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2015/01/24 20:18:41
    • Location: 0
    • Status: offline
    Re: Bad to call function inside interrupt? 2019/12/01 23:18:50 (permalink)
    0
    I found the problem in another section.
     
    I had old code that I intended to delete after I replaced with this new method to access the FIFO. Apparently I never deleted it so it was writing everything twice. When the string was long enough, it started writing into some other parts of memory and after that... well, bad things happened.
     
    But I will take your suggestions.
     
    Also, are these two routines really right next to each other?
    If yes, you don't need a GOTO at all
     
    This section was weird because I ported it over from C and forgot to delete some things.
     
    But it works now, so I guess that's good. Smile: Smile
    #11
    1and0
    Access is Denied
    • Total Posts : 10005
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Bad to call function inside interrupt? 2019/12/02 04:04:42 (permalink)
    0
    OUCH! Your ISR code in Posts #1 and #4 uses BANKSEL for EVERY data access (including core registers) and PAGESEL for EVERY branch. That is very inefficient and makes for one sloooower assembly interrupt routine, worse than one generated by MCC. ;) Anyway, learn to organize your data and program memories to write more efficient code; e.g. group common variables in same bank, use BRA instead of GOTO, rearrange code to avoid additional BANKSEL, etc. Just my 2 cents.
     
    Edit: By the way, is the rest of your code like this?!
    post edited by 1and0 - 2019/12/02 04:07:49
    #12
    NorthGuy
    Super Member
    • Total Posts : 5817
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Bad to call function inside interrupt? 2019/12/02 09:32:52 (permalink)
    +3 (3)
    gacekky1
    I found the problem in another section.



    This is how such threads usually end :)
    #13
    Jump to:
    © 2019 APG vNext Commercial Version 4.5