本帖最后由 hotpower 于 2012-9-22 12:50 编辑
调试了几天都没有调试成功,芯唐自带的例程也是有问题的。还道园地发帖求助也没有人回答。今天晚上调试通过了。是 uint32_t SpiFlash_ReadStatusReg1(void)函数有问题。
/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
#include "includes.h" //°üº¬ËùÐèµÄÍ·Îļþ
uint32_t i2cdata = 0,u32TxData,u32RxData;
uint8_t count;
uint8_t flag;
uint8_t u32write, u32read, u32PageNumber;
uint8_t DataBuffer0[256];
#define TEST_NUMBER 1 /* ¶¨Òå²âÊÔÒ³Êý */
//#define Enable_SPI_CS DrvGPIO_ClrBit(E_PORT3,5)
//#define DISABLE_SPI_CS DrvGPIO_SetBit(E_PORT3,5)
void SpiFlash_ChipErase(void)
{
uint32_t au32SourceData;
/* ÉèÖÃSPI³¤¶ÈΪ8¸öλԪ */
DrvSPI_SetBitLength(8);
/* Ƭѡ½ÅÉèΪÆô¶¯×´Ì¬ */
DrvSPI_SetSS();
/* ´«ËÍ0x06ÃüÁî, Write enable */
au32SourceData = 0x06;
DrvSPI_SingleWrite(&au32SourceData);
/* µÈ´ý´«ËͽáÊø */
while (DrvSPI_IsBusy());
/* Ƭѡ½ÅÉèΪ·ÇÆô¶¯×´Ì¬ */
DrvSPI_ClrSS();
/* Ƭѡ½ÅÉèΪÆô¶¯×´Ì¬ */
DrvSPI_SetSS();
/* ´«ËÍ0xC7ÃüÁî, Chip Erase */
au32SourceData = 0xc7;
DrvSPI_SingleWrite(&au32SourceData);
/* µÈ´ý´«ËͽáÊø */
while (DrvSPI_IsBusy());
/* Ƭѡ½ÅÉèΪ·ÇÆô¶¯×´Ì¬ */
DrvSPI_ClrSS();
}
/*uint32_t SpiFlash_ReadStatusReg1(void)
{
uint32_t au32SourceData;
uint32_t au32DestinationData;
//
DrvSPI_SetBitLength(16);
DrvSPI_SetSS();
au32SourceData = 0x05;
DrvSPI_SingleWrite(&au32SourceData);
while (DrvSPI_IsBusy());
DrvSPI_ClrSS();
DrvSPI_DumpRxRegister(&au32DestinationData, 1);
return (au32DestinationData & 0xFF);
} */
uint32_t SpiFlash_ReadStatusReg1(void)
{
uint32_t au32SourceData;
uint32_t au32DestinationData;
/* ÉèÖÃSPI³¤¶ÈΪ8¸öλԪ */
DrvSPI_SetBitLength(8);
/* Ƭѡ½ÅÉèΪÆô¶¯×´Ì¬ */
DrvSPI_SetSS();
/* ´«ËÍ0x05ÃüÁî, Read status register 1 */
au32SourceData = 0x05;
DrvSPI_SingleWrite(&au32SourceData);
/* µÈ´ý´«ËͽáÊø */
while (DrvSPI_IsBusy());
/* ¿ªÊ¼½ÓÊÕ×ÊÁÏ */
au32SourceData = 0x0;
DrvSPI_SingleWrite(&au32SourceData);
/* µÈ´ý´«ËͽáÊø */
while (DrvSPI_IsBusy());
/* Ƭѡ½ÅÉèΪ·ÇÆô¶¯×´Ì¬ */
DrvSPI_ClrSS();
/* ´ÓRx¼Ä´æÆ÷ÄÚ¶ÁÈ¡×ÊÁÏ */
DrvSPI_DumpRxRegister(&au32DestinationData, 1);
return (au32DestinationData & 0xFF);
}
void SpiFlash_WaitReady(void)
{
uint32_t ReturnValue;
do
{
ReturnValue = SpiFlash_ReadStatusReg1();
ReturnValue = ReturnValue & 1;
}
while (ReturnValue != 0); // check the BUSY bit
}
void SpiFlash_PageProgram(uint8_t *DataBuffer, uint32_t StartAddress, uint32_t ByteCount)
{
uint32_t au32SourceData;
uint32_t Counter;
/* ÉèÖÃSPI³¤¶ÈΪ8¸öλԪ */
DrvSPI_SetBitLength(8);
/* Ƭѡ½ÅÉèΪÆô¶¯×´Ì¬ */
DrvSPI_SetSS();
/* ´«ËÍ0x06ÃüÁî, Write enable */
au32SourceData = 0x06;
DrvSPI_SingleWrite(&au32SourceData);
/* µÈ´ý´«ËͽáÊø */
while (DrvSPI_IsBusy());
/* Ƭѡ½ÅÉèΪ·ÇÆô¶¯×´Ì¬ */
DrvSPI_ClrSS();
/* Ƭѡ½ÅÉèΪÆô¶¯×´Ì¬ */
DrvSPI_SetSS();
/* ´«ËÍ0x02ÃüÁî, Page program */
au32SourceData = 0x02;
DrvSPI_SingleWrite(&au32SourceData);
// wait
while (DrvSPI_IsBusy());
// configure transaction length as 24 bits
DrvSPI_SetBitLength(24);
// send 24-bit start address
au32SourceData = StartAddress;
DrvSPI_SingleWrite(&au32SourceData);
/* µÈ´ý´«ËͽáÊø */
while (DrvSPI_IsBusy());
/* ÉèÖÃSPI³¤¶ÈΪ8¸öλԪ */
DrvSPI_SetBitLength(8);
for (Counter = 0; Counter < ByteCount; Counter++)
{
/* ´«ËÍ×ÊÁÏ */
au32SourceData = DataBuffer[Counter];
DrvSPI_SingleWrite(&au32SourceData);
/* µÈ´ý´«ËͽáÊø */
while (DrvSPI_IsBusy());
}
/* Ƭѡ½ÅÉèΪ·ÇÆô¶¯×´Ì¬ */
DrvSPI_ClrSS();
}
void SpiFlash_ReadData(uint8_t *DataBuffer0, uint32_t StartAddress, uint32_t ByteCount)
{
uint32_t au32SourceData;
uint32_t au32DestinationData;
uint32_t Counter;
/* ÉèÖÃSPI³¤¶ÈΪ8¸öλԪ */
DrvSPI_SetBitLength(8);
/* Ƭѡ½ÅÉèΪÆô¶¯×´Ì¬ */
DrvSPI_SetSS();
/* ´«ËÍ0x03ÃüÁî, Read data */
au32SourceData = 0x03;
DrvSPI_SingleWrite(&au32SourceData);
/* µÈ´ý´«ËͽáÊø */
while (DrvSPI_IsBusy());
/* ÉèÖÃSPI³¤¶ÈΪ24¸öλԪ */
DrvSPI_SetBitLength(24);
/* ´«ËÍ24λԪλַ×ÊÁÏ */
au32SourceData = StartAddress;
DrvSPI_SingleWrite(&au32SourceData);
/* µÈ´ý´«ËͽáÊø */
while (DrvSPI_IsBusy());
/* ÉèÖÃSPI³¤¶ÈΪ8¸öλԪ */
DrvSPI_SetBitLength(8);
for (Counter = 0; Counter < ByteCount; Counter++)
{
/* ¶ÁÈ¡×ÊÁÏ */
au32SourceData = 0x0;
DrvSPI_SingleWrite(&au32SourceData);
/* µÈ´ý´«ËͽáÊø */
while (DrvSPI_IsBusy());
/* ´ÓRx¼Ä´æÆ÷ÄÚ¶ÁÈ¡×ÊÁÏ */
DrvSPI_DumpRxRegister(&au32DestinationData, 1);
DataBuffer0[Counter] = (uint8_t) au32DestinationData;
}
/* Ƭѡ½ÅÉèΪ·ÇÆô¶¯×´Ì¬ */
DrvSPI_ClrSS();
}
/*************************************************************************************
** Function name: main
** Descriptions:
** input parameters: ÎÞ
** output parameters: ÎÞ
** Returned value: ÎÞ
*************************************************************************************/
int main (void)
{
uint8_t WriteBuffer[256] = {0};
uint16_t i, id = 1;
uint8_t ReadBuffer[256] = {0};
//ReadBuffer[0]=0xfe;
Set_System();
ReadBuffer[0]=8; //µ÷ÓÃϵͳ³õʼ»¯º¯Êý
u32write=0X90;
/* ÉèÖÃSPI I/O */
DrvGPIO_InitFunction(FUNC_SPI);
/* ÉèÖÃSPIΪÖ÷ģʽ, 32-bit ´«Êä */
DrvSPI_Open(eDRVSPI_MASTER, eDRVSPI_TYPE1, 32);
/* ÅäÖô«ÊäʱµÄ±ÈÌØÓÅÏÈ´ÎÐòΪMSBÓÅÏÈ */
DrvSPI_SetEndian(eDRVSPI_MSB_FIRST);
/* ¹Ø±Õ×Ô¶¯Æ¬Ñ¡¹¦ÄÜ */
DrvSPI_DisableAutoSS();
/* É趨ƬѡÐźÅÆô¶¯µçƽΪƬѡÐźŵ͵çƽ»òÕßϽµÑØÆô¶¯ */
DrvSPI_SetSlaveSelectActiveLevel(eDRVSPI_ACTIVE_LOW_FALLING);
/* ÉèÖÃSPIʱÖÓËÙ¶ÈΪ 12MHz */
DrvSPI_SetClockFreq(1000000, 0);
// DISABLE_SPI_CS;
/* ²Á³ýSPI flash */
SpiFlash_ChipErase();
/* µÈ´ý²Á³ý½áÊø */
SpiFlash_WaitReady();
SpiFlash_ReadData(ReadBuffer,0,256);
DrvGPIO_ClrBit(E_PORT3, 1); //LED0-ON Erase OK
// delay_ms(1000);
/* SpiFlash_PageProgram(&u32write,0, 1);
SpiFlash_WaitReady();
DrvGPIO_ClrBit(E_PORT3, 6); //LED1-ON //Program done
delay_ms(1000);
SpiFlash_ReadData(&u32read,0, 1);
if (u32write !=u32read)
{
DrvGPIO_ClrBit(E_PORT2, 6); //LED3-ON Verify Error
//delay_ms(1000);
//while (1);
}
DrvGPIO_ClrBit(E_PORT5, 2); //LED2-ON Verify OK
delay_ms(1000);
DrvSPI_Close();
//delay_ms(200);*/
for(i=0;i<256;i++) //³õʼ»¯Êý×é
{
WriteBuffer = i;
}
SpiFlash_WaitReady(); //W25Q16BVæ״̬¼ì²é
// SPI_SectorErase(0x0000); //ÉÈÇø²Á³ý
SpiFlash_WaitReady(); //W25Q16BVæ״̬¼ì²é
SpiFlash_PageProgram(WriteBuffer,0,256); //W25Q16BV°´Ò³±à³Ìº¯Êý
SpiFlash_WaitReady();
//delay_ms(1000); //W25Q16BVæ״̬¼ì²é
SpiFlash_ReadData(ReadBuffer,0,256); //W25Q16BV¶ÁÊý¾Ýº¯Êý
//SpiFlash_WaitReady();
//delay_ms(1000);
for(i=0;i<256;i++) //½«¶Á³öÀ´µÄÊý¾ÝÓëд½øÈ¥µÄÊý¾Ý½øÐбȽÏ
{
if(WriteBuffer != ReadBuffer)
{
DrvGPIO_ClrBit(E_PORT5, 2); //LED2-ON Verify not OK
}
else
DrvGPIO_ClrBit(E_PORT2, 6); //LED3-ON
}
while(1);
}
|