int
main(int argc, char **argv)
{
    int frequency;
	if (argc < 2) {
		printf("Usage: %s <frequency>\n", argv[0]);
		printf("Sets up a PWM signal on gpio 145 and 146 at the given frequency\n");
        printf("and sweeps the duty-cycle up and down once\n");
		exit(-1);
	}

    frequency = atoi(argv[1]);
    int mem_fd = pwm_open_devmem();
    if (mem_fd == -1) {
        g_error("Unable to open /dev/mem, are you root?: %s", g_strerror(errno));
    }

    // Set instances 10 and 11 to use the 13 Mhz clock
    pwm_config_clock(mem_fd, TRUE, TRUE);
    guint8 *gpt10 = pwm_mmap_instance(mem_fd, 10);
    guint8 *gpt11 = pwm_mmap_instance(mem_fd, 11);

    // Get the resolution for 20 kHz PWM
    guint32 resolution = pwm_calc_resolution(frequency, PWM_FREQUENCY_13MHZ);
    printf("Resolution = %d\n", resolution);

    // Ramp up and down a bit
    int i;
    for (i = 0; i <= 100; i++) {
        g_print("%3d\n", i);
        pwm_config_timer(gpt10, resolution, i / 100.0);
        pwm_config_timer(gpt11, resolution, i / 100.0);
        usleep(100000);
    }
    sleep(1);
    for (i = 100; i >= 0; i--) {
        g_print("%3d\n", i);
        pwm_config_timer(gpt10, resolution, i / 100.0);
        pwm_config_timer(gpt11, resolution, i / 100.0);
        usleep(100000);
    }

    pwm_munmap_instance(gpt10);
    pwm_close_devmem(mem_fd);
}
Exemplo n.º 2
0
int start_pwm(int frequency, int duty_cycle){
	int dev_fd;
	unsigned int *PinConfig, *testValue;
	unsigned int CurValue;
	unsigned int *gpt10;
	unsigned long resolution;
	int i;

	dev_fd = open("/dev/mem", O_RDWR | O_SYNC);
	if (dev_fd == -1) {
		printf("Can not opem /dev/mem.");
		return -1;
	}
	// set the clock source to 13MHz
    // comparing to GPIO/gpio.c, (unsigned int *) is added here
    // so it then goes into int space and the offset (@char) must be devided by 4 (@int)
	PinConfig = (unsigned int *) mmap(NULL, 0x300, PROT_READ | PROT_WRITE, MAP_SHARED, dev_fd, CM_CLOCK_BASE);
	testValue = (volatile unsigned int*)(PinConfig+CM_CLKSEL_CORE/4);
	CurValue = *(volatile unsigned int*)(PinConfig+CM_CLKSEL_CORE/4);
	printf("The curvalue is CurValue %x\n", testValue);
	CurValue &= 0xffffffbf;
	CurValue |= 0x40;	// set CLKSEL_GPT10 1,
	*(volatile unsigned int*)(PinConfig+CM_CLKSEL_CORE/4) = CurValue;
	printf("13MHz clock source enabled........\n");

	// enable the clock: FCLK and ICLK
	PinConfig = (unsigned int *) mmap(NULL, 0x300, PROT_READ | PROT_WRITE, MAP_SHARED, dev_fd, CM_CLOCK_BASE);
	CurValue = *(volatile unsigned int*)(PinConfig+CM_FCLKEN1_CORE/4);
	CurValue &= 0xfffff7ff;
	CurValue |= 0x800;	// set EN_GPT10 1, GPTIMER 10 functional clock is enabled
	*(volatile unsigned int*)(PinConfig+CM_FCLKEN1_CORE/4) = CurValue;
	printf("FCLOCK enabled........\n");

	CurValue = *(volatile unsigned int*)(PinConfig+CM_ICLKEN1_CORE/4);
	CurValue &= 0xfffff7ff;
	CurValue |= 0x800;	// set EN_GPT10 1, GPTIMER 10 interface clock is enabled
	*(volatile unsigned int*)(PinConfig+CM_ICLKEN1_CORE/4) = CurValue;
	printf("ICLOCK enabled.........\n");
	munmap(PinConfig, 0x1000);

	// System control module: 0x4800 2000, found via devmem2
	PinConfig=(unsigned int *) mmap(NULL, 0x200, PROT_READ | PROT_WRITE, MAP_SHARED,dev_fd, 0x48002000);
	// Set PWM function on pin: GPIO_56, EMA-product-board GPMC_nCS5, DM3730 spec page 2428
	// division by 4 is necessary because the size of one element of "unsigned int" is 4 bytes, which corresponds to the size of control registers
	CurValue=*(volatile unsigned int*)(PinConfig+0x174/4); 
	CurValue &= 0x0000ffff;
	/* Timer 10: mode 2 - gpt_10_pwm_evt, PullUp selected, PullUp/Down enabled, Input enable value for pad_x */
	CurValue |= 0x011A0000; 
	*(volatile unsigned int*)(PinConfig+0x174/4) = CurValue; //PIN CONFIGURED AS BIDIRECTIONAL

	munmap(PinConfig, 0x200);

	// Configure timer 10 and 11 to 13 MHz
	// Config=(unsigned int *) mmap(NULL, 0x10000, PROT_READ | PROT_WRITE, MAP_SHARED,dev_fd, 0x48004000);
	// CurValue=Config[0xA40/4];
	// CurValue=CurValue | (1<<6) | (1<<7);
	// CurValue |= (1<<6);//Configure 10 timer to 13 MHz
	// CurValue |= (1<<7);//Configure 11 timer to 13 MHz
	// Config[0xA40/4]=CurValue;

	// GPTIMER10 base address: 0x48086000(in byte space)
        // gpt10: in int space
	gpt10=(unsigned int *) mmap(NULL, 0x10000, PROT_READ | PROT_WRITE, MAP_SHARED,dev_fd, 0x48086000);
	resolution = pwm_calc_resolution(frequency, PWM_FREQUENCY_13MHZ);
	pwm_config_timer(gpt10, resolution,duty_cycle);
	munmap(gpt10, 0x10000);
	close(dev_fd);
	return 0;
}
Exemplo n.º 3
0
int main(int argc, char *argv[]){
        int dev_fd;
        unsigned int *PinConfig;
        unsigned int CurValue;
        unsigned int *gpt10;
        unsigned int *gpt11;
        unsigned long resolution;
        int i;

    printf("Run like: sudo ./pwm_test 50\n");
        //Configure PIN
        dev_fd = open("/dev/mem", O_RDWR | O_SYNC);
        if (dev_fd == -1) {
                printf("Can not opem /dev/mem.");
                return -1;
        }
        else
                printf("dev_fd = %d...............\n", dev_fd);

        //set the clock source to 13MHz
        PinConfig = (unsigned int *) mmap(NULL, 0x300, PROT_READ | PROT_WRITE, MAP_SHARED,dev_fd, CM_CLOCK_BASE);
        CurValue = INT(PinConfig+CM_CLKSEL_CORE/4);
        CurValue &= 0xffffff3f;
        CurValue |= 0xc0;        //set CLKSEL_GPT10/11 1,
        INT(PinConfig+CM_CLKSEL_CORE/4) = CurValue;
        printf("13MHz clock source enabled........\n");

        //enable the clock: FCLK and ICLK
        PinConfig = (unsigned int *) mmap(NULL, 0x300, PROT_READ | PROT_WRITE, MAP_SHARED,dev_fd, CM_CLOCK_BASE);
        CurValue = INT(PinConfig+CM_FCLKEN1_CORE/4);
        CurValue &= 0xffffe7ff;
        CurValue |= 0x1800;        //set EN_GPT10/11 1, GPTIMER 10/11 functional clock is enabled
        INT(PinConfig+CM_FCLKEN1_CORE/4) = CurValue;
        printf("FCLOCK enabled........\n");

        CurValue = INT(PinConfig+CM_ICLKEN1_CORE/4);
        CurValue &= 0xffffe7ff;
        CurValue |= 0x1800;        //set EN_GPT10/11 1, GPTIMER 10/11 interface clock is enabled
        INT(PinConfig+CM_ICLKEN1_CORE/4) = CurValue;
        printf("ICLOCK enabled.........\n");
        munmap(PinConfig, 0x1000);

        //System control module: 0x4800 2000, found via devmem2
        PinConfig=(unsigned int *) mmap(NULL, 0x200, PROT_READ | PROT_WRITE, MAP_SHARED,dev_fd, 0x48002000);

        //Set PWM function on pin: GPIO_145/146, BB-xM-RevC P9-pin10/pin6
        //division by 4 is necessary because the size of one element of "unsigned int" is 4 bytes, which corresponds to the size of control registers
        CurValue=INT(PinConfig+0x174/4);
        CurValue &= 0x0000ffff;
        /* Timer 10: mode 2 - gpt_10_pwm_evt, PullUp selected, PullUp/Down enabled, Input enable value for pad_x */
        CurValue |= 0x011A0000;
        INT(PinConfig+0x174/4) = CurValue; //PIN CONFIGURED AS BIDIRECTIONAL

        CurValue=INT(PinConfig+0x178/4);
        CurValue &= 0xffff0000;
        /* Timer 11: mode 2 - gpt_11_pwm_evt, PullUp selected, PullUp/Down enabled, Input enable value for pad_x */
        CurValue |= 0x0000011A;
        INT(PinConfig+0x178/4) = CurValue; //PIN CONFIGURED AS BIDIRECTIONAL

        munmap(PinConfig, 0x200);

        //Configure timer 10 and 11 to 13 MHz
        //Config=(unsigned int *) mmap(NULL, 0x10000, PROT_READ | PROT_WRITE, MAP_SHARED,dev_fd, 0x48004000);
        //CurValue=Config[0xA40/4];
        //CurValue=CurValue | (1<<6) | (1<<7);
        //CurValue |= (1<<6);//Configure 10 timer to 13 MHz
        //CurValue |= (1<<7);//Configure 11 timer to 13 MHz
        //Config[0xA40/4]=CurValue;

        //GPTIMER10 base address: 0x48086000(in byte space)
    //gpt10: in int space
        gpt10=(unsigned int *) mmap(NULL, 0x10000, PROT_READ | PROT_WRITE, MAP_SHARED,dev_fd, 0x48086000);
        resolution = pwm_calc_resolution(2000000, PWM_FREQUENCY_13MHZ);
        pwm_config_timer(gpt10, resolution,atoi(argv[1]));
        munmap(gpt10, 0x10000);

    //gpt11: in int space
        gpt11=(unsigned int *) mmap(NULL, 0x10000, PROT_READ | PROT_WRITE, MAP_SHARED,dev_fd, 0x4808000);
        resolution = pwm_calc_resolution(2000000, PWM_FREQUENCY_13MHZ);
        pwm_config_timer(gpt11, resolution,atoi(argv[1]));
        munmap(gpt11, 0x10000);

        close(dev_fd);
        return 0;
}