1 /*
2  * Copyright (C) 2017-2019 Alibaba Group Holding Limited
3  */
4 
5 /******************************************************************************
6  * @file     drv_eflash.h
7  * @brief    header file for eflash driver
8  * @version  V1.0
9  * @date     02. June 2017
10  * @model    eflash
11  ******************************************************************************/
12 #ifndef _CSI_EFLASH_H_
13 #define _CSI_EFLASH_H_
14 
15 
16 #include <stdint.h>
17 #include <drv_common.h>
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 /// definition for eflash handle.
24 typedef void *eflash_handle_t;
25 
26 /**
27 \brief Flash information
28 */
29 typedef struct {
30     uint32_t          start;              ///< Chip Start address
31     uint32_t          end;                ///< Chip End address (start+size-1)
32     uint32_t          sector_count;       ///< Number of sectors
33     uint32_t          sector_size;        ///< Uniform sector size in bytes
34     uint32_t          page_size;          ///< Optimal programming page size in bytes
35     uint32_t          program_unit;       ///< Smallest programmable unit in bytes
36     uint8_t           erased_value;       ///< Contents of erased memory (usually 0xFF)
37 } eflash_info_t;
38 
39 /**
40 \brief Flash Status
41 */
42 typedef struct {
43     uint32_t busy  : 1;                   ///< Flash busy flag
44     uint32_t error : 1;                   ///< Read/Program/Erase error flag (cleared on start of next operation)
45 } eflash_status_t;
46 
47 /****** EFLASH Event *****/
48 typedef enum {
49     EFLASH_EVENT_READY           = 0,  ///< Flash Ready
50     EFLASH_EVENT_ERROR,                ///< Read/Program/Erase Error
51 } eflash_event_e;
52 
53 typedef void (*eflash_event_cb_t)(int32_t idx, eflash_event_e event);   ///< Pointer to \ref eflash_event_cb_t : EFLASH Event call back.
54 
55 /**
56 \brief Flash Driver Capabilities.
57 */
58 typedef struct {
59     uint32_t event_ready  : 1;            ///< Signal Flash Ready event
60     uint32_t data_width   : 2;            ///< Data width: 0=8-bit, 1=16-bit, 2=32-bit
61     uint32_t erase_chip   : 1;            ///< Supports EraseChip operation
62 } eflash_capabilities_t;
63 
64 // Function documentation
65 
66 /**
67   \brief       Initialize EFLASH Interface. 1. Initializes the resources needed for the EFLASH interface 2.registers event callback function
68   \param[in]   idx  device id
69   \param[in]   cb_event  event callback function \ref eflash_event_cb_t
70   \return      pointer to eflash handle
71 */
72 eflash_handle_t csi_eflash_initialize(int32_t idx, eflash_event_cb_t cb_event);
73 
74 /**
75   \brief       De-initialize EFLASH Interface. stops operation and releases the software resources used by the interface
76   \param[in]   handle  eflash handle to operate.
77   \return      error code
78 */
79 int32_t csi_eflash_uninitialize(eflash_handle_t handle);
80 
81 /**
82   \brief       Get driver capabilities.
83   \param[in]   idx  device id
84   \return      \ref eflash_capabilities_t
85 */
86 eflash_capabilities_t csi_eflash_get_capabilities(int32_t idx);
87 
88 /**
89   \brief       control eflash power.
90   \param[in]   handle  eflash handle to operate.
91   \param[in]   state   power state.\ref csi_power_stat_e.
92   \return      error code
93 */
94 int32_t csi_eflash_power_control(eflash_handle_t handle, csi_power_stat_e state);
95 
96 /**
97   \brief       Read data from Flash.
98   \param[in]   handle  eflash handle to operate.
99   \param[in]   addr  Data address.
100   \param[out]  data  Pointer to a buffer storing the data read from Flash.
101   \param[in]   cnt   Number of data items to read.
102   \return      number of data items read or error code
103 */
104 int32_t csi_eflash_read(eflash_handle_t handle, uint32_t addr, void *data, uint32_t cnt);
105 
106 /**
107   \brief       Program data to Flash.
108   \param[in]   handle  eflash handle to operate.
109   \param[in]   addr  Data address.
110   \param[in]   data  Pointer to a buffer containing the data to be programmed to Flash.
111   \param[in]   cnt   Number of data items to program.
112   \return      number of data items programmed or error code
113 */
114 int32_t csi_eflash_program(eflash_handle_t handle, uint32_t addr, const void *data, uint32_t cnt);
115 
116 /**
117   \brief       Erase Flash Sector.
118   \param[in]   handle  eflash handle to operate.
119   \param[in]   addr  Sector address
120   \return      error code
121 */
122 int32_t csi_eflash_erase_sector(eflash_handle_t handle, uint32_t addr);
123 
124 /**
125   \brief       Erase complete Flash.
126   \param[in]   handle  eflash handle to operate.
127   \return      error code
128 */
129 int32_t csi_eflash_erase_chip(eflash_handle_t handle);
130 
131 /**
132   \brief       Get Flash information.
133   \param[in]   handle  eflash handle to operate.
134   \return      Pointer to Flash information \ref eflash_info_t
135 */
136 eflash_info_t *csi_eflash_get_info(eflash_handle_t handle);
137 
138 /**
139   \brief       Get FLASH status.
140   \param[in]   handle  eflash handle to operate.
141   \return      EFLASH status \ref eflash_status_t
142 */
143 eflash_status_t csi_eflash_get_status(eflash_handle_t handle);
144 
145 #ifdef __cplusplus
146 }
147 #endif
148 
149 #endif /* _CSI_EFLASH_H_ */
150