diff --git a/Makefile.mak b/Makefile.mak index b5ff5524a..dc856ffc6 100644 --- a/Makefile.mak +++ b/Makefile.mak @@ -150,7 +150,7 @@ DEFINES_JERRY = -DMEM_HEAP_CHUNK_SIZE=256 -DMEM_HEAP_AREA_SIZE=32768 -DMEM_STATS SOURCES_JERRY = \ $(sort \ $(wildcard ./src/libruntime/*.c) \ - $(wildcard ./src/libperipherals/actuators.c) \ + $(wildcard ./src/libperipherals/*.c) \ $(wildcard ./src/libjsparser/*.c) \ $(wildcard ./src/libecmaobjects/*.c) \ $(wildcard ./src/libecmaoperations/*.c) \ diff --git a/src/libperipherals/actuators.c b/src/libperipherals/actuators.c index bd66af889..de0a29593 100644 --- a/src/libperipherals/actuators.c +++ b/src/libperipherals/actuators.c @@ -13,38 +13,66 @@ * limitations under the License. */ +#ifdef __TARGET_MCU #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wpedantic" #pragma GCC diagnostic ignored "-Wsign-conversion" -#ifdef __TARGET_MCU #include "stm32f4xx.h" #include "stm32f4xx_gpio.h" #include "stm32f4xx_rcc.h" -#endif #pragma GCC diagnostic pop +#endif #include "actuators.h" #include "jerry-libc.h" +void +led_toggle (uint32_t led_id) +{ + __printf ("led_toggle: %d\n", led_id); +} + +void +led_on (uint32_t led_id) +{ + __printf ("led_on: %d\n", led_id); +} + +void +led_off (uint32_t 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 +} + #ifdef __TARGET_MCU void blink_once (uint32_t led) { 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); - + 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; @@ -54,44 +82,27 @@ blink_once (uint32_t led) // // Toggle the selected LED indefinitely. // - int index; - + volatile 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++); + 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(uint32_t led_id) -{ - __printf("led_on: %d\n", led_id); -} - -void led_off(uint32_t 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 -} +#endif \ No newline at end of file diff --git a/src/libperipherals/actuators.h b/src/libperipherals/actuators.h index 5700e00d6..e5532ce6d 100644 --- a/src/libperipherals/actuators.h +++ b/src/libperipherals/actuators.h @@ -18,17 +18,14 @@ #include "globals.h" -// STM32 F4 -#define LED_GREEN 12 -#define LED_ORANGE 13 -#define LED_RED 14 -#define LED_BLUE 15 - void led_toggle(uint32_t); void led_on(uint32_t); void led_off(uint32_t); void led_blink_once(uint32_t); + +#ifdef __TARGET_MCU void blink_once (uint32_t); +#endif #endif /* ACTUATORS_H */ diff --git a/src/libperipherals/common-io.c b/src/libperipherals/common-io.c index 609246d9a..410d560b0 100644 --- a/src/libperipherals/common-io.c +++ b/src/libperipherals/common-io.c @@ -13,4 +13,121 @@ * limitations under the License. */ +#ifdef __TARGET_MCU +#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 +#ifdef __HOST +#include +#endif + +// STM32 F4 +#define LED_GREEN 12 +#define LED_ORANGE 13 +#define LED_RED 14 +#define LED_BLUE 15 + +#endif + +#include "common-io.h" +#include "jerry-libc.h" + +int +digital_read (uint32_t arg1 __unused, uint32_t arg2 __unused) +{ + JERRY_UNIMPLEMENTED (); +} + +void +digital_write (uint32_t arg1 __unused, uint32_t arg2 __unused) +{ + JERRY_UNIMPLEMENTED (); +} + +int +analog_read (uint32_t arg1 __unused, uint32_t arg2 __unused) +{ + JERRY_UNIMPLEMENTED (); +} + +void +analog_write (uint32_t arg1 __unused, uint32_t arg2 __unused) +{ + JERRY_UNIMPLEMENTED (); +} + +void +wait_ms (uint32_t time_ms) +{ +#ifdef __HOST + // 1 millisecond = 1,000,000 Nanoseconds +#define NANO_SECOND_MULTIPLIER 1000000 + __printf ("wait_ms: %d\n", time_ms); + // const long interval_ms = time_ms * NANO_SECOND_MULTIPLIER; + // + // timespec sleep_value = {0}; + // sleep_value.tv_nsec = interval_ms; + // nanosleep (&sleep_value, NULL); +#endif + +#ifdef __TARGET_MCU + volatile uint32_t index; + for (index = 0; index < time_ms; index++); +#endif +} + +#ifdef __TARGET_MCU +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; + + 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++); + + gpio->BSRRL = (uint16_t) (1 << pin); for (index = 0; index < dash; index++); gpio->BSRRH = (uint16_t) (1 << pin); for (index = 0; index < dash; index++); + gpio->BSRRL = (uint16_t) (1 << pin); for (index = 0; index < dash; index++); gpio->BSRRH = (uint16_t) (1 << pin); for (index = 0; index < dash; index++); + gpio->BSRRL = (uint16_t) (1 << pin); for (index = 0; index < dash; 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++); + gpio->BSRRL = (uint16_t) (1 << pin); for (index = 0; index < dot; index++); gpio->BSRRH = (uint16_t) (1 << pin); + + for (index = 0; index < dash * 7; index++); + } +} +#endif diff --git a/src/libperipherals/common-io.h b/src/libperipherals/common-io.h index b5da0692b..09f532b66 100644 --- a/src/libperipherals/common-io.h +++ b/src/libperipherals/common-io.h @@ -16,11 +16,20 @@ #ifndef COMMON_IO_H #define COMMON_IO_H -int digital_read(int, int); -void digital_write(int, int); +#include "globals.h" -int analog_read(int, int); -void analog_write(int, int); +int digital_read(uint32_t, uint32_t); +void digital_write(uint32_t, uint32_t); + +int analog_read(uint32_t, uint32_t); +void analog_write(uint32_t, uint32_t); + +void wait_ms(uint32_t); + + +#ifdef __TARGET_MCU +void fake_exit(void); +#endif #endif /* COMMON_IO_H */ diff --git a/src/libperipherals/sensors.c b/src/libperipherals/sensors.c index 609246d9a..1f5ea2f0d 100644 --- a/src/libperipherals/sensors.c +++ b/src/libperipherals/sensors.c @@ -13,4 +13,4 @@ * limitations under the License. */ - +#include "sensors.h" \ No newline at end of file diff --git a/src/libperipherals/sensors.h b/src/libperipherals/sensors.h index 4240aea74..3b6f5a45f 100644 --- a/src/libperipherals/sensors.h +++ b/src/libperipherals/sensors.h @@ -16,6 +16,7 @@ #ifndef SENSORS_H #define SENSORS_H +#include "globals.h" #endif /* SENSORS_H */ diff --git a/src/main.c b/src/main.c index 4fc3c6b52..b8ef72849 100644 --- a/src/main.c +++ b/src/main.c @@ -14,19 +14,7 @@ */ #ifdef __TARGET_MCU - -#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 - -#define LED_GREEN 12 -#define LED_ORANGE 13 -#define LED_RED 14 -#define LED_BLUE 15 +#include "common-io.h" #include "generated.h" #endif @@ -158,64 +146,12 @@ main (int argc __unused, return 0; } -#elif !defined(__HOST) && defined(__TARGET_MCU) -void fake_exit(void); - -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; - - 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++); - - gpio->BSRRL = (uint16_t) (1 << pin); for (index = 0; index < dash; index++); gpio->BSRRH = (uint16_t) (1 << pin); for (index = 0; index < dash; index++); - gpio->BSRRL = (uint16_t) (1 << pin); for (index = 0; index < dash; index++); gpio->BSRRH = (uint16_t) (1 << pin); for (index = 0; index < dash; index++); - gpio->BSRRL = (uint16_t) (1 << pin); for (index = 0; index < dash; 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++); - gpio->BSRRL = (uint16_t) (1 << pin); for (index = 0; index < dot; index++); gpio->BSRRH = (uint16_t) (1 << pin); - - for (index = 0; index < dash * 7; index++); - } -} +#endif +#ifdef __TARGET_MCU int main(void) { - - //fake_exit(); - const char *source_p = generated_source; const size_t source_size = sizeof(generated_source); @@ -226,6 +162,4 @@ main(void) JERRY_UNREACHABLE(); } -#else /* !__HOST && !__TARGET_MCU */ -# error "!__HOST && !__TARGET_MCU" -#endif /* !__HOST && !__TARGET_MCU */ +#endif