diff --git a/WaccaProtocolTranslator/main.c b/WaccaProtocolTranslator/main.c index e8e1b89..3d477ce 100644 --- a/WaccaProtocolTranslator/main.c +++ b/WaccaProtocolTranslator/main.c @@ -2,16 +2,17 @@ #include "pico/stdlib.h" #include "hardware/i2c.h" #include "waccaserial.h" +#include #define TOUCH_I2C_PORT i2c0 -#define TOUCH_I2C_SDA 6 -#define TOUCH_I2C_SCL 7 -#define TOUCH_PANEL_0_ADDR 0x11 -#define TOUCH_PANEL_1_ADDR 0x11 //0x12 -#define TOUCH_PANEL_2_ADDR 0x11 //0x13 -#define TOUCH_PANEL_3_ADDR 0x11 //0x14 -#define TOUCH_PANEL_4_ADDR 0x11 //0x15 -#define TOUCH_PANEL_5_ADDR 0x11 //0x16 +#define TOUCH_I2C_SDA 4 +#define TOUCH_I2C_SCL 5 +#define TOUCH_PANEL_0_ADDR 0x10 +#define TOUCH_PANEL_1_ADDR 0x10 //0x11 +#define TOUCH_PANEL_2_ADDR 0x10 //0x12 +#define TOUCH_PANEL_3_ADDR 0x10 //0x13 +#define TOUCH_PANEL_4_ADDR 0x10 //0x14 +#define TOUCH_PANEL_5_ADDR 0x10 //0x15 #define TOUCH_I2C_FREQ 100000 uint32_t touchPanel0Data = 0; @@ -25,14 +26,12 @@ uint32_t touchPanel5Data = 0; uint32_t getTouchPacket(i2c_inst_t *port, uint8_t addr) { int bytesRead = 0; uint8_t data[4]; - uint8_t dummyRegister = 0x69; - i2c_write_blocking(port, addr, &dummyRegister, 1, true); // it's okay to use blocking methods, the main protocol translation is running on a seperate cpu core bytesRead = i2c_read_blocking(port, addr, data, 4, false); if (bytesRead != 4) return 0; - return (uint32_t) (data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24)); + return (uint32_t) (data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24)); } #define PingWaccaPanel(ADDR, final) do { final = getTouchPacket(TOUCH_I2C_PORT, ADDR); } while (final == 0) @@ -57,10 +56,10 @@ void setupTouchPanels() { } int main() { - setupTouchPanels(); - ws_start(); + setupTouchPanels(); + while(1) { // note: getting a touch packet might take a bit, use this rolling update to speed up input throughput to the game touchPanel0Data = getTouchPacket(TOUCH_I2C_PORT, TOUCH_PANEL_0_ADDR); ws_setTouch0Data(touchPanel0Data); diff --git a/WaccaProtocolTranslator/waccaserial.c b/WaccaProtocolTranslator/waccaserial.c index e47ba92..ba0cde3 100644 --- a/WaccaProtocolTranslator/waccaserial.c +++ b/WaccaProtocolTranslator/waccaserial.c @@ -6,7 +6,10 @@ #include #include #include "queue.h" +#include "pico/stdio.h" #include "pico/stdio_usb.h" +#include "pico/stdlib.h" +#include // Massive thank you to the contributors for the WACVR project <3 @@ -21,20 +24,44 @@ uint32_t previousTime = 0; bool runningMulticore = false; +#ifdef DEBUG_MODE + #define led(state) gpio_put(PICO_DEFAULT_LED_PIN, state) + #define blink() led(1); sleep_ms(25); led(0); sleep_ms(75) + #define blinkL() led(1); sleep_ms(1000); led(0); sleep_ms(1000) + #define blink0() led(1); sleep_ms(1); led(0); sleep_ms(75) +#endif + void ws_start() { - stdio_usb_init(); + stdio_init_all(); + stdio_flush(); + + stdio_filter_driver(NULL); + + #ifdef DEBUG_MODE + gpio_init(PICO_DEFAULT_LED_PIN); + gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); + + blinkL(); + #endif previousTime = to_ms_since_boot(get_absolute_time()); + + multicore_launch_core1(ws_touchThreadLoop); + runningMulticore = true; } void ws_touchThreadLoop() { while (true) { + #ifdef DEBUG_MODE + blink0(); + #endif + uint32_t currentTime = to_ms_since_boot(get_absolute_time()); elapsedTime += currentTime - previousTime; previousTime = currentTime; - if (elapsedTime >= 1000) { - elapsedTime -= 1000; + if (elapsedTime >= 10) { + elapsedTime -= 10; enqueue(&touchQueueHead, 1); } @@ -43,7 +70,12 @@ void ws_touchThreadLoop() { ws_sendTouchState(); } - if (stdio_usb_connected()) ws_readHead(); + //if (StartUp) ws_sendTouchState(); + + //ws_sendTouchState(); + + //if (stdio_usb_connected()) ws_readHead(); + ws_readHead(); } } @@ -52,9 +84,9 @@ void ws_sendTouchState() { } void ws_readHead() { - uint8_t inByte = getchar_timeout_us(0); + inByte = getchar_timeout_us(0); - if (inByte != 0) { + if (inByte != 0xff && inByte != 0) { uint8_t data = 0; uint8_t importantData = 0; int i = 0; @@ -62,7 +94,11 @@ void ws_readHead() { data = getchar_timeout_us(0); if (i == 2) importantData = data; i++; - } while (data != 0); + } while (data != 0xff && data != 0); + + #ifdef DEBUG_MODE + blink(); + #endif ws_sendResp(importantData); } @@ -71,6 +107,10 @@ void ws_readHead() { void ws_sendResp(char importantNextReadData) { switch (inByte) { case CMD_GET_SYNC_BOARD_VER: + #ifdef DEBUG_MODE + blink(); + #endif + StartUp = false; putchar_raw(inByte); @@ -82,6 +122,11 @@ void ws_sendResp(char importantNextReadData) { putchar_raw(44); break; case CMD_NEXT_READ: + #ifdef DEBUG_MODE + blink(); + blink(); + #endif + StartUp = false; switch (importantNextReadData) { case 0x30: @@ -110,6 +155,12 @@ void ws_sendResp(char importantNextReadData) { } break; case CMD_GET_UNIT_BOARD_VER: + #ifdef DEBUG_MODE + blink(); + blink(); + blink(); + #endif + putchar_raw(inByte); for (int i = 0; i < 6; i++) { @@ -138,6 +189,13 @@ void ws_sendResp(char importantNextReadData) { break; case CMD_MYSTERY1: + #ifdef DEBUG_MODE + blink(); + blink(); + blink(); + blink(); + #endif + StartUp = false; for (int i = 0; i < 7; i++) { @@ -145,6 +203,10 @@ void ws_sendResp(char importantNextReadData) { } break; case CMD_MYSTERY2: + #ifdef DEBUG_MODE + blink(); + #endif + StartUp = false; for (int i = 0; i < 7; i++) { @@ -152,16 +214,24 @@ void ws_sendResp(char importantNextReadData) { } break; case CMD_START_AUTO_SCAN: + #ifdef DEBUG_MODE + blink(); + blink(); + blink(); + blink(); + blink(); + #endif + for (int i = 0; i < 7; i++) { putchar_raw(SettingData_201[i]); } StartUp = true; - if (!runningMulticore) { + /*if (!runningMulticore) { multicore_launch_core1(ws_touchThreadLoop); runningMulticore = true; - } + }*/ break; case CMD_BEGIN_WRITE: break; @@ -194,9 +264,9 @@ void ws_sendTouch() { // since i want the wacca panels to be as simple as possible, i need to reverse the input order on the right side to fit with how the game reads data #ifdef RIGHT uint8_t reverseAndShift(uint8_t b) { - b = (b & 0xF0) >> 4 | (b & 0x0F) << 4; - b = (b & 0xCC) >> 2 | (b & 0x33) << 2; - b = (b & 0xAA) >> 1 | (b & 0x55) << 1; + b = ((b & 0xF0) >> 4) | ((b & 0x0F) << 4); + b = ((b & 0xCC) >> 2) | ((b & 0x33) << 2); + b = ((b & 0xAA) >> 1) | ((b & 0x55) << 1); return b >> 3; } #endif diff --git a/WaccaProtocolTranslator/waccaserial.h b/WaccaProtocolTranslator/waccaserial.h index 4c2a9dd..b39a8d9 100644 --- a/WaccaProtocolTranslator/waccaserial.h +++ b/WaccaProtocolTranslator/waccaserial.h @@ -8,11 +8,16 @@ #include #include #include "queue.h" +#include "pico/stdio.h" #include "pico/stdio_usb.h" +#include "pico/stdlib.h" +#include // Massive thank you to the contributors for the WACVR project <3 -//#define RIGHT //uncomment to enable righty flip on interface and sync board data swap +//#define DEBUG_MODE // uncomment for debug messages (note: this will slow things down!) + +#define RIGHT //uncomment to enable righty flip on interface and sync board data swap #define CMD_GET_SYNC_BOARD_VER 0xa0 #define CMD_NEXT_READ 0x72 diff --git a/WaccaTouchPanel/main.c b/WaccaTouchPanel/main.c index b5d7265..bc19fa5 100644 --- a/WaccaTouchPanel/main.c +++ b/WaccaTouchPanel/main.c @@ -5,29 +5,24 @@ #include "hardware/irq.h" #include "mpr121.h" -#define DEBUG_MODE // uncomment for debug messages (note: this will slow things down!) +//#define DEBUG_MODE // uncomment for debug messages (note: this will slow things down!) #define MOTHERBOARD_I2C_PORT i2c1 -#define MOTHERBOARD_I2C_HWID 0x10 // 0x1[1-6 depending on which position it's in bottom to top] -#define MOTHERBOARD_I2C_SDA 4 -#define MOTHERBOARD_I2C_SCL 5 +#define MOTHERBOARD_I2C_IRQ I2C1_IRQ +#define MOTHERBOARD_I2C_HWID 0x10 // 0x1[0-5 depending on which position it's in bottom to top] +#define MOTHERBOARD_I2C_SDA 2 +#define MOTHERBOARD_I2C_SCL 3 #define MOTHERBOARD_I2C_FREQ 100000 #define TOUCH_I2C_PORT i2c0 -#define TOUCH_I2C_SDA 6 -#define TOUCH_I2C_SCL 7 +#define TOUCH_I2C_SDA 4 +#define TOUCH_I2C_SCL 5 #define INNER_MPR121_ADDR 0x5A #define OUTER_MPR121_ADDR 0x5B #define TOUCH_I2C_FREQ 100000 #define MPR121_TOUCH_THRESHOLD 16 #define MPR121_RELEASE_THRESHOLD 10 -typedef enum i2c_slave_event_t { - I2C_SLAVE_RECEIVE, - I2C_SLAVE_REQUEST, - I2C_SLAVE_FINISH, -} i2c_slave_event_t; - // Buttons // [--] [--] [--] [--] [--] // [--] [--] [--] [--] [--] @@ -56,78 +51,28 @@ uint32_t previousTouchDataPacket = 0; bool writingPacket = false; uint32_t touchDataPacket = 0; -bool transferInProgress; +uint8_t byteSelector = 0; -static inline uint8_t i2c_read_byte(i2c_inst_t *i2c) { - i2c_hw_t *hw = i2c_get_hw(i2c); - assert(hw->status & I2C_IC_STATUS_RFNE_BITS); - return (uint8_t) hw->data_cmd; -} +#define txByte(i2c, byte) while (!i2c_get_write_available(i2c)) { ; } i2c->hw->data_cmd = byte -static inline void i2c_write_byte(i2c_inst_t *i2c, uint8_t value) { - i2c_hw_t *hw = i2c_get_hw(i2c); - assert(hw->status & I2C_IC_STATUS_TFNF_BITS); - hw->data_cmd = value; -} +void i2cSlaveI2CIRQHandler() { + uint32_t status = MOTHERBOARD_I2C_PORT->hw->intr_stat; -static void i2cSlaveHandler(i2c_inst_t *i2c, i2c_slave_event_t event) { - switch (event) { - case I2C_SLAVE_RECEIVE: - break; - case I2C_SLAVE_REQUEST: ; - uint32_t dataToSend = previousTouchDataPacket; + if (status & I2C_IC_INTR_STAT_R_RD_REQ_BITS) { + uint32_t dataToSend = previousTouchDataPacket; - if (!writingPacket) dataToSend = touchDataPacket; + if (!writingPacket) dataToSend = touchDataPacket; - #ifdef DEBUG_MODE - printf("Master requested packet, sending [%u]...\n", dataToSend); - #endif + #ifdef DEBUG_MODE + printf("Master requested packet, sending [%u %u %u %u]...\n", (uint8_t) (dataToSend), (uint8_t) (dataToSend >> 8), (uint8_t) (dataToSend >> 16), (uint8_t) (dataToSend >> 24)); + #endif - i2c_write_byte(MOTHERBOARD_I2C_PORT, (uint8_t) dataToSend); - i2c_write_byte(MOTHERBOARD_I2C_PORT, (uint8_t) dataToSend >> 8); - i2c_write_byte(MOTHERBOARD_I2C_PORT, (uint8_t) dataToSend >> 16); - i2c_write_byte(MOTHERBOARD_I2C_PORT, (uint8_t) dataToSend >> 24); + txByte(MOTHERBOARD_I2C_PORT, (uint8_t) (dataToSend)); + txByte(MOTHERBOARD_I2C_PORT, (uint8_t) (dataToSend >> 8)); + txByte(MOTHERBOARD_I2C_PORT, (uint8_t) (dataToSend >> 16)); + txByte(MOTHERBOARD_I2C_PORT, (uint8_t) (dataToSend >> 24)); - break; - case I2C_SLAVE_FINISH: - break; - default: - break; - } -} - -static inline void finishTransfer() { - if (transferInProgress) { - i2cSlaveHandler(MOTHERBOARD_I2C_PORT, I2C_SLAVE_FINISH); - transferInProgress = false; - } -} - -static void __not_in_flash_func(i2cSlaveI2CIRQHandler)() { - uint32_t intrStat = MOTHERBOARD_I2C_PORT->hw->intr_stat; - if (intrStat == 0) { - return; - } - if (intrStat & I2C_IC_INTR_STAT_R_TX_ABRT_BITS) { - MOTHERBOARD_I2C_PORT->hw->clr_tx_abrt; - finishTransfer(); - } - if (intrStat & I2C_IC_INTR_STAT_R_START_DET_BITS) { - MOTHERBOARD_I2C_PORT->hw->clr_start_det; - finishTransfer(); - } - if (intrStat & I2C_IC_INTR_STAT_R_STOP_DET_BITS) { - MOTHERBOARD_I2C_PORT->hw->clr_stop_det; - finishTransfer(); - } - if (intrStat & I2C_IC_INTR_STAT_R_RX_FULL_BITS) { - transferInProgress = true; - i2cSlaveHandler(MOTHERBOARD_I2C_PORT, I2C_SLAVE_RECEIVE); - } - if (intrStat & I2C_IC_INTR_STAT_R_RD_REQ_BITS) { MOTHERBOARD_I2C_PORT->hw->clr_rd_req; - transferInProgress = true; - i2cSlaveHandler(MOTHERBOARD_I2C_PORT, I2C_SLAVE_REQUEST); } } @@ -151,25 +96,22 @@ void setupTouch() { void setupMotherboard() { i2c_init(MOTHERBOARD_I2C_PORT, MOTHERBOARD_I2C_FREQ); - - gpio_set_function(MOTHERBOARD_I2C_SDA, GPIO_FUNC_I2C); - gpio_set_function(MOTHERBOARD_I2C_SCL, GPIO_FUNC_I2C); - - gpio_pull_up(MOTHERBOARD_I2C_SDA); - gpio_pull_up(MOTHERBOARD_I2C_SCL); + i2c_set_slave_mode(MOTHERBOARD_I2C_PORT, true, MOTHERBOARD_I2C_HWID); MOTHERBOARD_I2C_PORT->hw->enable = 0; hw_set_bits(&MOTHERBOARD_I2C_PORT->hw->con, I2C_IC_CON_RX_FIFO_FULL_HLD_CTRL_BITS); MOTHERBOARD_I2C_PORT->hw->enable = 1; - uint i2cHardwareIndex = i2c_hw_index(MOTHERBOARD_I2C_PORT); - i2c_set_slave_mode(MOTHERBOARD_I2C_PORT, false, MOTHERBOARD_I2C_HWID); + gpio_set_function(MOTHERBOARD_I2C_SDA, GPIO_FUNC_I2C); + gpio_set_function(MOTHERBOARD_I2C_SCL, GPIO_FUNC_I2C); + gpio_pull_up(MOTHERBOARD_I2C_SDA); + gpio_pull_up(MOTHERBOARD_I2C_SCL); - MOTHERBOARD_I2C_PORT->hw->intr_mask = I2C_IC_INTR_MASK_M_RX_FULL_BITS | I2C_IC_INTR_MASK_M_RD_REQ_BITS | I2C_IC_RAW_INTR_STAT_TX_ABRT_BITS | I2C_IC_INTR_MASK_M_STOP_DET_BITS | I2C_IC_INTR_MASK_M_START_DET_BITS; + MOTHERBOARD_I2C_PORT->hw->intr_mask = I2C_IC_INTR_MASK_M_RD_REQ_BITS;//(I2C_IC_INTR_MASK_M_RD_REQ_BITS | I2C_IC_INTR_MASK_M_RX_FULL_BITS); - uint IRQNumber = I2C0_IRQ + i2cHardwareIndex; - irq_set_exclusive_handler(IRQNumber, i2cSlaveI2CIRQHandler); - irq_set_enabled(IRQNumber, true); + irq_set_exclusive_handler(MOTHERBOARD_I2C_IRQ, i2cSlaveI2CIRQHandler); + + irq_set_enabled(MOTHERBOARD_I2C_IRQ, true); } #ifdef DEBUG_MODE @@ -182,13 +124,15 @@ int main() { stdio_init_all(); printf("Wacca Touch Panel v0.1\n"); + printf("Setting up Touch..."); setupTouch(); - printf("Touch Ready\n"); + printf("OK\n"); + printf("Setting up Communications..."); setupMotherboard(); - printf("Communications Ready\n"); + printf("OK\n"); - while(1) { + while(true) { mpr121_touched(&touchedInner, &innerSensor); mpr121_touched(&touchedOuter, &outerSensor); @@ -197,7 +141,7 @@ int main() { writingPacket = true; - touchDataPacket = ((uint32_t) touchedOuter << 12 | touchedInner) | 0x06900000; + touchDataPacket = (((uint32_t) touchedOuter << 12) | touchedInner) | 0x69000000; #ifdef DEBUG_MODE printf("-------------------\n");