Hi,
I am trying to use SPI Interface to communicate with ALFAT. I tried to send the initialization command :
IM:
However I am getting back error code -99 i.e error unexpected.
#define ERR_UNEXPECTED 99
#define ERR_NO_ERROR 0
T_INT32U EncodeErrorCode(T_INT8U data, T_INT32U index)
{
if(data[index] != ‘!’) // Check if the value for index data[0] = !
return ERR_UNEXPECTED; //Check for the Error Code for “!00” . If !00 it means that the Command has been successfully executed
temp=data[index+1]-‘0’;
temp=(temp 16)+(data[index+2]-‘0’);
return temp;
delay(10);
}
I am using the sample code from ghi electronics for reference
@ sreya.0106 -
Can you provide more detail, please?
First of all, we don’t have error code 99 (or 0x99). We can see that is your define, not our.
Second
The "index" is not a fixed number. It depends on data in ALFAT buffers and the command you sent.
I meant, I see you are assuming that index is 0. but after a reset, (or alfat starts up) if you did not clear the banner string then this is not right. It should be = len(banner)+1.
@ DAT - Thank you for your reply.
I am using the sample code provided in the
Home > Catalog > File System Hardware > ALFAT 2 SoC Processor > Interface Examples
section of GHI Electronics website for my reference.
The code to generate the error code is a specified in the ALFAT_SPI.c file.
I tried to read the data[0] position for ‘!’ value but it is still not giving me the right answer.
C Language code #
#define DATA_BUFFER_SIZE 1024
#define DELAY_USER_TIME 1000
//Using media drive M
////// Initialize a Media drive //////////
// Command: I<SP>X:<LF> //
// Return : !00<LF> //
// X: Media drive such as U0, U1, M //
//////////////////////////////////////////
INT32 ALFAT_InitializeMedia(UINT8 drv[])
{
UINT32 num_read=0;
ResetCmd(command);
AddCmd(command,"I ");
AddCmd(command,drv);
AddCmd(command,"\r");
// Wait while ALFAT is not ready for new command
while(ActivePinState())
{
Delay(USER_TIME_DELAY);
ReadBytes(data_buffer, num_read,(DATA_BUFFER_SIZE-num_read));
}
SendCmd(command);
// Delay a litle bit
Delay(USER_TIME_DELAY);
// Wait if active pin is high
memset(data_buffer,0 ,DATA_BUFFER_SIZE);
// Because initialize a media driver maybe takes more one second. We should wait untill
// finishing initialize by check active pin.
while(ActivePinState())
{
Delay(USER_TIME_DELAY);
num_read +=ReadBytes(data_buffer, num_read,(DATA_BUFFER_SIZE-num_read));
}
if (num_read<4)
num_read +=ReadBytes(data_buffer, num_read,(DATA_BUFFER_SIZE-num_read));
// Process error code
return EncodeErrorCode(data_buffer,0); // "!00\n"
}
//////////// SPI Read bytes ////////////////////////
UINT32 STM32_SPI_ReadBytes(UINT8 *data, UINT32 index, UINT32 length)
{
INT32 actual_size=0;
INT32 start =index ;
INT32 end;
// Prepare frame header
// Add READ_CMD (2) to header
spi_frame_header_tx[0] = READ_CMD;
// Add size LSB
spi_frame_header_tx[1] = length & 0xFF;
// Add size MSB
spi_frame_header_tx[2] = (length>>8) & 0xFF;
// Send 3 bytes Header
SPI_PutC(spi_frame_header_tx[0]);
SPI_PutC(spi_frame_header_tx[1]);
SPI_PutC(spi_frame_header_tx[2]);
// Read 3 bytes header back
spi_frame_header_rx[0] = SPI_GetC();
spi_frame_header_rx[1] = SPI_GetC();
spi_frame_header_rx[2] = SPI_GetC();
// Get actual size
actual_size = (spi_frame_header_rx[1] | (spi_frame_header_rx[2]<<8));
// Check actual size
if (length > actual_size)
{
length = actual_size;
}
// Update end
end = length + index;
while(start<end)
{
// Send 0x00
SPI_PutC(0);
// Read back data
data[start++] = SPI_GetC();
}
return (start-index);
}
UINT32 STM32_SPI_WriteBytes(UINT8 *data, UINT32 index, UINT32 length)
{
INT32 start =index ;
INT32 end = length + index;
INT32 timeout = USER_TIME_OUT;
if (!is_interrupt_enable) //Commad 'L' has been used
{
start = STM32_SPI_WriteBytes_Use_SlaveDMA(data,index,length);
}
else //Commad 'W' has been used
{
// Add WRITE_CMD (1) to header
spi_frame_header_tx[0] = WRITE_CMD;
// Add size LSB
spi_frame_header_tx[1] = length & 0xFF;
// Add size MSB
spi_frame_header_tx[2] = (length>>8) & 0xFF;
spi_frame_header_rx[1] = 0;
// Wait for ALFAT is ready
// Send frame for write
// Add timeout
while ( spi_frame_header_rx[1] != TRUE && timeout>0)
{
while( STM32_SPI_BusyPinState());
// Send 2 bytes header
SPI_PutC(spi_frame_header_tx[0]);
SPI_PutC(spi_frame_header_tx[1]);
// Read 2 bytes back
spi_frame_header_rx[0] = SPI_GetC();
spi_frame_header_rx[1] = SPI_GetC();
if (spi_frame_header_rx[1] != TRUE)
{
Delay(1);
timeout--;
}
}
// Not timeout
if ( timeout!=0)
{
// Send the 3th byte
SPI_PutC(spi_frame_header_tx[2]);
// Get back 1 byte
SPI_GetC();
while( start< end)
{
// Wait while ALFAT is busy
while( STM32_SPI_BusyPinState());
// Send data
SPI_PutC(data[start++]);
// Read data back
SPI_GetC();
}
}
}
return (start-index);
}
INT32 EncodeErrorCode(UINT8 *data, INT32 index)
{
INT32 temp;
if(data[index] != '!')
return ERR_UNEXPECTED;
temp=data[index+1]-'0';
temp=(temp*16)+(data[index+2]-'0');
return temp;
}
This is the sample code I am using as reference
@ sreya.0106 -
Hi,
That function is correct, the problem is you need to know what “index” is. For some cases it is not zero, specially when ALFAT is reset or starts up.
because when ALFAT starts up, the banner “GHI Electronic…!00”. This case data[0] is ‘G’. data[1] is ‘H’…
when you sent “I M:” without clearing the banner. ALFAT buffer will be
“GHI Electronic…!00!00 ”, because “!00” will bed added to current buffer.
But if you clear the banner before sending “I M:”, buffer is clear and now it will be:
“!00”. This case index is ‘0’ and data[0] is ‘!’
Here is the banner when ALFAT boots up
"\n GHI Electronics, LLC \n----------------------\n ALFAT SoC Processor \n!00\n"
If I count correctly, data[68] = ‘!’, not zero
Did you clear (read) the banner?
but in the function to Initialize media the first line has the reset command :
Code Language: C#
ResetCmd(command);
void ResetCmd(UINT8 *cmd)
{
memset(cmd,0,256);
}
Does’nt this take care of clearing the banner before I M: being written to it?
but in the function to Initialize media the first line has the reset command :
Code Language: C#
ResetCmd(command);
void ResetCmd(UINT8 *cmd)
{
memset(cmd,0,256);
}
That array is for sending a cmd to ALFAT,
The array we are talking is for receiving responses from ALFAT. They are different.
You can clear before sending a command, or you can do it later. and of course the “index” will be different as I explained.
@ DAT thank you for the help.
I will clear the banner and then try reading it. Hopefully it should work then. I will come here again if I still have issues with having it work fine
@ sreya.0106 -
If you are successful reading the alfat banner then you should be OK reading other alfat’s responses. Because banner is also one of responses, so the reading way are same for all.