📄 ssi_atmel.c
字号:
// Wait until the SSI chip select deasserts, indicating the end of the
// transfer.
//
while(!(GPIOPinRead(GPIO_PORTA_BASE, SSI_CS) & SSI_CS))
{
}
}
//*****************************************************************************
//
// Read the manufacturer and produce ID from the Atmel device.
//
//*****************************************************************************
unsigned long
AtmelReadID(void)
{
unsigned char pucCmd[4];
//
// Send the read ID command.
//
pucCmd[0] = ATMEL_RDID;
//
// Do the transfer. There is one byte to write but three to read (the byte
// that gets transferred during the write and two bytes of ID).
//
SSITransfer(pucCmd, 1, pucCmd, 3);
//
// Return the ID.
//
return((pucCmd[1] << 8) | pucCmd[2]);
}
//*****************************************************************************
//
// Read the chip status from the Atmel device.
//
//*****************************************************************************
unsigned long
AtmelChipStatus(void)
{
unsigned char pucCmd[4];
//
// Send the read status register command.
//
pucCmd[0] = ATMEL_RDSR;
//
// Do the transfer. There is one byte to write but two to read (the byte
// that gets transferred during the write and one status byte).
//
SSITransfer(pucCmd, 1, pucCmd, 2);
//
// Return the status byte.
//
return(pucCmd[1]);
}
//*****************************************************************************
//
// Erase the Atmel device.
//
//*****************************************************************************
void
AtmelEraseChip(void)
{
unsigned char pucCmd[4];
//
// Enable the write latch.
//
pucCmd[0] = ATMEL_WREN;
//
// Send the command.
//
SSITransfer(pucCmd, 1, pucCmd, 1);
//
// Send the chip erase command.
//
pucCmd[0] = ATMEL_CHIP_ERASE;
//
// Send the command.
//
SSITransfer(pucCmd, 1, 0, 0);
//
// Wait until the erase is done.
//
while(AtmelChipStatus() & 0x01)
{
}
}
//*****************************************************************************
//
// Write to the Atmel device. Note that pucData MUST be preceeded by four
// bytes that can be destroyed by this function.
//
//*****************************************************************************
void
AtmelWrite(unsigned char *pucData, unsigned long ulOffset,
unsigned long ulCount)
{
unsigned char pucCmd[4];
//
// Enable the write latch.
//
pucCmd[0] = ATMEL_WREN;
//
// Send the command.
//
SSITransfer(pucCmd, 1, pucCmd, 1);
//
// Send the program command.
//
pucData[-4] = ATMEL_PROGRAM;
//
// Send the address.
//
pucData[-3] = (ulOffset >> 16) & 0xff;
pucData[-2] = (ulOffset >> 8) & 0xff;
pucData[-1] = ulOffset & 0xff;
//
// Send the command.
//
SSITransfer(pucData - 4, ulCount + 4, 0, 0);
//
// Wait until the programming is done.
//
while(AtmelChipStatus() & 0x01)
{
}
}
//*****************************************************************************
//
// Read from the Atmel device.
//
//*****************************************************************************
void
AtmelRead(unsigned char *pucData, unsigned long ulOffset,
unsigned long ulCount)
{
unsigned char pucCmd[4];
//
// Send the read command.
//
pucCmd[0] = ATMEL_READ;
//
// Send the address.
//
pucCmd[1] = (ulOffset >> 16) & 0xff;
pucCmd[2] = (ulOffset >> 8) & 0xff;
pucCmd[3] = ulOffset & 0xff;
//
// Send the command.
//
SSITransfer(pucCmd, 4, pucData - 4, ulCount + 4);
}
//*****************************************************************************
//
// This example demonstrates the use of the SSI block to connect to an Atmel
// AT25F1024A EEPROM.
//
//*****************************************************************************
int
main(void)
{
unsigned char *pucData;
unsigned long ulIdx;
//
// Set the clocking to run directly from the crystal.
//
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_6MHZ);
//
// Enable the peripherals used by this example.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//
// Init the PDC and the LCD.
//
PDCInit();
PDCLCDInit();
PDCLCDBacklightOn();
PDCLCDSetPos(0, 0);
PDCLCDWrite("SSI running...", 14);
//
// Enable processor interrupts.
//
IntMasterEnable();
//
// Configure the appropriate pins to be SSI instead of GPIO. Note that
// the chip select is kept as a GPIO to guarantee the appropriate
// signalling to the Atmel device.
//
GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, SSI_CS);
GPIOPinWrite(GPIO_PORTA_BASE, SSI_CS, SSI_CS);
GPIOPinTypeSSI(GPIO_PORTA_BASE, SSI_CLK | SSI_TX | SSI_RX);
//
// Configure and enable the SSI port for master mode.
//
SSIConfigSetExpClk(SSI0_BASE, SysCtlClockGet(), SSI_FRF_MOTO_MODE_3,
SSI_MODE_MASTER, 1000000, 8);
SSIEnable(SSI0_BASE);
//
// Read any residual data from the SSI port.
//
while(SSIDataGetNonBlocking(SSI0_BASE, &ulIdx))
{
}
//
// Enable the SSI interrupt.
//
IntEnable(INT_SSI0);
//
// Make sure that the Atmel device ID is correct.
//
if(AtmelReadID() != 0x1f60)
{
//
// Must reinitialize the PDC because the SSI setup for the
// flash device is different from the PDC.
//
PDCInit();
PDCLCDSetPos(0, 1);
PDCLCDWrite("Bad device ID.", 14);
DiagExit(0);
}
//
// Erase the Atmel device.
//
AtmelEraseChip();
//
// For simplified operation, the AtmelWrite() and AtmelRead() functions
// requires four bytes of padding BEFORE the buffer passed to them (used to
// store the command to be sent to the EEPROM before the data). Construct
// a pointer four bytes into the data buffer.
//
pucData = g_pucData + 4;
//
// Write a data=address pattern into the first 256 bytes of the Atmel
// device.
//
for(ulIdx = 0; ulIdx < 256; ulIdx++)
{
pucData[ulIdx] = ulIdx;
}
AtmelWrite(pucData, 0, 256);
//
// Read the first 256 bytes of the Atmel device and verify that it contains
// the data it should.
//
AtmelRead(pucData, 0, 256);
for(ulIdx = 0; ulIdx < 256; ulIdx++)
{
if(pucData[ulIdx] != ulIdx)
{
//
// Must reinitialize the PDC because the SSI setup for the
// flash device is different from the PDC.
//
PDCInit();
PDCLCDSetPos(0, 1);
PDCLCDWrite("Read error.", 11);
DiagExit(0);
}
}
//
// Success. Must reinitialize the PDC because the SSI setup for the
// the flash device is different from the PDC.
//
PDCInit();
PDCLCDSetPos(0, 1);
PDCLCDWrite("Success.", 8);
//
// Exit.
//
DiagExit(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -