10 years, 10 months ago.

Cant use USBKeyboard, from a interrupts/callback-function?

Hi, Im using Freescales KL25Z, and attempt to add USBKeyboard to a pushbutton. From the example here: http://mbed.org/users/4180_1/notebook/pushbuttons/ (Its example with interrupt/callback)

BUT, if I call USBKeyboard _putc() inside a callback-function, it hangs. (card stops blinking, and sends just 1 char very rapidly)

eew

#include "mbed.h"
#include "PinDetect.h"
#include "USBKeyboard.h"

USBKeyboard keyboard;

// must import Cookbook PinDetct library into project
// URL: http://mbed.org/users/AjK/libraries/PinDetect/lkyxpw
 
DigitalOut myled(LED_RED);
DigitalOut myled2(LED_GREEN);
DigitalOut myled3(LED_BLUE); 

PinDetect pb1(PTC12);
PinDetect pb2(PTA13);
// SPST Pushbutton debounced count demo using interrupts and callback
// no external PullUp resistor needed
// Pushbutton from P8 to GND.
// Second Pushbutton from P7 to GND.
// A pb hit generates an interrupt and activates the callback function
// after the switch is debounced
 
// Global count variable
int volatile count=0;

// Callback routine is interrupt activated by a debounced pb1 hit
void pb1_hit_callback (void) {
    count++;
    myled3 = count & 0x01;
    myled2 = (count & 0x02)>>1;
    keyboard._putc(44);    //THIS MESSES UP, dono why.
}
// Callback routine is interrupt activated by a debounced pb2 hit
void pb2_hit_callback (void) {
    count--;
    myled3 = count & 0x01;
    myled2 = (count & 0x02)>>1;
    keyboard._putc(45);  //SAME HERE
}

int main() {
    // Use internal pullups for pushbutton
    pb1.mode(PullUp);
    pb2.mode(PullUp);    
    // Delay for initial pullup to take effect
    wait(.01);
    // Setup Interrupt callback functions for a pb hit
    pb1.attach_deasserted(&pb1_hit_callback);
    pb2.attach_deasserted(&pb2_hit_callback);
    // Start sampling pb inputs using interrupts
    pb1.setSampleFrequency(100);
    pb2.setSampleFrequency(100);
    //Blink myled in main routine forever while responding to pb changes
    // via interrupts that activate the callback counter function
    while (1) {
/*        if ( myled3 )
            keyboard._putc(45);
        if ( myled2 )
            keyboard._putc(44); */ //Out here in the main, it does work.
        
        myled = !myled;
        wait(1);
    }
}

2 Answers

10 years, 10 months ago.

One possible reason (note the possible part, just some brainfarting) is that the putc waits until a USB interrupt is called, which cannot happen when you are in an interrupt. Options would then be not sending it from an interrupt (just setting a flag, and sending from your main loop, or from a seperate RTOS thread), or lowering the priority of the button interrupt.

Which brings us to the next question, how does that code do something besides flashing red LED? It tries to make an interruptin on port C, which should not support interrupts. Changing interrupt priority should be:

NVIC_SetPriority(PORTA_IRQn, 1);   //0 is highest and default
NVIC_SetPriority(PORTD_IRQn, 1);   //0 is highest and default

Accepted Answer

Hi.

I tested Eriks suggestion (NVIC_SetPriority..), no change.

Also I tested the "InterruptIn Class" (more strict mbed-class), than "PinDetect". BUT, same error (card stops blinking, and sends just 1 char very rapidly).

So, no input interupts is possible with the USBHID (and USBKeyboard etc).

posted by Per Blomqvist 17 Mar 2014
10 years, 10 months ago.

found a similar question: http://mbed.org/questions/1087/Need-help-regarding-using-USBKeyboard-wi/ (also unanswered)