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
16 #include "gpio_map.h"
17 #include "gpio_driver.h"
18 #include <driver/gpio.h>
19 #include <driver/uart.h>
20 #include "bk_sys_ctrl.h"
21
22 #if CONFIG_ATE
23
24 //if modify to 1, should change the value of gpio_cfg to {GPIO_INPUT_ENABLE, GPIO_PULL_DOWN_EN} and confirm HW change the GPIO default output level.
25 #define ATE_ENABLE_GPIO_LEVEL (0)
26
27 static bool s_ate_enabled = false;
28
29 //NOTICE:Now we re-use the GPIO which is used by UART.
ate_get_gpio_id(void)30 static gpio_id_t ate_get_gpio_id(void)
31 {
32 return bk_uart_get_ate_detect_gpio();
33 }
34
ate_gpio_init(void)35 static bk_err_t ate_gpio_init(void)
36 {
37 bk_err_t ret = BK_ERR_BUSY;
38 bool gpio_value = 0;
39 gpio_id_t gpio_id;
40 const gpio_config_t gpio_cfg = {GPIO_INPUT_ENABLE, GPIO_PULL_UP_EN};
41
42 gpio_id = ate_get_gpio_id();
43
44 //bootrom or bootloader may have inited this GPIO, so release it firstly.
45 gpio_dev_unmap(gpio_id);
46
47 //set to input and check the GPIO input level
48 ret = bk_gpio_set_config(gpio_id, &gpio_cfg);
49 if(ret == BK_OK)
50 {
51 gpio_value = bk_gpio_get_input(gpio_id);
52 if(gpio_value == ATE_ENABLE_GPIO_LEVEL)
53 s_ate_enabled = true;
54 }
55 if(s_ate_enabled == true)
56 {
57 rf_ps_enable_clear();
58 }
59 return ret;
60 }
61
ate_is_enabled(void)62 bool ate_is_enabled(void)
63 {
64 return s_ate_enabled;
65 }
66
67 //This function should be called before UART init, or caused UART can't work.
bk_ate_init(void)68 int bk_ate_init(void)
69 {
70 ate_gpio_init();
71 return BK_OK;
72 }
73
bk_ate_deinit(void)74 int bk_ate_deinit(void)
75 {
76 return BK_OK;
77 }
78 #else
ate_is_enabled(void)79 bool ate_is_enabled(void)
80 {
81 return false;
82 }
83 #endif
84
85
86