Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
Sys
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
GIHO KOH
Sys
Merge requests
!3
Main
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Main
nangtural02/sys:main
into
main
Overview
0
Commits
16
Pipelines
0
Changes
1
Merged
JangYeon Kim
requested to merge
nangtural02/sys:main
into
main
1 year ago
Overview
0
Commits
16
Pipelines
0
Changes
1
Expand
0
0
Merge request reports
Viewing commit
27adae0c
Prev
Next
Show latest version
1 file
+
155
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
27adae0c
Upload New File
· 27adae0c
JangYeon Kim
authored
1 year ago
Central_Control_Board/PWM.c
0 → 100644
+
155
−
0
Options
#include
"JaeYukBookkom.h"
#include
<pthread.h>
#include
<fcntl.h>
#include
<stdio.h>
static
int
PWMExport
(
int
pwmnum
);
static
int
PWMUnExport
(
int
pwmnum
);
static
int
PWMEnable
(
int
pwmnum
);
static
int
PWMWritePeriod
(
int
pwmnum
,
int
value
);
static
int
PWMWriteDutyCycle
(
int
pwmnum
,
int
value
);
int
PWMInit
(
int
pwm
,
int
period
,
int
dutycycle
);
void
*
bozzering
(
void
*
arg
);
void
boozer_Warning
(
void
);
void
SpeakerUnExport
(
void
*
args
);
int
boozer_on
=
0
;
pthread_t
*
boozerThread
;
int
PWMInit
(
int
pwm
,
int
period
,
int
dutycycle
){
PWMExport
(
pwm
);
PWMWritePeriod
(
pwm
,
period
);
PWMWriteDutyCycle
(
pwm
,
dutycycle
);
}
void
*
bozzering
(
void
*
arg
){
PWMInit
(
SPEAKER
,
10000000
,
0
);
pthread_setcancelstate
(
PTHREAD_CANCEL_ENABLE
,
NULL
);
pthread_cleanup_push
(
SpeakerUnExport
,
NULL
);
while
(
1
){
for
(
int
i
=
0
;
i
<
1000
;
i
++
){
PWMWriteDutyCycle
(
SPEAKER
,
i
*
10000
);
usleep
(
1000
);
}
for
(
int
i
=
1000
;
i
>
0
;
i
--
){
PWMWriteDutyCycle
(
SPEAKER
,
i
*
10000
);
usleep
(
1000
);
}
}
pthread_cleanup_pop
(
0
);
}
void
boozer_Warning
(
void
){
PWMInit
(
SPEAKER
,
10000000
,
0
);
for
(
int
i
=
0
;
i
<
1000
;
i
++
){
PWMWriteDutyCycle
(
SPEAKER
,
i
*
10000
);
usleep
(
1000
);
}
for
(
int
i
=
1000
;
i
>
0
;
i
--
){
PWMWriteDutyCycle
(
SPEAKER
,
i
*
10000
);
usleep
(
1000
);
}
PWMUnExport
(
SPEAKER
);
}
static
int
PWMExport
(
int
pwmnum
){
char
buffer
[
BUFFER_MAX
];
int
fd
,
byte
;
// TODO: Enter the export path.
fd
=
open
(
"/sys/class/pwm/pwmchip0/export"
,
O_WRONLY
);
if
(
-
1
==
fd
){
fprintf
(
stderr
,
"Failed to open export for export!
\n
"
);
return
(
-
1
);
}
byte
=
snprintf
(
buffer
,
BUFFER_MAX
,
"%d"
,
pwmnum
);
write
(
fd
,
buffer
,
byte
);
close
(
fd
);
usleep
(
1000000
);
return
(
0
);
}
static
int
PWMUnExport
(
int
pwmnum
){
char
buffer
[
BUFFER_MAX
];
int
fd
,
byte
;
// TODO: Enter the export path.
fd
=
open
(
"/sys/class/pwm/pwmchip0/unexport"
,
O_WRONLY
);
if
(
-
1
==
fd
){
fprintf
(
stderr
,
"Failed to open export for export!
\n
"
);
return
(
-
1
);
}
byte
=
snprintf
(
buffer
,
BUFFER_MAX
,
"%d"
,
pwmnum
);
write
(
fd
,
buffer
,
byte
);
close
(
fd
);
usleep
(
1000
);
return
(
0
);
}
void
SpeakerUnExport
(
void
*
args
){
PWMUnExport
(
SPEAKER
);
free
(
boozerThread
);
sleep
(
1
);
}
static
int
PWMEnable
(
int
pwmnum
){
static
const
char
s_enable_str
[]
=
"1"
;
char
path
[
DIRECTION_MAX
];
int
fd
;
// TODO: Enter the enable path.
snprintf
(
path
,
DIRECTION_MAX
,
"/sys/class/pwm/pwmchip0/pwm0/enable"
,
pwmnum
);
fd
=
open
(
path
,
O_WRONLY
);
if
(
-
1
==
fd
){
fprintf
(
stderr
,
"Failed to open in enable!
\n
"
);
return
-
1
;
}
write
(
fd
,
s_enable_str
,
strlen
(
s_enable_str
));
close
(
fd
);
return
(
0
);
}
static
int
PWMWritePeriod
(
int
pwmnum
,
int
value
){
char
s_value_str
[
VALUE_MAX
];
char
path
[
VALUE_MAX
];
int
fd
,
byte
;
// TODO: Enter the period path.
snprintf
(
path
,
VALUE_MAX
,
"/sys/class/pwm/pwmchip0/pwm0/period"
,
pwmnum
);
fd
=
open
(
path
,
O_WRONLY
);
if
(
-
1
==
fd
){
fprintf
(
stderr
,
"Failed to open in period!
\n
"
);
return
(
-
1
);
}
byte
=
snprintf
(
s_value_str
,
VALUE_MAX
,
"%d"
,
value
);
if
(
-
1
==
write
(
fd
,
s_value_str
,
byte
)){
fprintf
(
stderr
,
"Failed to write value in period!
\n
"
);
close
(
fd
);
return
-
1
;
}
close
(
fd
);
return
(
0
);
}
static
int
PWMWriteDutyCycle
(
int
pwmnum
,
int
value
)
{
char
s_value_str
[
VALUE_MAX
];
char
path
[
VALUE_MAX
];
int
fd
,
byte
;
// TODO: Enter the duty_cycle path.
snprintf
(
path
,
VALUE_MAX
,
"/sys/class/pwm/pwmchip0/pwm0/duty_cycle"
,
pwmnum
);
fd
=
open
(
path
,
O_WRONLY
);
if
(
-
1
==
fd
){
fprintf
(
stderr
,
"Failed to open in duty cycle!
\n
"
);
return
(
-
1
);
}
byte
=
snprintf
(
s_value_str
,
VALUE_MAX
,
"%d"
,
value
);
if
(
-
1
==
write
(
fd
,
s_value_str
,
byte
)){
fprintf
(
stderr
,
"Failed to write value in duty cycle!
\n
"
);
close
(
fd
);
return
-
1
;
}
close
(
fd
);
return
(
0
);
}
Loading