mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-05-30 04:38:44 +00:00
trezorhal: simplify api for init functions, use bool and void rather than int
This commit is contained in:
parent
b51ca0a16e
commit
79fe9162b5
@ -165,8 +165,8 @@ int main(void)
|
|||||||
|
|
||||||
clear_otg_hs_memory();
|
clear_otg_hs_memory();
|
||||||
|
|
||||||
ensure(0 == display_init(), NULL);
|
display_init();
|
||||||
ensure(0 == sdcard_init(), NULL);
|
sdcard_init();
|
||||||
|
|
||||||
if (check_sdcard()) {
|
if (check_sdcard()) {
|
||||||
copy_sdcard();
|
copy_sdcard();
|
||||||
|
@ -148,7 +148,7 @@ int usb_init_all(void) {
|
|||||||
|
|
||||||
ensure(0 == usb_init(&dev_info), NULL);
|
ensure(0 == usb_init(&dev_info), NULL);
|
||||||
ensure(0 == usb_hid_add(&hid_info), NULL);
|
ensure(0 == usb_hid_add(&hid_info), NULL);
|
||||||
ensure(0 == usb_start(), NULL);
|
usb_start();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -256,7 +256,7 @@ int main(void)
|
|||||||
|
|
||||||
display_orientation(0);
|
display_orientation(0);
|
||||||
|
|
||||||
ensure(0 == touch_init(), NULL);
|
ensure(touch_init(), NULL);
|
||||||
|
|
||||||
// delay to detect touch
|
// delay to detect touch
|
||||||
uint32_t touched = 0;
|
uint32_t touched = 0;
|
||||||
|
@ -439,7 +439,7 @@ STATIC mp_obj_t mod_trezorio_USB_open(mp_obj_t self) {
|
|||||||
mp_obj_get_array(MP_OBJ_FROM_PTR(&o->ifaces), &iface_cnt, &iface_objs);
|
mp_obj_get_array(MP_OBJ_FROM_PTR(&o->ifaces), &iface_cnt, &iface_objs);
|
||||||
|
|
||||||
// Initialize the USB stack
|
// Initialize the USB stack
|
||||||
if (usb_init(&o->info) != 0) {
|
if (usb_init(&o->info) != true) {
|
||||||
mp_raise_msg(&mp_type_RuntimeError, "failed to initialize USB");
|
mp_raise_msg(&mp_type_RuntimeError, "failed to initialize USB");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -469,10 +469,7 @@ STATIC mp_obj_t mod_trezorio_USB_open(mp_obj_t self) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Start the USB stack
|
// Start the USB stack
|
||||||
if (usb_start() != 0) {
|
usb_start();
|
||||||
usb_deinit();
|
|
||||||
mp_raise_msg(&mp_type_RuntimeError, "failed to start USB");
|
|
||||||
}
|
|
||||||
o->state = USB_OPENED;
|
o->state = USB_OPENED;
|
||||||
|
|
||||||
// If we found any VCP interfaces, use the last one for stdio,
|
// If we found any VCP interfaces, use the last one for stdio,
|
||||||
|
@ -7,11 +7,6 @@
|
|||||||
|
|
||||||
#include "../../trezorhal/flash.h"
|
#include "../../trezorhal/flash.h"
|
||||||
|
|
||||||
int flash_init(void)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void flash_set_option_bytes(void)
|
void flash_set_option_bytes(void)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -23,12 +23,12 @@ static int sock;
|
|||||||
static struct sockaddr_in si_me, si_other;
|
static struct sockaddr_in si_me, si_other;
|
||||||
static socklen_t slen = 0;
|
static socklen_t slen = 0;
|
||||||
|
|
||||||
int usb_init(const usb_dev_info_t *dev_info) {
|
bool usb_init(const usb_dev_info_t *dev_info) {
|
||||||
(void)dev_info;
|
(void)dev_info;
|
||||||
|
|
||||||
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
if (sock < 0) {
|
if (sock < 0) {
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
fcntl(sock, F_SETFL, O_NONBLOCK);
|
fcntl(sock, F_SETFL, O_NONBLOCK);
|
||||||
@ -49,22 +49,19 @@ int usb_init(const usb_dev_info_t *dev_info) {
|
|||||||
|
|
||||||
int b = bind(sock, (struct sockaddr*)&si_me, sizeof(si_me));
|
int b = bind(sock, (struct sockaddr*)&si_me, sizeof(si_me));
|
||||||
if (b < 0) {
|
if (b < 0) {
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int usb_deinit(void) {
|
void usb_deinit(void) {
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int usb_start(void) {
|
void usb_start(void) {
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int usb_stop(void) {
|
void usb_stop(void) {
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int usb_hid_add(const usb_hid_info_t *info) {
|
int usb_hid_add(const usb_hid_info_t *info) {
|
||||||
|
@ -7,9 +7,8 @@
|
|||||||
|
|
||||||
#include "../../trezorhal/sbu.h"
|
#include "../../trezorhal/sbu.h"
|
||||||
|
|
||||||
int sbu_init(void)
|
void sbu_init(void)
|
||||||
{
|
{
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sbu_set(bool sbu1, bool sbu2)
|
void sbu_set(bool sbu1, bool sbu2)
|
||||||
|
@ -9,8 +9,7 @@
|
|||||||
|
|
||||||
#define SD_ERROR 41U
|
#define SD_ERROR 41U
|
||||||
|
|
||||||
int sdcard_init(void) {
|
void sdcard_init(void) {
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool sdcard_is_present(void) {
|
bool sdcard_is_present(void) {
|
||||||
|
@ -103,7 +103,7 @@ void display_set_backlight(int val)
|
|||||||
TIM1->CCR1 = LED_PWM_TIM_PERIOD * val / 255;
|
TIM1->CCR1 = LED_PWM_TIM_PERIOD * val / 255;
|
||||||
}
|
}
|
||||||
|
|
||||||
int display_init(void)
|
void display_init(void)
|
||||||
{
|
{
|
||||||
// init peripherials
|
// init peripherials
|
||||||
__HAL_RCC_GPIOE_CLK_ENABLE();
|
__HAL_RCC_GPIOE_CLK_ENABLE();
|
||||||
@ -259,7 +259,6 @@ int display_init(void)
|
|||||||
display_clear();
|
display_clear();
|
||||||
display_orientation(0);
|
display_orientation(0);
|
||||||
display_unsleep();
|
display_unsleep();
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void display_set_window_raw(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
|
static void display_set_window_raw(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
|
||||||
|
@ -38,7 +38,7 @@ void DATA(uint8_t x) {
|
|||||||
#define DATA(X) (void)(X);
|
#define DATA(X) (void)(X);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int display_init(void)
|
void display_init(void)
|
||||||
{
|
{
|
||||||
#ifndef TREZOR_NOUI
|
#ifndef TREZOR_NOUI
|
||||||
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
||||||
@ -65,7 +65,6 @@ int display_init(void)
|
|||||||
DISPLAY_BACKLIGHT = 0;
|
DISPLAY_BACKLIGHT = 0;
|
||||||
DISPLAY_ORIENTATION = 0;
|
DISPLAY_ORIENTATION = 0;
|
||||||
#endif
|
#endif
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void display_set_window_raw(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
|
static void display_set_window_raw(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
|
||||||
|
@ -50,7 +50,7 @@
|
|||||||
|
|
||||||
// provided by port
|
// provided by port
|
||||||
|
|
||||||
int display_init(void);
|
void display_init(void);
|
||||||
void display_refresh(void);
|
void display_refresh(void);
|
||||||
void display_save(const char *filename);
|
void display_save(const char *filename);
|
||||||
|
|
||||||
|
@ -27,8 +27,8 @@ int main(void)
|
|||||||
|
|
||||||
display_orientation(0);
|
display_orientation(0);
|
||||||
|
|
||||||
ensure(0 == sdcard_init(), NULL);
|
sdcard_init();
|
||||||
ensure(0 == touch_init(), NULL);
|
ensure(touch_init(), NULL);
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
printf("CORE: Starting main loop\n");
|
printf("CORE: Starting main loop\n");
|
||||||
|
@ -38,8 +38,6 @@
|
|||||||
// 22
|
// 22
|
||||||
#define FLASH_SECTOR_FIRMWARE_EXTRA_END 23
|
#define FLASH_SECTOR_FIRMWARE_EXTRA_END 23
|
||||||
|
|
||||||
int flash_init(void);
|
|
||||||
|
|
||||||
void flash_set_option_bytes(void);
|
void flash_set_option_bytes(void);
|
||||||
|
|
||||||
bool flash_unlock(void);
|
bool flash_unlock(void);
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
#include "sbu.h"
|
#include "sbu.h"
|
||||||
|
|
||||||
int sbu_init(void) {
|
void sbu_init(void) {
|
||||||
GPIO_InitTypeDef GPIO_InitStructure;
|
GPIO_InitTypeDef GPIO_InitStructure;
|
||||||
|
|
||||||
// SBU1/PA2 SBU2/PA3
|
// SBU1/PA2 SBU2/PA3
|
||||||
@ -21,8 +21,6 @@ int sbu_init(void) {
|
|||||||
|
|
||||||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_RESET);
|
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_RESET);
|
||||||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET);
|
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET);
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sbu_set(bool sbu1, bool sbu2) {
|
void sbu_set(bool sbu1, bool sbu2) {
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
int sbu_init(void);
|
void sbu_init(void);
|
||||||
void sbu_set(bool sbu1, bool sbu2);
|
void sbu_set(bool sbu1, bool sbu2);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -32,7 +32,7 @@
|
|||||||
|
|
||||||
static SD_HandleTypeDef sd_handle;
|
static SD_HandleTypeDef sd_handle;
|
||||||
|
|
||||||
int sdcard_init(void) {
|
void sdcard_init(void) {
|
||||||
// invalidate the sd_handle
|
// invalidate the sd_handle
|
||||||
sd_handle.Instance = NULL;
|
sd_handle.Instance = NULL;
|
||||||
|
|
||||||
@ -54,8 +54,6 @@ int sdcard_init(void) {
|
|||||||
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||||
GPIO_InitStructure.Pin = GPIO_PIN_13;
|
GPIO_InitStructure.Pin = GPIO_PIN_13;
|
||||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
|
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HAL_SD_MspInit(SD_HandleTypeDef *hsd) {
|
void HAL_SD_MspInit(SD_HandleTypeDef *hsd) {
|
||||||
|
@ -32,7 +32,7 @@
|
|||||||
// this is a fixed size and should not be changed
|
// this is a fixed size and should not be changed
|
||||||
#define SDCARD_BLOCK_SIZE (512)
|
#define SDCARD_BLOCK_SIZE (512)
|
||||||
|
|
||||||
int sdcard_init(void);
|
void sdcard_init(void);
|
||||||
bool sdcard_is_present(void);
|
bool sdcard_is_present(void);
|
||||||
bool sdcard_power_on(void);
|
bool sdcard_power_on(void);
|
||||||
bool sdcard_power_off(void);
|
bool sdcard_power_off(void);
|
||||||
|
@ -14,7 +14,7 @@ I2C_HandleTypeDef i2c_handle = {
|
|||||||
.Instance = I2C1,
|
.Instance = I2C1,
|
||||||
};
|
};
|
||||||
|
|
||||||
int touch_init(void)
|
bool touch_init(void)
|
||||||
{
|
{
|
||||||
// Enable I2C clock
|
// Enable I2C clock
|
||||||
__HAL_RCC_I2C1_CLK_ENABLE();
|
__HAL_RCC_I2C1_CLK_ENABLE();
|
||||||
@ -41,10 +41,10 @@ int touch_init(void)
|
|||||||
|
|
||||||
// Init I2C handle
|
// Init I2C handle
|
||||||
if (HAL_I2C_Init(&i2c_handle) != HAL_OK) {
|
if (HAL_I2C_Init(&i2c_handle) != HAL_OK) {
|
||||||
return 1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define TOUCH_ADDRESS 56
|
#define TOUCH_ADDRESS 56
|
||||||
|
@ -8,11 +8,14 @@
|
|||||||
#ifndef __TREZORHAL_TOUCH_H__
|
#ifndef __TREZORHAL_TOUCH_H__
|
||||||
#define __TREZORHAL_TOUCH_H__
|
#define __TREZORHAL_TOUCH_H__
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
#define TOUCH_START 0x00010000
|
#define TOUCH_START 0x00010000
|
||||||
#define TOUCH_MOVE 0x00020000
|
#define TOUCH_MOVE 0x00020000
|
||||||
#define TOUCH_END 0x00040000
|
#define TOUCH_END 0x00040000
|
||||||
|
|
||||||
int touch_init(void);
|
bool touch_init(void);
|
||||||
uint32_t touch_read(void);
|
uint32_t touch_read(void);
|
||||||
void touch_click(void);
|
void touch_click(void);
|
||||||
|
|
||||||
|
@ -49,7 +49,7 @@ static int check_desc_str(const uint8_t *s) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int usb_init(const usb_dev_info_t *dev_info) {
|
bool usb_init(const usb_dev_info_t *dev_info) {
|
||||||
|
|
||||||
// Device descriptor
|
// Device descriptor
|
||||||
usb_dev_desc.bLength = sizeof(usb_device_descriptor_t);
|
usb_dev_desc.bLength = sizeof(usb_device_descriptor_t);
|
||||||
@ -71,7 +71,7 @@ int usb_init(const usb_dev_info_t *dev_info) {
|
|||||||
if ((0 != check_desc_str(dev_info->manufacturer)) ||
|
if ((0 != check_desc_str(dev_info->manufacturer)) ||
|
||||||
(0 != check_desc_str(dev_info->product)) ||
|
(0 != check_desc_str(dev_info->product)) ||
|
||||||
(0 != check_desc_str(dev_info->serial_number))) {
|
(0 != check_desc_str(dev_info->serial_number))) {
|
||||||
return 1; // Invalid descriptor string
|
return false; // Invalid descriptor string
|
||||||
}
|
}
|
||||||
usb_str_table.manufacturer = dev_info->manufacturer;
|
usb_str_table.manufacturer = dev_info->manufacturer;
|
||||||
usb_str_table.product = dev_info->product;
|
usb_str_table.product = dev_info->product;
|
||||||
@ -91,31 +91,28 @@ int usb_init(const usb_dev_info_t *dev_info) {
|
|||||||
usb_next_iface_desc = (usb_interface_descriptor_t *)(usb_config_buf + usb_config_desc->wTotalLength);
|
usb_next_iface_desc = (usb_interface_descriptor_t *)(usb_config_buf + usb_config_desc->wTotalLength);
|
||||||
|
|
||||||
if (0 != USBD_Init(&usb_dev_handle, (USBD_DescriptorsTypeDef*)&usb_descriptors, USB_PHY_ID)) {
|
if (0 != USBD_Init(&usb_dev_handle, (USBD_DescriptorsTypeDef*)&usb_descriptors, USB_PHY_ID)) {
|
||||||
return 1;
|
return false;
|
||||||
}
|
}
|
||||||
if (0 != USBD_RegisterClass(&usb_dev_handle, (USBD_ClassTypeDef*)&usb_class)) {
|
if (0 != USBD_RegisterClass(&usb_dev_handle, (USBD_ClassTypeDef*)&usb_class)) {
|
||||||
return 1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int usb_deinit(void) {
|
void usb_deinit(void) {
|
||||||
USBD_DeInit(&usb_dev_handle);
|
USBD_DeInit(&usb_dev_handle);
|
||||||
for (int i = 0; i < USBD_MAX_NUM_INTERFACES; i++) {
|
for (int i = 0; i < USBD_MAX_NUM_INTERFACES; i++) {
|
||||||
usb_ifaces[i].type = USB_IFACE_TYPE_DISABLED;
|
usb_ifaces[i].type = USB_IFACE_TYPE_DISABLED;
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int usb_start(void) {
|
void usb_start(void) {
|
||||||
USBD_Start(&usb_dev_handle);
|
USBD_Start(&usb_dev_handle);
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int usb_stop(void) {
|
void usb_stop(void) {
|
||||||
USBD_Stop(&usb_dev_handle);
|
USBD_Stop(&usb_dev_handle);
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
#ifndef __TREZORHAL_USB_H__
|
#ifndef __TREZORHAL_USB_H__
|
||||||
#define __TREZORHAL_USB_H__
|
#define __TREZORHAL_USB_H__
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#define USB_EP_DIR_OUT 0x00
|
#define USB_EP_DIR_OUT 0x00
|
||||||
@ -116,9 +117,9 @@ typedef struct {
|
|||||||
usb_iface_type_t type;
|
usb_iface_type_t type;
|
||||||
} usb_iface_t;
|
} usb_iface_t;
|
||||||
|
|
||||||
int usb_init(const usb_dev_info_t *dev_info);
|
bool usb_init(const usb_dev_info_t *dev_info);
|
||||||
int usb_deinit(void);
|
void usb_deinit(void);
|
||||||
int usb_start(void);
|
void usb_start(void);
|
||||||
int usb_stop(void);
|
void usb_stop(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user