1 // Copyright (C) 2022 Beken Corporation 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #pragma once 16 17 #include <common/bk_include.h> 18 19 #ifdef __cplusplus 20 extern "C" { 21 #endif 22 23 #if (!CONFIG_FLASH_ORIGIN_API) 24 #define PAR_OPT_READ_POS (0) 25 #define PAR_OPT_WRITE_POS (1) 26 27 #define PAR_OPT_READ_DIS (0x0u << PAR_OPT_READ_POS) 28 #define PAR_OPT_READ_EN (0x1u << PAR_OPT_READ_POS) 29 #define PAR_OPT_WRITE_DIS (0x0u << PAR_OPT_WRITE_POS) 30 #define PAR_OPT_WRITE_EN (0x1u << PAR_OPT_WRITE_POS) 31 #endif 32 33 typedef enum 34 { 35 BK_FLASH_EMBEDDED, 36 BK_FLASH_SPI, 37 BK_FLASH_MAX, 38 BK_FLASH_NONE, 39 } bk_flash_t; 40 41 typedef enum 42 { 43 BK_PARTITION_BOOTLOADER = 0, 44 BK_PARTITION_APPLICATION, 45 BK_PARTITION_OTA, 46 #if (CONFIG_SOC_BK7256XX) 47 BK_PARTITION_APPLICATION1, 48 #endif 49 BK_PARTITION_MATTER_FLASH, 50 BK_PARTITION_RF_FIRMWARE, 51 BK_PARTITION_NET_PARAM, 52 BK_PARTITION_USR_CONFIG, 53 BK_PARTITION_MAX, 54 } bk_partition_t; 55 56 typedef struct 57 { 58 bk_flash_t partition_owner; /**< flash partition owners */ 59 const char *partition_description; /**< flash partition description */ 60 uint32_t partition_start_addr; /**< flash partition start address */ 61 uint32_t partition_length; /**< flash partition length */ 62 uint32_t partition_options; /**< flash partition options */ 63 } bk_logic_partition_t; 64 65 /** 66 * @brief Get the infomation of the specified flash area 67 * 68 * @param partition: The target flash logical partition 69 * 70 * @return bk flash logic partition struct 71 */ 72 bk_logic_partition_t *bk_flash_partition_get_info(bk_partition_t partition); 73 74 /** 75 * @brief Erase an area on a Flash logical partition 76 * 77 * @note Erase on an address will erase all data on a sector that the 78 * address is belonged to, this function does not save data that 79 * beyond the address area but in the affected sector, the data 80 * will be lost. 81 * 82 * @param partition: The target flash logical partition which should be erased 83 * @param offset: Start address of the erased flash area 84 * @param size: Size of the erased flash area 85 * 86 * @return 87 * - BK_OK: succeed 88 * - others: other errors 89 */ 90 bk_err_t bk_flash_partition_erase(bk_partition_t partition, uint32_t offset, uint32_t size); 91 92 /** 93 * @brief Write data to an area on a Flash logical partition 94 * 95 * @param partition: The target flash logical partition which should be written 96 * @param buffer: Pointer to the data buffer that will be written to flash 97 * @param off_set: The offset of write address 98 * @param buffer_len: The length of the buffer 99 * 100 * @return 101 * - BK_OK: succeed 102 * - others: other errors 103 */ 104 bk_err_t bk_flash_partition_write(bk_partition_t partition, const uint8_t *buffer, uint32_t offset, uint32_t buffer_len); 105 106 /** 107 * @brief Read data from an area on a Flash to data buffer in RAM 108 * 109 * @param partition: The target flash logical partition which should be read 110 * @param out_buffer: Pointer to the data buffer that stores the data read from flash 111 * @param offsets: The offset of read address 112 * @param buffer_len: The length of the buffer 113 * 114 * @return 115 * - BK_OK: succeed 116 * - others: other errors 117 */ 118 bk_err_t bk_flash_partition_read(bk_partition_t partition, uint8_t *out_buffer, uint32_t offset, uint32_t buffer_len); 119 120 #ifdef __cplusplus 121 } 122 #endif 123 124