Subversion Repositories HomeAutomation

Rev

Rev 2234 | Go to most recent revision | 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. uint16_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.     uint32_t sum = tst_Benchmark_CpuTime_Avg * tst_Benchmark_CpuTime_Avg_Cnt + timeSpent;
  121.     if (tst_Benchmark_CpuTime_Avg_Cnt < 0xffff)
  122.     {
  123.         tst_Benchmark_CpuTime_Avg_Cnt++;
  124.     }
  125.     tst_Benchmark_CpuTime_Avg = sum / tst_Benchmark_CpuTime_Avg_Cnt;
  126.  
  127.     if (Timer_Expired(tst_Benchmark_CpuTime_SEND_TIMER))
  128.     {
  129.         tst_Benchmark_CpuTime_txMsg.Data[0] = 0; // Write test number?
  130.         tst_Benchmark_CpuTime_txMsg.Data[1] = 0;
  131.         tst_Benchmark_CpuTime_txMsg.Data[2] = (tst_Benchmark_CpuTime_Avg>>8)&0xff;
  132.         tst_Benchmark_CpuTime_txMsg.Data[3] = tst_Benchmark_CpuTime_Avg&0xff;
  133.         tst_Benchmark_CpuTime_txMsg.Data[4] = (tst_Benchmark_CpuTime_Min>>8)&0xff;
  134.         tst_Benchmark_CpuTime_txMsg.Data[5] = tst_Benchmark_CpuTime_Min&0xff;
  135.         tst_Benchmark_CpuTime_txMsg.Data[6] = (tst_Benchmark_CpuTime_Max>>8)&0xff;
  136.         tst_Benchmark_CpuTime_txMsg.Data[7] = tst_Benchmark_CpuTime_Max&0xff;
  137.  
  138.         /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
  139.         while (StdCan_Put(&tst_Benchmark_CpuTime_txMsg) != StdCan_Ret_OK) {}
  140.     }
  141.  
  142.     tst_Benchmark_CpuTime_timer_val = TCNT1;
  143. #endif
  144. }
  145.  
  146. void tst_Benchmark_HandleMessage(StdCan_Msg_t *rxMsg)
  147. {
  148. /*  if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_xyz && ///TODO: Change this to the actual class type
  149.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
  150.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_xyz && ///TODO: Change this to the actual module type
  151.         rxMsg->Header.ModuleId == tst_Benchmark_ID)
  152.     {
  153.         switch (rxMsg->Header.Command)
  154.         {
  155.         case CAN_CMD_MODULE_DUMMY:
  156.         ///TODO: Do something dummy
  157.         break;
  158.         }
  159.     }
  160. */
  161. }
  162.  
  163. void tst_Benchmark_List(uint8_t ModuleSequenceNumber)
  164. {
  165.     StdCan_Msg_t txMsg;
  166.  
  167.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_TST);
  168.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  169.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_TST_DEBUG;
  170.     txMsg.Header.ModuleId = tst_Benchmark_ID;
  171.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  172.     txMsg.Length = 6;
  173.  
  174.     uint32_t HwId=BIOS_GetHwId();
  175.     txMsg.Data[0] = HwId&0xff;
  176.     txMsg.Data[1] = (HwId>>8)&0xff;
  177.     txMsg.Data[2] = (HwId>>16)&0xff;
  178.     txMsg.Data[3] = (HwId>>24)&0xff;
  179.  
  180.     txMsg.Data[4] = NUMBER_OF_MODULES;
  181.     txMsg.Data[5] = ModuleSequenceNumber;
  182.  
  183.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  184. }
  185.