adding HardwarePWM support

This commit is contained in:
hathach committed 2017-03-24 15:57:47 +07:00
1 parent a3afb48e7f
commit 953c55b218
9 files changed
+343 -3

No files matched your search

+163
View File
@@ -0,0 +1,163 @@
/**************************************************************************/
/*!
@file HardwarePWM.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 "HardwarePWM.h"
HardwarePWM PWM0(NRF_PWM0);
HardwarePWM PWM1(NRF_PWM1);
HardwarePWM PWM2(NRF_PWM2);
HardwarePWM* PWMx[3] = { &PWM0, &PWM1, &PWM2 };
HardwarePWM::HardwarePWM(NRF_PWM_Type* pwm)
{
_pwm = pwm;
_count = 0;
arrclr(_seq0);
// Initialize Registers
_pwm->ENABLE = 0;
_pwm->MODE = PWM_MODE_UPDOWN_Up;
_pwm->COUNTERTOP = bit(8) - 1; // default is 8 bit like STD Arduino
_pwm->PRESCALER = PWM_PRESCALER_PRESCALER_DIV_1; // 16MHz
_pwm->DECODER = PWM_DECODER_LOAD_Individual;
_pwm->LOOP = 0;
_pwm->SEQ[0].PTR = (uint32_t) _seq0;
_pwm->SEQ[0].CNT = MAX_CHANNELS; // default mode is Individual --> count must be 4
_pwm->SEQ[0].REFRESH = 0;
_pwm->SEQ[0].ENDDELAY = 0;
_pwm->SEQ[1].PTR = 0;
_pwm->SEQ[1].CNT = 0;
_pwm->SEQ[1].REFRESH = 0;
_pwm->SEQ[1].ENDDELAY = 0;
for(int i=0; i<MAX_CHANNELS; i++) _pwm->PSEL.OUT[i] = 0xFFFFFFFF;
}
void HardwarePWM::printInfo(void)
{
PRINT_HEX( (uint32_t) _pwm);
PRINT_INT(_pwm->ENABLE);
PRINT_INT(_pwm->MODE);
PRINT_INT(_pwm->COUNTERTOP);
PRINT_INT(_pwm->PRESCALER);
PRINT_INT(_pwm->DECODER);
PRINT_INT(_pwm->LOOP);
Serial.println();
PRINT_HEX(_pwm->SEQ[0].PTR);
PRINT_INT(_pwm->SEQ[0].CNT);
PRINT_INT(_pwm->SEQ[0].REFRESH);
PRINT_INT(_pwm->SEQ[0].ENDDELAY);
Serial.println();
PRINT_INT(_count);
for(int i=0; i<MAX_CHANNELS; i++)
{
cprintf("%d: pin = %d, value = %04X\n", i, _pwm->PSEL.OUT[i], _seq0[i]);
}
Serial.println();
}
void HardwarePWM::setResolution(uint8_t bitnum)
{
bitnum = min8(bitnum, 15); // max is 15 bit
_pwm->COUNTERTOP = bit(bitnum) - 1;
}
bool HardwarePWM::addPin(uint8_t pin)
{
VERIFY( isPinValid(pin) && (_count < MAX_CHANNELS) );
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
if ( enabled() )
{
// disable before changing PSEL
disable();
_pwm->PSEL.OUT[_count++] = pin;
enable();
}else
{
_pwm->PSEL.OUT[_count++] = pin;
}
return true;
}
void HardwarePWM::start(void)
{
// update sequence count
_pwm->SEQ[0].CNT = MAX_CHANNELS;
// Unmark stopped
// _pwm->EVENTS_STOPPED = 0;
// start sequence
_pwm->TASKS_SEQSTART[0] = 1;
}
void HardwarePWM::stop(void)
{
// Already stopped
// if (_pwm->EVENTS_STOPPED) return ;
_pwm->EVENTS_STOPPED = 0;
_pwm->TASKS_STOP = 1;
while( !_pwm->EVENTS_STOPPED ) yield();
}
bool HardwarePWM::writeChannel(uint8_t ch, uint16_t value, bool inverted )
{
VERIFY( ch < _count );
_seq0[ch] = value | (inverted ? 0 : bit(15));
return true;
}
bool HardwarePWM::writePin(uint8_t pin, uint16_t value, bool inverted)
{
int ch = pin2channel(pin);
VERIFY( ch >= 0 );
return writeChannel(ch, value, inverted);
}
+95
View File
@@ -0,0 +1,95 @@
/**************************************************************************/
/*!
@file HardwarePWM.h
@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.
*/
/**************************************************************************/
#ifndef HARDWAREPWM_H_
#define HARDWAREPWM_H_
#include "Arduino.h"
#include "nrf.h"
class HardwarePWM
{
private:
enum { MAX_CHANNELS = 4 }; // Max channel per group
NRF_PWM_Type* _pwm;
uint8_t _count;
uint16_t _seq0[MAX_CHANNELS];
public:
HardwarePWM(NRF_PWM_Type* pwm);
// Enable & Disable module
void enable (void) ATTR_ALWAYS_INLINE { _pwm->ENABLE = 1; }
void disable(void) ATTR_ALWAYS_INLINE { _pwm->ENABLE = 0; }
bool enabled(void) ATTR_ALWAYS_INLINE { return _pwm->ENABLE; }
// Configure
void setResolution(uint8_t bitnum);
// void set
// Generate PWM
void start(void);
void stop (void);
bool addPin (uint8_t pin);
int pin2channel(uint8_t pin) ATTR_ALWAYS_INLINE
{
for(int i=0; i<_count; i++)
{
if ( _pwm->PSEL.OUT[i] == pin ) return i;
}
return (-1);
}
bool checkPin(uint8_t pin) ATTR_ALWAYS_INLINE
{
return pin2channel(pin) >= 0;
}
bool writePin (uint8_t pin, uint16_t value, bool inverted = false);
bool writeChannel(uint8_t ch , uint16_t value, bool inverted = false);
void printInfo(void);
};
extern HardwarePWM PWM0;
extern HardwarePWM PWM1;
extern HardwarePWM PWM2;
extern HardwarePWM* PWMx[3];
#endif /* HARDWAREPWM_H_ */
+1
View File
@@ -19,6 +19,7 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
+2
View File
@@ -45,6 +45,8 @@
#define memclr(buffer, size) memset(buffer, 0, size)
#define varclr(_var) memclr(_var, sizeof(*(_var)))
#define arrclr(_arr) memclr(_arr, sizeof(_arr))
#define arrcount(_arr) ( sizeof(_arr) / sizeof(_arr[0]) )
#define __swap32(x) __REV(x) ///< built-in function to swap Endian of 32-bit number
+4
View File
@@ -22,9 +22,13 @@
#include <stdint.h>
#ifdef __cplusplus
#include "HardwarePWM.h"
extern "C" {
#endif
/*
* \brief SAMD products have only one reference for ADC
*/
+23
View File
@@ -73,4 +73,27 @@ reset KEYWORD2
#######################################
INPUT_PULLDOWN LITERAL1
PIN_A0 LITERAL1
PIN_A1 LITERAL1
PIN_A2 LITERAL1
PIN_A3 LITERAL1
PIN_A4 LITERAL1
PIN_A5 LITERAL1
PIN_A6 LITERAL1
PIN_A7 LITERAL1
PIN_AREF LITERAL1
PIN_DFU LITERAL1
PIN_NFC1 LITERAL1
PIN_NFC2 LITERAL1
PIN_SERIAL_RX LITERAL1
PIN_SERIAL_TX LITERAL1
PIN_SPI_MISO LITERAL1
PIN_SPI_MOSI LITERAL1
PIN_SPI_SCK LITERAL1
PIN_WIRE_SDA LITERAL1
PIN_WIRE_SCL LITERAL1
@@ -0,0 +1,47 @@
/*********************************************************************
This is an example for our Feather Bluefruit modules
Pick one up today in the adafruit shop!
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in
any redistribution
*********************************************************************/
#include <Arduino.h>
//int pin = PIN_DFU;
int pin = LED_BLUE;
/**************************************************************************/
/*!
@brief The setup function runs once when reset the board
*/
/**************************************************************************/
void setup()
{
Serial.begin(115200);
analogWrite(LED_RED, 50);
PWM0.printInfo();
PWM1.addPin(pin);
PWM1.enable();
PWM1.writePin(pin, 50);
PWM1.start();
PWM1.printInfo();
}
/**************************************************************************/
/*!
@brief The loop function runs over and over again forever
*/
/**************************************************************************/
void loop()
{
}
+3 -3
View File
@@ -22,9 +22,6 @@
#include "wiring_digital.h"
#include <nrf.h>
// Must match temp register in bootloader
#define BOOTLOADER_VERSION_REGISTER NRF_TIMER2->CC[0]
const uint32_t g_ADigitalPinMap[] = {
// D0 - D7
0, // xtal 1
@@ -71,7 +68,10 @@ const uint32_t g_ADigitalPinMap[] = {
};
// Must match temp register in bootloader
#define BOOTLOADER_VERSION_REGISTER NRF_TIMER2->CC[0]
uint32_t bootloaderVersion = 0;
void initVariant()
{
bootloaderVersion = BOOTLOADER_VERSION_REGISTER;
+5
View File
@@ -122,6 +122,11 @@ static const uint8_t SCK = PIN_SPI_SCK ;
*/
extern uint32_t bootloaderVersion;
static inline bool isPinValid(uint32_t pin)
{
return (pin < PINS_COUNT) && (pin != PIN_SERIAL_RX) && (pin != PIN_SERIAL_TX);
}
#ifdef __cplusplus
}
#endif