Reply
Highlighted
Posts: 259
Registered: ‎02-20-2016
Accepted Solution

UART BGscrpt tutorial for beginners

He guys,

 

Since I wish I used this much earlier and only just now got the hang of it I figured I'd do a quick write up, hoping it helps anyone. What I'm about to share is largely based on Jaakov's SPP example. Feel free to correct me if you spot errors. 
 

http://community.silabs.com/t5/Bluetooth-Wi-Fi-Knowledge-Base/SPP-over-BLE-BGScript-example-for-BGM1...

Download SPP example

 

1. Edit your hardware.xml

Add the code bellow to enable UART to hardware.xml

 

	<!-- UART configuration -->
	<!-- Settings: @115200bps, no RTS/CTS and BGAPI serial protocol is enabled -->
	<uart index="1" baud="115200" flowcontrol="false" bgapi="false" />
	
	<!-- GPIO configuration needed for WSTK UART to work-->
	<gpio port="A" pin="5" mode="pushpull" out="1"/>
	<gpio port="A" pin="3" mode="pushpull" out="0"/>

For some reason the third line of the code above is commented out in the example but I found it's required to make it work. @JaakkoV ?

2. Define the UART endpoint

 

 

export const uart_ep = 2

("export" not required if you only use 1 file.)

 

3. Write your message

use "Call endpoint_send" to send your message to your computer terminal.

 

call endpoint_send(uart_ep, 13, "Hello world\r\n")

Here we use uart_ep defined earlier. then define the length (13) all the chars + r & n which create a line break. 

 

 

4. Fire up your terminal

I'm on a mac so I'm using Coolterm but there's plenty of others especially for Windows.
http://freeware.the-meiers.org/

Make sure to set your baudrate to 115200, all flow control off, parity to none and stop bits to 1.

Here's a screenshot of my settings:

 

settingsUART.png

 

Hope this helps anyone.

Cheers.

 

Posts: 259
Registered: ‎02-20-2016

Re: UART BGscrpt tutorial for beginners

I should add that this only works for strings, if you want to print Ints or hex please look at the helper functions in SPP example. 


Here's a quick example of the int helper function:

procedure print_int32(int32_value)

    call util_itoa(int32_value)(strlen, string(0:strlen))
    call endpoint_send(uart_ep, strlen, string(0:strlen))

end 
Posts: 2,052
Registered: ‎09-01-2015

Re: UART BGscrpt tutorial for beginners

Thanks for contributing!

 

A couple of remarks:

 

I checked the hardware.xml in the download link that you provided and there are no lines commented out? Pins PA5 and PA3 are configured as outputs. ???

 

Then the "magic" behind the endpoint numbers. You have configured in hardware.xml that you want to use UART1. (index = 1). The endpoint corresponding to this uart is endpoint number 2.

 

You could also use UART0 (index = 0) and the endpoint corresponding to this UART is number 5. (don't ask me why the numbering is like that... Robot Happy )

 

The endpoint numbering is documented in BGAPI reference manual. Recap:

UART1 -> endpoint 2

UART0 -> endpoint 5

 

You can also use both UART simultaneously if needed. For example, one UART for communication to external MCU and another UART for debug only.

Posts: 259
Registered: ‎02-20-2016

Re: UART BGscrpt tutorial for beginners

Thanks for pitching in @JaakkoV

 

The hardware XML in the download is as follows:
I highlighted the commented section in red and bold.

I could not get UART to work in my app before enabling that line and thus "un commenting it" 

 

<?xml version="1.0" encoding="UTF-8" ?>

<hardware>

	<sleep enable="true"/>
	
	<!-- UART configuration -->
	<!-- Settings: @115200bps, no RTS/CTS and BGAPI serial protocol is enabled -->
	<!--uart index="1" baud="115200" flowcontrol="false" bgapi="false" /-->

	<!-- GPIO configuration needed for WSTK UART to work-->
	<gpio port="A" pin="5" mode="pushpull" out="1"/>
	<gpio port="A" pin="3" mode="pushpull" out="0"/>
	
	<!-- GPIO configuration for LED1 -->
	<gpio port="F" pin="6" mode="pushpull" out="1"/>
	<!-- GPIO configuration for LED1 -->
	<gpio port="F" pin="7" mode="pushpull" out="1"/>
	
	<!--ctune value="0" /-->
	
</hardware>
Posts: 2,052
Registered: ‎09-01-2015

Re: UART BGscrpt tutorial for beginners

I checked it again and this is the hardware.xml in the zip file that you linked above:

<?xml version="1.0" encoding="UTF-8" ?>

<hardware>
       
	<!-- UART configuration -->
	<!-- Settings: @115200bps, no RTS/CTS and BGAPI serial protocol is enabled -->
	<uart index="1" baud="115200" flowcontrol="false" bgapi="false" />

	<!-- GPIO configuration needed for WSTK UART to work-->
	<gpio port="A" pin="5" mode="pushpull" out="1"/>
	<gpio port="A" pin="3" mode="pushpull" out="0"/>
	
	<!-- GPIO configuration for LEDs -->
	<gpio port="F" pin="6" mode="pushpull" out="0"/>
	<gpio port="F" pin="7" mode="pushpull" out="0"/>

	<!-- NOTE: you may need to adjust the ctune setting based on what module 
	     revision you have. Check the UG119 configuration guide for more info -->
	<ctune value="0" />
	
</hardware>

The uart setting is NOT commented out like in your version? And note also that the ctune setting is there (which should be included only for BGM111 v1) but in your code sample you just posted the ctune setting is commented out.

 

Are you sure you are not referring to your own local copy of the config file instead of the one that is available on our web page? 

Posts: 259
Registered: ‎02-20-2016

Re: UART BGscrpt tutorial for beginners

Could very well be a local copy, problem solved then Robot Happy