Sunday, August 3, 2008

Mutex


Priority arrangement
HH: RS485 Rx handler
H: Voltage control
M: Peripheral
L: SMR


void
COMM_Init()
{
Create mutex for RS485 driver.
Create a task, named RS485 Rx Handler.
}


bool
RS485_TxHandler(byte *pPacket, bool waitSlave, portTypeTick delay )
{
if ( mutex is not available )
{
delay;
if ( mutex is still not available ) return false;
}

if ( waitSlave ) resume task, RS485 Rx Handler.
......
.....
other codes.
....

release the mutex.
}


void
Task_RS485_RxHandler
()
{
byte c;
Suspend itselt;

for(;;)
{
Delay 5ms.
while( RS485_GetByte( &c, delay_2ms ) )
{
if ( Get a legal RS485 Packet ) {
Signal semaphore, SEM_RS485_Arrive_Packet;
break;
}
}
Suspend itself.
}
}


Binary Semaphore : 用於Task之間的同步
Mutex : (MUTual EXclusion semaphores), are used by tasks to gain exclusive access to resources. A mutex is used by App to reduce the priority inversion problem.

No comments: