void pwm_init(void){
	*AT91C_PMC_PCER |= (1<<AT91C_ID_PWMC);
	*AT91C_PIOA_PDR |= PWM_MASK;
	*AT91C_PIOA_BSR |= PWM_MASK;
	#if ORIGINAL_FREQ
	pwm_configure_clock(0x00000014);
	#elif DOUBLED_FREQ
	pwm_configure_clock(0x0000060F);
	#endif
	pwm_configure_channel(0, AT91C_PWMC_CPRE_MCKA, 0, 1);
	pwm_configure_channel(1, AT91C_PWMC_CPRE_MCKA, 0, 1);
	pwm_configure_channel(2, AT91C_PWMC_CPRE_MCKA, 0, 1);	
	pwm_configure_channel(3, AT91C_PWMC_CPRE_MCKA, 0, 1);
	pwm_set_period(0, 4800);
	pwm_set_period(1, 4800);
	pwm_set_period(2, 4800);
	pwm_set_period(3, 4800);
	pwm_set_duty_cycle(0, 0);
	pwm_set_duty_cycle(1, 0);
	pwm_set_duty_cycle(2, 0);
	pwm_set_duty_cycle(3, 0);	 //here must set 0 first, or the polarity will be wrong
	pwm_enable_channel(0);
	pwm_enable_channel(1);
	pwm_enable_channel(2);
	pwm_enable_channel(3);
	pwm_set_duty_cycle(0, 2400);
	pwm_set_duty_cycle(1, 2400);
	pwm_set_duty_cycle(2, 2400);
	pwm_set_duty_cycle(3, 2400);
}
Beispiel #2
0
int main()
{
	DDRB |= _BV(PB3);
	
	pwm_fast_init(2);
	spi_master_init(USI_PORTA);
	
	// Enable pullups on button inputs
	PORTA |= 1 << B_DOWN;
	PORTA |= 1 << B_UP; 

	uint8_t ton = 0x40;

	uint8_t timer = 0;
	
	pwm_set_period(0xFF);
	pwm_set_on_b(ton);

	while(1) {
		if ((PINA & (1 << B_UP)) == 0) {
			pwm_set_on_b(ton++);
		}
		if ((PINA & (1 << B_DOWN)) == 0) {
			pwm_set_on_b(ton--);
		}
		
		if (timer++ == 1000 / LOOP_DELAY) {
			spi_master_write(ton);
			timer = 0;
		}
		_delay_ms(50);
	}
}
Beispiel #3
0
void lcd_blt_set_pwm(int no, int level, int freq)
{
	int clock = auto_pll_divisor(DEV_PWM,GET_FREQ, 0, 0);
	int period, duty, scalar;

	clock = clock / freq;
	scalar = 0;
	period = 2000;

	while(period > 1023) {
		scalar++;
		period = clock / scalar;
	}

	duty = (period*level)/100;
	duty = (duty)? (duty-1):0;
	scalar = scalar-1;
	period = period -1;

	pwm_set_period(no,period);
	pwm_set_duty(no,duty);
	pwm_set_scalar(no,scalar);
	if (g_pwm_setting.config)
		pwm_set_control(no,(level)? 0x36:0x8);
	else
	pwm_set_control(no,(level)? 0x34:0x8);
	pwm_set_gpio(no,level);
}
Beispiel #4
0
static bool test_pwm_get_set_period_before_init(void)
{
    uint32_t period = 100;

    return pwm_get_period(MIKROBUS_1, &period) == -1
        && pwm_set_period(MIKROBUS_1, period) == -1;
}
Beispiel #5
0
static int display_set_pwm(char *val, int on)
{

	if (val == NULL) {
		printf("error : can not get pwm parameter\n");
		return -1;
	}

	lcd_blt_enable(g_pwm_setting.pwm_no, 0);

	parse_pwm_params(NULL, val);

	if ((g_display_vaild&PWMDEFMASK) == PWMDEFTP) {
		lcd_blt_set_pwm(g_pwm_setting.pwm_no, g_pwm_setting.duty, g_pwm_setting.period);
	} else {
		// fan : may need to check PWM power ..
		pwm_set_period(g_pwm_setting.pwm_no, g_pwm_setting.period-1);
		pwm_set_duty(g_pwm_setting.pwm_no, g_pwm_setting.duty-1);
		pwm_set_control(g_pwm_setting.pwm_no, (g_pwm_setting.duty-1)? 0x35:0x8);
		pwm_set_gpio(g_pwm_setting.pwm_no, g_pwm_setting.duty-1);
		pwm_set_scalar(g_pwm_setting.pwm_no, g_pwm_setting.scalar-1);
	}

	lcd_blt_enable(g_pwm_setting.pwm_no, on);

	return 0;
}
/******************************************************************************
 * FunctionName : user_light_set_duty
 * Description  : set pwm frequency
 * Parameters   : uint16 freq : 100hz typically
 * Returns      : NONE
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_light_set_period(uint32 period)
{
    if (period != light_param.pwm_period) {
        pwm_set_period(period);

        light_param.pwm_period = pwm_get_period();
    }
}
/******************************************************************************
 * FunctionName : user_light_set_duty
 * Description  : set pwm frequency
 * Parameters   : uint16 freq : 100hz typically
 * Returns      : NONE
*******************************************************************************/
void  
user_light_set_period(uint32 period)
{
    if (period != light_param.pwm_period) {
        pwm_set_period(period);

        light_param.pwm_period = pwm_get_period();
    }
}
Beispiel #8
0
void lcd_blt_set_pwm(int no,unsigned int scalar,unsigned int period,unsigned int duty)
{
	lcd_pwm_enable = 1;
	lcd_blt_id = no;
#ifdef CONFIG_PWM_WMT
	pwm_set_scalar(lcd_blt_id,scalar);
	pwm_set_period(lcd_blt_id,period);
	pwm_set_duty(lcd_blt_id,duty);
#endif					
}
Beispiel #9
0
void servo_init()
{
    int i;
    for(i=0;i<sizeof(servo_pin)/sizeof(servo_pin[0]);i++)
    {
        pwm_stop(servo_pin[i][0], servo_pin[i][1]);
        pwm_set_period(servo_pin[i][0], servo_pin[i][1], SERVO_STD_PERIOD);
        pwm_set_polarity(servo_pin[i][0], servo_pin[i][1], 1);
        pwm_set_duty_cycle(servo_pin[i][0], servo_pin[i][1], SERVO_ANGLE_TO_DUTY(0));  /* keep in the middle */
    }
    start_servo();
}
Beispiel #10
0
int main(void)
{
	DDRB |= _BV(PB3);
	DDRB |= _BV(PB0); // raw-pwm output

	spi_master_init(USI_PORTA);
	
	// Enable pullups on button inputs
	PORTA |= 1 << B_DOWN;
	PORTA |= 1 << B_UP; 

	uint16_t timer = 0;
	
	pwm_fast_init(2);
	pwm_set_period(0xFF);
	pwm_set_on_b(ton);

	DDRA |= _BV(PA0); // Force Data in (MISO) to 0
	
	// prescaler: 6 = div/64; fADC = 125kHz; 1 clockcycle = 8uS; 1 conversion=104uS
	adc_init(VREF_I2_56, 0x12, 6);

	// Configure timer0
	TCCR0A = 0x00;
	TCCR0B = 0x01; // Enable counter without prescaling
	OCR0A = 0xFF; // compare at 0xFF
	TIMSK |= (1 << OCIE0A); // enable interrupt for compare0A
	
	sei();
	
	while(1) {
		if (timer % 100 == 0) {
			if ((PINA & (1 << B_UP)) == 0 && pwm_t < BPWM_MAX) {
				pwm_t++;
			}
			if ((PINA & (1 << B_DOWN)) == 0 && pwm_t > BPWM_MIN) {
				pwm_t--;
			}
		}
		
		if (timer++ == 50000 / LOOP_DELAY) {
			spi_master_write(pwm_t >> 8);
			spi_master_write(pwm_t);
			spi_master_write(ton);
			spi_master_write((adc & 0xFF00) >> 8);
			spi_master_write(adc & 0x00FF);

			timer = 0;
		}
		_delay_us(LOOP_DELAY);
	}
static int pwm_backlight_probe(struct platform_device *pdev)
{
	struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
	struct platform_pwm_backlight_data defdata;
	struct backlight_properties props;
	struct backlight_device *bl;
	struct pwm_bl_data *pb;
	int ret;

	if (!data) {
		ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
		if (ret < 0) {
			dev_err(&pdev->dev, "failed to find platform data\n");
			return ret;
		}

		data = &defdata;
	}

	if (data->init) {
		ret = data->init(&pdev->dev);
		if (ret < 0)
			return ret;
	}

	pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
	if (!pb) {
		ret = -ENOMEM;
		goto err_alloc;
	}

	if (data->levels) {
		unsigned int i;

		for (i = 0; i <= data->max_brightness; i++)
			if (data->levels[i] > pb->scale)
				pb->scale = data->levels[i];

		pb->levels = data->levels;
	} else
		pb->scale = data->max_brightness;

	pb->enable_gpio = data->enable_gpio;
	pb->enable_gpio_flags = data->enable_gpio_flags;
	pb->notify = data->notify;
	pb->notify_after = data->notify_after;
	pb->check_fb = data->check_fb;
	pb->exit = data->exit;
	pb->dev = &pdev->dev;
	pb->enabled = false;

	if (gpio_is_valid(pb->enable_gpio)) {
		unsigned long flags;

		if (pb->enable_gpio_flags & PWM_BACKLIGHT_GPIO_ACTIVE_LOW)
			flags = GPIOF_OUT_INIT_HIGH;
		else
			flags = GPIOF_OUT_INIT_LOW;

		ret = gpio_request_one(pb->enable_gpio, flags, "enable");
		if (ret < 0) {
			dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
				pb->enable_gpio, ret);
			goto err_alloc;
		}
	}

	pb->power_supply = devm_regulator_get(&pdev->dev, "power");
	if (IS_ERR(pb->power_supply)) {
		ret = PTR_ERR(pb->power_supply);
		goto err_gpio;
	}

	pb->pwm = devm_pwm_get(&pdev->dev, NULL);
	if (IS_ERR(pb->pwm)) {
		dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");

		pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
		if (IS_ERR(pb->pwm)) {
			dev_err(&pdev->dev, "unable to request legacy PWM\n");
			ret = PTR_ERR(pb->pwm);
			goto err_gpio;
		}
	}

	dev_dbg(&pdev->dev, "got pwm for backlight\n");

	/*
	 * The DT case will set the pwm_period_ns field to 0 and store the
	 * period, parsed from the DT, in the PWM device. For the non-DT case,
	 * set the period from platform data.
	 */
	if (data->pwm_period_ns > 0)
		pwm_set_period(pb->pwm, data->pwm_period_ns);

	pb->period = pwm_get_period(pb->pwm);
	pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);

	memset(&props, 0, sizeof(struct backlight_properties));
	props.type = BACKLIGHT_RAW;
	props.max_brightness = data->max_brightness;
	bl = backlight_device_register("lcd-bl", &pdev->dev, pb,
				       &pwm_backlight_ops, &props);
	if (IS_ERR(bl)) {
		dev_err(&pdev->dev, "failed to register backlight\n");
		ret = PTR_ERR(bl);
		goto err_gpio;
	}

	if (data->dft_brightness > data->max_brightness) {
		dev_warn(&pdev->dev,
			 "invalid default brightness level: %u, using %u\n",
			 data->dft_brightness, data->max_brightness);
		data->dft_brightness = data->max_brightness;
	}

	bl->props.brightness = data->dft_brightness;
	backlight_update_status(bl);

	platform_set_drvdata(pdev, bl);
	return 0;

err_gpio:
	if (gpio_is_valid(pb->enable_gpio))
		gpio_free(pb->enable_gpio);
err_alloc:
	if (data->exit)
		data->exit(&pdev->dev);
	return ret;
}
Beispiel #12
0
static bool test_pwm_get_set_period_invalid_index(void)
{
    uint32_t period = 100;
    return pwm_set_period(3, period) == -1
        && pwm_get_period(3, &period) == -1;
}
static int pwm_backlight_probe(struct platform_device *pdev)
{
	struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
	struct platform_pwm_backlight_data defdata;
	struct backlight_properties props;
	struct backlight_device *bl;
	struct pwm_bl_data *pb;
	unsigned int max;
	int ret;

	if (!data) {
		ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
		if (ret < 0) {
			dev_err(&pdev->dev, "failed to find platform data\n");
			return ret;
		}

		data = &defdata;
	}

	if (data->init) {
		ret = data->init(&pdev->dev);
		if (ret < 0)
			return ret;
	}

	pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
	if (!pb) {
		dev_err(&pdev->dev, "no memory for state\n");
		ret = -ENOMEM;
		goto err_alloc;
	}

	if (data->levels) {
		max = data->levels[data->max_brightness];
		pb->levels = data->levels;
	} else
		max = data->max_brightness;

	pb->notify = data->notify;
	pb->notify_after = data->notify_after;
	pb->check_fb = data->check_fb;
	pb->exit = data->exit;
	pb->dev = &pdev->dev;

	pb->pwm = devm_pwm_get(&pdev->dev, NULL);
	if (IS_ERR(pb->pwm)) {
		dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");

		pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
		if (IS_ERR(pb->pwm)) {
			dev_err(&pdev->dev, "unable to request legacy PWM\n");
			ret = PTR_ERR(pb->pwm);
			goto err_alloc;
		}
	}

	dev_dbg(&pdev->dev, "got pwm for backlight\n");

	/*
	 * The DT case will set the pwm_period_ns field to 0 and store the
	 * period, parsed from the DT, in the PWM device. For the non-DT case,
	 * set the period from platform data.
	 */
	if (data->pwm_period_ns > 0)
		pwm_set_period(pb->pwm, data->pwm_period_ns);

	pb->period = pwm_get_period(pb->pwm);
	pb->lth_brightness = data->lth_brightness * (pb->period / max);

	memset(&props, 0, sizeof(struct backlight_properties));
	props.type = BACKLIGHT_RAW;
	props.max_brightness = data->max_brightness;
	bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
				       &pwm_backlight_ops, &props);
	if (IS_ERR(bl)) {
		dev_err(&pdev->dev, "failed to register backlight\n");
		ret = PTR_ERR(bl);
		goto err_alloc;
	}

	if (data->dft_brightness > data->max_brightness) {
		dev_warn(&pdev->dev,
			 "invalid default brightness level: %u, using %u\n",
			 data->dft_brightness, data->max_brightness);
		data->dft_brightness = data->max_brightness;
	}

	bl->props.brightness = data->dft_brightness;
	backlight_update_status(bl);

	platform_set_drvdata(pdev, bl);
	return 0;

err_alloc:
	if (data->exit)
		data->exit(&pdev->dev);
	return ret;
}
static int __init pwm_regulator_probe(struct platform_device *pdev)
{
	struct pwm_platform_data *pdata = pdev->dev.platform_data;
	struct pwm_regulator_board *pwm_pdev;
	struct of_regulator_match *pwm_reg_matches = NULL;
	struct regulator_init_data *reg_data;
	struct regulator_config config = { };
	const char *rail_name = NULL;
	struct regulator_dev *pwm_rdev;
	int ret, i = 0;
	struct regulator *dc;

	pwm_pdev = devm_kzalloc(&pdev->dev, sizeof(*pwm_pdev),
					GFP_KERNEL);
	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata),
					GFP_KERNEL);

	if (pdev->dev.of_node)
		pwm_pdev = pwm_regulator_parse_dt(pdev, &pwm_reg_matches);

	if (!pwm_pdev) {
		dev_err(&pdev->dev, "Platform data not found\n");
		return -EINVAL;
	}

	if (!pwm_pdev->pwm_init_vol)
		pdata->pwm_voltage = 1100000;	/* default 1.1v*/
	else
		pdata->pwm_voltage = pwm_pdev->pwm_init_vol;

	if (!pwm_pdev->pwm_max_vol)
		pdata->max_uV = 1400000;
	else
		pdata->max_uV = pwm_pdev->pwm_max_vol;

	if (!pwm_pdev->pwm_min_vol)
		pdata->min_uV = 1000000;
	else
		pdata->min_uV = pwm_pdev->pwm_min_vol;

	if (pwm_pdev->pwm_suspend_vol < pwm_pdev->pwm_min_vol)
		pdata->suspend_voltage = pwm_pdev->pwm_min_vol;
	else if (pwm_pdev->pwm_suspend_vol > pwm_pdev->pwm_max_vol)
		pdata->suspend_voltage = pwm_pdev->pwm_max_vol;
	else
		pdata->suspend_voltage = pwm_pdev->pwm_suspend_vol;

	pdata->pwm_voltage_map = pwm_pdev->pwm_voltage_map;
	pdata->pwm_id = pwm_pdev->pwm_id;
	pdata->coefficient = pwm_pdev->pwm_coefficient;
	pdata->pwm_vol_map_count = pwm_pdev->pwm_vol_map_count;

	pdata->pwm = devm_pwm_get(&pdev->dev, NULL);
	if (IS_ERR(pdata->pwm)) {
		dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");

		pdata->pwm = pwm_request(pdata->pwm_id, "pwm-regulator");
		if (IS_ERR(pdata->pwm)) {
			dev_err(&pdev->dev, "unable to request legacy PWM\n");
			ret = PTR_ERR(pdata->pwm);
			goto err;
		}
	}
	if (pdata->pwm_period_ns > 0)
		pwm_set_period(pdata->pwm, pdata->pwm_period_ns);

	pdata->period = pwm_get_period(pdata->pwm);

	mutex_init(&pdata->mutex_pwm);

	if (pwm_pdev) {
		pdata->num_regulators = pwm_pdev->num_regulators;
		pdata->rdev = kcalloc(pdata->num_regulators, sizeof(struct regulator_dev *), GFP_KERNEL);
		if (!pdata->rdev) {
			return -ENOMEM;
		}
		/* Instantiate the regulators */
		for (i = 0; i < pdata->num_regulators; i++) {
		reg_data = pwm_pdev->pwm_init_data[i];
		if (!reg_data)
			continue;
		config.dev = &pdev->dev;
		config.driver_data = pdata;
		if (&pdev->dev.of_node)
			config.of_node = pwm_pdev->of_node[i];
		if (reg_data && reg_data->constraints.name)
				rail_name = reg_data->constraints.name;
			else
				rail_name = regulators[i].name;
			reg_data->supply_regulator = rail_name;

		config.init_data = reg_data;

		pwm_rdev = regulator_register(&regulators[i], &config);
		if (IS_ERR(pwm_rdev)) {
			printk("failed to register %d regulator\n", i);
		goto err;
		}
		pdata->rdev[i] = pwm_rdev;

		/*********set pwm vol by defult***********/
		dc = regulator_get(NULL, rail_name);
		regulator_set_voltage(dc, pdata->pwm_voltage, pdata->pwm_voltage);
		regulator_put(dc);
		/**************************************/
		}
	}
	return 0;
err:
	printk("%s:error\n", __func__);
	return ret;

}