OpenCores
URL https://opencores.org/ocsvn/neorv32/neorv32/trunk

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [lib/] [source/] [neorv32_pwm.c] - Diff between revs 61 and 64

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 61 Rev 64
Line 50... Line 50...
 *
 *
 * @return 0 if PWM was not synthesized, 1 if PWM is available.
 * @return 0 if PWM was not synthesized, 1 if PWM is available.
 **************************************************************************/
 **************************************************************************/
int neorv32_pwm_available(void) {
int neorv32_pwm_available(void) {
 
 
  if (SYSINFO_FEATURES & (1 << SYSINFO_FEATURES_IO_PWM)) {
  if (NEORV32_SYSINFO.SOC & (1 << SYSINFO_SOC_IO_PWM)) {
    return 1;
    return 1;
  }
  }
  else {
  else {
    return 0;
    return 0;
  }
  }
}
}
 
 
 
 
/**********************************************************************//**
/**********************************************************************//**
 * Enable and configure pulse width modulation controller. The PWM control register bits are listed in #NEORV32_PWM_CT_enum.
 * Enable and configure pulse width modulation controller. The PWM control register bits are listed in #NEORV32_PWM_CTRL_enum.
 *
 *
 * @param[in] prsc Clock prescaler select (0..7). See #NEORV32_CLOCK_PRSC_enum.
 * @param[in] prsc Clock prescaler select (0..7). See #NEORV32_CLOCK_PRSC_enum.
 **************************************************************************/
 **************************************************************************/
void neorv32_pwm_setup(uint8_t prsc) {
void neorv32_pwm_setup(uint8_t prsc) {
 
 
  PWM_CT = 0; // reset
  NEORV32_PWM.CTRL = 0; // reset
 
 
  uint32_t ct_enable = 1;
  uint32_t ct_enable = 1;
  ct_enable = ct_enable << PWM_CT_EN;
  ct_enable = ct_enable << PWM_CTRL_EN;
 
 
  uint32_t ct_prsc = (uint32_t)(prsc & 0x07);
  uint32_t ct_prsc = (uint32_t)(prsc & 0x07);
  ct_prsc = ct_prsc << PWM_CT_PRSC0;
  ct_prsc = ct_prsc << PWM_CTRL_PRSC0;
 
 
  PWM_CT = ct_enable | ct_prsc;
  NEORV32_PWM.CTRL = ct_enable | ct_prsc;
}
}
 
 
 
 
/**********************************************************************//**
/**********************************************************************//**
 * Disable pulse width modulation controller.
 * Disable pulse width modulation controller.
 **************************************************************************/
 **************************************************************************/
void neorv32_pwm_disable(void) {
void neorv32_pwm_disable(void) {
 
 
  PWM_CT &= ~((uint32_t)(1 << PWM_CT_EN));
  NEORV32_PWM.CTRL &= ~((uint32_t)(1 << PWM_CTRL_EN));
}
}
 
 
 
 
/**********************************************************************//**
/**********************************************************************//**
 * Enable pulse width modulation controller.
 * Enable pulse width modulation controller.
 **************************************************************************/
 **************************************************************************/
void neorv32_pwm_enable(void) {
void neorv32_pwm_enable(void) {
 
 
  PWM_CT |= ((uint32_t)(1 << PWM_CT_EN));
  NEORV32_PWM.CTRL |= ((uint32_t)(1 << PWM_CTRL_EN));
}
}
 
 
 
 
/**********************************************************************//**
/**********************************************************************//**
 * Get number of implemented channels.
 * Get number of implemented channels.
Line 130... Line 130...
 
 
  if (channel > 59) {
  if (channel > 59) {
    return; // out-of-range
    return; // out-of-range
  }
  }
 
 
  // compute duty-cycle offset
 
  uint32_t reg_offset  = (uint32_t)(channel / 4);
 
  uint8_t  byte_offset = channel % 4;
 
 
 
  // read-modify-write
  // read-modify-write
  uint32_t duty_mask = 0xff;
  uint32_t duty_mask = 0xff;
  uint32_t duty_new  = (uint32_t)duty;
  uint32_t duty_new  = (uint32_t)duty;
 
 
  duty_mask = duty_mask << (byte_offset * 8);
  duty_mask = duty_mask << ((channel % 4) * 8);
  duty_new  = duty_new  << (byte_offset * 8);
  duty_new  = duty_new  << ((channel % 4) * 8);
 
 
  uint32_t duty_cycle = (*(IO_REG32 (&PWM_DUTY0 + reg_offset)));
  uint32_t duty_cycle = NEORV32_PWM.DUTY[channel/4];
 
 
  duty_cycle &= ~duty_mask; // clear previous duty cycle
  duty_cycle &= ~duty_mask; // clear previous duty cycle
  duty_cycle |= duty_new; // set new duty cycle
  duty_cycle |= duty_new; // set new duty cycle
 
 
  (*(IO_REG32 (&PWM_DUTY0 + reg_offset))) = duty_cycle;
  NEORV32_PWM.DUTY[channel/4] = duty_cycle;
}
}
 
 
 
 
/**********************************************************************//**
/**********************************************************************//**
 * Get duty cycle from channel.
 * Get duty cycle from channel.
Line 162... Line 158...
 
 
  if (channel > 59) {
  if (channel > 59) {
    return 0; // out-of-range
    return 0; // out-of-range
  }
  }
 
 
  // compute duty-cycle offset
  uint32_t reg_data = NEORV32_PWM.DUTY[channel/4] >> (((channel % 4) * 8));
  uint32_t reg_offset  = (uint32_t)(channel / 4);
 
  uint8_t  byte_offset = channel % 4;
 
 
 
  // read
 
  uint32_t tmp = (*(IO_REG32 (&PWM_DUTY0 + reg_offset)));
 
  tmp = tmp >> ((byte_offset * 8));
 
 
 
  return (uint8_t)tmp;
  return (uint8_t)reg_data;
}
}
 
 
 No newline at end of file
 No newline at end of file

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.