1 // Copyright (C) 2022 Beken Corporation 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 #include "iot_errno.h" 15 #include "iot_pwm.h" 16 17 #include <driver/pwm.h> 18 19 #define BK_PWM_MAX 6 20 IoTPwmInit(unsigned int port)21unsigned int IoTPwmInit(unsigned int port) 22 { 23 if(port >= BK_PWM_MAX) 24 return IOT_FAILURE; 25 26 return IOT_SUCCESS; 27 } 28 IoTPwmDeinit(unsigned int port)29unsigned int IoTPwmDeinit(unsigned int port) 30 { 31 if(port >= BK_PWM_MAX) 32 return IOT_FAILURE; 33 34 return IOT_SUCCESS; 35 } 36 IoTPwmStart(unsigned int port,unsigned short duty,unsigned int freq)37unsigned int IoTPwmStart(unsigned int port, unsigned short duty, unsigned int freq) 38 { 39 unsigned int ret; 40 pwm_init_config_t config; 41 42 if(port >= BK_PWM_MAX || (freq == 0)) 43 return IOT_FAILURE; 44 45 config.period_cycle = 1000000 / freq; 46 config.duty_cycle = duty * config.period_cycle / 100; 47 ret = bk_pwm_init(port, &config); 48 if (ret) 49 return IOT_FAILURE; 50 51 ret = bk_pwm_start(port); 52 if (ret) 53 return IOT_FAILURE; 54 55 return IOT_SUCCESS; 56 } 57 IoTPwmStop(unsigned int port)58unsigned int IoTPwmStop(unsigned int port) 59 { 60 if(port >= BK_PWM_MAX) 61 return IOT_FAILURE; 62 63 bk_pwm_stop(port); 64 return IOT_SUCCESS; 65 } 66 67