📄 f34x_spi0_master_to_eeprom3.c
字号:
// This example recognizes when an error occurs, but does not include
// any error handling. The transfer can be aborted or rescheduled,
// if desired.
// Error_Flag = 1;
//}
// When the Master enters the ISR, the SPIF flag should be set from
// sending the Command byte. This ISR handles the remaining steps of the
// SPI transfer process.
// <state> == 0: writing or reading 1 byte of data
// <state> == 1: for READ commands (first time, only a dummy byte is
// sent but the second time, the data must be read from
// SPI0DAT)
// <state> == 2: NSS = 1 to end the transfer, final byte read
//
// Note: SPI_WRITE_BUFFER is not handled here because it's done in
// polled mode
if (state == 0)
{
array_index++;
if (array_index < sendlength)
{
SPI0DAT = pSPIWord[array_index];
}
else
{
SPI0DAT = 0xFF;
array_index = 0;
state = 1;
if (array_index == (readlength-1))
{
state = 2;
}
}
}
else if (state == 1) // This state is for READ_ARRAY
{ // commands where the data must be read
// after the first dummy byte is sent
pSPIWord[array_index] = SPI0DAT;
SPI0DAT = 0xFF;
array_index++;
if (array_index == (readlength-1))
{
state = 2;
}
}
else if (state == 2)
{
pSPIWord[array_index] = SPI0DAT; // Read the last data
// without sending a
// dummy byte
CS = 1; // De-select the Slave
state = 0; // Reset the state
array_index = 0;
}
SPIF = 0; // Clear the SPIF flag
}
}
//-----------------------------------------------------------------------------
// Support Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// SPI_LED_On
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Turns the LED on the SPI Slave on. The slave does not respond to this
// command, so the command consists of:
//
// Command = SLAVE_LED_ON
// Length = 1 byte (the command itself)
//
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// SPI_LED_Off
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Turns the LED on the SPI Slave off. The slave does not respond to this
// command, so the command consists of:
//
// Command = SLAVE_LED_OFF
// Length = 1 byte (the command itself)
//
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// SPI_Byte_Write
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Note: SPI_Data must contain the data to be sent before calling this
// function.
//
// Writes a single byte to the SPI Slave. The slave does not respond to this
// command, so the command consists of:
//
// Command = SPI_WRITE
// Length = 1 byte of command, 1 byte of data
//
//-----------------------------------------------------------------------------
void WREN_Byte_Write (void)
{
while (!CS); // Wait until the SPI is free, in case
// it's already busy
CS = 0;
Command = WREN;
SPI0DAT = Command;
// The rest of this command will be handled by the SPI ISR, which will
// trigger when SPIF is set from sending the Command
}
void WRDI_Byte_Write (void)
{
while (!CS); // Wait until the SPI is free, in case
// it's already busy
CS = 0;
Command = WRDI;
SPI0DAT = Command;
// The rest of this command will be handled by the SPI ISR, which will
// trigger when SPIF is set from sending the Command
}
void WRSR_Byte_Write (void)
{
while (!CS); // Wait until the SPI is free, in case
// it's already busy
CS = 0;
Command = WRSR;
SPI0DAT = Command;
// The rest of this command will be handled by the SPI ISR, which will
// trigger when SPIF is set from sending the Command
}
void WRITE_Byte_Write (void)
{
while (!CS); // Wait until the SPI is free, in case
// it's already busy
CS = 0;
Command = WRITE;
SPI0DAT = Command;
// The rest of this command will be handled by the SPI ISR, which will
// trigger when SPIF is set from sending the Command
}
//-----------------------------------------------------------------------------
// SPI_Byte_Read
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Note: SPI_Data will contain the data received after calling this function.
//
// Reads a single byte from the SPI Slave. The command consists of:
//
// Command = SPI_READ
// Length = 1 byte of command, 1 byte of data
//
//-----------------------------------------------------------------------------
void READ_Byte_Read (void)
{
while (!CS); // Wait until the SPI is free, in case
// it's already busy
CS = 0;
Command = READ;
SPI0DAT = Command;
// The rest of this command will be handled by the SPI ISR, which will
// trigger when SPIF is set from sending the Command
}
void RDSR_Byte_Read (void)
{
while (!CS); // Wait until the SPI is free, in case
// it's already busy
CS = 0;
Command = RDSR;
SPI0DAT = Command;
// The rest of this command will be handled by the SPI ISR, which will
// trigger when SPIF is set from sending the Command
}
//-----------------------------------------------------------------------------
// SPI_Array_Write
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Note: SPI_Data_Array must contain the data to be sent before calling this
// function.
//
// Writes an array of values of size MAX_BUFFER_SIZE to the SPI Slave. The
// command consists of:
//
// Command = SPI_WRITE_BUFFER
// Length = 1 byte of command, MAX_BUFFER_SIZE bytes of data
//
// Note: Polled mode is used for this function in order to buffer the data
// being sent using the TXBMT flag.
//
//-----------------------------------------------------------------------------
void SPI_Byte_Write (void)
{
unsigned char array_index;
while (!CS); // Wait until the SPI is free, in case
// it's already busy
ESPI0 = 0; // Disable SPI interrupts
CS = 0;
SPI0DAT = SPI_Data; // Load the XMIT register
while (TXBMT != 1) // Wait until the command is moved into
{ // the XMIT buffer
}
SPIF = 0;
while (SPIF != 1) // Wait until the last byte of the
{ // data reaches the Slave
}
SPIF = 0;
CS = 1; // Diable the Slave
ESPI0 = 1; // Re-enable SPI interrupts
}
void SPI_Array_Write (void)
{
unsigned char array_index;
while (!CS); // Wait until the SPI is free, in case
// it's already busy
ESPI0 = 0; // Disable SPI interrupts
CS = 0;
//SPI0DAT = SPI_WRITE_BUFFER; // Load the XMIT register
//while (TXBMT != 1) // Wait until the command is moved into
//{ // the XMIT buffer
//}
for (array_index = 0; array_index < sendlength; array_index++)
{
SPI0DAT = pSPIWord[array_index]; // Load the data into the buffer
while (TXBMT != 1) // Wait until the data is moved into
{ // the XMIT buffer
}
}
SPIF = 0;
while (SPIF != 1) // Wait until the last byte of the
{ // data reaches the Slave
}
SPIF = 0;
CS = 1; // Diable the Slave
ESPI0 = 1; // Re-enable SPI interrupts
}
void SPI_Array_Write_And_Read (void)
{
//unsigned char array_index;
while (!CS); // Wait until the SPI is free, in case
// it's already busy
ESPI0 = 1;
Command = Write_And_Read;
CS = 0;
SPI0DAT = pSPIWord[0]; // Load the XMIT register
//while (TXBMT != 1) // Wait until the command is moved into
//{ // the XMIT buffer
//}
//for (array_index = 0; array_index < sendlength; array_index++)
//{
//if (array_index == (sendlength-1))
//{
// ESPI0 = 1;
//}
//SPI0DAT = pSPIWord[array_index]; // Load the data into the buffer
//if (array_index == (sendlength-1))
//{
//while (TXBMT != 1) // Wait until the data is moved into
//{ // the XMIT buffer
//}
//}
//}
// SPIF = 0;
// while (SPIF != 1) // Wait until the last byte of the
// { // data reaches the Slave
// }
// SPIF = 0;
//
// CS = 1; // Diable the Slave
//
// ESPI0 = 1; // Re-enable SPI interrupts
}
//-----------------------------------------------------------------------------
// SPI_Array_Read
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Note: SPI_Data_Array will contain the data received after calling this
// function.
//
// Reads a single byte from the SPI Slave. The command consists of:
//
// Command = SPI_READ_BUFFER
// Length = 1 byte of command, MAX_BUFFER_SIZE bytes of data
//
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Delay
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Delay for little while (used for blinking the LEDs)
//
//-----------------------------------------------------------------------------
void Delay (void)
{
unsigned long count;
for (count = 100000; count > 0; count--);
}
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -