📄 dbgu.c
字号:
//---------------------------------------------------------------------------------------------------------------------
//【 版 本 】v1.0
//【 函 数 名 称 】void dbgu_send_chars(char *chars,uint16 len)
//【 创建人及创建时间 】gliethttp 2007-04-05
//【 修改人及修改时间 】
//【 修 改 原 因 】0x200000
//【 功 能 描 述 】
//---------------------------------------------------------------------------------------------------------------------
void dbgu_send_chars(char *chars,uint16 len)
{
//while(AT91F_US_SendFrame((AT91PS_USART)AT91C_BASE_DBGU,chars,len,chars,len) == 0);
while(AT91C_BASE_PDC_DBGU->PDC_TCR);
//while( (((AT91PS_USART)AT91C_BASE_DBGU)->US_CSR & AT91C_US_TXRDY) == false );
AT91C_BASE_PDC_DBGU->PDC_TPR = (uint32)chars;
AT91C_BASE_PDC_DBGU->PDC_TCR = len;
}
//---------------------------------------------------------------------------------------------------------------------
//【 版 本 】v1.0
//【 函 数 名 称 】void dbgu_send_uint16(uint16 send)
//【 创建人及创建时间 】gliethttp 2007-04-05
//【 修改人及修改时间 】
//【 修 改 原 因 】
//【 功 能 描 述 】
//---------------------------------------------------------------------------------------------------------------------
void dbgu_send_uint16(uint16 send)
{uint8 data;
uint8 tmp[20];
uint8 *to;
uint8 i,j;
uint16 tmp_send;
tmp_send = send;
for(j = 0;tmp_send;j++)
{
tmp_send /= 10;
}
if(j >= 20)return;
if(j == 0)j = 1;
to = &tmp[j-1];
for(i = 0;i < j;i++)
{
data = send % 10;
data += '0';
*to-- = data;
send /= 10;
}
while(AT91C_BASE_PDC_DBGU->PDC_TCR);
for(i = 0;i < j;i++)
{
dbgu_send_char(tmp[i]);
}
}
//---------------------------------------------------------------------------------------------------------------------
//【 版 本 】v1.0
//【 函 数 名 称 】void dbgu_send_uint32(uint32 send)
//【 创建人及创建时间 】gliethttp 2007-04-05
//【 修改人及修改时间 】
//【 修 改 原 因 】
//【 功 能 描 述 】
//---------------------------------------------------------------------------------------------------------------------
void dbgu_send_uint32(uint32 send)
{uint8 data;
uint8 tmp[20];
uint8 *to;
uint8 i,j;
uint32 tmp_send;
tmp_send = send;
for(j = 0;tmp_send;j++)
{
tmp_send /= 10;
}
if(j >= 20)return;
if(j == 0)j = 1;
to = &tmp[j-1];
for(i = 0;i < j;i++)
{
data = send % 10;
data += '0';
*to-- = data;
send /= 10;
}
while(AT91C_BASE_PDC_DBGU->PDC_TCR);
for(i = 0;i < j;i++)
{
dbgu_send_char(tmp[i]);
}
}
//---------------------------------------------------------------------------------------------------------------------
//【 版 本 】v1.0
//【 函 数 名 称 】uint8 dbgu_rcve_char(void)
//【 创建人及创建时间 】gliethttp 2007-04-04
//【 修改人及修改时间 】
//【 修 改 原 因 】
//【 功 能 描 述 】
//---------------------------------------------------------------------------------------------------------------------
uint8 dbgu_rcve_char(void)
{
return 0;
}
//---------------------------------------------------------------------------------------------------------------------
//【 版 本 】v1.0
//【 函 数 名 称 】void *printf2dbgu_malloc(uint32 len)
//【 创建人及创建时间 】gliethttp 2007-04-04
//【 修改人及修改时间 】
//【 修 改 原 因 】
//【 功 能 描 述 】因为在中断中引用该函数,所以保证了原子性
// 采用先入、先出的环形队列原则
//---------------------------------------------------------------------------------------------------------------------
void *printf2dbgu_malloc(uint32 len)
{void *p;
boolean poll_forward;
void *result;
if(dbgu_control.mm_buf_counts == 0)
{//回0
dbgu_control.mm_lastp = (void*)&dbgu_control.dbgu_cache_buffer[0];
dbgu_control.mm_cur_index = 0;
dbgu_control.mm_in = 0;
dbgu_control.mm_out = 0;
dbgu_send_string("\r\n");
}
if(len > dbgu_cache_buffer_max)
{
result = 0;
goto printf2dbgu_malloc_exit;
}
p = (uint8*)dbgu_control.mm_lastp + len;
if(p < dbgu_control.mm_maxp)
{
if(dbgu_control.mm_cur_index < dbgu_control_vmalloc_max)
{
{uint32 mm_in;
mm_in = dbgu_control.mm_in + 1;
mm_in %= dbgu_control_vmalloc_max;
if(mm_in != dbgu_control.mm_out)
{//先要保证FIFO有空间
dbgu_control.mm_in_out[dbgu_control.mm_in] = dbgu_control.mm_cur_index;
dbgu_control.mm_in = mm_in;
dbgu_control.mm[dbgu_control.mm_cur_index].status = vmalloc_status_used;//|= vmalloc_status_used;
dbgu_control.mm[dbgu_control.mm_cur_index].p = dbgu_control.mm_lastp;
dbgu_control.mm[dbgu_control.mm_cur_index].len = len;
dbgu_control.mm_cur_index++;//mm_cur_index
dbgu_control.mm_buf_counts++;
result = dbgu_control.mm_lastp;
dbgu_control.mm_lastp = (uint8*)dbgu_control.mm_lastp + len;
//poll_forward = false;
goto printf2dbgu_malloc_exit;
}else
{
result = 0;
goto printf2dbgu_malloc_exit;
}
}
}else poll_forward = true;
}else poll_forward = true;
if(poll_forward)
{uint32 i;
for(i = 0;i < dbgu_control_vmalloc_max;i++)
{
if(dbgu_control.mm[i].status == 0)
{
if(len <= dbgu_control.mm[i].len)
{
goto poll_forward_ok;
}else
{//可能出现内存漏洞,不过最后能够自动恢复正常
uint32 j;
uint32 tmp_len;
tmp_len = len - dbgu_control.mm[i].len;
//查看以后单元是否可用,一般时候可用
for(j = i + 1;j < dbgu_control_vmalloc_max;j++)
{
if(dbgu_control.mm[j].status == 0)
{
dbgu_control.mm[j].status = vmalloc_status_tmpused;
if(tmp_len <= dbgu_control.mm[j].len)
{
goto poll_forward_ok;
}else tmp_len -= dbgu_control.mm[j].len;
}else
{//根据算法的特定性,不存在连续的,失败
for(j = i + 1;j < dbgu_control_vmalloc_max;j++)
{
if(dbgu_control.mm[j].status & vmalloc_status_tmpused)dbgu_control.mm[j].status &= ~vmalloc_status_tmpused;
}
result = 0;
goto printf2dbgu_malloc_exit;
}
}
}
}
}
poll_forward_ok:
{
if(i < dbgu_control_vmalloc_max)
{//i将占用后边的若干个 标志为 vmalloc_status_tmpused的单元,注意释放
{uint32 mm_in;
mm_in = dbgu_control.mm_in + 1;
mm_in %= dbgu_control_vmalloc_max;
if(mm_in != dbgu_control.mm_out)
{
dbgu_control.mm_in_out[dbgu_control.mm_in] = i;
dbgu_control.mm_in = mm_in;
dbgu_control.mm[i].status |= vmalloc_status_used;
dbgu_control.mm_buf_counts++;
result = dbgu_control.mm[i].p;
}
}
}else result = 0;
}
}
printf2dbgu_malloc_exit:
return result;
}
//---------------------------------------------------------------------------------------------------------------------
//【 版 本 】v1.0
//【 函 数 名 称 】uint32 printf2dbgu(void *data,uint32 len)
//【 创建人及创建时间 】gliethttp 2007-04-04
//【 修改人及修改时间 】
//【 修 改 原 因 】
//【 功 能 描 述 】
//---------------------------------------------------------------------------------------------------------------------
uint32 printf2dbgu(void *data,uint32 len)
{uint8 *dst;
dst = printf2dbgu_malloc(len);
if(dst)
{
xmemcopy(data,dst,len);
return true;
}
return false;
}
//---------------------------------------------------------------------------------------------------------------------
//【 版 本 】v1.0
//【 函 数 名 称 】uint32 printffdbgu_get_data(uint32 *mm_out)
//【 创建人及创建时间 】gliethttp 2007-04-05
//【 修改人及修改时间 】
//【 修 改 原 因 】
//【 功 能 描 述 】获取队列数据
//---------------------------------------------------------------------------------------------------------------------
uint32 printffdbgu_get_data(uint32 *mm_out)
{
if(dbgu_control.mm_in != dbgu_control.mm_out)
{
*mm_out = dbgu_control.mm_out;
return true;
}
return false;
}
//---------------------------------------------------------------------------------------------------------------------
//【 版 本 】v1.0
//【 函 数 名 称 】void printffdbgu_relase_data(uint32 *mm_out)
//【 创建人及创建时间 】gliethttp 2007-04-05
//【 修改人及修改时间 】
//【 修 改 原 因 】
//【 功 能 描 述 】释放
//---------------------------------------------------------------------------------------------------------------------
void printffdbgu_relase_data(uint32 *mm_out)
{uint32 mm_index;
mm_index = *mm_out;
dbgu_control.mm[mm_index].status = vmalloc_status_unused;
if(dbgu_control.mm_buf_counts)dbgu_control.mm_buf_counts--;
{uint32 j;
for(j = mm_index + 1;j < dbgu_control_vmalloc_max;j++)
{
if(dbgu_control.mm[j].status & vmalloc_status_tmpused)dbgu_control.mm[j].status &= ~vmalloc_status_tmpused;
}
}
dbgu_control.mm_out++;
dbgu_control.mm_out %= dbgu_control_vmalloc_max;
}
//---------------------------------------------------------------------------------------------------------------------
//【 版 本 】v1.0
//【 函 数 名 称 】void printffdbgu(void)
//【 创建人及创建时间 】gliethttp 2007-04-05
//【 修改人及修改时间 】
//【 修 改 原 因 】
//【 功 能 描 述 】检查是否有数据
//---------------------------------------------------------------------------------------------------------------------
void printffdbgu(void)
{static uint32 mm_out;
static boolean mm_out_enable = true;
if(dbgu_frame_over)
{
dbgu_frame_over = false;
mm_out_enable = true;
printffdbgu_relase_data(&mm_out);
}
if(mm_out_enable)
{
if(printffdbgu_get_data(&mm_out))
{
dbgu_send_chars(dbgu_control.mm[mm_out].p,dbgu_control.mm[mm_out].len);
AT91F_US_EnableIt((AT91PS_USART) AT91C_BASE_DBGU,AT91C_US_TXBUFE);
mm_out_enable = false;
}
#if 0
{uint32 i;for(i = 0;i < 0x5fffff;i++);
dbgu_send_string("dbgu_control.mm_in=");
dbgu_send_uint32(dbgu_control.mm_in);
dbgu_send_string("\r\n");
dbgu_send_string("dbgu_control.mm_out=");
dbgu_send_uint32(dbgu_control.mm_out);
dbgu_send_string("\r\n");
}
#endif
}
//while(AT91F_US_SendFrame((AT91PS_USART)AT91C_BASE_DBGU,data,len,data,len) == 0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -