Skip to content

Commit e0b8261

Browse files
rzryichoi
authored andcommitted
nuttx: Add basic PWM support for stm32f7 (#1888)
It was tested on Nucleo-f767ZI with 4 servo motors: * PWM1.CH1_1@PA8 * PWM2.CH1_1@PA0 * PWM3.CH1_1@PA6 * PWM4.CH1_1@PB6 Relate-to: rzr/webthing-iotjs#3 Forwarded: #1888 Change-Id: I8e195443b1bdcd56258e9bc635e3449dd037de5d IoT.js-DCO-1.0-Signed-off-by: Philippe Coval p.coval@samsung.com
1 parent 246b803 commit e0b8261

File tree

3 files changed

+117
-1
lines changed

3 files changed

+117
-1
lines changed

src/modules/iotjs_module_stm32f7nucleo.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919

2020
jerry_value_t iotjs_init_stm32f7nucleo() {
2121
jerry_value_t stm32f7nucleo = jerry_create_object();
22-
/* Hardware support in progress, do initialization here */
22+
/* Hardware support in progress, do initialization here */
2323

24+
#if defined(__NUTTX__)
25+
26+
iotjs_stm32f7nucleo_pin_initialize(stm32f7nucleo);
27+
28+
#endif
2429
return stm32f7nucleo;
2530
}

src/modules/iotjs_module_stm32f7nucleo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
#define IOTJS_MODULE_STM32F4DIS_H
1818

1919

20+
void iotjs_stm32f7nucleo_pin_initialize(jerry_value_t jobj);
21+
22+
2023
#endif /* IOTJS_MODULE_STM32F4DIS_H */

src/modules/nuttx/iotjs_module_stm32f7nucleo-nuttx.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,116 @@
1515

1616
#if defined(__NUTTX__) && (TARGET_BOARD == stm32f7nucleo)
1717

18+
#include "iotjs_systemio-nuttx.h"
19+
#include "stm32_gpio.h"
1820

1921
#include "iotjs_def.h"
2022
#include "modules/iotjs_module_stm32f7nucleo.h"
2123

24+
#if ENABLE_MODULE_GPIO
25+
26+
static void iotjs_pin_initialize_gpio(jerry_value_t jobj) {
27+
// Set GPIO pin from configuration bits of nuttx.
28+
// GPIO pin name is "P(port)(pin)".
29+
#define SET_GPIO_CONSTANT(port, pin) \
30+
iotjs_jval_set_property_number(jobj, "P" #port #pin, \
31+
(GPIO_PORT##port | GPIO_PIN##pin));
32+
33+
#define SET_GPIO_CONSTANT_PORT(port) \
34+
SET_GPIO_CONSTANT(port, 0); \
35+
SET_GPIO_CONSTANT(port, 1); \
36+
SET_GPIO_CONSTANT(port, 2); \
37+
SET_GPIO_CONSTANT(port, 3); \
38+
SET_GPIO_CONSTANT(port, 4); \
39+
SET_GPIO_CONSTANT(port, 5); \
40+
SET_GPIO_CONSTANT(port, 6); \
41+
SET_GPIO_CONSTANT(port, 7); \
42+
SET_GPIO_CONSTANT(port, 8); \
43+
SET_GPIO_CONSTANT(port, 9); \
44+
SET_GPIO_CONSTANT(port, 10); \
45+
SET_GPIO_CONSTANT(port, 11); \
46+
SET_GPIO_CONSTANT(port, 12); \
47+
SET_GPIO_CONSTANT(port, 13); \
48+
SET_GPIO_CONSTANT(port, 14); \
49+
SET_GPIO_CONSTANT(port, 15);
50+
51+
SET_GPIO_CONSTANT_PORT(A);
52+
SET_GPIO_CONSTANT_PORT(B);
53+
SET_GPIO_CONSTANT_PORT(C);
54+
SET_GPIO_CONSTANT_PORT(D);
55+
SET_GPIO_CONSTANT_PORT(E);
56+
57+
SET_GPIO_CONSTANT(H, 0);
58+
SET_GPIO_CONSTANT(H, 1);
59+
60+
#undef SET_GPIO_CONSTANT_PORT
61+
#undef SET_GPIO_CONSTANT
62+
}
63+
64+
#endif /* ENABLE_MODULE_GPIO */
65+
66+
67+
#if ENABLE_MODULE_PWM
68+
69+
static void iotjs_pin_initialize_pwm(jerry_value_t jobj) {
70+
unsigned int timer_bit;
71+
72+
// Set PWM pin from configuration bits of nuttx.
73+
// PWM pin name is "PWM(timer).CH(channel)_(n)".
74+
#define SET_GPIO_CONSTANT(timer, channel, order) \
75+
timer_bit = (GPIO_TIM##timer##_CH##channel##OUT_##order); \
76+
timer_bit |= (SYSIO_TIMER_NUMBER(timer)); \
77+
iotjs_jval_set_property_number(jtim##timer, "CH" #channel "_" #order, \
78+
timer_bit);
79+
80+
#define SET_GPIO_CONSTANT_CHANNEL(timer, channel) \
81+
SET_GPIO_CONSTANT(timer, channel, 1);
82+
83+
#define SET_GPIO_CONSTANT_TIM(timer) \
84+
jerry_value_t jtim##timer = jerry_create_object(); \
85+
iotjs_jval_set_property_jval(jobj, "PWM" #timer, jtim##timer);
86+
87+
88+
#define SET_GPIO_CONSTANT_TIM_1(timer) \
89+
SET_GPIO_CONSTANT_TIM(timer); \
90+
SET_GPIO_CONSTANT_CHANNEL(timer, 1);
91+
92+
SET_GPIO_CONSTANT_TIM_1(1);
93+
jerry_release_value(jtim1);
94+
95+
SET_GPIO_CONSTANT_TIM_1(2);
96+
jerry_release_value(jtim2);
97+
98+
SET_GPIO_CONSTANT_TIM_1(3);
99+
jerry_release_value(jtim3);
100+
101+
SET_GPIO_CONSTANT_TIM_1(4);
102+
jerry_release_value(jtim4);
103+
104+
#undef SET_GPIO_CONSTANT_TIM_1
105+
#undef SET_GPIO_CONSTANT_TIM_2
106+
#undef SET_GPIO_CONSTANT_TIM
107+
#undef SET_GPIO_CONSTANT_CHANNEL
108+
#undef SET_GPIO_CONSTANT
109+
}
110+
111+
#endif /* ENABLE_MODULE_PWM */
112+
113+
114+
void iotjs_stm32f7nucleo_pin_initialize(jerry_value_t jobj) {
115+
jerry_value_t jpin = jerry_create_object();
116+
iotjs_jval_set_property_jval(jobj, "pin", jpin);
117+
118+
#if ENABLE_MODULE_GPIO
119+
iotjs_pin_initialize_gpio(jpin);
120+
#endif /* ENABLE_MODULE_GPIO */
121+
122+
#if ENABLE_MODULE_PWM
123+
iotjs_pin_initialize_pwm(jpin);
124+
#endif /* ENABLE_MODULE_PWM */
125+
126+
jerry_release_value(jpin);
127+
}
128+
129+
22130
#endif // __NUTTX__

0 commit comments

Comments
 (0)