2 * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
3 * Copyright (c) 2014 Intel Corporation.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 maa_pwm_setup_duty_fp(maa_pwm_context* dev)
33 sprintf(bu, "/sys/class/pwm/pwmchip%d/pwm%d/duty_cycle", dev->chipid, dev->pin);
35 if ((dev->duty_fp = fopen(bu, "r+b")) == NULL) {
42 maa_pwm_write_period(maa_pwm_context* dev, int period)
46 sprintf(bu, "/sys/class/pwm/pwmchip%d/pwm%d/period", dev->chipid, dev->pin);
48 if ((period_f = fopen(bu, "r+b")) == NULL) {
49 fprintf(stderr, "Failed to open period for writing!\n");
50 return MAA_ERROR_INVALID_RESOURCE;
52 fprintf(period_f, "%d", period);
54 if (ferror(period_f) != 0)
55 return MAA_ERROR_INVALID_RESOURCE;
60 maa_pwm_write_duty(maa_pwm_context* dev, int duty)
62 if (dev->duty_fp == NULL) {
63 maa_pwm_setup_duty_fp(dev);
65 fprintf(dev->duty_fp, "%d", duty);
68 if (ferror(dev->duty_fp) != 0)
69 return MAA_ERROR_INVALID_RESOURCE;
74 maa_pwm_get_period(maa_pwm_context* dev)
80 sprintf(bu, "/sys/class/pwm/pwmchip%d/pwm%d/period", dev->chipid, dev->pin);
81 if ((period_f = fopen(bu, "rb")) == NULL) {
82 fprintf(stderr, "Failed to open period for reading!\n");
85 fgets(output, 16, period_f);
91 maa_pwm_get_duty(maa_pwm_context* dev)
93 if (dev->duty_fp == NULL) {
94 maa_pwm_setup_duty_fp(dev);
97 fgets(output, 16, dev->duty_fp);
98 fseek(dev->duty_fp, SEEK_SET, 0);
103 maa_pwm_init(int pin) {
105 return maa_pwm_init_raw(0, pin);
109 maa_pwm_init_raw(int chipin, int pin)
111 maa_pwm_context* dev = (maa_pwm_context*) malloc(sizeof(maa_pwm_context));
115 dev->chipid = chipin;
120 snprintf(buffer, 64, "/sys/class/pwm/pwmchip%d/export", dev->chipid);
122 if ((export_f = fopen(buffer, "w")) == NULL) {
123 fprintf(stderr, "Failed to open export for writing!\n");
127 fprintf(export_f, "%d", dev->pin);
129 maa_pwm_setup_duty_fp(dev);
135 maa_pwm_write(maa_pwm_context* dev, float percentage)
137 return maa_pwm_write_duty(dev, percentage * maa_pwm_get_period(dev));
141 maa_pwm_read(maa_pwm_context* dev)
143 float output = maa_pwm_get_duty(dev) / (float) maa_pwm_get_period(dev);
148 maa_pwm_period(maa_pwm_context* dev, float seconds)
150 return maa_pwm_period_ms(dev, seconds*1000);
154 maa_pwm_period_ms(maa_pwm_context* dev, int ms)
156 return maa_pwm_period_us(dev, ms*1000);
160 maa_pwm_period_us(maa_pwm_context* dev, int us)
162 return maa_pwm_write_period(dev, us*1000);
166 maa_pwm_pulsewidth(maa_pwm_context* dev, float seconds)
168 return maa_pwm_pulsewidth_ms(dev, seconds*1000);
172 maa_pwm_pulsewidth_ms(maa_pwm_context* dev, int ms)
174 return maa_pwm_pulsewidth_us(dev, ms*1000);
178 maa_pwm_pulsewidth_us(maa_pwm_context* dev, int us)
180 return maa_pwm_write_duty(dev, us*1000);
184 maa_pwm_enable(maa_pwm_context* dev, int enable)
194 sprintf(bu, "/sys/class/pwm/pwmchip%d/pwm%d/enable", dev->chipid, dev->pin);
196 if ((enable_f = fopen(bu, "w")) == NULL) {
197 fprintf(stderr, "Failed to open enable for writing!\n");
198 return MAA_ERROR_INVALID_RESOURCE;
200 fprintf(enable_f, "%d", status);
202 if (ferror(enable_f) != 0)
203 return MAA_ERROR_INVALID_RESOURCE;
208 maa_pwm_unexport(maa_pwm_context* dev)
210 // disable pwm before unexporting
211 maa_pwm_enable(dev, 0);
214 snprintf(buffer, 64, "/sys/class/pwm/pwmchip%d/unexport", dev->chipid);
216 if ((unexport_f = fopen(buffer, "w")) == NULL) {
217 fprintf(stderr, "Failed to open unexport for writing!\n");
218 return MAA_ERROR_INVALID_RESOURCE;
220 fprintf(unexport_f, "%d", dev->pin);
222 if (ferror(unexport_f) != 0)
223 return MAA_ERROR_INVALID_RESOURCE;
227 maa_pwm_close(maa_pwm_context* dev)
229 maa_pwm_unexport(dev);