Skip to content

Commit

Permalink
pwm: C: Add PWM example
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Riesco <andre.riesco@toradex.com>
  • Loading branch information
andreriesco committed Jul 14, 2023
1 parent bb9c258 commit 4e06979
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pwm/C/pwmC/Makefile
@@ -1,5 +1,5 @@
# tool macros
CC := g++
CC := gcc
CCFLAGS := -Iincludes/
DBGFLAGS := -g
LDFLAGS :=
Expand Down
4 changes: 4 additions & 0 deletions pwm/C/pwmC/docker-compose.yml
Expand Up @@ -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
59 changes: 59 additions & 0 deletions pwm/C/pwmC/src/main.c
@@ -0,0 +1,59 @@
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>

#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;
}
7 changes: 0 additions & 7 deletions pwm/C/pwmC/src/main.cpp

This file was deleted.

101 changes: 101 additions & 0 deletions pwm/C/pwmC/src/pwm_utils.c
@@ -0,0 +1,101 @@
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <dirent.h>

#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;
}
9 changes: 9 additions & 0 deletions 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

0 comments on commit 4e06979

Please sign in to comment.