Forum Discussion
Altera_Forum
Honored Contributor
15 years agoHow to use the mutex
Hello to everyone i am developing an application with two processor, one is using pure C code and the other one has an uCLinux running in it. I want them to share the same onchip memory so they...
Altera_Forum
Honored Contributor
15 years agoThanks for your post dsl. So basically i connect both CPU Data masters to a shared memory and to the mutex slave in QSys.
Then i have to write the C Code in each CPU, my code looks like this: CPU1:
/* get the mutex device handle */
alt_mutex_dev* mutex = altera_avalon_mutex_open( "/dev/mutex" );
/* acquire the mutex, setting the value to one */
while(1){
altera_avalon_mutex_lock( mutex, 1 );
if(altera_avalon_mutex_is_mine(mutex))
{
IOWR_32DIRECT(SHARED_MEM_BASE, 0x00, 0x54);
IOWR_32DIRECT(SHARED_MEM_BASE, 0x04, 0x64);
alt_printf("%x\n", IORD_32DIRECT(SHARED_MEM_BASE, 0x00));
alt_printf("%x\n", IORD_32DIRECT(SHARED_MEM_BASE, 0x04));
}
altera_avalon_mutex_unlock( mutex );
}
CPU2:
/* get the mutex device handle */
alt_mutex_dev* mutex = altera_avalon_mutex_open( "/dev/mutex" );
/* acquire the mutex, setting the value to one */
while(1){
altera_avalon_mutex_lock( mutex, 1 );
if(altera_avalon_mutex_is_mine(mutex))
{
IOWR_32DIRECT(SHARED_MEM_BASE, 0x00, 0x04);
IOWR_32DIRECT(SHARED_MEM_BASE, 0x04, 0x14);
alt_printf("%x\n", IORD_32DIRECT(SHARED_MEM_BASE, 0x00));
alt_printf("%x\n", IORD_32DIRECT(SHARED_MEM_BASE, 0x04));
}
altera_avalon_mutex_unlock( mutex );
}
Both are writing and reading from the same mem adress and showing that value to the terminal. And both are showing the right value Is my project correct? (i want both processors to communicate between them using a shared memory)