smartpower的笔记 https://bbs.21ic.com/?392597 [收藏] [复制] [RSS] 一些电子设计中用得到的实用资料

日志

用IO模拟SWD协议,读写Cortex m0+ 寄存器

已有 38 次阅读2020-12-31 14:00 |个人分类:单片机|系统分类:ARM| SWD, 协议, 寄存器, 模拟

int main(void)
{
    __IO uint8_t  tmp8;
    __IO uint32_t tmp32;
    
    M0P_SYSCTRL->PERICLKEN0 = 0xffffffff;
    M0P_SYSCTRL->PERICLKEN1 = 0xffffffff;
    M0P_GPIOA->ADS = 0x00;
    Swd_Bus_Reset();
    
    
    while1 )
    {
//        Swd_Bus_Reset();
//        
//        Swd_Bus_SendByte( 0x9E );
//        Swd_Bus_SendByte( 0xE7 );
//        Swd_Bus_SendByte( 0x00 );
//        Swd_Bus_SendByte( 0x00 );
//        
        Swd_Bus_Reset();
        Swd_Bus_SendByte0x00 );
        
        //--------------------------------------------------
        //Read DP.IDR
        Swd_Bus_SendByte0xa5 );
        Swd_Bus_Turn();
        tmp8 = Swd_Bus_RecvAck();
        tmp32 = Swd_Bus_RecvWordAndParity();
        Swd_Bus_Turn();
        Swd_Bus_SendByte0x00 );
        
        //--------------------------------------------------
        //write ctrl/state
        Swd_Bus_SendByte0xa9 );
        Swd_Bus_Turn();
        tmp8 = Swd_Bus_RecvAck();
        Swd_Bus_Turn();
        Swd_Bus_SendWordAndParity0x50000000 );
        Swd_Bus_SendByte0x00 );
        
        //--------------------------------------------------
        //Read AP.IDR
        Swd_Bus_SendByte0xb1 );  //write select AP BANK - 0F
        Swd_Bus_Turn();
        tmp8 = Swd_Bus_RecvAck();
        Swd_Bus_Turn();
        Swd_Bus_SendWordAndParity0x000000f0 );
        Swd_Bus_SendByte0x00 );
        
        Swd_Bus_SendByte0x9f );  //read DRW dummy
        Swd_Bus_Turn();
        tmp8  = Swd_Bus_RecvAck();
        tmp32 = Swd_Bus_RecvWordAndParity();
        Swd_Bus_Turn();
        Swd_Bus_SendByte0x00 );
        
        Swd_Bus_SendByte0xbd );  //read rdbuf
        Swd_Bus_Turn();
        tmp8  = Swd_Bus_RecvAck();
        tmp32 = Swd_Bus_RecvWordAndParity();
        Swd_Bus_Turn();
        Swd_Bus_SendByte0x00 );
        
        //--------------------------------------------------
        //SET AP 位宽为32位
        Swd_Bus_SendByte0xb1 );   //write select AP BANK - 00
        Swd_Bus_Turn();
        tmp8 = Swd_Bus_RecvAck();
        Swd_Bus_Turn();
        Swd_Bus_SendWordAndParity0x00000000 );
        Swd_Bus_SendByte0x00 );
        
        Swd_Bus_SendByte0xA3 );  //write CSW - 
        Swd_Bus_Turn();
        tmp8 = Swd_Bus_RecvAck();
        Swd_Bus_Turn();
        Swd_Bus_SendWordAndParity0x23000002 );  //32bit 位宽,地址不自动增加
        Swd_Bus_SendByte0x00 );
        
        //--------------------------------------------------
        //stop the cpu
        Swd_Bus_SendByte0x8B );  //write TAR
        Swd_Bus_Turn();
        tmp8 = Swd_Bus_RecvAck();
        Swd_Bus_Turn();
        Swd_Bus_SendWordAndParity0xE000EDF0 );  //
        Swd_Bus_SendByte0x00 );
        
        Swd_Bus_SendByte0xBB );  //WRITE DRW 
        Swd_Bus_Turn();
        tmp8 = Swd_Bus_RecvAck();
        Swd_Bus_Turn();
        Swd_Bus_SendWordAndParity0xA05F0303 );  
        Swd_Bus_SendByte0x00 );
        
        //--------------------------------------------------
        //Read User Memery  @ 0x00100d90
        Swd_Bus_SendByte0x8B );  //write TAR
        Swd_Bus_Turn();
        tmp8 = Swd_Bus_RecvAck();
        Swd_Bus_Turn();
        Swd_Bus_SendWordAndParity0x00100d90 );  
        Swd_Bus_SendByte0x00 );
        
        Swd_Bus_SendByte0x9f );  //read DRW 
        Swd_Bus_Turn();
        tmp8  = Swd_Bus_RecvAck();
        tmp32 = Swd_Bus_RecvWordAndParity();   //dummy
        Swd_Bus_Turn();
        Swd_Bus_SendByte0x00 );
        
        Swd_Bus_SendByte0xbd );  //read rdbuf
        Swd_Bus_Turn();
        tmp8  = Swd_Bus_RecvAck();
        tmp32 = Swd_Bus_RecvWordAndParity();   //read 
        Swd_Bus_Turn();
        Swd_Bus_SendByte0x00 );

        //--------------------------------
        //write and read reg  @ 0x40020F04
        Swd_Bus_SendByte0x8B );   //write TAR
        Swd_Bus_Turn();
        tmp8 = Swd_Bus_RecvAck();
        Swd_Bus_Turn();
        Swd_Bus_SendWordAndParity0x40020F04 );  
        Swd_Bus_SendByte0x00 );
        
        Swd_Bus_SendByte0xBB );  //WRITE DRW 
        Swd_Bus_Turn();
        tmp8 = Swd_Bus_RecvAck();
        Swd_Bus_Turn();
        Swd_Bus_SendWordAndParity0xffffffff );  
        Swd_Bus_SendByte0x00 );
        
        Swd_Bus_SendByte0x9f );  //read DRW dummy
        Swd_Bus_Turn();
        tmp8  = Swd_Bus_RecvAck();
        tmp32 = Swd_Bus_RecvWordAndParity();
        Swd_Bus_Turn();
        Swd_Bus_SendByte0x00 );
        
        Swd_Bus_SendByte0xbd );    //read rdbuf
        Swd_Bus_Turn();
        tmp8  = Swd_Bus_RecvAck();
        tmp32 = Swd_Bus_RecvWordAndParity();
        Swd_Bus_Turn();
        Swd_Bus_SendByte0x00 );
       
        while1 );

    }
    
}

//==============================================================================

void Swd_Bus_Resetvoid )
{
    uint8_t i;
    
    SWCLK_SetOutput();
    SWDIO_SetOutput();
    SWDIO_SetHigh();
    SWCLK_SetLow();
    
    fori=0i<56i++ )
    {
        SWCLK_SetHigh();
        SwdDly();
        SWCLK_SetLow();
        SwdDly();
    }
}

void Swd_Bus_Turnvoid )
{
    SWDIO_SetInput();
    SwdDly();
    SWCLK_SetHigh();
    SwdDly();
    SWCLK_SetLow();
    SwdDly();    
}

//LSB FIRST
void Swd_Bus_SendByteuint8_t Va )
{
    uint8_t i;
    
    SWDIO_SetLow();
    SWDIO_SetOutput();
    
    fori=0i<8i++ )
    {
        ifVa & 0x01 )
        {
            SWDIO_SetHigh();
        }
        else
        {
            SWDIO_SetLow();
        }

        Va >>= 1;
        SwdDly();
        SWCLK_SetHigh();
        SwdDly();
        SWCLK_SetLow();
    }
    SwdDly();
}

//LSB FIRST
void Swd_Bus_SendWordAndParityuint32_t Va )
{
    uint8_t i;
    uint8_t Pa = 0x00;
    
    SWDIO_SetLow();
    SWDIO_SetOutput();
    
    fori=0i<32i++ )
    {
        ifVa & 0x01 )
        {
            SWDIO_SetHigh();
            Pa++;
        }
        else
        {
            SWDIO_SetLow();
        }

        Va >>= 1;
        SwdDly();
        SWCLK_SetHigh();
        SwdDly();
        SWCLK_SetLow();
    }
    
    //send Parity
    ifPa & 0x01 )
    {
        SWDIO_SetHigh();
    }
    else
    {
        SWDIO_SetLow();
    }
    SwdDly();
    SWCLK_SetHigh();
    SwdDly();
    SWCLK_SetLow();
    SwdDly();

}

//LSB FIRST
uint32_t Swd_Bus_RecvWordAndParityvoid )
{
    uint8_t  i;
    uint32_t tmp32;
    
    tmp32 = 0x00;
    SWDIO_SetInput();
    SwdDly();
    fori=0i<32i++ )
    {
        ifSWDIO_GetValue() )
        {
            tmp32 >>= 1;
            tmp32 |= bv31;
        }
        else
        {
            tmp32 >>= 1;
        }

        SWCLK_SetHigh();
        SwdDly();
        SWCLK_SetLow();
        SwdDly();
    }
    
    //------------------------------
    //Parity
    SWCLK_SetHigh();
    SwdDly();
    SWCLK_SetLow();
    SwdDly();
    
    returntmp32 );

}

//LSB FIRST
uint8_t Swd_Bus_RecvAckvoid )
{
    uint8_t i;
    uint8_t tmp8;
    
    tmp8 = 0x00;
    SWDIO_SetInput();
    SwdDly();
    fori=0i<3i++ )
    {
        ifSWDIO_GetValue() )
        {
            tmp8 >>= 1;
            tmp8 |= bv2;
        }
        else
        {
            tmp8 >>= 1;
        }

        SWCLK_SetHigh();
        SwdDly();
        SWCLK_SetLow();
        SwdDly();
    }
    
    returntmp8 );
}




//==============================================================================







路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

返回顶部