本帖最后由 muyichuan2012 于 2024-11-5 14:46 编辑
AT32 MCU上使用DMA传输数据加速进行CRC计算
示例目的
演示在AT32 MCU上使用DMA传输数据加速进行CRC计算,支持CRC8,CRC16,CRC32。
支持型号列表:
支持型号 AT32 全系列
主要使用外设
CRC
DMA
USART
GPIO
快速使用方法
硬件资源
1) USART1(PA9/PA10)
2) AT-START实验板,如下是AT-START-F403A开发板。
软件资源
1) SourceCode
SC0150_SourceCode
2) Doc
SC0150_AT32_CRC_computing_by_using_DMA 使用方法。
Note: 所有project 都是基于keil 5 而建立,若用户需要在其他编译环境上使用,请参考
AT32F407_Firmware_Library_V2.x.x/project/at_start_f407/templates 中各种编译环境(例如
IAR6/7,keil 4/5)进行简单修改即可。
示例使用
1) 打开SC0150_SourceCode_V2.0.0\utilities\crc_dma\mdk_v5源程序。
2) 编译并下载到目标开发板,接上串口(PA9/PA10)。
3) 计算结果会从串口打印输出。
相关代码
#include "at32f403a_407_board.h"
#include "at32f403a_407_clock.h"
/** @addtogroup UTILITIES_examples
* @{
*/
/** @addtogroup CRC_dma_demo
* @{
*/
confirm_state dma_done_flag = FALSE;
uint8_t data_buffer[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB};
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] calculate 8-bit data CRC.
* @param pbuffer: pointer to the buffer containing the data to be computed.
* @param length: length of the buffer to be computed.
* @retval crc
*/
uint32_t crc8_block_calculate(uint8_t *pbuffer, uint32_t length)
{
uint32_t index = 0;
for(index = 0; index < length; index++)
{
(*(uint8_t *)&CRC->dt) = pbuffer[index];
}
return (CRC->dt);
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] calculate 16-bit data CRC.
* @param pbuffer: pointer to the buffer containing the data to be computed.
* @param length: length of the buffer to be computed.
* @retval crc
*/
uint32_t crc16_block_calculate(uint16_t *pbuffer, uint32_t length)
{
uint32_t index = 0;
for(index = 0; index < length; index++)
{
(*(uint16_t *)&CRC->dt) = pbuffer[index];
}
return (CRC->dt);
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] config nvic.
* @param none
* @retval none
*/
void nvic_config(void)
{
nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
nvic_irq_enable(DMA1_Channel1_IRQn, 1, 0);
}
/**
* @brief config crc.
* @param none
* @retval none
*/
void crc_config(void)
{
crm_periph_clock_enable(CRM_CRC_PERIPH_CLOCK, TRUE);
crc_init_data_set(0xFFFFFFFF);
crc_poly_value_set(0x04C11DB7);
crc_reverse_output_data_set(CRC_REVERSE_OUTPUT_DATA);
}
/**
* @brief config dma, using DMA1_CHANNEL1.
* @param none
* @retval none
*/
void dma_config(void)
{
dma_init_type dma_init_struct = {0};
crm_periph_clock_enable(CRM_DMA1_PERIPH_CLOCK, TRUE);
dma_reset(DMA1_CHANNEL1);
dma_default_para_init(&dma_init_struct);
dma_init_struct.direction = DMA_DIR_MEMORY_TO_MEMORY;
dma_init_struct.memory_inc_enable = FALSE;
dma_init_struct.peripheral_inc_enable = TRUE;
dma_init_struct.priority = DMA_PRIORITY_MEDIUM;
dma_init_struct.loop_mode_enable = FALSE;
dma_init(DMA1_CHANNEL1, &dma_init_struct);
dma_interrupt_enable(DMA1_CHANNEL1, DMA_FDT_INT, TRUE);
dma_channel_enable(DMA1_CHANNEL1, FALSE);
}
/**
* @brief calculate CRC with dma mode.
* @param pbuff: pointer of data buffer.
* @param length: data buffer length
* @retval none
*/
uint32_t dma_calculate(uint8_t *pbuff, uint32_t length)
{
crc_data_reset();
crc_reverse_input_data_set(CRC_REVERSE_INPUT_BY_WORD);
DMA1_CHANNEL1->dtcnt = length;
DMA1_CHANNEL1->maddr = (uint32_t)&(CRC->dt);
DMA1_CHANNEL1->ctrl_bit.mwidth = DMA_MEMORY_DATA_WIDTH_BYTE;
DMA1_CHANNEL1->paddr = (uint32_t)pbuff;
DMA1_CHANNEL1->ctrl_bit.pwidth = DMA_PERIPHERAL_DATA_WIDTH_BYTE;
dma_done_flag = FALSE;
dma_channel_enable(DMA1_CHANNEL1, TRUE);
/* waiting dma complete */
while(dma_done_flag == FALSE);
return crc_data_get();
}
/**
* @brief calculate CRC with polling mode.
* @param pbuff: pointer of data buffer.
* @param length: data buffer length
* @retval none
*/
uint32_t poll_calculate(uint8_t *pbuff, uint32_t length)
{
crc_data_reset();
/* word */
if(length > 3)
{
crc_reverse_input_data_set(CRC_REVERSE_INPUT_BY_WORD);
crc_block_calculate((uint32_t *)pbuff, length / 4);
pbuff = pbuff + ((length / 4) * 4);
length = length % 4;
}
/* halfword */
if(length > 2)
{
crc_reverse_input_data_set(CRC_REVERSE_INPUT_BY_HALFWORD);
crc16_block_calculate((uint16_t *)pbuff, 1);
pbuff = pbuff + 2;
length = length - 2;
}
/* byte */
if(length > 0)
{
crc_reverse_input_data_set(CRC_REVERSE_INPUT_BY_BYTE);
crc8_block_calculate((uint8_t *)pbuff, 1);
length = 0;
}
return crc_data_get();
}
/**
* @brief main function.
* @param none
* @retval none
*/
int main(void)
{
uint32_t crc_result = 0;
system_clock_config();
at32_board_init();
uart_print_init(115200);
nvic_config();
crc_config();
dma_config();
/* calculate CRC-32 using dma and polling mode */
crc_poly_size_set(CRC_POLY_SIZE_32B);
crc_result = dma_calculate(data_buffer, sizeof(data_buffer));
printf("crc32 dma result: 0x%x\n", crc_result);
crc_result = poll_calculate(data_buffer, sizeof(data_buffer));
printf("crc32 poll result: 0x%x\n", crc_result);
/* calculate CRC-16 using dma and polling mode */
crc_poly_size_set(CRC_POLY_SIZE_16B);
crc_result = dma_calculate(data_buffer, sizeof(data_buffer));
printf("crc16 dma result: 0x%x\n", crc_result);
crc_result = poll_calculate(data_buffer, sizeof(data_buffer));
printf("crc16 poll result: 0x%x\n", crc_result);
/* calculate CRC-8 using dma and polling mode */
crc_poly_size_set(CRC_POLY_SIZE_8B);
crc_result = dma_calculate(data_buffer, sizeof(data_buffer));
printf("crc8 dma result: 0x%x\n", crc_result);
crc_result = poll_calculate(data_buffer, sizeof(data_buffer));
printf("crc8 poll result: 0x%x\n", crc_result);
while(1)
{
}
}
|