1 // Copyright (C) 2022 Beken Corporation 2 // 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 #pragma once 16 #include <common/bk_include.h> 17 #include <driver/gpio_types.h> 18 19 #ifdef __cplusplus 20 extern "C" { 21 #endif 22 23 /* @brief Overview about this API header 24 * 25 */ 26 27 /** 28 * @brief GPIO API 29 * @defgroup bk_api_gpio GPIO API group 30 * @{ 31 */ 32 33 34 /** 35 * @brief Init the GPIO driver 36 * 37 * This API init the resoure common to all GPIO channels: 38 * - Init GPIO driver control memory 39 * 40 * This API should be called before any other GPIO APIs. 41 * 42 * @return 43 * - BK_OK: succeed 44 * - others: other errors. 45 */ 46 bk_err_t bk_gpio_driver_init(void); 47 48 49 /** 50 * @brief Deinit the GPIO driver 51 * 52 * This API free all resource related to GPIO and power down all GPIO channels. 53 * 54 * @return 55 * - BK_OK: succeed 56 * - others: other errors. 57 */ 58 bk_err_t bk_gpio_driver_deinit(void); 59 60 61 /** 62 * @brief enable GPIO output mode 63 * 64 * 65 * @return 66 * - BK_OK: succeed 67 * - BK_ERR_GPIO_CHAN_ID: invalid GPIO channel 68 * - others: other errors. 69 */ 70 bk_err_t bk_gpio_enable_output(gpio_id_t gpio_id); 71 72 73 /** 74 * @brief disable GPIO output mode 75 * 76 * 77 * @return 78 * - BK_OK: succeed 79 * - BK_ERR_GPIO_CHAN_ID: invalid GPIO channel 80 * - others: other errors. 81 */ 82 bk_err_t bk_gpio_disable_output(gpio_id_t gpio_id); 83 84 85 /** 86 * @brief enable GPIO input mode 87 * 88 * 89 * @return 90 * - BK_OK: succeed 91 * - BK_ERR_GPIO_CHAN_ID: invalid GPIO channel 92 * - others: other errors. 93 */ 94 bk_err_t bk_gpio_enable_input(gpio_id_t gpio_id); 95 96 97 /** 98 * @brief disable GPIO input mode 99 * 100 * 101 * @return 102 * - BK_OK: succeed 103 * - BK_ERR_GPIO_CHAN_ID: invalid GPIO channel 104 * - others: other errors. 105 */ 106 bk_err_t bk_gpio_disable_input(gpio_id_t gpio_id); 107 108 109 /** 110 * @brief enable GPIO pull mode 111 * 112 * 113 * @return 114 * - BK_OK: succeed 115 * - BK_ERR_GPIO_CHAN_ID: invalid GPIO channel 116 * - others: other errors. 117 */ 118 bk_err_t bk_gpio_enable_pull(gpio_id_t gpio_id); 119 120 121 /** 122 * @brief disable gpio pull mode 123 * 124 * 125 * @return 126 * - BK_OK: succeed 127 * - BK_ERR_GPIO_CHAN_ID: invalid GPIO channel 128 * - others: other errors. 129 */ 130 bk_err_t bk_gpio_disable_pull(gpio_id_t gpio_id); 131 132 133 /** 134 * @brief set GPIO as pull up mode 135 * 136 * 137 * @return 138 * - BK_OK: succeed 139 * - BK_ERR_GPIO_CHAN_ID: invalid GPIO channel 140 * - others: other errors. 141 */ 142 bk_err_t bk_gpio_pull_up(gpio_id_t gpio_id); 143 144 145 /** 146 * @brief set GPIO as pull down mode 147 * 148 * 149 * @return 150 * - BK_OK: succeed 151 * - BK_ERR_GPIO_CHAN_ID: invalid GPIO channel 152 * - others: other errors. 153 */ 154 bk_err_t bk_gpio_pull_down(gpio_id_t gpio_id); 155 156 157 /** 158 * @brief Config the GPIO mode 159 * 160 * This API config GPIO's mode 161 * - BK_OK: succeed 162 * - BK_ERR_GPIO_CHAN_ID: invalid GPIO channel 163 * - BK_ERR_GPIO_INVALID_MODE: invalid GPIO's io_mode/pull_mode 164 * - BK_ERR_GPIO_INTERNAL_USED:GPIO was map to another device 165 * - others: other errors. 166 */ 167 bk_err_t bk_gpio_set_config(gpio_id_t gpio_id, const gpio_config_t *config); 168 169 170 /** 171 * @brief Set the GPIO output high, 172 * 173 * 174 * @return 175 * - BK_OK: succeed 176 * - BK_ERR_GPIO_CHAN_ID: invalid GPIO channel 177 * - BK_ERR_GPIO_NOT_OUTPUT_MODE:GPIO not in output mode 178 179 * - others: other errors. 180 */ 181 bk_err_t bk_gpio_set_output_high(gpio_id_t gpio_id); 182 183 /** 184 * @brief Set the GPIO output low, 185 * 186 * 187 * @return 188 * - BK_OK: succeed 189 * - BK_ERR_GPIO_CHAN_ID: invalid GPIO channel 190 * - BK_ERR_GPIO_NOT_OUTPUT_MODE:GPIO not in output mode 191 * - others: other errors. 192 */ 193 194 bk_err_t bk_gpio_set_output_low(gpio_id_t gpio_id); 195 196 /** 197 * @brief Get the GPIO input value, 198 * 199 * This API get GPIO's input level: 0 :low_level 1:high_level. 200 * 201 * @return 202 * - input value 203 * - BK_ERR_GPIO_CHAN_ID: invalid GPIO channel 204 * - BK_ERR_GPIO_NOT_INPUT_MODE : GPIO is not input mode 205 * - others: other errors. 206 */ 207 bool bk_gpio_get_input(gpio_id_t gpio_id); 208 209 /** 210 * @brief Set the GPIO driver capacity. 211 * 212 * This API Set GPIO's output driver capacity which range is 0~3. 213 * 214 * @return 215 * - input value 216 * - BK_ERR_GPIO_CHAN_ID: invalid GPIO channel 217 * - BK_ERR_GPIO_NOT_INPUT_MODE : GPIO is not input mode 218 * - others: other errors. 219 */ 220 bool bk_gpio_set_capacity(gpio_id_t gpio_id, uint32 capacity); 221 222 /** 223 * @brief Config the GPIO intterrupt type mode when use gpio intterrupt mode, 224 * 225 * This API config all GPIO channels' intterrupt mode, the mode included in gpio_int_type_t. 226 * 227 * @return 228 * - BK_OK: succeed 229 * - BK_ERR_GPIO_CHAN_ID: invalid GPIO channel 230 * - BK_ERR_GPIO_INVALID_INT_TYPE: invalid GPIO int type 231 * - others: other errors. 232 */ 233 bk_err_t bk_gpio_set_interrupt_type(gpio_id_t gpio_id, gpio_int_type_t type); 234 235 /** 236 * @brief Enable GPIO intterrupt. 237 * 238 * 239 * @return 240 * - BK_OK: succeed 241 * - BK_ERR_GPIO_CHAN_ID: invalid GPIO channel 242 * - BK_ERR_GPIO_NOT_INPUT_MODE : GPIO is not input mode 243 * - others: other errors. 244 */ 245 bk_err_t bk_gpio_enable_interrupt(gpio_id_t id); 246 247 /** 248 * @brief Disable GPIO intterrupt. 249 * 250 * 251 * @return 252 * - BK_OK: succeed 253 * - BK_ERR_GPIO_CHAN_ID: invalid GPIO channel 254 * - others: other errors. 255 */ 256 bk_err_t bk_gpio_disable_interrupt(gpio_id_t id); 257 258 /** 259 * @brief Register the interrupt service routine for GPIO channel 260 * 261 * This API regist gpio isr callback function. 262 * 263 * @return 264 * - BK_OK: succeed 265 * - BK_ERR_GPIO_CHAN_ID: invalid gpio channel 266 * - others: other errors. 267 */ 268 bk_err_t bk_gpio_register_isr(gpio_id_t id, gpio_isr_t isr); 269 270 #if CONFIG_GPIO_DYNAMIC_WAKEUP_SUPPORT 271 /** 272 * @brief Register the GPIO channel to wakeup source with select int type. 273 * 274 * This API regist gpio to wakeup source. 275 * If the GPIO registered to wakeup source, the system at low voltage status 276 * can be wake up by the GPIO with selected int_type, and hold on the system 277 * at wakeup status some time. 278 * 279 * @return 280 * - BK_OK: succeed 281 * - BK_ERR_GPIO_CHAN_ID: invalid gpio channel 282 * - BK_ERR_GPIO_INVALID_INT_TYPE: invalid gpio int type 283 * - BK_ERR_GPIO_WAKESOURCE_OVER_MAX_CNT: too many gpio is register to wakeup source 284 * default max value is: CONFIG_GPIO_DYNAMIC_WAKEUP_SOURCE_MAX_CNT 285 * - others: other errors. 286 */ 287 bk_err_t bk_gpio_register_wakeup_source(gpio_id_t gpio_id, gpio_int_type_t int_type); 288 289 /** 290 * @brief Unregister the GPIO channel from wakeup source. 291 * 292 * This API unregist gpio from wakeup source. 293 * If the GPIO has registered to wakeup source, this API will unregister the gpio 294 * from wakeup source. 295 * If bk_gpio_register_wakeup_source failed with BK_ERR_GPIO_WAKESOURCE_OVER_MAX_CNT, 296 * it can call this API unregister one of gpio. 297 * 298 * @return 299 * - BK_OK: succeed 300 * - BK_ERR_GPIO_CHAN_ID: invalid gpio channel 301 * - others: other errors. 302 */ 303 bk_err_t bk_gpio_unregister_wakeup_source(gpio_id_t gpio_id); 304 305 /** 306 * @brief Get the awakened GPIO ID. 307 * 308 * This API is used to get the GPIO_ID of the current wakeup. 309 * If the GPIO ID is GPIO_NUM, please check that the registered interrupt wake up 310 * GPIO ID is correct.And after the GPIO initialization is complete, the wake up ID 311 * is registered 312 * 313 * @return 314 * - GPIO_ID: succeed 315 * - GPIO_NUM: error invalid gpio id 316 * 317 */ 318 gpio_id_t bk_gpio_get_wakeup_gpio_id(); 319 320 #else 321 /** 322 * @brief Register save all gpio reg value 323 * 324 * This API save all gpio reg value function. 325 * 326 * @return 327 * - BK_OK: succeed 328 * 329 * - others: other errors. 330 */ 331 bk_err_t bk_gpio_reg_save(uint32_t* gpio_cfg); 332 /** 333 * @brief Register restore all gpio reg value 334 * 335 * This API restore all gpio reg value function. 336 * 337 * @return 338 * - BK_OK: succeed 339 * - 340 * - others: other errors. 341 */ 342 bk_err_t bk_gpio_reg_restore(uint32_t* gpio_cfg); 343 /** 344 * @brief Register configue the gpio wakeup value 345 * 346 * This API configue the gpio wakeup value function. 347 * 348 * @return 349 * - BK_OK: succeed 350 * - B 351 * - others: other errors. 352 */ 353 bk_err_t bk_gpio_wakeup_enable(int64_t index, uint64_t type_l, uint64_t type_h); 354 355 /** 356 * @brief Register clear wake up interrupt 357 * 358 * This API clear wake up interrupt function. 359 * 360 * @return 361 * - BK_OK: succeed 362 * - B 363 * - others: other errors. 364 */ 365 bk_err_t bk_gpio_wakeup_interrupt_clear(); 366 #endif 367 368 /** 369 * @} 370 */ 371 372 #ifdef __cplusplus 373 } 374 #endif 375