Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
Implemented cpu clocking, system tick and sleep. Tested, confirmed wo…
Browse files Browse the repository at this point in the history
…rking
  • Loading branch information
OscarKro committed Oct 30, 2020
1 parent 5c43c3d commit 61d1d53
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 1 deletion.
49 changes: 48 additions & 1 deletion library/targets/hwlib-mimxrt1062.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,53 @@ namespace mimxrt1062
IOMUXC->SW_PAD_CTL_PAD[n] |= mask;
}

/// the number of ticks per us
int_fast64_t HWLIB_WEAK ticks_per_us()
{
return 600; // this number should be the same as the cpu freq in Mhz
}

uint_fast64_t HWLIB_WEAK now_ticks()
{
static bool init_done = false;
if (!init_done)
{

// kill the watchdog
//WDT->WDT_MR = WDT_MR_WDDIS;

// switch to the 84 MHz crystal/PLL clock
// sam3xa::SystemInit();

// EFC0->EEFC_FMR = EEFC_FMR_FWS(4);
// EFC1->EEFC_FMR = EEFC_FMR_FWS(4);

SysTick->CTRL = 0; // stop the timer
SysTick->LOAD = 0xFFFFFF; // use its as a 24-bit timer
SysTick->VAL = 0; // clear the timer
SysTick->CTRL = 5; // start the timer, 1:1

init_done = true;
}

static unsigned int last_low = 0;
static unsigned long long int high = 0;

// the timer ticks down, but we want an up counter
unsigned int low = 0xFFFFFF - (SysTick->VAL & 0xFFFFFF);
if (low < last_low)
{

// the timer rolled over, so increment the high part
high += 0x1ULL << 24;
}
last_low = low;

// return the aggregated ticks value
// the counter runs at 84 MHz
return (low | high);
}

} // namespace mimxrt1062

#endif //HWLIB_MIMXRT1062

68 changes: 68 additions & 0 deletions library/targets/hwlib-teensy_40.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#define TEENSY_40
namespace teensy_40
{
uint64_t ticks_per_us();
uint64_t now_us();
/**
* @brief This Struct contains information about the relation of the Teensy board pin with the chip Core.
*
Expand Down Expand Up @@ -184,5 +186,71 @@ namespace teensy_40
namespace hwlib
{
namespace target = ::teensy_40;

#ifdef _HWLIB_ONCE
uint64_t now_ticks()
{
return mimxrt1062::now_ticks();
}

uint64_t ticks_per_us()
{
return mimxrt1062::ticks_per_us();
}

uint64_t now_us()
{
return now_ticks() / ticks_per_us();
}

// busy waits

void wait_ns_busy(int_fast32_t n)
{
wait_us_busy((n + 999) / 1000);
}

void wait_us_busy(int_fast32_t n)
{
auto end = now_us() + n;
while (now_us() < end)
{
}
}

void wait_ms_busy(int_fast32_t n)
{
while (n > 0)
{
wait_us_busy(1000);
--n;
}
}

// non-busy waits

void HWLIB_WEAK wait_ns(int_fast32_t n)
{
wait_us((n + 999) / 1000);
}

void HWLIB_WEAK wait_us(int_fast32_t n)
{
auto end = now_us() + n;
while (now_us() < end)
{
// background::do_background_work();
}
}

void HWLIB_WEAK wait_ms(int_fast32_t n)
{
while (n > 0)
{
wait_us(1000);
--n;
}
}
#endif // _HWLIB_ONCE
};
#endif // TEENSY_40

0 comments on commit 61d1d53

Please sign in to comment.