GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lx_nor_flash_driver_read.c Lines: 28 30 93.3 %
Date: 2026-03-06 18:45:40 Branches: 18 20 90.0 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 * Copyright (c) 2026-present Eclipse ThreadX contributors
4
 *
5
 * This program and the accompanying materials are made available under the
6
 * terms of the MIT License which is available at
7
 * https://opensource.org/licenses/MIT.
8
 *
9
 * SPDX-License-Identifier: MIT
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_extended_cache_read                  PORTABLE C       */
43
/*                                                           6.2.1       */
44
/*  AUTHOR                                                                */
45
/*                                                                        */
46
/*    William E. Lamie, Microsoft Corporation                             */
47
/*                                                                        */
48
/*  DESCRIPTION                                                           */
49
/*                                                                        */
50
/*    This function performs a read of the NOR flash memory.              */
51
/*                                                                        */
52
/*  INPUT                                                                 */
53
/*                                                                        */
54
/*    nor_flash                             NOR flash instance            */
55
/*    flash_address                         Address of NOR flash to read  */
56
/*    destination                           Destination for the read      */
57
/*    words                                 Number of words to read       */
58
/*                                                                        */
59
/*  OUTPUT                                                                */
60
/*                                                                        */
61
/*    return status                                                       */
62
/*                                                                        */
63
/*  CALLS                                                                 */
64
/*                                                                        */
65
/*    (lx_nor_flash_driver_read)            Actual driver read            */
66
/*                                                                        */
67
/*  CALLED BY                                                             */
68
/*                                                                        */
69
/*    Application Code                                                    */
70
/*                                                                        */
71
/**************************************************************************/
72
361155
UINT  _lx_nor_flash_driver_read(LX_NOR_FLASH *nor_flash, ULONG *flash_address, ULONG *destination, ULONG words)
73
{
74
#ifndef LX_NOR_DISABLE_EXTENDED_CACHE
75
76
UINT    status;
77
UINT    i;
78
ULONG   *cache_entry_start;
79
ULONG   *cache_entry_end;
80
ULONG   cache_offset;
81
UINT    least_used_cache_entry;
82
83
84
    /* Is the request a whole sector or a partial sector.  */
85

361155
    if ((words == 1) && (nor_flash -> lx_nor_flash_extended_cache_entries))
86
    {
87
88
        /* One word request, which implies that it is a NOR flash metadata read.  */
89
90
91
        /* Initialize the least used cache entry.  */
92
172242
        least_used_cache_entry =  0;
93
94
        do
95
        {
96
97
            /* Loop through the cache entries to see if there is a sector in cache.  */
98
515018
            for (i = 0; i < nor_flash -> lx_nor_flash_extended_cache_entries; i++)
99
            {
100
101
                /* Search through the cache to find the entry.  */
102
103
                /* Determine the cache entry addresses.  */
104
498870
                cache_entry_start =  nor_flash -> lx_nor_flash_extended_cache[i].lx_nor_flash_extended_cache_entry_sector_address;
105
498870
                cache_entry_end =    cache_entry_start + LX_NOR_SECTOR_SIZE;
106
107
                /* Determine if the flash address in in the cache entry.  */
108

498870
                if ((cache_entry_start) && (flash_address >= cache_entry_start) && (flash_address < cache_entry_end))
109
                {
110
111
                    /* Yes, we found the entry.  */
112
113
                    /* Increment the accessed count.  */
114
172242
                    nor_flash -> lx_nor_flash_extended_cache[i].lx_nor_flash_extended_cache_entry_access_count++;
115
116
                    /* Calculate the offset into the cache entry.  */
117
172242
                    cache_offset =  (ULONG)(flash_address - cache_entry_start);
118
119
                    /* Copy the word from the cache.  */
120
172242
                    *destination =  *(nor_flash -> lx_nor_flash_extended_cache[i].lx_nor_flash_extended_cache_entry_sector_memory + cache_offset);
121
122
                    /* Increment the number of cache hits.  */
123
172242
                    nor_flash -> lx_nor_flash_extended_cache_hits++;
124
125
                    /* Return success.  */
126
172242
                    return(LX_SUCCESS);
127
                }
128
                else
129
                {
130
131
                    /* Determine if we have a new least used sector.  */
132
326628
                    if (i != least_used_cache_entry)
133
                    {
134
135
                        /* Determine if this entry has a smaller accessed count.  */
136
192865
                        if (nor_flash -> lx_nor_flash_extended_cache[i].lx_nor_flash_extended_cache_entry_access_count <
137
192865
                            nor_flash -> lx_nor_flash_extended_cache[least_used_cache_entry].lx_nor_flash_extended_cache_entry_access_count)
138
                        {
139
140
                            /* New least used entry.  */
141
68961
                            least_used_cache_entry =  i;
142
                        }
143
                    }
144
                }
145
            }
146
147
            /* Now read in the sector into the cache.  */
148
16148
            cache_offset =  (ULONG)(flash_address - nor_flash -> lx_nor_flash_base_address);
149
16148
            cache_offset =  cache_offset & ~((ULONG) (LX_NOR_SECTOR_SIZE-1));
150
16148
            cache_entry_start =  nor_flash -> lx_nor_flash_base_address + cache_offset;
151
152
            /* Call the actual driver read function.  */
153
#ifdef LX_NOR_ENABLE_CONTROL_BLOCK_FOR_DRIVER_INTERFACE
154
            status =  (nor_flash -> lx_nor_flash_driver_read)(nor_flash, cache_entry_start,
155
                            nor_flash -> lx_nor_flash_extended_cache[least_used_cache_entry].lx_nor_flash_extended_cache_entry_sector_memory,
156
                            LX_NOR_SECTOR_SIZE);
157
#else
158
16148
            status =  (nor_flash -> lx_nor_flash_driver_read)(cache_entry_start,
159
                            nor_flash -> lx_nor_flash_extended_cache[least_used_cache_entry].lx_nor_flash_extended_cache_entry_sector_memory,
160
                            LX_NOR_SECTOR_SIZE);
161
#endif
162
163
            /* Determine if there was an error.  */
164
16148
            if (status != LX_SUCCESS)
165
            {
166
167
                /* Return the error to the caller.  */
168
                return(status);
169
            }
170
171
            /* Setup the cache entry.  */
172
16148
            nor_flash -> lx_nor_flash_extended_cache[least_used_cache_entry].lx_nor_flash_extended_cache_entry_sector_address =  cache_entry_start;
173
16148
            nor_flash -> lx_nor_flash_extended_cache[least_used_cache_entry].lx_nor_flash_extended_cache_entry_access_count =    0;
174
175
            /* Increment the number of cache misses.  */
176
16148
            nor_flash -> lx_nor_flash_extended_cache_misses++;
177
178
            /* Decrement the number of cache hits, so that the increment that will happen next will be cancelled out.  */
179
16148
            nor_flash -> lx_nor_flash_extended_cache_hits--;
180
181
16148
        } while (status == LX_SUCCESS);
182
183
        /* Return success.  */
184
        return(LX_SUCCESS);
185
    }
186
    else
187
    {
188
189
        /* Call the actual driver read function.  */
190
#ifdef LX_NOR_ENABLE_CONTROL_BLOCK_FOR_DRIVER_INTERFACE
191
        status =  (nor_flash -> lx_nor_flash_driver_read)(nor_flash, flash_address, destination, words);
192
#else
193
188913
        status =  (nor_flash -> lx_nor_flash_driver_read)(flash_address, destination, words);
194
#endif
195
196
        /* Return completion status.  */
197
188913
        return(status);
198
    }
199
#else
200
UINT    status;
201
202
203
    /* Call the actual driver read function.  */
204
#ifdef LX_NOR_ENABLE_CONTROL_BLOCK_FOR_DRIVER_INTERFACE
205
    status =  (nor_flash -> lx_nor_flash_driver_read)(nor_flash, flash_address, destination, words);
206
#else
207
    status =  (nor_flash -> lx_nor_flash_driver_read)(flash_address, destination, words);
208
#endif
209
210
    /* Return completion status.  */
211
    return(status);
212
#endif
213
}
214
215