سلام
دوستان MikroC کار میدونن که برای واحد DMA لایبرری نداریم این کد رو که برای ارسال دیتا بوسیله DMA هست میزارم که خیلی کاربردیه :
میکرو : STM32F4
کامپایلر : MikroC ARM
روی میکروی STM32F407VGT6 تست کردم و کاملا کار می کنه.
لینک منبع که هم برای ارسال و هم برای دریافت برنامه رو به زبان بیسیک نوشته که من فقط ارسالش رو تبدیل کردم
کد برنامه :
کد:
void DMA_USART2_TX() iv IVT_INT_DMA1_Stream6 ics ICS_AUTO {
DMA1_HIFCR.CTCIF6 = 1; //clear the int or it will not restart again!
}
//Main Function ----------------------------------------------------------------
void main() {
char rec[4] = "QWER";
UART2_Init_Advanced(9600, _UART_8_BIT_DATA, _UART_NOPARITY, _UART_ONE_STOPBIT, &_GPIO_MODULE_USART2_PA23);
RCC_AHB1ENR.DMA1EN = 1; //Enable DMA1 CLOCK
DMA1_S6CR = 0; //Disable the DMA Stream by resetting EN bit to be able to change bit registers
while(DMA1_S6CR.EN); //wait until EN == 0 to make sure DMA is stopped
DMA1_HIFCR.CTCIF6 = 1; //All Stream Status dedicated registers to be cleared
DMA1_S6PAR = &USART2_DR; //Set peripheral port register address ; USART2 addr=40004400 and USART2_DR is at offset 0x4
DMA1_S6M0AR = rec; //Set memory address
DMA1_S6NDTR = 4; //total number of data items (in this case, send out 4 bytes)
DMA1_S6FCR.DMDIS = 0; //Direct mode
//Select DMA channel request number (CH4 for USART2)
DMA1_S6CR.CHSEL2 = 1;
DMA1_S6CR.CHSEL1 = 0;
DMA1_S6CR.CHSEL0 = 0;
//Config the stream priority (to Very High)
DMA1_S6CR.PL1 = 1;
DMA1_S6CR.PL0 = 1;
//Memory data size to 8 bit
DMA1_S6CR.MSIZE1 = 0;
DMA1_S6CR.MSIZE0 = 0;
//Peripheral data size to 8 bit
DMA1_S6CR.PSIZE1 = 0;
DMA1_S6CR.PSIZE0 = 0;
DMA1_S6CR.MINC = 1; //Memory increment mode
DMA1_S6CR.PINC = 0; //Peripheral increment mode
//Burst mode PERIPHERAL
DMA1_S6CR.PBURST1 = 0;
DMA1_S6CR.PBURST0 = 0;
//Burst mode Memory
DMA1_S6CR.MBURST1 = 0;
DMA1_S6CR.MBURST0 = 0;
//Data transfer direction (memory to peripheral)
DMA1_S6CR.DIR1 = 0;
DMA1_S6CR.DIR0 = 1;
DMA1_S6CR.PFCTRL = 0; //peripheral flow controller ,DMA flow
DMA1_S6CR.TCIE = 1; //Transfer complete interrupt enable
//DMA mode is enabled for transmission
USART2_CR3.DMAT = 1;
USART2_SR.TC = 0;
EnableInterrupts();
NVIC_IntEnable(IVT_INT_DMA1_Stream6);
//Stream enable (start the DMA Stream6)
DMA1_S6CR.EN = 1;
while(1);
}
آدرس مبدأ و آدرس مقصد و طول بافر با این سه دستور مشخص می شه:
کد:
DMA1_S6PAR = &USART2_DR; //Set peripheral port register address ; USART2 addr=40004400 and USART2_DR is at offset 0x4
DMA1_S6M0AR = rec; //Set memory address
DMA1_S6NDTR = 4; //total number of data items (in this case, send out 4 bytes)
و با این دستور عملیات ارسال آغاز می شه:
کد:
//Stream enable (start the DMA Stream6)
DMA1_S6CR.EN = 1;
در نهایت پس از اتمام ارسال DMA وقفه میزند و در بردار وقفه با دستور زیر واحد DMA را آماده برای ارسال بعدی می کنیم:
کد:
DMA1_HIFCR.CTCIF6 = 1; //clear the int or it will not restart again!
موفق باشید