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_driver_write                          PORTABLE C      */
43 /*                                                           6.2.1       */
44 /*  AUTHOR                                                                */
45 /*                                                                        */
46 /*    William E. Lamie, Microsoft Corporation                             */
47 /*                                                                        */
48 /*  DESCRIPTION                                                           */
49 /*                                                                        */
50 /*    This function performs a write of the NOR flash memory.             */
51 /*                                                                        */
52 /*  INPUT                                                                 */
53 /*                                                                        */
54 /*    nor_flash                             NOR flash instance            */
55 /*    flash_address                         Address of NOR flash to write */
56 /*    source                                Source for the write          */
57 /*    words                                 Number of words to write      */
58 /*                                                                        */
59 /*  OUTPUT                                                                */
60 /*                                                                        */
61 /*    return status                                                       */
62 /*                                                                        */
63 /*  CALLS                                                                 */
64 /*                                                                        */
65 /*    (lx_nor_flash_driver_write)           Actual driver write           */
66 /*                                                                        */
67 /*  CALLED BY                                                             */
68 /*                                                                        */
69 /*    Application Code                                                    */
70 /*                                                                        */
71 /*  RELEASE HISTORY                                                       */
72 /*                                                                        */
73 /*    DATE              NAME                      DESCRIPTION             */
74 /*                                                                        */
75 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
76 /*  09-30-2020     William E. Lamie         Modified comment(s),          */
77 /*                                            resulting in version 6.1    */
78 /*  06-02-2021     Bhupendra Naphade        Modified comment(s),          */
79 /*                                            resulting in version 6.1.7  */
80 /*  03-08-2023     Xiuwen Cai               Modified comment(s),          */
81 /*                                            added new driver interface, */
82 /*                                            resulting in version 6.2.1 */
83 /*                                                                        */
84 /**************************************************************************/
_lx_nor_flash_driver_write(LX_NOR_FLASH * nor_flash,ULONG * flash_address,ULONG * source,ULONG words)85 UINT  _lx_nor_flash_driver_write(LX_NOR_FLASH *nor_flash, ULONG *flash_address, ULONG *source, ULONG words)
86 {
87 
88 #ifndef LX_NOR_DISABLE_EXTENDED_CACHE
89 
90 UINT    status;
91 UINT    i;
92 ULONG   *cache_entry_start;
93 ULONG   *cache_entry_end;
94 ULONG   cache_offset;
95 
96 
97     /* Is the request a whole sector or a partial sector.  */
98     if ((words == 1) && (nor_flash -> lx_nor_flash_extended_cache_entries))
99     {
100 
101         /* One word request, which implies that it is a NOR flash metadata write.  */
102 
103         /* Loop through the cache entries to see if there is a sector in cache.  */
104         for (i = 0; i < nor_flash -> lx_nor_flash_extended_cache_entries; i++)
105         {
106 
107             /* Search through the cache to see if there is a cache entry.  */
108 
109             /* Determine the cache entry addresses.  */
110             cache_entry_start =  nor_flash -> lx_nor_flash_extended_cache[i].lx_nor_flash_extended_cache_entry_sector_address;
111             cache_entry_end =    cache_entry_start + LX_NOR_SECTOR_SIZE;
112 
113             /* Determine if the flash address in in the cache entry.  */
114             if ((cache_entry_start) && (flash_address >= cache_entry_start) && (flash_address < cache_entry_end))
115             {
116 
117                 /* Yes, we found the entry.  */
118 
119                 /* Calculate the offset into the cache entry.  */
120                 cache_offset =  (ULONG)(flash_address - cache_entry_start);
121 
122                 /* Copy the word into the cache.  */
123                 *(nor_flash -> lx_nor_flash_extended_cache[i].lx_nor_flash_extended_cache_entry_sector_memory + cache_offset) =  *source;
124 
125                 /* Get out of the loop.  */
126                 break;
127             }
128         }
129     }
130 
131     /* In any case, call the actual driver write function.  */
132 #ifdef LX_NOR_ENABLE_CONTROL_BLOCK_FOR_DRIVER_INTERFACE
133     status =  (nor_flash -> lx_nor_flash_driver_write)(nor_flash, flash_address, source, words);
134 #else
135     status =  (nor_flash -> lx_nor_flash_driver_write)(flash_address, source, words);
136 #endif
137 
138     /* Return completion status.  */
139     return(status);
140 
141 #else
142 UINT    status;
143 
144 
145     /* Call the actual driver write function.  */
146 #ifdef LX_NOR_ENABLE_CONTROL_BLOCK_FOR_DRIVER_INTERFACE
147     status =  (nor_flash -> lx_nor_flash_driver_write)(nor_flash, flash_address, source, words);
148 #else
149     status =  (nor_flash -> lx_nor_flash_driver_write)(flash_address, source, words);
150 #endif
151 
152     /* Return completion status.  */
153     return(status);
154 #endif
155 }
156 
157