Subversion Repositories HomeAutomation

Rev

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