Hello,
I'm working with the 4Mb FRAM module and running the attached code..
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!
FRAM module and negative numbers
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- dlleigh
- Posts: 225
- Joined: Wed Jan 30, 2013 8:08 pm
Re: FRAM module and negative numbers
Looking at the C++ code, the values being returned by FR_READ32 and written by FR_WRITE32 are declared as unsigned int:
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.
Code: Select all
unsigned int FR_READ32(int adr) {
Code: Select all
void FR_WRITE32(int adr, unsigned int data) {
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.
- accmeint
- Posts: 12
- Joined: Thu Apr 13, 2017 2:32 pm
Re: FRAM module and negative numbers
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!
- dlleigh
- Posts: 225
- Joined: Wed Jan 30, 2013 8:08 pm
Re: FRAM module and negative numbers
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:
FR_WRITE32 appears to send seven bytes via SpiWriteAndRead also using a Length parameter with value seven:
Is it possible that one of those Length values is incorrect?
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);
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);
- accmeint
- Posts: 12
- Joined: Thu Apr 13, 2017 2:32 pm
Re: FRAM module and negative numbers
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!
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!
- accmeint
- Posts: 12
- Joined: Thu Apr 13, 2017 2:32 pm
Re: FRAM module and negative numbers
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...
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...
- dlleigh
- Posts: 225
- Joined: Wed Jan 30, 2013 8:08 pm
Re: FRAM module and negative numbers
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.
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.
Please be positive and constructive with your questions and comments.