Просмотр исходного кода

Merge pull request #20 from Flipper-Zero/target_lo

Target lo
core glitch 5 лет назад
Родитель
Сommit
7a4254326c

+ 10 - 2
app/app.cpp

@@ -1,12 +1,20 @@
-#include "Arduino.h"
+#include "flipper.h"
+#include <stdio.h>
+
+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);

+ 3 - 2
app/Arduino.h → 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 <stdio.h>
 }
 
+// Arduino defines
+
 #define pinMode app_gpio_init
 #define digitalWrite app_gpio_write
 #define digitalRead app_gpio_read

+ 17 - 35
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 <yfuns.h>
+#define _GNU_SOURCE
 #include "main.h"
-#include "cmsis_os.h"
+#include <stdio.h>
 
 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;
+}

+ 1 - 3
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

+ 0 - 0
app/app_hal.h → target_f1/Inc/flipper_hal.h


+ 1 - 1
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

+ 1 - 1
app/app_hal.c → 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) {

+ 3 - 0
target_lo/Inc/cmsis_os.h

@@ -0,0 +1,3 @@
+#include "main.h"
+
+void osDelay(uint32_t ms);

+ 61 - 0
target_lo/Inc/flipper_hal.h

@@ -0,0 +1,61 @@
+/*
+Flipper devices inc.
+
+GPIO and HAL implementations
+*/
+
+#pragma once
+
+#include <stdio.h>
+#include <stdbool.h>
+#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");
+}

+ 18 - 0
target_lo/Inc/main.h

@@ -0,0 +1,18 @@
+#include <stdint.h>
+#include <stdlib.h>
+#include <limits.h>
+
+#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

+ 121 - 0
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 ***

+ 41 - 0
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 <stdio.h>
+
+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.);
+}

+ 21 - 0
target_lo/Src/lo_hal.c

@@ -0,0 +1,21 @@
+/*
+Flipper devices inc.
+
+Dummy hal for local fw build
+*/
+
+#include <stdio.h>
+#include "main.h"
+#include <unistd.h>
+
+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;
+}

+ 8 - 0
target_lo/Src/lo_os.c

@@ -0,0 +1,8 @@
+#include "cmsis_os.h"
+#include <unistd.h>
+#include <stdio.h>
+
+void osDelay(uint32_t ms) {
+	usleep(ms * 1000);
+	printf("[DELAY] %d ms\n", ms);
+}

+ 13 - 0
target_lo/Src/main.c

@@ -0,0 +1,13 @@
+/*
+Flipper devices inc.
+
+Local fw build entry point.
+*/
+
+void app();
+
+int main() {
+    app();
+
+    return 0;
+}

+ 1 - 1
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 <target_dir>` to build application.
 
 If Dockerfile is changed you should run `docker-compose down` and `docker-compose build` for rebuild the image.