Skip to content
This repository has been archived by the owner on Feb 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #66 from Rocketct/master
Browse files Browse the repository at this point in the history
changed pwm usage in favour of mbed pwm management
  • Loading branch information
pennam authored Aug 9, 2021
2 parents 8a6cdbe + 05324c0 commit 4983cc8
Showing 1 changed file with 5 additions and 138 deletions.
143 changes: 5 additions & 138 deletions src/Arduino_MachineControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,139 +193,6 @@ class AnalogInClass {

extern AnalogInClass analog_in;

class AnalogOutPWMClass {
public:
AnalogOutPWMClass() {
GPIO_InitTypeDef GPIO_InitStruct;

// Enables peripherals and GPIO Clocks HRTIM1 Peripheral clock enable
__HAL_RCC_HRTIM1_CLK_ENABLE();

// Enable GPIO Channels Clock
__HAL_RCC_GPIOG_CLK_ENABLE();

// Configure HRTIMA TIMA TA1/A2, TIMB TB1/2, TIMC TC1/2, TIMD TD1/2 and TIME TE1.2
// channels as alternate function mode

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF2_HRTIM1;
GPIO_InitStruct.Pin = GPIO_PIN_7;
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);

// Configure the HRTIM peripheral
// Initialize the HRTIM structure
HrtimHandle.Instance = HRTIM1;
HrtimHandle.Init.HRTIMInterruptResquests = HRTIM_IT_NONE;
HrtimHandle.Init.SyncOptions = HRTIM_SYNCOPTION_NONE;

HAL_HRTIM_Init(&HrtimHandle);

// Configure the HRTIM TIME PWM channels 2
sConfig_time_base.Mode = HRTIM_MODE_CONTINUOUS;
sConfig_time_base.Period = 100;
sConfig_time_base.PrescalerRatio = HRTIM_PRESCALERRATIO_DIV1;
sConfig_time_base.RepetitionCounter = 0;

HAL_HRTIM_TimeBaseConfig(&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, &sConfig_time_base);

sConfig_timerE.DMARequests = HRTIM_TIM_DMA_NONE;
sConfig_timerE.HalfModeEnable = HRTIM_HALFMODE_DISABLED;
sConfig_timerE.StartOnSync = HRTIM_SYNCSTART_DISABLED;
sConfig_timerE.ResetOnSync = HRTIM_SYNCRESET_DISABLED;
sConfig_timerE.DACSynchro = HRTIM_DACSYNC_NONE;
sConfig_timerE.PreloadEnable = HRTIM_PRELOAD_ENABLED;
sConfig_timerE.UpdateGating = HRTIM_UPDATEGATING_INDEPENDENT;
sConfig_timerE.BurstMode = HRTIM_TIMERBURSTMODE_MAINTAINCLOCK;
sConfig_timerE.RepetitionUpdate = HRTIM_UPDATEONREPETITION_ENABLED;
sConfig_timerE.ResetUpdate = HRTIM_TIMUPDATEONRESET_DISABLED;
sConfig_timerE.InterruptRequests = HRTIM_TIM_IT_NONE;
sConfig_timerE.PushPull = HRTIM_TIMPUSHPULLMODE_DISABLED;
sConfig_timerE.FaultEnable = HRTIM_TIMFAULTENABLE_NONE;
sConfig_timerE.FaultLock = HRTIM_TIMFAULTLOCK_READWRITE;
sConfig_timerE.DeadTimeInsertion = HRTIM_TIMDEADTIMEINSERTION_DISABLED;
sConfig_timerE.DelayedProtectionMode = HRTIM_TIMER_D_E_DELAYEDPROTECTION_DISABLED;
sConfig_timerE.UpdateTrigger= HRTIM_TIMUPDATETRIGGER_NONE;
sConfig_timerE.ResetTrigger = HRTIM_TIMRESETTRIGGER_NONE;

HAL_HRTIM_WaveformTimerConfig(&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E,&sConfig_timerE);

sConfig_compare.AutoDelayedMode = HRTIM_AUTODELAYEDMODE_REGULAR;
sConfig_compare.AutoDelayedTimeout = 0;
sConfig_compare.CompareValue = 1;

HAL_HRTIM_WaveformCompareConfig(&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, HRTIM_COMPAREUNIT_2, &sConfig_compare);

sConfig_output_config.Polarity = HRTIM_OUTPUTPOLARITY_LOW;
sConfig_output_config.SetSource = HRTIM_OUTPUTRESET_TIMCMP2;
sConfig_output_config.ResetSource = HRTIM_OUTPUTSET_TIMPER;
sConfig_output_config.IdleMode = HRTIM_OUTPUTIDLEMODE_NONE;
sConfig_output_config.IdleLevel = HRTIM_OUTPUTIDLELEVEL_INACTIVE;
sConfig_output_config.FaultLevel = HRTIM_OUTPUTFAULTLEVEL_NONE;
sConfig_output_config.ChopperModeEnable = HRTIM_OUTPUTCHOPPERMODE_DISABLED;
sConfig_output_config.BurstModeEntryDelayed = HRTIM_OUTPUTBURSTMODEENTRY_REGULAR;
sConfig_output_config.ResetSource = HRTIM_OUTPUTRESET_TIMPER;
sConfig_output_config.SetSource = HRTIM_OUTPUTSET_TIMCMP2;

HAL_HRTIM_WaveformOutputConfig(&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, HRTIM_OUTPUT_TE2, &sConfig_output_config);

// Start PWM signals generation
if (HAL_HRTIM_WaveformOutputStart(&HrtimHandle, HRTIM_OUTPUT_TE2) != HAL_OK)
{
// PWM Generation Error
}

// Start HRTIM counter
if (HAL_HRTIM_WaveformCounterStart(&HrtimHandle, HRTIM_TIMERID_TIMER_E) != HAL_OK)
{
// PWM Generation Error
}
}

~AnalogOutPWMClass(){}
void period_ms(int period) {
sConfig_time_base.Mode = HRTIM_MODE_CONTINUOUS;
sConfig_time_base.Period = period;
sConfig_time_base.PrescalerRatio = HRTIM_PRESCALERRATIO_DIV1;
sConfig_time_base.RepetitionCounter = 0;

HAL_HRTIM_TimeBaseConfig(&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, &sConfig_time_base);
}

bool write(uint8_t pulse) {
if (pulse > 100) {
pulse = 100;
}
sConfig_compare.CompareValue = pulse;
if (HAL_HRTIM_WaveformCompareConfig(&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, HRTIM_COMPAREUNIT_2, &sConfig_compare) != HAL_OK)
{
return false;
}
return true;
}

bool stop() {
if (HAL_HRTIM_SimplePWMStop(&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, HRTIM_OUTPUT_TE2) != HAL_OK)
{
return false;
}
return true;
}

private:
HRTIM_HandleTypeDef HrtimHandle;

HRTIM_TimeBaseCfgTypeDef sConfig_time_base;
HRTIM_TimerCfgTypeDef sConfig_timerE;
HRTIM_OutputCfgTypeDef sConfig_output_config;
HRTIM_CompareCfgTypeDef sConfig_compare;
};


extern AnalogOutPWMClass analopwm;


class AnalogOutClass {
public:
void write(int index, float voltage) {
Expand All @@ -341,7 +208,7 @@ class AnalogOutClass {
out_1.write(voltage / 10.5);
break;
case 2:
out_2.write((voltage*9.5));
out_2.write(voltage / 10.5);
break;
case 3:
out_3.write(voltage / 10.5);
Expand All @@ -357,7 +224,7 @@ class AnalogOutClass {
out_1.period_ms(period);
break;
case 2:
out_2.period_ms((period/4)*100);
out_2.period_ms(period);
break;
case 3:
out_3.period_ms(period);
Expand All @@ -370,16 +237,16 @@ class AnalogOutClass {
return out_0;
case 1:
return out_1;
//case 2:
//return out_2;
case 2:
return out_2;
case 3:
return out_3;
}
}
private:
mbed::PwmOut out_0 = mbed::PwmOut(PJ_11);
mbed::PwmOut out_1 = mbed::PwmOut(PK_1);
AnalogOutPWMClass out_2 = AnalogOutPWMClass();
mbed::PwmOut out_2 = mbed::PwmOut(PG_7);
mbed::PwmOut out_3 = mbed::PwmOut(PC_7);
};

Expand Down

0 comments on commit 4983cc8

Please sign in to comment.