* @brief Read a bit from the 1-Wire bus and return the value, with recovery time. * @param[in] bus Initialised bus instance. */
| 175 | * @param[in] bus Initialised bus instance. |
| 176 | */ |
| 177 | static int _read_bit(const OneWireBus * bus) |
| 178 | { |
| 179 | struct modGPIOConfigurationRecord *config = bus->config; |
| 180 | #if MODDEF_ONEWIRE_DIRECTGPIO |
| 181 | volatile IO_REG_TYPE *reg IO_REG_BASE_ATTR = PIN_TO_BASEREG(config->pin); |
| 182 | #endif |
| 183 | |
| 184 | modCriticalSectionBegin(); |
| 185 | #if MODDEF_ONEWIRE_DIRECTGPIO |
| 186 | DIRECT_MODE_OUTPUT(REG, PIN_TO_BITMASK(config->pin)); |
| 187 | #else |
| 188 | modGPIOSetMode(config, kModGPIOOutputOpenDrain); |
| 189 | #endif |
| 190 | modGPIOWrite(config, 0); // Drive DQ low |
| 191 | |
| 192 | ets_delay_us(bus->timing->A); |
| 193 | #if MODDEF_ONEWIRE_DIRECTGPIO |
| 194 | DIRECT_MODE_INPUT(reg, PIN_TO_BITMASK(config->pin)); // Release the bus |
| 195 | #else |
| 196 | modGPIOSetMode(config, kModGPIOInput); |
| 197 | #endif |
| 198 | ets_delay_us(bus->timing->E); |
| 199 | |
| 200 | int level = modGPIORead(config); |
| 201 | |
| 202 | modCriticalSectionEnd(); |
| 203 | |
| 204 | ets_delay_us(bus->timing->F); // Complete the timeslot and 10us recovery |
| 205 | |
| 206 | return level & 0x01; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * @brief Write 1-Wire data byte. |
no test coverage detected