Subversion Repositories HomeAutomation

Rev

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