Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
openshwprojects committed Dec 23, 2023
2 parents 73e9b8f + 7ff18c4 commit 8957d11
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/driver/drv_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/driver/drv_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
70 changes: 70 additions & 0 deletions src/driver/drv_pwm_groups.c
Original file line number Diff line number Diff line change
@@ -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 <math.h>

#if PLATFORM_BK7231N
#include <BkDriverPwm.h>
#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);
}



3 changes: 3 additions & 0 deletions src/obk_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 8957d11

Please sign in to comment.