/************************************************************* * ReadRingName * * Read startstop's configfile as far as the first ring name * *************************************************************/ void ReadRingName( char *ringname, char *configFile ) { int nfiles; char *com, *str; char configfile[20]; strcpy( configfile, "" ); nfiles = k_open( configFile ); if ( nfiles == 0 ) { printf( "status: Error opening file <%s>. Exiting.\n", configFile ); exit( -1 ); } /* Process Ring commands from startstop's command file ***************************************************/ while(nfiles > 0) /* While there are command files open */ { while(k_rd()) /* Read next line from active file */ { com = k_str(); /* Get the first token from line */ /* Ignore blank lines & comments *****************************/ if( !com ) continue; if( com[0] == '#' ) continue; /* Process only 1st Ring command *****************************/ if( k_its( "Ring" ) ) { str = k_str(); strcpy( ringname, str ); break; } /* See if there were any errors processing this command ****************************************************/ if( k_err() ) { printf( "status: Bad <%s> command in <%s>. Exiting.\n", com, configfile ); return; } } nfiles = k_close(); } return; }
/* handle a TX/RX, one char at a time * in SPI, there is no explicit separate TX and RX, instead in master the * RX is implied by TXing a 0x00 */ int spi_txrx(spi_portname_t portnum, void *tx_buf, void *rx_buf, uint16_t len) { /* this is marked as unused explicitly as a discard */ uint8_t __attribute__((unused)) discard; /* check we have a sane port first */ if (portnum >= MAX_SPI_PORTS || !spi_ports[portnum]) { return -ENODEV; } /* since we have three cases, depending on NULLs, check for NULL buffers * at the outset. Less branching this way. */ if (tx_buf && rx_buf) { /* we have both buffers, read from tx, write to rx */ while (len--) { spi_ports[portnum]->hw->DATA = *(uint8_t *)tx_buf; tx_buf++; /* wait for complete */ if (spi_wait_if(spi_ports[portnum]->hw,spi_ports[portnum]->timeout_us)) { k_err("hw timeout"); return -ETIME; } /* read the byte into the rx buffer */ *(uint8_t *)rx_buf = spi_ports[portnum]->hw->DATA; rx_buf++; } return 0; } if (tx_buf && !rx_buf) { /* discard RX, read from tx */ while (len--) { spi_ports[portnum]->hw->DATA = *(uint8_t *)tx_buf; tx_buf++; /* wait for complete */ if (spi_wait_if(spi_ports[portnum]->hw,spi_ports[portnum]->timeout_us)) { k_err("hw timeout"); return -ETIME; } /* we have to read this even tho we are discarding it */ discard = spi_ports[portnum]->hw->DATA; } return 0; } if (rx_buf && !tx_buf) { /* generate zeros for TX, write to rx */ while (len--) { spi_ports[portnum]->hw->DATA = spi_ports[portnum]->txdummy; /* wait for complete */ if (spi_wait_if(spi_ports[portnum]->hw,spi_ports[portnum]->timeout_us)) { k_err("hw timeout"); return -ETIME; } /* read the byte into the rx buffer */ *(uint8_t *)rx_buf = spi_ports[portnum]->hw->DATA; rx_buf++; } return 0; } /* if we reach this, we have something odd like NULL/NULL */ /* write zeros and discard, this might be used for a drain? */ while (len--) { spi_ports[portnum]->hw->DATA = spi_ports[portnum]->txdummy; /* wait for complete */ if (spi_wait_if(spi_ports[portnum]->hw,spi_ports[portnum]->timeout_us)) { k_err("hw timeout"); return -ETIME; } /* we have to read this even tho we are discarding it */ discard = spi_ports[portnum]->hw->DATA; } return 0; }
/****************************************************************************** * orb2eworm_config() processes command file(s) using kom.c functions; * * exits if any errors are encountered. * ******************************************************************************/ void orb2eworm_config( char *configfile ) { int ncommand; /* # of required commands you expect to process */ char init[10]; /* init flags, one byte for each required command */ int nmiss; /* number of required commands that were missed */ char *com; char *str; int nfiles; int success; int i; /* * Set to zero one init flag for each required command */ ncommand = 7; for ( i = 0; i < ncommand; i++ ) init[i] = 0; nLogo = 0; /* * Open the main configuration file */ nfiles = k_open( configfile ); if ( nfiles == 0 ) { fprintf( stderr, "orb2eworm: Error opening command file <%s>; exiting!\n", configfile ); exit( -1 ); } /* * Process all command files */ while ( nfiles > 0 ) { /* While there are command files open */ while ( k_rd() ) {/* Read next line from active file */ com = k_str(); /* Get the first token from line */ /* * Ignore blank lines & comments */ if ( !com ) continue; if ( com[0] == '#' ) continue; /* * Open a nested configuration file */ if ( com[0] == '@' ) { success = nfiles + 1; nfiles = k_open( &com[1] ); if ( nfiles != success ) { fprintf( stderr, "orb2eworm: Error opening command " ); fprintf( stderr, "file <%s>; exiting!\n", &com[1] ); exit( -1 ); } continue; } /* * Process anything else as a command */ /* 0 */ if ( k_its( "LogFile" ) ) { LogSwitch = k_int(); init[0] = 1; } /* 1 */ else if ( k_its( "MyModuleId" ) ) { str = k_str(); if ( str ) strcpy( MyModName, str ); init[1] = 1; } /* 2 */ else if ( k_its( "RingName" ) ) { str = k_str(); if ( str ) strcpy( RingName, str ); init[2] = 1; } /* 3 */ else if ( k_its( "HeartBeatInterval" ) ) { HeartBeatInterval = k_long(); init[3] = 1; } /* 4 */ else if ( k_its( "OrbName" ) ) { str = k_str(); if ( str ) strcpy( OrbName, str ); init[4] = 1; } /* 5 */ else if ( k_its( "ChannelSelect" ) ) { str = k_str(); if ( str ) strcpy( ChannelSelect, str ); init[5] = 1; } /* 6 */ else if ( k_its( "ChannelReject" ) ) { str = k_str(); if ( str ) strcpy( ChannelReject, str ); init[6] = 1; } /* * Unknown command */ else { fprintf( stderr, "orb2eworm: <%s> Unknown command in <%s>.\n", com, configfile ); continue; } /* * See if there were any errors processing the * command */ if ( k_err() ) { fprintf( stderr, "orb2eworm: Bad <%s> command in <%s>; exiting!\n", com, configfile ); exit( -1 ); } } nfiles = k_close(); } /* * After all files are closed, check init flags for missed commands */ nmiss = 0; for ( i = 0; i < ncommand; i++ ) if ( !init[i] ) nmiss++; if ( nmiss ) { fprintf( stderr, "orb2eworm: ERROR, no " ); if ( !init[0] ) fprintf( stderr, "<LogFile> " ); if ( !init[1] ) fprintf( stderr, "<MyModuleId> " ); if ( !init[2] ) fprintf( stderr, "<RingName> " ); if ( !init[3] ) fprintf( stderr, "<HeartBeatInterval> " ); if ( !init[4] ) fprintf( stderr, "<OrbName> " ); if ( !init[5] ) fprintf( stderr, "<ChannelSelect> " ); if ( !init[6] ) fprintf( stderr, "<ChannelReject> " ); fprintf( stderr, "command(s) in <%s>; exiting!\n", configfile ); exit( -1 ); } return; }