diff options
Diffstat (limited to 'os/various')
-rw-r--r-- | os/various/bitmap.c | 158 | ||||
-rw-r--r-- | os/various/bitmap.h | 77 | ||||
-rw-r--r-- | os/various/devices_lib/lcd/ili9341.c | 418 | ||||
-rw-r--r-- | os/various/devices_lib/lcd/ili9341.h | 593 | ||||
-rw-r--r-- | os/various/memtest.cpp | 286 | ||||
-rw-r--r-- | os/various/memtest.hpp | 73 | ||||
-rw-r--r-- | os/various/tribuf.c | 214 | ||||
-rw-r--r-- | os/various/tribuf.h | 225 |
8 files changed, 2044 insertions, 0 deletions
diff --git a/os/various/bitmap.c b/os/various/bitmap.c new file mode 100644 index 0000000..a17dfcb --- /dev/null +++ b/os/various/bitmap.c @@ -0,0 +1,158 @@ +/* + ChibiOS/HAL - Copyright (C) 2015 Uladzimir Pylinsky aka barthess + + 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 bitmap.c + * @brief Bit map code. + * + * @addtogroup bitmap + * @{ + */ + +#include "string.h" /* for memset() */ + +#include "hal.h" +#include "bitmap.h" + +/*===========================================================================*/ +/* Module local definitions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module exported variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local functions. */ +/*===========================================================================*/ + +/** + * @brief Get word number. + * + * @param[in] bit number of the bit + * + * @return Index of the word containing specified bit. + */ +static inline size_t word(size_t bit) { + return bit / (sizeof(bitmap_word_t) * 8); +} + +/** + * @brief Get bit position in word. + * + * @param[in] bit number of the bit + * + * @return Position of the specified bit related to word start. + */ +static inline size_t pos_in_word(size_t bit) { + return bit % (sizeof(bitmap_word_t) * 8); +} + +/*===========================================================================*/ +/* Module exported functions. */ +/*===========================================================================*/ +/** + * @brief Initializes an @p bitmap_t structure. + * + * @param[out] map the @p bitmap_t structure to be initialized + * @param[in] val the value to be written in all bitmap + */ +void bitmapObjectInit(bitmap_t *map, bitmap_word_t val) { + uint8_t pattern; + + osalDbgCheck(val == 1 || val == 0); + + if (val == 1) + pattern = 0xFF; + else + pattern = 0; + + memset(map->array, pattern, map->len*sizeof(bitmap_word_t)); +} + +/** + * @brief Set single bit in an @p bitmap_t structure. + * + * @param[out] map the @p bitmap_t structure + * @param[in] bit number of the bit to be set + */ +void bitmapSet(bitmap_t *map, size_t bit) { + size_t w = word(bit); + + osalDbgCheck(w < map->len); + map->array[w] |= (bitmap_word_t)1 << pos_in_word(bit); +} + +/** + * @brief Clear single bit in an @p bitmap_t structure. + * + * @param[out] map the @p bitmap_t structure + * @param[in] bit number of the bit to be cleared + */ +void bitmapClear(bitmap_t *map, size_t bit) { + size_t w = word(bit); + + osalDbgCheck(w < map->len); + map->array[w] &= ~((bitmap_word_t)1 << pos_in_word(bit)); +} + +/** + * @brief Invert single bit in an @p bitmap_t structure. + * + * @param[out] map the @p bitmap_t structure + * @param[in] bit number of the bit to be inverted + */ +void bitmapInvert(bitmap_t *map, size_t bit) { + size_t w = word(bit); + + osalDbgCheck(w < map->len); + map->array[w] ^= (bitmap_word_t)1 << pos_in_word(bit); +} + +/** + * @brief Get bit value from an @p bitmap_t structure. + * + * @param[in] map the @p bitmap_t structure + * @param[in] bit number of the requested bit + * + * @return Requested bit value. + */ +bitmap_word_t bitmapGet(const bitmap_t *map, size_t bit) { + size_t w = word(bit); + + osalDbgCheck(w < map->len); + return (map->array[w] >> pos_in_word(bit)) & 1; +} + +/** + * @brief Get total amount of bits in an @p bitmap_t structure. + * + * @param[in] map the @p bitmap_t structure + * + * @return Bit number. + */ +size_t bitmapGetBitsCount(const bitmap_t *map) { + return map->len * sizeof(bitmap_word_t) * 8; +} +/** @} */ diff --git a/os/various/bitmap.h b/os/various/bitmap.h new file mode 100644 index 0000000..d7831aa --- /dev/null +++ b/os/various/bitmap.h @@ -0,0 +1,77 @@ +/* + ChibiOS/HAL - Copyright (C) 2015 Uladzimir Pylinsky aka barthess + + 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 bitmap.h + * @brief Bit map structures and macros. + * + * @addtogroup bitmap + * @{ + */ + +#ifndef _BITMAP_H_ +#define _BITMAP_H_ + +/*===========================================================================*/ +/* Module constants. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module pre-compile time settings. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Derived constants and error checks. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module data structures and types. */ +/*===========================================================================*/ + +typedef unsigned int bitmap_word_t; + +/** + * @brief Type of a event timer structure. + */ +typedef struct { + bitmap_word_t *array; + size_t len; /* Array length in _words_ NOT bytes */ +} bitmap_t; + +/*===========================================================================*/ +/* Module macros. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* External declarations. */ +/*===========================================================================*/ + +#ifdef __cplusplus +extern "C" { +#endif + void bitmapObjectInit(bitmap_t *map, bitmap_word_t val); + void bitmapSet(bitmap_t *map, size_t bit); + void bitmapClear(bitmap_t *map, size_t bit); + void bitmapInvert(bitmap_t *map, size_t bit); + bitmap_word_t bitmapGet(const bitmap_t *map, size_t bit); + size_t bitmapGetBitsCount(const bitmap_t *map); +#ifdef __cplusplus +} +#endif + +#endif /* _BITMAP_H_ */ + +/** @} */ diff --git a/os/various/devices_lib/lcd/ili9341.c b/os/various/devices_lib/lcd/ili9341.c new file mode 100644 index 0000000..979e502 --- /dev/null +++ b/os/various/devices_lib/lcd/ili9341.c @@ -0,0 +1,418 @@ +/* + Copyright (C) 2013-2015 Andrea Zoppi + + 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 ili9341.c + * @brief ILI9341 TFT LCD diaplay controller driver. + * @note Does not support multiple calling threads natively. + */ + +#include "ch.h" +#include "hal.h" +#include "ili9341.h" + +/** + * @addtogroup ili9341 + * @{ + */ + +/*===========================================================================*/ +/* Driver local definitions. */ +/*===========================================================================*/ + +#if !ILI9341_USE_CHECKS && !defined(__DOXYGEN__) +/* Disable checks as needed.*/ + +#ifdef osalDbgCheck +#undef osalDbgCheck +#endif +#define osalDbgCheck(c, func) { \ + (void)(c), (void)__QUOTE_THIS(func)"()"; \ +} + +#ifdef osalDbgAssert +#undef osalDbgAssert +#endif +#define osalDbgAssert(c, m, r) { \ + (void)(c); \ +} + +#ifdef osalDbgCheckClassS +#undef osalDbgCheckClassS +#endif +#define osalDbgCheckClassS() {} + +#ifdef osalDbgCheckClassS +#undef osalDbgCheckClassS +#endif +#define osalDbgCheckClassI() {} + +#endif /* ILI9341_USE_CHECKS */ + +/*===========================================================================*/ +/* Driver exported variables. */ +/*===========================================================================*/ + +/** @brief ILI9341D1 driver identifier.*/ +ILI9341Driver ILI9341D1; + +/*===========================================================================*/ +/* Driver local variables and types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver local functions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver exported functions. */ +/*===========================================================================*/ + +/** + * @brief Initializes the standard part of a @p ILI9341Driver structure. + * + * @param[out] driverp pointer to the @p ILI9341Driver object + * + * @init + */ +void ili9341ObjectInit(ILI9341Driver *driverp) { + + osalDbgCheck(driverp != NULL); + + driverp->state = ILI9341_STOP; + driverp->config = NULL; +#if (TRUE == ILI9341_USE_MUTUAL_EXCLUSION) +#if (TRUE == CH_CFG_USE_MUTEXES) + chMtxObjectInit(&driverp->lock); +#else + chSemObjectInit(&driverp->lock, 1); +#endif +#endif /* (TRUE == ILI9341_USE_MUTUAL_EXCLUSION) */ +} + +/** + * @brief Configures and activates the ILI9341 peripheral. + * @pre ILI9341 is stopped. + * + * @param[in] driverp pointer to the @p ILI9341Driver object + * @param[in] configp pointer to the @p ILI9341Config object + * + * @api + */ +void ili9341Start(ILI9341Driver *driverp, const ILI9341Config *configp) { + + chSysLock(); + osalDbgCheck(driverp != NULL); + osalDbgCheck(configp != NULL); + osalDbgCheck(configp->spi != NULL); + osalDbgAssert(driverp->state == ILI9341_STOP, "invalid state"); + + spiSelectI(configp->spi); + spiUnselectI(configp->spi); + driverp->config = configp; + driverp->state = ILI9341_READY; + chSysUnlock(); +} + +/** + * @brief Deactivates the ILI9341 peripheral. + * @pre ILI9341 is ready. + * + * @param[in] driverp pointer to the @p ILI9341Driver object + * + * @api + */ +void ili9341Stop(ILI9341Driver *driverp) { + + chSysLock(); + osalDbgCheck(driverp != NULL); + osalDbgAssert(driverp->state == ILI9341_READY, "invalid state"); + + driverp->state = ILI9341_STOP; + chSysUnlock(); +} + +#if ILI9341_USE_MUTUAL_EXCLUSION + +/** + * @brief Gains exclusive access to the ILI9341 module. + * @details This function tries to gain ownership to the ILI9341 module, if the + * module is already being used then the invoking thread is queued. + * @pre In order to use this function the option + * @p ILI9341_USE_MUTUAL_EXCLUSION must be enabled. + * @pre ILI9341 is ready. + * + * @param[in] driverp pointer to the @p ILI9341Driver object + * + * @sclass + */ +void ili9341AcquireBusS(ILI9341Driver *driverp) { + + osalDbgCheckClassS(); + osalDbgCheck(driverp == &ILI9341D1); + osalDbgAssert(driverp->state == ILI9341_READY, "not ready"); + +#if (TRUE == CH_CFG_USE_MUTEXES) + chMtxLockS(&driverp->lock); +#else + chSemWaitS(&driverp->lock); +#endif +} + +/** + * @brief Gains exclusive access to the ILI9341 module. + * @details This function tries to gain ownership to the ILI9341 module, if the + * module is already being used then the invoking thread is queued. + * @pre In order to use this function the option + * @p ILI9341_USE_MUTUAL_EXCLUSION must be enabled. + * @pre ILI9341 is ready. + * + * @param[in] driverp pointer to the @p ILI9341Driver object + * + * @api + */ +void ili9341AcquireBus(ILI9341Driver *driverp) { + + chSysLock(); + ili9341AcquireBusS(driverp); + chSysUnlock(); +} + +/** + * @brief Releases exclusive access to the ILI9341 module. + * @pre In order to use this function the option + * @p ILI9341_USE_MUTUAL_EXCLUSION must be enabled. + * @pre ILI9341 is ready. + * + * @param[in] driverp pointer to the @p ILI9341Driver object + * + * @sclass + */ +void ili9341ReleaseBusS(ILI9341Driver *driverp) { + + osalDbgCheckClassS(); + osalDbgCheck(driverp == &ILI9341D1); + osalDbgAssert(driverp->state == ILI9341_READY, "not ready"); + +#if (TRUE == CH_CFG_USE_MUTEXES) + chMtxUnlockS(&driverp->lock); +#else + chSemSignalI(&driverp->lock); +#endif +} + +/** + * @brief Releases exclusive access to the ILI9341 module. + * @pre In order to use this function the option + * @p ILI9341_USE_MUTUAL_EXCLUSION must be enabled. + * @pre ILI9341 is ready. + * + * @param[in] driverp pointer to the @p ILI9341Driver object + * + * @api + */ +void ili9341ReleaseBus(ILI9341Driver *driverp) { + + chSysLock(); + ili9341ReleaseBusS(driverp); + chSysUnlock(); +} + +#endif /* ILI9341_USE_MUTUAL_EXCLUSION */ + +#if ILI9341_IM == ILI9341_IM_4LSI_1 /* 4-wire, half-duplex */ + +/** + * @brief Asserts the slave select signal and prepares for transfers. + * @pre ILI9341 is ready. + * + * @param[in] driverp pointer to the @p ILI9341Driver object + * + * @iclass + */ +void ili9341SelectI(ILI9341Driver *driverp) { + + osalDbgCheckClassI(); + osalDbgCheck(driverp != NULL); + osalDbgAssert(driverp->state == ILI9341_READY, "invalid state"); + + driverp->state = ILI9341_ACTIVE; + spiSelectI(driverp->config->spi); +} + +/** + * @brief Asserts the slave select signal and prepares for transfers. + * @pre ILI9341 is ready. + * + * @param[in] driverp pointer to the @p ILI9341Driver object + * + * @api + */ +void ili9341Select(ILI9341Driver *driverp) { + + chSysLock(); + ili9341SelectI(driverp); + chSysUnlock(); +} + +/** + * @brief Deasserts the slave select signal. + * @details The previously selected peripheral is unselected. + * @pre ILI9341 is active. + * + * @param[in] driverp pointer to the @p ILI9341Driver object + * + * @iclass + */ +void ili9341UnselectI(ILI9341Driver *driverp) { + + osalDbgCheckClassI(); + osalDbgCheck(driverp != NULL); + osalDbgAssert(driverp->state == ILI9341_ACTIVE, "invalid state"); + + spiUnselectI(driverp->config->spi); + driverp->state = ILI9341_READY; +} + +/** + * @brief Deasserts the slave select signal. + * @details The previously selected peripheral is unselected. + * @pre ILI9341 is active. + * + * @param[in] driverp pointer to the @p ILI9341Driver object + * + * @iclass + */ +void ili9341Unselect(ILI9341Driver *driverp) { + + chSysLock(); + ili9341UnselectI(driverp); + chSysUnlock(); +} + +/** + * @brief Write command byte. + * @details Sends a command byte via SPI. + * + * @param[in] driverp pointer to the @p ILI9341Driver object + * @param[in] cmd command byte + * + * @api + */ +void ili9341WriteCommand(ILI9341Driver *driverp, uint8_t cmd) { + + osalDbgCheck(driverp != NULL); + osalDbgAssert(driverp->state == ILI9341_ACTIVE, "invalid state"); + + driverp->value = cmd; + palClearPad(driverp->config->dcx_port, driverp->config->dcx_pad); /* !Cmd */ + spiSend(driverp->config->spi, 1, &driverp->value); +} + +/** + * @brief Write data byte. + * @details Sends a data byte via SPI. + * + * @param[in] driverp pointer to the @p ILI9341Driver object + * @param[in] value data byte + * + * @api + */ +void ili9341WriteByte(ILI9341Driver *driverp, uint8_t value) { + + osalDbgCheck(driverp != NULL); + osalDbgAssert(driverp->state == ILI9341_ACTIVE, "invalid state"); + + driverp->value = value; + palSetPad(driverp->config->dcx_port, driverp->config->dcx_pad); /* Data */ + spiSend(driverp->config->spi, 1, &driverp->value); +} + +/** + * @brief Read data byte. + * @details Receives a data byte via SPI. + * + * @param[in] driverp pointer to the @p ILI9341Driver object + * + * @return data byte + * + * @api + */ +uint8_t ili9341ReadByte(ILI9341Driver *driverp) { + + osalDbgAssert(FALSE, "should not be used"); + + osalDbgCheck(driverp != NULL); + osalDbgAssert(driverp->state == ILI9341_ACTIVE, "invalid state"); + + palSetPad(driverp->config->dcx_port, driverp->config->dcx_pad); /* Data */ + spiReceive(driverp->config->spi, 1, &driverp->value); + return driverp->value; +} + +/** + * @brief Write data chunk. + * @details Sends a data chunk via SPI. + * @pre The chunk must be accessed by DMA. + * + * @param[in] driverp pointer to the @p ILI9341Driver object + * @param[in] chunk chunk bytes + * @param[in] length chunk length + * + * @api + */ +void ili9341WriteChunk(ILI9341Driver *driverp, const uint8_t chunk[], + size_t length) { + + osalDbgCheck(driverp != NULL); + osalDbgCheck(chunk != NULL); + osalDbgAssert(driverp->state == ILI9341_ACTIVE, "invalid state"); + + if (length != 0) { + palSetPad(driverp->config->dcx_port, driverp->config->dcx_pad); /* Data */ + spiSend(driverp->config->spi, length, chunk); + } +} + +/** + * @brief Read data chunk. + * @details Receives a data chunk via SPI. + * @pre The chunk must be accessed by DMA. + * + * @param[in] driverp pointer to the @p ILI9341Driver object + * @param[out] chunk chunk bytes + * @param[in] length chunk length + * + * @api + */ +void ili9341ReadChunk(ILI9341Driver *driverp, uint8_t chunk[], + size_t length) { + + osalDbgCheck(driverp != NULL); + osalDbgCheck(chunk != NULL); + osalDbgAssert(driverp->state == ILI9341_ACTIVE, "invalid state"); + + if (length != 0) { + palSetPad(driverp->config->dcx_port, driverp->config->dcx_pad); /* Data */ + spiReceive(driverp->config->spi, length, chunk); + } +} + +#else /* ILI9341_IM == * */ +#error "Only the ILI9341_IM_4LSI_1 interface mode is currently supported" +#endif /* ILI9341_IM == * */ + +/** @} */ diff --git a/os/various/devices_lib/lcd/ili9341.h b/os/various/devices_lib/lcd/ili9341.h new file mode 100644 index 0000000..007c4fd --- /dev/null +++ b/os/various/devices_lib/lcd/ili9341.h @@ -0,0 +1,593 @@ +/* + Copyright (C) 2013-2015 Andrea Zoppi + + 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 ili9341.h + * @brief ILI9341 TFT LCD diaplay controller driver. + */ + +#ifndef _ILI9341_H_ +#define _ILI9341_H_ + +/** + * @addtogroup ili9341 + * @{ + */ + +/*===========================================================================*/ +/* Driver constants. */ +/*===========================================================================*/ + +/** + * @name ILI9341 regulative commands + * @{ + */ +#define ILI9341_CMD_NOP (0x00) /**< No operation.*/ +#define ILI9341_CMD_RESET (0x01) /**< Software reset.*/ +#define ILI9341_GET_ID_INFO (0x04) /**< Get ID information.*/ +#define ILI9341_GET_STATUS (0x09) /**< Get status.*/ +#define ILI9341_GET_PWR_MODE (0x0A) /**< Get power mode.*/ +#define ILI9341_GET_MADCTL (0x0B) /**< Get MADCTL.*/ +#define ILI9341_GET_PIX_FMT (0x0C) /**< Get pixel format.*/ +#define ILI9341_GET_IMG_FMT (0x0D) /**< Get image format.*/ +#define ILI9341_GET_SIG_MODE (0x0E) /**< Get signal mode.*/ +#define ILI9341_GET_SELF_DIAG (0x0F) /**< Get self-diagnostics.*/ +#define ILI9341_CMD_SLEEP_ON (0x10) /**< Enter sleep mode.*/ +#define ILI9341_CMD_SLEEP_OFF (0x11) /**< Exist sleep mode.*/ +#define ILI9341_CMD_PARTIAL_ON (0x12) /**< Enter partial mode.*/ +#define ILI9341_CMD_PARTIAL_OFF (0x13) /**< Exit partial mode.*/ +#define ILI9341_CMD_INVERT_ON (0x20) /**< Enter inverted mode.*/ +#define ILI9341_CMD_INVERT_OFF (0x21) /**< Exit inverted mode.*/ +#define ILI9341_SET_GAMMA (0x26) /**< Set gamma params.*/ +#define ILI9341_CMD_DISPLAY_OFF (0x28) /**< Disable display.*/ +#define ILI9341_CMD_DISPLAY_ON (0x29) /**< Enable display.*/ +#define ILI9341_SET_COL_ADDR (0x2A) /**< Set column address.*/ +#define ILI9341_SET_PAGE_ADDR (0x2B) /**< Set page address.*/ +#define ILI9341_SET_MEM (0x2C) /**< Set memory.*/ +#define ILI9341_SET_COLOR (0x2D) /**< Set color.*/ +#define ILI9341_GET_MEM (0x2E) /**< Get memory.*/ +#define ILI9341_SET_PARTIAL_AREA (0x30) /**< Set partial area.*/ +#define ILI9341_SET_VSCROLL (0x33) /**< Set vertical scroll def.*/ +#define ILI9341_CMD_TEARING_ON (0x34) /**< Tearing line enabled.*/ +#define ILI9341_CMD_TEARING_OFF (0x35) /**< Tearing line disabled.*/ +#define ILI9341_SET_MEM_ACS_CTL (0x36) /**< Set mem access ctl.*/ +#define ILI9341_SET_VSCROLL_ADDR (0x37) /**< Set vscroll start addr.*/ +#define ILI9341_CMD_IDLE_OFF (0x38) /**< Exit idle mode.*/ +#define ILI9341_CMD_IDLE_ON (0x39) /**< Enter idle mode.*/ +#define ILI9341_SET_PIX_FMT (0x3A) /**< Set pixel format.*/ +#define ILI9341_SET_MEM_CONT (0x3C) /**< Set memory continue.*/ +#define ILI9341_GET_MEM_CONT (0x3E) /**< Get memory continue.*/ +#define ILI9341_SET_TEAR_SCANLINE (0x44) /**< Set tearing scanline.*/ +#define ILI9341_GET_TEAR_SCANLINE (0x45) /**< Get tearing scanline.*/ +#define ILI9341_SET_BRIGHTNESS (0x51) /**< Set brightness.*/ +#define ILI9341_GET_BRIGHTNESS (0x52) /**< Get brightness.*/ +#define ILI9341_SET_DISPLAY_CTL (0x53) /**< Set display ctl.*/ +#define ILI9341_GET_DISPLAY_CTL (0x54) /**< Get display ctl.*/ +#define ILI9341_SET_CABC (0x55) /**< Set CABC.*/ +#define ILI9341_GET_CABC (0x56) /**< Get CABC.*/ +#define ILI9341_SET_CABC_MIN (0x5E) /**< Set CABC min.*/ +#define ILI9341_GET_CABC_MIN (0x5F) /**< Set CABC max.*/ +#define ILI9341_GET_ID1 (0xDA) /**< Get ID1.*/ +#define ILI9341_GET_ID2 (0xDB) /**< Get ID2.*/ +#define ILI9341_GET_ID3 (0xDC) /**< Get ID3.*/ +/** @} */ + +/** + * @name ILI9341 extended commands + * @{ + */ +#define ILI9341_SET_RGB_IF_SIG_CTL (0xB0) /**< RGB IF signal ctl.*/ +#define ILI9341_SET_FRAME_CTL_NORMAL (0xB1) /**< Set frame ctl (normal).*/ +#define ILI9341_SET_FRAME_CTL_IDLE (0xB2) /**< Set frame ctl (idle).*/ +#define ILI9341_SET_FRAME_CTL_PARTIAL (0xB3) /**< Set frame ctl (partial).*/ +#define ILI9341_SET_INVERSION_CTL (0xB4) /**< Set inversion ctl.*/ +#define ILI9341_SET_BLANKING_PORCH_CTL (0xB5) /**< Set blanking porch ctl.*/ +#define ILI9341_SET_FUNCTION_CTL (0xB6) /**< Set function ctl.*/ +#define ILI9341_SET_ENTRY_MODE (0xB7) /**< Set entry mode.*/ +#define ILI9341_SET_LIGHT_CTL_1 (0xB8) /**< Set backlight ctl 1.*/ +#define ILI9341_SET_LIGHT_CTL_2 (0xB9) /**< Set backlight ctl 2.*/ +#define ILI9341_SET_LIGHT_CTL_3 (0xBA) /**< Set backlight ctl 3.*/ +#define ILI9341_SET_LIGHT_CTL_4 (0xBB) /**< Set backlight ctl 4.*/ +#define ILI9341_SET_LIGHT_CTL_5 (0xBC) /**< Set backlight ctl 5.*/ +#define ILI9341_SET_LIGHT_CTL_7 (0xBE) /**< Set backlight ctl 7.*/ +#define ILI9341_SET_LIGHT_CTL_8 (0xBF) /**< Set backlight ctl 8.*/ +#define ILI9341_SET_POWER_CTL_1 (0xC0) /**< Set power ctl 1.*/ +#define ILI9341_SET_POWER_CTL_2 (0xC1) /**< Set power ctl 2.*/ +#define ILI9341_SET_VCOM_CTL_1 (0xC5) /**< Set VCOM ctl 1.*/ +#define ILI9341_SET_VCOM_CTL_2 (0xC6) /**< Set VCOM ctl 2.*/ +#define ILI9341_SET_NVMEM (0xD0) /**< Set NVMEM data.*/ +#define ILI9341_GET_NVMEM_KEY (0xD1) /**< Get NVMEM protect key.*/ +#define ILI9341_GET_NVMEM_STATUS (0xD2) /**< Get NVMEM status.*/ +#define ILI9341_GET_ID4 (0xD3) /**< Get ID4.*/ +#define ILI9341_SET_PGAMMA (0xE0) /**< Set positive gamma.*/ +#define ILI9341_SET_NGAMMA (0xE1) /**< Set negative gamma.*/ +#define ILI9341_SET_DGAMMA_CTL_1 (0xE2) /**< Set digital gamma ctl 1.*/ +#define ILI9341_SET_DGAMMA_CTL_2 (0xE3) /**< Set digital gamma ctl 2.*/ +#define ILI9341_SET_IF_CTL (0xF6) /**< Set interface control.*/ +/** @} */ + +/** + * @name ILI9341 interface modes + * @{ + */ +#define ILI9341_IM_3LSI_1 (0x5) /**< 3-line serial, mode 1.*/ +#define ILI9341_IM_3LSI_2 (0xD) /**< 3-line serial, mode 2.*/ +#define ILI9341_IM_4LSI_1 (0x6) /**< 4-line serial, mode 1.*/ +#define ILI9341_IM_4LSI_2 (0xE) /**< 4-line serial, mode 2.*/ +/** @} */ + +/*===========================================================================*/ +/* Driver pre-compile time settings. */ +/*===========================================================================*/ + +/** + * @name ILI9341 configuration options + * @{ + */ + +/** + * @brief Enables the @p ili9341AcquireBus() and @p ili9341ReleaseBus() APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(ILI9341_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__) +#define ILI9341_USE_MUTUAL_EXCLUSION TRUE +#endif + +/** + * @brief ILI9341 Interface Mode. + */ +#if !defined(ILI9341_IM) || defined(__DOXYGEN__) +#define ILI9341_IM (ILI9341_IM_4LSI_1) +#endif + +/** + * @brief Enables checks for ILI9341 functions. + * @note Disabling this option saves both code and data space. + * @note Disabling checks by ChibiOS will automatically disable ILI9341 + * checks. + */ +#if !defined(ILI9341_USE_CHECKS) || defined(__DOXYGEN__) +#define ILI9341_USE_CHECKS TRUE +#endif + +/** @} */ + +/*===========================================================================*/ +/* Derived constants and error checks. */ +/*===========================================================================*/ + +#if ((TRUE == ILI9341_USE_MUTUAL_EXCLUSION) && \ + (TRUE != CH_CFG_USE_MUTEXES) && \ + (TRUE != CH_CFG_USE_SEMAPHORES)) +#error "ILI9341_USE_MUTUAL_EXCLUSION requires CH_CFG_USE_MUTEXES and/or CH_CFG_USE_SEMAPHORES" +#endif + +/* TODO: Add the remaining modes.*/ +#if (ILI9341_IM != ILI9341_IM_4LSI_1) +#error "Only ILI9341_IM_4LSI_1 interface mode is supported currently" +#endif + +/*===========================================================================*/ +/* Driver data structures and types. */ +/*===========================================================================*/ + +/* Complex types forwarding.*/ +typedef struct ILI9341Config ILI9341Config; +typedef enum ili9341state_t ili9341state_t; +typedef struct ILI9341Driver ILI9341Driver; + +/** + * @brief ILI9341 driver configuration. + */ +typedef struct ILI9341Config { + SPIDriver *spi; /**< SPI driver used by ILI9341.*/ +#if (ILI9341_IM == ILI9341_IM_4LSI_1) + ioportid_t dcx_port; /**< <tt>D/!C</tt> signal port.*/ + uint16_t dcx_pad; /**< <tt>D/!C</tt> signal pad.*/ +#endif /* ILI9341_IM == * */ /* TODO: Add all modes.*/ +} ILI9341Config; + +/** + * @brief ILI9341 driver state. + */ +typedef enum ili9341state_t { + ILI9341_UNINIT = (0), /**< Not initialized.*/ + ILI9341_STOP = (1), /**< Stopped.*/ + ILI9341_READY = (2), /**< Ready.*/ + ILI9341_ACTIVE = (3), /**< Exchanging data.*/ +} ili9341state_t; + +/** + * @brief ILI9341 driver. + */ +typedef struct ILI9341Driver { + ili9341state_t state; /**< Driver state.*/ + const ILI9341Config *config; /**< Driver configuration.*/ + + /* Multithreading stuff.*/ +#if (TRUE == ILI9341_USE_MUTUAL_EXCLUSION) +#if (TRUE == CH_CFG_USE_MUTEXES) + mutex_t lock; /**< Multithreading lock.*/ +#elif (TRUE == CH_CFG_USE_SEMAPHORES) + semaphore_t lock; /**< Multithreading lock.*/ +#endif +#endif /* (TRUE == ILI9341_USE_MUTUAL_EXCLUSION) */ + + /* Temporary variables.*/ + uint8_t value; /**< Non-stacked value, for SPI with CCM.*/ +} ILI9341Driver; + +/** + * @name ILI9341 command params (little endian) + * @{ + */ +#pragma pack(push, 1) + +typedef union { + struct ILI9341ParamBits_GET_ID_INFO { + uint8_t reserved_; + uint8_t ID1; + uint8_t ID2; + uint8_t ID3; + } bits; + uint8_t bytes[4]; +} ILI9341Params_GET_ID_INFO; + +typedef union { + struct ILI9341ParamBits_GET_STATUS { + unsigned _reserved_1 : 5; /* D[ 4: 0] */ + unsigned tearing_mode : 1; /* D[ 5] */ + unsigned gamma_curve : 3; /* D[ 8: 6] */ + unsigned tearing : 1; /* D[ 9] */ + unsigned display : 1; /* D[10] */ + unsigned all_on : 1; /* D[11] */ + unsigned all_off : 1; /* D[12] */ + unsigned invert : 1; /* D[13] */ + unsigned _reserved_2 : 1; /* D[14] */ + unsigned vscroll : 1; /* D[15] */ + unsigned normal : 1; /* D[16] */ + unsigned sleep : 1; /* D[17] */ + unsigned partial : 1; /* D[18] */ + unsigned idle : 1; /* D[19] */ + unsigned pixel_format : 3; /* D[22:20] */ + unsigned _reserved_3 : 2; /* D[24:23] */ + unsigned hrefr_rtl_nltr : 1; /* D[25] */ + unsigned bgr_nrgb : 1; /* D[26] */ + unsigned vrefr_btt_nttb : 1; /* D[27] */ + unsigned transpose : 1; /* D[28] */ + unsigned coladr_rtl_nltr : 1; /* D[29] */ + unsigned rowadr_btt_nttb : 1; /* D[30] */ + unsigned booster : 1; /* D[31] */ + } bits; + uint8_t bytes[4]; +} ILI9341Params_GET_STATUS; + +typedef union { + struct ILI9341ParamBits_GET_PWR_MODE { + unsigned _reserved_1 : 2; /* D[1:0] */ + unsigned display : 1; /* D[2] */ + unsigned normal : 1; /* D[3] */ + unsigned sleep : 1; /* D[4] */ + unsigned partial : 1; /* D[5] */ + unsigned idle : 1; /* D[6] */ + unsigned booster : 1; /* D[7] */ + } bits; + uint8_t bytes[1]; +} ILI9341Params_GET_PWR_MODE; + +typedef union { + struct ILI9341ParamBits_GET_MADCTL { + unsigned _reserved_1 : 2; /* D[1:0] */ + unsigned refr_rtl_nltr : 1; /* D[2] */ + unsigned bgr_nrgb : 1; /* D[3] */ + unsigned refr_btt_nttb : 1; /* D[4] */ + unsigned invert : 1; /* D[5] */ + unsigned rtl_nltr : 1; /* D[6] */ + unsigned btt_nttb : 1; /* D[7] */ + } bits; + uint8_t bytes[1]; +} ILI9341Params_GET_MADCTL; + +typedef union { + struct ILI9341ParamBits_GET_PIX_FMT { + unsigned DBI : 3; /* D[2:0] */ + unsigned _reserved_1 : 1; /* D[3] */ + unsigned DPI : 3; /* D[6:4] */ + unsigned RIM : 1; /* D[7] */ + } bits; + uint8_t bytes[1]; +} ILI9341Params_GET_PIX_FMT; + +typedef union { + struct ILI9341ParamBits_GET_IMG_FMT { + unsigned gamma_curve : 3; /* D[2:0] */ + unsigned _reserved_1 : 5; /* D[7:3] */ + } bits; + uint8_t bytes[1]; +} ILI9341Params_GET_IMG_FMT; + +typedef union { + struct ILI9341ParamBits_GET_SIG_MODE { + unsigned _reserved_1 : 2; /* D[1:0] */ + unsigned data_enable : 1; /* D[2] */ + unsigned pixel_clock : 1; /* D[3] */ + unsigned vsync : 1; /* D[4] */ + unsigned hsync : 1; /* D[5] */ + unsigned tearing_mode : 1; /* D[6] */ + unsigned tearing : 1; /* D[7] */ + } bits; + uint8_t bytes[1]; +} ILI9341Params_GET_SIG_MODE; + +typedef union { + struct ILI9341ParamBits_GET_SELF_DIAG { + unsigned _reserved_1 : 6; /* D[5:0] */ + unsigned func_err : 1; /* D[6] */ + unsigned reg_err : 1; /* D[7] */ + } bits; + uint8_t bytes[1]; +} ILI9341Params_GET_SELF_DIAG; + +typedef union { + struct ILI9341ParamBits_SET_GAMMA { + uint8_t gamma_curve; /* D[7:0] */ + } bits; + uint8_t bytes[1]; +} ILI9341Params_SET_GAMMA; + +typedef union { + struct ILI9341ParamBits_SET_COL_ADDR { + uint8_t SC_15_8; /* D[ 7: 0] */ + uint8_t SC_7_0; /* D[15: 8] */ + uint8_t EC_15_8; /* D[23:16] */ + uint8_t EC_7_0; /* D[31:24] */ + } bits; + uint8_t bytes[4]; +} ILI9341Params_SET_COL_ADDR; + +typedef union { + struct ILI9341ParamBits_SET_PAGE_ADDR { + uint8_t SP_15_8; /* D[ 7: 0] */ + uint8_t SP_7_0; /* D[15: 8] */ + uint8_t EP_15_8; /* D[23:16] */ + uint8_t EP_7_0; /* D[31:24] */ + } bits; + uint8_t bytes[4]; +} ILI9341Params_SET_PAGE_ADDR; + +typedef union { + struct ILI9341ParamBits_SET_PARTIAL_AREA { + uint8_t SR_15_8; /* D[ 7: 0] */ + uint8_t SR_7_0; /* D[15: 8] */ + uint8_t ER_15_8; /* D[23:16] */ + uint8_t ER_7_0; /* D[31:24] */ + } bits; + uint8_t bytes[4]; +} ILI9341Params_SET_PARTIAL_AREA; + +typedef union { + struct ILI9341ParamBits_SET_VSCROLL { + uint8_t TFA_15_8; /* D[ 7: 0] */ + uint8_t TFA_7_0; /* D[15: 8] */ + uint8_t VSA_15_8; /* D[23:16] */ + uint8_t VSA_7_0; /* D[31:24] */ + uint8_t BFA_15_8; /* D[39:32] */ + uint8_t BFA_7_0; /* D[47:40] */ + } bits; + uint8_t bytes[6]; +} ILI9341Params_SET_VSCROLL; + +typedef union { + struct ILI9341ParamBits_CMD_TEARING_ON { + unsigned M : 1; /* D[0] */ + unsigned _reserved_1 : 7; /* D[7:1] */ + } bits; + uint8_t bytes[1]; +} ILI9341Params_CMD_TEARING_ON; + +typedef union { + struct ILI9341ParamBits_SET_MEM_ACS_CTL { + unsigned _reserved_1 : 2; /* D[1:0] */ + unsigned MH : 1; /* D[2] */ + unsigned BGR : 1; /* D[3] */ + unsigned ML : 1; /* D[4] */ + unsigned MV : 1; /* D[5] */ + unsigned MX : 1; /* D[6] */ + unsigned MY : 1; /* D[7] */ + } bits; + uint8_t bytes[1]; +} ILI9341Params_SET_MEM_ACS_CTL; + +typedef union { + struct ILI9341ParamBits_SET_VSCROLL_ADDR { + uint8_t VSP_15_8; /* D[ 7: 0] */ + uint8_t VSP_7_0; /* D[15: 8] */ + } bits; + uint8_t bytes[2]; +} ILI9341Params_SET_VSCROLL_ADDR; + +typedef union { + struct ILI9341ParamBits_SET_PIX_FMT { + unsigned DBI : 3; /* D[2:0] */ + unsigned _reserved_1 : 1; /* D[3] */ + unsigned DPI : 3; /* D[4:6] */ + unsigned _reserved_2 : 1; /* D[7] */ + } bits; + uint8_t bytes[1]; +} ILI9341Params_SET_PIX_FMT; + +typedef union { + struct ILI9341ParamBits_SET_TEAR_SCANLINE { + uint8_t STS_8; /* D[ 7: 0] */ + uint8_t STS_7_0; /* D[15: 8] */ + } bits; + uint8_t bytes[4]; +} ILI9341Params_SET_TEAR_SCANLINE; + +typedef union { + struct ILI9341ParamBits_GET_TEAR_SCANLINE { + uint8_t GTS_9_8; /* D[ 7: 0] */ + uint8_t GTS_7_0; /* D[15: 8] */ + } bits; + uint8_t bytes[2]; +} ILI9341Params_GET_TEAR_SCANLINE; + +typedef union { + struct ILI9341ParamBits_SET_BRIGHTNESS { + uint8_t DBV; /* D[7:0] */ + } bits; + uint8_t bytes[1]; +} ILI9341Params_SET_BRIGHTNESS; + +typedef union { + struct ILI9341ParamBits_GET_BRIGHTNESS { + uint8_t DBV; /* D[7:0] */ + } bits; + uint8_t bytes[1]; +} ILI9341Params_GET_BRIGHTNESS; + +typedef union { + struct ILI9341ParamBits_SET_DISPLAY_CTL { + unsigned _reserved_1 : 2; /* D[1:0] */ + unsigned BL : 1; /* D[2] */ + unsigned DD : 1; /* D[3] */ + unsigned _reserved_2 : 1; /* D[4] */ + unsigned BCTRL : 1; /* D[5] */ + unsigned _reserved_3 : 1; /* D[7:6] */ + } bits; + uint8_t bytes[1]; +} ILI9341Params_SET_DISPLAY_CTL; + +typedef union { + struct ILI9341ParamBits_GET_DISPLAY_CTL { + unsigned _reserved_1 : 2; /* D[1:0] */ + unsigned BL : 1; /* D[2] */ + unsigned DD : 1; /* D[3] */ + unsigned _reserved_2 : 1; /* D[4] */ + unsigned BCTRL : 1; /* D[5] */ + unsigned _reserved_3 : 1; /* D[7:6] */ + } bits; + uint8_t bytes[1]; +} ILI9341Params_GET_DISPLAY_CTL; + +typedef union { + struct ILI9341ParamBits_SET_CABC { + unsigned C : 2; /* D[1:0] */ + unsigned _reserved_1 : 6; /* D[7:2] */ + } bits; + uint8_t bytes[1]; +} ILI9341Params_SET_CABC; + +typedef union { + struct ILI9341ParamBits_GET_CABC { + unsigned C : 2; /* D[1:0] */ + unsigned _reserved_1 : 6; /* D[7:2] */ + } bits; + uint8_t bytes[1]; +} ILI9341Params_GET_CABC; + +typedef union { + struct ILI9341ParamBits_SET_CABC_MIN { + uint8_t CMB; /* D[7:0] */ + } bits; + uint8_t bytes[1]; +} ILI9341Params_SET_CABC_MIN; + +typedef union { + struct ILI9341ParamBits_GET_CABC_MIN { + uint8_t CMB; /* D[7:0] */ + } bits; + uint8_t bytes[1]; +} ILI9341Params_GET_CABC_MIN; + +#if 0 /* TODO: Extended command structs.*/ + +typedef union { + struct ILI9341ParamBits { + unsigned : 1; /* D[] */ + unsigned : 1; /* D[] */ + unsigned : 1; /* D[] */ + unsigned : 1; /* D[] */ + unsigned : 1; /* D[] */ + unsigned : 1; /* D[] */ + unsigned : 1; /* D[] */ + unsigned : 1; /* D[] */ + } bits; + uint8_t bytes[1]; +} ILI9341Params_; + +typedef union { + struct ILI9341ParamBits { + unsigned : 1; /* D[] */ + unsigned : 1; /* D[] */ + unsigned : 1; /* D[] */ + unsigned : 1; /* D[] */ + unsigned : 1; /* D[] */ + unsigned : 1; /* D[] */ + unsigned : 1; /* D[] */ + unsigned : 1; /* D[] */ + } bits; + uint8_t bytes[1]; +} ILI9341Params_; + +#endif /*0*/ + +#pragma pack(pop) + +/** @} */ + +/*===========================================================================*/ +/* Driver macros. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* External declarations. */ +/*===========================================================================*/ + +extern ILI9341Driver ILI9341D1; + +#ifdef __cplusplus +extern "C" { +#endif + + void ili9341ObjectInit(ILI9341Driver *driverp); + void ili9341Start(ILI9341Driver *driverp, const ILI9341Config *configp); + void ili9341Stop(ILI9341Driver *driverp); +#if (ILI9341_USE_MUTUAL_EXCLUSION == TRUE) + void ili9341AcquireBusS(ILI9341Driver *driverp); + void ili9341AcquireBus(ILI9341Driver *driverp); + void ili9341ReleaseBusS(ILI9341Driver *driverp); + void ili9341ReleaseBus(ILI9341Driver *driverp); +#endif /* (ILI9341_USE_MUTUAL_EXCLUSION == TRUE) */ + void ili9341SelectI(ILI9341Driver *driverp); + void ili9341Select(ILI9341Driver *driverp); + void ili9341UnselectI(ILI9341Driver *driverp); + void ili9341Unselect(ILI9341Driver *driverp); + void ili9341WriteCommand(ILI9341Driver *driverp, uint8_t cmd); + void ili9341WriteByte(ILI9341Driver *driverp, uint8_t value); + uint8_t ili9341ReadByte(ILI9341Driver *driverp); + void ili9341WriteChunk(ILI9341Driver *driverp, const uint8_t chunk[], + size_t length); + void ili9341ReadChunk(ILI9341Driver *driverp, uint8_t chunk[], + size_t length); + +#ifdef __cplusplus +} +#endif + +/** @} */ + +#endif /* _ILI9341_H_ */ diff --git a/os/various/memtest.cpp b/os/various/memtest.cpp new file mode 100644 index 0000000..49828ec --- /dev/null +++ b/os/various/memtest.cpp @@ -0,0 +1,286 @@ +/* + ChibiOS/RT - Copyright (C) 2013-2014 Uladzimir Pylinsky aka barthess + + 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. +*/ + +#include <stdint.h> +#include <stddef.h> +#include <stdlib.h> + +#include "memtest.hpp" + +/* + * + */ +template <typename T> +class Generator { +public: + Generator(void) : pattern(0) {;} + virtual T get(void) = 0; + virtual testtype get_type(void) = 0; + virtual void init(T seed) { + pattern = seed; + } +protected: + T pattern; +}; + +/* + * + */ +template <typename T> +class GeneratorWalkingOne : public Generator<T> { + T get(void) { + T ret = this->pattern; + + this->pattern <<= 1; + if (0 == this->pattern) + this->pattern = 1; + + return ret; + } + + testtype get_type(void) { + return MEMTEST_WALKING_ONE; + } +}; + +/* + * + */ +template <typename T> +class GeneratorWalkingZero : public Generator<T> { + T get(void) { + T ret = ~this->pattern; + + this->pattern <<= 1; + if (0 == this->pattern) + this->pattern = 1; + + return ret; + } + + testtype get_type(void) { + return MEMTEST_WALKING_ZERO; + } +}; + +/* + * + */ +template <typename T> +class GeneratorOwnAddress : public Generator<T> { + T get(void) { + T ret = this->pattern; + this->pattern++; + return ret; + } + + testtype get_type(void) { + return MEMTEST_OWN_ADDRESS; + } +}; + +/* + * + */ +template <typename T> +class GeneratorMovingInv : public Generator<T> { + T get(void) { + T ret = this->pattern; + this->pattern = ~this->pattern; + return ret; + } + + testtype get_type(void) { + if ((this->pattern == 0) || ((this->pattern & 0xFF) == 0xFF)) + return MEMTEST_MOVING_INVERSION_ZERO; + else + return MEMTEST_MOVING_INVERSION_55AA; + } +}; + +/* + * + */ +template <typename T> +class GeneratorMovingInvRand : public Generator<T> { +public: + GeneratorMovingInvRand(void) : step(0), prev(0){;} + void init(T seed) { + srand(seed); + step = 0; + prev = 0; + } + + T get(void) { + T ret; + T mask = -1; + if ((step & 1) == 0) { + ret = rand() & mask; + prev = ret; + } + else { + ret = ~prev & mask; + } + step++; + + return ret; + } + + testtype get_type(void) { + return MEMTEST_MOVING_INVERSION_RAND; + } + +private: + size_t step; + T prev; +}; + +/* + * + */ +template <typename T> +static void memtest_sequential(memtest_t *testp, Generator<T> &generator, T seed) { + const size_t steps = testp->size / sizeof(T); + size_t i; + T *mem = static_cast<T *>(testp->start); + + /* fill ram */ + generator.init(seed); + for (i=0; i<steps; i++) + mem[i] = generator.get(); + + /* read back and compare */ + generator.init(seed); + for (i=0; i<steps; i++) { + if (mem[i] != generator.get()) { + testp->ecb(testp, generator.get_type(), i*sizeof(T)); + return; + } + } +} + +template <typename T> +static void walking_one(memtest_t *testp) { + GeneratorWalkingOne<T> generator; + memtest_sequential<T>(testp, generator, 1); +} + +template <typename T> +static void walking_zero(memtest_t *testp) { + GeneratorWalkingZero<T> generator; + memtest_sequential<T>(testp, generator, 1); +} + +template <typename T> +static void own_address(memtest_t *testp) { + GeneratorOwnAddress<T> generator; + memtest_sequential<T>(testp, generator, 0); +} + +template <typename T> +static void moving_inversion_zero(memtest_t *testp) { + GeneratorMovingInv<T> generator; + T mask = -1; + memtest_sequential<T>(testp, generator, 0); + memtest_sequential<T>(testp, generator, 0xFFFFFFFF & mask); +} + +template <typename T> +static void moving_inversion_55aa(memtest_t *testp) { + GeneratorMovingInv<T> generator; + T mask = -1; + memtest_sequential<T>(testp, generator, 0x55555555 & mask); + memtest_sequential<T>(testp, generator, 0xAAAAAAAA & mask); +} + +template <typename T> +static void moving_inversion_rand(memtest_t *testp) { + GeneratorMovingInvRand<T> generator; + T mask = -1; + memtest_sequential<T>(testp, generator, testp->rand_seed & mask); +} + +/* + * + */ +static void memtest_wrapper(memtest_t *testp, + void (*p_u8)(memtest_t *testp), + void (*p_u16)(memtest_t *testp), + void (*p_u32)(memtest_t *testp)) { + switch(testp->width) { + case MEMTEST_WIDTH_32: + p_u8(testp); + p_u16(testp); + p_u32(testp); + break; + case MEMTEST_WIDTH_16: + p_u8(testp); + p_u16(testp); + break; + case MEMTEST_WIDTH_8: + p_u8(testp); + break; + } +} + +/* + * + */ +void memtest_run(memtest_t *testp, uint32_t testmask) { + + if (testmask & MEMTEST_WALKING_ONE) { + memtest_wrapper(testp, + walking_one<uint8_t>, + walking_one<uint16_t>, + walking_one<uint32_t>); + } + + if (testmask & MEMTEST_WALKING_ZERO) { + memtest_wrapper(testp, + walking_zero<uint8_t>, + walking_zero<uint16_t>, + walking_zero<uint32_t>); + } + + if (testmask & MEMTEST_OWN_ADDRESS) { + memtest_wrapper(testp, + own_address<uint8_t>, + own_address<uint16_t>, + own_address<uint32_t>); + } + + if (testmask & MEMTEST_MOVING_INVERSION_ZERO) { + memtest_wrapper(testp, + moving_inversion_zero<uint8_t>, + moving_inversion_zero<uint16_t>, + moving_inversion_zero<uint32_t>); + } + + if (testmask & MEMTEST_MOVING_INVERSION_55AA) { + memtest_wrapper(testp, + moving_inversion_55aa<uint8_t>, + moving_inversion_55aa<uint16_t>, + moving_inversion_55aa<uint32_t>); + } + + if (testmask & MEMTEST_MOVING_INVERSION_RAND) { + memtest_wrapper(testp, + moving_inversion_rand<uint8_t>, + moving_inversion_rand<uint16_t>, + moving_inversion_rand<uint32_t>); + } +} + diff --git a/os/various/memtest.hpp b/os/various/memtest.hpp new file mode 100644 index 0000000..200db1f --- /dev/null +++ b/os/various/memtest.hpp @@ -0,0 +1,73 @@ +/* + ChibiOS/RT - Copyright (C) 2013-2014 Uladzimir Pylinsky aka barthess + + 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. +*/ + +#ifndef MEMTEST_HPP_ +#define MEMTEST_HPP_ + +#define MEMTEST_WALKING_ONE (1 << 0) +#define MEMTEST_WALKING_ZERO (1 << 1) +#define MEMTEST_OWN_ADDRESS (1 << 2) +#define MEMTEST_MOVING_INVERSION_ZERO (1 << 3) +#define MEMTEST_MOVING_INVERSION_55AA (1 << 4) +#define MEMTEST_MOVING_INVERSION_RAND (1 << 5) + +#define MEMTEST_RUN_ALL (MEMTEST_WALKING_ONE | \ + MEMTEST_WALKING_ZERO | \ + MEMTEST_OWN_ADDRESS | \ + MEMTEST_MOVING_INVERSION_ZERO | \ + MEMTEST_MOVING_INVERSION_55AA | \ + MEMTEST_MOVING_INVERSION_RAND) + +typedef struct memtest_t memtest_t; +typedef uint32_t testtype; + +/* + * Error call back. + */ +typedef void (*memtestecb_t)(memtest_t *testp, testtype type, size_t address); + +/* + * + */ +typedef enum { + MEMTEST_WIDTH_8, + MEMTEST_WIDTH_16, + MEMTEST_WIDTH_32 +} memtest_bus_width_t; + +/* + * + */ +struct memtest_t { + void *start; + size_t size; + memtest_bus_width_t width; + memtestecb_t ecb; + unsigned int rand_seed; +}; + +/* + * + */ +#ifdef __cplusplus +extern "C" { +#endif + void memtest_run(memtest_t *testp, uint32_t testmask); +#ifdef __cplusplus +} +#endif + +#endif /* MEMTEST_HPP_ */ diff --git a/os/various/tribuf.c b/os/various/tribuf.c new file mode 100644 index 0000000..80eb258 --- /dev/null +++ b/os/various/tribuf.c @@ -0,0 +1,214 @@ +/*
+ Copyright (C) 2014..2015 Andrea Zoppi
+
+ 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.
+*/
+
+#include "osal.h"
+#include "tribuf.h"
+
+/**
+ * @file tribuf.c
+ * @brief Triple buffer handler source.
+ *
+ * @addtogroup TriBuf
+ * @{
+ */
+
+/*===========================================================================*/
+/* Driver local definitions. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver exported variables. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver local variables and types. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver local functions. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver interrupt handlers. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver exported functions. */
+/*===========================================================================*/
+
+/**
+ * @brief Initializes the tribuf handler object.
+ *
+ * @param[in] handler Pointer to the tribuf handler object.
+ * @param[in] front Pointer to the initial front buffer.
+ * @param[in] back Pointer to the initial back buffer.
+ * @param[in] orphan Pointer to the initial orphan buffer.
+ *
+ * @init
+ */
+void tribufObjectInit(tribuf_t *handler, void *front, void *back, void *orphan) {
+
+ handler->front = front;
+ handler->back = back;
+ handler->orphan = orphan;
+#if (TRIBUF_USE_WAIT == TRUE)
+ chSemObjectInit(&handler->ready, (cnt_t)0);
+#else
+ handler->ready = false;
+#endif
+}
+
+/**
+ * @brief Gets the current front buffer.
+ *
+ * @param[in] handler Pointer to the tribuf handler object.
+ * @return Pointer to the current front buffer.
+ *
+ * @api
+ */
+void *tribufGetFront(tribuf_t *handler) {
+
+ void *front;
+
+ osalSysLock();
+ front = tribufGetFrontI(handler);
+ osalSysUnlock();
+ return front;
+}
+
+/**
+ * @brief Swaps the current front buffer.
+ *
+ * @details Exchanges the pointer of the current front buffer, which will be
+ * dismissed, with the pointer of the current orphan buffer, which
+ * holds the content of the new front buffer.
+ *
+ * @pre The orphan buffer holds new data, swapped by the back buffer.
+ * @pre The fron buffer is ready for swap.
+ * @post The orphan buffer can be used as new back buffer in the future.
+ *
+ * @param[in] handler Pointer to the tribuf handler object.
+ *
+ * @iclass
+ */
+void tribufSwapFrontI(tribuf_t *handler) {
+
+ void *front;
+
+ osalDbgCheckClassI();
+
+ front = handler->orphan;
+ handler->orphan = handler->front;
+ handler->front = front;
+}
+
+/**
+ * @brief Swaps the current front buffer.
+ *
+ * @details Exchanges the pointer of the current front buffer, which will be
+ * dismissed, with the pointer of the current orphan buffer, which
+ * holds the content of the new front buffer.
+ *
+ * @pre The orphan buffer holds new data, swapped by the back buffer.
+ * @pre The fron buffer is ready for swap.
+ * @post The orphan buffer can be used as new back buffer in the future.
+ *
+ * @param[in] handler Pointer to the tribuf handler object.
+ *
+ * @api
+ */
+void tribufSwapFront(tribuf_t *handler) {
+
+ osalSysLock();
+ tribufSwapFrontI(handler);
+ osalSysUnlock();
+}
+
+/**
+ * @brief Gets the current back buffer.
+ *
+ * @param[in] handler Pointer to the tribuf handler object.
+ * @return Pointer to the current back buffer.
+ *
+ * @api
+ */
+void *tribufGetBack(tribuf_t *handler) {
+
+ void *back;
+
+ osalSysLock();
+ back = tribufGetBackI(handler);
+ osalSysUnlock();
+ return back;
+}
+
+/**
+ * @brief Swaps the current back buffer.
+ *
+ * @details Exchanges the pointer of the current back buffer, which holds new
+ * useful data, with the pointer of the current orphan buffer.
+ *
+ * @pre The orphan buffer holds no meaningful data.
+ * @post The orphan buffer is candidate for new front buffer.
+ * @post A new front buffer is ready and signaled.
+ *
+ * @param[in] handler Pointer to the tribuf handler object.
+ *
+ * @iclass
+ */
+void tribufSwapBackI(tribuf_t *handler) {
+
+ void *back;
+
+ osalDbgCheckClassI();
+
+ back = handler->orphan;
+ handler->orphan = handler->back;
+ handler->back = back;
+
+#if (TRIBUF_USE_WAIT == TRUE)
+ if (chSemGetCounterI(&handler->ready) < (cnt_t)1)
+ chSemSignalI(&handler->ready);
+#else
+ handler->ready = true;
+#endif
+}
+
+/**
+ * @brief Swaps the current back buffer.
+ *
+ * @details Exchanges the pointer of the current back buffer, which holds new
+ * useful data, with the pointer of the current orphan buffer.
+ *
+ * @pre The orphan buffer holds no meaningful data.
+ * @post The orphan buffer is candidate for new front buffer.
+ * @post A new front buffer is ready and signaled.
+ *
+ * @param[in] handler Pointer to the tribuf handler object.
+ *
+ * @api
+ */
+void tribufSwapBack(tribuf_t *handler) {
+
+ osalSysLock();
+ tribufSwapBackI(handler);
+#if (TRIBUF_USE_WAIT == TRUE)
+ osalOsRescheduleS();
+#endif
+ osalSysUnlock();
+}
+
+/** @} */
diff --git a/os/various/tribuf.h b/os/various/tribuf.h new file mode 100644 index 0000000..4ba3f25 --- /dev/null +++ b/os/various/tribuf.h @@ -0,0 +1,225 @@ +/*
+ Copyright (C) 2014..2015 Andrea Zoppi
+
+ 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 tribuf.h
+ * @brief Triple buffer handler header.
+ *
+ * @addtogroup TriBuf
+ * @{
+ */
+
+#ifndef _TRIBUF_H_
+#define _TRIBUF_H_
+
+/*===========================================================================*/
+/* Driver constants. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver pre-compile time settings. */
+/*===========================================================================*/
+
+/**
+ * @name Triple buffer configuration options
+ * @{
+ */
+
+/**
+ * @brief Triple buffers use blocking functions.
+ */
+#if !defined(TRIBUF_USE_WAIT) || defined(__DOXYGEN__)
+#define TRIBUF_USE_WAIT TRUE
+#endif
+
+/** @} */
+
+/*===========================================================================*/
+/* Derived constants and error checks. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver data structures and types. */
+/*===========================================================================*/
+
+/**
+ * @brief Triple buffer handler object.
+ */
+typedef struct {
+ void *front; /**< @brief Current front buffer pointer.*/
+ void *back; /**< @brief Current back buffer pointer.*/
+ void *orphan; /**< @brief Current orphan buffer pointer.*/
+#if (TRIBUF_USE_WAIT == TRUE)
+ semaphore_t ready; /**< @brief A new front buffer is ready.*/
+#else
+ bool ready; /**< @brief A new front buffer is ready.*/
+#endif
+} tribuf_t;
+
+/*===========================================================================*/
+/* Driver macros. */
+/*===========================================================================*/
+
+/**
+ * @brief Checks if a new front buffer is ready.
+ *
+ * @param[in] handler Pointer to the tribuf handler object.
+ * @return Availability of a new front buffer.
+ *
+ * @iclass
+ */
+static inline
+bool tribufIsReadyI(tribuf_t *handler)
+{
+ osalDbgCheckClassI();
+
+#if (TRIBUF_USE_WAIT == TRUE)
+ return (0 != chSemGetCounterI(&handler->ready));
+#else
+ return handler->ready;
+#endif
+}
+
+#if (TRIBUF_USE_WAIT == TRUE) || defined(__DOXYGEN__)
+
+/**
+ * @brief Waits until a new front buffer is ready, with timeout.
+ *
+ * @post The ready signal, result of the back buffer swap, is consumed.
+ *
+ * @param[in] handler Pointer to the tribuf handler object.
+ * @param[in] timeout Timeout of the wait operation.
+ * @return Timeout error code, as from @p chSemWaitTimeoutS.
+ *
+ * @see chSemWaitTimeoutS
+ * @sclass
+ */
+static inline
+msg_t tribufWaitReadyTimeoutS(tribuf_t *handler, systime_t timeout)
+{
+ osalDbgCheckClassS();
+
+ return chSemWaitTimeoutS(&handler->ready, timeout);
+}
+
+/**
+ * @brief Waits until a new front buffer is ready, with timeout.
+ *
+ * @post The ready signal, result of the back buffer swap, is consumed.
+ *
+ * @param[in] handler Pointer to the tribuf handler object.
+ * @param[in] timeout Timeout of the wait operation.
+ * @return Timeout error code, as from @p chSemWaitTimeout.
+ *
+ * @see chSemWaitTimeout
+ * @api
+ */
+static inline
+msg_t tribufWaitReadyTimeout(tribuf_t *handler, systime_t timeout)
+{
+ return chSemWaitTimeout(&handler->ready, timeout);
+}
+
+/**
+ * @brief Waits until a new front buffer is ready.
+ *
+ * @post The ready signal, result of the back buffer swap, is consumed.
+ *
+ * @param[in] handler Pointer to the tribuf handler object.
+ * @return Timeout error code, as from @p chSemWaitS.
+ *
+ * @see chSemWaitS
+ * @sclass
+ */
+static inline
+void tribufWaitReadyS(tribuf_t *handler)
+{
+ osalDbgCheckClassS();
+
+ chSemWaitS(&handler->ready);
+}
+
+/**
+ * @brief Waits until a new front buffer is ready.
+ *
+ * @post The ready signal, result of the back buffer swap, is consumed.
+ *
+ * @param[in] handler Pointer to the tribuf handler object.
+ * @return Timeout error code, as from @p chSemWait.
+ *
+ * @see chSemWait
+ * @api
+ */
+static inline
+void tribufWaitReady(tribuf_t *handler)
+{
+ chSemWait(&handler->ready);
+}
+
+#endif /* (TRIBUF_USE_WAIT == TRUE) || defined(__DOXYGEN__) */
+
+/**
+ * @brief Gets the current front buffer.
+ *
+ * @param[in] handler Pointer to the tribuf handler object.
+ * @return Pointer to the current front buffer.
+ *
+ * @iclass
+ */
+static inline
+void *tribufGetFrontI(tribuf_t *handler) {
+
+ osalDbgCheckClassI();
+
+ return handler->front;
+}
+
+/**
+ * @brief Gets the current back buffer.
+ *
+ * @param[in] handler Pointer to the tribuf handler object.
+ * @return Pointer to the current back buffer.
+ *
+ * @iclass
+ */
+static inline
+void *tribufGetBackI(tribuf_t *handler) {
+
+ osalDbgCheckClassI();
+
+ return handler->back;
+}
+
+/*===========================================================================*/
+/* External declarations. */
+/*===========================================================================*/
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+ void tribufObjectInit(tribuf_t *handler, void *front, void *back, void *orphan);
+ void *tribufGetFront(tribuf_t *handler);
+ void tribufSwapFrontI(tribuf_t *handler);
+ void tribufSwapFront(tribuf_t *handler);
+ void *tribufGetBack(tribuf_t *handler);
+ void tribufSwapBackI(tribuf_t *handler);
+ void tribufSwapBack(tribuf_t *handler);
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _TRIBUF_H_ */
+/** @} */
|