前面简单了解了一下双核的基本配置
以及双核间的通信
准备着手双核协同工作的一些内容了
简单设想了一下这次的方案:
M0产生一个随机数,共享给M4
M4读取这个随机数,并进行计算判断是否为偶数,并将结果通知M0
M0收到M4的计算判断结果后,,如果是,则点灯,如果不是,则灭灯
开搞,折腾了老半天,感觉LPCOpen的库有些很难描述的Bug,和c标准库之间的问题
有些时候会出现一些奇怪的现象,有空再复现一下,汇总起来
先上代码吧,很简单
M4侧:
- #include "board.h"
- #include <cr_section_macros.h>
- #include "boot_multicore_slave.h"
- #include <stdlib.h>
- #include <stdio.h>
- unsigned char cnt_m4;
- static MBOX_IDX_T myCoreBox, otherCoreBox;
- static void mutexTake(void)
- {
- while (Chip_MBOX_GetMutex(LPC_MBOX) == 0) {}
- }
- static void mutexGive(void)
- {
- Chip_MBOX_SetMutex(LPC_MBOX);
- }
- void MAILBOX_IRQHandler(void)
- {
- char pdata[20];
- uint32_t sdata,temp;
- uint32_t *p = (uint32_t *)Chip_MBOX_GetValue(LPC_MBOX, myCoreBox);
- mutexTake();
- itoa(*p,pdata,10);//获取M0的随机数
- Board_UARTPutSTR("Core M4 get the randnum:");
- Board_UARTPutSTR(pdata);Board_UARTPutSTR("\r\n");
- Board_UARTPutSTR("Core M4 computed the result:");
- temp = (*p)%2; //判断是否为偶数,是则sdata=1,否则sdata=0
- if(temp==0){sdata = 1;Board_UARTPutSTR("LED ON");}
- else{sdata = 0;Board_UARTPutSTR("LED OFF");}
- Board_UARTPutSTR("\r\n");
- Chip_MBOX_SetValue(LPC_MBOX, otherCoreBox, (uint32_t)&sdata);//把计算判断的结果返回给M0
- Chip_MBOX_ClearValueBits(LPC_MBOX, myCoreBox, 0xFFFFFFFF);
- mutexGive();
- }
- int main(void) {
- SystemCoreClockUpdate();
- Board_Init();
- myCoreBox = MAILBOX_CM4;
- otherCoreBox = MAILBOX_CM0PLUS;
- Chip_MBOX_Init(LPC_MBOX);
- mutexGive();
- Board_UARTPutSTR("Core M4 boot......\r\n");
- NVIC_EnableIRQ(MAILBOX_IRQn);
- boot_multicore_slave();
- volatile static int i = 0 ;
- while(1) {
- i++ ;
- }
- return 0 ;
- }
复制代码
M0侧:
- #include "board.h"
- #include <cr_section_macros.h>
- #include "boot_multicore_slave.h"
- #include <stdlib.h>
- #include <stdio.h>
- static void mutexTake(void)
- {
- while (Chip_MBOX_GetMutex(LPC_MBOX) == 0) {}
- }
- static void mutexGive(void)
- {
- Chip_MBOX_SetMutex(LPC_MBOX);
- }
- unsigned char cnt_m0;
- static MBOX_IDX_T myCoreBox, otherCoreBox;
- void MAILBOX_IRQHandler(void)
- {
- uint32_t *p = (uint32_t *)Chip_MBOX_GetValue(LPC_MBOX, myCoreBox);
- mutexTake();
- Board_UARTPutSTR("Core M0+ get the Result from M4,");
- if(*p==1){Board_LED_Set(1,true);Board_UARTPutSTR("SET LED ON");} //获取M4计算判断的结果,并配置LED灯
- else{Board_LED_Set(1,false);Board_UARTPutSTR("SET LED OFF");}
- Board_UARTPutSTR("\r\n");
- Chip_MBOX_ClearValueBits(LPC_MBOX, myCoreBox, 0xFFFFFFFF);
- mutexGive();
- }
- int main(void) {
- SystemCoreClockUpdate();
- Board_Init();
- myCoreBox = MAILBOX_CM0PLUS;
- otherCoreBox = MAILBOX_CM4;
- Chip_MBOX_Init(LPC_MBOX);
- mutexGive();
- Board_UARTPutSTR("Core M0+ boot......\r\n");
- NVIC_EnableIRQ(MAILBOX_IRQn);
- SysTick_Config(SystemCoreClock/10);
- volatile static int i = 0 ;
- while(1) {
- i++ ;
- }
- return 0 ;
- }
- void SysTick_Handler(void) //每10个systick产生一个伪随机数,并发送给M4进行计算
- {
- char pdata[10];
- int sdata;
- cnt_m0++;
- if(cnt_m0>=10)
- {
- mutexTake();
- cnt_m0 = 0;
- //sprintf(pdata,"Core M0+ make rand_num:%d\r\n",rand());
- sdata = rand();
- Board_UARTPutSTR("Core M0+ make the randnum:");Board_UARTPutSTR(itoa(sdata,pdata,10));Board_UARTPutSTR("\r\n");
- Chip_MBOX_SetValue(LPC_MBOX, otherCoreBox, (uint32_t) &sdata);
- Chip_MBOX_ClearValueBits(LPC_MBOX, myCoreBox, 0xFFFFFFFF);
- mutexGive();
- }
- }
复制代码
有图有真相
到现在,C语言还是有很多不明白的东西,学无止境啊