[MINI51] mini51 SPI FLASH调试不成功,求助

[复制链接]
4573|6
 楼主| cnjxlxp 发表于 2012-2-5 22:42 | 显示全部楼层 |阅读模式
本帖最后由 hotpower 于 2012-9-22 12:50 编辑

调试了几天都没有调试成功,写入和读出的数据不一致
请各位大侠帮分析一下原因,谢谢

  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/
  6. #include "includes.h"  //°üº¬ËùÐèµÄÍ·Îļþ
  7. uint32_t i2cdata = 0,u32TxData,u32RxData;
  8. uint8_t count;
  9. uint8_t flag;
  10. uint8_t u32write, u32read, u32PageNumber;
  11. uint8_t DataBuffer0[256];
  12. #define TEST_NUMBER 1  /* ¶¨Òå²âÊÔÒ³Êý    */
  13. #define  Enable_SPI_CS  DrvGPIO_ClrBit(E_PORT3,5)
  14. #define  DISABLE_SPI_CS  DrvGPIO_SetBit(E_PORT3,5)
  15. void SpiFlash_ChipErase(void)
  16. {
  17.     uint32_t au32SourceData;
  18.     /* ÉèÖÃSPI³¤¶ÈΪ8¸öλԪ    */
  19.     DrvSPI_SetBitLength(8);
  20.     /* Ƭѡ½ÅÉèΪÆô¶¯×´Ì¬   */
  21.    Enable_SPI_CS; //DrvSPI_SetSS();

  22.     /* ´«ËÍ0x06ÃüÁî, Write enable    */
  23.     au32SourceData = 0x06;
  24.     DrvSPI_SingleWrite(&au32SourceData);
  25.     /* µÈ´ý´«ËͽáÊø    */
  26.     while (DrvSPI_IsBusy());
  27.     /* Ƭѡ½ÅÉèΪ·ÇÆô¶¯×´Ì¬   */
  28.    DISABLE_SPI_CS; //DrvSPI_ClrSS();
  29.     /* Ƭѡ½ÅÉèΪÆô¶¯×´Ì¬   */
  30. Enable_SPI_CS; //DrvSPI_SetSS();

  31.     /* ´«ËÍ0xC7ÃüÁî, Chip Erase    */   
  32.     au32SourceData = 0xc7;
  33.     DrvSPI_SingleWrite(&au32SourceData);
  34.     /* µÈ´ý´«ËͽáÊø    */
  35.     while (DrvSPI_IsBusy());
  36.     /* Ƭѡ½ÅÉèΪ·ÇÆô¶¯×´Ì¬   */
  37.    DISABLE_SPI_CS; //DrvSPI_ClrSS();
  38. }
  39. uint32_t SpiFlash_ReadStatusReg1(void)
  40. {
  41.     uint32_t au32SourceData;
  42.     uint32_t au32DestinationData;
  43.     /* ÉèÖÃSPI³¤¶ÈΪ16¸öλԪ    */
  44.     DrvSPI_SetBitLength(16);
  45.     /* Ƭѡ½ÅÉèΪÆô¶¯×´Ì¬   */
  46.     Enable_SPI_CS;//DrvSPI_SetSS();

  47.     /* ´«ËÍ0x05ÃüÁî, Read status register 1    */   
  48.     au32SourceData = 0x05;
  49.     DrvSPI_SingleWrite(&au32SourceData);
  50.     /* µÈ´ý´«ËͽáÊø    */
  51.     while (DrvSPI_IsBusy());
  52.     /* Ƭѡ½ÅÉèΪ·ÇÆô¶¯×´Ì¬   */
  53.     DISABLE_SPI_CS;//DrvSPI_ClrSS();
  54. /* ´ÓRx¼Ä´æÆ÷ÄÚ¶ÁÈ¡×ÊÁÏ    */
  55.     DrvSPI_DumpRxRegister(&au32DestinationData, 1);
  56.     return (au32DestinationData & 0xFF);
  57. }
  58. void SpiFlash_WaitReady(void)
  59. {
  60.     uint32_t ReturnValue;
  61.     do
  62.     {
  63.         ReturnValue = SpiFlash_ReadStatusReg1();
  64.         ReturnValue = ReturnValue & 1;
  65.     }
  66.     while (ReturnValue != 0); // check the BUSY bit
  67. }
  68. void SpiFlash_PageProgram(uint8_t *DataBuffer, uint32_t StartAddress, uint32_t ByteCount)
  69. {
  70.     uint32_t au32SourceData;
  71.     uint32_t Counter;
  72.     /* ÉèÖÃSPI³¤¶ÈΪ8¸öλԪ    */
  73.     DrvSPI_SetBitLength(8);
  74.     /* Ƭѡ½ÅÉèΪÆô¶¯×´Ì¬   */
  75.     DrvSPI_SetSS();
  76.     /* ´«ËÍ0x06ÃüÁî, Write enable    */
  77.     au32SourceData = 0x06;
  78.     DrvSPI_SingleWrite(&au32SourceData);
  79.     /* µÈ´ý´«ËͽáÊø    */
  80.     while (DrvSPI_IsBusy());
  81.     /* Ƭѡ½ÅÉèΪ·ÇÆô¶¯×´Ì¬   */
  82.     DISABLE_SPI_CS;//DrvSPI_ClrSS();
  83.     /* Ƭѡ½ÅÉèΪÆô¶¯×´Ì¬   */
  84.    Enable_SPI_CS; //DrvSPI_SetSS();
  85.     /* ´«ËÍ0x02ÃüÁî, Page program    */   
  86.     au32SourceData = 0x02;
  87.     DrvSPI_SingleWrite(&au32SourceData);
  88.     // wait
  89.     while (DrvSPI_IsBusy());
  90.     // configure transaction length as 24 bits
  91.     DrvSPI_SetBitLength(24);
  92.     // send 24-bit start address
  93.     au32SourceData = StartAddress;
  94.     DrvSPI_SingleWrite(&au32SourceData);
  95.     /* µÈ´ý´«ËͽáÊø    */
  96.     while (DrvSPI_IsBusy());
  97.     /* ÉèÖÃSPI³¤¶ÈΪ8¸öλԪ    */
  98.     DrvSPI_SetBitLength(8);
  99.     for (Counter = 0; Counter < ByteCount; Counter++)
  100.     {
  101.         /* ´«ËÍ×ÊÁÏ    */
  102.         au32SourceData = DataBuffer[Counter];
  103.         DrvSPI_SingleWrite(&au32SourceData);
  104.      /* µÈ´ý´«ËͽáÊø    */
  105.         while (DrvSPI_IsBusy());
  106.     }
  107.     /* Ƭѡ½ÅÉèΪ·ÇÆô¶¯×´Ì¬   */
  108.     DISABLE_SPI_CS;//DrvSPI_ClrSS();
  109. }
  110. void SpiFlash_ReadData(uint8_t *DataBuffer0, uint32_t StartAddress, uint32_t ByteCount)
  111. {
  112.     uint32_t au32SourceData;
  113.     uint32_t au32DestinationData;
  114.     uint32_t Counter;
  115.     /* ÉèÖÃSPI³¤¶ÈΪ8¸öλԪ    */
  116.     DrvSPI_SetBitLength(8);
  117.     /* Ƭѡ½ÅÉèΪÆô¶¯×´Ì¬   */
  118.     Enable_SPI_CS;//DrvSPI_SetSS();

  119.     /* ´«ËÍ0x03ÃüÁî, Read data    */     
  120.     au32SourceData = 0x03;
  121.     DrvSPI_SingleWrite(&au32SourceData);
  122.     /* µÈ´ý´«ËͽáÊø    */
  123.     while (DrvSPI_IsBusy());
  124.     /* ÉèÖÃSPI³¤¶ÈΪ24¸öλԪ    */
  125.     DrvSPI_SetBitLength(24);

  126.     /* ´«ËÍ24λԪλַ×ÊÁÏ    */
  127.     au32SourceData = StartAddress;
  128.     DrvSPI_SingleWrite(&au32SourceData);
  129.     /* µÈ´ý´«ËͽáÊø    */
  130.     while (DrvSPI_IsBusy());
  131.     /* ÉèÖÃSPI³¤¶ÈΪ8¸öλԪ    */
  132.     DrvSPI_SetBitLength(8);
  133.     for (Counter = 0; Counter < ByteCount; Counter++)
  134.     {
  135.         /* ¶ÁÈ¡×ÊÁÏ    */
  136.         au32SourceData = 0x0;
  137.         DrvSPI_SingleWrite(&au32SourceData);
  138.      /* µÈ´ý´«ËͽáÊø    */
  139.         while (DrvSPI_IsBusy());
  140.   /* ´ÓRx¼Ä´æÆ÷ÄÚ¶ÁÈ¡×ÊÁÏ    */
  141.         DrvSPI_DumpRxRegister(&au32DestinationData, 1);
  142.         DataBuffer0[Counter] = (uint8_t) au32DestinationData;
  143.     }
  144.     /* Ƭѡ½ÅÉèΪ·ÇÆô¶¯×´Ì¬   */
  145.    DISABLE_SPI_CS;// DrvSPI_ClrSS();
  146. }

  147. /*************************************************************************************
  148. ** Function name:       main
  149. ** Descriptions:                 
  150. ** input parameters:    ÎÞ
  151. ** output parameters:   ÎÞ
  152. ** Returned value:      ÎÞ
  153. *************************************************************************************/
  154. int main (void)
  155. {
  156.       
  157.     uint8_t  WriteBuffer[256] = {0};
  158.       uint16_t i, id = 1;
  159.    
  160.   uint8_t  ReadBuffer[256] = {0};
  161.   //ReadBuffer[0]=0xfe;
  162.   Set_System();
  163.   ReadBuffer[0]=8;      //µ÷ÓÃϵͳ³õʼ»¯º¯Êý
  164.      u32write=0X90;
  165. /* ÉèÖÃSPI I/O    */
  166.     DrvGPIO_InitFunction(FUNC_SPI);
  167.     /* ÉèÖÃSPIΪÖ÷ģʽ, 32-bit ´«Êä    */
  168.     DrvSPI_Open(eDRVSPI_MASTER, eDRVSPI_TYPE1, 32);
  169.    
  170.     /* ÅäÖô«ÊäʱµÄ±ÈÌØÓÅÏÈ´ÎÐòΪMSBÓÅÏÈ     */
  171.     DrvSPI_SetEndian(eDRVSPI_MSB_FIRST);
  172.    
  173.     /* ¹Ø±Õ×Ô¶¯Æ¬Ñ¡¹¦ÄÜ    */
  174.     DrvSPI_DisableAutoSS();
  175.    
  176.     /* É趨ƬѡÐÅºÅÆô¶¯µçƽΪƬѡÐÅºÅµÍµçÆ½»òÕßϽµÑØÆô¶¯    */
  177.     DrvSPI_SetSlaveSelectActiveLevel(eDRVSPI_ACTIVE_LOW_FALLING);
  178.    
  179.     /* ÉèÖÃSPIʱÖÓËÙ¶ÈΪ 12MHz    */
  180.     DrvSPI_SetClockFreq(1000000, 0);
  181. DISABLE_SPI_CS;
  182.   /* ²Á³ýSPI flash    */
  183.     //SpiFlash_ChipErase();   
  184.     /* µÈ´ý²Á³ý½áÊø    */
  185.     SpiFlash_WaitReady();
  186.    
  187. SpiFlash_ReadData(ReadBuffer,0,256);
  188. DrvGPIO_ClrBit(E_PORT3, 1);  //LED0-ON  Erase OK
  189. // delay_ms(1000);
  190.   /*      SpiFlash_PageProgram(&u32write,0, 1);
  191.         SpiFlash_WaitReady();
  192. DrvGPIO_ClrBit(E_PORT3, 6);  //LED1-ON  //Program done
  193. delay_ms(1000);
  194.         SpiFlash_ReadData(&u32read,0, 1);
  195.             if (u32write !=u32read)
  196.             {
  197.                DrvGPIO_ClrBit(E_PORT2, 6);  //LED3-ON Verify Error
  198.       //delay_ms(1000);
  199.                 //while (1);
  200.             }
  201. DrvGPIO_ClrBit(E_PORT5, 2);  //LED2-ON Verify OK
  202. delay_ms(1000);
  203. DrvSPI_Close();
  204.   //delay_ms(200);*/
  205. for(i=0;i<256;i++)     //³õʼ»¯Êý×é
  206.   {
  207.    WriteBuffer = i;
  208.   }
  209.   SpiFlash_WaitReady();      //W25Q16BVæ״̬¼ì²é
  210. // SPI_SectorErase(0x0000);     //ÉÈÇø²Á³ý
  211.   SpiFlash_WaitReady();      //W25Q16BVæ״̬¼ì²é
  212.   SpiFlash_PageProgram(WriteBuffer,0,256);  //W25Q16BV°´Ò³±à³Ìº¯Êý
  213.   SpiFlash_WaitReady();
  214.   //delay_ms(1000);      //W25Q16BVæ״̬¼ì²é
  215.   SpiFlash_ReadData(ReadBuffer,0,256);  //W25Q16BV¶ÁÊý¾Ýº¯Êý
  216.   //SpiFlash_WaitReady();
  217.   //delay_ms(1000);
  218.   for(i=0;i<256;i++)     //½«¶Á³öÀ´µÄÊý¾ÝÓëд½øÈ¥µÄÊý¾Ý½øÐбȽÏ
  219.   {
  220.     if(WriteBuffer != ReadBuffer)
  221. {
  222.     DrvGPIO_ClrBit(E_PORT5, 2);  //LED2-ON Verify not OK
  223. }
  224. }   
  225.   while(1);  
  226.    
  227. }

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
 楼主| cnjxlxp 发表于 2012-2-6 21:57 | 显示全部楼层
问题已经解决,详情请看另一帖https://bbs.21ic.com/icview-305182-1-1.html
Ryanhsiung 发表于 2012-2-7 10:26 | 显示全部楼层
恭喜LZ
lixiaoxu2meng 发表于 2012-2-7 10:33 | 显示全部楼层
自己解决的印象深理解的透
缥缈九哥 发表于 2012-2-8 12:01 | 显示全部楼层
我上面的帖子有例程。
 楼主| cnjxlxp 发表于 2012-2-8 20:38 | 显示全部楼层
九哥说的是这个?这个程序测试不完整,只是测试了SPI读ID,而且ID也是和我的mini51对不上,我的mini51的ID是0xEF16
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <Nuvoton\iomini51.h>
  4. #include "mini51.h"
  5. #include "Driver/DrvGPIO.h"
  6. #include "Driver/DrvSYS.h"
  7. #include "Driver/DrvSPI.h"

  8. int w25x16_test(void);
  9. void SPI_init(void);
  10. // **************************************
  11. // For W25Q16BV, Manufacturer ID: 0xEF; Device ID: 0x14
  12. // For W26X16, Manufacturer ID: 0xEF; Device ID: 0x14
  13. uint32_t SpiFlash_ReadMidDid(void)
  14. {
  15.     uint32_t au32SourceData;
  16.     uint32_t au32DestinationData;

  17.     // configure transaction length as 8 bits
  18.     DrvSPI_SetBitLength(8);

  19.     // /CS: active
  20.     DrvSPI_SetSS();

  21.     // send Command: 0x90, Read Manufacturer/Device ID
  22.     au32SourceData = 0x90;
  23.     DrvSPI_SingleWrite(&au32SourceData);

  24.     // wait
  25.     while (DrvSPI_IsBusy());

  26.     // configure transaction length as 24 bits
  27.     DrvSPI_SetBitLength(24);

  28.     // send 24-bit '0', dummy
  29.     au32SourceData = 0x0;
  30.     DrvSPI_SingleWrite(&au32SourceData);

  31.     // wait
  32.     while (DrvSPI_IsBusy());

  33.     // configure transaction length as 16 bits
  34.     DrvSPI_SetBitLength(16);

  35.     // receive
  36.     au32SourceData = 0x0;
  37.     DrvSPI_SingleWrite(&au32SourceData);

  38.     // wait
  39.     while (DrvSPI_IsBusy());

  40.     // /CS: de-active
  41.     DrvSPI_ClrSS();

  42.     DrvSPI_DumpRxRegister(&au32DestinationData, 1);

  43.     if ((au32DestinationData & 0xffff) == 0xEF14)
  44.         printf("MID & DID=0xEF14\n");
  45.     else
  46.         printf("MID & DID Error!\n");
  47.    
  48.     return  (au32DestinationData & 0xffff);

  49. }
  50. void SPI_init(void)
  51. {
  52.     DrvGPIO_InitFunction(FUNC_SPI);

  53.     /*Initialize SPI*/
  54.     DrvSPI_Open(eDRVSPI_MASTER, eDRVSPI_TYPE1, 32);
  55.     /* MSB first */
  56.     DrvSPI_SetEndian(eDRVSPI_MSB_FIRST);
  57.     /* Disable the automatic slave select function of SS0. */
  58.     DrvSPI_DisableAutoSS();
  59.     /* Set the active level of slave select. */
  60.     DrvSPI_SetSlaveSelectActiveLevel(eDRVSPI_ACTIVE_LOW_FALLING);
  61.     /* SPI clock rate 12MHz */
  62.     DrvSPI_SetClockFreq(12000000, 0);
  63. }
  64. int w25x16_test(void)
  65. {   
  66.     int ret;
  67.     if (SpiFlash_ReadMidDid() != 0xEF14)  /*Get flash ID*/
  68.     {
  69.         printf("SPI ID fail!!\n");
  70.         ret = 0;
  71.     }
  72.     else
  73.     {
  74.         printf("SPI ID OK \n");
  75.         ret = 1;
  76.     }
  77.    
  78. //    SPItest();

  79.     DrvSPI_Close();
  80.    
  81.     return ret;
  82. }
缥缈九哥 发表于 2012-2-10 11:05 | 显示全部楼层
能读ID,基本没有难度了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

46

帖子

2

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