6 years, 11 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)


#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) {
    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) {
    myled3 = count & 0x01;
    myled2 = (count & 0x02)>>1;
    keyboard._putc(45);  //SAME HERE

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

2 Answers

6 years, 11 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


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
6 years, 11 months ago.

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