GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lx_nor_flash_format.c Lines: 17 24 70.8 %
Date: 2026-01-12 23:08:29 Branches: 9 14 64.3 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2025 Microsoft Corporation
3
 * Copyright (c) 2025 STmicroelectronics
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
/** LevelX Component                                                      */
16
/**                                                                       */
17
/**   NOR Flash                                                           */
18
/**                                                                       */
19
/**************************************************************************/
20
/**************************************************************************/
21
22
#define LX_SOURCE_CODE
23
24
25
/* Disable ThreadX error checking.  */
26
27
#ifndef LX_DISABLE_ERROR_CHECKING
28
#define LX_DISABLE_ERROR_CHECKING
29
#endif
30
31
32
/* Include necessary system files.  */
33
34
#include "lx_api.h"
35
36
37
/**************************************************************************/
38
/*                                                                        */
39
/*  FUNCTION                                               RELEASE        */
40
/*                                                                        */
41
/*    _lx_nor_flash_format                                 PORTABLE C     */
42
/*                                                                        */
43
/*  DESCRIPTION                                                           */
44
/*                                                                        */
45
/*    This function opens a NOR flash instance then erases all the non    */
46
/*      erased blocks, then writes the erase count                        */
47
/*                                                                        */
48
/*  INPUT                                                                 */
49
/*                                                                        */
50
/*    nor_flash                             NOR flash instance            */
51
/*    name                                  Name of NOR flash instance    */
52
/*    nor_driver_initialize                 Driver initialize             */
53
/*    nor_driver_info_ptr                   Drivier user data pointer     */
54
/*                                                                        */
55
/*  OUTPUT                                                                */
56
/*                                                                        */
57
/*    return status                                                       */
58
/*                                                                        */
59
/*  CALLS                                                                 */
60
/*                                                                        */
61
/*    (nor_driver_initialize)               Driver initialize             */
62
/*    _lx_nor_flash_driver_read             Driver read                   */
63
/*    _lx_nor_flash_driver_write            Driver write                  */
64
/*    (lx_nor_flash_driver_block_erased_verify)                           */
65
/*                                          NOR flash verify block erased */
66
/*    _lx_nor_flash_driver_block_erase      Driver block erase            */
67
/*                                                                        */
68
/*  CALLED BY                                                             */
69
/*                                                                        */
70
/*    Application Code                                                    */
71
/*                                                                        */
72
/**************************************************************************/
73
74
7
UINT  _lx_nor_flash_format(LX_NOR_FLASH  *nor_flash, CHAR *name, UINT (*nor_driver_initialize)(LX_NOR_FLASH *), VOID *nor_driver_info_ptr)
75
{
76
77
ULONG           l;
78
UINT            status;
79
80
ULONG           block_word;
81
ULONG           *block_word_ptr;
82
83
    LX_PARAMETER_NOT_USED(name);
84
85
    /* Clear the NOR flash control block. User extension is not cleared.  */
86
3423
    LX_MEMSET(nor_flash, 0, (ULONG)((UCHAR*)&(nor_flash -> lx_nor_flash_open_previous) - (UCHAR*)nor_flash) + sizeof(nor_flash -> lx_nor_flash_open_previous));
87
88
7
    nor_flash -> lx_nor_flash_driver_info_ptr = nor_driver_info_ptr;
89
90
    /* Call the flash driver's initialization function.  */
91
7
    (nor_driver_initialize)(nor_flash);
92
93
#ifndef LX_DIRECT_READ
94
95
    /* Determine if the driver supplied a RAM buffer for reading the NOR sector if direct read is not
96
       supported.  */
97
7
    if (nor_flash -> lx_nor_flash_sector_buffer == LX_NULL)
98
    {
99
        /* Return an error.  */
100
        return(LX_NO_MEMORY);
101
    }
102
#endif
103
104
63
    for (l = 0; l < nor_flash -> lx_nor_flash_total_blocks; l++)
105
    {
106
#ifdef LX_NOR_ENABLE_CONTROL_BLOCK_FOR_DRIVER_INTERFACE
107
        /* Check that the block is already erased */
108
        status =  (nor_flash -> lx_nor_flash_driver_block_erased_verify)(nor_flash, l);
109
#else
110
56
        status =  (nor_flash -> lx_nor_flash_driver_block_erased_verify)(l);
111
112
#endif
113
114
        /* the block is not erased, thus make sure to erase it */
115
56
        if (status)
116
        {
117
            /* the max erase count is not needed just pass the value 0 */
118
56
            status =  _lx_nor_flash_driver_block_erase(nor_flash, l, 0);
119
            /* Check for an error from flash driver */
120
56
            if (status)
121
            {
122
                /* Call system error handler.  */
123
                _lx_nor_flash_system_error(nor_flash, status);
124
125
                /* Return an error.  */
126
               return(LX_ERROR);
127
            }
128
129
            /* check that the sector was correctly erased */
130
#ifdef LX_NOR_ENABLE_CONTROL_BLOCK_FOR_DRIVER_INTERFACE
131
            status =  (nor_flash -> lx_nor_flash_driver_block_erased_verify)(nor_flash, l);
132
#else
133
56
            status =  (nor_flash -> lx_nor_flash_driver_block_erased_verify)(l);
134
#endif
135
56
            if (status)
136
            {
137
138
                /* Call system error handler.  */
139
                _lx_nor_flash_system_error(nor_flash, status);
140
141
                /* Return an error.  */
142
                return(LX_ERROR);
143
            }
144
        }
145
146
        /* Setup the block word pointer to the first word of the first block */
147
56
        block_word_ptr =  nor_flash -> lx_nor_flash_base_address + l * nor_flash -> lx_nor_flash_words_per_block;
148
149
        /* Setup the initial erase_count to 1 and mark the block as erased.
150
         * When the lx_nor_flash_open() gets called only the erase count will be kept.
151
         */
152
56
        block_word =  ((ULONG)1 | LX_BLOCK_ERASED);
153
154
56
        status =  _lx_nor_flash_driver_write(nor_flash, block_word_ptr, &block_word, 1);
155
56
        if (status)
156
        {
157
            /* Call system error handler.  */
158
            _lx_nor_flash_system_error(nor_flash, status);
159
160
            /* Return an error.  */
161
            return(LX_ERROR);
162
        }
163
164
    }
165
166
7
    return(LX_SUCCESS);
167
}