156ãã€ãã®Cã³ãŒã
int main(void) { int testArray[5U] = {-1,20,-3,0,4}; int lowest = INT_MAX; for (int i = 0; i < 5; i++) { lowest = ((lowest < testArray[i]) ? lowest : testArray[i]); }; return 0;
ãããŠããã®ã¢ã»ã³ãã©ãŒè¡šçŸ
152ãã€ããå ããC ++ã³ãŒã
int main() { int testArray[5U] = {-1, 20, -3, 0, 4}; int lowest = std::numeric_limits<int>.max(); for (auto it: testArray) { lowest = ((lowest < it) ? lowest : it); }; return 0;
ãããŠããã®ã¢ã»ã³ãã©ãŒè¡šçŸ
ã芧ã®ãšãããã³ã³ãã€ã©ã«ãã£ãŠçæãããC ++ã³ãŒãã¯4ãã€ãå°ãªããé床ã¯12ã¯ããã¯ãµã€ã¯ã«é«éã§ãã ãããã¯ãã¹ãŠãC ++ 14ã®æ°æ©èœã«ããå®çŸãããŠããŸãã ãã¡ãããäž¡æ¹ã®ã³ã³ãã€ã©ã®æé©åããªãã«ãªã£ãŠããããšãããããŸããããã¯å®éã®å®è£ ãšã¯é¢ä¿ã®ãªãéåžžã«ç·åçãªãã¹ãã§ããããã¹ãŠãããã»ã©åçŽã§ã¯ãªããšããããšã¯èšããŸãã
ãã€ã¯ãã³ã³ãããŒã©ã®ããã°ã©ãã³ã°æ©èœãèæ ®ããå¿ èŠããããŸããå°éã®ããã°ã©ã ã¡ã¢ãª32.64..512 kBãããã«å°ãªãRAMããã³ãã€ã¯ãããã»ããµã®äœåšæ³¢æ°ïŒç¹ã«äœé»åã»ã³ãµãŒã«äœ¿çšããå ŽåïŒã®èŠä»¶ã«ããå¶éã課ããããããã§ãã ãããŠãèªä¿¡ãæã£ãŠããã¹ãŠã®C ++æ©èœã圹ç«ã€ãšã¯éããŸããã ããšãã°ãæšæºãã³ãã¬ãŒãã©ã€ãã©ãªã䜿çšãããšã倧éã®ãªãœãŒã¹ãæ¶è²»ããå¯èœæ§ããããŸãããŸããäŸå€ãã³ãã©ãªã©ã®æ å ±ãæ ŒçŽããããã«ã¹ã¿ãã¯ãšã³ãŒãã®ãµã€ãºãå€§å¹ ã«å¢ããå¿ èŠããããããå°ããªãã€ã¯ãã³ã³ãããŒã©ãŒã®ãããžã§ã¯ãããäŸå€ãå®å šã«ã¹ããŒãããå¯èœæ§ããããªã©ã倧ããªC ++ã®äžçã§ã¯åœŒã®ãããªãæ€çŽ¢ã ãããã£ãŠãå°èŠæš¡ãªãããžã§ã¯ãã§C ++ãšãã®æ°æ©èœã䜿çšããæ¹æ³ã説æããè¯å¿ã®æºããããªããã°Cã®ä»£ããã«C ++ã䜿çšã§ããããšã瀺ããŸãã
ã¿ã¹ã¯ã決å®ããããã«æåã«å¿ èŠãªããšã ããã¯ååã«ã·ã³ãã«ã§ããå¿ èŠããããŸãããããšãã°ã ãã¯ããå®å šã«æŸæ£ã ããã€ã³ã¿ããéãããæããªãšã©ãŒã®ãªã¹ã¯ãæžãããªã©ã®æ¹æ³ã瀺ãã®ã«ååãªãã®ã§ãªããã°ãªããŸãã...
ãã€ãã®ããã«ãéžæè¢ã¯LEDã«ããã£ãŠããŸããã
èªè ãç§ãã¡ãäœããããã®ããç解ããããã«ããã€ã¯ãã³ã³ãããŒã©ãŒã«å®è£ ããªããã°ãªããªãã¿ã¹ã¯ã®æçµããŒãžã§ã³ã瀺ããŸãã
- äžå€ããŒãXNUCLEO-F411RE
- ãœãããŠã§ã¢ã¯ã16 MHzã®å€éšåšæ³¢æ°ã§åäœããSTMF411Rãã€ã¯ãããã»ããµã§å®è¡ããå¿ èŠããããŸãã
- ãœãããŠã§ã¢ã¯ãããŒãã«æ¥ç¶ãããããŒãäžã®4ã€ã®LEDïŒLED 1-GPIOA.5ãLED 2-GPIOC.9ãLED 3-GPIOC.8ãLED 4-GPIOC.5ïŒã®å¶åŸ¡ããµããŒãããå¿ èŠããããŸãã
- ãœãããŠã§ã¢ã¯3ã€ã®LEDå¶åŸ¡ã¢ãŒãããµããŒãããå¿ èŠããããŸãïŒããªã³ããŒã³-ãã¹ãŠã®LEDã亀äºã«ç¹ç¯ãã次ã«åãé åºã§äº€äºã«æ¶ç¯ããŸãããã§ã¹-æåã¯å¶æ°ã®LEDãç¹ç¯ããå¥æ°ã®LEDãæ¶ç¯ããéãåæ§ã§ãããã¹ãŠã®ã¢ãŒã-ãã¹ãŠã®LEDãç¹ç¯ããŠããæ¶ç¯ããŸãïŒã LEDã®ç¶æ æéã®å€æŽ-1ç§
- ãœãããŠã§ã¢ã¯ãFir-tree-Chess-Allã·ãŒã±ã³ã¹ã§åŸªç°é åºã§GPIOC.13ããŒãã«æ¥ç¶ããããã¿ã³ã䜿çšããŠãLEDå¶åŸ¡ã¢ãŒãã®å€æŽããµããŒãããå¿
èŠããããŸãã
ãããã顧客ããã®æçµçãªèŠä»¶ã®èŠãæ¹ã§ãã ããããéåžžã¯å®éã«çºçãããããæåã«é¡§å®¢ã¯ããç°¡åãªã¢ã€ãã¢ãæãã€ããããã圌ã¯å®å šãªå¹žçŠã®ããã«æããå åãã€ãŸã1ç§ããšã«ç·è²ã®LEDãç¹æ» ãããããšã«æ¬ ãããšå€æããŸããã ãã®ã¿ã¹ã¯ã¯ãSnezhinkaãšããååã®ããã°ã©ããŒã«ãã£ãŠéå§ãããŸããã
ãããã£ãŠãããŒãã«ã¯4ã€ã®LEDãLED1ãLED2ãLED3ãLED4ããããŸãã ãããã¯ããããããŒãGPIOA.5ãGPIOC.5ãGPIOC.8ãGPIOC.9ã«æ¥ç¶ãããŠããŸãã ããã§ã¯ãGPIOAã«ããLED1ã䜿çšããŸãã
ãŸããããã°ã©ããŒã®Snezhinkaã¯ãLEDãåãæ¿ãããããªåçŽãªCã³ãŒããäœæããŸããã 次ã®ããã«ãªããŸãã
int main() { GPIOC->ODR ^= GPIO_ODR_OD5; // LED1 Delay(1000U); GPIOC->ODR ^= GPIO_ODR_OD5; // , return 0; }
ã³ãŒãã¯ããŸãæ©èœããæ£ããåäœããŸããSnezhinkaã¯äœæ¥ã«æºè¶³ããäŒæ¯ããŸããã ããããããŒãã®é ç·ãšãŠãŒã¶ãŒã®ããŒã«æŒç®ã®è€éããæªçãªããããã®ã³ãŒãã¯å®å šã«æ確ã§ã¯ãªããããSnezhinkaã¯LEDãGPIOA.5ããŒãã«ããããšã説æããã³ã¡ã³ããè¿œå ããå¿ èŠããããå®éã«åãæ¿ããããšèããŠããŸãã
ãã®ãããªã³ãŒãã人éã®èšèªã§ã©ã®ããã«èŠãããã«ã€ããŠèããŠã¿ãŸãããã 次ã®ããã«ãªããŸãã
Toggle Led1 then Delay 1000ms then Toggle Led1
ã芧ã®ãšãããããã§ã¯ã³ã¡ã³ãã¯äžèŠã§ããããã®ãããªã³ãŒãã®ç®çã¯çŽæçã§ãã æã泚ç®ãã¹ãããšã¯ããã®æ¬äŒŒã³ãŒããC ++ã®ã³ãŒããšã»ãŒå®å šã«äžèŽããããšã§ãã èŠãŠãå¯äžã®éãã¯ãã©ã®ããŒãããªã³ã«ãªã£ãŠãããã瀺ãããšã«ãã£ãŠããŸãLEDãäœæããå¿ èŠãããããšã§ãã
int main() { Led Led1(*GPIOA, 5U); Led1.Toggle(); Delay(1000U); Led1.Toggle(); return 0; }
å®å
šãªã³ãŒã
startup.cpp
#pragma language = extended #pragma segment = "CSTACK" extern "C" void __iar_program_start( void ); class DummyModule { public: static void handler(); }; typedef void( *intfunc )( void ); //cstat !MISRAC++2008-9-5-1 typedef union { intfunc __fun; void * __ptr; } intvec_elem; #pragma location = ".intvec" //cstat !MISRAC++2008-0-1-4_b !MISRAC++2008-9-5-1 extern "C" const intvec_elem __vector_table[] = { { .__ptr = __sfe( "CSTACK" ) }, __iar_program_start, DummyModule::handler, DummyModule::handler, DummyModule::handler, DummyModule::handler, DummyModule::handler, 0, 0, 0, 0, DummyModule::handler, DummyModule::handler, 0, DummyModule::handler, DummyModule::handler, //External Interrupts DummyModule::handler, //Window Watchdog DummyModule::handler, //PVD through EXTI Line detect/EXTI16 DummyModule::handler, //Tamper and Time Stamp/EXTI21 DummyModule::handler, //RTC Wakeup/EXTI22 DummyModule::handler, //FLASH DummyModule::handler, //RCC DummyModule::handler, //EXTI Line 0 DummyModule::handler, //EXTI Line 1 DummyModule::handler, //EXTI Line 2 DummyModule::handler, //EXTI Line 3 DummyModule::handler, //EXTI Line 4 DummyModule::handler, //DMA1 Stream 0 DummyModule::handler, //DMA1 Stream 1 DummyModule::handler, //DMA1 Stream 2 DummyModule::handler, //DMA1 Stream 3 DummyModule::handler, //DMA1 Stream 4 DummyModule::handler, //DMA1 Stream 5 DummyModule::handler, //DMA1 Stream 6 DummyModule::handler, //ADC1 0, //USB High Priority 0, //USB Low Priority 0, //DAC 0, //COMP through EXTI Line DummyModule::handler, //EXTI Line 9..5 DummyModule::handler, //TIM9/TIM1 Break interrupt DummyModule::handler, //TIM10/TIM1 Update interrupt DummyModule::handler, //TIM11/TIM1 Trigger/Commutation interrupts DummyModule::handler, //TIM1 Capture Compare interrupt DummyModule::handler, //TIM2 DummyModule::handler, //TIM3 DummyModule::handler, //TIM4 DummyModule::handler, //I2C1 Event DummyModule::handler, //I2C1 Error DummyModule::handler, //I2C2 Event DummyModule::handler, //I2C2 Error DummyModule::handler, //SPI1 DummyModule::handler, //SPI2 DummyModule::handler, //USART1 DummyModule::handler, //USART2 0, DummyModule::handler, //EXTI Line 15..10 DummyModule::handler, //EXTI Line 17 interrupt / RTC Alarms (A and B) through EXTI line interrupt DummyModule::handler, //EXTI Line 18 interrupt / USB On-The-Go FS Wakeup through EXTI line interrupt 0, //TIM6 0, //TIM7 f0 0, 0, DummyModule::handler, //DMA1 Stream 7 global interrupt fc 0, DummyModule::handler, //SDIO global interrupt DummyModule::handler, //TIM5 global interrupt DummyModule::handler, //SPI3 global interrupt 0, // 110 0, 0, 0, DummyModule::handler, //DMA2 Stream0 global interrupt 120 DummyModule::handler, //DMA2 Stream1 global interrupt DummyModule::handler, //DMA2 Stream2 global interrupt DummyModule::handler, //DMA2 Stream3 global interrupt DummyModule::handler, //DMA2 Stream4 global interrupt 130 0, 0, 0, 0, 0, 0, DummyModule::handler, //USB On The Go FS global interrupt, 14C DummyModule::handler, //DMA2 Stream5 global interrupt DummyModule::handler, //DMA2 Stream6 global interrupt DummyModule::handler, //DMA2 Stream7 global interrupt DummyModule::handler, //USART6 15C DummyModule::handler, //I2C3 Event DummyModule::handler, //I2C3 Error 164 0, 0, 0, 0, 0, 0, 0, DummyModule::handler, //FPU 184 0, 0, DummyModule::handler, //SPI 4 global interrupt DummyModule::handler //SPI 5 global interrupt }; __weak void DummyModule::handler() { for(;;) {} }; extern "C" void __cmain( void ); extern "C" __weak void __iar_init_core( void ); extern "C" __weak void __iar_init_vfp( void ); #pragma required=__vector_table void __iar_program_start( void ) { __iar_init_core(); __iar_init_vfp(); __cmain(); }
utils.hpp
#ifndef UTILS_H #define UTILS_H #include <cassert> namespace utils { template<typename T, typename T1> inline void setBit(T &value, T1 bit) { assert((sizeof(T) * 8U) > bit); value |= static_cast<T>(static_cast<T>(1) << static_cast<T>(bit)); }; template<typename T, typename T1> inline void clearBit(T &value, T1 bit) { assert((sizeof(T) * 8U) > bit); value &= ~static_cast<T>(static_cast<T>(1) << static_cast<T>(bit)); }; template<typename T, typename T1> inline void toggleBit(T &value, T1 bit) { assert((sizeof(T) * 8U) > bit); value ^= static_cast<T>(static_cast<T>(1) << static_cast<T>(bit)); }; template<typename T, typename T1> inline bool checkBit(const T &value, T1 bit) { assert((sizeof(T) * 8U) > bit); return !((value & (static_cast<T>(1) << static_cast<T>(bit))) == static_cast<T>(0U)); }; }; #endif
led.hpp
#ifndef LED_H #define LED_H #include "utils.hpp" class Led { public: Led(GPIO_TypeDef &portName, unsigned int pinNum) : port(portName), pin(pinNum) {}; inline void Toggle() const { utils::toggleBit(port.ODR, pin); } inline void SwitchOn() const { utils::setBit(port.ODR, pin); } inline void SwitchOff() const { utils::clearBit(port.ODR, pin); } private: GPIO_TypeDef &port; unsigned int pin; }; #endif
main.cpp
#include <stm32f411xe.h> #include "led.hpp" extern "C" { int __low_level_init(void) { // 16 RCC->CR |= RCC_CR_HSION; while ((RCC->CR & RCC_CR_HSIRDY) != RCC_CR_HSIRDY) { } // RCC->CFGR |= RCC_CFGR_SW_HSI; while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI) { } // RCC->AHB1ENR |= (RCC_AHB1ENR_GPIOAEN); //LED1 PortA.5, PortA.5 GPIOA->MODER |= GPIO_MODER_MODE5_0; return 1; } //, inline void Delay(unsigned int mSec) { for (unsigned int i = 0U; i < mSec * 3000U; i++) { __NOP(); }; } } int main() { Led Led1(*GPIOA, 5U); Led1.Toggle(); Delay(1000U); Led1.Toggle(); return 0; }
ããããªã¹ãã®ããã°ã©ããŒã¯ãã³ãŒãã¯ããç解ãããããšèšããããããŸããããåé·ã§ããããªããžã§ã¯ããäœæãããã³ã³ã¹ãã©ã¯ã¿ãŒãã¡ãœãããåŒã³åºãããRAMãšè¿œå ã³ãŒãã®éãçæãããŸãã ããããã¢ã»ã³ãã©ãŒã®ãªã¹ããèŠããšãäž¡æ¹ã®ã³ã³ãã€ã©ãŒã§ã€ã³ã©ã€ã³é¢æ°ãªãã·ã§ã³ãæå¹ã«ãªã£ãŠããC ++ã³ãŒãã®ãµã€ãºãCããã°ã©ã ã®ãµã€ãºãšåãã§ãããã¡ã€ã³é¢æ°ãåŒã³åºããšããç¹æ®æ§ã®ããã«ãäžè¬çãªC +ã³ãŒã+ 1ã€å°ãªãåœä»€ã
CãœãŒã¹ããã®ã¢ã»ã³ãã©ãŒã³ãŒã
C ++ãœãŒã¹ããã®ã¢ã»ã³ãã©ãŒã³ãŒã
ããã¯ãææ°ã®ã³ã³ãã€ã©ãŒããããªãã®çŽ æŽããããŠç解ããããC ++ã³ãŒããæé©ãªã¢ã»ã³ãã©ãŒã³ãŒãã«å€ãããšããä»äºãããŠãããšããäºå®ãããäžåºŠç¢ºèªããŸãã ãŸãããã¹ãŠã®ã¢ã»ã³ãã©ããã°ã©ãããã®ã¬ãã«ã®æé©åãå®çŸã§ããããã§ã¯ãããŸããã
ãã¡ãããæé©åãç¡å¹ã«ãããšãC ++ã³ãŒãã¯ã¹ã¿ãã¯ãµã€ãºãšé床ã®ç¹ã§ããã»ã©ã³ã³ãã¯ãã«ãªããŸããã æ¯èŒã®ããã«ãã³ã³ã¹ãã©ã¯ã¿ãŒãšã¡ãœãããåŒã³åºããŠãæé©åãããŠããªãããŒãžã§ã³ãæäŸããŸãã
ç§ã«ãšã£ãŠãã»ã³ãµãŒãã¡ãŒã ãŠã§ã¢ïŒã©ã®èŠçŽ ãã©ã®ããŒãã«æ¥ç¶ãããŠããããçŸåšã®ç¶æ ãããŒããŸãã¯ãããŸãã¯ãã®ã¢ãžã¥ãŒã«ãªã©ïŒãèšè¿°ããããã«äžå¿ èŠãªè©³çŽ°ãé ã®äžã«ä¿æããããšãšãã³ãŒãã®åçŽããšããããããã®éã«ãžã¬ã³ãã¯ãããŸããã çµå±ãããã€ã¹ã®ããžãã¯ããŠãŒã¶ãŒã€ã³ã¿ã©ã¯ã·ã§ã³ã€ã³ã¿ãŒãã§ã€ã¹ãèšè¿°ããèšç®ãå®è£ ããå¿ èŠããããŸãããADCããããŒã¿ãèªã¿åãããã«ã¯ããŸãGPIOA.3ããŒãã«ããCSä¿¡å·ã䜿çšããŠãããéžæããã€ã³ã¹ããŒã«ããå¿ èŠãããããšãå¿ããªãã§ãã ãããã®åäœã ADCã¢ãžã¥ãŒã«ã®éçºè ã«ãããè¡ãããŠãã ããã
æåã¯ãå€ãã®è¿œå ã³ãŒããæžãå¿ èŠãããããã«æããããããããŸããããã¢ããªã±ãŒã·ã§ã³ãLEDãç¹æ» ããããããå°ãè€éã«ãªã£ãå Žåãããã¯å ±ããã以äžã§ãã
å²ãåœãŠã«æ»ããŸãã Snezhinkaã¯ä»äºã®çµæã顧客ã«ç€ºãæéãæã£ãŠããŸããã§ããã顧客ãå€ã«ç¹æ» ããLEDã®é åãæããŠãã¯ãªã¹ãã¹ããªãŒã¢ãŒãã§4ã€ã®LEDãç¹æ» ãããã®ã¯è¯ãããšã ãšå€æããŸããã
è€æ°ã®ãããžã§ã¯ããåæã«å®è¡ããŠããããã°ã©ããŒã®ã¹ãã·ã³ã«ã¯ãä¿¡é Œæ§ãé«ãç解ããããæ¹æ³ã§ãããšèããŠãããããæéãç¯çŽããé¡ãæ倧éã«æŽ»çšããããšã«æ±ºããŸããã
#define TOGGLE_BIT(A,B) ((A) ^= (1U << ((B) & 31UL))) #define SET_BIT(A,B) ((A) |= (1U << ((B) & 31UL))) int main(void) { // SET_BIT(GPIOC->ODR, 5U); SET_BIT(GPIOC->ODR, 8U); SET_BIT(GPIOC->ODR, 9U); SET_BIT(GPIOA->ODR, 5U); // for (;;) { Delay(1000U); TOGGLE_BIT(GPIOC->ODR, 5U); Delay(1000U); TOGGLE_BIT(GPIOC->ODR, 8U); Delay(1000U); TOGGLE_BIT(GPIOC->ODR, 9U); Delay(1000U); TOGGLE_BIT(GPIO->ODR, 5U); //: TOGGLE_BIT(GPIOA->ODR, 5U } return 0; }
ã³ãŒãã¯æ©èœããŸãããæåŸã®ãšã³ããªTOGGLE_BITïŒGPIO-> ODRã5UïŒã«æ³šæããŠãã ããã LED 1ãš4ã¯ã¬ãã°çªå·5ã«ãããŸãããç°ãªãããŒãã«ãããŸãã Ctrl C-Ctrl Vã䜿çšããŠãSnowflakeã¯æåã®ã¬ã³ãŒããã³ããŒããããŒãã®å€æŽãå¿ããŸããã ããã¯ã管çè ã®ãã¬ãã·ã£ãŒã®ããšã§äœæ¥ããããã°ã©ããŒã«ããå žåçãªééãã§ãããæéããæšæ¥ãã«èšå®ããŸãã åé¡ã¯ãã¿ã¹ã¯ã®ããã«ã³ãŒããè¿ éã«èšè¿°ããå¿ èŠããããSnezhinkaããœãããŠã§ã¢èšèšã«ã€ããŠèããæéããªãããã座ã£ãŠå¿ èŠãªãã®ãæžããã ãã§ãã»ãã®å°ãã®ãã¿ãèš±ããŸãããããã€ã¹ã®ãã¡ãŒã ãŠã§ã¢ã ãã ãã圌ã¯ããã«æéãè²»ããããšãç解ããå¿ èŠããããŸãã ããã«ãSnezhinkaã¯2ã€ã®æããããã¯ããè¿œå ããŸãããããã¯ã圌ã®æèŠã§ã¯äœæ¥ã容æã«ããŸãã åã®C ++ã®äŸã§ã¯ããããã®ãã¯ãããã°ãããçµã¿èŸŒã¿é¢æ°ã§çœ®ãæããããã®ã³ãŒããªã©ãéåžžã«å€ãã®ã³ãŒããè¿œå ããŸããã ãªãã§ïŒ
éåžžã«äººæ°ã®ãããããèšå®ãã¯ããèŠãŠã¿ãŸãããã ããã䜿çšããŠãä»»æã®æŽæ°åã®ããããèšå®ã§ããŸãã
#define SET_BIT(A,B) (A |= (1 << B)) int main() { unsigned char value = 0U; SET_BIT(value, 10); return 0; }
1ã€ãé€ããŠããã¹ãŠãéåžžã«çŸããèŠããŸã-ãã®ã³ãŒãã«ãšã©ãŒããããç®çã®ããããèšå®ãããŸããã SET_BITãã¯ãã䜿çšããŠãå€å€æ°ã«10ããããèšå®ãããŸããããã¯ããµã€ãºã8ãããã§ãã å€æ°å®£èšããã¯ãåŒã³åºãã«ããã»ã©è¿ããªãå Žåãããã°ã©ããŒã¯ã©ã®ããããã®ãããªãšã©ãŒãæ¢ãã®ã ãããïŒ ãã®ã¢ãããŒãã®å¯äžã®å©ç¹ã¯ãã³ãŒããæå°ãµã€ãºãå æãããšããçãã®ãªãäºå®ã§ãã
æœåšçãªãšã©ãŒãåé¿ããããã«ããã®ãã¯ãããã³ãã¬ãŒãé¢æ°ã«çœ®ãæããŸããã
template<typename T, typename T1> inline void setBit(T &value, T1 bit) { assert((sizeof(T) * 8U) > bit); value |= static_cast<T>(static_cast<T>(1) << static_cast<T>(bit)); };
ããã§ãçµã¿èŸŒã¿ã®setBité¢æ°ã¯ãããããšãããçªå·ãèšå®ãããã©ã¡ãŒã¿ãŒãžã®åç §ãåããŸãã ãã®é¢æ°ã¯ãä»»æã®ã¿ã€ãã®ãã©ã¡ãŒã¿ãŒãšãããçªå·ã䜿çšã§ããŸãã ãã®å Žåããããæ°ããã©ã¡ãŒã¿ãŒã¿ã€ãã®ãµã€ãºãè¶ ããªãããã«ãããããã€ãŸãããã®ã¿ã€ãã®ãã©ã¡ãŒã¿ãŒã§ãããã確å®ã«èšå®ã§ããããã«ããããã«ãã¢ãµãŒãé¢æ°ã䜿çšããŠãã§ãã¯ãè¡ããŸãã asserté¢æ°ã¯å®è¡æã«æ¡ä»¶ããã§ãã¯ããæ¡ä»¶ãæºããããå Žåãã³ãŒãã¯ããã«å®è¡ãç¶ããŸãããæ¡ä»¶ãæºããããªãå Žåãããã°ã©ã ã¯ãšã©ãŒã§çµäºããŸãã asserté¢æ°ã®ãããã¿ã€ãã®èª¬æã¯ãcassertãã¡ã€ã«ã«ãããæ¥ç¶ããå¿ èŠããããŸãã ãã®ãããªãã§ãã¯ã¯ãéçºäžã«åœ¹ç«ã¡ãŸããçªç¶èª°ãã誀ã£ãå ¥åãã©ã¡ãŒã¿ãŒãæž¡ãããšã«ããå Žåãåäœäžã«åäœãããšãã«ããã«æ°ä»ãã§ãããã 補åã³ãŒãã§ã¯ãå ¥åãã©ã¡ãŒã¿ãŒãã§ãã¯ã䜿çšããŠãæå³ããªãããšã¯æããã§ããããã¯å®è¡ãããããé床ãäœäžããããã«ãéçºäžã«ç¡å¹ãªãã©ã¡ãŒã¿ãŒãæž¡ãå¯èœæ§ããã¹ãŠãã£ããããããããœãŒã¹ãã¡ã€ã«ã§NDEBUGæåãå®çŸ©ããããšã§ã¢ãµãŒããç¡å¹ã«ã§ããŸããŸãã¯ããããžã§ã¯ãå šäœã«å¯ŸããŠå®çŸ©ããããšã«ããã
ã€ã³ã©ã€ã³ããŒã¯ãŒãã«æ³šæããŠãã ããã ãã®ããŒã¯ãŒãã¯ãã³ã³ãã€ã©ãŒã«ããã®é¢æ°ãçµã¿èŸŒã¿ãšèŠãªãããããšãäŒããŸãã ããªãã¡ ã³ã³ãã€ã©ã¯åã«é¢æ°åŒã³åºãããã®ã³ãŒãã§çœ®ãæãããšä»®å®ããŸãããå®éã«ã¯ããã¯ã³ã³ãã€ã©ã®æé©åèšå®ã§ã®ã¿éæã§ããŸãã IARã¯ãŒã¯ãã³ãã§ã¯ãããã¯C / C ++ã³ã³ãã€ã©->æé©åã¿ãã®ãé¢æ°ã®ã€ã³ã©ã€ã³åããªãã·ã§ã³ã®é£ã®ãã§ãã¯ããã¯ã¹ã§ãã ãã®å Žåãé¢æ°ãé«éã§ããããã¯ããšåããããã®ã¹ããŒã¹ãå æããŸãã
Snowflakeã³ãŒãã«æ»ããæ¡åŒµæ§ã¯ã©ã®ããã«ãªã£ãŠããŸããïŒ
ã¹ããŒãã¬ãŒã¯ã³ãŒã
#define TOGGLE_BIT(A,B) ((A) ^= (1U << ((B) & 31UL))) #define SET_BIT(A,B) ((A) |= (1U << ((B) & 31UL))) int main(void) { // SET_BIT(GPIOC->ODR, 5U); SET_BIT(GPIOC->ODR, 8U); SET_BIT(GPIOC->ODR, 9U); SET_BIT(GPIOA->ODR, 5U); // for (;;) { Delay(1000U); TOGGLE_BIT(GPIOC->ODR, 5U); Delay(1000U); TOGGLE_BIT(GPIOC->ODR, 8U); Delay(1000U); TOGGLE_BIT(GPIOC->ODR, 9U); Delay(1000U); TOGGLE_BIT(GPIO->ODR, 5U); //: TOGGLE_BIT(GPIOA->ODR, 5U } return 0; }
çµå±ãã©ããã顧客ã¯ããã§æ¢ãŸãããLEDã4ã§ã¯ãªã40ã®å Žåã¯ã©ããªããŸããïŒ ã³ãŒããµã€ãºã¯10åã«çŽç·çã«å¢å ããŸãã ãšã©ãŒãçºçããå¯èœæ§ã¯åãéã ãå¢å ããã³ãŒããµããŒãã¯å°æ¥ã«ãŒãã³ã«ãªããŸãã
è³¢æãªCããã°ã©ãã¯ã次ã®ãããªã³ãŒããæžãããšãã§ããŸãã
int main(void) { tLed pLeds[] = {{ GPIOC, 5U },{ GPIOC, 8U },{ GPIOC, 9U },{ GPIOA, 5U }}; SwitchOnAllLed(pLeds, LEDS_COUNT); for (;;) { for (int i = 0; i < LEDS_COUNT; i++) { Delay(1000U); ToggleLed(&pLeds[i]); } } return 0; }
ã¡ã€ã³é¢æ°ã®ã³ãŒããå°ãªããªããæãéèŠãªããšã¯ãç°¡åã«æ¡åŒµã§ããããã«ãªã£ãããšã§ãã LEDã®æ°ãå¢ããå ŽåãLEDãæ¥ç¶ãããŠããããŒããLEDã¢ã¬ã€ã«åçŽã«è¿œå ãããã¯ãLEDS_COUNTãLEDã®æ°ã«å€æŽããã ãã§ååã§ãã ãã®å Žåãã³ãŒããµã€ãºã¯ãŸã£ããå¢å ããŸããã ãã¡ãããã¹ã¿ãã¯äžã«LEDã®ã¢ã¬ã€ãäœæããããã§ã«56ãã€ãã§ãããããã¹ã¿ãã¯ã®æ·±ãã¯å€§å¹ ã«å¢å ããŸãã
æåã®ãœãªã¥ãŒã·ã§ã³ãš2çªç®ã®ãœãªã¥ãŒã·ã§ã³ã®éã«ã¯åžžã«éžæè¢ããããç¹å®ã®å®è£ ã«ãšã£ãŠããéèŠã§ããã³ãŒããµã€ãºãæ¡åŒµæ§ãèªã¿ããããç°¡æœããå°ããããããRAMãµã€ãºãšé床ãå°ããããŸãã ç§ã®çµéšã§ã¯ã90ïŒ ã®ã±ãŒã¹ã§æåã®ãã®ãéžæã§ããŸãã
ãããããã®ã³ãŒãã詳ããèŠãŠã¿ãŸãããã ããã¯ããã€ã³ã¿ãŒãšãSET_BITïŒïŒãTOGGLE_BITïŒïŒãªã©ã®ãã¯ãã䜿çšããå žåçãªCã³ãŒãã§ãã ããã«é¢é£ããŠãæœåšçãªåé¡ã®ãªã¹ã¯ããããŸããããšãã°ãSwitchOnAllLedé¢æ°ïŒtLed * pLedãintãµã€ãºïŒã¯ããã€ã³ã¿ãŒãšé åãµã€ãºãåãåããŸãã ãŸãããã®é¢æ°ã«NULLãã€ã³ã¿ãŒãæž¡ãããšãçŠæ¢ãããã®ã¯äœããªãããšãç解ããå¿ èŠããããŸãããããã£ãŠããã€ã³ã¿ãŒãNULLã§ãªãããšã確èªããå¿ èŠããããŸããçµå±ã誀ã£ãŠãã€ã³ã¿ãŒãå¥ã®ãªããžã§ã¯ãã«æž¡ãããšãã§ããŸãã 第äºã«ãããã°ã©ããŒãé åã®å®£èšããããµã€ãºããã倧ãããµã€ãºãçªç¶æž¡ããå Žåããã®ãããªé¢æ°ã®åäœã¯å®å šã«äºæž¬ã§ããªããªããŸãã ãããã£ãŠããã¡ããããã®é¢æ°ã§ãµã€ãºã確èªããããšããå§ãããŸãã ãã®ãããªãã§ãã¯ãè¿œå ãããšãã³ãŒããå¢å ããŸãããã§ãã¯ã¯assertã䜿çšããŠè¡ãããšãã§ããŸãããC ++ã§åãããšãæžãããšããå§ãããŸã
int main() { LedsController LedsContr; LedsContr.SwitchOnAll(); for (;;) { for (auto &led : LedsContr.Leds) { Delay(1000U); led.Toggle(); } } return 0; }
ã¯ãããã®ã³ãŒãã¯ãã§ã«å€ãã®ã¹ããŒã¹ãå æããŠããŸãã ããããåŸã§ãã®ãããªèšèšãã©ã®ããã«æéãç¯çŽãããã確èªããã³ãŒããµã€ãºã¯Cãšã»ãŒåãã«ãªããããã°ã©ã ãè€éã«ãªããŸãã
ããã§ã¯LedsControllerã¯ã©ã¹ã䜿çšãããŠããŸãããã®ã³ãŒãã瀺ããŸãã
#ifndef LEDSCONTROLLER_H #define LEDSCONTROLLER_H #include "led.hpp" #include <array> constexpr unsigned int LedsCount = 4U; class LedsController { public: LedsController() {}; inline void SwitchOnAll() { for (auto &led : Leds) { led.SwitchOn(); } }; std::array<Led, LedsCount> leds{Led{*GPIOC, 5U},Led{*GPIOC, 8U},Led{*GPIOC, 9U},Led{*GPIOA, 5U}}; }; #endif
SwitchOnAllïŒïŒã¡ãœããã¯ããã€ã³ã¿ãŒãé åã«æž¡ãå¿ èŠããªããªããã¯ã©ã¹ãªããžã§ã¯ãå ã«æ ŒçŽãããŠããæ¢åã®é åã䜿çšããŸãã
ãã®ã³ãŒãã¯ãªãä¿¡é Œæ§ãé«ããšèããããŸããïŒ ãŸããã©ãã§ããã€ã³ã¿ãŒã䜿çšãããã¯ã©ã¹å ã®ãã¹ãŠã®æ¢åã®LEDã«ãªããžã§ã¯ãã®é åãä¿åãããã€ã³ã¿ãŒã§ã¯ãªããªããžã§ã¯ããçŽæ¥åç §ããŸãã 次ã«ããµã€ãºãæå®ããããšãªãé åããã€ãã¹ããforã«ãŒãã«ç¹å¥ãªæ§æã䜿çšããŸã;ã³ã³ãã€ã©ãŒã¯ãããè¡ããŸãã ãã®ã«ãŒãã¯ãå埩åã§ãããã¹ãŠã®ãªããžã§ã¯ãã§æ©èœããŸãã C ++ã®é åã¯ãããã©ã«ãã§ã¯ãã®ãããªãªããžã§ã¯ãã§ãã
ééããå¯èœæ§ãããå¯äžã®å Žæã¯ãLedsCountå®æ°ã䜿çšããŠé åã®ãµã€ãºãèšå®ããããšã§ãã ãã ãããã®å°ããªäŸããã§ããC ++ã«ã¯ä¿¡é Œæ§ã®é«ãã³ãŒããèšè¿°ããããã®ããå€ãã®ããŒã«ãçšæãããŠããããšãããããŸãã
泚æãå¿ èŠãªãã1ã€ã®ç¹ã¯ãLedsControllerã¯ã©ã¹ã®ãªããžã§ã¯ãã誀ã£ãŠäœæããå¯èœæ§ãããããšã§ããããã«ããã䜿çšãããRAMïŒã¹ã¿ãã¯ïŒã®ãµã€ãºã倧ãããªããããã°ã©ã ã®èå³æ·±ãåäœãçºçããŸãã Lonerãã³ãã¬ãŒãã¯ããã«å¯Ÿããä¿è·ã«åœ¹ç«ã¡ãŸãããããªã倧èŠæš¡ãªãããžã§ã¯ãã倧èŠæš¡ãªéçºããŒã ãããã誰ããã³ã³ãããŒã©ãŒãªããžã§ã¯ããæ¢ã«äœæãããå¥ã®ã³ã³ãããŒã©ãŒã誀ã£ãŠäœæããŠããããšã誰ããå¿ãããªã¹ã¯ãããå Žåã«ã®ã¿ããããè¡ãå¿ èŠããããŸããç§ãã¡ã®å Žåãããã¯æããã«éå°ã§ãããé¢æ°ã¯å°ãããLedsControllerã¯ã©ã¹ã®ãªããžã§ã¯ãã1ã€ããããšãã¯ã£ãããšèŠããŠããŸãã
ããããéçºã«æ»ããšãéåžžãããã°ã©ããã¿ã¹ã¯ïŒãã®å Žåã¯ã¯ãªã¹ãã¹ããªãŒïŒãå®çŸããç¬éã«ã顧客ã¯ããã«ããã«2ã€ã®ã¢ãŒããå®è£ ããããã«èŠæ±ããŸãïŒãã§ãã«ãŒããŒããã¿ãŒã³ã§ç¹æ» ãããã¹ãŠã®LEDã§ç¹æ» ãããã¿ã³ãæŒãã ãã§ã¢ãŒããå€æŽãããŸãã Snezhinkaã®å Žåãã»ãŒå®å šãªãšã©ãŒãçºçããŸããããã°ã©ã ã³ãŒããSnezhinkaã¹ã¿ã€ã«ã§äœæãããšããã®èšäºã®ããŒãžã«åãŸããªãã»ã©é¢åã«ãªããããããã§ã¯èª¬æããŸããã
Cããã°ã©ããŒãäœãã§ããããããããèŠãŠãããŸãã顧客ããããã«æ°ãããªãã¡ãŒãæ¥ãå¯èœæ§ãããããšãç解ãããšã圌ã¯ãããã次ã®ãããªããšãããŸãã
int main(void) { tPort Leds[] = { { GPIOC, 5U },{ GPIOC, 8U },{ GPIOC, 9U },{ GPIOA, 5U } }; tPort Button = { GPIOC, BUTTON_PIN }; // GPIOC.13 tLedMode Mode = LM_Tree; int currentLed = 0; SwitchOnAllLed(Leds, LEDS_COUNT); for (;;) { // . 1, 0 if (!CHECK_BIT(Button.pPort->IDR, BUTTON_PIN)) { // Mode = (Mode < LM_End) ? (tLedMode)(Mode + 1U) : LM_Tree; // currentLed = 0; switch (Mode) { case LM_Tree: case LM_All: SwitchOnAllLed(Leds, LEDS_COUNT); break; case LM_Chess: SwitchChessLed(Leds, LEDS_COUNT); break; default: break; } } // switch (Mode) { case LM_Tree: ToggleLed(&Leds[currentLed]); break; case LM_All: case LM_Chess: ToggleAll(Leds, LEDS_COUNT); break; default: break; } currentLed = (currentLed < (LEDS_COUNT â 1)) ? (currentLed + 1) : 0; Delay(300U); } return 0; }
ãããŠãæ°ããã¢ãŒããè¿œå ããã«ã¯ãæ°ãããªã«ãŠã³ã¿ãŒãè¿œå ãããã®ã¢ãŒãã®åæå€ãè¿œå ãããã®ã¢ãŒãã®LEDãåŠçããã ãã§ããããã°ã©ã ã«ã¯ãŸã ååãªèª¬æãšã³ã¡ã³ããå¿ èŠã§ããã§ã«é¢åã«èŠããŸãããããã£ãŠãåå¥ã®ã¡ãœããã§ã¢ãŒãã®åŠçãåé€ãã決å®ãè¡ãããŸãã
inline void SetLedsBeginState(tLedMode mode, tPort *leds) { switch (mode) { case LM_Tree: case LM_All: SwitchOnAllLed(leds, LEDS_COUNT); break; case LM_Chess: SwitchChessLed(leds, LEDS_COUNT); break; default: break; } } inline void UpdateLeds(tLedMode mode, tPort *leds, int curLed) { switch (mode) { case LM_Tree: ToggleLed(&leds[curLed]); break; case LM_All: case LM_Chess: ToggleAll(leds, LEDS_COUNT); break; default: break; } }
ãã®å Žåãã¡ã€ã³ããã°ã©ã ã¯ããè¯ãèŠããŸãã
int main(void) { tPort Leds[] = { {GPIOC, 5U},{GPIOC, 8U},{GPIOC, 9U},{GPIOA, 5U} }; tPort Button = {GPIOC, BUTTON_PIN}; tLedMode Mode = LM_Tree; int currentLed = 0; SwitchOnAllLed(Leds, LEDS_COUNT); for (;;) { // . 1, 0 if (!CHECK_BIT(Button.pPort->IDR, BUTTON_PIN)) { // Mode = (Mode < LM_End) ? (tLedMode)(Mode + 1U) : LM_Tree; currentLed = 0; // SetLedsBeginState(Mode, Leds); } // UpdateLeds(Mode, Leds, currentLed); currentLed = (currentLed < (LEDS_COUNT -1)) ? (currentLed + 1) : 0; Delay(300U); } return 0; }
ããããããã§ã人éã®ãããªãã®ã欲ãã
If Button is Pressed then set Next Light Mode Update Leds Delay 1000ms
ãããCã§å®è¡ããããšãã§ããŸãããé¢æ°ã®å€éšã«ããããã€ãã®å€æ°ïŒcurrentLedãModeãªã©ïŒãä¿æããå¿ èŠããããŸãããããã®å€æ°ã¯ãé¢æ°ããããã«ã€ããŠèªèã§ããããã«ã°ããŒãã«ã§ãªããã°ãªããŸããããããŠãç§ãã¡ãç¥ã£ãŠããããã«ãã°ããŒãã«å€æ°ã¯åã³æœåšçãªãšã©ãŒã®ãªã¹ã¯ã§ããã¢ãžã¥ãŒã«ã®1ã€ã§ã°ããŒãã«å€æ°ã®å€ãäžæ³šæã«å€æŽããããšãã§ããŸããããã¯ããããã©ãã§ã©ã®ããã«å€åãããããã¹ãŠèŠããŠããããšãã§ããã1幎åŸã«ããããªãå¿ èŠãªã®ãããèŠããŠããªãããã§ã
æ§é äœã䜿çšããŠãã®ããŒã¿ãä¿åããCã§OOPã䜿çšããããšããããšãã§ããŸããããã®å Žåãå€ãã®ãªãŒããŒãããããããå°ãªããšãé¢æ°ãžã®ãã€ã³ã¿ãŒãä¿åããå¿ èŠããããã³ãŒãã¯C ++ã«éåžžã«äŒŒãŠããããšãç解ããå¿ èŠããããŸãã
ãããã£ãŠãC ++ã®ã³ãŒãã«çŽæ¥é²ã¿ãŸãã
int main() { LedsController leds; Button button{ *GPIOC, 13U }; for (;;) { if (button.IsPressed()) { leds.NextMode(); } else { leds.Update(); } Delay(1sec); } return 0; }
å®å
šãªã³ãŒã
utils.hpp
#ifndef UTILS_H #define UTILS_H #include <cassert> namespace utils { template<typename T, typename T1> inline void setBit(T &value, T1 bit) { assert((sizeof(T) * 8U) > bit); value |= static_cast<T>(static_cast<T>(1) << static_cast<T>(bit)); }; template<typename T, typename T1> inline void clearBit(T &value, T1 bit) { assert((sizeof(T) * 8U) > bit); value &=~ static_cast<T>(static_cast<T>(1) << static_cast<T>(bit)); }; template<typename T,typename T1> inline void toggleBit(T &value, T1 bit) { assert((sizeof(T) * 8U) > bit); value ^= static_cast<T>(static_cast<T>(1) << static_cast<T>(bit)); }; template<typename T, typename T1> inline bool checkBit(const T &value, T1 bit) { assert((sizeof(T) * 8U) > bit); return !((value & (static_cast<T>(1) << static_cast<T>(bit))) == static_cast<T>(0U)); }; }; constexpr unsigned long long operator "" sec(unsigned long long sec) { return sec * 1000U; } #endif
led.hpp
#ifndef LED_H #define LED_H #include "utils.hpp" class Led { public: Led(GPIO_TypeDef &portName, unsigned int pinNum): port(portName), pin(pinNum) {}; inline void Toggle() const { utils::toggleBit(port.ODR, pin); } inline void SwitchOn() const { utils::setBit(port.ODR, pin); } inline void SwitchOff() const { utils::clearBit(port.ODR, pin); } private: GPIO_TypeDef &port; unsigned int pin; }; #endif
LedsController.hpp
#ifndef LEDSCONTROLLER_H #define LEDSCONTROLLER_H #include "led.hpp" #include <array> enum class LedMode : unsigned char { Tree = 0, Chess = 1, All = 2, End = 2 }; constexpr int LedsCount = 4; class LedsController { public: LedsController() { SwitchOnAll(); }; void SwitchOnAll() { for (auto &led: leds) { led.SwitchOn(); } }; void ToggleAll() { for (auto &led: leds) { led.Toggle(); } }; void NextMode() { mode = (mode < LedMode::End) ? static_cast<LedMode>(static_cast<unsigned char>(mode) + 1U) : LedMode::Tree; currentLed = 0; if (mode == LedMode::Chess){ for(int i = 0; i < LedsCount; i++) { if ((i % 2) == 0) { leds[i].SwitchOn(); } else { leds[i].SwitchOff(); } } } else { SwitchOnAll(); } }; void Update() { switch(mode) { case LedMode::Tree: leds[currentLed].Toggle(); break; case LedMode::All: case LedMode::Chess: ToggleAll(); break; default: break; } currentLed = (currentLed < (LedsCount - 1)) ? (currentLed + 1) : 0; } private: LedMode mode = LedMode::Tree; int currentLed = 0; std::array<Led, LedsCount> leds{Led{*GPIOC, 5U},Led{*GPIOC, 8U},Led{*GPIOC, 9U},Led{*GPIOA, 5U}}; }; #endif
startup.cpp
#pragma language = extended #pragma segment = "CSTACK" extern "C" void __iar_program_start( void ); class DummyModule { public: static void handler(); }; typedef void( *intfunc )( void ); //cstat !MISRAC++2008-9-5-1 typedef union { intfunc __fun; void * __ptr; } intvec_elem; #pragma location = ".intvec" //cstat !MISRAC++2008-0-1-4_b !MISRAC++2008-9-5-1 extern "C" const intvec_elem __vector_table[] = { { .__ptr = __sfe( "CSTACK" ) }, __iar_program_start, DummyModule::handler, DummyModule::handler, DummyModule::handler, DummyModule::handler, DummyModule::handler, 0, 0, 0, 0, DummyModule::handler, DummyModule::handler, 0, DummyModule::handler, DummyModule::handler, //External Interrupts DummyModule::handler, //Window Watchdog DummyModule::handler, //PVD through EXTI Line detect/EXTI16 DummyModule::handler, //Tamper and Time Stamp/EXTI21 DummyModule::handler, //RTC Wakeup/EXTI22 DummyModule::handler, //FLASH DummyModule::handler, //RCC DummyModule::handler, //EXTI Line 0 DummyModule::handler, //EXTI Line 1 DummyModule::handler, //EXTI Line 2 DummyModule::handler, //EXTI Line 3 DummyModule::handler, //EXTI Line 4 DummyModule::handler, //DMA1 Stream 0 DummyModule::handler, //DMA1 Stream 1 DummyModule::handler, //DMA1 Stream 2 DummyModule::handler, //DMA1 Stream 3 DummyModule::handler, //DMA1 Stream 4 DummyModule::handler, //DMA1 Stream 5 DummyModule::handler, //DMA1 Stream 6 DummyModule::handler, //ADC1 0, //USB High Priority 0, //USB Low Priority 0, //DAC 0, //COMP through EXTI Line DummyModule::handler, //EXTI Line 9..5 DummyModule::handler, //TIM9/TIM1 Break interrupt DummyModule::handler, //TIM10/TIM1 Update interrupt DummyModule::handler, //TIM11/TIM1 Trigger/Commutation interrupts DummyModule::handler, //TIM1 Capture Compare interrupt DummyModule::handler, //TIM2 DummyModule::handler, //TIM3 DummyModule::handler, //TIM4 DummyModule::handler, //I2C1 Event DummyModule::handler, //I2C1 Error DummyModule::handler, //I2C2 Event DummyModule::handler, //I2C2 Error DummyModule::handler, //SPI1 DummyModule::handler, //SPI2 DummyModule::handler, //USART1 DummyModule::handler, //USART2 0, DummyModule::handler, //EXTI Line 15..10 DummyModule::handler, //EXTI Line 17 interrupt / RTC Alarms (A and B) through EXTI line interrupt DummyModule::handler, //EXTI Line 18 interrupt / USB On-The-Go FS Wakeup through EXTI line interrupt 0, //TIM6 0, //TIM7 f0 0, 0, DummyModule::handler, //DMA1 Stream 7 global interrupt fc 0, DummyModule::handler, //SDIO global interrupt DummyModule::handler, //TIM5 global interrupt DummyModule::handler, //SPI3 global interrupt 0, // 110 0, 0, 0, DummyModule::handler, //DMA2 Stream0 global interrupt 120 DummyModule::handler, //DMA2 Stream1 global interrupt DummyModule::handler, //DMA2 Stream2 global interrupt DummyModule::handler, //DMA2 Stream3 global interrupt DummyModule::handler, //DMA2 Stream4 global interrupt 130 0, 0, 0, 0, 0, 0, DummyModule::handler, //USB On The Go FS global interrupt, 14C DummyModule::handler, //DMA2 Stream5 global interrupt DummyModule::handler, //DMA2 Stream6 global interrupt DummyModule::handler, //DMA2 Stream7 global interrupt DummyModule::handler, //USART6 15C DummyModule::handler, //I2C3 Event DummyModule::handler, //I2C3 Error 164 0, 0, 0, 0, 0, 0, 0, DummyModule::handler, //FPU 184 0, 0, DummyModule::handler, //SPI 4 global interrupt DummyModule::handler //SPI 5 global interrupt }; __weak void DummyModule::handler() { for(;;) {} }; extern "C" void __cmain( void ); extern "C" __weak void __iar_init_core( void ); extern "C" __weak void __iar_init_vfp( void ); #pragma required=__vector_table void __iar_program_start( void ) { __iar_init_core(); __iar_init_vfp(); __cmain(); }
main.cpp
#include <stm32f411xe.h> #include "ledscontroller.hpp" #include "button.hpp" extern "C" { int __low_level_init(void) { // 16 RCC->CR |= RCC_CR_HSION; while ((RCC->CR & RCC_CR_HSIRDY) != RCC_CR_HSIRDY) { } // RCC->CFGR |= RCC_CFGR_SW_HSI; while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI) { } // RCC->AHB1ENR |= (RCC_AHB1ENR_GPIOCEN | RCC_AHB1ENR_GPIOAEN); //LED1 PortA.5, PortA.5 GPIOA->MODER |= GPIO_MODER_MODE5_0; //LED2 Port.9,LED3 PortC.8,LED4 PortC.5 PortC.5,8,9 GPIOC->MODER |= (GPIO_MODER_MODE5_0 | GPIO_MODER_MODE8_0 | GPIO_MODER_MODE9_0); return 1; } } //, inline void Delay(unsigned int mSec) { for (unsigned int i = 0U; i < mSec * 3000U; i++) { __NOP(); }; } int main() { LedsController leds; LedsController leds1; Button buttonUser{*GPIOC, 13U}; for(;;) { if (buttonUser.IsPressed()) { leds.NextMode(); } else { leds.Update(); leds1.Update(); } Delay(1sec); } return 0; }
ã»ãšãã©äººéã®èšèªã§C ++ã³ãŒããæžãããšãã§ããããã«èŠããŸããïŒã³ãŒãã¯éåžžã«æ確ã§ã·ã³ãã«ã§ããïŒãã®ã³ãŒãã«ã¯ã³ã¡ã³ãã¯å¿ èŠãããŸããããã¹ãŠãæ確ã§ãããŠãŒã¶ãŒãªãã©ã«ãsecãã䜿çšããŠãããã1ç§ã§ããããšãæ確ã«ãã次ã®æ§æã䜿çšããŠãé 延é¢æ°ãžã®éä¿¡çšã®ãµã³ãã«ã«å€æããŸãã
constexpr unsigned long long operator "" sec(unsigned long long sec) { return sec * 1000U; } ... Delay(1sec);
ã«ã¹ã¿ã ãªãã©ã«ã®å®çŸ©ã¯ã ""æŒç®åãšãªãã©ã«ã®ååã䜿çšããŠæå®ãããŸããconstexprããŒã¯ãŒãã¯ãå¯èœã§ããã°ãã³ã³ãã€ã«æ®µéã§å€ãèšç®ããã³ãŒãã«åçŽã«ä»£å ¥ããå¿ èŠãããããšãã³ãã€ã¬ãŒã¿ãŒã«äŒããŸãããã®å Žåããã¹ãŠã®å€ã¯å ¥åã§æ¢ç¥ã§ããã1ãæž¡ããåºåã§1000ãååŸãããããã³ã³ãã€ã©ã¯åã«DelayïŒ1secïŒåŒã³åºããDelayïŒ1000ïŒã«çœ®ãæããŸã-éåžžã«äŸ¿å©ã§èªã¿ãããã§ããåãããŒã¯ãŒãã䜿çšããŠããã¹ãŠã®ã¿ã€ããã¯ãã眮ãæããããšãã§ãã
#define MAGIC_NUM 0x5f3759df
ããç解ãããã
constexpr unsigned int MagicNumber = 0x5f3759df;
ç¹°ãè¿ãã«ãªããŸãããéåžžã«æ¡åŒµå¯èœã§ç解ããããã³ãŒããåŸãããŸãããLEDãç¹æ» ãããããã®æ°ããã¢ãŒããè¿œå ããããLEDã®æ°ãå€æŽãããããå Žåãããã§äœãå€æŽããå¿ èŠã¯ãããŸããã LedsControllerã¯ã©ã¹ã§ã®ã¿å°ããªå€æŽãè¡ãå¿ èŠããããŸããããã¯ãLEDã®åäœãæ åœããŸãããã®ã¢ãããŒãã䜿çšããå©ç¹ã¯æããã§ãã
ããã§ããã®ãããªè§£æ±ºçãçŸåšæ¡çšããŠãããªãœãŒã¹ã¯ããã€ã§ããïŒã³ãŒããèŠããšãã»ãšãã©ãã¹ãŠã®ããã°ã©ããŒãC ++ã³ãŒãããã£ãšå€§ãããã¹ãã ãšèšãã§ãããããäœãããã®ãââãç§èªèº«ãããã確信ããŠããŸããçµå±ã®ãšãããã¹ã¿ãã¯ã«ã¯ããã€ãã®ãªããžã§ã¯ããããããã¶ã€ããŒãžã®åŒã³åºããšè¿œå ã®ã¡ãœããããããŸããããããååãªä»®å®-æ°å€ã«ç§»ããæé©åãç¡å¹ã«ããŠCãšC ++ã®ã³ãŒãã®ãµã€ãºãæ¯èŒããŠã¿ãŸããããCã³ãŒãã¯496ãã€ããš80ãã€ãã®æ倧ã¹ã¿ãã¯ãã¹ãã£ã³ã°ããšããŸãã C ++ã³ãŒãã¯ã606ãã€ããš112ãã€ãã®ã¹ã¿ãã¯ãã¹ãã䜿çšããŸãã
Cãåªå ããŠãã³ãŒããšã¹ã¿ãã¯ãµã€ãºã®é¢ã§20ïŒ ã®å©ç¹ãããããã«èŠããŸãããã ããããã©ã«ãã§ã¯ãIARã³ã³ãã€ã©ãŒã¯é¢æ°ã®ã€ã³ã©ã€ã³ããŒã¯ãŒãã«ãŸã£ããå¿çããªããããæ¯åé¢æ°åŒã³åºããæ¿å ¥ããŸããããã«ãããé¢æ°ã³ã³ããã¹ãã®ä¿åãšåŸ©å ã«ããã³ãŒããšã¹ã¿ãã¯ã®å¢å ãããã³æžå°ã«ã€ãªãããŸããå®è¡é床ãããã¯ãã¡ãœãããšé¢æ°ãé©åã«ãããã°ã§ããããã«ããããã«è¡ãããŸããããããªããšãçµæã®ã³ãŒãã«äžéšã®é¢æ°ãšããŒã«ã«å€æ°ããŸã£ããååšããªããªããŸãã
ã€ã³ã©ã€ã³é¢æ°ã®æé©åãšãµããŒããæå¹ã«ãããšãç¶æ³ã¯ç°ãªããŸãã Cã³ãŒãã¯ãã¹ã¿ãã¯ã§396ãã€ããš72ãã€ãã䜿çšããŸãã
C ++ã³ãŒãã¯ã¹ã¿ãã¯ã§400ãã€ããš72ãã€ãã䜿çšããŸããéãã¯4ãã€ãã§ãã¢ã»ã³ãã©ã³ãŒãã¯Cã³ãŒããšã»ãšãã©åãã§ãããC ++ã³ãŒãã®åçŽããšç°¡æœãã«ãããŠæãããªå©ç¹ããããŸãããããŠãC ++ã§ã¯ãã¡ãŒã ãŠã§ã¢ãæžãããšã¯æçã§ã¯ãªããšèª°ãèšãã§ããããïŒ
PSïŒ
ãµã³ãã«ã³ãŒãã¯ãã¡ãããå ¥æã§ããŸãã
èŠã€ãã£ãæ¬ é¥ãããããšãvanxantããããEXCHGãAntervisããkosmos89è¯ãã¢ããã€ã¹ãšããããNightShad0w STDã©ã€ãã©ãªäœ¿çšããŠæå°æ¢çŽ¢äŸãã°STDãšæ€çŽ¢ã³ãŒãnamenshegoã
å©èšã«Jef239次ãã§ãLEDã®ã¢ã¬ã€ã®ããã®ã¡ã¢ãªãµã€ãºãå°ããããéçCONSTãšããŠå®çŸ©ããããã¹ãŠã®ã¯ã©ã¹ã¯å®æ°ãäœãããã®æ¹æ³ãå°ãããšãã§ãããããã»ããµäžã®ã¢ã¬ã€ããã°ã©ã ã¡ã¢ãªã«é 眮ãããã¹ã¿ãã¯ã¯ãã®é åã®ãµã€ãºã ãæžå°ããŸããéžæã¯éçºè 次第ã§ãããããã¯ããéèŠã§ã...