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

269 lines
4.8 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"
2024-11-01 17:07:35 +08:00
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
2024-10-29 17:32:17 +08:00
2024-11-01 17:07:35 +08:00
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
2024-10-29 17:32:17 +08:00
volatile uint32_t *WriteGpio = (uint32_t*) ( 0x400);
volatile uint32_t *ReadGpio = (uint32_t*) ( 0x400);
volatile uint32_t *ReadIsLast = (uint32_t*) ( 0x408);
2024-10-29 17:32:17 +08:00
2024-11-01 17:07:35 +08:00
2024-10-29 17:32:17 +08:00
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-11-01 17:07:35 +08:00
*cdr_softReset = 1; //复位脉冲
2024-10-30 17:51:33 +08:00
isLast = *ReadIsLast;
2024-11-01 17:07:35 +08:00
*cdr_direct = 0;
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
);
2024-11-01 17:07:35 +08:00
readDat = *cdr_rxFifo;
2024-10-29 17:32:17 +08:00
operator = (readDat >> 16) & 0xf;
downRecvLen = readDat >> 20;
2024-10-29 17:32:17 +08:00
2024-11-01 17:07:35 +08:00
readDat = *cdr_rxFifo;
2024-10-29 17:32:17 +08:00
if( operator == 0 ){
2024-11-04 16:47:01 +08:00
2024-10-29 17:32:17 +08:00
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-11-01 17:07:35 +08:00
readDat = *cdr_rxFifo;
2024-10-29 17:32:17 +08:00
}
2024-11-04 16:47:01 +08:00
2024-10-30 17:51:33 +08:00
if(!isLast){
// 下行转发新建包头
2024-11-01 17:07:35 +08:00
*cdr_txLen = downRecvLen; //downSendLen + 8
*cdr_txFifo = (downSendLen << 20) |( 0 << 16) | 0;
*cdr_txFifo = 0;
2024-10-29 17:32:17 +08:00
2024-11-01 17:07:35 +08:00
//下行转发负载
2024-10-30 17:51:33 +08:00
for( uint32_t i = 0; i < (downSendLen >> 2); i++ ){ //一次传4byte
asm volatile (
2024-11-01 17:07:35 +08:00
"andi ra, sp, 0x08\n"
"1:\n"
"bnez ra, 1b\n" //error 速度不够,不再检查接收
2024-11-04 16:47:01 +08:00
"lw t2, 0x01c(x0)\n"
"sw t2, 0x008(x0)\n"
2024-10-30 17:51:33 +08:00
);
//*cdr_0_tx_txFifo = *cdr_0_rx_rxFifo; //接收即发送
}
} else{ //isLast
//空转掉剩余负载
for( uint32_t i = 0; i < (downSendLen >> 2); i++ ){ //一次传4byte
asm volatile (
2024-11-01 17:07:35 +08:00
"andi ra, sp, 0x08\n"
"1:\n"
"bnez ra, 1b\n" //error 速度不够,不再检查接收
2024-10-30 17:51:33 +08:00
// "lui t0, %hi(0x40000000)\n"
2024-11-04 16:47:01 +08:00
"lw t2, 0x01c(x0)\n"
2024-10-30 17:51:33 +08:00
);
}
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
);
2024-11-01 17:07:35 +08:00
uint32_t crc = *cdr_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
);
if(!isLast){
//等待发送完成标志
asm volatile (
"1:\n"
"andi ra, sp, 0x01\n"
"beqz ra, 1b\n"
"andi sp, sp, 0xFE\n"
);
}
2024-11-01 17:07:35 +08:00
2024-11-04 14:39:26 +08:00
*cdr_softReset = 1; //复位脉冲
2024-11-01 17:07:35 +08:00
*cdr_direct = 1; //方向转换
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-11-01 17:07:35 +08:00
"andi ra, sp, 0x04\n"
2024-10-30 17:51:33 +08:00
"beqz ra, 1b\n"
2024-11-01 17:07:35 +08:00
"andi sp, sp, 0xFB\n"
2024-10-30 17:51:33 +08:00
);
readDat = *cdr_rxFifo;
2024-10-29 17:32:17 +08:00
// operator = (readDat >> 16) & 0xf; //速度不够,不校验
2024-10-30 17:51:33 +08:00
upRecvLen = readDat >> 20;
2024-10-29 17:32:17 +08:00
2024-10-30 17:51:33 +08:00
//发送上行包头
2024-11-01 17:07:35 +08:00
*cdr_txLen = upRecvLen + 16; //upSendLen + 8
*cdr_txFifo = ((upRecvLen + 8) << 20) |( 0 << 16) | 0;
*cdr_txFifo = 0;
2024-10-29 17:32:17 +08:00
for( uint32_t i = 0; i < 8/4; i ++ ){
2024-11-01 17:07:35 +08:00
*cdr_txFifo = 0;
2024-10-30 17:51:33 +08:00
}
//接收第二个包
// asm volatile (
// "andi ra, sp, 0x08\n"
// "1:\n"
// "bnez ra, 1b\n" //error //速度不够,不校验
// );
uint32_t temp = *cdr_rxFifo;
2024-10-30 17:51:33 +08:00
//上行转发负载
for( uint32_t i = 0; i < (upRecvLen >> 2); i ++ ){ //一次传4byte
2024-10-30 17:51:33 +08:00
asm volatile (
2024-11-01 17:07:35 +08:00
"andi ra, sp, 0x80\n"
"1:\n"
"bnez ra, 1b\n" //error 速度不够,不再检查接收
2024-11-04 16:47:01 +08:00
"lw t2, 0x01c(x0)\n"
"sw t2, 0x008(x0)\n"
2024-10-30 17:51:33 +08:00
);
// *cdr_1_tx_txFifo = *cdr_1_rx_rxFifo; //接收即发送
}
//CRC
2024-10-29 17:32:17 +08:00
asm volatile (
2024-11-01 17:07:35 +08:00
"andi ra, sp, 0x08\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-11-01 17:07:35 +08:00
crc = *cdr_rxFifo; //CRC
2024-10-30 17:51:33 +08:00
//等待接收完成标志
asm volatile (
"1:\n"
2024-11-01 17:07:35 +08:00
"andi ra, sp, 0x08\n"
2024-10-30 17:51:33 +08:00
"beqz ra, 1b\n"
2024-11-01 17:07:35 +08:00
"andi sp, sp, 0xF7\n"
2024-10-30 17:51:33 +08:00
);
2024-10-29 17:32:17 +08:00
totNum = slvNum + (upRecvLen >> 3) - 1;
2024-10-30 17:51:33 +08:00
} else { //islast
//发送上行包头
2024-11-01 17:07:35 +08:00
*cdr_txLen = 16; //upSendLen + 8
*cdr_txFifo = (8 << 20) |( 0 << 16) | 0;
*cdr_txFifo = 0;
2024-10-30 17:51:33 +08:00
for( uint32_t i = 0; i < 8/4; i ++ ){
2024-11-01 17:07:35 +08:00
*cdr_txFifo = 0;
2024-10-30 17:51:33 +08:00
}
2024-10-29 17:32:17 +08:00
}
2024-11-04 16:47:01 +08:00
//等待发送完成标志
asm volatile (
"1:\n"
"andi ra, sp, 0x01\n"
"beqz ra, 1b\n"
"andi sp, sp, 0xFE\n"
);
2024-10-30 17:51:33 +08:00
2024-11-04 16:47:01 +08:00
slvNum = 64 - ( downRecvLen >> 3 );
*WriteGpio = 1 << slvNum;
2024-10-29 17:32:17 +08:00
} else if ( operator == 1 ){
error();
} else if ( operator == 2 ){
error();
} else {
error();
}
}
}