flipperzero-firmware/lib/cyfral/cyfral_emulator.h
あく 68a3f6b4b7
[FL-873] Add F5 target, lp5562 driver and api-hal-light (#347)
* Add F5 target, lp5562 driver and api-hal-light. Update api-usage, switch to F5 by default.
* API HAL: add i2c and hardware version api. Dolphin: show hardware version.
* OTP version generator and flashing utility.
* Assets script: fix code formatting
* Backport F5 changes to F4
* F4: disable insomnia, prevent damage to BLE RX path
* F5 HAL API Light: remove magic delay to fix magic BLE
* Dolphin: HW target validation on start
* invert RSSI indication in sub-1
* API HAL: rename board to body in version api
* Gpio tester: detach and release viewport on exit

Co-authored-by: aanper <mail@s3f.ru>
2021-02-18 15:49:32 +03:00

96 lines
2.2 KiB
C++

#pragma once
#include <furi.h>
#include <api-hal.h>
class CyfralTiming {
public:
constexpr static const uint8_t ZERO_HIGH = 50;
constexpr static const uint8_t ZERO_LOW = 70;
constexpr static const uint8_t ONE_HIGH = 100;
constexpr static const uint8_t ONE_LOW = 70;
};
class CyfralEmulator {
private:
void send_nibble(uint8_t nibble);
void send_byte(uint8_t data);
inline void send_bit(bool bit);
const GpioPin* emulate_pin_record;
public:
CyfralEmulator(const GpioPin* emulate_pin);
~CyfralEmulator();
void send(uint8_t* data, uint8_t count = 1, uint8_t repeat = 1);
void start(void);
void stop(void);
};
// 7 = 0 1 1 1
// B = 1 0 1 1
// D = 1 1 0 1
// E = 1 1 1 0
void CyfralEmulator::send_nibble(uint8_t nibble) {
for(uint8_t i = 0; i < 4; i++) {
bool bit = nibble & (0b1000 >> i);
send_bit(bit);
}
}
void CyfralEmulator::send_byte(uint8_t data) {
for(uint8_t i = 0; i < 8; i++) {
bool bit = data & (0b10000000 >> i);
send_bit(bit);
}
}
void CyfralEmulator::send_bit(bool bit) {
if(!bit) {
gpio_write(&ibutton_gpio, false);
delay_us(CyfralTiming::ZERO_LOW);
gpio_write(&ibutton_gpio, true);
delay_us(CyfralTiming::ZERO_HIGH);
gpio_write(&ibutton_gpio, false);
delay_us(CyfralTiming::ZERO_LOW);
} else {
gpio_write(&ibutton_gpio, true);
delay_us(CyfralTiming::ONE_HIGH);
gpio_write(&ibutton_gpio, false);
delay_us(CyfralTiming::ONE_LOW);
}
}
CyfralEmulator::CyfralEmulator(const GpioPin* emulate_pin) {
emulate_pin_record = emulate_pin;
}
CyfralEmulator::~CyfralEmulator() {
}
void CyfralEmulator::send(uint8_t* data, uint8_t count, uint8_t repeat) {
osKernelLock();
__disable_irq();
for(uint8_t i = 0; i < repeat; i++) {
// start sequence
send_nibble(0x01);
// send data
for(uint8_t i = 0; i < count; i++) {
send_byte(data[i]);
}
}
__enable_irq();
osKernelUnlock();
}
void CyfralEmulator::start(void) {
gpio_init(emulate_pin_record, GpioModeOutputOpenDrain);
gpio_write(emulate_pin_record, false);
}
void CyfralEmulator::stop(void) {
gpio_init(emulate_pin_record, GpioModeAnalog);
}