/** ****************************************************************************** ** Callback function from timer module. Scan all buttons and debounce. Call ** callback function (if any). ** ** \param none ** ** \return none *****************************************************************************/ static void ScanTimerCallback(void) { uint_fast8_t fu8ButtonIndex; for (fu8ButtonIndex = 0; fu8ButtonIndex < BUTTON_COUNT; fu8ButtonIndex++) { en_button_state_t enSampledState; stc_button_control_t* pstcButton = &m_astcButtonCtrl[fu8ButtonIndex]; // Get current state of pin if ((PDR(pstcButton->u8PortNumber) & pstcButton->u8PinMask) == 0) { enSampledState = StateLow; pstcButton->u16stateRepeat++; if (pstcButton->u16stateRepeat > 50) { pstcButton->u16stateRepeat = 50; enSampledState = StateLong; } } else { enSampledState = StateHigh; pstcButton->u16stateRepeat = 0; } // If the sampled state is different to current state if ((enSampledState != pstcButton->enCurrState) || (pstcButton->u16stateRepeat == 50)) { // If debounce counter reached 0 if (pstcButton->enCurrState == StateLong) { pstcButton->enCurrState = enSampledState; return; } if (pstcButton->u8DebounceCounter == 0) { // Accept new state pstcButton->enCurrState = enSampledState; // Call callback function (if any) if (m_pfnCallback != NULL) { m_pfnCallback(pstcButton->u16ButtonId, pstcButton->enCurrState); } } else { // Count down pstcButton->u8DebounceCounter--; } } else { // state equal -> restart debounce counter pstcButton->u8DebounceCounter = BUTTON_DEBOUNCE_COUNT - 1; } } }
static void mb86s70_gpio_set(struct gpio_chip *gc, unsigned gpio, int value) { struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc); unsigned long flags; unsigned char val; spin_lock_irqsave(&gchip->lock, flags); val = readl(gchip->base + PDR(gpio)); if (value) val |= OFFSET(gpio); else val &= ~OFFSET(gpio); writel(val, gchip->base + PDR(gpio)); spin_unlock_irqrestore(&gchip->lock, flags); }
/** ****************************************************************************** ** Returns the current state of a button, independent of it's debounced state. ** ** \param u16ButtonId Button ID (BUTTON_ID_??) ** ** \return (StateLow, StateHigh, StateInvalid) *****************************************************************************/ en_button_state_t Button_GetCurrentButtonState(uint16_t u16ButtonId) { uint_fast8_t fu8ButtonIndex; for (fu8ButtonIndex = 0; fu8ButtonIndex < BUTTON_COUNT; fu8ButtonIndex++) { stc_button_control_t* pstcButton = &m_astcButtonCtrl[fu8ButtonIndex]; if (pstcButton->u16ButtonId == u16ButtonId) { // Get current state of pin if ((PDR(pstcButton->u8PortNumber) & pstcButton->u8PinMask) == 0) { return StateLow; } else { return StateHigh; } } } return StateInvalid; }
static int mb86s70_gpio_get(struct gpio_chip *gc, unsigned gpio) { struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc); return !!(readl(gchip->base + PDR(gpio)) & OFFSET(gpio)); }