本帖最后由 bitterheart 于 2022-11-23 10:21 编辑
DMA配置
RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_DMA, ENABLE);
DMA_DeInit(DMA_CH7);
DMA_StructInit(&DMA_InitStructure);
DMA_InitStructure.PeriphAddr = (uint32_t)(USART2_BASE + 0x04);
// DMA_InitStructure.MemAddr = (uint32_t)TxBuffer1;
DMA_InitStructure.Direction = DMA_DIR_PERIPH_DST;
// DMA_InitStructure.BufSize = TxBufferSize1;
DMA_InitStructure.PeriphInc = DMA_PERIPH_INC_DISABLE;
DMA_InitStructure.DMA_MemoryInc = DMA_MEM_INC_ENABLE;
DMA_InitStructure.PeriphDataSize = DMA_PERIPH_DATA_SIZE_BYTE;
DMA_InitStructure.MemDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.CircularMode = DMA_MODE_NORMAL;
DMA_InitStructure.Priority = DMA_PRIORITY_HIGH;
DMA_InitStructure.Mem2Mem = DMA_M2M_DISABLE;
DMA_Init(DMA_CH7, &DMA_InitStructure);
DMA_RequestRemap(DMA_REMAP_USART2_TX, DMA, DMA_CH7, ENABLE);
void UART_DMA_Send(uint8 *pbuf, uint16 len)
{
int len1;
if (len == 0) {
return ;
}
pbuf[len-1] = '\r';
pbuf[len++] = '\n';
if (buf_sel == 0) {
p_wr = print_buf_dma;
} else {
p_wr = print_buf_dma_bk;
}
len1 = print_buf_wrcnt + len;
if (len1 > DMA_PRINTBUF_LEN) {
memcpy(&p_wr[print_buf_wrcnt], pbuf, DMA_PRINTBUF_LEN-print_buf_wrcnt);
len1 = DMA_PRINTBUF_LEN;
dma_print_err_cnt++;
} else {
memcpy(&p_wr[print_buf_wrcnt], pbuf, len);
}
print_buf_wrcnt = len1;
dma_wait_cnt = 0;
if (print_dma_wait) {
while (DMA_GetFlagStatus(DMA_FLAG_TC7, DMA) == RESET) {
dma_wait_cnt++;
if (dma_wait_cnt >= DMA_WAIT_TIMEOUT) {
return;
}
}
}
DMA_ClearFlag(DMA_FLAG_TC7, DMA);
print_dma_wait = 1;
buf_sel = (buf_sel + 1) & 1;
print_buf_wrcnt = 0;
DMA_InitStructure.MemAddr = (uint32_t)p_wr;
DMA_InitStructure.BufSize = len1;
DMA_Init(DMA_CH7, &DMA_InitStructure);
DMA_RequestRemap(DMA_REMAP_USART2_TX, DMA, DMA_CH7, ENABLE);
USART_EnableDMA(USART2, USART_DMAREQ_TX, ENABLE);
DMA_EnableChannel(DMA_CH7, ENABLE);
}
void print_dma_flush(void)
{
dma_wait_cnt = 0;
if (print_buf_wrcnt > 0) {
while (DMA_GetFlagStatus(DMA_FLAG_TC7, DMA) == RESET) {
dma_wait_cnt++;
if (dma_wait_cnt >= 100000) { // DMA_WAIT_TIMEOUT) {
// dma_print_err_cnt++;
// break;
}
}
DMA_ClearFlag(DMA_FLAG_TC7, DMA);
print_dma_wait = 1;
DMA_InitStructure.MemAddr = (uint32_t)p_wr;
DMA_InitStructure.BufSize = print_buf_wrcnt;
DMA_Init(DMA_CH7, &DMA_InitStructure);
DMA_RequestRemap(DMA_REMAP_USART2_TX, DMA, DMA_CH7, ENABLE);
USART_EnableDMA(USART2, USART_DMAREQ_TX, ENABLE);
DMA_EnableChannel(DMA_CH7, ENABLE);
// dma_print_err_cnt++;
}
// if (print_dma_wait) {
while (DMA_GetFlagStatus(DMA_FLAG_TC7, DMA) == RESET) {
dma_wait_cnt++;
if (dma_wait_cnt >= 100000) { // DMA_WAIT_TIMEOUT) {
// dma_print_err_cnt++;
// break;
}
}
DMA_ClearFlag(DMA_FLAG_TC7, DMA);
// }
print_dma_wait = 0;
print_buf_wrcnt = 0;
buf_sel = 0;
}
|