#include // #include "uart.h" // #include "gpio.h" // #include "timer.h" volatile uint32_t *cdr_status = (uint32_t*)( 0x000 + 0x0 ); //read volatile uint32_t *cdr_txLen = (uint32_t*)( 0x000 + 0x4 ); //write volatile uint32_t *cdr_txFifo = (uint32_t*)( 0x000 + 0x8 ); //write volatile uint32_t *cdr_direct = (uint32_t*)( 0x000 + 0xc ); //write volatile uint32_t *cdr_softReset = (uint32_t*)( 0x000 + 0x10 ); //write volatile uint32_t *cdr_rxLen = (uint32_t*)( 0x000 + 0x18 ); //read volatile uint32_t *cdr_rxFifo = (uint32_t*)( 0x000 + 0x1c ); //read volatile uint32_t *WriteGpio = (uint32_t*) ( 0x400); volatile uint32_t *ReadGpio = (uint32_t*) ( 0x400); volatile uint32_t *ReadIsLast = (uint32_t*) ( 0x408); static inline void error() { while(1); } void main() { uint32_t readDat; uint32_t operator; uint32_t downRecvLen; uint32_t upRecvLen; uint32_t slvNum; uint32_t totNum; uint32_t isLast; while(1){ *cdr_softReset = 1; //复位脉冲 isLast = *ReadIsLast; *cdr_direct = 0; asm volatile ( "1:\n" "andi ra, sp, 0x04\n" "beqz ra, 1b\n" "andi sp, sp, 0xFB\n" ); readDat = *cdr_rxFifo; operator = (readDat >> 16) & 0xf; downRecvLen = readDat >> 20; readDat = *cdr_rxFifo; if( operator == 0 ){ slvNum = 64 - ( downRecvLen >> 3 ); uint32_t localLen = 8; uint32_t downSendLen = downRecvLen - 8; //接收本地包 for( uint32_t i = 0; i < 8/4; i++ ){ asm volatile ( "andi ra, sp, 0x08\n" "1:\n" "bnez ra, 1b\n" //error ); readDat = *cdr_rxFifo; } if(!isLast){ // 下行转发新建包头 *cdr_txLen = downRecvLen; //downSendLen + 8 *cdr_txFifo = (downSendLen << 20) |( 0 << 16) | 0; *cdr_txFifo = 0; //下行转发负载 for( uint32_t i = 0; i < (downSendLen >> 2); i++ ){ //一次传4byte asm volatile ( "andi ra, sp, 0x08\n" "1:\n" "bnez ra, 1b\n" //error 速度不够,不再检查接收 "lw t1, 0x01c(x0)\n" "sw t1, 0x008(x0)\n" ); //*cdr_0_tx_txFifo = *cdr_0_rx_rxFifo; //接收即发送 } } else{ //isLast //空转掉剩余负载 for( uint32_t i = 0; i < (downSendLen >> 2); i++ ){ //一次传4byte asm volatile ( "andi ra, sp, 0x08\n" "1:\n" "bnez ra, 1b\n" //error 速度不够,不再检查接收 // "lui t0, %hi(0x40000000)\n" "lw t1, 0x01c(x0)\n" ); } } //CRC asm volatile ( "andi ra, sp, 0x08\n" "1:\n" "bnez ra, 1b\n" //error ); uint32_t crc = *cdr_rxFifo; //CRC //等待接收完成标志 asm volatile ( "1:\n" "andi ra, sp, 0x08\n" "beqz ra, 1b\n" "andi sp, sp, 0xF7\n" ); *cdr_direct = 1; //方向转换 if(!isLast){ //上行转发 //接收第一个包 asm volatile ( "1:\n" "andi ra, sp, 0x04\n" "beqz ra, 1b\n" "andi sp, sp, 0xFB\n" ); readDat = *cdr_rxFifo; operator = (readDat >> 16) & 0xf; upRecvLen = readDat >> 20; totNum = slvNum + (upRecvLen >> 3) - 1; //接收第二个包 asm volatile ( "andi ra, sp, 0x08\n" "1:\n" "bnez ra, 1b\n" //error ); readDat = *cdr_rxFifo; //发送上行包头 *cdr_txLen = upRecvLen + 16; //upSendLen + 8 *cdr_txFifo = ((upRecvLen + 8) << 20) |( 0 << 16) | 0; *cdr_txFifo = 0; for( uint8_t i = 0; i < 8/4; i ++ ){ *cdr_txFifo = 0; } //上行转发负载 for( uint16_t i = 0; i < upRecvLen >> 2; i ++ ){ //一次传4byte asm volatile ( "andi ra, sp, 0x80\n" "1:\n" "bnez ra, 1b\n" //error 速度不够,不再检查接收 "lw t1, 0x01c(x0)\n" "sw t1, 0x008(x0)\n" ); // *cdr_1_tx_txFifo = *cdr_1_rx_rxFifo; //接收即发送 } //CRC asm volatile ( "andi ra, sp, 0x08\n" "1:\n" "bnez ra, 1b\n" //error ); crc = *cdr_rxFifo; //CRC //等待接收完成标志 asm volatile ( "1:\n" "andi ra, sp, 0x08\n" "beqz ra, 1b\n" "andi sp, sp, 0xF7\n" ); } else { //islast //发送上行包头 *cdr_txLen = 16; //upSendLen + 8 *cdr_txFifo = (8 << 20) |( 0 << 16) | 0; *cdr_txFifo = 0; for( uint8_t i = 0; i < 8/4; i ++ ){ *cdr_txFifo = 0; } } } else if ( operator == 1 ){ error(); } else if ( operator == 2 ){ error(); } else { error(); } } }