How to use 100us interval timer?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

I want to attach 2x2 LED matrix to a BCM20737, and implement dynamic lighting.

The following simple code works correctly using with bleapptimer_startFineTimer, but does not work using with hw_timer because the callback function is not called.


I want to use hw_timer because the function shiftRow must be called in 100us interval.

Let me know how to use 100us interval timer.


#define USE_HW_TIMER

// 2x2 LED matrix: 1 is on, 0 is off

static UINT8 matrix[2][2] = {

    { 1, 0 },

    { 0, 1 }

};

// GPIO pins connected to LED matrix

static int col_pins[2] = {2, 13};

static int row_pins[2] = {27, 25};

// row (anode) to light

static int prev_row;

static int next_row;

static void shiftRow() {

    // LOW: previous row (anode)

    int pin = row_pins[prev_row];

    gpio_setPinOutput(pin / 16, pin % 16, GPIO_PIN_OUTPUT_LOW);

    // LOW: cathode of current row

    int col;

    for (col = 0; col < 2; col++) {

        UINT8 val = matrix[next_row][col];

        pin = col_pins[col];

        gpio_setPinOutput(pin / 16, pin % 16, val ? GPIO_PIN_OUTPUT_LOW: GPIO_PIN_OUTPUT_HIGH);

    }

    // HIGH: current row

    pin = row_pins[next_row];

    gpio_setPinOutput(pin / 16, pin % 16, GPIO_PIN_OUTPUT_HIGH);

    prev_row = next_row;

    if (++next_row >= 2)

        next_row = 0;

   

#ifdef USE_HW_TIMER

    hw_timer_start(100);

#endif

}

static void test2x2() {

#if USE_HW_TIMER

    hw_timer_register_timer_expired_callback(shiftRow);

    hw_timer_start(100);

#else

    bleapptimer_startFineTimer(shiftRow, 1);

#endif

}

0 Likes
1 Solution
Anonymous
Not applicable

Hi moicci


As promised, we got the sample code for you.  However do keep in mind that 100us timer will cause you many problems. 


void app_register_callback_and_start_timer(void)

{

       // Register callback to be invoked when it times out.

       hw_timer_register_timer_expired_callback((HW_TIMER_EXPIRED_CALLBACK_FN)timer2_timeoutCallback);

       // Start the timer, set to expire in 100mS

       hw_timer_start(100000);

}

void timer2_timeoutCallback(void)

{

    // ISR context callback when timer expires. Serialize callback to app thread.

    bleappevt_serialize(appirtx_irTxTimerCallback, NULL);

}

int appirtx_irTxTimerCallback(void* unused)

{

    // Timer expired. Restart the HW timer to expire in 100ms.

    hw_timer_start(100000);

    return BLE_APP_EVENT_NO_ACTION;

}


Best,

Kevin

View solution in original post

11 Replies