1 //*****************************************************************************
2 //
3 //  am_hal_otp.c
4 //! @file
5 //!
6 //! @brief Functions for handling the OTP interface.
7 //
8 //*****************************************************************************
9 
10 //*****************************************************************************
11 //
12 // Copyright (c) 2017, Ambiq Micro
13 // All rights reserved.
14 //
15 // Redistribution and use in source and binary forms, with or without
16 // modification, are permitted provided that the following conditions are met:
17 //
18 // 1. Redistributions of source code must retain the above copyright notice,
19 // this list of conditions and the following disclaimer.
20 //
21 // 2. Redistributions in binary form must reproduce the above copyright
22 // notice, this list of conditions and the following disclaimer in the
23 // documentation and/or other materials provided with the distribution.
24 //
25 // 3. Neither the name of the copyright holder nor the names of its
26 // contributors may be used to endorse or promote products derived from this
27 // software without specific prior written permission.
28 //
29 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
30 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
33 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 // POSSIBILITY OF SUCH DAMAGE.
40 //
41 // This is part of revision 1.2.11 of the AmbiqSuite Development Package.
42 //
43 //*****************************************************************************
44 #include "am_mcu_apollo.h"
45 #include "am_hal_flash.h"
46 
47 //*****************************************************************************
48 //
49 //! THIS FUNCTION IS DEPRECATED!
50 //! Use the respective HAL flash function instead.
51 //!
52 // @brief Check if debugger is currently locked out.
53 //
54 // @param None.
55 //
56 // Determine if the debugger is already locked out.
57 //
58 // @return non-zero if debugger is currently locked out.
59 //     Specifically:
60 //     0 = debugger is not locked out.
61 //     1 = debugger is locked out.
62 //
63 //*****************************************************************************
64 int
am_hal_otp_is_debugger_lockedout(void)65 am_hal_otp_is_debugger_lockedout(void)
66 {
67     return am_hal_flash_debugger_disable_check();
68 }
69 
70 //*****************************************************************************
71 //
72 //! THIS FUNCTION IS DEPRECATED!
73 //! Use the respective HAL flash function instead.
74 //!
75 // @brief Lock out debugger access.
76 //
77 // @param None.
78 //
79 // This function locks out access by a debugger.
80 //
81 // @return 0 if lockout was successful or if lockout was already enabled.
82 //
83 //*****************************************************************************
84 int
am_hal_otp_debugger_lockout(void)85 am_hal_otp_debugger_lockout(void)
86 {
87     return am_hal_flash_debugger_disable();
88 }
89 
90 //*****************************************************************************
91 //
92 //! THIS FUNCTION IS DEPRECATED!
93 //! Use the respective HAL flash function instead.
94 //!
95 // @brief Lock out SRAM access.
96 //
97 // @param None.
98 //
99 // This function locks out access by a debugger to SRAM.
100 //
101 // @return 0 if lockout was successful or if lockout was already enabled.
102 //         Low byte=0xff, byte 1 contains current value of lockout.
103 //         Else, return value from HAL programming function.
104 //
105 //*****************************************************************************
106 int
am_hal_otp_sram_lockout(void)107 am_hal_otp_sram_lockout(void)
108 {
109     return am_hal_flash_wipe_sram_enable();
110 }
111 
112 //*****************************************************************************
113 //
114 //! THIS FUNCTION IS DEPRECATED!
115 //! Use the respective HAL flash function instead.
116 //!
117 // @brief Set copy (read) protection.
118 //
119 // @param @u32BegAddr The beginning address to be copy protected.
120 //        @u32EndAddr The ending address to be copy protected.
121 //
122 // @note For Apollo, the u32BegAddr parameter should be on a 16KB boundary, and
123 //       the u32EndAddr parameter should be on a (16KB-1) boundary. Otherwise
124 //       both parameters will be truncated/expanded to do so.
125 //       For example, if u32BegAddr=0x1000 and u32EndAddr=0xC200, the actual
126 //       range that protected is: 0x0 - 0xFFFF.
127 //
128 // This function enables copy protection on a given flash address range.
129 //
130 // @return 0 if copy protection was successfully enabled.
131 //
132 //*****************************************************************************
133 int
am_hal_otp_set_copy_protection(uint32_t u32BegAddr,uint32_t u32EndAddr)134 am_hal_otp_set_copy_protection(uint32_t u32BegAddr, uint32_t u32EndAddr)
135 {
136     return am_hal_flash_copy_protect_set((uint32_t*)u32BegAddr,
137                                          (uint32_t*)u32EndAddr);
138 }
139 
140 //*****************************************************************************
141 //
142 //! THIS FUNCTION IS DEPRECATED!
143 //! Use the respective HAL flash function instead.
144 //!
145 // @brief Set write protection.
146 //
147 // @param @u32BegAddr The beginning address to be write protected.
148 //        @u32EndAddr The ending address to be write protected.
149 //
150 // @note For Apollo, the u32BegAddr parameter should be on a 16KB boundary, and
151 //       the u32EndAddr parameter should be on a (16KB-1) boundary. Otherwise
152 //       both parameters will be truncated/expanded to do so.
153 //       For example, if u32BegAddr=0x1000 and u32EndAddr=0xC200, the actual
154 //       range that protected is: 0x0 - 0xFFFF.
155 //
156 // This function enables write protection on a given flash address range.
157 //
158 // @return 0 if write protection was successfully enabled.
159 //
160 //*****************************************************************************
161 int
am_hal_otp_set_write_protection(uint32_t u32BegAddr,uint32_t u32EndAddr)162 am_hal_otp_set_write_protection(uint32_t u32BegAddr, uint32_t u32EndAddr)
163 {
164     return am_hal_flash_write_protect_set((uint32_t*)u32BegAddr,
165                                           (uint32_t*)u32EndAddr);
166 }
167 
168 //*****************************************************************************
169 //
170 // End Doxygen group.
171 //! @}
172 //
173 //*****************************************************************************
174