47d40a4fe429348420e574930aecd29da92b81e9.svn-base 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /* */
  3. /* Copyright(c) 2016 Nuvoton Technology Corp. All rights reserved. */
  4. /* */
  5. /*---------------------------------------------------------------------------------------------------------*/
  6. //***********************************************************************************************************
  7. // Nuvoton Technoledge Corp.
  8. // Website: http://www.nuvoton.com
  9. // E-Mail : [email protected]
  10. // Date : Apr/21/2016
  11. //***********************************************************************************************************
  12. //***********************************************************************************************************
  13. // File Function: N76E003 APROM program DATAFLASH as EEPROM way
  14. //***********************************************************************************************************
  15. #include "N76E003.h"
  16. #include "Common.h"
  17. #include "Delay.h"
  18. #include "SFR_Macro.h"
  19. #include "Function_define.h"
  20. #define ADDR_BASE 0x4700
  21. #define FLASH_L_LOW ADDR_BASE+1
  22. #define FLASH_L_HIGH ADDR_BASE+2
  23. #define FLASH_H_LOW ADDR_BASE+3
  24. #define FLASH_H_HIGH ADDR_BASE+4
  25. volatile unsigned char xdata page_buffer[128];
  26. UINT8 Read_APROM_BYTE ( UINT16 code *u16_addr )
  27. {
  28. UINT8 rdata;
  29. rdata = *u16_addr >> 8;
  30. return rdata;
  31. }
  32. /*****************************************************************************************************************
  33. write_DATAFLASH_BYTE :
  34. user can copy all this subroutine into project, then call this function in main.
  35. ******************************************************************************************************************/
  36. void Write_DATAFLASH_BYTE ( unsigned int u16EPAddr, unsigned char u8EPData )
  37. {
  38. unsigned char looptmp = 0;
  39. unsigned int u16_addrl_r;
  40. unsigned int RAMtmp;
  41. //Check page start address
  42. u16_addrl_r = ( u16EPAddr / 128 ) * 128;
  43. //Save APROM data to XRAM0
  44. for ( looptmp = 0; looptmp < 0x80; looptmp++ )
  45. {
  46. RAMtmp = Read_APROM_BYTE ( ( unsigned int code * ) ( u16_addrl_r + looptmp ) );
  47. page_buffer[looptmp] = RAMtmp;
  48. }
  49. // Modify customer data in XRAM
  50. page_buffer[u16EPAddr & 0x7f] = u8EPData;
  51. //Erase APROM DATAFLASH page
  52. IAPAL = u16_addrl_r & 0xff;
  53. IAPAH = ( u16_addrl_r >> 8 ) & 0xff;
  54. IAPFD = 0xFF;
  55. set_IAPEN;
  56. set_APUEN;
  57. IAPCN = 0x22;
  58. set_IAPGO;
  59. //Save changed RAM data to APROM DATAFLASH
  60. set_IAPEN;
  61. set_APUEN;
  62. IAPCN = 0x21;
  63. for ( looptmp = 0; looptmp < 0x80; looptmp++ )
  64. {
  65. IAPAL = ( u16_addrl_r & 0xff ) + looptmp;
  66. IAPAH = ( u16_addrl_r >> 8 ) & 0xff;
  67. IAPFD = page_buffer[looptmp];
  68. set_IAPGO;
  69. }
  70. clr_APUEN;
  71. clr_IAPEN;
  72. }
  73. /******************************************************************************************************************/
  74. void main ( void )
  75. {
  76. UINT8 datatemp;
  77. UINT16 system16highsite;
  78. /* -------------------------------------------------------------------------*/
  79. /* Dataflash use APROM area, please ALWAYS care the address of you code */
  80. /* APROM 0x3800~0x38FF demo as dataflash */
  81. /* Please use Memory window key in C:0x3800 to check earse result */
  82. /* -------------------------------------------------------------------------*/
  83. InitialUART0_Timer1 ( 115200 );
  84. //call write byte
  85. Write_DATAFLASH_BYTE ( 0x3802, 0x34 );
  86. Write_DATAFLASH_BYTE ( FLASH_L_LOW, 0x55 );
  87. Write_DATAFLASH_BYTE ( FLASH_L_HIGH, 0x56 );
  88. Write_DATAFLASH_BYTE ( FLASH_H_LOW, 0xaa );
  89. Write_DATAFLASH_BYTE ( FLASH_H_HIGH, 0x66 );
  90. //call read byte
  91. datatemp = Read_APROM_BYTE ( 0x3802 );
  92. system16highsite = ( ( Read_APROM_BYTE ( FLASH_H_HIGH ) << 8 ) + Read_APROM_BYTE ( FLASH_H_LOW ) );
  93. while ( 1 )
  94. {
  95. // printf ("\n data temp = 0x%bx", datatemp);
  96. }
  97. }
  98. //-----------------------------------------------------------------------------------------------------------