avr-chat
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-chat] why does ISR not always seem to run on wake from sleep?


From: Britton Kerin
Subject: [avr-chat] why does ISR not always seem to run on wake from sleep?
Date: Mon, 7 Nov 2016 15:56:44 -0900

On vanilla Arduino Uno Rev3, the ISR always runs on wake from sleep
the first time through in the loop in the below program, but not
always later.  So eventually after lots of quick pin changes it ends
up awake but with got_pbpci false.  I cannot understand why.

I also attached the assembly below.  I looked at it to verify that
sei/cli don't appear to be getting reordered in some unexpected way.

Any help greatly appreciated.

#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h>

// The wake-up pin is PB2

// For showing when we hit a trap:
#define BLINK_PB5_LED_FOREVER(period_ms) \
  do {                                   \
    for ( ; ; ) {                        \
      PORTB |= _BV (PORTB5);             \
      _delay_ms (period_ms / 2.0);       \
      PORTB &= ~(_BV (PORTB5));          \
      _delay_ms (period_ms / 2.0);       \
    }                                    \
  } while ( 0 );


volatile uint8_t got_pbpci = 0;   // Got Port B Pin Change Interrupt

volatile uint8_t inry = 1;   // ISR Note Run Yet

ISR (PCINT0_vect)
{
  inry = 0;
  got_pbpci = 1;
}

int
main (void)
{
  // Initialize PB5 for output, with low as initial value
  PORTB &= ~(_BV (PORTB5));
  DDRB |= _BV (DDB5);

  // Initialize wake-up pin as an input, with internal pull-up
  DDRB &= ~(_BV (DDB2));
  PORTB |= _BV (PORTB2);

  // Enable pin change interrupts on wake-up pin
  PCICR |= _BV (PCIE0);
  PCMSK0 |= _BV (PCINT2);

  while ( 1 ) {
    set_sleep_mode (SLEEP_MODE_IDLE);
    sleep_enable ();
    sei ();
    sleep_cpu ();
    sleep_disable ();
    cli ();

    if ( inry ) {
      BLINK_PB5_LED_FOREVER (100.42);   // Never seems to happen
    }

    if ( ! got_pbpci ) {
      BLINK_PB5_LED_FOREVER (200.42);   // Always happend eventually
    }
    else {
      got_pbpci = 0;
    }
  }
}

And the assembly:

    .file    "alt_main.c"
__SP_H__ = 0x3e
__SP_L__ = 0x3d
__SREG__ = 0x3f
__tmp_reg__ = 0
__zero_reg__ = 1
 ;  GNU C (GCC) version 4.8.1 (avr)
 ;     compiled by GNU C version 4.9.1, GMP version 6.0.0, MPFR
version 3.1.2-p3, MPC version 1.0.2
 ;  GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
 ;  options passed:  -fpreprocessed alt_main.i -mmcu=atmega328p
 ;  -auxbase-strip alt_main.o -Os -Werror -Wall -Wextra -Winline
 ;  -Wmissing-prototypes -Wredundant-decls -Winit-self -Wstrict-prototypes
 ;  -std=gnu11 -fshort-enums -fverbose-asm
 ;  options enabled:  -faggressive-loop-optimizations -fauto-inc-dec
 ;  -fbranch-count-reg -fcaller-saves -fcombine-stack-adjustments -fcommon
 ;  -fcompare-elim -fcprop-registers -fcrossjumping -fcse-follow-jumps
 ;  -fdefer-pop -fdevirtualize -fdwarf2-cfi-asm -fearly-inlining
 ;  -feliminate-unused-debug-types -fexpensive-optimizations
 ;  -fforward-propagate -ffunction-cse -fgcse -fgcse-lm -fgnu-runtime
 ;  -fguess-branch-probability -fhoist-adjacent-loads -fident
 ;  -fif-conversion -fif-conversion2 -findirect-inlining -finline
 ;  -finline-atomics -finline-functions -finline-functions-called-once
 ;  -finline-small-functions -fipa-cp -fipa-profile -fipa-pure-const
 ;  -fipa-reference -fipa-sra -fira-hoist-pressure -fira-share-save-slots
 ;  -fira-share-spill-slots -fivopts -fkeep-static-consts
 ;  -fleading-underscore -fmath-errno -fmerge-constants
 ;  -fmerge-debug-strings -fmove-loop-invariants -fomit-frame-pointer
 ;  -foptimize-register-move -foptimize-sibling-calls -fpartial-inlining
 ;  -fpeephole -fpeephole2 -fprefetch-loop-arrays -freg-struct-return
 ;  -fregmove -freorder-blocks -freorder-functions -frerun-cse-after-loop
 ;  -fsched-critical-path-heuristic -fsched-dep-count-heuristic
 ;  -fsched-group-heuristic -fsched-interblock -fsched-last-insn-heuristic
 ;  -fsched-rank-heuristic -fsched-spec -fsched-spec-insn-heuristic
 ;  -fsched-stalled-insns-dep -fshow-column -fshrink-wrap -fsigned-zeros
 ;  -fsplit-ivs-in-unroller -fsplit-wide-types -fstrict-aliasing
 ;  -fstrict-overflow -fstrict-volatile-bitfields -fsync-libcalls
 ;  -fthread-jumps -ftoplevel-reorder -ftrapping-math -ftree-bit-ccp
 ;  -ftree-builtin-call-dce -ftree-ccp -ftree-ch -ftree-coalesce-vars
 ;  -ftree-copy-prop -ftree-copyrename -ftree-dce -ftree-dominator-opts
 ;  -ftree-dse -ftree-forwprop -ftree-fre -ftree-loop-if-convert
 ;  -ftree-loop-im -ftree-loop-ivcanon -ftree-loop-optimize
 ;  -ftree-parallelize-loops= -ftree-phiprop -ftree-pre -ftree-pta
 ;  -ftree-reassoc -ftree-scev-cprop -ftree-sink -ftree-slp-vectorize
 ;  -ftree-slsr -ftree-sra -ftree-switch-conversion -ftree-tail-merge
 ;  -ftree-ter -ftree-vect-loop-version -ftree-vrp -funit-at-a-time
 ;  -fverbose-asm -fzero-initialized-in-bss

    .text
.global    __vector_3
    .type    __vector_3, @function
__vector_3:
    push r1     ;
    push r0     ;
    in r0,__SREG__     ; ,
    push r0     ;
    clr __zero_reg__     ;
    push r24     ;
/* prologue: Signal */
/* frame size = 0 */
/* stack size = 4 */
.L__stack_usage = 4
    sts inry,__zero_reg__     ;  inry,
    ldi r24,lo8(1)     ;  tmp42,
    sts got_pbpci,r24     ;  got_pbpci, tmp42
/* epilogue start */
    pop r24     ;
    pop r0     ;
    out __SREG__,r0     ; ,
    pop r0     ;
    pop r1     ;
    reti
    .size    __vector_3, .-__vector_3
    .section    .text.startup,"ax",@progbits
.global    main
    .type    main, @function
main:
/* prologue: function */
/* frame size = 0 */
/* stack size = 0 */
.L__stack_usage = 0
    cbi 0x5,5     ; ,
    sbi 0x4,5     ; ,
    cbi 0x4,2     ; ,
    sbi 0x5,2     ; ,
    lds r24,104     ;  D.1697, MEM[(volatile uint8_t *)104B]
    ori r24,lo8(1)     ;  D.1697,
    sts 104,r24     ;  MEM[(volatile uint8_t *)104B], D.1697
    lds r24,107     ;  D.1697, MEM[(volatile uint8_t *)107B]
    ori r24,lo8(4)     ;  D.1697,
    sts 107,r24     ;  MEM[(volatile uint8_t *)107B], D.1697
.L7:
    in r24,0x33     ;  D.1697, MEM[(volatile uint8_t *)83B]
    andi r24,lo8(-15)     ;  D.1697,
    out 0x33,r24     ;  MEM[(volatile uint8_t *)83B], D.1697
    in r24,0x33     ;  D.1697, MEM[(volatile uint8_t *)83B]
    ori r24,lo8(1)     ;  D.1697,
    out 0x33,r24     ;  MEM[(volatile uint8_t *)83B], D.1697
/* #APP */
 ;  48 "alt_main.c" 1
    sei
 ;  0 "" 2
 ;  49 "alt_main.c" 1
    sleep

 ;  0 "" 2
/* #NOAPP */
    in r24,0x33     ;  D.1697, MEM[(volatile uint8_t *)83B]
    andi r24,lo8(-2)     ;  D.1697,
    out 0x33,r24     ;  MEM[(volatile uint8_t *)83B], D.1697
/* #APP */
 ;  51 "alt_main.c" 1
    cli
 ;  0 "" 2
/* #NOAPP */
    lds r24,inry     ;  inry.0, inry
    tst r24     ;  inry.0
    breq .L3     ; ,
.L8:
    sbi 0x5,5     ; ,
    ldi r18,lo8(160671)     ; ,
    ldi r24,hi8(160671)     ; ,
    ldi r25,hlo8(160671)     ; ,
    1: subi r18,1     ;
    sbci r24,0     ;
    sbci r25,0     ;
    brne 1b
    rjmp .
    nop
    cbi 0x5,5     ; ,
    ldi r18,lo8(160671)     ; ,
    ldi r24,hi8(160671)     ; ,
    ldi r25,hlo8(160671)     ; ,
    1: subi r18,1     ;
    sbci r24,0     ;
    sbci r25,0     ;
    brne 1b
    rjmp .
    nop
    rjmp .L8     ;
.L3:
    lds r24,got_pbpci     ;  got_pbpci.1, got_pbpci
    cpse r24,__zero_reg__     ;  got_pbpci.1,
    rjmp .L5     ;
.L9:
    sbi 0x5,5     ; ,
    ldi r18,lo8(320671)     ; ,
    ldi r24,hi8(320671)     ; ,
    ldi r25,hlo8(320671)     ; ,
    1: subi r18,1     ;
    sbci r24,0     ;
    sbci r25,0     ;
    brne 1b
    rjmp .
    nop
    cbi 0x5,5     ; ,
    ldi r18,lo8(320671)     ; ,
    ldi r24,hi8(320671)     ; ,
    ldi r25,hlo8(320671)     ; ,
    1: subi r18,1     ;
    sbci r24,0     ;
    sbci r25,0     ;
    brne 1b
    rjmp .
    nop
    rjmp .L9     ;
.L5:
    sts got_pbpci,__zero_reg__     ;  got_pbpci,
    rjmp .L7     ;
    .size    main, .-main
.global    inry
    .data
    .type    inry, @object
    .size    inry, 1
inry:
    .byte    1
.global    got_pbpci
    .section .bss
    .type    got_pbpci, @object
    .size    got_pbpci, 1
got_pbpci:
    .zero    1
    .ident    "GCC: (GNU) 4.8.1"
.global __do_copy_data
.global __do_clear_bss



reply via email to

[Prev in Thread] Current Thread [Next in Thread]