mirror of
https://github.com/vsergeev/c-periphery.git
synced 2026-10-06 04:59:26 +08:00
pwm: add pwm module
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
LIB = periphery.a
|
||||
SRCS = src/gpio.c src/led.c src/spi.c src/i2c.c src/mmio.c src/serial.c src/version.c
|
||||
SRCS = src/gpio.c src/led.c src/pwm.c src/spi.c src/i2c.c src/mmio.c src/serial.c src/version.c
|
||||
|
||||
SRCDIR = src
|
||||
OBJDIR = obj
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
# c-periphery [](https://travis-ci.org/vsergeev/c-periphery) [](https://github.com/vsergeev/c-periphery) [](https://github.com/vsergeev/c-periphery/blob/master/LICENSE)
|
||||
|
||||
## C Library for Linux Peripheral I/O (GPIO, LED, SPI, I2C, MMIO, Serial)
|
||||
## C Library for Linux Peripheral I/O (GPIO, LED, PWM, SPI, I2C, MMIO, Serial)
|
||||
|
||||
c-periphery is a small C library for GPIO, LED, SPI, I2C, MMIO, and Serial peripheral I/O interface access in userspace Linux. c-periphery simplifies and consolidate the native Linux APIs to these interfaces. c-periphery is useful in embedded Linux environments (including Raspberry Pi, BeagleBone, etc. platforms) for interfacing with external peripherals. c-periphery is re-entrant, has no dependencies outside the standard C library and Linux, compiles into a static library for easy integration with other projects, and is MIT licensed.
|
||||
c-periphery is a small C library for GPIO, LED, PWM, SPI, I2C, MMIO, and Serial peripheral I/O interface access in userspace Linux. c-periphery simplifies and consolidate the native Linux APIs to these interfaces. c-periphery is useful in embedded Linux environments (including Raspberry Pi, BeagleBone, etc. platforms) for interfacing with external peripherals. c-periphery is re-entrant, has no dependencies outside the standard C library and Linux, compiles into a static library for easy integration with other projects, and is MIT licensed.
|
||||
|
||||
Using Python or Lua? Check out the [python-periphery](https://github.com/vsergeev/python-periphery) and [lua-periphery](https://github.com/vsergeev/lua-periphery) projects.
|
||||
|
||||
@@ -109,6 +109,59 @@ int main(void) {
|
||||
|
||||
[Go to LED documentation.](docs/led.md)
|
||||
|
||||
### PWM
|
||||
|
||||
``` c
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "pwm.h"
|
||||
|
||||
int main(void) {
|
||||
pwm_t *pwm;
|
||||
|
||||
pwm = pwm_new();
|
||||
|
||||
/* Open PWM chip 0, channel 10 */
|
||||
if (pwm_open(pwm, 0, 10) < 0) {
|
||||
fprintf(stderr, "pwm_open(): %s\n", pwm_errmsg(pwm));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Set frequency to 1 kHz */
|
||||
if (pwm_set_frequency(pwm, 1e3) < 0) {
|
||||
fprintf(stderr, "pwm_set_frequency(): %s\n", pwm_errmsg(pwm));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Set duty cycle to 75% */
|
||||
if (pwm_set_duty_cycle(pwm, 0.75) < 0) {
|
||||
fprintf(stderr, "pwm_set_duty_cycle(): %s\n", pwm_errmsg(pwm));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Enable PWM */
|
||||
if (pwm_enable(pwm) < 0) {
|
||||
fprintf(stderr, "pwm_enable(): %s\n", pwm_errmsg(pwm));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Change duty cycle to 50% */
|
||||
if (pwm_set_duty_cycle(pwm, 0.50) < 0) {
|
||||
fprintf(stderr, "pwm_set_duty_cycle(): %s\n", pwm_errmsg(pwm));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
pwm_close(pwm);
|
||||
|
||||
pwm_free(pwm);
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
[Go to PWM documentation.](docs/pwm.md)
|
||||
|
||||
### SPI
|
||||
|
||||
``` c
|
||||
@@ -376,7 +429,7 @@ $
|
||||
|
||||
## Building c-periphery into another project
|
||||
|
||||
Include the header files in `src/` (e.g. `gpio.h`, `led.h`, `spi.h`, `i2c.h`, `mmio.h`, `serial.h`) and link in the `periphery.a` static library.
|
||||
Include the header files in `src/` (e.g. `gpio.h`, `led.h`, `pwm.h`, `spi.h`, `i2c.h`, `mmio.h`, `serial.h`) and link in the `periphery.a` static library.
|
||||
|
||||
``` console
|
||||
$ gcc -I/path/to/periphery/src myprog.c /path/to/periphery/periphery.a -o myprog
|
||||
|
||||
+321
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* c-periphery
|
||||
* https://github.com/vsergeev/c-periphery
|
||||
* License: MIT
|
||||
*/
|
||||
|
||||
#ifndef _PERIPHERY_PWM_H
|
||||
#define _PERIPHERY_PWM_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
enum pwm_error_code {
|
||||
PWM_ERROR_ARG = -1, /* Invalid arguments */
|
||||
PWM_ERROR_OPEN = -2, /* Opening PWM */
|
||||
PWM_ERROR_QUERY = -3, /* Querying PWM attributes */
|
||||
PWM_ERROR_CONFIGURE = -4, /* Configuring PWM attributes */
|
||||
PWM_ERROR_CLOSE = -5, /* Closing PWM */
|
||||
};
|
||||
|
||||
typedef enum pwm_polarity {
|
||||
PWM_POLARITY_NORMAL, /* Normal polarity */
|
||||
PWM_POLARITY_INVERSED, /* Inversed polarity */
|
||||
} pwm_polarity_t;
|
||||
|
||||
typedef struct pwm_handle pwm_t;
|
||||
|
||||
/* Primary Functions */
|
||||
pwm_t *pwm_new(void);
|
||||
int pwm_open(pwm_t *pwm, unsigned int chip, unsigned int channel);
|
||||
int pwm_enable(pwm_t *pwm);
|
||||
int pwm_disable(pwm_t *pwm);
|
||||
int pwm_close(pwm_t *pwm);
|
||||
void pwm_free(pwm_t *pwm);
|
||||
|
||||
/* Getters */
|
||||
int pwm_get_enabled(pwm_t *pwm, bool *enabled);
|
||||
int pwm_get_period_ns(pwm_t *pwm, uint64_t *period_ns);
|
||||
int pwm_get_duty_cycle_ns(pwm_t *pwm, uint64_t *duty_cycle_ns);
|
||||
int pwm_get_period(pwm_t *pwm, double *period);
|
||||
int pwm_get_duty_cycle(pwm_t *pwm, double *duty_cycle);
|
||||
int pwm_get_frequency(pwm_t *pwm, double *frequency);
|
||||
int pwm_get_polarity(pwm_t *pwm, pwm_polarity_t *polarity);
|
||||
|
||||
/* Setters */
|
||||
int pwm_set_enabled(pwm_t *pwm, bool enabled);
|
||||
int pwm_set_period_ns(pwm_t *pwm, uint64_t period_ns);
|
||||
int pwm_set_duty_cycle_ns(pwm_t *pwm, uint64_t duty_cycle_ns);
|
||||
int pwm_set_period(pwm_t *pwm, double period);
|
||||
int pwm_set_duty_cycle(pwm_t *pwm, double duty_cycle);
|
||||
int pwm_set_frequency(pwm_t *pwm, double frequency);
|
||||
int pwm_set_polarity(pwm_t *pwm, pwm_polarity_t polarity);
|
||||
|
||||
/* Miscellaneous */
|
||||
unsigned int pwm_chip(pwm_t *pwm);
|
||||
unsigned int pwm_channel(pwm_t *pwm);
|
||||
int pwm_tostring(pwm_t *pwm, char *str, size_t len);
|
||||
|
||||
/* Error Handling */
|
||||
int pwm_errno(pwm_t *pwm);
|
||||
const char *pwm_errmsg(pwm_t *pwm);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user