Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. #include "act_pcaPWM.h"
  3. #include "drivers/io/PCA9634.h"
  4.  
  5. static uint16_t setValue[8]={0,0,0,0,0,0,0,0};
  6. static uint16_t isValue[8];
  7.  
  8. #ifdef act_pcaPWM_USEEEPROM
  9. #include "act_pcaPWM_eeprom.h"
  10. struct eeprom_act_pcaPWM EEMEM eeprom_act_pcaPWM =
  11. {
  12.     {
  13.         ///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.
  14.         0x0000, // ch 1
  15.         0x0000, // ch 2
  16.         0x0000, // ch 3
  17.         0x0000, // ch 4
  18.         0x0000, // ch 5
  19.         0x0000, // ch 6
  20.         0x0000, // ch 7
  21.         0x0000  // ch 8
  22.     },
  23.     0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
  24. };
  25. #endif
  26.  
  27. void act_pcaPWM_Init(void)
  28. {
  29. #ifdef act_pcaPWM_USEEEPROM
  30.     if (EEDATA_OK)
  31.     {
  32.       setValue[0] = eeprom_read_word(EEDATA16.ch1);
  33.       setValue[1] = eeprom_read_word(EEDATA16.ch2);
  34.       setValue[2] = eeprom_read_word(EEDATA16.ch3);
  35.       setValue[3] = eeprom_read_word(EEDATA16.ch4);
  36.       setValue[4] = eeprom_read_word(EEDATA16.ch5);
  37.       setValue[5] = eeprom_read_word(EEDATA16.ch6);
  38.       setValue[6] = eeprom_read_word(EEDATA16.ch7);
  39.       setValue[7] = eeprom_read_word(EEDATA16.ch8);
  40.     } else
  41.     {   //The CRC of the EEPROM is not correct, store default values and update CRC
  42.       eeprom_write_word_crc(EEDATA16.ch1, 0x0000, WITHOUT_CRC);
  43.       eeprom_write_word_crc(EEDATA16.ch2, 0x0000, WITHOUT_CRC);
  44.       eeprom_write_word_crc(EEDATA16.ch3, 0x0000, WITHOUT_CRC);
  45.       eeprom_write_word_crc(EEDATA16.ch4, 0x0000, WITHOUT_CRC);
  46.       eeprom_write_word_crc(EEDATA16.ch5, 0x0000, WITHOUT_CRC);
  47.       eeprom_write_word_crc(EEDATA16.ch6, 0x0000, WITHOUT_CRC);
  48.       eeprom_write_word_crc(EEDATA16.ch7, 0x0000, WITHOUT_CRC);
  49.       eeprom_write_word_crc(EEDATA16.ch8, 0x0000, WITHOUT_CRC);
  50.       EEDATA_UPDATE_CRC;
  51.     }
  52. #endif
  53.  
  54.     pca9634_init(act_pcaPWM_I2C_ADDRESS);
  55.     for (uint8_t i=0; i<8; i++)
  56.     {
  57.         pca9634_setOpMode(i, PCA9634_OP_IND);
  58.         /* Set "current" value to something impossible to force an update at start */
  59.         isValue[i] = 10001;
  60.     }
  61.    
  62. #ifdef act_pcaPWM_OUTPUT_ENABLE_IO
  63.     gpio_set_out(act_pcaPWM_OUTPUT_ENABLE_IO);
  64.     gpio_clr_pin(act_pcaPWM_OUTPUT_ENABLE_IO);
  65. #endif
  66. #ifdef act_pcaPWM_LED_OVERRIDE_IO
  67.     gpio_set_out(act_pcaPWM_LED_OVERRIDE_IO);
  68.     gpio_clr_pin(act_pcaPWM_LED_OVERRIDE_IO);
  69. #endif
  70. }
  71.  
  72. void act_pcaPWM_Process(void)
  73. {
  74.  
  75.     for (uint8_t i=0; i < 8; i++)
  76.     {
  77.         if (setValue[i] != isValue[i])
  78.         {
  79.             isValue[i] = setValue[i];
  80.             pca9634_setDuty(i, isValue[i]*act_pcaPWM_FACT);
  81. #ifdef act_pcaPWM_USEEEPROM
  82.             Timer_SetTimeout(act_pcaPWM_STORE_VALUE_TIMEOUT, act_pcaPWM_STORE_VALUE_TIMEOUT_TIME*1000, TimerTypeOneShot, 0);
  83. #endif
  84.         }
  85.     }
  86.  
  87. #ifdef act_pcaPWM_USEEEPROM
  88.     if (Timer_Expired(act_pcaPWM_STORE_VALUE_TIMEOUT))
  89.     {
  90.         if (isValue[0] != eeprom_read_word(EEDATA16.ch1))
  91.         {
  92.             eeprom_write_word_crc(EEDATA16.ch1, isValue[0], WITH_CRC);
  93.         }
  94.         if (isValue[1] != eeprom_read_word(EEDATA16.ch2))
  95.         {
  96.             eeprom_write_word_crc(EEDATA16.ch2, isValue[1], WITH_CRC);
  97.         }
  98.         if (isValue[2] != eeprom_read_word(EEDATA16.ch3))
  99.         {
  100.             eeprom_write_word_crc(EEDATA16.ch3, isValue[2], WITH_CRC);
  101.         }
  102.         if (isValue[3] != eeprom_read_word(EEDATA16.ch4))
  103.         {
  104.             eeprom_write_word_crc(EEDATA16.ch4, isValue[3], WITH_CRC);
  105.         }
  106.         if (isValue[4] != eeprom_read_word(EEDATA16.ch5))
  107.         {
  108.             eeprom_write_word_crc(EEDATA16.ch5, isValue[4], WITH_CRC);
  109.         }
  110.         if (isValue[5] != eeprom_read_word(EEDATA16.ch6))
  111.         {
  112.             eeprom_write_word_crc(EEDATA16.ch6, isValue[5], WITH_CRC);
  113.         }
  114.         if (isValue[6] != eeprom_read_word(EEDATA16.ch7))
  115.         {
  116.             eeprom_write_word_crc(EEDATA16.ch7, isValue[6], WITH_CRC);
  117.         }
  118.         if (isValue[7] != eeprom_read_word(EEDATA16.ch8))
  119.         {
  120.             eeprom_write_word_crc(EEDATA16.ch8, isValue[7], WITH_CRC);
  121.         }
  122.     }
  123. #endif
  124. }
  125.  
  126. void act_pcaPWM_HandleMessage(StdCan_Msg_t *rxMsg)
  127. {
  128.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT &&
  129.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  130.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_PCAPWM &&
  131.         rxMsg->Header.ModuleId == act_pcaPWM_ID)
  132.     {
  133.         uint8_t channel = 0;
  134.         switch (rxMsg->Header.Command)
  135.         {
  136.         case CAN_MODULE_CMD_PHYSICAL_PWM:
  137.             if (rxMsg->Length == 3) {
  138.                 channel = rxMsg->Data[0];
  139.                 setValue[channel] = (rxMsg->Data[1]<<8)+(rxMsg->Data[2]);
  140.             }
  141.         break;
  142.         }
  143.     }
  144. }
  145.  
  146. void act_pcaPWM_List(uint8_t ModuleSequenceNumber)
  147. {
  148.     StdCan_Msg_t txMsg;
  149.  
  150.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
  151.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  152.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_PCAPWM;
  153.     txMsg.Header.ModuleId = act_pcaPWM_ID;
  154.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  155.     txMsg.Length = 6;
  156.  
  157.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  158.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  159.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  160.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  161.  
  162.     txMsg.Data[4] = NUMBER_OF_MODULES;
  163.     txMsg.Data[5] = ModuleSequenceNumber;
  164.  
  165.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  166. }
  167.