⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 app.c

📁 This project contains code for testin realloc api tested in fre rtos ported to AT91SAM7x256.the code
💻 C
字号:

/* Standard includes. */
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
//#include "queue.h"
/* application includes. */
#include "uip_task.h"
#include "Board.h"
#include "App.h"
/* Priorities/stacks for the application tasks. */
#define MY_TASK1_PRIORITY	        ( 5 )            //highest priority task.
                                                         //the Max priority has to be
                                                         //registered in the freertos configuration 
                                                         //file also
#define UIP_PRIORITY		        ( 4 )             
#define USB_PRIORITY                    ( 2 )    
#define INTERRUPT_TASK_PRIORITY         ( 3 ) 
#define MY_TASK2_PRIORITY	        ( 1 )

#define UIP_TASK_STACK_SIZE	          100
#define MY_TASK1_STACK_SIZE	          100
#define MY_TASK2_STACK_SIZE	          100
#define USB_TASK_STACK_SIZE               100
#define INTERRUPT_TASK_STACK_SIZE         100

#define LONG_TIME 0xffff

static void SetupHardware( void );
void my_task1( void *pvParameters );
void my_task2( void *pvParameters );
void interrupt_task( void *pvParameters );
void USBDemoTask( void *pvParameters );

unsigned int ISR_flag = IRQ_NOTUSED ;
xQueueHandle Queue1 , Queue2 ;
char *data1;
int main( void )
{	               
        SetupHardware();
        UART0_Printk("%c%c%c%c%c%c" ,0x1B,0x5B,0x32,0x4A,0x1B,0x63 );           //clear screen.
        UART0_Printk("\n\n\r\t\t AT91SAM7X512 FREE RTOS V 5.1.0  \n\n");
               
        //------------------------------------------------------------------------	 
        xTaskCreate( vuIP_TASK,     // Start the task that handles the TCP/IP and WEB server functionality. 
                     "uIP",
                     UIP_TASK_STACK_SIZE, 
                     NULL,
                     UIP_PRIORITY, 
                     NULL );  
	//-------------------------------------------------
        xTaskCreate( my_task1,
                     "MY TASK1",
                     MY_TASK1_STACK_SIZE,
                     NULL,
                     MY_TASK1_PRIORITY,
                     NULL ); 
        //-------------------------------------------------
        xTaskCreate( my_task2,
                     "MY TASK2",
                     MY_TASK2_STACK_SIZE,
                     NULL,
                     MY_TASK2_PRIORITY,
                     NULL ); 
        //-------------------------------------------------  
        xTaskCreate( USBDemoTask,
                     "USBtsk",
                     USB_TASK_STACK_SIZE,
                     NULL,
                     USB_PRIORITY,
                     NULL );
        //-------------------------------------------------
        xTaskCreate( interrupt_task,
                     "ISR_tsk",
                     INTERRUPT_TASK_STACK_SIZE,
                     NULL,
                     INTERRUPT_TASK_PRIORITY,
                     NULL );
        //-------------------------------------------------
        vTaskStartScheduler();      // Now all the tasks have been started - start the scheduler.
	return 0;                               
}
/*-----------------------------------------------------------*/

static void SetupHardware( void )
{
    /* Enable the peripheral clock. */
    AT91F_PMC_EnablePeriphClock( AT91C_BASE_PMC, 1 << AT91C_ID_PIOA );
    AT91F_PMC_EnablePeriphClock( AT91C_BASE_PMC, 1 << AT91C_ID_EMAC ) ;
    AT91F_PMC_EnablePeriphClock( AT91C_BASE_PMC, 1 << AT91C_ID_PIOB ) ;
    AT91C_BASE_AIC->AIC_EOICR = 0; 
    UART0_init();
}
//--------------------------------------------------------------
void my_task1( void *pvParameters ){
  
    static unsigned long  last_unblocked_time;
    static const portTickType freq = 50 ;   // in ms
    unsigned portLONG ulVar = 1;
    last_unblocked_time = xTaskGetTickCount();
    
    Queue1 = xQueueCreate( 1 , sizeof( unsigned portLONG ) );
    if( Queue1 == 0 ){
        UART0_Printk( "\n\r queue not created ... process suspended ");
        for(;;){           //trapped here if queue creation failed .
           last_unblocked_time = xTaskGetTickCount();
           vTaskDelayUntil( &last_unblocked_time , 1000*60*60 );   
        }
    }
    vQueueAddToRegistry( Queue1 , "scan_QUEUE" );
   
    for( ;; )
     {
             vTaskDelayUntil( &last_unblocked_time, freq );    // Wait for the next cycle.
             last_unblocked_time = xTaskGetTickCount();
            
             if( xQueueSend( Queue1 , ( void * ) &ulVar, ( portTickType ) 0 ) != 1 )
              //          UART0_Printk("\n\rData over written \n\r");
             ulVar++; 
           
     }
}
//--------------------------------------------------------------
void my_task2( void *pvParameters ){
  static unsigned long  last_unblocked_time;
  static portTickType freq = 50 ;   // in ms
  unsigned portLONG temp;
  
  /*
  static i = 0 ;
  char *test ;
  */
   
  /*
  //***************************************************
  char *test = NULL , *p = NULL ;
  static int i = 1,j;
  //***************************************************
  */
  
  
  //----------------------------------------------------------------------------
  static int k = 0 , j = 0 , i = 0 , l = 0;
  char test[30];
  char *t,*p;
  test[0]   = 'a';
  test[1]   = 'b';
  test[2]   = 'c';
  test[3]   = 'd';
  test[4]   = 'e';
  test[5]   = 'f';
  test[6]   = 'g';
  test[7]   = 'h';
  test[8]   = 'i';
  test[9]   = 'j';
  test[10]  = 'k';
  test[11]  = 'l';
  test[12]  = 'm';
  test[13]  = 'n';
  test[14]  = 'o';
  test[15]  = 'p';
  test[16]  = 'q';
  test[17]  = 'r';
  test[18]  = 's';
  test[19]  = 't';
  test[20]  = 'u';
  test[21]  = 'v';
  test[22]  = 'w';
  test[23]  = 'x';
  test[24]  = 'y';
  test[25]  = 'z';
  test[26]  = '1';
  test[27]  = '2';
  test[28]  = '3';
  test[29]  = '4';
  //----------------------------------------------------------------------------
 
  last_unblocked_time = xTaskGetTickCount();
       
    for( ;; )
     {
             xQueueReceive( Queue2, &( freq ), ( portTickType ) 0 ); //for receiving the frequency
             
             vTaskDelayUntil( &last_unblocked_time, freq );    // Wait for the next cycle.
             last_unblocked_time = xTaskGetTickCount();
             
           //  if( xQueueReceive( Queue1, &( temp ), ( portTickType ) 0 ) )  
           //             UART0_Printk("-[%d]" , temp);
             
             
       /*      
            test = (char *)malloc( 6000 * sizeof(char));
            if( test == NULL ){
               UART0_Printk("\n\rAllocation failed ");
            }
            else{
               for( i = 0 ; i < 6000 ; i++ ){
                 test[i] = 'a';
               }
               
               UART0_Printk("\n\r array = [");
               for( i = 0 ; i < 6000 ; i++ ){
                 UART0_Printk( "%c" , test[i] );
               } 
               UART0_Printk("]");
               free(test);
               
            }
      */
             
      /*      
            //***************************************************      
             p = realloc( (void *)test  , i );
             if(p == NULL ){
                  UART0_Printk("\n\rRealloc returned NULL ");
                  while(1){
                    vTaskDelayUntil( &last_unblocked_time, freq );    // Wait for the next cycle.
                    last_unblocked_time = xTaskGetTickCount(); 
                  }
             }else{
                 test = p ;
                 test[i-1] = 'a' ;
                 
                 UART0_Printk("\n\arrya= [");
                 for(j = 0 ; j < i ; j++ )
                   UART0_Printk("%c" , test[j] )   ;
                 UART0_Printk("]");
                 
                 p = NULL ;
                 i++;
                UART0_Printk("\r i = [%d]" , i ); 
             }
              //***************************************************
        */      
             
                       
             //-----------------------------------------------------------------
             for(;l < 1;l++)  {
               
                   UART0_Printk("\n\rthe sizeof test = [%d]" , sizeof(test));
                   i = 0 ;
                   k = 30 ;
                   while(1)  {   
                         vTaskDelayUntil( &last_unblocked_time, freq );    // Wait for the next cycle.
                         last_unblocked_time = xTaskGetTickCount();
                          t= data1;
                          p = realloc( (void *)data1  , k);
                          if(p == NULL ){
                            UART0_Printk("\n\rRealloc returned NULL \n\rHeap full ");
                            //---------------
                             UART0_Printk("\n\rDATA1 array -->[");
                              for(j = 0 ; j < i ; j++ ){
                                  UART0_Printk("%c" , data1[j] );
                              }
                             UART0_Printk("]");
                            //---------------
                            free(data1);
                            while(1){
                              vTaskDelayUntil( &last_unblocked_time, freq );    // Wait for the next cycle.
                              last_unblocked_time = xTaskGetTickCount();
                            
                            }
                          }else{
                            data1 = p ;
                            p = NULL ;
                            t = t + k;
                            
                            for( j = 0 ; j < 30 ; j++ ){
                                data1[ j+i ] = test[j] ;
                            }
                            
                            i = i+30 ; 
                                                  
                           //------------------------------------------------------- 
                           // UART0_Printk("\n\rData is -->[%s] sizeof(data) = [%d]" , data1 , i );
                           //------------------------------------------------------- 
                            
                          //------------------------  
                            
                          UART0_Printk("\n\r t = [0x%X] \n\rSizeof(data1) = [%d]" , t , i );
                                            
                          //------------------------
                          k = k+30;
                          }
                   }
             }
            //------------------------------------------------------------------ 
             
     }
}
//--------------------------------------------------------------
void interrupt_task( void *pvParameters ){

  unsigned long  last_unblocked_time;
  unsigned portLONG freq1 = 50 , freq2 = 100 ;
  static unsigned char temp0;
  const portTickType freq_key = 200 ;   //in ms
  last_unblocked_time = xTaskGetTickCount();
  Queue2 = xQueueCreate( 1 , sizeof( unsigned portLONG ) );
  
  for(;;)
    { //---------------delay needed (otherwise starvation of other tasks)-------
      vTaskDelayUntil( &last_unblocked_time, freq_key );    
      last_unblocked_time = xTaskGetTickCount(); 
      //------------------------------------------------------------------------
      switch(ISR_flag) {                            //here que can be used .
        
             case US0_int :                                                          
                            temp0 = (char)AT91C_BASE_US0->US_RHR;
                            switch( temp0 ){
                                        case '0':
                                               UART0_Printk("%c%c%c%c%c%c" ,0x1B,0x5B,0x32,0x4A,0x1B,0x63 );   //clear screen.
                                               UART0_Printk("\n\n\r\t\t AT91SAM7X512 FREE RTOS V 5.1.0 \n\n");
                                               break ;
                                    
                                        case '1':
                                              xQueueSend( Queue2 , (void *)&freq1 , ( portTickType ) 0 );
                                              break;
                                        case '2':
                                              xQueueSend( Queue2 , (void *)&freq2 , ( portTickType ) 0 );
                                              break;
            
                                        default:
                                              UART0_Printk( "\r\tUART0 RECEIVED -->    [%c]", temp0 );  
                                              break;
                            }
                            ISR_flag = IRQ_NOTUSED ;
                            break;
                           
             case US1_int :                                                          
                            temp0 = (char)AT91C_BASE_US1->US_RHR;
                         // UART1_Printk( "\r\tUART1 RECEIVED -->    [%c]", temp0 );        
                            ISR_flag = IRQ_NOTUSED ;                                        
                            break;        
            default:
                          break;
      }
    }//end of while
  
}
//------------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -