日韩高清在线日韩大片观看网址,一级毛片毛片**毛片毛片,女**毛片一级毛片一,狠狠插天天干,天天干夜夜噜,亚洲人成在线免费观看,天天操天天草

RTOS實現(xiàn)雙核MCU消息通信

  • strongerHuang
  • 2023-06-08 02:27:36

手機(jī)電腦多核的CPU你可能經(jīng)??匆姡嗪说?u>單片機(jī)相對來說就不那么常見了。隨著需求的增加、技術(shù)的進(jìn)步,單片機(jī)已不再局限于單核了,因此,近幾年陸續(xù)出現(xiàn)了雙核的單片機(jī)了。 你可能會好奇,雙核單片機(jī)之間怎么通信?其實,通信的方式和方法有很多種。本文就給大家描述一下:使用FreeRTOS消息緩沖區(qū),實現(xiàn)簡單的非對稱多處理(AMP)核心到核心通信,結(jié)合STM32H7(M4和M7) 雙核處理器為例。

概述


(資料圖片僅供參考)

實現(xiàn)STM32H7雙核之間通信是FreeRTOS官方提供的一個方案,是基于FreeRTOS消息緩沖區(qū),該消息緩沖區(qū)是無鎖循環(huán)緩沖區(qū),可以將大小不同的數(shù)據(jù)包從單個發(fā)送方傳遞到單個接收方。 說明,該消息緩沖區(qū)僅提供數(shù)據(jù)的傳輸,不提供通信相關(guān)協(xié)議處理。

基本原理

實現(xiàn)雙核之間通信基本原理:發(fā)送和接收任務(wù)位于非對稱多處理器(AMP)配置中的多核微控制器(MCU)的不同內(nèi)核上,這意味著每個內(nèi)核都運行自己的FreeRTOS程序。 同時,一個內(nèi)核在另一個內(nèi)核中具有生成中斷的能力,以及兩個內(nèi)核都有訪問的內(nèi)存區(qū)域(共享內(nèi)存)。消息緩沖區(qū)以每個內(nèi)核上運行在應(yīng)用程序已知的地址置在共享內(nèi)存中,如下圖: 理想情況下,還將有一個內(nèi)存保護(hù)單元(MPU),以確保只能通過內(nèi)核的消息緩沖區(qū)API來訪問消息緩沖區(qū),并最好將共享內(nèi)存標(biāo)記為不可被其他程序占用。

單消息代碼描述

這里官方提供了實現(xiàn)該方案的基礎(chǔ)代碼(僅供參考)。 將數(shù)據(jù)發(fā)送到流緩沖區(qū)的代碼:

xMessageBufferSend(){    /* If a time out is specified and there isn"t enough    space in the message buffer to send the data, then    enter the blocked state to wait for more space. */    if( time out != 0 )    {        while( there is insufficient space in the buffer &&               not timed out waiting )        {            Enter the blocked state to wait for space in the buffer        }    }    if( there is enough space in the buffer )    {        write data to buffer        sbSEND_COMPLETED()    }}
從流緩沖區(qū)讀取數(shù)據(jù)的代碼:
xMessageBufferReceive(){    /* If a time out is specified and the buffer doesn"t    contain any data that canbe read, then enter the    blocked state to wait for the buffer to contain data. */    if( time out != 0 )    {        while( there is no data in the buffer &&               not timed out waiting )        {            Enter the blocked state to wait for data        }    }    if( there is data in the buffer )    {        read data from buffer        sbRECEIVE_COMPLETED()    }}
如果任務(wù)在xMessageBufferReceive()中進(jìn)入阻塞狀態(tài)以等待緩沖區(qū)包含數(shù)據(jù),則將數(shù)據(jù)發(fā)送到緩沖區(qū)必須取消阻塞該任務(wù),以便它可以完成其操作。 當(dāng)xMessageBufferSend()調(diào)用sbSEND_COMPLETED()時,任務(wù)將不受阻礙。 ISR通過將消息緩沖區(qū)的句柄作為參數(shù)傳遞給xMessageBufferSendCompletedFromISR()函數(shù)來解除對任務(wù)的阻塞。 如圖箭頭所示,其中發(fā)送和接收任務(wù)位于不同的MCU內(nèi)核上:1.接收任務(wù)嘗試從空的消息緩沖區(qū)中讀取數(shù)據(jù),并進(jìn)入阻止?fàn)顟B(tài)以等待數(shù)據(jù)到達(dá)。2.發(fā)送任務(wù)將數(shù)據(jù)寫入消息緩沖區(qū)。3.sbSEND_COMPLETED()在正在執(zhí)行接收任務(wù)的內(nèi)核中觸發(fā)一個中斷。4.中斷服務(wù)例程調(diào)用xMessageBufferSendCompletedFromISR()來解除阻止接收任務(wù),該任務(wù)現(xiàn)在可以從緩沖區(qū)讀取,因為緩沖區(qū)不再為空。

多消息代碼描述

當(dāng)只有一個消息緩沖區(qū)時,很容易將消息緩沖區(qū)的句柄傳遞到xMessageBufferSendCompletedFromISR()中。 但是要考慮有兩個或更多消息緩沖區(qū)的情況,ISR必須首先確定哪個消息緩沖區(qū)包含數(shù)據(jù)。如果消息緩沖區(qū)的數(shù)量很少,則有幾種方法可以實現(xiàn):

如果硬件允許,則每個消息緩沖區(qū)可以使用不同的中斷線,從而使中斷服務(wù)程序和消息緩沖區(qū)之間保持一對一的映射。

中斷服務(wù)例程可以簡單地查詢每個消息緩沖區(qū)以查看其是否包含數(shù)據(jù)。

可以通過傳遞元數(shù)據(jù)(消息是什么,消息的預(yù)期接收者是什么等等)以及實際數(shù)據(jù)的單個消息緩沖區(qū)來代替多個消息緩沖區(qū)。

但是,如果存在大量或未知的消息緩沖區(qū),則這些技術(shù)效率不高。 在這種情況下,可伸縮的解決方案是引入單獨的控制消息緩沖區(qū)。如下面的代碼所示,sbSEND_COMPLETED()使用控制消息緩沖區(qū)將包含數(shù)據(jù)的消息緩沖區(qū)的句柄傳遞到中斷服務(wù)例程中。 使用sbSEND_COMPLETED()的實現(xiàn):

/* Added to FreeRTOSConfig.h to override the default implementation. */#define sbSEND_COMPLETED( pxStreamBuffer ) vGenerateCoreToCoreInterrupt( pxStreamBuffer )/* Implemented in a C file. */void vGenerateCoreToCoreInterrupt( MessageBufferHandle_t xUpdatedBuffer ){size_t BytesWritten.    /* Called by the implementation of sbSEND_COMPLETED() in FreeRTOSConfig.h.    If this function was called because data was written to any message buffer    other than the control message buffer then write the handle of the message    buffer that contains data to the control message buffer, then raise an    interrupt in the other core.  If this function was called because data was    written to the control message buffer then do nothing. */    if( xUpdatedBuffer != xControlMessageBuffer )    {        BytesWritten = xMessageBufferSend(  xControlMessageBuffer,                                            &xUpdatedBuffer,                                            sizeof( xUpdatedBuffer ),                                            0 );        /* If the bytes could not be written then the control message buffer        is too small! */        configASSERT( BytesWritten == sizeof( xUpdatedBuffer );        /* Generate interrupt in the other core (pseudocode). */        GenerateInterrupt();    }}
然后,ISR讀取控制消息緩沖區(qū)以獲得句柄,將句柄作為參數(shù)傳遞到xMessageBufferSendCompletedFromISR()中:
void InterruptServiceRoutine( void ){MessageBufferHandle_t xUpdatedMessageBuffer;BaseType_t xHigherPriorityTaskWoken = pdFALSE;    /* Receive the handle of the message buffer that contains data from the    control message buffer.  Ensure to drain the buffer before returning. */    while( xMessageBufferReceiveFromISR( xControlMessageBuffer,                                         &xUpdatedMessageBuffer,                                         sizeof( xUpdatedMessageBuffer ),                                         &xHigherPriorityTaskWoken )                                           == sizeof( xUpdatedMessageBuffer ) )    {        /* Call the API function that sends a notification to any task that is        blocked on the xUpdatedMessageBuffer message buffer waiting for data to        arrive. */        xMessageBufferSendCompletedFromISR( xUpdatedMessageBuffer,                                            &xHigherPriorityTaskWoken );    }    /* Normal FreeRTOS "yield from interrupt" semantics, where    xHigherPriorityTaskWoken is initialised to pdFALSE and will then get set to    pdTRUE if the interrupt unblocks a task that has a priority above that of    the currently executing task. */    portYIELD_FROM_ISR( xHigherPriorityTaskWoken );}
如圖,使用控制消息緩沖區(qū)時的順序:1.接收任務(wù)嘗試從空的消息緩沖區(qū)中讀取數(shù)據(jù),并進(jìn)入阻止?fàn)顟B(tài)以等待數(shù)據(jù)到達(dá)。2.發(fā)送任務(wù)將數(shù)據(jù)寫入消息緩沖區(qū)。3.sbSEND_COMPLETED()將現(xiàn)在包含數(shù)據(jù)的消息緩沖區(qū)的句柄發(fā)送到控制消息緩沖區(qū)。4.sbSEND_COMPLETED()在正在執(zhí)行接收任務(wù)的內(nèi)核中觸發(fā)一個中斷。5.中斷服務(wù)例程從控制消息緩沖區(qū)中讀取包含數(shù)據(jù)的消息緩沖區(qū)的句柄,然后將該句柄傳遞給xMessageBufferSendCompletedFromISR()API函數(shù)以取消阻止接收任務(wù),該任務(wù)現(xiàn)在可以從緩沖區(qū)讀取,因為緩沖區(qū)不再存在空的。 當(dāng)然,以上僅提供基礎(chǔ)原理和方法,具體實現(xiàn)需結(jié)合項目實際情況。更多相關(guān)內(nèi)容,請參看官方相關(guān)資料。審核編輯:湯梓紅

關(guān)鍵詞:

分享到:
?
  • 至少輸入5個字符
  • 表情

熱門資訊