Subversion Repositories HomeAutomation

Rev

Rev 2238 | 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.  
  12.  
  13. */
  14.  
  15.  
  16. #if tst_Benchmark_CanSend_TestActive == 1
  17. uint16_t tst_Benchmark_CanSend_Min = 0xffff;
  18. uint16_t tst_Benchmark_CanSend_Max = 0x0000;
  19. uint16_t tst_Benchmark_CanSend_Prev = 0xffff;
  20. StdCan_Msg_t tst_Benchmark_CanSend_txMsg;
  21. #endif
  22.  
  23. void tst_Benchmark_Init(void)
  24. {
  25.     /* Set up benchmark timer (timer1, 16bit) */
  26.     //Normal port operation, Normal mode
  27.     TCCR1A = ((0<<COM1A1)|(0<<COM1A0)|(0<<COM1B1)|(0<<COM1B0)|(0<<WGM11)|(0<<WGM10));
  28.     TCCR1B = ((0<<ICNC1)|(0<<ICES1)|(0<<WGM12)|(0<<WGM13));
  29.    
  30.     /* Start timer */
  31.     //prescaler /8
  32.     TCCR1B |= ((0<<CS12)|(1<<CS11)|(0<<CS10));
  33.  
  34. #if tst_Benchmark_CanSend_TestActive == 1
  35.     StdCan_Set_class(tst_Benchmark_CanSend_txMsg.Header, CAN_MODULE_CLASS_SNS);
  36.     StdCan_Set_direction(tst_Benchmark_CanSend_txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  37.     tst_Benchmark_CanSend_txMsg.Header.ModuleType = CAN_MODULE_TYPE_TST_DEBUG;
  38.     tst_Benchmark_CanSend_txMsg.Header.ModuleId = tst_Benchmark_ID;
  39.     tst_Benchmark_CanSend_txMsg.Header.Command = CAN_MODULE_CMD_DEBUG_BENCHMARK;
  40.     tst_Benchmark_CanSend_txMsg.Length = 8;
  41.  
  42.     Timer_SetTimeout(tst_Benchmark_CanSend_TIMER, tst_Benchmark_CanSend_PERIOD_MS , TimerTypeFreeRunning, 0);
  43. #endif
  44. }
  45.  
  46. void tst_Benchmark_Process(void)
  47. {
  48. #if tst_Benchmark_CanSend_TestActive == 1
  49.     if (Timer_Expired(tst_Benchmark_CanSend_TIMER))
  50.     {
  51.         uint16_t timer_val;
  52.         uint16_t timer_val_new;
  53.        
  54.         for (uint8_t i=0; i < tst_Benchmark_CanSend_NumTx; i++)
  55.         {
  56.             tst_Benchmark_CanSend_txMsg.Data[0] = (tst_Benchmark_CanSend_Prev>>8)&0xff;
  57.             tst_Benchmark_CanSend_txMsg.Data[1] = tst_Benchmark_CanSend_Prev&0xff;
  58.             tst_Benchmark_CanSend_txMsg.Data[2] = 0; // Implement avarage?
  59.             tst_Benchmark_CanSend_txMsg.Data[3] = 0;
  60.             tst_Benchmark_CanSend_txMsg.Data[4] = (tst_Benchmark_CanSend_Min>>8)&0xff;
  61.             tst_Benchmark_CanSend_txMsg.Data[5] = tst_Benchmark_CanSend_Min&0xff;
  62.             tst_Benchmark_CanSend_txMsg.Data[6] = (tst_Benchmark_CanSend_Max>>8)&0xff;
  63.             tst_Benchmark_CanSend_txMsg.Data[7] = tst_Benchmark_CanSend_Max&0xff;
  64.  
  65.             timer_val = TCNT1;
  66.             /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
  67.             while (StdCan_Put(&tst_Benchmark_CanSend_txMsg) != StdCan_Ret_OK) {}
  68.             timer_val_new = TCNT1;
  69.            
  70.             tst_Benchmark_CanSend_Prev = timer_val_new - timer_val;
  71.             if (tst_Benchmark_CanSend_Min > tst_Benchmark_CanSend_Prev)
  72.             {
  73.                 tst_Benchmark_CanSend_Min = tst_Benchmark_CanSend_Prev;
  74.             }
  75.             if (tst_Benchmark_CanSend_Max < tst_Benchmark_CanSend_Prev)
  76.             {
  77.                 tst_Benchmark_CanSend_Max = tst_Benchmark_CanSend_Prev;
  78.             }
  79.         }
  80.        
  81.     }
  82. #endif
  83. }
  84.  
  85. void tst_Benchmark_HandleMessage(StdCan_Msg_t *rxMsg)
  86. {
  87. /*  if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_xyz && ///TODO: Change this to the actual class type
  88.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
  89.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_xyz && ///TODO: Change this to the actual module type
  90.         rxMsg->Header.ModuleId == tst_Benchmark_ID)
  91.     {
  92.         switch (rxMsg->Header.Command)
  93.         {
  94.         case CAN_CMD_MODULE_DUMMY:
  95.         ///TODO: Do something dummy
  96.         break;
  97.         }
  98.     }
  99. */
  100. }
  101.  
  102. void tst_Benchmark_List(uint8_t ModuleSequenceNumber)
  103. {
  104.     StdCan_Msg_t txMsg;
  105.  
  106.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_TST);
  107.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  108.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_TST_DEBUG;
  109.     txMsg.Header.ModuleId = tst_Benchmark_ID;
  110.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  111.     txMsg.Length = 6;
  112.  
  113.     uint32_t HwId=BIOS_GetHwId();
  114.     txMsg.Data[0] = HwId&0xff;
  115.     txMsg.Data[1] = (HwId>>8)&0xff;
  116.     txMsg.Data[2] = (HwId>>16)&0xff;
  117.     txMsg.Data[3] = (HwId>>24)&0xff;
  118.  
  119.     txMsg.Data[4] = NUMBER_OF_MODULES;
  120.     txMsg.Data[5] = ModuleSequenceNumber;
  121.  
  122.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  123. }
  124.