Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Allow RTOS CPU yielding #8

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions mcb.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ static void Mcb_BlockingGetInfo(Mcb_TInst* ptInst, Mcb_TInfoMsg* pMcbInfoMsg)
pMcbInfoMsg->eStatus = Mcb_IntfGetInfo(&ptInst->tIntf, pMcbInfoMsg->u16Node, pMcbInfoMsg->u16Addr,
(uint16_t*)&pMcbInfoMsg->tInfoMsgData, &pMcbInfoMsg->u16Size);

Mcb_RelinquishCPU();

if ((Mcb_GetMillis() - u32Millis) > ptInst->u32Timeout)
{
pMcbInfoMsg->eStatus = MCB_GETINFO_ERROR;
Expand All @@ -199,6 +201,8 @@ static void Mcb_BlockingGetInfo(Mcb_TInst* ptInst, Mcb_TInfoMsg* pMcbInfoMsg)

do
{
Mcb_RelinquishCPU();

if ((Mcb_GetMillis() - u32Millis) > ptInst->u32Timeout)
{
pMcbInfoMsg->eStatus = MCB_GETINFO_ERROR;
Expand Down Expand Up @@ -231,6 +235,8 @@ static void Mcb_BlockingRead(Mcb_TInst* ptInst, Mcb_TMsg* pMcbMsg)
pMcbMsg->eStatus = Mcb_IntfRead(&ptInst->tIntf, pMcbMsg->u16Node, pMcbMsg->u16Addr,
&pMcbMsg->u16Data[0], &pMcbMsg->u16Size);

Mcb_RelinquishCPU();

if ((Mcb_GetMillis() - u32Millis) > ptInst->u32Timeout)
{
pMcbMsg->eStatus = MCB_READ_ERROR;
Expand All @@ -249,6 +255,8 @@ static void Mcb_BlockingRead(Mcb_TInst* ptInst, Mcb_TMsg* pMcbMsg)

do
{
Mcb_RelinquishCPU();

if ((Mcb_GetMillis() - u32Millis) > ptInst->u32Timeout)
{
pMcbMsg->eStatus = MCB_READ_ERROR;
Expand Down Expand Up @@ -281,6 +289,8 @@ static void Mcb_BlockingWrite(Mcb_TInst* ptInst, Mcb_TMsg* pMcbMsg)
pMcbMsg->eStatus = Mcb_IntfWrite(&ptInst->tIntf, pMcbMsg->u16Node, pMcbMsg->u16Addr,
&pMcbMsg->u16Data[0], &pMcbMsg->u16Size);

Mcb_RelinquishCPU();

if ((Mcb_GetMillis() - u32Millis) > ptInst->u32Timeout)
{
pMcbMsg->eStatus = MCB_WRITE_ERROR;
Expand All @@ -299,6 +309,8 @@ static void Mcb_BlockingWrite(Mcb_TInst* ptInst, Mcb_TMsg* pMcbMsg)

do
{
Mcb_RelinquishCPU();

if ((Mcb_GetMillis() - u32Millis) > ptInst->u32Timeout)
{
pMcbMsg->eStatus = MCB_WRITE_ERROR;
Expand Down Expand Up @@ -447,6 +459,8 @@ void* Mcb_TxMap(Mcb_TInst* ptInst, uint16_t u16Addr, uint16_t u16Sz)
{
ptInst->Mcb_Write(ptInst, &tMcbMsg);

Mcb_RelinquishCPU();

if ((Mcb_GetMillis() - u32Millis) > ptInst->u32Timeout)
{
tMcbMsg.eStatus = MCB_WRITE_ERROR;
Expand Down Expand Up @@ -513,6 +527,8 @@ void* Mcb_RxMap(Mcb_TInst* ptInst, uint16_t u16Addr, uint16_t u16Sz)
{
ptInst->Mcb_Write(ptInst, &tMcbMsg);

Mcb_RelinquishCPU();

if ((Mcb_GetMillis() - u32Millis) > ptInst->u32Timeout)
{
tMcbMsg.eStatus = MCB_WRITE_ERROR;
Expand Down Expand Up @@ -560,6 +576,8 @@ uint8_t Mcb_TxUnmap(Mcb_TInst* ptInst)
{
ptInst->Mcb_Write(ptInst, &tMcbMsg);

Mcb_RelinquishCPU();

if ((Mcb_GetMillis() - u32Millis) > ptInst->u32Timeout)
{
tMcbMsg.eStatus = MCB_WRITE_ERROR;
Expand Down Expand Up @@ -605,6 +623,8 @@ uint8_t Mcb_RxUnmap(Mcb_TInst* ptInst)
{
ptInst->Mcb_Write(ptInst, &tMcbMsg);

Mcb_RelinquishCPU();

if ((Mcb_GetMillis() - u32Millis) > ptInst->u32Timeout)
{
tMcbMsg.eStatus = MCB_WRITE_ERROR;
Expand Down Expand Up @@ -648,6 +668,8 @@ void Mcb_UnmapAll(Mcb_TInst* ptInst)
{
ptInst->Mcb_Write(ptInst, &tMcbMsg);

Mcb_RelinquishCPU();

if ((Mcb_GetMillis() - u32Millis) > ptInst->u32Timeout)
{
tMcbMsg.eStatus = MCB_WRITE_ERROR;
Expand Down Expand Up @@ -680,6 +702,8 @@ void Mcb_UnmapAll(Mcb_TInst* ptInst)
{
ptInst->Mcb_Write(ptInst, &tMcbMsg);

Mcb_RelinquishCPU();

if ((Mcb_GetMillis() - u32Millis) > ptInst->u32Timeout)
{
tMcbMsg.eStatus = MCB_WRITE_ERROR;
Expand Down Expand Up @@ -720,6 +744,8 @@ int32_t Mcb_EnableCyclic(Mcb_TInst* ptInst)
{
ptInst->Mcb_Write(ptInst, &tMcbMsg);

Mcb_RelinquishCPU();

if ((Mcb_GetMillis() - u32Millis) > ptInst->u32Timeout)
{
tMcbMsg.eStatus = MCB_WRITE_ERROR;
Expand Down Expand Up @@ -753,6 +779,8 @@ int32_t Mcb_EnableCyclic(Mcb_TInst* ptInst)
{
ptInst->Mcb_Write(ptInst, &tMcbMsg);

Mcb_RelinquishCPU();

if ((Mcb_GetMillis() - u32Millis) > ptInst->u32Timeout)
{
tMcbMsg.eStatus = MCB_WRITE_ERROR;
Expand Down Expand Up @@ -787,6 +815,8 @@ int32_t Mcb_EnableCyclic(Mcb_TInst* ptInst)
{
ptInst->Mcb_Write(ptInst, &tMcbMsg);

Mcb_RelinquishCPU();

if ((Mcb_GetMillis() - u32Millis) > ptInst->u32Timeout)
{
tMcbMsg.eStatus = MCB_WRITE_ERROR;
Expand Down Expand Up @@ -872,6 +902,8 @@ Mcb_ECyclicMode Mcb_GetCyclicMode(Mcb_TInst* ptInst)
{
ptInst->Mcb_Read(ptInst, &tMcbMsg);

Mcb_RelinquishCPU();

if ((Mcb_GetMillis() - u32Millis) > ptInst->u32Timeout)
{
tMcbMsg.eStatus = MCB_READ_ERROR;
Expand Down Expand Up @@ -908,6 +940,8 @@ Mcb_ECyclicMode Mcb_SetCyclicMode(Mcb_TInst* ptInst, Mcb_ECyclicMode eNewCycMode
{
ptInst->Mcb_Write(ptInst, &tMcbMsg);

Mcb_RelinquishCPU();

if ((Mcb_GetMillis() - u32Millis) > ptInst->u32Timeout)
{
tMcbMsg.eStatus = MCB_WRITE_ERROR;
Expand Down
5 changes: 5 additions & 0 deletions mcb_usr.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ __attribute__((weak))uint32_t Mcb_GetMillis(void)
return (uint32_t)0U;
}

__attribute__((weak))void Mcb_RelinquishCPU(void)
{
return;
}

__attribute__((weak))bool Mcb_IntfIsReady(uint16_t u16Id)
{
/** Check if SPI instance is ready for initiate a new transmission */
Expand Down
12 changes: 12 additions & 0 deletions mcb_usr.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ Mcb_IntfCheckCrc(uint16_t u16Id, const uint16_t* pu16Buf, uint16_t u16Sz);
uint32_t
Mcb_GetMillis(void);

/**
* Relinquish (give up / allow others to execute) CPU.
* This is especially useful on RTOS systems which rely on cooperative
* threads in order share the CPU time.
* This routine will be invoked on every iteration if a blocking method
* uses busy waiting (i.e. timeouts).
*
* This should be a no-op on non RTOS systems.
*/
void
Mcb_RelinquishCPU(void);

/**
* Executes a SPI transfer
*
Expand Down