static void prvTestAbortingSemaphoreTake( void ) { TickType_t xTimeAtStart; BaseType_t xReturn; SemaphoreHandle_t xSemaphore; #if( configSUPPORT_STATIC_ALLOCATION == 1 ) { static StaticSemaphore_t xSemaphoreBuffer; /* Create the semaphore. Statically allocated memory is used so the creation cannot fail. */ xSemaphore = xSemaphoreCreateBinaryStatic( &xSemaphoreBuffer ); } #else { xSemaphore = xSemaphoreCreateBinary(); } #endif /* Note the time before the delay so the length of the delay is known. */ xTimeAtStart = xTaskGetTickCount(); /* This first delay should just time out. */ xReturn = xSemaphoreTake( xSemaphore, xMaxBlockTime ); if( xReturn != pdFALSE ) { xErrorOccurred = pdTRUE; } prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xMaxBlockTime ); /* Note the time before the delay so the length of the delay is known. */ xTimeAtStart = xTaskGetTickCount(); /* This second delay should be aborted by the primary task half way through. */ xReturn = xSemaphoreTake( xSemaphore, xMaxBlockTime ); if( xReturn != pdFALSE ) { xErrorOccurred = pdTRUE; } prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xHalfMaxBlockTime ); /* Note the time before the delay so the length of the delay is known. */ xTimeAtStart = xTaskGetTickCount(); /* This third delay should just time out again. */ xReturn = xSemaphoreTake( xSemaphore, xMaxBlockTime ); if( xReturn != pdFALSE ) { xErrorOccurred = pdTRUE; } prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xMaxBlockTime ); /* Not really necessary in this case, but for completeness. */ vSemaphoreDelete( xSemaphore ); }
void InitMsgArray() { // 初始化消息数组 for (uint8_t i = 0; i < TOTAL_MSG_NUMBER; ++i) { s_MsgArray[i].Id = MSG_UNUSED; } // 初始化互斥量 //s_ArrayMutex = xSemaphoreCreateBinary(); s_ArrayMutex = xSemaphoreCreateBinaryStatic(&xSemaphoreBuffer); // Default is empty state. Need Give once. xSemaphoreGive(s_ArrayMutex); }
status_t I2C_RTOS_Init(i2c_rtos_handle_t *handle, I2C_Type *base, const i2c_master_config_t *masterConfig, uint32_t srcClock_Hz) { if (handle == NULL) { return kStatus_InvalidArgument; } if (base == NULL) { return kStatus_InvalidArgument; } memset(handle, 0, sizeof(i2c_rtos_handle_t)); #if (configSUPPORT_STATIC_ALLOCATION == 1) handle->mutex = xSemaphoreCreateMutexStatic(&handle->mutexBuffer); #else handle->mutex = xSemaphoreCreateMutex(); #endif if (handle->mutex == NULL) { return kStatus_Fail; } #if (configSUPPORT_STATIC_ALLOCATION == 1) handle->semaphore = xSemaphoreCreateBinaryStatic(&handle->semaphoreBuffer); #else handle->semaphore = xSemaphoreCreateBinary(); #endif if (handle->semaphore == NULL) { vSemaphoreDelete(handle->mutex); return kStatus_Fail; } handle->base = base; I2C_MasterInit(handle->base, masterConfig, srcClock_Hz); I2C_MasterTransferCreateHandle(base, &handle->drv_handle, I2C_RTOS_Callback, (void *)handle); return kStatus_Success; }
static void prvCreateAndDeleteStaticallyAllocatedBinarySemaphores( void ) { SemaphoreHandle_t xSemaphore; /* StaticSemaphore_t is a publicly accessible structure that has the same size and alignment requirements as the real semaphore structure. It is provided as a mechanism for applications to know the size of the semaphore (which is dependent on the architecture and configuration file settings) without breaking the strict data hiding policy by exposing the real semaphore internals. This StaticSemaphore_t variable is passed into the xSemaphoreCreateBinaryStatic() function calls within this function. NOTE: In most usage scenarios now it is faster and more memory efficient to use a direct to task notification instead of a binary semaphore. http://www.freertos.org/RTOS-task-notifications.html */ StaticSemaphore_t xSemaphoreBuffer; /* Create the semaphore. xSemaphoreCreateBinaryStatic() has one more parameter than the usual xSemaphoreCreateBinary() function. The parameter is a pointer to the pre-allocated StaticSemaphore_t structure, which will hold information on the semaphore in an anonymous way. If the pointer is passed as NULL then the structure will be allocated dynamically, just as when xSemaphoreCreateBinary() is called. */ xSemaphore = xSemaphoreCreateBinaryStatic( &xSemaphoreBuffer ); /* The semaphore handle should equal the static semaphore structure passed into the xSemaphoreCreateBinaryStatic() function. */ configASSERT( xSemaphore == ( SemaphoreHandle_t ) &xSemaphoreBuffer ); /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */ prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT ); /* Delete the semaphore again so the buffers can be reused. */ vSemaphoreDelete( xSemaphore ); /* Now do the same using a dynamically allocated semaphore to check the delete function is working correctly in both the static and dynamic allocation cases. */ #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) { xSemaphore = xSemaphoreCreateBinary(); configASSERT( xSemaphore != NULL ); prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT ); vSemaphoreDelete( xSemaphore ); } #endif /* There isn't a static version of the old and deprecated vSemaphoreCreateBinary() macro (because its deprecated!), but check it is still functioning correctly. */ #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) { vSemaphoreCreateBinary( xSemaphore ); /* The macro starts with the binary semaphore available, but the test function expects it to be unavailable. */ if( xSemaphoreTake( xSemaphore, staticDONT_BLOCK ) == pdFAIL ) { xErrorOccurred = pdTRUE; } prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT ); vSemaphoreDelete( xSemaphore ); } #endif }