Show
Ignore:
Timestamp:
07/31/08 13:16:09 (5 months ago)
Author:
liamstask
Message:

- incorporating additions from #64
- can more easily configure PWM channels - timing, alignment, etc.
- more & better doc for PWM

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • firmware/trunk/libraries/pwmout/pwmout.c

    r771 r774  
    324324} 
    325325 
    326 /** @} 
    327 */ 
    328  
    329326int PwmOut_Start( int index ) 
    330327{ 
     
    445442} 
    446443 
     444/** 
     445  Set the divider A value. 
     446  Contributed by TheStigg - http://www.makingthings.com/author/thestigg 
     447  @param val The value (between 0 and 255) 
     448  @return 0 on success. 
     449*/ 
     450int PwmOut_SetDividerAValue(int val) 
     451{ 
     452   if( val < 0 || val > 255 ) 
     453    return CONTROLLER_ERROR_ILLEGAL_PARAMETER_VALUE; 
     454    
     455   int v = Pwm_GetDividerA(); 
     456    
     457   //strip off the divider Value 
     458   v &= 0x00000f00; 
     459    
     460   //add on the new Value 
     461   v |= val; 
     462 
     463   return Pwm_SetDividerA( v ); 
     464} 
     465 
     466/** 
     467  Read the divider A value. 
     468  Contributed by TheStigg - http://www.makingthings.com/author/thestigg 
     469  @return the divider A value (0 - 255) 
     470  @see PwmOut_SetDividerAValue( ) 
     471*/ 
     472int PwmOut_GetDividerAValue() 
     473{ 
     474  return ( Pwm_GetDividerA() & 0x000000ff ); 
     475} 
     476 
     477/** 
     478  Set the divider A mux value. 
     479  @param mux The mux value (between 0 and 10) 
     480  Contributed by TheStigg - http://www.makingthings.com/author/thestigg 
     481  @return 0 on success. 
     482*/ 
     483int PwmOut_SetDividerAMux(int mux) 
     484{ 
     485   if( mux < 0 || mux > 10 ) 
     486    return CONTROLLER_ERROR_ILLEGAL_PARAMETER_VALUE; 
     487    
     488   int v = Pwm_GetDividerA(); 
     489    
     490   //strip off the divider Value 
     491   v &= 0x000000ff; 
     492    
     493   //add on the new Value 
     494   v |= ( mux << 8 ); 
     495 
     496   return Pwm_SetDividerA( v ); 
     497} 
     498/** 
     499  Read the divider A mux value. 
     500  Contributed by TheStigg - http://www.makingthings.com/author/thestigg 
     501  @return the divider A mux value (0 - 10) 
     502  @see PwmOut_SetDividerAMux( ) 
     503*/ 
     504int PwmOut_GetDividerAMux() 
     505{ 
     506  return ( ( Pwm_GetDividerA() >> 8 ) & 0x0000000f ); 
     507} 
     508 
     509/** 
     510  Set the divider B value. 
     511  Contributed by TheStigg - http://www.makingthings.com/author/thestigg 
     512  @param val The value (between 0 and 255) 
     513  @return 0 on success. 
     514*/ 
     515int PwmOut_SetDividerBValue(int val) 
     516{ 
     517   if( val < 0 || val > 255 ) 
     518    return CONTROLLER_ERROR_ILLEGAL_PARAMETER_VALUE; 
     519    
     520   int v = Pwm_GetDividerB(); 
     521    
     522   //strip off the divider Value 
     523   v &= 0x00000f00; 
     524    
     525   //add on the new Value 
     526   v |= val; 
     527 
     528   return Pwm_SetDividerA( v ); 
     529} 
     530 
     531/** 
     532  Read the divider B value. 
     533  Contributed by TheStigg - http://www.makingthings.com/author/thestigg 
     534  @return the divider B value (0 - 255) 
     535  @see PwmOut_SetDividerBValue( ) 
     536*/ 
     537int PwmOut_GetDividerBValue() 
     538{ 
     539  return ( Pwm_GetDividerB() & 0x000000ff ); 
     540} 
     541 
     542/** 
     543  Set the divider B mux value. 
     544  @param mux The mux value (between 0 and 10) 
     545  Contributed by TheStigg - http://www.makingthings.com/author/thestigg 
     546  @return 0 on success. 
     547*/ 
     548int PwmOut_SetDividerBMux(int mux) 
     549{ 
     550   if( mux < 0 || mux > 10 ) 
     551    return CONTROLLER_ERROR_ILLEGAL_PARAMETER_VALUE; 
     552    
     553   int v = Pwm_GetDividerB(); 
     554    
     555   //strip off the divider Value 
     556   v &= 0x000000ff; 
     557    
     558   //add on the new Value 
     559   v |= ( mux << 8 ); 
     560 
     561   return Pwm_SetDividerB( v ); 
     562} 
     563 
     564/** 
     565  Read the divider B mux value. 
     566  Contributed by TheStigg - http://www.makingthings.com/author/thestigg 
     567  @return the divider B mux value (0 - 10) 
     568  @see PwmOut_SetDividerBMux( ) 
     569*/ 
     570int PwmOut_GetDividerBMux() 
     571{ 
     572  return ( ( Pwm_GetDividerB() >> 8 ) & 0x0000000f ); 
     573} 
     574 
     575/** 
     576  Set the alignment of a channel's waveform. 
     577  Valid values are: 
     578  - 0 = Left Aligned (default) 
     579  - 1 = Center Aligned 
     580 
     581  Contributed by TheStigg - http://www.makingthings.com/author/thestigg 
     582  @param index The PWM channel (0-3) that you'd like to configure. 
     583  @param val The alignment value, as described above. 
     584  @return 0 on success. 
     585*/ 
     586int PwmOut_SetWaveformAlignment(int index, int val) 
     587{ 
     588  if ( index < 0 || index > 3 ) 
     589    return CONTROLLER_ERROR_ILLEGAL_INDEX; 
     590  if ( val > 1 || val < 0 ) 
     591    return CONTROLLER_ERROR_ILLEGAL_PARAMETER_VALUE; 
     592 
     593  int currval = Pwm_GetWaveformProperties( index ) & 0x00000006; //Leave bits 2 & 3 alone 
     594  currval |= val; 
     595  return Pwm_SetWaveformProperties( index, currval ); 
     596} 
     597 
     598/** 
     599  Read the configured waveform alignment for a given PWM channel. 
     600 
     601  Contributed by TheStigg - http://www.makingthings.com/author/thestigg 
     602  @param index The PWM channel (0-3) you'd like to read from. 
     603  @return the waveform alignment - see PwmOut_SetWaveformAlignment( ) 
     604*/ 
     605int PwmOut_GetWaveformAlignment( int index ) 
     606{ 
     607  int val = Pwm_GetWaveformProperties( index ); 
     608  if ( val < 0 ) 
     609    return val; 
     610   
     611  return val  & 0x00000001; 
     612} 
     613 
     614/** 
     615  Set the Waveform Polarity for a PWM channel. 
     616  Valid values are: 
     617   - 0 = Start Waveform Low 
     618   - 1 = Start Waveform High (default) 
     619 
     620  Contributed by TheStigg - http://www.makingthings.com/author/thestigg 
     621  @param index The PWM channel (0-3) that you'd like to configure. 
     622  @param val The value, as described above. 
     623  @return 0 on success. 
     624*/ 
     625int PwmOut_SetWaveformPolarity(int index, int val) 
     626{ 
     627  if ( index < 0 || index > 3 ) 
     628    return CONTROLLER_ERROR_ILLEGAL_INDEX; 
     629  if ( val > 1 || val < 0 ) 
     630    return CONTROLLER_ERROR_ILLEGAL_PARAMETER_VALUE; 
     631 
     632  int currval = Pwm_GetWaveformProperties( index ) & 0x00000005; //Leave bits 3 & 1 alone 
     633  currval |= (val << 1); 
     634  return Pwm_SetWaveformProperties( index, currval ); 
     635} 
     636 
     637/** 
     638  Read the waveform polarity for a given channel. 
     639 
     640  Contributed by TheStigg - http://www.makingthings.com/author/thestigg 
     641  @param index The PWM channel (0-3) that you'd like to read from. 
     642  @return The configured polarity for that channel. 
     643  @see PwmOut_SetWaveformPolarity( ) 
     644*/ 
     645int PwmOut_GetWaveformPolarity( int index ) 
     646{ 
     647  int val = Pwm_GetWaveformProperties( index ); 
     648  if ( val < 0 ) 
     649    return val; 
     650   
     651  return val  & 0x00000002; 
     652} 
     653 
     654/** @} 
     655*/ 
     656 
    447657#ifdef OSC 
    448658 
    449 /** \defgroup PWMOutOSC PWM Out - OSC 
     659/** \defgroup pwmout_osc PWM Out - OSC 
    450660  Generate PWM signals with the Application Board via OSC. 
    451661  \ingroup OSC 
     
    463673   
    464674  \section properties Properties 
    465   Each PWM Out has four properties: 
     675  Each PWM Out has the following properties: 
    466676  - duty 
    467677  - invA 
    468678  - invB 
    469679  - active 
    470  
    471   \par Duty 
     680  - dividerAValue 
     681  - dividerAMux 
     682  - dividerBValue 
     683  - dividerBMux 
     684  - clockSource 
     685  - alignment 
     686  - polarity 
     687 
     688  \subsection Duty 
    472689  The \b duty property corresponds to the duty at which a load connected to the output is being driven. 
    473690  This value can be both read and written.  The range of values expected by the board 
    474691  is from 0 - 1023. 
    475   \par 
    476   To generate a 75% on PWM signal, send a message like 
     692 
     693  To generate a 75% on PWM signal, send a message like 
    477694  \verbatim /pwmout/1/duty 768 \endverbatim 
    478695  Leave the argument value off to read the duty: 
    479696  \verbatim /pwmout/1/duty \endverbatim 
    480697   
    481   \par invA 
     698  \subsection invA 
    482699  The \b invA property corresponds to the inversion of the A channel of a PWM Out. 
    483700  This value can be both read and written, and the range of values expected is simply  
    484701  0 or 1.  1 means inverted and 0 means normal.  0 is the default. 
    485   \par 
     702   
    486703  To set the A channel of the second PWM Out as inverted, send the message 
    487704  \verbatim /pwmout/1/invA 1 \endverbatim 
    488705  Note that the A channel of PWM Out 1 is Digital Out 2. 
    489706   
    490   \par invB 
     707  \subsection invB 
    491708  The \b invB property corresponds to the inversion of the B channel of a PWM Out. 
    492709  This value can be both read and written, and the range of values expected is simply  
    493710  0 or 1.  1 means inverted and 0 means normal.  0 is the default. 
    494   \par 
     711   
    495712  To set the B channel of the fourth PWM Out back to normal, send the message 
    496713  \verbatim /pwmout/1/invB 0 \endverbatim 
    497714  Note that the A channel of PWM Out 1 is Digital Out 7. 
    498715   
    499   \par Active 
     716  \subsection Active 
    500717  The \b active property corresponds to the active state of the PWM Out. 
    501718  If the device is set to be active, no other tasks will be able to 
     
    504721  locked by sending a message like 
    505722  \verbatim /pwmout/0/active \endverbatim 
    506   \par 
     723   
    507724  If you're no longer using the PWM Out, it's a good idea to free the 2 Digital Outs by  
    508725  sending the message 
    509726  \verbatim /pwmout/0/active 0 \endverbatim 
     727 
     728  \section p_adjust Period adjustment of the PWM unit 
     729 
     730  The below values allow you to adjust the period length to all possible values as supported 
     731  by the physical limits of the Make Controller.  Before altering the values, it is good to understand 
     732  a little about the subsustem of the PWM module.  Please see the general introduction in the \ref Pwm section. 
     733 
     734  \subsection DividerBValue 
     735  The \b dividerXValue property corresponds to the linear divider value within clock divider module X of the  
     736  PWM Out.  This value can be between 0 and 255, and linearly divides the clock that is fed into it.  You can  
     737  change the incomming clock value by altering the DividerAMux parameter.  Higher linear divider values mean  
     738  a longer period. 
     739   
     740  Default value of DividerA is 4. Default value of DividerB is 0. 
     741 
     742  \subsection divs DividerAMux & DividerBMux 
     743  The \b dividerAMux and \b dividerBMux properties select the incoming clock value for divider module A or B (respectively) 
     744  of the PWM Out.  This value ranges between 0 and 10.  This is similar to the clock source value, eg. a DividerAMux  
     745  value of 5 = a clock rate of MasterClock/(2^5) 
     746   
     747  Increasing this value will substantially increase the period of the PWM.  Use the DividerXValue parameter  
     748  to more finely tune the value of the period.   
     749   
     750  Default value of MuxA is 4.  Default value of MuxB is 0.         
     751 
     752  \subsection ClockSource 
     753  The \b clockSource property selects the incoming clock value for the specified PWM channel. A clock  
     754  source of \b 0-10 represents the Master clock divided by 2 to the power of the Clock Source Value, eg. a clock  
     755  source value of 5 = a clock rate of MasterClock/(2^5).  A value of \b 11 sets the Clock source to be  
     756  generated by clock Divider A.  A value of \b 12 sets the Clock source to be generated by clock Divider B 
     757   
     758  The default value is 11 (Divider A). 
     759 
     760  \subsection WaveformAlignment 
     761  The \b waveformAlignment property chooses whether the channel is left aligned or center aligned. 
     762  The following values are valid: 
     763   - 0 = Left aligned 
     764   - 1 = Center aligned 
     765   
     766  A left aligned channel counts up to the maximum duty cycle, then resets the counter.  A center aligned  
     767  channel counts up to the maximum duty cycle, then counts down to zero, etc. The default is left aligned. 
     768 
     769  \subsection WaveformPolarity 
     770  The \b polarity property chooses whether the channel begins its period high or low.  The following values are valid: 
     771   - 0 = Normal Polarity 
     772   - 1 = Inverted Polarity 
     773   
     774  Normal polarity starts low, then goes high.  Inverted polarity starts high, then goes low.  The default is inverted polarity. 
    510775*/ 
    511776 
     
    517782// MUST end in zero 
    518783static char* PwmOutOsc_Name = "pwmout"; 
    519 static char* PwmOutOsc_PropertyNames[] = { "active", "duty", "invA", "invB", 0 }; // must have a trailing 0 
     784static char* PwmOutOsc_PropertyNames[] = { "active", "duty", "invA", "invB",  
     785             "dividerAValue", "dividerAMux", 
     786             "dividerBValue", "dividerBMux", 
     787             "clockSource", "alignment", 
     788             "polarity", 
     789             0 }; // must have a trailing 0 
    520790 
    521791int PwmOutOsc_PropertySet( int index, int property, int value ); 
     
    559829      PwmOut_SetInvertB( index, value ); 
    560830      break; 
     831    case 4: 
     832      PwmOut_SetDividerAValue(value); 
     833      break; 
     834    case 5: 
     835      PwmOut_SetDividerAMux(value); 
     836      break; 
     837    case 6: 
     838      PwmOut_SetDividerBValue(value); 
     839      break; 
     840    case 7: 
     841      PwmOut_SetDividerBMux(value); 
     842      break; 
     843    case 8: 
     844      Pwm_SetClockSource( index, value ); 
     845      break; 
     846    case 9: 
     847      PwmOut_SetWaveformAlignment( index, value ); 
     848      break; 
     849    case 10: 
     850      PwmOut_SetWaveformPolarity( index, value ); 
     851      break; 
    561852  } 
    562853  return CONTROLLER_OK; 
     
    581872      value = PwmOut_GetInvertB( index ); 
    582873      break; 
     874    case 4: 
     875      value = PwmOut_GetDividerAValue(); 
     876      break; 
     877    case 5: 
     878      value = PwmOut_GetDividerAMux(); 
     879      break; 
     880    case 6: 
     881      value = PwmOut_GetDividerBValue(); 
     882      break; 
     883    case 7: 
     884      value = PwmOut_GetDividerBMux(); 
     885      break; 
     886    case 8: 
     887      value = Pwm_GetClockSource( index ); 
     888      break; 
     889    case 9: 
     890      value = PwmOut_GetWaveformAlignment( index ); 
     891      break; 
     892    case 10: 
     893      value = PwmOut_GetWaveformPolarity( index ); 
     894      break; 
    583895  } 
    584896  return value;