www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當(dāng)前位置:首頁 > 公眾號精選 > strongerHuang
[導(dǎo)讀]關(guān)注、星標(biāo)公眾號,不錯(cuò)過精彩內(nèi)容 轉(zhuǎn)自:Mculover666 之前給大家分享過關(guān)于CMSIS的內(nèi)容,比如: Cortex-M微控制器軟件接口標(biāo)準(zhǔn)CMSIS詳細(xì)內(nèi)容 CMSIS-DAP和J-Link、ST-Link是什么關(guān)系? 今天繼續(xù)給大家分享由“Mculover666”整理的關(guān)于CMSIS的內(nèi)容。 1. CMSIS-


關(guān)注、星標(biāo)公眾,不錯(cuò)過精彩內(nèi)容

轉(zhuǎn)自:Mculover666


之前給大家分享過關(guān)于CMSIS的內(nèi)容,比如:

接口標(biāo)準(zhǔn)CMSIS詳細(xì)內(nèi)容" tab="innerlink" style="text-decoration: underline;" data-linktype="2" rel="nofollow">Cortex-M微控制器軟件接口標(biāo)準(zhǔn)CMSIS詳細(xì)內(nèi)容

CMSIS-DAP和J-Link、ST-Link是什么關(guān)系?


今天繼續(xù)給大家分享由“Mculover666”整理的關(guān)于CMSIS的內(nèi)容。

1. CMSIS-RTOS API

CMSIS-RTOS API是ARM公司為RTOS內(nèi)核制定的一套通用接口協(xié)議,它提供了一套「標(biāo)準(zhǔn)的API接口」,可以移植到各種各樣的RTOS上,使得上層的軟件、中間件、庫以及其他組件在不同的RTOS之上都可以正常工作。

這套API表現(xiàn)為兩個(gè)文件:cmsis-os.h和cmsis-os.c,也就是說,不同的RTOS內(nèi)核分別用自己的一套東西去適配.c文件中的接口,而用戶只需要調(diào)用.h文件中給出的API編寫應(yīng)用。

本文會列舉性的給出CMSIS-RTOS有哪些API和宏定義,并給出每類API的使用demo,學(xué)習(xí)者只需要了解這些東西,能看懂用CMSIS-RTOS API編寫的應(yīng)用程序即可~

在TencentOS-tiny中如下。

  • 基于TencentOS-tiny的CMSIS-RTOS API v1.02版本實(shí)現(xiàn):
    • cmsis_os.h
    • cmsis_os.c
  • 基于TencentOS-tiny的CMSIS-RTOS API v2.1.3版本實(shí)現(xiàn):
    • cmsis_os2.h
    • cmsis_os2.c

CMSIS-RTOS API的整體架構(gòu)如下圖:

2. CMSIS-RTOS API列表

下面列出了 CMSIS-RTOS API v1.02 版本提供的所有API。

CMSIS-RTOS 所有API使用的錯(cuò)誤碼(cmsis-os.h):

typedef enum {
    osOK                    =     0,       ///< function completed; no error or event occurred.
    osEventSignal           =  0x08,       ///< function completed; signal event occurred.
    osEventMessage          =  0x10,       ///< function completed; message event occurred.
    osEventMail             =  0x20,       ///< function completed; mail event occurred.
    osEventTimeout          =  0x40,       ///< function completed; timeout occurred.
    osErrorParameter        =  0x80,       ///< parameter error: a mandatory parameter was missing or specified an incorrect object.
    osErrorResource         =  0x81,       ///< resource not available: a specified resource was not available.
    osErrorTimeoutResource  =  0xC1,       ///< resource not available within given time: a specified resource was not available within the timeout period.
    osErrorISR              =  0x82,       ///< not allowed in ISR context: the function cannot be called from interrupt service routines.
    osErrorISRRecursive     =  0x83,       ///< function called multiple times from ISR with same object.
    osErrorPriority         =  0x84,       ///< system cannot determine priority or thread has illegal priority.
    osErrorNoMemory         =  0x85,       ///< system is out of memory: it was impossible to allocate or reserve memory for the operation.
    osErrorValue            =  0x86,       ///< value of a parameter is out of range.
    osErrorOS               =  0xFF,       ///< unspecified RTOS error: run-time error but no other error message fits.
    os_status_reserved      =  0x7FFFFFFF  ///< prevent from enum down-size compiler optimization.
} osStatus;

CMSIS-RTOS API一些可選項(xiàng)控制是否開啟(cmsis-os.h):

#define osFeature_MainThread   1       ///< main thread      1=main can be thread, 0=not available
#define osFeature_Pool         1       ///< Memory Pools:    1=available, 0=not available
#define osFeature_MailQ        1       ///< Mail Queues:     1=available, 0=not available
#define osFeature_MessageQ     1       ///< Message Queues:  1=available, 0=not available
#define osFeature_Signals      0       ///< maximum number of Signal Flags available per thread
#define osFeature_Semaphore    30       ///< maximum count for \ref osSemaphoreCreate function
#define osFeature_Wait         0       ///< osWait function: 1=available, 0=not available
#define osFeature_SysTick      1       ///< osKernelSysTick functions: 1=available, 0=not available

2.1. 內(nèi)核信息和控制

API 描述
osKernelInitialize 初始化RTOS內(nèi)核
osKernelStart 啟動RTOS內(nèi)核
osKernelRunning Query if the RTOS kernel is running
osKernelSysTick (可選) Get RTOS kernel system timer counter
osKernelSysTickFrequency (可選) RTOS kernel system timer frequency in Hz
osKernelSysTickMicroSec (可選) Convert microseconds value to RTOS kernel system timer value
  • osKernelInitialize
osStatus osKernelInitialize(void);

返回值:status code

  • osKernelStart
osStatus osKernelStart(void);

返回值:status code

  • osKernelRunning
int32_t osKernelRunning(void);

返回值:0表示RTOS未啟動,1表示RTOS已經(jīng)啟動

  • osKernelSysTick
uint32_t osKernelSysTick(void);

返回值:RTOS內(nèi)核系統(tǒng)當(dāng)前的時(shí)間

2.2. 線程管理

##連接符的作用是連接兩個(gè)字符串,合為一個(gè)字符串。

CMSIS-RTOS API 存放線程參數(shù)管理的結(jié)構(gòu)體如下:

typedef struct os_thread_def {
    char           *name;       ///< Thread name
    os_pthread      pthread;    ///< start address of thread function
    osPriority      tpriority;  ///< initial thread priority
    uint32_t        instances;  ///< maximum number of instances of that thread function
    k_stack_t      *stackbase;  ///< base address of task
    uint32_t        stacksize;  ///< stack size requirements in bytes; 0 is default stack size
    k_timeslice_t   timeslice;  ///< timeslice
    k_task_t       *task;
} osThreadDef_t;

CMSIS-RTOS API 定義線程的宏如下:

#define osThreadDef(name, priority, instances, stacksz)  \
    k_task_t task_handler_##name; \
    k_stack_t task_stack_##name[(stacksz)]; \
    const osThreadDef_t os_thread_def_##name = \
        { #name, (os_pthread)(name), (osPriority)(priority), (instances), \
        (&((task_stack_##name)[0])), (stacksz), ((k_timeslice_t)0u), (&(task_handler_##name)) }

宏定義中的 instances 表示基于此任務(wù)參數(shù),創(chuàng)建出幾個(gè)任務(wù)實(shí)例,比如instances為2,則會創(chuàng)建出兩個(gè)任務(wù)。

CMSIS-RTOS API定義的獲取線程參數(shù)結(jié)構(gòu)體的宏如下:

#define osThread(name)  \
    &os_thread_def_##name

管理線程參數(shù)的API如下:

API 描述
osThreadCreate 創(chuàng)建線程并開始執(zhí)行
osThreadTerminate 停止線程執(zhí)行
osThreadYield 線程主動讓出
osThreadGetID 獲取當(dāng)前正在運(yùn)行線程的ID
osThreadSetPriority 改變線程優(yōu)先級
osThreadGetPriority 獲取線程優(yōu)先級
  • osThreadCreate
osThreadId osThreadCreate(const osThreadDef_t *thread_def, void *argument);

其中osThreadId被定義為k_task_t指針類型:

typedef k_task_t *osThreadId;

返回值:TencentOS-tiny中的任務(wù)控制塊類型指針。

  • osThreadTerminate
osStatus osThreadTerminate(osThreadId thread_id);

返回值:osStatus

  • osThreadYield
osStatus osThreadYield(void);

返回值:osStatus

  • osThreadGetID
osThreadId osThreadGetId(void);
  • osThreadSetPriority
osStatus osThreadSetPriority(osThreadId thread_id, osPriority priority);
  • osThreadGetPriority
osPriority osThreadGetPriority(osThreadId thread_id);

使用時(shí)需要特別注意,在TencentOS-tiny中,調(diào)用CMSIS-RTOS API提供的優(yōu)先級選項(xiàng)設(shè)置之后,實(shí)際設(shè)置的任務(wù)值是不同的。

CMSIS-RTOS API提供的線程優(yōu)先級宏定義如下:

typedef enum {
    osPriorityIdle          = -3,          ///< priority: idle (lowest)
    osPriorityLow           = -2,          ///< priority: low
    osPriorityBelowNormal   = -1,          ///< priority: below normal
    osPriorityNormal        =  0,          ///< priority: normal (default)
    osPriorityAboveNormal   = +1,          ///< priority: above normal
    osPriorityHigh          = +2,          ///< priority: high
    osPriorityRealtime      = +3,          ///< priority: realtime (highest)
    osPriorityError         =  0x84        ///< system cannot determine priority or thread has illegal priority
} osPriority;

在TecentOS-tiny中實(shí)現(xiàn)的時(shí)候進(jìn)行了轉(zhuǎn)化:

static k_prio_t priority_cmsis2knl(osPriority prio)
{
    if (prio == osPriorityError) {
        return K_TASK_PRIO_INVALID;
    }

    return (k_prio_t)(3 - prio);
}

static osPriority priority_knl2cmsis(k_prio_t prio)
{
    return (osPriority)(3 - prio);
}

比如創(chuàng)建線程時(shí)設(shè)置為 osPriorityNormal=0,則「實(shí)際設(shè)置的任務(wù)優(yōu)先級為3」。

2.3. 通用等待函數(shù)

CMSIS-RTOS提供的等待函數(shù)API如下:

API 描述
osDelay 等待指定的時(shí)間
osWait(可選) 等待信號、消息、郵箱的某個(gè)事件
  • osDelay
osStatus osDelay(uint32_t millisec);

返回值:osStatus。

2.4. 軟件定時(shí)器管理

CMSIS-RTOS API提供的存儲定時(shí)器參數(shù)的結(jié)構(gòu)體如下:

typedef struct os_timer_def {
    os_ptimer                 cb;   ///< start address of a timer function
    k_timer_t                *timer;
} osTimerDef_t;

CMSIS-RTOS API提供的定義一個(gè)軟件定時(shí)器的宏定義如下:

#define osTimerDef(name, function)  \
    k_timer_t timer_handler_##name; \
    const osTimerDef_t os_timer_def_##name = \
        { (os_ptimer)(function), (&(timer_handler_##name)) }

CMSIS-RTOS API定義的獲取軟件定時(shí)器參數(shù)結(jié)構(gòu)體的宏如下:

#define osTimer(name) \
    &os_timer_def_##name

CMSIS-RTOS API提供的軟件定時(shí)器管理API如下:

API 描述
osTimerCreate 創(chuàng)建一個(gè)軟件定時(shí)器
osTimerStart 啟動軟件定時(shí)器
osTimerStop 停止軟件定時(shí)器
osTimerDelete 刪除軟件定時(shí)器
  • osTimerCreate
osTimerId osTimerCreate(const osTimerDef_t *timer_def, os_timer_type type, void *argument);

其中osTimerId被定義為k_timer_t指針類型:

typedef k_timer_t *osTimerId;

type參數(shù)為 os_timer_type 類型,表示軟件定時(shí)器的類型為單次觸發(fā)或者周期觸發(fā):

typedef enum  {
    osTimerOnce             =     0,       ///< one-shot timer
    osTimerPeriodic         =     1        ///< repeating timer
} os_timer_type;
  • osTimerStart
osStatus osTimerStart(osTimerId timer_id, uint32_t millisec);

返回值:osStatus。

  • osTimerStop
osStatus osTimerStop(osTimerId timer_id)

返回值:osStatus。

  • osTimerDelete
osStatus osTimerDelete(osTimerId timer_id);

返回值:osStatus。

2.5. 信號量管理

CMSIS-RTOS API提供的存儲信號量參數(shù)的結(jié)構(gòu)體如下:

typedef struct os_semaphore_def {
    uint32_t                    dummy;  ///< dummy value.
    k_sem_t                    *sem;
} osSemaphoreDef_t;

CMSIS-RTOS API提供的定義一個(gè)信號量的宏定義如下:

#define osSemaphoreDef(name)  \
    k_sem_t sem_handler_##name; \
    const osSemaphoreDef_t os_semaphore_def_##name = { 0, (&(sem_handler_##name)) }

CMSIS-RTOS API定義的獲取信號量參數(shù)結(jié)構(gòu)體的宏如下:

#define osSemaphore(name)  \
    &os_semaphore_def_##name

CMSIS-RTOS API提供的信號量管理API如下:

API 描述
osSemaphoreCreate 創(chuàng)建一個(gè)信號量
osSemaphoreWait 等待信號量
osSemaphoreRelease 釋放信號量
osSemaphoreDelete 刪除信號量
  • osSemaphoreCreate
osSemaphoreId osSemaphoreCreate(const osSemaphoreDef_t *semaphore_def, int32_t count);

其中 osSemaphoreId 被定義為k_sem_t指針類型:

typedef k_sem_t *osSemaphoreId;
  • osSemaphoreWait
int32_t osSemaphoreWait(osSemaphoreId semaphore_id, uint32_t millisec);

返回值:int32_t ,正常返回當(dāng)前count數(shù),失敗返回-1。

如果需要阻塞延時(shí),參數(shù)應(yīng)該設(shè)置為CMSIS-RTOS API提供的宏定義 osWaitForever :

#define osWaitForever     0xFFFFFFFF     ///< wait forever timeout value
  • osSemaphoreRelease
osStatus osSemaphoreRelease(osSemaphoreId semaphore_id);

返回值:osStatus。

  • osSemaphoreDelete
osStatus osSemaphoreDelete(osSemaphoreId semaphore_id);

返回值:osStatus。

2.6. 互斥鎖管理

CMSIS-RTOS API提供的存儲互斥鎖參數(shù)的結(jié)構(gòu)體如下:

typedef struct os_mutex_def {
    uint32_t                    dummy;  ///< dummy value.
    k_mutex_t                  *mutex;
} osMutexDef_t;

CMSIS-RTOS API提供的定義一個(gè)互斥鎖的宏定義如下:

#define osMutexDef(name)  \
    k_mutex_t mutex_handler_##name; \
    const osMutexDef_t os_mutex_def_##name = { 0, (&(mutex_handler_##name)) }

CMSIS-RTOS API定義的獲取互斥鎖參數(shù)結(jié)構(gòu)體的宏如下:

#define osMutex(name)  \
    &os_mutex_def_##name

CMSIS-RTOS API提供的互斥鎖管理API如下:

API 描述
osMutexCreate 創(chuàng)建一個(gè)互斥鎖
osMutexWait 等待獲取互斥鎖
osMutexRelease 釋放互斥鎖
osMutexDelete 刪除互斥鎖
  • osMutexCreate
osMutexId osMutexCreate(const osMutexDef_t *mutex_def);

其中 osMutexId 被定義為k_mutex_t指針類型:

typedef k_mutex_t *osMutexId;
  • osMutexWait
osStatus osMutexWait(osMutexId mutex_id, uint32_t millisec);

返回值:osStatus 。

如果需要阻塞延時(shí),參數(shù)應(yīng)該設(shè)置為CMSIS-RTOS API提供的宏定義 osWaitForever :

#define osWaitForever     0xFFFFFFFF     ///< wait forever timeout value
  • osMutexRelease
osStatus osMutexRelease(osMutexId mutex_id);

返回值:osStatus。

  • osMutexDelete
osStatus osMutexDelete(osMutexId mutex_id);

返回值:osStatus。

2.7. 靜態(tài)內(nèi)存池管理

CMSIS-RTOS API提供的存儲靜態(tài)內(nèi)存池參數(shù)的結(jié)構(gòu)體如下:

typedef struct os_pool_def {
    uint32_t                    pool_sz;    ///< number of items (elements) in the pool
    uint32_t                    item_sz;    ///< size of an item
    void                       *pool;       ///< pointer to memory for pool
    k_mmblk_pool_t             *mmblk_pool; ///< memory blk pool handler
} osPoolDef_t;

CMSIS-RTOS API提供的定義一個(gè)互斥鎖的宏定義如下:

#define osPoolDef(name, no, type)   \
    k_mmblk_pool_t mmblk_pool_handler_##name; \
    uint8_t mmblk_pool_buf_##name[(no) * sizeof(type)]; \
    const osPoolDef_t os_pool_def_##name = \
        { (no), sizeof(type), (&((mmblk_pool_buf_##name)[0])), (&(mmblk_pool_handler_##name)) }

CMSIS-RTOS API定義的獲取互斥鎖參數(shù)結(jié)構(gòu)體的宏如下:

#define osPool(name) \
 &os_pool_def_##name

CMSIS-RTOS API提供的互斥鎖管理API如下:

API 描述
osPoolCreate 創(chuàng)建一塊固定大小的靜態(tài)內(nèi)存池
osPoolAlloc 申請分配內(nèi)存
osPoolCAlloc 申請分配一塊內(nèi)存并全部初始化為0
osPoolFree 申請回收內(nèi)存
  • osPoolCreate
osPoolId osPoolCreate(const osPoolDef_t *pool_def);

其中 osPoolId 被定義為 k_mmblk_pool_t 指針類型:

typedef k_mmblk_pool_t *osPoolId;
  • osPoolAlloc
void *osPoolAlloc(osPoolId pool_id);
  • osPoolCAlloc
void *osPoolCAlloc(osPoolId pool_id);
  • osPoolFree
osStatus osPoolFree(osPoolId pool_id, void *block);

返回值:osStatus。

2.8. 消息隊(duì)列管理

CMSIS-RTOS API提供的存儲消息隊(duì)列參數(shù)的結(jié)構(gòu)體如下:

typedef struct os_messageQ_def {
    uint32_t                    queue_sz;   ///< number of elements in the queue
    uint32_t                    item_sz;    ///< size of an item
    void                       *pool;       ///< memory array for messages
    k_msg_q_t                  *queue;      ///< queue handler
} osMessageQDef_t;

CMSIS-RTOS API提供的定義一個(gè)消息隊(duì)列的宏定義如下:

#define osMessageQDef(name, queue_sz, type)   \
    k_msg_q_t msg_q_handler_##name; \
    const osMessageQDef_t os_messageQ_def_##name = \
        { (queue_sz), sizeof(type), NULL, (&(msg_q_handler_##name)) }

CMSIS-RTOS API定義的獲取消息隊(duì)列參數(shù)結(jié)構(gòu)體的宏如下:

#define osMessageQ(name) \
    &os_messageQ_def_##name

CMSIS-RTOS API提供的消息隊(duì)列管理API如下:

API 描述
osMessageCreate 初始化一個(gè)消息隊(duì)列
osMessagePut 向消息隊(duì)列中加入數(shù)據(jù)
osMessageGet 從消息隊(duì)列中取出數(shù)據(jù)
  • osMessageCreate
osMessageQId osMessageCreate(const osMessageQDef_t *queue_def, osThreadId thread_id);

其中 osMessageQId 被定義為 k_msg_q_t 指針類型:

typedef k_msg_q_t *osMessageQId;
  • osMessagePut
osStatus osMessagePut(osMessageQId queue_id, uint32_t info, uint32_t millisec);

返回值:osStatus 。

因?yàn)門encentOS-tiny中消息隊(duì)列實(shí)現(xiàn)機(jī)制的不同,此API中的 millisec 參數(shù)未用到。

  • osMessageGet
osEvent osMessageGet(osMessageQId queue_id, uint32_t millisec);

返回值:osEvent ,其中包含了事件信息和錯(cuò)誤碼,以及消息隊(duì)列收到的值。

如果需要阻塞延時(shí),參數(shù)應(yīng)該設(shè)置為CMSIS-RTOS API提供的宏定義 osWaitForever :

#define osWaitForever     0xFFFFFFFF     ///< wait forever timeout value

3. 使用示例

3.1. 任務(wù)創(chuàng)建示例

#include <cmsis_os.h>

void task1_entry(void *arg)
{
    while(1)
    {
        printf("task1 is running...\r\n");
        osDelay(1000);
    }
}
osThreadDef(task1_entry, osPriorityNormal, 1512);

void task2_entry(void *arg)
{
    
    while(1)
    {
        printf("task2 is running...\r\n");
        osDelay(1000);
    }
}
osThreadDef(task2_entry, osPriorityNormal, 1512);

void application_entry(void *arg)
{

    osThreadCreate(osThread(task1_entry), NULL);
    osThreadCreate(osThread(task2_entry), NULL);
    
    return;
}

任務(wù)運(yùn)行結(jié)果如下:

task1 is running...
task2 is running...
task1 is running...
task2 is running...
task1 is running...
task2 is running...

3.2. 軟件定時(shí)器使用示例

#include <cmsis_os.h>

void timer1_cb(void *arg)
{
    printf("timer1 is timeout!\r\n");
}

void timer2_cb(void *arg)
{
    printf("timer2 is timeout!\r\n");
}

osTimerDef(timer1, timer1_cb);
osTimerDef(timer2, timer2_cb);

void application_entry(void *arg)
{
    osTimerId timer1;
    osTimerId timer2;
    
    timer1 = osTimerCreate(osTimer(timer1), osTimerOnce, NULL);
    timer2 = osTimerCreate(osTimer(timer2), osTimerPeriodic, NULL);
    
    osTimerStart(timer1, 5000);
    osTimerStart(timer2, 1000);
    
    return;
}

運(yùn)行結(jié)果如下:

timer2 is timeout!
timer2 is timeout!
timer2 is timeout!
timer2 is timeout!
timer1 is timeout!
timer2 is timeout!
timer2 is timeout!
timer2 is timeout!
timer2 is timeout!

3.3. 信號量使用示例

#include <cmsis_os.h>

osSemaphoreId sync_sem_id;
osSemaphoreDef(sync_sem);

void task1_entry(void *arg)
{
    while(1)
    {
        printf("task1 is waiting sem forever...\r\n");
        osSemaphoreWait(sync_sem_id, osWaitForever);
        printf("task1 get sem!\r\n");
    }
}
osThreadDef(task1_entry, osPriorityNormal, 1512);

void task2_entry(void *arg)
{
    
    while(1)
    {
        printf("task2 will release a sem...\r\n");
        osSemaphoreRelease(sync_sem_id);
        osDelay(1000);
    }
}
osThreadDef(task2_entry, osPriorityNormal, 1512);

void application_entry(void *arg)
{
    sync_sem_id = osSemaphoreCreate(osSemaphore(sync_sem), 0);

    osThreadCreate(osThread(task1_entry), NULL);
    osThreadCreate(osThread(task2_entry), NULL);
    
    return;
}

運(yùn)行結(jié)果為:

task1 is waiting sem forever...
task1 get sem!
task1 is waiting sem forever...
task2 will release a sem...
task1 get sem!
task1 is waiting sem forever...
task2 will release a sem...
task1 get sem!
task1 is waiting sem forever...
task2 will release a sem...
task1 get sem!
task1 is waiting sem forever...
task2 will release a sem...
task1 get sem!
task1 is waiting sem forever...

3.4. 互斥鎖使用示例

#include <cmsis_os.h>

osMutexId sync_mutex_id;
osMutexDef(sync_mutex);

void task1_entry(void *arg)
{
    while(1)
    {
        osMutexWait(sync_mutex_id, osWaitForever);
        
        printf("task1 get mutex,doing sth...\r\n");
        HAL_Delay(1000);    //死循環(huán)占用CPU
        printf("task1 finish do sth!\r\n");
        
        osMutexRelease(sync_mutex_id);
        
        osDelay(1000);
    }
}
osThreadDef(task1_entry, osPriorityHigh, 1512);

void task2_entry(void *arg)
{
    
    while(1)
    {
        osMutexWait(sync_mutex_id, osWaitForever);
        
        printf("task2 get mutex,doing sth...\r\n");
        HAL_Delay(2000);    //死循環(huán)占用CPU
        printf("task2 finish do sth!\r\n");
        
        osMutexRelease(sync_mutex_id);
        
        osDelay(1000);
    }
}
osThreadDef(task2_entry, osPriorityNormal, 1512);

void application_entry(void *arg)
{
    sync_mutex_id = osMutexCreate(osMutex(sync_mutex));

    osThreadCreate(osThread(task1_entry), NULL);
    osThreadCreate(osThread(task2_entry), NULL);
    
    return;
}

運(yùn)行結(jié)果為:

task1 get mutex,doing sth...
task1 finish do sth!
task2 get mutex,doing sth...
task2 finish do sth!
task1 get mutex,doing sth...
task1 finish do sth!
task1 get mutex,doing sth...
task1 finish do sth!
task2 get mutex,doing sth...

3.5. 動態(tài)內(nèi)存使用示例

#include <cmsis_os.h>

typedef struct blk_st {
    int   id;
    char* payload;
blk_t;

#define MMBLK_BLK_NUM 10

osPoolDef (MemPool, MMBLK_BLK_NUM, blk_t);
osPoolId mem_pool_id;

void task1_entry(void *arg)
{   
    
    blk_t *ptr = NULL;
    osStatus err;
    
    /* 打印出一個(gè)塊的大小 */
    printf("block size is %d bytes\r\n"sizeof(blk_t));
    
    /* 申請一個(gè)塊 */
    ptr = osPoolAlloc(mem_pool_id);
    if (ptr == NULL) {
        printf("a mmblk alloc fail\r\n");
        return;
    }
    else {
        printf("a mmblk alloc success\r\n");
    }
    
    /* 使用該塊 */
    ptr->id = 1;
    ptr->payload = "hello";
    printf("mmblk id:%d payload:%s\r\n", ptr->id, ptr->payload);
    
    /* 使用完畢之后釋放 */
    err = osPoolFree(mem_pool_id, ptr);
    if (err != osOK) {
        printf("a mmblk free fail, err = %d\r\n", err);
        return;
    }
    else {
        printf("a mmblk free success\r\n");
    }
    
    while (1) {
        tos_task_delay(1000);
    }
}

#define STK_SIZE_TASK1      1024
osThreadDef(task1_entry, osPriorityNormal, 1, STK_SIZE_TASK1);

void application_entry(void *arg)
{
    //初始化靜態(tài)內(nèi)存池
    mem_pool_id = osPoolCreate(osPool(MemPool));
    if (mem_pool_id == NULL) {
        printf("mmblk pool create fail\r\n");
        return;
    }
    else {
        printf("mmblk pool create success\r\n");
    }

    //創(chuàng)建任務(wù)
    osThreadCreate(osThread(task1_entry), NULL);

    return;
}

運(yùn)行結(jié)果為:

mmblk pool create success
block size is 8 bytes
a mmblk alloc success
mmblk id:1 payload:hello
a mmblk free success

3.6. 消息隊(duì)列使用示例

#include <cmsis_os.h>

#define STK_SIZE_TASK_RECEIVER      512
#define STK_SIZE_TASK_SENDER        512

#define MESSAGE_MAX     10

osMessageQId msg_q_id;
osMessageQDef(msg_q,MESSAGE_MAX,uint32_t);

void task_receiver_entry(void *arg)
{
    osEvent event;
    osStatus ret;
    uint32_t value;

    while (1)
    {
        event = osMessageGet(msg_q_id, osWaitForever);
        ret = event.status;
        if (ret == osOK)
        {
            value = event.value.v;
            printf("receiver: msg incoming[%s]\r\n", (char*)value);
        }
    }
}
osThreadDef(task_receiver_entry, osPriorityNormal, 1, STK_SIZE_TASK_RECEIVER);

void task_sender_entry(void *arg)
{
    char *msg_prio_0 = "msg 0";
    char *msg_prio_1 = "msg 1";
    char *msg_prio_2 = "msg 2";

    printf("sender: post a messgae:[%s]\r\n", msg_prio_2);
    osMessagePut(msg_q_id,(uint32_t)msg_prio_2,0);
    
    printf("sender: post a messgae:[%s]\r\n", msg_prio_1);
    osMessagePut(msg_q_id,(uint32_t)msg_prio_1,0);
    
    printf("sender: post a messgae:[%s]\r\n", msg_prio_0);
    osMessagePut(msg_q_id,(uint32_t)msg_prio_0,0);

}
osThreadDef(task_sender_entry, osPriorityNormal, 1, STK_SIZE_TASK_SENDER);

void application_entry(void *arg)
{
    msg_q_id = osMessageCreate(osMessageQ(msg_q),NULL);

    osThreadCreate(osThread(task_receiver_entry), NULL);
    osThreadCreate(osThread(task_sender_entry), NULL);
    
    return;
}

運(yùn)行結(jié)果為:

sender: post a messgae:[msg 2]
sender: post a messgae:[msg 1]
sender: post a messgae:[msg 0]
receiver: msg incoming[msg 2]
receiver: msg incoming[msg 1]
receiver: msg incoming[msg 0]


推薦閱讀:

文本或代碼中 \n 和 \r 的區(qū)別

幾個(gè)類似 VS Code的開源編輯器工具

STM32中CRC計(jì)算單元,及CRC校驗(yàn)的應(yīng)用


關(guān)注 微信公眾號『strongerHuang』,后臺回復(fù)“1024”查看更多內(nèi)容,回復(fù)“加群”按規(guī)則加入技術(shù)交流群。


長按前往圖中包含的公眾號關(guān)注

免責(zé)聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺僅提供信息存儲服務(wù)。文章僅代表作者個(gè)人觀點(diǎn),不代表本平臺立場,如有問題,請聯(lián)系我們,謝謝!

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

加利福尼亞州圣克拉拉縣2024年8月30日 /美通社/ -- 數(shù)字化轉(zhuǎn)型技術(shù)解決方案公司Trianz今天宣布,該公司與Amazon Web Services (AWS)簽訂了...

關(guān)鍵字: AWS AN BSP 數(shù)字化

倫敦2024年8月29日 /美通社/ -- 英國汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時(shí)1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動 BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時(shí)企業(yè)卻面臨越來越多業(yè)務(wù)中斷的風(fēng)險(xiǎn),如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報(bào)道,騰訊和網(wǎng)易近期正在縮減他們對日本游戲市場的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會開幕式在貴陽舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對環(huán)境變化,經(jīng)營業(yè)績穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤率延續(xù)升勢 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競爭力 堅(jiān)持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競爭優(yōu)勢...

關(guān)鍵字: 通信 BSP 電信運(yùn)營商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺與中國電影電視技術(shù)學(xué)會聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會上宣布正式成立。 活動現(xiàn)場 NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會上,軟通動力信息技術(shù)(集團(tuán))股份有限公司(以下簡稱"軟通動力")與長三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉