OpenCores
no use no use 1/1 no use no use
Possible Memory Bug or help adding Malloc
by cstack89 on Jul 24, 2012
cstack89
Posts: 14
Joined: May 17, 2012
Last seen: Dec 5, 2012
I am working on a program where I need to read multiple bytes in over I2C. In my I2C read function I define 2 or more char* to hold the data, but the data from the first byte is overwritten by the second byte. I'm running a bare-metal C program so I wasn't sure if I could use malloc (and if so I am unsure of how to link it to the program). I have pasted my code below and would appreciate any help you can give me.

unsigned char *msb;
unsigned char *lsb;

i2c_master_slave_master_start(0,0x77,0);
i2c_master_slave_write(0,address,0,1);

i2c_master_slave_master_start(0,0x77,1);
i2c_master_slave_master_read(0,0,0,msb);
i2c_master_slave_master_read(0,0,1,lsb);

If I do it like that msb and lsb will have the same value, but if I insert something like unsigned short m = *msb; inbetween the two reads then the values are different (and correct). Any ideas? I am running the code on a DE0-Nano using a slightly modified version of the VM image from the WIKI (gdb and gcc versions have not changed)

Thank you,

Chris
RE: Possible Memory Bug or help adding Malloc
by lenzo on Jul 24, 2012
lenzo
Posts: 2
Joined: Aug 3, 2011
Last seen: Feb 11, 2015
Never use pointer before you assign it a known value.
You define two char type pointer here.
unsigned char* msb;
unsigned char* lsb;

The compiler may set msb = lsb = 0x00. They point at the same memory location!
You can solve the problem in two ways.
NO 1: defined the two variables in the stack. So, no pointer type is needed.
unsigned char msb;
unsigned char lsb;

i2c_master_slave_master_start(0,0x77,0);
i2c_master_slave_write(0,address,0,1);

i2c_master_slave_master_start(0,0x77,1);
i2c_master_slave_master_read(0,0,0,&msb);
i2c_master_slave_master_read(0,0,1,&lsb);
NO 2: defined the two variables in the heap. Malloc is needed.
unsigned char *msb;
unsigned char *lsb;

msb = (char*)malloc (sizeof(char));
lsb = (char*)malloc (sizeof(char));
if(!msb || !lsb ) {
printf("Malloc error!\n");
return -1;
}
i2c_master_slave_master_start(0,0x77,0);
i2c_master_slave_write(0,address,0,1);

i2c_master_slave_master_start(0,0x77,1);
i2c_master_slave_master_read(0,0,0,&msb);
i2c_master_slave_master_read(0,0,1,&lsb);
.........
free(msb);
free(lsb);
RE: Possible Memory Bug or help adding Malloc
by cstack89 on Jul 24, 2012
cstack89
Posts: 14
Joined: May 17, 2012
Last seen: Dec 5, 2012
Thank you for your response, after thinking about it more this morning (I was tired last night) I guessed that they probably have the same memory address.

I would like the option to use the heap, so if possible I would like to avoid number 1.

For number 2 how can I use malloc? What needs to be included to make it available? I tried including malloc.h and stdlib.h, but neither helped. I think I just got an error that said malloc was still undefined but I'm not 100% sure. I also thought that malloc was only available if I was using an operating system?

The other thing I was thinking was to just initalize the variables before I sent passed them to the function.

Something like:
unsigned char* msb = null;
unsigned char* lsb = null;

Would that solve the problem or probably not?

Thank you for the help,

Chris
Makefile help
by cstack89 on Jul 24, 2012
cstack89
Posts: 14
Joined: May 17, 2012
Last seen: Dec 5, 2012
Ok I tried to do a little more digging and I think I am just trying to include the wrong libraries to get malloc.

To be completely honest I'm bad with makefiles and therefore am just using the one located in the SW folder.

I am guessing that I need to add a reference to newlib on this line:

OR32_CFLAGS ?=-g -nostdlib -O2 $(MARCH_FLAGS) \
$(INCLUDE_FLAGS) \
-I$(SW_ROOT)/lib/include \

Looking through the wiki I found some references but am still unsure.
On the newlib toolchain page I found this: -mnewlib
On the GNU toolchain page I found this: -mor32-newlib

Both seem like viable options so which one is right? Or am I completely going the wrong way. Do I need to delete the reference to -nostdlib as well?

Finally assuming one of those flags is correct, which header file to I need to include in my program?

Thank you,

Chris
RE: Possible Memory Bug or help adding Malloc
by cstack89 on Jul 24, 2012
cstack89
Posts: 14
Joined: May 17, 2012
Last seen: Dec 5, 2012
I tried a couple of configurations and unfortunately still no luck.

I tried just setting unsigned char* msb = 0; and not too surprisingly this did not help.

If I edited the make file to remove the -nostdlib flag then I get an error that in brief says:

in _start: multiple definitions of _start
in /or32-elf/bin/ld: section vectors will not fit in region vectors
in /or32-elf/bin/ld: no memory region specified for loadable section .init

And if I add the -mnewlib flag (while the -nostdlib flag is there) it just reports that malloc can't be found.

The last option -mor32-newlib doesn't exist.

I'm guessing that there must be other flags I need to turn off or add, but I'm not sure what they are, so please let me know!

Thanks,

Chris
RE: Possible Memory Bug or help adding Malloc
by stekern on Jul 30, 2012
stekern
Posts: 84
Joined: Apr 28, 2009
Last seen: Nov 10, 2016
Why are you insisting on using malloc to allocate two chars??
If you (for some strange reason) don't want them on the stack,
then just declare them outside the function definition.
Help Linking NewLib
by cstack89 on Jul 31, 2012
cstack89
Posts: 14
Joined: May 17, 2012
Last seen: Dec 5, 2012
It was more just so I knew how it could be done if I needed it for something more complicated in the future, but between a mixture of declaring pointers globally and passing by reference I think I can achieve everything I need without using malloc and free.


That being said I'm still interested in how to use functions available in newlib (like math.h), unless I am misunderstanding and that's not an option?

Thanks,

Chris
RE: Possible Memory Bug or help adding Malloc
by stekern on Jul 31, 2012
stekern
Posts: 84
Joined: Apr 28, 2009
Last seen: Nov 10, 2016
If you have installed a newlib toolchain properly,
this program:
http://pastebin.com/Kj3qPEmp

should compile with:
or32-elf-gcc malloc.c -o malloc

and you should be able to test it with:
or32-elf-sim -f sim.cfg malloc
RE: Possible Memory Bug or help adding Malloc
by cstack89 on Aug 2, 2012
cstack89
Posts: 14
Joined: May 17, 2012
Last seen: Dec 5, 2012
Sorry for the delay, the program compiled fine just like you said. Terminal complains that I don't have or32-elf-sim, and nothing is outputted when I put it onto the board.

If I go back to try using a makefile with Makefile.inc it complains about an undefined reference to printf and malloc.

Can someone please help me generate my own makefile that will compile and output correctly on a board, or help me modify the Makefile.inc so that it works? Or point me in the right direction on how to do it?

Thank you for the help,

Chris
RE: Possible Memory Bug or help adding Malloc
by stekern on Aug 3, 2012
stekern
Posts: 84
Joined: Apr 28, 2009
Last seen: Nov 10, 2016
You will need to link it together with a board support file suitable for your board.
I.e. or32-elf-gcc -mboard=boardname
The default board is or1ksim, that's why it wasn't needed in my example.
More information about the -mboard switch (and how to create a board support file)
here: http://opencores.org/or1k/Newlib#Board_support_details
What board do you have?

RE: Possible Memory Bug or help adding Malloc
by cstack89 on Aug 22, 2012
cstack89
Posts: 14
Joined: May 17, 2012
Last seen: Dec 5, 2012
Sorry for the delay I had a couple of things pop up. You were correct though with the addition of the -mboard line it worked correctly. I am using a Altera De0-Nano and had previously written the board support package.

For my actual program I need to link in with a lot of the different drivers that have been provided. I started the process of creating my own makefile to link in the many files that are needed, but I'm not sure I can create a makefile that complex. Is there anyway to modify the Makefile.inc so that it can find things like malloc? While using the Makefile.inc I'm still stuck where it either can't find malloc or where it finds multiple definitions of _start after i delete the -nostdlib flag.

Thank you for the help,

Chris
RE: Possible Memory Bug or help adding Malloc
by stekern on Aug 23, 2012
stekern
Posts: 84
Joined: Apr 28, 2009
Last seen: Nov 10, 2016
If your original program has the -nostdlib flag it probably provides it's
own _start etc.
Again: are you sure you that you need to use malloc for your project?
RE: Possible Memory Bug or help adding Malloc
by cstack89 on Aug 23, 2012
cstack89
Posts: 14
Joined: May 17, 2012
Last seen: Dec 5, 2012
No I think I can get away without malloc. I do need math functions though, I haven't tested yet but I am assuming if I #include math.h I'm going to have the same problems? I apologize if that's a bad assumption and I'm planning on testing it tonight.
RE: Possible Memory Bug or help adding Malloc
by cstack89 on Aug 23, 2012
cstack89
Posts: 14
Joined: May 17, 2012
Last seen: Dec 5, 2012
It appears it was a bad assumption, I haven't done any real tests but I was able to compile with a sin function in my program. So thank you for your help and I guess I don't need to modify the makefile after all.

Thanks,

Chris
no use no use 1/1 no use no use
© copyright 1999-2026 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.