mirror of
https://github.com/DarkFlippers/unleashed-firmware.git
synced 2024-12-23 21:34:35 +03:00
e02040107b
* WIP on stripping fw * Compact FW build - use RAM_EXEC=1 COMPACT=1 DEBUG=0 * Fixed uninitialized storage struct; small fixes to compact fw * Flasher srv w/mocked flash ops * Fixed typos & accomodated FFF changes * Alternative fw startup branch * Working load & jmp to RAM fw * +manifest processing for stage loader; + crc verification for stage payload * Fixed questionable code & potential leaks * Lowered screen update rate; added radio stack update stubs; working dfu write * Console EP with manifest & stage validation * Added microtar lib; minor ui fixes for updater * Removed microtar * Removed mtar #2 * Added a better version of microtar * TAR archive api; LFS backup & restore core * Recursive backup/restore * LFS worker thread * Added system apps to loader - not visible in UI; full update process with restarts * Typo fix * Dropped BL & f6; tooling for updater WIP * Minor py fixes * Minor fixes to make it build after merge * Ported flash workaround from BL + fixed visuals * Minor cleanup * Chmod + loader app search fix * Python linter fix * Removed usb stuff & float read support for staged loader == -10% of binary size * Added backup/restore & update pb requests * Added stub impl to RPC for backup/restore/update commands * Reworked TAR to use borrowed Storage api; slightly reduced build size by removing `static string`; hidden update-related RPC behind defines * Moved backup&restore to storage * Fixed new message types * Backup/restore/update RPC impl * Moved furi_hal_crc to LL; minor fixes * CRC HAL rework to LL * Purging STM HAL * Brought back minimal DFU boot mode (no gui); additional crc state checks * Added splash screen, BROKEN usb function * Clock init rework WIP * Stripped graphics from DFU mode * Temp fix for unused static fun * WIP update picker - broken! * Fixed UI * Bumping version * Fixed RTC setup * Backup to update folder instead of ext root * Removed unused scenes & more usb remnants from staged loader * CI updates * Fixed update bundle name * Temporary restored USB handler * Attempt to prevent .text corruption * Comments on how I spent this Saturday * Added update file icon * Documentation updates * Moved common code to lib folder * Storage: more unit tests * Storage: blocking dir open, differentiate file and dir when freed. * Major refactoring; added input processing to updater to allow retrying on failures (not very useful prob). Added API for extraction of thread return value * Removed re-init check for manifest * Changed low-level path manipulation to toolbox/path.h; makefile cleanup; tiny fix in lint.py * Increased update worker stack size * Text fixes in backup CLI * Displaying number of update stages to run; removed timeout in handling errors * Bumping version * Added thread cleanup for spawner thread * Updated build targets to exclude firmware bundle from 'ALL' * Fixed makefile for update_package; skipping VCP init for update mode (ugly) * Switched github build from ALL to update_package * Added +x for dist_update.sh * Cli: add total heap size to "free" command * Moved (RAM) suffix to build version instead of git commit no. * DFU comment * Some fixes suggested by clang-tidy * Fixed recursive PREFIX macro * Makefile: gather all new rules in updater namespace. FuriHal: rename bootloader to boot, isr safe delays * Github: correct build target name in firmware build * FuriHal: move target switch to boot * Makefile: fix firmware flash * Furi, FuriHal: move kernel start to furi, early init * Drop bootloader related stuff * Drop cube. Drop bootloader linker script. * Renamed update_hl, moved constants to #defines * Moved update-related boot mode to separate bitfield * Reworked updater cli to single entry point; fixed crash on tar cleanup * Added Python replacement for dist shell scripts * Linter fixes for dist.py +x * Fixes for environment suffix * Dropped bash scripts * Added dirty build flag to version structure & interfaces * Version string escapes * Fixed flag logic in dist.py; added support for App instances being imported and not terminating the whole program * Fixed fw address in ReadMe.md * Rpc: fix crash on double screen start * Return back original boot behavior and fix jump to system bootloader * Cleanup code, add error sequence for RTC * Update firmware readme * FuriHal: drop boot, restructure RTC registers usage and add header register check * Furi goes first * Toolchain: add ccache support * Renamed update bundle dir Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com> Co-authored-by: あく <alleteam@gmail.com>
318 lines
8.4 KiB
C
318 lines
8.4 KiB
C
#include "one_wire_slave.h"
|
|
#include "one_wire_slave_i.h"
|
|
#include "one_wire_device.h"
|
|
#include <furi.h>
|
|
#include <furi_hal_delay.h>
|
|
#include <furi_hal_ibutton.h>
|
|
|
|
#define OWS_RESET_MIN 270
|
|
#define OWS_RESET_MAX 960
|
|
#define OWS_PRESENCE_TIMEOUT 20
|
|
#define OWS_PRESENCE_MIN 100
|
|
#define OWS_PRESENCE_MAX 480
|
|
#define OWS_MSG_HIGH_TIMEOUT 15000
|
|
#define OWS_SLOT_MAX 135
|
|
#define OWS_READ_MIN 20
|
|
#define OWS_READ_MAX 60
|
|
#define OWS_WRITE_ZERO 30
|
|
|
|
typedef enum {
|
|
NO_ERROR = 0,
|
|
VERY_LONG_RESET,
|
|
VERY_SHORT_RESET,
|
|
PRESENCE_LOW_ON_LINE,
|
|
AWAIT_TIMESLOT_TIMEOUT_HIGH,
|
|
INCORRECT_ONEWIRE_CMD,
|
|
FIRST_BIT_OF_BYTE_TIMEOUT,
|
|
RESET_IN_PROGRESS
|
|
} OneWireSlaveError;
|
|
|
|
struct OneWireSlave {
|
|
OneWireSlaveError error;
|
|
OneWireDevice* device;
|
|
OneWireSlaveResultCallback result_cb;
|
|
void* result_cb_ctx;
|
|
};
|
|
|
|
/*********************** PRIVATE ***********************/
|
|
|
|
uint32_t onewire_slave_wait_while_gpio_is(OneWireSlave* bus, uint32_t time, const bool pin_value) {
|
|
uint32_t start = DWT->CYCCNT;
|
|
uint32_t time_ticks = time * furi_hal_delay_instructions_per_microsecond();
|
|
uint32_t time_captured;
|
|
|
|
do {
|
|
time_captured = DWT->CYCCNT;
|
|
if(furi_hal_ibutton_pin_get_level() != pin_value) {
|
|
uint32_t remaining_time = time_ticks - (time_captured - start);
|
|
remaining_time /= furi_hal_delay_instructions_per_microsecond();
|
|
return remaining_time;
|
|
}
|
|
} while((time_captured - start) < time_ticks);
|
|
|
|
return 0;
|
|
}
|
|
|
|
bool onewire_slave_show_presence(OneWireSlave* bus) {
|
|
// wait while master delay presence check
|
|
onewire_slave_wait_while_gpio_is(bus, OWS_PRESENCE_TIMEOUT, true);
|
|
|
|
// show presence
|
|
furi_hal_ibutton_pin_low();
|
|
furi_hal_delay_us(OWS_PRESENCE_MIN);
|
|
furi_hal_ibutton_pin_high();
|
|
|
|
// somebody also can show presence
|
|
const uint32_t wait_low_time = OWS_PRESENCE_MAX - OWS_PRESENCE_MIN;
|
|
|
|
// so we will wait
|
|
if(onewire_slave_wait_while_gpio_is(bus, wait_low_time, false) == 0) {
|
|
bus->error = PRESENCE_LOW_ON_LINE;
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool onewire_slave_receive_bit(OneWireSlave* bus) {
|
|
// wait while bus is low
|
|
uint32_t time = OWS_SLOT_MAX;
|
|
time = onewire_slave_wait_while_gpio_is(bus, time, false);
|
|
if(time == 0) {
|
|
bus->error = RESET_IN_PROGRESS;
|
|
return false;
|
|
}
|
|
|
|
// wait while bus is high
|
|
time = OWS_MSG_HIGH_TIMEOUT;
|
|
time = onewire_slave_wait_while_gpio_is(bus, time, true);
|
|
if(time == 0) {
|
|
bus->error = AWAIT_TIMESLOT_TIMEOUT_HIGH;
|
|
return false;
|
|
}
|
|
|
|
// wait a time of zero
|
|
time = OWS_READ_MIN;
|
|
time = onewire_slave_wait_while_gpio_is(bus, time, false);
|
|
|
|
return (time > 0);
|
|
}
|
|
|
|
bool onewire_slave_send_bit(OneWireSlave* bus, bool value) {
|
|
const bool write_zero = !value;
|
|
|
|
// wait while bus is low
|
|
uint32_t time = OWS_SLOT_MAX;
|
|
time = onewire_slave_wait_while_gpio_is(bus, time, false);
|
|
if(time == 0) {
|
|
bus->error = RESET_IN_PROGRESS;
|
|
return false;
|
|
}
|
|
|
|
// wait while bus is high
|
|
time = OWS_MSG_HIGH_TIMEOUT;
|
|
time = onewire_slave_wait_while_gpio_is(bus, time, true);
|
|
if(time == 0) {
|
|
bus->error = AWAIT_TIMESLOT_TIMEOUT_HIGH;
|
|
return false;
|
|
}
|
|
|
|
// choose write time
|
|
if(write_zero) {
|
|
furi_hal_ibutton_pin_low();
|
|
time = OWS_WRITE_ZERO;
|
|
} else {
|
|
time = OWS_READ_MAX;
|
|
}
|
|
|
|
// hold line for ZERO or ONE time
|
|
furi_hal_delay_us(time);
|
|
furi_hal_ibutton_pin_high();
|
|
|
|
return true;
|
|
}
|
|
|
|
void onewire_slave_cmd_search_rom(OneWireSlave* bus) {
|
|
const uint8_t key_bytes = 8;
|
|
uint8_t* key = onewire_device_get_id_p(bus->device);
|
|
|
|
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(!onewire_slave_send_bit(bus, bit)) return;
|
|
if(!onewire_slave_send_bit(bus, !bit)) return;
|
|
|
|
onewire_slave_receive_bit(bus);
|
|
if(bus->error != NO_ERROR) return;
|
|
}
|
|
}
|
|
}
|
|
|
|
bool onewire_slave_receive_and_process_cmd(OneWireSlave* bus) {
|
|
uint8_t cmd;
|
|
onewire_slave_receive(bus, &cmd, 1);
|
|
|
|
if(bus->error == RESET_IN_PROGRESS) return true;
|
|
if(bus->error != NO_ERROR) return false;
|
|
|
|
switch(cmd) {
|
|
case 0xF0:
|
|
// SEARCH ROM
|
|
onewire_slave_cmd_search_rom(bus);
|
|
return true;
|
|
|
|
case 0x0F:
|
|
case 0x33:
|
|
// READ ROM
|
|
onewire_device_send_id(bus->device);
|
|
return true;
|
|
|
|
default: // Unknown command
|
|
bus->error = INCORRECT_ONEWIRE_CMD;
|
|
}
|
|
|
|
if(bus->error == RESET_IN_PROGRESS) return true;
|
|
return (bus->error == NO_ERROR);
|
|
}
|
|
|
|
bool onewire_slave_bus_start(OneWireSlave* bus) {
|
|
bool result = true;
|
|
|
|
if(bus->device == NULL) {
|
|
result = false;
|
|
} else {
|
|
FURI_CRITICAL_ENTER();
|
|
furi_hal_ibutton_start_drive_in_isr();
|
|
bus->error = NO_ERROR;
|
|
|
|
if(onewire_slave_show_presence(bus)) {
|
|
// TODO think about multiple command cycles
|
|
onewire_slave_receive_and_process_cmd(bus);
|
|
result = (bus->error == NO_ERROR || bus->error == INCORRECT_ONEWIRE_CMD);
|
|
|
|
} else {
|
|
result = false;
|
|
}
|
|
|
|
furi_hal_ibutton_start_interrupt_in_isr();
|
|
FURI_CRITICAL_EXIT();
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
static void exti_cb(void* context) {
|
|
OneWireSlave* bus = context;
|
|
|
|
volatile bool input_state = furi_hal_ibutton_pin_get_level();
|
|
static uint32_t pulse_start = 0;
|
|
|
|
if(input_state) {
|
|
uint32_t pulse_length = (DWT->CYCCNT - pulse_start) / furi_hal_delay_instructions_per_microsecond();
|
|
if(pulse_length >= OWS_RESET_MIN) {
|
|
if(pulse_length <= OWS_RESET_MAX) {
|
|
// reset cycle ok
|
|
bool result = onewire_slave_bus_start(bus);
|
|
if(result && bus->result_cb != NULL) {
|
|
bus->result_cb(bus->result_cb_ctx);
|
|
}
|
|
} else {
|
|
bus->error = VERY_LONG_RESET;
|
|
}
|
|
} else {
|
|
bus->error = VERY_SHORT_RESET;
|
|
}
|
|
} else {
|
|
//FALL event
|
|
pulse_start = DWT->CYCCNT;
|
|
}
|
|
};
|
|
|
|
/*********************** PUBLIC ***********************/
|
|
|
|
OneWireSlave* onewire_slave_alloc() {
|
|
OneWireSlave* bus = malloc(sizeof(OneWireSlave));
|
|
bus->error = NO_ERROR;
|
|
bus->device = NULL;
|
|
bus->result_cb = NULL;
|
|
bus->result_cb_ctx = NULL;
|
|
return bus;
|
|
}
|
|
|
|
void onewire_slave_free(OneWireSlave* bus) {
|
|
onewire_slave_stop(bus);
|
|
free(bus);
|
|
}
|
|
|
|
void onewire_slave_start(OneWireSlave* bus) {
|
|
furi_hal_ibutton_add_interrupt(exti_cb, bus);
|
|
furi_hal_ibutton_start_interrupt();
|
|
}
|
|
|
|
void onewire_slave_stop(OneWireSlave* bus) {
|
|
furi_hal_ibutton_stop();
|
|
furi_hal_ibutton_remove_interrupt();
|
|
}
|
|
|
|
void onewire_slave_attach(OneWireSlave* bus, OneWireDevice* device) {
|
|
bus->device = device;
|
|
onewire_device_attach(device, bus);
|
|
}
|
|
|
|
void onewire_slave_detach(OneWireSlave* bus) {
|
|
if(bus->device != NULL) {
|
|
onewire_device_detach(bus->device);
|
|
}
|
|
bus->device = NULL;
|
|
}
|
|
|
|
void onewire_slave_set_result_callback(
|
|
OneWireSlave* bus,
|
|
OneWireSlaveResultCallback result_cb,
|
|
void* context) {
|
|
bus->result_cb = result_cb;
|
|
bus->result_cb_ctx = context;
|
|
}
|
|
|
|
bool onewire_slave_send(OneWireSlave* bus, const uint8_t* address, const uint8_t data_length) {
|
|
uint8_t bytes_sent = 0;
|
|
|
|
furi_hal_ibutton_pin_high();
|
|
|
|
// 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(!onewire_slave_send_bit(bus, bit_mask & data_byte)) {
|
|
// if we cannot send first bit
|
|
if((bit_mask == 0x01) && (bus->error == AWAIT_TIMESLOT_TIMEOUT_HIGH))
|
|
bus->error = FIRST_BIT_OF_BYTE_TIMEOUT;
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool onewire_slave_receive(OneWireSlave* bus, uint8_t* data, const uint8_t data_length) {
|
|
uint8_t bytes_received = 0;
|
|
|
|
furi_hal_ibutton_pin_high();
|
|
|
|
for(; bytes_received < data_length; ++bytes_received) {
|
|
uint8_t value = 0;
|
|
|
|
for(uint8_t bit_mask = 0x01; bit_mask != 0; bit_mask <<= 1) {
|
|
if(onewire_slave_receive_bit(bus)) value |= bit_mask;
|
|
}
|
|
|
|
data[bytes_received] = value;
|
|
}
|
|
return (bytes_received != data_length);
|
|
}
|