Subversion Repositories HomeAutomation

Rev

Rev 1360 | Rev 1573 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1.  
  2. #include "sns_power.h"
  3.  
  4. static uint32_t volatile PreviusTimerValue, lastMeasurment;
  5.  
  6. #ifdef sns_power_10000_PULSES_PER_KWH
  7. static uint8_t volatile tmpCounter=0;
  8. #endif
  9. static uint8_t volatile StoreInEEPROM = 0;
  10. static uint8_t sns_power_ReportInterval = (uint8_t)sns_power_SEND_PERIOD;
  11. static uint16_t volatile MeasurmentBuffer[32]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  12. static uint8_t volatile MeasurmentBufferPointer=0;
  13. static uint32_t volatile EnergyCounter=0;
  14. #if sns_power_SEND_1_MIN_AVG == 1
  15. static uint16_t volatile avgCounter = 0;
  16. #endif
  17.  
  18. #ifdef sns_power_USEEEPROM
  19. #include "sns_power_eeprom.h"
  20. struct eeprom_sns_power EEMEM eeprom_sns_power =
  21. {
  22.     {
  23.         ///TODO: Define initialization values on the EEPROM variables here, this will generate a *.eep file that can be used to store this values to the node, can in future be done with a EEPROM module and the make-scrips. Write the values in the exact same order as the struct is defined in the *.h file.
  24.         (uint8_t)sns_power_SEND_PERIOD, // reportInterval
  25.         0,  // EnergyCounterUpper
  26.         0,  // EnergyCounterLower
  27.     },
  28.     0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
  29. };
  30. #endif
  31.  
  32. #if sns_power_SEND_1_MIN_AVG == 1
  33. void sns_power_timer_callback(uint8_t timer)
  34. {
  35.     StdCan_Msg_t txMsg;
  36.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS); ///TODO: Change this to the actual class type
  37.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  38.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_POWER; ///TODO: Change this to the actual module type
  39.     txMsg.Header.ModuleId = sns_power_ID;
  40.     txMsg.Header.Command = CAN_MODULE_CMD_POWER_AVGPOWER;
  41.     txMsg.Length = 2;
  42.     cli();
  43.     txMsg.Data[0] = (uint8_t)((avgCounter>>8) & 0xff);
  44.     txMsg.Data[1] = (uint8_t)(avgCounter & 0xff);
  45.     avgCounter = 0;
  46.     sei();
  47.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  48. }
  49. #endif
  50.  
  51. void sns_power_pcint_callback(uint8_t id, uint8_t status)
  52. {
  53.     if (status == 0)
  54.     {
  55.         if (Timer_GetTicks() - PreviusTimerValue >= 16)
  56.         {
  57.             MeasurmentBufferPointer++;
  58.             if (MeasurmentBufferPointer >= 32) MeasurmentBufferPointer = 0;
  59.             lastMeasurment =  Timer_GetTicks() - PreviusTimerValue;
  60.             MeasurmentBuffer[MeasurmentBufferPointer] = (uint16_t) (lastMeasurment & 0x0000FFFF);
  61.             PreviusTimerValue = Timer_GetTicks();
  62.             #if sns_power_1000_PULSES_PER_KWH == 1
  63.                 EnergyCounter++;
  64.                 if (EnergyCounter % 1024 == 0)
  65.                     StoreInEEPROM = 1; 
  66.             #endif
  67.             #if sns_power_10000_PULSES_PER_KWH == 1
  68.                 tmpCounter++;
  69.                 #if sns_power_SEND_1_MIN_AVG == 1
  70.                 avgCounter++;
  71.                 #endif
  72.                 if(tmpCounter >= 10) {
  73.                     EnergyCounter++;
  74.                     if (EnergyCounter % 1024 == 0)
  75.                         StoreInEEPROM = 1;
  76.                     tmpCounter = 0;
  77.                 }
  78.             #endif
  79.         }
  80.     }
  81. }
  82. void sns_power_Init(void)
  83. {
  84. #ifdef sns_power_USEEEPROM
  85.     if (EEDATA_OK)
  86.     {
  87.       ///TODO: Use stored data to set initial values for the module
  88.       sns_power_ReportInterval = eeprom_read_byte(EEDATA.reportInterval);
  89.       EnergyCounter = eeprom_read_word(EEDATA16.EnergyCounterLower);
  90.       EnergyCounter += (((uint32_t)(eeprom_read_word(EEDATA16.EnergyCounterUpper)))<<16);
  91.     } else
  92.     {   //The CRC of the EEPROM is not correct, store default values and update CRC
  93.       eeprom_write_byte_crc(EEDATA.reportInterval, sns_power_SEND_PERIOD, WITHOUT_CRC);
  94.       eeprom_write_word_crc(EEDATA16.EnergyCounterUpper, 0, WITHOUT_CRC);
  95.       eeprom_write_word_crc(EEDATA16.EnergyCounterLower, 0, WITHOUT_CRC);
  96.       EEDATA_UPDATE_CRC;
  97.       sns_power_ReportInterval = eeprom_read_byte(EEDATA.reportInterval);
  98.     }
  99. #endif  
  100.     ///TODO: Initialize hardware etc here
  101.     gpio_set_in(POWER_SNS_PIN); // Set to input
  102.     gpio_set_pin(POWER_SNS_PIN);    // Enable pull-up
  103.    
  104.     Pcint_SetCallbackPin(sns_power_PCINT, POWER_SNS_PIN, &sns_power_pcint_callback);
  105.  
  106.     MeasurmentBufferPointer = 0;
  107.     Timer_SetTimeout(sns_power_SEND_TIMER, sns_power_ReportInterval*1000 , TimerTypeFreeRunning, 0);
  108. #if sns_power_SEND_1_MIN_AVG == 1
  109.     Timer_SetTimeout(sns_power_SEND_TIMER_1_MIN_AVG, 60000-10 , TimerTypeFreeRunning, &sns_power_timer_callback);
  110. #endif
  111. }
  112.  
  113. void sns_power_Process(void)
  114. {
  115.     if (StoreInEEPROM == 1)
  116.     {
  117.         StoreInEEPROM = 0;
  118.         eeprom_write_word_crc(EEDATA16.EnergyCounterUpper, (uint16_t)((EnergyCounter>>16) & 0xffff), WITHOUT_CRC);
  119.         eeprom_write_word_crc(EEDATA16.EnergyCounterLower, (uint16_t)(EnergyCounter & 0xffff), WITH_CRC);
  120.     }
  121.     StdCan_Msg_t txMsg;
  122.     ///TODO: Stuff that needs doing is done here
  123.     if (Timer_Expired(sns_power_SEND_TIMER)) {
  124.         //4 times average
  125.         /*uint32_t Avg4 = MeasurmentBuffer[MeasurmentBufferPointer] + MeasurmentBuffer[MeasurmentBufferPointer-1] + MeasurmentBuffer[MeasurmentBufferPointer-2] + MeasurmentBuffer[MeasurmentBufferPointer-3];
  126.         Avg4 = (360000/(Avg4/4));
  127.         */
  128.         //32 times average
  129.         uint32_t Avg32 = 0;
  130.         for (uint8_t i = 0; i<32;i++) {
  131.              Avg32 += MeasurmentBuffer[i];
  132.         }
  133.         Avg32 /= 32;
  134.         #if sns_power_1000_PULSES_PER_KWH == 1
  135.             Avg32 = (3600000/Avg32);
  136.         #endif
  137.         #if sns_power_10000_PULSES_PER_KWH == 1
  138.             Avg32 = (360000/Avg32);
  139.         #endif
  140.  
  141.         StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS); ///TODO: Change this to the actual class type
  142.         StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  143.         txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_POWER; ///TODO: Change this to the actual module type
  144.         txMsg.Header.ModuleId = sns_power_ID;
  145.         txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_ELECTRICPOWER;
  146.         txMsg.Length = 8;
  147.         #if sns_power_1000_PULSES_PER_KWH == 1
  148.             uint32_t tmp = 3600000/MeasurmentBuffer[MeasurmentBufferPointer];
  149.         #endif
  150.         #if sns_power_10000_PULSES_PER_KWH == 1
  151.             uint32_t tmp = 360000/MeasurmentBuffer[MeasurmentBufferPointer];
  152.         #endif
  153.         txMsg.Data[0] = (uint8_t)((tmp>>8) & 0xff);
  154.         txMsg.Data[1] = (uint8_t)(tmp & 0xff);
  155.         txMsg.Data[5] = (uint8_t)EnergyCounter & 0xff;
  156.         txMsg.Data[4] = (uint8_t)(EnergyCounter >> 8) & 0xff;
  157.         txMsg.Data[3] = (uint8_t)(EnergyCounter >> 16) & 0xff;
  158.         txMsg.Data[2] = (uint8_t)(EnergyCounter >> 24) & 0xff;
  159.         txMsg.Data[6] = (uint8_t)((Avg32>>8) & 0xff);
  160.         txMsg.Data[7] = (uint8_t)(Avg32 & 0xff);
  161.         while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  162.     }
  163. }
  164.  
  165. void sns_power_HandleMessage(StdCan_Msg_t *rxMsg)
  166. {
  167.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  168.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  169.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_POWER &&
  170.         rxMsg->Header.ModuleId == sns_power_ID)
  171.     {
  172. StdCan_Msg_t txMsg;
  173.         switch (rxMsg->Header.Command)
  174.         {
  175.         case CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL:
  176.             if (rxMsg->Length > 0)
  177.             {
  178.                 sns_power_ReportInterval = rxMsg->Data[0];
  179.                 Timer_SetTimeout(sns_power_SEND_TIMER, sns_power_ReportInterval*1000 , TimerTypeFreeRunning, 0);
  180.             }
  181.             StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  182.             StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  183.             txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_POWER;
  184.             txMsg.Header.ModuleId = sns_power_ID;
  185.             txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL;
  186.             txMsg.Length = 1;
  187.             txMsg.Data[0] = sns_power_ReportInterval;
  188.             StdCan_Put(&txMsg);
  189.             break;
  190.         case CAN_MODULE_CMD_POWER_SETENERGY:
  191.             if (rxMsg->Length == 4)
  192.             {
  193.                 EnergyCounter = rxMsg->Data[3];
  194.                 EnergyCounter += ((uint32_t)rxMsg->Data[2])<<8;
  195.                 EnergyCounter += ((uint32_t)rxMsg->Data[1])<<16;
  196.                 EnergyCounter += ((uint32_t)rxMsg->Data[0])<<24;
  197.             }
  198.             StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  199.             StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  200.             txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_POWER;
  201.             txMsg.Header.ModuleId = sns_power_ID;
  202.             txMsg.Header.Command = CAN_MODULE_CMD_POWER_SETENERGY;
  203.             txMsg.Length = 1;
  204.             txMsg.Data[3] = (uint8_t)EnergyCounter & 0xff;
  205.             txMsg.Data[2] = (uint8_t)(EnergyCounter >> 8) & 0xff;
  206.             txMsg.Data[1] = (uint8_t)(EnergyCounter >> 16) & 0xff;
  207.             txMsg.Data[0] = (uint8_t)(EnergyCounter >> 24) & 0xff;
  208.             StdCan_Put(&txMsg);
  209.            
  210.             break;
  211.         }
  212.     }
  213. }
  214.  
  215. void sns_power_List(uint8_t ModuleSequenceNumber)
  216. {
  217.     StdCan_Msg_t txMsg;
  218.    
  219.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS); ///TODO: Change this to the actual class type
  220.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  221.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_POWER; ///TODO: Change this to the actual module type
  222.     txMsg.Header.ModuleId = sns_power_ID;
  223.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  224.     txMsg.Length = 6;
  225.  
  226.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  227.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  228.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  229.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  230.    
  231.     txMsg.Data[4] = NUMBER_OF_MODULES;
  232.     txMsg.Data[5] = ModuleSequenceNumber;
  233.    
  234.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  235. }
  236.