diff --git a/car_fmw/src/car_engine.h b/car_fmw/src/car_engine.h new file mode 100644 index 00000000000..97a67a39741 --- /dev/null +++ b/car_fmw/src/car_engine.h @@ -0,0 +1,53 @@ +#pragma once + +#include "stm32f4_discovery.h" +#include "stm32f4xx_conf.h" + +//#define CAR_ENGINE_ENABLE + +#define CAR_ENGINE_GPIO_PORT_CLOCK RCC_AHB1Periph_GPIOB +#define CAR_ENGINE_GPIO_PORT GPIOB +#define CAR_ENGINE_ENABLE_PIN GPIO_Pin_4 +#define CAR_ENGINE_DIRECTION_FORWARD_PIN GPIO_Pin_5 +#define CAR_ENGINE_DIRECTION_BACKWARD_PIN GPIO_Pin_6 + +inline void engine_init(void) +{ + GPIO_InitTypeDef GPIO_InitStructure; + RCC_AHB1PeriphClockCmd(CAR_ENGINE_GPIO_PORT_CLOCK, ENABLE); + GPIO_InitStructure.GPIO_Pin = CAR_ENGINE_ENABLE_PIN + | CAR_ENGINE_DIRECTION_FORWARD_PIN + | CAR_ENGINE_DIRECTION_BACKWARD_PIN; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_Init(CAR_ENGINE_GPIO_PORT, &GPIO_InitStructure); +} + +inline void engine_stop(void) +{ + GPIO_ResetBits(CAR_ENGINE_GPIO_PORT, 0 + | CAR_ENGINE_DIRECTION_BACKWARD_PIN + | CAR_ENGINE_ENABLE_PIN + | CAR_ENGINE_DIRECTION_FORWARD_PIN); +} + +#ifdef CAR_ENGINE_ENABLE +inline void engine_forward(void) +{ + engine_stop(); + GPIO_SetBits(CAR_ENGINE_GPIO_PORT, CAR_ENGINE_ENABLE_PIN); + GPIO_SetBits(CAR_ENGINE_GPIO_PORT, CAR_ENGINE_DIRECTION_FORWARD_PIN); +} + +inline void engine_backward(void) +{ + engine_stop(); + GPIO_SetBits(CAR_ENGINE_GPIO_PORT, CAR_ENGINE_ENABLE_PIN); + GPIO_SetBits(CAR_ENGINE_GPIO_PORT, CAR_ENGINE_DIRECTION_BACKWARD_PIN); +} +#else +#define engine_forward(...); +#define engine_backward(...); +#endif // CAR_ENGINE_ENABLE diff --git a/car_fmw/src/main.c b/car_fmw/src/main.c index 6e0b1a3f6dc..198cfdff03a 100644 --- a/car_fmw/src/main.c +++ b/car_fmw/src/main.c @@ -1,4 +1,5 @@ #include "car_leds.h" +#include "car_engine.h" #include "delay.h" int main(void) @@ -11,21 +12,31 @@ int main(void) */ leds_init(); + engine_init(); + const int DELAY = 0x3FFFFF; while (1) { led_set(LED_GREEN, true); - Delay(0x3FFFFF); + Delay(DELAY); + + engine_forward(); led_set(LED_ORANGE, true); - Delay(0x3FFFFF); + Delay(DELAY); + + engine_stop(); led_set(LED_RED, true); - Delay(0x3FFFFF); + Delay(DELAY); + + engine_backward(); led_set(LED_BLUE, true); - Delay(0x7FFFFF); + Delay(DELAY); + + engine_stop(); led_set(LED_GREEN, false); led_set(LED_ORANGE, false); led_set(LED_RED, false); led_set(LED_BLUE, false); - Delay(0xFFFFFF); + Delay(DELAY); } }