/* ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ /** * @file hal_qspi.c * @brief QSPI Driver code. * * @addtogroup QSPI * @{ */ #include "hal.h" #if (HAL_USE_QSPI == TRUE) || defined(__DOXYGEN__) /*===========================================================================*/ /* Driver local definitions. */ /*===========================================================================*/ /*===========================================================================*/ /* Driver exported variables. */ /*===========================================================================*/ /*===========================================================================*/ /* Driver local variables and types. */ /*===========================================================================*/ /*===========================================================================*/ /* Driver local functions. */ /*===========================================================================*/ /*===========================================================================*/ /* Driver exported functions. */ /*===========================================================================*/ /** * @brief QSPI Driver initialization. * @note This function is implicitly invoked by @p halInit(), there is * no need to explicitly initialize the driver. * * @init */ void qspiInit(void) { qspi_lld_init(); } /** * @brief Initializes the standard part of a @p QSPIDriver structure. * * @param[out] qspip pointer to the @p QSPIDriver object * * @init */ void qspiObjectInit(QSPIDriver *qspip) { qspip->state = QSPI_STOP; qspip->config = NULL; #if QSPI_USE_WAIT == TRUE qspip->thread = NULL; #endif #if QSPI_USE_MUTUAL_EXCLUSION == TRUE osalMutexObjectInit(&qspip->mutex); #endif #if defined(QSPI_DRIVER_EXT_INIT_HOOK) QSPI_DRIVER_EXT_INIT_HOOK(qspip); #endif } /** * @brief Configures and activates the QSPI peripheral. * * @param[in] qspip pointer to the @p QSPIDriver object * @param[in] config pointer to the @p QSPIConfig object * * @api */ void qspiStart(QSPIDriver *qspip, const QSPIConfig *config) { osalDbgCheck((qspip != NULL) && (config != NULL)); osalSysLock(); osalDbgAssert((qspip->state == QSPI_STOP) || (qspip->state == QSPI_READY), "invalid state"); qspip->config = config; qspi_lld_start(qspip); qspip->state = QSPI_READY; osalSysUnlock(); } /** * @brief Deactivates the QSPI peripheral. * @note Deactivating the peripheral also enforces a release of the slave * select line. * * @param[in] qspip pointer to the @p QSPIDriver object * * @api */ void qspiStop(QSPIDriver *qspip) { osalDbgCheck(qspip != NULL); osalSysLock(); osalDbgAssert((qspip->state == QSPI_STOP) || (qspip->state == QSPI_READY), "invalid state"); qspi_lld_stop(qspip); qspip->config = NULL; qspip->state = QSPI_STOP; osalSysUnlock(); } /** * @brief Sends a command without data phase. * @post At the end of the operation the configured callback is invoked. * * @param[in] qspip pointer to the @p QSPIDriver object * @param[in] cmdp pointer to the command descriptor * * @api */ void qspiStartCommand(QSPIDriver *qspip, const qspi_command_t *cmdp) { osalDbgCheck((qspip != NULL) && (cmdp != NULL)); osalSysLock(); osalDbgAssert(qspip->state == QSPI_READY, "not ready"); qspiStartCommandI(qspip, cmdp); osalSysUnlock(); } /** * @brief Sends a command with data over the QSPI bus. * @post At the end of the operation the configured callback is invoked. * * @param[in] qspip pointer to the @p QSPIDriver object * @param[in] cmdp pointer to the command descriptor * @param[in] n number of bytes to send * @param[in] txbuf the pointer to the transmit buffer * * @api */ void qspiStartSend(QSPIDriver *qspip, const qspi_command_t *cmdp, size_t n, const uint8_t *txbuf) { osalDbgCheck((qspip != NULL) && (cmdp != NULL)); osalDbgCheck((n > 0U) && (txbuf != NULL)); osalSysLock(); osalDbgAssert(qspip->state == QSPI_READY, "not ready"); qspiStartSendI(qspip, cmdp, n, txbuf); osalSysUnlock(); } /** * @brief Sends a command then receives data over the QSPI bus. * @post At the end of the operation the configured callback is invoked. * * @param[in] qspip pointer to the @p QSPIDriver object * @param[in] cmdp pointer to the command descriptor * @param[in] n number of bytes to send * @param[out] rxbuf the pointer to the receive buffer * * @api */ void qspiStartReceive(QSPIDriver *qspip, const qspi_command_t *cmdp, size_t n, uint8_t *rxbuf) { osalDbgCheck((qspip != NULL) && (cmdp != NULL)); osalDbgCheck((n > 0U) && (rxbuf != NULL)); osalSysLock(); osalDbgAssert(qspip->state == QSPI_READY, "not ready"); qspiStartReceiveI(qspip, cmdp, n, rxbuf); osalSysUnlock(); } #if (QSPI_USE_WAIT == TRUE) || defined(__DOXYGEN__) /** * @brief Sends a command without data phase. * @pre In order to use this function the option @p QSPI_USE_WAIT must be * enabled. * @pre In order to use this function the driver must have been configured * without callbacks (@p end_cb = @p NULL). * * @param[in] qspip pointer to the @p QSPIDriver object * @param[in] cmdp pointer to the command descriptor * * @api */ void qspiCommand(QSPIDriver *qspip, const qspi_command_t *cmdp) { osalDbgCheck((qspip != NULL) && (cmdp != NULL)); osalDbgCheck((cmdp->cfg & QSPI_CFG_DATA_MODE_MASK) == QSPI_CFG_DATA_MODE_NONE); osalSysLock(); osalDbgAssert(qspip->state == QSPI_READY, "not ready"); osalDbgAssert(qspip->config->end_cb == NULL, "has callback"); qspiStartCommandI(qspip, cmdp); (void) osalThreadSuspendS(&qspip->thread); osalSysUnlock(); } /** * @brief Sends a command with data over the QSPI bus. * @pre In order to use this function the option @p QSPI_USE_WAIT must be * enabled. * @pre In order to use this function the driver must have been configured * without callbacks (@p end_cb = @p NULL). * * @param[in] qspip pointer to the @p QSPIDriver object * @param[in] cmdp pointer to the command descriptor * @param[in] n number of bytes to send * @param[in] txbuf the pointer to the transmit buffer * * @api */ void qspiSend(QSPIDriver *qspip, const qspi_command_t *cmdp, size_t n, const uint8_t *txbuf) { osalDbgCheck((qspip != NULL) && (cmdp != NULL)); osalDbgCheck((n > 0U) && (txbuf != NULL)); osalDbgCheck((cmdp->cfg & QSPI_CFG_DATA_MODE_MASK) != QSPI_CFG_DATA_MODE_NONE); osalSysLock(); osalDbgAssert(qspip->state == QSPI_READY, "not ready"); osalDbgAssert(qspip->config->end_cb == NULL, "has callback"); qspiStartSendI(qspip, cmdp, n, txbuf); (void) osalThreadSuspendS(&qspip->thread); osalSysUnlock(); } /** * @brief Sends a command then receives data over the QSPI bus. * @pre In order to use this function the option @p QSPI_USE_WAIT must be * enabled. * @pre In order to use this function the driver must have been configured * without callbacks (@p end_cb = @p NULL). * * @param[in] qspip pointer to the @p QSPIDriver object * @param[in] cmdp pointer to the c
/*
Copyright 2013 Mathias Andersson <wraul@dbox.se>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "backlight.h"
#include "eeconfig.h"
#include "debug.h"
backlight_config_t backlight_config;
/** \brief Backlight initialization
*
* FIXME: needs doc
*/
void backlight_init(void)
{
/* check signature */
if (!eeconfig_is_enabled()) {
eeconfig_init();
}
backlight_config.raw = eeconfig_read_backlight();
if (backlight_config.level > BACKLIGHT_LEVELS) {
backlight_config.level = BACKLIGHT_LEVELS;
}
backlight_set(backlight_config.enable ? backlight_config.level : 0);
}
/** \brief Backlight increase
*
* FIXME: needs doc
*/
void backlight_increase(void)
{
if(backlight_config.level < BACKLIGHT_LEVELS)
{
backlight_config.level++;
}
backlight_config.enable = 1;
eeconfig_update_backlight(backlight_config.raw);
dprintf("backlight increase: %u\n", backlight_config.level);
backlight_set(backlight_config.level);
}
/** \brief Backlight decrease
*
* FIXME: needs doc
*/
void backlight_decrease(void)
{
if(backlight_config.level > 0)
{
backlight_config.level--;
backlight_config.enable = !!backlight_config.level;
eeconfig_update_backlight(backlight_config.raw);
}
dprintf("backlight decrease: %u\n", backlight_config.level);
backlight_set(backlight_config.level);
}
/** \brief Backlight toggle
*
* FIXME: needs doc
*/
void backlight_toggle(void)
{
bool enabled = backlight_config.enable;
dprintf("backlight toggle: %u\n", enabled);
if (enabled)
backlight_disable();
else
backlight_enable();
}
/** \brief Enable backlight
*
* FIXME: needs doc
*/
void backlight_enable(void)
{
if (backlight_config.enable) return; // do nothing if backlight is already on
backlight_config.enable = true;
if (backlight_config.raw == 1) // enabled but level == 0
backlight_config.level = 1;
eeconfig_update_backlight(backlight_config.raw);
dprintf("backlight enable\n");
backlight_set(backlight_config.level);
}
/** /brief Disable backlight
*
* FIXME: needs doc
*/
void backlight_disable(void)
{
if (!backlight_config.enable) return; // do nothing if backlight is already off
backlight_config.enable = false;
eeconfig_update_backlight(backlight_config.raw);
dprintf("backlight disable\n");
backlight_set(0);
}
/** /brief Get the backlight status
*
* FIXME: needs doc
*/
bool is_backlight_enabled(void)
{
return backlight_config.enable;
}
/** \brief Backlight step through levels
*
* FIXME: needs doc
*/
void backlight_step(void)
{
backlight_config.level++;
if(backlight_config.level > BACKLIGHT_LEVELS)
{
backlight_config.level = 0;
}
backlight_config.enable = !!backlight_config.level;
eeconfig_update_backlight(backlight_config.raw);
dprintf("backlight step: %u\n", backlight_config.level);
backlight_set(backlight_config.level);
}
/** \brief Backlight set level
*
* FIXME: needs doc
*/
void backlight_level(uint8_t level)
{
if (level > BACKLIGHT_LEVELS)
level = BACKLIGHT_LEVELS;
backlight_config.level = level;
backlight_config.enable = !!backlight_config.level;
eeconfig_update_backlight(backlight_config.raw);
backlight_set(backlight_config.level);
}
/** \brief Get backlight level
*
* FIXME: needs doc
*/
uint8_t get_backlight_level(void)
{
return backlight_config.level;
}