1 /*
2  * Copyright (c) 2019 - 2020, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  *    list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  *    contributors may be used to endorse or promote products derived from this
17  *    software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <nrfx_twi_twim.h>
33 
34 #if NRFX_CHECK(NRFX_TWI_ENABLED) || NRFX_CHECK(NRFX_TWIM_ENABLED)
35 
36 #include <hal/nrf_gpio.h>
37 
38 #define TWI_TWIM_PIN_CONFIGURE(_pin) nrf_gpio_cfg((_pin),                     \
39                                                   NRF_GPIO_PIN_DIR_OUTPUT,    \
40                                                   NRF_GPIO_PIN_INPUT_CONNECT, \
41                                                   NRF_GPIO_PIN_PULLUP,        \
42                                                   NRF_GPIO_PIN_S0D1,          \
43                                                   NRF_GPIO_PIN_NOSENSE)
44 
nrfx_twi_twim_bus_recover(uint32_t scl_pin,uint32_t sda_pin)45 nrfx_err_t nrfx_twi_twim_bus_recover(uint32_t scl_pin, uint32_t sda_pin)
46 {
47     nrf_gpio_pin_set(scl_pin);
48     nrf_gpio_pin_set(sda_pin);
49 
50     TWI_TWIM_PIN_CONFIGURE(scl_pin);
51     TWI_TWIM_PIN_CONFIGURE(sda_pin);
52     NRFX_DELAY_US(4);
53 
54     for (uint8_t i = 0; i < 9; i++)
55     {
56         if (nrf_gpio_pin_read(sda_pin))
57         {
58             break;
59         }
60         else
61         {
62             // Pulse CLOCK signal
63             nrf_gpio_pin_clear(scl_pin);
64             NRFX_DELAY_US(4);
65             nrf_gpio_pin_set(scl_pin);
66             NRFX_DELAY_US(4);
67         }
68     }
69 
70     // Generate a STOP condition on the bus
71     nrf_gpio_pin_clear(sda_pin);
72     NRFX_DELAY_US(4);
73     nrf_gpio_pin_set(sda_pin);
74     NRFX_DELAY_US(4);
75 
76     if (nrf_gpio_pin_read(sda_pin))
77     {
78         return NRFX_SUCCESS;
79     }
80     else
81     {
82         return NRFX_ERROR_INTERNAL;
83     }
84 }
85 
86 #endif // NRFX_CHECK(NRFX_TWI_ENABLED) || NRFX_CHECK(NRFX_TWIM_ENABLED)
87