//upgrade original audiointerrupt if needed (hackish...) void init_interrupt() { if (NVIC_GET_PRIORITY(IRQ_AUDIO) == 240) { NVIC_SET_PRIORITY(IRQ_AUDIO, 224); } }
/******************************************************************************* * Enable digital interrupt and configure the pin *******************************************************************************/ void SnoozeDigital::enableDriver( void ) { if ( mode == RUN_LP ) { return; } #if defined(KINETISK) uint64_t _pin = pin; isr_pin = pin; if ( mode == VLPW || mode == VLPS ) { return_isr_a_enabled = NVIC_IS_ENABLED( IRQ_PORTA ); return_isr_b_enabled = NVIC_IS_ENABLED( IRQ_PORTB ); return_isr_c_enabled = NVIC_IS_ENABLED( IRQ_PORTC ); return_isr_d_enabled = NVIC_IS_ENABLED( IRQ_PORTD ); return_isr_e_enabled = NVIC_IS_ENABLED( IRQ_PORTE ); NVIC_DISABLE_IRQ(IRQ_PORTA); NVIC_DISABLE_IRQ(IRQ_PORTB); NVIC_DISABLE_IRQ(IRQ_PORTC); NVIC_DISABLE_IRQ(IRQ_PORTD); NVIC_DISABLE_IRQ(IRQ_PORTE); NVIC_CLEAR_PENDING(IRQ_PORTA); NVIC_CLEAR_PENDING(IRQ_PORTB); NVIC_CLEAR_PENDING(IRQ_PORTC); NVIC_CLEAR_PENDING(IRQ_PORTD); NVIC_CLEAR_PENDING(IRQ_PORTE); int priority = nvic_execution_priority( );// get current priority // if running from interrupt set priority higher than current interrupt priority = ( priority < 256 ) && ( ( priority - 16 ) > 0 ) ? priority - 16 : 128; return_priority_a = NVIC_GET_PRIORITY( IRQ_PORTA );//get current priority return_priority_b = NVIC_GET_PRIORITY( IRQ_PORTB );//get current priority return_priority_c = NVIC_GET_PRIORITY( IRQ_PORTC );//get current priority return_priority_d = NVIC_GET_PRIORITY( IRQ_PORTD );//get current priority return_priority_e = NVIC_GET_PRIORITY( IRQ_PORTE );//get current priority NVIC_SET_PRIORITY( IRQ_PORTA, priority );//set priority to new level NVIC_SET_PRIORITY( IRQ_PORTB, priority );//set priority to new level NVIC_SET_PRIORITY( IRQ_PORTC, priority );//set priority to new level NVIC_SET_PRIORITY( IRQ_PORTD, priority );//set priority to new level NVIC_SET_PRIORITY( IRQ_PORTE, priority );//set priority to new level __disable_irq( ); return_porta_irq = _VectorsRam[IRQ_PORTA+16];// save prev isr handler return_portb_irq = _VectorsRam[IRQ_PORTB+16];// save prev isr handler return_portc_irq = _VectorsRam[IRQ_PORTC+16];// save prev isr handler return_portd_irq = _VectorsRam[IRQ_PORTD+16];// save prev isr handler return_porte_irq = _VectorsRam[IRQ_PORTE+16];// save prev isr handler attachInterruptVector( IRQ_PORTA, isr );// set snooze digA isr attachInterruptVector( IRQ_PORTB, isr );// set snooze digB isr attachInterruptVector( IRQ_PORTC, isr );// set snooze digC isr attachInterruptVector( IRQ_PORTD, isr );// set snooze digD isr attachInterruptVector( IRQ_PORTE, isr );// set snooze digE isr __enable_irq( ); NVIC_ENABLE_IRQ( IRQ_PORTA ); NVIC_ENABLE_IRQ( IRQ_PORTB ); NVIC_ENABLE_IRQ( IRQ_PORTC ); NVIC_ENABLE_IRQ( IRQ_PORTD ); NVIC_ENABLE_IRQ( IRQ_PORTE ); } _pin = pin; while ( __builtin_popcountll( _pin ) ) { uint32_t pinNumber = 63 - __builtin_clzll( _pin );// get pin if ( pinNumber > 33 ) break; uint32_t pin_mode = irqType[pinNumber] >> 4;// get type uint32_t pin_type = irqType[pinNumber] & 0x0F;// get mode volatile uint32_t *config; config = portConfigRegister( pinNumber ); return_core_pin_config[pinNumber] = *config; if ( pin_mode == INPUT || pin_mode == INPUT_PULLUP ) {// setup pin mode/type/interrupt *portModeRegister( pinNumber ) = 0; if ( pin_mode == INPUT ) *config = PORT_PCR_MUX( 1 ); else *config = PORT_PCR_MUX( 1 ) | PORT_PCR_PE | PORT_PCR_PS;// pullup if ( mode == VLPW || mode == VLPS ) { attachDigitalInterrupt( pinNumber, pin_type );// set pin interrupt } else { llwu_configure_pin_mask( pinNumber, mode ); } } else { //pinMode( pinNumber, pin_mode ); //digitalWriteFast( pinNumber, pin_type ); } _pin &= ~( ( uint64_t )1 << pinNumber );// remove pin from list } #elif defined(KINETISL) uint32_t _pin = pin; isr_pin = pin; if ( mode == VLPW || mode == VLPS ) {// if using sleep must setup pin interrupt to wake return_isr_a_enabled = NVIC_IS_ENABLED( IRQ_PORTA ); return_isr_cd_enabled = NVIC_IS_ENABLED( IRQ_PORTCD ); NVIC_DISABLE_IRQ(IRQ_PORTA); NVIC_DISABLE_IRQ(IRQ_PORTCD); NVIC_CLEAR_PENDING(IRQ_PORTA); NVIC_CLEAR_PENDING(IRQ_PORTCD); int priority = nvic_execution_priority( );// get current priority // if running from interrupt set priority higher than current interrupt priority = ( priority < 256 ) && ( ( priority - 16 ) > 0 ) ? priority - 16 : 128; return_priority_a = NVIC_GET_PRIORITY( IRQ_PORTA );//get current priority return_priority_cd = NVIC_GET_PRIORITY( IRQ_PORTCD );//get current priority NVIC_SET_PRIORITY( IRQ_PORTA, priority );//set priority to new level NVIC_SET_PRIORITY( IRQ_PORTCD, priority );//set priority to new level __disable_irq( ); return_porta_irq = _VectorsRam[IRQ_PORTA+16];// save prev isr handler return_portcd_irq = _VectorsRam[IRQ_PORTCD+16];// save prev isr handler attachInterruptVector( IRQ_PORTA, isr );// set snooze isr attachInterruptVector( IRQ_PORTCD, isr );// set snooze isr __enable_irq( ); NVIC_ENABLE_IRQ( IRQ_PORTA ); NVIC_ENABLE_IRQ( IRQ_PORTCD ); } _pin = pin; while ( __builtin_popcount( _pin ) ) { uint32_t pinNumber = 31 - __builtin_clz( _pin );// get pin if ( pinNumber > 33 ) break; uint32_t pin_mode = irqType[pinNumber] >> 4;// get type uint32_t pin_type = irqType[pinNumber] & 0x0F;// get mode volatile uint32_t *config; config = portConfigRegister( pinNumber ); return_core_pin_config[pinNumber] = *config; if ( pin_mode == INPUT || pin_mode == INPUT_PULLUP ) {// setup pin mode/type/interrupt *portModeRegister( pinNumber ) &= ~digitalPinToBitMask( pinNumber ); // TODO: atomic if ( pin_mode == INPUT ) *config = PORT_PCR_MUX( 1 ); else *config = PORT_PCR_MUX( 1 ) | PORT_PCR_PE | PORT_PCR_PS;// pullup if ( pin_mode == VLPW || pin_mode == VLPS ) { attachDigitalInterrupt( pinNumber, pin_type );// set pin interrupt } else { llwu_configure_pin_mask( pinNumber, pin_mode ); } } else { //pinMode( pinNumber, pin_mode ); //digitalWriteFast( pinNumber, pin_type ); } _pin &= ~( ( uint32_t )1 << pinNumber );// remove pin from list } #endif }
/******************************************************************************* * Enable Driver *******************************************************************************/ void SnoozeCompare::enableDriver( void ) { if ( mode == RUN_LP ) { return; } if ( mode == VLPW || mode == VLPS ) { IRQ_NUMBER_t IRQ_CMP; switch (pin) { case 11: IRQ_CMP = IRQ_CMP0; break; #if defined(KINETISK) case 9: IRQ_CMP = IRQ_CMP1; break; case 4: IRQ_CMP = IRQ_CMP2; break; #endif default: IRQ_CMP = IRQ_CMP; return; } return_priority = NVIC_GET_PRIORITY( IRQ_CMP );//get current priority int priority = nvic_execution_priority( );// get current priority // if running from handler mode set priority higher than current handler priority = ( priority < 256 ) && ( ( priority - 16 ) > 0 ) ? priority - 16 : 128; NVIC_SET_PRIORITY( IRQ_CMP, priority );//set priority to new level __disable_irq( ); return_cmp_irq = _VectorsRam[IRQ_CMP+16];// save prev isr attachInterruptVector( IRQ_CMP, wakeupIsr ); __enable_irq( ); } if ( SIM_SCGC4 & SIM_SCGC4_CMP ) SIM_SCGC4_clock_active = true; else SIM_SCGC4 |= SIM_SCGC4_CMP; CR0 = *cmpx_cr0; CR1 = *cmpx_cr1; SCR = *cmpx_scr; FPR = *cmpx_fpr; MUXCR = *cmpx_muxcr; DACCR = *cmpx_daccr; uint8_t _pin = 0; *cmpx_cr0 = 0; *cmpx_cr1 = 0; *cmpx_scr = 0; #if defined(__MKL26Z64__) || defined(__MK66FX1M0__) if ( SIM_SCGC5 & SIM_SCGC5_LPTIMER ) SIM_SCGC5_clock_active = true; else SIM_SCGC5 |= SIM_SCGC5_LPTIMER; PSR = LPTMR0_PSR; CMR = LPTMR0_CMR; CSR = LPTMR0_CSR; #endif if ( pin == 11 ) { if ( mode >= LLS ) llwu_configure_modules_mask( LLWU_CMP0_MOD ); return_core_pin_config[0] = CORE_PIN11_CONFIG; CORE_PIN11_CONFIG = PORT_PCR_MUX( 0 ); _pin = 0x00; } else if ( pin == 4 ) { #if defined(KINETISK) if ( mode >= LLS ) llwu_configure_modules_mask( LLWU_CMP2_MOD ); return_core_pin_config[1] = CORE_PIN4_CONFIG; CORE_PIN4_CONFIG = PORT_PCR_MUX( 0 ); _pin = 0x01; #else return; #endif } else if ( pin == 9 ) { #if defined(KINETISK) if ( mode >= LLS ) llwu_configure_modules_mask( LLWU_CMP1_MOD ); return_core_pin_config[2] = CORE_PIN9_CONFIG; CORE_PIN9_CONFIG = PORT_PCR_MUX( 0 ); _pin = 0x01; #else return; #endif } // save if isr is already enabled and enable isr if not if ( mode == VLPW || mode == VLPS ) { IRQ_NUMBER_t IRQ_CMP; switch (pin) { case 11: IRQ_CMP = IRQ_CMP0; break; #if defined(KINETISK) case 9: IRQ_CMP = IRQ_CMP1; break; case 4: IRQ_CMP = IRQ_CMP2; break; #endif default: IRQ_CMP = IRQ_CMP; return; } return_isr_enabled = NVIC_IS_ENABLED( IRQ_CMP ); if ( return_isr_enabled == 0 ) NVIC_ENABLE_IRQ( IRQ_CMP ); } // setup compare *cmpx_cr0 = CMP_CR0_FILTER_CNT( 0x07 ); if ( type == CHANGE ) *cmpx_scr = CMP_SCR_CFF | CMP_SCR_CFR | CMP_SCR_IEF | CMP_SCR_IER; else if ( type == RISING || type == HIGH ) *cmpx_scr = CMP_SCR_CFF | CMP_SCR_CFR | CMP_SCR_IER; else if ( type == FALLING || type == LOW ) *cmpx_scr = CMP_SCR_CFF | CMP_SCR_CFR | CMP_SCR_IEF; else return; uint8_t tap = ( threshold_crossing/0.0515625 ) - 1; *cmpx_fpr = 0x00; *cmpx_muxcr = CMP_MUXCR_MSEL( 0x07 ) | CMP_MUXCR_PSEL( _pin ); *cmpx_daccr = CMP_DACCR_DACEN | CMP_DACCR_VRSEL | CMP_DACCR_VOSEL( tap ); #if defined(__MKL26Z64__) || defined(__MK66FX1M0__) // compare needs lptmr to operate in low power with LC, 3.6 *cmpx_cr1 = CMP_CR1_EN | CMP_CR1_TRIGM; SIM_SCGC5 |= SIM_SCGC5_LPTIMER; LPTMR0_CSR = 0; LPTMR0_PSR = LPTMR_PSR_PBYP | LPTMR_PSR_PCS( LPTMR_LPO );//LPO Clock LPTMR0_CMR = 1; LPTMR0_CSR = LPTMR_CSR_TEN | LPTMR_CSR_TCF; #else *cmpx_cr1 = CMP_CR1_EN; #endif }