//===========================================================================
//文件名称：task_write.c
//功能概要：定义task_write任务
//版权所有：苏州大学飞思卡尔嵌入式中心(sumcu.suda.edu.cn)
//===========================================================================
#include "app_inc.h"

//===========================================================================
//函数名称：task_write
//参数说明：无意义
//函数返回：无
//功能概要：打开信号量的连接，等待sem.write，sem.index信号量，读完之后，释放sem.read信号量
//===========================================================================      
void task_write(uint_32 initial_data)
{
   pointer SEM_WRITE_ptr;
   pointer SEM_READ_ptr;
   pointer SEM_INDEX_ptr;
   //打开和信号量的连接
   _sem_open("SEM_WRITE", &SEM_WRITE_ptr);              
   _sem_open("SEM_INDEX", &SEM_INDEX_ptr);
   _sem_open("SEM_READ", &SEM_READ_ptr);
   while (TRUE) 
   {
	  printf("\nTask %lX is waiting to write...",_task_get_id());
	  //等待写信号量 
	   _sem_wait(SEM_WRITE_ptr, 0);                 
      //等待共享区访问信号量 
	  _sem_wait(SEM_INDEX_ptr, 0);
      fifo.DATA[fifo.WRITE_INDEX++] = _task_get_id();
      printf("\nTask %lX's writing has been completed",_task_get_id());
      if (fifo.WRITE_INDEX >= ARRAY_SIZE) 
      {
         fifo.WRITE_INDEX = 0;
      }
      //释放共享区访问信号量
      _sem_post(SEM_INDEX_ptr);
      //释放读信号量
      _sem_post(SEM_READ_ptr);
      _time_delay(1500);
   }
}

