/* * Common IO - basic V1.0.0 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * http://aws.amazon.com/freertos * http://www.FreeRTOS.org */ /** * @file iot_flash.h * @brief This file contains all the Flash HAL API definitions */ #ifndef _IOT_FLASH_H_ #define _IOT_FLASH_H_ /** * @defgroup iot_flash Flash HAL APIs * @{ */ /** * @brief Return values used by this driver */ #define IOT_FLASH_SUCCESS ( 0 ) /*!< Flash operation completed successfully. */ #define IOT_FLASH_INVALID_VALUE ( 1 ) /*!< At least one parameter is invalid. */ #define IOT_FLASH_WRITE_FAILED ( 2 ) /*!< Flash write operation failed. */ #define IOT_FLASH_READ_FAILED ( 3 ) /*!< Flash read operation failed. */ #define IOT_FLASH_ERASE_FAILED ( 4 ) /*!< Flash erase operation failed. */ #define IOT_FLASH_DEVICE_BUSY ( 5 ) /*!< Previous flash operation not complete yet. */ #define IOT_FLASH_CTRL_OP_FAILED ( 6 ) /*!< Flash control operation failed. */ #define IOT_FLASH_FUNCTION_NOT_SUPPORTED ( 7 ) /*!< Flash operation not supported. */ /** * @brief Flash current status */ typedef enum { eFlashIdle, /*!< Flash is idle. */ eFlashCmdInProgress, /*!< Flash command in progress. */ eFlashEraseFailed, /*!< Flash erase failed. */ eFlashProgramSuspended, /*!< Flash program operation suspended. */ eFlashEraseSuspended /*Example * @code{c} * IotFlashHandle_t xFlashHandle; * IotFlashInfo_t* pxFlashInfo; * int32_t lRetVal; * uint32_t ulChunkOffset; * * // Open flash to initialize hardware. * xFlashHandle = iot_flash_open(0); * // assert(xFlashHandle == NULL ); * * // Get the flash information. * pxFlashInfo = iot_flash_getinfo(xFlashHandle); * // assert(pxFlashInfo == NULL); * * // If Erase async is supported, register a callback * if ( pxFlashInfo->ucAsyncSupported ) * { * iot_flash_set_callback(xFlashHandle, * prvIotFlashEraseCallback, * NULL); * } * * // Erase 2 sectors * lRetVal = iot_flash_erase_sectors(xFlashHandle, * ultestIotFlashStartOffset, * pxFlashInfo->ulSectorSize * 2); * //assert(IOT_FLASH_SUCCESS != lRetVal); * * if ( pxFlashInfo->ucAsyncSupported ) * { * // Wait for the Erase to be completed and callback is called. * lRetVal = xSemaphoreTake(xtestIotFlashSemaphore, portMAX_DELAY); * //assert(pdTRUE != lRetVal); * } * * // Close the flash handle. * lRetVal = iot_flash_close(xFlashHandle); * //assert(IOT_FLASH_SUCCESS != lRetVal); * * @endcode */ int32_t iot_flash_erase_sectors( IotFlashHandle_t const pxFlashHandle, uint32_t ulStartAddress, size_t xSize ); /*! * @brief iot_erase_chip is used to erase the entire flash chip. * If there is another flash operation is in progress, the erase_chip API will return an error. * * @param[in] pxFlashHandle handle to flash driver returned in * iot_flash_open() * * @return * - IOT_FLASH_SUCCESS on success. * - IOT_FLASH_INVALID_VALUE if any parameter is invalid. * - IOT_FLASH_DEVICE_BUSY if another asynchronous operation is currently being executed. * - IOT_FLASH_ERASE_FAILED on error. */ int32_t iot_flash_erase_chip( IotFlashHandle_t const pxFlashHandle ); /*! * @brief iot_flash_write_sync is used to write data to flash starting at the address provided. * The sector(s) being written to, must be erased first before any write can take place. * This is a blocking operation and waits until the number of bytes are written before returning. * If there is another flash operation is in progress, write will return an error. * * @warning writing to a sector that was not erased first, may result in incorrect data being written while * the API returns IOT_FLASH_SUCCESS. * * @param[in] pxFlashHandle handle to flash driver returned in * iot_flash_open() * @param[in] ulAddress starting address(offset) in flash to write. * @param[in] xBytes number of bytes to write. * @param[in] pvBuffer data buffer to write to flash * * @return * - IOT_FLASH_SUCCESS on success. * - IOT_FLASH_INVALID_VALUE if any parameter is invalid. * - IOT_FLASH_DEVICE_BUSY if another asynchronous operation is currently being executed. * - IOT_FLASH_WRITE_FAILED on error. */ int32_t iot_flash_write_sync( IotFlashHandle_t const pxFlashHandle, uint32_t ulAddress, uint8_t * const pvBuffer, size_t xBytes ); /*! * @brief iot_flash_read_sync is used to read data from flash. This is a blocking operation * and waits until the number of bytes are read before returning. * If there is another flash operation is in progress, this will return an error. * * @param[in] pxFlashHandle handle to flash driver returned in * iot_flash_open() * @param[in] ulAddress starting address(offset) in flash to read. * @param[in] xBytes number of bytes to be read. * @param[out] pvBuffer data buffer to hold the data read from flash * * @return * - IOT_FLASH_SUCCESS on success. * - IOT_FLASH_INVALID_VALUE if any parameter is invalid. * - IOT_FLASH_DEVICE_BUSY if another asynchronous operation is currently being executed. * - IOT_FLASH_READ_FAILED on error. */ int32_t iot_flash_read_sync( IotFlashHandle_t const pxFlashHandle, uint32_t ulAddress, uint8_t * const pvBuffer, size_t xBytes ); /*! * @brief iot_flash_write_async is used to write data to flash starting at the address provided. * The sector(s) being written to, must be erased first before any write can take place. * This is an asynchronous (non-blocking) operation and returns as soon as the write * operation is started. When the write is completed, user callback is called to * notify that the write is complete. The caller can check the status of the operation * by using eGetStatus IOCTL. User must register for a callback when using the non-blocking * operations to know when they are complete. * If there is another flash operation is in progress, write will return an error. * * @param[in] pxFlashHandle handle to flash driver returned in * iot_flash_open() * @param[in] ulAddress starting address(offset) in flash to write. * @param[in] xBytes number of bytes to write. * @param[in] pvBuffer data buffer to write to flash * * @return * - IOT_FLASH_SUCCESS on success. * - IOT_FLASH_INVALID_VALUE if any parameter is invalid. * - IOT_FLASH_DEVICE_BUSY if another asynchronous operation is currently being executed. * - IOT_FLASH_WRITE_FAILED on error. * - IOT_FLASH_FUNCTION_NOT_SUPPORTED if asynchronous operation is not supported * (i,e ucAsyncSupported is set to false) */ int32_t iot_flash_write_async( IotFlashHandle_t const pxFlashHandle, uint32_t ulAddress, uint8_t * const pvBuffer, size_t xBytes ); /*! * @brief iot_flash_read_async is used to read data from flash. * This is an asynchronous (non-blocking) operation and returns as soon as the read * operation is started. When the read is completed, user callback is called to * notify that the read is complete. The caller can check the status of the operation * by using eGetStatus IOCTL and use the buffer. User must register for a callback when * using the non-blocking operations to know when they are complete. * If there is another flash operation is in progress, this will return an error. * * @param[in] pxFlashHandle handle to flash driver returned in * iot_flash_open() * @param[in] ulAddress starting address(offset) in flash to read. * @param[in] xBytes number of bytes to be read. * @param[out] pvBuffer data buffer to hold the data read from flash * * @return * - IOT_FLASH_SUCCESS on success. * - IOT_FLASH_INVALID_VALUE if any parameter is invalid. * - IOT_FLASH_DEVICE_BUSY if another asynchronous operation is currently being executed. * - IOT_FLASH_READ_FAILED on error. * - IOT_FLASH_FUNCTION_NOT_SUPPORTED if asynchronous operation is not supported * (i,e ucAsyncSupported is set to false) */ int32_t iot_flash_read_async( IotFlashHandle_t const pxFlashHandle, uint32_t ulAddress, uint8_t * const pvBuffer, size_t xBytes ); /*! * @brief iot_flash_close is used to close the flash device. * If any operations are in progress when close is called, * flash driver aborts those if possible. * * @param[in] pxFlashHandle handle to flash driver returned in * iot_flash_open() * * @return * - IOT_FLASH_SUCCESS on success close * - IOT_FLASH_INVALID_VALUE on invalid pxFlashHandle. */ int32_t iot_flash_close( IotFlashHandle_t const pxFlashHandle ); /** * @} */ #endif /* ifndef _IOT_FLASH_H_ */