static bool test_pwm_get_set_frequency_invalid_index(void) { uint32_t frequency = 5000; return pwm_set_frequency(3, frequency) == -1 && pwm_get_frequency(3, &frequency) == -1; }
// python method PWM.set_frequency(channel, frequency) static PyObject *py_set_frequency(PyObject *self, PyObject *args, PyObject *kwargs) { char key[8]; char *channel; float frequency = 1.0; static char *kwlist[] = {"channel", "frequency", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|f", kwlist, &channel, &frequency)) return NULL; if (frequency <= 0.0) { PyErr_SetString(PyExc_ValueError, "frequency must be greater than 0.0"); return NULL; } if (!get_pwm_key(channel, key)) { PyErr_SetString(PyExc_ValueError, "Invalid PWM key or name."); return NULL; } if (pwm_set_frequency(key, frequency) == -1) { PyErr_SetString(PyExc_RuntimeError, "You must start() the PWM channel first"); return NULL; } Py_RETURN_NONE; }
static bool test_pwm_get_set_frequency_before_init(void) { uint32_t frequency = 5000; return pwm_set_frequency(MIKROBUS_1, frequency) == -1 && pwm_get_frequency(MIKROBUS_1, &frequency) == -1; }
int main (void) { board_init(); // Insert application code here, after the board has been initialized. lcd_init(); lcd_cls(); lcd_locate(0,0); lcd_set_font(Lcd_Font_5x7); lcd_write_s(c_GreetingMsg); /* Initialize the PWM driver and set proper frequency */ pwm_init(); pwm_set_duty(0); pwm_set_frequency(Pwm_31250Hz); /* Initialize the encoder and register a callback function */ encoder_init(); encoder_register_event(OnEncoderEvent); while(1) { encoder_cyclic(); switch(CurrentState) { case State_Info: // info_cyclic(&CurrentEventMask); CurrentState = State_MainMenu; break; case State_MainMenu: CurrentState = main_menu_cyclic(MainMessagePump); break; case State_Reflow: // refow_cyclic(); CurrentState = State_MainMenu; break; case State_Learn: // learn_cyclic(); CurrentState = State_MainMenu; break; case State_Parameterize: // parameterize_cyclic(); CurrentState = State_MainMenu; break; default: CurrentState = State_MainMenu; break; } } }
static ssize_t pwm_period_freq_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len) { unsigned long freq_hz; int ret; struct pwm_device *p = dev_get_drvdata(dev); if (!kstrtoul(buf, 10, &freq_hz)) { ret = pwm_set_frequency(p, freq_hz); if (ret < 0) return ret; } return len; }
// python method PWM. ChangeFrequency(self, frequency) static PyObject *PWM_ChangeFrequency(PWMObject *self, PyObject *args) { float frequency = 1.0; if (!PyArg_ParseTuple(args, "f", &frequency)) return NULL; if (frequency <= 0.0) { PyErr_SetString(PyExc_ValueError, "frequency must be greater than 0.0"); return NULL; } self->freq = frequency; pwm_set_frequency(self->gpio, self->freq); Py_RETURN_NONE; }
static bool test_pwm_set_frequency(void) { uint32_t frequency = 5000; uint32_t tmp; uint32_t period; if (pwm_set_frequency(MIKROBUS_1, frequency) == -1) return false; if (pwm_get_frequency(MIKROBUS_1, &tmp) == -1) return false; if (abs(tmp - frequency) > 1) return false; if (pwm_get_period(MIKROBUS_1, &period) == -1) return false; tmp = 1000000000/period; return fabs(tmp - frequency) < 1.f; }
/* Init LED module */ void led_init(void) { /* init module info flags */ SET_BLINKING_STATUS_OFF(); /* init blinking counter */ ui16BlinkingCounter = 0; /* init blinking period counter value */ ui16BlinkPeriodCounter = US_BLINK_PERIOD_COUNTER_VALUE; /* init blinking TON counter value */ ui16BlinkTONCounter = US_BLINK_DC_COUNTER_VALUE; /* init PWM module and channels */ pwm_init(); pwm_set_frequency(1000); pwm_set_dc(PWM_CH1, 0); pwm_set_dc(PWM_CH2, 0); pwm_set_dc(PWM_CH3, 0); pwm_set_dc(PWM_CH4, 0); pwm_start(); }
int setPwmFrequency(char *channel, float frequency) { char key[8]; int allowed = -1; clear_error_msg(); if (frequency <= 0.0) { printf("frequency must be greater than 0.0"); return 0; } if (!get_pwm_key(channel, key)) { printf("Invalid PWM key or name."); return 0; } // Check to see if PWM is allowed on the hardware // A 1 means we're good to go allowed = pwm_allowed(key); if (allowed == -1) { char err[2000]; snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg()); return 0; } else if (allowed == 0) { char err[2000]; snprintf(err, sizeof(err), "PWM %s not available on current Hardware", key); return 0; } if (pwm_set_frequency(key, frequency) < 0) { char err[2000]; snprintf(err, sizeof(err), "PWM: %s issue: (%s)", channel, get_error_msg()); return 0; } }
static bool test_pwm_manual_check(void) { int ret = ask_question("Do you have a frequency meter ?", 15); if (ret == 2) return true; if (ret == -1) return false; if (pwm_init(MIKROBUS_1) < 0) return false; if (pwm_enable(MIKROBUS_1) < 0) return false; printf("The PWM pin on mikrobus 1 has been configured.\n"); printf("frequency: 3kHz\n"); printf("duty cycle: 50%%\n"); if (ask_question("Does it match what you measure ?", 60) != 1) return false; if (pwm_set_frequency(MIKROBUS_1, 10000) < 0) return false; printf("Frequency has been changed to 10kHz.\n"); if (ask_question("Does it match what you measure ?", 60) != 1) return false; if (pwm_disable(MIKROBUS_1) < 0) return false; printf("PWM pin has been disabled.\n"); if (ask_question("Is there still some output?", 60) != 2) return false; return pwm_release(MIKROBUS_1) == 0; }
// python method PWM.__init__(self, channel, frequency) static int PWM_init(PWMObject *self, PyObject *args, PyObject *kwds) { int channel; float frequency; unsigned int tempt_gpio; if (!PyArg_ParseTuple(args, "if", &channel, &frequency)) return -1; // convert channel to gpio if (get_gpio_number(channel, &tempt_gpio, &(self->gpio))) return -1; debug("pwm_init self->gpio = %d\n",self->gpio); // ensure channel set as output if (gpio_direction[self->gpio] != OUTPUT) { PyErr_SetString(PyExc_RuntimeError, "You must setup() the GPIO channel as an output first"); return -1; } if (frequency <= 0.0) { PyErr_SetString(PyExc_ValueError, "frequency must be greater than 0.0"); return -1; } self->freq = frequency; debug("self->gpio = %d and self->freq = %f\n", self->gpio, self->freq); pwm_set_frequency(self->gpio, self->freq); return 0; }
static void buzzer_pwm_set_frequency (uint16_t frequency) { pwm_set_frequency (frequency); pwm_chan_set_duty (pwm_channel, Q15_50_PERCENT); }
int pwm_start(const char *key, float duty, float freq, int polarity) { char fragment[18]; char pwm_test_fragment[20]; char pwm_test_path[45]; char period_path[50]; char duty_path[50]; char polarity_path[55]; int period_fd, duty_fd, polarity_fd; struct pwm_exp *new_pwm, *pwm; if(!BBB_G(pwm_initialized)) { initialize_pwm(); } snprintf(fragment, sizeof(fragment), "bone_pwm_%s", key); if (!load_device_tree(fragment)) { //error enabling pin for pwm return -1; } //creates the fragment in order to build the pwm_test_filename, such as "pwm_test_P9_13" snprintf(pwm_test_fragment, sizeof(pwm_test_fragment), "pwm_test_%s", key); //finds and builds the pwm_test_path, as it can be variable... build_path(BBB_G(ocp_dir), pwm_test_fragment, pwm_test_path, sizeof(pwm_test_path)); //create the path for the period and duty snprintf(period_path, sizeof(period_path), "%s/period", pwm_test_path); snprintf(duty_path, sizeof(duty_path), "%s/duty", pwm_test_path); snprintf(polarity_path, sizeof(polarity_path), "%s/polarity", pwm_test_path); //add period and duty fd to pwm list if ((period_fd = open(period_path, O_RDWR)) < 0) return -1; if ((duty_fd = open(duty_path, O_RDWR)) < 0) { //error, close already opened period_fd. close(period_fd); return -1; } if ((polarity_fd = open(polarity_path, O_RDWR)) < 0) { //error, close already opened period_fd and duty_fd. close(period_fd); close(duty_fd); return -1; } // add to list new_pwm = malloc(sizeof(struct pwm_exp)); if (new_pwm == 0) { return -1; // out of memory } strncpy(new_pwm->key, key, KEYLEN); /* can leave string unterminated */ new_pwm->key[KEYLEN] = '\0'; /* terminate string */ new_pwm->period_fd = period_fd; new_pwm->duty_fd = duty_fd; new_pwm->polarity_fd = polarity_fd; new_pwm->next = NULL; if (exported_pwms == NULL) { // create new list exported_pwms = new_pwm; } else { // add to end of existing list pwm = exported_pwms; while (pwm->next != NULL) pwm = pwm->next; pwm->next = new_pwm; } pwm_set_frequency(key, freq); pwm_set_polarity(key, polarity); pwm_set_duty_cycle(key, duty); return 1; }
/** * \brief Initialize PWM configuration struct and set correct I/O pin to output * * \param config Pointer to PWM configuration struct. * \param tc \ref pwm_tc_t "TC" to use for this PWM. * \param channel \ref pwm_channel_t "CC channel" to use for this PWM. * \param freq_hz Frequency to use for this PWM. */ void pwm_init(struct pwm_config *config, enum pwm_tc_t tc, enum pwm_channel_t channel, uint16_t freq_hz) { /* Number of channels for this TC */ uint8_t num_chan = 0; UNUSED(num_chan); /* Set TC and correct I/O pin to output */ /* * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a> */ switch (tc) { #if defined(TCC0) case PWM_TCC0: config->tc = &TCC0; PORTC.DIR |= (1 << (channel-1)); num_chan = 4; break; #endif #if defined(TCC1) case PWM_TCC1: config->tc = &TCC1; PORTC.DIR |= (1 << (channel+3)); num_chan = 2; break; #endif #if defined(TCD0) case PWM_TCD0: config->tc = &TCD0; PORTD.DIR |= (1 << (channel-1)); num_chan = 4; break; #endif #if defined(TCD1) case PWM_TCD1: config->tc = &TCD1; PORTD.DIR |= (1 << (channel+3)); num_chan = 2; break; #endif #if defined(TCE0) case PWM_TCE0: config->tc = &TCE0; PORTE.DIR |= (1 << (channel-1)); num_chan = 4; break; #endif #if defined(TCE1) case PWM_TCE1: config->tc = &TCE1; PORTE.DIR |= (1 << (channel+3)); num_chan = 2; break; #endif #if defined(TCF0) case PWM_TCF0: config->tc = &TCF0; PORTF.DIR |= (1 << (channel-1)); num_chan = 4; break; #endif #if defined(TCF1) case PWM_TCF1: config->tc = &TCF1; PORTF.DIR |= (1 << (channel+3)); num_chan = 2; break; #endif default: Assert(false); break; } /* Make sure we are not given a channel number larger than this TC can handle */ Assert(channel <= num_chan); config->channel = channel; /* Set the correct cc_mask */ switch (channel) { case PWM_CH_A: config->cc_mask = TC_CCAEN; break; case PWM_CH_B: config->cc_mask = TC_CCBEN; break; case PWM_CH_C: config->cc_mask = TC_CCCEN; break; case PWM_CH_D: config->cc_mask = TC_CCDEN; break; default: Assert(false); break; } /* Enable peripheral clock for this TC */ tc_enable(config->tc); /* Set this TC's waveform generator in single slope mode */ tc_set_wgm(config->tc, TC_WG_SS); /* Default values (disable TC and set minimum period)*/ config->period = 0; config->clk_sel = PWM_CLK_OFF; tc_write_clock_source(config->tc, PWM_CLK_OFF); /* Set the PWM frequency */ pwm_set_frequency(config, freq_hz); }
static BT_ERROR pwm_setconfig(BT_HANDLE hPwm, BT_PWM_CONFIG *pConfig) { pwm_set_frequency(hPwm, pConfig->ulFrequency); return BT_ERR_NONE; }
int pwm_start(const char *pinName, int exportNumber, char *pwmDir, float duty, float freq, int polarity, PwmChannel_t *pwm) { char pwm_control_path[PATHLEN]; char pwm_path[PATHLEN]; //pwm channel control folder char export[2]; int fd; strncpy(pwm->key, pinName, KEYLEN); if (set_pinmux(pinName, "pwm") < 0) { return -1; } //export pwm channel if ((fd = open("/sys/class/pwm/export", O_WRONLY)) < 0) { LogError("pwm open(/sys/class/pwm/export) fail (%s)\n", strerror(errno)); return -1; } snprintf(export, 2, "%i", exportNumber); write(fd,export,1); close(fd); snprintf(pwm_path, sizeof(pwm_path), "/sys/class/pwm/%s", pwmDir); //create the path for period control snprintf(pwm_control_path, sizeof(pwm_control_path), "%s/period_ns", pwm_path); if ((pwm->period_fd = open(pwm_control_path, O_WRONLY)) < 0) { LogError("pwm open(%s) fail (%s)\n", pwm_control_path, strerror(errno)); return -1; } snprintf(pwm_control_path, sizeof(pwm_control_path), "%s/duty_ns", pwm_path); if ((pwm->duty_fd = open(pwm_control_path, O_WRONLY)) < 0) { //error, close already opened period_fd. LogError("pwm open(%s) fail (%s)\n", pwm_control_path, strerror(errno)); close(pwm->period_fd); return -1; } snprintf(pwm_control_path, sizeof(pwm_control_path), "%s/polarity", pwm_path); if ((pwm->polarity_fd = open(pwm_control_path, O_WRONLY)) < 0) { LogError("pwm open(%s) fail (%s)\n", pwm_control_path, strerror(errno)); //error, close already opened period_fd and duty_fd. close(pwm->period_fd); close(pwm->duty_fd); return -1; } snprintf(pwm_control_path, sizeof(pwm_control_path), "%s/run", pwm_path); if ((pwm->run_fd = open(pwm_control_path, O_WRONLY)) < 0) { LogError("pwm open(%s) fail (%s)\n", pwm_control_path, strerror(errno)); //error, close already opened period_fd and duty_fd. close(pwm->polarity_fd); close(pwm->period_fd); close(pwm->duty_fd); return -1; } pwm_set_frequency(pwm, freq); pwm_set_polarity(pwm, polarity); pwm_set_duty_cycle(pwm, duty); pwm_set_run(pwm, 1); return 0; }
/*********************************************************************** * sh7109_setup_ctrs() * * */ static int sh7109_setup_ctrs(void) { pwm_set_frequency(PROFILER_FREQ); return 0; }
void Pwm::set_frequency(float frequency) { (CheckError)pwm_set_frequency(key_.c_str(), frequency); }