1 /**************************************************************************/
2 /* */
3 /* Copyright (c) Microsoft Corporation. All rights reserved. */
4 /* */
5 /* This software is licensed under the Microsoft Software License */
6 /* Terms for Microsoft Azure RTOS. Full text of the license can be */
7 /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */
8 /* and in the root directory of this software. */
9 /* */
10 /**************************************************************************/
11
12
13 /**************************************************************************/
14 /**************************************************************************/
15 /** */
16 /** LevelX Component */
17 /** */
18 /** NOR Flash */
19 /** */
20 /**************************************************************************/
21 /**************************************************************************/
22
23 #define LX_SOURCE_CODE
24
25
26 /* Disable ThreadX error checking. */
27
28 #ifndef LX_DISABLE_ERROR_CHECKING
29 #define LX_DISABLE_ERROR_CHECKING
30 #endif
31
32
33 /* Include necessary system files. */
34
35 #include "lx_api.h"
36
37
38 /**************************************************************************/
39 /* */
40 /* FUNCTION RELEASE */
41 /* */
42 /* _lx_nor_flash_sector_read PORTABLE C */
43 /* 6.1.7 */
44 /* AUTHOR */
45 /* */
46 /* William E. Lamie, Microsoft Corporation */
47 /* */
48 /* DESCRIPTION */
49 /* */
50 /* This function reads a logical sector from NOR flash. */
51 /* */
52 /* INPUT */
53 /* */
54 /* nor_flash NOR flash instance */
55 /* logical_sector Logical sector number */
56 /* buffer Pointer to buffer to read into*/
57 /* (the size is 512 bytes) */
58 /* */
59 /* OUTPUT */
60 /* */
61 /* return status */
62 /* */
63 /* CALLS */
64 /* */
65 /* _lx_nor_flash_driver_write Driver flash sector write */
66 /* _lx_nor_flash_driver_read Driver flash sector read */
67 /* _lx_nor_flash_logical_sector_find Find logical sector */
68 /* _lx_nor_flash_physical_sector_allocate */
69 /* Allocate new logical sector */
70 /* _lx_nor_flash_system_error Internal system error handler */
71 /* tx_mutex_get Get thread protection */
72 /* tx_mutex_put Release thread protection */
73 /* */
74 /* CALLED BY */
75 /* */
76 /* Application Code */
77 /* */
78 /* RELEASE HISTORY */
79 /* */
80 /* DATE NAME DESCRIPTION */
81 /* */
82 /* 05-19-2020 William E. Lamie Initial Version 6.0 */
83 /* 09-30-2020 William E. Lamie Modified comment(s), */
84 /* resulting in version 6.1 */
85 /* 06-02-2021 Bhupendra Naphade Modified comment(s), */
86 /* resulting in version 6.1.7 */
87 /* */
88 /**************************************************************************/
_lx_nor_flash_sector_read(LX_NOR_FLASH * nor_flash,ULONG logical_sector,VOID * buffer)89 UINT _lx_nor_flash_sector_read(LX_NOR_FLASH *nor_flash, ULONG logical_sector, VOID *buffer)
90 {
91
92 UINT status;
93 ULONG *mapping_address;
94 ULONG mapping_entry;
95 ULONG *sector_address;
96
97
98 #ifdef LX_THREAD_SAFE_ENABLE
99
100 /* Obtain the thread safe mutex. */
101 tx_mutex_get(&nor_flash -> lx_nor_flash_mutex, TX_WAIT_FOREVER);
102 #endif
103
104 /* Increment the number of read requests. */
105 nor_flash -> lx_nor_flash_read_requests++;
106
107 /* See if we can find the sector in the current mapping. */
108 _lx_nor_flash_logical_sector_find(nor_flash, logical_sector, LX_FALSE, &mapping_address, §or_address);
109
110 /* Determine if the logical sector was found. */
111 if (mapping_address)
112 {
113
114 /* Yes, we were able to find the logical sector. */
115
116 /* Read the sector data from the physical sector. */
117 status = _lx_nor_flash_driver_read(nor_flash, sector_address, buffer, LX_NOR_SECTOR_SIZE);
118
119 /* Check for an error from flash driver. Drivers should never return an error.. */
120 if (status)
121 {
122
123 /* Call system error handler. */
124 _lx_nor_flash_system_error(nor_flash, status);
125
126 /* Adjust return status. */
127 status = LX_ERROR;
128 }
129 else
130 {
131
132 /* Set the status to success. */
133 status = LX_SUCCESS;
134 }
135 }
136 else
137 {
138
139 /* Allocate a new physical sector for this write. */
140 _lx_nor_flash_physical_sector_allocate(nor_flash, logical_sector, &mapping_address, §or_address);
141
142 /* Determine if the new sector allocation was successful. */
143 if (mapping_address)
144 {
145
146 /* Update the number of free physical sectors. */
147 nor_flash -> lx_nor_flash_free_physical_sectors--;
148
149 /* Read the sector data from the physical sector. */
150 status = _lx_nor_flash_driver_read(nor_flash, sector_address, buffer, LX_NOR_SECTOR_SIZE);
151
152 /* Check for an error from flash driver. Drivers should never return an error.. */
153 if (status)
154 {
155
156 /* Call system error handler. */
157 _lx_nor_flash_system_error(nor_flash, status);
158 }
159
160 /* Now build the new mapping entry. */
161 mapping_entry = ((ULONG) LX_NOR_PHYSICAL_SECTOR_VALID) | ((ULONG) LX_NOR_PHYSICAL_SECTOR_SUPERCEDED) | logical_sector;
162
163 /* Write out the new mapping entry. */
164 status = _lx_nor_flash_driver_write(nor_flash, mapping_address, &mapping_entry, 1);
165
166 /* Check for an error from flash driver. Drivers should never return an error.. */
167 if (status)
168 {
169
170 /* Call system error handler. */
171 _lx_nor_flash_system_error(nor_flash, status);
172
173 #ifdef LX_THREAD_SAFE_ENABLE
174
175 /* Release the thread safe mutex. */
176 tx_mutex_put(&nor_flash -> lx_nor_flash_mutex);
177 #endif
178
179 /* Return status. */
180 return(LX_ERROR);
181 }
182
183 /* Increment the number of mapped physical sectors. */
184 nor_flash -> lx_nor_flash_mapped_physical_sectors++;
185
186 /* Set the status to success. */
187 status = LX_SUCCESS;
188 }
189 else
190 {
191
192 /* Could not find the logical sector. */
193 status = LX_SECTOR_NOT_FOUND;
194 }
195 }
196
197 #ifdef LX_THREAD_SAFE_ENABLE
198
199 /* Release the thread safe mutex. */
200 tx_mutex_put(&nor_flash -> lx_nor_flash_mutex);
201 #endif
202
203 /* Return status. */
204 return(status);
205 }
206
207