- 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
This commit is contained in:
Wolfvak 2019-04-29 19:20:39 -03:00 committed by d0k3
parent dad662610d
commit 77f857ab64
5 changed files with 16 additions and 28 deletions

View File

@ -68,6 +68,12 @@ static u8 CODEC_RegRead(u8 reg)
return ret; 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) static void CODEC_RegWrite(u8 reg, u8 val)
{ {
SPI_XferInfo xfer; SPI_XferInfo xfer;
@ -82,54 +88,34 @@ static void CODEC_RegWrite(u8 reg, u8 val)
SPI_DoXfer(CODEC_SPI_DEV, &xfer, 1); 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) static void CODEC_RegMask(u8 reg, u8 mask0, u8 mask1)
{ {
CODEC_RegWrite(reg, (CODEC_RegRead(reg) & ~mask1) | (mask0 & mask1)); CODEC_RegWrite(reg, (CODEC_RegRead(reg) & ~mask1) | (mask0 & mask1));
} }
// elder god magic
void CODEC_Init(void) void CODEC_Init(void)
{ {
CODEC_RegSelect(0x67); CODEC_RegSelect(0x67);
CODEC_RegWrite(0x24, 0x98); CODEC_RegWrite(0x24, 0x98);
CODEC_RegSelect(0x67);
CODEC_RegWrite(0x26, 0x00); CODEC_RegWrite(0x26, 0x00);
CODEC_RegSelect(0x67);
CODEC_RegWrite(0x25, 0x43); CODEC_RegWrite(0x25, 0x43);
CODEC_RegSelect(0x67);
CODEC_RegWrite(0x24, 0x18); CODEC_RegWrite(0x24, 0x18);
CODEC_RegSelect(0x67);
CODEC_RegWrite(0x17, 0x43); CODEC_RegWrite(0x17, 0x43);
CODEC_RegSelect(0x67);
CODEC_RegWrite(0x19, 0x69); CODEC_RegWrite(0x19, 0x69);
CODEC_RegSelect(0x67);
CODEC_RegWrite(0x1B, 0x80); CODEC_RegWrite(0x1B, 0x80);
CODEC_RegSelect(0x67);
CODEC_RegWrite(0x27, 0x11); CODEC_RegWrite(0x27, 0x11);
CODEC_RegSelect(0x67);
CODEC_RegWrite(0x26, 0xEC); CODEC_RegWrite(0x26, 0xEC);
CODEC_RegSelect(0x67);
CODEC_RegWrite(0x24, 0x18); CODEC_RegWrite(0x24, 0x18);
CODEC_RegSelect(0x67);
CODEC_RegWrite(0x25, 0x53); CODEC_RegWrite(0x25, 0x53);
CODEC_RegSelect(0x67);
CODEC_RegMask(0x26, 0x80, 0x80); CODEC_RegMask(0x26, 0x80, 0x80);
CODEC_RegSelect(0x67);
CODEC_RegMask(0x24, 0x00, 0x80); CODEC_RegMask(0x24, 0x00, 0x80);
CODEC_RegSelect(0x67);
CODEC_RegMask(0x25, 0x10, 0x3C); CODEC_RegMask(0x25, 0x10, 0x3C);
} }
void CODEC_GetRawData(u32 *buffer) void CODEC_GetRawData(u32 *buffer)
{ {
CODEC_RegSelect(0x67);
CODEC_RegRead(0x26);
CODEC_RegSelect(0xFB); CODEC_RegSelect(0xFB);
CODEC_RegReadBuf(1, buffer, 0x34); CODEC_RegReadBuf(1, buffer, 0x34);
} }

View File

@ -52,7 +52,7 @@ static const struct
{1, 0x2E}, {1, 0x2E},
{1, 0x40}, {1, 0x40},
{1, 0x44}, {1, 0x44},
{2, 0xA6}, // TODO: Find out if 0xA6 or 0xD6 is correct {2, 0xD6},
{2, 0xD0}, {2, 0xD0},
{2, 0xD2}, {2, 0xD2},
{2, 0xA4}, {2, 0xA4},

View File

@ -31,13 +31,16 @@ static fixp_t ts_mult[2];
// ts_org indicates the coordinate system origin // ts_org indicates the coordinate system origin
static int ts_org[2]; static int ts_org[2];
void HID_ReadTouchState(u16 *x, u16 *y) bool HID_ReadTouchState(u16 *x, u16 *y)
{ {
u32 ts; u32 ts;
int xc, yc; int xc, yc;
fixp_t tx, ty; fixp_t tx, ty;
ts = HID_ReadRawTouchState(); ts = HID_ReadRawTouchState();
if (ts & BIT(31))
return false;
tx = INT_TO_FIXP(HID_RAW_TX(ts) - HID_TOUCH_MIDPOINT); tx = INT_TO_FIXP(HID_RAW_TX(ts) - HID_TOUCH_MIDPOINT);
ty = INT_TO_FIXP(HID_RAW_TY(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); *x = clamp(xc, 0, (ts_org[0] * 2) - 1);
*y = clamp(yc, 0, (ts_org[1] * 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) bool HID_SetCalibrationData(const HID_CalibrationData *calibs, int point_cnt, int screen_w, int screen_h)

View File

@ -23,7 +23,7 @@ typedef struct {
} HID_CalibrationData; } HID_CalibrationData;
u32 HID_ReadRawTouchState(void); 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); bool HID_SetCalibrationData(const HID_CalibrationData *calibs, int point_cnt, int screen_w, int screen_h);
u32 InputWait(u32 timeout_sec); u32 InputWait(u32 timeout_sec);

View File

@ -117,10 +117,8 @@ bool CalibrateTouchFromFlash(void) {
u32 fw_usercfg_buf[0x100 / 0x4]; u32 fw_usercfg_buf[0x100 / 0x4];
u8* fw_usercfg = (u8*) fw_usercfg_buf; u8* fw_usercfg = (u8*) fw_usercfg_buf;
spiflash_read(0x1FE00, 0x100, fw_usercfg); spiflash_read(0x1FE00, 0x100, fw_usercfg);
if (getle16(fw_usercfg + 0x72) != crc16_quick(fw_usercfg, 0x70)) { if (getle16(fw_usercfg + 0x72) != crc16_quick(fw_usercfg, 0x70))
ShowPrompt(false, "ugh"); return false;
return false;
}
// get touchscreen calibration data from user settings // get touchscreen calibration data from user settings
u8 *ts_data = fw_usercfg + 0x58; u8 *ts_data = fw_usercfg + 0x58;