使用at91sam7s64读写AT45DB321 spi flash

时间:2015-05-23 11:16:25

标签: c gcc spi

我正在使用带有At91sam7s64的AT45DB321 spi flash但是我无法正确读写AT45DB321,任何人都可以帮我保存下周! 主要的问题是似乎没有写任何内容所以我无法回读任何内容
以下是我用过的一些功能:

void AT45_MainMemoryProgram(unsigned char buffer, unsigned long address,
                            unsigned char *pointer_ram ,unsigned long n)
{
  AT45_Ready(); //wait until dataflash is ready

  SPI_TXRX(AT91C_BASE_SPI ,(buffer==1)?0x82:0x85); //send opcode

  SPI_TXRX(AT91C_BASE_SPI ,(address>>16)&0xff); //send address
  SPI_TXRX(AT91C_BASE_SPI ,(address>>8)&0xff);
  SPI_TXRX(AT91C_BASE_SPI ,address&0xff);

  while (n--) //write data
  {
    SPI_TXRX(AT91C_BASE_SPI ,*(pointer_ram++));
  }
  AT45_Reset();
}
void AT45_ContinousArrayRead(unsigned long address, unsigned char *pointer_ram,
                             unsigned long n)
{
  AT45_Ready(); //wait until dataflash is ready

  SPI_TXRX(AT91C_BASE_SPI ,0xE8); //send opcode

  SPI_TXRX(AT91C_BASE_SPI ,(address>>16)&0xff); //send address
  SPI_TXRX(AT91C_BASE_SPI ,(address>>8)&0xff);
  SPI_TXRX(AT91C_BASE_SPI ,address&0xff);

  //send don't care clocks
  SPI_TXRX(AT91C_BASE_SPI ,0);
  SPI_TXRX(AT91C_BASE_SPI ,0);
  SPI_TXRX(AT91C_BASE_SPI ,0);
  SPI_TXRX(AT91C_BASE_SPI ,0);

  while (n--) //read data
  {
    *(pointer_ram++) =  SPI_TXRX(AT91C_BASE_SPI ,0);;
  }
  AT91C_BASE_SPI->SPI_CR = AT91C_SPI_SPIEN | AT91C_SPI_LASTXFER;
  AT45_Reset();

}
unsigned char SPI_TXRX(AT91S_SPI *spi , unsigned char data)
{
  unsigned char temp;
  spi->SPI_TDR = data; // send data
  while(!(spi->SPI_SR & 1));
  temp = spi->SPI_RDR & 0xff;
  return temp;
}
void spi_config()
{
    AT91C_BASE_PIOA->PIO_ASR = (AT91C_PA12_MISO | AT91C_PA13_MOSI | AT91C_PA11_NPCS0 | AT91C_PA14_SPCK);
    AT91C_BASE_PIOA->PIO_PDR = (AT91C_PA12_MISO | AT91C_PA13_MOSI | AT91C_PA11_NPCS0 | AT91C_PA14_SPCK);    // Set in Periph mode
    AT91C_BASE_PMC->PMC_PCER = (unsigned int) 1 << AT91C_ID_SPI;
    AT91C_BASE_SPI->SPI_CR = AT91C_SPI_SWRST;
    AT91C_BASE_SPI->SPI_CR = AT91C_SPI_SWRST;
    AT91C_BASE_SPI->SPI_CR = AT91C_SPI_SPIEN;
    wait();
    AT91C_BASE_SPI->SPI_MR = (AT91C_SPI_MSTR | AT91C_SPI_PS | T91C_SPI_MODFDIS);
    AT91C_BASE_SPI->SPI_CSR[0] = AT45_CSR(mck, spck);
    AT91C_BASE_SPI->SPI_CSR[1] = AT45_CSR(mck, spck);
    AT91C_BASE_SPI->SPI_CSR[2] = AT45_CSR(mck, spck);
    AT91C_BASE_SPI->SPI_CSR[3] = AT45_CSR(mck, spck);
}

0 个答案:

没有答案