ISR is not clearing a flag

Generic boards that are not Maple or Maple mini clones, and don't contain the additional USB reset hardware
Post Reply
Bobalong
Posts: 3
Joined: Sat Jun 20, 2015 3:29 am

ISR is not clearing a flag

Post by Bobalong » Sat Dec 29, 2018 1:28 pm

I'm using:
Arduino IDE with Roger Clarke's STM32duino
Blue Pill STM32F103C8T6
with I2C connection to an Arduino UNO.

The project is to use the STM32F103C8T6 as a keyboard scanner that I can customize.
I have use an SX1509, but it starts scanning again immediately after it is read which I do not want.

Code summary:
The code sets a 'waiting' flag and starts scanning the keypad
On return a signal is sent to a program that does something with the keystroke using I2C.
That program then requests (as master) the keystroke data.
The STM32's ISR sends the data, and resets the 'waiting' flag so the scan can resume.
Only 'waiting' never gets reset.
The I2C master receives the data requested, but only the first press is received since the scan is not restarted.
What am I missing? What more info is needed?

Thanks

Code: Select all

/* 
   Keypad scan
*/
#include 
#define LED      PC13
#define ROWS       8
#define COLS       6
#define DataGood   PB12
int      count = 0;
boolean  waiting;
int cols[COLS] = { PA6,  PB11, PB10, PB1,  PB0,  PA7,  };  
int rows[ROWS] = { PA5,  PA4,  PA3,  PA2,  PA1,  PA0,  PC15, PC14 };
uint8_t  x;
char     Msg[3] = "B ";
void Blink ( void )
{
    digitalWrite ( LED, LOW );   // low active
    delay ( 100 );
    digitalWrite ( LED, HIGH );
}
void GetKeyPress ( void ) 
{
    int      c, r, x;
    uint8_t  key;
    int      pressed = 0;
    boolean  found = false;
    for ( r = 0; r < ROWS; r++ )
        digitalWrite ( rows[r], HIGH );    // Start in known state
    while ( 1 )                       //   only exits on keypress
    {
        key = 0;
        for ( r = 0; r < ROWS; r++ )
        {
            digitalWrite ( rows[r], LOW );
            for ( c = 0; c < COLS; c++ )
            {
                key++;
                if ( digitalRead ( cols[c] ) == LOW )               // a key was pressed
                {
                    delay ( 5 );             // debounce
                    while ( digitalRead ( cols[c] ) == LOW ); // wait for release
                    delay ( 20 );             // debounce
                    found = true;
                    break;
                }
            }
            digitalWrite ( rows[r], HIGH );
            if ( found )
                break;
        }
        if ( found )
            break;
    }
    Msg[1] = key;
//    Serial1.print ( key );
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent ( )
{
    Wire.write ( Msg ); // respond with message of 2 bytes as expected by master
    digitalWrite ( DataGood, HIGH );
    waiting = false;    // resume scan          does not work
}
void setup ( )
{
    int   i;
//    Serial1.begin(9600);
    Wire.begin ( 0x13 );
    Wire.onRequest ( requestEvent ); // register event
    for ( i = 0; i < COLS; i++ )
    {
        pinMode ( cols[i], INPUT_PULLUP );
        delay ( 1 );
    }
    for ( i = 0; i < ROWS; i++ )
    {
        pinMode ( rows[i], OUTPUT );
        digitalWrite ( rows[i], HIGH );
        delay ( 1 );
    }
    pinMode ( DataGood, OUTPUT );
    digitalWrite ( DataGood, HIGH );
//    Serial1.println("  Starting...");
    pinMode ( LED, OUTPUT );
    digitalWrite ( LED, HIGH );    // low active
}
void loop()
{
    waiting = true;
    GetKeyPress ( );                    // blocking, key returned in Msg[1]
    digitalWrite ( DataGood, LOW );     // notify master that key has been pressed
    digitalWrite ( LED, LOW );           // turn on LED
    while ( waiting );                  // reset in requestEvent()
    digitalWrite ( DataGood, HIGH );    // low active         never gets here
    digitalWrite ( LED, HIGH );         // turn off LED
}

User avatar
Rick Kimball
Posts: 1400
Joined: Tue Apr 28, 2015 1:26 am
Location: Eastern NC, US
Contact:

Re: ISR is not clearing a flag

Post by Rick Kimball » Sat Dec 29, 2018 1:29 pm

volatile boolean waiting;
-rick

Bobalong
Posts: 3
Joined: Sat Jun 20, 2015 3:29 am

Re: ISR is not clearing a flag

Post by Bobalong » Sat Dec 29, 2018 2:55 pm

Thanks. I knew it would be something simple.

Post Reply