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.3.0        */
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 /*  10-31-2023     Xiuwen Cai               Modified comment(s),          */
88 /*                                            added mapping bitmap cache, */
89 /*                                            resulting in version 6.3.0  */
90 /*                                                                        */
91 /**************************************************************************/
_lx_nor_flash_sector_read(LX_NOR_FLASH * nor_flash,ULONG logical_sector,VOID * buffer)92 UINT  _lx_nor_flash_sector_read(LX_NOR_FLASH *nor_flash, ULONG logical_sector, VOID *buffer)
93 {
94 
95 UINT    status;
96 ULONG   *mapping_address;
97 ULONG   mapping_entry;
98 ULONG   *sector_address;
99 
100 
101 #ifdef LX_THREAD_SAFE_ENABLE
102 
103     /* Obtain the thread safe mutex.  */
104     tx_mutex_get(&nor_flash -> lx_nor_flash_mutex, TX_WAIT_FOREVER);
105 #endif
106 
107     /* Increment the number of read requests.  */
108     nor_flash -> lx_nor_flash_read_requests++;
109 
110     /* See if we can find the sector in the current mapping.  */
111     _lx_nor_flash_logical_sector_find(nor_flash, logical_sector, LX_FALSE, &mapping_address, &sector_address);
112 
113     /* Determine if the logical sector was found.  */
114     if (mapping_address)
115     {
116 
117         /* Yes, we were able to find the logical sector.  */
118 
119         /* Read the sector data from the physical sector.  */
120         status =  _lx_nor_flash_driver_read(nor_flash, sector_address, buffer, LX_NOR_SECTOR_SIZE);
121 
122         /* Check for an error from flash driver. Drivers should never return an error..  */
123         if (status)
124         {
125 
126             /* Call system error handler.  */
127             _lx_nor_flash_system_error(nor_flash, status);
128 
129             /* Adjust return status.  */
130             status =  LX_ERROR;
131         }
132         else
133         {
134 
135             /* Set the status to success.  */
136             status =  LX_SUCCESS;
137         }
138     }
139     else
140     {
141 
142         /* Allocate a new physical sector for this write.  */
143         _lx_nor_flash_physical_sector_allocate(nor_flash, logical_sector, &mapping_address, &sector_address);
144 
145         /* Determine if the new sector allocation was successful.  */
146         if (mapping_address)
147         {
148 
149             /* Update the number of free physical sectors.  */
150             nor_flash -> lx_nor_flash_free_physical_sectors--;
151 
152             /* Read the sector data from the physical sector.  */
153             status =  _lx_nor_flash_driver_read(nor_flash, sector_address, buffer, LX_NOR_SECTOR_SIZE);
154 
155             /* Check for an error from flash driver. Drivers should never return an error..  */
156             if (status)
157             {
158 
159                 /* Call system error handler.  */
160                 _lx_nor_flash_system_error(nor_flash, status);
161             }
162 
163             /* Now build the new mapping entry.  */
164             mapping_entry =  ((ULONG) LX_NOR_PHYSICAL_SECTOR_VALID) | ((ULONG) LX_NOR_PHYSICAL_SECTOR_SUPERCEDED) | logical_sector;
165 
166             /* Write out the new mapping entry.  */
167             status =  _lx_nor_flash_driver_write(nor_flash, mapping_address, &mapping_entry, 1);
168 
169             /* Check for an error from flash driver. Drivers should never return an error..  */
170             if (status)
171             {
172 
173                 /* Call system error handler.  */
174                 _lx_nor_flash_system_error(nor_flash, status);
175 
176 #ifdef LX_THREAD_SAFE_ENABLE
177 
178                 /* Release the thread safe mutex.  */
179                 tx_mutex_put(&nor_flash -> lx_nor_flash_mutex);
180 #endif
181 
182                 /* Return status.  */
183                 return(LX_ERROR);
184             }
185 #ifndef LX_NOR_DISABLE_EXTENDED_CACHE
186 #ifdef LX_NOR_ENABLE_MAPPING_BITMAP
187 
188             /* Determine if the logical sector is within the mapping bitmap.  */
189             if (logical_sector < nor_flash -> lx_nor_flash_extended_cache_mapping_bitmap_max_logical_sector)
190             {
191 
192                 /* Set the bit in the mapping bitmap.  */
193                 nor_flash -> lx_nor_flash_extended_cache_mapping_bitmap[logical_sector >> 5] |= (ULONG)(1 << (logical_sector & 31));
194             }
195 #endif
196 #endif
197 
198             /* Increment the number of mapped physical sectors.  */
199             nor_flash -> lx_nor_flash_mapped_physical_sectors++;
200 
201             /* Set the status to success.  */
202             status =  LX_SUCCESS;
203         }
204         else
205         {
206 
207             /* Could not find the logical sector.  */
208             status =  LX_SECTOR_NOT_FOUND;
209         }
210     }
211 
212 #ifdef LX_THREAD_SAFE_ENABLE
213 
214     /* Release the thread safe mutex.  */
215     tx_mutex_put(&nor_flash -> lx_nor_flash_mutex);
216 #endif
217 
218     /* Return status.  */
219     return(status);
220 }
221 
222