implement analogWrite() and analogWriteResolution() with HardwarePWM

This commit is contained in:
hathach committed 2017-03-25 01:16:53 +07:00
1 parent b77e907702
commit 774311e4b8
4 files changed
+101 -5

No files matched your search

+11
View File
@@ -110,10 +110,21 @@ void HardwarePWM::setClockDiv(uint8_t div)
_pwm->PRESCALER = div;
}
/**
* Add pin to this group.
* @param pin Pin to add
* @return true if add succeeded, or pin is already added
*/
bool HardwarePWM::addPin(uint8_t pin)
{
VERIFY( isPinValid(pin) && (_count < MAX_CHANNELS) );
// Check if pin is already configured
for(uint8_t i=0; i<_count; i++)
{
if (_pwm->PSEL.OUT[i] == pin) return true;
}
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
+82
View File
@@ -0,0 +1,82 @@
/**************************************************************************/
/*!
@file wiring_analog.cpp
@author hathach
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2017, Adafruit Industries (adafruit.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**************************************************************************/
#include "Arduino.h"
/* Implement analogWrite() and analogWriteResolution() using
* HardwarePWM.
*
* Note: Arduino's core implement this in C linker. So do we
*/
extern "C"
{
/**
* This will apply to all PWM Hardware
*/
void analogWriteResolution( uint8_t res )
{
PWM0.setResolution(res);
PWM1.setResolution(res);
PWM2.setResolution(res);
}
/**
* Generate PWM without pre-configured. this function will
* configure pin to available HardwarePWM and start it if not started
*
* @param pin
* @param value
*/
void analogWrite( uint32_t pin, uint32_t value )
{
for(int i=0; i<3; i++)
{
// If pin is added (already) in this group
if ( PWMx[i]->addPin(pin) )
{
// begin if not previously
if ( !PWMx[i]->begun() ) PWMx[i]->begin();
PWMx[i]->writePin(pin, value);
}
}
}
}
+1 -4
View File
@@ -22,10 +22,7 @@
#include <stdint.h>
#ifdef __cplusplus
#include "HardwarePWM.h"
extern "C" {
#endif
@@ -93,7 +90,7 @@ extern void analogReadResolution(int res);
*
* \param res
*/
extern void analogWriteResolution(int res);
extern void analogWriteResolution(uint8_t res);
extern void analogOutputInit( void ) ;
+7 -1
View File
@@ -31,6 +31,7 @@ extern "C" {
static uint32_t saadcReference = SAADC_CH_CONFIG_REFSEL_Internal;
static uint32_t saadcGain = SAADC_CH_CONFIG_GAIN_Gain1_6;
#if 0
#define PWM_COUNT 3
static NRF_PWM_Type* pwms[PWM_COUNT] = {
@@ -45,19 +46,22 @@ static uint32_t pwmChannelPins[PWM_COUNT] = {
0xFFFFFFFF
};
static uint16_t pwmChannelSequence[PWM_COUNT];
#endif
static int readResolution = 10;
static int writeResolution = 8;
//static int writeResolution = 8;
void analogReadResolution( int res )
{
readResolution = res;
}
#if 0
void analogWriteResolution( int res )
{
writeResolution = res;
}
#endif
static inline uint32_t mapResolution( uint32_t value, uint32_t from, uint32_t to )
{
@@ -228,6 +232,7 @@ uint32_t analogRead( uint32_t ulPin )
return mapResolution(value, resolution, readResolution);
}
#if 0
// Right now, PWM output only works on the pins with
// hardware support. These are defined in the appropriate
// pins_*.c file. For the rest of the pins, we default
@@ -267,6 +272,7 @@ void analogWrite( uint32_t ulPin, uint32_t ulValue )
}
}
}
#endif
#ifdef __cplusplus
}