unleashed-firmware/lib/onewire/one_wire_gpio.h

130 lines
2.6 KiB
C
Raw Normal View History

2020-11-16 11:21:58 +00:00
#pragma once
[FL-140] Core api dynamic records (#296) * SYSTEM: tickless mode with deep sleep. * Move FreeRTOS ticks to lptim2 * API: move all sumbodules init routines to one place. Timebase: working lptim2 at tick source. * API Timebase: lp-timer routines, timer access safe zones prediction and synchronization. FreeRTOS: adjust configuration for tickless mode. * NFC: support for tickless mode. * API Timebase: improve tick error handling in IRQ. Apploader: use insomnia mode to run applications. * BLE: prevent sleep while core2 starting * HAL: nap while in insomnia mode * init records work * try to implement record delete * tests and flapp * flapp subsystem * new core functions to get app stat, simplify core code * fix thread termination * add strdup to core * fix tests * Refactoring: remove all unusued parts, update API usage, aggreagate API sources and headers, new record storage * Refactoring: update furi record api usage, cleanup code * Fix broken merge for freertos apps * Core, Target: fix compilation warnings * Drop firmware target local * HAL Timebase, Power, Clock: semaphore guarded access to clock and power modes, better sleep mode. * SD-Filesystem: wait for all deps to arrive before adding widget. Core, BLE: disable debug dump to serial. * delete old app example-ipc * delete old app fatfs list * fix strobe app, add input header * delete old display driver * comment old app qr-code * fix sd-card test, add forced widget update * remove unused new core test * increase heap to 128k * comment and assert old core tests * fix syntax Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2021-01-20 16:09:26 +00:00
#include <furi.h>
2020-11-16 11:21:58 +00:00
#include "one_wire_timings.h"
class OneWireGpio {
private:
const GpioPin* gpio;
2020-11-16 11:21:58 +00:00
public:
OneWireGpio(const GpioPin* one_wire_gpio);
2020-11-16 11:21:58 +00:00
~OneWireGpio();
bool reset(void);
bool read_bit(void);
uint8_t read(void);
void read_bytes(uint8_t* buf, uint16_t count);
void write_bit(bool value);
void write(uint8_t value);
void start(void);
void stop(void);
};
OneWireGpio::OneWireGpio(const GpioPin* one_wire_gpio) {
2020-11-16 11:21:58 +00:00
gpio = one_wire_gpio;
}
OneWireGpio::~OneWireGpio() {
stop();
}
void OneWireGpio::start(void) {
gpio_init(gpio, GpioModeOutputOpenDrain);
}
void OneWireGpio::stop(void) {
gpio_init(gpio, GpioModeAnalog);
}
bool OneWireGpio::reset(void) {
uint8_t r;
uint8_t retries = 125;
// wait until the gpio is high
gpio_write(gpio, true);
do {
if(--retries == 0) return 0;
delay_us(2);
} while(!gpio_read(gpio));
// pre delay
delay_us(OneWireTiming::RESET_DELAY_PRE);
// drive low
gpio_write(gpio, false);
delay_us(OneWireTiming::RESET_DRIVE);
// release
gpio_write(gpio, true);
delay_us(OneWireTiming::RESET_RELEASE);
// read and post delay
r = !gpio_read(gpio);
delay_us(OneWireTiming::RESET_DELAY_POST);
return r;
}
bool OneWireGpio::read_bit(void) {
bool result;
// drive low
gpio_write(gpio, false);
delay_us(OneWireTiming::READ_DRIVE);
// release
gpio_write(gpio, true);
delay_us(OneWireTiming::READ_RELEASE);
// read and post delay
result = gpio_read(gpio);
delay_us(OneWireTiming::READ_DELAY_POST);
return result;
}
void OneWireGpio::write_bit(bool value) {
if(value) {
// drive low
gpio_write(gpio, false);
delay_us(OneWireTiming::WRITE_1_DRIVE);
// release
gpio_write(gpio, true);
delay_us(OneWireTiming::WRITE_1_RELEASE);
} else {
// drive low
gpio_write(gpio, false);
delay_us(OneWireTiming::WRITE_0_DRIVE);
// release
gpio_write(gpio, true);
delay_us(OneWireTiming::WRITE_0_RELEASE);
}
}
uint8_t OneWireGpio::read(void) {
uint8_t result = 0;
for(uint8_t bitMask = 0x01; bitMask; bitMask <<= 1) {
if(read_bit()) {
result |= bitMask;
}
}
return result;
}
void OneWireGpio::read_bytes(uint8_t* buffer, uint16_t count) {
for(uint16_t i = 0; i < count; i++) {
buffer[i] = read();
}
}
void OneWireGpio::write(uint8_t value) {
uint8_t bitMask;
for(bitMask = 0x01; bitMask; bitMask <<= 1) {
write_bit((bitMask & value) ? 1 : 0);
}
}