#include <ios.h>
#include <hw.h>
#include <iostypes.h>
#include <ioslibc.h>

#define STACK_SIZE 1024
const u8 _initStack[STACK_SIZE];
const u32 _initStackSize = sizeof(_initStack);
const u32 _initPriority = 10;

void 
fl_intr_test(void)
{
  IOSMessageQueueId mqid;
  IOSMessage array;
  u32 reg;


  /* Register handler for FLA event */
  /* Create a message queue and register for flash events */
  if ((mqid = IOS_CreateMessageQueue(&array, 1)) < 0) {
	 printf("Unable to create FLA message queue: %d\n", mqid);
         goto done;
  }
  if (IOS_HandleEvent(IOS_EVENT_FLA, mqid, 0) != IOS_ERROR_OK) {
	 printf("Unable to register FLA event handler: %d\n");
         goto done;
  }

  /* Enable FLA access to SRAM */
  reg = busRd32(ACRSRNPROT_REG) | ACRSRNPROT_FLAEN_MASK;
  busWrt32(ACRSRNPROT_REG, reg);

  /* IOS has enabled top-level FLA interrupt. Only enable interrupt
   * in device.
   */

  while(1) {
      printf("Resetting flash with interrupts enabled\n");
      IO_WRITE(FL_CTRL_REG, FL_CTRL_EXEC |
                          FL_CTRL_RDY | FL_CTRL_MASK |
                          (0xff << FL_CTRL_CMD_SHIFT));
	  if (IOS_ReceiveMessage(mqid, NULL, IOS_MESSAGE_BLOCK) == IOS_ERROR_OK) {
		  printf("Received flash completion event\n");
	  }
  }
done:
  while(1); 
}

void
main(u32 arg)
{ 
	fl_intr_test(); 
}
