From c2f82eefc0afe9cd826b0779c6feec4da9704e01 Mon Sep 17 00:00:00 2001 From: "e.gavrin" Date: Thu, 24 Jul 2014 19:27:19 +0400 Subject: [PATCH] add blink_toggle_once --- Makefile.mak | 2 +- src/generated.h | 15 +------ src/libcoreint/opcodes.c | 25 ++++++++++-- src/libperipherals/actuators.c | 75 +++++++++++++++++++++++++++++++--- src/libperipherals/actuators.h | 10 +++-- src/main.c | 7 +++- 6 files changed, 105 insertions(+), 29 deletions(-) diff --git a/Makefile.mak b/Makefile.mak index 551d18c55..a491df449 100644 --- a/Makefile.mak +++ b/Makefile.mak @@ -110,7 +110,7 @@ CFLAGS_CORTEXM4 ?= -mlittle-endian -mcpu=cortex-m4 -march=armv7e-m -mthumb \ # Common # -CFLAGS_COMMON ?= $(INCLUDES) -std=c99 -fsanitize=address -fdiagnostics-color=always +CFLAGS_COMMON ?= $(INCLUDES) -std=c99 #-fsanitize=address -fdiagnostics-color=always LDFLAGS ?= diff --git a/src/generated.h b/src/generated.h index fae8816bc..68e831091 100644 --- a/src/generated.h +++ b/src/generated.h @@ -15,17 +15,6 @@ #include "globals.h" -static const char* generated_source __unused = "" -"while (true) {\n" -"LEDToggle (LED3);\n" -"LEDToggle (LED6);\n" -"LEDToggle (LED7);\n" -"LEDToggle (LED4);\n" -"LEDToggle (LED10);\n" -"LEDToggle (LED8);\n" -"LEDToggle (LED9);\n" -"LEDToggle (LED5);\n" -"\n" -"wait(500);\n" -"}\n" +static const char* generated_source = "" +"LEDToggle (14);\n" ; diff --git a/src/libcoreint/opcodes.c b/src/libcoreint/opcodes.c index 5b97ab233..3e4460a73 100644 --- a/src/libcoreint/opcodes.c +++ b/src/libcoreint/opcodes.c @@ -25,6 +25,10 @@ #include "mem-heap.h" #include "opcodes.h" +#include "actuators.h" +#include "common-io.h" +#include "sensors.h" + /** * Note: * The note describes exception handling in opcode handlers that perform operations, @@ -416,7 +420,10 @@ OP_UNIMPLEMENTED_LIST(DEFINE_UNIMPLEMENTED_OP); ecma_completion_value_t opfunc_call_1 (OPCODE opdata __unused, struct __int_data *int_data) -{ +{ + ecma_completion_value_t ret_value; + ret_value = ecma_make_empty_completion_value (); + #ifdef __HOST __printf ("%d::op_call_1:idx:%d:%d\t", int_data->pos, @@ -432,11 +439,21 @@ opfunc_call_1 (OPCODE opdata __unused, struct __int_data *int_data) #ifdef __HOST __printf("%s\n", str_value.str_p); #endif + + if (!__strcmp ((const char*)str_value.str_p, "LEDToggle")) + { + TRY_CATCH (cond_value, get_variable_value (int_data, opdata.data.call_1.arg1_lit_idx, false), ret_value); + JERRY_ASSERT(cond_value.value.value_type == ECMA_TYPE_NUMBER ); + ecma_number_t * num_p = (ecma_number_t*)ecma_get_pointer(cond_value.value.value); + uint32_t int_num = (uint32_t)*num_p; + led_blink_once (int_num); + ret_value = ecma_make_empty_completion_value (); + FINALIZE (cond_value); + } - free_string_literal_copy( &str_value); + free_string_literal_copy (&str_value); - // FIXME - return ecma_make_empty_completion_value(); + return ret_value; } /** diff --git a/src/libperipherals/actuators.c b/src/libperipherals/actuators.c index 1fa50043a..0c37242ff 100644 --- a/src/libperipherals/actuators.c +++ b/src/libperipherals/actuators.c @@ -13,20 +13,83 @@ * limitations under the License. */ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpedantic" +#pragma GCC diagnostic ignored "-Wsign-conversion" +#include "stm32f4xx.h" +#include "stm32f4xx_gpio.h" +#include "stm32f4xx_rcc.h" +#pragma GCC diagnostic pop + #include "actuators.h" #include "jerry-libc.h" -void led_toggle(int led_id) +#ifdef __TARGET_MCU + +void +blink_once (uint32_t led) { - __printf("led_toogle: %d", led_id); + uint32_t pin = led; + 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); + + TODO (INITIALIZE ONCE); + // + // 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. + // + int index; + + int dot = 600000; + int dash = dot * 3; + + while (1) + { + gpio->BSRRL = (uint16_t) (1 << pin); for (index = 0; index < dot; index++); gpio->BSRRH = (uint16_t) (1 << pin); for (index = 0; index < dash; index++); + gpio->BSRRL = (uint16_t) (1 << pin); for (index = 0; index < dot; index++); gpio->BSRRH = (uint16_t) (1 << pin); for (index = 0; index < dash; index++); + gpio->BSRRL = (uint16_t) (1 << pin); for (index = 0; index < dot; index++); gpio->BSRRH = (uint16_t) (1 << pin); for (index = 0; index < dash; index++); + + for (index = 0; index < dash * 7; index++); + } +} +#endif + +void led_toggle(uint32_t led_id) +{ + __printf("led_toggle: %d\n", led_id); } -void led_on(int led_id) +void led_on(uint32_t led_id) { - __printf("led_on: %d", led_id); + __printf("led_on: %d\n", led_id); } -void led_off(int led_id) +void led_off(uint32_t led_id) { - __printf("led_off: %d", led_id); + __printf("led_off: %d\n", led_id); +} + +void led_blink_once(uint32_t led_id) +{ +#ifdef __HOST + __printf("led_blink_once: %d\n", led_id); +#endif + +#ifdef __TARGET_MCU + blink_once(led_id); +#endif } diff --git a/src/libperipherals/actuators.h b/src/libperipherals/actuators.h index 40114e69f..5700e00d6 100644 --- a/src/libperipherals/actuators.h +++ b/src/libperipherals/actuators.h @@ -16,15 +16,19 @@ #ifndef ACTUATORS_H #define ACTUATORS_H +#include "globals.h" + // STM32 F4 #define LED_GREEN 12 #define LED_ORANGE 13 #define LED_RED 14 #define LED_BLUE 15 -void led_toggle(int); -void led_on(int); -void led_off(int); +void led_toggle(uint32_t); +void led_on(uint32_t); +void led_off(uint32_t); +void led_blink_once(uint32_t); +void blink_once (uint32_t); #endif /* ACTUATORS_H */ diff --git a/src/main.c b/src/main.c index 8d4b46a9b..67b39c10c 100644 --- a/src/main.c +++ b/src/main.c @@ -154,7 +154,7 @@ void fake_exit(void); void fake_exit (void) { - uint32_t pin = LED_RED; + 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; @@ -175,7 +175,7 @@ fake_exit (void) // // Toggle the selected LED indefinitely. // - int index; + volatile int index; // SOS @@ -203,6 +203,9 @@ fake_exit (void) int main(void) { + + //fake_exit(); + const char *source_p = generated_source; const size_t source_size = sizeof(generated_source);