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_close                                 PORTABLE C      */
43 /*                                                           6.1.7        */
44 /*  AUTHOR                                                                */
45 /*                                                                        */
46 /*    William E. Lamie, Microsoft Corporation                             */
47 /*                                                                        */
48 /*  DESCRIPTION                                                           */
49 /*                                                                        */
50 /*    This function closes a NOR flash instance.                          */
51 /*                                                                        */
52 /*  INPUT                                                                 */
53 /*                                                                        */
54 /*    nor_flash                             NOR flash instance            */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    return status                                                       */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    tx_mutex_delete                       Delete thread-safe mutex      */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    Application Code                                                    */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
73 /*  09-30-2020     William E. Lamie         Modified comment(s),          */
74 /*                                            resulting in version 6.1    */
75 /*  06-02-2021     Bhupendra Naphade        Modified comment(s),          */
76 /*                                            resulting in version 6.1.7  */
77 /*                                                                        */
78 /**************************************************************************/
_lx_nor_flash_close(LX_NOR_FLASH * nor_flash)79 UINT  _lx_nor_flash_close(LX_NOR_FLASH *nor_flash)
80 {
81 
82 LX_INTERRUPT_SAVE_AREA
83 
84 
85     /* Lockout interrupts for NOR flash close.  */
86     LX_DISABLE
87 
88     /* See if the media is the only one on the media opened list.  */
89     if ((_lx_nor_flash_opened_ptr == nor_flash) &&
90         (_lx_nor_flash_opened_ptr == nor_flash -> lx_nor_flash_open_next) &&
91         (_lx_nor_flash_opened_ptr == nor_flash -> lx_nor_flash_open_previous))
92     {
93 
94         /* Only opened NOR flash, just set the opened list to NULL.  */
95         _lx_nor_flash_opened_ptr =  LX_NULL;
96     }
97     else
98     {
99 
100         /* Otherwise, not the only opened NOR flash, link-up the neighbors.  */
101         (nor_flash -> lx_nor_flash_open_next) -> lx_nor_flash_open_previous =
102                                             nor_flash -> lx_nor_flash_open_previous;
103         (nor_flash -> lx_nor_flash_open_previous) -> lx_nor_flash_open_next =
104                                             nor_flash -> lx_nor_flash_open_next;
105 
106         /* See if we have to update the opened list head pointer.  */
107         if (_lx_nor_flash_opened_ptr == nor_flash)
108         {
109 
110             /* Yes, move the head pointer to the next opened NOR flash. */
111             _lx_nor_flash_opened_ptr =  nor_flash -> lx_nor_flash_open_next;
112         }
113     }
114 
115     /* Decrement the opened NOR flash counter.  */
116     _lx_nor_flash_opened_count--;
117 
118     /* Finally, indicate that this NOR flash is closed.  */
119     nor_flash -> lx_nor_flash_state =  LX_NOR_FLASH_CLOSED;
120 
121     /* Restore interrupt posture.  */
122     LX_RESTORE
123 
124 #ifdef LX_THREAD_SAFE_ENABLE
125 
126     /* Delete the thread safe mutex.  */
127     tx_mutex_delete(&nor_flash -> lx_nor_flash_mutex);
128 #endif
129     /* Return success.  */
130     return(LX_SUCCESS);
131 }
132 
133 
134