From 7ff18c4b96f2937badd2ad3e13e529469e4e94b2 Mon Sep 17 00:00:00 2001 From: Tester23 Date: Sat, 23 Dec 2023 11:18:06 +0100 Subject: [PATCH] submit pwm groups test code - doesn't seem to work well, or maybe I need investigate more --- src/driver/drv_local.h | 2 ++ src/driver/drv_main.c | 7 ++++ src/driver/drv_pwm_groups.c | 70 +++++++++++++++++++++++++++++++++++++ src/obk_config.h | 3 ++ 4 files changed, 82 insertions(+) create mode 100644 src/driver/drv_pwm_groups.c diff --git a/src/driver/drv_local.h b/src/driver/drv_local.h index 4d4df0267..112877f85 100644 --- a/src/driver/drv_local.h +++ b/src/driver/drv_local.h @@ -128,6 +128,8 @@ void ChargingLimit_AppendInformationToHTTPIndexPage(http_request_t *request); void RN8209_Init(void); void RN8029_RunEverySecond(void); +void PWMG_Init(); + #define SM2135_DELAY 4 // Software I2C diff --git a/src/driver/drv_main.c b/src/driver/drv_main.c index 3d4d76868..59a0e0c92 100644 --- a/src/driver/drv_main.c +++ b/src/driver/drv_main.c @@ -125,6 +125,13 @@ static driver_t g_drivers[] = { //drvdetail:"requires":""} { "BL0942", BL0942_UART_Init, BL0942_UART_RunEverySecond, BL09XX_AppendInformationToHTTPIndexPage, NULL, NULL, NULL, false }, #endif +#if ENABLE_DRIVER_PWM_GROUP + //drvdetail:{"name":"PWMG", + //drvdetail:"title":"TODO", + //drvdetail:"descr":" ", + //drvdetail:"requires":""} + { "PWMG", PWMG_Init, NULL, NULL, NULL, NULL, NULL, false }, +#endif #if ENABLE_DRIVER_BL0942SPI //drvdetail:{"name":"BL0942SPI", //drvdetail:"title":"TODO", diff --git a/src/driver/drv_pwm_groups.c b/src/driver/drv_pwm_groups.c new file mode 100644 index 000000000..9f43e677a --- /dev/null +++ b/src/driver/drv_pwm_groups.c @@ -0,0 +1,70 @@ +#include "../obk_config.h" + + +#include "../new_common.h" +#include "../new_pins.h" +#include "../new_cfg.h" +// Commands register, execution API and cmd tokenizer +#include "../cmnds/cmd_public.h" +#include "../mqtt/new_mqtt.h" +#include "../logging/logging.h" +#include "drv_local.h" +#include "drv_uart.h" +#include "../httpserver/new_http.h" +#include "../hal/hal_pins.h" +#include + +#if PLATFORM_BK7231N +#include +#endif + +int PIN_GetPWMIndexForPinIndex(int pin); + + +int myDuty(int value, int pwmfrequency) { + //uint32_t value_upscaled = value * 10.0f; //Duty cycle 0...100 -> 0...1000 + uint32_t period = (26000000 / pwmfrequency); //TODO: Move to global variable and set in init func so it does not have to be recalculated every time... + uint32_t duty = (value / 100.0 * period); //No need to use upscaled variable + return duty; +} +static commandResult_t CMD_PWMG_Setup(const void* context, const char* cmd, const char* args, int cmdFlags) { + Tokenizer_TokenizeString(args, TOKENIZER_ALLOW_QUOTES); + // following check must be done after 'Tokenizer_TokenizeString', + // so we know arguments count in Tokenizer. 'cmd' argument is + // only for warning display + if (Tokenizer_CheckArgsCountAndPrintWarning(cmd, 2)) { + return CMD_RES_NOT_ENOUGH_ARGUMENTS; + } + + int err; + // OSStatus bk_pwm_group_initialize(bk_pwm_t pwm1, bk_pwm_t pwm2, + // uint32_t frequency, uint32_t duty_cycle1, uint32_t duty_cycle2, uint32_t dead_band); + int p1 = 6; // PWM0 + int p2 = 7; // PWM1 + int pwm1 = PIN_GetPWMIndexForPinIndex(p1); + int pwm2 = PIN_GetPWMIndexForPinIndex(p2); + int freq = 1000; + int duty1 = (Tokenizer_GetArgIntegerDefault(0, 20)); + int duty2 = (Tokenizer_GetArgIntegerDefault(1, 20)); + int dead = (Tokenizer_GetArgIntegerDefault(2, 10)); + +#if PLATFORM_BK7231N + err = bk_pwm_group_mode_disable(pwm1); + addLogAdv(LOG_INFO, LOG_FEATURE_GENERAL, "bk_pwm_group_mode_disable %i", err); + err = bk_pwm_group_initialize(pwm1, pwm2, freq, duty1, duty2, dead); + addLogAdv(LOG_INFO, LOG_FEATURE_GENERAL, "bk_pwm_group_initialize %i", err); + err = bk_pwm_group_mode_enable(pwm1); + addLogAdv(LOG_INFO, LOG_FEATURE_GENERAL, "bk_pwm_group_mode_enable %i", err); +#endif + + return CMD_RES_OK; +} +// backlog startDriver PWMG; PWMG_Setup 300 200 10 +// backlog startDriver PWMG; PWMG_Setup 100 500 10 +void PWMG_Init() { + + CMD_RegisterCommand("PWMG_Setup", CMD_PWMG_Setup, NULL); +} + + + diff --git a/src/obk_config.h b/src/obk_config.h index e4dcebb35..00355d9f3 100644 --- a/src/obk_config.h +++ b/src/obk_config.h @@ -92,6 +92,9 @@ #define ENABLE_DRIVER_HUE 1 #define ENABLE_DRIVER_CHARGINGLIMIT 1 #define ENABLE_DRIVER_BATTERY 1 +#if PLATFORM_BK7231N +//#define ENABLE_DRIVER_PWM_GROUP 1 +#endif #else