ธันวาคม 11, 2018, 04:03:12 am *
ยินดีต้อนรับคุณ, บุคคลทั่วไป กรุณา เข้าสู่ระบบ หรือ ลงทะเบียน
ส่งอีเมล์ยืนยันการใช้งาน?

เข้าสู่ระบบด้วยชื่อผู้ใช้ รหัสผ่าน และระยะเวลาในเซสชั่น
   หน้าแรก   ช่วยเหลือ เข้าสู่ระบบ สมัครสมาชิก  
หน้า: [1]   ลงล่าง
  พิมพ์  
ผู้เขียน หัวข้อ: RTX-ARM & LabVIEW Full Test (Protocol Design for Embedded and HMI Systems)  (อ่าน 6910 ครั้ง)
0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้
ShadowMan
Administrator
Hero Member
*****
ออฟไลน์ ออฟไลน์

เพศ: ชาย
กระทู้: 8272


ShadowWares


| |
« เมื่อ: มิถุนายน 01, 2013, 05:08:25 pm »

RTX-ARM & LabVIEW Full Test (Protocol Design for Embedded and HMI Systems)

KeilARM- Simulator:


LabVIEW Applications:


Code: (c)
/************************************************************************
* FILE NAME:   RTX_LabVIW_01.c                                 *
* DESCRIPTION:   Basic Protocol Design for Embedded and HMI Systems      *
*                                                      *
* HARDWARE:                                                *
*            P0.16 <---> EINT0                              *
*            P0.14 <---> EINT1                              *
*            P0.15 <---> EINT2                              *
*            P0.20 <---> EINT3                              *
*************************************************************************
* AUTHOR:                                                *
*      Dr.Santi Nuratch                                    *
*       Department of Control System and Instrumentation Engineering,     *
*      King Mongkut’s University of Technology Thonburi (KMUTT)      *
************************************************************************/

#include "./INC_ARM7_Library/INC_ARM7_Library.h" // BPS

#define WAIT_FOREVER 0xFFFF   // Used for Wait_x/Delay functions

// Function prototypes
///////////////////////////////////////////////////////////////
void RxFrame_Callback_UART_ISR0(void *frameData);   // Request frame detected Callback function of UART0
void RxFrame_Callback_UART_ISR1(void *frameData);   // Request frame detected Callback function of UART1

void TxFrame_Callback_UART_ISR0(void *frameData);   // Transmittion complete Callback function of UART0
void TxFrame_Callback_UART_ISR1(void *frameData);   // Transmittion complete Callback function of UART1

void Timer0_Callback_ISR(void * pTickCount);      // Timer0 on match interrupt Callback function

void ExInt_Callback_ISR0(void * pExtId);         // External ISR0 callback function
void ExInt_Callback_ISR1(void * pExtId);         // External ISR1 callback function
void ExInt_Callback_ISR2(void * pExtId);         // External ISR2 callback function
void ExInt_Callback_ISR3(void * pExtId);         // External ISR3 callback function


//---[#1]---------------------------------------------------------------
__task void Init(void);      // Initializes/Creates other taks
__task void Time(void);      // System time
__task void UartTx0(void);   // UART0 Transmitter
__task void UartTx1(void);   // UART1 Rransmitter
__task void UartRx0(void);   // UART0 Receiver
__task void UartRx1(void);   // UART1 Receiver

__task void ExtISRs(void);   // for ecternal ISRs

OS_TID TIdInit, TIdTime, TIdUartTx0, TIdUartRx0, TIdUartRx1, TIdExtISRs;

#define USER_NUM_TASK 10      // Must be <= OS_TASKCNT, see in RTX_Config.c
OS_TID TaskId[USER_NUM_TASK];   // TId array, used to save TId of created tasks
U8 TaskIdx = 0;               // TId index, see in Init Task

SYS_CLOCK __Clk;            // System clock structure, TPS HH MM SS TT
OS_MUT  mut_Clk;            // for a shared variable, __Clk

OS_MUT  mut_Spf;            // for sprintf()
OS_MUT  mut_Uwr;            // for UART_Write()
OS_MUT  mut_Lwr;            // for Write-buffer
OS_MUT  mut_Lrd;            // for Read-buffer
   
OS_SEM  sem_Tx0, sem_Tx1;      // for transmitter 0, 1

#define MEM_BLOCK_COUNT (int)40   // # of blocks (Mem_Pool)
#define MEM_BLOCK_SIZE  (int)64   // # of bytes in eac block (Mem_Pool)

#define MBX_BLOCK_COUNT   MEM_BLOCK_COUNT/4   // # of blocks (MBX), we have 4 mail boxes   
#define MBX_BLOCK_SIZE   MEM_BLOCK_SIZE      // # of bytes (MBX)   

_declare_box(Mem_Pool, MEM_BLOCK_SIZE, MEM_BLOCK_COUNT);

os_mbx_declare (mbx_Rx0,  MBX_BLOCK_COUNT); // holds pointers of string
os_mbx_declare (mbx_Rx1,  MBX_BLOCK_COUNT); // holds pointers of string
os_mbx_declare (mbx_Tx0,  MBX_BLOCK_COUNT); // holds pointers of string
os_mbx_declare (mbx_Tx1,  MBX_BLOCK_COUNT); // holds pointers of string

//
unsigned int LabVIEW_Wr[4]={0,0,0,0};
unsigned int LabVIEW_Rd[4]={124,456,789,246};

int Write_Data(unsigned int idx, unsigned int *data){
   unsigned int prev;
   os_mut_wait(mut_Lwr, WAIT_FOREVER);
   if(idx > 3){
      os_mut_release(mut_Lwr);
      return(0);
   }
   prev = LabVIEW_Wr[idx];
   LabVIEW_Wr[idx] = *data;
   *data = prev;
   os_mut_release(mut_Lwr);
   return(1);
}

int Read_Data(int idx, unsigned int *data){
   os_mut_wait(mut_Lrd, WAIT_FOREVER);
   if(idx > 3){
      os_mut_release(mut_Lrd);
      return(0);
   }
   *data = LabVIEW_Rd[idx];
   os_mut_release(mut_Lrd);
   return(1);
}   

__task void Time(void){
   SYS_CLOCK clock;
   os_itv_set(1);             // 1:=10msec @ 10 msec/tick   
   Sys_Clock_Init(&__Clk, 100);
   for(;;){
      os_itv_wait();
      
      //--- Read shared data
      os_mut_wait(mut_Clk, WAIT_FOREVER);
      clock = __Clk;
      os_mut_release(mut_Clk);
      
      //--- clock tick
      Sys_Clock_Tick(&clock);  // Called by this task only   
      
      //--- Write shared data
      os_mut_wait(mut_Clk, WAIT_FOREVER);
      __Clk = clock;
      os_mut_release(mut_Clk);
   }
}

__task void UartRx0(void){
   char *pframe, *pstring;
   SYS_CLOCK clock;
   char tbuff[32];
   unsigned int target, value;
   for(;;){
      os_mbx_wait (mbx_Rx0, (void **)&pframe, WAIT_FOREVER);
      pstring  = (char*)_alloc_box(Mem_Pool);
      if(pstring == NULL){
          // Error   
      }
      else{
         if( Protocol_Decode(pframe, &target, &value) == PROTOCOL_ERR){
            // Error   
            _free_box(Mem_Pool, pframe);
         }
         else{
            switch(target){
               case 0: Write_Data(0, &value); break;
               case 1: Write_Data(1, &value); break;
               case 2: Write_Data(2, &value); break;
               case 3: Write_Data(3, &value); break;
               
               case 4: Read_Data(0, &value);  break;
               case 5: Read_Data(1, &value);  break;
               case 6: Read_Data(2, &value);  break;
               case 7: Read_Data(3, &value);  break;
               
               
               case 8: break;
               case 9: break;
            }

            os_mut_wait(mut_Clk, WAIT_FOREVER);
            clock = __Clk;   
            os_mut_release(mut_Clk);
            
            os_mut_wait(mut_Spf, WAIT_FOREVER);   // shared function
            Sys_Clock_String(&clock, tbuff);   // This function uses sprintf()
            sprintf(pstring, ":U0 %d %.5d %s\r\n", target, value, tbuff);
            os_mut_release(mut_Spf);
      
            os_mbx_send (mbx_Tx0, pstring, WAIT_FOREVER);
         }
      }
   }
}

__task void UartRx1(void){
   char *pframe, *pstring;
   SYS_CLOCK clock;
   char tbuff[64];
   unsigned int target, value;
   for(;;){
      os_mbx_wait (mbx_Rx1, (void **)&pframe, WAIT_FOREVER);
      pstring  = (char*)_alloc_box(Mem_Pool);
      if(pstring == NULL){
          // Error   
      }
      else{
         if( Protocol_Decode(pframe, &target, &value) == PROTOCOL_ERR){
            // Error   
            _free_box(Mem_Pool, pframe);
         }
         else{
            switch(target){
               case 0: Write_Data(0, &value); break;
               case 1: Write_Data(1, &value); break;
               case 2: Write_Data(2, &value); break;
               case 3: Write_Data(3, &value); break;
               
               case 4: Read_Data(0, &value);  break;
               case 5: Read_Data(1, &value);  break;
               case 6: Read_Data(2, &value);  break;
               case 7: Read_Data(3, &value);  break;
               
               case 8: break; // Reserverd
               case 9: break;
            }

            os_mut_wait(mut_Clk, WAIT_FOREVER);
            clock = __Clk;   
            os_mut_release(mut_Clk);
            
            os_mut_wait(mut_Spf, WAIT_FOREVER);   // shared function, sprintf()
            Sys_Clock_String(&clock, tbuff);   // This function uses sprintf()
            sprintf(pstring, ":U1 %d %.5d %s\r\n", target, value, tbuff);
            os_mut_release(mut_Spf);
      
            os_mbx_send (mbx_Tx1, pstring, WAIT_FOREVER);
         }
      }
   }
}

__task void UartTx0(void){
   char *pframe; int ret;
   for(;;){
      os_sem_wait (sem_Tx0, WAIT_FOREVER);    
      os_mbx_wait (mbx_Tx0, (void **)&pframe, WAIT_FOREVER);
      
      os_mut_wait (mut_Uwr, WAIT_FOREVER ); // UART_Write() is used in both UartTx0 & UartTx1
      ret = UART_Write(UART_ID0, pframe);
      if( ret == TRANSMITT_ERR ){
         // No free space for this frame. It can't be sent!!
         // ...
         os_sem_send (sem_Tx0);
      }
      else{
         // the sem_Tx0 will be released in the tx complete callack by ir_sem_send(sem_Tx0)
      }
      os_mut_release (mut_Uwr);
      
      _free_box (Mem_Pool, pframe);         // free memory, allocated in the Task_Process_Rx0()
   }
}

__task void ExtISRs(void){
   int ev;
   while(1){
      os_evt_wait_or(0x000F, WAIT_FOREVER);
      ev = os_evt_get();
      
      if(ev & 1){
         os_mut_wait(mut_Lrd, WAIT_FOREVER);
         LabVIEW_Rd[0]++;
         os_mut_release(mut_Lrd);
      }
      
      if(ev & 2){
         os_mut_wait(mut_Lrd, WAIT_FOREVER);
         LabVIEW_Rd[1]++;
         os_mut_release(mut_Lrd);
      }
      
      if(ev & 4){
         os_mut_wait(mut_Lrd, WAIT_FOREVER);
         LabVIEW_Rd[2]++;
         os_mut_release(mut_Lrd);
      }
      
      if(ev & 8){
         os_mut_wait(mut_Lrd, WAIT_FOREVER);
         LabVIEW_Rd[3]++;
         os_mut_release(mut_Lrd);
      }
   }
}

__task void UartTx1(void){
   char *pframe; int ret;
   for(;;){
      os_sem_wait (sem_Tx1, WAIT_FOREVER);
      os_mbx_wait (mbx_Tx1, (void **)&pframe, WAIT_FOREVER);
      
      os_mut_wait (mut_Uwr, WAIT_FOREVER ); // UART_Write() is used in both UartTx0 & UartTx1
      
      ret = UART_Write(UART_ID1, pframe);
      if( ret == TRANSMITT_ERR ){
         // No free space for this frame. It can't be sent!!
         // ...
         os_sem_send (sem_Tx1);
      }
      else{
         // the sem_Tx1 will be released in the tx complete callack by ir_sem_send(sem_Tx1)
      }
      os_mut_release (mut_Uwr);
      
      _free_box (Mem_Pool, pframe);         // free memory, allocated in the Task_Process_Rx1()
   }
}

// Called by RX-ISR of UART #0, when a frame of data is received
void RxFrame_Callback_UART_ISR0(void *frameData){
   char *ptr = (char *)frameData;
   isr_mbx_send(mbx_Rx0, ptr);
   
}

// Called by RX-ISR of UART #1, when a frame of data is received
void RxFrame_Callback_UART_ISR1(void *frameData){
   char *ptr = (char *)frameData;
   isr_mbx_send(mbx_Rx1,(void *)ptr);
}

// Called by TX-ISR of UART #0, when a frame of data is completely transmitted
void TxFrame_Callback_UART_ISR0(void *frameData){
   isr_sem_send(sem_Tx0); // This function is called from UART0 TX-ISR, isr_xxx() must be used
}

// Called by TX-ISR of UART #1, when a frame of data is completely transmitted
void TxFrame_Callback_UART_ISR1(void *frameData){
   isr_sem_send(sem_Tx1);
}

// Called by ISR of Timer #0 every period of specific time
void Timer0_Callback_ISR(void * pTickCount){
   IO0PIN ^= 0x00000004;   
}

void ExInt_Callback_ISR0(void * pExtId){
   isr_evt_set(0x0001, TIdExtISRs);
}

void ExInt_Callback_ISR1(void * pExtId){
   isr_evt_set(0x0002, TIdExtISRs);
}

void ExInt_Callback_ISR2(void * pExtId){
   isr_evt_set(0x0004, TIdExtISRs);
}   

void ExInt_Callback_ISR3(void * pExtId){
   isr_evt_set(0x0008, TIdExtISRs);
}

__task void Init(void) {
   
   os_mut_init (mut_Clk);
   os_mut_init (mut_Spf);
   os_mut_init (mut_Uwr);
   os_mut_init (mut_Lwr);
   os_mut_init (mut_Lrd);
   
   os_sem_init (sem_Tx1, 1);
   os_sem_init (sem_Tx0, 1);

   _init_box (Mem_Pool, sizeof(Mem_Pool), MEM_BLOCK_SIZE);
   
   os_mbx_init (mbx_Rx0, sizeof(mbx_Rx0));
   os_mbx_init (mbx_Rx1, sizeof(mbx_Rx1));
   
   os_mbx_init (mbx_Tx0, sizeof(mbx_Tx0));
   os_mbx_init (mbx_Tx1, sizeof(mbx_Tx1));
   
   // TIdInit, TIdTime, TIdUartTx0, TIdUartRx0, TIdUartRx1, TIdExtISRs;
   TIdTime  = os_tsk_create(Time, 1);
   TIdUartTx0  = os_tsk_create(UartTx0, 3);
   TIdUartRx0 = os_tsk_create(UartRx0,  3);
   
   TIdUartRx1  = os_tsk_create(UartTx1, 3);
   TIdUartRx1  = os_tsk_create(UartRx1, 3);
   
   TIdExtISRs  = os_tsk_create(ExtISRs, 3);

   os_tsk_delete_self();   
}

int main(void){
   IO0DIR |= 0x0000000F;
   INC_ARM7_Init();
   os_sys_init_prio(Init, 254);
}


สร้าง Application ซับซ้อนสูงได้ในพริบตาด้วย RTX
บันทึกการเข้า

By SDW: Do No Wrong Is Do Nothing
          If you want to increase your success rate, double your failure rate
chirawat
Jr. Member
**
ออฟไลน์ ออฟไลน์

กระทู้: 54


| |
« ตอบ #1 เมื่อ: มิถุนายน 02, 2013, 07:16:32 pm »

 Shocked Shocked Shocked
ได้แค่มอง บอกผมหน่อยครับว่านี่คืออะไร
บันทึกการเข้า
ShadowMan
Administrator
Hero Member
*****
ออฟไลน์ ออฟไลน์

เพศ: ชาย
กระทู้: 8272


ShadowWares


| |
« ตอบ #2 เมื่อ: มิถุนายน 02, 2013, 07:49:33 pm »

เริ่มจากที่นี่ http://www.keil.com/support/man/docs/rlarm/rlarm_library.htm
แล้วคลิก อ่านไปเรื่อยๆ
บันทึกการเข้า

By SDW: Do No Wrong Is Do Nothing
          If you want to increase your success rate, double your failure rate
หน้า: [1]   ขึ้นบน
  พิมพ์  
 
กระโดดไป: