<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7816989781989773186</id><updated>2011-04-21T19:01:29.395-07:00</updated><title type='text'>Mr. Pichead's World</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://pichead.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7816989781989773186/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://pichead.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Pichead K.</name><uri>http://www.blogger.com/profile/02596972453549150835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7816989781989773186.post-2767048117073410479</id><published>2007-01-19T00:49:00.000-08:00</published><updated>2007-11-12T05:49:34.350-08:00</updated><title type='text'>Hi</title><content type='html'>&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_Yk-QVqcI_z4/RzhZvJDh2bI/AAAAAAAAABY/L19eclcqk3g/s1600-h/Naruto.jpg"&gt;&lt;img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;" src="http://4.bp.blogspot.com/_Yk-QVqcI_z4/RzhZvJDh2bI/AAAAAAAAABY/L19eclcqk3g/s320/Naruto.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5131950441808124338" /&gt;&lt;/a&gt;&lt;br /&gt;this is me ... and i prefer to say hello world by following program :&lt;br /&gt;&lt;span style="font-size:78%;"&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;#include &lt;string.h&gt;&lt;br /&gt;#include "sys/alt_alarm.h"&lt;br /&gt;#include "system.h"&lt;br /&gt;#include "nios2.h"&lt;br /&gt;#include "altera_avalon_mutex.h"&lt;br /&gt;&lt;br /&gt;#define MESSAGE_WAITING 1&lt;br /&gt;#define NO_MESSAGE 0&lt;br /&gt;&lt;br /&gt;#define LOCK_SUCCESS 0&lt;br /&gt;#define LOCK_FAIL 1&lt;br /&gt;&lt;br /&gt;#define MESSAGE_BUFFER_BASE MESSAGE_BUFFER_RAM_BASE&lt;br /&gt;&lt;br /&gt;#define FIRST_LOCK 1 /* for testing only */&lt;br /&gt;#define ERROR_OPENED_INVALID_MUTEX 1 /* for testing only */&lt;br /&gt;#define ERROR_ALLOWED_ACCESS_WITHOUT_OWNING_MUTEX 2 /* for testing only */&lt;br /&gt;#define ERROR_COULDNT_OPEN_MUTEX 3 /* for testing only */&lt;br /&gt;&lt;br /&gt;#define MS_DELAY 1000&lt;br /&gt;&lt;br /&gt;// Message buffer structure&lt;br /&gt;typedef struct {&lt;br /&gt;  char flag;&lt;br /&gt;  char buf[100];&lt;br /&gt;} message_buffer_struct;&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;  // Pointer to our mutex device&lt;br /&gt;  alt_mutex_dev* mutex = NULL;&lt;br /&gt;&lt;br /&gt;  // Local variables&lt;br /&gt;  unsigned int id;&lt;br /&gt;  unsigned int value;&lt;br /&gt;  unsigned int count = 0;&lt;br /&gt;  unsigned int ticks_at_last_message;&lt;br /&gt; &lt;br /&gt;  char got_first_lock = 0; /* for testing only */&lt;br /&gt;  unsigned int error_code = 0; /* for testing only */&lt;br /&gt; &lt;br /&gt;  message_buffer_struct *message;&lt;br /&gt;&lt;br /&gt;  // Get our processor ID (add 1 so it matches the cpu name in SOPC Builder)&lt;br /&gt;  NIOS2_READ_CPUID(id);&lt;br /&gt;  id += 1;&lt;br /&gt; &lt;br /&gt;  // Value can be any non-zero value&lt;br /&gt;  value = 1;&lt;br /&gt;&lt;br /&gt;  // Initialize the message buffer location&lt;br /&gt;  message = (message_buffer_struct*)MESSAGE_BUFFER_BASE;&lt;br /&gt; &lt;br /&gt;  // We'll try to open the wrong mutex here and hope it's not successful.&lt;br /&gt;  mutex = altera_avalon_mutex_open("/dev/wrong_device_name"); /* for testing only */&lt;br /&gt;  if (mutex != NULL) /* for testing only */&lt;br /&gt;  {&lt;br /&gt;    // Whoops, opening the invalid mutex was successful.&lt;br /&gt;    error_code = ERROR_OPENED_INVALID_MUTEX; /* for testing only */&lt;br /&gt;    goto error; /* for testing only */&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  // Okay, now we'll open the real mutex&lt;br /&gt;  // It's not actually a mutex to share the jtag_uart, but to share a message&lt;br /&gt;  // buffer which CPU1 is responsible for reading and printing to the jtag_uart.&lt;br /&gt;  mutex = altera_avalon_mutex_open(MESSAGE_BUFFER_MUTEX_NAME);&lt;br /&gt;&lt;br /&gt;  // We'll use the system clock to keep track of our delay time.&lt;br /&gt;  // Here we initialize delay tracking variable.&lt;br /&gt;  ticks_at_last_message = alt_nticks();&lt;br /&gt;&lt;br /&gt;  if (mutex)&lt;br /&gt;  {&lt;br /&gt;              // If the mutex has never been locked, clear the message flag.&lt;br /&gt;              // We are safe to use the non-blocking "trylock" function here&lt;br /&gt;              // because if we dont get the mutex on the first shot, it means&lt;br /&gt;              // someone else already has it and we clearly arent going to be&lt;br /&gt;              // the first to release it anyway.&lt;br /&gt;              if(altera_avalon_mutex_trylock(mutex, value) == LOCK_SUCCESS) /* for testing only */&lt;br /&gt;              {&lt;br /&gt;                if (altera_avalon_mutex_first_lock(mutex) == FIRST_LOCK) /* for testing only */&lt;br /&gt;                {&lt;br /&gt;                  message-&gt;flag = NO_MESSAGE; /* for testing only */&lt;br /&gt;                  got_first_lock = 1; /* for testing only */&lt;br /&gt;                }&lt;br /&gt;                altera_avalon_mutex_unlock(mutex); /* for testing only */&lt;br /&gt;              }&lt;br /&gt;           &lt;br /&gt;    while(1)&lt;br /&gt;    {&lt;br /&gt;      // See if it's time to send a message yet&lt;br /&gt;      if (alt_nticks() &gt;= (ticks_at_last_message + ((alt_ticks_per_second() * (MS_DELAY)) / 1000)))&lt;br /&gt;      {&lt;br /&gt;        ticks_at_last_message = alt_nticks();&lt;br /&gt;&lt;br /&gt;        // Try and aquire the mutex (non-blocking).&lt;br /&gt;        if(altera_avalon_mutex_trylock(mutex, value) == LOCK_SUCCESS)&lt;br /&gt;        {&lt;br /&gt;          // Just make sure we own the mutex&lt;br /&gt;          if(altera_avalon_mutex_is_mine(mutex)) /* for testing only */&lt;br /&gt;          {&lt;br /&gt;            // Check if the message buffer is empty&lt;br /&gt;            if(message-&gt;flag == NO_MESSAGE)&lt;br /&gt;            {&lt;br /&gt;              count++;&lt;br /&gt;              // If we were the first to lock the mutex, say so in our first message.&lt;br /&gt;              if (got_first_lock) /* for testing only */&lt;br /&gt;              {&lt;br /&gt;                sprintf(message-&gt;buf, "FIRST LOCK - Message from CPU %d.  Number sent: %d\n", id, count); /* for testing only */&lt;br /&gt;                got_first_lock = 0; /* for testing only */&lt;br /&gt;              }&lt;br /&gt;              else&lt;br /&gt;              {&lt;br /&gt;                sprintf(message-&gt;buf, "Message from CPU %d.  Number sent: %d\n", id, count);&lt;br /&gt;              }&lt;br /&gt;              // Set the flag that a message has been put in the buffer.&lt;br /&gt;              message-&gt;flag = MESSAGE_WAITING;&lt;br /&gt;            }&lt;br /&gt;          }&lt;br /&gt;          else /* for testing only */&lt;br /&gt;          {&lt;br /&gt;            error_code = ERROR_ALLOWED_ACCESS_WITHOUT_OWNING_MUTEX; /* for testing only */&lt;br /&gt;            goto error; /* for testing only */&lt;br /&gt;          }&lt;br /&gt;          // Release the mutex&lt;br /&gt;          altera_avalon_mutex_unlock(mutex);&lt;br /&gt;        }&lt;br /&gt;      }&lt;br /&gt;     &lt;br /&gt;      // If we are CPU1, check the message buffer&lt;br /&gt;      // and if there's a message, print it to stdout.&lt;br /&gt;//      if(id == 1)&lt;br /&gt;#ifdef JTAG_UART_NAME&lt;br /&gt;      {&lt;br /&gt;        if(message-&gt;flag == MESSAGE_WAITING)&lt;br /&gt;        {&lt;br /&gt;          // We dont really need to lock the mutex here since the if(id == 1) statement&lt;br /&gt;          // assures we are the only CPU grabbing messages out of the buffer, but&lt;br /&gt;          // we'll do it anyway to test the blocking lock routine.&lt;br /&gt;          altera_avalon_mutex_lock(mutex, value);  /* for testing only */&lt;br /&gt;&lt;br /&gt;          // Just make sure we own the mutex&lt;br /&gt;          if(altera_avalon_mutex_is_mine(mutex)) /* for testing only */&lt;br /&gt;          {&lt;br /&gt;            printf("%s", message-&gt;buf);&lt;br /&gt;            message-&gt;flag = NO_MESSAGE;&lt;br /&gt;          }&lt;br /&gt;          else /* for testing only */&lt;br /&gt;          {&lt;br /&gt;            error_code = ERROR_ALLOWED_ACCESS_WITHOUT_OWNING_MUTEX; /* for testing only */&lt;br /&gt;            goto error; /* for testing only */&lt;br /&gt;          }&lt;br /&gt;          // Release the Mutex&lt;br /&gt;          altera_avalon_mutex_unlock(mutex); /* for testing only */&lt;br /&gt;        }&lt;br /&gt;      }&lt;br /&gt;#endif /* JTAG_UART_NAME */     &lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;  else /* for testing only */&lt;br /&gt;  {&lt;br /&gt;            error_code = ERROR_COULDNT_OPEN_MUTEX; /* for testing only */&lt;br /&gt;            goto error; /* for testing only */&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;error: /* for testing only */&lt;br /&gt;  return(error_code); /* for testing only */&lt;br /&gt;//          return(0);&lt;br /&gt;}&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7816989781989773186-2767048117073410479?l=pichead.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://pichead.blogspot.com/feeds/2767048117073410479/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7816989781989773186&amp;postID=2767048117073410479' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7816989781989773186/posts/default/2767048117073410479'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7816989781989773186/posts/default/2767048117073410479'/><link rel='alternate' type='text/html' href='http://pichead.blogspot.com/2007/01/hi.html' title='Hi'/><author><name>Pichead K.</name><uri>http://www.blogger.com/profile/02596972453549150835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_Yk-QVqcI_z4/RzhZvJDh2bI/AAAAAAAAABY/L19eclcqk3g/s72-c/Naruto.jpg' height='72' width='72'/><thr:total>0</thr:total></entry></feed>
