diff --git a/app/app.cpp b/app/app.cpp index 70d072be..26a4bf94 100644 --- a/app/app.cpp +++ b/app/app.cpp @@ -1,12 +1,20 @@ -#include "Arduino.h" +#include "flipper.h" +#include + +extern "C" { + FILE* get_debug(); +} extern "C" void app() { - printf("hello Flipper!\n"); + FILE* debug_uart = get_debug(); + + fprintf(debug_uart, "hello Flipper!\n"); GpioPin red_led = {LED_RED_GPIO_Port, LED_RED_Pin}; app_gpio_init(red_led, GpioModeOutput); + while(1) { delay(100); app_gpio_write(red_led, true); diff --git a/app/Arduino.h b/app/flipper.h similarity index 87% rename from app/Arduino.h rename to app/flipper.h index 2d52e136..f94bc1b2 100644 --- a/app/Arduino.h +++ b/app/flipper.h @@ -1,10 +1,11 @@ extern "C" { #include "main.h" + #include "flipper_hal.h" #include "cmsis_os.h" - #include "app_hal.h" - #include } +// Arduino defines + #define pinMode app_gpio_init #define digitalWrite app_gpio_write #define digitalRead app_gpio_read diff --git a/app/write.c b/app/write.c index 9e018329..2146e4f0 100644 --- a/app/write.c +++ b/app/write.c @@ -1,39 +1,10 @@ -/******************* - * - * Copyright 1998-2010 IAR Systems AB. - * - * This is a template implementation of the "__write" function used by - * the standard library. Replace it with a system-specific - * implementation. - * - * The "__write" function should output "size" number of bytes from - * "buffer" in some application-specific way. It should return the - * number of characters written, or _LLIO_ERROR on failure. - * - * If "buffer" is zero then __write should perform flushing of - * internal buffers, if any. In this case "handle" can be -1 to - * indicate that all handles should be flushed. - * - * The template implementation below assumes that the application - * provides the function "MyLowLevelPutchar". It should return the - * character written, or -1 on failure. - * - ********************/ - -// #include +#define _GNU_SOURCE #include "main.h" -#include "cmsis_os.h" +#include extern UART_HandleTypeDef DEBUG_UART; - -/* - * If the __write implementation uses internal buffering, uncomment - * the following line to ensure that we are called with "buffer" as 0 - * (i.e. flush) when the application terminates. - */ - -size_t _write(int handle, const unsigned char * buffer, size_t size) { +ssize_t uart_write(void* cookie, const char * buffer, size_t size) { if (buffer == 0) { /* * This means that we should flush internal buffers. Since we @@ -43,7 +14,18 @@ size_t _write(int handle, const unsigned char * buffer, size_t size) { return 0; } - HAL_UART_Transmit(&DEBUG_UART, (uint8_t*)buffer, (uint16_t)size, HAL_MAX_DELAY); - - return (int)size; + return (ssize_t)HAL_UART_Transmit(&DEBUG_UART, (uint8_t*)buffer, (uint16_t)size, HAL_MAX_DELAY); } + +FILE* get_debug() { + FILE* fp = fopencookie(NULL,"w+", (cookie_io_functions_t){ + .read = NULL, + .write = uart_write, + .seek = NULL, + .close = NULL + }); + + setvbuf(fp, NULL, _IONBF, 0); + + return fp; +} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 940caa63..dc166214 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,8 +9,6 @@ services: volumes: - .:/project # - /dev/bus/usb:/dev/bus/usb - # working_dir: "/project/target_nucleo476" - # working_dir: "/project/target_bluepill" - working_dir: "/project/target_f1" + working_dir: "/project" environment: DISPLAY: $DISPLAY diff --git a/app/app_hal.h b/target_f1/Inc/flipper_hal.h similarity index 100% rename from app/app_hal.h rename to target_f1/Inc/flipper_hal.h diff --git a/target_f1/Makefile b/target_f1/Makefile index 8dc93bce..f8bb8931 100644 --- a/target_f1/Makefile +++ b/target_f1/Makefile @@ -96,7 +96,7 @@ startup_stm32l476xx.s CPP_SOURCES = ../app/app.cpp C_SOURCES += ../app/write.c -C_SOURCES += ../app/app_hal.c +C_SOURCES += Src/flipper_hal.c ####################################### # binaries diff --git a/app/app_hal.c b/target_f1/Src/flipper_hal.c similarity index 98% rename from app/app_hal.c rename to target_f1/Src/flipper_hal.c index 6fd649c5..f1bccde1 100644 --- a/app/app_hal.c +++ b/target_f1/Src/flipper_hal.c @@ -5,7 +5,7 @@ GPIO and HAL implementations */ #include "main.h" -#include "app_hal.h" +#include "flipper_hal.h" void app_gpio_init(GpioPin gpio, GpioMode mode) { if(gpio.pin != 0) { diff --git a/target_lo/Inc/cmsis_os.h b/target_lo/Inc/cmsis_os.h new file mode 100644 index 00000000..7a14c8bc --- /dev/null +++ b/target_lo/Inc/cmsis_os.h @@ -0,0 +1,3 @@ +#include "main.h" + +void osDelay(uint32_t ms); \ No newline at end of file diff --git a/target_lo/Inc/flipper_hal.h b/target_lo/Inc/flipper_hal.h new file mode 100644 index 00000000..561ddfe2 --- /dev/null +++ b/target_lo/Inc/flipper_hal.h @@ -0,0 +1,61 @@ +/* +Flipper devices inc. + +GPIO and HAL implementations +*/ + +#pragma once + +#include +#include +#include "main.h" + +typedef enum { + GpioModeInput, + GpioModeOutput, + GpioModeOpenDrain +} GpioMode; + +typedef struct { + uint32_t port; + uint32_t pin; + GpioMode mode; +} GpioPin; + +void app_gpio_init(GpioPin gpio, GpioMode mode); + +inline void app_gpio_write(GpioPin gpio, bool state) { + if(gpio.pin != 0) { + if(state) { + printf("[GPIO] %d:%d on\n", gpio.port, gpio.pin); + } else { + printf("[GPIO] %d:%d off\n", gpio.port, gpio.pin); + } + } else { + printf("[GPIO] no pin\n"); + } +} + +inline bool app_gpio_read(GpioPin gpio) { + // TODO emulate pin state? + + return false; +} + +void delay_us(uint32_t time); + +void pwm_set(float value, float freq, TIM_HandleTypeDef* tim, uint32_t channel); + +extern TIM_HandleTypeDef htim8; + +inline void app_tim_ic_init(bool both) { + printf("[TIM] init\n"); +} + +inline void app_tim_pulse(uint32_t width) { + printf("[TIM] pulse %d\n", width); +} + +inline void app_tim_stop() { + printf("[TIM] stop\n"); +} \ No newline at end of file diff --git a/target_lo/Inc/main.h b/target_lo/Inc/main.h new file mode 100644 index 00000000..ee095608 --- /dev/null +++ b/target_lo/Inc/main.h @@ -0,0 +1,18 @@ +#include +#include +#include + +#define HAL_MAX_DELAY INT_MAX + +typedef uint32_t UART_HandleTypeDef; +uint16_t HAL_UART_Transmit( + UART_HandleTypeDef* handle, + uint8_t* bufer, + uint16_t size, + uint32_t wait_ms +); + +typedef uint32_t TIM_HandleTypeDef; + +#define LED_RED_GPIO_Port 1 +#define LED_RED_Pin 1 \ No newline at end of file diff --git a/target_lo/Makefile b/target_lo/Makefile new file mode 100644 index 00000000..93bdd886 --- /dev/null +++ b/target_lo/Makefile @@ -0,0 +1,121 @@ +TARGET = target_lo + + +###################################### +# building variables +###################################### +# debug build? +DEBUG = 1 +# optimization +OPT = -Og + + +####################################### +# paths +####################################### +# Build path +BUILD_DIR = build + +###################################### +# source +###################################### +# C sources +C_SOURCES = \ +Src/main.c + +CPP_SOURCES = ../app/app.cpp + +C_SOURCES += ../app/write.c +C_SOURCES += Src/flipper_hal.c +C_SOURCES += Src/lo_os.c +C_SOURCES += Src/lo_hal.c + +####################################### +# binaries +####################################### + +CC = gcc +CPP = g++ +AS = +CP = objcopy +SZ = size +HEX = $(CP) -O ihex +BIN = $(CP) -O binary -S + +####################################### +# CFLAGS +####################################### + +# C defines +C_DEFS = \ +-DUSE_HAL_DRIVER \ +-DSTM32L476xx \ +-DBUTON_INVERT=false \ +-DDEBUG_UART=huart1 + +# C includes +C_INCLUDES = \ +-IInc \ +-I../app + +# compile gcc flags +CFLAGS = $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections + +ifeq ($(DEBUG), 1) +CFLAGS += -g -gdwarf-2 +endif + + +# Generate dependency information +CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)" + +CPPFLAGS = -fno-threadsafe-statics + +####################################### +# LDFLAGS +####################################### + +# libraries +LIBS = -lc -lm +LIBDIR = +LDFLAGS = $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections + +# default action: build all +all: $(BUILD_DIR)/$(TARGET) + + +####################################### +# build the application +####################################### +# list of objects +OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o))) +vpath %.c $(sort $(dir $(C_SOURCES))) + +OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(CPP_SOURCES:.cpp=.o))) +vpath %.cpp $(sort $(dir $(CPP_SOURCES))) + +$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) + $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@ + +$(BUILD_DIR)/%.o: %.cpp Makefile | $(BUILD_DIR) + $(CPP) -c $(CFLAGS) $(CPPFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@ + +$(BUILD_DIR)/$(TARGET): $(OBJECTS) Makefile + $(CPP) $(OBJECTS) $(LDFLAGS) -o $@ + $(SZ) $@ + +$(BUILD_DIR): + mkdir $@ + +####################################### +# clean up +####################################### +clean: + -rm -fR $(BUILD_DIR) + +####################################### +# dependencies +####################################### +-include $(wildcard $(BUILD_DIR)/*.d) + +# *** EOF *** diff --git a/target_lo/Src/flipper_hal.c b/target_lo/Src/flipper_hal.c new file mode 100644 index 00000000..8f20d3dd --- /dev/null +++ b/target_lo/Src/flipper_hal.c @@ -0,0 +1,41 @@ +/* +Flipper devices inc. + +GPIO and HAL implementations +*/ + +#include "main.h" +#include "flipper_hal.h" +#include + +void app_gpio_init(GpioPin gpio, GpioMode mode) { + if(gpio.pin != 0) { + + switch(mode) { + case GpioModeInput: + printf("[GPIO] %d:%d input\n", gpio.port, gpio.pin); + break; + + case GpioModeOutput: + printf("[GPIO] %d:%d push pull\n", gpio.port, gpio.pin); + break; + + case GpioModeOpenDrain: + printf("[GPIO] %d:%d open drain\n", gpio.port, gpio.pin); + break; + } + + gpio.mode = mode; + } else { + printf("[GPIO] no pin\n"); + } +} + +void delay_us(uint32_t time) { + // How to deal with it + printf("[DELAY] %d us\n", time); +} + +void pwm_set(float value, float freq, TIM_HandleTypeDef* tim, uint32_t channel) { + printf("[TIM] set pwm %d:%d %f Hz, %f%%\n", *tim, channel, freq, value * 100.); +} \ No newline at end of file diff --git a/target_lo/Src/lo_hal.c b/target_lo/Src/lo_hal.c new file mode 100644 index 00000000..a72b7e9e --- /dev/null +++ b/target_lo/Src/lo_hal.c @@ -0,0 +1,21 @@ +/* +Flipper devices inc. + +Dummy hal for local fw build +*/ + +#include +#include "main.h" +#include + +UART_HandleTypeDef DEBUG_UART = 0; + +uint16_t HAL_UART_Transmit( + UART_HandleTypeDef* handle, + uint8_t* bufer, + uint16_t size, + uint32_t wait_ms +) { + uint16_t res = write(1, (const char*)bufer, size); + return res; +} \ No newline at end of file diff --git a/target_lo/Src/lo_os.c b/target_lo/Src/lo_os.c new file mode 100644 index 00000000..ad748b23 --- /dev/null +++ b/target_lo/Src/lo_os.c @@ -0,0 +1,8 @@ +#include "cmsis_os.h" +#include +#include + +void osDelay(uint32_t ms) { + usleep(ms * 1000); + printf("[DELAY] %d ms\n", ms); +} \ No newline at end of file diff --git a/target_lo/Src/main.c b/target_lo/Src/main.c new file mode 100644 index 00000000..0a8a59be --- /dev/null +++ b/target_lo/Src/main.c @@ -0,0 +1,13 @@ +/* +Flipper devices inc. + +Local fw build entry point. +*/ + +void app(); + +int main() { + app(); + + return 0; +} \ No newline at end of file diff --git a/wiki/fw/Firmware.md b/wiki/fw/Firmware.md index 361744ee..2c4f2103 100644 --- a/wiki/fw/Firmware.md +++ b/wiki/fw/Firmware.md @@ -107,7 +107,7 @@ After start, bootloader run first. It can: 1. Install [docker compose](https://docs.docker.com/compose/install/) 2. After startup you should run `docker-compose up -d` to run the container. -3. Then you can run `docker-compose exec dev make` to build application. +3. Then you can run `docker-compose exec dev make -C ` to build application. If Dockerfile is changed you should run `docker-compose down` and `docker-compose build` for rebuild the image.