Subversion Repositories HomeAutomation

Rev

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