From 4e0697952d02c766441a5a89150cc09333d969c4 Mon Sep 17 00:00:00 2001 From: Andre Riesco Date: Thu, 15 Jun 2023 14:24:41 -0300 Subject: [PATCH] pwm: C: Add PWM example Signed-off-by: Andre Riesco --- pwm/C/pwmC/Makefile | 2 +- pwm/C/pwmC/docker-compose.yml | 4 ++ pwm/C/pwmC/src/main.c | 59 ++++++++++++++++++++ pwm/C/pwmC/src/main.cpp | 7 --- pwm/C/pwmC/src/pwm_utils.c | 101 ++++++++++++++++++++++++++++++++++ pwm/C/pwmC/src/pwm_utils.h | 9 +++ 6 files changed, 174 insertions(+), 8 deletions(-) create mode 100644 pwm/C/pwmC/src/main.c delete mode 100644 pwm/C/pwmC/src/main.cpp create mode 100644 pwm/C/pwmC/src/pwm_utils.c create mode 100644 pwm/C/pwmC/src/pwm_utils.h diff --git a/pwm/C/pwmC/Makefile b/pwm/C/pwmC/Makefile index da94c54..f978303 100644 --- a/pwm/C/pwmC/Makefile +++ b/pwm/C/pwmC/Makefile @@ -1,5 +1,5 @@ # tool macros -CC := g++ +CC := gcc CCFLAGS := -Iincludes/ DBGFLAGS := -g LDFLAGS := diff --git a/pwm/C/pwmC/docker-compose.yml b/pwm/C/pwmC/docker-compose.yml index a3338cc..11eed8e 100644 --- a/pwm/C/pwmC/docker-compose.yml +++ b/pwm/C/pwmC/docker-compose.yml @@ -7,9 +7,13 @@ services: image: ${LOCAL_REGISTRY}:5002/pwmc-debug:${TAG} ports: - 2230:2230 + volumes: + - /sys/class/pwm/pwmchip0:/sys/class/pwm/pwmchip0 pwmc: build: context: . dockerfile: Dockerfile image: ${DOCKER_LOGIN}/pwmc:${TAG} + volumes: + - /sys/class/pwm/pwmchip0:/sys/class/pwm/pwmchip0 diff --git a/pwm/C/pwmC/src/main.c b/pwm/C/pwmC/src/main.c new file mode 100644 index 0000000..4ca58d4 --- /dev/null +++ b/pwm/C/pwmC/src/main.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +#include "pwm_utils.h" + +#define PERIOD 1000000000 +#define DUTY_CYCLE 500000000 +#define POLARITY "normal" + +int main(int argc, char **argv) +{ + if ( pwm_exists("/sys/class/pwm/pwmchip0/pwm0/") < 0 ) { + if( pwm_write_val("/sys/class/pwm/pwmchip0/export", 0) < 0) + { + printf("failed to export \n"); + exit(EXIT_FAILURE); + } + } + + if( pwm_write_val("/sys/class/pwm/pwmchip0/pwm0/period", PERIOD) < 0) + { + printf("failed to set period\n"); + exit(EXIT_FAILURE); + } + + if( pwm_write_val("/sys/class/pwm/pwmchip0/pwm0/duty_cycle", DUTY_CYCLE) < 0) + { + printf("failed to set duty cycle\n"); + exit(EXIT_FAILURE); + } + + if( pwm_write_str("/sys/class/pwm/pwmchip0/pwm0/polarity", POLARITY) < 0) + { + printf("failed to set polarity\n"); + exit(EXIT_FAILURE); + } + + if( pwm_write_val("/sys/class/pwm/pwmchip0/pwm0/enable", 1) < 0) + { + printf("failed to enable pwm\n"); + exit(EXIT_FAILURE); + } + + char d_cycle[10]={0}; + if( pwm_read("/sys/class/pwm/pwmchip0/pwm0/duty_cycle", d_cycle, 9) < 0) + printf("unable to read duty cycle\n"); + else + printf("duty cycle is %s\n",d_cycle); + + char polarity[10]={0}; + if( pwm_read("/sys/class/pwm/pwmchip0/pwm0/polarity", polarity, 6) < 0) + printf("failed to read polarity\n"); + else + printf("polarity is %s\n",polarity); + + return 0; +} \ No newline at end of file diff --git a/pwm/C/pwmC/src/main.cpp b/pwm/C/pwmC/src/main.cpp deleted file mode 100644 index 8d7ee3f..0000000 --- a/pwm/C/pwmC/src/main.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include - -int main() { - std::cout << "Hello Torizon!" << std::endl; - - return 0; -} diff --git a/pwm/C/pwmC/src/pwm_utils.c b/pwm/C/pwmC/src/pwm_utils.c new file mode 100644 index 0000000..fd7cccb --- /dev/null +++ b/pwm/C/pwmC/src/pwm_utils.c @@ -0,0 +1,101 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "pwm_utils.h" + +#define MAX_BUF 50 + +int pwm_write_val(char *file, uint32_t val) +{ + int fd; + char buf[MAX_BUF]; + + fd = open(file, O_WRONLY); + if(fd < 0) + { + printf("Error Number % d\n", errno); + perror("pwm"); + return -1; + } + if( snprintf(buf, MAX_BUF, "%u", val) < 0) + goto failed; + + if( write(fd, buf, strlen(buf)) < 0) + { + printf("Error Number % d\n", errno); + perror("pwm"); + goto failed; + } + + close(fd); + return 0; + +failed: + close(fd); + return -1; +} + +int pwm_write_str(char *file, char *val) +{ + int fd; + + fd = open(file, O_WRONLY); + if(fd < 0) + { + printf("Error Number % d\n", errno); + perror("pwm"); + return -1; + } + if( write(fd, val, strlen(val)) < 0) + { + printf("Error Number % d\n", errno); + perror("pwm"); + close(fd); + return -1; + } + + close(fd); + return 0; +} + +int pwm_read(char *file, char *val, uint8_t size) +{ + int fd; + + fd = open(file, O_RDONLY); + if(fd < 0) + { + printf("Error Number % d\n", errno); + perror("pwm"); + return -1; + } + if( read(fd, val, size) < 0) + { + printf("Error Number % d\n", errno); + perror("pwm"); + close(fd); + return -1; + } + + close(fd); + return 0; +} + +int pwm_exists(char *file) { + + DIR* fd = opendir(file); + int exists; + if(fd) + { + exists = 0; + } else { + exists = -1; + } + closedir(fd); + return exists; +} \ No newline at end of file diff --git a/pwm/C/pwmC/src/pwm_utils.h b/pwm/C/pwmC/src/pwm_utils.h new file mode 100644 index 0000000..72ab060 --- /dev/null +++ b/pwm/C/pwmC/src/pwm_utils.h @@ -0,0 +1,9 @@ +#ifndef __PWM_UTILS_H__ +#define __PWM_UTILS_H__ + +int pwm_write_val(char *file, uint32_t val); +int pwm_write_str(char *file, char *val); +int pwm_read(char *file, char *val, uint8_t size); +int pwm_exists(char *file); + +#endif \ No newline at end of file