Subversion Repositories HomeAutomation

Rev

Rev 2238 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1.  
  2. #include "tst_Benchmark.h"
  3.  
  4.  
  5. /*
  6. Benchmark test usage of timer1
  7.  
  8. CanSend
  9. Uses Timer1 as free running timer, only reads counter value
  10.  
  11. CpuTime
  12. Uses Timer1 as free running timer, only reads counter value
  13.  
  14.  
  15. */
  16.  
  17.  
  18. #if tst_Benchmark_CanSend_TestActive == 1
  19. uint16_t tst_Benchmark_CanSend_Min = 0xffff;
  20. uint16_t tst_Benchmark_CanSend_Max = 0x0000;
  21. uint16_t tst_Benchmark_CanSend_Prev = 0xffff;
  22. StdCan_Msg_t tst_Benchmark_CanSend_txMsg;
  23. #endif
  24.  
  25. #if tst_Benchmark_CpuTime_TestActive == 1
  26. uint16_t tst_Benchmark_CpuTime_Min = 0xffff;
  27. uint16_t tst_Benchmark_CpuTime_Max = 0x0000;
  28. uint32_t tst_Benchmark_CpuTime_Avg = 0x0000;
  29. //uint16_t tst_Benchmark_CpuTime_Avg_Cnt = 0;
  30. StdCan_Msg_t tst_Benchmark_CpuTime_txMsg;
  31. uint16_t tst_Benchmark_CpuTime_timer_val;
  32. #endif
  33.  
  34.  
  35. void tst_Benchmark_Init(void)
  36. {
  37.     /* Set up benchmark timer (timer1, 16bit) */
  38.     //Normal port operation, Normal mode
  39.     TCCR1A = ((0<<COM1A1)|(0<<COM1A0)|(0<<COM1B1)|(0<<COM1B0)|(0<<WGM11)|(0<<WGM10));
  40.     TCCR1B = ((0<<ICNC1)|(0<<ICES1)|(0<<WGM12)|(0<<WGM13));
  41.    
  42.     /* Start timer */
  43.     //prescaler /8
  44.     TCCR1B |= ((0<<CS12)|(1<<CS11)|(0<<CS10));
  45.  
  46. #if tst_Benchmark_CanSend_TestActive == 1
  47.     StdCan_Set_class(tst_Benchmark_CanSend_txMsg.Header, CAN_MODULE_CLASS_SNS);
  48.     StdCan_Set_direction(tst_Benchmark_CanSend_txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  49.     tst_Benchmark_CanSend_txMsg.Header.ModuleType = CAN_MODULE_TYPE_TST_DEBUG;
  50.     tst_Benchmark_CanSend_txMsg.Header.ModuleId = tst_Benchmark_ID;
  51.     tst_Benchmark_CanSend_txMsg.Header.Command = CAN_MODULE_CMD_DEBUG_BENCHMARK;
  52.     tst_Benchmark_CanSend_txMsg.Length = 8;
  53.  
  54.     Timer_SetTimeout(tst_Benchmark_CanSend_TIMER, tst_Benchmark_CanSend_PERIOD_MS , TimerTypeFreeRunning, 0);
  55. #endif
  56.  
  57. #if tst_Benchmark_CpuTime_TestActive == 1
  58.     StdCan_Set_class(tst_Benchmark_CpuTime_txMsg.Header, CAN_MODULE_CLASS_SNS);
  59.     StdCan_Set_direction(tst_Benchmark_CpuTime_txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  60.     tst_Benchmark_CpuTime_txMsg.Header.ModuleType = CAN_MODULE_TYPE_TST_DEBUG;
  61.     tst_Benchmark_CpuTime_txMsg.Header.ModuleId = tst_Benchmark_ID;
  62.     tst_Benchmark_CpuTime_txMsg.Header.Command = CAN_MODULE_CMD_DEBUG_BENCHMARK;
  63.     tst_Benchmark_CpuTime_txMsg.Length = 8;
  64.  
  65.     Timer_SetTimeout(tst_Benchmark_CpuTime_SEND_TIMER, tst_Benchmark_CpuTime_SEND_PERIOD_MS , TimerTypeFreeRunning, 0);
  66. #endif
  67. }
  68.  
  69. void tst_Benchmark_Process(void)
  70. {
  71. #if tst_Benchmark_CanSend_TestActive == 1
  72.     if (Timer_Expired(tst_Benchmark_CanSend_TIMER))
  73.     {
  74.         uint16_t timer_val;
  75.         uint16_t timer_val_new;
  76.        
  77.         for (uint8_t i=0; i < tst_Benchmark_CanSend_NumTx; i++)
  78.         {
  79.             tst_Benchmark_CanSend_txMsg.Data[0] = 0; // Implement avarage? Write test number?
  80.             tst_Benchmark_CanSend_txMsg.Data[1] = 0;
  81.             tst_Benchmark_CanSend_txMsg.Data[2] = (tst_Benchmark_CanSend_Prev>>8)&0xff;
  82.             tst_Benchmark_CanSend_txMsg.Data[3] = tst_Benchmark_CanSend_Prev&0xff;
  83.             tst_Benchmark_CanSend_txMsg.Data[4] = (tst_Benchmark_CanSend_Min>>8)&0xff;
  84.             tst_Benchmark_CanSend_txMsg.Data[5] = tst_Benchmark_CanSend_Min&0xff;
  85.             tst_Benchmark_CanSend_txMsg.Data[6] = (tst_Benchmark_CanSend_Max>>8)&0xff;
  86.             tst_Benchmark_CanSend_txMsg.Data[7] = tst_Benchmark_CanSend_Max&0xff;
  87.  
  88.             timer_val = TCNT1;
  89.             /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
  90.             while (StdCan_Put(&tst_Benchmark_CanSend_txMsg) != StdCan_Ret_OK) {}
  91.             timer_val_new = TCNT1;
  92.            
  93.             tst_Benchmark_CanSend_Prev = timer_val_new - timer_val;
  94.             if (tst_Benchmark_CanSend_Min > tst_Benchmark_CanSend_Prev)
  95.             {
  96.                 tst_Benchmark_CanSend_Min = tst_Benchmark_CanSend_Prev;
  97.             }
  98.             if (tst_Benchmark_CanSend_Max < tst_Benchmark_CanSend_Prev)
  99.             {
  100.                 tst_Benchmark_CanSend_Max = tst_Benchmark_CanSend_Prev;
  101.             }
  102.         }
  103.        
  104.     }
  105. #endif
  106.  
  107. #if tst_Benchmark_CpuTime_TestActive == 1
  108.     uint16_t timer_val_new = TCNT1;
  109.  
  110.     uint16_t timeSpent = timer_val_new - tst_Benchmark_CpuTime_timer_val;
  111.     if (tst_Benchmark_CpuTime_Min > timeSpent)
  112.     {
  113.         tst_Benchmark_CpuTime_Min = timeSpent;
  114.     }
  115.     if (tst_Benchmark_CpuTime_Max < timeSpent)
  116.     {
  117.         tst_Benchmark_CpuTime_Max = timeSpent;
  118.     }
  119.    
  120.     uint64_t sum = tst_Benchmark_CpuTime_Avg * 127 + (timeSpent<<8);
  121.     tst_Benchmark_CpuTime_Avg = sum / 128;
  122.  
  123.     if (Timer_Expired(tst_Benchmark_CpuTime_SEND_TIMER))
  124.     {
  125.         tst_Benchmark_CpuTime_txMsg.Data[0] = 0; // Write test number?
  126.         tst_Benchmark_CpuTime_txMsg.Data[1] = 0;
  127.         tst_Benchmark_CpuTime_txMsg.Data[2] = (tst_Benchmark_CpuTime_Avg>>16)&0xff;
  128.         tst_Benchmark_CpuTime_txMsg.Data[3] = (tst_Benchmark_CpuTime_Avg>>8)&0xff;
  129.         tst_Benchmark_CpuTime_txMsg.Data[4] = (tst_Benchmark_CpuTime_Min>>8)&0xff;
  130.         tst_Benchmark_CpuTime_txMsg.Data[5] = tst_Benchmark_CpuTime_Min&0xff;
  131.         tst_Benchmark_CpuTime_txMsg.Data[6] = (tst_Benchmark_CpuTime_Max>>8)&0xff;
  132.         tst_Benchmark_CpuTime_txMsg.Data[7] = tst_Benchmark_CpuTime_Max&0xff;
  133.  
  134.         /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
  135.         while (StdCan_Put(&tst_Benchmark_CpuTime_txMsg) != StdCan_Ret_OK) {}
  136.     }
  137.  
  138.     tst_Benchmark_CpuTime_timer_val = TCNT1;
  139. #endif
  140. }
  141.  
  142. void tst_Benchmark_HandleMessage(StdCan_Msg_t *rxMsg)
  143. {
  144. /*  if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_xyz && ///TODO: Change this to the actual class type
  145.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
  146.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_xyz && ///TODO: Change this to the actual module type
  147.         rxMsg->Header.ModuleId == tst_Benchmark_ID)
  148.     {
  149.         switch (rxMsg->Header.Command)
  150.         {
  151.         case CAN_CMD_MODULE_DUMMY:
  152.         ///TODO: Do something dummy
  153.         break;
  154.         }
  155.     }
  156. */
  157. }
  158.  
  159. void tst_Benchmark_List(uint8_t ModuleSequenceNumber)
  160. {
  161.     StdCan_Msg_t txMsg;
  162.  
  163.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_TST);
  164.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  165.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_TST_DEBUG;
  166.     txMsg.Header.ModuleId = tst_Benchmark_ID;
  167.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  168.     txMsg.Length = 6;
  169.  
  170.     uint32_t HwId=BIOS_GetHwId();
  171.     txMsg.Data[0] = HwId&0xff;
  172.     txMsg.Data[1] = (HwId>>8)&0xff;
  173.     txMsg.Data[2] = (HwId>>16)&0xff;
  174.     txMsg.Data[3] = (HwId>>24)&0xff;
  175.  
  176.     txMsg.Data[4] = NUMBER_OF_MODULES;
  177.     txMsg.Data[5] = ModuleSequenceNumber;
  178.  
  179.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  180. }
  181.