×
嵌入式 > 技术百科 > 详情

Freescale 9S12 系列单片机应用笔记(EETS4K模块) 1

发布时间:2020-06-20 发布时间:
|

EETS4K 模块应用笔记(1)

9S12 系列单片机的通常包含4KB 的EEPROM。Freescale 将EEPROM 模块称之为 EETS4K。实际上,这里所谓的 EEPROM 其实是FLASH,只不过Freescale 特意将这里Flash 的 sector 做的很小(4Bytes),使得用户用起来像是在用 EEPROM。

 

EEPROM 是直接映射到9S12 单片机的地址空间的,如果程序中只是读取 EEPROM 中的内容,而不涉及到对 EEPROM 中数据的修改。那就不需要特殊的编程。就像读取RAM数据那样直接访问就可以了。

 

只有当需要在程序中更新EEPROM 中内容时,才需要学习下面的内容。

初始化EETS4K

在向EETS4K写入数据或擦除数据前要先配置EETS4K的时钟。EETS4K的时钟频率必须在150KHz——200KHz之间,为此需要配置 ECLKDIV 寄存器。

ECLKDIV 寄存器(EEPROM Clock Divider Register)

图 1ECLKDIV 寄存器

 

PRDIV8是预分频位:当PRDIV8=1时输入时钟被预分频为1/8。

EDIV5——EDIV8为分频除数寄存器,最多可以产生1/64 的分频比。简单的计算可知,当输入时钟大于12.8MHz 时需要将 PRDIV8 置位。

经过PRDIV8和EDIV两级分频最多可将时钟频率分为1/512。

擦除和写入和读取

这里不详细介绍每一个寄存器的用法。只对需要注意的地方加以说明。

EETS4K 模块的最小擦除单位是4Bytes,EETS4K 模块提供了两条相关命令,一条是擦除一个 sector,也就是 4 字节,并且要求是字节对其的双字。另一条命令擦除全部EEPROM 空间。

每次编程(写入)单位为两个字节。并且这两个字节要是对其字。

当EETS4K 模块正在进行擦除或编程操作时是不能同时读取EEPROM中内容的。

 

有了这些介绍就够了。下面给出一个具体的例子。


 

  1. /*EETS4K.h*/  
  2. #ifndef  NVM_H  
  3. #define  NVM_H  
  4.   
  5. /* 
  6.  *                                       CONSTANTS 
  7.  */  
  8.   
  9. #define  NVM_NO_ERR                (1)  
  10. #define  NVM_ODD_ACCESS_ERR       (-1)  
  11. #define  NVM_ACCESS_ERR           (-2)  
  12. #define  NVM_PROTECTION_ERR       (-3)  
  13.   
  14. /* 
  15.  *                                           FUNCTION PROTOTYPES 
  16.  */  
  17.   
  18. void  EEPROM_Init(unsigned long sysclk);  
  19. char  EEPROM_Write_Word(unsigned int address, unsigned int data);  
  20. char  EEPROM_Erase_Sector(unsigned int address);  
  21. char  EEPROM_Erase_All (void);  
  22. unsigned int EEPROM_Read_Word(unsigned int address);  
  23.   
  24. #endif                    /* End of file */  
 
  1. /*EETS4K.C*/  
  2. #include       /* common defines and macros */  
  3. #include "derivative.h"      /* derivative-specific definitions */  
  4. #include "eets4k.h"  
  5.   
  6.   
  7. /**  @brief This function initializes the Non Volatile EEPROM control registers  
  8.  *            and must be called before attempting to write or erase an EEPROM sector. 
  9.  *   
  10.  *   @para sysclk the CPU clock frequency (SYSCLK) driven by the onboard oscillator or the PLL if enabled. 
  11.  */  
  12. void  EEPROM_Init (unsigned long sysclk)  
  13. {  
  14.     unsigned char  eclk_val;  
  15.       
  16.       
  17.     if (sysclk >= 12000) {                /* If the SYSCLK is > 12MHz, then set FDIV8 bit */            
  18.         eclk_val  =  (sysclk  / (8*200)) - 1;    /* Compute the correct divider value */  
  19.         ECLKDIV  |=   ECLKDIV_PRDIV8_MASK | eclk_val;   /* Write the ECLKDIV register with the correct settings */  
  20.     } else {  
  21.         eclk_val  =  (sysclk / 200) - 1;         /* Compute the correct divider value */  
  22.         ECLKDIV  |=   eclk_val;                  /* Write the ECLKDIV register with the correct settings */  
  23.     }  
  24.   
  25.     ESTAT        |=  (ESTAT_PVIOL_MASK | ESTAT_ACCERR_MASK); /* Clear any error flags  */  
  26. }  
  27.   
  28. /**  @brief This function writes a 16-bit word to EEPROM 
  29.  *   @param  address, the destination EEPROM address to write the data 
  30.  *   @param  data,    the data to write to argument address. 
  31.  *   @return  
  32.  *               NVM_NO_ERR           - EEPROM Write Success 
  33.  *               NVM_ODD_ACCESS_ERR   - EEPROM Write Error, Address not on an even address boundry 
  34.  *               NVM_ACCESS_ERR       - EEPROM Write Error, Access Violation 
  35.  *               NVM_PROTECTION_ERR   - EEPROM Write Error, Attempted to write a protected sector 
  36.  */  
  37. char  EEPROM_Write_Word (unsigned int address, unsigned int data)  
  38. {  
  39.     while (!ESTAT_CBEIF) {    /* Wait for EEPROM access controller to become ready */  
  40.         ;  
  41.     }  
  42.       
  43.     ESTAT = (ESTAT_ACCERR_MASK | ESTAT_PVIOL_MASK); /* Clear existing error flags */  
  44.       
  45.     if (address & 0x0001) {  
  46.         return (NVM_ODD_ACCESS_ERR);                /* Address is NOT aligned on an even boundry? */  
  47.     }  
  48.       
  49.     (*(unsigned int *)address) = data;              /* Write the data to the specified address */  
  50.   
  51.     ECMD = ECMD_CMDB5_MASK;                          /* Store programming command in FCMD */  
  52.     ESTAT_CBEIF = 1;                                /* Execute the command */  
  53.   
  54.     if (ESTAT_ACCERR) {                             /* Check if there has been an access error */  
  55.         return (NVM_ACCESS_ERR);                    /* Return an Access Error code */  
  56.     }  
  57.       
  58.     if (ESTAT_PVIOL) {                              /* Check if there has been a protection error  */  
  59.         return (NVM_PROTECTION_ERR);                /* Return a Protection Error code */  
  60.     }  
  61.       
  62.     return (NVM_NO_ERR);                            /* Return No Error */  
  63. }  
  64.   
  65. /**  @brief This function erases a 4-byte sector of EEPROM 
  66.  *   @param address, the start of the 4-byte sector to address 
  67.  *   @return   
  68.  *          NVM_NO_ERR           - EEPROM Write Success 
  69.  *          NVM_ODD_ACCESS_ERR   - EEPROM Write Error, Address not on an even address boundry 
  70.  *          NVM_ACCESS_ERR       - EEPROM Write Error, Access Violation 
  71.  *          NVM_PROTECTION_ERR   - EEPROM Write Error, Attempted to write a protected sector 
  72.  */  
  73. char  EEPROM_Erase_Sector (unsigned int address)  
  74. {  
  75.     while (!ESTAT_CBEIF) {                            /* Wait for EEPROM access controller to become ready */  
  76.         ;  
  77.     }  
  78.       
  79.     ESTAT = (ESTAT_ACCERR_MASK | ESTAT_PVIOL_MASK);   /* Clear existing error flags  */  
  80.       
  81.     if (address & 0x0001) {  
  82.         return (NVM_ODD_ACCESS_ERR);                  /* Address is NOT aligned on an even boundry? */  
  83.     }  
  84.       
  85.     (*(unsigned int *)address) = 0xFFFF;              /* Write the data to the specified address */  
  86.   
  87.     ECMD = ECMD_CMDB6_MASK;                           /* Store programming command in FCMD */  
  88.     ESTAT_CBEIF = 1;                                  /* Execute the command  */  
  89.   
  90.     if (ESTAT_ACCERR) {                               /* Check if there has been an access error */  
  91.         return (NVM_ACCESS_ERR);                      /* Return an Access Error code */  
  92.     }  
  93.       
  94.     if (ESTAT_PVIOL) {                                /* Check if there has been a protection error */  
  95.         return (NVM_PROTECTION_ERR);                  /* Return a Protection Error code */  
  96.     }  
  97.       
  98.     return (NVM_NO_ERR);                              /* Return No Error */  
  99. }  
  100. char  EEPROM_Erase_All (void)  
  101. {  
  102.     while (!ESTAT_CBEIF) {                            /* Wait for EEPROM access controller to become ready */  
  103.         ;  
  104.     }  
  105.       
  106.     ESTAT = (ESTAT_ACCERR_MASK | ESTAT_PVIOL_MASK);   /* Clear existing error flags */  
  107.       
  108.       
  109.     (*(unsigned int *)0x0400) = 0xFFFF;               /* Write the data to the specified address */  
  110.   
  111.     ECMD = 0x41;                                      /* Store programming command in FCMD */  
  112.     ESTAT_CBEIF = 1;                                  /* Execute the command  */  
  113.   
  114.     if (ESTAT_ACCERR) {                               /* Check if there has been an access error */  
  115.         return (NVM_ACCESS_ERR);                      /* Return an Access Error code */  
  116.     }  
  117.       
  118.     if (ESTAT_PVIOL) {                                /* Check if there has been a protection error */  
  119.         return (NVM_PROTECTION_ERR);                  /* Return a Protection Error code */  
  120.     }  
  121.       
  122.     return (NVM_NO_ERR);                              /* Return No Error */  
  123. }  
  124.   
  125. /** @brief This function reads a 16-bit word from the specified address in EEPROM 
  126.  *  @param address, the start of the 16-bit data to read 
  127.  *  @return The 16-bit word stored in location 'address' 
  128.  */  
  129. unsigned int  EEPROM_Read_Word (unsigned int address)  
  130. {  
  131.     unsigned int  data;  
  132.       
  133.       
  134.     while (!ESTAT_CBEIF) {    /* Wait for EEPROM access controller to become ready */  
  135.         ;  
  136.     }  
  137.       
  138.     data = (*(unsigned int *)address); /* Read the data at location 'address'  */  
  139.     return (data);  /* Return the data*/  
  140. }  


 


 
  1. #include       /* common defines and macros */  
  2. #include "derivative.h"      /* derivative-specific definitions */  
  3. #include "sci.h"  
  4. #include "eets4k.h"  
  5.   
  6. void main(void)   
  7. {  
  8.     unsigned int data;  
  9.      EEPROM_Init(16384);  
  10.     EnableInterrupts;  
  11.       
  12.     EEPROM_Erase_All ();  
  13.     EEPROM_Write_Word(0x400, 1234);  
  14.     data = EEPROM_Read_Word(0x400);  
  15.     for(;;)   
  16.     {  
  17.         _FEED_COP(); /* feeds the dog */  
  18.     } /* loop forever */  
  19. }  

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
NTMD6N03R2G的技术参数