summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bus.c4
-rw-r--r--flash.c40
-rw-r--r--flash.h1
-rw-r--r--globals.h2
-rw-r--r--gpib.c123
-rw-r--r--i2c.c9
-rw-r--r--instr-daemon.c32
7 files changed, 163 insertions, 48 deletions
diff --git a/bus.c b/bus.c
index 3c83eac..bcd916f 100644
--- a/bus.c
+++ b/bus.c
@@ -200,8 +200,8 @@ static void gpmc_setup(void)
int WRACCESSTIME = DATA_READY;
int config6 = WRACCESSTIME << 24;
-
-
+
+
pinmux_configurepin("gpmc_csn0", PULL_UP | MODE_0);
pinmux_configurepin("gpmc_oen_ren", PULL_UP | MODE_0);
pinmux_configurepin("gpmc_wen", PULL_UP | MODE_0);
diff --git a/flash.c b/flash.c
index e3ce2ce..34f6e7f 100644
--- a/flash.c
+++ b/flash.c
@@ -259,7 +259,7 @@ bool persistence_unfreeze(char* dest, void* result, unsigned int len, uint32_t v
}
-int readUserBlock(FlashStruct *mem)
+static int readUserBlock(FlashStruct *mem)
{
// try to unfreeze the main file
@@ -272,7 +272,11 @@ int readUserBlock(FlashStruct *mem)
// hopefully we can use the backup..
if (persistence_unfreeze(BACKUPFILE, mem, sizeof(*mem), 0)) {
// if the backup was good overwrite the main file
- persistence_copyfile(BACKUPFILE, MAINFILE);
+ if (!globals.Flags.shutdown_started) {
+ globals.Flags.flash_write_in_progress = TRUE;
+ persistence_copyfile(BACKUPFILE, MAINFILE);
+ globals.Flags.flash_write_in_progress = FALSE;
+ }
return sizeof(*mem);
}
// deadend :(
@@ -291,21 +295,37 @@ void writeUserBlock(FlashStruct *mem, int addr, int numbytes)
// *** that the main file is valid before backing it up I guess... ***
// *** but I don't think this situation should arise. ***
+ static GMutex mutex;
+
+ g_mutex_lock (&mutex);
+
if (!globals.Flags.flash_writes_suspended) {
// backup the main copy of the file
- if (persistence_copyfile(MAINFILE, BACKUPFILE)) {
- if (!persistence_freeze(MAINFILE, mem, addr, numbytes, sizeof(*mem), 0)) {
- if (errno != PERSIST_ERR_COULDNTWRITE) {
- printf("Error while trying to write, %d. **Write did not happen!!!**\n", errno);
- } else {
- printf("Error while writing data to disk. **File is potentially corrupt!**\n");
+ if (!globals.Flags.shutdown_started) {
+
+ globals.Flags.flash_write_in_progress = TRUE;
+ bool backup_ok = persistence_copyfile(MAINFILE, BACKUPFILE);
+ globals.Flags.flash_write_in_progress = FALSE;
+
+ if (backup_ok && !globals.Flags.shutdown_started) {
+ globals.Flags.flash_write_in_progress = TRUE;
+
+ if (!persistence_freeze(MAINFILE, mem, addr, numbytes, sizeof(*mem), 0)) {
+ if (errno != PERSIST_ERR_COULDNTWRITE) {
+ printf("Error while trying to write, %d. **Write did not happen!!!**\n", errno);
+ } else {
+ printf("Error while writing data to disk. **File is potentially corrupt!**\n");
+ }
}
+ globals.Flags.flash_write_in_progress = FALSE;
+ } else {
+ printf("Could not backup current file. **Write did not happen!!!**\n");
}
- } else {
- printf("Could not backup current file. **Write did not happen!!!**\n");
}
}
+
+ g_mutex_unlock (&mutex);
}
diff --git a/flash.h b/flash.h
index 739fefa..c2d3b0e 100644
--- a/flash.h
+++ b/flash.h
@@ -5,7 +5,6 @@
#include <glib.h>
void initFlash(FlashStruct *mem, gboolean reset_to_defaults, int starting_location);
-int readUserBlock(FlashStruct *mem);
void writeUserBlock(FlashStruct *mem, int addr, int numbytes);
void fixFlash(FlashStruct *mem);
diff --git a/globals.h b/globals.h
index 13b6cc9..9e008dc 100644
--- a/globals.h
+++ b/globals.h
@@ -771,6 +771,8 @@ typedef struct {
int do_check_settings;
int flash_writes_suspended;
int force_output_fully_off;
+ int shutdown_started;
+ int flash_write_in_progress;
} FlagStruct;
diff --git a/gpib.c b/gpib.c
index 8d02267..d28e7ca 100644
--- a/gpib.c
+++ b/gpib.c
@@ -240,6 +240,8 @@
/* TNT4882 GLOBAL VARIABLES -------------------------------------------------*/
+int is_gpib;
+
int INTERFACE_ERROR; /* Error Code */
int INTERFACE_STATUS; /* Interface Status */
unsigned char MR_4882_status[5]; /* 4882 status memory registers */
@@ -416,34 +418,38 @@ void GPIB_initialize(void)
{
int i;
- for(i=0; i<5; ++i) {
- MR_4882_status[i]=0; /* do this because these variable are 2-byte integers */
- }
- /* but Nat Inst code assumes they are one-byte */
-
- INTERFACE_STATUS=0; /* Initialize Globals to zero */
+ INTERFACE_STATUS=0; /* Initialize Globals to zero */
INTERFACE_ERROR=0;
DATA_COUNT=0;
Requested_Count=0;
- TNT_Out(R_cmdr,F_softreset); /* Reset FIFOS */
+ TNT_Out(R_cmdr,F_softreset); /* Reset FIFOS */
+
+ TNT_Out(R_spmr ,0x80); /* This sequence of commands */
+
+ if (TNT_In(R_spmr) == 0x80) {
+ is_gpib = 1;
+ } else {
+ is_gpib = 0;
+ printf ("Error: TNT4882 chip not found\n");
+ return;
+ }
- TNT_Out(R_spmr ,0x80); /* This sequence of commands */
- TNT_Out(R_auxmr,0x80); /* insures that the TNT */
- TNT_Out(R_auxmr,0x99); /* will be in the normal 7210 */
- TNT_Out(R_keyrg,0); /* mode and not 9914 */
+ TNT_Out(R_auxmr,0x80); /* insures that the TNT */
+ TNT_Out(R_auxmr,0x99); /* will be in the normal 7210 */
+ TNT_Out(R_keyrg,0); /* mode and not 9914 */
- TNT_Out(R_hssel,F_onechip); /* Set TNT to one chip WINK mode */
- TNT_Out(R_misc,0); /* Disable HS mode */
- TNT_Out(R_hier,B_dga|B_dgb); /* Set deglitching circuits to */
+ TNT_Out(R_hssel,F_onechip); /* Set TNT to one chip WINK mode */
+ TNT_Out(R_misc,0); /* Disable HS mode */
+ TNT_Out(R_hier,B_dga|B_dgb); /* Set deglitching circuits to */
- TNT_Out(R_auxmr,F_chrst); /* Reset TNT */
+ TNT_Out(R_auxmr,F_chrst); /* Reset TNT */
- TNT_4882_Status(STB,0xff,CLEAR); /* Initialize Serial Poll Byte */
- TNT_4882_Status(SRE,0xff,CLEAR); /* Initialize SRE memory register */
- TNT_4882_Status(ESR,0xff,CLEAR); /* Initialize ESR memory register */
- TNT_4882_Status(ESR,0x80,SET); /* Indicate power on */
- TNT_4882_Status(ESE,0xff,CLEAR); /* Initialize ESE memory register */
+ TNT_4882_Status(STB,0xff,CLEAR); /* Initialize Serial Poll Byte */
+ TNT_4882_Status(SRE,0xff,CLEAR); /* Initialize SRE memory register */
+ TNT_4882_Status(ESR,0xff,CLEAR); /* Initialize ESR memory register */
+ TNT_4882_Status(ESR,0x80,SET); /* Indicate power on */
+ TNT_4882_Status(ESE,0xff,CLEAR); /* Initialize ESE memory register */
TNT_Adr_Mode();
GPIB_change_address(globals.Flash.gpib_address);
@@ -454,10 +460,9 @@ void GPIB_initialize(void)
/* If not using HS488 set only hst1*/
TNT_Out(R_auxmr,HR_auxrb|((USE_HIGH_SPEED_T1)? B_hst1 : 0));
- TNT_Out(R_auxmr,F_hldi); /* Issue hold off immediately */
- TNT_Out(R_auxmr,F_pon); /* Clear Power On */
-
- TNT_Out(R_imr0,B_glint); /* Enable setting of tlcint */
+ TNT_Out(R_auxmr,F_hldi); /* Issue hold off immediately */
+ TNT_Out(R_auxmr,F_pon); /* Clear Power On */
+ TNT_Out(R_imr0,B_glint); /* Enable setting of tlcint */
}
@@ -474,6 +479,10 @@ static void TNT_Adr_Mode()
void GPIB_change_address(int new_address)
{
+ if (!is_gpib) {
+ return;
+ }
+
int eprom_loc;
TNT_Out(R_adr,new_address); /* Load new address setting */
@@ -628,6 +637,10 @@ int GPIB_check_for_device_clear_signal(void)
/* added by MJC - June 20/06 */
/* reset interface if a device clear is received */
+ if (!is_gpib) {
+ return FALSE;
+ }
+
if (TNT_INT_STATUS() & DCAS) {
TNT_Out(R_auxmr,F_clrDEC);
TNT_INT_STATUS();
@@ -642,6 +655,10 @@ int GPIB_check_for_messages(char *gpib_buf)
{
#define ib_empty (!(strlen(gpib_buf)))
+ if (!is_gpib) {
+ return FALSE;
+ }
+
/* If the GPIB has requested data, and no output messages are in the TNT4882 FIFOs, generate a query error */
if (TNT_update_brq() && !TNT_input_bav() && ib_empty) {
queue_error_for_gpib_only(query_error_unterminated);
@@ -670,6 +687,10 @@ int GPIB_check_for_messages(char *gpib_buf)
int GPIB_handle_new_input(char *gpib_buf)
{
+ if (!is_gpib) {
+ return FALSE;
+ }
+
/* read until done or buffers empty. Then reset DAC holdoff */
unsigned long int count_sent; /* Local count variable */
@@ -754,6 +775,10 @@ static void TNT_Holdoff_off()
int GPIB_send_query_response(char *out_buffer)
{
+ if (!is_gpib) {
+ return OK;
+ }
+
/* message must be available if this function has been called */
unsigned long int out_cnt;
@@ -877,6 +902,9 @@ int GPIB_send_query_response(char *out_buffer)
void GPIB_check_remote_status (int *is_remote, int *is_lockout)
{
*is_remote = *is_lockout = 0;
+ if (!is_gpib) {
+ return;
+ }
TNT_INT_STATUS(); /* Update to get current status */
if ((INTERFACE_STATUS&REM) == REM) {
@@ -892,12 +920,20 @@ void GPIB_check_remote_status (int *is_remote, int *is_lockout)
unsigned char GPIB_response_already_pending ()
{
+ if (!is_gpib) {
+ return 0;
+ }
+
return TNT_In(R_spsr) & 0x10;
}
void GPIB_go_to_local ()
{
+ if (!is_gpib) {
+ return;
+ }
+
TNT_Out(R_auxmr,0x05); /* issue TNT rtl command */
return;
}
@@ -905,6 +941,10 @@ void GPIB_go_to_local ()
void GPIB_clear_events ()
{
+ if (!is_gpib) {
+ return;
+ }
+
TNT_4882_Status(ESR,0xff,CLEAR); /* Clear ESR register */
TNT_4882_Status(STB,0x20,CLEAR); /* Clear ESB bit in STB */
return;
@@ -931,51 +971,80 @@ unsigned int GPIB_get_ESE ()
unsigned int GPIB_get_STB ()
{
+ if (!is_gpib) {
+ return;
+ }
+
return (TNT_In(R_spsr)&0xbf) | (((TNT_In(R_spsr)&0xbf) & MR_4882_status[SRE])?0x40:0);
}
void GPIB_set_ESR (unsigned int byte,int operation)
{
+ if (!is_gpib) {
+ return;
+ }
+
TNT_4882_Status(ESR,byte,operation);
- return;
}
void GPIB_set_SRE (unsigned int byte,int operation)
{
+ if (!is_gpib) {
+ return;
+ }
+
TNT_4882_Status(SRE,byte,operation);
- return;
}
void GPIB_set_ESE (unsigned int byte,int operation)
{
+ if (!is_gpib) {
+ return;
+ }
+
TNT_4882_Status(ESE,byte,operation);
- return;
}
void GPIB_Set_Execution_Error ()
{
+ if (!is_gpib) {
+ return;
+ }
+
TNT_4882_Status(3,0x10,1);
}
void GPIB_Set_Command_Error ()
{
+ if (!is_gpib) {
+ return;
+ }
+
TNT_4882_Status(3,0x20,1);
}
void GPIB_Set_Query_Error ()
{
+ if (!is_gpib) {
+ return;
+ }
+
TNT_4882_Status(3,0x04,1);
}
void GPIB_Set_Device_Dependent_Error ()
{
+ if (!is_gpib) {
+ return;
+ }
+
TNT_4882_Status(3,0x08,1);
}
diff --git a/i2c.c b/i2c.c
index 6571798..dc52337 100644
--- a/i2c.c
+++ b/i2c.c
@@ -10,6 +10,9 @@
#define I2C_BUS "/dev/i2c-3"
+gboolean write_error_flagged;
+gboolean read_error_flagged;
+
void I2C_Write(gulong address, guchar value)
{
@@ -23,7 +26,8 @@ void I2C_Write(gulong address, guchar value)
int device = open(I2C_BUS, O_RDWR);
- if (device == -1) {
+ if ((device == -1) && !write_error_flagged) {
+ write_error_flagged = TRUE;
g_print_debug("ERROR: could not open I2C bus %s for writing\n", I2C_BUS);
return;
}
@@ -48,7 +52,8 @@ guchar I2C_Read(gulong address)
int device = open(I2C_BUS, O_RDWR);
- if (device == -1) {
+ if ((device == -1) && !read_error_flagged) {
+ read_error_flagged = TRUE;
g_print_debug("ERROR: could not open I2C bus %s for reading\n", I2C_BUS);
return 0;
}
diff --git a/instr-daemon.c b/instr-daemon.c
index 40a65b2..ee77e82 100644
--- a/instr-daemon.c
+++ b/instr-daemon.c
@@ -14,7 +14,10 @@
#include <stdlib.h>
#include <ctype.h>
#include <glib.h>
+<<<<<<< HEAD
#include <unistd.h>
+=======
+>>>>>>> b2c2c72d381ab1b32fa5b5fc4e890fef6c2bf1e0
#include <fcntl.h>
#define STDIN_BUF_SIZE 1024
@@ -317,7 +320,19 @@ int main(int argc, char **argv)
static gboolean periodic_poll (void)
{
if (bus_getpin (POWER_FAIL)) {
+<<<<<<< HEAD
//system ("/usr/bin/systemctl poweroff");
+=======
+
+ globals.Flags.shutdown_started = TRUE;
+
+ while (globals.Flags.flash_write_in_progress) {
+ g_usleep(1000);
+ }
+
+ LCD_clear();
+ LCD_write(0,0,"Power failed. Shutdown.");
+>>>>>>> b2c2c72d381ab1b32fa5b5fc4e890fef6c2bf1e0
// use plain old open to avoid any buffering etc
int enablefd = open("/proc/sys/kernel/sysrq", O_SYNC, O_RDWR);
@@ -332,6 +347,11 @@ static gboolean periodic_poll (void)
// sync disks
write(trgfd, "s\n", 2);
+<<<<<<< HEAD
+=======
+ // poweroff
+ system ("/usr/bin/systemctl poweroff -f");
+>>>>>>> b2c2c72d381ab1b32fa5b5fc4e890fef6c2bf1e0
}
Menu_Check_Buttons ();
@@ -342,12 +362,12 @@ static gboolean periodic_poll (void)
// is crashing due to non-functional bus
GPIB_check_for_device_clear_signal();
if (GPIB_check_for_messages(globals.Registers.gpib_buffer)) {
- if (GPIB_handle_new_input(globals.Registers.gpib_buffer)) {
- // FIXME check_for_ctrl_mode_changes();
- Parser_main(globals.Registers.gpib_buffer, 0, NULL, NULL);
- }
- }
- GPIB_check_for_device_clear_signal();
+ if (GPIB_handle_new_input(globals.Registers.gpib_buffer)) {
+ // FIXME check_for_ctrl_mode_changes();
+ Parser_main(globals.Registers.gpib_buffer, 0, NULL, NULL);
+ }
+ }
+ GPIB_check_for_device_clear_signal();
return TRUE;
}