📄 dsp_modem.c
字号:
{
// delcaration needed if update LCD code is re-enabled.
// u16 emeterMsgString[20];
PMST |= 0x0020; // Turn on OVLY bit to force internal memory use
PMST |= 0x0008; // Turn on DROM bit to enable high memory
SWWSR = 0xFFFF; // Max out all wait states
IFR = 0xFFFF; // Clear all pending interrupt flags
IMR = 0; // Disable all interrupts until we specifically enable them
// Shut off MCBSP frame sync generation (FS)
MCBSP_SUBREG_BITWRITE(AFE_MCBSP, SPCR2_SUBADDR, FRST, FRST_SZ, 0);
MCBSP_SUBREG_BITWRITE(UART_MCBSP, SPCR2_SUBADDR, FRST, FRST_SZ, 0);
MCBSP_SUBREG_BITWRITE(LED_MCBSP, SPCR2_SUBADDR, FRST, FRST_SZ, 0);
DebugDelay(); // Flush C54x if in debug mode.
INTR_INIT; // Initialize the interrupt vector table pointer
CLKMD = 0x0000; // Turn off PLL
RPTNOP(10); // Wait for PLL to stabilize at new speed
CLKMD = (15u<<12) // PLLMUL = 15
|(1<<11) // PLLNDIV = 1
|(0xFF<<3) // PLLCOUNT = 255
|(1<<2) // PLLONOFF = 1
|(1<<1) // PLLNDIV = 1
|(1<<0); // PLLSTATUS = 1
RPTNOP(255); // Wait for PLL to stabilize at new speed
InitVars(); // Initialize variables
// GPIO register
GPIOCR = (0<<7) // In Master/~Slave
|(1<<6) // Out +MSP_Reset
|(1<<5) // Out ~EN485
|(1<<4) // Out ~485RE
|(1<<3) // Out 485DE
|(0<<2) // In ~Low5V
|(1<<1) // Out DRV Bias2
|(1<<0); // Out DRV Bias1
InitHardware(); // Initialize AFE hardware. Start DMA sequencers.
DebugDelay(); // Flush C54x if in debug mode.
SetTxBias(TX_BIAS_OFF); // Turn off the bias to the transmitter amplifier
DebugDelay(); // Flush C54x if in debug mode.
ConfigureLEDMCBSP (); // Configure MCBSP to control status LEDs
DebugDelay(); // Flush C54x if in debug mode.
ConfigureUART(); // Initialize the 16C550 and UART buffers.
DebugDelay(); // Flush C54x if in debug mode.
// Read jumper input to determine if board is a master or slave.
if (TestBit(GPIOSR, 1<<7)) // GPIO-7 tied to +Master/-Slave jumper.
{
uBoard = BOARD_MASTER;
ulMyAddr = 0x3; // Will be overwritten with read from MSP flash below.
uPlcState = PLC_FIND_CLOSEST_SLAVE;
uUartState = UART_RECEIVE_COMMAND;
uUartCommandIndex = 0; // Index into command buffer for the next char received.
uUartDataIndex = 0; // Index into data buffer for the next char received.
uSlaveFound = 0; // No closest slave identified yet.
SelectUARTTarget(UART_TARGET_HOST); // Make sure we're ready to talk.
}
else
{
uBoard = BOARD_SLAVE;
ulMyAddr = 0x4; // Will be overwritten with read from MSP flash below.
uPlcState = PLC_RECEIVE_COMMAND;
uUartState = UART_IDLE;
uSlaveFound = 1; // Setting 1, will avoid looking in SLAVE case.
}
ulMyAddr = (u32)(*((u32*)(0X100))); // Get address from DSP address 0x100.
InitTesterParms(); // Initialize the variable address arrays used by the tester.
uTransmitPacketReady = 0; // Nothing to send yet.
ClearXF(); // Turn off XF flag for debug
DelayNus(7000); // Wait 7 ms (a little over 2 frame times) before we start trying to look for signal
SetXF(); // Turn on XF flag for debug
prevSnrSample = ReadRxDMAPointer() - recSignalArray; // This MUST be done just before enabling interrupts.
InitTimer0(); // Configure and start Timer0 to generate periodic interrupts
INTR_GLOBAL_ENABLE(); // Enable Global Interrupts (KEEP THIS RIGHT AFTER InitTimer0();
InitFlash(); // initialize flash manager routines
// MSP430 needs time to come out of reset. If we don't wait, the first
// few emeter commands often don't work. Use the AutoPollCounter for a
// timer, rather than create a new variable for this purpose.
ulAutoPollCounter = 0L;
while(ulAutoPollCounter < ((u32)T0_INTS_PER_SEC*3))
{
// Wait.
}
//====== Display startup text on LCDs ======
// !!!!! This works, but we've opted to let the meter values be displayed instead...
// !!!!! Don't delete this, as it's a good example of how the DSP can write to the
// !!!!! LCD display if we ever want to use it again...
// !!!!!if(uBoard == BOARD_MASTER)
// !!!!!{
// !!!!! emeterMsgString[0] = (u16) 'W'; // Write command.
// !!!!! emeterMsgString[1] = (u16) 'L';
// !!!!! emeterMsgString[2] = (u16) 'o';
// !!!!! emeterMsgString[3] = (u16) 'o';
// !!!!! emeterMsgString[4] = (u16) 'k'; // "Looking"
// !!!!! emeterMsgString[5] = (u16) 'i';
// !!!!! emeterMsgString[6] = (u16) 'n';
// !!!!! emeterMsgString[7] = (u16) 'g';
// !!!!! emeterMsgString[8] = (u16) 0xd; // \r
// !!!!! emeterMsgString[9] = 0; // String terminator.
// !!!!! WriteUARTString(emeterMsgString);
// !!!!! uUartState = UART_RECEIVE_COMMAND;
// !!!!! uAckAfter485 = 0;
// !!!!!}
// !!!!!else // Slave.
// !!!!!{
// !!!!! emeterMsgString[0] = (u16) 'W'; // Write command.
// !!!!! emeterMsgString[1] = (u16) 'W';
// !!!!! emeterMsgString[2] = (u16) 'a';
// !!!!! emeterMsgString[3] = (u16) 'i';
// !!!!! emeterMsgString[4] = (u16) 't'; // "Waiting"
// !!!!! emeterMsgString[5] = (u16) 'i';
// !!!!! emeterMsgString[6] = (u16) 'n';
// !!!!! emeterMsgString[7] = (u16) 'g';
// !!!!! emeterMsgString[8] = (u16) 0xd; // \r
// !!!!! emeterMsgString[9] = 0; // String terminator.
// !!!!! WriteUARTString(emeterMsgString);
// !!!!! uAckAfter485 = 0;
// !!!!!}
}
//==========================================================================================
// Function: InitVars()
//
// Description: This function initializes some global variables.
//
// Revision History:
//==========================================================================================
void InitVars(void)
{
u16 i; // Loop index
// DSP code version reported in GUI. 1st byte = integer portion, 2nd byte = fraction
uCodeVersion = 0x0214; // Code version xx.xx 0x0214=2.20
for(i=0; i<UART_BUFFER_SIZE; i++)
{
upUartBufferFifo[i] = 0; // Clear FIFO buffer (not required, but nice for debug).
}
//---- initialize stuff ------------------------------
memset(txUserDataArray, 0, DATA_BUFFER_LEN*sizeof(txUserDataArray[0])); // Fill User Data buffer with zeroes
memset(rxUserDataArray, 0, DATA_BUFFER_LEN*sizeof(rxUserDataArray[0])); // Fill User Data buffer with zeroes
memset(rxUserDataCopy, 0, DATA_BUFFER_LEN*sizeof(rxUserDataCopy[0])); // Fill reference buffer with zeroes
memset(uErrCnt, 0, 32*sizeof(uErrCnt[0])); // Zero the error count array
initCRCtable( CRCtableArray ); // Initialize CRC table
agcState = AgcIdle;
prevSnrSample = 0; // used to time SNR calc
SNRflag = 0;
recSignal = recSignalArray;
uBerTestInProgress = 0;
// Clear all timeout counters
ulAutoPollCounter = 0;
ulUartCounter = 0;
ulLastRxCounter = 0;
// Optional: Useful for debug
memset(recSignalArray, 0xFFFE, RX_CIRC_BUFFER_LEN*sizeof(recSignalArray[0])); // Fill receive buffer with FFFE
memset(txSignalArray, 0xBBBB, TX_BUFFER_LEN*sizeof(txSignalArray[0])); // Fill tx buffer with BBBB
#if (SAVETRACE == TRUE) || (SAVESYMBOLS == TRUE)
memset((u16*)uTraceData, 0xABCD, TRACE_BUFF_SIZE*sizeof(uTraceData[0])); // Fill with known characters
#endif
DebugDelay(); // Flush C54x if in debug mode.
}
//==========================================================================================
// Function: InitTimer0()
//
// Description: Configure and start Timer0 to generate periodic interrupts
//
// Revision History:
//==========================================================================================
void InitTimer0(void)
{
#define TIMER0 0 //!!!TEMP - Move somewhere else
#define SER_CLK_DIV 25 //!!!TEMP - Eliminate
#define TIMER0_PERIOD ((AGC_INTERVAL * SER_CLK_DIV * 16 * AFE_SIZE)-1) //!!!TEMP Move to ofdm_defines.xls
#define TDDR_INIT (1-1)
TIM(TIMER0) = TIMER0_PERIOD-1;
PRD(TIMER0) = TIMER0_PERIOD-1;
SetBits(TCR(TIMER0), (1<<TSS || 1<<TRB)); // Stop Timer and Reset
TCR(TIMER0) = ( (0<<TIMSOFT) // 0 = freeze timer at breakpoint, 1= let timer expire
|| (0<<TIMFREE) // 0 = TIMSOFT bit selects timer action @ breakpoint, 1= timer runs free
|| (TDDR_INIT<<PSC) // Timer Prescale counter
|| (0<<TRB) // 0 = Normal, 1= Reset Timer
|| (0<<TSS) // 0 = Start timer, 1 = Stop timer
|| (TDDR_INIT<TDDR) ); // Timer Divide-Down Ratio
INTR_CLR_FLAG(TINT0); // Clear Timer0 Interrupt Flags
INTR_ENABLE(TINT0); // Enable Timer0 Interrupt
return;
}
//==========================================================================================
// Function: InitHardware()
//
// Description: This function configures hardware I/O devices such as the AFE (Analog
// Front End).
//
// Revision History:
//==========================================================================================
u16 InitHardware(void)
{
u16 uStatus = SUCCESS;
AFEctrl1 = (1<<7) // Power Control: 0=Normal Power, 1=Low Power,Low Speed
+ (0<<6) // Reserved: 0
+ (1<<4) // Tx Cutoff Freq: 0=.25x, 1=.38x, 2=.5x word rate
+ (1<<3) // Tx Filter Order: 0= 5th order, 1= 7th order
+ (0<<1) // Tx Power Backoff: 0=Normal, 1=-6dB, 2=-12dB, 3=-18dB
+ (1<<0); // Rx Cutoff Freq: 0=.25x, 1=.5x word rate
AFEctrl2 = (0<<6) // Reserved: 0
+ (5<<3) // Rx Gain: 0=0 dB, 1=3dB, 2=6dB,..., 7=21dB
+ (0<<1) // Loop-Back: 0=Normal, 1=Digital Loopback, 2=Hybrid LB, 3=Line LB
+ (0<<0); // Reserved: 0
FillIdleBuffer(); // Write new control setting to idle buffer if it has changed
uStatus = ConfigureAFE ();
if (uStatus != SUCCESS)
{
postError("InitHardware failed doing ConfigureAFE./n");
return(uStatus);
}
// Use DMA for both Rx and Tx
ConfigureMCBSPDMA(AFE_MCBSP);
uStatus = ConfigureAFE (); // Reset all MCBSP and DMA
if (uStatus != SUCCESS)
{
postError("InitHardware failed doing ConfigureAFE./n");
}
DMAReadAFE((u16)&recSignalArray[0], RX_CIRC_BUFFER_LEN); //!!!TEMP
// Arm and start the Idle DMA sequence.
DMAWriteAFE(IdleBuffArray, IDLE_BUFFER_LEN*AFE_SIZE);
DebugDelay(); // Flush C54x if in debug mode.
return(uStatus);
}
//==========================================================================================
// Function: SetTxBias()
//
// Description: This function sets the bias current in the transmit amplifier.
//
// Revision History:
//==========================================================================================
void SetTxBias(u16 uBias)
{
#define TX_BIAS_MASK 3
AssignBits(GPIOSR, TX_BIAS_MASK<<0, uBias<<0); // Send tx bias settings to bits 0 and 1 of HPI GPIO pins
}
//==========================================================================================
// Function: TransmitPacket()
//
// Description: This function takes a message from the txUserDataArray and transmits
// it to another station as a power-line communication packet.
//
// Revision History:
//==========================================================================================
void TransmitPacket(void)
{
TXDATA *pTxSignal;
i16 winSignal[WINDOW_LEN]; // Smoothed transition from preamble to data
u16 SNRflagBackup;
DebugDelay(); // Flush C54x if in debug mode.
SNRflagBackup = SNRflag; // Make a backup copy of the SNRflag
SNRflag = -2; // Tell receiver not to look for preambles in my Tx signal
TurnLEDsOn(LED_TX_PACKET); // Turn on green Tx LED1
SetTxBias(TX_BIAS_ON); // Turn on the bias to the transmitter amplifier
DebugDelay(); // Flush C54x if in debug mode.
PostErrorCode(0xBAE0, "TransmitPacket", "dsp_modem.c", "Transmitting Packet");
#if SAVETRACE == TRUE
SaveTraceData(0xBAE0); //!!!DEBUG Put a marker in the trace buffer
SaveTraceData((u16)recSignal); //!!!DEBUG Put a marker in the trace buffer
SaveTraceData(SNRflagBackup); //!!!DEBUG Put a marker in the trace buffer
#endif
DebugDelay(); // Flush C54x if in debug mode.
pTxSignal = makePreamble((TXDATA*)txSignalArray, winSignal); // start at beginning of tx buffer and build preamble
DebugDelay(); // Flush C54x if in debug mode.
pTxSignal = makeDataFrames(pTxSignal, winSignal, txUserDataArray); // start at current ptr and build all data frames
DelayNus(1500); // delay 1.5 mSec to allow 7V power supply to settle after TX
SetTxBias(TX_BIAS_OFF); // Turn off the bias to the transmitter amplifier
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -