FRAM module and negative numbers

Breakout boards, sensors, other Adafruit kits, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
accmeint
 
Posts: 12
Joined: Thu Apr 13, 2017 2:32 pm

FRAM module and negative numbers

Post by accmeint »

Hello,
I'm working with the 4Mb FRAM module and running the attached code..
main.cpp
(6.72 KiB) Downloaded 5 times
When I run this I get the following output:
1 2 3 feedbe 54cb

I'm not great with bit operations but does anyone know why I can't handle a negative number or a large number, (i.e. 0xfeedbeef).
Thank you!

User avatar
dlleigh
 
Posts: 225
Joined: Wed Jan 30, 2013 8:08 pm

Re: FRAM module and negative numbers

Post by dlleigh »

Looking at the C++ code, the values being returned by FR_READ32 and written by FR_WRITE32 are declared as unsigned int:

Code: Select all

unsigned int FR_READ32(int adr) {

Code: Select all

void FR_WRITE32(int adr, unsigned int data) {
They will never do what you want for negative numbers.

Arduino ints and unsigned ints are only 16-bits long. If you want 32-bit values, you will need to use long or unsigned long.

User avatar
accmeint
 
Posts: 12
Joined: Thu Apr 13, 2017 2:32 pm

Re: FRAM module and negative numbers

Post by accmeint »

Thank you for the reply!! I'm sorry I neglected to say that I'm using a Raspberry Pi, running 32bit Rasbian. A rookie mistake on my part!

User avatar
dlleigh
 
Posts: 225
Joined: Wed Jan 30, 2013 8:08 pm

Re: FRAM module and negative numbers

Post by dlleigh »

The values you pass in the Length variable to SpiWriteAndRead are inconsistent between FR_READ32 and FR_WRITE32.

FR_READ32 appears to send eight bytes via SpiWriteAndRead using a Length parameter with value seven:

Code: Select all

    TRXdata[0] = FR_READ;
    TRXdata[1] = adr >> 8;
    TRXdata[2] = adr;
    TRXdata[3] = 0;
    TRXdata[4] = 0;
    TRXdata[5] = 0;
    TRXdata[6] = 0;
    TRXdata[7] = 0;

    SpiWriteAndRead(0, TRXdata, TRXdata,7, 0);
FR_WRITE32 appears to send seven bytes via SpiWriteAndRead also using a Length parameter with value seven:

Code: Select all

    TRXdata[0] = FR_WRITE;
    TRXdata[1] = adr >> 8; //adr hi
    TRXdata[2] = adr; //adr lo
    TRXdata[3] = data >> 24; //data hi
    TRXdata[4] = data >> 16; //data
    TRXdata[5] = data >> 8; //data
    TRXdata[6] = data; //data lo
    SpiWriteAndRead(0, TRXdata, TRXdata,7, 0);
Is it possible that one of those Length values is incorrect?

User avatar
accmeint
 
Posts: 12
Joined: Thu Apr 13, 2017 2:32 pm

Re: FRAM module and negative numbers

Post by accmeint »

Hello and thank you,
Yes, I looked at those and said "Why 7,?? maybe I'm loosing the sign bit", so I changed both them to 8 and there was no change. BTW, I didn't write this code as you probable have surmised!

User avatar
accmeint
 
Posts: 12
Joined: Thu Apr 13, 2017 2:32 pm

Re: FRAM module and negative numbers

Post by accmeint »

Hello,
I finally got the code to work!! It required some mods to the address specification to work with my FRAM module. Thank you "dlleigh"! your tip about arduino ints/unsigned ints got me going down the right path! Here's my code...
main.cpp
(7.25 KiB) Downloaded 6 times

User avatar
dlleigh
 
Posts: 225
Joined: Wed Jan 30, 2013 8:08 pm

Re: FRAM module and negative numbers

Post by dlleigh »

I'm glad I could help.

Looking at your code, it appears that the FRAM chip you are using wants 32-bit memory addresses instead of 16-bit. Different addressing modes can get tricky.

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Products from Adafruit”