yangrobin 发表于 2014-10-9 15:29:04

STM32 串口通信导致hard fault 产生

 硬件:

STM32F103VE

实现过程:
串口中断接收的数据采用 全局变量来接收,然后再main 循环处理串口接收数据。
上位机每次发送8个字节给STM32开发板,即消息处理中的case 'o'的情况。
现象:

1.当全局变量分配为400个字节时,开发板能够处理5次上位机消息。
2.当全局变量分配为200个字节时,开发板能够处理17次上位机消息。
3.当全局变量分配为100个字节时,开发板能够处理26次上位机消息。

检查了代码没有内存泄露的情况。请高手帮忙检查一下代码。谢谢!

追加一个问题:

在主函数中的while循环中,如果不加上count>7 这个判断条件  硬件错误中断也会直接发生。

请问有没有更好的办法在主函数来处理中断上来的消息。最好能给一个相对较完整的代码演示。谢谢!
主循环函数:
C/C++ code 
?
                                    1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
                                                u8 *usart_buff;
            u32 count=0;
            int main(void)
            {
               u16 i=0,j=0,icnt=0;
               RCC_Configuration();    //系统时钟初始化
               GPIO_Configuration();//端口初始化
               USART_Configuration();
               NVIC_Configuration();
               usart_buff=(u8*)malloc(PC_MSG_LEN);
               BuffSet(usart_buff,0x0,PC_MSG_LEN);
               
               while(1)
               {     
                if(!USART_GetFlagStatus(USART1,USART_FLAG_RXNE)&&count>7)
                {
                      MessageProcess();
                 
                }
               }   
            }
            
                     



消息处理函数:
C/C++ code 
?
                                    1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            22
            23
            24
            25
            26
            27
            28
            29
            30
            31
            32
            33
            34
            35
            36
            37
            38
            39
            40
            41
            42
            43
            44
            45
            46
            47
            48
            49
            50
            51
            52
            53
            54
            55
            56
            57
            58
            59
            60
                                                extern unsigned int count;
            extern u8* usart_buff;
             
            void  MessageProcess(void)
            {
                MSGINFO* msg_info=(MSGINFO*)malloc(sizeof(MSGINFO));
                msg_info=GetMsgInfo(usart_buff);
                 switch(msg_info->command)
                 {
                 
                case 'o':
                printf("o\n");
                break;
                 default:
                     break;
                 }
                 free(msg_info);
                count=0;
                BuffSet(usart_buff,0x0,PC_MSG_LEN);
            }
             
            void BuffSet(unsigned char* buff,unsigned char vaule, unsigned int length)
            {
                unsigned char* temp=buff;
                int i=0;
                for(i=0;icommand=0;
                msginfo->data_start_addr=0;
                msginfo->length=0;
                while(buff==0xaa && ilength+1+1+2);
                   // printf("sum = 0x%x\r\n",sum);
                    if(VerifyChksum(sum,&buff))
                    {
                       msginfo->length =GetLength(&buff);
                       msginfo->command = buff;
                       return  msginfo;
                    }
                    else
                    {
                         return 0;
                    }
                }
                return 0;
            }
            
                     



中断接收函数:
C/C++ code 
?
                                    1
            2
            3
            4
            5
            6
            7
            8
                                                void USART1_IRQHandler(void)
            {
               if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
               {
                 
                usart_buff=USART_ReceiveData(USART1);
               }
            }
            
                     

更多0

发表于 2014-10-10 10:44:41

RE:STM32 串口通信导致hard fault 产生

count++溢出了。导致buff发生了错误,count不要超出buff的最大值。
页: [1]
查看完整版本: STM32 串口通信导致hard fault 产生