unleashed-firmware/lib/onewire/one_wire_slave.cpp
SG 1daef3d025
new iButton app (#328)
* rename old ibutton app to ibutton-test

* more renames

* updated onewire library compilation condition

* add submenu_clean subroutine

* add index for submenu callback

* c++ guard for gui modules

* add released ibutton app

* fix the position of the submenu window if there are too few items

* iButton app basis

* negative icon position info

* fix submenu_clean subroutine

* add ibutton app to applications makefile

* add onewire key read routine to read mode

* rename mode to scene

* rename files and folder (mode to scene)

* rename ibutton view to view manager

* rename get_view to get_view_manager

* cpp guards

* key read, store and notify features

* syntax fix

* make iButtonScene functions pure virtual

* fix syntax

* add text store, add new scene (crc error)

* not a key scene

* syntax fix

* read success scene

* app, switching to the previous scene with the number of scenes to be skipped

* scene whith menu when key is readed

* fix font height calculation, fix offsets

* add key write scene

* view_dispatcher_remove_view subroutine

* generic pause/resume os methods

* fix furi_assert usage

* key store, worker

* fix pointer comparsion

* saved keys, saved key action scenes

* key delete/confirm delete scenes and routines

* use last input subsystem changes

* fix syntax

* fix new model usage in submenu

* fix includes

* use vibro pin

* use stored key name if valid

* emulate scene

* random name generator

* name and save readed key scenes, new icon

* fix icon position

* fix text scene exit

* fix naming, fix text placement, new info scene

* state-driven cyfral decoder

* better cyfral decoder

* better cyfral decoder

* one wire: search command set

* metakom decoder

* more key types

* add next scene to error scenes

* universal key reader

* use new key reader

* syntax fix

* warning fix

* byte input module template

* new thread and insomnia api usage

* New element: slightly rounded frame

* Use elements_slightly_rounded_frame in text input

* Gui test app: byte input usage

* Byte input module: data drawing and selection

* Byte input: comment currently unused fns

* remove volatile qualifier

* base byte input realisation

* App gui test: remove internal fns visibility

* Byne input, final version

* test install gcc-arm-none-eabi-10-2020-q4-major

* test install gcc-arm-none-eabi-10-2020-q4-major

* App iButton: byte input view managment

* App iButton: add key manually scenes

* App iButton: rename scenes, add popup timeout

* App iButton: use new scenes, new fn for rollback to specific prevous scene.

* App iButton: remove byte input view on app exit

* App iButton: edit key scene

* Module byte input: reduce swintch value to uint8_t

* Module byte input: switch from switch-case to if, unfortunately we need compile-time constants to use with switch

* Icons: new small arrows

* Module byte input: new arrangement of elements

* OneWire slave lib: fix deattach sequence

* App iButton: pulse sequencer

* App iButton: add more keys to store

* App iButton: split key worker to separate read/write/emulate entitys

* App iButton: use new read/emulate entities

* fix callback pointer saving

* App iButton: use KeyReader error enum instead of KeyWorker error list handling

* App iButton: do not use insomnia fns in pulse sequencer

* App iButton: use KeyReader error enum in read scene

* OneWire slave lib: more READ ROM command variants, call callback only if positive result

* GPIO resources: add external gpio

* App SD/NFC: removed application

* App iButton-test: update to new light api

* App iButton: update to new light-api

* Outdated apps: add api-light-usage

* Gpio: update SD card CS pin settings

* API-power: added fns to disable/enable external 3v3 dc-dc

* API-gpio: separated SD card detect routines

* Resources: removed sd cs pin

* SD card: low level init now resets card power supply

* App SD-filesystem: use new card detect fns

* SD card: fix low level init headers

* SD card: more realilable low level init, power reset, exit from command read cycle conditionally

* App SD-filesystem: led notifiers, init cycling

* SD card: backport to F4

* Api PWM: add c++ guards

* App iButton: yellow blink in emulate scene, vibro on

* App iButton: one wire keys command set

* App iButton: successful write scene

* App iButton: key writer

* App iButton: syntax fix

* App iButton: notify write success

* App iButton: fix double scene change

* SD card: handle eject in init sequence

* SD card: api to set level on detect gpio

* SPI: api to set state on bus pins

* SD card: set low state on bus pins while power reset

* File select: init

* File select: fix input consuming

* SD Card: fixed dir open api error

* SD-card: replace strncpy by strlcpy. Fix buffer overflow error.

* API HAL OS: replace CMP based ticks with ARR based one, hard reset lptimer on reconfiguration.

* GUI: More stack size for (temporary, wee need to implement sd card api in separate thread)

* GUI: File select module.

* App iButton-test: remove obsolete app

Co-authored-by: rusdacent <rusdacentx0x08@gmail.com>
Co-authored-by: coreglitch <mail@s3f.ru>
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2021-03-12 15:45:18 +03:00

313 lines
8.3 KiB
C++

#include "one_wire_slave.h"
#include "callback-connector.h"
#include "main.h"
#include "one_wire_device.h"
#define OWET OneWireEmulateTiming
void OneWireSlave::start(void) {
// add exti interrupt
api_interrupt_add(exti_cb, InterruptTypeExternalInterrupt, this);
// init gpio
gpio_init(one_wire_pin_record, GpioModeInterruptRiseFall);
pin_set_float();
// init instructions per us count
__instructions_per_us = (SystemCoreClock / 1000000.0f);
}
void OneWireSlave::stop(void) {
// deinit gpio
gpio_init_ex(one_wire_pin_record, GpioModeInput, GpioPullNo, GpioSpeedLow);
// remove exti interrupt
api_interrupt_remove(exti_cb, InterruptTypeExternalInterrupt);
// deattach devices
deattach();
}
OneWireSlave::OneWireSlave(const GpioPin* pin) {
one_wire_pin_record = pin;
exti_cb = cbc::obtain_connector(this, &OneWireSlave::exti_callback);
}
OneWireSlave::~OneWireSlave() {
stop();
}
void OneWireSlave::attach(OneWireDevice* attached_device) {
device = attached_device;
device->attach(this);
}
void OneWireSlave::deattach(void) {
if(device != nullptr) {
device->deattach();
}
device = nullptr;
}
void OneWireSlave::set_result_callback(OneWireSlaveResultCallback result_cb, void* ctx) {
this->result_cb = result_cb;
this->result_cb_ctx = ctx;
}
void OneWireSlave::pin_set_float() {
gpio_write(one_wire_pin_record, true);
}
void OneWireSlave::pin_set_low() {
gpio_write(one_wire_pin_record, false);
}
void OneWireSlave::pin_init_interrupt_in_isr_ctx(void) {
hal_gpio_init(one_wire_pin_record, GpioModeInterruptRiseFall, GpioPullNo, GpioSpeedLow);
__HAL_GPIO_EXTI_CLEAR_IT(one_wire_pin_record->pin);
}
void OneWireSlave::pin_init_opendrain_in_isr_ctx(void) {
hal_gpio_init(one_wire_pin_record, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
__HAL_GPIO_EXTI_CLEAR_IT(one_wire_pin_record->pin);
}
OneWiteTimeType OneWireSlave::wait_while_gpio_is(OneWiteTimeType time, const bool pin_value) {
uint32_t start = DWT->CYCCNT;
uint32_t time_ticks = time * __instructions_per_us;
uint32_t time_captured;
do {
time_captured = DWT->CYCCNT;
if(gpio_read(one_wire_pin_record) != pin_value) {
OneWiteTimeType remaining_time = time_ticks - (time_captured - start);
remaining_time /= __instructions_per_us;
return remaining_time;
}
} while((time_captured - start) < time_ticks);
return 0;
}
bool OneWireSlave::show_presence(void) {
// wait while master delay presence check
wait_while_gpio_is(OWET::PRESENCE_TIMEOUT, true);
// show presence
pin_set_low();
delay_us(OWET::PRESENCE_MIN);
pin_set_float();
// somebody also can show presence
const OneWiteTimeType wait_low_time = OWET::PRESENCE_MAX - OWET::PRESENCE_MIN;
// so we will wait
if(wait_while_gpio_is(wait_low_time, false) == 0) {
error = OneWireSlaveError::PRESENCE_LOW_ON_LINE;
return false;
}
return true;
}
bool OneWireSlave::receive_bit(void) {
// wait while bus is low
OneWiteTimeType time = OWET::SLOT_MAX;
time = wait_while_gpio_is(time, false);
if(time == 0) {
error = OneWireSlaveError::RESET_IN_PROGRESS;
return false;
}
// wait while bus is high
time = OWET::MSG_HIGH_TIMEOUT;
time = wait_while_gpio_is(time, true);
if(time == 0) {
error = OneWireSlaveError::AWAIT_TIMESLOT_TIMEOUT_HIGH;
return false;
}
// wait a time of zero
time = OWET::READ_MIN;
time = wait_while_gpio_is(time, false);
return (time > 0);
}
bool OneWireSlave::send_bit(bool value) {
const bool write_zero = !value;
// wait while bus is low
OneWiteTimeType time = OWET::SLOT_MAX;
time = wait_while_gpio_is(time, false);
if(time == 0) {
error = OneWireSlaveError::RESET_IN_PROGRESS;
return false;
}
// wait while bus is high
time = OWET::MSG_HIGH_TIMEOUT;
time = wait_while_gpio_is(time, true);
if(time == 0) {
error = OneWireSlaveError::AWAIT_TIMESLOT_TIMEOUT_HIGH;
return false;
}
// choose write time
if(write_zero) {
pin_set_low();
time = OWET::WRITE_ZERO;
} else {
time = OWET::READ_MAX;
}
// hold line for ZERO or ONE time
delay_us(time);
pin_set_float();
return true;
}
bool OneWireSlave::send(const uint8_t* address, const uint8_t data_length) {
uint8_t bytes_sent = 0;
pin_set_float();
// bytes loop
for(; bytes_sent < data_length; ++bytes_sent) {
const uint8_t data_byte = address[bytes_sent];
// bit loop
for(uint8_t bit_mask = 0x01; bit_mask != 0; bit_mask <<= 1) {
if(!send_bit(static_cast<bool>(bit_mask & data_byte))) {
// if we cannot send first bit
if((bit_mask == 0x01) && (error == OneWireSlaveError::AWAIT_TIMESLOT_TIMEOUT_HIGH))
error = OneWireSlaveError::FIRST_BIT_OF_BYTE_TIMEOUT;
return false;
}
}
}
return true;
}
bool OneWireSlave::receive(uint8_t* data, const uint8_t data_length) {
uint8_t bytes_received = 0;
pin_set_float();
for(; bytes_received < data_length; ++bytes_received) {
uint8_t value = 0;
for(uint8_t bit_mask = 0x01; bit_mask != 0; bit_mask <<= 1) {
if(receive_bit()) value |= bit_mask;
}
data[bytes_received] = value;
}
return (bytes_received != data_length);
}
void OneWireSlave::cmd_search_rom(void) {
const uint8_t key_bytes = 8;
uint8_t* key = device->id_storage;
for(uint8_t i = 0; i < key_bytes; i++) {
uint8_t key_byte = key[i];
for(uint8_t j = 0; j < 8; j++) {
bool bit = (key_byte >> j) & 0x01;
if(!send_bit(bit)) return;
if(!send_bit(!bit)) return;
receive_bit();
if(error != OneWireSlaveError::NO_ERROR) return;
}
}
}
bool OneWireSlave::receive_and_process_cmd(void) {
uint8_t cmd;
receive(&cmd, 1);
if(error == OneWireSlaveError::RESET_IN_PROGRESS) return true;
if(error != OneWireSlaveError::NO_ERROR) return false;
switch(cmd) {
case 0xF0:
// SEARCH ROM
cmd_search_rom();
return true;
case 0x0F:
case 0x33:
// READ ROM
device->send_id();
return true;
default: // Unknown command
error = OneWireSlaveError::INCORRECT_ONEWIRE_CMD;
}
if(error == OneWireSlaveError::RESET_IN_PROGRESS) return true;
return (error == OneWireSlaveError::NO_ERROR);
}
bool OneWireSlave::bus_start(void) {
bool result = true;
if(device == nullptr) {
result = false;
} else {
__disable_irq();
pin_init_opendrain_in_isr_ctx();
error = OneWireSlaveError::NO_ERROR;
if(show_presence()) {
// TODO think about multiple command cycles
receive_and_process_cmd();
result =
(error == OneWireSlaveError::NO_ERROR ||
error == OneWireSlaveError::INCORRECT_ONEWIRE_CMD);
} else {
result = false;
}
pin_init_interrupt_in_isr_ctx();
__enable_irq();
}
return result;
}
void OneWireSlave::exti_callback(void* _pin, void* _ctx) {
// interrupt manager get us pin constant, so...
uint32_t pin = (uint32_t)_pin;
OneWireSlave* _this = static_cast<OneWireSlave*>(_ctx);
if(pin == _this->one_wire_pin_record->pin) {
volatile bool input_state = gpio_read(_this->one_wire_pin_record);
static uint32_t pulse_start = 0;
if(input_state) {
uint32_t pulse_length = (DWT->CYCCNT - pulse_start) / __instructions_per_us;
if(pulse_length >= OWET::RESET_MIN) {
if(pulse_length <= OWET::RESET_MAX) {
// reset cycle ok
bool result = _this->bus_start();
if(result && _this->result_cb != nullptr) {
_this->result_cb(result, _this->result_cb_ctx);
}
} else {
error = OneWireSlaveError::VERY_LONG_RESET;
}
} else {
error = OneWireSlaveError::VERY_SHORT_RESET;
}
} else {
//FALL event
pulse_start = DWT->CYCCNT;
}
}
}