Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. #include "act_softPWM.h"
  3.  
  4.  
  5. uint16_t currentTimer = 0;
  6. uint16_t maxTimer = 10;
  7. uint8_t resolution = 1;
  8. uint8_t pwmDefaultState,pwmDefaultStartState,act_softPWM_ReportInterval;
  9. uint16_t pwmDefaultValue;
  10. uint16_t pwmPeriod;
  11. uint8_t pwmStatus;
  12. uint8_t currentSendChannelId = 0;
  13.  
  14.  
  15. #ifdef act_softPWM_USEEEPROM
  16. #include "act_softPWM_eeprom.h"
  17. struct eeprom_act_softPWM EEMEM eeprom_act_softPWM =
  18. {
  19.     {
  20.         ///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.
  21.         0xABcd, // x
  22.         0x1234, // y
  23.         0x00,
  24.         0x14    //Reportinteval 20 sec.
  25.     },
  26.     0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
  27. };
  28. #endif
  29.  
  30. uint16_t pwmValue[NUMBEROFCHANNELS];
  31.  
  32. void act_softPWM_Init(void)
  33. {
  34. #ifdef act_softPWM_USEEEPROM
  35.     if (EEDATA_OK)
  36.     {
  37.       ;
  38.     } else
  39.     {   //The CRC of the EEPROM is not correct, store default values and update CRC
  40.         eeprom_write_word_crc(EEDATA16.PwmPeriod, 10 , WITHOUT_CRC);
  41.         eeprom_write_word_crc(EEDATA16.defaultPwmValue, 0 , WITHOUT_CRC);
  42.         eeprom_write_byte_crc(EEDATA.defaultStates, 0xa0 , WITHOUT_CRC);
  43.         eeprom_write_byte_crc(EEDATA.ReportInterval, 0x14 , WITHOUT_CRC);
  44.         EEDATA_UPDATE_CRC;
  45.     }
  46.     pwmDefaultValue = eeprom_read_word(EEDATA16.defaultPwmValue);
  47.     pwmPeriod = eeprom_read_word(EEDATA16.PwmPeriod);
  48.     pwmDefaultState = (eeprom_read_byte(EEDATA.defaultStates)>>6) & 0x03;
  49.     pwmDefaultStartState = ((eeprom_read_byte(EEDATA.defaultStates)>>5) & 0x01);
  50.     act_softPWM_ReportInterval = (eeprom_read_byte(EEDATA.ReportInterval));
  51.     Timer_SetTimeout(act_softPWM_SEND_TIMER, act_softPWM_ReportInterval*1000 , TimerTypeFreeRunning, 0);
  52. #endif  
  53.     ///TODO: Initialize hardware etc here
  54. #ifdef PIN_0
  55.     gpio_set_out(PIN_0);
  56.     gpio_clr_pin(PIN_0);
  57. #endif
  58. #ifdef PIN_1
  59.     gpio_set_out(PIN_1);
  60.     gpio_clr_pin(PIN_1);
  61. #endif
  62. #ifdef PIN_2
  63.     gpio_set_out(PIN_2);
  64.     gpio_clr_pin(PIN_2);
  65. #endif
  66. #ifdef PIN_3
  67.     gpio_set_out(PIN_3);
  68.     gpio_clr_pin(PIN_3);
  69. #endif
  70. #ifdef PIN_4
  71.     gpio_set_out(PIN_4);
  72.     gpio_clr_pin(PIN_4);
  73. #endif
  74. #ifdef PIN_5
  75.     gpio_set_out(PIN_5);
  76.     gpio_clr_pin(PIN_5);
  77. #endif
  78. #ifdef PIN_6
  79.     gpio_set_out(PIN_6);
  80.     gpio_clr_pin(PIN_6);
  81. #endif
  82. #ifdef PIN_7
  83.     gpio_set_out(PIN_7);
  84.     gpio_clr_pin(PIN_7);
  85. #endif
  86.    
  87.     Timer_SetTimeout(act_softPWM_TIMER, resolution, TimerTypeFreeRunning, NULL);
  88.     // to use PCINt lib, call this function: (the callback function look as a timer callback function)
  89.     // Pcint_SetCallbackPin(act_softPWM_PCINT, EXP_C , &act_softPWM_pcint_callback);
  90.  
  91. }
  92.  
  93. void act_softPWM_Process(void)
  94. {
  95.     if (Timer_Expired(act_softPWM_TIMER)) {
  96.         currentTimer++;
  97.         if (currentTimer == 10) {
  98.             currentTimer=0;
  99.             #ifdef PIN_0
  100.                 gpio_set_pin(PIN_0);
  101.             #endif
  102.             #ifdef PIN_1
  103.                 gpio_set_pin(PIN_1);
  104.             #endif
  105.             #ifdef PIN_2
  106.                 gpio_set_pin(PIN_2);
  107.             #endif
  108.             #ifdef PIN_3
  109.                 gpio_set_pin(PIN_3);
  110.             #endif
  111.             #ifdef PIN_4
  112.                 gpio_set_pin(PIN_4);
  113.             #endif
  114.             #ifdef PIN_5
  115.                 gpio_set_pin(PIN_5);
  116.             #endif
  117.             #ifdef PIN_6
  118.                 gpio_set_pin(PIN_6);
  119.             #endif
  120.             #ifdef PIN_7
  121.                 gpio_set_pin(PIN_7);
  122.             #endif
  123.         }
  124.         #ifdef PIN_0
  125.         if (currentTimer >= pwmValue[0]) {
  126.             gpio_clr_pin(PIN_0);
  127.         }
  128.         #endif
  129.         #ifdef PIN_1
  130.         if (currentTimer >= pwmValue[1]) {
  131.             gpio_clr_pin(PIN_1);
  132.         }
  133.         #endif
  134.         #ifdef PIN_2
  135.         if (currentTimer >= pwmValue[2]) {
  136.             gpio_clr_pin(PIN_2);
  137.         }
  138.         #endif
  139.         #ifdef PIN_3
  140.         if (currentTimer >= pwmValue[3]) {
  141.             gpio_clr_pin(PIN_3);
  142.         }
  143.         #endif
  144.         #ifdef PIN_4
  145.         if (currentTimer >= pwmValue[4]) {
  146.             gpio_clr_pin(PIN_4);
  147.         }
  148.         #endif
  149.         #ifdef PIN_5
  150.         if (currentTimer >= pwmValue[5]) {
  151.             gpio_clr_pin(PIN_5);
  152.         }
  153.         #endif
  154.         #ifdef PIN_6
  155.         if (currentTimer >= pwmValue[6]) {
  156.             gpio_clr_pin(PIN_6);
  157.         }
  158.         #endif
  159.         #ifdef PIN_7
  160.         if (currentTimer >= pwmValue[7]) {
  161.             gpio_clr_pin(PIN_7);
  162.         }
  163.         #endif
  164.     }
  165.     if (Timer_Expired(act_softPWM_SEND_TIMER)) {
  166.         while(1)
  167.         {
  168.             if (0
  169.                 #ifdef PIN_0
  170.                 || currentSendChannelId == 0
  171.                 #endif
  172.                 #ifdef PIN_1
  173.                 || currentSendChannelId == 1
  174.                 #endif
  175.                 #ifdef PIN_2
  176.                 || currentSendChannelId == 2
  177.                 #endif
  178.                 #ifdef PIN_3
  179.                 || currentSendChannelId == 3
  180.                 #endif
  181.                 #ifdef PIN_4
  182.                 || currentSendChannelId == 4
  183.                 #endif
  184.                 #ifdef PIN_5
  185.                 || currentSendChannelId == 5
  186.                 #endif
  187.                 #ifdef PIN_6
  188.                 || currentSendChannelId == 6
  189.                 #endif
  190.                 #ifdef PIN_7
  191.                 || currentSendChannelId == 7
  192.                 #endif
  193.             ) {
  194.                 break;
  195.             }
  196.             currentSendChannelId++;
  197.             if (currentSendChannelId >= NUMBEROFCHANNELS)
  198.                 currentSendChannelId=0;
  199.         }
  200.         StdCan_Msg_t txMsg;
  201.         StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
  202.         StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  203.         txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_SOFTPWM;
  204.         txMsg.Header.ModuleId = act_softPWM_ID;
  205.         txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PWM;
  206.         txMsg.Length = 3;
  207.         txMsg.Data[0] = currentSendChannelId;
  208.         txMsg.Data[1] = (pwmValue[currentSendChannelId]>>8)&0xff;
  209.         txMsg.Data[2] = (pwmValue[currentSendChannelId])&0xff;
  210.         StdCan_Put(&txMsg);
  211.         currentSendChannelId++;
  212.         if (currentSendChannelId >= NUMBEROFCHANNELS)
  213.             currentSendChannelId=0;
  214.     }
  215. }
  216.  
  217.  
  218.  
  219. void act_softPWM_HandleMessage(StdCan_Msg_t *rxMsg)
  220. {
  221.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT &&
  222.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  223.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_SOFTPWM &&
  224.         rxMsg->Header.ModuleId == act_softPWM_ID)
  225.     {
  226.         switch (rxMsg->Header.Command)
  227.         {
  228.         case CAN_MODULE_CMD_PHYSICAL_PWM:
  229.             if (rxMsg->Length == 2)
  230.             {
  231.                 pwmValue[rxMsg->Data[0]] = ((uint32_t)rxMsg->Data[1]*maxTimer)/255;
  232.                 StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
  233.                 rxMsg->Length = 2;
  234.                 StdCan_Put(rxMsg);
  235.             } else if (rxMsg->Length == 1) {
  236.                 rxMsg->Data[1] = (pwmValue[rxMsg->Data[0]]*255/maxTimer);
  237.                 StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
  238.                 rxMsg->Length = 2;
  239.                 StdCan_Put(rxMsg);
  240.             }
  241.             break;
  242.         case CAN_MODULE_CMD_SOFTPWM_CONFIG:
  243.             maxTimer = (((uint16_t)rxMsg->Data[0])<<8) + rxMsg->Data[1];
  244.             resolution = rxMsg->Data[2];
  245.             Timer_SetTimeout(act_softPWM_TIMER, resolution, TimerTypeFreeRunning, NULL);
  246.             break;
  247.         case CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL:
  248.             if (rxMsg->Length > 0)
  249.             {
  250.                 act_softPWM_ReportInterval = rxMsg->Data[0];
  251. #ifdef act_softPWM_USEEEPROM
  252.                 eeprom_write_byte_crc(EEDATA.ReportInterval, act_softPWM_ReportInterval , WITH_CRC);
  253. #endif
  254.                 Timer_SetTimeout(act_softPWM_SEND_TIMER, act_softPWM_ReportInterval*1000 , TimerTypeFreeRunning, 0);
  255.             }
  256.  
  257.             StdCan_Msg_t txMsg;
  258.  
  259.             StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
  260.             StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  261.             txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_SOFTPWM;
  262.             txMsg.Header.ModuleId = act_softPWM_ID;
  263.             txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL;
  264.             txMsg.Length = 1;
  265.             txMsg.Data[0] = act_softPWM_ReportInterval;
  266.             StdCan_Put(&txMsg);
  267.             break;
  268.         }
  269.     }
  270. }
  271.  
  272. void act_softPWM_List(uint8_t ModuleSequenceNumber)
  273. {
  274.     StdCan_Msg_t txMsg;
  275.    
  276.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT); ///TODO: Change this to the actual class type
  277.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  278.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_SOFTPWM; ///TODO: Change this to the actual module type
  279.     txMsg.Header.ModuleId = act_softPWM_ID;
  280.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  281.     txMsg.Length = 6;
  282.  
  283.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  284.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  285.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  286.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  287.    
  288.     txMsg.Data[4] = NUMBER_OF_MODULES;
  289.     txMsg.Data[5] = ModuleSequenceNumber;
  290.    
  291.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  292. }
  293.