diff options
Diffstat (limited to 'platform/drivers/inc/gpt.h')
-rw-r--r-- | platform/drivers/inc/gpt.h | 266 |
1 files changed, 135 insertions, 131 deletions
diff --git a/platform/drivers/inc/gpt.h b/platform/drivers/inc/gpt.h index 14c3b9e..3c95c1b 100644 --- a/platform/drivers/inc/gpt.h +++ b/platform/drivers/inc/gpt.h @@ -45,78 +45,70 @@ * Definitions ******************************************************************************/ -/*! - * @brief Clock source - */ -enum _gpt_clock_source { - gptClockSourceNone = 0U, /*!< No source selected.*/ - gptClockSourcePeriph = 1U, /*!< Use peripheral module clock.*/ - gptClockSourceLowFreq = 4U, /*!< Use 32K clock.*/ - gptClockSourceOsc = 5U /*!< Use 24M OSC clock.*/ +/*! @brief Clock source. */ +enum _gpt_clock_source +{ + gptClockSourceNone = 0U, /*!< No source selected.*/ + gptClockSourcePeriph = 1U, /*!< Use peripheral module clock.*/ + gptClockSourceLowFreq = 4U, /*!< Use 32 K clock.*/ + gptClockSourceOsc = 5U, /*!< Use 24 M OSC clock.*/ }; -/*! - * @brief Input capture channel number - */ -enum _gpt_input_capture_channel { - gptInputCaptureChannel1 = 0U, - gptInputCaptureChannel2 = 1U +/*! @brief Input capture channel number. */ +enum _gpt_input_capture_channel +{ + gptInputCaptureChannel1 = 0U, /*!< Input Capture Channel1.*/ + gptInputCaptureChannel2 = 1U, /*!< Input Capture Channel2.*/ }; -/*! - * @brief Input capture operation mode - */ -enum _gpt_input_operation_mode { - gptInputOperationDisabled = 0U, /*!< Don't capture.*/ - gptInputOperationRiseEdge = 1U, /*!< Capture on rising edge of input pin.*/ - gptInputOperationFallEdge = 2U, /*!< Capture on falling edge of input pin.*/ - gptInputOperationBothEdge = 3U /*!< Capture on both edges of input pin.*/ +/*! @brief Input capture operation mode. */ +enum _gpt_input_operation_mode +{ + gptInputOperationDisabled = 0U, /*!< Don't capture.*/ + gptInputOperationRiseEdge = 1U, /*!< Capture on rising edge of input pin.*/ + gptInputOperationFallEdge = 2U, /*!< Capture on falling edge of input pin.*/ + gptInputOperationBothEdge = 3U, /*!< Capture on both edges of input pin.*/ }; -/*! - * @brief Output compare channel number - */ -enum _gpt_output_compare_channel { - gptOutputCompareChannel1 = 0U, - gptOutputCompareChannel2 = 1U, - gptOutputCompareChannel3 = 2U +/*! @brief Output compare channel number. */ +enum _gpt_output_compare_channel +{ + gptOutputCompareChannel1 = 0U, /*!< Output Compare Channel1.*/ + gptOutputCompareChannel2 = 1U, /*!< Output Compare Channel2.*/ + gptOutputCompareChannel3 = 2U, /*!< Output Compare Channel3.*/ }; -/*! - * @brief Output compare operation mode - */ -enum _gpt_output_operation_mode { - gptOutputOperationDisconnected = 0U, /*!< Don't change output pin.*/ - gptOutputOperationToggle = 1U, /*!< Toggle output pin.*/ - gptOutputOperationClear = 2U, /*!< Set output pin low.*/ - gptOutputOperationSet = 3U, /*!< Set output pin high.*/ - gptOutputOperationActivelow = 4U /*!< Generate a active low pulse on output pin.*/ +/*! @brief Output compare operation mode. */ +enum _gpt_output_operation_mode +{ + gptOutputOperationDisconnected = 0U, /*!< Don't change output pin.*/ + gptOutputOperationToggle = 1U, /*!< Toggle output pin.*/ + gptOutputOperationClear = 2U, /*!< Set output pin low.*/ + gptOutputOperationSet = 3U, /*!< Set output pin high.*/ + gptOutputOperationActivelow = 4U, /*!< Generate a active low pulse on output pin.*/ }; -/*! - * @brief Status flag - */ -enum _gpt_status_flag { - gptStatusFlagOutputCompare1 = 1U << 0, /*!< Output compare channel 1 evevnt.*/ - gptStatusFlagOutputCompare2 = 1U << 1, /*!< Output compare channel 2 evevnt.*/ - gptStatusFlagOutputCompare3 = 1U << 2, /*!< Output compare channel 3 evevnt.*/ - gptStatusFlagInputCapture1 = 1U << 3, /*!< Capture channel 1 evevnt.*/ - gptStatusFlagInputCapture2 = 1U << 4, /*!< Capture channel 2 evevnt.*/ - gptStatusFlagRollOver = 1U << 5 /*!< Counter reaches maximum value and rolled over to 0 evevnt.*/ +/*! @brief Status flag. */ +enum _gpt_status_flag +{ + gptStatusFlagOutputCompare1 = 1U << 0, /*!< Output compare channel 1 event.*/ + gptStatusFlagOutputCompare2 = 1U << 1, /*!< Output compare channel 2 event.*/ + gptStatusFlagOutputCompare3 = 1U << 2, /*!< Output compare channel 3 event.*/ + gptStatusFlagInputCapture1 = 1U << 3, /*!< Capture channel 1 event.*/ + gptStatusFlagInputCapture2 = 1U << 4, /*!< Capture channel 2 event.*/ + gptStatusFlagRollOver = 1U << 5, /*!< Counter reaches maximum value and rolled over to 0 event.*/ }; -/*! - * @brief Structure to configure the running mode. - */ -typedef struct GptModeConfig +/*! @brief Structure to configure the running mode. */ +typedef struct _gpt_init_config { - bool freeRun; /*!< true: FreeRun mode, false: Restart mode */ - bool waitEnable; /*!< GPT enabled in wait mode */ - bool stopEnable; /*!< GPT enabled in stop mode */ - bool dozeEnable; /*!< GPT enabled in doze mode */ - bool dbgEnable; /*!< GPT enabled in debug mode */ - bool enableMode; /*!< true: counter reset to 0 when enabled, false: counter retain its value when enabled */ -} gpt_mode_config_t; + bool freeRun; /*!< true: FreeRun mode, false: Restart mode. */ + bool waitEnable; /*!< GPT enabled in wait mode. */ + bool stopEnable; /*!< GPT enabled in stop mode. */ + bool dozeEnable; /*!< GPT enabled in doze mode. */ + bool dbgEnable; /*!< GPT enabled in debug mode. */ + bool enableMode; /*!< true: counter reset to 0 when enabled, false: counter retain its value when enabled. */ +} gpt_init_config_t; /******************************************************************************* * API @@ -132,117 +124,119 @@ extern "C" { */ /*! - * @brief Initialize GPT to reset state and initialize running mode + * @brief Initialize GPT to reset state and initialize running mode. * * @param base GPT base pointer. - * @param config GPT mode setting configuration. + * @param initConfig GPT mode setting configuration. */ -void GPT_Init(GPT_Type * base, gpt_mode_config_t *config); +void GPT_Init(GPT_Type* base, const gpt_init_config_t* initConfig); /*! - * @brief Software reset of GPT module + * @brief Software reset of GPT module. * * @param base GPT base pointer. */ -static inline void GPT_SoftReset(GPT_Type * base) +static inline void GPT_SoftReset(GPT_Type* base) { base->CR |= GPT_CR_SWR_MASK; - /* Wait reset finished */ - while (base->CR & GPT_CR_SWR_MASK) { } + /* Wait reset finished. */ + while (base->CR & GPT_CR_SWR_MASK) {}; } /*! - * @brief Set clock source of GPT + * @brief Set clock source of GPT. * * @param base GPT base pointer. - * @param source clock source (see _gpt_clock_source) + * @param source Clock source (see @ref _gpt_clock_source enumeration). */ -void GPT_SetClockSource(GPT_Type * base, uint32_t source); +void GPT_SetClockSource(GPT_Type* base, uint32_t source); /*! - * @brief Get clock source of GPT + * @brief Get clock source of GPT. * * @param base GPT base pointer. - * @return clock source (see _gpt_clock_source) + * @return clock source (see @ref _gpt_clock_source enumeration). */ -static inline uint32_t GPT_GetClockSource(GPT_Type * base) +static inline uint32_t GPT_GetClockSource(GPT_Type* base) { return (base->CR & GPT_CR_CLKSRC_MASK) >> GPT_CR_CLKSRC_SHIFT; } /*! - * @brief Set pre scaler of GPT + * @brief Set pre scaler of GPT. * * @param base GPT base pointer. - * @param prescaler pre scaler of GPT (0-4095, divider=prescaler+1) + * @param prescaler Pre-scaler of GPT (0-4095, divider = prescaler + 1). */ -static inline void GPT_SetPrescaler(GPT_Type * base, uint32_t prescaler) +static inline void GPT_SetPrescaler(GPT_Type* base, uint32_t prescaler) { assert(prescaler <= GPT_PR_PRESCALER_MASK); + base->PR = (base->PR & ~GPT_PR_PRESCALER_MASK) | GPT_PR_PRESCALER(prescaler); } /*! - * @brief Get pre scaler of GPT + * @brief Get pre scaler of GPT. * * @param base GPT base pointer. - * @return pre scaler of GPT (0-4095) + * @return pre scaler of GPT (0-4095). */ -static inline uint32_t GPT_GetPrescaler(GPT_Type * base) +static inline uint32_t GPT_GetPrescaler(GPT_Type* base) { return (base->PR & GPT_PR_PRESCALER_MASK) >> GPT_PR_PRESCALER_SHIFT; } /*! - * @brief OSC 24M pre scaler before selected by clock source + * @brief OSC 24M pre-scaler before selected by clock source. * * @param base GPT base pointer. - * @param prescaler OSC pre scaler(0-15, divider=prescaler+1) + * @param prescaler OSC pre-scaler(0-15, divider = prescaler + 1). */ -static inline void GPT_SetOscPrescaler(GPT_Type * base, uint32_t prescaler) +static inline void GPT_SetOscPrescaler(GPT_Type* base, uint32_t prescaler) { assert(prescaler <= (GPT_PR_PRESCALER24M_MASK >> GPT_PR_PRESCALER24M_SHIFT)); + base->PR = (base->PR & ~GPT_PR_PRESCALER24M_MASK) | GPT_PR_PRESCALER24M(prescaler); } /*! - * @brief Get pre scaler of GPT + * @brief Get pre-scaler of GPT. * * @param base GPT base pointer. - * @return OSC pre scaler of GPT (0-15) + * @return OSC pre scaler of GPT (0-15). */ -static inline uint32_t GPT_GetOscPrescaler(GPT_Type * base) +static inline uint32_t GPT_GetOscPrescaler(GPT_Type* base) { return (base->PR & GPT_PR_PRESCALER24M_MASK) >> GPT_PR_PRESCALER24M_SHIFT; } /*! - * @brief Enable GPT module + * @brief Enable GPT module. * * @param base GPT base pointer. */ -static inline void GPT_Enable(GPT_Type * base) +static inline void GPT_Enable(GPT_Type* base) { base->CR |= GPT_CR_EN_MASK; } /*! - * @brief Disable GPT module + * @brief Disable GPT module. * * @param base GPT base pointer. */ -static inline void GPT_Disable(GPT_Type * base) +static inline void GPT_Disable(GPT_Type* base) { base->CR &= ~GPT_CR_EN_MASK; } /*! - * @brief Get GPT counter value + * @brief Get GPT counter value. * * @param base GPT base pointer. - * @return GPT counter value + * @return GPT counter value. */ -static inline uint32_t GPT_ReadCounter(GPT_Type * base) +static inline uint32_t GPT_ReadCounter(GPT_Type* base) { return base->CNT; } @@ -255,93 +249,100 @@ static inline uint32_t GPT_ReadCounter(GPT_Type * base) */ /*! - * @brief Set GPT operation mode of input capture channel + * @brief Set GPT operation mode of input capture channel. * * @param base GPT base pointer. - * @param channel GPT capture channel (see _gpt_input_capture_channel). - * @param mode GPT input capture operation mode (see _gpt_input_operation_mode). + * @param channel GPT capture channel (see @ref _gpt_input_capture_channel enumeration). + * @param mode GPT input capture operation mode (see @ref _gpt_input_operation_mode enumeration). */ -static inline void GPT_SetInputOperationMode(GPT_Type * base, uint32_t channel, uint32_t mode) +static inline void GPT_SetInputOperationMode(GPT_Type* base, uint32_t channel, uint32_t mode) { assert (channel <= gptInputCaptureChannel2); + base->CR = (base->CR & ~(GPT_CR_IM1_MASK << (channel * 2))) | (GPT_CR_IM1(mode) << (channel * 2)); } /*! - * @brief Get GPT operation mode of input capture channel + * @brief Get GPT operation mode of input capture channel. * * @param base GPT base pointer. - * @param channel GPT capture channel (see _gpt_input_capture_channel). - * @return GPT input capture operation mode (see _gpt_input_operation_mode). + * @param channel GPT capture channel (see @ref _gpt_input_capture_channel enumeration). + * @return GPT input capture operation mode (see @ref _gpt_input_operation_mode enumeration). */ -static inline uint32_t GPT_GetInputOperationMode(GPT_Type * base, uint32_t channel) +static inline uint32_t GPT_GetInputOperationMode(GPT_Type* base, uint32_t channel) { assert (channel <= gptInputCaptureChannel2); + return (base->CR >> (GPT_CR_IM1_SHIFT + channel * 2)) & (GPT_CR_IM1_MASK >> GPT_CR_IM1_SHIFT); } /*! - * @brief Get GPT input capture value of certain channel + * @brief Get GPT input capture value of certain channel. * * @param base GPT base pointer. - * @param channel GPT capture channel (see _gpt_input_capture_channel). - * @return GPT input capture value + * @param channel GPT capture channel (see @ref _gpt_input_capture_channel enumeration). + * @return GPT input capture value. */ -static inline uint32_t GPT_GetInputCaptureValue(GPT_Type * base, uint32_t channel) +static inline uint32_t GPT_GetInputCaptureValue(GPT_Type* base, uint32_t channel) { assert (channel <= gptInputCaptureChannel2); + return *(&base->ICR1 + channel); } /*! - * @brief Set GPT operation mode of output compare channel + * @brief Set GPT operation mode of output compare channel. * * @param base GPT base pointer. - * @param channel GPT output compare channel (see _gpt_output_compare_channel). - * @param mode GPT output operation mode (see _gpt_output_operation_mode). + * @param channel GPT output compare channel (see @ref _gpt_output_compare_channel enumeration). + * @param mode GPT output operation mode (see @ref _gpt_output_operation_mode enumeration). */ -static inline void GPT_SetOutputOperationMode(GPT_Type * base, uint32_t channel, uint32_t mode) +static inline void GPT_SetOutputOperationMode(GPT_Type* base, uint32_t channel, uint32_t mode) { assert (channel <= gptOutputCompareChannel3); + base->CR = (base->CR & ~(GPT_CR_OM1_MASK << (channel * 3))) | (GPT_CR_OM1(mode) << (channel * 3)); } /*! - * @brief Get GPT operation mode of output compare channel + * @brief Get GPT operation mode of output compare channel. * * @param base GPT base pointer. - * @param channel GPT output compare channel (see _gpt_output_compare_channel). - * @return GPT output operation mode (see _gpt_output_operation_mode). + * @param channel GPT output compare channel (see @ref _gpt_output_compare_channel enumeration). + * @return GPT output operation mode (see @ref _gpt_output_operation_mode enumeration). */ -static inline uint32_t GPT_GetOutputOperationMode(GPT_Type * base, uint32_t channel) +static inline uint32_t GPT_GetOutputOperationMode(GPT_Type* base, uint32_t channel) { assert (channel <= gptOutputCompareChannel3); + return (base->CR >> (GPT_CR_OM1_SHIFT + channel * 3)) & (GPT_CR_OM1_MASK >> GPT_CR_OM1_SHIFT); } /*! - * @brief Set GPT output compare value of output compare channel + * @brief Set GPT output compare value of output compare channel. * * @param base GPT base pointer. - * @param channel GPT output compare channel (see _gpt_output_compare_channel). - * @param value GPT output compare value + * @param channel GPT output compare channel (see @ref _gpt_output_compare_channel enumeration). + * @param value GPT output compare value. */ -static inline void GPT_SetOutputCompareValue(GPT_Type * base, uint32_t channel, uint32_t value) +static inline void GPT_SetOutputCompareValue(GPT_Type* base, uint32_t channel, uint32_t value) { assert (channel <= gptOutputCompareChannel3); + *(&base->OCR1 + channel) = value; } /*! - * @brief Get GPT output compare value of output compare channel + * @brief Get GPT output compare value of output compare channel. * * @param base GPT base pointer. - * @param channel GPT output compare channel (see _gpt_output_compare_channel). - * @return GPT output compare value + * @param channel GPT output compare channel (see @ref _gpt_output_compare_channel enumeration). + * @return GPT output compare value. */ -static inline uint32_t GPT_GetOutputCompareValue(GPT_Type * base, uint32_t channel) +static inline uint32_t GPT_GetOutputCompareValue(GPT_Type* base, uint32_t channel) { assert (channel <= gptOutputCompareChannel3); + return *(&base->OCR1 + channel); } @@ -349,18 +350,19 @@ static inline uint32_t GPT_GetOutputCompareValue(GPT_Type * base, uint32_t chann * @brief Force GPT output action on output compare channel, ignoring comparator. * * @param base GPT base pointer. - * @param channel GPT output compare channel (see _gpt_output_compare_channel). + * @param channel GPT output compare channel (see @ref _gpt_output_compare_channel enumeration). */ -static inline void GPT_ForceOutput(GPT_Type * base, uint32_t channel) +static inline void GPT_ForceOutput(GPT_Type* base, uint32_t channel) { assert (channel <= gptOutputCompareChannel3); + base->CR |= (GPT_CR_FO1_MASK << channel); } /*@}*/ /*! - * @name GPT Interupt and Status Control + * @name GPT Interrupt and Status Control * @{ */ @@ -368,10 +370,10 @@ static inline void GPT_ForceOutput(GPT_Type * base, uint32_t channel) * @brief Get GPT status flag. * * @param base GPT base pointer. - * @param flags GPT status flag mask (see _gpt_status_flag for bit definition). - * @return GPT status, each bit represents one status flag + * @param flags GPT status flag mask (see @ref _gpt_status_flag for bit definition). + * @return GPT status, each bit represents one status flag. */ -static inline uint32_t GPT_GetStatusFlag(GPT_Type * base, uint32_t flags) +static inline uint32_t GPT_GetStatusFlag(GPT_Type* base, uint32_t flags) { return base->SR & flags; } @@ -380,21 +382,23 @@ static inline uint32_t GPT_GetStatusFlag(GPT_Type * base, uint32_t flags) * @brief Clear one or more GPT status flag. * * @param base GPT base pointer. - * @param flags GPT status flag mask (see _gpt_status_flag for bit definition). + * @param flags GPT status flag mask (see @ref _gpt_status_flag for bit definition). */ -static inline void GPT_ClearStatusFlag(GPT_Type * base, uint32_t flags) +static inline void GPT_ClearStatusFlag(GPT_Type* base, uint32_t flags) { base->SR = flags; } /*! - * @brief Enable or disable GPT interrupts. + * @brief Enable or Disable GPT interrupts. * * @param base GPT base pointer. - * @param flags GPT status flag mask (see _gpt_status_flag for bit definition). - * @param enable Interrupt enable (true: enable, false: disable). + * @param flags GPT status flag mask (see @ref _gpt_status_flag for bit definition). + * @param enable Enable/Disable GPT interrupts. + * -true: Enable GPT interrupts. + * -false: Disable GPT interrupts. */ -void GPT_SetIntCmd(GPT_Type * base, uint32_t flags, bool enable); +void GPT_SetIntCmd(GPT_Type* base, uint32_t flags, bool enable); /*@}*/ |