i2c: reorganize sources

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

@ -5,15 +5,13 @@
* see LICENSE file for details
*/
#include "touch.h"
extern I2C_HandleTypeDef i2c_handle;
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);
void msg_init(void)
{
touch_init();
}
ssize_t msg_recv(uint8_t *iface, uint8_t *buf, size_t len)
@ -33,5 +31,23 @@ ssize_t msg_send(uint8_t iface, const uint8_t *buf, size_t len)
uint32_t msg_poll_touch(void)
{
return touch_read();
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;
}

@ -1,41 +0,0 @@
extern void i2c_init(I2C_HandleTypeDef *i2c);
extern HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
static I2C_HandleTypeDef touch_i2c_handle;
void touch_init(void)
{
I2C_InitTypeDef *init = &(touch_i2c_handle.Init);
init->OwnAddress1 = 0xFE; // master
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(&touch_i2c_handle);
}
uint32_t touch_read(void)
{
static uint8_t data[16], old_data[16];
if (HAL_OK != HAL_I2C_Master_Receive(&touch_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;
}

@ -19,6 +19,7 @@ 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);
int main(void) {
@ -50,6 +51,7 @@ int main(void) {
pendsv_init();
flash_init();
usb_init();
i2c_init();
// TODO: sdcard

@ -1,10 +1,10 @@
#include STM32_HAL_H
I2C_HandleTypeDef *i2c_handle = 0;
I2C_HandleTypeDef i2c_handle;
void __fatal_error(const char *msg);
void i2c_init(I2C_HandleTypeDef *i2c) {
void i2c_init(void) {
// Enable I2C clock
__HAL_RCC_I2C1_CLK_ENABLE();
@ -19,14 +19,23 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
};
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
I2C_InitTypeDef *init = &(i2c_handle.Init);
init->OwnAddress1 = 0xFE; // master
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;
// Init I2C handle
if (HAL_I2C_Init(i2c) != HAL_OK) {
if (HAL_I2C_Init(&i2c_handle) != HAL_OK) {
__fatal_error("i2c_init failed");
return;
}
// Enable IRQs
i2c_handle = i2c;
HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);
HAL_NVIC_EnableIRQ(I2C1_ER_IRQn);
}

@ -736,22 +736,16 @@ void FLASH_IRQHandler(void) {
// }
// #endif // MICROPY_HW_ENABLE_CAN
extern I2C_HandleTypeDef *i2c_handle;
extern I2C_HandleTypeDef i2c_handle;
void I2C1_EV_IRQHandler(void) {
printf("ev\n");
IRQ_ENTER(I2C1_EV_IRQn);
if (i2c_handle) {
HAL_I2C_EV_IRQHandler(i2c_handle);
}
HAL_I2C_EV_IRQHandler(&i2c_handle);
IRQ_EXIT(I2C1_EV_IRQn);
}
void I2C1_ER_IRQHandler(void) {
printf("er\n");
IRQ_ENTER(I2C1_ER_IRQn);
if (i2c_handle) {
HAL_I2C_ER_IRQHandler(i2c_handle);
}
HAL_I2C_ER_IRQHandler(&i2c_handle);
IRQ_EXIT(I2C1_ER_IRQn);
}

Loading…
Cancel
Save