Example_Flash2803x_API.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. //###########################################################################
  2. // Description:
  3. //! \addtogroup f2803x_example_list
  4. //! <h1> Flash Programming (flash_programming)</h1>
  5. //!
  6. //! This example shows how to program flash using flash API.
  7. //! It shows.
  8. //! - The required software setup before calling the API (setting the
  9. //! PLL, checking for limp mode etc.),
  10. //! - How to copy the API from flash into SARAM for execution.
  11. //! - How to call the API functions.
  12. //!
  13. //! \note
  14. //! This example runs from Flash. First program the example
  15. //! into flash. The code will then copy the API's to RAM and
  16. //! modify the flash.
  17. //!
  18. //
  19. //###########################################################################
  20. // $TI Release: F2803x C/C++ Header Files and Peripheral Examples V130 $
  21. // $Release Date: May 8, 2015 $
  22. // $Copyright: Copyright (C) 2009-2015 Texas Instruments Incorporated -
  23. // http://www.ti.com/ ALL RIGHTS RESERVED $
  24. //###########################################################################
  25. /*---- Flash API include file -------------------------------------------------*/
  26. #include "Flash2803x_API_Library.h"
  27. /*---- example include file ---------------------------------------------------*/
  28. #include "Example_Flash2803x_API.h"
  29. /*---- device support includes ------------------------------------------------*/
  30. #include "DSP28x_Project.h"
  31. /*---- Standard headers -------------------------------------------------------*/
  32. #include <stdio.h>
  33. /*--- Callback function. Function specified by defining Flash_CallbackPtr */
  34. void MyCallbackFunction(void);
  35. Uint32 MyCallbackCounter; // Just increment a counter in the callback function
  36. /*--- Global variables used to interface to the flash routines */
  37. FLASH_ST FlashStatus;
  38. /*---------------------------------------------------------------------------
  39. Data/Program Buffer used for testing the flash API functions
  40. ---------------------------------------------------------------------------*/
  41. #define WORDS_IN_FLASH_BUFFER 0x100 // Programming data buffer, Words
  42. Uint16 Buffer[WORDS_IN_FLASH_BUFFER];
  43. /*---------------------------------------------------------------------------
  44. Sector address info
  45. ---------------------------------------------------------------------------*/
  46. typedef struct {
  47. Uint16 *StartAddr;
  48. Uint16 *EndAddr;
  49. } SECTOR;
  50. #define OTP_START_ADDR 0x3D7800
  51. #define OTP_END_ADDR 0x3D7BFF
  52. #if FLASH_F28035
  53. #define FLASH_START_ADDR 0x3E8000
  54. #define FLASH_END_ADDR 0x3F7FFF
  55. SECTOR Sector[4] = {
  56. (Uint16 *)0x3F4000,(Uint16 *)0x3F7FFF,
  57. (Uint16 *)0x3F0000,(Uint16 *)0x3F3FFF,
  58. (Uint16 *)0x3EC000,(Uint16 *)0x3EFFFF,
  59. (Uint16 *)0x3E8000,(Uint16 *)0x3EBFFF
  60. };
  61. #endif
  62. #if FLASH_F2806
  63. #define FLASH_START_ADDR 0x3F0000
  64. #define FLASH_END_ADDR 0x3F7FFF
  65. SECTOR Sector[4] = {
  66. (Uint16 *)0x3F6000,(Uint16 *)0x3F7FFF,
  67. (Uint16 *)0x3F4000,(Uint16 *)0x3F5FFF,
  68. (Uint16 *)0x3F2000,(Uint16 *)0x3F3FFF,
  69. (Uint16 *)0x3F0000,(Uint16 *)0x3F1FFF
  70. };
  71. #endif
  72. #if FLASH_F2802
  73. #define FLASH_START_ADDR 0x3F0000
  74. #define FLASH_END_ADDR 0x3F7FFF
  75. SECTOR Sector[4] = {
  76. (Uint16 *)0x3F6000,(Uint16 *)0x3F7FFF,
  77. (Uint16 *)0x3F4000,(Uint16 *)0x3F5FFF,
  78. (Uint16 *)0x3F2000,(Uint16 *)0x3F3FFF,
  79. (Uint16 *)0x3F0000,(Uint16 *)0x3F1FFF
  80. };
  81. #endif
  82. #if FLASH_F2801
  83. #define FLASH_START_ADDR 0x3F4000
  84. #define FLASH_END_ADDR 0x3F7FFF
  85. SECTOR Sector[4] = {
  86. (Uint16 *)0x3F7000,(Uint16 *)0x3F7FFF,
  87. (Uint16 *)0x3F6000,(Uint16 *)0x3F5FFF,
  88. (Uint16 *)0x3F5000,(Uint16 *)0x3F5FFF,
  89. (Uint16 *)0x3F4000,(Uint16 *)0x3F4FFF
  90. };
  91. #endif
  92. extern Uint32 Flash_CPUScaleFactor;
  93. /****************************************************************/
  94. #define RecordLenght 0
  95. #define LoadOffset_H 1
  96. #define LoadOffset_L 2
  97. #define RecordType 3
  98. #define AddrBase 5
  99. #pragma CODE_SECTION(FlashDataCopy,"ramfuncs");
  100. int16 FlashDataCopy(unsigned char *buffer,int16 len)
  101. {
  102. unsigned char databuffer[100];
  103. Uint16 tbuffer[100];
  104. int16 datalen;
  105. int16 i;
  106. Uint16 *Flash_ptr;
  107. Uint16 Length;
  108. Uint16 Status = 0;
  109. static Uint16 *addrbase;
  110. datalen = len;
  111. if(datalen > 100)
  112. {
  113. return STATUS_ERROR;
  114. }
  115. for(i=0;i<100;i++)
  116. {
  117. databuffer[i] = buffer[i];
  118. }
  119. if(databuffer[RecordType] == 0x04)
  120. {
  121. if(databuffer[AddrBase] == 0x3F)
  122. {
  123. addrbase = Sector[1].StartAddr;
  124. }else if(databuffer[AddrBase] == 0x3E)
  125. {
  126. addrbase = Sector[2].StartAddr - 0xC000;
  127. }
  128. return STATUS_SUCCESS;
  129. }else if(databuffer[RecordType] == 0x00)
  130. {
  131. for(i=0;i<100;i++)
  132. {
  133. tbuffer[i] = 0;
  134. }
  135. Flash_ptr = (Uint16 *)(addrbase + (databuffer[LoadOffset_H] << 8) + databuffer[LoadOffset_L]);
  136. Length = databuffer[RecordLenght] / 2;
  137. /*-----------------------------------------------------------------------*/
  138. if((addrbase == (Uint16 *)0x3F0000) && (databuffer[LoadOffset_H] >= 0x40))
  139. {
  140. Flash_ptr = (Uint16 *)(addrbase + (databuffer[LoadOffset_H] << 8) + databuffer[LoadOffset_L] - 0x4000);
  141. }
  142. /*-----------------------------------------------------------------------*/
  143. for(i=0;i<Length;i++)
  144. {
  145. tbuffer[i] = ((databuffer[i*2 + 4] << 8) & 0xFF00) + (databuffer[i*2 + 4 + 1] & 0x00FF);
  146. }
  147. Status = Flash_Program(Flash_ptr,tbuffer,Length,&FlashStatus);
  148. if(Status != STATUS_SUCCESS)
  149. {
  150. return STATUS_ERROR;
  151. }
  152. Status = Flash_Verify(Flash_ptr,tbuffer,Length,&FlashStatus);
  153. if(Status != STATUS_SUCCESS)
  154. {
  155. return STATUS_ERROR;
  156. }
  157. return STATUS_SUCCESS;
  158. }else if(databuffer[RecordType] == 0x01)
  159. {
  160. return STATUS_SUCCESS;
  161. }else
  162. {
  163. return STATUS_ERROR;
  164. }
  165. return STATUS_SUCCESS;
  166. }
  167. #pragma CODE_SECTION(FlashErase,"ramfuncs");
  168. int16 FlashErase()
  169. {
  170. Uint16 Status = 0;
  171. float32 Version; // Version of the API in floating point
  172. Uint16 VersionHex; // Version of the API in decimal encoded hex
  173. Status = Example_CsmUnlock();
  174. if(Status != STATUS_SUCCESS)
  175. {
  176. return Status;
  177. }
  178. EALLOW;
  179. Flash_CPUScaleFactor = SCALE_FACTOR;
  180. EDIS;
  181. VersionHex = Flash_APIVersionHex();
  182. if(VersionHex != 0x0100)
  183. {
  184. return STATUS_ERROR;
  185. }
  186. Version = Flash_APIVersion();
  187. if(Version != (float32)1.00)
  188. {
  189. return STATUS_ERROR;
  190. }
  191. // Status = Flash_Erase((SECTORB|SECTORC|SECTORD),&FlashStatus);
  192. Status = Flash_Erase((SECTORC|SECTORD|SECTORE|SECTORF|SECTORG|SECTORH),&FlashStatus);
  193. if(Status != STATUS_SUCCESS)
  194. {
  195. return Status;
  196. }
  197. return STATUS_SUCCESS;
  198. }
  199. #if USEDEFAULT
  200. /*--- Callback function. Function specified by defining Flash_CallbackPtr */
  201. void MyCallbackFunction(void);
  202. Uint32 MyCallbackCounter; // Just increment a counter in the callback function
  203. /*--- Global variables used to interface to the flash routines */
  204. FLASH_ST FlashStatus;
  205. /*---------------------------------------------------------------------------
  206. Data/Program Buffer used for testing the flash API functions
  207. ---------------------------------------------------------------------------*/
  208. #define WORDS_IN_FLASH_BUFFER 0x100 // Programming data buffer, Words
  209. Uint16 Buffer[WORDS_IN_FLASH_BUFFER];
  210. /*---------------------------------------------------------------------------
  211. Sector address info
  212. ---------------------------------------------------------------------------*/
  213. typedef struct {
  214. Uint16 *StartAddr;
  215. Uint16 *EndAddr;
  216. } SECTOR;
  217. #define OTP_START_ADDR 0x3D7800
  218. #define OTP_END_ADDR 0x3D7BFF
  219. #define FLASH_END_ADDR 0x3F7FFF
  220. #if (FLASH_F28035 || FLASH_F28034)
  221. #define FLASH_START_ADDR 0x3E8000
  222. SECTOR Sector[8]= {
  223. (Uint16 *) 0x3E8000,(Uint16 *) 0x3E9FFF,
  224. (Uint16 *) 0x3EA000,(Uint16 *) 0x3EBFFF,
  225. (Uint16 *) 0x3EC000,(Uint16 *) 0x3EDFFF,
  226. (Uint16 *) 0x3EE000,(Uint16 *) 0x3EFFFF,
  227. (Uint16 *) 0x3F0000,(Uint16 *) 0x3F1FFF,
  228. (Uint16 *) 0x3F2000,(Uint16 *) 0x3F3FFF,
  229. (Uint16 *) 0x3F4000,(Uint16 *) 0x3F5FFF,
  230. (Uint16 *) 0x3F6000,(Uint16 *) 0x3F7FFF,
  231. };
  232. #elif (FLASH_F28033 || FLASH_F28032)
  233. #define FLASH_START_ADDR 0x3F0000
  234. SECTOR Sector[8] = {
  235. (Uint16 *) 0x3F0000,(Uint16 *) 0x3F0FFF,
  236. (Uint16 *) 0x3F1000,(Uint16 *) 0x3F1FFF,
  237. (Uint16 *) 0x3F2000,(Uint16 *) 0x3F2FFF,
  238. (Uint16 *) 0x3F3000,(Uint16 *) 0x3F3FFF,
  239. (Uint16 *) 0x3F4000,(Uint16 *) 0x3F4FFF,
  240. (Uint16 *) 0x3F5000,(Uint16 *) 0x3F5FFF,
  241. (Uint16 *) 0x3F6000,(Uint16 *) 0x3F6FFF,
  242. (Uint16 *) 0x3F7000,(Uint16 *) 0x3F7FFF,
  243. };
  244. #endif
  245. void main( void )
  246. {
  247. /*------------------------------------------------------------------
  248. To use the Flash API, the following steps
  249. must be followed:
  250. 1. Modify Flash2803x_API_Config.h for your targets operating
  251. conditions.
  252. 2. Include Flash2803x_API_Library.h in the application.
  253. 3. Add the appropriate Flash API library to the project.
  254. The user's code is responsible for the following:
  255. 4. Initialize the PLL to the proper CPU operating frequency.
  256. 5. If required, copy the flash API functions into on-chip zero waitstate
  257. RAM.
  258. 6. Initialize the Flash_CPUScaleFactor variable to SCALE_FACTOR
  259. 7. Initialize the callback function pointer or set it to NULL
  260. 8. Optional: Run the Toggle test to confirm proper frequency configuration
  261. of the API.
  262. 9. Optional: Unlock the CSM.
  263. 10. Make sure the PLL is not running in limp mode
  264. 11. Call the API functions: Flash_Erase(), Flash_Program(), Flash_Verify()
  265. The API functions will:
  266. Disable the watchdog
  267. Check the device PARTID.
  268. Disable interrupts during time critical code.
  269. Perform the desired operation and return status
  270. ------------------------------------------------------------------*/
  271. Uint16 Status;
  272. /*------------------------------------------------------------------
  273. Initialize the PLLCR value before calling any of the F2803x Flash API
  274. functions.
  275. Check to see if the PLL needs to changed
  276. PLLCR_VALUE is defined in Example_Flash2803x_API.h
  277. 1) Make sure PLL is not in limp mode
  278. 2) Disable missing clock detect logic
  279. 3) Make the change
  280. 4) Wait for the DSP to switch to the PLL clock
  281. This wait is performed to ensure that the flash API functions
  282. will be executed at the correct frequency.
  283. 5) While waiting, feed the watchdog so it will not reset.
  284. 6) Re-enable the missing clock detect logic
  285. ------------------------------------------------------------------*/
  286. // Step 1. Initialize System Control:
  287. // PLL, WatchDog, enable Peripheral Clocks
  288. // This example function is found in the DSP2803x_SysCtrl.c file.
  289. InitSysCtrl();
  290. /*------------------------------------------------------------------
  291. Unlock the CSM.
  292. If the API functions are going to run in unsecured RAM
  293. then the CSM must be unlocked in order for the flash
  294. API functions to access the flash.
  295. If the flash API functions are executed from secure memory
  296. (L0-L3) then this step is not required.
  297. ------------------------------------------------------------------*/
  298. Status = Example_CsmUnlock();
  299. if(Status != STATUS_SUCCESS)
  300. {
  301. Example_Error(Status);
  302. }
  303. /*------------------------------------------------------------------
  304. Copy API Functions into SARAM
  305. The flash API functions MUST be run out of internal
  306. zero-waitstate SARAM memory. This is required for
  307. the algos to execute at the proper CPU frequency.
  308. If the algos are already in SARAM then this step
  309. can be skipped.
  310. DO NOT run the algos from Flash
  311. DO NOT run the algos from external memory
  312. ------------------------------------------------------------------*/
  313. /******************************************************************/
  314. // For 2803x, we dont need to copy the API from Flash as it is
  315. // present in BOOT ROM
  316. /******************************************************************/
  317. // If the build links in the 2803x_FlashAPI_BootROMSymbols.lib, then
  318. // The API is in boot ROM and we do not need to copy it from flash.
  319. //
  320. // If the build links in the software API library:
  321. // i.e. Flash2803x_API_<version>.lib
  322. // then we need to copy the flash API from the flash memory to RAM.
  323. #if (BOOT_ROM_API == 0)
  324. Example_MemCopy(&Flash28_API_LoadStart, &Flash28_API_LoadEnd, &Flash28_API_RunStart);
  325. #endif
  326. // We must also copy required user interface functions to RAM.
  327. Example_MemCopy(&RamfuncsLoadStart, &RamfuncsLoadEnd, &RamfuncsRunStart);
  328. /*------------------------------------------------------------------
  329. Initialize Flash_CPUScaleFactor.
  330. Flash_CPUScaleFactor is a 32-bit global variable that the flash
  331. API functions use to scale software delays. This scale factor
  332. must be initialized to SCALE_FACTOR by the user's code prior
  333. to calling any of the Flash API functions. This initialization
  334. is VITAL to the proper operation of the flash API functions.
  335. SCALE_FACTOR is defined in Example_Flash2803x_API.h as
  336. #define SCALE_FACTOR 1048576.0L*( (200L/CPU_RATE) )
  337. This value is calculated during the compile based on the CPU
  338. rate, in nanoseconds, at which the algorithms will be run.
  339. ------------------------------------------------------------------*/
  340. EALLOW;
  341. Flash_CPUScaleFactor = SCALE_FACTOR;
  342. EDIS;
  343. /*------------------------------------------------------------------
  344. Initialize Flash_CallbackPtr.
  345. Flash_CallbackPtr is a pointer to a function. The API uses
  346. this pointer to invoke a callback function during the API operations.
  347. If this function is not going to be used, set the pointer to NULL
  348. NULL is defined in <stdio.h>.
  349. ------------------------------------------------------------------*/
  350. EALLOW;
  351. Flash_CallbackPtr = &MyCallbackFunction;
  352. EDIS;
  353. MyCallbackCounter = 0; // Increment this counter in the callback function
  354. // Jump to SARAM and call the Flash API functions
  355. Example_CallFlashAPI();
  356. }
  357. #endif
  358. /*------------------------------------------------------------------
  359. Example_CallFlashAPI
  360. This function will interface to the flash API.
  361. Parameters:
  362. Return Value:
  363. Notes: This function will be executed from SARAM
  364. -----------------------------------------------------------------*/
  365. #pragma CODE_SECTION(Example_CallFlashAPI,"ramfuncs");
  366. void Example_CallFlashAPI(void)
  367. {
  368. Uint16 i;
  369. Uint16 Status;
  370. Uint16 *Flash_ptr; // Pointer to a location in flash
  371. Uint32 Length; // Number of 16-bit values to be programmed
  372. float32 Version; // Version of the API in floating point
  373. Uint16 VersionHex; // Version of the API in decimal encoded hex
  374. /*------------------------------------------------------------------
  375. Toggle Test
  376. The toggle test is run to verify the frequency configuration of
  377. the API functions.
  378. The selected pin will toggle at 10kHz (100uS cycle time) if the
  379. API is configured correctly.
  380. Example_ToggleTest() supports common output pins. Other pins can be used
  381. by modifying the Example_ToggleTest() function or calling the Flash_ToggleTest()
  382. function directly.
  383. Select a pin that makes sense for the hardware platform being used.
  384. This test will run forever and not return, thus only run this test
  385. to confirm frequency configuration and not during normal API use.
  386. ------------------------------------------------------------------*/
  387. // Example: Toggle GPIO0
  388. // Example_ToggleTest(0);
  389. // Example: Toggle GPIO10
  390. // Example_ToggleTest(10);
  391. // Example: Toggle GPIO15
  392. // Example_ToggleTest(15);
  393. // Example: Toggle GPIO31
  394. // Example_ToggleTest(31);
  395. // Example: Toggle GPIO34
  396. // Example_ToggleTest(34);
  397. /*------------------------------------------------------------------
  398. Check the version of the API
  399. Flash_APIVersion() returns the version in floating point.
  400. FlashAPIVersionHex() returns the version as a decimal encoded hex.
  401. FlashAPIVersionHex() can be used to avoid processing issues
  402. associated with floating point values.
  403. ------------------------------------------------------------------*/
  404. VersionHex = Flash_APIVersionHex();
  405. if(VersionHex != 0x0100)
  406. {
  407. // Unexpected API version
  408. // Make a decision based on this info.
  409. asm(" ESTOP0");
  410. }
  411. Version = Flash_APIVersion();
  412. if(Version != (float32)1.00)
  413. {
  414. // Unexpected API version
  415. // Make a decision based on this info.
  416. asm(" ESTOP0");
  417. }
  418. /*------------------------------------------------------------------
  419. Before programming make sure the sectors are Erased.
  420. ------------------------------------------------------------------*/
  421. // Example: Erase Sector B - Sector H
  422. // Sectors A has example code so leave them unerased
  423. // SECTORA-SECTORH are defined in Flash2803x_API_Library.h
  424. Status = Flash_Erase((SECTORB|SECTORC|SECTORD|SECTORE|SECTORF|SECTORG|SECTORH),&FlashStatus);
  425. if(Status != STATUS_SUCCESS)
  426. {
  427. Example_Error(Status);
  428. }
  429. /*------------------------------------------------------------------
  430. Program Flash Examples
  431. ------------------------------------------------------------------*/
  432. // A buffer can be supplied to the program function. Each word is
  433. // programmed until the whole buffer is programmed or a problem is
  434. // found. If the buffer goes outside of the range of OTP or Flash
  435. // then nothing is done and an error is returned.
  436. // Example: Program 0x400 values in Flash SectorG
  437. // In this case just fill a buffer with data to program into the flash.
  438. for(i=0;i<WORDS_IN_FLASH_BUFFER;i++)
  439. {
  440. Buffer[i] = 0x100+i;
  441. }
  442. Flash_ptr = Sector[1].StartAddr;
  443. Length = 0x400;
  444. Status = Flash_Program(Flash_ptr,Buffer,Length,&FlashStatus);
  445. if(Status != STATUS_SUCCESS)
  446. {
  447. Example_Error(Status);
  448. }
  449. // Verify the values programmed. The Program step itself does a verify
  450. // as it goes. This verify is a 2nd verification that can be done.
  451. Status = Flash_Verify(Flash_ptr,Buffer,Length,&FlashStatus);
  452. if(Status != STATUS_SUCCESS)
  453. {
  454. Example_Error(Status);
  455. }
  456. // Example: Program 0x199 values in Flash SectorG
  457. for(i=0;i<WORDS_IN_FLASH_BUFFER;i++)
  458. {
  459. Buffer[i] = 0x4500+i;
  460. }
  461. Flash_ptr = (Uint16 *)Sector[1].StartAddr+0x450;
  462. Length = 0x199;
  463. Status = Flash_Program(Flash_ptr,Buffer,Length,&FlashStatus);
  464. if(Status != STATUS_SUCCESS)
  465. {
  466. Example_Error(Status);
  467. }
  468. // Verify the values programmed. The Program step itself does a verify
  469. // as it goes. This verify is a 2nd verification that can be done.
  470. Status = Flash_Verify(Flash_ptr,Buffer,Length,&FlashStatus);
  471. if(Status != STATUS_SUCCESS)
  472. {
  473. Example_Error(Status);
  474. }
  475. // You can program a single bit in a memory location and then go back to
  476. // program another bit in the same memory location.
  477. // Example: Program bit 0 in location in Flash SectorF.
  478. // That is program the value 0xFFFE
  479. Flash_ptr = Sector[2].StartAddr;
  480. i = 0xFFFE;
  481. Length = 1;
  482. Status = Flash_Program(Flash_ptr,&i,Length,&FlashStatus);
  483. if(Status != STATUS_SUCCESS)
  484. {
  485. Example_Error(Status);
  486. }
  487. // Example: Program bit 1 in the same location. Remember
  488. // that bit 0 was already programmed so the value will be 0xFFFC
  489. // (bit 0 and bit 1 will both be 0)
  490. i = 0xFFFC;
  491. Length = 1;
  492. Status = Flash_Program(Flash_ptr,&i,Length,&FlashStatus);
  493. if(Status != STATUS_SUCCESS)
  494. {
  495. Example_Error(Status);
  496. }
  497. // Verify the value. This first verify should fail.
  498. i = 0xFFFE;
  499. Status = Flash_Verify(Flash_ptr,&i,Length,&FlashStatus);
  500. if(Status != STATUS_FAIL_VERIFY)
  501. {
  502. Example_Error(Status);
  503. }
  504. // This is the correct value and will pass.
  505. i = 0xFFFC;
  506. Status = Flash_Verify(Flash_ptr,&i,Length,&FlashStatus);
  507. if(Status != STATUS_SUCCESS)
  508. {
  509. Example_Error(Status);
  510. }
  511. // If a bit has already been programmed, it cannot be brought back to a 1 by
  512. // the program function. The only way to bring a bit back to a 1 is by erasing
  513. // the entire sector that the bit belongs to. This example shows the error
  514. // that program will return if a bit is specified as a 1 when it has already
  515. // been programmed to 0.
  516. // Example: Program a single 16-bit value 0x0002, in Flash Sector B
  517. Flash_ptr = Sector[2].StartAddr+1;
  518. i = 0x0002;
  519. Length = 1;
  520. Status = Flash_Program(Flash_ptr,&i,Length,&FlashStatus);
  521. if(Status != STATUS_SUCCESS)
  522. {
  523. Example_Error(Status);
  524. }
  525. // Example: This will return an error!! Can't program 0x0001
  526. // because bit 0 in the the location was previously programmed
  527. // to zero!
  528. i = 0x0001;
  529. Length = 1;
  530. Status = Flash_Program(Flash_ptr,&i,Length,&FlashStatus);
  531. // This should return a STATUS_FAIL_ZERO_BIT_ERROR
  532. if(Status != STATUS_FAIL_ZERO_BIT_ERROR)
  533. {
  534. Example_Error(Status);
  535. }
  536. // Example: This will return an error!! The location specified
  537. // is outside of the Flash and OTP!
  538. Flash_ptr = (Uint16 *)0x00340000;
  539. i = 0x0001;
  540. Length = 1;
  541. Status = Flash_Program(Flash_ptr,&i,Length,&FlashStatus);
  542. // This should return a STATUS_FAIL_ADDR_INVALID error
  543. if(Status != STATUS_FAIL_ADDR_INVALID)
  544. {
  545. Example_Error(Status);
  546. }
  547. // Example: This will return an error!! Can't program 1
  548. // because bit 0 in the the location was previously programmed
  549. // to zero!
  550. for(i=0;i<WORDS_IN_FLASH_BUFFER;i++)
  551. {
  552. Buffer[i] = 0xFFFF;
  553. }
  554. Flash_ptr = Sector[0].EndAddr;
  555. Length = 13;
  556. Status = Flash_Program(Flash_ptr,Buffer,Length,&FlashStatus);
  557. if(Status != STATUS_FAIL_ZERO_BIT_ERROR)//STATUS_FAIL_ADDR_INVALID)
  558. {
  559. Example_Error(Status);
  560. }
  561. /*------------------------------------------------------------------
  562. More Erase Sectors Examples - Clean up the sectors we wrote to:
  563. ------------------------------------------------------------------*/
  564. // Example: Erase Sector G
  565. // SECTORB is defined in Flash2803x_API_Library.h
  566. Status = Flash_Erase(SECTORG,&FlashStatus);
  567. if(Status != STATUS_SUCCESS)
  568. {
  569. Example_Error(Status);
  570. }
  571. // Example: Erase Sector F
  572. // SECTORC is defined in Flash2803x_API_Library.h
  573. Status = Flash_Erase((SECTORF),&FlashStatus);
  574. if(Status != STATUS_SUCCESS)
  575. {
  576. Example_Error(Status);
  577. }
  578. // Example: This will return an error. No valid sector is specified.
  579. Status = Flash_Erase(0,&FlashStatus);
  580. // Should return STATUS_FAIL_NO_SECTOR_SPECIFIED
  581. if(Status != STATUS_FAIL_NO_SECTOR_SPECIFIED)
  582. {
  583. Example_Error(Status);
  584. }
  585. Example_Done();
  586. }
  587. /*------------------------------------------------------------------
  588. Example_CsmUnlock
  589. Unlock the code security module (CSM)
  590. Parameters:
  591. Return Value:
  592. STATUS_SUCCESS CSM is unlocked
  593. STATUS_FAIL_UNLOCK CSM did not unlock
  594. Notes:
  595. -----------------------------------------------------------------*/
  596. Uint16 Example_CsmUnlock()
  597. {
  598. volatile Uint16 temp;
  599. // Load the key registers with the current password
  600. // These are defined in Example_Flash2803x_CsmKeys.asm
  601. EALLOW;
  602. CsmRegs.KEY0 = PRG_key0;
  603. CsmRegs.KEY1 = PRG_key1;
  604. CsmRegs.KEY2 = PRG_key2;
  605. CsmRegs.KEY3 = PRG_key3;
  606. CsmRegs.KEY4 = PRG_key4;
  607. CsmRegs.KEY5 = PRG_key5;
  608. CsmRegs.KEY6 = PRG_key6;
  609. CsmRegs.KEY7 = PRG_key7;
  610. EDIS;
  611. // Perform a dummy read of the password locations
  612. // if they match the key values, the CSM will unlock
  613. temp = CsmPwl.PSWD0;
  614. temp = CsmPwl.PSWD1;
  615. temp = CsmPwl.PSWD2;
  616. temp = CsmPwl.PSWD3;
  617. temp = CsmPwl.PSWD4;
  618. temp = CsmPwl.PSWD5;
  619. temp = CsmPwl.PSWD6;
  620. temp = CsmPwl.PSWD7;
  621. // If the CSM unlocked, return success, otherwise return
  622. // failure.
  623. if ( (CsmRegs.CSMSCR.all & 0x0001) == 0) return STATUS_SUCCESS;
  624. else return STATUS_FAIL_CSM_LOCKED;
  625. }
  626. /*------------------------------------------------------------------
  627. Example_ToggleTest
  628. This function shows example calls to the ToggleTest.
  629. This test is used to Toggle a GPIO pin at a known rate and thus
  630. confirm the frequency configuration of the API functions.
  631. A pin should be selected based on the hardware being used.
  632. Return Value: The toggle test does not return. It will loop
  633. forever and is used only for testing purposes.
  634. Notes:
  635. ----------------------------------------------------------------*/
  636. void Example_ToggleTest(Uint16 PinNumber)
  637. {
  638. Uint32 mask;
  639. // Before calling the Toggle Test, we must setup
  640. // the MUX and DIR registers.
  641. if(PinNumber > (Uint16)34)
  642. {
  643. asm(" ESTOP0"); // Stop here. Invalid option.
  644. for(;;);
  645. }
  646. // Pins GPIO16-GPIO31
  647. else if(PinNumber >= 32)
  648. {
  649. EALLOW;
  650. mask = ~( ((Uint32)1 << (PinNumber-16)*2) | ((Uint32)1 << (PinNumber-32)*2+1) );
  651. GpioCtrlRegs.GPBMUX1.all &= mask;
  652. GpioCtrlRegs.GPBDIR.all = GpioCtrlRegs.GPADIR.all | ((Uint32)1 << (PinNumber-32) );
  653. Flash_ToggleTest(&GpioDataRegs.GPBTOGGLE.all, ((Uint32)1 << PinNumber) );
  654. EDIS;
  655. }
  656. // Pins GPIO16-GPIO31
  657. else if(PinNumber >= 16)
  658. {
  659. EALLOW;
  660. mask = ~( ((Uint32)1 << (PinNumber-16)*2) | ((Uint32)1 << (PinNumber-16)*2+1) );
  661. GpioCtrlRegs.GPAMUX2.all &= mask;
  662. GpioCtrlRegs.GPADIR.all = GpioCtrlRegs.GPADIR.all | ((Uint32)1 << PinNumber);
  663. Flash_ToggleTest(&GpioDataRegs.GPATOGGLE.all, ((Uint32)1 << PinNumber) );
  664. EDIS;
  665. }
  666. // Pins GPIO0-GPIO15
  667. else
  668. {
  669. EALLOW;
  670. mask = ~( ((Uint32)1 << PinNumber*2) | ((Uint32)1 << PinNumber*2+1 ));
  671. GpioCtrlRegs.GPAMUX1.all &= mask;
  672. GpioCtrlRegs.GPADIR.all = GpioCtrlRegs.GPADIR.all | ((Uint32)1 << PinNumber);
  673. EDIS;
  674. Flash_ToggleTest(&GpioDataRegs.GPATOGGLE.all, ((Uint32)1 << PinNumber) );
  675. }
  676. }
  677. /*------------------------------------------------------------------
  678. Simple memory copy routine to move code out of flash into SARAM
  679. -----------------------------------------------------------------*/
  680. void Example_MemCopy(Uint16 *SourceAddr, Uint16* SourceEndAddr, Uint16* DestAddr)
  681. {
  682. while(SourceAddr < SourceEndAddr)
  683. {
  684. *DestAddr++ = *SourceAddr++;
  685. }
  686. return;
  687. }
  688. /*------------------------------------------------------------------
  689. For this example, if an error is found just stop here
  690. -----------------------------------------------------------------*/
  691. #pragma CODE_SECTION(Example_Error,"ramfuncs");
  692. void Example_Error(Uint16 Status)
  693. {
  694. // Error code will be in the AL register.
  695. asm(" ESTOP0");
  696. asm(" SB 0, UNC");
  697. }
  698. /*------------------------------------------------------------------
  699. For this example, once we are done just stop here
  700. -----------------------------------------------------------------*/
  701. #pragma CODE_SECTION(Example_Done,"ramfuncs");
  702. void Example_Done(void)
  703. {
  704. asm(" ESTOP0");
  705. asm(" SB 0, UNC");
  706. }
  707. /*------------------------------------------------------------------
  708. Callback function - must be executed from outside flash/OTP
  709. -----------------------------------------------------------------*/
  710. #pragma CODE_SECTION(MyCallbackFunction,"ramfuncs");
  711. void MyCallbackFunction(void)
  712. {
  713. // Toggle pin, service external watchdog etc
  714. MyCallbackCounter++;
  715. asm(" NOP");
  716. }