åèšäºã¯ãèªå® ã家æããŸãã¯ãªã©ã¯ãŒãŒã·ã§ã³ã®ããã«ã1æéè¿ãã®ã©ããã§ãç¹°ãè¿ãããšãåçãã®ããã«èšèšãããŠããŸã...
å€ãã®å ŽåïŒã»ãšãã©ã®å Žåãã»ãšãã©ã®å ŽåïŒããã€ã¯ãã³ã³ãããŒã©ãŒã¯äžåºŠã«è€æ°ã®ãã©ã¡ãŒã¿ãŒãã¢ãã¿ãŒããå¿ èŠãããç¶æ³ã§äœ¿çšãããŸãã ãŸãã¯ãã®éã«ãè€æ°ã®ããã€ã¹ãåæã«ç®¡çããŸãã
äŸã®ããã®åé¡ã¯ããã«ãããŸãïŒç°ãªãäŒæ¢ã®ç°ãªãæéã®ãã«ã¹ãåºåããå¿ èŠããã4ã€ã®åºåããããŸãã æã£ãŠããã®ã¯ãããªç§åäœã§ã«ãŠã³ãããã·ã¹ãã ã¿ã€ããŒã§ãã
ãarduinoã§èªåèªèº«ãæ·åããããšãã粟ç¥ã§ã¿ã¹ã¯ãè€éã«ããŸãã ã¿ã€ããŒã¯å¥ã®ã¿ã€ããŒã§ããžãŒã§ãããPWMã¯ãã¹ãŠã®ã¬ãã°ã§æ©èœããªããããé©åã§ã¯ãããŸãããéåžžãåžæã®ã¢ãŒãã«é§åããããšã¯ã§ããŸããã å°ãèããåŸã座ã£ãŠãã®ã³ãŒãã®ãããªãã®ãæžããŸã
// int time1on=500; // , 1 int time1off=250; // , 1 unsigned int now=millis(); .... // - if(millis()<now+time1on) { port1=ON; } else { port1=OFF; if(millis()>now+time1on+time1off) { now=millis(); } }
ãããŠã4ã€ãã¹ãŠã®ããŒãã§ãã®ãããªããšãããŸãã ããã€ãã®ç»é¢ã§é©åãªãããã¯ãã¹ã衚瀺ãããŸããããã®ãããã¯ãã¹ã¯éåžžã«è¿ éã«æ©èœããåäœããŸããããã¯ãã€ã¯ãã³ã³ãããŒã©ãŒã«ãšã£ãŠéèŠã§ãã
ãã®åŸãããã°ã©ããŒã¯ãããŒãã®ç¶æ ãå€åããªããŠããåãµã€ã¯ã«äžã«ããŒãããŽããŽãåãããšã«çªç¶æ°ä»ããŸãã ãããã¯ãã¹å šäœãæ¯é ããŸãã 次ã«ãåãããŒãºãæã€ããŒãã®æ°ã2åã«ãªããŸãã ããã°ã©ããŒã¯ããã¹ãŠãåãåºããã¿ã€ããPortBlinkïŒint port numïŒã®åäžã®é¢æ°ã«æžãæããŸãã
幞çŠã¯ã»ãšãã©æ¥ãŸããããçªç¶ãããã€ãã®ããŒãã§ããçµäºãã³ã³ãããŒã«ãšäžç·ã«äœããäºåã«èªã¿åãããã®èªã¿åãã«åºã¥ããŠããŒããå¶åŸ¡ããå¿ èŠããããŸããã ããã°ã©ããŒã¯åã³èªããç¹ã«ããŒãã«å¯ŸããŠå¥ã®æ©èœãå®è¡ããŸãã
幞ãïŒ ããããæ°ã«ããªãã§ã 顧客ãäœããæ¥ç¶ãããšãæ°ç§ã§ããã»ã¹ãç°¡åã«é ããªãå¯èœæ§ããããŸã...ããã声ãå§ãŸããããã°ã©ããŒãåã³ã³ãŒããä¿®æ£ããæçµçã«èªã¿åãäžå¯èœãªãŽãã«å€ãããããŒãžã£ãŒãæ©èœãè¿œå ããããã«é¡§å®¢ã«ã¯ã€ã«ãäŸ¡æ ŒãããŒã«ã¢ãŠããã顧客ã¯äºåºŠãšæ··ä¹±ããªãããšãèªããŸãçµ±åãœãªã¥ãŒã·ã§ã³ã
ïŒåºåãè³è³ãªã©ïŒãããŠãªãã§ããïŒ åœåããã©ãããã©ãŒã ã«é¢ããŠééã£ã決å®ãè¡ãããããã§ãã å¯èœã§ããã°ãåå§çãªã¿ã¹ã¯ã§ãæŽç·Žããããã©ãããã©ãŒã ãæäŸããŸãã çµéšãããéçºãšãµããŒãã®ã³ã¹ãã¯ã¯ããã«äœããªããŸãã 次ã«ã8çªç®ã®åºåãå¶åŸ¡ããããã«ã72MHzã§åäœå¯èœãªSTM32F3ã䜿çšããŸãã ïŒãããã声ïŒå®éãç§ã¯åœŒãšã®ãã¢æ¯æããæå ã«çœ®ããŠããŸãïŒã¡ãŒã«ïŒã L1ã¯ãŸã 䜿çšãããŠããŸãããããããžã§ã¯ãã®1ã€ã§èª€ã£ãŠäœ¿çšããŠããŸããã
STM32CubeãéããããŒããéžæããFreeRTOSã®æšªã®ãã§ãã¯ããã¯ã¹ããªã³ã«ããŠãéåžžã©ãããããžã§ã¯ããçµã¿ç«ãŠãŸãã ãã®ãããªãã®ã¯å¿ èŠãªãã®ã§ãããã©ã«ãã§ãã¹ãŠãæ®ããŸãã
FreeRTOSãšã¯äœã§ããïŒ ããã¯ããã€ã¯ãã³ã³ãããŒã©ãŒçšã®ã»ãŒãªã¢ã«ã¿ã€ã ã®ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã§ãã ã€ãŸãããã«ãã¿ã¹ã¯ãã»ããã©ããã®ä»ã®ãã¥ãŒããã¯ã¹ãªã©ã®ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã«ã€ããŠèããããšããããã¹ãŠã§ãã FreeRTOSãéžã¶çç± STM32CubeããµããŒãããŠããã ãã§ã;-)ã ä»ã®åæ§ã®ã·ã¹ãã ããããããããŸã-åãChibiOSã§ãã æ¬è³ªçã«ããããã¯ãã¹ãŠåãã§ãããããŒã ãšãã®åœ¢åŒãç°ãªãã ãã§ãã ããã§ã¯ããªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã®æäœã«é¢ããæ¬ãæ瀺ã®å±±ãæžãçŽãã€ããã¯ãããŸãããããã°ã©ããŒã®å€§å€ãªä»äºã«éåžžã«åœ¹ç«ã€æãèå³æ·±ãããšã®åºãã¹ãããŒã¯ãå®è¡ããã ãã§ãã
ããŠãç§ã¯åœŒããã€ã³ã¿ãŒãããã§ãããèªã¿ãã€ã³ã¹ãã¬ãŒã·ã§ã³ãåŸããšèããŸãã äœãå€ãã£ãããèŠã
main.cã®å é ã®ã©ããã«
static void StartThread(void const * argument);
ãã¹ãŠã®åæåã®åŸ
/* Create Start thread */ osThreadDef(USER_Thread, StartThread, osPriorityNormal, 0, configMINIMAL_STACK_SIZE); osThreadCreate (osThread(USER_Thread), NULL); /* Start scheduler */ osKernelStart(NULL, NULL);
ãããŠã1ã€ã®ç¡éã«ãŒããšosDelayïŒ1ïŒãæã€ç©ºã®StartThreadã
ã³ã£ããããïŒ äžæ¹ãããªãã䜿çšããæ©èœã®ã»ãŒ90ïŒ ã«ãªãåã«ã æåã®2è¡ã¯éåžžã®åªå 床ã§ã¹ã¬ãããäœæããæåŸã®è¡ã¯ã¿ã¹ã¯ã¹ã±ãžã¥ãŒã©ãèµ·åããŸãã ãããŠããã®ãã¹ãŠã®çŽ æŽãããã¯6ãããã€ãã®ãã©ãã·ã¥ã«åãŸããŸãã
ããããäœæ¥ã確èªããå¿ èŠããããŸãã osDelayã次ã®ã³ãŒãã«å€æŽããŸã
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_8,GPIO_PIN_RESET); osDelay(500); HAL_GPIO_WritePin(GPIOE,GPIO_PIN_8,GPIO_PIN_SET); osDelay(500);
ã³ã³ãã€ã«ããŠå¡ãã€ã¶ããŸãã ãã¹ãŠãæ£ããå®äºãããšãéè²ã®LEDãç¹æ» ããŸãïŒPE8-PE15ã®STM32F3Discoveryã«å€æ°ã®LEDãã¯ãã ä»ããããŠãããããå¥ã®ããŒããããå Žåã¯ã³ãŒããå€æŽããŸãïŒ
次ã«ãåLEDã®æ©èœãååŸããŠè€è£œããŸãã
static void PE8Thread(void const * argument); static void PE9Thread(void const * argument); static void PE10Thread(void const * argument); static void PE11Thread(void const * argument); static void PE12Thread(void const * argument); static void PE13Thread(void const * argument); static void PE14Thread(void const * argument); static void PE15Thread(void const * argument);
åLEDã«ã¹ããªãŒã ãè¿œå ããŸã
osThreadDef(PE8_Thread, PE8Thread, osPriorityNormal, 0, configMINIMAL_STACK_SIZE); osThreadCreate (osThread(PE8_Thread), NULL);
ãããŠã³ãŒããåãããŠLEDãç¹ç¯ãããŸã
static void PE8Thread(void const * argument) { for(;;) { HAL_GPIO_WritePin(GPIOE,GPIO_PIN_8,GPIO_PIN_RESET); osDelay(500); HAL_GPIO_WritePin(GPIOE,GPIO_PIN_8,GPIO_PIN_SET); osDelay(500); } }
äžè¬çã«ããã¹ãŠãåãã§ãã
ã³ã³ãã€ã«ããèšå ¥ããå³ãååŸããŸãã å®äºã LEDã¯ç¹æ» ããŸããã
commentã¡ãœããã«ããã²ã©ããããã°ã«ããã3ã€ã®ã¹ã¬ãããåäœããŠããã4ã€ã®ã¹ã¬ããã¯ããåäœããŠããªãããšãããããŸãã åé¡ã¯äœã§ããïŒ åé¡ã¯ãã·ã§ã«ããšã¹ã¿ãã¯ã«å²ãåœãŠãããã¡ã¢ãªã«ãããŸãã
FreeRTOSConfig.hã確èªããŸã
#define configMINIMAL_STACK_SIZE ((unsigned short)128) #define configTOTAL_HEAP_SIZE ((size_t)3000)
ãã¹ãŠã«3000ãã€ããåã¿ã¹ã¯ã«ã¯128ãã€ãããããŸãã ããã«ãä»ã®ã©ããã«ãã¿ã¹ã¯ã«é¢ããæ å ±ããã®ä»ã®æçšãªæ å ±ãä¿åããå¿ èŠããããŸãã ãã®ãããäœãããªããšãååãªã¡ã¢ãªããªãå Žåã§ãã¹ã±ãžã¥ãŒã©ã¯èµ·åããŸããã
äºå®ããå€æãããšãå®å šãªæé©åãæå¹ã«ãããšãFreeRTOSèªäœã¯250ãã€ãããããŸãã ããã«ãã¿ã¹ã¯ããšã«ãã¹ã¿ãã¯çšã«128ãã€ããå éšãªã¹ãçšã«64ãã€ããã¿ã¹ã¯åçšã«16ãã€ãã 250 + 3 *ïŒ128 + 64 + 16ïŒ= 874ãèæ ®ããŸãã ãããã€ãã§ãå±ããªãã ãããŠãæã ã¯3ãæã£ãŠããŸã...
åé¡ã¯äœã§ããïŒ STM32Cubeã§æäŸãããFreeRTOSã®ããŒãžã§ã³ã¯å€ãããŠïŒ7.6.0ïŒvTaskInfoãååŸã§ããªããããç§ã¯èã«è¡ããŸãïŒ
ã¹ããªãŒã ãäœæããååŸã«ã以äžãèšå®ããŸãïŒfreã¯éåžžã®size_tã§ãïŒ
fre=xPortGetFreeHeapSize();
ãã¬ãŒã¯ãã€ã³ãã«åºå®ããŠã次ã®æ°åãååŸããŸããã¿ã¹ã¯ãäœæããåã«2376ãã€ãã1768åŸã«ç©ºããã€ãããããŸãããã€ãŸãã1ã€ã®ã¿ã¹ã¯ã«608ãã€ããè²»ããããŠããŸãã ããäžåºŠç¢ºèªããŠãã ããã çªå·2992-2376-1768-1160ãååŸããŸãã çªå·ã¯åãã§ãã åçŽãªè«çççµè«ã«ããããã¡ãã¯ã¹ããã®ãããã®çªå·ã¯ãæé©åããªã³ã«ãªãããã¹ãŠã®çš®é¡ã®ã¢ãžã¥ãŒã«ããªãã«ãªã£ããäžéšã®ãããããã»ããµã«å¯ŸããŠååŸãããããšãããããŸãã ããã«èª¿ã¹ãŠãã·ã§ãã©ãŒã®éå§ãçŽ580ãã€ããæ¶è²»ããããšãç解ããŸãã
äžè¬ã«ãæå°ã¹ã¿ãã¯ã䜿çšããŠã¿ã¹ã¯ããšã«610ãã€ããOSèªäœã«ããã«580ãã€ããèšç®ããŸãã TOTAL_HEAP_SIZEã®åèšã¯ã610 * 9 + 580 = 6070ãšèšè¿°ããå¿ èŠããããŸãã äžžããŠ6100ãã€ããäžããŸã-é£ã¹ãããŸãã
ãã¹ãŠã®LEDãäžåºŠã«ç¹æ» ããæ§åãç·šéãèšå ¥ã芳å¯ããŸãã ã¹ã¿ãã¯ã6050ã«åæžããããšããŠããŸããããã¯ãäœãæ©èœããŸããã ã ããç§ãã¡ã¯æ£ããèšç®ããŸãã:)
ããã§ãåLEDã«ãã€ã³ãã«ã¹ããšãäžæåæ¢ãã®ç¬èªã®ééãèšå®ã§ããŸãã ååãšããŠãFreeRTOSãæŽæ°ããããã³ãŒããæ³èµ·ãããå Žåã0.01msã®ç²ŸåºŠãäžããã®ã¯ç°¡åã§ãïŒããã©ã«ãã§ã¯ã1ãã£ãã¯ã¯1msã§ãïŒã
åæããŠã8ã€ã®ã¿ã¹ã¯ãåç¬ã§åŠçããããšã¯ãåæã«8ã€ã®ã¿ã¹ã¯ãåŠçãããããã¯ããã«å¿«é©ã§ããïŒ å®éã«ã¯ããããžã§ã¯ãã§ã¯éåžž30ã40ã¹ã¬ããã§ã¹ãã³ããŸãã ããã°ã©ããŒãäœäººæ»ã¬ãããã¹ãŠã®åŠçã1ã€ã®é¢æ°ã«ããã·ã¥ãããšãèšç®ããã®ãæããªããŸã:)
次ã®ã¹ãããã¯ãåªå é äœã«å¯ŸåŠããå¿ èŠããããŸãã å®éã®å Žåãšåæ§ã«ãäžéšã®ã¿ã¹ã¯ã¯ä»ã®ã¿ã¹ã¯ããããåçãã§ãããããå€ãã®ãªãœãŒã¹ãå¿ èŠã§ãã ãŸãã1ã€ã®ãã©ãã·ã£ãŒããã©ãã·ã£ãŒã«äº€æããŸãããOSã®æ段ã§ã¯ãªãåçŽãªãµã€ã¯ã«ã§äžæåæ¢ãè¡ããããšãééã£ãŠè¡ãããŸãã
ã€ãŸããosDelayïŒïŒã®ä»£ããã«ããã®ãããªææãããã«æ¿å ¥ãããŸãã
unsigned long c; for(int i=0;i<1000000;i++) { c++; }
éåžžããµã€ã¯ã«æ°ã¯å®éšçã«éžæãããŸãïŒãã®ãããªé 延ãè€æ°ããå Žåã¯ãèšç®ã«å€ãã®é çã䌎ãããïŒã 麻é ã¯ãã³ãã³ãã®å®è¡æéãèšç®ã§ããŸãã
眮æãã³ã³ãã€ã«ãå®è¡ã LEDã¯ä»¥åãšåæ§ã«ç¹æ» ããŸãããã©ããããããåããé ããªããŸãã ãªã·ãã¹ã³ãŒãã§èŠããšãæ»ãããªå¢çïŒçãã50ããªç§ãšçãããªã50ããªç§ïŒã®ä»£ããã«ãå¢çã1ã2ããªç§æµ®ãã³å§ããããšãããããŸãïŒå¥åŠãªããšã«ãç®ã«ã¯èŠããŸãïŒã ãªãã§ïŒ FreeRTOSã¯ãªã¢ã«ã¿ã€ã ã·ã¹ãã ã§ã¯ãªãããããã®ãããªèªç±ã確ä¿ã§ããŸãã
ããã§ããã®ã¿ã¹ã¯ã®åªå 床ãosPriorityAboveNormalã«1ã¹ãããäžããŸãã å®è¡ããŠãå€ç¬ã«ç¹æ» ããLEDã確èªããŸãã ãªãã§ïŒ
ã¹ã±ãžã¥ãŒã©ãã¿ã¹ã¯ã«åªå é äœãä»ããããã§ãã 圌ã¯äœãèŠãŠããŸããïŒ ãã®åªå 床ã®é«ãã¿ã¹ã¯ã«ã¯ãåžžã«ããã»ããµãå¿ èŠã§ãã çµæã¯äœã§ããïŒ ä»ã®ã¿ã¹ã¯ã«ã¯äœæ¥æéããããŸããã
ãããŠãåªå 床ãéåžžããosPriorityBelowNormalã«1ã¹ãããäžããŸãã ãã®çµæãéåžžã®ã¿ã¹ã¯ã«äœæ¥ãå²ãåœãŠãã¹ã±ãžã¥ãŒã©ãŒã¯ãæ®ãã®ãªãœãŒã¹ããäžè¯ãã«ããŸãã
ãããããããã°ã©ãã®æåã®ã«ãŒã«ãç°¡åã«æšæž¬ã§ããŸããé¢æ°ã«äœã®é¢ä¿ããªãå Žåã¯ãã¹ã±ãžã¥ãŒã©ã«å¶åŸ¡ãæž¡ããŸãã
FreeRTOSã«ã¯2ã€ã®ãåŸ æ©ããªãã·ã§ã³ããããŸã
æåã®ãªãã·ã§ã³ã¯ãNãã£ãã¯ã ãåŸ ã€ãã§ãã ããªã«ã®ãªãéåžžã®ããŒãºïŒåœŒããã©ãã ãåŸ ã£ãŠããããã©ãã ãåŸ ã£ãŠãããã ããã¯vTaskDelayã§ãïŒosDelayã¯åãªãå矩èªã§ãïŒã å®è¡æãèŠããšã次ã®ãããªãã®ããããŸãïŒäŸ¿å©ãªã¿ã¹ã¯ãå®äºãããŸã§ã«24ããªç§ããããšä»®å®ããŸãïŒã
... [0ms]-ã³ã³ãããŒã«è»¢é-æäœ[24ms] 100msã§äžæåæ¢[124ms]-ã³ã³ãããŒã«è»¢é-æäœ[148ms] 100ms [248ms]ã§äžæåæ¢...
åäœã«å¿ èŠãªæéã®ãããæåã«æ³å®ããããã«ã100msããšã«ã³ã³ãããŒã«è»¢éãçºçããªãããšã¯ç°¡åã«ããããŸãã ãã®ãããªå Žåã«ã¯ãvTaskDelayUntilããããŸãã ããã«ãããã¿ã€ã ã©ã€ã³ã¯æ¬¡ã®ããã«ãªããŸã
... [0ms]-ã³ã³ãããŒã«è»¢é-æäœ[24ms] 76ms [100ms]ã§äžæåæ¢-ã³ã³ãããŒã«è»¢é-æäœ[124ms] 76ms [200ms]ã§äžæåæ¢...
ã芧ã®ãšãããã¿ã¹ã¯ã¯æ確ã«å®çŸ©ãããæéééã§å¶åŸ¡ãåãåããŸãã ã¹ã¬ããã®1ã€ã§ã¹ã±ãžã¥ãŒã©ã®ç²ŸåºŠã確èªããããã«ã1ããªç§éåæ¢ããããã«èŠæ±ããŸããã å³ã§ã¯ã9ã€ã®ã¹ã¬ããã§ã®äœæ¥ã®ç²ŸåºŠãè©äŸ¡ã§ããŸãïŒStartThreadãå¿ããªãã§ãã ããïŒ
人ã ã¯åªå é äœãä»ããŠã²ãŒã ã«æ²¡é ããããã€å£ããã®ãããææ¡ããŠããã®ã§ãç§ã¯éåžžããã§çµãããŸãã
ãã¹ãŠã®ãœãŒã¹ã³ãŒããå«ãå®å šã«çµã¿ç«ãŠããããããžã§ã¯ãã¯ã kaloshin.ru / stm32 / freertos / stage1.rarã§ååŸã§ããŸãã
ç¶ã2.ã»ããã©ã«ã€ããŠ