Reply
Posts: 12
Registered: ‎05-10-2017

BGM111 UART Data receiving

[ Edited ]

Hello everyone, I've been working with Silicon Labs products with quite a while, specially in the BLE area. Now, I want to implement a UART/BLE adapter using a BGM111 (i.e., receiving data from UART Rx and transmitting it via BLE to a peer device  such as an Iphone). However, the current problem I'm dealing with, is related to receive UART Rx Data, below are the functions that I'm using in order to configured the USART1 and its corresponding ISR function, if there is any suggestion or help it would be very appreciated.

 

- Is there another option for receiving RX data such as even driven instead of Interrupt driven?

 

// Function to configured the respective USART (UART) 
void uart_setup(USART_TypeDef* uart) { // Temporal variables USART_InitAsync_TypeDef uartInit = USART_INITASYNC_DEFAULT; // UART I/O configurations: GPIO_PinModeSet(TX_PORT, TX_PIN, gpioModePushPull, 1); GPIO_PinModeSet(RX_PORT, RX_PIN, gpioModeInput, 0); // Enable clock for USART module CMU_ClockEnable(cmuClock_USART1, true); // Enable clock for GPIO module (required for pin configuration) CMU_ClockEnable(cmuClock_GPIO, true); uartInit.enable = usartDisable; // Don't enable UART upon intialization uartInit.refFreq = 0; // Provide information on reference frequency. When set to 0, the reference frequency is the Baud rate uartInit.baudrate = 31250; // Baud rate uartInit.oversampling = usartOVS16; // Oversampling. Range is 4x, 6x, 8x or 16x uartInit.databits = usartDatabits8; // Number of data bits. Range is 4 to 10 uartInit.parity = usartNoParity; // Parity mode uartInit.stopbits = usartStopbits1; // Number of stop bits. Range is 0 to 2 uartInit.mvdis = false; // Disable majority voting uartInit.prsRxEnable = false; // Enable USART Rx via Peripheral Reflex System uartInit.prsRxCh = usartPrsRxCh0; // Select PRS channel if enabled // Initialize UART with uartInit struct USART_InitAsync(uart, &uartInit); // Interrupt handled for receiving Data USART_IntClear(uart, _USART_IFC_MASK); USART_IntEnable(uart, USART_IEN_RXDATAV); NVIC_ClearPendingIRQ(USART1_RX_IRQn); NVIC_EnableIRQ(USART1_RX_IRQn); // Enable RX & TX pins in pins PC7 and PC6, respectively uart->ROUTEPEN = USART_ROUTEPEN_RXPEN | USART_ROUTEPEN_TXPEN; uart->ROUTELOC0 = USART_ROUTELOC0_RXLOC_LOC11 | USART_ROUTELOC0_TXLOC_LOC11; USART_Enable(uart,usartEnable); }

// ISR that triggers when there is Data available inside the RX register
void USART1_TX_IRQHandler(void)
{
// Check for RX data valid interrupt
if (uart_1->IF & USART_IF_RXDATAV)
{
// Save received Data into Rx Buffer
uint8_t rxDataByte = USART_RxDataGet(uart_1);
rxp->data[rxp->index] = rxDataByte;
rxp->index = (rxp->index + 1) % MAX_SIZE;

// For debugging purposes the rxp->state will be changed from the HOLD state to the READY state
rxp->state = READY;
if (rxp->index)
rxp->state = READY;
// Clear Interrupt Flag
uart_1->IFC = USART_IF_RXDATAV;
}
}

  

Posts: 439
Registered: ‎12-05-2016

Re: BGM111 UART Data receiving

Hi @adrianToro,

 

I am going to move this post to the Wireless Bluetooth forum as they should be best suited to offer suggestions on how to handle the use case and let you know if there is existing support for that use case.

 

Thank you,

jpitt

Posts: 12
Registered: ‎05-10-2017

Re: BGM111 UART Data receiving

@jpitt thank you very much for answering and take my case to other people; just to keep all of you updated, I managed to receive the Data from UART RX using the UART RX ISR. Nevertheless, the Data seems not to be the one I'm expecting; I still have some doubts about the relationship between the Registers and the RX data buffer, specially related to the questions below:

 

1. If I'm expecting to receive three bytes of Data and the plan is to save that Data in a 8-bit array of 3 elements (i.e., uint8_t array[3]).

1.1. Will that 3 bytes of Data (coming in a serial manner) triggers the UART ISR three times?. So it would be as easy as just move through the array and save each byte inside each element :

 

// ******** SIMPLE EXAMPLE CODE ********//

//****** Global declarations

uint8_t data_buffer [3];
uint8_t index = 0;

//****** ISR function calls

void USART1_RX_IRQHandler(void)

{
     uint8_t temp_variable = USART_RxDataGet (USART1);

    data_buffer [ index] = temp_variable;
    index++;
    
// index variable initialization
if (index > 2)
index = 0; }

 

2. If not, what is the correct way to receive and fetch the Data into a buffer?

 

 

Posts: 2,597
Registered: ‎08-25-2015

Re: BGM111 UART Data receiving

Hi,

 

Sorry for bouncing your thread again but this question is unrelated with the Bluetooth stack so I moved it to the 32-bit MCU board where you can get on the SoC peripherals.

 

Regards,

Tiago

Highlighted
Posts: 12
Registered: ‎05-10-2017

Re: BGM111 UART Data receiving

Now I'm able to receive Data (coming at 31250 as a baud rate) every time, however, that same Data is not the one I'm expecting, I implement the similar logic  in the Arduino as a POC and the Data is being sent just find, I haven't been able to figure how to receive a clean and logic Data from the Rx buffer in the BGM111 though.

 

This are my last changes:

 

// UART1 Set up function

void uart_setup(USART_TypeDef* uart)
{
	// Temporal variables	
    USART_InitAsync_TypeDef uartInit = USART_INITASYNC_DEFAULT;

    // UART I/O configurations:
    GPIO_PinModeSet(TX_PORT, TX_PIN, gpioModePushPull, 1);
    GPIO_PinModeSet(RX_PORT, RX_PIN, gpioModeInput, 0);

    // Enable clock for peripherals
    CMU_ClockEnable(cmuClock_HFPER, true);

	// Enable clock for USART module 
    CMU_ClockEnable(cmuClock_USART1, true);

    // Enable clock for GPIO module (required for pin configuration) 
    CMU_ClockEnable(cmuClock_GPIO, true);


    uartInit.baudrate     = 31250;         // Baud rate
    uartInit.oversampling = usartOVS16;     // Oversampling. Range is 4x, 6x, 8x or 16x
    uartInit.databits     = usartDatabits8; // Number of data bits. Range is 4 to 10 
    uartInit.parity       = usartNoParity;  // Parity mode 
    uartInit.stopbits     = usartStopbits1; // Number of stop bits. Range is 0 to 2 
    uartInit.mvdis        = false;          // Disable majority voting
    uartInit.prsRxEnable  = false;          // Enable USART Rx via Peripheral Reflex System 



    // Initialize UART with uartInit struct
    USART_InitAsync(uart, &uartInit);
    // Interrupt handled for receiving Data
    NVIC_ClearPendingIRQ(USART1_RX_IRQn);
    NVIC_EnableIRQ(USART1_RX_IRQn);
    USART_IntEnable(uart, USART_IEN_RXDATAV);


    // Enable RX & TX pins in pins PC7 and PC6, respectively
    uart->ROUTEPEN = USART_ROUTEPEN_RXPEN | USART_ROUTEPEN_TXPEN;
    uart->ROUTELOC0 = USART_ROUTELOC0_RXLOC_LOC11 | USART_ROUTELOC0_TXLOC_LOC11;
    USART_Enable(uart,usartEnable);
}
// ISR UART function 

void USART1_RX_IRQHandler(void)
{
  // Check for RX data valid interrupt
  if (uart_1->IF & USART_IEN_RXDATAV)
  {
    // Save received Data into Rx Buffer
	  rxp->data[rxp->index] = USART_RxDataGet(uart_1);
	  rxp->index = (rxp->index + 1) % MAX_SIZE;
	  rxp->pendingBytes++;
  }
}
// UART Data get function 

uint8_t uart_getData(uint8_t* dataPointer, uint8_t dataLen)
{
	uint8_t i;
	uint8_t* dp = dataPointer;
	if (dataLen == 0)
		dataLen = rxp->pendingBytes;
	for (i=0; i<dataLen;i++)
		dp[i] = rxp->data[i];

	return dataLen;
}