Files
eb001/tb/sw/src/SlvMain.c

234 lines
5.2 KiB
C
Raw Normal View History

2024-10-29 17:32:17 +08:00
#include <stdint.h>
// #include "uart.h"
// #include "gpio.h"
// #include "timer.h"
volatile uint32_t *cdr_0_tx_status = (uint32_t*)( 0x40000000 + 0x0 ); //read
volatile uint32_t *cdr_0_tx_txLen = (uint32_t*)( 0x40000000 + 0x4 ); //write
volatile uint32_t *cdr_0_tx_txFifo = (uint32_t*)( 0x40000000 + 0x8 ); //write
volatile uint32_t *cdr_0_rx_softReset = (uint32_t*)( 0x40000000 + 0x10 ); //write
volatile uint32_t *cdr_0_rx_status = (uint32_t*)( 0x40000000 + 0x14 ); //read
volatile uint32_t *cdr_0_rx_rxLen = (uint32_t*)( 0x40000000 + 0x18 ); //read
volatile uint32_t *cdr_0_rx_rxFifo = (uint32_t*)( 0x40000000 + 0x1c ); //read
volatile uint32_t *cdr_1_tx_status = (uint32_t*)( 0x40000080 + 0x0 ); //read
volatile uint32_t *cdr_1_tx_txLen = (uint32_t*)( 0x40000080 + 0x4 ); //write
volatile uint32_t *cdr_1_tx_txFifo = (uint32_t*)( 0x40000080 + 0x8 ); //write
volatile uint32_t *cdr_1_rx_softReset = (uint32_t*)( 0x40000080 + 0x10 ); //write
volatile uint32_t *cdr_1_rx_status = (uint32_t*)( 0x40000080 + 0x14 ); //read
volatile uint32_t *cdr_1_rx_rxLen = (uint32_t*)( 0x40000080 + 0x18 ); //read
volatile uint32_t *cdr_1_rx_rxFifo = (uint32_t*)( 0x40000080 + 0x1c ); //read
volatile uint32_t *WriteGpio = (uint32_t*) ( 0x60000000);
volatile uint32_t *ReadGpio = (uint32_t*) ( 0x60000000);
volatile uint32_t *ReadIsLast = (uint32_t*) ( 0x60000008);
static inline void error() {
while(1);
}
void main()
{
uint32_t readDat;
2024-10-30 17:51:33 +08:00
uint32_t operator;
uint32_t downRecvLen;
uint32_t upRecvLen;
2024-10-29 17:32:17 +08:00
2024-10-30 17:51:33 +08:00
uint32_t slvNum;
uint32_t totNum;
uint32_t isLast;
2024-10-29 17:32:17 +08:00
while(1){
2024-10-30 17:51:33 +08:00
isLast = *ReadIsLast;
2024-10-29 17:32:17 +08:00
asm volatile (
"1:\n"
"andi ra, sp, 0x04\n"
"beqz ra, 1b\n"
"andi sp, sp, 0xFB\n"
2024-10-29 17:32:17 +08:00
);
readDat = *cdr_0_rx_rxFifo;
operator = (readDat >> 16) & 0xf;
downRecvLen = readDat >> 20;
2024-10-29 17:32:17 +08:00
readDat = *cdr_0_rx_rxFifo;
if( operator == 0 ){
slvNum = 64 - ( downRecvLen >> 3 );
2024-10-30 17:51:33 +08:00
uint32_t localLen = 8;
uint32_t downSendLen = downRecvLen - 8;
2024-10-29 17:32:17 +08:00
//接收本地包
2024-10-30 17:51:33 +08:00
for( uint32_t i = 0; i < 8/4; i++ ){
asm volatile (
"andi ra, sp, 0x08\n"
"1:\n"
"bnez ra, 1b\n" //error
);
2024-10-29 17:32:17 +08:00
readDat = *cdr_0_rx_rxFifo;
}
2024-10-30 17:51:33 +08:00
if(!isLast){
// 下行转发新建包头
*cdr_0_tx_txLen = downRecvLen; //downSendLen + 8
*cdr_0_tx_txFifo = (downSendLen << 20) |( 0 << 16) | 0;
*cdr_0_tx_txFifo = 0;
2024-10-29 17:32:17 +08:00
2024-10-30 17:51:33 +08:00
//下行转发负载
asm volatile (
"lui t0, %hi(0x40000000)\n"
);
2024-10-30 17:51:33 +08:00
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, 0x1c(t0)\n"
"sw t1, 0x08(t0)\n"
// : : : "a0", "a1"
);
//*cdr_0_tx_txFifo = *cdr_0_rx_rxFifo; //接收即发送
}
} else{ //isLast
//空转掉剩余负载
asm volatile (
2024-10-30 17:51:33 +08:00
"lui t0, %hi(0x40000000)\n"
);
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, 0x1c(t0)\n"
);
}
2024-10-29 17:32:17 +08:00
}
2024-10-30 17:51:33 +08:00
//CRC
asm volatile (
"andi ra, sp, 0x08\n"
"1:\n"
"bnez ra, 1b\n" //error
);
uint32_t crc = *cdr_0_rx_rxFifo; //CRC
2024-10-29 17:32:17 +08:00
//等待接收完成标志
2024-10-29 17:32:17 +08:00
asm volatile (
"1:\n"
"andi ra, sp, 0x08\n"
"beqz ra, 1b\n"
"andi sp, sp, 0xF7\n"
2024-10-29 17:32:17 +08:00
);
2024-10-30 17:51:33 +08:00
if(!isLast){
//上行转发
2024-10-29 17:32:17 +08:00
2024-10-30 17:51:33 +08:00
//接收第一个包
asm volatile (
"1:\n"
2024-10-30 17:51:33 +08:00
"andi ra, sp, 0x40\n"
"beqz ra, 1b\n"
"andi sp, sp, 0xBF\n"
);
2024-10-29 17:32:17 +08:00
2024-10-30 17:51:33 +08:00
readDat = *cdr_1_rx_rxFifo;
operator = (readDat >> 16) & 0xf;
upRecvLen = readDat >> 20;
totNum = slvNum + (upRecvLen >> 3) - 1;
2024-10-29 17:32:17 +08:00
2024-10-30 17:51:33 +08:00
//接收第二个包
asm volatile (
"andi ra, sp, 0x80\n"
"1:\n"
"bnez ra, 1b\n" //error
);
readDat = *cdr_1_rx_rxFifo;
//发送上行包头
*cdr_1_tx_txLen = upRecvLen + 16; //upSendLen + 8
*cdr_1_tx_txFifo = ((upRecvLen + 8) << 20) |( 0 << 16) | 0;
2024-10-29 17:32:17 +08:00
*cdr_1_tx_txFifo = 0;
2024-10-30 17:51:33 +08:00
for( uint8_t i = 0; i < 8/4; i ++ ){
*cdr_1_tx_txFifo = 0;
}
//上行转发负载
asm volatile (
"lui t0, %hi(0x40000000)\n"
);
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, 0x9c(t0)\n"
"sw t1, 0x88(t0)\n"
);
// *cdr_1_tx_txFifo = *cdr_1_rx_rxFifo; //接收即发送
}
//CRC
2024-10-29 17:32:17 +08:00
asm volatile (
"andi ra, sp, 0x80\n"
2024-10-29 17:32:17 +08:00
"1:\n"
"bnez ra, 1b\n" //error
2024-10-29 17:32:17 +08:00
);
2024-10-30 17:51:33 +08:00
crc = *cdr_1_rx_rxFifo; //CRC
//等待接收完成标志
asm volatile (
"1:\n"
"andi ra, sp, 0x80\n"
"beqz ra, 1b\n"
"andi sp, sp, 0x7F\n"
);
2024-10-29 17:32:17 +08:00
2024-10-30 17:51:33 +08:00
} else { //islast
//发送上行包头
*cdr_1_tx_txLen = 16; //upSendLen + 8
*cdr_1_tx_txFifo = (8 << 20) |( 0 << 16) | 0;
*cdr_1_tx_txFifo = 0;
for( uint8_t i = 0; i < 8/4; i ++ ){
*cdr_1_tx_txFifo = 0;
}
2024-10-29 17:32:17 +08:00
}
2024-10-30 17:51:33 +08:00
2024-10-29 17:32:17 +08:00
} else if ( operator == 1 ){
error();
} else if ( operator == 2 ){
error();
} else {
error();
}
}
}