From f5b342abbe1be200ed1298f3d1cccea21878507b Mon Sep 17 00:00:00 2001 From: coreglitch Date: Mon, 26 Oct 2020 13:16:54 +0600 Subject: [PATCH] Api hw gpio pwm (#199) * initial gpio layer * move temlplate.c to template.c.example in preparing to applications.mk rework * separate arduino layer * separate flipper_hal.x * prepare to switch applications on v2 core gpio api * swithch applications to v2 gpio api * gpio api for local target * better gpio_disable handling * remove pwm functions from local target * inline gpio funcs * common function to init all api's * fix local example blink * move delay us to hal api folder * move pwm_set/pwm_stop to hal api folder * update applications to use hal pwm api * remove gpio mode case warning * add speaker demo to build Co-authored-by: DrZlo13 --- applications/applications.h | 15 ++- applications/applications.mk | 16 ++- .../cc1101-workaround/cc1101-workaround.cpp | 13 ++- applications/cc1101-workaround/cc1101.cpp | 22 +++-- applications/cc1101-workaround/cc1101.h | 10 +- .../coreglitch_demo_0/coreglitch_demo_0.c | 25 +---- applications/examples/blink.c | 10 +- applications/examples/uart_write.c | 10 +- applications/input/input.c | 2 +- applications/irda/irda.c | 4 +- applications/irda/irda_nec.c | 8 +- .../{template.c => template.c.example} | 0 applications/tests/test_index.c | 31 +++--- core/api-hal/api-gpio.c | 49 ++++++++++ core/api-hal/api-gpio.h | 37 +++++++ core/app.cpp | 2 + core/core.mk | 1 + core/flipper.h | 18 +--- core/flipper_arduino.h | 16 +++ core/flipper_v2.c | 5 + core/flipper_v2.h | 7 ++ firmware/targets/f2/Inc/flipper_hal.h | 98 ------------------- firmware/targets/f2/Inc/input_priv.h | 2 +- firmware/targets/f2/Src/flipper_hal.c | 69 ------------- firmware/targets/f2/Src/main.c | 2 +- firmware/targets/f2/api-hal/api-hal-delay.c | 14 +++ firmware/targets/f2/api-hal/api-hal-delay.h | 5 + firmware/targets/f2/api-hal/api-hal-gpio.c | 14 +++ firmware/targets/f2/api-hal/api-hal-gpio.h | 61 ++++++++++++ firmware/targets/f2/api-hal/api-hal-pwm.c | 22 +++++ firmware/targets/f2/api-hal/api-hal-pwm.h | 6 ++ firmware/targets/f2/api-hal/api-hal.h | 5 + firmware/targets/f2/target.mk | 6 +- firmware/targets/local/Inc/flipper_hal.h | 50 ---------- firmware/targets/local/Src/flipper_hal.c | 35 ------- .../targets/local/api-hal/api-hal-delay.c | 7 ++ .../targets/local/api-hal/api-hal-delay.h | 4 + firmware/targets/local/api-hal/api-hal-gpio.c | 47 +++++++++ firmware/targets/local/api-hal/api-hal-gpio.h | 49 ++++++++++ firmware/targets/local/api-hal/api-hal.h | 5 + firmware/targets/local/target.mk | 3 + 41 files changed, 464 insertions(+), 341 deletions(-) rename applications/template/{template.c => template.c.example} (100%) create mode 100644 core/api-hal/api-gpio.c create mode 100644 core/api-hal/api-gpio.h create mode 100644 core/flipper_arduino.h create mode 100644 core/flipper_v2.c delete mode 100644 firmware/targets/f2/Inc/flipper_hal.h delete mode 100644 firmware/targets/f2/Src/flipper_hal.c create mode 100644 firmware/targets/f2/api-hal/api-hal-delay.c create mode 100644 firmware/targets/f2/api-hal/api-hal-delay.h create mode 100644 firmware/targets/f2/api-hal/api-hal-gpio.c create mode 100644 firmware/targets/f2/api-hal/api-hal-gpio.h create mode 100644 firmware/targets/f2/api-hal/api-hal-pwm.c create mode 100644 firmware/targets/f2/api-hal/api-hal-pwm.h create mode 100644 firmware/targets/f2/api-hal/api-hal.h create mode 100644 firmware/targets/local/api-hal/api-hal-delay.c create mode 100644 firmware/targets/local/api-hal/api-hal-delay.h create mode 100644 firmware/targets/local/api-hal/api-hal-gpio.c create mode 100644 firmware/targets/local/api-hal/api-hal-gpio.h create mode 100644 firmware/targets/local/api-hal/api-hal.h diff --git a/applications/applications.h b/applications/applications.h index 7de48699..9ff172a8 100644 --- a/applications/applications.h +++ b/applications/applications.h @@ -35,6 +35,10 @@ const FlipperStartupApp FLIPPER_STARTUP[] = { {.app = display_u8g2, .name = "display_u8g2", .libs = {0}}, #endif +#ifdef APP_EXAMPLE_BLINK + {.app = application_blink, .name = "blink", .libs = {0}}, +#endif + #ifdef APP_INPUT {.app = input_task, .name = "input_task", .libs = {0}}, #endif @@ -61,8 +65,6 @@ const FlipperStartupApp FLIPPER_STARTUP[] = { {.app = nfc_task, .name = "nfc_task", .libs = {1, FURI_LIB{"menu_task"}}}, #endif -// {.app = coreglitch_demo_0, .name = "coreglitch_demo_0", .libs = ""}, - #ifdef APP_TEST {.app = flipper_test_app, .name = "test app", .libs = {0}}, #endif @@ -83,6 +85,10 @@ const FlipperStartupApp FLIPPER_STARTUP[] = { #ifdef APP_EXAMPLE_DISPLAY {.app = u8g2_example, .name = "u8g2_example", .libs = {1, FURI_LIB{"display_u8g2"}}}, #endif + +#ifdef APP_SPEAKER_DEMO + {.app = coreglitch_demo_0, .name = "coreglitch_demo_0", .libs = ""}, +#endif }; const FlipperStartupApp FLIPPER_APPS[] = { @@ -101,7 +107,12 @@ const FlipperStartupApp FLIPPER_APPS[] = { #ifdef BUILD_CC1101 {.app = cc1101_workaround, .name = "cc1101 workaround", .libs = {1, FURI_LIB{"gui_task"}}}, #endif + #ifdef BUILD_IRDA {.app = irda, .name = "irda", .libs = {1, FURI_LIB{"gui_task"}}}, #endif + +#ifdef BUILD_SPEAKER_DEMO + {.app = coreglitch_demo_0, .name = "coreglitch_demo_0", .libs = {0}}, +#endif }; \ No newline at end of file diff --git a/applications/applications.mk b/applications/applications.mk index 6dc0baf9..f527dd89 100644 --- a/applications/applications.mk +++ b/applications/applications.mk @@ -15,6 +15,7 @@ BUILD_EXAMPLE_BLINK = 1 BUILD_EXAMPLE_UART_WRITE = 1 BUILD_EXAMPLE_INPUT_DUMP = 1 BUILD_CC1101 = 1 +BUILD_SPEAKER_DEMO = 1 endif APP_NFC ?= 0 @@ -143,7 +144,7 @@ APP_INPUT = 1 APP_GUI = 1 endif -APP_IRDA?= 0 +APP_IRDA ?= 0 ifeq ($(APP_IRDA), 1) CFLAGS += -DAPP_IRDA BUILD_IRDA = 1 @@ -156,6 +157,19 @@ APP_INPUT = 1 APP_GUI = 1 endif +APP_SPEAKER_DEMO ?= 0 +ifeq ($(APP_SPEAKER_DEMO), 1) +CFLAGS += -DAPP_SPEAKER_DEMO +BUILD_SPEAKER_DEMO = 1 +endif +BUILD_SPEAKER_DEMO ?= 0 +ifeq ($(BUILD_SPEAKER_DEMO), 1) +CFLAGS += -DBUILD_SPEAKER_DEMO +C_SOURCES += $(wildcard $(APP_DIR)/coreglitch_demo_0/*.c) +APP_INPUT = 1 +APP_GUI = 1 +endif + # device drivers APP_GUI ?= 0 diff --git a/applications/cc1101-workaround/cc1101-workaround.cpp b/applications/cc1101-workaround/cc1101-workaround.cpp index 3f09a816..ecac480a 100644 --- a/applications/cc1101-workaround/cc1101-workaround.cpp +++ b/applications/cc1101-workaround/cc1101-workaround.cpp @@ -232,7 +232,11 @@ extern "C" void cc1101_workaround(void* p) { gui->add_widget(gui, widget, WidgetLayerFullscreen); printf("[cc1101] creating device\n"); - CC1101 cc1101(GpioPin{CC1101_CS_GPIO_Port, CC1101_CS_Pin}); + GpioPin cs_pin = {CC1101_CS_GPIO_Port, CC1101_CS_Pin}; + + // TODO open record + GpioPin* cs_pin_record = &cs_pin; + CC1101 cc1101(cs_pin_record); printf("[cc1101] init device\n"); uint8_t address = cc1101.Init(); @@ -254,9 +258,11 @@ extern "C" void cc1101_workaround(void* p) { // create pin GpioPin led = {GPIOA, GPIO_PIN_8}; + // TODO open record + GpioPin* led_record = &led; // configure pin - pinMode(led, GpioModeOpenDrain); + pinMode(led_record, GpioModeOutputOpenDrain); const int16_t RSSI_THRESHOLD = -89; @@ -322,7 +328,8 @@ extern "C" void cc1101_workaround(void* p) { } digitalWrite( - led, (state->last_rssi > RSSI_THRESHOLD && !state->need_cc1101_conf) ? LOW : HIGH); + led_record, + (state->last_rssi > RSSI_THRESHOLD && !state->need_cc1101_conf) ? LOW : HIGH); release_mutex(&state_mutex, state); widget_update(widget); diff --git a/applications/cc1101-workaround/cc1101.cpp b/applications/cc1101-workaround/cc1101.cpp index 001cba60..279e1256 100644 --- a/applications/cc1101-workaround/cc1101.cpp +++ b/applications/cc1101-workaround/cc1101.cpp @@ -12,13 +12,17 @@ /******************************************************************************/ GpioPin ss_pin; -CC1101::CC1101(GpioPin ss_pin) { +CC1101::CC1101(GpioPin* ss_pin) { /* pinMode(gdo0_pin, OUTPUT); //GDO0 as asynchronous serial mode input pinMode(gdo2_pin, INPUT); //GDO2 as asynchronous serial mode output */ pinMode(ss_pin, OUTPUT); this->ss_pin = ss_pin; + + // TODO open record + this->miso_pin = MISO_PIN; + this->miso_pin_record = &this->miso_pin; } //****************************************************************************** //SpiInit @@ -108,7 +112,7 @@ byte CC1101::SpiTransfer(byte value) { ****************************************************************/ void CC1101::SpiWriteReg(byte addr, byte value) { digitalWrite(ss_pin, LOW); - while(digitalRead(MISO_PIN)) + while(digitalRead(this->miso_pin_record)) ; SpiTransfer(addr); SpiTransfer(value); @@ -126,7 +130,7 @@ void CC1101::SpiWriteBurstReg(byte addr, byte* buffer, byte num) { temp = addr | WRITE_BURST; digitalWrite(ss_pin, LOW); - while(digitalRead(MISO_PIN)) + while(digitalRead(this->miso_pin_record)) ; SpiTransfer(temp); for(i = 0; i < num; i++) { @@ -143,7 +147,7 @@ void CC1101::SpiWriteBurstReg(byte addr, byte* buffer, byte num) { ****************************************************************/ void CC1101::SpiStrobe(byte strobe) { digitalWrite(ss_pin, LOW); - while(digitalRead(MISO_PIN)) + while(digitalRead(this->miso_pin_record)) ; SpiTransfer(strobe); digitalWrite(ss_pin, HIGH); @@ -160,7 +164,7 @@ byte CC1101::SpiReadReg(byte addr) { temp = addr | READ_SINGLE; digitalWrite(ss_pin, LOW); - while(digitalRead(MISO_PIN)) + while(digitalRead(this->miso_pin_record)) ; SpiTransfer(temp); value = SpiTransfer(0); @@ -180,7 +184,7 @@ void CC1101::SpiReadBurstReg(byte addr, byte* buffer, byte num) { temp = addr | READ_BURST; digitalWrite(ss_pin, LOW); - while(digitalRead(MISO_PIN)) + while(digitalRead(this->miso_pin_record)) ; SpiTransfer(temp); for(i = 0; i < num; i++) { @@ -200,7 +204,7 @@ byte CC1101::SpiReadStatus(byte addr) { temp = addr | READ_BURST; digitalWrite(ss_pin, LOW); - while(digitalRead(MISO_PIN)) + while(digitalRead(this->miso_pin_record)) ; SpiTransfer(temp); value = SpiTransfer(0); @@ -221,10 +225,10 @@ void CC1101::Reset(void) { digitalWrite(ss_pin, HIGH); delay(1); digitalWrite(ss_pin, LOW); - while(digitalRead(MISO_PIN)) + while(digitalRead(this->miso_pin_record)) ; SpiTransfer(CC1101_SRES); - while(digitalRead(MISO_PIN)) + while(digitalRead(this->miso_pin_record)) ; digitalWrite(ss_pin, HIGH); } diff --git a/applications/cc1101-workaround/cc1101.h b/applications/cc1101-workaround/cc1101.h index a4c01996..862e4525 100644 --- a/applications/cc1101-workaround/cc1101.h +++ b/applications/cc1101-workaround/cc1101.h @@ -136,9 +136,11 @@ //******************************** class **************************************// class CC1101 { private: - GpioPin ss_pin; - GpioPin gdo0_pin; - GpioPin gdo2_pin; + GpioPin* ss_pin; + GpioPin miso_pin; + GpioPin* miso_pin_record; + GpioPin* gdo0_pin; + GpioPin* gdo2_pin; private: void SpiMode(byte config); @@ -150,7 +152,7 @@ private: void RegConfigSettings(void); public: - CC1101(GpioPin ss_pin); + CC1101(GpioPin* ss_pin); void SpiWriteReg(byte addr, byte value); void SpiInit(void); diff --git a/applications/coreglitch_demo_0/coreglitch_demo_0.c b/applications/coreglitch_demo_0/coreglitch_demo_0.c index bc4b67b7..d5f04900 100644 --- a/applications/coreglitch_demo_0/coreglitch_demo_0.c +++ b/applications/coreglitch_demo_0/coreglitch_demo_0.c @@ -8,15 +8,6 @@ void coreglitch_demo_0(void* p) { fuprintf(log, "coreglitch demo!\n"); - // open record - FuriRecordSubscriber* fb_record = - furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL); - - if(fb_record == NULL) { - fuprintf(log, "[widget] cannot create fb record\n"); - furiac_exit(NULL); - } - float notes[] = { 0.0, 330.0, @@ -49,25 +40,11 @@ void coreglitch_demo_0(void* p) { } // TODO get sound from FURI - pwm_set(width, freq, &htim5, TIM_CHANNEL_4); + hal_pwm_set(width, freq, &htim5, TIM_CHANNEL_4); // delay(1); cnt++; - u8g2_t* fb = furi_take(fb_record); - if(fb != NULL) { - u8g2_SetDrawColor(fb, 0); - u8g2_DrawBox(fb, 0, 0, 120, 30); - - u8g2_SetFont(fb, u8g2_font_6x10_mf); - u8g2_SetDrawColor(fb, 1); - u8g2_SetFontMode(fb, 1); - char buf[64]; - sprintf(buf, "freq: %d Hz", (uint32_t)freq); - u8g2_DrawStr(fb, 2 + width * 20, 12 + freq / 100, buf); - } - furi_commit(fb_record); - delay(100); } } diff --git a/applications/examples/blink.c b/applications/examples/blink.c index d6d23bf2..195a13cf 100644 --- a/applications/examples/blink.c +++ b/applications/examples/blink.c @@ -1,16 +1,20 @@ #include "flipper.h" +#include "flipper_v2.h" void application_blink(void* p) { // create pin GpioPin led = {.pin = GPIO_PIN_8, .port = GPIOA}; + // TODO open record + GpioPin* led_record = &led; + // configure pin - pinMode(led, GpioModeOpenDrain); + pinMode(led_record, GpioModeOutputOpenDrain); while(1) { - digitalWrite(led, HIGH); + digitalWrite(led_record, HIGH); delay(500); - digitalWrite(led, LOW); + digitalWrite(led_record, LOW); delay(500); } } \ No newline at end of file diff --git a/applications/examples/uart_write.c b/applications/examples/uart_write.c index 1025eeaa..95d6ec49 100644 --- a/applications/examples/uart_write.c +++ b/applications/examples/uart_write.c @@ -1,11 +1,15 @@ #include "flipper.h" #include #include "log.h" +#include "flipper_v2.h" void application_uart_write(void* p) { // Red led for showing progress GpioPin led = {.pin = GPIO_PIN_8, .port = GPIOA}; - pinMode(led, GpioModeOpenDrain); + // TODO open record + GpioPin* led_record = &led; + + pinMode(led_record, GpioModeOutputOpenDrain); // get_default_log open "tty" record FuriRecordSubscriber* log = get_default_log(); @@ -23,9 +27,9 @@ void application_uart_write(void* p) { counter++; // flash at every send - digitalWrite(led, LOW); + digitalWrite(led_record, LOW); delay(50); - digitalWrite(led, HIGH); + digitalWrite(led_record, HIGH); // delay with overall perion of 1s delay(950); diff --git a/applications/input/input.c b/applications/input/input.c index 814cb2fb..eec75eb1 100644 --- a/applications/input/input.c +++ b/applications/input/input.c @@ -56,7 +56,7 @@ void input_task(void* p) { for(;;) { bool changed = false; for(uint32_t i = 0; i < INPUT_COUNT; i++) { - bool input_state = app_gpio_read(input_gpio[i]) ^ input_invert[i]; + bool input_state = gpio_read(&input_gpio[i]) ^ input_invert[i]; if(input_state) { if(debounce_counters[i] < DEBOUNCE_TICKS) { debounce_counters[i] += 1; diff --git a/applications/irda/irda.c b/applications/irda/irda.c index 9aa5a3d1..1c3d745b 100644 --- a/applications/irda/irda.c +++ b/applications/irda/irda.c @@ -90,13 +90,13 @@ void render_nec(CanvasApi* canvas, State* state) { void input_carrier(Event* event, State* state) { if(event->value.input.input == InputOk) { if(event->value.input.state) { - pwm_set( + hal_pwm_set( duty_cycles[state->carrier_duty_cycle_id], state->carrier_freq, &htim2, TIM_CHANNEL_4); } else { - pwm_stop(&htim2, TIM_CHANNEL_4); + hal_pwm_stop(&htim2, TIM_CHANNEL_4); } } diff --git a/applications/irda/irda_nec.c b/applications/irda/irda_nec.c index 683fbb8f..be5252d7 100644 --- a/applications/irda/irda_nec.c +++ b/applications/irda/irda_nec.c @@ -4,18 +4,18 @@ void ir_nec_preambula(void) { // 9ms carrier + 4.5ms pause - pwm_set(NEC_DUTY_CYCLE, NEC_CARRIER_FREQUENCY, &htim2, TIM_CHANNEL_4); + hal_pwm_set(NEC_DUTY_CYCLE, NEC_CARRIER_FREQUENCY, &htim2, TIM_CHANNEL_4); delay_us(9000); - pwm_stop(&htim2, TIM_CHANNEL_4); + hal_pwm_stop(&htim2, TIM_CHANNEL_4); delay_us(4500); } void ir_nec_send_bit(bool bit) { // 0 is 562.5us carrier + 1687.5us pause // 1 is 562.5us carrier + 562.5us pause - pwm_set(NEC_DUTY_CYCLE, NEC_CARRIER_FREQUENCY, &htim2, TIM_CHANNEL_4); + hal_pwm_set(NEC_DUTY_CYCLE, NEC_CARRIER_FREQUENCY, &htim2, TIM_CHANNEL_4); delay_us(562.5); - pwm_stop(&htim2, TIM_CHANNEL_4); + hal_pwm_stop(&htim2, TIM_CHANNEL_4); if(bit) { delay_us(562.5); } else { diff --git a/applications/template/template.c b/applications/template/template.c.example similarity index 100% rename from applications/template/template.c rename to applications/template/template.c.example diff --git a/applications/tests/test_index.c b/applications/tests/test_index.c index c15985ce..fc459336 100644 --- a/applications/tests/test_index.c +++ b/applications/tests/test_index.c @@ -1,5 +1,6 @@ #include #include "flipper.h" +#include "flipper_v2.h" #include "log.h" // #include "flipper-core.h" TODO: Rust build disabled @@ -12,27 +13,31 @@ void flipper_test_app(void* p) { GpioPin green = {.pin = LED_GREEN_Pin, .port = LED_GREEN_GPIO_Port}; GpioPin blue = {.pin = LED_BLUE_Pin, .port = LED_BLUE_GPIO_Port}; - // configure pins - pinMode(red, GpioModeOpenDrain); - pinMode(green, GpioModeOpenDrain); - pinMode(blue, GpioModeOpenDrain); + GpioPin* red_record = &red; + GpioPin* green_record = &green; + GpioPin* blue_record = &blue; - digitalWrite(red, HIGH); - digitalWrite(green, HIGH); - digitalWrite(blue, LOW); + // configure pins + pinMode(red_record, GpioModeOutputOpenDrain); + pinMode(green_record, GpioModeOutputOpenDrain); + pinMode(blue_record, GpioModeOutputOpenDrain); + + digitalWrite(red_record, HIGH); + digitalWrite(green_record, HIGH); + digitalWrite(blue_record, LOW); uint32_t exitcode = run_minunit(); if(exitcode == 0) { // test passed - digitalWrite(red, HIGH); - digitalWrite(green, LOW); - digitalWrite(blue, HIGH); + digitalWrite(red_record, HIGH); + digitalWrite(green_record, LOW); + digitalWrite(blue_record, HIGH); } else { // test failed - digitalWrite(red, LOW); - digitalWrite(green, HIGH); - digitalWrite(blue, HIGH); + digitalWrite(red_record, LOW); + digitalWrite(green_record, HIGH); + digitalWrite(blue_record, HIGH); } set_exitcode(exitcode); diff --git a/core/api-hal/api-gpio.c b/core/api-hal/api-gpio.c new file mode 100644 index 00000000..74e2adeb --- /dev/null +++ b/core/api-hal/api-gpio.c @@ -0,0 +1,49 @@ +#include "api-gpio.h" + +osMutexId_t gpioInitMutex; + +bool gpio_api_init(void) { + gpioInitMutex = osMutexNew(NULL); + if(gpioInitMutex == NULL) return false; + return true; +} + +// init GPIO +void gpio_init(GpioPin* gpio, GpioMode mode) { + if(osMutexAcquire(gpioInitMutex, osWaitForever) == osOK) { + hal_gpio_init(gpio, mode, GpioPullNo, GpioSpeedLow); + osMutexRelease(gpioInitMutex); + } +} + +// init GPIO, extended version +void gpio_init_ex(GpioPin* gpio, GpioMode mode, GpioPull pull, GpioSpeed speed) { + hal_gpio_init(gpio, mode, pull, speed); +} + +// put GPIO to Z-state +void gpio_disable(GpioDisableRecord* gpio_record) { + GpioPin* gpio_pin = acquire_mutex(gpio_record->gpio_mutex, 0); + if(gpio_pin == NULL) { + gpio_pin = gpio_record->gpio; + } + hal_gpio_init(gpio_pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow); + release_mutex(gpio_record->gpio_mutex, gpio_pin); +} + +// get GPIO record +ValueMutex* gpio_open_mutex(const char* name) { + ValueMutex* gpio_mutex = (ValueMutex*)furi_open(name); + + // TODO disable gpio on app exit + //if(gpio_mutex != NULL) flapp_on_exit(gpio_disable, gpio_mutex); + + return gpio_mutex; +} + +// get GPIO record and acquire mutex +GpioPin* gpio_open(const char* name) { + ValueMutex* gpio_mutex = gpio_open_mutex(name); + GpioPin* gpio_pin = acquire_mutex(gpio_mutex, FLIPPER_HELPER_TIMEOUT); + return gpio_pin; +} \ No newline at end of file diff --git a/core/api-hal/api-gpio.h b/core/api-hal/api-gpio.h new file mode 100644 index 00000000..e9176393 --- /dev/null +++ b/core/api-hal/api-gpio.h @@ -0,0 +1,37 @@ +#pragma once +#include "flipper.h" +#include "flipper_v2.h" +#include "api-hal-gpio.h" + +typedef struct { + ValueMutex* gpio_mutex; + GpioPin* gpio; +} GpioDisableRecord; + +// init GPIO API +bool gpio_api_init(); + +// init GPIO +void gpio_init(GpioPin* gpio, GpioMode mode); + +// init GPIO, extended version +void gpio_init_ex(GpioPin* gpio, GpioMode mode, GpioPull pull, GpioSpeed speed); + +// write value to GPIO, false = LOW, true = HIGH +static inline void gpio_write(GpioPin* gpio, bool state) { + hal_gpio_write(gpio, state); +} + +// read value from GPIO, false = LOW, true = HIGH +static inline bool gpio_read(GpioPin* gpio) { + return hal_gpio_read(gpio); +} + +// put GPIO to Z-state +void gpio_disable(GpioDisableRecord* gpio_record); + +// get GPIO record +ValueMutex* gpio_open_mutex(const char* name); + +// get GPIO record and acquire mutex +GpioPin* gpio_open(const char* name); \ No newline at end of file diff --git a/core/app.cpp b/core/app.cpp index ff179510..ef994292 100644 --- a/core/app.cpp +++ b/core/app.cpp @@ -2,6 +2,7 @@ extern "C" { #include "flipper.h" +#include "flipper_v2.h" #include "log.h" #include "applications.h" #include "tty_uart.h" @@ -14,6 +15,7 @@ extern "C" void set_exitcode(uint32_t _exitcode) { } extern "C" int app() { + init_flipper_api(); register_tty_uart(); FuriRecordSubscriber* log = get_default_log(); diff --git a/core/core.mk b/core/core.mk index c9a00e80..478e4e80 100644 --- a/core/core.mk +++ b/core/core.mk @@ -4,4 +4,5 @@ CFLAGS += -I$(CORE_DIR) ASM_SOURCES += $(wildcard $(CORE_DIR)/*.s) C_SOURCES += $(wildcard $(CORE_DIR)/*.c) C_SOURCES += $(wildcard $(CORE_DIR)/api-basic/*.c) +C_SOURCES += $(wildcard $(CORE_DIR)/api-hal/*.c) CPP_SOURCES += $(wildcard $(CORE_DIR)/*.cpp) diff --git a/core/flipper.h b/core/flipper.h index 6f867a5f..a1dc6659 100644 --- a/core/flipper.h +++ b/core/flipper.h @@ -5,7 +5,7 @@ extern "C" { #endif #include "main.h" -#include "flipper_hal.h" +#include "api-hal.h" #include "cmsis_os.h" #include "furi-deprecated.h" @@ -17,21 +17,7 @@ extern "C" { #endif #include - -// Arduino defines - -#define pinMode app_gpio_init -#define digitalWrite app_gpio_write -#define digitalRead app_gpio_read -#define EEMEM -#define delayMicroseconds delay_us -#define delay osDelay -#define byte uint8_t - -#define OUTPUT GpioModeOutput -#define INPUT GpioModeInput -#define LOW false -#define HIGH true +#include "flipper_arduino.h" void set_exitcode(uint32_t _exitcode); diff --git a/core/flipper_arduino.h b/core/flipper_arduino.h new file mode 100644 index 00000000..fe1da069 --- /dev/null +++ b/core/flipper_arduino.h @@ -0,0 +1,16 @@ +#pragma once +#include "stdint.h" + +// Arduino defines +#define pinMode gpio_init +#define digitalWrite gpio_write +#define digitalRead gpio_read +#define delayMicroseconds delay_us +#define delay osDelay + +#define OUTPUT GpioModeOutputPushPull +#define INPUT GpioModeInput +#define LOW false +#define HIGH true + +typedef uint8_t byte; \ No newline at end of file diff --git a/core/flipper_v2.c b/core/flipper_v2.c new file mode 100644 index 00000000..9a4e4bc6 --- /dev/null +++ b/core/flipper_v2.c @@ -0,0 +1,5 @@ +#include "flipper_v2.h" + +bool init_flipper_api(void) { + return gpio_api_init(); +} \ No newline at end of file diff --git a/core/flipper_v2.h b/core/flipper_v2.h index ca362468..cb6b63e8 100644 --- a/core/flipper_v2.h +++ b/core/flipper_v2.h @@ -14,8 +14,15 @@ extern "C" { #include "api-basic/memmgr.h" +#include "api-hal/api-gpio.h" + #include "gui/gui.h" +// tmeout for helper functions +#define FLIPPER_HELPER_TIMEOUT 10 + +bool init_flipper_api(void); + #ifdef __cplusplus } #endif \ No newline at end of file diff --git a/firmware/targets/f2/Inc/flipper_hal.h b/firmware/targets/f2/Inc/flipper_hal.h deleted file mode 100644 index 9e12a237..00000000 --- a/firmware/targets/f2/Inc/flipper_hal.h +++ /dev/null @@ -1,98 +0,0 @@ -/* -Flipper devices inc. - -GPIO and HAL implementations -*/ - -#pragma once - -#include -#include "main.h" - -typedef enum { GpioModeInput, GpioModeOutput, GpioModeOpenDrain } GpioMode; - -typedef struct { - GPIO_TypeDef* port; - uint32_t pin; -} GpioPin; - -void app_gpio_init(GpioPin gpio, GpioMode mode); - -static inline void app_gpio_write(GpioPin gpio, bool state) { - if(gpio.pin != 0) { - if(state) { - gpio.port->BSRR = (uint32_t)gpio.pin; - } else { - gpio.port->BRR = (uint32_t)gpio.pin; - } - } -} - -static inline bool app_gpio_read(GpioPin gpio) { - if(gpio.pin != 0) { - return (gpio.port->IDR & gpio.pin) != 0x00u; - } - - return false; -} -void delay_us_init_DWT(void); -void delay_us(float time); - -void pwm_set(float value, float freq, TIM_HandleTypeDef* tim, uint32_t channel); -void pwm_stop(TIM_HandleTypeDef* tim, uint32_t channel); - -extern TIM_HandleTypeDef htim8; - -static inline void app_tim_ic_init(bool both) { - HAL_TIM_OC_Stop(&htim8, TIM_CHANNEL_2); - - TIM_IC_InitTypeDef sConfigIC = {0}; - sConfigIC.ICPolarity = both ? TIM_INPUTCHANNELPOLARITY_BOTHEDGE : - TIM_INPUTCHANNELPOLARITY_FALLING; - sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI; - sConfigIC.ICPrescaler = TIM_ICPSC_DIV1; - sConfigIC.ICFilter = 0; - HAL_TIM_IC_ConfigChannel(&htim8, &sConfigIC, TIM_CHANNEL_2); - - HAL_TIM_IC_Start_IT(&htim8, TIM_CHANNEL_2); -} - -static inline void app_tim_pulse(uint32_t width) { - htim8.State = HAL_TIM_STATE_BUSY; - - __HAL_TIM_DISABLE(&htim8); - - __HAL_TIM_SET_COUNTER(&htim8, 0); - - TIM_OC_InitTypeDef sConfigOC; - sConfigOC.OCMode = TIM_OCMODE_INACTIVE; - sConfigOC.Pulse = (uint16_t)(width); - sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; - sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; - // HAL_TIM_OC_ConfigChannel(&htim8, &sConfigOC, TIM_CHANNEL_2); - - htim8.Lock = HAL_LOCKED; - - /* Configure the TIM Channel 2 in Output Compare */ - TIM_OC2_SetConfig(htim8.Instance, &sConfigOC); - - htim8.Lock = HAL_UNLOCKED; - - // TIM_CCxChannelCmd(htim8.Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE); - - /* Reset the CCxE Bit */ - htim8.Instance->CCER &= ~(TIM_CCER_CC1E << (TIM_CHANNEL_2 & 0x1FU)); - - /* Set or reset the CCxE Bit */ - htim8.Instance->CCER |= (uint32_t)(TIM_CCx_ENABLE << (TIM_CHANNEL_2 & 0x1FU)); - - __HAL_TIM_MOE_ENABLE(&htim8); - __HAL_TIM_ENABLE(&htim8); - - htim8.State = HAL_TIM_STATE_READY; -} - -static inline void app_tim_stop() { - HAL_TIM_OC_Stop(&htim8, TIM_CHANNEL_2); - HAL_TIM_IC_Stop(&htim8, TIM_CHANNEL_2); -} diff --git a/firmware/targets/f2/Inc/input_priv.h b/firmware/targets/f2/Inc/input_priv.h index 90fd495e..86ba658d 100644 --- a/firmware/targets/f2/Inc/input_priv.h +++ b/firmware/targets/f2/Inc/input_priv.h @@ -2,7 +2,7 @@ #define __INPUT_PRIV_H #include "main.h" -#include "flipper_hal.h" +#include "flipper_v2.h" #define DEBOUNCE_TICKS 10 diff --git a/firmware/targets/f2/Src/flipper_hal.c b/firmware/targets/f2/Src/flipper_hal.c deleted file mode 100644 index 04264b20..00000000 --- a/firmware/targets/f2/Src/flipper_hal.c +++ /dev/null @@ -1,69 +0,0 @@ -/* -Flipper devices inc. - -GPIO and HAL implementations -*/ - -#include "main.h" -#include "flipper_hal.h" - -void app_gpio_init(GpioPin gpio, GpioMode mode) { - if(gpio.pin != 0) { - GPIO_InitTypeDef GPIO_InitStruct; - - GPIO_InitStruct.Pin = gpio.pin; - GPIO_InitStruct.Pull = GPIO_NOPULL; - - switch(mode) { - case GpioModeInput: - GPIO_InitStruct.Mode = GPIO_MODE_INPUT; - break; - - case GpioModeOutput: - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; - break; - - case GpioModeOpenDrain: - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; - break; - } - - HAL_GPIO_Init(gpio.port, &GPIO_InitStruct); - } -} - -void delay_us_init_DWT(void) { - CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; - DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; - DWT->CYCCNT = 0U; -} - -void delay_us(float time) { - uint32_t start = DWT->CYCCNT; - uint32_t time_ticks = time * (SystemCoreClock / 1000000); - while((DWT->CYCCNT - start) < time_ticks) { - }; -} - -void pwm_set(float value, float freq, TIM_HandleTypeDef* tim, uint32_t channel) { - tim->Init.CounterMode = TIM_COUNTERMODE_UP; - tim->Init.Period = (uint32_t)((SystemCoreClock / (tim->Init.Prescaler + 1)) / freq); - tim->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; - tim->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; - HAL_TIM_PWM_Init(tim); - - TIM_OC_InitTypeDef sConfigOC; - - sConfigOC.OCMode = TIM_OCMODE_PWM1; - sConfigOC.Pulse = (uint16_t)(tim->Init.Period * value); - sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; - sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; - HAL_TIM_PWM_ConfigChannel(tim, &sConfigOC, channel); - HAL_TIM_PWM_Start(tim, channel); -} - -void pwm_stop(TIM_HandleTypeDef* tim, uint32_t channel) { - HAL_TIM_PWM_Stop(tim, channel); -} \ No newline at end of file diff --git a/firmware/targets/f2/Src/main.c b/firmware/targets/f2/Src/main.c index 87b31489..4780056f 100644 --- a/firmware/targets/f2/Src/main.c +++ b/firmware/targets/f2/Src/main.c @@ -31,7 +31,7 @@ /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "fatfs/fatfs.h" -#include "flipper_hal.h" +#include "api-hal.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ diff --git a/firmware/targets/f2/api-hal/api-hal-delay.c b/firmware/targets/f2/api-hal/api-hal-delay.c new file mode 100644 index 00000000..53db1fb0 --- /dev/null +++ b/firmware/targets/f2/api-hal/api-hal-delay.c @@ -0,0 +1,14 @@ +#include "api-hal-delay.h" + +void delay_us_init_DWT(void) { + CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; + DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; + DWT->CYCCNT = 0U; +} + +void delay_us(float time) { + uint32_t start = DWT->CYCCNT; + uint32_t time_ticks = time * (SystemCoreClock / 1000000); + while((DWT->CYCCNT - start) < time_ticks) { + }; +} \ No newline at end of file diff --git a/firmware/targets/f2/api-hal/api-hal-delay.h b/firmware/targets/f2/api-hal/api-hal-delay.h new file mode 100644 index 00000000..4df5cd4b --- /dev/null +++ b/firmware/targets/f2/api-hal/api-hal-delay.h @@ -0,0 +1,5 @@ +#pragma once +#include "main.h" + +void delay_us(float time); +void delay_us_init_DWT(void); \ No newline at end of file diff --git a/firmware/targets/f2/api-hal/api-hal-gpio.c b/firmware/targets/f2/api-hal/api-hal-gpio.c new file mode 100644 index 00000000..cf886353 --- /dev/null +++ b/firmware/targets/f2/api-hal/api-hal-gpio.c @@ -0,0 +1,14 @@ +#include "api-hal-gpio.h" + +// init GPIO +void hal_gpio_init(GpioPin* gpio, GpioMode mode, GpioPull pull, GpioSpeed speed) { + // TODO: Alternate Functions + GPIO_InitTypeDef GPIO_InitStruct = {0}; + + GPIO_InitStruct.Pin = gpio->pin; + GPIO_InitStruct.Mode = mode; + GPIO_InitStruct.Pull = pull; + GPIO_InitStruct.Speed = speed; + + HAL_GPIO_Init(gpio->port, &GPIO_InitStruct); +} diff --git a/firmware/targets/f2/api-hal/api-hal-gpio.h b/firmware/targets/f2/api-hal/api-hal-gpio.h new file mode 100644 index 00000000..e31b63f9 --- /dev/null +++ b/firmware/targets/f2/api-hal/api-hal-gpio.h @@ -0,0 +1,61 @@ +#pragma once +#include "main.h" +#include "stdbool.h" + +// this defined in xx_hal_gpio.c, so... +#define GPIO_NUMBER (16U) + +typedef enum { + GpioModeInput = GPIO_MODE_INPUT, + GpioModeOutputPushPull = GPIO_MODE_OUTPUT_PP, + GpioModeOutputOpenDrain = GPIO_MODE_OUTPUT_OD, + GpioModeAltFunctionPushPull = GPIO_MODE_AF_PP, + GpioModeAltFunctionOpenDrain = GPIO_MODE_AF_OD, + GpioModeAnalog = GPIO_MODE_ANALOG, + GpioModeInterruptRise = GPIO_MODE_IT_RISING, + GpioModeInterruptFall = GPIO_MODE_IT_FALLING, + GpioModeInterruptRiseFall = GPIO_MODE_IT_RISING_FALLING, + GpioModeEventRise = GPIO_MODE_EVT_RISING, + GpioModeEventFall = GPIO_MODE_EVT_FALLING, + GpioModeEventRiseFall = GPIO_MODE_EVT_RISING_FALLING, +} GpioMode; + +typedef enum { + GpioSpeedLow = GPIO_SPEED_FREQ_LOW, + GpioSpeedMedium = GPIO_SPEED_FREQ_MEDIUM, + GpioSpeedHigh = GPIO_SPEED_FREQ_HIGH, + GpioSpeedVeryHigh = GPIO_SPEED_FREQ_VERY_HIGH, +} GpioSpeed; + +typedef enum { + GpioPullNo = GPIO_NOPULL, + GpioPullUp = GPIO_PULLUP, + GpioPullDown = GPIO_PULLDOWN, +} GpioPull; + +typedef struct { + GPIO_TypeDef* port; + uint16_t pin; +} GpioPin; + +// init GPIO +void hal_gpio_init(GpioPin* gpio, GpioMode mode, GpioPull pull, GpioSpeed speed); + +// write value to GPIO, false = LOW, true = HIGH +static inline void hal_gpio_write(GpioPin* gpio, bool state) { + // writing to BSSR is an atomic operation + if(state == true) { + gpio->port->BSRR = gpio->pin; + } else { + gpio->port->BSRR = (uint32_t)gpio->pin << GPIO_NUMBER; + } +} + +// read value from GPIO, false = LOW, true = HIGH +static inline bool hal_gpio_read(GpioPin* gpio) { + if((gpio->port->IDR & gpio->pin) != 0x00U) { + return true; + } else { + return false; + } +} \ No newline at end of file diff --git a/firmware/targets/f2/api-hal/api-hal-pwm.c b/firmware/targets/f2/api-hal/api-hal-pwm.c new file mode 100644 index 00000000..f102e349 --- /dev/null +++ b/firmware/targets/f2/api-hal/api-hal-pwm.c @@ -0,0 +1,22 @@ +#include "api-hal-pwm.h" + +void hal_pwm_set(float value, float freq, TIM_HandleTypeDef* tim, uint32_t channel) { + tim->Init.CounterMode = TIM_COUNTERMODE_UP; + tim->Init.Period = (uint32_t)((SystemCoreClock / (tim->Init.Prescaler + 1)) / freq); + tim->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + tim->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + HAL_TIM_PWM_Init(tim); + + TIM_OC_InitTypeDef sConfigOC; + + sConfigOC.OCMode = TIM_OCMODE_PWM1; + sConfigOC.Pulse = (uint16_t)(tim->Init.Period * value); + sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; + sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; + HAL_TIM_PWM_ConfigChannel(tim, &sConfigOC, channel); + HAL_TIM_PWM_Start(tim, channel); +} + +void hal_pwm_stop(TIM_HandleTypeDef* tim, uint32_t channel) { + HAL_TIM_PWM_Stop(tim, channel); +} \ No newline at end of file diff --git a/firmware/targets/f2/api-hal/api-hal-pwm.h b/firmware/targets/f2/api-hal/api-hal-pwm.h new file mode 100644 index 00000000..604a55f8 --- /dev/null +++ b/firmware/targets/f2/api-hal/api-hal-pwm.h @@ -0,0 +1,6 @@ +#pragma once +#include "main.h" +#include "stdbool.h" + +void hal_pwm_set(float value, float freq, TIM_HandleTypeDef* tim, uint32_t channel); +void hal_pwm_stop(TIM_HandleTypeDef* tim, uint32_t channel); \ No newline at end of file diff --git a/firmware/targets/f2/api-hal/api-hal.h b/firmware/targets/f2/api-hal/api-hal.h new file mode 100644 index 00000000..b5dd7707 --- /dev/null +++ b/firmware/targets/f2/api-hal/api-hal.h @@ -0,0 +1,5 @@ +#pragma once + +#include "api-hal-gpio.h" +#include "api-hal-delay.h" +#include "api-hal-pwm.h" diff --git a/firmware/targets/f2/target.mk b/firmware/targets/f2/target.mk index f9a5e109..2359df93 100644 --- a/firmware/targets/f2/target.mk +++ b/firmware/targets/f2/target.mk @@ -64,7 +64,8 @@ C_SOURCES += \ $(CUBE_DIR)/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ioreq.c \ $(CUBE_DIR)/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Src/usbd_cdc.c \ $(wildcard $(TARGET_DIR)/Src/*.c) \ - $(wildcard $(TARGET_DIR)/Src/fatfs/*.c) + $(wildcard $(TARGET_DIR)/Src/fatfs/*.c) \ + $(wildcard $(TARGET_DIR)/api-hal/*.c) ASM_SOURCES += $(TARGET_DIR)/startup_stm32l476xx.s @@ -93,5 +94,6 @@ CFLAGS += \ -I$(CUBE_DIR)/Drivers/CMSIS/Device/ST/STM32L4xx/Include \ -I$(CUBE_DIR)/Drivers/CMSIS/Include \ -I$(CUBE_DIR)/Drivers/CMSIS/Include \ - -I$(TARGET_DIR)/Src/fatfs + -I$(TARGET_DIR)/Src/fatfs \ + -I$(TARGET_DIR)/api-hal diff --git a/firmware/targets/local/Inc/flipper_hal.h b/firmware/targets/local/Inc/flipper_hal.h index 230b710c..cafe8dcc 100644 --- a/firmware/targets/local/Inc/flipper_hal.h +++ b/firmware/targets/local/Inc/flipper_hal.h @@ -10,56 +10,6 @@ GPIO and HAL implementations #include #include "main.h" -typedef enum { GpioModeInput, GpioModeOutput, GpioModeOpenDrain } GpioMode; - -typedef struct { - const char* port; - uint32_t pin; - GpioMode mode; -} GpioPin; - -void app_gpio_init(GpioPin gpio, GpioMode mode); - -static inline void app_gpio_write(GpioPin gpio, bool state) { - if(gpio.pin != 0) { - if(state) { - printf("[GPIO] %s%d on\n", gpio.port, gpio.pin); - } else { - printf("[GPIO] %s%d off\n", gpio.port, gpio.pin); - } - } else { - printf("[GPIO] no pin\n"); - } -} - -static inline bool app_gpio_read(GpioPin gpio) { - // TODO emulate pin state? - - return false; -} - -typedef enum { GPIO_PIN_SET = 1, GPIO_PIN_RESET = 0 } HAL_GPIO_PIN_STATE; - -void HAL_GPIO_WritePin(const char* port, uint32_t pin, HAL_GPIO_PIN_STATE state); - -void delay_us(uint32_t time); - -void pwm_set(float value, float freq, TIM_HandleTypeDef* tim, uint32_t channel); - -extern TIM_HandleTypeDef htim8; - -static inline void app_tim_ic_init(bool both) { - printf("[TIM] init\n"); -} - -static inline void app_tim_pulse(uint32_t width) { - printf("[TIM] pulse %d\n", width); -} - -static inline void app_tim_stop() { - printf("[TIM] stop\n"); -} - #define GPIOA "PA" #define GPIOB "PB" #define GPIOC "PC" diff --git a/firmware/targets/local/Src/flipper_hal.c b/firmware/targets/local/Src/flipper_hal.c index a121bb43..65392da9 100644 --- a/firmware/targets/local/Src/flipper_hal.c +++ b/firmware/targets/local/Src/flipper_hal.c @@ -8,41 +8,6 @@ GPIO and HAL implementations #include "flipper_hal.h" #include -void app_gpio_init(GpioPin gpio, GpioMode mode) { - if(gpio.pin != 0) { - switch(mode) { - case GpioModeInput: - printf("[GPIO] %s%d input\n", gpio.port, gpio.pin); - break; - - case GpioModeOutput: - printf("[GPIO] %s%d push pull\n", gpio.port, gpio.pin); - break; - - case GpioModeOpenDrain: - printf("[GPIO] %s%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.); -} - -void HAL_GPIO_WritePin(const char* port, uint32_t pin, HAL_GPIO_PIN_STATE state) { - printf("[GPIO] set pin %s:%d = %d\n", port, pin, state); -} - HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef* hspi, uint8_t* pData, uint16_t size, uint32_t Timeout) { printf("[SPI] write %d to %s: ", size, *hspi); diff --git a/firmware/targets/local/api-hal/api-hal-delay.c b/firmware/targets/local/api-hal/api-hal-delay.c new file mode 100644 index 00000000..e40d084f --- /dev/null +++ b/firmware/targets/local/api-hal/api-hal-delay.c @@ -0,0 +1,7 @@ +#include "api-hal-delay.h" +#include + +void delay_us(uint32_t time) { + // How to deal with it + printf("[DELAY] %d us\n", time); +} \ No newline at end of file diff --git a/firmware/targets/local/api-hal/api-hal-delay.h b/firmware/targets/local/api-hal/api-hal-delay.h new file mode 100644 index 00000000..9db6e3a8 --- /dev/null +++ b/firmware/targets/local/api-hal/api-hal-delay.h @@ -0,0 +1,4 @@ +#pragma once +#include "main.h" + +void delay_us(uint32_t time); \ No newline at end of file diff --git a/firmware/targets/local/api-hal/api-hal-gpio.c b/firmware/targets/local/api-hal/api-hal-gpio.c new file mode 100644 index 00000000..13124ace --- /dev/null +++ b/firmware/targets/local/api-hal/api-hal-gpio.c @@ -0,0 +1,47 @@ +#include "api-hal-gpio.h" +#include + +// init GPIO +void hal_gpio_init(GpioPin* gpio, GpioMode mode, GpioPull pull, GpioSpeed speed){ + // TODO more mode + if(gpio->pin != 0) { + switch(mode) { + case GpioModeInput: + printf("[GPIO] %s%d input\n", gpio->port, gpio->pin); + break; + + case GpioModeOutputPushPull: + printf("[GPIO] %s%d push pull\n", gpio->port, gpio->pin); + break; + + case GpioModeOutputOpenDrain: + printf("[GPIO] %s%d open drain\n", gpio->port, gpio->pin); + break; + + default: + printf("[GPIO] %s%d mode %d unsupported\n", gpio->port, gpio->pin, mode); + break; + } + } else { + printf("[GPIO] no pin\n"); + } +} + +// write value to GPIO, false = LOW, true = HIGH +void hal_gpio_write(GpioPin* gpio, bool state){ + if(gpio->pin != 0) { + if(state) { + printf("[GPIO] %s%d on\n", gpio->port, gpio->pin); + } else { + printf("[GPIO] %s%d off\n", gpio->port, gpio->pin); + } + } else { + printf("[GPIO] no pin\n"); + } +} + +// read value from GPIO, false = LOW, true = HIGH +bool hal_gpio_read(GpioPin* gpio){ + // TODO emulate pin state? + return false; +} diff --git a/firmware/targets/local/api-hal/api-hal-gpio.h b/firmware/targets/local/api-hal/api-hal-gpio.h new file mode 100644 index 00000000..e6dd4856 --- /dev/null +++ b/firmware/targets/local/api-hal/api-hal-gpio.h @@ -0,0 +1,49 @@ +#pragma once +#include "main.h" +#include "stdbool.h" + +// hw-api + +typedef char GPIO_TypeDef; + +typedef enum { + GpioModeInput, + GpioModeOutputPushPull, + GpioModeOutputOpenDrain, + GpioModeAltFunctionPushPull, + GpioModeAltFunctionOpenDrain, + GpioModeAnalog, + GpioModeInterruptRise, + GpioModeInterruptFall, + GpioModeInterruptRiseFall, + GpioModeEventRise, + GpioModeEventFall, + GpioModeEventRiseFall, +} GpioMode; + +typedef enum { + GpioSpeedLow, + GpioSpeedMedium, + GpioSpeedHigh, + GpioSpeedVeryHigh, +} GpioSpeed; + +typedef enum { + GpioPullNo, + GpioPullUp, + GpioPullDown, +} GpioPull; + +typedef struct { + GPIO_TypeDef* port; + uint16_t pin; +} GpioPin; + +// init GPIO +void hal_gpio_init(GpioPin* gpio, GpioMode mode, GpioPull pull, GpioSpeed speed); + +// write value to GPIO, false = LOW, true = HIGH +void hal_gpio_write(GpioPin* gpio, bool state); + +// read value from GPIO, false = LOW, true = HIGH +bool hal_gpio_read(GpioPin* gpio); \ No newline at end of file diff --git a/firmware/targets/local/api-hal/api-hal.h b/firmware/targets/local/api-hal/api-hal.h new file mode 100644 index 00000000..0125618f --- /dev/null +++ b/firmware/targets/local/api-hal/api-hal.h @@ -0,0 +1,5 @@ +#pragma once + +#include "api-hal-gpio.h" +#include "api-hal-delay.h" +#include "flipper_hal.h" \ No newline at end of file diff --git a/firmware/targets/local/target.mk b/firmware/targets/local/target.mk index 741b16ab..d8ae2454 100644 --- a/firmware/targets/local/target.mk +++ b/firmware/targets/local/target.mk @@ -18,5 +18,8 @@ C_SOURCES += $(TARGET_DIR)/fatfs/syscall.c # memory manager C_SOURCES += $(TARGET_DIR)/Src/heap_4.c +CFLAGS += -I$(TARGET_DIR)/api-hal +C_SOURCES += $(wildcard $(TARGET_DIR)/api-hal/*.c) + run: all $(OBJ_DIR)/$(PROJECT).elf \ No newline at end of file