⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flag1.c

📁 TDK 6521 SOC 芯片 DEMO程序
💻 C
📖 第 1 页 / 共 3 页
字号:
                    error = max(error,MessageBad);
            log(0x90);
            log(error);
                }
            }
        } else {
            // interpret an input field
            (*flag_in_state_table[(msg_fld & 0x7f)])();
        }
    } while (msg_fld == BEGIN_BCC); // begin bcc doesn't use the char

    // at the end of the message, decide what to do next
    if (msg_fld_next == END) {
        #if TIMERS
        reset_session_timer (); // restart the session timer
        #endif
        flag_state (); // figure out what to do next
    }
    log(4);
}
#pragma restore


// start of logic for flag output state machine

#pragma save
#pragma NOAREGS
static void send_w_bcc (uint8_t ch) small reentrant
{
    ch &= 0x7f;
    bcc ^= ch;
    ser_xmit (ch);
}
#pragma restore

#pragma save
#pragma NOAREGS
// at the end! do not increment the field pointer!
static void end_out (void) small reentrant
{
    --msg_fld_index;
}
#pragma restore

// 
#pragma save
#pragma NOAREGS
// send field Z, the maximum possible baud rate
static void z_out (void) small reentrant
{
    switch(BAUD)
    {
    default: 
        error = max(error,CommandBad);
        log(0x91);
        log(error);
        break;
    case  _RATE_300: send_w_bcc('0'); break;
    case  _RATE_600: send_w_bcc('1'); break;
    case _RATE_1200: send_w_bcc('2'); break;
    case _RATE_2400: send_w_bcc('3'); break;
    case _RATE_4800: send_w_bcc('4'); break;
    case _RATE_9600: send_w_bcc('5'); break;
//    case 14400: send_w_bcc('6'); break;
    }
}
#pragma restore

#pragma save
#pragma NOAREGS
// send the communication mode
static void y_out (void) small reentrant
{
    send_w_bcc('1');
}
#pragma restore

#pragma save
#pragma NOAREGS
// send the command byte
static void c_out (void) small reentrant
{
    send_w_bcc(cmd);
}
#pragma restore

#pragma save
#pragma NOAREGS
// send the command subtype
static void d_out (void) small reentrant
{
    send_w_bcc(cmd_subtype);
}
#pragma restore

#pragma save
#pragma NOAREGS
// send a device address
static void device_address_out (void) small reentrant
{
    send_w_bcc('0'); // send a place-holder
}
#pragma restore

// hexadecimal digits
static uint8r_t hex_digits[] =
{ '0', '1', '2', '3', '4', '5', '6', '7', 
  '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

// Sends a hex character
#pragma save
#pragma NOAREGS
static void hex_output (void) small reentrant
{
    cur_char = hex_digits[cur_char];
    send_w_bcc (cur_char);
    if (data_bit_index <= 0) {
        ++msg_fld_index; // digits are done
    }
}
#pragma restore

#pragma save
#pragma NOAREGS
// send a data address out
static void data_address_out (void) small reentrant
{
    --msg_fld_index; // stay in this field until the digits are done
    if (data_bit_index <= 0 || data_bit_index > 12)
        data_bit_index = 16;
    data_bit_index -= 4;
    cur_char = (uint8d_t)(0x0f & (address >> data_bit_index));
    hex_output (); // increments msg_fld_index
}
#pragma restore

#pragma save
#pragma NOAREGS
// Send a data count out
static void data_count_out (void) small reentrant
{
    --msg_fld_index; // stay in this field until the digits are done
    if (data_bit_index == 0) {
        data_bit_index = 4;
    } else {
        data_bit_index = 0;
    }
    cur_char = (uint8d_t)(0x0f & (char_cnt >> data_bit_index));
    hex_output (); // increments msg_fld_index
}
#pragma restore

#pragma save
#pragma NOAREGS
// Send hex data out
static void hex_data_out (void) small reentrant
{
    --msg_fld_index; // stay in this field until the digits are done
    if (data_bit_index == 0) {
        data_bit_index = 4;
    } else {
        data_bit_index = 0;
    }
    cur_char = (uint8d_t)(0x0f & ((data_buffer[data_index] >> data_bit_index)));
    cur_char = hex_digits[cur_char];
    send_w_bcc(cur_char);
    if (data_bit_index == 0) {
        data_index += 1;
    }
    if (--char_cnt <= 0) {
        ++msg_fld_index; // digits are done
    }
}
#pragma restore

#pragma save
#pragma NOAREGS
// Start the data protected by a block check character.
static void begin_bcc_out (void) small reentrant
{
    bcc = 0;
}
#pragma restore

#pragma save
#pragma NOAREGS
// Send a block check character
static void bcc_out (void) small reentrant
{
    ser_xmit (bcc);
}
#pragma restore

#pragma save
#pragma NOAREGS
// Send a block check character
static void error_out (void) small reentrant
{
    send_w_bcc (error_last_rcv);
}
#pragma restore

// table of data fields that can be sent
static code const void (*flag_out_state_table[MSG_FLD_COUNT])(void) small reentrant =
    {
    end_out, // END: last field- do not increment field pointer
    z_out, // field z- baud rate negotiation
    y_out, // field Y- baud rate negotiation
    c_out, // field c- command
    d_out, // field d- command subtype
    device_address_out, // DEVICE_ADDRESS: ignore a hexadecimal device address
    data_address_out, // DATA_ADDRESS: hexadecimal data address
    data_count_out, // DATA_COUNT: hexadecimal data count
    hex_data_out, // HEX_DATA: a string of bytes in hex.
    begin_bcc_out, // BEGIN_BCC: start of data protected by a BCC.
    bcc_out, // BCC: check the bcc (block check character).
    error_out // ERROR: send an error character.
    };

/* run a flag state machine for output to a serial port */
#pragma save
#pragma NOAREGS
#if PORT==0
void flag0_out (void) small reentrant
#endif
#if PORT==1
void flag1_out (void) small reentrant
#endif
{
    log(5);
    log(msg_fld_index);
    if (out != 1) { // do not send on the last interrupt
        // out == 0... so stop output
        ser_disable_xmit_rdy(); // disable transmit interrupt
    log(0xfc);
    } else {
        // the character level state machine
        // It reads fields in message templates.
        // The message templates are selected by the
        // protocol-level state machine.
        do {
            msg_fld = *(msg_ptr + msg_fld_index);
            ++msg_fld_index;
            msg_fld_next = *(msg_ptr + msg_fld_index);
            // if it's not an executable field value
            if (msg_fld < MSG_FLD_LOWER_LIMIT 
                || msg_fld > MSG_FLD_UPPER_LIMIT)
            {
                /* the output should be the same as the template */
                send_w_bcc(msg_fld);
            } else {
                // send a data field
                (*flag_out_state_table[(msg_fld & 0x7f)])();
            }
        } while (msg_fld == BEGIN_BCC); // begin bcc doesn't send anything
    
        // after the last character of the message is sent, 
    // decide what to do next
        if (msg_fld == END)
            flag_state ();
    }
    log(6);
}
#pragma restore


/* Run Flag */
#if PORT==0
void flag0_run (void)
#endif
#if PORT==1
void flag1_run (void)
#endif
{
    uint8_t cmd;


    // execute a command in the main
    if (cmd_main != 0) {
        EA = 0;
        cmd = cmd_main;
        cmd_main = 0;
        EA = 1;
    
        switch (cmd)
        {
        default: break;
        case 'U': REG_UPDATE(); break; // get a fresh copy, if possible
        #if CAL_SAVE
        case 'C': CALB_SAVE (); break; // save calibration data
        #endif
        }
    }

    #if TIMERS // if a 1.5 second timeout between received character
    if ( error == CharTimeout ) {
        log(0xe0);
        flag_state (); // perform error recovery
    }
    // if no message sent for a minute, end the session
    if ( error == SessionTimeout ) {
        log(0xe1);
        sign_on (); // return to the sign-out state
    }
    #endif
}

/* Initialize Flag */
#if PORT==0
void flag0_initialize (void)
#endif
#if PORT==1
void flag1_initialize (void)
#endif
{
    state = STATE_INVALID; // an invalid value forces the default state
    char_timer = NULL; // indicate that the timers are invalid
    session_timer = NULL;
    flag_state (); // an invalid state restarts the machine
    log(7);
}

#endif // FLAG.

/***************************************************************************
 *  $Log: flag1.c,v $
 *  Revision 1.27  2006/09/09 01:08:59  gmikef
 *  *** empty log message ***
 *
 *  Revision 1.26  2006/08/04 21:24:53  tvander
 *  Moved some const arrays into code space, where they belong.
 *
 *  Revision 1.25  2006/08/04 21:23:05  tvander
 *  Added a comment explaining the design.
 *
 *  Revision 1.24  2006/06/06 05:16:46  tvander
 *  clean build
 *
 *  Revision 1.23  2006/03/24 01:22:25  tvander
 *  Pretty-printed the code
 *
 *  Revision 1.22  2006/03/08 00:13:43  tvander
 *  Clean build
 *
 *  Revision 1.21  2006/03/06 03:30:19  Michael T. Fischer
 *  More 6530 prep.
 *
 *  Revision 1.19  2005/10/08 04:41:19  tvander
 *  Fixed priority inversion.
 *  Rewrote watchdog to work in brownout, but of course it doesn't work.
 *  Watchdog can now be defeated by clearing watchdog option to 0.
 *  Reorganized watt hour modules (at last!).
 *  Disabled reading of STATUS in 6521_cli because the CE's status is always SAG.
 *  Tested with 6521_CLI; measurements seem to work.
 *  Fixed other builds.
 *
 *  Revision 1.18  2005/09/22 23:45:01  tvander
 *  Clean build all models and unit tests, updated copyright to be fore Teridian
 *
 *  Revision 1.23  2005/09/01 02:03:05  gmikef
 *  Cleaned up the builds.
 *
 *  Revision 1.22  2005/08/29 17:55:51  tvander
 *  Restored SFR access.
 *
 *  Revision 1.20  2005/08/23 01:10:28  tvander
 *  Fixed baud rate
 *
 *  Revision 1.19  2005/08/22 22:30:54  tvander
 *  Clean build of FLAG system
 *  No pulse counting.
 *  No debug/CLI.
 *
 *  Revision 1.18  2005/08/22 21:20:55  tvander
 *  Added logic to read SFRs
 *
 *  Revision 1.17  2005/08/18 20:44:42  tvander
 *  Added temperature measurement to GUI-available fields.
 *  FIxed memory-space problem in add8_8
 *  Added temp_x, temp_nom, ppmc and ppmc2 to register def. file.,
 *  moved many other registers, whcih where in the way.
 *
 *  Revision 1.16  2005/07/14 20:15:47  tvander
 *  ce code concentrated in ce.c
 *  ce interface concentrated in ce652x.c, .h
 *  Fixed 0-length read or write using flag protocol.
 *  display.c is out of the build
 *  kwh_initialize now reads the LRC.
 *
 *  Revision 1.15  2005/07/01 00:23:29  tvander
 *  Modified to reduce the idata used.
 *
 *  Revision 1.14  2005/06/09 00:01:21  tvander
 *  Flag reads and writes work for power registers, the real time clock and CE RAM.
 *
 *  Revision 1.13  2005/06/04 01:24:41  tvander
 *  Fixed real time failure to read.  Added conditional debug code to log state transitions and routine boundaries.
 *
 *  Revision 1.12  2005/06/01 20:56:29  tvander
 *  Reworked to count characters rather than bytes.
 *
 *  Revision 1.11  2005/05/27 23:27:43  tvander
 *  Table-driven flag, verified large (64 byte) transfers and calibration command.
 *
 *  Revision 1.10  2005/05/26 21:54:36  tvander
 *  Flag is grossly working with GUI: signs on, reads, writes both xdata 
 *  and idata, interlocks with CE cycle, and timeouts work.
 *
 *  Revision 1.9  2005/05/21 01:47:39  gmikef
 *  *** empty log message ***
 *
 *  Revision 1.8  2005/05/19 00:49:04  tvander
 *  Flag0 verified to compile without calibration flag.
 *
 *  Revision 1.7  2005/05/18 22:42:52  tvander
 *  Added second FLAG interface
 *
 *  Revision 1.6  2005/05/18 22:23:55  tvander
 *  Successfully signed on with an HHU
 *
 *  Revision 1.5  2005/05/14 00:56:18  tvander
 *  Integrated flag0, ser0
 *  Regression tested and fixed 6521b
 *
 *  Revision 1.4  2005/04/22 21:40:08  tvander
 *  Major revision- it works pretty well.
 *
 *  Revision 1.3  2005/04/19 18:08:13  tvander
 *  First working flag interface.
 *
 *  Revision 1.2  2005/04/07 21:25:50  tvander
 *  Gotos removed
 *
 *  Revision 1.1  2005/04/06 23:02:49  tvander
 *  First valid compile of flag
 *
 * Copyright (C) 2005 Teridian Semiconductor Corp. All Rights Reserved.    *
 * this program is fully protected by the United States copyright          *
 * laws and is the property of Teridian Semiconductor Corporation.         *
 ***************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -