Serial communication protocols are a very convenient way to transfer information between MCUs and other types of electronic devices; this guide will show a way to setup and enable an I2C Serial Interface instance using one of the Wireless Bluetooth SDK examples and the Serial Manager API.
- FRDM-KW41Z development kit
- MCUXpresso IDE version 10.3.1 or newer
- KW41Z SDK with BLE stack and examples (It can be downloaded from the SDK Builder)
Serial Manager Setup as I2C Master
1. Import the freertos version of the “heart_rate_sensor” example included in the KW41Z’s SDK.
Note: Be sure to select UART as your debug console.
2. Increase the size of the heap that’s going to be used by the RTOS, as by default, the example is configured to only assign enough space for the original example to run.
To increase heap size, you should change the following macro to a bigger number, so that the RTOS has enough memory to create the required semaphore and queues for the Serial Manager:
#define gTotalHeapSize_c 9000
Also increase the number of serial manager instances by adding 1 to the value defined in the following macro:
#define gSerialManagerMaxInterfaces_c 1
Finally, make sure you disable the use of Power Down mode for this example by changing the following macro to a 0:
#define cPWR_UsePowerDownMode 0
These macros can be found in the app_preinclude.h header file, inside the source folder of the example project.
3. Create the new header and source files to which you will add your I2C related functions. For this example, we will name the files I2C_Test.h and I2C_Test.c, respectively.
4. Add function prototypes to your newly created header file for two functions, I2C_Init and I2C_send.
Also include the serial manager library:
5. Define the I2C instance that will be used, as well as a global variable for Identification purposes and a test string to send:
#define I2C_INSTANCE (1)
char test_string  = "hello world";
Note: for this example we are using I2C1, but it’s also possible to enable I2C0 using this method by changing the I2C_INSTANCE definition to a 0.
6. Initialize the Serial Manager if it’s not already initialized, and create a new instance of it configured as a I2C Master:
if(Serial_InitInterface(&I2C_peripheral_Id, gSerialMgrIICMaster_c,I2C_INSTANCE) != gSerial_Success_c)
7. Create a function to send your test string. For this function, we will call a Serial Manager function named Serial_Print, which takes a string and puts it in the Tx buffer to be sent to the serial manager instance:
Serial_Print(0, test_string, gNoBlock_d);
8. Write a call to the function in a known part of the program. In this example we will add the call to SW3 Button Press event:
These events can be found inside the “BleApp_HandleKeys” function in the heart_rate_sensor.c source file.
mToggle16BitHeartRate = (mToggle16BitHeartRate)?FALSE:TRUE;
To see the output by using a logic analyzer, and if you’re using I2C1 as your Serial Manager instance, connect the SDA line to J2 pin 9, and the SCL line to J2 pin 10.
Note: Also connect a GND line from the board to your logic analyzer, you can use J2 pin 7 as your GND connection.
When pressing SW3, you should see the following output: