Example #1
0
//--------------------------------------------------------------------------
// Reset all of the devices on the 1-Wire Net and return the result.
//
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
//                indicate the symbolic port number.
//
// Returns: TRUE(1):  presence pulse(s) detected, device(s) reset
//          FALSE(0): no presence pulses detected
//
SMALLINT owTouchReset(int portnum)
{
   int reg;
   int ovd = (sockit_owm.ovd >> portnum) & 0x1;

   // lock transfer
   ALT_SEM_PEND (sockit_owm.trn, 0);

   // write RST
   IOWR_SOCKIT_OWM (sockit_owm.base, (sockit_owm.pwr << SOCKIT_OWM_POWER_OFST)
                                   | (portnum        << SOCKIT_OWM_SEL_OFST)
                                   | (sockit_owm.ena << SOCKIT_OWM_ETX_OFST)
                                   | (ovd            << SOCKIT_OWM_OVD_OFST)
                                   |                    SOCKIT_OWM_RST_MSK);

   // wait for irq to set the transfer end flag
   ALT_FLAG_PEND (sockit_owm.irq, 0x1, OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME, 0);
   // wait for STX (end of transfer cycle) and read the presence status
   while ((reg = IORD_SOCKIT_OWM (sockit_owm.base)) & SOCKIT_OWM_TRN_MSK);

   // release transfer lock
   ALT_SEM_POST (sockit_owm.trn);

   // return DRX (presence detect)
   return (~reg >> SOCKIT_OWM_DAT_OFST) & 0x1;
}
Example #2
0
//--------------------------------------------------------------------------
//  Description:
//     Delay for at least 'len' ms
//
void msDelay(int len)
{
#if SOCKIT_OWM_HW_DLY
   int i;

   // lock transfer
   ALT_SEM_PEND (sockit_owm.trn, 0);

   for (i=0; i<len; i++) {
      // create a 960us pause
      IOWR_SOCKIT_OWM (sockit_owm.base, ( sockit_owm.pwr        << SOCKIT_OWM_POWER_OFST)
                                      | ( sockit_owm.ena        << SOCKIT_OWM_ETX_OFST)
                                      | ((sockit_owm.pwr & 0x1) << SOCKIT_OWM_PWR_OFST)
                                      |                            SOCKIT_OWM_DLY_MSK);

     // wait for irq to set the transfer end flag
     ALT_FLAG_PEND (sockit_owm.irq, 0x1, OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME, 0);
     // wait for STX (end of transfer cycle)
     while (IORD_SOCKIT_OWM (sockit_owm.base) & SOCKIT_OWM_TRN_MSK);

     // release transfer lock
     ALT_SEM_POST (sockit_owm.trn);
   }
#else
#ifdef UCOS_II
   // uCOS-II timed delay
   OSTimeDlyHMSM(0,0,0,len);
#else
   // Altera HAL us delay
   usleep (1000*len);
#endif
#endif
}
Example #3
0
//-------------------------------------------------------------------------------
int alt_avn_jtag_uart_read(alt_avn_jtag_uart_state* sp, 
		char * buffer, int space, int flags)
{
	char * ptr = buffer;

	alt_irq_context context;
	unsigned int n;

	/*
	 * When running in a multi threaded environment, obtain the "read_lock"
	 * semaphore. This ensures that reading from the device is thread-safe.
	 */
	ALT_SEM_PEND (sp->read_lock, 0);

	while (space > 0)
	{
		unsigned int in, out;

		/* Read as much data as possible */
		do
		{
			in  = sp->rx_in;
			out = sp->rx_out;

			if (in >= out)
				n = in - out;
			else
				n = ALTERA_AVALON_JTAG_UART_BUF_LEN - out;

			if (n == 0)
				break; /* No more data available */

			if (n > space)
				n = space;

			memcpy(ptr, sp->rx_buf + out, n);
			ptr   += n;
			space -= n;

			sp->rx_out = (out + n) % ALTERA_AVALON_JTAG_UART_BUF_LEN;
		}
		while (space > 0);

		/* If we read any data then return it */
		if (ptr != buffer)
			break;

		/* If in non-blocking mode then return error */
		if (flags & O_NONBLOCK)
			break;

		/* OS Present: Pend on a flag if the OS is running, otherwise spin */
		if(OSRunning == OS_TRUE) {
			/*
			 * When running in a multi-threaded mode, we pend on the read event
			 * flag set and timeout event flag set in the isr. This avoids wasting CPU
			 * cycles waiting in this thread, when we could be doing something more
			 * profitable elsewhere.
			 */
			ALT_FLAG_PEND (sp->events,
					ALT_JTAG_UART_READ_RDY | ALT_JTAG_UART_TIMEOUT,
					OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME,
					0);
		}
		else {
			/* Spin until more data arrives or until host disconnects */
			while (in == sp->rx_in && sp->host_inactive < sp->timeout)
				;
		}


		if (in == sp->rx_in)
			break;
	}

	/*
	 * Now that access to the circular buffer is complete, release the read
	 * semaphore so that other threads can access the buffer.
	 */

	ALT_SEM_POST (sp->read_lock);

	if (ptr != buffer)
	{
		/* If we read any data then there is space in the buffer so enable interrupts */
		context = alt_irq_disable_all();
		sp->irq_enable |= ALTERA_AVALON_JTAG_UART_CONTROL_RE_MSK;
		IOWR_ALTERA_AVALON_JTAG_UART_CONTROL(sp->base, sp->irq_enable);
		alt_irq_enable_all(context);
	}

	if (ptr != buffer)
		return ptr - buffer;
	else if (flags & O_NONBLOCK)
		return -EWOULDBLOCK;
	else
		return -EIO;
}
Example #4
0
int alt_avn_jtag_uart_write(alt_avn_jtag_uart_state* sp, 
		const char * ptr, int count, int flags)
{
	/* Remove warning at optimisation level 03 by seting out to 0 */
	unsigned int in, out=0;
	unsigned int n;
	alt_irq_context context;

	const char * start = ptr;

	/*
	 * When running in a multi threaded environment, obtain the "write_lock"
	 * semaphore. This ensures that writing to the device is thread-safe.
	 */
	ALT_SEM_PEND (sp->write_lock, 0);

	do
	{
		/* Copy as much as we can into the transmit buffer */
		while (count > 0)
		{
			/* We need a stable value of the out pointer to calculate the space available */
			in  = sp->tx_in;
			out = sp->tx_out;

			if (in < out)
				n = out - 1 - in;
			else if (out > 0)
				n = ALTERA_AVALON_JTAG_UART_BUF_LEN - in;
			else
				n = ALTERA_AVALON_JTAG_UART_BUF_LEN - 1 - in;

			if (n == 0)
				break;

			if (n > count)
				n = count;

			memcpy(sp->tx_buf + in, ptr, n);
			ptr   += n;
			count -= n;

			sp->tx_in = (in + n) % ALTERA_AVALON_JTAG_UART_BUF_LEN;
		}

		/*
		 * If interrupts are disabled then we could transmit here, we only need 
		 * to enable interrupts if there is no space left in the FIFO
		 *
		 * For now kick the interrupt routine every time to make it transmit 
		 * the data 
		 */
		context = alt_irq_disable_all();
		sp->irq_enable |= ALTERA_AVALON_JTAG_UART_CONTROL_WE_MSK;
		IOWR_ALTERA_AVALON_JTAG_UART_CONTROL(sp->base, sp->irq_enable);
		alt_irq_enable_all(context);

		/* 
		 * If there is any data left then either return now or block until 
		 * some has been sent 
		 */
		/* consider: test whether there is anything there while doing this and delay for at most 2s. */
		if (count > 0)
		{
			if (flags & O_NONBLOCK)
				break;

			/* OS Present: Pend on a flag if the OS is running, otherwise spin */
			if(OSRunning == OS_TRUE) {
				/*
				 * When running in a multi-threaded mode, we pend on the write event
				 * flag set or the timeout flag in the isr. This avoids wasting CPU
				 * cycles waiting in this thread, when we could be doing something
				 * more profitable elsewhere.
				 */
				ALT_FLAG_PEND (sp->events,
						ALT_JTAG_UART_WRITE_RDY | ALT_JTAG_UART_TIMEOUT,
						OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME,
						0);
			}
			else {
				/*
				 * OS not running: Wait for data to be removed from buffer.
				 * Once the interrupt routine has removed some data then we
				 * will be able to insert some more.
				 */
				while (out == sp->tx_out && sp->host_inactive < sp->timeout)
					;
			}

			if (out == sp->tx_out)
				break;
		}
	}
	while (count > 0);

	/*
	 * Now that access to the circular buffer is complete, release the write
	 * semaphore so that other threads can access the buffer.
	 */
	ALT_SEM_POST (sp->write_lock);

	if (ptr != start)
		return ptr - start;
	else if (flags & O_NONBLOCK)
		return -EWOULDBLOCK;
	else
		return -EIO; /* Host not connected */
}
int fifoed_avalon_uart_write (fifoed_avalon_uart_state* sp, const char* ptr, int len, int flags)
{
  alt_irq_context context;
  int             no_block;
  alt_u32         next;
  int count                = len;

  /* 
   * Construct a flag to indicate whether the device is being accessed in
   * blocking or non-blocking mode.
   */

  no_block = (flags & O_NONBLOCK);

  /*
   * When running in a multi threaded environment, obtain the "write_lock"
   * semaphore. This ensures that writing to the device is thread-safe.
   */

  ALT_SEM_PEND (sp->write_lock, 0);

  /*
   * Loop transferring data from the input buffer to the transmit circular
   * buffer. The loop is terminated once all the data has been transferred,
   * or, (if in non-blocking mode) the buffer becomes full.
   */

  while (count)
  {
    /* Determine the next slot in the buffer to access */

    next = (sp->tx_end + 1) & FIFOED_AVALON_UART_BUF_MSK;

    /* block waiting for space if necessary */

    if (next == sp->tx_start)
    {
      if (no_block)
      {
        /* Set errno to indicate why this function returned early */
 
        ALT_ERRNO = EWOULDBLOCK;
        break;
      }
      else
      {
        /* Block waiting for space in the circular buffer */

        /* First, ensure transmit interrupts are enabled to avoid deadlock */

        context = alt_irq_disable_all ();
        sp->ctrl |= (FIFOED_AVALON_UART_CONTROL_TRDY_MSK |
                        FIFOED_AVALON_UART_CONTROL_DCTS_MSK);
        IOWR_FIFOED_AVALON_UART_CONTROL(sp->base, sp->ctrl);
        alt_irq_enable_all (context);

        /* wait for space to come free */

        do
        {
          /*
           * When running in a multi-threaded mode, we pend on the write event 
           * flag set in the interrupt service routine. This avoids wasting CPU
           * cycles waiting in this thread, when we could be doing something
           * more profitable elsewhere.
           */

          ALT_FLAG_PEND (sp->events,
                         ALT_UART_WRITE_RDY,
                         OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME,
                         0);
        }
        while ((next == sp->tx_start));
      }
    }

    count--;

    /* Add the next character to the transmit buffer */

    sp->tx_buf[sp->tx_end] = *ptr++;
    sp->tx_end = next;
  }

  /*
   * Now that access to the circular buffer is complete, release the write
   * semaphore so that other threads can access the buffer.
   */

  ALT_SEM_POST (sp->write_lock);

  /* 
   * Ensure that interrupts are enabled, so that the circular buffer can 
   * drain.
   */

  context = alt_irq_disable_all ();
  sp->ctrl |= FIFOED_AVALON_UART_CONTROL_TRDY_MSK |
                 FIFOED_AVALON_UART_CONTROL_DCTS_MSK;
  IOWR_FIFOED_AVALON_UART_CONTROL(sp->base, sp->ctrl);
  alt_irq_enable_all (context);

  /* return the number of bytes written */

  return (len - count);
}
int fifoed_avalon_uart_read (fifoed_avalon_uart_state* sp, char* ptr, int len, int flags)
{
  alt_irq_context context;
  int             block;
  alt_u32         next;

  int count                = 0;

  /* 
   * Construct a flag to indicate whether the device is being accessed in
   * blocking or non-blocking mode.
   */

  block = !(flags & O_NONBLOCK);

  /*
   * When running in a multi threaded environment, obtain the "read_lock"
   * semaphore. This ensures that reading from the device is thread-safe.
   */

  ALT_SEM_PEND (sp->read_lock, 0);

  /*
   * Calculate which slot in the circular buffer is the next one to read
   * data from.
   */

  next = (sp->rx_start + 1) & FIFOED_AVALON_UART_BUF_MSK;

  /*
   * Loop, copying data from the circular buffer to the destination address
   * supplied in "ptr". This loop is terminated when the required number of
   * bytes have been read. If the circular buffer is empty, and no data has
   * been read, then the loop will block (when in blocking mode).
   *
   * If the circular buffer is empty, and some data has already been 
   * transferred, or the device is being accessed in non-blocking mode, then
   * the loop terminates without necessarily reading all the requested data.
   */

  do
  {
    /*
     * Read the required amount of data, until the circular buffer runs
     * empty
     */

    while ((count < len) && (sp->rx_start != sp->rx_end))
    {
      count++;
      *ptr++ = sp->rx_buf[sp->rx_start];
      
      sp->rx_start = (++sp->rx_start) & FIFOED_AVALON_UART_BUF_MSK;
    }

    /*
     * If no data has been transferred, the circular buffer is empty, and
     * this is not a non-blocking access, block waiting for data to arrive.
     */

    if (!count && (sp->rx_start == sp->rx_end))
    {
      if (!block)
      {
        /* Set errno to indicate the reason we're not returning any data */

        ALT_ERRNO = EWOULDBLOCK;
        break;
      }
      else
      {
       /* Block waiting for some data to arrive */

       /* First, ensure read interrupts are enabled to avoid deadlock */

       context = alt_irq_disable_all ();
       sp->ctrl |= FIFOED_AVALON_UART_CONTROL_RRDY_MSK;
       IOWR_FIFOED_AVALON_UART_CONTROL(sp->base, sp->ctrl);
       alt_irq_enable_all (context);

       /*
        * When running in a multi-threaded mode, we pend on the read event 
        * flag set in the interrupt service routine. This avoids wasting CPU
        * cycles waiting in this thread, when we could be doing something more 
        * profitable elsewhere.
        */

       ALT_FLAG_PEND (sp->events,
                      ALT_UART_READ_RDY,
                      OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME,
                      0);
      }
    }
  }
  while (!count && len);

  /*
   * Now that access to the circular buffer is complete, release the read
   * semaphore so that other threads can access the buffer.
   */

  ALT_SEM_POST (sp->read_lock);

  /*
   * Ensure that interrupts are enabled, so that the circular buffer can
   * re-fill.
   */

  context = alt_irq_disable_all ();
  sp->ctrl |= FIFOED_AVALON_UART_CONTROL_RRDY_MSK;
  IOWR_FIFOED_AVALON_UART_CONTROL(sp->base, sp->ctrl);
  alt_irq_enable_all (context);

  /* Return the number of bytes read */

  return count;
}