flash.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. //*****************************************************************************
  2. //
  3. // flash.c - Driver for programming the on-chip flash.
  4. //
  5. // Copyright (c) 2005-2020 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions
  10. // are met:
  11. //
  12. // Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. //
  15. // Redistributions in binary form must reproduce the above copyright
  16. // notice, this list of conditions and the following disclaimer in the
  17. // documentation and/or other materials provided with the
  18. // distribution.
  19. //
  20. // Neither the name of Texas Instruments Incorporated nor the names of
  21. // its contributors may be used to endorse or promote products derived
  22. // from this software without specific prior written permission.
  23. //
  24. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. // This is part of revision 2.2.0.295 of the Tiva Peripheral Driver Library.
  37. //
  38. //*****************************************************************************
  39. //*****************************************************************************
  40. //
  41. //! \addtogroup flash_api
  42. //! @{
  43. //
  44. //*****************************************************************************
  45. #include <stdbool.h>
  46. #include <stdint.h>
  47. #include "inc/hw_flash.h"
  48. #include "inc/hw_ints.h"
  49. #include "inc/hw_sysctl.h"
  50. #include "inc/hw_types.h"
  51. #include "driverlib/debug.h"
  52. #include "driverlib/flash.h"
  53. #include "driverlib/interrupt.h"
  54. //*****************************************************************************
  55. //
  56. // An array that maps the specified memory bank to the appropriate Flash
  57. // Memory Protection Program Enable (FMPPE) register.
  58. //
  59. //*****************************************************************************
  60. static const uint32_t g_pui32FMPPERegs[] =
  61. {
  62. FLASH_FMPPE0,
  63. FLASH_FMPPE1,
  64. FLASH_FMPPE2,
  65. FLASH_FMPPE3,
  66. FLASH_FMPPE4,
  67. FLASH_FMPPE5,
  68. FLASH_FMPPE6,
  69. FLASH_FMPPE7,
  70. FLASH_FMPPE8,
  71. FLASH_FMPPE9,
  72. FLASH_FMPPE10,
  73. FLASH_FMPPE11,
  74. FLASH_FMPPE12,
  75. FLASH_FMPPE13,
  76. FLASH_FMPPE14,
  77. FLASH_FMPPE15,
  78. };
  79. //*****************************************************************************
  80. //
  81. // An array that maps the specified memory bank to the appropriate Flash
  82. // Memory Protection Read Enable (FMPRE) register.
  83. //
  84. //*****************************************************************************
  85. static const uint32_t g_pui32FMPRERegs[] =
  86. {
  87. FLASH_FMPRE0,
  88. FLASH_FMPRE1,
  89. FLASH_FMPRE2,
  90. FLASH_FMPRE3,
  91. FLASH_FMPRE4,
  92. FLASH_FMPRE5,
  93. FLASH_FMPRE6,
  94. FLASH_FMPRE7,
  95. FLASH_FMPRE8,
  96. FLASH_FMPRE9,
  97. FLASH_FMPRE10,
  98. FLASH_FMPRE11,
  99. FLASH_FMPRE12,
  100. FLASH_FMPRE13,
  101. FLASH_FMPRE14,
  102. FLASH_FMPRE15,
  103. };
  104. //*****************************************************************************
  105. //
  106. //! Erases a block of flash.
  107. //!
  108. //! \param ui32Address is the start address of the flash block to be erased.
  109. //!
  110. //! This function erases a block of the on-chip flash. After erasing, the
  111. //! block is filled with 0xFF bytes. Read-only and execute-only blocks cannot
  112. //! be erased.
  113. //!
  114. //! The flash block size is device-class dependent. All TM4C123x devices use
  115. //! 1-KB blocks but TM4C129x devices use 16-KB blocks. Please consult the
  116. //! device datasheet to determine the block size in use.
  117. //!
  118. //! This function does not return until the block has been erased.
  119. //!
  120. //! \return Returns 0 on success, or -1 if an invalid block address was
  121. //! specified or the block is write-protected.
  122. //
  123. //*****************************************************************************
  124. int32_t
  125. FlashErase(uint32_t ui32Address)
  126. {
  127. //
  128. // Check the arguments.
  129. //
  130. ASSERT(!(ui32Address & (FLASH_ERASE_SIZE - 1)));
  131. //
  132. // Clear the flash access and error interrupts.
  133. //
  134. HWREG(FLASH_FCMISC) = (FLASH_FCMISC_AMISC | FLASH_FCMISC_VOLTMISC |
  135. FLASH_FCMISC_ERMISC);
  136. //
  137. // Erase the block.
  138. //
  139. HWREG(FLASH_FMA) = ui32Address;
  140. HWREG(FLASH_FMC) = FLASH_FMC_WRKEY | FLASH_FMC_ERASE;
  141. //
  142. // Wait until the block has been erased.
  143. //
  144. while(HWREG(FLASH_FMC) & FLASH_FMC_ERASE)
  145. {
  146. }
  147. //
  148. // Return an error if an access violation or erase error occurred.
  149. //
  150. if(HWREG(FLASH_FCRIS) & (FLASH_FCRIS_ARIS | FLASH_FCRIS_VOLTRIS |
  151. FLASH_FCRIS_ERRIS))
  152. {
  153. return(-1);
  154. }
  155. //
  156. // Success.
  157. //
  158. return(0);
  159. }
  160. //*****************************************************************************
  161. //
  162. //! Programs flash.
  163. //!
  164. //! \param pui32Data is a pointer to the data to be programmed.
  165. //! \param ui32Address is the starting address in flash to be programmed. Must
  166. //! be a multiple of four.
  167. //! \param ui32Count is the number of bytes to be programmed. Must be a
  168. //! multiple of four.
  169. //!
  170. //! This function programs a sequence of words into the on-chip flash.
  171. //! Because the flash is programmed one word at a time, the starting address
  172. //! and byte count must both be multiples of four. It is up to the caller to
  173. //! verify the programmed contents, if such verification is required.
  174. //!
  175. //! This function does not return until the data has been programmed.
  176. //!
  177. //! \return Returns 0 on success, or -1 if a programming error is encountered.
  178. //
  179. //*****************************************************************************
  180. int32_t
  181. FlashProgram(uint32_t *pui32Data, uint32_t ui32Address, uint32_t ui32Count)
  182. {
  183. //
  184. // Check the arguments.
  185. //
  186. ASSERT(!(ui32Address & 3));
  187. ASSERT(!(ui32Count & 3));
  188. //
  189. // Clear the flash access and error interrupts.
  190. //
  191. HWREG(FLASH_FCMISC) = (FLASH_FCMISC_AMISC | FLASH_FCMISC_VOLTMISC |
  192. FLASH_FCMISC_INVDMISC | FLASH_FCMISC_PROGMISC);
  193. //
  194. // Loop over the words to be programmed.
  195. //
  196. while(ui32Count)
  197. {
  198. //
  199. // Set the address of this block of words.
  200. //
  201. HWREG(FLASH_FMA) = ui32Address & ~(0x7f);
  202. //
  203. // Loop over the words in this 32-word block.
  204. //
  205. while(((ui32Address & 0x7c) || (HWREG(FLASH_FWBVAL) == 0)) &&
  206. (ui32Count != 0))
  207. {
  208. //
  209. // Write this word into the write buffer.
  210. //
  211. HWREG(FLASH_FWBN + (ui32Address & 0x7c)) = *pui32Data++;
  212. ui32Address += 4;
  213. ui32Count -= 4;
  214. }
  215. //
  216. // Program the contents of the write buffer into flash.
  217. //
  218. HWREG(FLASH_FMC2) = FLASH_FMC2_WRKEY | FLASH_FMC2_WRBUF;
  219. //
  220. // Wait until the write buffer has been programmed.
  221. //
  222. while(HWREG(FLASH_FMC2) & FLASH_FMC2_WRBUF)
  223. {
  224. }
  225. }
  226. //
  227. // Return an error if an access violation occurred.
  228. //
  229. if(HWREG(FLASH_FCRIS) & (FLASH_FCRIS_ARIS | FLASH_FCRIS_VOLTRIS |
  230. FLASH_FCRIS_INVDRIS | FLASH_FCRIS_PROGRIS))
  231. {
  232. return(-1);
  233. }
  234. //
  235. // Success.
  236. //
  237. return(0);
  238. }
  239. //*****************************************************************************
  240. //
  241. //! Gets the protection setting for a block of flash.
  242. //!
  243. //! \param ui32Address is the start address of the flash block to be queried.
  244. //!
  245. //! This function gets the current protection for the specified block of flash.
  246. //! Refer to the device data sheet to determine the granularity for each
  247. //! protection option. A block can be read/write, read-only, or execute-only.
  248. //! Read/write blocks can be read, executed, erased, and programmed. Read-only
  249. //! blocks can be read and executed. Execute-only blocks can only be executed;
  250. //! processor and debugger data reads are not allowed.
  251. //!
  252. //! \return Returns the protection setting for this block. See
  253. //! FlashProtectSet() for possible values.
  254. //
  255. //*****************************************************************************
  256. tFlashProtection
  257. FlashProtectGet(uint32_t ui32Address)
  258. {
  259. uint32_t ui32FMPRE, ui32FMPPE;
  260. uint32_t ui32Bank;
  261. //
  262. // Check the argument.
  263. //
  264. ASSERT(!(ui32Address & (FLASH_PROTECT_SIZE - 1)));
  265. //
  266. // Calculate the Flash Bank from Base Address, and mask off the Bank
  267. // from ui32Address for subsequent reference.
  268. //
  269. ui32Bank = (((ui32Address / FLASH_PROTECT_SIZE) / 32) % 4);
  270. ui32Address &= ((FLASH_PROTECT_SIZE * 32) - 1);
  271. //
  272. // Read the appropriate flash protection registers for the specified
  273. // flash bank.
  274. //
  275. ui32FMPRE = HWREG(g_pui32FMPRERegs[ui32Bank]);
  276. ui32FMPPE = HWREG(g_pui32FMPPERegs[ui32Bank]);
  277. //
  278. // Check the appropriate protection bits for the block of memory that
  279. // is specified by the address.
  280. //
  281. switch((((ui32FMPRE >> (ui32Address / FLASH_PROTECT_SIZE)) & 0x1) << 1) |
  282. ((ui32FMPPE >> (ui32Address / FLASH_PROTECT_SIZE)) & 0x1))
  283. {
  284. //
  285. // This block is marked as execute only (that is, it can not be erased
  286. // or programmed, and the only reads allowed are via the instruction
  287. // fetch interface).
  288. //
  289. case 0:
  290. case 1:
  291. {
  292. return(FlashExecuteOnly);
  293. }
  294. //
  295. // This block is marked as read only (that is, it can not be erased or
  296. // programmed).
  297. //
  298. case 2:
  299. {
  300. return(FlashReadOnly);
  301. }
  302. //
  303. // This block is read/write; it can be read, erased, and programmed.
  304. //
  305. case 3:
  306. default:
  307. {
  308. return(FlashReadWrite);
  309. }
  310. }
  311. }
  312. //*****************************************************************************
  313. //
  314. //! Sets the protection setting for a block of flash.
  315. //!
  316. //! \param ui32Address is the start address of the flash block to be protected.
  317. //! \param eProtect is the protection to be applied to the block. Can be one
  318. //! of \b FlashReadWrite, \b FlashReadOnly, or \b FlashExecuteOnly.
  319. //!
  320. //! This function sets the protection for the specified block of flash. Refer
  321. //! to the device data sheet to determine the granularity for each protection
  322. //! option. Blocks that are read/write can be made read-only or execute-only.
  323. //! Blocks that are read-only can be made execute-only. Blocks that are
  324. //! execute-only cannot have their protection modified. Attempts to make the
  325. //! block protection less stringent (that is, read-only to read/write)
  326. //! result in a failure (and are prevented by the hardware).
  327. //!
  328. //! Changes to the flash protection are maintained only until the next reset.
  329. //! This protocol allows the application to be executed in the desired flash
  330. //! protection environment to check for inappropriate flash access (via the
  331. //! flash interrupt). To make the flash protection permanent, use the
  332. //! FlashProtectSave() function.
  333. //!
  334. //! \return Returns 0 on success, or -1 if an invalid address or an invalid
  335. //! protection was specified.
  336. //
  337. //*****************************************************************************
  338. int32_t
  339. FlashProtectSet(uint32_t ui32Address, tFlashProtection eProtect)
  340. {
  341. uint32_t ui32ProtectRE, ui32ProtectPE;
  342. uint32_t ui32Bank;
  343. //
  344. // Check the argument.
  345. //
  346. ASSERT(!(ui32Address & (FLASH_PROTECT_SIZE - 1)));
  347. ASSERT((eProtect == FlashReadWrite) || (eProtect == FlashReadOnly) ||
  348. (eProtect == FlashExecuteOnly));
  349. //
  350. // Convert the address into a block number.
  351. //
  352. ui32Address /= FLASH_PROTECT_SIZE;
  353. //
  354. // ui32Address contains a "raw" block number. Derive the Flash Bank from
  355. // the "raw" block number, and convert ui32Address to a "relative"
  356. // block number.
  357. //
  358. ui32Bank = ((ui32Address / 32) % 4);
  359. ui32Address %= 32;
  360. //
  361. // Get the current protection for the specified flash bank.
  362. //
  363. ui32ProtectRE = HWREG(g_pui32FMPRERegs[ui32Bank]);
  364. ui32ProtectPE = HWREG(g_pui32FMPPERegs[ui32Bank]);
  365. //
  366. // Set the protection based on the requested protection.
  367. //
  368. switch(eProtect)
  369. {
  370. //
  371. // Make this block execute only.
  372. //
  373. case FlashExecuteOnly:
  374. {
  375. //
  376. // Turn off the read and program bits for this block.
  377. //
  378. ui32ProtectRE &= ~(0x1 << ui32Address);
  379. ui32ProtectPE &= ~(0x1 << ui32Address);
  380. //
  381. // We're done handling this protection.
  382. //
  383. break;
  384. }
  385. //
  386. // Make this block read only.
  387. //
  388. case FlashReadOnly:
  389. {
  390. //
  391. // The block can not be made read only if it is execute only.
  392. //
  393. if(((ui32ProtectRE >> ui32Address) & 0x1) != 0x1)
  394. {
  395. return(-1);
  396. }
  397. //
  398. // Make this block read only.
  399. //
  400. ui32ProtectPE &= ~(0x1 << ui32Address);
  401. //
  402. // We're done handling this protection.
  403. //
  404. break;
  405. }
  406. //
  407. // Make this block read/write.
  408. //
  409. case FlashReadWrite:
  410. default:
  411. {
  412. //
  413. // The block can not be made read/write if it is not already
  414. // read/write.
  415. //
  416. if((((ui32ProtectRE >> ui32Address) & 0x1) != 0x1) ||
  417. (((ui32ProtectPE >> ui32Address) & 0x1) != 0x1))
  418. {
  419. return(-1);
  420. }
  421. //
  422. // The block is already read/write, so there is nothing to do.
  423. //
  424. return(0);
  425. }
  426. }
  427. //
  428. // Set the new protection for the specified flash bank.
  429. //
  430. HWREG(g_pui32FMPRERegs[ui32Bank]) = ui32ProtectRE;
  431. HWREG(g_pui32FMPPERegs[ui32Bank]) = ui32ProtectPE;
  432. //
  433. // Success.
  434. //
  435. return(0);
  436. }
  437. //*****************************************************************************
  438. //
  439. //! Saves the flash protection settings.
  440. //!
  441. //! This function makes the currently programmed flash protection settings
  442. //! permanent. This operation is non-reversible; a chip reset or power cycle
  443. //! does not change the flash protection.
  444. //!
  445. //! This function does not return until the protection has been saved.
  446. //!
  447. //! \return Returns 0 on success, or -1 if a hardware error is encountered.
  448. //
  449. //*****************************************************************************
  450. int32_t
  451. FlashProtectSave(void)
  452. {
  453. uint32_t ui32Temp;
  454. //
  455. // Save the entire bank of 8 flash protection registers.
  456. //
  457. for(ui32Temp = 0; ui32Temp < 8; ui32Temp++)
  458. {
  459. //
  460. // Tell the flash controller to write the flash protection register.
  461. //
  462. HWREG(FLASH_FMA) = ui32Temp;
  463. HWREG(FLASH_FMC) = FLASH_FMC_WRKEY | FLASH_FMC_COMT;
  464. //
  465. // Wait until the write has completed.
  466. //
  467. while(HWREG(FLASH_FMC) & FLASH_FMC_COMT)
  468. {
  469. }
  470. }
  471. //
  472. // Success.
  473. //
  474. return(0);
  475. }
  476. //*****************************************************************************
  477. //
  478. //! Gets the user registers.
  479. //!
  480. //! \param pui32User0 is a pointer to the location to store USER Register 0.
  481. //! \param pui32User1 is a pointer to the location to store USER Register 1.
  482. //!
  483. //! This function reads the contents of user registers 0 and 1, and
  484. //! stores them in the specified locations.
  485. //!
  486. //! \return Returns 0 on success, or -1 if a hardware error is encountered.
  487. //
  488. //*****************************************************************************
  489. int32_t
  490. FlashUserGet(uint32_t *pui32User0, uint32_t *pui32User1)
  491. {
  492. //
  493. // Verify that the pointers are valid.
  494. //
  495. ASSERT(pui32User0 != 0);
  496. ASSERT(pui32User1 != 0);
  497. //
  498. // Get and store the current value of the user registers.
  499. //
  500. *pui32User0 = HWREG(FLASH_USERREG0);
  501. *pui32User1 = HWREG(FLASH_USERREG1);
  502. //
  503. // Success.
  504. //
  505. return(0);
  506. }
  507. //*****************************************************************************
  508. //
  509. //! Sets the user registers.
  510. //!
  511. //! \param ui32User0 is the value to store in USER Register 0.
  512. //! \param ui32User1 is the value to store in USER Register 1.
  513. //!
  514. //! This function sets the contents of the user registers 0 and 1 to
  515. //! the specified values.
  516. //!
  517. //! \return Returns 0 on success, or -1 if a hardware error is encountered.
  518. //
  519. //*****************************************************************************
  520. int32_t
  521. FlashUserSet(uint32_t ui32User0, uint32_t ui32User1)
  522. {
  523. //
  524. // Save the new values into the user registers.
  525. //
  526. HWREG(FLASH_USERREG0) = ui32User0;
  527. HWREG(FLASH_USERREG1) = ui32User1;
  528. //
  529. // Success.
  530. //
  531. return(0);
  532. }
  533. //*****************************************************************************
  534. //
  535. //! Gets all the user registers.
  536. //!
  537. //! \param pui32User0 is a pointer to the location to store USER Register 0.
  538. //! \param pui32User1 is a pointer to the location to store USER Register 1.
  539. //! \param pui32User2 is a pointer to the location to store USER Register 2.
  540. //! \param pui32User3 is a pointer to the location to store USER Register 3.
  541. //!
  542. //! This function reads the contents of user registers 0, 1, 2 and 3, and
  543. //! stores them in the specified locations.
  544. //!
  545. //! \return Returns 0 on success, or -1 if a hardware error is encountered.
  546. //
  547. //*****************************************************************************
  548. int32_t
  549. FlashAllUserRegisterGet(uint32_t *pui32User0, uint32_t *pui32User1,
  550. uint32_t *pui32User2, uint32_t *pui32User3)
  551. {
  552. //
  553. // Verify that the pointers are valid.
  554. //
  555. ASSERT(pui32User0 != 0);
  556. ASSERT(pui32User1 != 0);
  557. ASSERT(pui32User2 != 0);
  558. ASSERT(pui32User3 != 0);
  559. //
  560. // Get and store the current value of the user registers.
  561. //
  562. *pui32User0 = HWREG(FLASH_USERREG0);
  563. *pui32User1 = HWREG(FLASH_USERREG1);
  564. *pui32User2 = HWREG(FLASH_USERREG2);
  565. *pui32User3 = HWREG(FLASH_USERREG3);
  566. //
  567. // Success.
  568. //
  569. return(0);
  570. }
  571. //*****************************************************************************
  572. //
  573. //! Sets the user registers 0 to 3
  574. //!
  575. //! \param ui32User0 is the value to store in USER Register 0.
  576. //! \param ui32User1 is the value to store in USER Register 1.
  577. //! \param ui32User2 is the value to store in USER Register 2.
  578. //! \param ui32User3 is the value to store in USER Register 3.
  579. //!
  580. //! This function sets the contents of the user registers 0, 1, 2 and 3 to
  581. //! the specified values.
  582. //!
  583. //! \return Returns 0 on success, or -1 if a hardware error is encountered.
  584. //
  585. //*****************************************************************************
  586. int32_t
  587. FlashAllUserRegisterSet(uint32_t ui32User0, uint32_t ui32User1,
  588. uint32_t ui32User2, uint32_t ui32User3)
  589. {
  590. //
  591. // Save the new values into the user registers.
  592. //
  593. HWREG(FLASH_USERREG0) = ui32User0;
  594. HWREG(FLASH_USERREG1) = ui32User1;
  595. HWREG(FLASH_USERREG2) = ui32User2;
  596. HWREG(FLASH_USERREG3) = ui32User3;
  597. //
  598. // Success.
  599. //
  600. return(0);
  601. }
  602. //*****************************************************************************
  603. //
  604. //! Saves the user registers 0 and 1.
  605. //!
  606. //! This function makes the currently programmed user register 0 and 1 settings
  607. //! permanent. This operation is non-reversible; a chip reset or power cycle
  608. //! does not change the flash protection.
  609. //!
  610. //! This function does not return until the protection has been saved.
  611. //!
  612. //! \return Returns 0 on success, or -1 if a hardware error is encountered.
  613. //
  614. //*****************************************************************************
  615. int32_t
  616. FlashUserSave(void)
  617. {
  618. //
  619. // Setting the MSB of FMA will trigger a permanent save of a USER
  620. // register. Bit 0 will indicate User 0 (0) or User 1 (1).
  621. //
  622. HWREG(FLASH_FMA) = 0x80000000;
  623. HWREG(FLASH_FMC) = FLASH_FMC_WRKEY | FLASH_FMC_COMT;
  624. //
  625. // Wait until the write has completed.
  626. //
  627. while(HWREG(FLASH_FMC) & FLASH_FMC_COMT)
  628. {
  629. }
  630. //
  631. // Tell the flash controller to write the USER1 Register.
  632. //
  633. HWREG(FLASH_FMA) = 0x80000001;
  634. HWREG(FLASH_FMC) = FLASH_FMC_WRKEY | FLASH_FMC_COMT;
  635. //
  636. // Wait until the write has completed.
  637. //
  638. while(HWREG(FLASH_FMC) & FLASH_FMC_COMT)
  639. {
  640. }
  641. //
  642. // Success.
  643. //
  644. return(0);
  645. }
  646. //*****************************************************************************
  647. //
  648. //! Saves the user registers.
  649. //!
  650. //! This function makes the currently programmed user register 0, 1, 2 and 3
  651. //! settings permanent. This operation is non-reversible; a chip reset or
  652. //! power cycle does not change the flash protection.
  653. //!
  654. //! This function does not return until the protection has been saved.
  655. //!
  656. //! \note To ensure data integrity of the user registers, the commits should
  657. //! not be interrupted with a power loss.
  658. //!
  659. //! \return Returns 0 on success, or -1 if a hardware error is encountered.
  660. //
  661. //*****************************************************************************
  662. int32_t
  663. FlashAllUserRegisterSave(void)
  664. {
  665. uint32_t ui32Index;
  666. //
  667. // Setting the MSB of FMA will trigger a permanent save of a USER Register.
  668. // The 2 least signigicant bits, specify the exact User Register to save.
  669. // The value of the least significant bits for
  670. // USER Register 0 is 00,
  671. // USER Register 1 is 01,
  672. // USER Register 2 is 10 and
  673. // USER Register 3 is 11.
  674. //
  675. for(ui32Index = 0; ui32Index < 4; ui32Index++)
  676. {
  677. //
  678. // Tell the flash controller to commit a USER Register.
  679. //
  680. HWREG(FLASH_FMA) = (0x80000000 + ui32Index);
  681. HWREG(FLASH_FMC) = FLASH_FMC_WRKEY | FLASH_FMC_COMT;
  682. //
  683. // Wait until the write has completed.
  684. //
  685. while(HWREG(FLASH_FMC) & FLASH_FMC_COMT)
  686. {
  687. }
  688. }
  689. //
  690. // Success.
  691. //
  692. return(0);
  693. }
  694. //*****************************************************************************
  695. //
  696. //! Registers an interrupt handler for the flash interrupt.
  697. //!
  698. //! \param pfnHandler is a pointer to the function to be called when the flash
  699. //! interrupt occurs.
  700. //!
  701. //! This function sets the handler to be called when the flash interrupt
  702. //! occurs. The flash controller can generate an interrupt when an invalid
  703. //! flash access occurs, such as trying to program or erase a read-only block,
  704. //! or trying to read from an execute-only block. It can also generate an
  705. //! interrupt when a program or erase operation has completed. The interrupt
  706. //! is automatically enabled when the handler is registered.
  707. //!
  708. //! \sa IntRegister() for important information about registering interrupt
  709. //! handlers.
  710. //!
  711. //! \return None.
  712. //
  713. //*****************************************************************************
  714. void
  715. FlashIntRegister(void (*pfnHandler)(void))
  716. {
  717. //
  718. // Register the interrupt handler, returning an error if an error occurs.
  719. //
  720. IntRegister(INT_FLASH_TM4C123, pfnHandler);
  721. //
  722. // Enable the flash interrupt.
  723. //
  724. IntEnable(INT_FLASH_TM4C123);
  725. }
  726. //*****************************************************************************
  727. //
  728. //! Unregisters the interrupt handler for the flash interrupt.
  729. //!
  730. //! This function clears the handler to be called when the flash interrupt
  731. //! occurs. This function also masks off the interrupt in the interrupt
  732. //! controller so that the interrupt handler is no longer called.
  733. //!
  734. //! \sa IntRegister() for important information about registering interrupt
  735. //! handlers.
  736. //!
  737. //! \return None.
  738. //
  739. //*****************************************************************************
  740. void
  741. FlashIntUnregister(void)
  742. {
  743. //
  744. // Disable the interrupt.
  745. //
  746. IntDisable(INT_FLASH_TM4C123);
  747. //
  748. // Unregister the interrupt handler.
  749. //
  750. IntUnregister(INT_FLASH_TM4C123);
  751. }
  752. //*****************************************************************************
  753. //
  754. //! Enables individual flash controller interrupt sources.
  755. //!
  756. //! \param ui32IntFlags is a bit mask of the interrupt sources to be enabled.
  757. //! The ui32IntFlags parameter can be the logical OR of any of the following
  758. //! values:
  759. //!
  760. //! - \b FLASH_INT_ACCESS occurs when a program or erase action was attempted
  761. //! on a block of flash that is marked as read-only or execute-only.
  762. //! - \b FLASH_INT_PROGRAM occurs when a programming or erase cycle completes.
  763. //! - \b FLASH_INT_EEPROM occurs when an EEPROM interrupt occurs. The source of
  764. //! the EEPROM interrupt can be determined by reading the EEDONE register.
  765. //! - \b FLASH_INT_VOLTAGE_ERR occurs when the voltage was out of spec during
  766. //! the flash operation and the operation was terminated.
  767. //! - \b FLASH_INT_DATA_ERR occurs when an operation attempts to program a bit that
  768. //! contains a 0 to a 1.
  769. //! - \b FLASH_INT_ERASE_ERR occurs when an erase operation fails.
  770. //! - \b FLASH_INT_PROGRAM_ERR occurs when a program operation fails.
  771. //!
  772. //! This function enables the indicated flash controller interrupt sources.
  773. //! Only the sources that are enabled can be reflected to the processor
  774. //! interrupt; disabled sources have no effect on the processor.
  775. //!
  776. //! \return None.
  777. //
  778. //*****************************************************************************
  779. void
  780. FlashIntEnable(uint32_t ui32IntFlags)
  781. {
  782. //
  783. // Enable the specified interrupts.
  784. //
  785. HWREG(FLASH_FCIM) |= ui32IntFlags;
  786. }
  787. //*****************************************************************************
  788. //
  789. //! Disables individual flash controller interrupt sources.
  790. //!
  791. //! \param ui32IntFlags is a bit mask of the interrupt sources to be disabled.
  792. //! The ui32IntFlags parameter can be the logical OR of any of the following
  793. //! values:
  794. //!
  795. //! - \b FLASH_INT_ACCESS occurs when a program or erase action was attempted
  796. //! on a block of flash that is marked as read-only or execute-only.
  797. //! - \b FLASH_INT_PROGRAM occurs when a programming or erase cycle completes.
  798. //! - \b FLASH_INT_EEPROM occurs when an EEPROM interrupt occurs. The source of
  799. //! the EEPROM interrupt can be determined by reading the EEDONE register.
  800. //! - \b FLASH_INT_VOLTAGE_ERR occurs when the voltage was out of spec during
  801. //! the flash operation and the operation was terminated.
  802. //! - \b FLASH_INT_DATA_ERR occurs when an operation attempts to program a bit that
  803. //! contains a 0 to a 1.
  804. //! - \b FLASH_INT_ERASE_ERR occurs when an erase operation fails.
  805. //! - \b FLASH_INT_PROGRAM_ERR occurs when a program operation fails.
  806. //!
  807. //! This function disables the indicated flash controller interrupt sources.
  808. //! Only the sources that are enabled can be reflected to the processor
  809. //! interrupt; disabled sources have no effect on the processor.
  810. //!
  811. //! \return None.
  812. //
  813. //*****************************************************************************
  814. void
  815. FlashIntDisable(uint32_t ui32IntFlags)
  816. {
  817. //
  818. // Disable the specified interrupts.
  819. //
  820. HWREG(FLASH_FCIM) &= ~(ui32IntFlags);
  821. }
  822. //*****************************************************************************
  823. //
  824. //! Gets the current interrupt status.
  825. //!
  826. //! \param bMasked is false if the raw interrupt status is required and true if
  827. //! the masked interrupt status is required.
  828. //!
  829. //! This function returns the interrupt status for the flash controller.
  830. //! Either the raw interrupt status or the status of interrupts that are
  831. //! allowed to reflect to the processor can be returned.
  832. //!
  833. //! \return The current interrupt status, enumerated as a bit field of
  834. //! \b FLASH_INT_ACCESS, \b FLASH_INT_PROGRAM, \b FLASH_INT_EEPROM,
  835. //! FLASH_INT_VOLTAGE_ERR, FLASH_INT_DATA_ERR, FLASH_INT_ERASE_ERR, and
  836. //! FLASH_INT_PROGRAM_ERR.
  837. //
  838. //*****************************************************************************
  839. uint32_t
  840. FlashIntStatus(bool bMasked)
  841. {
  842. //
  843. // Return either the interrupt status or the raw interrupt status as
  844. // requested.
  845. //
  846. if(bMasked)
  847. {
  848. return(HWREG(FLASH_FCMISC));
  849. }
  850. else
  851. {
  852. return(HWREG(FLASH_FCRIS));
  853. }
  854. }
  855. //*****************************************************************************
  856. //
  857. //! Clears flash controller interrupt sources.
  858. //!
  859. //! \param ui32IntFlags is the bit mask of the interrupt sources to be cleared.
  860. //!
  861. //! The specified flash controller interrupt sources are cleared, so that they
  862. //! no longer assert. The
  863. //! ui32IntFlags parameter can be the logical OR of any of the following
  864. //! values:
  865. //!
  866. //! - \b FLASH_INT_ACCESS occurs when a program or erase action was attempted
  867. //! on a block of flash that is marked as read-only or execute-only.
  868. //! - \b FLASH_INT_PROGRAM occurs when a programming or erase cycle completes.
  869. //! - \b FLASH_INT_EEPROM occurs when an EEPROM interrupt occurs. The source of
  870. //! the EEPROM interrupt can be determined by reading the EEDONE register.
  871. //! - \b FLASH_INT_VOLTAGE_ERR occurs when the voltage was out of spec during
  872. //! the flash operation and the operation was terminated.
  873. //! - \b FLASH_INT_DATA_ERR occurs when an operation attempts to program a bit that
  874. //! contains a 0 to a 1.
  875. //! - \b FLASH_INT_ERASE_ERR occurs when an erase operation fails.
  876. //! - \b FLASH_INT_PROGRAM_ERR occurs when a program operation fails.
  877. //!
  878. //! This function must be called in the interrupt handler to keep the
  879. //! interrupt from being triggered again immediately upon exit.
  880. //!
  881. //! \note Because there is a write buffer in the Cortex-M processor, it may
  882. //! take several clock cycles before the interrupt source is actually cleared.
  883. //! Therefore, it is recommended that the interrupt source be cleared early in
  884. //! the interrupt handler (as opposed to the very last action) to avoid
  885. //! returning from the interrupt handler before the interrupt source is
  886. //! actually cleared. Failure to do so may result in the interrupt handler
  887. //! being immediately reentered (because the interrupt controller still sees
  888. //! the interrupt source asserted).
  889. //!
  890. //! \return None.
  891. //
  892. //*****************************************************************************
  893. void
  894. FlashIntClear(uint32_t ui32IntFlags)
  895. {
  896. //
  897. // Clear the flash interrupt.
  898. //
  899. HWREG(FLASH_FCMISC) = ui32IntFlags;
  900. }
  901. //*****************************************************************************
  902. //
  903. // Close the Doxygen group.
  904. //! @}
  905. //
  906. //*****************************************************************************