Skip to content

Commit 2734993

Browse files
authored
Merge pull request #128 from adafruit/issue126-fix-ecap-pwm
Add PWM support for ecap pins
2 parents cf9771a + 67e0f61 commit 2734993

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

source/c_pwm.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ SOFTWARE.
3838
#endif
3939

4040
#define KEYLEN 7
41+
#define PIN_MODE_LEN 5
4142

4243
int pwm_initialized = 0;
4344

@@ -286,6 +287,7 @@ BBIO_err pwm_setup(const char *key, float duty, float freq, int polarity)
286287
char period_path[90];
287288
char polarity_path[90];
288289
char enable_path[90];
290+
char pin_mode[PIN_MODE_LEN]; // "pwm" or "pwm2"
289291

290292
int e;
291293
int period_fd, duty_fd, polarity_fd, enable_fd;
@@ -316,7 +318,14 @@ BBIO_err pwm_setup(const char *key, float duty, float freq, int polarity)
316318
return BBIO_CAPE;
317319
}
318320
// Do pinmuxing
319-
set_pin_mode(key, "pwm");
321+
if(!strcmp(key, "P9_28")) {
322+
// ecap2 (P9_28) requires mode pwm2
323+
// based on bonescript commit 23bf443 by Matthew West
324+
strncpy(pin_mode, "pwm2", PIN_MODE_LEN);
325+
} else {
326+
strncpy(pin_mode, "pwm", PIN_MODE_LEN);
327+
}
328+
set_pin_mode(key, pin_mode);
320329

321330
// Get info for pwm
322331
err = get_pwm_by_key(key, &p);

source/common.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ uart_t uart_table[] = {
174174
};
175175

176176
// Copied from https://github.com/jadonk/bonescript/blob/master/src/bone.js
177-
177+
// See am335x technical manual, p. 183, for more info:
178+
// http://www.ti.com/lit/ug/spruh73n/spruh73n.pdf
178179
pwm_t pwm_table[] = {
179180
{ "ehrpwm2", 6, 1, 4, "ehrpwm.2:1", "EHRPWM2B", "48304000", "48304200", "P8_13"},
180181
{ "ehrpwm2", 5, 0, 4, "ehrpwm.2:0", "EHRPWM2A", "48304000", "48304200", "P8_19"},
@@ -186,10 +187,10 @@ pwm_t pwm_table[] = {
186187
{ "ehrpwm1", 4, 1, 6, "ehrpwm.1:1", "EHRPWM1B", "48302000", "48302200", "P9_16"},
187188
{ "ehrpwm0", 1, 1, 3, "ehrpwm.0:1", "EHRPWM0B", "48300000", "48300200", "P9_21"},
188189
{ "ehrpwm0", 0, 0, 3, "ehrpwm.0:0", "EHRPWM0A", "48300000", "48300200", "P9_22"},
189-
{ "ecap2", 7, 2, 4, "ecap.2", "ECAPPWM2", "", "", "P9_28"},
190+
{ "ecap2", 7, 0, 4, "ecap.2", "ECAPPWM2", "48304000", "48304100", "P9_28"},
190191
{ "ehrpwm0", 1, 1, 1, "ehrpwm.0:1", "EHRPWM0B", "48300000", "48300200", "P9_29"},
191192
{ "ehrpwm0", 0, 0, 1, "ehrpwm.0:0", "EHRPWM0A", "48300000", "48300200", "P9_31"},
192-
{ "ecap0", 2, 0, 0, "ecap.0", "ECAPPWM0", "", "", "P9_42"},
193+
{ "ecap0", 2, 0, 0, "ecap.0", "ECAPPWM0", "48300000", "48300100", "P9_42"},
193194
{ NULL, 0, 0, 0, NULL, NULL, NULL, NULL, NULL }
194195
};
195196

test/test_pwm_setup.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,38 @@ def test_start_pwm(self):
4848
assert int(period) == 500000
4949
PWM.cleanup()
5050

51+
def test_start_pwm_ecap0(self):
52+
print("test_start_pwm_ecap0\n");
53+
PWM.cleanup()
54+
PWM.start("P9_42", 0)
55+
pwm_dir = get_pwm_dir()
56+
assert os.path.exists(pwm_dir)
57+
if kernel >= '4.1.0':
58+
duty = open(pwm_dir + '/duty_cycle').read()
59+
else:
60+
duty = open(pwm_dir + '/duty').read()
61+
period = open(pwm_dir + '/period').read()
62+
assert int(duty) == 0
63+
assert int(period) == 500000
64+
PWM.cleanup()
65+
66+
# test not enabled as default as
67+
# cape-universala overlay required
68+
#def test_start_pwm_ecap2(self):
69+
#print("test_start_pwm_ecap2\n");
70+
#PWM.cleanup()
71+
#PWM.start("P9_28", 0)
72+
#pwm_dir = get_pwm_dir()
73+
#assert os.path.exists(pwm_dir)
74+
#if kernel >= '4.1.0':
75+
#duty = open(pwm_dir + '/duty_cycle').read()
76+
#else:
77+
#duty = open(pwm_dir + '/duty').read()
78+
#period = open(pwm_dir + '/period').read()
79+
#assert int(duty) == 0
80+
#assert int(period) == 500000
81+
#PWM.cleanup()
82+
5183
def test_start_pwm_with_polarity_one(self):
5284
PWM.cleanup()
5385
PWM.start("P9_14", 0, 2000, 1)

0 commit comments

Comments
 (0)