⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 anologat45db021.cpp

📁 AT45db021的模拟总线驱动程序。具有很详细的说明。绝对好用。
💻 CPP
📖 第 1 页 / 共 2 页
字号:


// Purpose:       Write data to a buffer
// Inputs:        1) A buffer number (0 or 1)
//                2) An index into the buffer
//                3) A pointer to the data to write
//                4) The number of bytes of data to write
// Outputs:       None
// Dependencies:  ext_flash_sendData(), ext_flash_waitUntilReady(), ext_flash_sendBytes()
void ext_flash_writeToBuffer(int1 bufferNumber, int16 bufferAddress, BYTE* data, int16 size)
{
   BYTE opcode;

   output_low(FLASH_SELECT);                 // Enable select line

   if(bufferNumber)
      opcode = 0x87;                         // Opcode for second buffer
   else
      opcode = 0x84;                         // Opcode for first buffer

   ext_flash_sendData(opcode, 8);            // Send opcode
   ext_flash_sendData(0, 15);                // Send 15 don't care bits
   ext_flash_sendData(bufferAddress, 9);     // Send buffer address
   ext_flash_sendBytes(data, size);          // Write data to the buffer
   output_high(FLASH_SELECT);                // Disable select line
}


// Purpose:       Write the data in a buffer to a page
// Inputs:        1) A page address
//                2) A buffer number (0 or 1)
//                3) The writing mode to use
//                   - Use ERASE to first erase a page then write
//                   - Use NO_ERASE to write to a previously erased page
// Outputs:       None
// Dependencies:  ext_flash_sendData(), ext_flash_waitUntilReady()
void ext_flash_BufferToPage(int1 bufferNumber, int16 pageAddress, int1 mode)
{
   BYTE opcode;
   ext_flash_waitUntilReady();               // Wait until ready
   output_low(FLASH_SELECT);                 // Enable select line

   if(mode)
   {
      if(bufferNumber)
         opcode = 0x86;                      // Opcode for second buffer
      else
         opcode = 0x83;                      // Opcode for first buffer
   }
   else
   {
      if(bufferNumber)
         opcode = 0x89;                      // Opcode for second buffer
      else
         opcode = 0x88;                      // Opcode for first buffer
   }
   ext_flash_sendData(opcode, 8);            // Send opcode
   ext_flash_sendData(pageAddress, 15);      // Send page address
   ext_flash_sendData(0, 9);                 // Send 9 don't care bits
   output_high(FLASH_SELECT);                // Disable select line
}


// Purpose:       Erase a page
// Inputs:        A page address
// Outputs:       None
// Dependencies:  ext_flash_sendData(), ext_flash_waitUntilReady()
void ext_flash_erasePage(int16 pageAddress)
{
   ext_flash_waitUntilReady();
   output_low(FLASH_SELECT);                 // Enable select line
   ext_flash_sendData(0x81, 8);              // Send opcode
   ext_flash_sendData(pageAddress, 15);      // Send page address
   ext_flash_sendData(0, 9);                 // Send 9 don't care bits
   output_high(FLASH_SELECT);                // Disable select line
}


// Purpose:       Erase a block of 8 pages
// Inputs:        A block address
// Outputs:       None
// Dependencies:  ext_flash_sendData(), ext_flash_waitUntilReady()
void ext_flash_eraseBlock(int8 blockAddress)
{
   ext_flash_waitUntilReady();
   output_low(FLASH_SELECT);                 // Enable select line
   ext_flash_sendData(0x50, 8);              // Send opcode
   ext_flash_sendData(blockAddress, 12);     // Send block address
   ext_flash_sendData(0, 12);                // Send 12 don't care bits
   output_high(FLASH_SELECT);                // Disable select line
}


// Purpose:       Write data to a page through a buffer
// Inputs:        1) The address of the page to write to
//                2) The number of the buffer to use (0 or 1)
//                3) The index into the buffer to start writing at
//                4) A pointer to the data to write
//                5) The number of bytes of data to write
// Outputs:       None
// Dependencies:  ext_flash_sendData(), ext_flash_waitUntilReady(), ext_flash_sendBytes()
void ext_flash_writePageThroughBuffer(int16 pageAddress,
                                      int1 bufferNumber, int16 bufferAddress,
                                      BYTE* data, int16 size)
{
   BYTE opcode;

   ext_flash_waitUntilReady();
   output_low(FLASH_SELECT);                 // Enable select line

   if(bufferNumber)
      opcode = 0x85;                         // Opcode for second buffer
   else
      opcode = 0x82;                         // Opcode for first buffer

   ext_flash_sendData(opcode, 8);            // Send opcode
   ext_flash_sendData(pageAddress, 15);      // Send page address
   ext_flash_sendData(bufferAddress, 9);     // Send buffer address
   ext_flash_sendBytes(data, size);          // Write data to the buffer
   output_high(FLASH_SELECT);                // Disable select line
}


// Purpose:       Get the data from a page and put it in a buffer
// Inputs:        1) A page address
//                2) A buffer number (0 or 1)
// Outputs:       None
// Dependencies:  ext_flash_sendData(), ext_flash_waitUntilReady()
void ext_flash_PageToBuffer(int16 pageAddress, int1 bufferNumber)
{
   BYTE opcode;
   ext_flash_waitUntilReady();
   output_low(FLASH_SELECT);                 // Enable select line

   if(bufferNumber)
      opcode = 0x55;                         // Opcode for second buffer
   else
      opcode = 0x53;                         // Opcode for first buffer

   ext_flash_sendData(opcode, 8);            // Send opcode
   ext_flash_sendData(pageAddress, 15);      // Send page address
   ext_flash_sendData(0, 9);                 // Send 9 don't care bits
   output_high(FLASH_SELECT);                // Disable select line
}


// Purpose:       Compare the data in a page to the data in a buffer
// Inputs:        1) A page address
//                2) A buffer number (0 or 1)
// Outputs:       1 if the data is the same, 0 if the data is not the same
// Dependencies:  ext_flash_sendData(), ext_flash_waitUntilReady()
int1 ext_flash_comparePageToBuffer(int16 pageAddress, int1 bufferNumber)
{
   int1 CompareFlag;
   BYTE opcode;
   ext_flash_waitUntilReady();
   output_low(FLASH_SELECT);                 // Enable select line

   if(bufferNumber)
      opcode = 0x61;                         // Opcode for second buffer
   else
      opcode = 0x60;                         // Opcode for first buffer

   ext_flash_sendData(opcode, 8);            // Send opcode
   ext_flash_sendData(pageAddress, 15);      // Send page address
   ext_flash_sendData(0, 9);                 // Send 9 don't care bits
   output_high(FLASH_SELECT);                // Disable select line

   output_low(FLASH_SELECT);                 // Enable select line
   ext_flash_sendData(0xD7, 8);              // Send status command
   while(!input(FLASH_DO));                  // Wait until ready
   output_high(FLASH_CLOCK);                 // Pulse the clock
   output_low(FLASH_CLOCK);
   CompareFlag = !input(FLASH_DO);           // Get the compare flag
   output_high(FLASH_SELECT);                // Disable select line

   return CompareFlag;
}


// Purpose:       Rewrite the data in a page.
//                The flash device does this by transfering the data to a
//                buffer, then writing the data back to the page.
// Inputs:        1) A page address
//                2) A buffer number (0 or 1)
// Outputs:       None
// Dependencies:  ext_flash_sendData(), ext_flash_waitUntilReady()
void ext_flash_rewritePage(int16 pageAddress, int bufferNumber)
{
   BYTE opcode;
   ext_flash_waitUntilReady();
   output_low(FLASH_SELECT);                 // Enable select line

   if(bufferNumber == 1)
      opcode = 0x58;
   else
      opcode = 0x59;

   ext_flash_sendData(opcode, 8);            // Send opcode
   ext_flash_sendData(pageAddress, 15);      // Send page address
   ext_flash_sendData(0, 9);                 // Send 9 don't care bits
   output_high(FLASH_SELECT);                // Disable select line
}


// Purpose:       Send some data to the flash device
// Inputs:        1) Up to 16 bits of data
//                2) The number of bits to send
// Outputs:       None
// Dependencies:  None
void ext_flash_sendData(int16 data, int8 size)
{
   int8 i;
   data <<= (16-size);
   for(i=0; i<size; ++i)
   {
      output_bit(FLASH_DI, shift_left(&data,2,0));    // Send a data bit
      output_high(FLASH_CLOCK);                       // Pulse the clock
      output_low(FLASH_CLOCK);
   }
}


// Purpose:       Send some bytes of data to the flash device
// Inputs:        1) A pointer to an array of data to send
//                2) The number of bytes to send
// Outputs:       None
// Dependencies:  None
void ext_flash_sendBytes(BYTE* data, int16 size)
{
   int16 i;
   signed int8  j;
   for(i=0; i<size; ++i)
   {
      for(j=7; j>=0; --j)
      {
         output_bit(FLASH_DI, bit_test(data[i], j));  // Send a data bit
         output_high(FLASH_CLOCK);                    // Pulse the clock
         output_low(FLASH_CLOCK);
      }
   }
}

// Purpose:       Wait until the flash device is ready to accept commands
// Inputs:        None
// Outputs:       None
// Dependencies:  ext_flash_sendData()
void ext_flash_waitUntilReady()
{
   output_low(FLASH_SELECT);                 // Enable select line
   ext_flash_sendData(0xD7, 8);              // Send status command
   while(!input(FLASH_DO));                  // Wait until ready
   output_high(FLASH_SELECT);                // Disable select line
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -