下面还是给个简单的示例程序,示例程序中还用到了uCOS的信号量,中断处理函数通过信号量通知Task 完成了DMA传输:#include "stm32f10x.h"
#include "uart.h"
#include "led.h"
#include "COMMRTOS.H"
#include "ucos_ii.h"
#define TASK_STK_SIZE 128
OS_STK TaskStartStk[TASK_STK_SIZE];
void USART2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void UART2_TX_DMA_Init(uint8_t *p_str, uint16_t cnt)
{
// DMA_InitTypeDef DMA_InitStructure;
// DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &(USART2->DR);
// DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) str;
// DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
// DMA_InitStructure.DMA_BufferSize = 14;
// DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
// DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
// DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
// DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
// DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
// DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
// DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
// DMA_Init(DMA1_Channel7, &DMA_InitStructure);
DMA1_Channel7->CPAR = (uint32_t) &(USART2->DR);
DMA1_Channel7->CMAR = (uint32_t) p_str;
DMA1_Channel7->CNDTR = cnt;
DMA1_Channel7->CCR = DMA_DIR_PeripheralDST | DMA_Priority_Low |
DMA_Mode_Normal | DMA_PeripheralInc_Disable |
DMA_MemoryInc_Enable | DMA_PeripheralDataSize_Byte |
DMA_MemoryDataSize_Byte | DMA_M2M_Disable;
}
OS_EVENT *UART2_DMA_TX_Sem;
uint8_t str[] = "Hello World!!!";
void TaskStart(void *pdata)
{
unsigned char err;
SysTick_Config(SystemCoreClock/10);
USART2_Init();
USART_DMACmd(USART2, USART_DMAReq_Tx, ENABLE);
UART2_TX_DMA_Init(str);
UART2_DMA_TX_Sem = OSSemCreate(1);
for(;;)
{
LED_Spark();
OSSemPend(UART2_DMA_TX_Sem, 0, &err);
//DMA_Cmd(DMA1_Channel7, DISABLE);
DMA1_Channel7->CCR &= (uint16_t)(~DMA_CCR1_EN);
//DMA_Init(DMA1_Channel7, &DMA_InitStructure);
DMA1_Channel7->CNDTR = 14;
//DMA_Cmd(DMA1_Channel7, ENABLE);
DMA1_Channel7->CCR |= DMA_CCR1_EN;
//USART_DMACmd(USART2, USART_DMAReq_Tx, ENABLE);
OSTimeDly(10);
}
}
int main(void)
{
SystemInit();
LED_Init();
OSInit();
OSTaskCreate(TaskStart, (void *)0, &(TaskStartStk[TASK_STK_SIZE-1]), 1);
OSStart();
for(;;)
{
}
}
void DMA1_Channel7_IRQHandler(void)
{
OS_CPU_SR cpu_sr;
OS_ENTER_CRITICAL(); /* Tell uC/OS-II that we are starting an ISR */
OSIntNesting++;
OS_EXIT_CRITICAL();
OSSemPost(UART2_DMA_TX_Sem);
//UART_PutChar(USART2, '+');
DMA1->IFCR = DMA1_FLAG_TC7;
OSIntExit();
}
|