📄 stm32f2xx_dma.c
字号:
assert_param(IS_DMA_PERIPHERAL_DATA_SIZE(DMA_InitStruct->DMA_PeripheralDataSize));
assert_param(IS_DMA_MEMORY_DATA_SIZE(DMA_InitStruct->DMA_MemoryDataSize));
assert_param(IS_DMA_MODE(DMA_InitStruct->DMA_Mode));
assert_param(IS_DMA_PRIORITY(DMA_InitStruct->DMA_Priority));
assert_param(IS_DMA_FIFO_MODE_STATE(DMA_InitStruct->DMA_FIFOMode));
assert_param(IS_DMA_FIFO_THRESHOLD(DMA_InitStruct->DMA_FIFOThreshold));
assert_param(IS_DMA_MEMORY_BURST(DMA_InitStruct->DMA_MemoryBurst));
assert_param(IS_DMA_PERIPHERAL_BURST(DMA_InitStruct->DMA_PeripheralBurst));
/*------------------------- DMAy Streamx CR Configuration ------------------*/
/* Get the DMAy_Streamx CR value */
tmpreg = DMAy_Streamx->CR;
/* Clear CHSEL, MBURST, PBURST, PL, MSIZE, PSIZE, MINC, PINC, CIRC and DIR bits */
tmpreg &= ((uint32_t)~(DMA_SxCR_CHSEL | DMA_SxCR_MBURST | DMA_SxCR_PBURST | \
DMA_SxCR_PL | DMA_SxCR_MSIZE | DMA_SxCR_PSIZE | \
DMA_SxCR_MINC | DMA_SxCR_PINC | DMA_SxCR_CIRC | \
DMA_SxCR_DIR));
/* Configure DMAy Streamx: */
/* Set CHSEL bits according to DMA_CHSEL value */
/* Set DIR bits according to DMA_DIR value */
/* Set PINC bit according to DMA_PeripheralInc value */
/* Set MINC bit according to DMA_MemoryInc value */
/* Set PSIZE bits according to DMA_PeripheralDataSize value */
/* Set MSIZE bits according to DMA_MemoryDataSize value */
/* Set CIRC bit according to DMA_Mode value */
/* Set PL bits according to DMA_Priority value */
/* Set MBURST bits according to DMA_MemoryBurst value */
/* Set PBURST bits according to DMA_PeripheralBurst value */
tmpreg |= DMA_InitStruct->DMA_Channel | DMA_InitStruct->DMA_DIR |
DMA_InitStruct->DMA_PeripheralInc | DMA_InitStruct->DMA_MemoryInc |
DMA_InitStruct->DMA_PeripheralDataSize | DMA_InitStruct->DMA_MemoryDataSize |
DMA_InitStruct->DMA_Mode | DMA_InitStruct->DMA_Priority |
DMA_InitStruct->DMA_MemoryBurst | DMA_InitStruct->DMA_PeripheralBurst;
/* Write to DMAy Streamx CR register */
DMAy_Streamx->CR = tmpreg;
/*------------------------- DMAy Streamx FCR Configuration -----------------*/
/* Get the DMAy_Streamx FCR value */
tmpreg = DMAy_Streamx->FCR;
/* Clear DMDIS and FTH bits */
tmpreg &= (uint32_t)~(DMA_SxFCR_DMDIS | DMA_SxFCR_FTH);
/* Configure DMAy Streamx FIFO:
Set DMDIS bits according to DMA_FIFOMode value
Set FTH bits according to DMA_FIFOThreshold value */
tmpreg |= DMA_InitStruct->DMA_FIFOMode | DMA_InitStruct->DMA_FIFOThreshold;
/* Write to DMAy Streamx CR */
DMAy_Streamx->FCR = tmpreg;
/*------------------------- DMAy Streamx NDTR Configuration ----------------*/
/* Write to DMAy Streamx NDTR register */
DMAy_Streamx->NDTR = DMA_InitStruct->DMA_BufferSize;
/*------------------------- DMAy Streamx PAR Configuration -----------------*/
/* Write to DMAy Streamx PAR */
DMAy_Streamx->PAR = DMA_InitStruct->DMA_PeripheralBaseAddr;
/*------------------------- DMAy Streamx M0AR Configuration ----------------*/
/* Write to DMAy Streamx M0AR */
DMAy_Streamx->M0AR = DMA_InitStruct->DMA_Memory0BaseAddr;
}
/**
* @brief Fills each DMA_InitStruct member with its default value.
* @param DMA_InitStruct : pointer to a DMA_InitTypeDef structure which will
* be initialized.
* @retval None
*/
void DMA_StructInit(DMA_InitTypeDef* DMA_InitStruct)
{
/*-------------- Reset DMA init structure parameters values ----------------*/
/* Initialize the DMA_Channel member */
DMA_InitStruct->DMA_Channel = 0;
/* Initialize the DMA_PeripheralBaseAddr member */
DMA_InitStruct->DMA_PeripheralBaseAddr = 0;
/* Initialize the DMA_Memory0BaseAddr member */
DMA_InitStruct->DMA_Memory0BaseAddr = 0;
/* Initialize the DMA_DIR member */
DMA_InitStruct->DMA_DIR = DMA_DIR_PeripheralToMemory;
/* Initialize the DMA_BufferSize member */
DMA_InitStruct->DMA_BufferSize = 0;
/* Initialize the DMA_PeripheralInc member */
DMA_InitStruct->DMA_PeripheralInc = DMA_PeripheralInc_Disable;
/* Initialize the DMA_MemoryInc member */
DMA_InitStruct->DMA_MemoryInc = DMA_MemoryInc_Disable;
/* Initialize the DMA_PeripheralDataSize member */
DMA_InitStruct->DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
/* Initialize the DMA_MemoryDataSize member */
DMA_InitStruct->DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
/* Initialize the DMA_Mode member */
DMA_InitStruct->DMA_Mode = DMA_Mode_Normal;
/* Initialize the DMA_Priority member */
DMA_InitStruct->DMA_Priority = DMA_Priority_Low;
/* Initialize the DMA_FIFOMode member */
DMA_InitStruct->DMA_FIFOMode = DMA_FIFOMode_Disable;
/* Initialize the DMA_FIFOThreshold member */
DMA_InitStruct->DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;
/* Initialize the DMA_MemoryBurst member */
DMA_InitStruct->DMA_MemoryBurst = DMA_MemoryBurst_Single;
/* Initialize the DMA_PeripheralBurst member */
DMA_InitStruct->DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
}
/**
* @brief Enables or disables the specified DMAy Streamx.
* @param DMAy_Streamx: where y can be 1 or 2 to select the DMA and x can be 0
* to 7 to select the DMA Stream.
* @param NewState: new state of the DMAy Streamx.
* This parameter can be: ENABLE or DISABLE.
*
* @note This function may be used to perform Pause-Resume operation. When a
* transfer is ongoing, calling this function to disable the Stream will
* cause the transfer to be paused. All configuration registers and the
* number of remaining data will be preserved. When calling again this
* function to re-enable the Stream, the transfer will be resumed from
* the point where it was paused.
*
* @note After configuring the DMA Stream (DMA_Init() function) and enabling the
* stream, it is recommended to check (or wait until) the DMA Stream is
* effectively enabled. A Stream may remain disabled if a configuration
* parameter is wrong.
* After disabling a DMA Stream, it is also recommended to check (or wait
* until) the DMA Stream is effectively disabled. If a Stream is disabled
* while a data transfer is ongoing, the current data will be transferred
* and the Stream will be effectively disabled only after the transfer of
* this single data is finished.
*
* @retval None
*/
void DMA_Cmd(DMA_Stream_TypeDef* DMAy_Streamx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_DMA_ALL_PERIPH(DMAy_Streamx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the selected DMAy Streamx by setting EN bit */
DMAy_Streamx->CR |= (uint32_t)DMA_SxCR_EN;
}
else
{
/* Disable the selected DMAy Streamx by clearing EN bit */
DMAy_Streamx->CR &= ~(uint32_t)DMA_SxCR_EN;
}
}
/**
* @brief Configures, when the PINC (Peripheral Increment address mode) bit is
* set, if the peripheral address should be incremented with the data
* size (configured with PSIZE bits) or by a fixed offset equal to 4
* (32-bit aligned addresses).
*
* @note This function has no effect if the Peripheral Increment mode is disabled.
*
* @param DMAy_Streamx: where y can be 1 or 2 to select the DMA and x can be 0
* to 7 to select the DMA Stream.
* @param DMA_Pincos: specifies the Peripheral increment offset size.
* This parameter can be one of the following values:
* @arg DMA_PINCOS_Psize: Peripheral address increment is done
* accordingly to PSIZE parameter.
* @arg DMA_PINCOS_WordAligned: Peripheral address increment offset is
* fixed to 4 (32-bit aligned addresses).
* @retval None
*/
void DMA_PeriphIncOffsetSizeConfig(DMA_Stream_TypeDef* DMAy_Streamx, uint32_t DMA_Pincos)
{
/* Check the parameters */
assert_param(IS_DMA_ALL_PERIPH(DMAy_Streamx));
assert_param(IS_DMA_PINCOS_SIZE(DMA_Pincos));
/* Check the needed Peripheral increment offset */
if(DMA_Pincos != DMA_PINCOS_Psize)
{
/* Configure DMA_SxCR_PINCOS bit with the input parameter */
DMAy_Streamx->CR |= (uint32_t)DMA_SxCR_PINCOS;
}
else
{
/* Clear the PINCOS bit: Peripheral address incremented according to PSIZE */
DMAy_Streamx->CR &= ~(uint32_t)DMA_SxCR_PINCOS;
}
}
/**
* @brief Configures, when the DMAy Streamx is disabled, the flow controller for
* the next transactions (Peripheral or Memory).
*
* @note Before enabling this feature, check if the used peripheral supports
* the Flow Controller mode or not.
*
* @param DMAy_Streamx: where y can be 1 or 2 to select the DMA and x can be 0
* to 7 to select the DMA Stream.
* @param DMA_FlowCtrl: specifies the DMA flow controller.
* This parameter can be one of the following values:
* @arg DMA_FlowCtrl_Memory: DMAy_Streamx transactions flow controller is
* the DMA controller.
* @arg DMA_FlowCtrl_Peripheral: DMAy_Streamx transactions flow controller
* is the peripheral.
* @retval None
*/
void DMA_FlowControllerConfig(DMA_Stream_TypeDef* DMAy_Streamx, uint32_t DMA_FlowCtrl)
{
/* Check the parameters */
assert_param(IS_DMA_ALL_PERIPH(DMAy_Streamx));
assert_param(IS_DMA_FLOW_CTRL(DMA_FlowCtrl));
/* Check the needed flow controller */
if(DMA_FlowCtrl != DMA_FlowCtrl_Memory)
{
/* Configure DMA_SxCR_PFCTRL bit with the input parameter */
DMAy_Streamx->CR |= (uint32_t)DMA_SxCR_PFCTRL;
}
else
{
/* Clear the PFCTRL bit: Memory is the flow controller */
DMAy_Streamx->CR &= ~(uint32_t)DMA_SxCR_PFCTRL;
}
}
/**
* @}
*/
/** @defgroup DMA_Group2 Data Counter functions
* @brief Data Counter functions
*
@verbatim
===============================================================================
Data Counter functions
===============================================================================
This subsection provides function allowing to configure and read the buffer size
(number of data to be transferred).
The DMA data counter can be written only when the DMA Stream is disabled
(ie. after transfer complete event).
The following function can be used to write the Stream data counter value:
- void DMA_SetCurrDataCounter(DMA_Stream_TypeDef* DMAy_Streamx, uint16_t Counter);
@note It is advised to use this function rather than DMA_Init() in situations where
only the Data buffer needs to be reloaded.
@note If the Source and Destination Data Sizes are different, then the value written in
data counter, expressing the number of transfers, is relative to the number of
transfers from the Peripheral point of view.
ie. If Memory data size is Word, Peripheral data size is Half-Words, then the value
to be configured in the data counter is the number of Half-Words to be transferred
from/to the peripheral.
The DMA data counter can be read to indicate the number of remaining transfers for
the relative DMA Stream. This counter is decremented at the end of each data
transfer and when the transfer is complete:
- If Normal mode is selected: the counter is set to 0.
- If Circular mode is selected: the counter is reloaded with the initial value
(configured before enabling the DMA Stream)
The following function can be used to read the Stream data counter value:
- uint16_t DMA_GetCurrDataCounter(DMA_Stream_TypeDef* DMAy_Streamx);
@endverbatim
* @{
*/
/**
* @brief Writes the number of data units to be transferred on the DMAy Streamx.
* @param DMAy_Streamx: where y can be 1 or 2 to select the DMA and x can be 0
* to 7 to select the DMA Stream.
* @param Counter: Number of data units to be transferred (from 0 to 65535)
* Number of data items depends only on the Peripheral data format.
*
* @note If Peripheral data format is Bytes: number of data units is equal
* to total number of bytes to be transferred.
*
* @note If Peripheral data format is Half-Word: number of data units is
* equal to total number of bytes to be transferred / 2.
*
* @note If Peripheral data format is Word: number of data units is equal
* to total number of bytes to be transferred / 4.
*
* @note In Memory-to-Memory transfer mode, the memory buffer pointed by
* DMAy_SxPAR register is considered as Peripheral.
*
* @retval The number of remaining data units in the current DMAy Streamx transfer.
*/
void DMA_SetCurrDataCounter(DMA_Stream_TypeDef* DMAy_Streamx, uint16_t Counter)
{
/* Check the parameters */
assert_param(IS_DMA_ALL_PERIPH(DMAy_Streamx));
/* Write the number of data units to be transferred */
DMAy_Streamx->NDTR = (uint16_t)Counter;
}
/**
* @brief Returns the number of remaining data units in the current DMAy Streamx transfer.
* @param DMAy_Streamx: where y can be 1 or 2 to select the DMA and x can be 0
* to 7 to select the DMA Stream.
* @retval The number of remaining data units in the current DMAy Streamx transfer.
*/
uint16_t DMA_GetCurrDataCounter(DMA_Stream_TypeDef* DMAy_Streamx)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -