Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Andre Riesco <andre.riesco@toradex.com>
- Loading branch information
1 parent
bb9c258
commit 4e06979
Showing
6 changed files
with
174 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# tool macros | ||
CC := g++ | ||
CC := gcc | ||
CCFLAGS := -Iincludes/ | ||
DBGFLAGS := -g | ||
LDFLAGS := | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |