void pwmout_period_ms(pwmout_t *obj, int ms) { uint32_t period; uint32_t pulse_width; MBED_ASSERT(obj); if (ms < 0.0) { ms = 0.0; } period = (uint32_t)(ms * 1000 + 0.5); pulse_width = (uint32_t)((uint64_t)period * obj->pulse_width / obj->period); pwm_start(obj, period, pulse_width); }
pwm_result_t pwm_enable(pwm_handle handle) { pwm_internal *pwm; struct pwm_channel *p; pwm_result_t ret = PWM_OK; pwm = (pwm_internal *)handle; if (!pwm || (PWM_MAGIC !=pwm->magic) || !pwm->data) { return PWM_INVALID_PARAM; } p = (struct pwm_channel *)pwm->data; if (pwm_start(p)) ret = PWM_INVALID_PARAM; return ret; }
void ICACHE_FLASH_ATTR peri_rgb_light_param_set( struct PWM_APP_PARAM light_param) { pwm_set_freq(light_param.pwm_freq); pwm_set_duty(light_param.pwm_duty[0], 0); // red colour. pwm_set_duty(light_param.pwm_duty[1], 1); // green colour. pwm_set_duty(light_param.pwm_duty[2], 2); // blue colour. pwm_start(); spi_flash_erase_sector(PRIV_PARAM_START_SEC + RGB_LIGHT_PRIV_SAVE); spi_flash_write((PRIV_PARAM_START_SEC + RGB_LIGHT_PRIV_SAVE) * SPI_FLASH_SEC_SIZE, (uint32 *)&light_param, sizeof(struct PWM_APP_PARAM)); }
void pwmout_period(pwmout_t *obj, float seconds) { uint32_t period; uint32_t pulse_width; MBED_ASSERT(obj); if (seconds < 0.0) { seconds = 0.0; } period = (uint32_t)(seconds * 1000000 + 0.5); pulse_width = (uint32_t)((uint64_t)period * obj->pulse_width / obj->period); pwm_start(obj, period, pulse_width); }
/****************************************************************************** * FunctionName : peri_rgb_light_init * Description : light demo initialize, mainly initialize pwm mode * Parameters : struct LIGHT_PARAM light_param,struct LIGHT_INIT light_init * Returns : none *******************************************************************************/ void ICACHE_FLASH_ATTR peri_rgb_light_init(struct LIGHT_PARAM light_param,struct LIGHT_INIT light_init) { PRINTF("I am the tri-colored light\n"); PRINTF("pwm_freq: %d, pwm_duty_red: %d, pwm_duty_green: %d, pwm_duty_blue: %d\n", light_param.pwm_freq, (light_param.pwm_duty)[0], (light_param.pwm_duty)[1], (light_param.pwm_duty)[2]); spi_flash_write((PRIV_PARAM_START_SEC + PRIV_PARAM_SAVE) * SPI_FLASH_SEC_SIZE, (uint32 *)&light_param, sizeof(struct LIGHT_PARAM)); pwm_init(light_param,light_init); pwm_start(); }
// Set the PWM duty uint32_t platform_pwm_set_duty( unsigned pin, uint32_t duty ) { // NODE_DBG("Function platform_pwm_set_duty() is called.\n"); if ( pin < NUM_PWM) { if(!pwm_exist(pin)) return 0; pwm_set_duty(DUTY(duty), pin); } else { return 0; } pwm_start(); pwms_duty[pin] = NORMAL_DUTY(pwm_get_duty(pin)); return pwms_duty[pin]; }
void initServo() { /* We may want to change this in the future, right now we are only using 2 pins for pwm */ uint32 io_info[][3] = { {PWM_0_OUT_IO_MUX,PWM_0_OUT_IO_FUNC,PWM_0_OUT_IO_NUM}, {PWM_1_OUT_IO_MUX,PWM_1_OUT_IO_FUNC,PWM_1_OUT_IO_NUM}, {PWM_2_OUT_IO_MUX,PWM_2_OUT_IO_FUNC,PWM_2_OUT_IO_NUM}, {PWM_3_OUT_IO_MUX,PWM_3_OUT_IO_FUNC,PWM_3_OUT_IO_NUM}, {PWM_4_OUT_IO_MUX,PWM_4_OUT_IO_FUNC,PWM_4_OUT_IO_NUM}, }; // Initialize duty cycle for channels 0 and 1 to 0 uint32 pwm_duty_init[] = {MAXDUTY/2, MAXDUTY}; pwm_init(PERIOD, pwm_duty_init, 2, io_info ); pwm_start(); }
LOCAL void ICACHE_FLASH_ATTR light_blink(uint32 color) { static bool blink_flg = true; if(blink_flg){ switch(color){ case HINT_WHITE: user_light_set_duty(0,LIGHT_RED); user_light_set_duty(0,LIGHT_GREEN); user_light_set_duty(0,LIGHT_BLUE); user_light_set_duty(5000,LIGHT_COLD_WHITE); user_light_set_duty(5000,LIGHT_WARM_WHITE); break; case HINT_RED: user_light_set_duty(10000,LIGHT_RED); user_light_set_duty(0,LIGHT_GREEN); user_light_set_duty(0,LIGHT_BLUE); user_light_set_duty(2000,LIGHT_COLD_WHITE); user_light_set_duty(2000,LIGHT_WARM_WHITE); break; case HINT_GREEN: user_light_set_duty(0,LIGHT_RED); user_light_set_duty(10000,LIGHT_GREEN); user_light_set_duty(0,LIGHT_BLUE); user_light_set_duty(2000,LIGHT_COLD_WHITE); user_light_set_duty(2000,LIGHT_WARM_WHITE); break; case HINT_BLUE: user_light_set_duty(0,LIGHT_RED); user_light_set_duty(0,LIGHT_GREEN); user_light_set_duty(10000,LIGHT_BLUE); user_light_set_duty(2000,LIGHT_COLD_WHITE); user_light_set_duty(2000,LIGHT_WARM_WHITE); break; default : break; } blink_flg = false; }else{ user_light_set_duty(0,LIGHT_RED); user_light_set_duty(0,LIGHT_GREEN); user_light_set_duty(0,LIGHT_BLUE); user_light_set_duty(0,LIGHT_COLD_WHITE); user_light_set_duty(0,LIGHT_WARM_WHITE); blink_flg = true; } pwm_start(); }
void vibtonz_en(bool en) { t_vib_desc *vib_iter =&vib_desc; printk("%s %s \n", __func__, (en?"enabled":"disabled")); if( vib_iter->initialized == 0) return; if(en) { vib_iter->gpio_en(en); pwm_start(vib_iter->pwm); } else { pwm_stop(vib_iter->pwm); vib_iter->gpio_en(en); } }
// python method PWM.start(self, dutycycle) static PyObject *PWM_start(PWMObject *self, PyObject *args) { float dutycycle; if (!PyArg_ParseTuple(args, "f", &dutycycle)) return NULL; if (dutycycle < 0.0 || dutycycle > 100.0) { PyErr_SetString(PyExc_ValueError, "dutycycle must have a value from 0.0 to 100.0"); return NULL; } self->dutycycle = dutycycle; pwm_set_duty_cycle(self->gpio, self->dutycycle); pwm_start(self->gpio); Py_RETURN_NONE; }
static ssize_t pwm_run_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len) { struct pwm_device *p = dev_get_drvdata(dev); int ret; if (sysfs_streq(buf, "1")) ret = pwm_start(p); else if (sysfs_streq(buf, "0")) ret = pwm_stop(p); else ret = -EINVAL; if (ret < 0) return ret; return len; }
void pwm_set_freq(uint16_t freq) { pwmInfo.freq = freq; /* Stop now to avoid load being used */ if (pwmInfo.running) { pwm_stop(); pwmInfo.running = 1; } timer_set_frequency(FRC1, freq); pwmInfo._maxLoad = timer_get_load(FRC1); if (pwmInfo.running) { pwm_start(); } }
uint32_t platform_pwm_setup( unsigned pin, uint32_t frequency, unsigned duty ) { uint32_t clock; if ( pin < NUM_PWM) { platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT); // disable gpio interrupt first if(!pwm_add(pin)) return 0; // pwm_set_duty(DUTY(duty), pin); pwm_set_duty(0, pin); pwms_duty[pin] = duty; pwm_set_freq((uint16_t)frequency, pin); } else { return 0; } clock = platform_pwm_get_clock( pin ); pwm_start(); return clock; }
bool setServo(ServoNum servoNum, uint8 dutyPercent) { // First verify that duty is within acceptable range // For now, we error out, in the future we could clip to min, max if( (dutyPercent < 0) || (dutyPercent > 100) ){ return false; } // need to convert percentage to actual duty value double decimalPercent = ((double)dutyPercent)/100; double actualDuty = decimalPercent * (double)MAXDUTY; uint32 dutyToSet = round(actualDuty); os_printf("Actual actualDuty = %.2f\n", actualDuty); os_printf("Actual dutyToSet = %d\n", dutyToSet); pwm_set_duty( dutyToSet, (uint8)servoNum ); pwm_start(); // start must be called after every change }
static int ehrpwm_freq_transition_cb(struct pwm_device *p) { struct ehrpwm_pwm *ehrpwm = to_ehrpwm_pwm(p); unsigned long duty_ns; p->tick_hz = clk_get_rate(ehrpwm->clk); duty_ns = p->duty_ns; if (pwm_is_running(p)) { pwm_stop(p); pwm_set_duty_ns(p, 0); pwm_set_period_ns(p, p->period_ns); pwm_set_duty_ns(p, duty_ns); pwm_start(p); } else { pwm_set_duty_ns(p, 0); pwm_set_period_ns(p, p->period_ns); pwm_set_duty_ns(p, duty_ns); } return 0; }
/** * Initialize the dimmer and start the RTOS task * * @param initial_status initial dimmer status structure * @param pwm_period PWM period * @param pwm_channel_num PWM channel number */ void dimmer_init(dimmer_status_t *initial_status, uint32_t pwm_period, uint32_t pwm_channel_num) { /* There can be only one task, so return if already initialized. */ if (s_initialized) return; /* Create status mutex */ s_status_mutex = xSemaphoreCreateMutex(); /* Create notification message queue */ s_dimmer_queue = xQueueCreate(5, sizeof(dimmer_status_t)); s_pwm_period = pwm_period; /* Initialize status struct */ s_status.power_on = initial_status->power_on; s_status.dim_pct = initial_status->dim_pct; /* Init PWM */ s_pwm_channel = pwm_channel_num; if (s_status.power_on == true) { pwm_init_dcs[0] = s_dimmer_pct_to_dc(s_status.dim_pct); } pwm_init(pwm_period, pwm_init_dcs, 1, pwm_init_info); pwm_start(); printf("Dimmer: PWM initialized and started\r\n"); printf("Ch0 DC: %d\r\n", pwm_get_duty(0)); printf("Period: %d\r\n", pwm_get_period()); /* Create dimmer task */ xTaskCreate(dimmer_task, "Dimmer", configMINIMAL_STACK_SIZE, NULL, 3, NULL); /* Prevent further intialization */ s_initialized = 1; }
// python function start(channel, duty_cycle, freq) static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwargs) { char key[8]; char *channel; float frequency = 2000.0; float duty_cycle = 0.0; int polarity = 0; static char *kwlist[] = {"channel", "duty_cycle", "frequency", "polarity", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|ffi", kwlist, &channel, &duty_cycle, &frequency, &polarity)) { return NULL; } if (!get_pwm_key(channel, key)) { PyErr_SetString(PyExc_ValueError, "Invalid PWM key or name."); return NULL; } if (duty_cycle < 0.0 || duty_cycle > 100.0) { PyErr_SetString(PyExc_ValueError, "duty_cycle must have a value from 0.0 to 100.0"); return NULL; } if (frequency <= 0.0) { PyErr_SetString(PyExc_ValueError, "frequency must be greater than 0.0"); return NULL; } if (polarity < 0 || polarity > 1) { PyErr_SetString(PyExc_ValueError, "polarity must be either 0 or 1"); return NULL; } if (!pwm_start(key, duty_cycle, frequency, polarity)) return NULL; Py_RETURN_NONE; }
void user_init(void) { uint8_t pins[1]; uart_set_baud(0, 115200); printf("SDK version:%s\n", sdk_system_get_sdk_version()); printf("pwm_init(1, [14])\n"); pins[0] = 14; pwm_init(1, pins); printf("pwm_set_freq(1000) # 1 kHz\n"); pwm_set_freq(1000); printf("pwm_set_duty(UINT16_MAX/2) # 50%%\n"); pwm_set_duty(UINT16_MAX/2); printf("pwm_start()\n"); pwm_start(); xTaskCreate(task1, (signed char *)"tsk1", 256, NULL, 2, NULL); }
void motorTest() { motorInit(); // Start PWM process. Period 1 ms, Freq 1 kHz pwm_start(basepwm); // Turn motors counterclockwise for 3 s. high(M1forward); high(M2forward); pwm_set(M1enable, 0, 1000); pwm_set(M2enable, 1, 1000); pause(2000); // Stop again pwm_set(M1enable, 0, 0); pwm_set(M2enable, 1, 0); // End the PWM process pwm_stop(); }
/****************************************************************************** * FunctionName : peri_motor_init * Description : motor initialize , mainly initialize pwm mode * Parameters : struct LIGHT_PARAM motor_param,struct LIGHT_INIT motor_init * Returns : none *******************************************************************************/ void ICACHE_FLASH_ATTR peri_motor_double_init(void) { PRINTF("I am the dual-motor\n"); struct PWM_APP_PARAM motor_param; struct PWM_INIT motor_init; motor_param.pwm_freq=25000; motor_param.pwm_duty[0]=0; motor_param.pwm_duty[1]=0; motor_init.io_num=2; motor_init.io_id[0]=PIN_PWMA; motor_init.io_id[1]=PIN_PWMB; // spi_flash_write((PRIV_PARAM_START_SEC + MOTOR_FLASH_PRIV_SAVE) * SPI_FLASH_SEC_SIZE, // (uint32 *)&motor_param, sizeof(struct PWM_APP_PARAM)); PRINTF("finished \n"); pwm_init(motor_param,motor_init); pwm_start(); }
/* 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(); }
static int ecap_frequency_transition_cb(struct pwm_device *p) { struct ecap_pwm *ep = to_ecap_pwm(p); unsigned long duty_ns, rate; rate = clk_get_rate(ep->clk); if (rate == p->tick_hz) return 0; p->tick_hz = rate; duty_ns = p->duty_ns; if (pwm_is_running(p)) { pwm_stop(p); pwm_set_duty_ns(p, 0); pwm_set_period_ns(p, p->period_ns); pwm_set_duty_ns(p, duty_ns); pwm_start(p); } else { pwm_set_duty_ns(p, 0); pwm_set_period_ns(p, p->period_ns); pwm_set_duty_ns(p, duty_ns); } return 0; }
void moveMotors(int left, int right) { motorInit(); // Start PWM process. Period 1 ms, Freq 1 kHz pwm_start(basepwm); if(left >=0){ high(M1forward); } else { high(M1reverse); left=abs(left); } if(right >=0){ high(M2forward); } else { high(M2reverse); right=abs(right); } pwm_set(M1enable, 0, left); pwm_set(M2enable, 1, right); }
uint32_t pwm_init(pwm_t dev, pwm_mode_t mode, uint32_t freq, uint16_t res) { TIM_TypeDef *tim = NULL; GPIO_TypeDef *port = NULL; uint32_t pins[PWM_MAX_CHANNELS]; uint32_t af; int channels; uint32_t pwm_clk; pwm_poweron(dev); switch (dev) { #if PWM_0_EN case PWM_0: tim = PWM_0_DEV; port = PWM_0_PORT; pins[0] = PWM_0_PIN_CH0; pins[1] = PWM_0_PIN_CH1; pins[2] = PWM_0_PIN_CH2; pins[3] = PWM_0_PIN_CH3; af = PWM_0_PIN_AF; channels = PWM_0_CHANNELS; pwm_clk = PWM_0_CLK; PWM_0_PORT_CLKEN(); break; #endif #if PWM_1_EN case PWM_1: tim = PWM_1_DEV; port = PWM_1_PORT; pins[0] = PWM_1_PIN_CH0; pins[1] = PWM_1_PIN_CH1; pins[2] = PWM_1_PIN_CH2; pins[3] = PWM_1_PIN_CH3; af = PWM_1_PIN_AF; channels = PWM_1_CHANNELS; pwm_clk = PWM_1_CLK; PWM_1_PORT_CLKEN(); break; #endif default: pwm_poweroff(dev); return 0; } /* setup pins: alternate function */ for (int i = 0; i < channels; i++) { port->MODER &= ~(3 << (pins[i] * 2)); port->MODER |= (2 << (pins[i] * 2)); if (pins[i] < 8) { port->AFR[0] &= ~(0xf << (pins[i] * 4)); port->AFR[0] |= (af << (pins[i] * 4)); } else { port->AFR[1] &= ~(0xf << ((pins[i] - 8) * 4)); port->AFR[1] |= (af << ((pins[i] - 8) * 4)); } } /* reset timer configuration registers */ tim->CR1 = 0; tim->CR2 = 0; tim->CCMR1 = 0; tim->CCMR2 = 0; /* set prescale and auto-reload registers to matching values for resolution and frequency */ if ((res > 0xffff) || ((res * freq) > pwm_clk)) { return 0; } tim->PSC = (pwm_clk / (res * freq)) - 1; tim->ARR = res - 1; freq = (pwm_clk / (res * (tim->PSC + 1))); /* set PWM mode */ switch (mode) { case PWM_LEFT: tim->CCMR1 |= (TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC2M_1 | TIM_CCMR1_OC2M_2); tim->CCMR2 |= (TIM_CCMR2_OC3M_1 | TIM_CCMR2_OC3M_2 | TIM_CCMR2_OC4M_1 | TIM_CCMR2_OC4M_2); break; case PWM_RIGHT: tim->CCMR1 |= (TIM_CCMR1_OC1M_0 | TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC2M_0 | TIM_CCMR1_OC2M_1 | TIM_CCMR1_OC2M_2); tim->CCMR2 |= (TIM_CCMR2_OC3M_0 | TIM_CCMR2_OC3M_1 | TIM_CCMR2_OC3M_2 | TIM_CCMR2_OC4M_0 | TIM_CCMR2_OC4M_1 | TIM_CCMR2_OC4M_2); break; case PWM_CENTER: tim->CR1 |= (TIM_CR1_CMS_0 | TIM_CR1_CMS_1); break; } /* enable output on PWM pins */ tim->CCER |= (TIM_CCER_CC1E | TIM_CCER_CC2E | TIM_CCER_CC3E | TIM_CCER_CC4E); /* enable PWM generation */ pwm_start(dev); return freq; }
static int pwm_ioctl(FAR struct file *filep, int cmd, unsigned long arg) { FAR struct inode *inode = filep->f_inode; FAR struct pwm_upperhalf_s *upper = inode->i_private; FAR struct pwm_lowerhalf_s *lower = upper->dev; int ret; pwmvdbg("cmd: %d arg: %ld\n", cmd, arg); /* Get exclusive access to the device structures */ ret = sem_wait(&upper->exclsem); if (ret < 0) { return ret; } /* Handle built-in ioctl commands */ switch (cmd) { /* PWMIOC_SETCHARACTERISTICS - Set the characteristics of the next pulsed * output. This command will neither start nor stop the pulsed output. * It will either setup the configuration that will be used when the * output is started; or it will change the characteristics of the pulsed * output on the fly if the timer is already started. * * ioctl argument: A read-only reference to struct pwm_info_s that provides * the characteristics of the pulsed output. */ case PWMIOC_SETCHARACTERISTICS: { FAR const struct pwm_info_s *info = (FAR const struct pwm_info_s *)((uintptr_t)arg); DEBUGASSERT(info != NULL && lower->ops->start != NULL); pwm_dump("PWMIOC_SETCHARACTERISTICS", info, upper->started); /* Save the pulse train characteristics */ memcpy(&upper->info, info, sizeof(struct pwm_info_s)); /* If PWM is already running, then re-start it with the new characteristics */ if (upper->started) { #ifdef CONFIG_PWM_PULSECOUNT ret = lower->ops->start(lower, &upper->info, upper); #else ret = lower->ops->start(lower, &upper->info); #endif } } break; /* PWMIOC_GETCHARACTERISTICS - Get the currently selected characteristics of * the pulsed output (independent of whether the output is start or stopped). * * ioctl argument: A reference to struct pwm_info_s to receive the * characteristics of the pulsed output. */ case PWMIOC_GETCHARACTERISTICS: { FAR struct pwm_info_s *info = (FAR struct pwm_info_s *)((uintptr_t)arg); DEBUGASSERT(info != NULL); memcpy(info, &upper->info, sizeof(struct pwm_info_s)); pwm_dump("PWMIOC_GETCHARACTERISTICS", info, upper->started); } break; /* PWMIOC_START - Start the pulsed output. The PWMIOC_SETCHARACTERISTICS * command must have previously been sent. * * ioctl argument: None */ case PWMIOC_START: { pwm_dump("PWMIOC_START", &upper->info, upper->started); DEBUGASSERT(lower->ops->start != NULL); /* Start the pulse train */ ret = pwm_start(upper, filep->f_oflags); } break; /* PWMIOC_STOP - Stop the pulsed output. * * ioctl argument: None */ case PWMIOC_STOP: { pwmvdbg("PWMIOC_STOP: started: %d\n", upper->started); DEBUGASSERT(lower->ops->stop != NULL); if (upper->started) { ret = lower->ops->stop(lower); upper->started = false; #ifdef CONFIG_PWM_PULSECOUNT if (upper->waiting) { upper->waiting = false; } #endif } } break; /* Any unrecognized IOCTL commands might be platform-specific ioctl commands */ default: { pwmvdbg("Forwarding unrecognized cmd: %d arg: %ld\n", cmd, arg); DEBUGASSERT(lower->ops->ioctl != NULL); ret = lower->ops->ioctl(lower, cmd, arg); } break; } sem_post(&upper->exclsem); return ret; }
//Main routine void ICACHE_FLASH_ATTR user_init(void) { stdoutInit(); os_delay_us(100000); CFG_Load(); ioInit(); WIFI_Connect(wifiConnectCb); httpdInit(builtInUrls, sysCfg.httpd_port); if(sysCfg.ntp_enable==1) { sntp_init(sysCfg.ntp_tz); //timezone } if(sysCfg.mqtt_enable==1) { MQTT_InitConnection(&mqttClient, (uint8_t *)sysCfg.mqtt_host, sysCfg.mqtt_port, sysCfg.mqtt_use_ssl ); MQTT_InitClient(&mqttClient, (uint8_t *)sysCfg.mqtt_devid, (uint8_t *)sysCfg.mqtt_user, (uint8_t *)sysCfg.mqtt_pass, sysCfg.mqtt_keepalive,1); MQTT_OnConnected(&mqttClient, mqttConnectedCb); MQTT_OnDisconnected(&mqttClient, mqttDisconnectedCb); MQTT_OnPublished(&mqttClient, mqttPublishedCb); MQTT_OnData(&mqttClient, mqttDataCb); } if(sysCfg.sensor_dht22_enable) DHTInit(SENSOR_DHT22, 30000); if(sysCfg.sensor_ds18b20_enable) ds_init(30000); broadcastd_init(); thermostat_init(30000); /* //Netbios to set the name struct softap_config wiconfig; os_memset(netbios_name, ' ', sizeof(netbios_name)-1); if(wifi_softap_get_config(&wiconfig)) { int i; for(i = 0; i < sizeof(netbios_name)-1; i++) { if(wiconfig.ssid[i] < ' ') break; netbios_name[i] = wiconfig.ssid[i]; }; } else os_sprintf(netbios_name, "ESP8266"); netbios_name[sizeof(netbios_name)-1]='\0'; netbios_init(); */ os_printf("\nRelay Board Ready\n"); os_printf("Free heap size:%d\n",system_get_free_heap_size()); #ifdef CGIPWM_H //Mind the PWM pin!! defined in pwm.h duty=0; pwm_init( 50, &duty); pwm_set_duty(duty, 0); pwm_start(); #endif //OLEDInit(); }
irom app_action_t application_function_gpio_set(application_parameters_t ap) { unsigned int gpio_index; gpio_t *gpio; const gpio_config_entry_t *cfg; gpio_index = atoi((*ap.args)[1]); if(!(gpio = find_gpio(gpio_index))) { snprintf(ap.dst, ap.size, "gpio-set: invalid gpio %u\n", gpio_index); return(app_action_error); } cfg = get_config(gpio); switch(cfg->mode) { case(gpio_disabled): { snprintf(ap.dst, ap.size, "gpio-set: gpio %s is disabled\n", gpio->name); return(app_action_error); } case(gpio_input): { snprintf(ap.dst, ap.size, "gpio-set: gpio %s is input\n", gpio->name); return(app_action_error); } case(gpio_counter): { if(ap.nargs < 3) gpio->counter.count = 0; else gpio->counter.count = atoi((*ap.args)[2]); break; } case(gpio_output): { if(ap.nargs < 3) { snprintf(ap.dst, ap.size, "gpio-set: missing arguments\n"); return(app_action_error); } set_output(gpio, !!atoi((*ap.args)[2])); break; } case(gpio_timer): { if(ap.nargs == 3) trigger_timer(gpio, !!atoi((*ap.args)[2])); else trigger_timer(gpio, !gpio->timer.delay); break; } case(gpio_pwm): { unsigned int min_duty; unsigned int max_duty; unsigned int delay; min_duty = 0; max_duty = 0; delay = 0; if(ap.nargs > 2) min_duty = atoi((*ap.args)[2]); if(ap.nargs > 3) max_duty = atoi((*ap.args)[3]); if(ap.nargs > 4) delay = atoi((*ap.args)[4]); if(min_duty > 65535) { snprintf(ap.dst, ap.size, "gpio-set(pwm): min_duty too large: %u > 65535\n", min_duty); return(app_action_error); } if(max_duty > 65535) { snprintf(ap.dst, ap.size, "gpio-set(pwm): max_duty too large: %u > 65535\n", max_duty); return(app_action_error); } if(delay > 100) { snprintf(ap.dst, ap.size, "gpio-set: gpio %s, delay %u%% > 100%%\n", gpio->name, delay); return(app_action_error); } gpio->pwm.min_duty = min_duty; gpio->pwm.max_duty = max_duty; gpio->pwm.delay_top = delay; gpio->pwm.direction = gpio_up; pwm_set_duty(min_duty, gpio->pwm.channel); pwm_start(); break; } case(gpio_i2c): { snprintf(ap.dst, ap.size, "gpio-set: gpio %s is reserved for i2c\n", gpio->name); return(app_action_error); } default: { snprintf(ap.dst, ap.size, "gpio-set: cannot set gpio %u\n", cfg->mode); return(app_action_error); } } dump(&config->gpios, gpio, ap.size, ap.dst); return(app_action_normal); }
iram void gpios_periodic(void) { gpio_t *gpio; const gpio_config_entry_t *cfg; unsigned int current; unsigned int duty; bool_t pwm_changed = false; for(current = 0; current < gpio_size; current++) { gpio = &gpios[current]; cfg = get_config(gpio); if((cfg->mode == gpio_counter) && (gpio->counter.debounce != 0)) { if(gpio->counter.debounce >= 10) gpio->counter.debounce -= 10; // 10 ms per tick else gpio->counter.debounce = 0; if(gpio->counter.debounce == 0) arm_counter(gpio); } if((cfg->mode == gpio_timer) && (gpio->timer.delay > 0)) { if(gpio->timer.delay >= 10) gpio->timer.delay -= 10; // 10 ms per tick else gpio->timer.delay = 0; if(gpio->timer.delay == 0) { set_output(gpio, !get_input(gpio)); if(cfg->timer.repeat) gpio->timer.delay = cfg->timer.delay; } } if((cfg->mode == gpio_pwm) && (gpio->pwm.delay_top > 0)) { if(++gpio->pwm.delay_current > gpio->pwm.delay_top) { gpio->pwm.delay_current = 0; duty = pwm_get_duty(gpio->pwm.channel); if(gpio->pwm.direction == gpio_up) { if(duty < gpio->pwm.min_duty) duty = gpio->pwm.min_duty; if(duty < 16) duty = 16; duty *= 115; duty /= 100; if(duty >= gpio->pwm.max_duty) { duty = gpio->pwm.max_duty; gpio->pwm.direction = gpio_down; } } else { if(duty > gpio->pwm.max_duty) duty = gpio->pwm.max_duty; duty *= 100; duty /= 115; if(duty <= gpio->pwm.min_duty) { duty = gpio->pwm.min_duty; gpio->pwm.direction = gpio_up; } if(duty < 16) { duty = 16; gpio->pwm.direction = gpio_up; } } pwm_changed = true; pwm_set_duty(duty, gpio->pwm.channel); } } } if(pwm_changed) pwm_start(); }
/* * Main - Call the ioctl functions */ int main() { int choice = ' '; char line[80]; char strVal[80]; unsigned int val; unsigned int id; pwm_file_desc = open(PWM_DEVICE_NAME, 0); if (pwm_file_desc < 0) { printf("Can't open device file: %s\n", PWM_DEVICE_NAME); exit(-1); } pwm0 = pwm_data(0, 14, 10000, 500); pwm1 = pwm_data(1, 15, 10000, 500); while (choice != '9') { printf ("\nMenu\n" "===================\n" " [1] set clock\n" " [2] PWM init\n" " [3] PWM set cycle\n" " [4] PWM set duty\n" " [5] PWM start\n" " [6] PWM stop\n" "" " [9] quit\n" "Enter choice: " ); fgets (line, sizeof (line), stdin); choice = line[0]; printf ("\n"); switch (choice) { case '1': printf("Enter clock divider [example 96]: "); fgets (strVal, sizeof (strVal), stdin); val = atoi(strVal); clock_set(val); break; case '2': printf("Enter PWM id [0, 1]: "); fgets (strVal, sizeof (strVal), stdin); id = atoi(strVal); printf("Enter GPIO [14, 15, etc]: "); fgets (strVal, sizeof (strVal), stdin); val = atoi(strVal); id ? pwm_init(pwm1, val) : pwm_init(pwm0, val); break; case '3': printf("Enter PWM id [0, 1]: "); fgets (strVal, sizeof (strVal), stdin); id = atoi(strVal); printf("Enter PWM cycle [10000]: "); fgets (strVal, sizeof (strVal), stdin); val = atoi(strVal); if (id == 0) { pwm0->cycle = val; pwm_set(pwm0); } else { pwm1->cycle = val; pwm_set(pwm1); } break; case '4': printf("Enter PWM id [0, 1]: "); fgets (strVal, sizeof (strVal), stdin); id = atoi(strVal); printf("Enter PWM duty [0..1000]: "); fgets (strVal, sizeof (strVal), stdin); val = atoi(strVal); if (id == 0) { pwm0->duty = val; pwm_set(pwm0); } else { pwm1->duty = val; pwm_set(pwm1); } break; case '5': printf("Enter PWM id [0, 1]: "); fgets (strVal, sizeof (strVal), stdin); id = atoi(strVal); id ? pwm_start(pwm1) : pwm_start(pwm0); break; case '6': printf("Enter PWM id [0, 1]: "); fgets (strVal, sizeof (strVal), stdin); id = atoi(strVal); id ? pwm_stop(pwm1) : pwm_stop(pwm0); break; } } close(pwm_file_desc); free(pwm0); free(pwm1); return 0; }
static void ICACHE_FLASH_ATTR io_pwm_init() { uint32 duty[]={0,0,0}; pwm_init(470, duty, 3, pwmIoInfo); pwm_start(); }