diff --git a/SConscript.firmware b/SConscript.firmware index 52df3a4cb9..2b059b4adc 100644 --- a/SConscript.firmware +++ b/SConscript.firmware @@ -278,6 +278,7 @@ SOURCE_TREZORHAL = [ 'embed/trezorhal/flash.c', 'embed/trezorhal/mini_printf.c', 'embed/trezorhal/rng.c', + 'embed/trezorhal/sbu.c', 'embed/trezorhal/sdcard.c', 'embed/trezorhal/stm32_it.c', 'embed/trezorhal/stm32_system.c', diff --git a/embed/extmod/modtrezorio/modtrezorio-sbu.h b/embed/extmod/modtrezorio/modtrezorio-sbu.h new file mode 100644 index 0000000000..fb50066c03 --- /dev/null +++ b/embed/extmod/modtrezorio/modtrezorio-sbu.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) Pavol Rusnak, SatoshiLabs + * + * Licensed under TREZOR License + * see LICENSE file for details + */ + +#if defined TREZOR_STM32 +#include "sbu.h" +#elif defined TREZOR_UNIX +#include "unix-sbu-mock.h" +#else +#error Unsupported TREZOR port. Only STM32 and UNIX ports are supported. +#endif + +/// class SBU: +/// ''' +/// ''' +typedef struct _mp_obj_SBU_t { + mp_obj_base_t base; +} mp_obj_SBU_t; + +/// def __init__(self) -> None: +/// ''' +/// ''' +STATIC mp_obj_t mod_trezorio_SBU_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 0, 0, false); + mp_obj_SBU_t *o = m_new_obj(mp_obj_SBU_t); + o->base.type = type; + sbu_init(); + return MP_OBJ_FROM_PTR(o); +} + +/// def set(self, sbu1: bool, sbu2: bool) -> None: +/// ''' +/// Sets SBU wires to sbu1 and sbu2 values respectively +/// ''' +STATIC mp_obj_t mod_trezorio_SBU_set(mp_obj_t self, mp_obj_t sbu1, mp_obj_t sbu2) { + sbu_set(mp_obj_is_true(sbu1), mp_obj_is_true(sbu2)); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorio_SBU_set_obj, mod_trezorio_SBU_set); + +STATIC const mp_rom_map_elem_t mod_trezorio_SBU_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_set), MP_ROM_PTR(&mod_trezorio_SBU_set_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(mod_trezorio_SBU_locals_dict, mod_trezorio_SBU_locals_dict_table); + +STATIC const mp_obj_type_t mod_trezorio_SBU_type = { + { &mp_type_type }, + .name = MP_QSTR_SBU, + .make_new = mod_trezorio_SBU_make_new, + .locals_dict = (void*)&mod_trezorio_SBU_locals_dict, +}; diff --git a/embed/extmod/modtrezorio/modtrezorio.c b/embed/extmod/modtrezorio/modtrezorio.c index 71a85fab88..0a7bd3bb1c 100644 --- a/embed/extmod/modtrezorio/modtrezorio.c +++ b/embed/extmod/modtrezorio/modtrezorio.c @@ -11,12 +11,15 @@ #if MICROPY_PY_TREZORIO +#include "modtrezorio-sbu.h" #include "modtrezorio-sdcard.h" #include "modtrezorio-msg.h" STATIC const mp_rom_map_elem_t mp_module_trezorio_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_trezorio) }, + { MP_ROM_QSTR(MP_QSTR_SBU), MP_ROM_PTR(&mod_trezorio_SBU_type) }, + { MP_ROM_QSTR(MP_QSTR_SDCard), MP_ROM_PTR(&mod_trezorio_SDCard_type) }, { MP_ROM_QSTR(MP_QSTR_USB), MP_ROM_PTR(&mod_trezorio_USB_type) }, diff --git a/embed/extmod/modtrezorio/unix-sbu-mock.h b/embed/extmod/modtrezorio/unix-sbu-mock.h new file mode 100644 index 0000000000..758cf00ce0 --- /dev/null +++ b/embed/extmod/modtrezorio/unix-sbu-mock.h @@ -0,0 +1,17 @@ +/* + * Copyright (c) Pavol Rusnak, SatoshiLabs + * + * Licensed under TREZOR License + * see LICENSE file for details + */ + +#include "../../trezorhal/sbu.h" + +int sbu_init(void) +{ + return 0; +} + +void sbu_set(bool sbu1, bool sbu2) +{ +} diff --git a/embed/trezorhal/sbu.c b/embed/trezorhal/sbu.c new file mode 100644 index 0000000000..d290ef9ac7 --- /dev/null +++ b/embed/trezorhal/sbu.c @@ -0,0 +1,31 @@ +/* + * Copyright (c) Pavol Rusnak, SatoshiLabs + * + * Licensed under TREZOR License + * see LICENSE file for details + */ + +#include STM32_HAL_H + +#include "sbu.h" + +int sbu_init(void) { + GPIO_InitTypeDef GPIO_InitStructure; + + // SBU1/PA2 SBU2/PA3 + GPIO_InitStructure.Pin = GPIO_PIN_2 | GPIO_PIN_3; + GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStructure.Pull = GPIO_NOPULL; + GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + HAL_GPIO_Init(GPIOC, &GPIO_InitStructure); + + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_RESET); + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET); + + return 0; +} + +void sbu_set(bool sbu1, bool sbu2) { + HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2, sbu1 ? GPIO_PIN_SET : GPIO_PIN_RESET); + HAL_GPIO_WritePin(GPIOC, GPIO_PIN_3, sbu2 ? GPIO_PIN_SET : GPIO_PIN_RESET); +} diff --git a/embed/trezorhal/sbu.h b/embed/trezorhal/sbu.h new file mode 100644 index 0000000000..9682dafb32 --- /dev/null +++ b/embed/trezorhal/sbu.h @@ -0,0 +1,16 @@ +/* + * Copyright (c) Pavol Rusnak, SatoshiLabs + * + * Licensed under TREZOR License + * see LICENSE file for details + */ + +#ifndef __TREZORHAL_SBU_H__ +#define __TREZORHAL_SBU_H__ + +#include + +int sbu_init(void); +void sbu_set(bool sbu1, bool sbu2); + +#endif