1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-11 16:00:57 +00:00

trezorhal: simplify api for init functions, use bool and void rather than int

This commit is contained in:
Pavol Rusnak 2017-10-20 15:25:24 +02:00
parent b51ca0a16e
commit 79fe9162b5
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
20 changed files with 45 additions and 65 deletions

View File

@ -165,8 +165,8 @@ int main(void)
clear_otg_hs_memory();
ensure(0 == display_init(), NULL);
ensure(0 == sdcard_init(), NULL);
display_init();
sdcard_init();
if (check_sdcard()) {
copy_sdcard();

View File

@ -148,7 +148,7 @@ int usb_init_all(void) {
ensure(0 == usb_init(&dev_info), NULL);
ensure(0 == usb_hid_add(&hid_info), NULL);
ensure(0 == usb_start(), NULL);
usb_start();
return 0;
}
@ -256,7 +256,7 @@ int main(void)
display_orientation(0);
ensure(0 == touch_init(), NULL);
ensure(touch_init(), NULL);
// delay to detect touch
uint32_t touched = 0;

View File

@ -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);
// 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");
}
@ -469,10 +469,7 @@ STATIC mp_obj_t mod_trezorio_USB_open(mp_obj_t self) {
}
// Start the USB stack
if (usb_start() != 0) {
usb_deinit();
mp_raise_msg(&mp_type_RuntimeError, "failed to start USB");
}
usb_start();
o->state = USB_OPENED;
// If we found any VCP interfaces, use the last one for stdio,
@ -533,4 +530,4 @@ STATIC const mp_obj_type_t mod_trezorio_USB_type = {
.name = MP_QSTR_USB,
.make_new = mod_trezorio_USB_make_new,
.locals_dict = (void*)&mod_trezorio_USB_locals_dict,
};
};

View File

@ -7,11 +7,6 @@
#include "../../trezorhal/flash.h"
int flash_init(void)
{
return 0;
}
void flash_set_option_bytes(void)
{
}

View File

@ -23,12 +23,12 @@ static int sock;
static struct sockaddr_in si_me, si_other;
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;
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock < 0) {
return -1;
return false;
}
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));
if (b < 0) {
return -1;
return false;
}
return 0;
return true;
}
int usb_deinit(void) {
return 0;
void usb_deinit(void) {
}
int usb_start(void) {
return 0;
void usb_start(void) {
}
int usb_stop(void) {
return 0;
void usb_stop(void) {
}
int usb_hid_add(const usb_hid_info_t *info) {

View File

@ -7,9 +7,8 @@
#include "../../trezorhal/sbu.h"
int sbu_init(void)
void sbu_init(void)
{
return 0;
}
void sbu_set(bool sbu1, bool sbu2)

View File

@ -9,8 +9,7 @@
#define SD_ERROR 41U
int sdcard_init(void) {
return 0;
void sdcard_init(void) {
}
bool sdcard_is_present(void) {

View File

@ -103,7 +103,7 @@ void display_set_backlight(int val)
TIM1->CCR1 = LED_PWM_TIM_PERIOD * val / 255;
}
int display_init(void)
void display_init(void)
{
// init peripherials
__HAL_RCC_GPIOE_CLK_ENABLE();
@ -259,7 +259,6 @@ int display_init(void)
display_clear();
display_orientation(0);
display_unsleep();
return 0;
}
static void display_set_window_raw(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)

View File

@ -38,7 +38,7 @@ void DATA(uint8_t x) {
#define DATA(X) (void)(X);
#endif
int display_init(void)
void display_init(void)
{
#ifndef TREZOR_NOUI
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
@ -65,7 +65,6 @@ int display_init(void)
DISPLAY_BACKLIGHT = 0;
DISPLAY_ORIENTATION = 0;
#endif
return 0;
}
static void display_set_window_raw(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)

View File

@ -50,7 +50,7 @@
// provided by port
int display_init(void);
void display_init(void);
void display_refresh(void);
void display_save(const char *filename);

View File

@ -27,8 +27,8 @@ int main(void)
display_orientation(0);
ensure(0 == sdcard_init(), NULL);
ensure(0 == touch_init(), NULL);
sdcard_init();
ensure(touch_init(), NULL);
for (;;) {
printf("CORE: Starting main loop\n");

View File

@ -38,8 +38,6 @@
// 22
#define FLASH_SECTOR_FIRMWARE_EXTRA_END 23
int flash_init(void);
void flash_set_option_bytes(void);
bool flash_unlock(void);

View File

@ -9,7 +9,7 @@
#include "sbu.h"
int sbu_init(void) {
void sbu_init(void) {
GPIO_InitTypeDef GPIO_InitStructure;
// 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_3, GPIO_PIN_RESET);
return 0;
}
void sbu_set(bool sbu1, bool sbu2) {

View File

@ -10,7 +10,7 @@
#include <stdbool.h>
int sbu_init(void);
void sbu_init(void);
void sbu_set(bool sbu1, bool sbu2);
#endif

View File

@ -32,7 +32,7 @@
static SD_HandleTypeDef sd_handle;
int sdcard_init(void) {
void sdcard_init(void) {
// invalidate the sd_handle
sd_handle.Instance = NULL;
@ -54,8 +54,6 @@ int sdcard_init(void) {
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStructure.Pin = GPIO_PIN_13;
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
return 0;
}
void HAL_SD_MspInit(SD_HandleTypeDef *hsd) {

View File

@ -32,7 +32,7 @@
// this is a fixed size and should not be changed
#define SDCARD_BLOCK_SIZE (512)
int sdcard_init(void);
void sdcard_init(void);
bool sdcard_is_present(void);
bool sdcard_power_on(void);
bool sdcard_power_off(void);

View File

@ -14,7 +14,7 @@ I2C_HandleTypeDef i2c_handle = {
.Instance = I2C1,
};
int touch_init(void)
bool touch_init(void)
{
// Enable I2C clock
__HAL_RCC_I2C1_CLK_ENABLE();
@ -41,10 +41,10 @@ int touch_init(void)
// Init I2C handle
if (HAL_I2C_Init(&i2c_handle) != HAL_OK) {
return 1;
return false;
}
return 0;
return true;
}
#define TOUCH_ADDRESS 56

View File

@ -8,11 +8,14 @@
#ifndef __TREZORHAL_TOUCH_H__
#define __TREZORHAL_TOUCH_H__
#include <stdbool.h>
#include <stdint.h>
#define TOUCH_START 0x00010000
#define TOUCH_MOVE 0x00020000
#define TOUCH_END 0x00040000
int touch_init(void);
bool touch_init(void);
uint32_t touch_read(void);
void touch_click(void);

View File

@ -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
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)) ||
(0 != check_desc_str(dev_info->product)) ||
(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.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);
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)) {
return 1;
return false;
}
return 0;
return true;
}
int usb_deinit(void) {
void usb_deinit(void) {
USBD_DeInit(&usb_dev_handle);
for (int i = 0; i < USBD_MAX_NUM_INTERFACES; i++) {
usb_ifaces[i].type = USB_IFACE_TYPE_DISABLED;
}
return 0;
}
int usb_start(void) {
void usb_start(void) {
USBD_Start(&usb_dev_handle);
return 0;
}
int usb_stop(void) {
void usb_stop(void) {
USBD_Stop(&usb_dev_handle);
return 0;
}
/*

View File

@ -8,6 +8,7 @@
#ifndef __TREZORHAL_USB_H__
#define __TREZORHAL_USB_H__
#include <stdbool.h>
#include <stdint.h>
#define USB_EP_DIR_OUT 0x00
@ -116,9 +117,9 @@ typedef struct {
usb_iface_type_t type;
} usb_iface_t;
int usb_init(const usb_dev_info_t *dev_info);
int usb_deinit(void);
int usb_start(void);
int usb_stop(void);
bool usb_init(const usb_dev_info_t *dev_info);
void usb_deinit(void);
void usb_start(void);
void usb_stop(void);
#endif