trezorhal: fix i2c, more reorg

pull/25/head
Pavol Rusnak 7 years ago
parent 3f0026912c
commit 935e9b2912
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

@ -312,16 +312,17 @@ OBJ_MP += $(addprefix $(BUILD_MP)/,\
# OBJ micropython/
OBJ_FW += $(addprefix $(BUILD_FW)/, \
firmware/main.o \
firmware/main.o \
trezorhal/flash.o \
trezorhal/i2c.o \
trezorhal/rng.o \
trezorhal/rng.o \
trezorhal/sdcard.o \
trezorhal/touch.o \
trezorhal/usb.o \
trezorhal/stm32_it.o \
trezorhal/stm32_it.o \
trezorhal/stm32_system.o \
trezorhal/hal/stm32f4xx_hal_sram.o \
trezorhal/hal/stm32f4xx_hal_sram.o \
trezorhal/hal/stm32f4xx_ll_fsmc.o \
)
)
# make a list of all the .py files that need compiling and freezing
FROZEN_MPY_PY_FILES := $(shell find -L $(FROZEN_MPY_DIR) -type f -name '*.py' | $(SED) -e 's=^$(FROZEN_MPY_DIR)/==')

@ -1,42 +0,0 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __BOOTLOADER_SDCARD_H__
#define __BOOTLOADER_SDCARD_H__
#include <stdbool.h>
// this is a fixed size and should not be changed
#define SDCARD_BLOCK_SIZE (512)
void sdcard_init(void);
bool sdcard_is_present(void);
bool sdcard_power_on(void);
void sdcard_power_off(void);
uint64_t sdcard_get_capacity_in_bytes(void);
uint32_t sdcard_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks);
#endif

@ -5,7 +5,8 @@
* see LICENSE file for details
*/
extern I2C_HandleTypeDef i2c_handle;
#include "touch.h"
extern struct _USBD_HandleTypeDef hUSBDDevice;
extern uint8_t USBD_HID_SendReport(struct _USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len);
extern int USBD_HID_Rx(uint8_t *buf, uint32_t len, uint32_t timeout);
@ -31,23 +32,5 @@ ssize_t msg_send(uint8_t iface, const uint8_t *buf, size_t len)
uint32_t msg_poll_touch(void)
{
static uint8_t data[16], old_data[16];
if (HAL_OK != HAL_I2C_Master_Receive(&i2c_handle, 56 << 1, data, 16, 1)) {
return 0; // read failure
}
if (0 == memcmp(data, old_data, 16)) {
return 0; // no new event
}
uint32_t r = 0;
if (old_data[2] == 0 && data[2] == 1) {
r = 0x00010000 + (data[4] << 8) + data[6]; // touch start
} else
if (old_data[2] == 1 && data[2] == 1) {
r = 0x00020000 + (data[4] << 8) + data[6]; // touch move
}
if (old_data[2] == 1 && data[2] == 0) {
r = 0x00040000 + (data[4] << 8) + data[6]; // touch end
}
memcpy(old_data, data, 16);
return r;
return touch_read();
}

@ -13,13 +13,25 @@
#include "gccollect.h"
#include "pendsv.h"
#include "flash.h"
#include "rng.h"
#include "sdcard.h"
#include "touch.h"
#include "usb.h"
void SystemClock_Config(void);
void USBD_CDC_TxAlways(const uint8_t * buf, uint32_t len);
int USBD_CDC_Rx(uint8_t * buf, uint32_t len, uint32_t timeout);
void flash_init(void);
void usb_init(void);
void i2c_init(void);
// Errors
void NORETURN nlr_jump_fail(void *val) {
for (;;) {}
}
void NORETURN __fatal_error(const char *msg) {
for (;;) {}
}
int main(void) {
@ -49,11 +61,26 @@ int main(void) {
RCC->CSR |= RCC_CSR_RMVF;
pendsv_init();
flash_init();
usb_init();
i2c_init();
// TODO: sdcard
if (0 != flash_init()) {
__fatal_error("flash_init failed");
}
if (0 != rng_init()) {
__fatal_error("rng_init failed");
}
if (0 != sdcard_init()) {
__fatal_error("sdcard_init failed");
}
if (0 != touch_init()) {
__fatal_error("touch_init failed");
}
if (0 != usb_init()) {
__fatal_error("usb_init failed");
}
for (;;) {
// Stack limit should be less than real stack size, so we have a chance
@ -80,16 +107,6 @@ int main(void) {
return 0;
}
// Errors
void NORETURN nlr_jump_fail(void *val) {
for (;;) {}
}
void NORETURN __fatal_error(const char *msg) {
for (;;) {}
}
#ifndef NDEBUG
void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) {
printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line);

@ -1,11 +1,11 @@
#include STM32_HAL_H
void __fatal_error(const char *msg);
void flash_init(void) {
int flash_init(void) {
// Enable the flash IRQ, which is used to also call our storage IRQ handler
// It needs to go at a higher priority than all those components that rely on
// the flash storage (eg higher than USB MSC).
HAL_NVIC_SetPriority(FLASH_IRQn, 2, 0);
HAL_NVIC_EnableIRQ(FLASH_IRQn);
return 0;
}

@ -0,0 +1,6 @@
#ifndef __TREZORHAL_FLASH_H__
#define __TREZORHAL_FLASH_H__
int flash_init(void);
#endif

@ -5,19 +5,12 @@ static RNG_HandleTypeDef rng_handle = {
.Instance = RNG,
};
void rng_init(RNG_HandleTypeDef *rng) {
// Enable RNG clock
int rng_init(void) {
__HAL_RCC_RNG_CLK_ENABLE();
// Init RNG handle
HAL_RNG_Init(rng);
HAL_RNG_Init(&rng_handle);
return 0;
}
uint32_t rng_get(void) {
if (rng_handle.State == HAL_RNG_STATE_RESET) {
rng_init(&rng_handle);
}
return HAL_RNG_GetRandomNumber(&rng_handle);
}

@ -0,0 +1,7 @@
#ifndef __TREZORHAL_RNG_H__
#define __TREZORHAL_RNG_H__
int rng_init(void);
uint32_t rng_get(void);
#endif

@ -1,29 +1,3 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include STM32_HAL_H
#include <string.h>
@ -35,7 +9,7 @@
static SD_HandleTypeDef sd_handle;
void sdcard_init(void) {
int sdcard_init(void) {
// invalidate the sd_handle
sd_handle.Instance = NULL;
@ -57,6 +31,8 @@ void sdcard_init(void) {
GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
GPIO_InitStructure.Pin = GPIO_PIN_13;
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
return 0;
}
void HAL_SD_MspInit(SD_HandleTypeDef *hsd) {

@ -0,0 +1,16 @@
#ifndef __TREZORHAL_SDCARD_H__
#define __TREZORHAL_SDCARD_H__
#include <stdbool.h>
// this is a fixed size and should not be changed
#define SDCARD_BLOCK_SIZE (512)
int sdcard_init(void);
bool sdcard_is_present(void);
bool sdcard_power_on(void);
void sdcard_power_off(void);
uint64_t sdcard_get_capacity_in_bytes(void);
uint32_t sdcard_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks);
#endif

@ -1,14 +1,18 @@
#include STM32_HAL_H
I2C_HandleTypeDef i2c_handle;
#include <string.h>
void __fatal_error(const char *msg);
I2C_HandleTypeDef i2c_handle = {
.Instance = I2C1,
};
void i2c_init(void) {
int touch_init(void) {
// Enable I2C clock
__HAL_RCC_I2C1_CLK_ENABLE();
__I2C1_CLK_ENABLE();
// Init SCL and SDA GPIO lines (PB6 & PB7)
GPIO_InitTypeDef GPIO_InitStructure = {
.Pin = GPIO_PIN_6 | GPIO_PIN_7,
@ -31,11 +35,34 @@ void i2c_init(void) {
// Init I2C handle
if (HAL_I2C_Init(&i2c_handle) != HAL_OK) {
__fatal_error("i2c_init failed");
return;
return 1;
}
// Enable IRQs
HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);
HAL_NVIC_EnableIRQ(I2C1_ER_IRQn);
return 0;
}
uint32_t touch_read(void) {
static uint8_t data[16], old_data[16];
if (HAL_OK != HAL_I2C_Master_Receive(&i2c_handle, 56 << 1, data, 16, 1)) {
return 0; // read failure
}
if (0 == memcmp(data, old_data, 16)) {
return 0; // no new event
}
uint32_t r = 0;
if (old_data[2] == 0 && data[2] == 1) {
r = 0x00010000 + (data[4] << 8) + data[6]; // touch start
} else
if (old_data[2] == 1 && data[2] == 1) {
r = 0x00020000 + (data[4] << 8) + data[6]; // touch move
}
if (old_data[2] == 1 && data[2] == 0) {
r = 0x00040000 + (data[4] << 8) + data[6]; // touch end
}
memcpy(old_data, data, 16);
return r;
}

@ -0,0 +1,7 @@
#ifndef __TREZORHAL_TOUCH_H__
#define __TREZORHAL_TOUCH_H__
int touch_init(void);
uint32_t touch_read(void);
#endif

@ -8,9 +8,7 @@
USBD_HandleTypeDef hUSBDDevice;
void __fatal_error(const char *msg);
void usb_init(void) {
int usb_init(void) {
const uint16_t vid = 0x1209;
const uint16_t pid = 0x53C1;
@ -25,12 +23,13 @@ void usb_init(void) {
USBD_SetVIDPIDRelease(vid, pid, 0x0200, 0);
if (USBD_SelectMode(USBD_MODE_CDC_HID, &hid_info) != 0) {
__fatal_error("USB init failed");
return;
return 1;
}
USBD_Init(&hUSBDDevice, (USBD_DescriptorsTypeDef*)&USBD_Descriptors, 0); // 0 == full speed
USBD_RegisterClass(&hUSBDDevice, &USBD_CDC_MSC_HID);
USBD_CDC_RegisterInterface(&hUSBDDevice, (USBD_CDC_ItfTypeDef*)&USBD_CDC_fops);
USBD_HID_RegisterInterface(&hUSBDDevice, (USBD_HID_ItfTypeDef*)&USBD_HID_fops);
USBD_Start(&hUSBDDevice);
return 0;
}

@ -0,0 +1,6 @@
#ifndef __TREZORHAL_USB_H__
#define __TREZORHAL_USB_H__
int usb_init(void);
#endif
Loading…
Cancel
Save