
#ifdef CONFIG_TOOLCHAIN_IAR
#pragma inline
#endif
static void INLINE_KEYWORD busWriteNegate(void) { IOSET = (unsigned long)0x0200; }

#ifdef CONFIG_TOOLCHAIN_IAR
#pragma inline
#endif
static void INLINE_KEYWORD busWriteAssert(void) { IOCLR = (unsigned long)0x0200; }

#ifdef CONFIG_TOOLCHAIN_IAR
#pragma inline
#endif
static void INLINE_KEYWORD busReadNegate(void) { IOSET = (unsigned long)0x0400; }

#ifdef CONFIG_TOOLCHAIN_IAR
#pragma inline
#endif
static void INLINE_KEYWORD busReadAssert(void) { IOCLR = (unsigned long)0x0400; }

#ifdef CONFIG_TOOLCHAIN_IAR
#pragma inline
#endif
static void INLINE_KEYWORD busCsNegate(void) { IOSET = (unsigned long)0x0800; }

#ifdef CONFIG_TOOLCHAIN_IAR
#pragma inline
#endif
static void INLINE_KEYWORD busCsAssert(void) { IOCLR = (unsigned long)0x0800; }

#ifdef CONFIG_TOOLCHAIN_IAR
#pragma inline
#endif
static void INLINE_KEYWORD busNegateAll(void) { IOSET = (unsigned long)0x0e00; }


// only drive the bus when writing to save power
static void busWrite(unsigned char writeAddr, unsigned char writeData)
{
  unsigned long iopinMask;
  unsigned long iodirSave;

  iopinMask  = (unsigned long)((writeData << 16) & 0x00ff0000);
  iopinMask |= (unsigned long)((writeAddr << 24) & 0x07000000);

  // drive the data bus
  iodirSave = IODIR;
  IODIR = (iodirSave | 0x07ff0000);

  IOSET = iopinMask;
  IOCLR = ((~iopinMask) & 0x07ff0000);

  ssiDelay(); // let the data settle
  ssiDelay(); // let the data settle


  // assert CS_N, then WE_N
  busCsAssert();
  ssiDelay();

  busWriteAssert();
  ssiDelay();
  ssiDelay();


  busWriteNegate();
  ssiDelay(); // let the data settle 
  
  busCsNegate();
  ssiDelay(); // let the data settle 


  // stop driving the data bus
  IODIR = iodirSave;
}

static unsigned char busRead(unsigned char readAddr)
{
  unsigned long iopinMask;
  unsigned long iopinState;
  unsigned long iodirSave = IODIR;

  // drive address, not data
  IODIR = ((iodirSave | 0x07000000) & 0xff00ffff);

  iopinMask = (unsigned long)((readAddr << 24) & 0x07000000);

  // drive the address bits
  IOSET = iopinMask;
  IOCLR = ((~iopinMask) & 0x07000000);

  ssiDelay();
  ssiDelay();

  // assert CS_N, then RD_N
  busCsAssert();
  ssiDelay();

  busReadAssert();
  ssiDelay();
  ssiDelay();

  // grab the data
  iopinState = IOPIN;

  // negate read
  busReadNegate();
  ssiDelay(); // let the data settle 
  busCsNegate();
  ssiDelay(); // let the data settle 

  // restore GPIO direction
  IODIR = iodirSave;

  return ((unsigned char)((iopinState >> 16) & 0xff));
}
