[Atmel] SAML21走起16:加密解密DMA模式

[复制链接]
1631|0
 楼主| ddllxxrr 发表于 2015-8-19 21:50 | 显示全部楼层 |阅读模式
DMA模式,就是不用CPU参与,直接就加密解密。

同以前一样,按照快速指导建立程序。

只不过这次有点意外,编译时总是通不过去。

后来仔细地读了下文档,原来少给了几行定义:就是下图阴影部分。加上后编译通过了。


程序如下:
  1. #include <asf.h>
  2. #define AES_EXAMPLE_REFBUF_SIZE 4
  3. uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = {
  4.         0xe2bec16b,
  5.         0x969f402e,
  6.         0x117e3de9,
  7.         0x2a179373
  8. };
  9. uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = {
  10.         0xb47bd73a,
  11.         0x60367a0d,
  12.         0xf3ca9ea8,
  13.         0x97ef6624
  14. };
  15. const uint32_t key128[4] = {
  16.         0x16157e2b,
  17.         0xa6d2ae28,
  18.         0x8815f7ab,
  19.         0x3c4fcf09
  20. };


  21. /* Output data array */
  22. static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE];
  23. /* State indicate */
  24. volatile bool state = false;
  25. struct aes_config g_aes_cfg;
  26. struct aes_module aes_instance;
  27. struct usart_module usart_instance;

  28. struct dma_resource example_resource_tx;
  29. struct dma_resource example_resource_rx;
  30. COMPILER_ALIGNED(16)
  31. DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTOR;
  32. DmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR;

  33. static void configure_usart(void)
  34. {
  35.         struct usart_config config_usart;
  36.         usart_get_config_defaults(&config_usart);
  37.         config_usart.baudrate    = 38400;
  38.         config_usart.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING;
  39.         config_usart.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0;
  40.         config_usart.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;
  41.         config_usart.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;
  42.         config_usart.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;
  43.         stdio_serial_init(&usart_instance, EDBG_CDC_MODULE, &config_usart);
  44.         usart_enable(&usart_instance);
  45. }
  46. static void ecb_mode_test_dma(void)
  47. {
  48.         printf("\r\n-----------------------------------\r\n");
  49.         printf("- 128bit cryptographic key\r\n");
  50.         printf("- ECB cipher mode\r\n");
  51.         printf("- DMA mode\r\n");
  52.         printf("- 4 32bit words with DMA\r\n");
  53.         printf("-----------------------------------\r\n");
  54.         state = false;
  55.         /* Configure the AES. */
  56.         g_aes_cfg.encrypt_mode = AES_ENCRYPTION;
  57.         g_aes_cfg.key_size = AES_KEY_SIZE_128;
  58.         g_aes_cfg.start_mode = AES_AUTO_START;
  59.         g_aes_cfg.opmode = AES_ECB_MODE;
  60.         g_aes_cfg.cfb_size = AES_CFB_SIZE_128;
  61.         g_aes_cfg.lod = false;
  62.         aes_set_config(&aes_instance,AES, &g_aes_cfg);
  63.         /* Set the cryptographic key. */
  64.         aes_write_key(&aes_instance, key128);
  65.         /* The initialization vector is not used by the ECB cipher mode. */
  66.         dma_start_transfer_job(&example_resource_tx);
  67.         aes_set_new_message(&aes_instance);
  68.         aes_clear_new_message(&aes_instance);
  69.         /* Wait DMA transfer */
  70.         while (false == state) {
  71.         }
  72.         /* Wait for the end of the encryption process. */
  73.         while (!(aes_get_status(&aes_instance) & AES_ENCRYPTION_COMPLETE)) {
  74.         }
  75.         state = false;
  76.         dma_start_transfer_job(&example_resource_rx);
  77.         /* Wait DMA transfer */
  78.         while (false == state) {
  79.         }
  80.         if ((ref_cipher_text_ecb[0] != output_data[0]) ||
  81.         (ref_cipher_text_ecb[1] != output_data[1]) ||
  82.         (ref_cipher_text_ecb[2] != output_data[2]) ||
  83.         (ref_cipher_text_ecb[3] != output_data[3])) {
  84.                 printf("\r\nKO!!!\r\n");
  85.                 } else {
  86.                 printf("\r\nOK!!!\r\n");
  87.         }
  88. }
  89. static void transfer_tx_rx_done(struct dma_resource* const resource )
  90. {
  91.         state = true;
  92. }
  93. static void configure_dma_aes_wr(void)
  94. {
  95.         struct dma_resource_config tx_config;
  96.         dma_get_config_defaults(&tx_config);
  97.         tx_config.peripheral_trigger = AES_DMAC_ID_WR;
  98.         tx_config.trigger_action = DMA_TRIGGER_ACTON_BLOCK;
  99.         dma_allocate(&example_resource_tx, &tx_config);
  100.         struct dma_descriptor_config tx_descriptor_config;
  101.         dma_descriptor_get_config_defaults(&tx_descriptor_config);
  102.         tx_descriptor_config.beat_size = DMA_BEAT_SIZE_WORD;
  103.         tx_descriptor_config.dst_increment_enable = false;
  104.         tx_descriptor_config.block_transfer_count = AES_EXAMPLE_REFBUF_SIZE;
  105.         tx_descriptor_config.source_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text);
  106.         tx_descriptor_config.destination_address =(uint32_t) &(AES->INDATA);
  107.         dma_descriptor_create(&example_descriptor_tx, &tx_descriptor_config);
  108.         dma_add_descriptor(&example_resource_tx, &example_descriptor_tx);
  109. }
  110. static void configure_dma_aes_rd(void)
  111. {
  112.         struct dma_resource_config rx_config;
  113.         dma_get_config_defaults(&rx_config);
  114.         rx_config.peripheral_trigger = AES_DMAC_ID_RD;
  115.         rx_config.trigger_action = DMA_TRIGGER_ACTON_BLOCK;
  116.         dma_allocate(&example_resource_rx, &rx_config);
  117.         struct dma_descriptor_config rx_descriptor_config;
  118.         dma_descriptor_get_config_defaults(&rx_descriptor_config);
  119.         rx_descriptor_config.beat_size = DMA_BEAT_SIZE_WORD;
  120.         rx_descriptor_config.src_increment_enable = false;
  121.         rx_descriptor_config.block_transfer_count = AES_EXAMPLE_REFBUF_SIZE;
  122.         rx_descriptor_config.source_address = (uint32_t)&(AES->INDATA);
  123.         rx_descriptor_config.destination_address =
  124.         (uint32_t)output_data + sizeof(output_data);
  125.         dma_descriptor_create(&example_descriptor_rx, &rx_descriptor_config);
  126.         dma_add_descriptor(&example_resource_rx, &example_descriptor_rx);
  127. }
  128. int main (void)
  129. {
  130.        
  131.           
  132.           /* Initialize the system and console*/
  133.           system_init();
  134.           configure_usart();
  135.           /* Configure AES DMA and enable callback */
  136.           configure_dma_aes_wr();
  137.           configure_dma_aes_rd();
  138.           dma_register_callback(&example_resource_tx, transfer_tx_rx_done,
  139.           DMA_CALLBACK_TRANSFER_DONE);
  140.           dma_enable_callback(&example_resource_tx, DMA_CALLBACK_TRANSFER_DONE);
  141.           dma_register_callback(&example_resource_rx, transfer_tx_rx_done,
  142.           DMA_CALLBACK_TRANSFER_DONE);
  143.           dma_enable_callback(&example_resource_rx, DMA_CALLBACK_TRANSFER_DONE);
  144.           aes_get_config_defaults(&g_aes_cfg);
  145.           aes_init(&aes_instance,AES, &g_aes_cfg);
  146.           aes_enable(&aes_instance);
  147.           
  148.       ecb_mode_test_dma();
  149.        
  150. }


运行结果如下:





本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2404

主题

7008

帖子

68

粉丝
快速回复 在线客服 返回列表 返回顶部