cancel
Showing results for 
Search instead for 
Did you mean: 

f_write without char data types

con3
Senior
Posted on January 23, 2018 at 16:29

Good day everyone,

I have a slight issue, I'm storing data in the form of uint16_t data types.This data will be read in from an ADC and have values of 0 and 1. I'll have an array of 192 of these 16 bit values that I'd effectively like to store in a SDcard. The problem is that I can't directly send these to the sdcard using fwrite as it would be completely unreadable, so I'd need to convert it to a char datatype. 

It seems a little counter-intuitive as char are 8 bit long and I'd need to cast 192 of these 16 bit unsigned integers.

I tried doing a cast to wchar_t and it works perfectly in the code. I can cast a uint16_t value to a wchar_t without it seeming at all computationally expensive, although when I try to send this to f_write and send it out I get complete junk.

Is there any computational inexpensive way that I can get a uint16_t array[192] into a readable format for text or binary files?

Here's a code snippet of what I've used:

uint16_t aDST_Buffer= 1100;

wchar_t Conv;

Conv =(wchar_t)aDST_Buffer;                                             //This gives me Conv = 1100

res1 = f_write(&SDFile,&Conv , sizeof(Conv ), (void *)&wbytes);        // This produces a large amount of junk.

I've seen some examples where they pass the data directly and not the address, I also tried that out of desperation without any success:

uint16_t aDST_Buffer= 1100;

wchar_t Conv;

Conv =(wchar_t)aDST_Buffer;                                             //This gives me Conv = 1100

res1 = f_write(&SDFile,Conv , sizeof(Conv ), (void *)&wbytes);        // This produces a large amount of junk.

It doesn't seem that unicode is supported.

Thanks in advance for any help!

#fatfs #wchar_t #f_write #stm32f7
1 ACCEPTED SOLUTION

Accepted Solutions
Posted on January 23, 2018 at 17:32

Perhaps look at the file in a viewer that can show you the binary content.

1000 as 16-bit words would appear as

E8 03 E8 03 ...

Not printable ASCII words, not human readable. Data is represented in a machine readable form, the byte order would depend on the endian format the machine uses.

int i = 1000;

char buffer[12];

int len = sprintf(buffer, '%d ', i); // ASCII sprintf() or itoa()

res1 = f_write(&SDFile, buffer , len, &wbytes);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

5 REPLIES 5
Posted on January 23, 2018 at 16:57

Review chapters on pointers and structures, basic C stuff

wchar_t Conv;

UINT wbytes; // needs to be 32-bit wide otherwise corruption will occur

res1 = f_write(&SDFile, &Conv , sizeof(Conv ), &wbytes);  // correct form, but super inefficient

uint16_t array[192];

UINT wbytes; // needs to be 32-bit wide

res1 = f_write(&SDFile, &array[0] , sizeof(array), &wbytes);  // write whole array

sizeof(array) // size in bytes

array or &array[0] // base address of array in memory

You need to write a copy of the array that is NOT being updated via DMA in the background

The size of the array should be a multiple of 512 bytes, ideally much larger, otherwise the filesystem will grind and do a lot of unnecessary read/write operations

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 23, 2018 at 17:23

Hi CLive,

Thank you for the reply.

If I write an integer as in the following:

uint16_t array[192];

UINT wbytes; // needs to be 32-bit wide

res1 = f_write(&SDFile, &array[0] , sizeof(array), &wbytes); // write whole array

In my case:

uint16_t wtext [192] = {[0 ... 191] = 1000};

UINT wbytes;

res1 = f_write(&SDFile, &wtext[0], sizeof(wtext), &wbytes);

Then I get the following in the text file:

0690X0000060492QAA.jpg

Full code:

uint16_t wtext [192] = {[0 ... 191] = 1000};

UINT wbytes;

if(FATFS_LinkDriver(&SD_Driver, SDPath)==0){

res = f_mount(&SDFatFS,SDPath,1);

if(res == FR_OK){

res2 =f_open(&SDFile,''hitxt'',FA_OPEN_APPEND | FA_WRITE);

res1 = f_write(&SDFile, &wtext[0], sizeof(wtext), &wbytes);

f_close(&SDFile);

}

}

Although when I write a character I can see the correct data in the text file.

Posted on January 23, 2018 at 17:32

Perhaps look at the file in a viewer that can show you the binary content.

1000 as 16-bit words would appear as

E8 03 E8 03 ...

Not printable ASCII words, not human readable. Data is represented in a machine readable form, the byte order would depend on the endian format the machine uses.

int i = 1000;

char buffer[12];

int len = sprintf(buffer, '%d ', i); // ASCII sprintf() or itoa()

res1 = f_write(&SDFile, buffer , len, &wbytes);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 23, 2018 at 17:38

C:\CT\MSVC>dump test.out

00000000 : E8 03 E8 03 E8 03 E8 03 - E8 03 E8 03 E8 03 E8 03   ................

00000010 : E8 03 E8 03 E8 03 E8 03 - E8 03 E8 03 E8 03 E8 03   ................

00000020 : E8 03 E8 03 E8 03 E8 03 - E8 03 E8 03 E8 03 E8 03   ................

00000030 : E8 03 E8 03 E8 03 E8 03 - E8 03 E8 03 E8 03 E8 03   ................

00000040 : E8 03 E8 03 E8 03 E8 03 - E8 03 E8 03 E8 03 E8 03   ................

00000050 : E8 03 E8 03 E8 03 E8 03 - E8 03 E8 03 E8 03 E8 03   ................

00000060 : E8 03 E8 03 E8 03 E8 03 - E8 03 E8 03 E8 03 E8 03   ................

00000070 : E8 03 E8 03 E8 03 E8 03 - E8 03 E8 03 E8 03 E8 03   ................

00000080 : E8 03 E8 03 E8 03 E8 03 - E8 03 E8 03 E8 03 E8 03   ................

00000090 : E8 03 E8 03 E8 03 E8 03 - E8 03 E8 03 E8 03 E8 03   ................

000000A0 : E8 03 E8 03 E8 03 E8 03 - E8 03 E8 03 E8 03 E8 03   ................

000000B0 : E8 03 E8 03 E8 03 E8 03 - E8 03 E8 03 E8 03 E8 03   ................

000000C0 : E8 03 E8 03 E8 03 E8 03 - E8 03 E8 03 E8 03 E8 03   ................

000000D0 : E8 03 E8 03 E8 03 E8 03 - E8 03 E8 03 E8 03 E8 03   ................

000000E0 : E8 03 E8 03 E8 03 E8 03 - E8 03 E8 03 E8 03 E8 03   ................

000000F0 : E8 03 E8 03 E8 03 E8 03 - E8 03 E8 03 E8 03 E8 03   ................

00000100 : E8 03 E8 03 E8 03 E8 03 - E8 03 E8 03 E8 03 E8 03   ................

00000110 : E8 03 E8 03 E8 03 E8 03 - E8 03 E8 03 E8 03 E8 03   ................

00000120 : E8 03 E8 03 E8 03 E8 03 - E8 03 E8 03 E8 03 E8 03   ................

00000130 : E8 03 E8 03 E8 03 E8 03 - E8 03 E8 03 E8 03 E8 03   ................

00000140 : E8 03 E8 03 E8 03 E8 03 - E8 03 E8 03 E8 03 E8 03   ................

00000150 : E8 03 E8 03 E8 03 E8 03 - E8 03 E8 03 E8 03 E8 03   ................

00000160 : E8 03 E8 03 E8 03 E8 03 - E8 03 E8 03 E8 03 E8 03   ................

00000170 : E8 03 E8 03 E8 03 E8 03 - E8 03 E8 03 E8 03 E8 03   ................
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 23, 2018 at 19:07

Thank you Clive,

I completely understand now. I opened it with a binary file viewer and saw the logged data.

Thank you for always helping!