/*************************************************
ת»»JTAG ΪSWDµ÷ÊÔ½Ó¿Ú
ÒòΪоƬÉϵçĬÈÏΪJTAGµ÷ÊÔ¿Ú ËùÒÔÐèÒª½øÐнӿڵÄת»»
*************************************************/
void swd_rst(void)
{
uint16_t i;
SWDIO1;///SWDIO=1
SWDCK0;///SWDCK=0
for(i=0;i<53;i++)
{
SWDCK1;///SWDCK=1
SWDCK0;///SWDCK=0
}
swd_tx_data(0x79E70000,16,1);///·¢ËÍ16λÊý0x79e7
SWDIO1;///SWDIO=1
for(i=0;i<53;i++)
{
SWDCK1;///SWDCK=1
SWDCK0;///SWDCK=0
}
SWDIO0;///SWDIO=0
SWDCK1; SWDCK0;
SWDCK1; SWDCK0;
}
/*************************************************
·¢ËÍÖ¸¶¨³¤¶ÈµÄÊý¾Ýµ½SWD×ÜÏßÉÏ
*************************************************/
void swd_tx_data(uint32_t d,uint8_t length,uint8_t MSB)
{
uint32_t d32=d;
uint8_t i;
for(i=0;i<length;i++)
{
if(MSB)
{
if(d32&0x80000000) {SWDIO1;}else {SWDIO0;}
SWDCK1;SWDCK0;
d32=d32<<1;
}
else
{
if(d32&0x0001) {SWDIO1;}else {SWDIO0;}
SWDCK1;SWDCK0;
d32=d32>>1;
}
}
}
/*************************************************
½ÓÊÕÖ¸¶¨³¤¶ÈµÄÊý¾Ýµ½SWD×ÜÏßÉÏ
*************************************************/
uint32_t swd_rx_data(uint8_t num)
{
uint32_t d32;
uint8_t i;
for(i=0;i<num;i++)
{
SWDCK1;
if(ReadGpio(SWDIO)) d32=(d32>>1) +0x80000000;
else d32=(d32>>1) ;
SWDCK0;
}
return d32;
}
/*************************************************
Éú²úDPÃüÁî×Ö
*************************************************/
uint8_t swd_cmd(uint8_t AP,uint8_t RD,uint8_t adr)
{
uint8_t cmd,one;
cmd=0;
one=0;
cmd=1; ///start
if(AP){cmd=(cmd<<1)+1;one++;} else {cmd=(cmd<<1)+0;} ///dp/ap 0-dp 1-ap
if(RD){cmd=(cmd<<1)+1;one++;} else {cmd=(cmd<<1)+0;} ///rw 1-r 0-w
if(adr&0x01){cmd=(cmd<<1)+1;one++;} else {cmd=(cmd<<1)+0;}///adr0
if(adr&0x02){cmd=(cmd<<1)+1;one++;} else {cmd=(cmd<<1)+0;}///adr1
if(one&0x01){cmd=(cmd<<1)+1;} else {cmd=(cmd<<1)+0;}///parity
cmd=(cmd<<1)+0; ///stop
cmd=(cmd<<1)+1; ///park
return cmd;
}
/*************************************************
¶ÁAP»òDP
*************************************************/
uint8_t swd_rd(uint8_t AP,uint8_t adr,uint32_t *d32)
{
uint8_t ack;
uint8_t one=0;
uint32_t cmd,ACK;
cmd=swd_cmd(AP,1,adr);//////Éú³ÉÃüÁî
swd_tx_data(cmd<<24,8,1);
SWDIO1; ack=0;
ACK=swd_rx_data(3); //////////////////½ÓÊÕACK
if(ACK&0x20000000) ack=(ack<<1)+1;else ack=ack<<1;
if(ACK&0x40000000) ack=(ack<<1)+1;else ack=ack<<1;
if(ACK&0x80000000) ack=(ack<<1)+1;else ack=ack<<1;
sleep_us(5);
*d32= swd_rx_data(32); ///RX 32bit
sleep_us(5);
SWDIO1;
SWDCK1; SWDCK0; ///parity
SWDCK1; SWDCK0; ///////Õâ¸öµØ·½ºÍÎĵµËµÃ÷»¹ÊÇÓгöÈëµÄ
SWDCK1; SWDCK0; ///////Õâ¸öµØ·½ºÍÎĵµËµÃ÷»¹ÊÇÓгöÈëµÄ
return ack;
}
/*************************************************
дAP»òDP
*************************************************/
uint8_t swd_wr(uint8_t AP,uint8_t adr,uint32_t d32)
{
uint8_t ack;
uint32_t cmd,ACK;
cmd=swd_cmd(AP,0,adr);//////Éú³ÉÃüÁî
swd_tx_data(cmd<<24,8,1);
SWDIO1; ack=0;
ACK=swd_rx_data(3); //////////////////½ÓÊÕACK
if(ACK&0x20000000) ack=(ack<<1)+1;else ack=ack<<1;
if(ACK&0x40000000) ack=(ack<<1)+1;else ack=ack<<1;
if(ACK&0x80000000) ack=(ack<<1)+1;else ack=ack<<1;
SWDCK1; SWDCK0;////////////¡//Õâ¸öµØ·½ºÍÎĵµËµÃ÷»¹ÊÇÓгöÈëµÄ
SWDCK1; SWDCK0;//////////////Õâ¸öµØ·½ºÍÎĵµËµÃ÷»¹ÊÇÓгöÈëµÄ
sleep_us(5);
swd_tx_data(d32,32,0); ///32bit
sleep_us(5);
if(bit_QiJiaoYan(d32)){SWDIO1;}
else {SWDIO0;}
SWDCK1; SWDCK0; ///parity
SWDIO1;
return ack;
} |