#include "SFBHWMisc.h"
#include "SFBWiring.h"
#include "SFBPrintf.h"

void facePrintTimestamp(u8 face) {
  u64 now = microseconds();
  u32 secs = (u32) (now/1000000);
  u32 ms = (u32) ((now%1000000)/1000);
  facePrintf(face,"%d.",secs);
  if (ms < 10)
    facePrintf(face,"00");
  else if (ms < 100) 
    facePrintf(face,"0");
  facePrintf(face,"%d",ms);
}

volatile u32 stackCanary __attribute__((section (".bssstackcanary")));

u32 getCurrentStackSpace() {
  u32 stackItem;
  u32 words = (&stackItem-&stackCanary);
  return (words-1)<<2;
}

u32 getMinimumStackSpace() {
  volatile const u32 * bottom = &stackCanary;
  volatile const u32 * lastUsed;
  for (lastUsed = bottom+1; *lastUsed == -((u32) lastUsed); ++lastUsed) ;
  return (lastUsed-bottom)<<2;
}
