static void HalConsoleDMAConfigration(void) { CLK_PeripheralClockConfig(CLK_Peripheral_DMA1, ENABLE); /* Deinitialize DMA channels */ DMA_GlobalDeInit(); DMA_DeInit(USART_DMA_CHANNEL_TX); DMA_DeInit(USART_DMA_CHANNEL_RX); /* DMA channel Rx of USART Configuration */ DMA_Init(USART_DMA_CHANNEL_RX, (uint16_t)HalConsoleRxBuffer, (uint16_t)USART_DR_ADDRESS, sizeof(HalConsoleRxBuffer), DMA_DIR_PeripheralToMemory, DMA_Mode_Normal, DMA_MemoryIncMode_Inc, DMA_Priority_Low, DMA_MemoryDataSize_Byte); /* Enable the USART Tx/Rx DMA requests */ USART_DMACmd(USART2, USART_DMAReq_TX, ENABLE); USART_DMACmd(USART2, USART_DMAReq_RX, ENABLE); /* Global DMA Enable */ DMA_GlobalCmd(ENABLE); /* Enable the USART Rx DMA channel */ DMA_Cmd(USART_DMA_CHANNEL_RX, ENABLE); USART_Cmd(USART2, ENABLE); }
/** * @brief Configure DMA peripheral * @param None * @retval None */ static void DMA_Config(void) { /* Deinitialize DMA channels */ DMA_GlobalDeInit(); DMA_DeInit(SPI_DMAChannelRx); DMA_SetTimeOut(0x3F); /* DMA channel Rx of SPI Configuration */ DMA_Init(SPI_DMAChannelRx, (uint16_t)SPIBuffer_Rx, (uint16_t)SPI_DR_Address, \ RX_BUFFER_SIZE, DMA_DIR_PeripheralToMemory, DMA_Mode_Normal, \ DMA_MemoryIncMode_Inc, DMA_Priority_High, DMA_MemoryDataSize_Byte); /* Enable the SPI Rx DMA requests */ SPI_DMACmd(SPI1, SPI_DMAReq_RX, ENABLE); /* Enable Global DMA */ DMA_GlobalCmd(ENABLE); /* Enable the SPI RX DMA channel */ DMA_Cmd(SPI_DMAChannelRx, ENABLE); }
/** * @brief Main program. * @param None * @retval None */ void main(void) { uint8_t arrayindex = 0; uint8_t index1 = 0, index2 = 0; ErrorStatus cryptostatus = ERROR; /* Initialize LEDs mounted on STM8L1528-EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED3); /****************************************************************************/ /* Encryption phase */ /****************************************************************************/ /* Prepare the buffer to be transferred by DMA: Alternating key and plain text */ while (arrayindex < PLAINTEXT_SIZE * 2) { SrcBuffer[arrayindex] = EncryptionKey[index1]; arrayindex++; SrcBuffer[arrayindex] = PlainText[index2]; arrayindex++; index1++; index2++; if (index1 == 16) index1 = 0; } /* DMA configuration to transfer data to/from AES --------------------------*/ /* Enable DMA1 clock */ CLK_PeripheralClockConfig(CLK_Peripheral_DMA1, ENABLE); /* DMA DeInit */ DMA_GlobalDeInit(); DMA_DeInit(DMA1_Channel0); DMA_DeInit(DMA1_Channel3); /* DMA1 channel 0 configuration Input phase: data transfer from memory "SrcBuffer" to AES_DINR register */ DMA_Init(DMA1_Channel0, (uint16_t)SrcBuffer, AES_DINR_ADDRESS, PLAINTEXT_SIZE * 2, DMA_DIR_MemoryToPeripheral, DMA_Mode_Normal, DMA_MemoryIncMode_Inc, DMA_Priority_VeryHigh, DMA_MemoryDataSize_Byte); /* DMA1 channel 3 configuration Output phase: data transfer from AES_DOUTR register to memory "CypherText" */ DMA_Init(DMA1_Channel3, (uint16_t)CypherText, AES_DOUTR_ADDRESS, PLAINTEXT_SIZE, DMA_DIR_PeripheralToMemory, DMA_Mode_Normal, DMA_MemoryIncMode_Inc, DMA_Priority_VeryHigh, DMA_MemoryDataSize_Byte); /* DMA1 Channel 0 and channel 3 enable */ DMA_Cmd(DMA1_Channel0, ENABLE); DMA_Cmd(DMA1_Channel3, ENABLE); /* DMA1 global enable */ DMA_GlobalCmd(ENABLE); /* AES configuration to encrypt data using DMA transfer --------------------*/ /* Enable AES clock */ CLK_PeripheralClockConfig(CLK_Peripheral_AES, ENABLE); /* Select the encryption mode */ AES_OperationModeConfig(AES_Operation_Encryp); /* Enable using DMA for data transfer */ AES_DMAConfig(AES_DMATransfer_InOut, ENABLE); /* Enable the AES peripheral: the AES initiates the DMA request */ AES_Cmd(ENABLE); /* Wait for transfer from AES_DOUTR to memory to be completed */ while (DMA_GetFlagStatus(DMA1_FLAG_TC3) == RESET); /****************************************************************************/ /* Decryption phase */ /****************************************************************************/ /* Prepare the buffer to be transferred by DMA: Alternating key and cypher text */ arrayindex = 0; index1 = 0; index2 = 0; while (arrayindex < PLAINTEXT_SIZE * 2) { SrcBuffer[arrayindex] = EncryptionKey[index1]; arrayindex++; SrcBuffer[arrayindex] = CypherText[index2]; arrayindex++; index1++; index2++; if (index1 == 16) index1 = 0; } /* Disable the AES peripheral to change AES operation mode */ AES_Cmd(DISABLE); /* DeInit DMA1 channel 3 */ DMA_DeInit(DMA1_Channel3); /* DMA1 global disable */ DMA_GlobalCmd(DISABLE); /* DMA1 channel 0 is already configured in Encryption phase Input phase: data transfer from memory "SrcBuffer" to AES_DINR register */ /* Reconfigure channel 0 counter to start a new transfer */ DMA_Cmd(DMA1_Channel0, DISABLE); DMA_SetCurrDataCounter(DMA1_Channel0, PLAINTEXT_SIZE * 2); /* DMA1 channel 3 configuration Output phase: data transfer from AES_DOUTR register to memory "ComputedPlainText" */ DMA_Init(DMA1_Channel3, (uint16_t)ComputedPlainText, AES_DOUTR_ADDRESS, PLAINTEXT_SIZE, DMA_DIR_PeripheralToMemory, DMA_Mode_Normal, DMA_MemoryIncMode_Inc, DMA_Priority_VeryHigh, DMA_MemoryDataSize_Byte); /* DMA1 Channel 0 and Channel 3 enable */ DMA_Cmd(DMA1_Channel3, ENABLE); DMA_Cmd(DMA1_Channel0, ENABLE); /* DMA1 global enable */ DMA_GlobalCmd(ENABLE); /********** AES configuration to decrypt data using DMA transfer ***********/ /* Select the key derivation and decryption mode */ AES_OperationModeConfig(AES_Operation_KeyDerivAndDecryp); /* Enable the AES peripheral */ AES_Cmd(ENABLE); /* wait for transfer from AES_DOUTR register to memory to be completed */ while (DMA_GetFlagStatus(DMA1_FLAG_TC3) == RESET); /****************************************************************************/ /* Checking buffers */ /****************************************************************************/ /* Check if decrypted cypher text is equal to original plain text */ cryptostatus = Buffercmp(PlainText, ComputedPlainText, PLAINTEXT_SIZE); if (cryptostatus == SUCCESS) { /* Turn on green led LD1 */ STM_EVAL_LEDOn(LED1); } else { /* Turn on red led LD3 */ STM_EVAL_LEDOn(LED3); } /* Infinite loop */ while (1) {} }