Skip to content

Commit

Permalink
Merge pull request chipKIT32#573 from EmbeddedMan/master
Browse files Browse the repository at this point in the history
Fix for issue chipKIT32#572 - SoftPWMServo library update, no longer crashes w…
  • Loading branch information
EmbeddedMan committed Jun 19, 2015
2 parents f635c71 + 127e5f4 commit f601945
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
51 changes: 45 additions & 6 deletions hardware/pic32/libraries/SoftPWMServo/SoftPWMServo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@
* Re-wrote ISR and helper functions to utilize the new core timer
attach() and detach() functions that Keith put into wiring.c
02/07/2013 <GeneApperson>:
* Removed dependency on Microchip plib library.
12/21/2013 <BrianSchmalz>;
* Removed dependency on Microchip plib library.
12/21/2013 <BrianSchmalz>:
* Fixed bug that caused glitches every 107 seconds.
06/19/2015 <BrianSchmalz>:
* Fixed bug that crashed library when non-existant pin was passed in (chipKIT32-MAX #572)
*/

/* Note: plib.h must be included before WProgram.h. There is a fundamental
** incompatibility between GenericTypedefs.h (included by plib.h) and Print.h
** (included by WProgram.h) on the declaration of the symbol BYTE.
*/

#define OPT_BOARD_INTERNAL
#include <WProgram.h>
Expand Down Expand Up @@ -157,6 +155,11 @@ int32_t SoftPWMServoUnload(void)
// Enable SoftPWM functionality on a particular pin number
int32_t SoftPWMServoPinEnable(uint32_t Pin, bool PinType)
{
if (Pin >= SOFTPWMSERVO_MAX_PINS)
{
return SOFTPWMSERVO_ERROR;
}

// If user has not already enabled this pin for SoftPWM, then initialize it
if (Chan[InactiveBuffer][Pin].SetPort == NULL)
{
Expand Down Expand Up @@ -190,6 +193,12 @@ int32_t SoftPWMServoPinEnable(uint32_t Pin, bool PinType)
int32_t SoftPWMServoPinDisable(uint32_t Pin)
{
int32_t intr;

if (Pin >= SOFTPWMSERVO_MAX_PINS)
{
return SOFTPWMSERVO_ERROR;
}

intr = disableInterrupts();
CopyBuffers();

Expand All @@ -213,6 +222,11 @@ int32_t SoftPWMServoRawWrite(uint32_t Pin, uint32_t Value, bool PinType)
int i;
int32_t intr;

if (Pin >= SOFTPWMSERVO_MAX_PINS)
{
return SOFTPWMSERVO_ERROR;
}

// Insert our ISR handler, if it's not already there
if (!Initalized)
{
Expand Down Expand Up @@ -271,6 +285,11 @@ int32_t SoftPWMServoRawWrite(uint32_t Pin, uint32_t Value, bool PinType)
// Return SOFTPWM_ERROR if pin not enabled
int32_t SoftPWMServoRawRead(uint32_t Pin)
{
if (Pin >= SOFTPWMSERVO_MAX_PINS)
{
return SOFTPWMSERVO_ERROR;
}

if (Chan[InactiveBuffer][Pin].SetPort == NULL)
{
return SOFTPWMSERVO_ERROR;
Expand All @@ -285,6 +304,11 @@ int32_t SoftPWMServoRawRead(uint32_t Pin)
// Pin is the pin number being changed
int32_t SoftPWMServoServoWrite(uint32_t Pin, float Value)
{
if (Pin >= SOFTPWMSERVO_MAX_PINS)
{
return SOFTPWMSERVO_ERROR;
}

SoftPWMServoRawWrite(Pin, usToTicks(Value), SOFTPWMSERVO_SERVO);

return SOFTPWMSERVO_OK;
Expand All @@ -293,6 +317,11 @@ int32_t SoftPWMServoServoWrite(uint32_t Pin, float Value)
// Convert from 8-bit percentage to absolute 40MHz tick units
int32_t SoftPWMServoPWMWrite(uint32_t Pin, uint8_t Value)
{
if (Pin >= SOFTPWMSERVO_MAX_PINS)
{
return SOFTPWMSERVO_ERROR;
}

SoftPWMServoRawWrite(Pin, (((uint32_t)Value * FrameTime)/255), SOFTPWMSERVO_PWM);

return SOFTPWMSERVO_OK;
Expand All @@ -301,12 +330,22 @@ int32_t SoftPWMServoPWMWrite(uint32_t Pin, uint8_t Value)
// Read the current time, for this Pin, in us (as a float)
float SoftPWMServoServoRead(uint32_t Pin)
{
if (Pin >= SOFTPWMSERVO_MAX_PINS)
{
return (float)SOFTPWMSERVO_ERROR;
}

return (ticksToUs(SoftPWMServoRawRead(Pin)));
}

// Return an 8-bit percentage based on PWMValue
int8_t SoftPWMServoPWMRead(uint32_t Pin)
{
if (Pin >= SOFTPWMSERVO_MAX_PINS)
{
return SOFTPWMSERVO_ERROR;
}

return ((SoftPWMServoRawRead(Pin) * 255)/FrameTime);
}

Expand Down
4 changes: 2 additions & 2 deletions hardware/pic32/libraries/SoftPWMServo/SoftPWMServo.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@

#include <inttypes.h>

#define SOFTPWMSERVO_VERSION 1.1 // software version of this library
#define SOFTPWMSERVO_MAX_PINS (86) // Max number of pins the library can handle
#define SOFTPWMSERVO_VERSION 1.2 // software version of this library
#define SOFTPWMSERVO_MAX_PINS (NUM_DIGITAL_PINS) // Max number of pins the library can handle (from variant Board_Defs.h)
#define SOFTPWMSERVO_ERROR (-1) // Returned when a function fails
#define SOFTPWMSERVO_OK (0) // Returned when a function passes
#define SOFTPWMSERVO_SERVO (1) // Used to enable a pin for servo operation
Expand Down

0 comments on commit f601945

Please sign in to comment.