📄 main.c
字号:
/*************************************************************************
main.c
This code demonstrates interfacing to the CobraNet module using
a command line interpreter interface. This code is written for the 8051
type microcontroller that is used on the CobraNet evaluation board,
the EV-2.
Initial version written by Bill Lowe
Copyright (C) 2001-2004 by Cirrus Logic Inc. All Rights Reserved
*************************************************************************/
#include <stdio.h>
#include <intrpt.h>
#include <8051.h>
#include <conio.h>
#include "serial.h"
#include "cnmutil.h"
#include "command.h"
#include "test.h"
#include "led.h"
#include "audio.h"
#include "hostport.h"
#include "mib.h"
#include "error.h"
#include "packet.h"
#include "query.h"
#include "mystrings.h"
#define cCheck_Rate 100 //how often to check hex switches
void initialize();
void init_audio();
void init_hack();
void init_Interrupts();
extern unsigned char init_cm( unsigned char do_reset);
void main_aux();
interrupt void timer1_int();
extern void ( * Host_Interrupt_Ack )( void );
//some globals defined here
extern near unsigned char gRXD_RD_idx; //buffer pointer to serial input.
extern near unsigned char gRXD_WR_idx; //buffer pointer to serial output.
unsigned char gWHICH_MODULE; //defines whether CM1 or CM2 is present.
unsigned char gSystem_Status; //the state of the EV-2 board.
unsigned char gProcess_Packets; //ignore packets when set.
extern unsigned char gSkip_Packets; //for test, ack packet but don't read.
unsigned char gHack_Polarity; //hack interrupt polarity can change
//from application to application.
unsigned char gHex_Check; //when set, the hex switches are checked.
static char text_buffer[ 128 ]; //main command line buffer, used to store
//the input string which is then parsed
//and a series of null terminated strings
//are created consisting of the command
//and its arguments.
/***************** code starts here *****************/
main () {
near static unsigned char error_code = cERR_NO_ERROR;
load_FPGA(); //do this right away
initialize();
printStrCodeC( s_Intro1 );
if ( gSystem_Status = init_cm( cRESET ) ) {
printErr( cERR_CM_NOT_FOUND );
LED_Operation( cGREEN_LED + cBLINK_OFFSET, cBLINK_ON );
LED_Operation( cYELLOW_LED + cBLINK_OFFSET, cBLINK_ON );
LED_Operation( cRED_LED + cBLINK_OFFSET, cBLINK_ON );
if ( !( get_hex_switches( cUPDATE_HEX ) == 0xFFF8 ) )
while ( 1 )
;
else {//this is here for debug purposes
printStrCodeC( str_Waiting );
while (!RI) //wait for any serial input.
;
RI = 0;
init_cm( cNO_RESET );
}
}
else
LED_Operation( cGREEN_LED, cLED_ON );
init_hack(); //initialize how hack in handled.
init_audio(); //initialize audio converters
init_Interrupts(); //initialize the interrupt routines.
if ( Host_Sync_Translation() ) {
printStrCodeC( str_ErrorColon );
printStrCodeC( str_NoHost );
}
error_code = get_display_string( sysDESCRIPTION_ptr, text_buffer );
printStrC( text_buffer );
printStrCode( str_Prompt );
while( 1 ) { //main event loop here
//wait for serial input or packet
while ( ( gRXD_RD_idx == gRXD_WR_idx ) && ( !( CNMHACK ^ gHack_Polarity ) ) && ( !gHex_Check ) )
;
if ( gRXD_RD_idx != gRXD_WR_idx )
service_Line_Command();
else
if ( CNMHACK ^ gHack_Polarity) {
if ( gProcess_Packets )
service_Packet();
}
else {
check_hex();
gHex_Check = 0;
}
}//while
}
void initialize () {
init_uart(); //initialize serial communications
LED_Operation( cGREEN_LED, cLED_OFF );
LED_Operation( cRED_LED, cLED_OFF );
LED_Operation( cYELLOW_LED, cLED_OFF );
//initialize global variables
gProcess_Packets = 1;
gSkip_Packets = 0;
gHex_Check = 0;
gSystem_Status = 0;
}
void init_audio() {
audio_ADCCaste( cSLAVE ); //put ADC into slave mode.
audio_DACMode( cDAC_SINGLE_SPEED ); //initialize DAC to 48 kHz
audio_DACreset( cDAC_RESET_OFF ); //release the DAC from reset.
//set mute or unmute condition
audio_unmute( ( char * ) cSSI_REG0_ptr );
audio_unmute( ( char * ) cSSI_REG1_ptr );
audio_mute( ( char * ) cSSI_REG2_ptr );
audio_mute( ( char * ) cSSI_REG3_ptr );
audio_unmute( ( char * ) cAES_REG_ptr );
audio_unmute( ( char * ) cDAC_REG_ptr );
//set up default route
audio_put_route( cAUDIO_ADC1, ( char * ) cSSI_REG0_ptr );
audio_put_route( cAUDIO_AES, ( char * ) cSSI_REG1_ptr );
audio_put_route( cAUDIO_SSI0, ( char * ) cDAC_REG_ptr );
audio_put_route( cAUDIO_SSI1, ( char * ) cAES_REG_ptr );
sine_gain( cGAIN_0DB );
sine_freq( cFREQ_FUNDAMENTAL );
}
extern unsigned char init_cm( unsigned char do_reset ) {
near static unsigned char error_code = cERR_NO_ERROR;
if ( do_reset ) {
Host_Reset( cHRESET_ON ); //make sure CM is in reset
waiting( mHALFSECOND ); //wait just a bit
Host_Reset( cHRESET_OFF ); //take CobraNet module out of reset.
waiting( cWAIT_ONE_SECOND ); //give module time to come out of reset
waiting( cWAIT_ONE_SECOND ); // ~2 seconds
}
if ( ( gWHICH_MODULE = CM_Detect_Test() ) == 0 )
return( cERR_CM_NOT_FOUND );
init_Host_funcs( gWHICH_MODULE );
clr_print_packet(); //make sure print is not redirected.
get_MAC_Address(); //store MAC address locally
set_sysname();
return( error_code );
}
void init_hack() {
near static unsigned char error_code = cERR_NO_ERROR;
Host_Interrupt_Ack(); //to reset hack in case asserted
gHack_Polarity = CNMHACK; //save polarity of hack
//enable bridge of packets
error_code = Host_Poke( cBRIDGE_RX_FILTER, cFILTER_UCN );
//enable HACK on bridge packet receipt
error_code = Host_Poke( cHACK_ENABLE_ptr, cHACK_ACTIVATE_RX );
}
void init_Interrupts() {
//set up timer 1 to check hex switches on a periodic basis
TMOD = 0x10; //16 bit timer
TR1 = 1; //turn on timer 1
ROM_VECTOR( TIMER1,timer1_int ); //put interrupt vector in proper place
ET1 = 1; //enable timer 1 interrupt
EA = 1; //enable interrupts
}
interrupt void timer1_int() {
// this interrupt routine checks hex switches about once every 2 seconds
static unsigned char check_count = 0;
check_count++;
if ( check_count == cCheck_Rate ) {
gHex_Check = 1;
check_count = 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -