1 /* 2 * Copyright (c) 2022 FuZhou Lockzhiner Electronic Co., Ltd. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 /** 17 * @addtogroup Lockzhiner 18 * 19 * @file pwm.h 20 */ 21 22 #ifndef LZ_HARDWARE_PWM_H 23 #define LZ_HARDWARE_PWM_H 24 typedef void (*PwmIsrFunc) (void *arg); 25 26 typedef enum _LZ_HARDWARE_PWM_CHN { 27 LZ_HARDWARE_PWM_CHN0 = 0, 28 LZ_HARDWARE_PWM_CHN1, 29 LZ_HARDWARE_PWM_CHN2, 30 LZ_HARDWARE_PWM_CHN3, 31 LZ_HARDWARE_PWM_MAX_CHANNEL, 32 } LZ_HARDWARE_PWM_CHN; 33 34 #define LZ_HARDWARE_PWM_POLARITY_INVERTED (1 << 0) 35 36 typedef enum _LZ_HARDWARE_PWM_CMD { 37 LZ_HARDWARE_PWM_CMD_ENABLE = (128 + 0), 38 LZ_HARDWARE_PWM_CMD_DISABLE, 39 LZ_HARDWARE_PWM_CMD_SET, 40 LZ_HARDWARE_PWM_CMD_SET_ONESHOT, 41 } LZ_HARDWARE_PWM_CMD; 42 43 typedef enum _LZ_HARDWARE_PWM_MODE { 44 PWM_ONESHOT = 0, 45 PWM_CONTINUOUS, 46 PWM_CAPTURE, 47 } LzPwmMode; 48 49 typedef struct _LZ_HARDWARE_PWM_CONFIG { 50 uint8_t channel; /* 0-3 */ 51 uint32_t period; /* unit:ns 1ns~4.29s:1Ghz~0.23hz */ 52 uint32_t pulse; /* unit:ns (pulse<=period) */ 53 uint8_t mode; /* LzPwmMode */ 54 bool polarity; /* invert or not */ 55 } PwmConfig; 56 57 typedef struct _LZ_HARDWARE_PWM_ONESHOT_CONFIG { 58 PwmConfig config; 59 uint32_t count; 60 } LZ_HARDWARE_PWM_ONESHOT_CONFIG; 61 62 typedef struct _LZ_HARDWARE_PWM_CAPTURE_CONFIG { 63 uint32_t period; 64 bool pol; 65 bool active; 66 } LZ_HARDWARE_PWM_CAPTURE_CONFIG; 67 68 typedef struct _LZ_HARDWARE_PWM_ENABLED_CONFIG { 69 uint8_t channel; /* 0-3 */ 70 LzPwmMode mode; 71 } LZ_HARDWARE_PWM_ENABLED_CONFIG; 72 /** 73 * @brief Initializes a PWM device. 74 * 75 * @param port Indicates the port number of the PWM device. 76 * @return Returns {@link LZ_HARDWARE_SUCCESS} if the PWM device is initialized; 77 * returns {@link LZ_HARDWARE_FAILURE} otherwise. For details about other return values, see the chip description. 78 */ 79 unsigned int LzPwmInit(unsigned int port); 80 81 /** 82 * @brief Deinitializes a PWM device. 83 * 84 * @param port Indicates the port number of the PWM device. 85 * @return Returns {@link LZ_HARDWARE_SUCCESS} if the PWM device is deinitialized; 86 * returns {@link LZ_HARDWARE_FAILURE} otherwise. For details about other return values, see the chip description. 87 */ 88 unsigned int LzPwmDeinit(unsigned int port); 89 90 /** 91 * @brief Starts PWM signal output from a specified port based on the given output frequency and duty cycle. 92 * 93 * 94 * 95 * @param port Indicates the port number of the PWM device. 96 * @param duty Indicates the duty cycle for PWM signal output. The value ranges from 1 to 99. 97 * @param cycle Indicates the cycle for PWM signal output. 98 * @return Returns {@link LZ_HARDWARE_SUCCESS} if the PWM signal output is started; 99 * returns {@link LZ_HARDWARE_FAILURE} otherwise. For details about other return values, see the chip description. 100 */ 101 unsigned int LzPwmStart(unsigned int port, unsigned int duty, unsigned int cycle); 102 103 /** 104 * @brief Stops PWM signal output from a specified port. 105 * 106 * @param port Indicates the port number of the PWM device. 107 * @return Returns {@link LZ_HARDWARE_SUCCESS} if the PWM signal output is stopped; 108 * returns {@link LZ_HARDWARE_FAILURE} otherwise. For details about other return values, see the chip description. 109 */ 110 unsigned int LzPwmStop(unsigned int port); 111 112 /** 113 * @brief Enables the interrupt feature for a PWM device. 114 * 115 * This function can be used to set the interrupt type, interrupt polarity, and interrupt callback for a PWM device. 116 * 117 * @param port Indicates the port number of the PWM device. 118 * @param func Indicates the interrupt callback function. 119 * @param arg Indicates the pointer to the argument used in the interrupt callback function. 120 * @return Returns {@link LZ_HARDWARE_SUCCESS} if the interrupt feature is enabled; 121 * returns {@link LZ_HARDWARE_FAILURE} otherwise. For details about other return values, see the chip description. 122 */ 123 unsigned int LzPwmRegisterIsrFunc(unsigned int port, PwmIsrFunc func, void *arg); 124 125 /** 126 * @brief Disables the interrupt feature for a PWM device. 127 * 128 * @param port Indicates the port number of the PWM device. 129 * @return Returns {@link LZ_HARDWARE_SUCCESS} if the interrupt feature is disabled; 130 * returns {@link LZ_HARDWARE_FAILURE} otherwise. For details about other return values, see the chip description. 131 */ 132 unsigned int LzPwmUnregisterIsrFunc(unsigned int port); 133 134 /** 135 * @brief Enable the interrupt for a PWM device. 136 * 137 * @param port Indicates the port number of the PWM device. 138 * @return Returns {@link LZ_HARDWARE_SUCCESS} if the interrupt feature is masked; 139 * returns {@link LZ_HARDWARE_FAILURE} otherwise. For details about other return values, see the chip description. 140 */ 141 unsigned int LzPwmEnableIsr(unsigned int port); 142 143 /** 144 * @brief Disable the interrupt for a PWM device. 145 * 146 * @param port Indicates the port number of the PWM device. 147 * @return Returns {@link LZ_HARDWARE_SUCCESS} if the interrupt feature is masked; 148 * returns {@link LZ_HARDWARE_FAILURE} otherwise. For details about other return values, see the chip description. 149 */ 150 unsigned int LzPwmDisableIsr(unsigned int port); 151 152 /** 153 * @brief SET the MODE for a PWM device. 154 * 155 * @param port Indicates the port number of the PWM device. 156 * @param Working mode of the PWM device. 157 * @return Returns {@link LZ_HARDWARE_SUCCESS} if the interrupt feature is masked; 158 * returns {@link LZ_HARDWARE_FAILURE} otherwise. For details about other return values, see the chip description. 159 */ 160 unsigned int LzPwmSetMode(unsigned int port, LzPwmMode mode); 161 162 #endif 163 164