2024-11-07 04:57 AM
I'm using the STM32F407T6 for the first time and would like to be able to communicate with my computer using TTL.
I'm trying to transfer the data by DMA but why the RX interrupt is not triggered after confirming the receipt of the data I want to receive the DMA_GetState = true result.
main.c
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "beep.h"
#include "key.h"
#include "lcd.h"
#include <string.h>
#include <stdbool.h>
#define SEND_BUF_SIZE 8200
u8 SendBuff[SEND_BUF_SIZE];
int main(void)
{
u8 t;
u8 len;
u8 key;
u16 times=0;
bool showMessageFlag = false;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168);
uart_init(115200);
LED_Init();
LCD_Init();
KEY_Init();
char TEXT_TO_SEND[]={"STM32: Test the DMA sending function.\r\n"};
//USART3
char* currentData;
USART3_DMA_Init();
USART3_init(115200);
char text1[20];
while(1)
{
//LCD
LCD_Clear(WHITE);
POINT_COLOR = BLACK;
LCD_ShowString(10, 40, 210, 24,24,"USART Test");
LCD_ShowString(10, 70, 200, 16, 16, "Show Test:");
LCD_ShowString(10, 100, 200, 16, 16, "Send State:");
//LCD_ShowString(10, 100, 200, 16, 16, TEXT_TO_SEND);
sprintf(text1, "%d", sizeof(TEXT_TO_SEND));
LCD_ShowString(10, 150, 200, 16, 16, "text1: ");
LCD_ShowString(70, 150, 200, 16, 16, text1);
LCD_ShowString(10, 170, 200, 16, 16, "text2: ");
LCD_ShowString(70, 170, 200, 16, 16, text);
LCD_ShowString(10, 190, 200, 16, 16, "text3: ");
LCD_ShowString(70, 190, 200, 16, 16, text3);
//memcpy(text3, USART_RX_BUF, sizeof(USART3_RX_Buffer));
//RecieveData();
if(DMA_GetState)
{
LCD_ShowString(10, 210, 200, 16, 16, "Have Date!");
//text3[1] = USART3_RX_Buffer[1];
//RecieveData();
}
else
{
LCD_ShowString(10, 210, 200, 16, 16, "No Date!");
}
if(DMA_SendState)
{
LCD_ShowString(110, 100, 200, 16, 16, "Successes");
LCD_ShowString(10, 130, 200, 16, 16, "Send Command Done!");
}
else
{
LCD_ShowString(110, 100, 200, 16, 16, "Failed");
}
key = KEY_Scan(0);
if(key == 1)
{
//showMessageFlag = true;
//RecieveData();
}
else if(key == 2)
{
//DMA_SendMessage(TEXT_TO_SEND, sizeof(TEXT_TO_SEND));USART3_DMA_BufferSize
DMA_SendMessage(TEXT_TO_SEND, sizeof(TEXT_TO_SEND));
}
else if(key == 3)
{
DMA_SendState = false;
//Test
memset(text, 0,sizeof(text));
memset(text3, 0,sizeof(text3));
//CleanBuffer();
}
if(showMessageFlag)
{
currentData = (char*)USART_RX_BUF;
LCD_ShowString(120, 70, 200, 16, 16, currentData);
USART_RX_STA=0;
}
delay_ms(100);
}
}
usart.c
#include "sys.h"
#include "usart.h"
#include "stdlib.h"
#include "string.h"
//////////////////////////////////////////////////////////////////////////////////
#if SYSTEM_SUPPORT_OS
#include "includes.h"
#endif
//////////////////////////////////////////////////////////////////
#if 1
#pragma import(__use_no_semihosting)
//¼Ð·Ç®w»Ýnªº¤ä«ù¨ç¼Æ
struct __FILE
{
int handle;
};
FILE __stdout;
void _sys_exit(int x)
{
x = x;
}
//«©w¸qfputc¨ç¼Æ
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);//´`Àôµo°e,ª½¨ìµo°e§¹²¦
USART1->DR = (u8) ch;
return ch;
}
#endif
#if EN_USART1_RX //¦pªG¨Ï¯à¤F±µ¦¬
u8 USART_RX_BUF[USART_REC_LEN]; //±µ¦¬½w½Ä,³Ì¤jUSART_REC_LENÓ¦r¸`.
u16 USART_RX_STA=0; //±µ¦¬ª¬ºA¼Ð°O
u8 USART3_TX_Buffer[USART3_DMA_BufferSize];
u8 USART3_RX_Buffer[USART3_DMA_BufferSize];
bool DMA_SendState = false;
bool DMA_GetState = false;
bool USART3_TX_FLAG = false;
bool USART3_RX_FLAG = false;
char text[20] = "0";
char text3[USART3_DMA_BufferSize] = "0";
u16 testValue = 0;
void uart_init(u32 bound){
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; //GPIOA9»PGPIOA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//´_¥Î¥\¯à
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //³t«×50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //±À®¾´_¥Î¿é¥X
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //¤W©Ô
GPIO_Init(GPIOA,&GPIO_InitStructure); //ªì©l¤ÆPA9¡APA10
USART_InitStructure.USART_BaudRate = bound;
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(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
#if EN_USART1_RX
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
#endif
}
void USART3_init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = bound;
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(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
USART_ClearFlag(USART3, USART_FLAG_TC);
while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
USART_ClearFlag(USART3, USART_FLAG_TC);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
//USART_ITConfig(USART3, USART_IT_TXE, ENABLE);
//USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);
//USART_ITConfig(USART3, USART_IT_TC, ENABLE);
USART_DMACmd(USART3, USART_DMAReq_Tx, ENABLE);
USART_DMACmd(USART3, USART_DMAReq_Rx, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 8;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART1_IRQHandler(void)
{
u8 Res;
#if SYSTEM_SUPPORT_OS
OSIntEnter();
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
Res =USART_ReceiveData(USART1);
if((USART_RX_STA&0x8000)==0)
{
if(USART_RX_STA&0x4000)//±µ¦¬¨ì¤F0x0d
{
if(Res!=0x0a)USART_RX_STA=0;
else USART_RX_STA|=0x8000;
}
else
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;
}
}
}
}
#if SYSTEM_SUPPORT_OS
OSIntExit();
#endif
}
#endif
void USART3_IRQHandler(void)
{
u8 Res;
if(USART_GetFlagStatus(USART3,USART_FLAG_ORE) == SET)
{
USART_ClearFlag(USART3,USART_FLAG_ORE);
USART_ReceiveData(USART3);
}
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
{
Res =USART_ReceiveData(USART3);//Get message
if((USART_RX_STA&0x8000)==0)//Send action not done
{
if(USART_RX_STA&0x4000)
{
if(Res!=0x0a)USART_RX_STA=0;
else USART_RX_STA|=0x8000;
}
else
{
if(Res==0x0d)
{
USART_RX_STA|=0x4000;
}
else
{
//USART3_RX_Buffer[USART_RX_STA&0X3FFF]=Res ;
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))
{
USART_RX_STA = 0;
}
}
}
}
}
}
void USART_SendChar(USART_TypeDef *USART, char ch)
{
USART_SendData(USART, (uint8_t)ch);
while (USART_GetFlagStatus(USART, USART_FLAG_TXE) == RESET);//Wait for the message to sinish transmitting
}
void USART_SendString(USART_TypeDef *USART, const char* str)
{
while (*str)
{
USART_SendChar(USART, *str++);
}
}
void USART_ReadMessage(USART_TypeDef *USART)
{
u8 data;
data = USART_ReceiveData(USART);
USART_RX_BUF[USART_RX_STA&0X3FFF] = data;
USART_RX_STA++;
}
void CleanBuffer()
{
memset(USART_RX_BUF, 0x00, sizeof(USART_RX_BUF));
}
void MYDMA_Config(DMA_Stream_TypeDef *DMA_Streamx,u32 chx,u32 par,u32 mar,u16 ndtr)
{
DMA_InitTypeDef DMA_InitStructure;
if((u32)DMA_Streamx>(u32)DMA2)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);
}else
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);
}
DMA_DeInit(DMA_Streamx);
while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE){}
DMA_InitStructure.DMA_Channel = chx;
DMA_InitStructure.DMA_PeripheralBaseAddr = par;
DMA_InitStructure.DMA_Memory0BaseAddr = mar;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = ndtr;
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_Medium;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA_Streamx, &DMA_InitStructure);//ªì©l¤ÆDMA Stream
}
void MYDMA_Enable(DMA_Stream_TypeDef *DMA_Streamx,u16 ndtr)
{
DMA_Cmd(DMA_Streamx, DISABLE);
while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE){}
DMA_SetCurrDataCounter(DMA_Streamx,ndtr);
DMA_Cmd(DMA_Streamx, ENABLE);
}
void USART3_DMA_Init(void)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);
//TX
DMA_DeInit(DMA1_Stream3);
while (DMA_GetCmdStatus(DMA1_Stream3) != DISABLE){}
DMA_InitStructure.DMA_Channel = DMA_Channel_4;
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART3->DR;
DMA_InitStructure.DMA_Memory0BaseAddr = (u32)USART3_TX_Buffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = USART3_DMA_BufferSize;
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_Medium;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream3, &DMA_InitStructure);
//USART3 Sending Interruption
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream3_IRQn ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_ITConfig(DMA1_Stream3, DMA_IT_TC, ENABLE);//Open USART3 TX DMA Interrupt
//RX
DMA_DeInit(DMA1_Stream1);
while (DMA_GetCmdStatus(DMA1_Stream1) != DISABLE){}
DMA_InitStructure.DMA_Channel = DMA_Channel_4;
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART3->DR;
DMA_InitStructure.DMA_Memory0BaseAddr = (u32)USART3_RX_Buffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = USART3_DMA_BufferSize;
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_Medium;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream1, &DMA_InitStructure);
//USART3 Receive Interruption
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream1_IRQn ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_ClearITPendingBit(DMA1_Stream1, DMA_IT_TCIF1);
DMA_ITConfig(DMA1_Stream1, DMA_IT_TC, ENABLE);//Open USART3 RX DMA Interrupt
DMA_Cmd(DMA1_Stream1, ENABLE);
}
//TX Interrupt
void DMA1_Stream3_IRQHandler (void)
{
//When the DMA has finished sending data.
if(DMA_GetFlagStatus(DMA1_Stream3,DMA_FLAG_TCIF3) != RESET)
{
DMA_ClearFlag(DMA1_Stream3,DMA_FLAG_TCIF3);
DMA_SendState = true;
}
}
//RX Interrupt
void DMA1_Stream1_IRQHandler (void)
{
if(DMA_GetITStatus(DMA1_Stream1, DMA_IT_TCIF1))
{
DMA_GetState = true;
/*memcpy(text3, "test123", 15);
DMA_ClearITPendingBit(DMA1_Stream1, DMA_IT_TCIF1);
USART_ClearFlag(USART3, USART_FLAG_TC);
DMA_Cmd(DMA1_Stream1, DISABLE);*/
}
}
void DMA_SendMessage(char* Data, u16 DataLength)
{
//CopyData(Data, USART3_DMA_BufferSize);
//while(USART3_TX_FLAG);
//USART3_TX_FLAG = true;
sprintf(text, "%d", DataLength);
CopyData(Data, DataLength);
USART_DMACmd(USART3, USART_DMAReq_Tx, ENABLE);
DMA_Cmd(DMA1_Stream3, DISABLE);
while (DMA_GetCmdStatus(DMA1_Stream3) != DISABLE){}
DMA_SetCurrDataCounter(DMA1_Stream3, DataLength);
DMA_Cmd(DMA1_Stream3, ENABLE);
}
void InitialBuffer(void)
{
DMA_SendState = false;
memset(USART3_TX_Buffer, 0, sizeof(USART3_TX_Buffer));
USART3_TX_Buffer[0] = '\0';
}
void CopyData(char* Data, int length)
{
InitialBuffer();
memcpy(USART3_TX_Buffer, Data, length);
}
void RecieveData(void)
{
u16 length = 0;
DMA_Cmd(DMA1_Stream1, DISABLE);
DMA_ClearFlag(DMA1_Stream1, DMA_FLAG_TCIF1);
length = USART3_DMA_BufferSize - DMA_GetCurrDataCounter(DMA1_Stream1);
//test
//text3 = USART3_RX_Buffer;
//memcpy(text3, "test", 10);
memcpy(text3, USART_RX_BUF, 10);
//sprintf(text3, "%d", DMA_GetCurrDataCounter(DMA1_Stream1));
//
DMA_Cmd(DMA1_Stream1, ENABLE);
//memcpy(USART3_RX_Buffer, Data, length);
}
void USART_DMA_Clear(void)
{
}