Skip to content

Commit

Permalink
pwm freqq (#1203)
Browse files Browse the repository at this point in the history
  • Loading branch information
openshwprojects committed May 2, 2024
1 parent d983502 commit 846a215
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
20 changes: 20 additions & 0 deletions src/cmnds/cmd_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,24 @@ commandResult_t CMD_FindPattern(const void *context, const char *cmd, const char
return CMD_RES_OK;
}*/

#define PWM_FREQUENCY_DEFAULT 1000 //Default Frequency

int g_pwmFrequency = PWM_FREQUENCY_DEFAULT;

commandResult_t CMD_PWMHz(const void* context, const char* cmd, const char* args, int cmdFlags) {

Tokenizer_TokenizeString(args, TOKENIZER_ALLOW_QUOTES | TOKENIZER_DONT_EXPAND);
// 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, 1))
{
return CMD_RES_NOT_ENOUGH_ARGUMENTS;
}

g_pwmFrequency = Tokenizer_GetArgInteger(0);
return CMD_RES_OK;
}
commandResult_t CMD_DeepSleep_SetEdge(const void* context, const char* cmd, const char* args, int cmdFlags) {

Tokenizer_TokenizeString(args, TOKENIZER_ALLOW_QUOTES | TOKENIZER_DONT_EXPAND);
Expand Down Expand Up @@ -797,6 +815,8 @@ void CMD_Init_Early() {

CMD_RegisterCommand("TimeSize", CMD_TimeSize, NULL);

CMD_RegisterCommand("PWMHz", CMD_PWMHz, NULL);

#if (defined WINDOWS) || (defined PLATFORM_BEKEN) || (defined PLATFORM_BL602)
CMD_InitScripting();
#endif
Expand Down
12 changes: 7 additions & 5 deletions src/hal/bk7231/hal_pins_bk7231.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@

//100hz to 20000hz according to tuya code
#define PWM_FREQUENCY_SLOW 600 //Slow frequency for LED Drivers requiring slower PWM Freq
#define PWM_FREQUENCY_DEFAULT 1000 //Default Frequency

int pwmfrequency = PWM_FREQUENCY_DEFAULT;
extern int g_pwmFrequency;

int PIN_GetPWMIndexForPinIndex(int pin) {
if(pin == 6)
Expand Down Expand Up @@ -102,17 +101,20 @@ void HAL_PIN_PWM_Stop(int index) {

void HAL_PIN_PWM_Start(int index) {
int pwmIndex;
int useFreq;

pwmIndex = PIN_GetPWMIndexForPinIndex(index);

// is this pin capable of PWM?
if(pwmIndex == -1) {
return;
}
useFreq = g_pwmFrequency;
//Use slow pwm if user has set checkbox in webif
if(CFG_HasFlag(OBK_FLAG_SLOW_PWM)) pwmfrequency = PWM_FREQUENCY_SLOW;
if(CFG_HasFlag(OBK_FLAG_SLOW_PWM))
useFreq = PWM_FREQUENCY_SLOW;

uint32_t frequency = (26000000 / pwmfrequency);
uint32_t frequency = (26000000 / useFreq);
#if PLATFORM_BK7231N
// OSStatus bk_pwm_initialize(bk_pwm_t pwm, uint32_t frequency, uint32_t duty_cycle);
bk_pwm_initialize(pwmIndex, frequency, 0, 0, 0);
Expand All @@ -136,7 +138,7 @@ void HAL_PIN_PWM_Update(int index, float value) {
value = 100;

//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 period = (26000000 / g_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
#if PLATFORM_BK7231N
bk_pwm_update_param(pwmIndex, period, duty,0,0);
Expand Down

0 comments on commit 846a215

Please sign in to comment.