📄 bdmcf.c
字号:
bset DSCLK_OUT_BITNUM,DSCLK_OUT_PORT
#endif
#ifdef DEBUG
lda #(BDMCF_IDLE*2) /* reload idle value into A */
#else
lda #(BDMCF_IDLE/2)
#endif
lslx /* shift MSB into C */
#ifdef DEBUG
rora /* rotate C into A */
#else
rola
#endif
lsrx /* shift X back to what it was */
sta PTA /* create falling edge on DSCLK and write the next bit value to the port */
lsr PTC /* shift the input data bit (PTC0) into C */
rolx /* shift the sample into X from the bottom */
/* transmit and receive bit 4 */
#ifdef INVERT
bclr DSCLK_OUT_BITNUM,DSCLK_OUT_PORT /* create rising edge on DSCLK */
#else
bset DSCLK_OUT_BITNUM,DSCLK_OUT_PORT
#endif
#ifdef DEBUG
lda #(BDMCF_IDLE*2) /* reload idle value into A */
#else
lda #(BDMCF_IDLE/2)
#endif
lslx /* shift MSB into C */
#ifdef DEBUG
rora /* rotate C into A */
#else
rola
#endif
lsrx /* shift X back to what it was */
sta PTA /* create falling edge on DSCLK and write the next bit value to the port */
lsr PTC /* shift the input data bit (PTC0) into C */
rolx /* shift the sample into X from the bottom */
/* transmit and receive bit 3 */
#ifdef INVERT
bclr DSCLK_OUT_BITNUM,DSCLK_OUT_PORT /* create rising edge on DSCLK */
#else
bset DSCLK_OUT_BITNUM,DSCLK_OUT_PORT
#endif
#ifdef DEBUG
lda #(BDMCF_IDLE*2) /* reload idle value into A */
#else
lda #(BDMCF_IDLE/2)
#endif
lslx /* shift MSB into C */
#ifdef DEBUG
rora /* rotate C into A */
#else
rola
#endif
lsrx /* shift X back to what it was */
sta PTA /* create falling edge on DSCLK and write the next bit value to the port */
lsr PTC /* shift the input data bit (PTC0) into C */
rolx /* shift the sample into X from the bottom */
/* transmit and receive bit 2 */
#ifdef INVERT
bclr DSCLK_OUT_BITNUM,DSCLK_OUT_PORT /* create rising edge on DSCLK */
#else
bset DSCLK_OUT_BITNUM,DSCLK_OUT_PORT
#endif
#ifdef DEBUG
lda #(BDMCF_IDLE*2) /* reload idle value into A */
#else
lda #(BDMCF_IDLE/2)
#endif
lslx /* shift MSB into C */
#ifdef DEBUG
rora /* rotate C into A */
#else
rola
#endif
lsrx /* shift X back to what it was */
sta PTA /* create falling edge on DSCLK and write the next bit value to the port */
lsr PTC /* shift the input data bit (PTC0) into C */
rolx /* shift the sample into X from the bottom */
/* transmit and receive bit 1 */
#ifdef INVERT
bclr DSCLK_OUT_BITNUM,DSCLK_OUT_PORT /* create rising edge on DSCLK */
#else
bset DSCLK_OUT_BITNUM,DSCLK_OUT_PORT
#endif
#ifdef DEBUG
lda #(BDMCF_IDLE*2) /* reload idle value into A */
#else
lda #(BDMCF_IDLE/2)
#endif
lslx /* shift MSB into C */
#ifdef DEBUG
rora /* rotate C into A */
#else
rola
#endif
lsrx /* shift X back to what it was */
sta PTA /* create falling edge on DSCLK and write the next bit value to the port */
lsr PTC /* shift the input data bit (PTC0) into C */
rolx /* shift the sample into X from the bottom */
/* transmit and receive bit 0 */
#ifdef INVERT
bclr DSCLK_OUT_BITNUM,DSCLK_OUT_PORT /* create rising edge on DSCLK */
#else
bset DSCLK_OUT_BITNUM,DSCLK_OUT_PORT
#endif
#ifdef DEBUG
lda #(BDMCF_IDLE*2) /* reload idle value into A */
#else
lda #(BDMCF_IDLE/2)
#endif
lslx /* shift MSB into C */
#ifdef DEBUG
rora /* rotate C into A */
#else
rola
#endif
lsrx /* shift X back to what it was */
sta PTA /* create falling edge on DSCLK and write the next bit value to the port */
lsr PTC /* shift the input data bit (PTC0) into C */
rolx /* shift the sample into X from the bottom */
/* transmission and reception is now complete, the result is in X */
txa /* move the result data to A */
#ifdef INVERT
coma /* invert the data */
#endif
}
}
/* transmits 1 bit of logic low value and receives 1 bit */
unsigned char bdmcf_txrx_start_1(void) {
asm {
lda #BDMCF_IDLE /* preload idle state of signals into A */
/* transmit zero and receive 1 bit */
sta PTA /* bring DSCLK and DSI low */
nop /* make the time between the assignement and the rising edge the same as during normal transmission */
nop
#ifdef INVERT
bclr DSCLK_OUT_BITNUM,DSCLK_OUT_PORT /* create rising edge on DSCLK */
#else
bset DSCLK_OUT_BITNUM,DSCLK_OUT_PORT
#endif
nop /* make the time between the edges the same as during normal reception */
#ifdef INVERT
bset DSCLK_OUT_BITNUM,DSCLK_OUT_PORT /* create falling edge on DSCLK */
#else
bclr DSCLK_OUT_BITNUM,DSCLK_OUT_PORT
#endif
lsr PTC /* shift the input data bit (PTC0) into C */
clra /* clear the accumulator */
rola /* shift the sample into A from the bottom */
#ifdef INVERT
eor #0x01 /* negate the received bit */
#endif
}
}
/* JTAG support */
/* initialises the JTAG TAP and brings the TAP into RUN-TEST/IDLE state */
void jtag_init(void) {
unsigned char i;
PTA = JTAG_IDLE; /* preload idle state into port A data register */
#ifdef DEBUG
DDRA = TDI_OUT_MASK | TCLK_OUT_MASK | TRST_OUT_MASK | TMS_OUT_MASK; /* RSTI_OUT and TA_OUT are inactive, the remaining OUT signals are outputs */
#else
DDRA = TDI_OUT_MASK | TCLK_OUT_MASK | TRST_OUT_MASK | TMS_OUT_MASK | DDRA_DDRA7; /* PTA7 is unused when not debugging, make sure it is output in such case */
#endif
PTC = 0;
DDRC = DDRC_DDRC1; /* make pin PTC1 output (it is not bonded out on the 20 pin package anyway) */
POCR = POCR_PTE20P; /* enable pull-ups on PTE0-2 (unused pins) */
/* RSTO edge capture */
T1SC = 0; /* enable timer 1 */
T1SC0; /* read the status and control register */
#ifdef INVERT
T1SC0 = T1SC0_ELS0A_MASK; /* capture rising edge (invert), this write will also clear the interrupt flag if set */
#else
T1SC0 = T1SC0_ELS0B_MASK; /* capture falling edge (non-invert), this write will also clear the interrupt flag if set */
#endif
T1SC0 |= T1SC0_CH0IE_MASK; /* enable input capture interrupt */
cable_status.reset=NO_RESET_ACTIVITY; /* clear the reset flag */
/* now the JTAG pins are in their default states and the RSTO sensing is set-up */
TRST_RESET(); /* assert TRST */
wait_1ms(50); /* 50ms */
TRST_SET(); /* de-assert TRST */
wait_1ms(10); /* 10ms */
TMS_SET(); /* bring TMS high */
for (i=10;i>0;i--) { /* create 10 pulses on the TCLK line in case TRST is not connected to bring TAP to TEST-LOGIC-RESET (but it better should be as it is BKPT...) */
TCLK_SET();
asm (NOP); asm (NOP); asm (NOP); /* nop padding to ensure that TCLK frequency is <0.5MHz, TCLK freq must be <= system clock / 4 and min crystal freq for 52235 is 2MHz... */
TCLK_RESET();
asm (NOP); asm (NOP); asm (NOP);
}
TMS_RESET(); /* take TMS low */
TCLK_SET(); /* transition TAP to RUN-TEST/IDLE */
asm (NOP); asm (NOP); asm (NOP);
TCLK_RESET();
asm (NOP); asm (NOP); asm (NOP);
}
/* trasitions the state machine from RUN-TEST/IDLE to SHIFT-DR or SHIFT-IR state */
/* if mode == 0 DR path is used, if mode != 0 IR path is used */
/* leaves TCLK high and TMS low on exit */
void jtag_transition_shift(unsigned char mode) {
TMS_SET(); /* bring TMS high */
TCLK_RESET(); /* just in case TCLK was left high */
asm (NOP); asm (NOP); asm (NOP);
TCLK_SET(); /* transition TAP to SELECT DR-SCAN */
asm (NOP); asm (NOP); asm (NOP);
TCLK_RESET();
asm (NOP); asm (NOP); asm (NOP);
if (mode!=0) { /* select the IR path */
TCLK_SET(); /* transition TAP to SELECT IR-SCAN */
asm (NOP); asm (NOP); asm (NOP);
TCLK_RESET();
asm (NOP); asm (NOP); asm (NOP);
}
TMS_RESET(); /* take TMS low */
TCLK_SET(); /* transition TAP to CAPTURE-DR/IR */
asm (NOP); asm (NOP); asm (NOP);
TCLK_RESET();
asm (NOP); asm (NOP); asm (NOP);
TCLK_SET(); /* transition TAP to SHIFT-DR/IR */
asm (NOP); asm (NOP); asm (NOP);
TCLK_RESET();
}
/* transitions the JTAG state machine to the TEST-LOGIC-REST state */
/* the jtag must be re-initialised after this has happened since no other routine knows how to get out of this state */
void jtag_transition_reset(void) {
unsigned char i;
TMS_SET(); /* bring TMS high */
for (i=10;i>0;i--) { /* create 10 pulses on the TCLK line */
TCLK_SET();
asm (NOP); asm (NOP); asm (NOP);
TCLK_RESET();
asm (NOP); asm (NOP); asm (NOP);
}
}
/* writes given bit stream into data/instruction path of JTAG */
/* bit_count specifies the number of bits to write */
/* data are transmitted starting with LSB of the LAST byte in the supplied buffer (for easier readability of code which uses JTAG) */
/* expects to find the TAP in SHIFT-DR or SHIFT-IR state */
/* if tap_transition == 0 leaves the TAP state unchanged, if tap_transition != 0 leaves the tap in RUN-TEST/IDLE */
void jtag_write(unsigned char tap_transition, unsigned char bit_count, unsigned char * datap) {
unsigned char bit_no=0;
unsigned char data;
datap += (bit_count>>3); /* each byte has 8 bits, point to the last byte in the buffer */
if ((bit_count&0x07)==0) datap--; /* the size fits into the bytes precisely */
while (bit_no<bit_count) {
if ((bit_no&0x07)==0) data=*(datap--); /* fetch new byte from the buffer and update the pointer */
if (data&0x01) TDI_OUT_SET(); else TDI_OUT_RESET(); /* assign new bit value to TDI */
TCLK_RESET(); /* take TCLK low */
data>>=1; /* shift the data down */
bit_no++; /* update the bit number */
if (bit_no==bit_count) if (tap_transition!=0) TMS_SET();/* bring TMS high if exit from the SHIFT state is required */
TCLK_SET(); /* take TCLK high */
}
if (tap_transition) { /* the TAP is now in EXIT1-DR/IR if exit was requested */
TCLK_RESET(); /* take TCLK low */
asm (NOP); asm (NOP); asm (NOP);
TCLK_SET(); /* bring TCLK high */ /* transition TAP to UPDATE-DR/IR */
asm (NOP); asm (NOP); asm (NOP);
TMS_RESET(); /* take TMS low */
TCLK_RESET(); /* take TCLK low */
asm (NOP); asm (NOP); asm (NOP);
TCLK_SET(); /* bring TCLK high */ /* transition TAP to RUN-TEST/IDLE */
asm (NOP); asm (NOP); asm (NOP);
TCLK_RESET(); /* take TCLK low */
} /* now the tap is in RUN-TEST/IDLE if exit was requested */
}
/* reads bitstream out of JTAG */
/* bit_count specifies the number of bits to read */
/* data are stored starting with LSB of the LAST byte in the supplied buffer (for easier readability of code which uses JTAG) */
/* expects to find the TAP in SHIFT-DR or SHIFT-IR state */
/* if tap_transition == 0 leaves the TAP state unchanged, if tap_transition != 0 leaves the tap in RUN-TEST/IDLE */
void jtag_read(unsigned char tap_transition, unsigned char bit_count, unsigned char * datap) {
unsigned char bit_no=0;
unsigned char data=0;
datap += (bit_count>>3); /* each byte has 8 bits, point to the last byte in the buffer */
if ((bit_count&0x07)==0) datap--; /* the size fits into the bytes precisely */
while (bit_no<bit_count) {
TCLK_RESET(); /* take TCLK low */
data>>=1; /* shift the data down */
if (TDO_IN_SET) data|=0x80; /* move the current bit into the data variable */
bit_no++; /* update the bit number */
if ((bit_no&0x07)==0) {
*(datap--)=data; /* store the received byte into the buffer and update the pointer */
data=0; /* clear the receiving variable */
}
if (bit_no==bit_count) if (tap_transition!=0) TMS_SET();/* bring TMS high if exit from the SHIFT state is required */
TCLK_SET(); /* take TCLK high */
}
if ((bit_no&0x07)!=0) { /* there are bits in the data variable to be stored into the buffer */
data>>=(8-(bit_no&0x07)); /* shift the bits into the right place */
*(datap)=data; /* store the last bits into the buffer */
}
if (tap_transition) { /* the TAP is now in EXIT1-DR/IR if exit was requested */
TCLK_RESET(); /* take TCLK low */
asm (NOP); asm (NOP); asm (NOP);
TCLK_SET(); /* bring TCLK high */ /* transition TAP to UPDATE-DR/IR */
asm (NOP); asm (NOP); asm (NOP);
TMS_RESET(); /* take TMS low */
TCLK_RESET(); /* take TCLK low */
asm (NOP); asm (NOP); asm (NOP);
TCLK_SET(); /* bring TCLK high */ /* transition TAP to RUN-TEST/IDLE */
asm (NOP); asm (NOP); asm (NOP);
TCLK_RESET(); /* take TCLK low */
} /* now the tap is in RUN-TEST/IDLE if exit was requested */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -