📄 monitor.c
字号:
//----------------------------------------------------------------------------
// Monitor Program
//
// Version: 0.00 (for MC9S08GB60)
// Author : John Clayton
// Date : July 14, 2003
//
// This program is a small serial command driven monitor program that allows
// the user to set up different modes of operation within the DDRPP board
//
//
//----------------------------------------------------------------------------
#include <stddef.h>
#include <stdtypes.h>
// Processor specific include file
#include "mc9s08gb60.h"
#define BUS_SPEED 2000000 // Hz
#define CONST (BUS_SPEED / 1000 /*ms*/ / 8 /* loop cycles*/ )
#define MAXCOUNTER 1200
#define CMD_LINE_LENGTH 21 // Number of characters in command line buffer
// IO Line defines
#define TCLK_O PTBD_PTBD5
#define TCLK_DDR PTBDD_PTBDD5
#define TCLK_PE PTBPE_PTBPE5
#define TMS_O PTFD_PTFD5
#define TMS_DDR PTFDD_PTFDD5
#define TMS_PE PTFPE_PTFPE5
#define TRST_O PTBD_PTBD7
#define TRST_DDR PTBDD_PTBDD7
#define TRST_PE PTBPE_PTBPE7
#define TDI_O PTFD_PTFD6
#define TDI_DDR PTFDD_PTFDD6
#define TDI_PE PTFPE_PTFPE6
#define TDO_I PTBD_PTBD6
#define TDO_DDR PTBDD_PTBDD6
#define TDO_PE PTBPE_PTBPE6
//----------------------------------------------------------------------------
// Global variables
unsigned long counter; // Incremented in timer ISR
unsigned char user_attn; // Indicates user interface needs servicing
unsigned char ci; // Command line character index variable
unsigned char trst_state;
unsigned long DR_O[4];
unsigned long DR_I[4];
unsigned long IR_O;
unsigned long IR_I;
//-- some Assembly Level Stuff -----------------------------------------------
#define EnableInts asm cli
#define DisableInts asm sei
//-- Function prototypes -----------------------------------------------------
void _Startup(void);
void send_string(unsigned char *text);
//----------------------------------------------------------------------------
// ISRs
__interrupt void Dummy_ISR(void)
{
// Unimplemented ISRs trap.
asm BGND;
}
__interrupt void Trap_ISR(void)
{
// Unimplemented ISRs trap.
asm BGND;
}
interrupt void TimerOverflow_ISR(void) { /* simple RTI interrupt service routine */
/* clear TOF flag */ /* freq ~ 16 ms */
TPM1SC = TPM1SC & 0x7F;
counter++;
if (counter == MAXCOUNTER) counter = 0;
}
//----------------------------------------------------------------------------
// Interrupt vector table
typedef void(*tIsrFunc)(void);
const tIsrFunc _vect[] @0xFFCC = { /* Interrupt table */
Dummy_ISR, // vector 25 - real-time int.
Dummy_ISR, // vector 24 - IIC
Dummy_ISR, // vector 23 - AD conversion
Dummy_ISR, // vector 22 - keyboard pins
Dummy_ISR, // vector 21 - SCI2 transmit
Dummy_ISR, // vector 20 - SCI2 receive
Dummy_ISR, // vector 19 - SCI2 error
Trap_ISR, // vector 18 - SCI1 transmit
Trap_ISR, // vector 17 - SCI1 receive
Dummy_ISR, // vector 16 - SCI1 error
Dummy_ISR, // vector 15 - SPI
Dummy_ISR, // vector 14 - TPM2, overflow
Dummy_ISR, // vector 13 - TPM2, ch4
Dummy_ISR, // vector 12 - TPM2, ch3
Dummy_ISR, // vector 11 - TPM2, ch2
Dummy_ISR, // vector 10 - TPM2, ch1
Dummy_ISR, // vector 09 - TPM2, ch0
TimerOverflow_ISR, // vector 08 - TPM1, overflow
Dummy_ISR, // vector 07 - TPM1, ch2
Dummy_ISR, // vector 06 - TPM1, ch1
Dummy_ISR, // vector 05 - TPM1, ch0
Trap_ISR, // vector 04 - clock (LOLRE/LOCRE)
Dummy_ISR, // vector 03 - Low voltage (LVDIE)
Dummy_ISR, // vector 02 - IRQ pin
Dummy_ISR, // vector 01 - SWI
_Startup // Reset vector
};
//----------------------------------------------------------------------------
// Delay routines
void Wait1ms(void){
asm {
PSHX
PSHH
LDHX #CONST
loop:
AIX #-1 ;2 cycles
CPHX #0 ;3 cycles
BNE loop ;3 cycles
PULH
PULX
}
}
void WaitNms(int n){
/* This function waits for N ms. */
int i;
for(i=1;i<=n;i++) Wait1ms();
}
//----------------------------------------------------------------------------
static void Timer_Init(void) {
counter = 0;
TPM1SC = 0x48; /* Timer overflow IT enabled */
EnableInts;
}
//----------------------------------------
// This function selects a bit to send out to the TAP controller
//
unsigned char bit_select(unsigned long* data,unsigned char i)
{
unsigned long* dptr;
unsigned char j;
unsigned char retval;
dptr = data;
j = i;
while (j>31)
{
j-=32;
dptr++;
}
retval = (unsigned char) ((*dptr >> j)&0x00000001);
return(retval);
}
//----------------------------------------
// This function stores a single bit into the received data.
// It always uses TDO_I as the data bit to store.
//
void bit_store(unsigned long* data,unsigned char i)
{
unsigned long* dptr;
unsigned char j;
unsigned long insval;
unsigned long val;
if (TDO_I == 0) insval = 0x00000000;
else insval = 0x00000001;
dptr = data;
j = i;
while (j>31)
{
j-=32;
dptr++;
}
val = *dptr;
val &= (0xffffffff ^ (0x00000001 << j)); // clear the bit
val |= (insval << j); // OR in the new data bit
*dptr = val;
}
//----------------------------------------
// This function sends out a clock pulse to the TAP controller.
//
void tap_send_clk()
{
TCLK_O = 1; // Rising edge
TCLK_O = 0; // Falling edge
}
//----------------------------------------
// This function sends a new instruction to the IR
//
void tap_send_ir()
{
unsigned char i;
TMS_O = 1;
tap_send_clk();
TMS_O = 0;
tap_send_clk();
tap_send_clk();
// Now shift out the IR contents
for (i=0;i<7;i++)
{
TDI_O = bit_select(&IR_O,i);
bit_store(&IR_I,i);
tap_send_clk();
}
TMS_O = 1; // The last bit is sent out with TMS = 1;
TDI_O = bit_select(&IR_O,i);
bit_store(&IR_I,i);
tap_send_clk();
// Go back to reset state
for (i=0;i<2;i++) tap_send_clk();
}
//----------------------------------------
// This function sends new contents to the DR
//
void tap_send_dr()
{
unsigned char i;
TMS_O = 0;
tap_send_clk();
tap_send_clk();
// Now shift out the IR contents
for (i=0;i<117;i++)
{
TDI_O = bit_select((unsigned long *)&DR_O,i);
bit_store((unsigned long *)&DR_I,i);
tap_send_clk();
}
TMS_O = 1;
TDI_O = bit_select((unsigned long *)&DR_O,i);
bit_store((unsigned long *)&DR_I,i);
tap_send_clk();
// Go back to reset state
for (i=0;i<2;i++) tap_send_clk();
}
//----------------------------------------
// This function sets the IR, then the DR
//
void tap_test()
{
unsigned char i;
TMS_O = 1;
for (i=0;i<5;i++) tap_send_clk();
TMS_O = 0;
tap_send_clk();
TMS_O = 1;
tap_send_clk();
tap_send_clk();
TMS_O = 0;
tap_send_clk();
tap_send_clk();
// Now shift out the IR contents
for (i=0;i<7;i++)
{
TDI_O = bit_select(&IR_O,i);
bit_store(&IR_I,i);
tap_send_clk();
}
TMS_O = 1; // The last bit is sent out with TMS = 1;
TDI_O = bit_select(&IR_O,i);
bit_store(&IR_I,i);
tap_send_clk();
// Go back and set DR now (Updates IR)
for (i=0;i<2;i++) tap_send_clk();
TMS_O = 0;
tap_send_clk();
tap_send_clk();
// Now shift out the DR contents
for (i=0;i<117;i++)
{
TDI_O = bit_select((unsigned long *)&DR_O,i);
bit_store((unsigned long *)&DR_I,i);
tap_send_clk();
}
TMS_O = 1;
TDI_O = bit_select((unsigned long *)&DR_O,i);
bit_store((unsigned long *)&DR_I,i);
tap_send_clk();
// Go back to "select_DR_scan" now (Updates DR)
for (i=0;i<2;i++) tap_send_clk();
}
//----------------------------------------
// This function initializes the registers of COM1 for 9600 N,8,1
// operation.
void Com1_Init(void) {
unsigned char i;
SCI1BDH = 0x00; // BAUD rate setting of 9600, from 20MHz Fbus
SCI1BDL = 0x0b; // write the low value last
SCI1C1 = 0x00; // Various options disabled (loopback etc.)
SCI1C2 = 0x0c; // TE = 1, RE = 1, interrupts OFF.
i = SCI1S1; // Read status flags (part of clearing them.)
i = SCI1D; // Part of clearing flags
}
//----------------------------------------
void init_micro_ports(void)
{
// First disable the COP timer
asm {
PSHA;
LDA #$03;
STA $1802; /* disable COP */
PULA;
}
// Set Ports A, B and C to low
PTAD = 0x00;
PTBD = 0x00;
PTCD = 0x00;
PTDD = 0x00;
PTED = 0x00;
PTFD = 0x00;
PTGD = 0x00; // R/W (PTG3) set to 0 (W)
// Enable pullups on the inputs, to prevent oscillations
PTAPE = 0xff;
PTBPE = 0xff;
PTCPE = 0xff;
PTDPE = 0xff;
PTEPE = 0xff;
PTFPE = 0xff;
PTGPE = 0xff;
// Initialize them to be inputs (i.e. not driving = tristate)
PTADD = 0x00;
PTBDD = 0x00;
PTCDD = 0x00;
PTDDD = 0x00;
PTEDD = 0x00;
PTFDD = 0x00;
PTGDD = 0x00;
// Setup lines for JTAG TAP controller interface
// States of the actual JTAG lines
trst_state = 1;
TRST_O = 1; // Enable JTAG mode by default
TCLK_O = 0; // Start clock in "low" state
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -