Skip to content

Commit

Permalink
adding hardwaret timer and fixing PWM pins errors (#71)
Browse files Browse the repository at this point in the history
* add yield(default weak)

* 1.add hardware timer
2.fix PWM pins error
  • Loading branch information
TianpeiLee committed Mar 11, 2024
1 parent d77f8e0 commit c40c5c8
Show file tree
Hide file tree
Showing 22 changed files with 2,652 additions and 448 deletions.
1,780 changes: 1,780 additions & 0 deletions cores/arduino/HardwareTimer.cpp

Large diffs are not rendered by default.

197 changes: 197 additions & 0 deletions cores/arduino/HardwareTimer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/*
Copyright (c) 2017 Daniel Fekete
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Copyright (c) 2019 STMicroelectronics
Modified to support Arduino_Core_STM32
Modified by TempersLee to support Adruino_Core_CH32
*/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef HARDWARETIMER_H_
#define HARDWARETIMER_H_

/* Includes ------------------------------------------------------------------*/
#include "timer.h"
#include "ch32yyxx_tim.h"

#if defined(TIM_MODULE_ENABLED) && !defined(TIM_MODULE_ONLY)

#define TIMER_CHANNELS 4 // channel5 and channel 6 are not considered here has they don't have gpio output and they don't have interrupt


#define TIM_CHANNEL_CH1 TIM_CC1E /*!< Timer input/output channel 1 */
#define TIM_CHANNEL_CH1N TIM_CC1NE /*!< Timer complementary output channel 1 */
#define TIM_CHANNEL_CH2 TIM_CC2E /*!< Timer input/output channel 2 */
#define TIM_CHANNEL_CH2N TIM_CC2NE /*!< Timer complementary output channel 2 */
#define TIM_CHANNEL_CH3 TIM_CC3E /*!< Timer input/output channel 3 */
#define TIM_CHANNEL_CH3N TIM_CC3NE /*!< Timer complementary output channel 3 */
#define TIM_CHANNEL_CH4 TIM_CC4E /*!< Timer input/output channel 4 */



typedef enum {
TIMER_DISABLED, // == TIM_OCMODE_TIMING no output, useful for only-interrupt
// Output Compare
TIMER_OUTPUT_COMPARE, // == Obsolete, use TIMER_DISABLED instead. Kept for compatibility reason
TIMER_OUTPUT_COMPARE_ACTIVE, // == TIM_OCMODE_ACTIVE pin is set high when counter == channel compare
TIMER_OUTPUT_COMPARE_INACTIVE, // == TIM_OCMODE_INACTIVE pin is set low when counter == channel compare
TIMER_OUTPUT_COMPARE_TOGGLE, // == TIM_OCMODE_TOGGLE pin toggles when counter == channel compare
TIMER_OUTPUT_COMPARE_PWM1, // == TIM_OCMODE_PWM1 pin high when counter < channel compare, low otherwise
TIMER_OUTPUT_COMPARE_PWM2, // == TIM_OCMODE_PWM2 pin low when counter < channel compare, high otherwise
TIMER_OUTPUT_COMPARE_FORCED_ACTIVE, // == TIM_OCMODE_FORCED_ACTIVE pin always high
TIMER_OUTPUT_COMPARE_FORCED_INACTIVE, // == TIM_OCMODE_FORCED_INACTIVE pin always low

//Input capture
TIMER_INPUT_CAPTURE_RISING, // == TIM_INPUTCHANNELPOLARITY_RISING
TIMER_INPUT_CAPTURE_FALLING, // == TIM_INPUTCHANNELPOLARITY_FALLING
TIMER_INPUT_CAPTURE_BOTHEDGE, // == TIM_INPUTCHANNELPOLARITY_BOTHEDGE

// Used 2 channels for a single pin. One channel in TIM_INPUTCHANNELPOLARITY_RISING another channel in TIM_INPUTCHANNELPOLARITY_FALLING.
// Channels must be used by pair: CH1 with CH2, or CH3 with CH4
// This mode is very useful for Frequency and Dutycycle measurement
TIMER_INPUT_FREQ_DUTY_MEASUREMENT,

TIMER_NOT_USED = 0xFFFF // This must be the last item of this enum
} TimerModes_t;

typedef enum {
TICK_FORMAT, // default
MICROSEC_FORMAT,
HERTZ_FORMAT,
} TimerFormat_t;

typedef enum {
RESOLUTION_1B_COMPARE_FORMAT = 1, // used for Dutycycle: [0 .. 1]
RESOLUTION_2B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 3]
RESOLUTION_3B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 7]
RESOLUTION_4B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 15]
RESOLUTION_5B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 31]
RESOLUTION_6B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 63]
RESOLUTION_7B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 127]
RESOLUTION_8B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 255]
RESOLUTION_9B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 511]
RESOLUTION_10B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 1023]
RESOLUTION_11B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 2047]
RESOLUTION_12B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 4095]
RESOLUTION_13B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 8191]
RESOLUTION_14B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 16383]
RESOLUTION_15B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 32767]
RESOLUTION_16B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 65535]

TICK_COMPARE_FORMAT = 0x80, // default
MICROSEC_COMPARE_FORMAT,
HERTZ_COMPARE_FORMAT,
PERCENT_COMPARE_FORMAT, // used for Dutycycle
} TimerCompareFormat_t;

#ifdef __cplusplus

#include <functional>
using callback_function_t = std::function<void(void)>;

/* Class --------------------------------------------------------*/
class HardwareTimer {
public:
HardwareTimer();
HardwareTimer(TIM_TypeDef *instance);
~HardwareTimer(); // destructor

void setup(TIM_TypeDef *instance); // Setup, only needed if no instance was passed to the constructor

void pause(void); // Pause counter and all output channels
void pauseChannel(uint32_t channel); // Timer is still running but channel (output and interrupt) is disabled
void resume(void); // Resume counter and all output channels
void resumeChannel(uint32_t channel); // Resume only one channel

void setPrescaleFactor(uint32_t prescaler); // set prescaler register (which is factor value - 1)
uint32_t getPrescaleFactor();

void setOverflow(uint32_t val, TimerFormat_t format = TICK_FORMAT); // set AutoReload register depending on format provided
uint32_t getOverflow(TimerFormat_t format = TICK_FORMAT); // return overflow depending on format provided

void setPWM(uint32_t channel, PinName pin, uint32_t frequency, uint32_t dutycycle, callback_function_t PeriodCallback = nullptr, callback_function_t CompareCallback = nullptr); // Set all in one command freq in HZ, Duty in percentage. Including both interrupt.
void setPWM(uint32_t channel, uint32_t pin, uint32_t frequency, uint32_t dutycycle, callback_function_t PeriodCallback = nullptr, callback_function_t CompareCallback = nullptr);

void setCount(uint32_t val, TimerFormat_t format = TICK_FORMAT); // set timer counter to value 'val' depending on format provided
uint32_t getCount(TimerFormat_t format = TICK_FORMAT); // return current counter value of timer depending on format provided

void setMode(uint32_t channel, TimerModes_t mode, PinName pin = NC); // Configure timer channel with specified mode on specified pin if available
void setMode(uint32_t channel, TimerModes_t mode, uint32_t pin);

TimerModes_t getMode(uint32_t channel); // Retrieve configured mode

void setPreloadEnable(bool value); // Configure overflow preload enable setting

uint32_t getCaptureCompare(uint32_t channel, TimerCompareFormat_t format = TICK_COMPARE_FORMAT); // return Capture/Compare register value of specified channel depending on format provided
void setCaptureCompare(uint32_t channel, uint32_t compare, TimerCompareFormat_t format = TICK_COMPARE_FORMAT); // set Compare register value of specified channel depending on format provided

void setInterruptPriority(uint32_t preemptPriority, uint32_t subPriority); // set interrupt priority

//Add interrupt to period update
void attachInterrupt(callback_function_t callback); // Attach interrupt callback which will be called upon update event (timer rollover)
void detachInterrupt(); // remove interrupt callback which was attached to update event
bool hasInterrupt(); //returns true if a timer rollover interrupt has already been set
//Add interrupt to capture/compare channel
void attachInterrupt(uint32_t channel, callback_function_t callback); // Attach interrupt callback which will be called upon compare match event of specified channel
void detachInterrupt(uint32_t channel); // remove interrupt callback which was attached to compare match event of specified channel
bool hasInterrupt(uint32_t channel); //returns true if an interrupt has already been set on the channel compare match
void timerHandleDeinit(); // Timer deinitialization

// Refresh() is useful while timer is running after some registers update
void refresh(void); // Generate update event to force all registers (Autoreload, prescaler, compare) to be taken into account

uint32_t getTimerClkFreq(); // return timer clock frequency in Hz.

static void captureCompareCallback(TIM_HandleTypeDef *htim); // Generic Capture and Compare callback which will call user callback
static void updateCallback(TIM_HandleTypeDef *htim); // Generic Update (rollover) callback which will call user callback

void updateRegistersIfNotRunning(TIM_TypeDef *TIMx); // Take into account registers update immediately if timer is not running,

bool isRunning(); // return true if HardwareTimer is running
bool isRunningChannel(uint32_t channel); // return true if channel is running

// The following function(s) are available for more advanced timer options
TIM_HandleTypeDef *getHandle(); // return the handle address for HAL related configuration
int getChannel(uint32_t channel);
int getLLChannel(uint32_t channel);
int getIT(uint32_t channel);
int getAssociatedChannel(uint32_t channel);
#if defined(TIM_CC1NE)
bool isComplementaryChannel[TIMER_CHANNELS];
#endif
void TIM_OC_ConfigChannel_Static(TIM_TypeDef *htim, TIM_OCInitTypeDef *sConfig, uint32_t Channel);
void TIM_IC_ConfigChannel_Static(TIM_TypeDef *htim, TIM_ICInitTypeDef *sConfig, uint32_t Channel);

private:
TimerModes_t _ChannelMode[TIMER_CHANNELS];
timerObj_t _timerObj;
callback_function_t callbacks[1 + TIMER_CHANNELS]; //Callbacks: 0 for update, 1-4 for channels. (channel5/channel6, if any, doesn't have interrupt)
};

extern timerObj_t *HardwareTimer_Handle[TIMER_NUM];

extern timer_index_t get_timer_index(TIM_TypeDef *htim);

#endif /* __cplusplus */

#endif // TIM_MODULE_ENABLED && TIM_MODULE_ONLY
#endif // HARDWARETIMER_H_
2 changes: 1 addition & 1 deletion cores/arduino/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// #include "dwt.h"
#include "hw_config.h"
// #include "otp.h"
// #include "timer.h"
#include "timer.h"
#include "uart.h"


Expand Down
5 changes: 4 additions & 1 deletion cores/arduino/ch32/PinNamesTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ extern "C" {
#define CH_PIN_SPEED_MASK 0x03
#define CH_PIN_SPEED_SHIFT 6
#define CH_PIN_SPEED_BITS (CH_PIN_SPEED_MASK << CH_PIN_SPEED_SHIFT)
*/
#define CH_PIN_INV_MASK 0x01
#define CH_PIN_INV_SHIFT 20
#define CH_PIN_INV_BIT (CH_PIN_INV_MASK << CH_PIN_INV_SHIFT)
Expand All @@ -97,6 +97,7 @@ extern "C" {
#define CH_PIN_AN_CTRL_SHIFT 21
#define CH_PIN_ANALOG_CONTROL_BIT (CH_PIN_AN_CTRL_MASK << CH_PIN_AN_CTRL_SHIFT)

/*
#define CH_PIN_AN_CHAN_BANK_B_MASK 0x01
#define CH_PIN_AN_CHAN_BANK_B_SHIFT 22
#define CH_PIN_ANALOG_CHAN_BANK_B_BIT (CH_PIN_AN_CHAN_BANK_B_MASK << CH_PIN_AN_CHAN_BANK_B_SHIFT)
Expand All @@ -115,8 +116,10 @@ extern "C" {
#define CH_PIN_SPEED(X) (((X) >> CH_PIN_SPEED_SHIFT) & CH_PIN_SPEED_MASK)
#define CH_PIN_AFNUM(X) (((X) >> CH_PIN_AFNUM_SHIFT) & CH_PIN_AFNUM_MASK)
#define CH_PIN_CHANNEL(X) (((X) >> CH_PIN_CHAN_SHIFT) & CH_PIN_CHAN_MASK)
*/
#define CH_PIN_INVERTED(X) (((X) >> CH_PIN_INV_SHIFT) & CH_PIN_INV_MASK)
#define CH_PIN_ANALOG_CONTROL(X) (((X) >> CH_PIN_AN_CTRL_SHIFT) & CH_PIN_AN_CTRL_MASK)
/*
#define CH_PIN_ANALOG_CHANNEL_BANK_B(X) (((X) >> CH_PIN_AN_CHAN_BANK_B_SHIFT) & CH_PIN_AN_CHAN_BANK_B_MASK)
#define CH_PIN_MODE(X) ((CH_PIN_OD((X)) << 4) | \
(CH_PIN_FUNCTION((X)) & (~CH_PIN_OD_BITS)))
Expand Down
3 changes: 1 addition & 2 deletions cores/arduino/ch32/analog.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@
#include "ch32_def.h"
#include "PeripheralPins.h"
#include "variant.h"

// #include "HardwareTimer.h"
#include "HardwareTimer.h"

#ifdef __cplusplus
extern "C" {
Expand Down
Loading

0 comments on commit c40c5c8

Please sign in to comment.