am_hal_mcuctrl.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //*****************************************************************************
  2. //
  3. // am_hal_mcuctrl.c
  4. //! @file
  5. //!
  6. //! @brief Functions for interfacing with the MCUCTRL.
  7. //!
  8. //! @addtogroup mcuctrl2 MCU Control (MCUCTRL)
  9. //! @ingroup apollo2hal
  10. //! @{
  11. //
  12. //*****************************************************************************
  13. //*****************************************************************************
  14. //
  15. // Copyright (c) 2017, Ambiq Micro
  16. // All rights reserved.
  17. //
  18. // Redistribution and use in source and binary forms, with or without
  19. // modification, are permitted provided that the following conditions are met:
  20. //
  21. // 1. Redistributions of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // 2. Redistributions in binary form must reproduce the above copyright
  25. // notice, this list of conditions and the following disclaimer in the
  26. // documentation and/or other materials provided with the distribution.
  27. //
  28. // 3. Neither the name of the copyright holder nor the names of its
  29. // contributors may be used to endorse or promote products derived from this
  30. // software without specific prior written permission.
  31. //
  32. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  33. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  36. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  37. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  38. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  39. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  40. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  42. // POSSIBILITY OF SUCH DAMAGE.
  43. //
  44. // This is part of revision 1.2.11 of the AmbiqSuite Development Package.
  45. //
  46. //*****************************************************************************
  47. #include <stdint.h>
  48. #include <stdbool.h>
  49. #include "am_mcu_apollo.h"
  50. #define LDO_TRIM_REG_ADDR (0x50023004)
  51. #define BUCK_TRIM_REG_ADDR (0x50023000)
  52. //*****************************************************************************
  53. //
  54. // Global Variables.
  55. //
  56. //*****************************************************************************
  57. //
  58. // Define the flash sizes from CHIP_INFO.
  59. //
  60. const uint32_t g_am_hal_mcuctrl_flash_size[16] =
  61. {
  62. 16 * 1024, /* 0x0 0x00004000 16 KB */
  63. 32 * 1024, /* 0x1 0x00008000 32 KB */
  64. 64 * 1024, /* 0x2 0x00010000 64 KB */
  65. 128 * 1024, /* 0x3 0x00020000 128 KB */
  66. 256 * 1024, /* 0x4 0x00040000 256 KB */
  67. 512 * 1024, /* 0x5 0x00080000 512 KB */
  68. 1 * 1024 * 1024, /* 0x6 0x00100000 1 MB */
  69. 2 * 1024 * 1024, /* 0x7 0x00200000 2 MB */
  70. 4 * 1024 * 1024, /* 0x8 0x00400000 4 MB */
  71. 8 * 1024 * 1024, /* 0x9 0x00800000 8 MB */
  72. 16 * 1024 * 1024, /* 0xA 0x01000000 16 MB */
  73. 32 * 1024 * 1024, /* 0xB 0x02000000 32 MB */
  74. 64 * 1024 * 1024, /* 0xC 0x04000000 64 MB */
  75. 128 * 1024 * 1024, /* 0xD 0x08000000 128 MB */
  76. 256 * 1024 * 1024, /* 0xE 0x10000000 256 MB */
  77. 512 * 1024 * 1024 /* 0xF 0x20000000 512 MB */
  78. };
  79. //
  80. // Define the SRAM sizes from CHIP_INFO.
  81. // For Apollo2, the SRAM sizes are defined exactly the same as the flash sizes.
  82. //
  83. #define g_am_hal_mcuctrl_sram_size g_am_hal_mcuctrl_flash_size
  84. //*****************************************************************************
  85. //
  86. //! @brief Gets all relevant device information.
  87. //!
  88. //! @param psDevice is a pointer to a structure that will be used to store all
  89. //! device info.
  90. //!
  91. //! This function gets the device part number, chip IDs, and revision and
  92. //! stores them in the passed structure.
  93. //!
  94. //! @return None
  95. //
  96. //*****************************************************************************
  97. void
  98. am_hal_mcuctrl_device_info_get(am_hal_mcuctrl_device_t *psDevice)
  99. {
  100. //
  101. // Read the Part Number.
  102. //
  103. psDevice->ui32ChipPN = AM_REG(MCUCTRL, CHIP_INFO);
  104. //
  105. // Read the Chip ID0.
  106. //
  107. psDevice->ui32ChipID0 = AM_REG(MCUCTRL, CHIPID0);
  108. //
  109. // Read the Chip ID1.
  110. //
  111. psDevice->ui32ChipID1 = AM_REG(MCUCTRL, CHIPID1);
  112. //
  113. // Read the Chip Revision.
  114. //
  115. psDevice->ui32ChipRev = AM_REG(MCUCTRL, CHIPREV);
  116. //
  117. // Read the Part Number.
  118. //
  119. psDevice->ui32ChipPN = AM_REG(MCUCTRL, CHIP_INFO);
  120. //
  121. // Read the Chip ID0.
  122. //
  123. psDevice->ui32ChipID0 = AM_REG(MCUCTRL, CHIPID0);
  124. //
  125. // Read the Chip ID1.
  126. //
  127. psDevice->ui32ChipID1 = AM_REG(MCUCTRL, CHIPID1);
  128. //
  129. // Read the Chip Revision.
  130. //
  131. psDevice->ui32ChipRev = AM_REG(MCUCTRL, CHIPREV);
  132. //
  133. // Read the Chip VENDOR ID.
  134. //
  135. psDevice->ui32VendorID = AM_REG(MCUCTRL, VENDORID);
  136. //
  137. // Qualified from Part Number.
  138. //
  139. psDevice->ui32Qualified =
  140. (psDevice->ui32ChipPN & AM_HAL_MCUCTRL_CHIP_INFO_QUAL_M) >>
  141. AM_HAL_MCUCTRL_CHIP_INFO_QUAL_S;
  142. //
  143. // Flash size from Part Number.
  144. //
  145. psDevice->ui32FlashSize =
  146. g_am_hal_mcuctrl_flash_size[
  147. (psDevice->ui32ChipPN & AM_HAL_MCUCTRL_CHIP_INFO_FLASH_SIZE_M) >>
  148. AM_HAL_MCUCTRL_CHIP_INFO_FLASH_SIZE_S];
  149. //
  150. // SRAM size from Part Number.
  151. //
  152. psDevice->ui32SRAMSize =
  153. g_am_hal_mcuctrl_flash_size[
  154. (psDevice->ui32ChipPN & AM_HAL_MCUCTRL_CHIP_INFO_SRAM_SIZE_M) >>
  155. AM_HAL_MCUCTRL_CHIP_INFO_SRAM_SIZE_S];
  156. //
  157. // Now, let's look at the JEDEC info.
  158. // The full partnumber is 12 bits total, but is scattered across 2 registers.
  159. // Bits [11:8] are 0xE.
  160. // Bits [7:4] are 0xE for Apollo, 0xD for Apollo2.
  161. // Bits [3:0] are defined differently for Apollo and Apollo2.
  162. // For Apollo, the low nibble is 0x0.
  163. // For Apollo2, the low nibble indicates flash and SRAM size.
  164. //
  165. psDevice->ui32JedecPN = (AM_BFR(JEDEC, PID0, PNL8) << 0);
  166. psDevice->ui32JedecPN |= (AM_BFR(JEDEC, PID1, PNH4) << 8);
  167. //
  168. // JEPID is the JEP-106 Manufacturer ID Code, which is assigned to Ambiq as
  169. // 0x1B, with parity bit is 0x9B. It is 8 bits located across 2 registers.
  170. //
  171. psDevice->ui32JedecJEPID = (AM_BFR(JEDEC, PID1, JEPIDL) << 0);
  172. psDevice->ui32JedecJEPID |= (AM_BFR(JEDEC, PID2, JEPIDH) << 4);
  173. //
  174. // CHIPREV is 8 bits located across 2 registers.
  175. //
  176. psDevice->ui32JedecCHIPREV = (AM_BFR(JEDEC, PID2, CHIPREVH4) << 4);
  177. psDevice->ui32JedecCHIPREV |= (AM_BFR(JEDEC, PID3, CHIPREVL4) << 0);
  178. //
  179. // Let's get the Coresight ID (32-bits across 4 registers)
  180. // For Apollo and Apollo2, it's expected to be 0xB105100D.
  181. //
  182. psDevice->ui32JedecCID = (AM_BFR(JEDEC, CID3, CID) << 24);
  183. psDevice->ui32JedecCID |= (AM_BFR(JEDEC, CID2, CID) << 16);
  184. psDevice->ui32JedecCID |= (AM_BFR(JEDEC, CID1, CID) << 8);
  185. psDevice->ui32JedecCID |= (AM_BFR(JEDEC, CID0, CID) << 0);
  186. }
  187. //*****************************************************************************
  188. //
  189. //! @brief Enables the fault capture registers.
  190. //!
  191. //! This function enables the DCODEFAULTADDR and ICODEFAULTADDR registers.
  192. //!
  193. //! @return None
  194. //
  195. //*****************************************************************************
  196. void
  197. am_hal_mcuctrl_fault_capture_enable(void)
  198. {
  199. //
  200. // Enable the Fault Capture registers.
  201. //
  202. AM_BFW(MCUCTRL, FAULTCAPTUREEN, ENABLE, 1);
  203. }
  204. //*****************************************************************************
  205. //
  206. //! @brief Disables the fault capture registers.
  207. //!
  208. //! This function disables the DCODEFAULTADDR and ICODEFAULTADDR registers.
  209. //!
  210. //! @return None
  211. //
  212. //*****************************************************************************
  213. void
  214. am_hal_mcuctrl_fault_capture_disable(void)
  215. {
  216. //
  217. // Disable the Fault Capture registers.
  218. //
  219. AM_BFW(MCUCTRL, FAULTCAPTUREEN, ENABLE, 0);
  220. }
  221. //*****************************************************************************
  222. //
  223. //! @brief Gets the fault status and capture registers.
  224. //!
  225. //! @param psFault is a pointer to a structure that will be used to store all
  226. //! fault info.
  227. //!
  228. //! This function gets the status of the ICODE, DCODE, and SYS bus faults and
  229. //! the addresses associated with the fault.
  230. //!
  231. //! @return None
  232. //
  233. //*****************************************************************************
  234. void
  235. am_hal_mcuctrl_fault_status(am_hal_mcuctrl_fault_t *psFault)
  236. {
  237. uint32_t ui32FaultStat;
  238. //
  239. // Read the Fault Status Register.
  240. //
  241. ui32FaultStat = AM_REG(MCUCTRL, FAULTSTATUS);
  242. psFault->bICODE = (ui32FaultStat & AM_REG_MCUCTRL_FAULTSTATUS_ICODE_M);
  243. psFault->bDCODE = (ui32FaultStat & AM_REG_MCUCTRL_FAULTSTATUS_DCODE_M);
  244. psFault->bSYS = (ui32FaultStat & AM_REG_MCUCTRL_FAULTSTATUS_SYS_M);
  245. //
  246. // Read the DCODE fault capture address register.
  247. //
  248. psFault->ui32DCODE = AM_REG(MCUCTRL, DCODEFAULTADDR);
  249. //
  250. // Read the ICODE fault capture address register.
  251. //
  252. psFault->ui32ICODE |= AM_REG(MCUCTRL, ICODEFAULTADDR);
  253. //
  254. // Read the ICODE fault capture address register.
  255. //
  256. psFault->ui32SYS |= AM_REG(MCUCTRL, SYSFAULTADDR);
  257. }
  258. //*****************************************************************************
  259. //
  260. // End Doxygen group.
  261. //! @}
  262. //
  263. //*****************************************************************************