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 <who.just.the.doctor@gmail.com>
This commit is contained in:
coreglitch 2020-10-26 13:16:54 +06:00 committed by GitHub
parent c8cc3c7dc8
commit f5b342abbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
41 changed files with 464 additions and 341 deletions

View File

@ -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
};

View File

@ -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

View File

@ -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);

View File

@ -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);
}

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -1,11 +1,15 @@
#include "flipper.h"
#include <string.h>
#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);

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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 {

View File

@ -1,5 +1,6 @@
#include <stdio.h>
#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);

49
core/api-hal/api-gpio.c Normal file
View File

@ -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;
}

37
core/api-hal/api-gpio.h Normal file
View File

@ -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);

View File

@ -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();

View File

@ -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)

View File

@ -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 <stdio.h>
// 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);

16
core/flipper_arduino.h Normal file
View File

@ -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;

5
core/flipper_v2.c Normal file
View File

@ -0,0 +1,5 @@
#include "flipper_v2.h"
bool init_flipper_api(void) {
return gpio_api_init();
}

View File

@ -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

View File

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

View File

@ -2,7 +2,7 @@
#define __INPUT_PRIV_H
#include "main.h"
#include "flipper_hal.h"
#include "flipper_v2.h"
#define DEBOUNCE_TICKS 10

View File

@ -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);
}

View File

@ -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 -----------------------------------------------------------*/

View File

@ -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) {
};
}

View File

@ -0,0 +1,5 @@
#pragma once
#include "main.h"
void delay_us(float time);
void delay_us_init_DWT(void);

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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);

View File

@ -0,0 +1,5 @@
#pragma once
#include "api-hal-gpio.h"
#include "api-hal-delay.h"
#include "api-hal-pwm.h"

View File

@ -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

View File

@ -10,56 +10,6 @@ GPIO and HAL implementations
#include <stdbool.h>
#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"

View File

@ -8,41 +8,6 @@ GPIO and HAL implementations
#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] %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);

View File

@ -0,0 +1,7 @@
#include "api-hal-delay.h"
#include <stdio.h>
void delay_us(uint32_t time) {
// How to deal with it
printf("[DELAY] %d us\n", time);
}

View File

@ -0,0 +1,4 @@
#pragma once
#include "main.h"
void delay_us(uint32_t time);

View File

@ -0,0 +1,47 @@
#include "api-hal-gpio.h"
#include <stdio.h>
// 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;
}

View File

@ -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);

View File

@ -0,0 +1,5 @@
#pragma once
#include "api-hal-gpio.h"
#include "api-hal-delay.h"
#include "flipper_hal.h"

View File

@ -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