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
00048 static USART_InitAsync_TypeDef usartInit = USART_INITASYNC_DEFAULT;
00049 static uint32_t rxByteCount;
00050 static uint32_t txByteCount;
00051
00052
00053 static void TxByte( uint8_t data );
00054 static uint8_t RxByte( void );
00055
00058
00063
00068
00074 int BSP_BccDeInit( void )
00075 {
00076
00077 rxByteCount = 0xFFFFFFFFUL;
00078 txByteCount = 0xFFFFFFFFUL;
00079
00080
00081 GPIO_PinModeSet( BSP_BCC_USART_TXPORT, BSP_BCC_USART_TXPIN,
00082 gpioModeInput, 0 );
00083
00084
00085 GPIO_PinModeSet( BSP_BCC_USART_RXPORT, BSP_BCC_USART_RXPIN,
00086 gpioModeInput, 0 );
00087
00088
00089
00090 GPIO_PinModeSet( BSP_BCC_U602A_PORT, BSP_BCC_U602A_PIN,
00091 gpioModeInput, 0 );
00092
00093
00094 USART_Reset( BSP_BCC_USART );
00095
00096
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
00114 CMU_ClockEnable(cmuClock_HFPER, true);
00115
00116
00117 CMU_ClockEnable(cmuClock_GPIO, true);
00118
00119
00120
00121 GPIO_PinModeSet( BSP_BCC_USART_TXPORT, BSP_BCC_USART_TXPIN,
00122 gpioModePushPull, 1 );
00123
00124
00125 GPIO_PinModeSet( BSP_BCC_USART_RXPORT, BSP_BCC_USART_RXPIN,
00126 gpioModeInput, 1 );
00127
00128
00129
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
00136 USART_InitAsync( BSP_BCC_USART, &usartInit );
00137
00138
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
00159 bptr = (uint8_t *) pkt;
00160
00161
00162 *bptr++ = RxByte();
00163 if (pkt->magic != BSP_BCP_MAGIC)
00164 {
00165 return false;
00166 }
00167
00168
00169 *bptr++ = RxByte();
00170 if ( (pkt->type < BSP_BCP_FIRST) || (pkt->type > BSP_BCP_LAST) )
00171 {
00172 return false;
00173 }
00174
00175
00176 *bptr++ = RxByte();
00177 if (pkt->payloadLength > BSP_BCP_PACKET_SIZE)
00178 {
00179 return false;
00180 }
00181
00182 #if ( BSP_BCP_VERSION == 2 )
00183
00184 *bptr++ = RxByte();
00185 #endif
00186
00187
00188 length = pkt->payloadLength;
00189 if (length > BSP_BCP_PACKET_SIZE)
00190 {
00191 length = BSP_BCP_PACKET_SIZE;
00192 }
00193
00194
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
00215 pkt->magic = BSP_BCP_MAGIC;
00216
00217
00218 TxByte( pkt->magic );
00219
00220
00221 TxByte( pkt->type );
00222
00223
00224 TxByte( pkt->payloadLength );
00225
00226 #if ( BSP_BCP_VERSION == 2 )
00227
00228 TxByte( pkt->reserved );
00229 #endif
00230
00231
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
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
00267 while (!(BSP_BCC_USART->STATUS & USART_STATUS_TXBL)) ;
00268 BSP_BCC_USART->TXDATA = (uint32_t) data;
00269 txByteCount++;
00270 }
00271
00277 #endif