EFR32 Mighty Gecko 1 Software Documentation  efr32mg1-doc-5.1.2
bsp_bcc.c
Go to the documentation of this file.
1 /***************************************************************************/
18 #include <string.h>
19 #include "em_device.h"
20 #include "em_cmu.h"
21 #include "em_gpio.h"
22 
23 #include "bsp.h"
24 
25 #if defined( BSP_BCC_LEUART )
26 #include "em_leuart.h"
27 #else
28 #include "em_usart.h"
29 #endif
30 
31 #if defined( BSP_STK )
32 
35 /* Module local variables */
36 static uint32_t rxByteCount;
37 static uint32_t txByteCount;
38 
39 /* Module local prototypes */
40 static void TxByte( uint8_t data );
41 static uint8_t RxByte( void );
42 
45 /***************************************************************************/
50 /***************************************************************************/
55 /**************************************************************************/
61 int BSP_BccDeInit( void )
62 {
63  /* Reset counters */
64  rxByteCount = 0xFFFFFFFFUL;
65  txByteCount = 0xFFFFFFFFUL;
66 
67  BSP_BccPinsEnable( false );
68 
69 #if defined( BSP_BCC_LEUART )
70  /* Reset LEUART */
71  LEUART_Reset( BSP_BCC_LEUART );
72 #else
73  /* Reset USART */
74  USART_Reset( BSP_BCC_USART );
75 #endif
76 
77  /* Disable clock */
78  CMU_ClockEnable( BSP_BCC_CLK, false );
79 
80  return BSP_STATUS_OK;
81 }
82 
83 /**************************************************************************/
89 int BSP_BccInit( void )
90 {
91 #if defined( BSP_BCC_LEUART )
93 #else
95 #endif
96 
97  rxByteCount = 0;
98  txByteCount = 0;
99 
100  /* Enable High Frequency Peripherals */
102 
103  /* Enable clocks to GPIO */
105 
106  /* Enable UART clock */
107  CMU_ClockEnable( BSP_BCC_CLK, true );
108 
109 #if defined( BSP_BCC_LEUART )
110  /* Enable CORE LE clock in order to access LE modules */
111  CMU_ClockEnable(cmuClock_CORELE, true);
112 
113  /* Select CORE LE clock for LE modules */
114  CMU_ClockSelectSet( cmuClock_LFB, cmuSelect_CORELEDIV2 );
115 
116  /* Initialize LEUART */
117  leuartInit.baudrate = 115200;
118  LEUART_Init( BSP_BCC_LEUART, &leuartInit );
119 #else
120  /* Initialize USART */
121  USART_InitAsync( BSP_BCC_USART, &usartInit );
122 #endif
123 
124  /* Initialize UART pins */
125  BSP_BccPinsEnable( true );
126 
127  return BSP_STATUS_OK;
128 }
129 
130 /**************************************************************************/
138 {
139  int i;
140  int length;
141  uint8_t *bptr;
142 
143  /* Setup a byte pointer to start of packet buffer */
144  bptr = (uint8_t *) pkt;
145 
146  /* Receive packet magic */
147  *bptr++ = RxByte();
148  if (pkt->magic != BSP_BCP_MAGIC)
149  {
150  return false;
151  }
152 
153  /* Receive packet type */
154  *bptr++ = RxByte();
155  if ( (pkt->type < BSP_BCP_FIRST) || (pkt->type > BSP_BCP_LAST) )
156  {
157  return false;
158  }
159 
160  /* Receive packet length */
161  *bptr++ = RxByte();
163  {
164  return false;
165  }
166 
167 #if ( BSP_BCP_VERSION == 2 )
168  /* Receive reserved byte */
169  *bptr++ = RxByte();
170 #endif
171 
172  /* Receive packet data length field and sanity check it */
173  length = pkt->payloadLength;
174  if (length > BSP_BCP_PACKET_SIZE)
175  {
176  length = BSP_BCP_PACKET_SIZE;
177  }
178 
179  /* Receive packet payload */
180  for( i=0; i<length; i++ )
181  {
182  *bptr++ = RxByte();
183  }
184 
185  return true;
186 }
187 
188 /**************************************************************************/
196 {
197  int i;
198 
199  /* Apply magic */
200  pkt->magic = BSP_BCP_MAGIC;
201 
202  /* Transmit packet magic */
203  TxByte( pkt->magic );
204 
205  /* Transmit packet type */
206  TxByte( pkt->type );
207 
208  /* Transmit packet length */
209  TxByte( pkt->payloadLength );
210 
211 #if ( BSP_BCP_VERSION == 2 )
212  /* Transmit reserved byte */
213  TxByte( pkt->reserved );
214 #endif
215 
216  /* Transmit packet payload */
217  for ( i=0; i<pkt->payloadLength; i++ )
218  {
219  TxByte( pkt->data[i] );
220  }
221 
222  return BSP_STATUS_OK;
223 }
224 
225 /**************************************************************************/
230 void BSP_BccPinsEnable( bool enable )
231 {
232  if (enable)
233  {
234  /* Configure GPIO pin for UART TX */
235  /* To avoid false start, configure output as high. */
236  GPIO_PinModeSet( BSP_BCC_TXPORT, BSP_BCC_TXPIN, gpioModePushPull, 1 );
237 
238  /* Configure GPIO pin for UART RX */
239  GPIO_PinModeSet( BSP_BCC_RXPORT, BSP_BCC_RXPIN, gpioModeInput, 1 );
240 
241 #if defined(BSP_BCC_ENABLE_PORT)
242  /* Enable the switch that enables UART communication. */
243  GPIO_PinModeSet( BSP_BCC_ENABLE_PORT, BSP_BCC_ENABLE_PIN, gpioModePushPull, 1 );
244 #endif
245 
246 #if defined( BSP_BCC_LEUART )
247  BSP_BCC_LEUART->ROUTE |= LEUART_ROUTE_RXPEN | LEUART_ROUTE_TXPEN | BSP_BCC_LOCATION;
248 #else
249 
250 #if defined( USART_ROUTEPEN_TXPEN )
251  BSP_BCC_USART->ROUTEPEN = USART_ROUTEPEN_TXPEN | USART_ROUTEPEN_RXPEN;
252  BSP_BCC_USART->ROUTELOC0 =
253  ( BSP_BCC_USART->ROUTELOC0 &
255  | ( BSP_BCC_TX_LOCATION << _USART_ROUTELOC0_TXLOC_SHIFT )
256  | ( BSP_BCC_RX_LOCATION << _USART_ROUTELOC0_RXLOC_SHIFT );
257 #else
258  BSP_BCC_USART->ROUTE |= USART_ROUTE_RXPEN | USART_ROUTE_TXPEN | BSP_BCC_LOCATION;
259 #endif
260 #endif
261  }
262  else
263  {
264 #if defined(BSP_BCC_ENABLE_PORT)
265  GPIO_PinModeSet( BSP_BCC_ENABLE_PORT, BSP_BCC_ENABLE_PIN, gpioModeDisabled, 0 );
266 #endif
267 #if defined( BSP_BCC_LEUART )
268  BSP_BCC_LEUART->ROUTE &= ~(LEUART_ROUTE_RXPEN | LEUART_ROUTE_TXPEN);
269 #else
270 #if defined( USART_ROUTEPEN_TXPEN )
271  BSP_BCC_USART->ROUTEPEN &= ~(USART_ROUTEPEN_TXPEN | USART_ROUTEPEN_RXPEN);
272 #else
273  BSP_BCC_USART->ROUTE &= ~(USART_ROUTE_RXPEN | USART_ROUTE_TXPEN);
274 #endif
275 #endif
276 
277  GPIO_PinModeSet( BSP_BCC_TXPORT, BSP_BCC_TXPIN, gpioModeDisabled, 0 );
278 
279  GPIO_PinModeSet( BSP_BCC_RXPORT, BSP_BCC_RXPIN, gpioModeDisabled, 0 );
280  }
281 }
282 
285 static uint8_t RxByte( void )
286 {
287  uint8_t byte;
288 
289  /* Poll RX data available flag and return a character when one is available */
290 
291 #if defined( BSP_BCC_LEUART )
292  while (!(BSP_BCC_LEUART->IF & LEUART_IF_RXDATAV)) ;
293  byte = BSP_BCC_LEUART->RXDATA;
294 #else
295  while (!(BSP_BCC_USART->STATUS & USART_STATUS_RXDATAV)) ;
296  byte = BSP_BCC_USART->RXDATA;
297 #endif
298 
299  rxByteCount++;
300  return byte;
301 }
302 
303 static void TxByte( uint8_t data )
304 {
305  /* Check TX buffer and allow for a pending transfer to complete */
306 
307 #if defined( BSP_BCC_LEUART )
308  while (!(BSP_BCC_LEUART->STATUS & LEUART_STATUS_TXBL)) ;
309  BSP_BCC_LEUART->TXDATA = (uint32_t) data;
310 #else
311  while (!(BSP_BCC_USART->STATUS & USART_STATUS_TXBL)) ;
312  BSP_BCC_USART->TXDATA = (uint32_t) data;
313 #endif
314 
315  txByteCount++;
316 }
317 
323 #endif /* BSP_STK */
Clock management unit (CMU) API.
void CMU_ClockSelectSet(CMU_Clock_TypeDef clock, CMU_Select_TypeDef ref)
Select reference clock/oscillator used for a clock branch.
Definition: em_cmu.c:2521
Board support package API definitions.
#define USART_STATUS_TXBL
#define _USART_ROUTELOC0_RXLOC_MASK
#define LEUART_STATUS_TXBL
BCP Packet Structure - Board controller communication protocol version 2.
Definition: bsp_bcp.h:95
uint8_t magic
Definition: bsp_bcp.h:97
#define _USART_ROUTELOC0_TXLOC_SHIFT
#define USART_STATUS_RXDATAV
uint8_t payloadLength
Definition: bsp_bcp.h:99
#define USART_ROUTEPEN_TXPEN
CMSIS Cortex-M Peripheral Access Layer for Silicon Laboratories microcontroller devices.
uint8_t reserved
Definition: bsp_bcp.h:100
Universal synchronous/asynchronous receiver/transmitter (USART/UART) peripheral API.
#define BSP_STATUS_OK
Definition: bsp.h:45
void LEUART_Init(LEUART_TypeDef *leuart, LEUART_Init_TypeDef const *init)
Init LEUART.
Definition: em_leuart.c:461
void BSP_BccPinsEnable(bool enable)
Enable GPIO pins for the USART/LEUART used for board communication.
Definition: bsp_bcc.c:230
#define BSP_BCP_FIRST
Definition: bsp_bcp.h:40
void GPIO_PinModeSet(GPIO_Port_TypeDef port, unsigned int pin, GPIO_Mode_TypeDef mode, unsigned int out)
Set the mode for a GPIO pin.
Definition: em_gpio.c:269
General Purpose IO (GPIO) peripheral API.
int BSP_BccInit(void)
Initialize board controller communication support (BCC) functionality.
Definition: bsp_bcc.c:89
#define _USART_ROUTELOC0_TXLOC_MASK
void CMU_ClockEnable(CMU_Clock_TypeDef clock, bool enable)
Enable/disable a clock.
Definition: em_cmu.c:1453
uint8_t data[BSP_BCP_PACKET_SIZE]
Definition: bsp_bcp.h:101
Low Energy Universal Asynchronous Receiver/Transmitter (LEUART) peripheral API.
void LEUART_Reset(LEUART_TypeDef *leuart)
Reset LEUART to same state as after a HW reset.
Definition: em_leuart.c:500
#define BSP_BCP_PACKET_SIZE
Definition: bsp_bcp.h:92
int BSP_BccPacketSend(BCP_Packet *pkt)
Send a packet to the board controller.
Definition: bsp_bcc.c:195
void USART_Reset(USART_TypeDef *usart)
Reset USART/UART to same state as after a HW reset.
Definition: em_usart.c:856
#define LEUART_IF_RXDATAV
#define USART_ROUTEPEN_RXPEN
uint8_t type
Definition: bsp_bcp.h:98
#define USART_INITASYNC_DEFAULT
Definition: em_usart.h:356
#define BSP_BCP_LAST
Definition: bsp_bcp.h:57
void USART_InitAsync(USART_TypeDef *usart, const USART_InitAsync_TypeDef *init)
Init USART/UART for normal asynchronous mode.
Definition: em_usart.c:569
#define LEUART_INIT_DEFAULT
Definition: em_leuart.h:132
#define BSP_BCP_MAGIC
Definition: bsp_bcp.h:59
bool BSP_BccPacketReceive(BCP_Packet *pkt)
Get a packet from the board controller.
Definition: bsp_bcc.c:137
int BSP_BccDeInit(void)
Deinitialize board controller communication support (BCC) functionality. Reverse actions performed by...
Definition: bsp_bcc.c:61
#define _USART_ROUTELOC0_RXLOC_SHIFT