STM8S写入Flash为什么超过64K就写不进去?

[复制链接]
5204|4
 楼主| sed2003 发表于 2012-11-14 10:55 | 显示全部楼层 |阅读模式
我用RAM来写Flash,结果发现,一旦超过64K(10000H),就无法写入了。
而在64K以内,却可以写。这是怎么回事啊?
/* Copy data bytes from RAM to FLASH memory */
    for (Count = 0; Count < BLOCK_SIZE; Count++)
    {
        *((@far u16*)0x10000 + Count) = ((u8)(Buffer[Count]));
    }
IJK 发表于 2012-11-14 11:17 | 显示全部楼层
看看生成的汇编代码 是否正确。
 楼主| sed2003 发表于 2012-11-14 12:01 | 显示全部楼层
本帖最后由 sed2003 于 2012-11-14 12:10 编辑

就是这个,但看不明白。
L513:
3608                     ; 128         *((@far u8*)0x10000 + Count) = ((u8 Buffer[Count])); // 这里
3610  0030 1e0d           ldw x,(OFST+9,sp)
3611  0032 72fb03         addw x,(OFST-1,sp)
3612  0035 f6             ld a,(x)
3613  0036 1e03           ldw x,(OFST-1,sp)
3614  0038 a7010000       ldf (65536,x),a
3615                     ; 126     for (Count = 0; Count < BLOCK_SIZE; Count++)
3617  003c 1e03           ldw x,(OFST-1,sp)
3618  003e 1c0001         addw x,#1
3619  0041 1f03           ldw (OFST-1,sp),x
3622  0043 1e03           ldw x,(OFST-1,sp)
3623  0045 a30080         cpw x,#128
3624  0048 25e6           jrult L513
IJK 发表于 2012-11-14 15:17 | 显示全部楼层
汇编代码似乎是对的。前3行是把数读到 a寄存器里,后2行把 a寄存器里的数 写入64K(10000H)后的目标地址

3608                     ; 128         *((@far u8*)0x10000 + Count) = ((u8 Buffer[Count])); // 这里
3610  0030 1e0d           ldw x,(OFST+9,sp)
3611  0032 72fb03         addw x,(OFST-1,sp)
3612  0035 f6             ld a,(x)
3613  0036 1e03           ldw x,(OFST-1,sp)
3614  0038 a7010000       ldf (65536,x),a
 楼主| sed2003 发表于 2012-11-14 18:15 | 显示全部楼层
本帖最后由 sed2003 于 2012-11-14 18:19 编辑

IJK,我发一份我的代码给你看看怎么样,请你帮我解疑一下。
我实在搞不明白到底是哪里错了。

  1. int main(void)
  2. {
  3.         SysInit();

  4.         while (1)
  5.         {
  6.                 //ClearWatchdog;
  7.                 ClockRun( );
  8.                 AccProc( );        

  9.                 if(bUpdateMCU)
  10.                 {
  11.                         bUpdateMCU = 0;        
  12.                         disableInterrupts();               
  13.                         //protocol_init();                        
  14.                         unlock_PROG();
  15.                         unlock_DATA();      
  16.                         _fctcpy('U');                     
  17.                         MCUUpdateCodeProc();
  18.                 }        
  19.         }
  20. }
然后是写FLASH的所有代码:
  1. #include "sys.h"


  2. #pragma section (UPDATEFLASH)

  3. static u8 DataBuffer[DatBufSize]={0}; // 55 aa len GR MI PAR1 128PAR2 CS
  4. static u8 *ReceivedData;
  5. static u8 data[10];
  6. static u8 FlashBuf[128];
  7. static u8 FAR* FlhDataAddress;
  8. static u8 FlhDataCount;
  9. static U16 blockcnt = 0;
  10. //static u8 RdData[10]={0};

  11. void  UpdateUartTx(uchar dat)
  12. {
  13.         u8 sr;

  14.         //wait for Tx empty
  15.         sr = UART1->SR;
  16.         while(!(sr & 0x80/*TxNE*/)) sr = UART1->SR;
  17.         //send data0
  18.         UART1->DR = dat;
  19.         //wait for transmission complete
  20.         sr = UART1->SR;
  21.         while(!(sr & 0x40/*TxNE*/)) sr = UART1->SR;
  22. }

  23. void Transmit(uchar *p,U8 len)
  24. {
  25.           uchar i,OutStr[15],CheckSum=0;        
  26.     OutStr[0]=0x55;
  27.           OutStr[1]=0xAA;
  28.           OutStr[2]=len+1;            

  29.         for(i=3;i<len+3;i++)
  30.     {        
  31.               OutStr = *p;
  32.                 CheckSum += *p;
  33.                 p++;
  34.     }
  35.         CheckSum = CheckSum ^ 0xFF;
  36.         OutStr[len+3] = CheckSum;

  37.         for(i=0;i<len+4;i++)
  38.     {        
  39.        UpdateUartTx(OutStr);        // 发送数据        
  40.     }
  41. }


  42. void Receive(u8* ReceivedData)
  43. {
  44.         u8 sr;                // working copy of SPI_SR register

  45.         //wait for Rx full
  46.         sr = UART1->SR;
  47.         while(!(sr & 0x20 /*RXNE*/)) sr = UART1->SR ;        
  48.         //receive data
  49.         *ReceivedData = UART1->DR;
  50. }//Receive


  51. void Mem_ProgramBlock(u16 BlockNum, FLASH_MemType_TypeDef MemType, u8 *Buffer)
  52. {
  53.     u16 Count = 0;
  54.     u32 StartAddress = 0;
  55.     u16 timeout = (u16)0x6000;

  56.         /* Set Start address wich refers to mem_type */
  57.     if (MemType == FLASH_MEMTYPE_PROG)
  58.     {
  59.         StartAddress = FLASH_START;
  60.     }
  61.     else
  62.     {
  63.         StartAddress = EEPROM_START;
  64.     }

  65.     /* Point to the first block address */
  66.     StartAddress = StartAddress + ((u32)BlockNum * BLOCK_SIZE);

  67.     /* Standard programming mode */
  68.     FLASH->CR2 |= (u8)0x01;
  69.     FLASH->NCR2 &= (u8)~0x01;
  70.    
  71.     /* Copy data bytes from RAM to FLASH memory */
  72.     for (Count = 0; Count < BLOCK_SIZE; Count++)
  73.     {
  74.         *((@far u8*)StartAddress + Count) = ((u8)(Buffer[Count]));
  75.     }

  76.     if (MemType == FLASH_MEMTYPE_DATA)
  77.     {
  78.         /* Waiting until High voltage flag is cleared*/
  79.         while ((FLASH->IAPSR & 0x40) != 0x00 || (timeout == 0x00))
  80.         {
  81.             timeout--;
  82.         }
  83.     }
  84. }


  85. u8 WriteBufferFlash(u8 FAR* DataAddress, u8 * DataPointer, u8 DataCount)
  86. {
  87.   u32 Address = (u32) DataAddress;
  88. //  u8 * DataPointer = FlashBuf;
  89.   u32 Offset;
  90.   FLASH_MemType_TypeDef MemType;
  91.   //set offset according memory type
  92. //  if(MemType == FLASH_MEMTYPE_PROG)
  93. //    Offset = FLASH_START;
  94. //  else
  95. //    Offset = EEPROM_START;
  96.   Offset = FLASH_START;
  97.   MemType = FLASH_MEMTYPE_PROG;
  98. #if 0
  99.   //program beginning bytes before words
  100.   while((Address % 4) && (DataCount))
  101.   {
  102.     *((PointerAttr u8*) Address) = *DataPointer;
  103.         while( (FLASH->IAPSR & (FLASH_IAPSR_EOP | FLASH_IAPSR_WR_PG_DIS)) == 0);
  104.                 Address++;
  105.     DataPointer++;
  106.     DataCount--;
  107.   }
  108.   //program beginning words before blocks
  109.   while((Address % BLOCK_BYTES) && (DataCount >= 4))
  110.   {
  111.                 FLASH->CR2 |= (u8)0x40;
  112.                 FLASH->NCR2 &= (u8)~0x40;
  113.                 *((PointerAttr u8*)Address) = (u8)*DataPointer  ;          /* Write one byte - from lowest address*/
  114.                 *((PointerAttr u8*)(Address + 1)) = *(DataPointer + 1); /* Write one byte*/
  115.                 *((PointerAttr u8*)(Address + 2)) = *(DataPointer + 2); /* Write one byte*/
  116.                 *((PointerAttr u8*)(Address + 3)) = *(DataPointer + 3); /* Write one byte - from higher address*/
  117.     while( (FLASH->IAPSR & (FLASH_IAPSR_EOP | FLASH_IAPSR_WR_PG_DIS)) == 0);
  118.                 Address    += 4;
  119.     DataPointer+= 4;
  120.     DataCount  -= 4;
  121.   }
  122. #endif  
  123.   //program blocks
  124.   while(DataCount >= BLOCK_BYTES)
  125.   {
  126.     Mem_ProgramBlock((Address - Offset)/BLOCK_BYTES, MemType, DataPointer);
  127.    // Address    += BLOCK_BYTES;
  128.    // DataPointer+= BLOCK_BYTES;   
  129.     DataCount  -= BLOCK_BYTES;
  130.   }
  131. #if 0
  132.   //program remaining words (after blocks)
  133.   while(DataCount >= 4)
  134.   {
  135.                 FLASH->CR2 |= (u8)0x40;
  136.                 FLASH->NCR2 &= (u8)~0x40;
  137.                 *((PointerAttr u8*)Address) = (u8)*DataPointer  ;                  /* Write one byte - from lowest address*/
  138.                 *((PointerAttr u8*)(Address + 1)) = *(DataPointer + 1); /* Write one byte*/
  139.                 *((PointerAttr u8*)(Address + 2)) = *(DataPointer + 2); /* Write one byte*/
  140.                 *((PointerAttr u8*)(Address + 3)) = *(DataPointer + 3); /* Write one byte - from higher address*/
  141.             while( (FLASH->IAPSR & (FLASH_IAPSR_EOP | FLASH_IAPSR_WR_PG_DIS)) == 0);
  142.                 Address    += 4;
  143.             DataPointer+= 4;
  144.             DataCount  -= 4;
  145.   }

  146.   //program remaining bytes (after words)
  147.   while(DataCount)
  148.   {
  149.     *((PointerAttr u8*) Address) = *DataPointer;
  150.     while( (FLASH->IAPSR & (FLASH_IAPSR_EOP | FLASH_IAPSR_WR_PG_DIS)) == 0);
  151.                 Address++;
  152.     DataPointer++;
  153.     DataCount--;
  154.   }
  155. #endif   
  156.   return 1;
  157. }//WriteBufferFlash


  158. u8 WriteBuffer(u8 FAR* DataAddress, u8 * DataPointer, u8 DataCount)
  159. {
  160.   u8 i;
  161.   
  162.   //for Flash
  163.   if (((u32)DataAddress >= FLASH_START) && (((u32)DataAddress + DataCount - 1) <= FLASH_END))
  164.     return WriteBufferFlash(DataAddress, DataPointer, DataCount);
  165. #if 0   
  166.   //for EEPROM
  167.   if (((u32)DataAddress >= EEPROM_START) && (((u32)DataAddress + DataCount - 1) <= EEPROM_END))
  168.     return WriteBufferFlash(DataAddress, DataCount, FLASH_MEMTYPE_DATA);
  169.    
  170.   //for RAM
  171.   if (((u32)DataAddress >= RAM_START) && (((u32)DataAddress + DataCount - 1) <= RAM_END))
  172.   {
  173.     for(i=0; i<DataCount; i++)
  174.       DataAddress = DataBuffer;
  175.     return 1;
  176.   }
  177.   
  178.   //for Option bytes
  179.   if (((u32)DataAddress >= OPTION_START) && (((u32)DataAddress + DataCount - 1) <= OPTION_END))
  180.   {
  181.     for(i=0; i<DataCount; i++)
  182.     {
  183.        FLASH_ProgramOptionByte((u32)(&DataAddress), DataBuffer);
  184.     }
  185.     return 1;
  186.   }
  187. #endif
  188.   //otherwise fail
  189.   return 0;
  190. }//WriteBuffer

  191. u8 FLASH_ReadByteData(uint32_t Address)
  192. {   
  193.     /* Read byte */
  194.     return(*(FAR uint8_t *) (uint16_t)Address);

  195. }


  196. void MCUUpdateCodeProc(void)
  197. {
  198.         u8 sr,*RevData;                // working copy of SPI_SR register
  199.         u8 cnt;
  200.         u8 i,cs,WrFlashSt;

  201.         
  202. #if 1
  203.         data[0] = _GR_Menu;
  204.         data[1] = _MI_Update_MCU;
  205.         Transmit(data,2);        
  206.                         
  207.         //FlashDataAddr = (u32*)FLASH_START;
  208.         FlhDataAddress = (u8 FAR*)FLASH_START;
  209.         FlhDataCount = BLOCK_SIZE;
  210.         while(1)
  211.         {
  212. /************* get uart data *************/               
  213.                 RevData = &DataBuffer[0];
  214.                 cnt = 0;
  215.                 while(cnt<135)
  216.                 {
  217.                         sr = UART1->SR;
  218.                         while(!(sr & 0x20 /*RXNE*/))
  219.                         {
  220.                                 sr = UART1->SR ;
  221.                         }
  222.                         *RevData = UART1->DR;
  223.                         RevData++;
  224.                         cnt++;
  225.                 }
  226. /************* analyse data *************/               
  227.                 cs = 0;
  228.                 for(i=3;i<134;i++)
  229.             {   
  230.                         cs += DataBuffer;
  231.             }
  232.                 cs = cs ^ 0xFF;
  233.                
  234.                 if(DataBuffer[0]!=0x55 || DataBuffer[1]!=0xAA || DataBuffer[2]!=132 || DataBuffer[134]!=cs)
  235.                 {
  236.                         //send err ack to host
  237.                         data[0] = _GR_Menu;
  238.                         data[1] = _Update_Code;
  239.                         data[2] = _Fail;
  240.                         Transmit(data,3);                        
  241.                 }
  242.                 else
  243.                 {
  244.                 #if 1
  245. /************* write into flash rom *************/               
  246.                         for(i=0;i<128;i++)
  247.                         {
  248.                                 FlashBuf = DataBuffer[6+i];
  249.                         }               
  250.                         WriteBuffer(FlhDataAddress,FlashBuf,FlhDataCount);        
  251.                 #endif
  252.                         if(blockcnt==256)        while(1);        //41
  253.                         if(DataBuffer[5] == 0x00)
  254.                         {// last frame data
  255.                                 /* Lock program memory */
  256.                             //FLASH->IAPSR = ~0x02;
  257.                             /* Lock data memory */
  258.                             //FLASH->IAPSR = ~0x08;
  259.                                 data[0] = _GR_Menu;
  260.                                 data[1] = _Update_Code;
  261.                                 data[2] = _Success;
  262.                                 Transmit(data,3);
  263.                         #if 1
  264.                                 //reset stack pointer (lower byte - because compiler decreases SP with some bytes)                                 
  265.                                 _asm("LDW X,  SP ");
  266.                                 _asm("LD  A,  $FF");
  267.                                 _asm("LD  XL, A  ");
  268.                                 _asm("LDW SP, X  ");
  269.                                 _asm("JPF $8000");
  270.                         #endif                                
  271.                                 
  272.                         }
  273.                         else
  274.                         {// next frame data
  275.                                 FlhDataAddress += FlhDataCount;
  276.                                 blockcnt++;
  277.                                 data[0] = _GR_Menu;
  278.                                 data[1] = _Update_Code;
  279.                                 data[2] = _Success;
  280.                                 Transmit(data,3);
  281.                         }                  
  282.                 }               
  283.         }
  284. #endif        
  285. }        


  286. #pragma section ()


您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

34

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部