日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁(yè) > 單片機(jī) > 單片機(jī)
[導(dǎo)讀] 一、前言DMA會(huì)在不同的寄存器/ram/存儲(chǔ)設(shè)備之間建立通道,自動(dòng)傳輸數(shù)據(jù),以達(dá)到解放CPU的目的。比如你想用DAC模塊去輸出一段特定的波形,就要讓CPU將預(yù)設(shè)的數(shù)值不斷寫入DAC的寄存器。這時(shí)CPU被DAC任務(wù)長(zhǎng)

一、前言

DMA會(huì)在不同的寄存器/ram/存儲(chǔ)設(shè)備之間建立通道,自動(dòng)傳輸數(shù)據(jù),以達(dá)到解放CPU的目的。

比如你想用DAC模塊去輸出一段特定的波形,就要讓CPU將預(yù)設(shè)的數(shù)值不斷寫入DAC的寄存器。這時(shí)CPU被DAC任務(wù)長(zhǎng)期占用,系統(tǒng)處理其他任務(wù)和響應(yīng)其他事件的能力被大幅降低。

在實(shí)際應(yīng)用里,經(jīng)常有一些繁重的讀寫操作。這些操作不需要經(jīng)過(guò)計(jì)算,卻依然占用了大量的CPU資源,遇到這種情況就要考慮使用DMA了。

我開發(fā)板上的stm芯片上共有7個(gè)dma通道,它可以建立7個(gè)DMA連接。但是DMA控制器只有一個(gè),所以同時(shí)只能有一個(gè)DMA連接被相應(yīng)。

二、DMA的初始化

針對(duì)每一個(gè)DMA頻道,都要初始化它的控制寄存器,來(lái)看一下DMA的init結(jié)構(gòu)體的原型:

/**

* @brief DMA Configuration Structure definition

*/

typedef struct

{

uint32_t Direction; /*!< Specifies if the data will be transferred from memory to peripheral,

from memory to memory or from peripheral to memory.

This parameter can be a value of @ref DMA_Data_transfer_direction */


uint32_t PeriphInc; /*!< Specifies whether the Peripheral address register should be incremented or not.

This parameter can be a value of @ref DMA_Peripheral_incremented_mode */


uint32_t MemInc; /*!< Specifies whether the memory address register should be incremented or not.

This parameter can be a value of @ref DMA_Memory_incremented_mode */


uint32_t PeriphDataAlignment; /*!< Specifies the Peripheral data width.

This parameter can be a value of @ref DMA_Peripheral_data_size */


uint32_t MemDataAlignment; /*!< Specifies the Memory data width.

This parameter can be a value of @ref DMA_Memory_data_size */


uint32_t Mode; /*!< Specifies the operation mode of the DMAy Channelx.

This parameter can be a value of @ref DMA_mode

@note The circular buffer mode cannot be used if the memory-to-memory

data transfer is configured on the selected Channel */


uint32_t Priority; /*!< Specifies the software priority for the DMAy Channelx.

This parameter can be a value of @ref DMA_Priority_level */


} DMA_InitTypeDef;

  • Direction的值表示通道類型,外設(shè)到ram、ram到外設(shè)、ram到ram。

  • PeriphInc和MemInc表示外設(shè)和ram地址要不要遞增。像上述的DAC例子,ram的地址一定是遞增的,而外設(shè)寄存器的地址則無(wú)需遞增。

  • PeriphDataAlignment和MemDataAlignment表示外設(shè)和ram的字節(jié)寬度,有一個(gè)字節(jié),半字和全字。這將決定上面的增量模式里,一次讀取數(shù)據(jù)的大小。

  • Mode有兩種,普通和循環(huán)。普通模式下一次DMA請(qǐng)求處理完成后就不再傳輸數(shù)據(jù)。

  • Priority是DMA頻道的優(yōu)先級(jí),一共4個(gè),如果優(yōu)先級(jí)相同,頻道號(hào)小的通道率先被響應(yīng)。

這些屬性被設(shè)置完畢后,在Init函數(shù)里會(huì)將它寫入控制寄存器。


/* Get the CR register value */

tmp = hdma->Instance->CCR;


/* Clear PL, MSIZE, PSIZE, MINC, PINC, CIRC, DIR bits */

tmp &= ((uint32_t)~(DMA_CCR_PL | DMA_CCR_MSIZE | DMA_CCR_PSIZE |

DMA_CCR_MINC | DMA_CCR_PINC | DMA_CCR_CIRC |

DMA_CCR_DIR));


/* Prepare the DMA Channel configuration */

tmp |= hdma->Init.Direction |

hdma->Init.PeriphInc | hdma->Init.MemInc |

hdma->Init.PeriphDataAlignment | hdma->Init.MemDataAlignment |

hdma->Init.Mode | hdma->Init.Priority;


/* Write to DMA Channel CR register */

hdma->Instance->CCR = tmp;


三、DMA通道的建立

在初始化完畢后,只需要將源地址、起始地址、傳輸總長(zhǎng)寫入寄存器,再使能該頻道即可。

HAL_StatusTypeDef HAL_DMA_Start (DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength);

HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength);

這個(gè)DMA_HandleTypeDef *hdma是用C實(shí)現(xiàn)面向?qū)ο笤O(shè)計(jì)的一個(gè)典型的例子。當(dāng)我們創(chuàng)建一個(gè)DMA頻道時(shí),必須要先建立一個(gè)DMA_HandleTypeDef類型的結(jié)構(gòu)體變量,這個(gè)行為實(shí)際上就是創(chuàng)建了一個(gè)DMA類的實(shí)例。

typedef struct __DMA_HandleTypeDef

{

DMA_Channel_TypeDef *Instance; /*!< Register base address */


DMA_InitTypeDef Init; /*!< DMA communication parameters */


HAL_LockTypeDef Lock; /*!< DMA locking object */


HAL_DMA_StateTypeDef State; /*!< DMA transfer state */


void *Parent; /*!< Parent object state */


void (* XferCpltCallback)( struct __DMA_HandleTypeDef * hdma); /*!< DMA transfer complete callback */


void (* XferHalfCpltCallback)( struct __DMA_HandleTypeDef * hdma); /*!< DMA Half transfer complete callback */


void (* XferErrorCallback)( struct __DMA_HandleTypeDef * hdma); /*!< DMA transfer error callback */


__IO uint32_t ErrorCode; /*!< DMA Error code */


} DMA_HandleTypeDef;

這個(gè)結(jié)構(gòu)體除了包含有init結(jié)構(gòu)體、鎖、DMA寄存器指針、狀態(tài)變量、錯(cuò)誤變量之外,還包含了一些callback函數(shù)的指針。它甚至有一個(gè)父類指針,只要將該指針指向一些adc、uart等外設(shè)的handle類,就等于完成了繼承。


除了有init和start函數(shù)外、HAL里還提供常規(guī)的DMA中斷處理函數(shù),等待DMA傳輸,獲取DMA狀態(tài)的一些函數(shù),結(jié)構(gòu)上與前面的adc、flash等類似,就不做敘述了。


四、例子


來(lái)看一個(gè)串口用dma收發(fā)的例子。

首先是寫收發(fā)兩個(gè)頻道的控制寄存器:

/*##-3- Configure the DMA ########

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉