1 #include <twr_onewire_gpio.h>
2 #include <twr_system.h>
7 static bool _twr_onewire_gpio_init(
void *ctx);
8 static bool _twr_onewire_gpio_enable(
void *ctx);
9 static bool _twr_onewire_gpio_disable(
void *ctx);
10 static bool _twr_onewire_gpio_reset(
void *ctx);
11 static void _twr_onewire_gpio_write_bit(
void *ctx, uint8_t bit);
12 static uint8_t _twr_onewire_gpio_read_bit(
void *ctx);
13 static void _twr_onewire_gpio_write_byte(
void *ctx, uint8_t
byte);
14 static uint8_t _twr_onewire_gpio_read_byte(
void *ctx);
15 static bool _twr_onewire_gpio_search_next(
void *ctx,
twr_onewire_t *onewire, uint64_t *device_number);
17 static const twr_onewire_driver_t _twr_onewire_gpio_driver =
19 .init = _twr_onewire_gpio_init,
20 .enable = _twr_onewire_gpio_enable,
21 .disable = _twr_onewire_gpio_disable,
22 .reset = _twr_onewire_gpio_reset,
23 .write_bit = _twr_onewire_gpio_write_bit,
24 .read_bit = _twr_onewire_gpio_read_bit,
25 .write_byte = _twr_onewire_gpio_write_byte,
26 .read_byte = _twr_onewire_gpio_read_byte,
27 .search_next = _twr_onewire_gpio_search_next
35 const twr_onewire_driver_t *twr_onewire_gpio_det_driver(
void)
37 return &_twr_onewire_gpio_driver;
40 static bool _twr_onewire_gpio_init(
void *ctx)
53 static bool _twr_onewire_gpio_enable(
void *ctx)
56 twr_system_pll_enable();
62 static bool _twr_onewire_gpio_disable(
void *ctx)
67 twr_system_pll_disable();
71 static bool _twr_onewire_gpio_reset(
void *ctx)
76 uint8_t retries = 125;
113 while (i && --retries);
122 static void _twr_onewire_gpio_write_bit(
void *ctx, uint8_t bit)
151 static uint8_t _twr_onewire_gpio_read_bit(
void *ctx)
182 static void _twr_onewire_gpio_write_byte(
void *ctx, uint8_t
byte)
184 for (uint8_t i = 0; i < 8; i++)
186 _twr_onewire_gpio_write_bit(ctx,
byte & 0x01);
191 static uint8_t _twr_onewire_gpio_read_byte(
void *ctx)
194 for (uint8_t i = 0; i < 8; i++)
196 byte |= (_twr_onewire_gpio_read_bit(ctx) << i);
201 static bool _twr_onewire_gpio_search_next(
void *ctx,
twr_onewire_t *onewire, uint64_t *device_number)
203 bool search_result =
false;
204 uint8_t id_bit_number;
205 uint8_t last_zero, rom_byte_number;
206 uint8_t id_bit, cmp_id_bit;
207 uint8_t rom_byte_mask, search_direction;
215 if (!_twr_onewire_gpio_reset(ctx))
221 _twr_onewire_gpio_write_byte(ctx, 0xf0);
226 id_bit = _twr_onewire_gpio_read_bit(ctx);
227 cmp_id_bit = _twr_onewire_gpio_read_bit(ctx);
230 if ((id_bit == 1) && (cmp_id_bit == 1))
237 if (id_bit != cmp_id_bit)
240 search_direction = id_bit;
245 if (id_bit_number < onewire->_last_discrepancy)
247 search_direction = ((onewire->_last_rom_no[rom_byte_number] & rom_byte_mask) > 0);
252 search_direction = (id_bit_number == onewire->_last_discrepancy);
256 if (search_direction == 0)
258 last_zero = id_bit_number;
263 onewire->_last_family_discrepancy = last_zero;
269 if (search_direction == 1)
271 onewire->_last_rom_no[rom_byte_number] |= rom_byte_mask;
275 onewire->_last_rom_no[rom_byte_number] &= ~rom_byte_mask;
279 _twr_onewire_gpio_write_bit(ctx, search_direction);
286 if (rom_byte_mask == 0)
293 while (rom_byte_number < 8);
296 if (!(id_bit_number < 65))
299 onewire->_last_discrepancy = last_zero;
302 if (onewire->_last_discrepancy == 0)
304 onewire->_last_device_flag =
true;
307 search_result = !onewire->_last_rom_no[0] ? false :
true;
310 if (search_result &&
twr_onewire_crc8(onewire->_last_rom_no,
sizeof(onewire->_last_rom_no), 0x00) != 0)
312 search_result =
false;
317 memcpy(device_number, onewire->_last_rom_no,
sizeof(onewire->_last_rom_no));
321 _twr_onewire_gpio_reset(ctx);
324 return search_result;
void twr_gpio_set_output(twr_gpio_channel_t channel, int state)
Set output state for GPIO channel.
void twr_gpio_set_pull(twr_gpio_channel_t channel, twr_gpio_pull_t pull)
Set pull-up/pull-down configuration for GPIO channel.
void twr_gpio_init(twr_gpio_channel_t channel)
Initialize GPIO channel.
twr_gpio_channel_t
GPIO channels.
int twr_gpio_get_input(twr_gpio_channel_t channel)
Get input state for GPIO channel.
void twr_gpio_set_mode(twr_gpio_channel_t channel, twr_gpio_mode_t mode)
Set mode of operation for GPIO channel.
@ TWR_GPIO_MODE_INPUT
GPIO channel operates as input.
@ TWR_GPIO_MODE_OUTPUT
GPIO channel operates as output.
@ TWR_GPIO_MODE_ANALOG
GPIO channel operates in analog mode.
@ TWR_GPIO_PULL_NONE
GPIO channel has no pull-up/pull-down.
void twr_irq_disable(void)
Disable interrupt requests globally (call can be nested)
void twr_irq_enable(void)
Enable interrupt requests globally (call can be nested)
struct twr_onewire_t twr_onewire_t
1-Wire instance
void twr_onewire_gpio_init(twr_onewire_t *onewire, twr_gpio_channel_t channel)
Initialize 1-Wire.
bool twr_onewire_init(twr_onewire_t *self, const twr_onewire_driver_t *driver, void *driver_ctx)
Initialize 1-Wire.
uint8_t twr_onewire_crc8(const void *buffer, size_t length, uint8_t crc)
Calculate 8-bit CRC.
void twr_timer_init(void)
Initialize timer.
void twr_timer_delay(uint16_t microseconds)
Relative delay.
void twr_timer_stop(void)
Stop timer.
void twr_timer_start(void)
Start timer.