bsp_bcc.c

Go to the documentation of this file.
00001 /***************************************************************************/
00035 #include <string.h>
00036 #include "em_device.h"
00037 #include "em_cmu.h"
00038 #include "em_gpio.h"
00039 #include "em_usart.h"
00040 
00041 #include "bsp.h"
00042 
00043 #if defined( BSP_STK )
00044 
00047 /* Module local variables */
00048 static USART_InitAsync_TypeDef usartInit = USART_INITASYNC_DEFAULT;
00049 static uint32_t rxByteCount;
00050 static uint32_t txByteCount;
00051 
00052 /* Module local prototypes */
00053 static void    TxByte( uint8_t data );
00054 static uint8_t RxByte( void );
00055 
00058 /***************************************************************************/
00063 /***************************************************************************/
00068 /**************************************************************************/
00074 int BSP_BccDeInit( void )
00075 {
00076   /* Reset counters */
00077   rxByteCount = 0xFFFFFFFFUL;
00078   txByteCount = 0xFFFFFFFFUL;
00079 
00080   /* Set USART TX GPIO pin mode to input. */
00081   GPIO_PinModeSet( BSP_BCC_USART_TXPORT, BSP_BCC_USART_TXPIN,
00082                    gpioModeInput, 0 );
00083 
00084   /* Set USART RX GPIO pin mode to input. */
00085   GPIO_PinModeSet( BSP_BCC_USART_RXPORT, BSP_BCC_USART_RXPIN,
00086                    gpioModeInput, 0 );
00087 
00088   /* Disable switch U602A "VMCU switch" - to disable USART communication. */
00089   /* See board schematics for details. */
00090   GPIO_PinModeSet( BSP_BCC_U602A_PORT, BSP_BCC_U602A_PIN,
00091                    gpioModeInput, 0 );
00092 
00093   /* Reset USART */
00094   USART_Reset( BSP_BCC_USART );
00095 
00096   /* Disable clock */
00097   CMU_ClockEnable( BSP_BCC_USART_CLK, false );
00098 
00099   return BSP_STATUS_OK;
00100 }
00101 
00102 /**************************************************************************/
00108 int BSP_BccInit( void )
00109 {
00110   rxByteCount = 0;
00111   txByteCount = 0;
00112 
00113   /* Enable High Frequency Peripherals */
00114   CMU_ClockEnable(cmuClock_HFPER, true);
00115 
00116   /* Enable clocks to GPIO */
00117   CMU_ClockEnable(cmuClock_GPIO, true);
00118 
00119   /* Configure GPIO pin for USART TX */
00120   /* To avoid false start, configure output as high. */
00121   GPIO_PinModeSet( BSP_BCC_USART_TXPORT, BSP_BCC_USART_TXPIN,
00122                    gpioModePushPull, 1 );
00123 
00124   /* Configure GPIO pin for USART RX */
00125   GPIO_PinModeSet( BSP_BCC_USART_RXPORT, BSP_BCC_USART_RXPIN,
00126                    gpioModeInput, 1 );
00127 
00128   /* Enable switch U602A "VMCU switch" - to enable USART communication. */
00129   /* See board schematics for details. */
00130   GPIO_PinModeSet( BSP_BCC_U602A_PORT, BSP_BCC_U602A_PIN,
00131                    gpioModePushPull, 1 );
00132 
00133   CMU_ClockEnable( BSP_BCC_USART_CLK, true );
00134 
00135   /* Initialize USART */
00136   USART_InitAsync( BSP_BCC_USART, &usartInit );
00137 
00138   /* Enable correct USART location. */
00139   BSP_BCC_USART->ROUTE = USART_ROUTE_RXPEN | USART_ROUTE_TXPEN |
00140                          BSP_BCC_USART_LOCATION;
00141 
00142   return BSP_STATUS_OK;
00143 }
00144 
00145 /**************************************************************************/
00152 bool BSP_BccPacketReceive( BCP_Packet *pkt )
00153 {
00154   int i;
00155   int length;
00156   uint8_t *bptr;
00157 
00158   /* Setup a byte pointer to start of packet buffer */
00159   bptr   = (uint8_t *) pkt;
00160 
00161   /* Receive packet magic */
00162   *bptr++ = RxByte();
00163   if (pkt->magic != BSP_BCP_MAGIC)
00164   {
00165      return false;
00166   }
00167 
00168   /* Receive packet type */
00169   *bptr++ = RxByte();
00170   if ( (pkt->type < BSP_BCP_FIRST) || (pkt->type > BSP_BCP_LAST) )
00171   {
00172      return false;
00173   }
00174 
00175   /* Receive packet length */
00176   *bptr++ = RxByte();
00177   if (pkt->payloadLength > BSP_BCP_PACKET_SIZE)
00178   {
00179      return false;
00180   }
00181 
00182 #if ( BSP_BCP_VERSION == 2 )
00183    /* Receive reserved byte */
00184    *bptr++ = RxByte();
00185 #endif
00186 
00187   /* Receive packet data length field and sanity check it */
00188   length  = pkt->payloadLength;
00189   if (length > BSP_BCP_PACKET_SIZE)
00190   {
00191      length = BSP_BCP_PACKET_SIZE;
00192   }
00193 
00194   /* Receive packet payload */
00195   for( i=0; i<length; i++ )
00196   {
00197      *bptr++ = RxByte();
00198   }
00199 
00200   return true;
00201 }
00202 
00203 /**************************************************************************/
00210 int BSP_BccPacketSend( BCP_Packet *pkt )
00211 {
00212   int i;
00213 
00214   /* Apply magic */
00215   pkt->magic = BSP_BCP_MAGIC;
00216 
00217   /* Transmit packet magic */
00218   TxByte( pkt->magic );
00219 
00220   /* Transmit packet type */
00221   TxByte( pkt->type );
00222 
00223   /* Transmit packet length */
00224   TxByte( pkt->payloadLength );
00225 
00226 #if ( BSP_BCP_VERSION == 2 )
00227   /* Transmit reserved byte */
00228   TxByte( pkt->reserved );
00229 #endif
00230 
00231   /* Transmit packet payload */
00232   for ( i=0; i<pkt->payloadLength; i++ )
00233   {
00234     TxByte( pkt->data[i] );
00235   }
00236 
00237   return BSP_STATUS_OK;
00238 }
00239 
00240 /**************************************************************************/
00246 USART_TypeDef *BSP_BccUsartGet( void )
00247 {
00248   return BSP_BCC_USART;
00249 }
00250 
00253 static uint8_t RxByte( void )
00254 {
00255   uint8_t byte;
00256 
00257   /* Poll RX data available flag and return a character when one is available */
00258   while (!(BSP_BCC_USART->STATUS & USART_STATUS_RXDATAV)) ;
00259   byte = BSP_BCC_USART->RXDATA;
00260   rxByteCount++;
00261   return byte;
00262 }
00263 
00264 static void TxByte( uint8_t data )
00265 {
00266   /* Check TX buffer and allow for a pending transfer to complete */
00267   while (!(BSP_BCC_USART->STATUS & USART_STATUS_TXBL)) ;
00268   BSP_BCC_USART->TXDATA = (uint32_t) data;
00269   txByteCount++;
00270 }
00271 
00277 #endif /* BSP_STK */