Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pwm example #29

Merged
merged 1 commit into from
Mar 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add pwm example
  • Loading branch information
xipengwang committed Mar 25, 2017
commit a74f8b367046fd0794c28b006dae8ededc24af10
2 changes: 1 addition & 1 deletion src/examples/gpio_test/gpio_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

int main(int argc, char **args)
{
if (rpi_init() ==-1) {
if (rpi_init() == -1) {
printf("initialization failed");
return -1;
}
Expand Down
8 changes: 0 additions & 8 deletions src/examples/pwm_test/meson.build

This file was deleted.

49 changes: 38 additions & 11 deletions src/examples/pwm_test/pwm_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,46 @@

int main(int argc, char **args)
{
if(rpi_init() ==-1){
printf("initialization failed");
return -1;
if(rpi_init() == -1){
printf("initialization failed");
return -1;
}
//configure GPIO pin
print_version();

// PIN_33: PWM0 [ALT0];
// PIN_32: PWM1 [ALT0];
// PIN_12: PWM0 [ALT5];
// PIN_35: PWM1 [ALT5];
// Configure GPIO pin as ALT5, so it could be used for PWM function
rpi_gpio_fsel(PIN_12, RPI_GPIO_FSEL_ALT5);

pwm_init(RPI_PWM_CHANNEL_0, 1, 1);
pwm_set_clock(RPI_PWM_CLOCK_DIVIDER_16); //600khz
pwm_set_range(0, 1024);
pwm_set_data(0, 512);


// Initialize PWM Channel 0; Return 0 on success
if(pwm_init(RPI_PWM_CHANNEL_0, 1, 1)) {
printf("Initializing PWM fails \n");
exit(-1);
}

// RPI_PWM_CLOCK_DIVIDER_2048 = 2048, //9.375khz
// RPI_PWM_CLOCK_DIVIDER_1024 = 1024, //18.75khz
// RPI_PWM_CLOCK_DIVIDER_512 = 512, //37.5khz
// RPI_PWM_CLOCK_DIVIDER_256 = 256, //75khz
// RPI_PWM_CLOCK_DIVIDER_128 = 128, //150khz
// RPI_PWM_CLOCK_DIVIDER_64 = 64, //300khz
// RPI_PWM_CLOCK_DIVIDER_32 = 32, //600.0khz
// RPI_PWM_CLOCK_DIVIDER_16 = 16, //1.2Mhz
// RPI_PWM_CLOCK_DIVIDER_8 = 8, //2.4Mhz
// RPI_PWM_CLOCK_DIVIDER_4 = 4, //4.8Mhz
// RPI_PWM_CLOCK_DIVIDER_2 = 2, //9.6Mhz, fastest you can get
// RPI_PWM_CLOCK_DIVIDER_1 = 1 //4.6875khz, same as divider 4096

// Configure PWM clock = 19.2Mhz / 32 = 600kHz
pwm_set_clock(32);

// Set PWM Frequency = 19.2Mhz / 16 / 1000 = 600Hz
pwm_set_range(0, 1000);

// Set PWM duty cycle as 800 / 1000 = 80%
pwm_set_data(0, 800);

return 0;
}

8 changes: 5 additions & 3 deletions src/rpi/pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
volatile rpi_pwm_t *rpi_pwm;
volatile rpi_pwm_clk_t* rpi_pwm_clk;

void pwm_init(Rpi_Pwm_Channel channel, uint8_t markspace, uint8_t enable)
int pwm_init(Rpi_Pwm_Channel channel, uint8_t markspace, uint8_t enable)
{
if(rpi_pwm == MAP_FAILED || rpi_pwm_clk == MAP_FAILED)
return;
return -1;

if(channel == RPI_PWM_CHANNEL_0){
if (markspace) {
rpi_pwm->CTL.reg |= RPI_PWM_CTL_PWEN1_Msk;
Expand All @@ -29,6 +30,7 @@ void pwm_init(Rpi_Pwm_Channel channel, uint8_t markspace, uint8_t enable)
rpi_pwm->CTL.reg &= ~RPI_PWM_CTL_MSEN2_Msk;
}
}
return 0;
}

void pwm_set_clock(uint32_t divisor)
Expand All @@ -46,7 +48,7 @@ void pwm_set_clock(uint32_t divisor)
rpi_pwm_clk->DIV.reg = RPI_PWM_PASSWD | (divisor << 12);

rpi_pwm_clk->CTL.reg = RPI_PWM_PASSWD | 0x11;

}

void pwm_set_range(Rpi_Pwm_Channel channel, uint32_t range)
Expand Down
2 changes: 1 addition & 1 deletion src/rpi/pwm.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ extern volatile rpi_pwm_clk_t *rpi_pwm_clk;
#ifdef __cplusplus
extern "C" {
#endif
void pwm_init(Rpi_Pwm_Channel channel, uint8_t markspace, uint8_t enable);
int pwm_init(Rpi_Pwm_Channel channel, uint8_t markspace, uint8_t enable);

void pwm_set_clock(uint32_t divisor);

Expand Down
2 changes: 1 addition & 1 deletion src/rpi/rpi.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#define RPI_GPIO_BASE 0x200000
#define RPI_SYS_TIMER 0x3000
#define RPI_PWM 0x20C000
#define RPI_PWM_CLK 0x1010a0
#define RPI_PWM_CLK 0x1010a0
#define RPI_I2C0 0x205000
#define RPI_I2C1 0x804000
#define RPI_SPI0 0x204000
Expand Down