Posts: 5
Registered: ‎08-07-2015
Accepted Solution

BGM121 GPIO not working with BGLib



I am having trouble controlling the GPIO lines on a custom board with a BGM121.  I was successful in controlling them using BGScript, but it turns out this design requires more than BGScript can provide, so I switched to using the IAR C compiler with Simplicity Studio.


To be clear, I am using my own board, not a demo board.  I started with the “SOC – Empty” project and am making changes as needed.  Using the hardware configurator, I can configure three GPIO lines to turn on and off LEDs on that board.  However, I cannot change the state of those lines at run-time using the BGLIB C API call

gecko_cmd_hardware_write_gpio(uint8 port, uint16 mask, uint16 data)

Other BGLIB C API calls, such as

gecko_cmd_le_gap_set_mode(uint8 discover, uint8 connect) 

seem to work correctly, since I can discover and connect with the device while it is running.


The LEDs are on lines PD12, PD13, and PD14.  Here is the code generated by the hardware configurator to configure those GPIO lines:



 /* Pin PD12 is configured to Push-pull */
GPIO_PinModeSet(gpioPortD, 12, gpioModePushPull, 1); /* Pin PD13 is configured to Push-pull */
GPIO_PinModeSet(gpioPortD, 13, gpioModePushPull, 1); /* Pin PD14 is configured to Push-pull */ GPIO_PinModeSet(gpioPortD, 14, gpioModePushPull, 1);


That correctly sets the three lines high.


Here is the code I am using to set the lines low:


gecko_cmd_hardware_write_gpio(0x03, 0x7000, 0x0000);


That line is located in the main function after the call to enter_DefaultMode_from_RESET() and before the main loop, as seen here:


void main(void)
  /* Put the SPI flash into Deep Power Down mode for those radio boards where it is available */
/* Initialize peripherals */ enter_DefaultMode_from_RESET(); /* Initialize stack */ gecko_init(&config); gecko_cmd_hardware_write_gpio(0x03, 0x7000, 0x0000);
while (1) { /* Event pointer for handling events */ struct gecko_cmd_packet* evt;
/* Check for stack event. */ evt = gecko_wait_event(); /* Handle events */ switch (BGLIB_MSG_ID(evt->header)) { /* This boot event is generated when the system boots up after reset. * Here the system is set to start advertising immediately after boot procedure. */ case gecko_evt_system_boot_id: /* Set advertising parameters. 100ms advertisement interval. All channels used. * The first two parameters are minimum and maximum advertising interval, both in * units of (milliseconds * 1.6). The third parameter '7' sets advertising on all channels. */ gecko_cmd_le_gap_set_adv_parameters(160,160,7);
/* Start general advertising and enable connections. */ gecko_cmd_le_gap_set_mode(le_gap_general_discoverable, le_gap_undirected_connectable); break;
case gecko_evt_le_connection_closed_id: /* Restart advertising after client has disconnected */ gecko_cmd_le_gap_set_mode(le_gap_general_discoverable, le_gap_undirected_connectable); break; /* Events related to OTA upgrading ----------------------------------------------------------------------------- */ /* Check if the user-type OTA Control Characteristic was written. * If ota_control was written, boot the device into Device Firmware Upgrade (DFU) mode. */ case gecko_evt_gatt_server_user_write_request_id: if(evt->data.evt_gatt_server_user_write_request.characteristic==gattdb_ota_control) { gecko_cmd_system_reset(1); } break; default: break; } } }


I have also tried reconfiguring the GPIO programmatically with

gecko_cmd_hardware_configure_gpio(uint8 port, uint8 gpio, uint8 mode, uint8 output) 

but that also did not have the desired effect.


Can anyone guide me to the correct way of controlling the GPIO lines with BGLib?


Thank you very much,


Posts: 143
Registered: ‎10-03-2015

Re: BGM121 GPIO not working with BGLib

I think your issue is that you are taking a bit of a hybrid approach. If you are using the c SDK then you will likely want to avoid using the Bluetooth stack BGAPI to control gpio. Instead you will want to use emlib which you partially did with the code generated by the configurator. You just need to set and clear the outstanding using commands from emlib.

Posts: 5
Registered: ‎08-07-2015

Re: BGM121 GPIO not working with BGLib

Thank you superpanda.  The documentation for the BGM121 suggests using BGLib and doesn't mention EMLib, but I believe you are correct.  EMLib files are included in the project when it is created and a brief review of the library files looks very promising. 


Thanks for you help!