Example #1
0
void ADC_Config(void)
{
	CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE);
	CLK_PeripheralClockConfig(CLK_Peripheral_DMA1, ENABLE);
	
	ADC_Init(ADC1, ADC_ConversionMode_Continuous, ADC_Resolution_12Bit, ADC_Prescaler_2);
	ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_384Cycles);
	ADC_Cmd(ADC1, ENABLE);
	
	ADC_ChannelCmd(ADC1, ADC_Channel_24, ENABLE); //设置ADC通道
	
	SYSCFG_REMAPDMAChannelConfig(REMAP_DMA1Channel_ADC1ToChannel0);
	
	DMA_Init(DMA1_Channel0, 
		BUFFER_ADDRESS,
		ADC1_DR_ADDRESS,
		BUFFER_SIZE,
		DMA_DIR_PeripheralToMemory,
		DMA_Mode_Circular,
		DMA_MemoryIncMode_Inc,
		DMA_Priority_High,
		DMA_MemoryDataSize_HalfWord);
	
	DMA_Cmd(DMA1_Channel0, ENABLE);
	DMA_ITConfig(DMA1_Channel0, DMA_ITx_TC, ENABLE);
	DMA_GlobalCmd(ENABLE);
}        
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);
}
Example #3
0
/**
  * @brief  Configure DMA peripheral
  * @param  None
  * @retval None
  */
static void DMA_Config(void)
{
  /* Connect ADC1 to DMA1 channel 0 */
  SYSCFG_REMAPDMAChannelConfig(REMAP_DMA1Channel_ADC1ToChannel0);
  DMA_Init(DMA1_Channel0, BUFFER_ADDRESS, ADC1_DR_ADDRESS, BUFFER_SIZE, DMA_DIR_PeripheralToMemory,
           DMA_Mode_Circular, DMA_MemoryIncMode_Inc, DMA_Priority_High, DMA_MemoryDataSize_HalfWord);

  /* Connect DMA1 channel 2 to TIM1 */
  DMA_Init(DMA1_Channel2, BUFFER_ADDRESS, TIM1_CCR1_ADDRESS, BUFFER_SIZE, DMA_DIR_MemoryToPeripheral,
           DMA_Mode_Circular, DMA_MemoryIncMode_Inc, DMA_Priority_High, DMA_MemoryDataSize_HalfWord);

  /* DMA1 Channel0 enable */
  DMA_Cmd(DMA1_Channel0, ENABLE);

  /* DMA1 Channel2 enable */
  DMA_Cmd(DMA1_Channel2, ENABLE);

  /* DMA1 enable */
  DMA_GlobalCmd(ENABLE);
}
Example #4
0
/**
  * @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);
}
Example #5
0
/**
  * @brief  Configure DMA peripheral 
  * @param  None
  * @retval None
  */
static void DMA_Config(void)
{
  /* Connect ADC to DMA channel 0 */
  SYSCFG_REMAPDMAChannelConfig(REMAP_DMA1Channel_ADC1ToChannel0);

  DMA_Init(DMA1_Channel0, BUFFER_ADDRESS,
           ADC1_DR_ADDRESS,
           BUFFER_SIZE,
           DMA_DIR_PeripheralToMemory,
           DMA_Mode_Circular,
           DMA_MemoryIncMode_Inc,
           DMA_Priority_High,
           DMA_MemoryDataSize_HalfWord);

  /* DMA Channel0 enable */
  DMA_Cmd(DMA1_Channel0, ENABLE);

  /* Enable DMA1 channel0 Transfer complete interrupt */
  DMA_ITConfig(DMA1_Channel0, DMA_ITx_TC, ENABLE);

  /* DMA enable */
  DMA_GlobalCmd(ENABLE);
}
/**
  * @brief  Writes more than one byte to the EEPROM with a single WRITE cycle.
  * @note   The number of byte can't exceed the EEPROM page size.
  * @param  pBuffer : pointer to the buffer containing the data to be written to
  *         the EEPROM.
  * @param  WriteAddr : EEPROM's internal address to write to.
  * @param  NumByteToWrite : pointer to the variable holding number of bytes to
  *         written to the EEPROM.
  *
  *        @note The variable pointed by NumByteToWrite is reset to 0 when all the
  *              data are read from the EEPROM. Application should monitor this
  *              variable in order know when the transfer is complete.
  *
  * @note When number of data to be written is higher than 1, this function just
  *       configure the communication and enable the DMA channel to transfer data.
  *       Meanwhile, the user application may perform other tasks.
  *       When number of data to be written is 1, then the DMA is not used.
  *
  * @retval None
  */
void sEE_WritePage(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t* NumByteToWrite)
{
    __IO uint32_t timeout = 0xFFFF;

    /*!< Wait the end of last communication */
    for (; timeout > 0; timeout--);

    /* Set the pointer to the Number of data to be written. This pointer will be used
        by the DMA Transfer Completer interrupt Handler in order to reset the
        variable to 0. User should check on this variable in order to know if the
        DMA transfer has been complete or not. */
    sEEDataWritePointer = NumByteToWrite;

    /*!< While the bus is busy */
    while (I2C_GetFlagStatus(sEE_I2C, I2C_FLAG_BUSY))
    {}

    /*!< Send START condition */
    I2C_GenerateSTART(sEE_I2C, ENABLE);

    /*!< Test on EV5 and clear it */
    while (!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_MODE_SELECT))
    {}

    /*!< Send EEPROM address for write */
    I2C_Send7bitAddress(sEE_I2C, (uint8_t)sEEAddress, I2C_Direction_Transmitter);

    /*!< Test on EV6 and clear it */
    while (!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
    {}

#ifdef sEE_M24C64_32

    /*!< Send the EEPROM's internal address to write to : MSB of the address first */
    I2C_SendData(sEE_I2C, (uint8_t)((WriteAddr & 0xFF00) >> 8));

    /*!< Test on EV8 and clear it */
    while (!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
    {}

    /*!< Send the EEPROM's internal address to write to : LSB of the address */
    I2C_SendData(sEE_I2C, (uint8_t)(WriteAddr & 0x00FF));

#endif /*!< sEE_M24C64_32 */

    /*!< Test on EV8 and clear it */
    while (! I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
    {}

    /* If number of data to be written is 1, then DMA couldn't be used */
    if ((uint16_t)(*NumByteToWrite) < 2)
    {
        /*!< Send the byte to be written */
        I2C_SendData(sEE_I2C, *pBuffer);

        /*!< Test on EV8 and clear it */
        while (!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
        {}

        /*!< Send STOP condition */
        I2C_GenerateSTOP(sEE_I2C, ENABLE);

        (uint8_t)(*NumByteToWrite)--;
    }
    /* DMA could be used for number of data higher than 1 */
    else
    {
        /* Configure the DMA Tx Channel with the buffer address and the buffer size */
        sEE_LowLevel_DMAConfig((uint16_t)pBuffer, (uint8_t)(*NumByteToWrite), sEE_DIRECTION_TX);

        /* Enable the DMA Tx Channel */
        DMA_Cmd(sEE_I2C_DMA_CHANNEL_TX, ENABLE);

        /* Global DMA Enable */
        DMA_GlobalCmd(ENABLE);
    }
}
/**
  * @brief  Reads a block of data from the EEPROM.
  * @param  pBuffer : pointer to the buffer that receives the data read from
  *         the EEPROM.
  * @param  ReadAddr : EEPROM's internal address to read from.
  * @param  NumByteToRead : pointer to the variable holding number of bytes to
  *         read from the EEPROM.
  *
  *        @note The variable pointed by NumByteToRead is reset to 0 when all the
  *              data are read from the EEPROM. Application should monitor this
  *              variable in order know when the transfer is complete.
  *
  * @note When number of data to be read is higher than 1, this function just
  *       configure the communication and enable the DMA channel to transfer data.
  *       Meanwhile, the user application may perform other tasks.
  *       When number of data to be read is 1, then the DMA is not used.
  *
  * @retval None
  */
void sEE_ReadBuffer(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t* NumByteToRead)
{
    __IO uint32_t timeout = 0xFFFF;

    /*!< Wait the end of last communication */
    for (; timeout > 0; timeout--);

    /* Set the pointer to the Number of data to be read. This pointer will be used
        by the DMA Transfer Complete interrupt Handler in order to reset the
        variable to 0. User should check on this variable in order to know if the
        DMA transfer has been completed or not. */
    sEEDataReadPointer = NumByteToRead;

    /*!< While the bus is busy */
    while (I2C_GetFlagStatus(sEE_I2C, I2C_FLAG_BUSY))
    {}

    /*!< Send START condition */
    I2C_GenerateSTART(sEE_I2C, ENABLE);

    /*!< Test on EV5 and clear it */
    while (!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_MODE_SELECT))
    {}

    /*!< Send EEPROM address for write */
    I2C_Send7bitAddress(sEE_I2C, (uint8_t)sEEAddress, I2C_Direction_Transmitter);

    /*!< Test on EV6 and clear it */
    while (!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
    {}

#ifdef sEE_M24C64_32

    /*!< Send the EEPROM's internal address to read from: MSB of the address first */
    I2C_SendData(sEE_I2C, (uint8_t)((ReadAddr & 0xFF00) >> 8));

    /*!< Test on EV8 and clear it */
    while (!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
    {}

    /*!< Send the EEPROM's internal address to read from: LSB of the address */
    I2C_SendData(sEE_I2C, (uint8_t)(ReadAddr & 0x00FF));

#endif /*!< sEE_M24C64_32 */

    /*!< Test on EV8 and clear it */
    while (!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
    {}

    /*!< Send STRAT condition a second time */
    I2C_GenerateSTART(sEE_I2C, ENABLE);

    /*!< Test on EV5 and clear it */
    while (!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_MODE_SELECT))
    {}

    /*!< Send EEPROM address for read */
    I2C_Send7bitAddress(sEE_I2C, (uint8_t)sEEAddress, I2C_Direction_Receiver);

    /*!< Test on EV6 and clear it */
    while (!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
    {}

    /* If number of data to be read is 1, then DMA couldn't be used */
    if ((uint16_t)(*NumByteToRead) < 2)
    {
        /*!< Disable Acknowledgement */
        I2C_AcknowledgeConfig(sEE_I2C, DISABLE);

        /*!< Send STOP Condition */
        I2C_GenerateSTOP(sEE_I2C, ENABLE);

        /*!< Test on EV7 and clear it */
        while (!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_BYTE_RECEIVED))
        {}

        /*!< Read a byte from the EEPROM */
        *pBuffer = I2C_ReceiveData(sEE_I2C);

        /*!< Decrement the read bytes counter */
        (uint16_t)(*NumByteToRead)--;

        /*!< Enable Acknowledgement to be ready for another reception */
        I2C_AcknowledgeConfig(sEE_I2C, ENABLE);
    }
    /* DMA could be used for number of data higher than 1 */
    else
    {
        /* Configure the DMA Rx Channel with the buffer address and the buffer size */
        sEE_LowLevel_DMAConfig((uint16_t)pBuffer, (uint8_t)(*NumByteToRead), sEE_DIRECTION_RX);

        /* Inform the DMA that the next End Of Transfer Signal will be the last one */
        I2C_DMALastTransferCmd(sEE_I2C, ENABLE);

        /* Enable the DMA Rx Channel */
        DMA_Cmd(sEE_I2C_DMA_CHANNEL_RX, ENABLE);

        /* Global DMA Enable */
        DMA_GlobalCmd(ENABLE);
    }
}
Example #8
0
/**
  * @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)
  {}
}