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

166 lines
3.1 KiB
C
Raw Normal View History

#include <stdint.h>
// #include "uart.h"
// #include "gpio.h"
// #include "timer.h"
2024-11-05 17:48:34 +08:00
#include "custom_ops.S"
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;
2024-11-05 11:09:40 +08:00
uint32_t led = 0;
2024-11-05 17:48:34 +08:00
asm volatile (
"li sp, 0\n"
".4byte 0x600600b\n"
);
*cdr_softReset = 1; //复位脉冲
*cdr_direct = 0;
2024-11-05 17:48:34 +08:00
register uint32_t flag;
asm volatile ( //timer 2s
"1:\n"
2024-11-05 17:48:34 +08:00
"rdcycle %0\n"
// "srli %0, %0, 12\n"
"srli %0, %0, 27\n"
2024-11-05 17:48:34 +08:00
// "srli %0, %0, 26\n"
"andi %0, %0, 0x1\n"
"beqz %0, 1b\n"
:"+r"(flag)
);
// if( led ){
// *WriteGpio = 0xFFFFFFFF;
// led = 0;
// } else{
// *WriteGpio = 0x0;
// led = 1;
// }
2024-11-05 11:09:40 +08:00
//发送下行包头
*cdr_txLen = 520; //总长520
*cdr_txFifo = (512 << 20) |( 0 << 16) | 0x5a5a; //负载长度512操作数0 参数全0//测试,防止脱锁
*cdr_txFifo = 0x5a5a5a5a; //测试,防止脱锁
for( uint32_t i = 0; i < 512/4; i ++ ){
*cdr_txFifo = 0xaa5555aa;//测试,防止脱锁
}
//等待发送完成标志
asm volatile (
"1:\n"
2024-11-05 17:48:34 +08:00
"andi %0, sp, 0x01\n"
"beqz %0, 1b\n"
"andi sp, sp, 0xFE\n"
2024-11-05 17:48:34 +08:00
:"+r"(flag)
);
*cdr_softReset = 1; //复位脉冲
*cdr_direct = 1; //方向切换上行
//接收第一个包
asm volatile (
"1:\n"
2024-11-05 17:48:34 +08:00
"andi %0, sp, 0x04\n"
"beqz %0, 1b\n"
"andi sp, sp, 0xFB\n"
2024-11-05 17:48:34 +08:00
:"+r"(flag)
);
readDat = *cdr_rxFifo;
// operator = (readDat >> 16) & 0xf; //速度不够,不校验
upRecvLen = readDat >> 20;
//接收第二个包
// asm volatile (
2024-11-05 17:48:34 +08:00
// "andi %0, sp, 0x08\n"
// "1:\n"
2024-11-05 17:48:34 +08:00
// "bnez %0, 1b\n" //error //速度不够,不校验
// :"+r"(flag),
// );
uint32_t temp = *cdr_rxFifo;
//上行转发负载
for( uint32_t i = 0; i < (upRecvLen >> 2); i ++ ){ //一次传4byte
2024-11-05 17:48:34 +08:00
register uint32_t upData;
asm volatile (
2024-11-05 17:48:34 +08:00
"andi %0, sp, 0x80\n"
"1:\n"
2024-11-05 17:48:34 +08:00
"bnez %0, 1b\n" //error 速度不够,不再检查接收
"lw %1, 0x01c(x0)\n"
:"+r"(flag), "+r"(upData)
);
}
//CRC
asm volatile (
2024-11-05 17:48:34 +08:00
"andi %0, sp, 0x08\n"
"1:\n"
2024-11-05 17:48:34 +08:00
"bnez %0, 1b\n" //error
:"+r"(flag)
);
uint32_t crc = *cdr_rxFifo; //CRC
//等待接收完成标志
asm volatile (
"1:\n"
2024-11-05 17:48:34 +08:00
"andi %0, sp, 0x08\n"
"beqz %0, 1b\n"
"andi sp, sp, 0xF7\n"
2024-11-05 17:48:34 +08:00
:"+r"(flag)
);
totNum = (upRecvLen >> 3);
}