Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. #include "act_hwPWM.h"
  3.  
  4. uint16_t pwmValue[4];
  5.  
  6. #ifdef act_hwPWM_USEEEPROM
  7. #include "act_hwPWM_eeprom.h"
  8. struct eeprom_act_hwPWM EEMEM eeprom_act_hwPWM =
  9. {
  10.     {
  11.         ///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.
  12.         0x0000, // ch 1
  13.         0x0000, // ch 2
  14.         0x0000, // ch 3
  15.         0x0000  // ch 4
  16.     },
  17.     0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
  18. };
  19. #endif
  20.  
  21. void act_hwPWM_Init(void)
  22. {
  23. #ifdef act_hwPWM_USEEEPROM
  24.     if (EEDATA_OK)
  25.     {
  26.       ///TODO: Use stored data to set initial values for the module
  27.       pwmValue[0] = eeprom_read_word(EEDATA16.ch1);
  28.       pwmValue[1] = eeprom_read_word(EEDATA16.ch2);
  29.       pwmValue[2] = eeprom_read_word(EEDATA16.ch3);
  30.       pwmValue[3] = eeprom_read_word(EEDATA16.ch4);
  31.     } else
  32.     {   //The CRC of the EEPROM is not correct, store default values and update CRC
  33.       eeprom_write_word_crc(EEDATA16.ch1, 0x0000, WITHOUT_CRC);
  34.       eeprom_write_word_crc(EEDATA16.ch2, 0x0000, WITHOUT_CRC);
  35.       eeprom_write_word_crc(EEDATA16.ch3, 0x0000, WITHOUT_CRC);
  36.       eeprom_write_word_crc(EEDATA16.ch4, 0x0000, WITHOUT_CRC);
  37.       EEDATA_UPDATE_CRC;
  38.     }
  39. #endif
  40.  
  41. TCCR1A = 0;
  42. TCCR1B = 0;
  43. TCCR0A = 0;
  44. TCCR0B = 0;
  45.  
  46. /* set up pwm values */
  47. cli();
  48. #if act_hwPWM_CH1_COM>0
  49. OCR_1=(uint16_t)(pwmValue[0]*act_hwPWM_CH1_FACT)>>8;
  50. #endif
  51. #if act_hwPWM_CH2_COM>0
  52. OCR_2=(uint16_t)(pwmValue[1]*act_hwPWM_CH2_FACT)>>8;
  53. #endif
  54. #if act_hwPWM_CH3_COM>0
  55. OCR_3=(uint16_t)(pwmValue[2]*act_hwPWM_CH3_FACT)>>8;
  56. #endif
  57. #if act_hwPWM_CH4_COM>0
  58. OCR_4=(uint16_t)(pwmValue[3]*act_hwPWM_CH4_FACT)>>8;
  59. #endif
  60. sei();
  61.  
  62. /* set up waveform generation mode for timer 1 */
  63. #if act_hwPWM_CH1_COM>0
  64. TCCR1A=((act_hwPWM_CH1_WGM&0x03)<<WGM00);
  65. TCCR1B=(((act_hwPWM_CH1_WGM>>2)&0x03)<<WGM12);
  66. #elif act_hwPWM_CH2_COM>0
  67. TCCR1A=((act_hwPWM_CH2_WGM&0x03)<<WGM00);
  68. TCCR1B=(((act_hwPWM_CH2_WGM>>2)&0x03)<<WGM12);
  69. #endif
  70.  
  71. /* enable outputs for timer 1 */
  72. #if act_hwPWM_CH1_COM>0
  73. gpio_set_out(EXP_B);
  74. #endif
  75. #if act_hwPWM_CH2_COM>0
  76. gpio_set_out(EXP_C);
  77. #endif
  78.  
  79. /* set up counter mode for timer 1 */
  80. #if act_hwPWM_CH1_COM>0 || act_hwPWM_CH2_COM>0
  81. TCCR1A|=(act_hwPWM_CH1_COM<<COM1B0)|(act_hwPWM_CH2_COM<<COM1A0);
  82. #endif
  83.  
  84. /* enable timer 1 */
  85. #if act_hwPWM_CH1_COM>0
  86. TCCR1B|=(act_hwPWM_CH1_CS<<CS10);
  87. #elif act_hwPWM_CH2_COM>0
  88. TCCR1B|=(act_hwPWM_CH2_CS<<CS10);
  89. #endif
  90.  
  91.  
  92. /* set up waveform generation mode for timer 0 */
  93. #if act_hwPWM_CH3_COM>0
  94. TCCR0A=((act_hwPWM_CH3_WGM&0x03)<<WGM00);
  95. TCCR0B=(((act_hwPWM_CH3_WGM>>2)&0x01)<<WGM02);
  96. #elif act_hwPWM_CH4_COM>0
  97. TCCR0A=((act_hwPWM_CH4_WGM&0x03)<<WGM00);
  98. TCCR0B=(((act_hwPWM_CH4_WGM>>2)&0x01)<<WGM02);
  99. #endif
  100.  
  101. /* enable outputs for timer 1 */
  102. #if act_hwPWM_CH3_COM>0
  103. gpio_set_out(EXP_F);
  104. #endif
  105. #if act_hwPWM_CH4_COM>0
  106. gpio_set_out(EXP_G);
  107. #endif
  108.  
  109. /* set up counter mode for timer 0 */
  110. #if act_hwPWM_CH3_COM>0 || act_hwPWM_CH4_COM>0
  111. TCCR0A|=(act_hwPWM_CH3_COM<<COM1A0)|(act_hwPWM_CH4_COM<<COM1B0);
  112. #endif
  113.  
  114. /* enable timer 0 */
  115. #if act_hwPWM_CH3_COM>0
  116. TCCR0B|=(act_hwPWM_CH3_CS<<CS00);
  117. #elif act_hwPWM_CH4_COM>0
  118. TCCR0B|=(act_hwPWM_CH4_CS<<CS00);
  119. #endif
  120.  
  121.  
  122. }
  123.  
  124. void act_hwPWM_Process(void)
  125. {
  126.     if (Timer_Expired(act_hwPWM_STORE_VALUE_TIMEOUT))
  127.     {
  128.         if (pwmValue[0] != eeprom_read_word(EEDATA16.ch1))
  129.         {
  130.             eeprom_write_word_crc(EEDATA16.ch1, pwmValue[0], WITH_CRC);
  131.         }
  132.         if (pwmValue[1] != eeprom_read_word(EEDATA16.ch2))
  133.         {
  134.             eeprom_write_word_crc(EEDATA16.ch2, pwmValue[1], WITH_CRC);
  135.         }
  136.         if (pwmValue[2] != eeprom_read_word(EEDATA16.ch3))
  137.         {
  138.             eeprom_write_word_crc(EEDATA16.ch3, pwmValue[2], WITH_CRC);
  139.         }
  140.         if (pwmValue[3] != eeprom_read_word(EEDATA16.ch4))
  141.         {
  142.             eeprom_write_word_crc(EEDATA16.ch4, pwmValue[3], WITH_CRC);
  143.         }
  144.     }
  145. }
  146.  
  147. void act_hwPWM_HandleMessage(StdCan_Msg_t *rxMsg)
  148. {
  149.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT &&
  150.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  151.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_HWPWM &&
  152.         rxMsg->Header.ModuleId == act_hwPWM_ID)
  153.     {
  154.         switch (rxMsg->Header.Command)
  155.         {
  156.         case CAN_MODULE_CMD_PHYSICAL_PWM:
  157.             if (rxMsg->Length == 3) {
  158.                 uint8_t channel = rxMsg->Data[0];
  159.                 pwmValue[channel-1] = (rxMsg->Data[1]<<8)+(rxMsg->Data[2]);
  160.                 cli();
  161.                 switch (channel)
  162.                 {
  163. #if act_hwPWM_CH1_COM>0
  164.                     case 1:
  165.                         OCR_1=(uint16_t)(pwmValue[channel-1]*act_hwPWM_CH1_FACT)>>8;
  166.                     break;
  167. #endif
  168. #if act_hwPWM_CH2_COM>0
  169.                     case 2:
  170.                         OCR_2=(uint16_t)(pwmValue[channel-1]*act_hwPWM_CH2_FACT)>>8;
  171.                     break;
  172. #endif
  173. #if act_hwPWM_CH3_COM>0
  174.                     case 3:
  175.                         OCR_3=(uint16_t)(pwmValue[channel-1]*act_hwPWM_CH3_FACT)>>8;
  176.                     break;
  177. #endif
  178. #if act_hwPWM_CH4_COM>0
  179.                     case 4:
  180.                         OCR_4=(uint16_t)(pwmValue[channel-1]*act_hwPWM_CH4_FACT)>>8;
  181.                     break;
  182. #endif
  183.                 }
  184.                 sei();
  185.                 Timer_SetTimeout(act_hwPWM_STORE_VALUE_TIMEOUT, act_hwPWM_STORE_VALUE_TIMEOUT_TIME*1000, TimerTypeOneShot, 0);
  186.             }
  187.            
  188.         break;
  189.         }
  190.     }
  191. }
  192.  
  193. void act_hwPWM_List(uint8_t ModuleSequenceNumber)
  194. {
  195.     StdCan_Msg_t txMsg;
  196.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
  197.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  198.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_HWPWM;
  199.     txMsg.Header.ModuleId = act_hwPWM_ID;
  200.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  201.     txMsg.Length = 6;
  202.  
  203.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  204.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  205.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  206.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  207.    
  208.     txMsg.Data[4] = NUMBER_OF_MODULES;
  209.     txMsg.Data[5] = ModuleSequenceNumber;
  210.  
  211.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  212. }
  213.