Advertisement
Guest User

Flash code TI

a guest
Sep 11th, 2024
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.26 KB | None | 0 0
  1.  
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<stddef.h>
  5. #include "driverlib.h"
  6. #include "device.h"
  7. #include "FlashTech_F28P55x_C28x.h"
  8. #include "flash_programming_f28p55x.h"
  9.  
  10. #define WORDS_IN_FLASH_BUFFER 0x1000
  11.  
  12. #define n 216
  13.  
  14. #pragma DATA_SECTION(Buffer,"DataBufferSection");
  15. uint16 Buffer[WORDS_IN_FLASH_BUFFER];
  16. uint32 *Buffer32 = (uint32 *)Buffer;
  17. size_t datasize;
  18.  
  19. #define CRC16_POLYNOMIAL 0x8005 // Define the CRC16 polynomial
  20.  
  21. // Define the PDUData structure
  22. typedef struct {
  23. // Voltage readings (6 values)
  24. uint16_t primaryVoltageR;
  25. uint16_t primaryVoltageY;
  26. uint16_t primaryVoltageB;
  27. uint16_t secondaryVoltageR;
  28. uint16_t secondaryVoltageY;
  29. uint16_t secondaryVoltageB;
  30.  
  31. // Current readings (8 values)
  32. uint16_t primaryCurrentR;
  33. uint16_t primaryCurrentY;
  34. uint16_t primaryCurrentB;
  35. uint16_t secondaryCurrentR;
  36. uint16_t secondaryCurrentY;
  37. uint16_t secondaryCurrentB;
  38. uint16_t primaryNeutralCurrent;
  39. uint16_t secondaryNeutralCurrent;
  40.  
  41. // Power readings (5 values)
  42. uint16_t primaryKW;
  43. uint16_t secondaryKWR;
  44. uint16_t secondaryKWY;
  45. uint16_t secondaryKWB;
  46. uint16_t groundCurrent;
  47.  
  48. // CT Current Gain (216 values)
  49. uint16_t ctCurrentGain[n];
  50.  
  51. // kW Gain (216 values)
  52. uint16_t kwGain[n];
  53. } PDUData;
  54.  
  55. // Define the VersionInfo structure
  56. typedef struct {
  57. uint16_t sectorIndex;
  58. uint16_t majorVersion;
  59. uint16_t minorVersion;
  60. uint16_t isValid;
  61. uint16_t crc;
  62. PDUData calibdata;
  63. } VersionInfo;
  64.  
  65. // Function prototypes
  66. uint16_t crc16(uint16_t *data, size_t length_in_16bit_units);
  67. void WritePDUDataToFlash(void);
  68.  
  69. // CRC16 calculation function
  70. uint16_t crc16(uint16_t *data, size_t length_in_16bit_units) {
  71. uint16_t crc = 0xFFFF;
  72. size_t i;
  73. for ( i = 0; i < length_in_16bit_units; i++) {
  74. crc ^= data[i];
  75. uint8_t j;
  76. for ( j = 0; j < 16; j++) {
  77. if (crc & 0x8000) {
  78. crc = (crc << 1) ^ CRC16_POLYNOMIAL;
  79. } else {
  80. crc <<= 1;
  81. }
  82. }
  83. }
  84. return crc;
  85. }
  86.  
  87. // Function to write PDU data to flash
  88. void WritePDUDataToFlash(void) {
  89. PDUData pduData = {
  90. .primaryVoltageR = 3770,
  91. .primaryVoltageY = 3770,
  92. .primaryVoltageB = 3770,
  93. .secondaryVoltageR = 2405,
  94. .secondaryVoltageY = 2345,
  95. .secondaryVoltageB = 2392,
  96. .primaryCurrentR = 3300,
  97. .primaryCurrentY = 3300,
  98. .primaryCurrentB = 3300,
  99. .secondaryCurrentR = 1855,
  100. .secondaryCurrentY = 1865,
  101. .secondaryCurrentB = 1865,
  102. .primaryNeutralCurrent = 2000,
  103. .secondaryNeutralCurrent = 1905,
  104. .primaryKW = 165,
  105. .secondaryKWR = 3950,
  106. .secondaryKWY = 4100,
  107. .secondaryKWB = 4100,
  108. .groundCurrent = 100,
  109. // .kwGain=9677,
  110. // .ctCurrentGain=885
  111.  
  112. };
  113.  
  114. // Initialize CT Current Gain and kW Gain
  115. int k;
  116. for ( k = 0; k < n; k++) {
  117. pduData.ctCurrentGain[k] = 885;
  118. pduData.kwGain[k] = 9677;
  119. }
  120.  
  121. uint16_t wordCount = sizeof(PDUData) / sizeof(uint16_t);
  122.  
  123. VersionInfo verinfo = {
  124. .calibdata = pduData,
  125. .majorVersion = 1,
  126. .minorVersion = 0,
  127. .sectorIndex = 1,
  128. .isValid = 0xFFFF,
  129. .crc = crc16((uint16_t*)&pduData, wordCount)
  130. };
  131.  
  132. size_t ver = sizeof(VersionInfo) / sizeof(uint16_t);
  133. uint16_t* dataToWrite = (uint16_t*)&verinfo;
  134. uint16_t i;
  135. for ( i = 0; i < ver; i++) {
  136. Buffer[i] = dataToWrite[i];
  137. }
  138.  
  139. printf("Word Count : %d \n", (int)wordCount);
  140. datasize = ver; // Update datasize with the total size of VersionInfo
  141.  
  142. printf("Size of PDUData: %zu bytes\n", sizeof(PDUData));
  143. printf("Size of VersionInfo: %zu bytes\n", sizeof(VersionInfo));
  144. printf("datasize: %zu words\n", sizeof(VersionInfo) / sizeof(uint16_t));
  145. }
  146.  
  147.  
  148. void Example_Error(Fapi_StatusType status);
  149. void Example_Done(void);
  150. void Example_CallFlashAPI(void);
  151. void FMSTAT_Fail(void);
  152. void ECC_Fail(void);
  153. void Example_EraseSector(void);
  154.  
  155.  
  156. void Example_ProgramUsingAutoECC(void);
  157. void ClearFSMStatus(void);
  158.  
  159.  
  160.  
  161. void Example_ReadFlash(uint32_t startAddress, uint32_t length);
  162.  
  163. void main(void)
  164. {
  165. //
  166. // Initialize device clock and peripherals
  167. // Copy the Flash initialization code from Flash to RAM
  168. // Copy the Flash API from Flash to RAM
  169. // Configure Flash wait-states, fall back power mode, performance features
  170. // and ECC
  171. //
  172. Device_init();
  173.  
  174. //
  175. // Initialize GPIO
  176. //
  177. Device_initGPIO();
  178.  
  179. //
  180. // Initialize PIE and clear PIE registers. Disables CPU interrupts.
  181. //
  182. Interrupt_initModule();
  183.  
  184. //
  185. // Initialize the PIE vector table with pointers to the shell Interrupt
  186. // Service Routines (ISR).
  187. //
  188. Interrupt_initVectorTable();
  189.  
  190. //
  191. // Enable Global Interrupt (INTM) and realtime interrupt (DBGM)
  192. //
  193. EINT;
  194. ERTM;
  195.  
  196. //
  197. // At 150MHz, execution wait-states for external oscillator is 3. Modify the
  198. // wait-states when the system clock frequency is changed.
  199. //
  200. Flash_initModule(FLASH0CTRL_BASE, FLASH0ECC_BASE, 3);
  201.  
  202. //
  203. // Flash API functions should not be executed from the same bank on which
  204. // erase/program operations are in progress.
  205. // Also, note that there should not be any access to the Flash bank on
  206. // which erase/program operations are in progress. Hence below function
  207. // is mapped to RAM for execution.
  208. //
  209. Example_CallFlashAPI();
  210.  
  211. //
  212. // Example is done here
  213. //
  214.  
  215. puts("done");
  216. Example_Done();
  217. }
  218.  
  219.  
  220.  
  221. //***********************************************************************************************
  222. // ClearFSMStatus
  223. //
  224. // This function clears the status (STATCMD, similar to FMSTAT of the previous
  225. // devices) of the previous flash operation.
  226. // This function and the flash API functions used in this function are
  227. // executed from RAM in this example.
  228. // Note: this function is applicable for only F280013X, F280015X F28P55X and F28P65X devices
  229. //***********************************************************************************************
  230. #ifdef __cplusplus
  231. #pragma CODE_SECTION(".TI.ramfunc");
  232. #else
  233. #pragma CODE_SECTION(ClearFSMStatus, ".TI.ramfunc");
  234. #endif
  235. void ClearFSMStatus(void){
  236. Fapi_FlashStatusType oFlashStatus;
  237. Fapi_StatusType oReturnCheck;
  238.  
  239. //
  240. // Wait until FSM is done with the previous flash operation
  241. //
  242. while (Fapi_checkFsmForReady() != Fapi_Status_FsmReady){}
  243. oFlashStatus = Fapi_getFsmStatus();
  244. if(oFlashStatus != 0)
  245. {
  246.  
  247. /* Clear the Status register */
  248. oReturnCheck = Fapi_issueAsyncCommand(Fapi_ClearStatus);
  249. //
  250. // Wait until status is cleared
  251. //
  252. while (Fapi_getFsmStatus() != 0) {}
  253.  
  254. if(oReturnCheck != Fapi_Status_Success)
  255. {
  256. //
  257. // Check Flash API documentation for possible errors
  258. //
  259. Example_Error(oReturnCheck);
  260. }
  261. }
  262. }
  263.  
  264.  
  265.  
  266. //*****************************************************************************
  267. // Example_CallFlashAPI
  268. //
  269. // This function will interface to the flash API.
  270. // Flash API functions used in this function are executed from RAM in this
  271. // example.
  272. //*****************************************************************************
  273. #ifdef __cplusplus
  274. #pragma CODE_SECTION(".TI.ramfunc");
  275. #else
  276. #pragma CODE_SECTION(Example_CallFlashAPI, ".TI.ramfunc");
  277. #endif
  278. void Example_CallFlashAPI(void)
  279. {
  280. uint16 i = 0;
  281. Fapi_StatusType oReturnCheck;
  282.  
  283. //
  284. // Initialize the Flash API by providing the Flash register base address
  285. // and operating frequency(in MHz).
  286. // This function is required to initialize the Flash API based on System
  287. // frequency before any other Flash API operation can be performed.
  288. // This function must also be called whenever System frequency or RWAIT is
  289. // changed.
  290. //
  291. oReturnCheck = Fapi_initializeAPI(FlashTech_CPU0_BASE_ADDRESS,
  292. DEVICE_SYSCLK_FREQ/1000000U);
  293.  
  294. if(oReturnCheck != Fapi_Status_Success)
  295. {
  296. //
  297. // Check Flash API documentation for possible errors
  298. //
  299. Example_Error(oReturnCheck);
  300. }
  301.  
  302. //
  303. // Initialize the Flash banks and FMC for erase and program operations.
  304. // Fapi_setActiveFlashBank() function sets the Flash banks and FMC for
  305. // further Flash operations to be performed on the banks.
  306. //
  307. oReturnCheck = Fapi_setActiveFlashBank(Fapi_FlashBank0);
  308.  
  309. if(oReturnCheck != Fapi_Status_Success)
  310. {
  311. //
  312. // Check Flash API documentation for possible errors
  313. //
  314. Example_Error(oReturnCheck);
  315. }
  316.  
  317. Example_EraseSector();
  318.  
  319. WritePDUDataToFlash();
  320. //
  321. // Erase bank before programming
  322. //
  323. //Example_EraseBanks();
  324.  
  325. //
  326. // Fill a buffer with data to program into the flash.
  327. //
  328. //for(i=0; i < WORDS_IN_FLASH_BUFFER; i++)
  329. // {
  330. // Buffer[i] = i;
  331. //}
  332.  
  333. //
  334. // Program the sector using AutoECC option
  335. //
  336. Example_ProgramUsingAutoECC();
  337.  
  338.  
  339.  
  340. //Example_ReadFlash( FlashBank4StartAddress , datasize);
  341. //
  342. // Erase bank before programming
  343. //
  344. //Example_EraseBanks();
  345.  
  346. //
  347. // Program the sector using DataOnly and ECCOnly options
  348. //
  349. // Example_ProgramUsingDataOnlyECCOnly();
  350.  
  351. //
  352. // Erase the sector before programming
  353. //
  354.  
  355.  
  356. //
  357. // Program Bank using AutoECC option
  358. //
  359. // Note that executing entire bank needs a special flash API library -
  360. // for this
  361. // Example_ProgramBankUsingAutoECC();
  362.  
  363. //
  364. // Erase bank before programming
  365. //
  366. // Example_EraseBanks();
  367.  
  368. //
  369. // Program the sector using DataAndECC option
  370. //
  371. // Example_ProgramUsingDataAndECC();
  372.  
  373. //
  374. // Erase the sector for cleaner exit from the example.
  375. //
  376. //Example_EraseSector();
  377.  
  378.  
  379. }
  380.  
  381. //*****************************************************************************
  382. // Example_ProgramUsingAutoECC
  383. //
  384. // Example function to Program data in Flash using "AutoEccGeneration" option.
  385. // Flash API functions used in this function are executed from RAM in this
  386. // example.
  387. //*****************************************************************************
  388. #ifdef __cplusplus
  389. #pragma CODE_SECTION(".TI.ramfunc");
  390. #else
  391. #pragma CODE_SECTION(Example_ProgramUsingAutoECC, ".TI.ramfunc");
  392. #endif
  393. void Example_ProgramUsingAutoECC(void)
  394. {
  395. uint32 u32Index = 0;
  396. uint16 i = 0;
  397. Fapi_StatusType oReturnCheck;
  398. Fapi_FlashStatusType oFlashStatus;
  399. Fapi_FlashStatusWordType oFlashStatusWord;
  400.  
  401. //
  402. // A data buffer of max 8 16-bit words can be supplied to the program
  403. // function.
  404. // Each word is programmed until the whole buffer is programmed or a
  405. // problem is found. However to program a buffer that has more than 8
  406. // words, program function can be called in a loop to program 8 words for
  407. // each loop iteration until the whole buffer is programmed.
  408. //
  409. // Remember that the main array flash programming must be aligned to
  410. // 64-bit address boundaries and each 64 bit word may only be programmed
  411. // once per write/erase cycle. Meaning the length of the data buffer
  412. // (3rd parameter for Fapi_issueProgrammingCommand() function) passed
  413. // to the program function can only be either 4 or 8.
  414. //
  415. // Program data in Flash using "AutoEccGeneration" option.
  416. // When AutoEccGeneration option is used, Flash API calculates ECC for the
  417. // given 64-bit data and programs it along with the 64-bit main array data.
  418. // Note that any unprovided data with in a 64-bit data slice
  419. // will be assumed as 1s for calculating ECC and will be programmed.
  420. //
  421. // Note that data buffer (Buffer) is aligned on 64-bit boundary for verify
  422. // reasons.
  423. //
  424. // Monitor ECC address for the sector below while programming with
  425. // AutoEcc mode.
  426. //
  427. // In this example, the number of bytes specified in the flash buffer
  428. // are programmed in the flash sector below along with auto-generated
  429. // ECC.
  430. //
  431.  
  432. for(i=0, u32Index = FlashBank4StartAddress;
  433. (u32Index < (FlashBank4StartAddress + datasize));
  434. i+= 8, u32Index+= 8)
  435. {
  436. ClearFSMStatus();
  437.  
  438. // Enable program/erase protection for select sectors where this example is
  439. // located
  440. // CMDWEPROTA is applicable for sectors 0-31
  441. // Bits 0-11 of CMDWEPROTB is applicable for sectors 32-127, each bit represents
  442. // a group of 8 sectors, e.g bit 0 represents sectors 32-39, bit 1 represents
  443. // sectors 40-47, etc
  444. Fapi_setupBankSectorEnable(FLASH_WRAPPER_PROGRAM_BASE+FLASH_O_CMDWEPROTA, 0xFFFFFF00);
  445. Fapi_setupBankSectorEnable(FLASH_WRAPPER_PROGRAM_BASE+FLASH_O_CMDWEPROTB, 0x00000003);
  446.  
  447.  
  448. oReturnCheck = Fapi_issueProgrammingCommand((uint32 *)u32Index,Buffer+i,
  449. 8, 0, 0, Fapi_AutoEccGeneration);
  450.  
  451. //
  452. // Wait until the Flash program operation is over
  453. //
  454. while(Fapi_checkFsmForReady() == Fapi_Status_FsmBusy);
  455.  
  456. if(oReturnCheck != Fapi_Status_Success)
  457. {
  458. //
  459. // Check Flash API documentation for possible errors
  460. //
  461. Example_Error(oReturnCheck);
  462. }
  463.  
  464. //
  465. // Read FMSTAT register contents to know the status of FSM after
  466. // program command to see if there are any program operation related
  467. // errors
  468. //
  469. oFlashStatus = Fapi_getFsmStatus();
  470. if(oFlashStatus != 3)
  471. {
  472. //
  473. //Check FMSTAT and debug accordingly
  474. //
  475. FMSTAT_Fail();
  476. }
  477.  
  478. //
  479. // Verify the programmed values. Check for any ECC errors.
  480. //
  481. oReturnCheck = Fapi_doVerify((uint32 *)u32Index,
  482. 4, (uint32 *)(uint32)(Buffer + i),
  483. &oFlashStatusWord);
  484.  
  485. Fapi_setupBankSectorEnable(FLASH_WRAPPER_PROGRAM_BASE+FLASH_O_CMDWEPROTA, 0xFFFFFFFF);
  486.  
  487. if(oReturnCheck != Fapi_Status_Success)
  488. {
  489. //
  490. // Check Flash API documentation for possible errors
  491. //
  492. Example_Error(oReturnCheck);
  493. }
  494. }
  495. }
  496.  
  497.  
  498.  
  499. #ifdef __cplusplus
  500. #pragma CODE_SECTION(".TI.ramfunc");
  501. #else
  502. #pragma CODE_SECTION(Example_EraseSector, ".TI.ramfunc");
  503. #endif
  504. void Example_EraseSector(void)
  505. {
  506. Fapi_StatusType oReturnCheck;
  507. Fapi_FlashStatusType oFlashStatus;
  508. Fapi_FlashStatusWordType oFlashStatusWord;
  509. ClearFSMStatus();
  510.  
  511. // Enable program/erase protection for select sectors where this example is
  512. // located
  513. // CMDWEPROTA is applicable for sectors 0-31
  514. // Bits 0-11 of CMDWEPROTB is applicable for sectors 32-127, each bit represents
  515. // a group of 8 sectors, e.g bit 0 represents sectors 32-39, bit 1 represents
  516. // sectors 40-47, etc
  517. Fapi_setupBankSectorEnable(FLASH_WRAPPER_PROGRAM_BASE+FLASH_O_CMDWEPROTA, 0xFFFFFF00);
  518. Fapi_setupBankSectorEnable(FLASH_WRAPPER_PROGRAM_BASE+FLASH_O_CMDWEPROTB, 0x00000000);
  519.  
  520.  
  521. //
  522. // Erase the sector that is programmed in the above example
  523. // Erase Sector 0
  524. //
  525. oReturnCheck = Fapi_issueAsyncCommandWithAddress(Fapi_EraseSector,
  526. (uint32 *)FlashBank4StartAddress);
  527. //
  528. // Wait until FSM is done with erase sector operation
  529. //
  530. while (Fapi_checkFsmForReady() != Fapi_Status_FsmReady){}
  531.  
  532. if(oReturnCheck != Fapi_Status_Success)
  533. {
  534. //
  535. // Check Flash API documentation for possible errors
  536. //
  537. Example_Error(oReturnCheck);
  538. }
  539.  
  540. //
  541. // Read FMSTAT register contents to know the status of FSM after
  542. // erase command to see if there are any erase operation related errors
  543. //
  544. oFlashStatus = Fapi_getFsmStatus();
  545. if(oFlashStatus != 3)
  546. {
  547. //
  548. // Check Flash API documentation for FMSTAT and debug accordingly
  549. // Fapi_getFsmStatus() function gives the FMSTAT register contents.
  550. // Check to see if any of the EV bit, ESUSP bit, CSTAT bit or
  551. // VOLTSTAT bit is set (Refer to API documentation for more details).
  552. //
  553. FMSTAT_Fail();
  554. }
  555.  
  556. //
  557. // Verify that Sector0 is erased
  558. //
  559. oReturnCheck = Fapi_doBlankCheck((uint32 *)FlashBank4StartAddress,
  560. Sector2KB_u32length,
  561. &oFlashStatusWord);
  562. if(oReturnCheck != Fapi_Status_Success)
  563. {
  564. //
  565. // Check Flash API documentation for error info
  566. //
  567. Example_Error(oReturnCheck);
  568. }
  569. }
  570.  
  571.  
  572.  
  573. //******************************************************************************
  574. // For this example, just stop here if an API error is found
  575. //******************************************************************************
  576. void Example_Error(Fapi_StatusType status)
  577. {
  578. //
  579. // Error code will be in the status parameter
  580. //
  581. __asm(" ESTOP0");
  582. }
  583.  
  584. //******************************************************************************
  585. // For this example, once we are done just stop here
  586. //******************************************************************************
  587. void Example_Done(void)
  588. {
  589. __asm(" ESTOP0");
  590. }
  591.  
  592. //******************************************************************************
  593. // For this example, just stop here if FMSTAT fail occurs
  594. //******************************************************************************
  595. void FMSTAT_Fail(void)
  596. {
  597. __asm(" ESTOP0");
  598. }
  599.  
  600. //******************************************************************************
  601. // For this example, just stop here if ECC fail occurs
  602. //******************************************************************************
  603. void ECC_Fail(void)
  604. {
  605. __asm(" ESTOP0");
  606. }
  607.  
  608. //
  609. // End of File
  610. //
  611. /*
  612. void Example_ReadFlash(uint32_t startAddress, uint32_t length)
  613. {
  614. uint16_t *flashPtr = (uint16_t *)startAddress;
  615. char buffer[64];
  616. uint32_t i;
  617.  
  618. for (i = 0; i < length; i++)
  619. {
  620. // Read the content from flash memory
  621. uint16_t data = *(flashPtr + i);
  622.  
  623. // Convert the data to a string and print it as an integer
  624. snprintf(buffer, sizeof(buffer), "Address: 0x%08X, Data: %u",
  625. startAddress + (i * sizeof(uint16_t)), data);
  626. puts(buffer);
  627. }
  628. }*/
  629.  
  630. void Example_ReadFlash(uint32_t startAddress, uint32_t length)
  631. {
  632. printf("Reading %zu words from flash address %4X\n", length, startAddress);
  633. uint16_t *flashPtr = (uint16_t *)startAddress;
  634. uint32_t i;
  635.  
  636. for (i = 0; i < length; i++)
  637. {
  638.  
  639. // Read the content from flash memory and print it as a decimal value
  640. printf("%5u ", flashPtr[i]);
  641.  
  642. // Add a newline every 8 values for better readability
  643. if ((i + 1) % 8 == 0)
  644. {
  645. printf("\n");
  646. }
  647. }
  648.  
  649. // Ensure we end with a newline
  650. if (length % 8 != 0)
  651. {
  652. printf("\n");
  653. }
  654. }
  655.  
  656.  
  657.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement