Remove unneeded comments.
This commit is contained in:
@@ -1,43 +1,15 @@
|
|||||||
/*
|
|
||||||
* @file microseconds.c
|
|
||||||
* @brief Microseconds timer driver source file
|
|
||||||
*
|
|
||||||
* Notes: The driver configure PIT as lifetime timer
|
|
||||||
*/
|
|
||||||
#include "microseconds/microseconds.h"
|
#include "microseconds/microseconds.h"
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include "bus_pal_hardware.h"
|
#include "bus_pal_hardware.h"
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
enum {
|
||||||
// Definitions
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
// Below MACROs are defined in order to keep this driver compabtile among all targets.
|
|
||||||
#if defined(PIT0)
|
|
||||||
#define PIT PIT0
|
|
||||||
#endif
|
|
||||||
#if defined(SIM_SCGC6_PIT0_MASK)
|
|
||||||
#define SIM_SCGC6_PIT_MASK SIM_SCGC6_PIT0_MASK
|
|
||||||
#endif
|
|
||||||
|
|
||||||
enum
|
|
||||||
{
|
|
||||||
kFrequency_1MHz = 1000000UL
|
kFrequency_1MHz = 1000000UL
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
uint32_t s_tickPerMicrosecondMul8; // This value equals to 8 times ticks per microseconds
|
||||||
// Variables
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
uint32_t s_tickPerMicrosecondMul8; //!< This value equal to 8 times ticks per microseconds
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
// Initialize the timer to lifetime timer by chained channel 0 and
|
||||||
// Code
|
// channel 1 together, and set both channels to maximum counting period.
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
//! @brief Initialize timer facilities.
|
|
||||||
//!
|
|
||||||
//! It is initialize the timer to lifetime timer by chained channel 0
|
|
||||||
//! and channel 1 together, and set b0th channels to maximum counting period
|
|
||||||
void microseconds_init(void)
|
void microseconds_init(void)
|
||||||
{
|
{
|
||||||
uint32_t busClock;
|
uint32_t busClock;
|
||||||
@@ -60,84 +32,65 @@ void microseconds_init(void)
|
|||||||
PIT->CHANNEL[0].TFLG = 1; // clear the timer 0 flag
|
PIT->CHANNEL[0].TFLG = 1; // clear the timer 0 flag
|
||||||
PIT->CHANNEL[0].TCTRL = PIT_TCTRL_TEN_MASK; // start timer 0
|
PIT->CHANNEL[0].TCTRL = PIT_TCTRL_TEN_MASK; // start timer 0
|
||||||
|
|
||||||
/* Calculate this value early
|
// Calculate this value early
|
||||||
* The reason why use this solution is that lowest clock frequency supported by L0PB and L4KS
|
// The reason why use this solution is that lowest clock frequency supported by L0PB and L4KS
|
||||||
* is 0.25MHz, this solution will make sure ticks per microscond is greater than 0.
|
// is 0.25MHz, this solution will make sure ticks per microscond is greater than 0.
|
||||||
*/
|
|
||||||
|
|
||||||
busClock = get_bus_clock();
|
busClock = get_bus_clock();
|
||||||
s_tickPerMicrosecondMul8 = (busClock * 8) / kFrequency_1MHz;
|
s_tickPerMicrosecondMul8 = (busClock * 8) / kFrequency_1MHz;
|
||||||
|
|
||||||
// Make sure this value is greater than 0
|
// Make sure this value is greater than 0
|
||||||
if (!s_tickPerMicrosecondMul8)
|
if (!s_tickPerMicrosecondMul8) {
|
||||||
{
|
|
||||||
s_tickPerMicrosecondMul8 = 1;
|
s_tickPerMicrosecondMul8 = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//! @brief Shutdown the microsecond timer
|
|
||||||
void microseconds_shutdown(void)
|
void microseconds_shutdown(void)
|
||||||
{
|
{
|
||||||
// Turn off PIT: MDIS = 1, FRZ = 0
|
PIT->MCR |= PIT_MCR_MDIS_MASK; // Turn off PIT: MDIS = 1, FRZ = 0
|
||||||
PIT->MCR |= PIT_MCR_MDIS_MASK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//! @brief Read back running tick count
|
|
||||||
uint64_t microseconds_get_ticks(void)
|
uint64_t microseconds_get_ticks(void)
|
||||||
{
|
{
|
||||||
uint64_t valueH;
|
uint64_t valueH;
|
||||||
uint32_t valueL;
|
uint32_t valueL;
|
||||||
|
|
||||||
#if defined(FSL_FEATURE_PIT_HAS_LIFETIME_TIMER) && (FSL_FEATURE_PIT_HAS_LIFETIME_TIMER == 1)
|
|
||||||
valueH = PIT->LTMR64H;
|
|
||||||
valueL = PIT->LTMR64L;
|
|
||||||
#else
|
|
||||||
// Make sure that there are no rollover of valueL.
|
// Make sure that there are no rollover of valueL.
|
||||||
// Because the valueL always decreases, so, if the formal valueL is greater than
|
// Because the valueL always decreases, so, if the formal valueL is greater than
|
||||||
// current value, that means the valueH is updated during read valueL.
|
// current value, that means the valueH is updated during read valueL.
|
||||||
// In this case, we need to re-update valueH and valueL.
|
// In this case, we need to re-update valueH and valueL.
|
||||||
do
|
do {
|
||||||
{
|
|
||||||
valueH = PIT->CHANNEL[1].CVAL;
|
valueH = PIT->CHANNEL[1].CVAL;
|
||||||
valueL = PIT->CHANNEL[0].CVAL;
|
valueL = PIT->CHANNEL[0].CVAL;
|
||||||
} while (valueL < PIT->CHANNEL[0].CVAL);
|
} while (valueL < PIT->CHANNEL[0].CVAL);
|
||||||
#endif // FSL_FEATURE_PIT_HAS_LIFETIME_TIMER
|
|
||||||
|
|
||||||
// Invert to turn into an up counter
|
// Invert to turn into an up counter
|
||||||
return ~((valueH << 32) | valueL);
|
return ~((valueH << 32) | valueL);
|
||||||
}
|
}
|
||||||
|
|
||||||
//! @brief Returns the conversion of ticks to actual microseconds
|
// This is used to seperate any calculations from getting a timer
|
||||||
//! This is used to seperate any calculations from getting a timer
|
// value for speed critical scenarios
|
||||||
// value for speed critical scenarios
|
|
||||||
uint32_t microseconds_convert_to_microseconds(uint32_t ticks)
|
uint32_t microseconds_convert_to_microseconds(uint32_t ticks)
|
||||||
{
|
{
|
||||||
// return the total ticks divided by the number of Mhz the system clock is at to give microseconds
|
// return the total ticks divided by the number of Mhz the system clock is at to give microseconds
|
||||||
return (8 * ticks / s_tickPerMicrosecondMul8); //!< Assumes system clock will never be < 0.125 Mhz
|
return (8 * ticks / s_tickPerMicrosecondMul8); // Assumes system clock will never be < 0.125 Mhz
|
||||||
}
|
}
|
||||||
|
|
||||||
//! @brief Returns the conversion of microseconds to ticks
|
|
||||||
uint64_t microseconds_convert_to_ticks(uint32_t microseconds)
|
uint64_t microseconds_convert_to_ticks(uint32_t microseconds)
|
||||||
{
|
{
|
||||||
return ((uint64_t)microseconds * s_tickPerMicrosecondMul8 / 8);
|
return ((uint64_t)microseconds * s_tickPerMicrosecondMul8 / 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
//! @brief Delay specified time
|
|
||||||
//!
|
|
||||||
//! @param us Delay time in microseconds unit
|
|
||||||
void microseconds_delay(uint32_t us)
|
void microseconds_delay(uint32_t us)
|
||||||
{
|
{
|
||||||
uint64_t currentTicks = microseconds_get_ticks();
|
uint64_t currentTicks = microseconds_get_ticks();
|
||||||
|
// The clock value in Mhz = ticks/microsecond
|
||||||
//! The clock value in Mhz = ticks/microsecond
|
|
||||||
uint64_t ticksNeeded = ((uint64_t)us * s_tickPerMicrosecondMul8 / 8) + currentTicks;
|
uint64_t ticksNeeded = ((uint64_t)us * s_tickPerMicrosecondMul8 / 8) + currentTicks;
|
||||||
while (microseconds_get_ticks() < ticksNeeded)
|
while (microseconds_get_ticks() < ticksNeeded) {
|
||||||
{
|
|
||||||
;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//! @brief Gets the clock value used for microseconds driver
|
// Get the clock value used for microseconds driver
|
||||||
uint32_t microseconds_get_clock(void)
|
uint32_t microseconds_get_clock(void)
|
||||||
{
|
{
|
||||||
return get_bus_clock();
|
return get_bus_clock();
|
||||||
|
|||||||
Reference in New Issue
Block a user