Subversion Repositories HomeAutomation

Rev

Rev 1393 | Rev 1425 | 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. if (pwmValue[0]>0)
  82. {
  83.     TCCR1A|=(act_hwPWM_CH1_COM<<COM1B0);
  84. }
  85. if (pwmValue[1]>0)
  86. {
  87.     TCCR1A|=(act_hwPWM_CH2_COM<<COM1A0);
  88. }
  89. #endif
  90.  
  91. /* enable timer 1 */
  92. #if act_hwPWM_CH1_COM>0
  93. TCCR1B|=(act_hwPWM_CH1_CS<<CS10);
  94. #elif act_hwPWM_CH2_COM>0
  95. TCCR1B|=(act_hwPWM_CH2_CS<<CS10);
  96. #endif
  97.  
  98.  
  99. /* set up waveform generation mode for timer 0 */
  100. #if act_hwPWM_CH3_COM>0
  101. TCCR0A=((act_hwPWM_CH3_WGM&0x03)<<WGM00);
  102. TCCR0B=(((act_hwPWM_CH3_WGM>>2)&0x01)<<WGM02);
  103. #elif act_hwPWM_CH4_COM>0
  104. TCCR0A=((act_hwPWM_CH4_WGM&0x03)<<WGM00);
  105. TCCR0B=(((act_hwPWM_CH4_WGM>>2)&0x01)<<WGM02);
  106. #endif
  107.  
  108. /* enable outputs for timer 1 */
  109. #if act_hwPWM_CH3_COM>0
  110. gpio_set_out(EXP_F);
  111. #endif
  112. #if act_hwPWM_CH4_COM>0
  113. gpio_set_out(EXP_G);
  114. #endif
  115.  
  116. /* set up counter mode for timer 0 */
  117. #if act_hwPWM_CH3_COM>0 || act_hwPWM_CH4_COM>0
  118. if (pwmValue[2]>0)
  119. {
  120.     TCCR0A|=(act_hwPWM_CH3_COM<<COM0A0);
  121. }
  122. if (pwmValue[3]>0)
  123. {
  124.     TCCR0A|=(act_hwPWM_CH4_COM<<COM0B0);
  125. }
  126. #endif
  127.  
  128. /* enable timer 0 */
  129. #if act_hwPWM_CH3_COM>0
  130. TCCR0B|=(act_hwPWM_CH3_CS<<CS00);
  131. #elif act_hwPWM_CH4_COM>0
  132. TCCR0B|=(act_hwPWM_CH4_CS<<CS00);
  133. #endif
  134.  
  135. //printf("1A %x, 1B %x, 0A %x, 0B %x\n", TCCR1A, TCCR1B, TCCR0A, TCCR0B);
  136.  
  137. }
  138.  
  139. void act_hwPWM_Process(void)
  140. {
  141.     if (Timer_Expired(act_hwPWM_STORE_VALUE_TIMEOUT))
  142.     {
  143.         if (pwmValue[0] != eeprom_read_word(EEDATA16.ch1))
  144.         {
  145.             eeprom_write_word_crc(EEDATA16.ch1, pwmValue[0], WITH_CRC);
  146.         }
  147.         if (pwmValue[1] != eeprom_read_word(EEDATA16.ch2))
  148.         {
  149.             eeprom_write_word_crc(EEDATA16.ch2, pwmValue[1], WITH_CRC);
  150.         }
  151.         if (pwmValue[2] != eeprom_read_word(EEDATA16.ch3))
  152.         {
  153.             eeprom_write_word_crc(EEDATA16.ch3, pwmValue[2], WITH_CRC);
  154.         }
  155.         if (pwmValue[3] != eeprom_read_word(EEDATA16.ch4))
  156.         {
  157.             eeprom_write_word_crc(EEDATA16.ch4, pwmValue[3], WITH_CRC);
  158.         }
  159.     }
  160. }
  161.  
  162. void act_hwPWM_HandleMessage(StdCan_Msg_t *rxMsg)
  163. {
  164.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT &&
  165.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  166.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_HWPWM &&
  167.         rxMsg->Header.ModuleId == act_hwPWM_ID)
  168.     {
  169.         switch (rxMsg->Header.Command)
  170.         {
  171.         case CAN_MODULE_CMD_PHYSICAL_PWM:
  172.             if (rxMsg->Length == 3) {
  173.                 uint8_t channel = rxMsg->Data[0];
  174.                 pwmValue[channel-1] = (rxMsg->Data[1]<<8)+(rxMsg->Data[2]);
  175.                 cli();
  176.                 switch (channel)
  177.                 {
  178. #if act_hwPWM_CH1_COM>0
  179.                     case 1:
  180.                         OCR_1=(uint16_t)(pwmValue[channel-1]*act_hwPWM_CH1_FACT)>>8;
  181.                         if (pwmValue[channel-1]==0)
  182.                         {
  183.                             TCCR1A&=~((1<<COM1B0)|(1<<COM1B1));
  184.                         }
  185.                         else
  186.                         {
  187.                             TCCR1A|=(act_hwPWM_CH1_COM<<COM1B0);
  188.                         }
  189.                     break;
  190. #endif
  191. #if act_hwPWM_CH2_COM>0
  192.                     case 2:
  193.                         OCR_2=(uint16_t)(pwmValue[channel-1]*act_hwPWM_CH2_FACT)>>8;
  194.                         if (pwmValue[channel-1]==0)
  195.                         {
  196.                             TCCR1A&=~((1<<COM1A0)|(1<<COM1A1));
  197.                         }
  198.                         else
  199.                         {
  200.                             TCCR1A|=(act_hwPWM_CH2_COM<<COM1A0);
  201.                         }
  202.                     break;
  203. #endif
  204. #if act_hwPWM_CH3_COM>0
  205.                     case 3:
  206.                         OCR_3=(uint16_t)(pwmValue[channel-1]*act_hwPWM_CH3_FACT)>>8;
  207.                         if (pwmValue[channel-1]==0)
  208.                         {
  209.                             TCCR0A &= ~((1<<COM0A0)|(1<<COM0A1));
  210.                         }
  211.                         else
  212.                         {
  213.                             TCCR0A|=(act_hwPWM_CH3_COM<<COM0A0);
  214.                         }
  215.                     break;
  216. #endif
  217. #if act_hwPWM_CH4_COM>0
  218.                     case 4:
  219.                         OCR_4=(uint16_t)(pwmValue[channel-1]*act_hwPWM_CH4_FACT)>>8;
  220.                         if (pwmValue[channel-1]==0)
  221.                         {
  222.                             TCCR0A &= ~((1<<COM0B0)|(1<<COM0B1));
  223.                         }
  224.                         else
  225.                         {
  226.                             TCCR0A|=(act_hwPWM_CH4_COM<<COM0B0);
  227.                         }
  228.                     break;
  229. #endif
  230.                 }
  231.                 sei();
  232.                 Timer_SetTimeout(act_hwPWM_STORE_VALUE_TIMEOUT, act_hwPWM_STORE_VALUE_TIMEOUT_TIME*1000, TimerTypeOneShot, 0);
  233.             }
  234.            
  235.         break;
  236.         }
  237.     }
  238. }
  239.  
  240. void act_hwPWM_List(uint8_t ModuleSequenceNumber)
  241. {
  242.     StdCan_Msg_t txMsg;
  243.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
  244.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  245.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_HWPWM;
  246.     txMsg.Header.ModuleId = act_hwPWM_ID;
  247.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  248.     txMsg.Length = 6;
  249.  
  250.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  251.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  252.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  253.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  254.    
  255.     txMsg.Data[4] = NUMBER_OF_MODULES;
  256.     txMsg.Data[5] = ModuleSequenceNumber;
  257.  
  258.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  259. }
  260.