From 77f857ab64629d895365149fad0103ad5dd29c13 Mon Sep 17 00:00:00 2001 From: Wolfvak Date: Mon, 29 Apr 2019 19:20:39 -0300 Subject: [PATCH] - added way to prevent a race condition when reading the touchscreen values - simplified CODEC init and read code - fixed I2C register thanks to @profi200 for the last two points --- arm11/source/hw/codec.c | 28 +++++++--------------------- arm11/source/hw/i2c.c | 2 +- arm9/source/common/hid.c | 6 +++++- arm9/source/common/hid.h | 2 +- arm9/source/common/touchcal.c | 6 ++---- 5 files changed, 16 insertions(+), 28 deletions(-) diff --git a/arm11/source/hw/codec.c b/arm11/source/hw/codec.c index 73f7d4c..37bde49 100755 --- a/arm11/source/hw/codec.c +++ b/arm11/source/hw/codec.c @@ -68,6 +68,12 @@ static u8 CODEC_RegRead(u8 reg) return ret; } +static void CODEC_RegReadBuf(u8 reg, u32 *out, u8 size) +{ + u32 cmd = (reg << 1) | 1; + CODEC_WriteRead(&cmd, 1, out, size); +} + static void CODEC_RegWrite(u8 reg, u8 val) { SPI_XferInfo xfer; @@ -82,54 +88,34 @@ static void CODEC_RegWrite(u8 reg, u8 val) SPI_DoXfer(CODEC_SPI_DEV, &xfer, 1); } -static void CODEC_RegReadBuf(u8 reg, u32 *out, u8 size) -{ - u32 cmd = (reg << 1) | 1; - CODEC_WriteRead(&cmd, 1, out, size); -} - static void CODEC_RegMask(u8 reg, u8 mask0, u8 mask1) { CODEC_RegWrite(reg, (CODEC_RegRead(reg) & ~mask1) | (mask0 & mask1)); } +// elder god magic void CODEC_Init(void) { CODEC_RegSelect(0x67); CODEC_RegWrite(0x24, 0x98); - CODEC_RegSelect(0x67); CODEC_RegWrite(0x26, 0x00); - CODEC_RegSelect(0x67); CODEC_RegWrite(0x25, 0x43); - CODEC_RegSelect(0x67); CODEC_RegWrite(0x24, 0x18); - CODEC_RegSelect(0x67); CODEC_RegWrite(0x17, 0x43); - CODEC_RegSelect(0x67); CODEC_RegWrite(0x19, 0x69); - CODEC_RegSelect(0x67); CODEC_RegWrite(0x1B, 0x80); - CODEC_RegSelect(0x67); CODEC_RegWrite(0x27, 0x11); - CODEC_RegSelect(0x67); CODEC_RegWrite(0x26, 0xEC); - CODEC_RegSelect(0x67); CODEC_RegWrite(0x24, 0x18); - CODEC_RegSelect(0x67); CODEC_RegWrite(0x25, 0x53); - CODEC_RegSelect(0x67); CODEC_RegMask(0x26, 0x80, 0x80); - CODEC_RegSelect(0x67); CODEC_RegMask(0x24, 0x00, 0x80); - CODEC_RegSelect(0x67); CODEC_RegMask(0x25, 0x10, 0x3C); } void CODEC_GetRawData(u32 *buffer) { - CODEC_RegSelect(0x67); - CODEC_RegRead(0x26); CODEC_RegSelect(0xFB); CODEC_RegReadBuf(1, buffer, 0x34); } diff --git a/arm11/source/hw/i2c.c b/arm11/source/hw/i2c.c index 6800c31..a7c01f5 100644 --- a/arm11/source/hw/i2c.c +++ b/arm11/source/hw/i2c.c @@ -52,7 +52,7 @@ static const struct {1, 0x2E}, {1, 0x40}, {1, 0x44}, - {2, 0xA6}, // TODO: Find out if 0xA6 or 0xD6 is correct + {2, 0xD6}, {2, 0xD0}, {2, 0xD2}, {2, 0xA4}, diff --git a/arm9/source/common/hid.c b/arm9/source/common/hid.c index a7bd26d..756bbe5 100644 --- a/arm9/source/common/hid.c +++ b/arm9/source/common/hid.c @@ -31,13 +31,16 @@ static fixp_t ts_mult[2]; // ts_org indicates the coordinate system origin static int ts_org[2]; -void HID_ReadTouchState(u16 *x, u16 *y) +bool HID_ReadTouchState(u16 *x, u16 *y) { u32 ts; int xc, yc; fixp_t tx, ty; ts = HID_ReadRawTouchState(); + if (ts & BIT(31)) + return false; + tx = INT_TO_FIXP(HID_RAW_TX(ts) - HID_TOUCH_MIDPOINT); ty = INT_TO_FIXP(HID_RAW_TY(ts) - HID_TOUCH_MIDPOINT); @@ -46,6 +49,7 @@ void HID_ReadTouchState(u16 *x, u16 *y) *x = clamp(xc, 0, (ts_org[0] * 2) - 1); *y = clamp(yc, 0, (ts_org[1] * 2) - 1); + return true; } bool HID_SetCalibrationData(const HID_CalibrationData *calibs, int point_cnt, int screen_w, int screen_h) diff --git a/arm9/source/common/hid.h b/arm9/source/common/hid.h index f3c8e62..c2619fb 100644 --- a/arm9/source/common/hid.h +++ b/arm9/source/common/hid.h @@ -23,7 +23,7 @@ typedef struct { } HID_CalibrationData; u32 HID_ReadRawTouchState(void); -void HID_ReadTouchState(u16 *x, u16 *y); +bool HID_ReadTouchState(u16 *x, u16 *y); bool HID_SetCalibrationData(const HID_CalibrationData *calibs, int point_cnt, int screen_w, int screen_h); u32 InputWait(u32 timeout_sec); diff --git a/arm9/source/common/touchcal.c b/arm9/source/common/touchcal.c index 8495e40..631d17e 100644 --- a/arm9/source/common/touchcal.c +++ b/arm9/source/common/touchcal.c @@ -117,10 +117,8 @@ bool CalibrateTouchFromFlash(void) { u32 fw_usercfg_buf[0x100 / 0x4]; u8* fw_usercfg = (u8*) fw_usercfg_buf; spiflash_read(0x1FE00, 0x100, fw_usercfg); - if (getle16(fw_usercfg + 0x72) != crc16_quick(fw_usercfg, 0x70)) { - ShowPrompt(false, "ugh"); - return false; - } + if (getle16(fw_usercfg + 0x72) != crc16_quick(fw_usercfg, 0x70)) + return false; // get touchscreen calibration data from user settings u8 *ts_data = fw_usercfg + 0x58;