Thank you for your response Agehall,
I've done a bit of work on it. I have left the ints as they were, but will do the quick changeover later once everything is working.
#include <unistd.h>
#include <wiringPi.h>
#include <wiringPiSPI.h>
#include <sys/resource.h>
int main ( int argc, char *argv[] )
{
if ( argc != 2 ) /* argc should be 2 for correct execution */
{
/* We print argv[0] assuming it is the program name */
printf( "usage: %s filename", argv[0] );
}
else
{
FILE *hexFile = fopen( argv[1], "rb" );
if (!hexFile)
{
printf("Failed to open file.\n");
exit(-1);
}
int spi = wiringPiSPISetup(0, 1000000); // Setup SPI channel 0, 1MHz
fseek (hexFile, 0, SEEK_END);
size_t file_size = ftell(hexFile);
rewind(hexFile);
char * buffer = calloc(file_size, sizeof(char));
if (!buffer)
{
printf("Failed to allocate file buffer.\n");
exit(-1);
}
fread(buffer, file_size, file_size, hexFile);
fclose(hexFile);
/*
for (int index = 0; index < file_size; index += 9)
{
int size = file_size - index;
if(size > 9)
{
write(spi, &buffer[index], 10);
}
else
{
write(spi, &buffer[index], size);
}
}*/
char dat[5] = {0x01, 0x02, 0x03, 0x04, 0x05};
write(spi, dat , 5);
}
}
I've got a bit of a problem right now, where this works:
char dat[5] = {0x01, 0x02, 0x03, 0x04, 0x05};
write(spi, dat , 5);
but not this....
for (int index = 0; index < file_size; index += 9)
{
int size = file_size - index;
if(size > 9)
{
write(spi, &buffer[index], 10);
}
else
{
write(spi, &buffer[index], size);
}
}
The latter one which is not working seems to be looping around just fine, and sending the correct final amount... however the SPI is just clocking and not outputting data, as if the entire thing is only zeros.
Now I know there are a lot of zeros in my bin file, from the ICE40 software, however I get nothing even if I set my logic analyser to trigger on the data line.....