Hi, I'm getting a weird problem with a button interrupt while using the RF1A module on a CC430F5137 chip, where the code inside the main loop only works 1 time and never again even though the ISR is being executing and flag (buttonPressed) are being set correctly. I can't find what's wrong and can't proceed to fix the problem.
Main.c
extern uint8_t buttonPressed;
uint8_t main(void) {
WDTCTL = (uint16_t)(WDTPW | WDTHOLD); /* Stop watch dog timer */
__disable_interrupt();
//ACLK = REFO 32768 Hz
//MCLK = SMCLK = 12 MHz
//UART 38400
hal_init430();
hal_initTimers(); //2Hz timer for alive-LED
uint8_t i;
for(i = 0; i < 4; i++) printf("Syncing UART\r\n\r\n"); //prep realTerm to display ascii characters
printf("Initializing RF core\r\n");
phy_reset(); //set voltage levels, write rf registers, etc.
phy_enableRX(); //go into RX mode
printf("RF Core initialized\r\n");
//main while loop
while(1) {
if(buttonPressed) {
__no_operation(); //for debugging
buttonPressed = 0;
printf("\r\nButton pressed! main-while-loop\r\n");
phy_transmit((uint8_t*)TxBuffer, sizeof TxBuffer);
}
__bis_SR_register(LPM3_bits + GIE);
}
return 0;
}
isr.c
extern uint8_t buttonPressed; //hal.c
/*************************************************************************************
* MSP430 Core Interrupts
*************************************************************************************/
#pragma vector=PORT1_VECTOR
__interrupt void PORT1_ISR(void) {
switch(__even_in_range(P1IV, 16))
{
case 0: break;
case 2: break; // P1.0 IFG
case 4: // P1.1 IFG
P1IFG &= ~BIT1;
__delay_cycles(24000); // 2ms debounce delay
//button is active low
if((P1IN & BIT1) == 0x00) {
//button is pressed down, transmit "Hello World"
printf("\r\nButton is pressed Line 39 isr.c\r\n");
buttonPressed = 1;
}
__bic_SR_register_on_exit(LPM3_bits); //Exit active
break;
case 6: break; // P1.2 IFG
case 8: break; // P1.3 IFG
case 10: break; // P1.4 IFG
case 12: break; // P1.5 IFG
case 14: break; // P1.6 IFG
case 16: // P1.7 IFG
break;
}
}
UART log for RF project
ðàððð øÀüüàÎÎæã§¸tœV Syncing UART Syncing UART Initializing RF core RF Core initialized Button is pressed Line 39 isr.c Button pressed! main-while-loop Starting transmission Transmitting using CCA 1. Strobe RX 2. Wait for RX mode to be entered 3. Wait ~500us for RSSI to be valid 4. Strobe TX 5. Check that the packet is being sent RFIFG9 Packet has been sent Button is pressed Line 39 isr.c Button is pressed Line 39 isr.c Button is pressed Line 39 isr.c Button is pressed Line 39 isr.c Button is pressed Line 39 isr.c Button is pressed Line 39 isr.c
I'm not sure what's wrong with the code. I made a clean MSP430-only project without the RF code and that one is working perfectly fine
main.c for MSP430-only
#include <msp430.h>
#include <stdio.h>
#include <stdint.h>
#define UART_PRINTF
uint8_t buttonPressed;
void hal_init430(void);
void hal_initGPIO(void);
void hal_initClocks(void);
void hal_initUART(void);
void hal_defaultGPIO(void);
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
hal_init430();
while(1) {
printf("Main while loop Line 21\r\n");
__bis_SR_register(LPM3_bits + GIE);
if(buttonPressed) {
P1OUT ^= BIT0;
printf("Button is pressed Line 24\r\n");
}
}
return 0;
}
void hal_init430(void) {
buttonPressed = 0;
hal_initGPIO();
hal_initClocks();
hal_initUART();
}
//gpio working
void hal_initGPIO(void) {
hal_defaultGPIO();
P1OUT &= ~BIT0;
P1DIR |= BIT0;
P1DIR &= ~BIT1;
/* internal pull-up resistor (active low) */
P1REN |= BIT1;
P1OUT |= BIT1;
/* clear existing interrupts if any */
P1IFG = 0;
/* interrupt on high-to-low edge */
P1IES |= BIT1;
P1IE |= BIT1;
}
//clock working
/**
* @brief Setting internal DCO to run at 12MHz.
* ACLK = LFXT1 = 32768 Hz, MCLK = SMCLK = DCO = (x+1)*REFO = 12000000Hz
* @param none
* @return none
*/
void hal_initClocks(void) {
PMAPPWD = 0x02D52;
P2MAP0 = PM_ACLK;
P2MAP2 = PM_MCLK;
P2MAP4 = PM_SMCLK;
PMAPPWD = 0;
P2DIR |= BIT0 + BIT2 + BIT4;
P2SEL |= BIT0 + BIT2 + BIT4;
UCSCTL0 = 0x00;
UCSCTL1 = DCORSEL_5;
UCSCTL2 = FLLD_1 | 0x16E;
UCSCTL3 = SELREF__REFOCLK;
/* Select REFO as ACLK source and DCOCLK as MCLK and SMCLK source */
UCSCTL4 = SELA__REFOCLK | SELS__DCOCLKDIV | SELM__DCOCLKDIV;
}
//uart working
void hal_initUART(void) {
P1DIR |= BIT6;
P1SEL |= BIT5 + BIT6;
UCA0CTL1 |= UCSWRST;
UCA0CTL1 |= UCSSEL_2;
// 12000000 Hz 38400 bps
UCA0BR0 = 0x38;
UCA0BR1 = 0x01;
UCA0MCTL = UCBRS_4 + UCBRF_0;
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt
}
void hal_defaultGPIO(void) {
#ifdef __MSP430_HAS_PORT1_R__
P1OUT = 0;
P1DIR |= 0xFF;
#endif
#ifdef __MSP430_HAS_PORT2_R__
P2OUT = 0;
P2DIR |= 0xFF;
#endif
#ifdef __MSP430_HAS_PORT3_R__
P3OUT = 0;
P3DIR |= 0xFF;
#endif
#ifdef __MSP430_HAS_PORT4_R__
P4OUT = 0;
P4DIR |= 0xFF;
#endif
#ifdef __MSP430_HAS_PORT5_R__
P5OUT = 0;
P5DIR |= 0xFF;
#endif
#ifdef __MSP430_HAS_PORT6_R__
P6OUT = 0;
P6DIR |= 0xFF;
#endif
#ifdef __MSP430_HAS_PORT7_R__
P7OUT = 0;
P7DIR |= 0xFF;
#endif
#ifdef __MSP430_HAS_PORT8_R__
P8OUT = 0;
P8DIR |= 0xFF;
#endif
#ifdef __MSP430_HAS_PORT9_R__
P9OUT = 0;
P9DIR |= 0xFF;
#endif
#ifdef __MSP430_HAS_PORTJ_R__
PJOUT = 0;
PJDIR |= 0xFF;
#endif
}
void hal_initTimers(void) {
//Physical layer testing - 2Hz timer
TA1CCTL0 = CCIE; // CCR0 interrupt enabled
TA1CCR0 = 16383; // CCR0 = 0.5 seconds * 32768 (ACLK Freq) - 1 = 16383
TA1CTL = TASSEL__ACLK + MC__UP + TACLR; // ACLK, upmode, clear TAR
}
#ifdef UART_PRINTF
int fputc(int _c, register FILE *_fp)
{
while(!(UCA0IFG&UCTXIFG));
UCA0TXBUF = (unsigned char) _c;
return((unsigned char)_c);
}
int fputs(const char *_ptr, register FILE *_fp)
{
unsigned int i, len;
len = strlen(_ptr);
for(i=0 ; i<len ; i++)
{
while(!(UCA0IFG&UCTXIFG));
UCA0TXBUF = (unsigned char) _ptr[i];
}
return len;
}
#endif
#pragma vector=PORT1_VECTOR
__interrupt void PORT1_ISR(void) {
switch(__even_in_range(P1IV, 16))
{
case 0: break;
case 2: break; // P1.0 IFG
case 4: // P1.1 IFG
P1IFG &= ~BIT1;
__delay_cycles(24000); // 2ms debounce delay
//button is active low
if((P1IN & BIT1) == 0x00) {
//button is pressed down, transmit "Hello World"
printf("Button is pressed Line 196\r\n");
buttonPressed = 1;
}
__bic_SR_register_on_exit(LPM3_bits); //Exit active
break;
case 6: break; // P1.2 IFG
case 8: break; // P1.3 IFG
case 10: break; // P1.4 IFG
case 12: break; // P1.5 IFG
case 14: break; // P1.6 IFG
case 16: // P1.7 IFG
break;
}
}
UART log for MSP430-only
Button is pressed Line 24 Main while loop Line 21 Button is pressed Line 196 Button is pressed Line 24 Main while loop Line 21 Button is pressed Line 196 Button is pressed Line 24 Main while loop Line 21 Button is pressed Line 196 Button is pressed Line 24 Main while loop Line 21 Button is pressed Line 196 Button is pressed Line 24 Main while loop Line 21 Button is pressed Line 196 Button is pressed Line 24 Main while loop Line 21
Thanks for your time.