2016-10-11 12:05:55 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) Pavol Rusnak, SatoshiLabs
|
|
|
|
*
|
|
|
|
* Licensed under TREZOR License
|
|
|
|
* see LICENSE file for details
|
|
|
|
*/
|
|
|
|
|
2016-09-23 09:27:26 +00:00
|
|
|
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);
|
2016-04-29 14:02:18 +00:00
|
|
|
|
2016-09-24 16:50:15 +00:00
|
|
|
extern I2C_HandleTypeDef I2CHandle1;
|
2016-09-26 13:46:16 +00:00
|
|
|
extern void i2c_init(I2C_HandleTypeDef *i2c);
|
2016-09-24 16:50:15 +00:00
|
|
|
extern HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
|
|
|
|
|
2016-04-29 01:15:18 +00:00
|
|
|
void msg_init(void)
|
|
|
|
{
|
2016-09-26 13:46:16 +00:00
|
|
|
I2C_InitTypeDef *init = &(I2CHandle1.Init);
|
2016-10-11 11:13:14 +00:00
|
|
|
init->OwnAddress1 = 0xFE; // master
|
2016-09-26 13:46:16 +00:00
|
|
|
init->ClockSpeed = 400000;
|
|
|
|
init->DutyCycle = I2C_DUTYCYCLE_16_9;
|
|
|
|
init->AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
|
|
|
init->DualAddressMode = I2C_DUALADDRESS_DISABLED;
|
|
|
|
init->GeneralCallMode = I2C_GENERALCALL_DISABLED;
|
|
|
|
init->NoStretchMode = I2C_NOSTRETCH_DISABLED;
|
|
|
|
init->OwnAddress2 = 0;
|
|
|
|
i2c_init(&I2CHandle1);
|
2016-04-29 01:15:18 +00:00
|
|
|
}
|
|
|
|
|
2016-10-11 11:13:14 +00:00
|
|
|
ssize_t msg_recv(uint8_t *iface, uint8_t *buf, size_t len)
|
2016-04-29 01:15:18 +00:00
|
|
|
{
|
2016-10-11 11:13:14 +00:00
|
|
|
*iface = 0; // TODO: return proper interface
|
2016-09-23 09:27:26 +00:00
|
|
|
return USBD_HID_Rx(buf, len, 1);
|
2016-04-29 01:15:18 +00:00
|
|
|
}
|
|
|
|
|
2016-10-11 11:13:14 +00:00
|
|
|
ssize_t msg_send(uint8_t iface, const uint8_t *buf, size_t len)
|
2016-04-29 01:15:18 +00:00
|
|
|
{
|
2016-10-11 11:13:14 +00:00
|
|
|
(void)iface; // TODO: ignore interface for now
|
2016-10-04 16:53:32 +00:00
|
|
|
if (len > 0) {
|
|
|
|
USBD_HID_SendReport(&hUSBDDevice, (uint8_t *)buf, len);
|
|
|
|
}
|
2016-09-23 09:27:26 +00:00
|
|
|
return len;
|
2016-04-29 01:15:18 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 14:56:47 +00:00
|
|
|
// this should match values used in trezorui_poll_sdl_event() in modtrezorui/display-unix.h
|
2016-04-29 01:15:18 +00:00
|
|
|
uint32_t msg_poll_ui_event(void)
|
|
|
|
{
|
2016-09-24 16:50:15 +00:00
|
|
|
static uint8_t data[16], old_data[16];
|
|
|
|
if (HAL_OK != HAL_I2C_Master_Receive(&I2CHandle1, 56 << 1, data, 16, 1)) {
|
|
|
|
return 0; // read failure
|
|
|
|
}
|
|
|
|
if (0 == memcmp(data, old_data, 16)) {
|
|
|
|
return 0; // no new event
|
|
|
|
}
|
2016-04-29 14:02:18 +00:00
|
|
|
uint32_t r = 0;
|
2016-09-24 16:50:15 +00:00
|
|
|
if (old_data[2] == 0 && data[2] == 1) {
|
|
|
|
r = 0x00010000 + (data[4] << 8) + data[6]; // touch start
|
2016-04-29 14:02:18 +00:00
|
|
|
} else
|
2016-09-24 16:50:15 +00:00
|
|
|
if (old_data[2] == 1 && data[2] == 1) {
|
|
|
|
r = 0x00020000 + (data[4] << 8) + data[6]; // touch move
|
2016-04-29 14:02:18 +00:00
|
|
|
}
|
2016-09-24 16:50:15 +00:00
|
|
|
if (old_data[2] == 1 && data[2] == 0) {
|
|
|
|
r = 0x00040000 + (data[4] << 8) + data[6]; // touch end
|
2016-04-29 14:02:18 +00:00
|
|
|
}
|
2016-09-24 16:50:15 +00:00
|
|
|
memcpy(old_data, data, 16);
|
2016-04-29 14:02:18 +00:00
|
|
|
return r;
|
2016-04-29 01:15:18 +00:00
|
|
|
}
|