bool WDRV_SemInit(OSAL_SEM_HANDLE_TYPE *SemID) { if (OSAL_SEM_Create(SemID, OSAL_SEM_TYPE_COUNTING, 10, 0) == OSAL_RESULT_TRUE) return true; else return false; }
static __inline__ bool __attribute__((always_inline)) _UserGblLockCreate(void) { // create the shared Data Lock return OSAL_SEM_Create(&sSysTmrObject.userSem, OSAL_SEM_TYPE_BINARY, 1, 1) == OSAL_RESULT_TRUE; }
DRV_HANDLE DRV_USART_Open ( const SYS_MODULE_INDEX drvIndex, const DRV_IO_INTENT ioIntent ) { DRV_USART_CLIENT_OBJ *clientObj; DRV_USART_OBJ *dObj; unsigned int iClient; if (drvIndex >= DRV_USART_INSTANCES_NUMBER) { /* Invalid driver index */ SYS_DEBUG(0, "Invalid Driver Instance"); return (DRV_HANDLE_INVALID); } dObj = &gDrvUSARTObj[drvIndex]; if((dObj->status != SYS_STATUS_READY) || (dObj->inUse == false)) { /* The USART module should be ready */ SYS_DEBUG(0, "Was the driver initialized?"); return DRV_HANDLE_INVALID; } if(dObj->isExclusive) { /* This means the another client has opened the driver in exclusive mode. The driver cannot be opened again */ SYS_DEBUG(0, "Driver already opened exclusively"); return ( DRV_HANDLE_INVALID ) ; } if((dObj->nClients > 0) && (ioIntent & DRV_IO_INTENT_EXCLUSIVE)) { /* This means the driver was already opened and another driver was trying to open it exclusively. We cannot give exclusive access in this case */ SYS_DEBUG(0, "Driver already opened. Cannot be opened exclusively"); return(DRV_HANDLE_INVALID); } /* Grab client object mutex here */ if(OSAL_MUTEX_Lock(gDrvUSARTCommonDataObj.mutexClientObjects, OSAL_WAIT_FOREVER) == OSAL_RESULT_TRUE) { /* Enter here only if the lock was obtained (appplicable in RTOS only). If the mutex lock fails due to time out then this code does not get executed */ for(iClient = 0; iClient != DRV_USART_CLIENTS_NUMBER; iClient ++) { if(!gDrvUSARTClientObj[iClient].inUse) { /* This means we have a free client object to use */ clientObj = &gDrvUSARTClientObj[iClient]; clientObj->inUse = true; /* We have found a client object. Release the mutex */ OSAL_ASSERT((OSAL_MUTEX_Unlock(gDrvUSARTCommonDataObj.mutexClientObjects)), "Unable to unlock clients objects routine mutex"); clientObj->hDriver = dObj; /* In a case where the driver is configured for polled and bare metal operation, it will not suppot blocking operation */ clientObj->ioIntent = (ioIntent | _DRV_USART_ALWAYS_NON_BLOCKING); clientObj->eventHandler = NULL; clientObj->context = NULL; clientObj->error = DRV_USART_ERROR_NONE; if(ioIntent & DRV_IO_INTENT_EXCLUSIVE) { /* Set the driver exclusive flag */ dObj->isExclusive = true; } dObj->nClients ++; /* Create the semaphores */ OSAL_ASSERT(((OSAL_SEM_Create(&(clientObj->semReadDone), OSAL_SEM_TYPE_COUNTING, 1, 0)) == OSAL_RESULT_TRUE), "Unable to create client read done semaphore"); OSAL_ASSERT(((OSAL_SEM_Create(&(clientObj->semWriteDone), OSAL_SEM_TYPE_COUNTING, 1, 0)) == OSAL_RESULT_TRUE), "Unable to create client write done semaphore"); /* Update the client status */ clientObj->status = DRV_USART_CLIENT_STATUS_READY; return ((DRV_HANDLE) clientObj ); } } /* Could not find a client object. Release the mutex and return with an invalid handle. */ OSAL_ASSERT((OSAL_MUTEX_Unlock(gDrvUSARTCommonDataObj.mutexClientObjects)), "Unable to unlock clients objects routine mutex"); } /* If we have reached here, it means either we could not find a spare client object or the mutex timed out in a RTOS environment. */ return DRV_HANDLE_INVALID; }