DSP

DM642对FLASH的读写

2019-07-13 19:37发布

说是原创其实也没多少是自己的东西,主要就是合众达的SEEDVPM642的例程,再加上一些程序说明,有本书《TMS320DM642 DSP应用系统设计与开发》(人民邮电出版社)里面写的很全,还包括使用CPLD对地址空间的拓展。 注意:FLASH在写之前必须先擦除FLASH上的内容。 1.     擦除FLASH 程序:   static Uint32 sector_end[SEEDDM642_FLASH_SECTORS] = {     SEEDDM642_FLASH_BASE + 0x00ffff,     SEEDDM642_FLASH_BASE + 0x01ffff,     SEEDDM642_FLASH_BASE + 0x02ffff,     SEEDDM642_FLASH_BASE + 0x03ffff,     SEEDDM642_FLASH_BASE + 0x04ffff,     SEEDDM642_FLASH_BASE + 0x05ffff,     SEEDDM642_FLASH_BASE + 0x06ffff,     SEEDDM642_FLASH_BASE + 0x07ffff  }; void SEEDDM642_FLASH_erase(Uint32 start, Uint32 length) {     Int16 i;     Uint8 *pdata;     Uint32 sector_base, end;             end = start + length - 1;             sector_base = SEEDDM642_FLASH_BASE;           for (i = 0; i < SEEDDM642_FLASH_SECTORS; i++)     {         if ((start <= sector_base) && (sector_end[i] <= end))          {                         *((Uint8 *)SEEDDM642_FLASH_BASE) = 0xaa;             *((Uint8 *)SEEDDM642_FLASH_BASE) = 0x55;             *((Uint8 *)SEEDDM642_FLASH_BASE) = 0x80;             *((Uint8 *)SEEDDM642_FLASH_BASE) = 0xaa;             *((Uint8 *)SEEDDM642_FLASH_BASE) = 0x55;                                     pdata = (Uint8 *)sector_base;             *pdata = 0x30;                                     while (1)             {                 if (*pdata & 0x80)                 {                     break;                 }              }                               *((Uint8 *)SEEDDM642_FLASH_BASE) = 0xf0;                            }                         sector_base = sector_end[i] + 1;     } }   程序说明: 1)       首先是擦除FLASH必须是擦整个扇区,这也就是为什么需要判断该页是否要擦除:if ((start <= sector_base) && (sector_end[i] <= end)) 2)       擦除FLASH时先向整个FLASH的首地址(SEEDDM642_FLASH_BASE)写入0xaa0x550x800xaa0x55 3)       在向被擦除的扇区的首地址(sector_base)写入0x30,在擦完后写入0xf0   2.     FLASH   void SEEDDM642_FLASH_read(Uint32 src, Uint32 dst, Uint32 length) {     Uint8 *psrc, *pdst;     Uint32 i;             psrc = (Uint8 *)src;     pdst = (Uint8 *)dst;     for (i = 0; i < length; i++)     {         *pdst++ = *psrc++;     } } 程序说明: 这段程序主要就是将FLASH中从src开始的length(字节)的内容读到dst开始的存储空间。 3.     FLASH   void SEEDDM642_FLASH_write(Uint32 src, Uint32 dst, Uint32 length) {     Uint8 *psrc, *pdst;     Uint32 i;           psrc = (Uint8 *)src;     pdst = (Uint8 *)dst;     for (i = 0; i < length; i++)     {         // Program one 8-bit word         *((Uint8 *)SEEDDM642_FLASH_BASE) = 0xaa;         *((Uint8 *)SEEDDM642_FLASH_BASE) = 0x55;         *((Uint8 *)SEEDDM642_FLASH_BASE) = 0xa0;         *pdst = *psrc;                 // Wait for operation to complete         while(1)         {             if (*pdst == *psrc)             {                 break;             }         }               pdst++;         psrc++;     }             *((Uint16 *)SEEDDM642_FLASH_BASE) = 0xf0;    } 程序说明: FLASH时先向整个FLASH的首地址(SEEDDM642_FLASH_BASE)写入0xaa0x550xa0