static uint16_t SpiBcAccess(uint8_t addr, uint8_t rw, uint16_t data)
{
  uint16_t tmp;

  /* Enable CS */
  GPIO_PinOutClear(BSP_PORT_SPI_CS, BSP_PIN_SPI_CS);

  /* Write SPI address MSB */
  USART_Tx(BSP_SPI_USART_USED, (addr & 0x3) | rw << 3);
  /* Just ignore data read back */
  USART_Rx(BSP_SPI_USART_USED);

  /* Write SPI address  LSB */
  USART_Tx(BSP_SPI_USART_USED, data & 0xFF);

  tmp = (uint16_t) USART_Rx(BSP_SPI_USART_USED);

  /* SPI data MSB */
  USART_Tx(BSP_SPI_USART_USED, data >> 8);
  tmp |= (uint16_t) USART_Rx(BSP_SPI_USART_USED) << 8;

  /* Disable CS */
  GPIO_PinOutSet(BSP_PORT_SPI_CS, BSP_PIN_SPI_CS);

  return tmp;
}
void DebugInterface::printHexadecimal( uint32_t input, uint8_t size, bool newline )
{
#if SentioEM_Emulator_Interface == OFF && SentioEM_Debug_Interface == ON

	uint8_t temp[8];
	uint8_t i = size;

	do
	{
		temp[i - 1] = ( input &  0x000F );
		input >>= 4;
		--i;
	} while( i );

	for( i = 0; i < size; i++ )
	{
		USART_Tx( DEBUG_USART, ( temp[i] + ( (  temp[i] <= 0x09 ) ? '0' : '7' ) ) );
	}

	if( newline )
	{
		USART_Tx( DEBUG_USART, '\n' );
		USART_Tx( DEBUG_USART, '\r' );
	}
	while( !( DEBUG_USART->STATUS & USART_STATUS_TXC ) );
#endif
}
/*
 *  Access multiple registers
 */
uint8_t CC1101_Radio::multi_register_access( uint8_t address, uint8_t access_mode, uint8_t* data, uint8_t length )
{
	uint8_t reg;
	uint8_t status;
	uint8_t count;

	GPIO_PinOutClear( _CC1101_SPI_CS_PIN );
	USART_Tx( _CC1101_USART, address | access_mode );
	status = USART_Rx( _CC1101_USART );

	for ( count = 0; count < length; count++ )
	{
		if ( ( access_mode == CC1101_READ_SINGLE ) || ( access_mode == CC1101_READ_BURST ) )
		{
			USART_Tx( _CC1101_USART, CC1101_SNOP );
			reg = USART_Rx( _CC1101_USART );
			data[count] = ( uint8_t )reg;
		}
		else
		{
			USART_Tx( _CC1101_USART, data[count] );
			status = USART_Rx( _CC1101_USART );
		}
	}

	GPIO_PinOutSet( _CC1101_SPI_CS_PIN );

	return status;
}
void NRF_ReceivePayload(uint8_t cmd, uint8_t bytes, uint8_t *buf)
{
  NRF_CSN_lo;
  NRF_USART->CMD = USART_CMD_CLEARRX;
  USART_Tx(NRF_USART, cmd);
  USART_Rx(NRF_USART);
  for (int i = 0; i < bytes; i++) {
	  USART_Tx(NRF_USART, 0xFF);
    buf[i] = USART_Rx(NRF_USART);
  }
  //while(!(NRF_USART->STATUS & USART_STATUS_TXC)) ;
  NRF_CSN_hi;
}
/*
 *  SPI register access
 */
uint8_t CC1101_Radio::register_access_SPI( uint8_t address, uint8_t access_mode, uint8_t data )
{
	uint8_t reg = 0;

	GPIO_PinOutClear( _CC1101_SPI_CS_PIN );
	USART_Tx( _CC1101_USART, address | access_mode ); // write the address byte
	USART_Rx( _CC1101_USART );                      // read reply
	USART_Tx( _CC1101_USART, data );                // write the data byte(s)
	reg = USART_Rx( _CC1101_USART );                // read and save the reply
	GPIO_PinOutSet( _CC1101_SPI_CS_PIN );

	return reg;
}
Beispiel #6
0
/**************************************************************************//**
 * @brief      Transmit data on the SPI interface.
 *
 * @param[in]  data    Pointer to the data to be transmitted.
 * @param[in]  len     Length of data to transmit.
 *
 * @return     EMSTATUS code of the operation.
 *****************************************************************************/
EMSTATUS PAL_SpiTransmit (uint8_t* data, unsigned int len)
{
  EMSTATUS status = PAL_EMSTATUS_OK;

  while (len>1)
  {
    /* Send only one byte if len==1 or data pointer is not aligned at a 16 bit
       word location in memory. */
    if ((len == 1) || ((unsigned int)data & 0x1))
    {
      USART_Tx( PAL_SPI_USART_UNIT, *(uint8_t*)data );
      len  --;
      data ++;
    }
    else
    {
      USART_TxDouble( PAL_SPI_USART_UNIT, *(uint16_t*)data );
      len  -= 2;
      data += 2;
    }
  }

  /* Wait for transfer to finish */
  while (!(PAL_SPI_USART_UNIT->STATUS & USART_STATUS_TXC)) ;

  return status;
}
/*********************************************************************
 *
 *       _write()
 *
 * Function description
 *   Low-level write function.
 *   libc subroutines will use this system routine for output to all files,
 *   including stdout.
 *   Write data via USART1.
 */
int _write(int file, char *ptr, int len)
{
    int i;
    (void) file; /* Not used, avoid warning */

    for (i = 0; i < len; i++) {
        if (*ptr == 0)
            break;
        else if (*ptr == '\n')
            USART_Tx(USART1, (uint8_t) '\r');
        USART_Tx(USART1, (uint8_t) *ptr);
        ptr++;
    }

    return len;
}
void spi_write(spi_t *obj, int value)
{
    if (obj->spi.bits <= 8) {
        USART_Tx(obj->spi.spi, (uint8_t) value);
    } else if (obj->spi.bits == 9) {
        USART_TxExt(obj->spi.spi, (uint16_t) value & 0x1FF);
    } else {
        USART_TxDouble(obj->spi.spi, (uint16_t) value);
    }
}
/*********************************************************************
 *
 *       _write_r()
 *
 * Function description
 *   Low-level reentrant write function.
 *   libc subroutines will use this system routine for output to all files,
 *   including stdout.
 *   Write data via USART1.
 */
_ssize_t _write_r(struct _reent *r, int file, const void *ptr, size_t len)
{
    size_t i;
    uint8_t* buffer = (uint8_t*)ptr;

    (void) file; /* Not used, avoid warning */
    (void) r; /* Not used, avoid warning */

    for (i = 0; i < len; i++) {
        if (*buffer == 0)
            break;
        else if (*buffer == '\n')
            USART_Tx(USART1, (uint8_t) '\r');
        USART_Tx(USART1, *buffer);
        buffer++;
    }

    return len;
}
/*
 *  Send a strobe command
 */
uint8_t CC1101_Radio::strobe( uint8_t l_strobe )
{
	uint8_t reg = 0;

	GPIO_PinOutClear( _CC1101_SPI_CS_PIN );
	USART_Tx( _CC1101_USART, l_strobe );    // Write strobe command
	reg = USART_Rx( _CC1101_USART );        // Read reply
	GPIO_PinOutSet( _CC1101_SPI_CS_PIN );

	return reg;
}
Beispiel #11
0
/*---------------------------------------------------------------------------*/
void
usart2_writeb(unsigned char c)
{
  watchdog_periodic();
#ifdef USART2_LF_TO_CRLF
  if(c == '\n')
  {
    USART_Tx(USART2, '\r');
  }
#endif

  USART_Tx(USART2, c);

#ifdef USART2_LF_TO_CRLF
  if(c == '\r')
  {
    USART_Tx(USART2, '\n');
  }
#endif
}
/**************************************************************************//**
 * @brief SPI_TFT_Write Write registers/data to SSD2119 controller
 * @param reg
 *      Register to write to
 * @param data
 *      16-bit data to write into register
 * @note
 *      It's not possible to read back register value through SSD2119 SPI
 *      interface, so no SPI_TFT_ReadRegister function is implemented
 *****************************************************************************/
void SPI_TFT_WriteRegister(uint8_t reg, uint16_t data)
{
  /* Enable chip select */
  GPIO_PinOutClear(gpioPortD, 3);

  /* Select register first */
  USART1->CTRL = USART1->CTRL & ~USART_CTRL_BIT8DV;

  USART_Tx(USART1, reg & 0xFF);
  USART_Rx(USART1);

  /* Write data */
  USART1->CTRL = USART1->CTRL | USART_CTRL_BIT8DV;
  USART_Tx(USART1, (data & 0xff00) >> 8);
  USART_Rx(USART1);
  USART_Tx(USART1, (data & 0x00ff));
  USART_Rx(USART1);

  /* Clear chip select */
  GPIO_PinOutSet(gpioPortD, 3);
}
Beispiel #13
0
/******************************************************************************
 * @brief  usartPutChar function
 *
 * This function will send single char return UART_STATUS based on what happens. 
 *
 * @param[in] ch
 *   unsigned char to transmit. 
 *
 * @return
 *   UART_STATUS status enum.
 *****************************************************************************/  
 uint8_t usartPutChar(uint8_t ch)
{
  int8_t txSuccess = 0;
  
  /* This function should only be called with rx accept = false. */
  if(rxBuf.acceptRXData){
    return UART_STATUS_USAGE_ERROR;
  }
  
  /* Transmit byte and retransmit if error is detected, up to MAX_ERROR_COUNT times. */
  
  while(txSuccess == 0){
    txStatus = 0;
    USART_Tx(usart, ch);
    
    /* Wait for rx interrupt to receive the transmittet byte and indicate so. */
    while (txStatus == 0);
    
    if( txStatus == -1 )
    {
      /* Error, retransmit */
      txErrorCounter++;
      if(txErrorCounter > MAX_ERROR_COUNT)
      {
        txSuccess = -1;
        txErrorCounter = 0;
      }
    }
    else if( txStatus == 1 && ch == rxByteOnTx)
    {
      /* success */
      txSuccess = 1;
    }
    else
    {
      /* unspecified error, success transmitting wrong byte or something... */
      /* Could be caused by calling this putChar while receiving data, this should */
      /* never happen as the ccid should have full a-priori knowledge of comm-direction. */
      return UART_STATUS_USAGE_ERROR;
    }
    
  }
  
  if(txSuccess == 1)
  {
    return UART_STATUS_SUCCESS;
  }
  else
  {
    return UART_STATUS_FRAMING_OR_PARITY_ERROR;
  }
  
}
Beispiel #14
0
/**************************************************************************//**
 * @brief Transmit single byte to USART/LEUART
 * @param data Character to transmit
 *****************************************************************************/
int UART1WriteChar(char c)
{
  if (initialized == false)
  {
    UART1_SerialInit();
  }

  /* Add CR or LF to CRLF if enabled */
  if (LFtoCRLF && (c == '\n'))
  {
    USART_Tx(UART1, '\r');
  }
  USART_Tx(UART1, c);

  if (LFtoCRLF && (c == '\r'))
  {
    USART_Tx(UART1, '\n');
  }

  return c;
}
void DebugInterface::printHex( int32_t input, bool newline )
{
#if SentioEM_Emulator_Interface == OFF && SentioEM_Debug_Interface == ON

	if(input&0x80000000)
	{
		USART_Tx( DEBUG_USART, '-' );
		input = ~input + 1;
	}

	printHexadecimal( (uint32_t) input, 8, newline );
#endif
}
void DebugInterface::printLine( const char *input, bool newline )
{
#if SentioEM_Emulator_Interface == OFF && SentioEM_Debug_Interface == ON

	uint8_t i = 0;

	do
	{
		USART_Tx( DEBUG_USART, input[i] );
		i++;

	} while( ( input[i] != '\0' ) );

	if( newline )
	{
		USART_Tx( DEBUG_USART, '\n' );
		USART_Tx( DEBUG_USART, '\r' );
	}

	while( !( DEBUG_USART->STATUS & USART_STATUS_TXC ) );
#endif
}
time DebugInterface::printTimeDet( time input, bool newline )
{
#if SentioEM_Emulator_Interface == OFF && SentioEM_Debug_Interface == ON

	printLine("\n\rSec:", false );
	printDecimal( input.getSecond(), false );
	printLine(" Min:", false );
	printDecimal( input.getMinute(), false );
	printLine(" Hr:", false );
	printDecimal( input.getHour(),false );

	if( newline )
	{
		USART_Tx( DEBUG_USART, '\n' );
		USART_Tx( DEBUG_USART, '\r' );
	}

	while( !( DEBUG_USART->STATUS & USART_STATUS_TXC ) );
#endif

	return input;
}
void DebugInterface::printFloat( float input, uint8_t displayLength, bool newline )
{
#if SentioEM_Emulator_Interface == OFF && SentioEM_Debug_Interface == ON

	uint8_t length;

	if(input < 0)
	{
		USART_Tx( DEBUG_USART, '-' );
		input *= -1;
	}

	length = printDecimal( (uint32_t) input, false );

	if( length <= displayLength )
		USART_Tx( DEBUG_USART, '.' );

	while( length < displayLength )
	{

		input = input - (uint32_t)input;

		input *= 10;
		USART_Tx( DEBUG_USART, ( (uint8_t) input ) + ( ( (uint8_t) input > 0x09 ) ? '7' : '0' ) );

		length++;
	}

	if( newline )
	{
		USART_Tx( DEBUG_USART, '\n' );
		USART_Tx( DEBUG_USART, '\r' );
	}

	while( !( DEBUG_USART->STATUS & USART_STATUS_TXC ) );
#endif
}
uint8_t DebugInterface::printDecimal( uint32_t input, bool newline )
{
#if SentioEM_Emulator_Interface == OFF && SentioEM_Debug_Interface == ON

	uint8_t temp[11];
	uint8_t length;

	uint8_t  i = 0;

	do
	{
		temp[i] = input%10;
		input /=10;
		i++;
	} while( input );

	length = i;

	do
	{
		USART_Tx( DEBUG_USART, temp[i-1] + '0' );

		i--;
	}while( i );

	if( newline )
	{
		USART_Tx( DEBUG_USART, '\n' );
		USART_Tx( DEBUG_USART, '\r' );
	}

	while( !( DEBUG_USART->STATUS & USART_STATUS_TXC ) );
#endif

	return length;
}
Beispiel #20
0
/**************************************************************************//**
 * @brief USART1 TX IRQ Handler
 *
 * Set up the interrupt prior to use
 *
 *****************************************************************************/
void USART1_TX_IRQHandler(void)
{
  /* Check TX buffer level status */
  if (uart->STATUS & USART_STATUS_TXBL)
  {
    if (txBuf.pendingBytes > 0)
    {
      /* Transmit pending character */
      USART_Tx(uart, txBuf.data[txBuf.rdI]);
      txBuf.rdI = (txBuf.rdI + 1) % BUFFERSIZE;
      txBuf.pendingBytes--;
    }

    /* Disable Tx interrupt if no more bytes in queue */
    if (txBuf.pendingBytes == 0)
    {
      USART_IntDisable(uart, USART_IF_TXBL);
    }
  }
}
Beispiel #21
0
//GPRS发字节
void drv_gprs_send_byte(const INT8U data)
{
	USART_Tx(UART0, data);
}
Beispiel #22
0
/**
 * Writes a byte to the ENC28J60 through SPI.
 * The chip must be selected prior to this write.
 * @param data the 8-bit data byte to write.
 */
void enc28j60_spi_write(uint8_t data)
{
  USART_Tx(USART0, data);
  USART_Rx(USART0);
}
Beispiel #23
0
/**
 * Explicitly read a byte from the ENC28J60 by first sending the dummy byte
 * 0x00.
 * The chip must be selected prior to this write.
 * @return the data read.
 */
uint8_t enc28j60_spi_read(void)
{
  USART_Tx(USART0, 0);
  return USART_Rx(USART0);
}
void usart_send_data(uint8_t data) {
	USART_Tx( USART1, data);
}
Beispiel #25
0
static void uart_gecko_poll_out(struct device *dev, unsigned char c)
{
	const struct uart_gecko_config *config = dev->config->config_info;

	USART_Tx(config->base, c);
}