Skip to content

Instantly share code, notes, and snippets.

@m3x1m0m
Last active April 25, 2020 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m3x1m0m/4906d02c562a00461db634505b4c2645 to your computer and use it in GitHub Desktop.
Save m3x1m0m/4906d02c562a00461db634505b4c2645 to your computer and use it in GitHub Desktop.
PWM FE310
#include "pwm_fe310.h"
int pwm_set_comp(const uint8_t comp_val)
{
if (comp_val > 100)
{
printf("Error: PWM duty cycle of more than 100 \% is not possible.\n");
return 1;
}
*PWM_CMP_0_ADDR(PWM0_ADDR) = (uint8_t)(((float)comp_val*255.0)/100.0);
return 0;
}
int pwm_initialize(const uint8_t pwm_pin, const uint8_t comp_value, const bool xo)
{
// Make sure 32 bit config register is initialized with all 0
*PWM_CFG_ADDR(PWM0_ADDR) = 0;
// Enable PWM and scale the 16 MHz clock with a factor of 2
*PWM_CFG_ADDR(PWM0_ADDR) |= (1<<PWMENALWAYS) | (0x2<<PWMSCALE);
// Set count to 0
*PWM_COUNT_ADDR(PWM0_ADDR) = 0;
// Set comparator 0 to the initial value
if (fan_pwm_set_comp(comp_value)) {
printf("Error: Could not set PWM comparator.\n");
return 1;
}
// Switch MUX to HW function
*GPIO_IOF_SEL_ADDR(GPIO_ADDR) |= (1<<pwm_pin);
*GPIO_IOF_EN_ADDR(GPIO_ADDR) |= (1<<pwm_pin);
// Enable or disable the XOR at the output GPIO
if (xo) {
*GPIO_OUT_XOR(GPIO_ADDR) |= (1<<pwm_pin);
} else {
*GPIO_OUT_XOR(GPIO_ADDR) &= ~(1<<pwm_pin);
}
if(*PWM_COUNT_ADDR(PWM0_ADDR) == 0)
{
printf("Error: PWM is not running.\n");
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment