Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
LinuxPWM.hxx
Go to the documentation of this file.
1
42#ifndef _OS_LINUXPWM_HXX_
43#define _OS_LINUXPWM_HXX_
44
45#include <fcntl.h>
46#include <stdio.h>
47#include <string.h>
48#include <string>
49#include <sys/stat.h>
50#include <sys/types.h>
51
53#include "utils/FileUtils.hxx"
56
58class LinuxPWM : public PWM
59{
60public:
66 LinuxPWM(int chip, int channel)
67 : chip_(chip)
68 , channel_(channel)
69 , pwmdir_(
70 StringPrintf("/sys/class/pwm/pwmchip%d/pwm%d/", chip, channel))
71 {
72 }
73
77 {
78 if (access((pwmdir_ + PERIOD_FILE).c_str(), F_OK) != -1)
79 {
80 set_period(1);
81 return;
82 }
83
84 string export_file =
85 StringPrintf("/sys/class/pwm/pwmchip%d/export", chip_);
86 if (access(export_file.c_str(), W_OK) < 0)
87 {
88 int err = errno;
89 LOG_ERROR("Cannot write to %s: %s\n",export_file.c_str(),
90 strerror(errno));
91 exit(err);
92 }
93 write_string_to_file(export_file, integer_to_string(channel_) + "\n");
94 // 50ms delay IS needed while kernel changes ownership of created GPIO directory
95 usleep(50000);
96 set_period(1);
97 }
98
99 void set_period(uint32_t counts) override
100 {
101
103 if (counts > 0)
104 {
105
106
107 enable();
108 }
109 else
110 {
111 disable();
112 }
113 }
114
115 uint32_t get_period() override
116 {
117
119
120 }
121
122 void set_duty(uint32_t counts) override
123 {
124
126 if (counts > 0)
127 {
128
129 enable();
130 }
131 else
132 {
133 disable();
134 }
135 }
136
137 uint32_t get_duty() override
138 {
139
141
142 }
143
144 uint32_t get_period_max() override
145 {
146 return 0xffffffff;
147 }
148
149 uint32_t get_period_min() override
150 {
151 return 1;
152 }
153
154
156 void enable()
157 {
159 }
160
162 void disable()
163 {
165 }
166
168 bool enabled()
169 {
171
172
173 }
174
175private:
177 static constexpr const char *ENABLE_FILE = "enable";
179 static constexpr const char *PERIOD_FILE = "period";
181 static constexpr const char *DUTY_CYCLE_FILE = "duty_cycle";
182
184 const uint32_t chip_;
186 const uint32_t channel_;
188 const string pwmdir_;
189
193 uint32_t get_sysfs_file_value(const char *basename)
194 {
195 string filename(pwmdir_);
196 filename += basename;
197 uint32_t value = 0;
198 FILE *fp = fopen(filename.c_str(), "r");
199 if (fp == NULL)
200 {
201 int err = errno;
202 LOG_ERROR("Cannot read from %s: %s\n",filename.c_str(),
203 strerror(errno));
204 exit(err);
205 }
206 fscanf(fp, "%d", &value);
207 fclose(fp);
208 return value;
209 }
210
214 void set_sysfs_file_value(const char *basename, uint32_t value)
215 {
216 string filename(pwmdir_);
217 filename += basename;
218 FILE *fp = fopen(filename.c_str(), "w");
219 if (fp == NULL)
220 {
221 int err = errno;
222 LOG_ERROR("Cannot write to %s: %s\n",filename.c_str(),
223 strerror(errno));
224 exit(err);
225 }
226 fprintf(fp, "%d\n", value);
227
228 fclose(fp);
229 }
230};
231
232#endif // _OS_LINUXPWM_HXX_
void write_string_to_file(const string &filename, const string &data)
Opens (or creates) a file, truncates it and overwrites the contents with what is given in a string.
Implementation of a single PWM channel using the linux sysfs API.
Definition LinuxPWM.hxx:59
static constexpr const char * DUTY_CYCLE_FILE
Basename for sysfs file for setting the duty cycle.
Definition LinuxPWM.hxx:181
uint32_t get_period_max() override
Get max period supported.
Definition LinuxPWM.hxx:144
static constexpr const char * ENABLE_FILE
Basename for sysfs file for enabling the channel.
Definition LinuxPWM.hxx:177
uint32_t get_period_min() override
Get min period supported.
Definition LinuxPWM.hxx:149
void set_period(uint32_t counts) override
Set PWM period.
Definition LinuxPWM.hxx:99
void enable()
Enables the output PWM waveform.
Definition LinuxPWM.hxx:156
void set_sysfs_file_value(const char *basename, uint32_t value)
Writes a single integer into a sysfs file of the channel directory.
Definition LinuxPWM.hxx:214
uint32_t get_sysfs_file_value(const char *basename)
Returns a single file's content from the channel directory on sysfs.
Definition LinuxPWM.hxx:193
void disable()
Disables the output PWM waveform.
Definition LinuxPWM.hxx:162
void export_pin()
Call this once after instantiating the object but before using any of the PWM apis.
Definition LinuxPWM.hxx:76
void set_duty(uint32_t counts) override
Sets the duty cycle.
Definition LinuxPWM.hxx:122
static constexpr const char * PERIOD_FILE
Basename for sysfs file for setting the period.
Definition LinuxPWM.hxx:179
LinuxPWM(int chip, int channel)
Constructor.
Definition LinuxPWM.hxx:66
bool enabled()
Returns whether the output PWM waveform is enabled or not.
Definition LinuxPWM.hxx:168
const uint32_t channel_
Number of the channel in the chip.
Definition LinuxPWM.hxx:186
const uint32_t chip_
Kernel number of the chip.
Definition LinuxPWM.hxx:184
const string pwmdir_
Diretory path of the channel, with trailing /.
Definition LinuxPWM.hxx:188
uint32_t get_duty() override
Gets the duty cycle.
Definition LinuxPWM.hxx:137
uint32_t get_period() override
Get PWM period.
Definition LinuxPWM.hxx:115
Abstract interface for a PWM driver.
Definition PWM.hxx:43
#define LOG_ERROR(message...)
Shorthand for LOG(LEVEL_ERROR, message...). See LOG.
Definition logging.h:124