libstock fat32 library and SPI use for other communications

General discussion on mikroPascal PRO for AVR.
Post Reply
Author
Message
robfaz
Posts: 31
Joined: 26 Apr 2009 13:20
Location: Italia

libstock fat32 library and SPI use for other communications

#1 Post by robfaz » 08 May 2014 14:11

Hi,
I'm using the fat32 libstock library routines to write a file on an sd card and everything is ok, but if I add in the program setting spdr for communication via spi with other devices happens that new program write operations on files are not complete and more precisely "mmc_error : = FAT32_Write (fHandle , @bufferr, 100); " Returns mmc_error = 255.
I have done numerous tests and the problem occurs after the first time after the first time you perform an assignment operation to spdr for example spdr: = c.
I attached a picture of the code.
Attachments
in this mode it's all well and the program write and no errors.
in this mode it's all well and the program write and no errors.
ok.JPG (35.41 KiB) Viewed 3763 times
in this mode the runtime operation of write return error code=255.
in this mode the runtime operation of write return error code=255.
ko.JPG (35.37 KiB) Viewed 3763 times
« Tutti gli esseri umani nascono liberi ed eguali in dignità e diritti.
Essi sono dotati di ragione e di coscienza e devono agire gli uni verso gli altri in spirito di fratellanza. »

User avatar
marina.petrovic
Posts: 2986
Joined: 18 Apr 2013 08:11

Re: libstock fat32 library and SPI use for other communicati

#2 Post by marina.petrovic » 09 May 2014 14:38

Hi,

Please, can you tell me which microcontroller (and hardware) you use?
If you can send me some small project (zipped project folder) that represent problem that you have,
so I can test it on my system and try to find source of the problem.

Best regards,
Marina

robfaz
Posts: 31
Joined: 26 Apr 2009 13:20
Location: Italia

Re: libstock fat32 library and SPI use for other communicati

#3 Post by robfaz » 10 May 2014 11:02

I use atmega32 and easyavr5a
this is the program:
program a32;
const CR_ : char = 0x0D;
const LF_ : char = 0x0A;
VAR
bufferr:string[32];
Mmc_Chip_Select : sbit at PORTB.B2;
Mmc_Chip_Select_Direction : sbit at DDRB.B2;
fHandle : short;
c,c1,mmc_error:BYTE;
a:string[3];
aa:array[4] of char;
txt : array[12] of char;
Begin
PORTA:=%10000001;
DDRA:=%10000011;
PORTB:=%00011111;//.............
DDRB:=%10110100;
PORTC:=%00000011;//....................
DDRC:=%11111101; //
PORTD:=%00011011;//.....................
DDRD:=%01111110;
UART1_Init(9600);
DELAY_MS(200);
//FINE INIZIALIZZAZIONE UART
//INIZIALIZZAZIONE SPI
SPI1_Init_Advanced(_SPI_MASTER, _SPI_FCY_DIV2, _SPI_CLK_LO_LEADING);
//FINE INIZIALIZZAZIONE SPI}
UART1_WRITE_TEXT('-----------------------------start------------------------------------------- ');
uart1_write (cr_);
uart1_write(lf_);
MMC_ERROR:=1;
while (mmc_error<>0) do
begin
UART1_WRITE_TEXT('start FAT32_Dev_Init----- ');
mmc_error := FAT32_Dev_Init () ;
ByteToStr(mmc_error,a);
UART1_WRITE_TEXT(a);
uart1_write (cr_);
uart1_write (lf_);
end;
UART1_WRITE_TEXT('end FAT32_Dev_Init----- ');
uart1_write (cr_);
uart1_write (lf_);
for c:=0 to 32 do
begin
memcpy(@bufferr+c,45,1);
end;
MMC_ERROR:=1;
while (mmc_error<>0) do
begin
UART1_WRITE_TEXT('start FAT32_Init () ----- ');
mmc_error:=FAT32_Init () ;
ByteToStr(mmc_error,a);
UART1_WRITE_TEXT(a);
uart1_write (cr_);
uart1_write (lf_);
end;
UART1_WRITE_TEXT('end FAT32_Init () ----- ');
uart1_write (cr_);
uart1_write (lf_);
txt:='datiacce.txt';
fHandle:=-1;
while (fHandle<0) do
begin
UART1_WRITE_TEXT('START FAT32_Open --------');
fHandle:=FAT32_Open(@txt, FILE_WRITE_F32);
//FAT32_Open('datiacce.txt', 1 );
ShortToStr(fHandle,aa);
UART1_WRITE_TEXT(aa);
uart1_write (cr_);
uart1_write (lf_);
end;
UART1_WRITE_TEXT('END FAT32_Open -------');
uart1_write (cr_);
uart1_write (lf_);
UART1_WRITE_TEXT('start first FAT32_Write ----');
uart1_write (cr_);
uart1_write (lf_);
mmc_error:=FAT32_Write(fHandle , @bufferr, 32) ;
ByteToStr(mmc_error,a);
UART1_WRITE_TEXT('end first FAT32_Write----');
UART1_WRITE_TEXT(a);
uart1_write (cr_);
uart1_write (lf_);
UART1_WRITE_TEXT('start second FAT32_Write ----');
uart1_write (cr_);
uart1_write (lf_);
mmc_error:=FAT32_Write(fHandle , @bufferr, 32) ;
ByteToStr(mmc_error,a);
UART1_WRITE_TEXT('end second FAT32_Write----');
UART1_WRITE_TEXT(a);
uart1_write (cr_);
uart1_write (lf_);
c:=0;
c1:=45;
while (pinb.b0=1) do
begin
porta.b7:=0;
SPDR:=c; // this instruction on atmega32 with spi configured in
//master mode transmit a byte on MOSI pin and receive a
//byte in spdr register, when I add this instruction the
// FAT32_Write instruction go bad and give error=255

DELAY_US(50);
PORTa.b7:=1;
c1:=spdr;
memcpy(@bufferr+c,@c1,1);
c:=c+1;
porta.b7:=0;
SPDR:=c; // this instruction on atmega32 with spi configured in
//master mode transmit a byte on MOSI pin and receive a
//byte in spdr register, when I add this instruction the
// FAT32_Write instruction go bad and give error=255

DELAY_US(50);
PORTa.b7:=1;
c1:=spdr;
memcpy(@bufferr+c,@c1,1);
c:=c+1;
DELAY_US(1);
if (c=32) then
begin
c:=0;
mmc_error:=1;
while ((mmc_error<>0) and (pinb.b0=1)) do
begin
UART1_WRITE_TEXT('start FAT32_Write ----');
porta.b1:=1;
mmc_error:=FAT32_Write(fHandle , @bufferr, 32) ;
porta.b1:=0;
ByteToStr(mmc_error,a);
UART1_WRITE_TEXT('end FAT32_Write----');
UART1_WRITE_TEXT(a);
uart1_write (cr_);
uart1_write (lf_);
delay_ms(700);
end;
end;
end;
mmc_error:=1;
while (mmc_error<>0) do
begin
UART1_WRITE_TEXT('start FAT32_Close-----');
mmc_error:=FAT32_Close(fhandle);
ByteToStr(mmc_error,a);
UART1_WRITE_TEXT(a);
uart1_write (cr_);
end;
UART1_WRITE_TEXT('end FAT32_Close-----');
end.
Attachments
THIS IS THE OUTPUT ON A SERIAL RS232 PORT
THIS IS THE OUTPUT ON A SERIAL RS232 PORT
hyperterminal.JPG (91.76 KiB) Viewed 3733 times
« Tutti gli esseri umani nascono liberi ed eguali in dignità e diritti.
Essi sono dotati di ragione e di coscienza e devono agire gli uni verso gli altri in spirito di fratellanza. »

User avatar
marina.petrovic
Posts: 2986
Joined: 18 Apr 2013 08:11

Re: libstock fat32 library and SPI use for other communicati

#4 Post by marina.petrovic » 13 May 2014 16:45

Hi,

I believe that I answered you on this question via e-mail, so we can continue our correspondence there.

Best regards,
Marina

robfaz
Posts: 31
Joined: 26 Apr 2009 13:20
Location: Italia

Re: libstock fat32 library and SPI use for other communicati

#5 Post by robfaz » 13 May 2014 18:10

I know well especially the spi onboard functions on atmel atmegaavr microcontroller and i've used sucessfully that setting of spdr in many other projects.
If i write a byte in spdr register, as you can read on datasheet, start the trasmission on a spi interface and after the transmission i can read what i've received during transmission.
Attachments
spdr.JPG
spdr.JPG (49.11 KiB) Viewed 3700 times
« Tutti gli esseri umani nascono liberi ed eguali in dignità e diritti.
Essi sono dotati di ragione e di coscienza e devono agire gli uni verso gli altri in spirito di fratellanza. »

robfaz
Posts: 31
Joined: 26 Apr 2009 13:20
Location: Italia

Re: libstock fat32 library and SPI use for other communicati

#6 Post by robfaz » 09 Jun 2014 21:39

Marina il ook the solution, in easy words is this: for work well before use a spi interface you must disable the end of spi trasmission interrupt use spi interface and reenable it before use a fat32 library.
bye bye
« Tutti gli esseri umani nascono liberi ed eguali in dignità e diritti.
Essi sono dotati di ragione e di coscienza e devono agire gli uni verso gli altri in spirito di fratellanza. »

robfaz
Posts: 31
Joined: 26 Apr 2009 13:20
Location: Italia

Re: libstock fat32 library and SPI use for other communicati

#7 Post by robfaz » 09 Jun 2014 23:32

oldspie:=SPIE_bit; //save the flag that enable spi end trasmission interrupt
portb.b3:=0;// slave select to low
spie_bit:=0;//disable spi end trasmission interrupt
spdr:=%01010101;//write spdr to start trasmission
delay_us(30);
c1:=SPSR;//read on datasheet of atmega 8
c1:=spdr;//save the received value
spdr:=%10101010;// start another trasmission
delay_us(30);
portb.b3:=1;//slave select to high
c2:=SPSR;//read on datasheet of atmega 8
c2:=spdr;//save the received value
SPIE_bit:=oldspie;//restore the saved value of the flag that enable spi end trasmission interrupt
bytetostr(c1,a);
memcpy(@bufferr+c,@a,3);
c:=c+3;
bytetostr(c2,a);
memcpy(@bufferr+c,@a,3);
c:=c+3;
portb.B2:=0;//dati letti 8 pd4
c1:=0;
« Tutti gli esseri umani nascono liberi ed eguali in dignità e diritti.
Essi sono dotati di ragione e di coscienza e devono agire gli uni verso gli altri in spirito di fratellanza. »

Post Reply

Return to “mikroPascal PRO for AVR General”