/*-------------------------------------------
| Name:OS_GetMailCond
| Description:
| Parameters:
| Return Type:
| Comments:
| See:
---------------------------------------------*/
char OS_GetMailCond (OS_MAILBOX* pMB, void* Result){
   
   
   //interrupt protection
   ENTER_INTERRUPT();

   WaitForSingleObject(pMB->hSemMB,INFINITE);
  
   if(pMB->pRead==pMB->pWrite) {
      ReleaseSemaphore(pMB->hSemMB,1,NULL);

      //
      LEAVE_INTERRUPT();
      return 1;
   }
   
   memcpy(Result,pMB->pRead,pMB->sizeofMsg);

   if(pMB->pRead == pMB->pEnd){
      pMB->pRead = pMB->pData;
   }
   else{
      pMB->pRead=pMB->pRead+pMB->sizeofMsg;
   }

   ReleaseSemaphore(pMB->hSemMB,1,NULL);

   //
   LEAVE_INTERRUPT();

   return 0;
}
/*-------------------------------------------
| Name:OS_GetMailTimed
| Description:
| Parameters:
| Return Type:
| Comments:
| See:
---------------------------------------------*/
char OS_GetMailTimed (OS_MAILBOX* pMB, void* Result,int TimeOut){

   int end_time = 0, start_time = 0;
   
   while(pMB->pRead==pMB->pWrite)
      if(WaitForSingleObject(pMB->hEvtMB,TimeOut)==WAIT_TIMEOUT)
         return 1;

   //
   //interrupt protection
   ENTER_INTERRUPT();

   //
   if(WaitForSingleObject(pMB->hSemMB,TimeOut)==WAIT_TIMEOUT)
      return 1;
   
   memcpy(Result,pMB->pRead,pMB->sizeofMsg);

   if(pMB->pRead == pMB->pEnd){
      pMB->pRead = pMB->pData;
   }
   else{
      pMB->pRead=pMB->pRead+pMB->sizeofMsg;
   }
  
   pMB->dwThreadId=0;
   ReleaseSemaphore(pMB->hSemMB,1,NULL);

   //
   LEAVE_INTERRUPT();

   return 0;
}
/*-------------------------------------------
| Name:OS_GetMail
| Description:
| Parameters:
| Return Type:
| Comments:
| See:
---------------------------------------------*/
void OS_GetMail (OS_MAILBOX* pMB, void* Result){

   while(pMB->pRead==pMB->pWrite)
      WaitForSingleObject(pMB->hEvtMB,INFINITE);

   //
   //interrupt protection
   ENTER_INTERRUPT();

   //
   WaitForSingleObject(pMB->hSemMB,INFINITE);
   
   memcpy(Result,pMB->pRead,pMB->sizeofMsg);

   if(pMB->pRead == pMB->pEnd){
      pMB->pRead = pMB->pData;
   }
   else{
      pMB->pRead=pMB->pRead+pMB->sizeofMsg;
   }
  
   pMB->dwThreadId=0;
   ReleaseSemaphore(pMB->hSemMB,1,NULL);

   //
   LEAVE_INTERRUPT();
 
}
Example #4
0
/**
 * swithc thread out the interrupt
 * 
 * @author LXZ (2014/11/8)
 * 
 * @param rt_uint32_t from 
 * @param rt_uint32_t to 
 */
void rt_hw_context_switch_interrupt(rt_uint32_t from, rt_uint32_t to)
{
    if (rt_thread_switch_interrupt_flag == 0)
    {
        rt_thread_switch_interrupt_flag = 1;
        rt_interrupt_from_thread = from;
    }

    rt_interrupt_to_thread = to;
    ENTER_INTERRUPT();
}
Example #5
0
/**
 * switch to the first thread,it just call one time
 * 
 * @author LXZ (2014/11/8)
 * 
 * @param rt_uint32_t to 
 */
void rt_hw_context_switch_to(rt_uint32_t to)
{

    rt_interrupt_from_thread = 0;
    rt_interrupt_to_thread = to;
    rt_thread_switch_interrupt_flag = 1;
    /* enable interrupt*/
    _IEN( _ICU_SWINT ) = 1;

    /*clear the interrupt flag*/
    _IR( _ICU_SWINT ) = 0;
    _IPR( _ICU_SWINT ) = MAX_SYSCALL_INTERRUPT_PRIORITY + 1;

    /*touch the software interrupt*/
    ENTER_INTERRUPT();
    /*wait for first thread start up*/
    while(1);
}
/*-------------------------------------------
| Name:OS_PutMailCond
| Description:
| Parameters:
| Return Type:
| Comments:
| See:
---------------------------------------------*/
char OS_PutMailCond (OS_MAILBOX* pMB, void* pmail){

   char * pTmpWrite;

   if(!pMB)return 1;

   //interrupt protection
   ENTER_INTERRUPT();

   WaitForSingleObject(pMB->hSemMB,INFINITE);


   if(pMB->pWrite == pMB->pEnd){
      pTmpWrite = pMB->pData;
   }
   else{
      pTmpWrite=pMB->pWrite+pMB->sizeofMsg;
   }

   if(pTmpWrite==pMB->pRead) {
      //
      ReleaseSemaphore(pMB->hSemMB,1,NULL);
      //
      LEAVE_INTERRUPT();
      return 1;
   }

   memcpy(pMB->pWrite,pmail,pMB->sizeofMsg);

   pMB->pWrite=pTmpWrite;

   SetEvent(pMB->hEvtMB);
   /*
   if(pMB->dwThreadId){
      PostThreadMessage(pMB->dwThreadId,OS_MAIL_POSTED,0L,0L);
   }*/

   ReleaseSemaphore(pMB->hSemMB,1,NULL);

   //
   LEAVE_INTERRUPT();
   
   return 0;
}
/*-------------------------------------------
| Name:OS_ClearMB
| Description:
| Parameters:
| Return Type:
| Comments:
| See:
---------------------------------------------*/
void OS_ClearMB (OS_MAILBOX* pMB){

   if(!pMB)return;

   //interrupt protection
   ENTER_INTERRUPT();

   //
   WaitForSingleObject(pMB->hSemMB,INFINITE);

   pMB->r      = 0;
   pMB->w      = 0;

   pMB->pRead  = pMB->pData;
   pMB->pWrite = pMB->pData;

   ReleaseSemaphore(pMB->hSemMB,1,NULL);

   //
   LEAVE_INTERRUPT();
   
}