diff --git a/Makefile.mak b/Makefile.mak index 7dca19015..fbe2cc7e3 100644 --- a/Makefile.mak +++ b/Makefile.mak @@ -269,12 +269,16 @@ CFLAGS_THIRDPARTY = ifeq ($(TARGET_SYSTEM),stm32f4) SOURCES_THIRDPARTY += \ ./third-party/STM32F4-Discovery_FW_V1.1.0/Libraries/CMSIS/ST/STM32F4xx/Source/Templates/system_stm32f4xx.c \ - ./third-party/STM32F4-Discovery_FW_V1.1.0/Libraries/CMSIS/ST/STM32F4xx/Source/Templates/gcc_ride7/startup_stm32f4xx.s + ./third-party/STM32F4-Discovery_FW_V1.1.0/Libraries/CMSIS/ST/STM32F4xx/Source/Templates/gcc_ride7/startup_stm32f4xx.s \ + ./third-party/STM32F4-Discovery_FW_V1.1.0/Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_tim.c \ + ./third-party/STM32F4-Discovery_FW_V1.1.0/Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_gpio.c \ + ./third-party/STM32F4-Discovery_FW_V1.1.0/Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_rcc.c INCLUDES_THIRDPARTY += \ -I third-party/STM32F4-Discovery_FW_V1.1.0/Libraries/CMSIS/ST/STM32F4xx/Include \ -I third-party/STM32F4-Discovery_FW_V1.1.0/Libraries/STM32F4xx_StdPeriph_Driver/inc \ - -I third-party/STM32F4-Discovery_FW_V1.1.0/Libraries/CMSIS/Include + -I third-party/STM32F4-Discovery_FW_V1.1.0/Libraries/CMSIS/Include \ + -I third-party/STM32F4-Discovery_FW_V1.1.0/ #-I third-party/STM32F4-Discovery_FW_V1.1.0/Project/Demonstration \ diff --git a/src/generated.h b/src/generated.h index c46ac9190..d6fb5b4b4 100644 --- a/src/generated.h +++ b/src/generated.h @@ -24,7 +24,7 @@ static const char* generated_source = "" "a = tmp + g;\n" "d = tmp * e + a;\n" "\n" -"var waitTime = 600000;\n" +"var waitTime = 50;\n" "var numOfIterations = 10;\n" "\n" "while (1)\n" diff --git a/src/libperipherals/actuators.c b/src/libperipherals/actuators.c index 818a498d0..22ccd6c09 100644 --- a/src/libperipherals/actuators.c +++ b/src/libperipherals/actuators.c @@ -17,9 +17,8 @@ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wpedantic" #pragma GCC diagnostic ignored "-Wsign-conversion" +#include "stm32f4xx_conf.h" #include "stm32f4xx.h" -#include "stm32f4xx_gpio.h" -#include "stm32f4xx_rcc.h" #pragma GCC diagnostic pop #endif @@ -36,8 +35,6 @@ led_toggle (uint32_t led_id) #ifdef __TARGET_MCU - init_led (led_id); - GPIOD->ODR ^= (uint16_t) (1 << led_id); #endif } @@ -51,10 +48,7 @@ led_on (uint32_t led_id) #ifdef __TARGET_MCU - init_led (led_id); - - GPIOD->BSRRH = (uint16_t) (1 << led_id); - GPIOD->BSRRL = (uint16_t) (1 << led_id); + GPIO_WriteBit(GPIOD, (uint16_t) (1 << led_id), Bit_SET); #endif } @@ -66,10 +60,7 @@ led_off (uint32_t led_id) #endif #ifdef __TARGET_MCU - init_led (led_id); - - GPIOD->BSRRL = (uint16_t) (1 << led_id); - GPIOD->BSRRH = (uint16_t) (1 << led_id); + GPIO_WriteBit(GPIOD, (uint16_t) (1 << led_id), Bit_RESET); #endif } @@ -81,8 +72,6 @@ led_blink_once (uint32_t led_id) #endif #ifdef __TARGET_MCU - init_led (led_id); - uint32_t dot = 300000; GPIOD->BSRRL = (uint16_t) (1 << led_id); @@ -92,26 +81,16 @@ led_blink_once (uint32_t led_id) } #ifdef __TARGET_MCU -void -init_led (uint32_t led_id) +void initialize_leds() { - uint32_t pin = led_id; - uint32_t mode = (uint32_t) GPIO_Mode_OUT << (pin * 2); - uint32_t speed = (uint32_t) GPIO_Speed_100MHz << (pin * 2); - uint32_t type = (uint32_t) GPIO_OType_PP << pin; - uint32_t pullup = (uint32_t) GPIO_PuPd_NOPULL << (pin * 2); + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); - TODO (INITIALIZE ONCE); + GPIO_InitTypeDef gpioStructure; + gpioStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; + gpioStructure.GPIO_Mode = GPIO_Mode_OUT; + gpioStructure.GPIO_Speed = GPIO_Speed_100MHz; + GPIO_Init(GPIOD, &gpioStructure); - // Initialise the peripheral clock. - RCC->AHB1ENR |= RCC_AHB1Periph_GPIOD; - - // Initilaise the GPIO port. - volatile GPIO_TypeDef* gpio = GPIOD; - - gpio->MODER |= mode; - gpio->OSPEEDR |= speed; - gpio->OTYPER |= type; - gpio->PUPDR |= pullup; + GPIO_WriteBit(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15, Bit_RESET); } #endif \ No newline at end of file diff --git a/src/libperipherals/actuators.h b/src/libperipherals/actuators.h index fb911f338..2e15ec4aa 100644 --- a/src/libperipherals/actuators.h +++ b/src/libperipherals/actuators.h @@ -24,7 +24,7 @@ void led_off(uint32_t); void led_blink_once(uint32_t); #ifdef __TARGET_MCU -void init_led (uint32_t); +void initialize_leds(void); #endif #endif /* ACTUATORS_H */ diff --git a/src/libperipherals/common-io.c b/src/libperipherals/common-io.c index 410d560b0..edae12f55 100644 --- a/src/libperipherals/common-io.c +++ b/src/libperipherals/common-io.c @@ -13,19 +13,18 @@ * limitations under the License. */ +#include "common-io.h" +#include "jerry-libc.h" + #ifdef __TARGET_MCU + #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wpedantic" #pragma GCC diagnostic ignored "-Wsign-conversion" +#include "stm32f4xx_conf.h" #include "stm32f4xx.h" -#include "stm32f4xx_gpio.h" -#include "stm32f4xx_rcc.h" #pragma GCC diagnostic pop -#ifdef __HOST -#include -#endif - // STM32 F4 #define LED_GREEN 12 #define LED_ORANGE 13 @@ -34,9 +33,6 @@ #endif -#include "common-io.h" -#include "jerry-libc.h" - int digital_read (uint32_t arg1 __unused, uint32_t arg2 __unused) { @@ -76,40 +72,36 @@ wait_ms (uint32_t time_ms) #endif #ifdef __TARGET_MCU - volatile uint32_t index; - for (index = 0; index < time_ms; index++); + while (time_ms--) + { + wait_1ms (); + } #endif } #ifdef __TARGET_MCU + +void initialize_timer() +{ + RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); + + TIM_TimeBaseInitTypeDef timerInitStructure; + timerInitStructure.TIM_Prescaler = 40000; + timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up; + timerInitStructure.TIM_Period = 500; + timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; + timerInitStructure.TIM_RepetitionCounter = 0; + TIM_TimeBaseInit(TIM2, &timerInitStructure); + TIM_Cmd(TIM2, ENABLE); +} + void fake_exit (void) { uint32_t pin = LED_ORANGE; - uint32_t mode = (uint32_t)GPIO_Mode_OUT << (pin * 2); - uint32_t speed = (uint32_t)GPIO_Speed_100MHz << (pin * 2); - uint32_t type = (uint32_t)GPIO_OType_PP << pin; - uint32_t pullup = (uint32_t)GPIO_PuPd_NOPULL << (pin * 2); - // - // Initialise the peripheral clock. - // - RCC->AHB1ENR |= RCC_AHB1Periph_GPIOD; - // - // Initilaise the GPIO port. - // volatile GPIO_TypeDef* gpio = GPIOD; - - gpio->MODER |= mode; - gpio->OSPEEDR |= speed; - gpio->OTYPER |= type; - gpio->PUPDR |= pullup; - // - // Toggle the selected LED indefinitely. - // volatile int index; - - // SOS - + int dot = 600000; int dash = dot * 3; @@ -130,4 +122,44 @@ fake_exit (void) for (index = 0; index < dash * 7; index++); } } + + + +static __IO uint32_t sys_tick_counter; + +void +initialize_sys_tick (void) +{ + /**************************************** + *SystemFrequency/1000 1ms * + *SystemFrequency/100000 10us * + *SystemFrequency/1000000 1us * + *****************************************/ + while (SysTick_Config (SystemCoreClock / 1000000) != 0) + { + } // One SysTick interrupt now equals 1us + +} + +void SysTick_Handler(void) { + time_tick_decrement(); +} + +void +time_tick_decrement (void) +{ + if (sys_tick_counter != 0x00) + { + sys_tick_counter--; + } +} + +void +wait_1ms (void) +{ + sys_tick_counter = 1000; + while (sys_tick_counter != 0) + { + } +} #endif diff --git a/src/libperipherals/common-io.h b/src/libperipherals/common-io.h index 09f532b66..85458058d 100644 --- a/src/libperipherals/common-io.h +++ b/src/libperipherals/common-io.h @@ -29,6 +29,13 @@ void wait_ms(uint32_t); #ifdef __TARGET_MCU void fake_exit(void); + +void initialize_timer(void); +void initialize_sys_tick(void); +void SysTick_Handler(void); + +void time_tick_decrement(void); +void wait_1ms(void); #endif #endif /* COMMON_IO_H */ diff --git a/src/main.c b/src/main.c index 78c2f4b77..78d87ef17 100644 --- a/src/main.c +++ b/src/main.c @@ -15,6 +15,8 @@ #ifdef __TARGET_MCU #include "common-io.h" +#include "actuators.h" +#include "sensors.h" #include "generated.h" #endif @@ -186,6 +188,10 @@ main (int argc __unused, int main(void) { + initialize_sys_tick(); + initialize_leds(); + initialize_timer(); + const char *source_p = generated_source; const size_t source_size = sizeof(generated_source);