2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "st_things.h"
19 #include "resource/resource_servo_motor.h"
21 #define ARTIK_PWM_CHIPID 0
22 #define ARTIK_PWM_PIN 2
24 #define MILLI_SECOND (1000000)
26 peripheral_pwm_h g_pwm_h = NULL;
28 static int _get_duty_cycle(door_state_e mode, int *duty_cycle)
30 if ((mode == DOOR_STATE_OPENED) || (mode == DOOR_STATE_OPENING))
31 *duty_cycle = 2 * MILLI_SECOND; // CLOCKWISE
32 else if ((mode == DOOR_STATE_CLOSE) || (mode == DOOR_STATE_CLOSING))
33 *duty_cycle = 1 * MILLI_SECOND; // COUNTER-CLOCKWISE
35 ERR("get_duty_cycle unknown mode=[%d]", mode);
39 INFO("get_duty_cycle mode=[%d:%s] : duty_cycle [%d] ms",
41 mode == DOOR_STATE_OPENED ? "OPENED " :
42 (mode == DOOR_STATE_CLOSE ? "CLOSE " :
43 (mode == DOOR_STATE_CLOSING ? "CLOSING" : "OPENING")),
48 peripheral_error_e resource_motor_driving(door_state_e mode)
50 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
52 int chip = ARTIK_PWM_CHIPID; // Chip 0
53 int pin = ARTIK_PWM_PIN; // Pin 2
55 int period = 20 * MILLI_SECOND;
59 ret = _get_duty_cycle(mode, &duty_cycle);
61 ERR("_get_duty_cycle unknown mode=[%d]", mode);
62 return PERIPHERAL_ERROR_INVALID_PARAMETER;
66 // Opening a PWM Handle : The chip and pin parameters required for this function must be set
67 if ((ret = peripheral_pwm_open(chip, pin, &g_pwm_h)) != PERIPHERAL_ERROR_NONE ) {
68 ERR("peripheral_pwm_open() failed!![%d]", ret);
73 // Setting the Period : sets the period to 20 milliseconds. The unit is nanoseconds
74 if ((ret = peripheral_pwm_set_period(g_pwm_h, period)) != PERIPHERAL_ERROR_NONE) {
75 ERR("peripheral_pwm_set_period() failed!![%d]", ret);
79 // Setting the Duty Cycle : sets the duty cycle to 1~2 milliseconds. The unit is nanoseconds
80 if ((ret = peripheral_pwm_set_duty_cycle(g_pwm_h, duty_cycle)) != PERIPHERAL_ERROR_NONE) {
81 ERR("peripheral_pwm_set_duty_cycle() failed!![%d]", ret);
85 // Enabling Repetition
86 if ((ret = peripheral_pwm_set_enabled(g_pwm_h, enable)) != PERIPHERAL_ERROR_NONE) {
87 ERR("peripheral_pwm_set_enabled() failed!![%d]", ret);
91 if (g_pwm_h != NULL) {
92 // Closing a PWM Handle : close a PWM handle that is no longer used,
93 if ((ret = peripheral_pwm_close(g_pwm_h)) != PERIPHERAL_ERROR_NONE ) {
94 ERR("peripheral_pwm_close() failed!![%d]", ret);