From 054bc386830e25cec18ba00c1746584ef39c63b9 Mon Sep 17 00:00:00 2001 From: kuwoyuki Date: Tue, 2 Dec 2025 20:39:09 +0600 Subject: [PATCH] fix: don't pull REN up on startup before DMM is online --- main.c | 100 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 58 insertions(+), 42 deletions(-) diff --git a/main.c b/main.c index 0bf4fa6..d07e9de 100644 --- a/main.c +++ b/main.c @@ -55,12 +55,12 @@ #define MENU_DOT_INTERVAL_MS 500 // Speed of "..." addition #define MENU_COMMIT_DELAY_MS 2400 // Time to hold before entering mode #define MENU_SUBLAYER_DELAY_MS 500 // Time to "hover" before dots start -#define MENU_DEBOUNCE_MS 200 // Button press dead-time +#define MENU_DEBOUNCE_MS 100 // Button press dead-time #define MENU_LOCKOUT_MS 1000 // How long to ignore SRQ for after exiting menu // Polling #define POLL_INTERVAL_MS 100 // 10Hz polling when in Passthrough -#define DMM_RECOVERY_DELAY_MS 1000 // Backoff if DMM vanishes +#define DMM_RECOVERY_DELAY_MS 2000 // Backoff if DMM vanishes // Diode sound #define DIODE_TH_SHORT 0.050 // Volts (below this = SHORT) @@ -1007,7 +1007,6 @@ int gpib_read_byte(uint8_t* data, int* eoi_asserted) { // Sets up Talker/Listener for data transfer int gpib_start_session(uint8_t target_addr, session_mode_t mode) { - printf("gpib_start_session\n"); GPIB_ASSERT(PIN_ATN); Delay_Us(1); @@ -1159,30 +1158,41 @@ int gpib_serial_poll(uint8_t addr, uint8_t* status) { if (gpib_write_byte(GPIB_CMD_LAD | MY_ADDR, 0) < 0) goto err; if (gpib_write_byte(GPIB_CMD_TAD | addr, 0) < 0) goto err; - // gpib_write_data(0x00); // Float data lines - // GPIB_ASSERT(PIN_NDAC); // Busy / Not Accepted - // GPIB_ASSERT(PIN_NRFD); // Busy / Not Ready - // Delay_Us(5); - - // drop ATN to read data - GPIB_RELEASE(PIN_ATN); + gpib_write_data(0x00); // Float data lines + GPIB_ASSERT(PIN_NDAC); // Busy / Not Accepted + GPIB_ASSERT(PIN_NRFD); // Busy / Not Ready + GPIB_RELEASE(PIN_ATN); // Handover to data mode int eoi; - int res = gpib_read_byte(status, &eoi); + if (gpib_read_byte(status, &eoi) < 0) { + goto err_data; + } - // handshake complete, clean up lines + GPIB_ASSERT(PIN_ATN); + Delay_Us(1); + + // return to cmd mode GPIB_RELEASE(PIN_NRFD); GPIB_RELEASE(PIN_NDAC); - // end seq: ATN -> SPD -> UNT - GPIB_ASSERT(PIN_ATN); - gpib_write_byte(GPIB_CMD_SPD, 0); // disable spoll - gpib_write_byte(GPIB_CMD_UNT, 0); // untalk + // end seq: SPD -> UNT + gpib_write_byte(GPIB_CMD_SPD, 0); + gpib_write_byte(GPIB_CMD_UNT, 0); GPIB_RELEASE(PIN_ATN); - return res; + return 0; + +err_data: + // if read failed, assert ATN and release those lines + GPIB_ASSERT(PIN_ATN); + Delay_Us(1); + GPIB_RELEASE(PIN_NRFD); + GPIB_RELEASE(PIN_NDAC); err: + // just ensure we don't leave the device in spoll mode + gpib_write_byte(GPIB_CMD_SPD, 0); + gpib_write_byte(GPIB_CMD_UNT, 0); GPIB_RELEASE(PIN_ATN); return -1; } @@ -1334,18 +1344,14 @@ int gpib_receive_binary(uint8_t addr, char* buf, int expected_len) { } void gpib_init(void) { - // configure control lines as open-drain outputs - funPinMode(PIN_EOI, GPIO_Speed_50MHz | GPIO_CNF_OUT_OD); - funPinMode(PIN_REN, GPIO_Speed_50MHz | GPIO_CNF_OUT_OD); - funPinMode(PIN_ATN, GPIO_Speed_50MHz | GPIO_CNF_OUT_OD); - funPinMode(PIN_IFC, GPIO_Speed_50MHz | GPIO_CNF_OUT_OD); - funPinMode(PIN_DAV, GPIO_Speed_50MHz | GPIO_CNF_OUT_OD); - funPinMode(PIN_NDAC, GPIO_Speed_50MHz | GPIO_CNF_OUT_OD); - funPinMode(PIN_NRFD, GPIO_Speed_50MHz | GPIO_CNF_OUT_OD); - - // SRQ is input with pull-up - funPinMode(PIN_SRQ, GPIO_CNF_IN_PUPD); - funDigitalWrite(PIN_SRQ, 1); + // calculate BSHR for the DIO lines + for (int i = 0; i < 256; i++) { + gpib_write_lut[i] = + CALC_PIN_BSHR(i, 0, PIN_POS_D1) | CALC_PIN_BSHR(i, 1, PIN_POS_D2) | + CALC_PIN_BSHR(i, 2, PIN_POS_D3) | CALC_PIN_BSHR(i, 3, PIN_POS_D4) | + CALC_PIN_BSHR(i, 4, PIN_POS_D5) | CALC_PIN_BSHR(i, 5, PIN_POS_D6) | + CALC_PIN_BSHR(i, 6, PIN_POS_D7) | CALC_PIN_BSHR(i, 7, PIN_POS_D8); + } // float all control lines GPIB_RELEASE(PIN_EOI); @@ -1356,6 +1362,18 @@ void gpib_init(void) { GPIB_RELEASE(PIN_NDAC); GPIB_RELEASE(PIN_NRFD); + // configure control lines as open-drain outputs + funPinMode(PIN_EOI, GPIO_Speed_50MHz | GPIO_CNF_OUT_OD); + funPinMode(PIN_REN, GPIO_Speed_50MHz | GPIO_CNF_OUT_OD); + funPinMode(PIN_ATN, GPIO_Speed_50MHz | GPIO_CNF_OUT_OD); + funPinMode(PIN_IFC, GPIO_Speed_50MHz | GPIO_CNF_OUT_OD); + funPinMode(PIN_DAV, GPIO_Speed_50MHz | GPIO_CNF_OUT_OD); + funPinMode(PIN_NDAC, GPIO_Speed_50MHz | GPIO_CNF_OUT_OD); + funPinMode(PIN_NRFD, GPIO_Speed_50MHz | GPIO_CNF_OUT_OD); + + // float data lines + gpib_write_data(0x00); + // data lines funPinMode(PIN_DIO1, GPIO_Speed_50MHz | GPIO_CNF_OUT_OD); funPinMode(PIN_DIO2, GPIO_Speed_50MHz | GPIO_CNF_OUT_OD); @@ -1366,17 +1384,12 @@ void gpib_init(void) { funPinMode(PIN_DIO7, GPIO_Speed_50MHz | GPIO_CNF_OUT_OD); funPinMode(PIN_DIO8, GPIO_Speed_50MHz | GPIO_CNF_OUT_OD); - // calculate BSHR for the DIO lines - for (int i = 0; i < 256; i++) { - gpib_write_lut[i] = - CALC_PIN_BSHR(i, 0, PIN_POS_D1) | CALC_PIN_BSHR(i, 1, PIN_POS_D2) | - CALC_PIN_BSHR(i, 2, PIN_POS_D3) | CALC_PIN_BSHR(i, 3, PIN_POS_D4) | - CALC_PIN_BSHR(i, 4, PIN_POS_D5) | CALC_PIN_BSHR(i, 5, PIN_POS_D6) | - CALC_PIN_BSHR(i, 6, PIN_POS_D7) | CALC_PIN_BSHR(i, 7, PIN_POS_D8); - } + // SRQ is input with pull-up + funPinMode(PIN_SRQ, GPIO_CNF_IN_PUPD); + funDigitalWrite(PIN_SRQ, 1); - // float data lines - gpib_write_data(0x00); + // clr + gpib_interface_clear(); } // ------------------------------------ @@ -2297,7 +2310,8 @@ void handle_menu_navigation(void) { // only poll GPIB if physical line is asserted if (gpib_check_srq()) { uint8_t stb = 0; - gpib_serial_poll(app.dmm_addr, &stb); + + if (gpib_serial_poll(app.dmm_addr, &stb) != 0) return; // check if it was the front panel btn if (stb & HP3478A_MASK_KEYBOARD_SRQ) { @@ -2429,7 +2443,6 @@ void app_loop(void) { if (app.dmm_online) { // printf("DMM Lost connection.\n"); app.dmm_online = 0; - gpib_interface_clear(); } // slow down polling when offline app.poll_interval = DMM_RECOVERY_DELAY_MS; @@ -2439,6 +2452,10 @@ void app_loop(void) { // got a valid response, check if this is a recovery if (!app.dmm_online) { // printf("DMM Recovered.\n"); + // only assert REN here when it's online + gpib_remote_enable(1); + gpib_interface_clear(); + app.dmm_online = 1; gpib_send(app.dmm_addr, HP3478A_CMD_MASK_BTN_ONLY HP3478A_CMD_SRQ_CLEAR); gpib_go_to_local(app.dmm_addr); @@ -2894,7 +2911,6 @@ int main() { // GPIB controller gpib_init(); - gpib_remote_enable(1); // USB interface USBFSSetup();