Subversion Repositories HomeAutomation

Rev

Blame | Last modification | View Log | SVN | RSS feed

  1.  
  2. #include "sns_flower.h"
  3.  
  4. #if sns_flower_USEEEPROM==1
  5. #include "sns_flower_eeprom.h"
  6.  
  7. struct eeprom_sns_flower EEMEM eeprom_sns_flower =
  8. {
  9.     {
  10.         ///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.
  11.         0xAB,   // x
  12.         0x1234  // y
  13.     },
  14.     0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
  15. };
  16. #endif
  17.  
  18. void sns_flower_Init(void)
  19. {
  20. #if sns_flower_USEEEPROM==1
  21.     if (EEDATA_OK)
  22.     {
  23.       ///TODO: Use stored data to set initial values for the module
  24.       blablaX = eeprom_read_byte(EEDATA.x);
  25.       blablaY = eeprom_read_word(EEDATA.y);
  26.     } else
  27.     {   //The CRC of the EEPROM is not correct, store default values and update CRC
  28.       eeprom_write_byte_crc(EEDATA.x, 0xAB, WITHOUT_CRC);
  29.       eeprom_write_word_crc(EEDATA.y, 0x1234, WITHOUT_CRC);
  30.       EEDATA_UPDATE_CRC;
  31.     }
  32. #endif  
  33.  
  34.     // to use PCINt lib, call this function: (the callback function look as a timer callback function)
  35.     // Pcint_SetCallbackPin(sns_flower_PCINT, EXP_C , &sns_flower_pcint_callback);
  36.  
  37.     ADC_Init();
  38.     Timer_SetTimeout(sns_flower_WAIT_TIMER, sns_flower_WAIT_PERIOD_S*1000u , TimerTypeFreeRunning, 0);
  39. }
  40.  
  41.  
  42.  
  43. void sns_flower_Process(void)
  44. {
  45.     static uint8_t measureState=0;
  46.     static uint16_t results[6];
  47.     static uint16_t resultsInv[6];
  48.     uint16_t temp;
  49.     static uint8_t flowerChannelToSend = 0;
  50.  
  51.    
  52.     if (Timer_Expired(sns_flower_WAIT_TIMER)) {
  53.        
  54.         gpio_set_out(sns_flower_highSide_PIN);
  55.         gpio_set_out(sns_flower_lowSide_PIN);
  56.         gpio_set_pin(sns_flower_highSide_PIN);
  57.         gpio_clr_pin(sns_flower_lowSide_PIN);
  58.         measureState=0;
  59.         Timer_SetTimeout(sns_flower_MEASUREMENT_TIMER, sns_flower_MEASUREMENT_PERIOD_MS , TimerTypeOneShot, 0);
  60.     }
  61.    
  62.     if (Timer_Expired(sns_flower_MEASUREMENT_TIMER)) {
  63.         StdCan_Msg_t txMsg;
  64.         StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  65.         StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  66.         txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_VOLTAGECURRENT;
  67.         txMsg.Header.ModuleId = sns_flower_ID;
  68.         txMsg.Length = 3;
  69.         switch (measureState) {
  70.             case 0:
  71. #ifdef sns_flower0AD
  72.                 results[0] = ADC_Get(sns_flower0AD);
  73. #endif
  74. #ifdef sns_flower1AD
  75.                 results[1] = ADC_Get(sns_flower1AD);
  76. #endif
  77. #ifdef sns_flower2AD
  78.                 results[2] = ADC_Get(sns_flower2AD);
  79. #endif
  80. #ifdef sns_flower3AD
  81.                 results[3] = ADC_Get(sns_flower3AD);
  82. #endif
  83. #ifdef sns_flower4AD
  84.                 results[4] = ADC_Get(sns_flower4AD);
  85. #endif
  86. #ifdef sns_flower5AD
  87.                 results[5] = ADC_Get(sns_flower5AD);
  88. #endif 
  89.                 gpio_clr_pin(sns_flower_highSide_PIN);
  90.                 gpio_set_pin(sns_flower_lowSide_PIN);
  91.                 measureState=1;
  92.                 Timer_SetTimeout(sns_flower_MEASUREMENT_TIMER, sns_flower_MEASUREMENT_PERIOD_MS , TimerTypeOneShot, 0);
  93.                 break;
  94.             case 1:
  95. #ifdef sns_flower0AD
  96.                 resultsInv[0] = 1023-ADC_Get(sns_flower0AD);
  97. #endif
  98. #ifdef sns_flower1AD
  99.                 resultsInv[1] = 1023-ADC_Get(sns_flower1AD);
  100. #endif
  101. #ifdef sns_flower2AD
  102.                 resultsInv[2] = 1023-ADC_Get(sns_flower2AD);
  103. #endif
  104. #ifdef sns_flower3AD
  105.                 resultsInv[3] = 1023-ADC_Get(sns_flower3AD);
  106. #endif
  107. #ifdef sns_flower4AD
  108.                 resultsInv[4] = 1023-ADC_Get(sns_flower4AD);
  109. #endif
  110. #ifdef sns_flower5AD
  111.                 resultsInv[5] = 1023-ADC_Get(sns_flower5AD);
  112. #endif 
  113.                 gpio_clr_pin(sns_flower_highSide_PIN);
  114.                 gpio_clr_pin(sns_flower_lowSide_PIN);
  115.                 gpio_set_in(sns_flower_highSide_PIN);
  116.                 gpio_set_in(sns_flower_lowSide_PIN);
  117.                 measureState=2;
  118.                 flowerChannelToSend=0;
  119.                 Timer_SetTimeout(sns_flower_MEASUREMENT_TIMER, sns_flower_MEASUREMENT_PERIOD_MS , TimerTypeOneShot, 0);
  120.                 break;
  121.             case 2:
  122.                   switch(flowerChannelToSend) {
  123.                       case 0:
  124. #ifdef sns_flower0AD
  125.                         txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PERCENT;
  126.                         txMsg.Data[0] = 0;
  127.                         temp = (uint16_t)((float)(results[0]+resultsInv[0])*4.888);
  128.                         txMsg.Data[1] = (temp>>8)&0xff;
  129.                         txMsg.Data[2] = (temp)&0xff;
  130.  
  131.                         while (StdCan_Put(&txMsg) != StdCan_Ret_OK) {}
  132.                         flowerChannelToSend = 1;
  133. # if  !(defined(sns_flower1AD) || defined(sns_flower2AD) || defined(sns_flower3AD) || defined(sns_flower4AD) || defined(sns_flower5AD))
  134.                         flowerChannelToSend = 0;
  135.                         measureState=0;
  136. # endif
  137.                         break;
  138. #endif
  139.                     case 1:
  140. #ifdef sns_flower1AD
  141.                         txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PERCENT;
  142.                         txMsg.Data[0] = 1;
  143.                         temp = (uint16_t)((float)(results[1]+resultsInv[1])*4.888);
  144.                         txMsg.Data[1] = (temp>>8)&0xff;
  145.                         txMsg.Data[2] = (temp)&0xff;
  146.  
  147.                         while (StdCan_Put(&txMsg) != StdCan_Ret_OK) {}
  148.                         flowerChannelToSend = 2;
  149. # if  !(defined(sns_flower2AD) || defined(sns_flower3AD) || defined(sns_flower4AD) || defined(sns_flower5AD))
  150.                         flowerChannelToSend = 0;
  151.                         measureState=0;
  152. # endif
  153.                         break;
  154. #endif
  155.                     case 2:
  156. #ifdef sns_flower2AD
  157.                         txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PERCENT;
  158.                         txMsg.Data[0] = 2;
  159.                         temp = (uint16_t)((float)(results[2]+resultsInv[2])*4.888);
  160.                         txMsg.Data[1] = (temp>>8)&0xff;
  161.                         txMsg.Data[2] = (temp)&0xff;
  162.  
  163.                         while (StdCan_Put(&txMsg) != StdCan_Ret_OK) {}
  164.                         flowerChannelToSend = 3;
  165. #if  !(defined(sns_flower3AD) || defined(sns_flower4AD) || defined(sns_flower5AD))
  166.                         flowerChannelToSend = 0;
  167.                         measureState=0;
  168. #endif
  169.                         break;
  170. #endif
  171.                     case 3:
  172. #ifdef sns_flower3AD
  173.                         txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PERCENT;
  174.                         txMsg.Data[0] = 3;
  175.                         temp = (uint16_t)((float)(results[3]+resultsInv[3])*4.888);
  176.                         txMsg.Data[1] = (temp>>8)&0xff;
  177.                         txMsg.Data[2] = (temp)&0xff;
  178.  
  179.                         while (StdCan_Put(&txMsg) != StdCan_Ret_OK) {}
  180.                         flowerChannelToSend = 4;
  181. #if  !( defined(sns_flower4AD) || defined(sns_flower5AD))
  182.                         flowerChannelToSend = 0;
  183.                         measureState=0;
  184. #endif
  185.                         break;
  186. #endif
  187.                     case 4:
  188. #ifdef sns_flower4AD
  189.                         txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PERCENT;
  190.                         txMsg.Data[0] = 4;
  191.                         temp = (uint16_t)((float)(results[4]+resultsInv[4])*4.888);
  192.                         txMsg.Data[1] = (temp>>8)&0xff;
  193.                         txMsg.Data[2] = (temp)&0xff;
  194.  
  195.                         while (StdCan_Put(&txMsg) != StdCan_Ret_OK) {}
  196.                         flowerChannelToSend = 5;
  197. #if  !(defined(sns_flower5AD))
  198.                         flowerChannelToSend = 0;
  199.                         measureState=0;
  200. #endif
  201.                         break;
  202. #endif
  203.                     case 5:
  204. #ifdef sns_flower5AD
  205.                         txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PERCENT;
  206.                         txMsg.Data[0] = 5;
  207.                         temp = (uint16_t)((float)(results[5]+resultsInv[5])*4.888);
  208.                         txMsg.Data[1] = (temp>>8)&0xff;
  209.                         txMsg.Data[2] = (temp)&0xff;
  210.  
  211.                         while (StdCan_Put(&txMsg) != StdCan_Ret_OK) {}
  212.                         flowerChannelToSend = 0;
  213.                         measureState=0;
  214.                         break;
  215. #endif
  216.                     default:
  217.                         measureState=0;
  218.                         break;
  219.                 }
  220.                 if (measureState != 0) {
  221.                     Timer_SetTimeout(sns_flower_MEASUREMENT_TIMER, sns_flower_CAN_MSG_PAUS_MS , TimerTypeOneShot, 0);
  222.                 }
  223.                 break;
  224.         }
  225.  
  226.     }
  227. }
  228.  
  229. void sns_flower_HandleMessage(StdCan_Msg_t *rxMsg)
  230. {
  231. /*  if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  232.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
  233.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_FLOWER &&
  234.         rxMsg->Header.ModuleId == sns_flower_ID)
  235.     {
  236.         switch (rxMsg->Header.Command)
  237.         {
  238.         case CAN_CMD_MODULE_DUMMY:
  239.         ///TODO: Do something dummy
  240.         break;
  241.         }
  242.     }*/
  243. }
  244.  
  245. void sns_flower_List(uint8_t ModuleSequenceNumber)
  246. {
  247.     StdCan_Msg_t txMsg;
  248.    
  249.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  250.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  251.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_FLOWER;
  252.     txMsg.Header.ModuleId = sns_flower_ID;
  253.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  254.     txMsg.Length = 6;
  255.  
  256.     uint32_t HwId=BIOS_GetHwId();
  257.     txMsg.Data[0] = HwId&0xff;
  258.     txMsg.Data[1] = (HwId>>8)&0xff;
  259.     txMsg.Data[2] = (HwId>>16)&0xff;
  260.     txMsg.Data[3] = (HwId>>24)&0xff;
  261.    
  262.     txMsg.Data[4] = NUMBER_OF_MODULES;
  263.     txMsg.Data[5] = ModuleSequenceNumber;
  264.    
  265.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  266. }
  267.