在线时间434 小时
UID3066878
注册时间2014-12-11
NXP金币0
TA的每日心情 | 奋斗 2017-5-3 11:19 |
---|
签到天数: 10 天 连续签到: 1 天 [LV.3]偶尔看看II
金牌会员
 
- 积分
- 14090
- 最后登录
- 2024-4-19
|
每次是都快到点了才交任务,实在不好意思。
今天我的实验就是让双核互相配合来完成一个计算任务。在任务一里,我们知道两个核心分工协作,M4侧重于运算,M0侧重于IO控制。今天我们就根据这个来完成一个简单的运算任务。
M0来进行输出输出的控制(M0内核通过UART输出一个提示,提示用户输出一个数值,然后把数据值给M4内核)
M4内核取得M0内核传过来的数值做一个简单的乘法(乘23)来模拟数据运算。(本来我是打算写一个表达式解析器,就是M0传过来的是一个表达式,比如:23*45+32-190/3 ,用M4内核来解析出表达式并计算出结果。可是没时间来,就用简单的乘法模拟),M4计算完成后通知M0,M0收到计算结果,并输出来。
基本上就这样子,我们基于SDK里的multicore_examples的hello world来修改,废话不多说直接用代码来解释:
M4工程的代码:
- #include "fsl_gpio.h"
- #include "board.h"
- #include "mcmgr.h"
- #include "fsl_common.h"
- #include "pin_mux.h"
- #include "fsl_mailbox.h"
- #include "stdlib.h"
- /*******************************************************************************
- * Definitions
- ******************************************************************************/
- /* Address of RAM, where the image for core1 should be copied */
- #define CORE1_BOOT_ADDRESS (void *)0x20010000
- #if defined(__CC_ARM)
- extern uint32_t Image$CORE1_REGION$Base;
- extern uint32_t Image$CORE1_REGION$Length;
- #define CORE1_IMAGE_START &Image$CORE1_REGION$Base
- #elif defined(__ICCARM__)
- extern unsigned char core1_image_start[];
- #define CORE1_IMAGE_START core1_image_start
- #endif
- /*******************************************************************************
- * Prototypes
- ******************************************************************************/
- #ifdef CORE1_IMAGE_COPY_TO_RAM
- uint32_t get_core1_image_size(void);
- #endif
- /*******************************************************************************
- * Code
- ******************************************************************************/
- #ifdef CORE1_IMAGE_COPY_TO_RAM
- uint32_t get_core1_image_size()
- {
- uint32_t core1_image_size;
- #if defined(__CC_ARM)
- core1_image_size = (uint32_t)&Image$CORE1_REGION$Length;
- #elif defined(__ICCARM__)
- #pragma section = "__sec_core"
- core1_image_size = (uint32_t)__section_end("__sec_core") - (uint32_t)&core1_image_start;
- #endif
- return core1_image_size;
- }
- #endif
- /*!
- * @brief Main function
- */
- int main(void)
- {
- /* Define the init structure for the switches*/
- gpio_pin_config_t sw_config = {kGPIO_DigitalInput, 0};
- /* Init board hardware.*/
- /* attach 12 MHz clock to FLEXCOMM0 (debug console) */
- CLOCK_AttachClk(kFRO12M_to_FLEXCOMM0);
- BOARD_InitPins_Core0();
- BOARD_BootClockFROHF48M();
- #ifdef CORE1_IMAGE_COPY_TO_RAM
- /* Calculate size of the image - not required on LPCExpresso. LPCExpresso copies image to RAM during startup
- * automatically */
- uint32_t core1_image_size;
- core1_image_size = get_core1_image_size();
- /* Copy Secondary core application from FLASH to RAM. Primary core code is executed from FLASH, Secondary from RAM
- * for maximal effectivity.*/
- memcpy(CORE1_BOOT_ADDRESS, (void *)CORE1_IMAGE_START, core1_image_size);
- #endif
- /* Initialize MCMGR before calling its API */
- MCMGR_Init();
- /* Boot Secondary core application */
- MCMGR_StartCore(kMCMGR_Core1, CORE1_BOOT_ADDRESS, 1, kMCMGR_Start_Synchronous);
-
- MAILBOX_Init(MAILBOX);
- NVIC_EnableIRQ(MAILBOX_IRQn);
- while (1)
- {
- __WFI;
- }
- }
- void MAILBOX_IRQHandler(void)
- {
- /* 收到m0传过的用户输值 */
- char* buf = (char*)MAILBOX_GetValue(MAILBOX, kMAILBOX_CM4);
- /* Clear my mailbox */
- MAILBOX_ClearValueBits(MAILBOX, kMAILBOX_CM4, 0xFFFFFFFF);
- //计算结果
- int32_t psharedLEDStates = atoi(buf) * 23;
- //通知M0
- MAILBOX_SetValue(MAILBOX, kMAILBOX_CM0Plus, psharedLEDStates);
- }
复制代码
M0工程的代码:
- #include "fsl_debug_console.h"
- #include "board.h"
- #include "mcmgr.h"
- #include "fsl_common.h"
- #include "pin_mux.h"
- #include "fsl_gpio.h"
- #include "fsl_mailbox.h"
- /*******************************************************************************
- * Definitions
- ******************************************************************************/
- #define LED_INIT() GPIO_PinInit(GPIO, BOARD_LED_RED_GPIO_PORT, BOARD_LED_RED_GPIO_PIN, &led_config);
- #define LED_TOGGLE() GPIO_TogglePinsOutput(GPIO, BOARD_LED_RED_GPIO_PORT, 1u << BOARD_LED_RED_GPIO_PIN);
- /*******************************************************************************
- * Prototypes
- ******************************************************************************/
- /*******************************************************************************
- * Code
- ******************************************************************************/
- /*!
- * @brief Function to create delay for Led blink.
- */
- void delay(void)
- {
- volatile uint32_t i = 0;
- for (i = 0; i < 1000000; ++i)
- {
- __asm("NOP"); /* delay */
- }
- }
- volatile char buf[32];
- /*!
- * @brief Main function
- */
- int main(void)
- {
- uint32_t startupData, i;
- /* Define the init structure for the output LED pin*/
- gpio_pin_config_t led_config = {
- kGPIO_DigitalOutput, 0,
- };
- /* Initialize MCMGR before calling its API */
- MCMGR_Init();
- /* Get the startup data */
- MCMGR_GetStartupData(kMCMGR_Core1, &startupData);
- /* Make a noticable delay after the reset */
- /* Use startup parameter from the master core... */
- for (i = 0; i < startupData; i++)
- delay();
- /* Init board hardware.*/
- /* enable clock for GPIO */
- CLOCK_EnableClock(kCLOCK_Gpio0);
- CLOCK_EnableClock(kCLOCK_Gpio1);
- BOARD_InitPins_Core1();
- BOARD_BootClockFROHF48M();
- BOARD_InitDebugConsole();
- MAILBOX_Init(MAILBOX);
- NVIC_EnableIRQ(MAILBOX_IRQn);
- /* Configure LED */
- LED_INIT();
- /* Signal the other core we are ready */
- MCMGR_SignalReady(kMCMGR_Core1);
- while (1)
- {
- delay();
- LED_TOGGLE();
- PRINTF("M0 say:Please input the number:\n");
- uint32_t i = 0;
- while(1)
- {
- //获取用户输入 .表示输入结果符
- char ch = DbgConsole_Getchar();
- if(ch == '.')
- {
- buf[i] = 0x0;
- break;
- }
- buf[i] = ch;
- i++;
- }
- //通知M4 用户输出结束,可以进行运算了
- MAILBOX_SetValue(MAILBOX, kMAILBOX_CM4, (uint32_t)&buf);
- }
- }
- void MAILBOX_IRQHandler(void)
- {
- /* 取M4计算好的值 */
- int32_t result = MAILBOX_GetValue(MAILBOX, kMAILBOX_CM0Plus);
- /* Clear my mailbox */
- MAILBOX_ClearValueBits(MAILBOX, kMAILBOX_CM0Plus, 0xFFFFFFFF);
- //输出M4的计算结果
- PRINTF("M4 calculation(%s * 23) results are:%d\n\n", buf, result);
- }
复制代码 实验结果 :
|
|