Zephyr and the BBC Microbit V2 Tutorial Part 3: I2C

The BBC Microbit V2 includes an LSM303AGR IC. This can sense acceleration and magnetic fields in three dimensions. It’s typical use is for tracking orientation and direction of motion. It communicates with the NRF52833 using an I2C (Inter-Integrated Circuit) serial bus. This has two signal lines as shown in the extract from the schematic below:

I2C_INT_SCL : Serial clock line for internal I2C communications

I2C_INT_SDA : Serial data line for internal I2C communications.

The phrase “internal” is used as these signals are not brought out to the edge connector and are used for on-board communications only.

A third connection called I2C_INT_INT allows the LSM303 interrupt the NRF52833 when something “interesting” happens e.g. the device is dropped or tapped.

The trace below shows the a data exchange between the NRF52833 and the LSM303. The I2C_INT_SCL line is used to pace the transmission and reception of data. This signal is generated by the NRF52833 which is playing the role of a master or controller device; the LSM303 is a slave or peripheral device. The trace shows two signals and also a higher level interpretation of what is going between the devices.

The I2C transaction begins with a Start signal. This is a High-Low transition of the SDA line when the SCL is high. Next follows an address value. Each I2C device has a manufacturer set 7 or 10 bit address, this discussion will deal with 7 bit addresses only. Several different devices can exist on the same I2C bus. When a controller wishes to communicate with a peripheral it must first send the peripheral’s address. Other I2C devices on the bus effectively disconnect from the bus for the remainder of that transaction. An additional bit is added just after the 7 bit address called the Read/Write bit. If this bit is a 0 then a write transaction is about to take place; if it’s a 1 then a read operation will follow. Altogether then the controller sends 8 bits at the start of a transaction : the 7 bit address and an R/W bit. It then sets it’s SDA signal to a high impedance state so that it can listen for a returning signal from the peripheral.

If a peripheral on the I2C bus recognizes the 7 bit address it pulls the SDA line to 0. This is an Acknowledge signal. If there is no peripheral with that address then the SDA line will remain high.

The LSM303 has a number of internal registers. Typical interactions with it involve reading from and writing to these registers. Immediately following the I2C addressing phase the register number of interest is transmitted. In the above trace this value is hex AA. The slave acknowledges this write. The transaction shown above actually consists of two parts. The first part writes the register number of interest to the LSM303, the second part reads data from that register (and the one following). The controller indicates this by sending a start signal again (labelled Sr = repeated start). If that had been the end of the transaction the controller would have sent a stop signal.

The last phase of this transaction is a read of the registers within the LSM303. Once again the I2C address is sent but this time the least significant bit i.e. the R/W bit is a 1 to indicate that a register read is required. Following the Ack from the peripheral the controller puts its SDA line into a high impedance or listening state and outputs 16 clock pulses. The peripheral takes control of the SDA line and sends back two bytes of data. A Negative Acknowledge or NACK signal is sent by the controller to indicate that the transaction is done. It follows this by sending a Stop signal (a low to high transition of the SDA line when the SCL line is high).

Overall the transaction in the graph above is a read of registers 0x2A and 0x2B in the LSM303. These contain the low and high byte values for the Y acceleration. Each byte could have been read separately (slower) but the LSM303 allows you read multiple bytes in successive registers by setting the most significant bit of the register number to a ‘1’ so register number 0xAA represents a transaction involving register number 0x2A and subsequent registers within the LSM303.

The code for interfacing with the LSM303 is shown below. Inside the function lsm303_ll_begin a pointer to the I2C device structure for device I2C_1 is obtained. The code then attempts to read a particular register within the LSM303 which contains a known value. This is a mechanism to allow us check to see whether the device is actually on the bus. In this case, the register number in question is 0x0f and it should contain the value 0x33 (decimal 51). If all of this works ok the function then sets the operating mode of the accelerometer : +/- 2g range with 12 bit resolution.

The function lsm303_ll_readAccelY performs the transaction shown in the I2C trace above. It makes use of the Zephyr I2C API function i2c_burst_read. This function takes the following arguments:

A pointer to the I2C device

The address of the I2C peripheral

The number of the register from which you want to read

A pointer to a receive buffer

A count of the number of bytes required.

Two other function lsm303_ll_readRegister and lsm303_ll_writeRegister facilitate reading and writing of single bytes from/to registers.

Note the way lsm303_ll_readAccelY combines the low and high bytes and scales them to a value of acceleration scaled up by a factor of 100.

static const struct device *i2c;
int lsm303_ll_begin()
{
	int nack;
	uint8_t device_id;
	// Set up the I2C interface
	i2c = device_get_binding("I2C_1");
	if (i2c==NULL)
	{
		printf("Error acquiring i2c1 interface\n");
		return -1;
	}	
	// Check to make sure the device is present by reading the WHO_AM_I register
	nack = lsm303_ll_readRegister(0x0f,&device_id);
	if (nack != 0)
	{
		printf("Error finding LSM303 on the I2C bus\n");
		return -2;
	}
	else	
	{
		printf("Found LSM303.  WHO_AM_I = %d\n",device_id);
	}
	lsm303_ll_writeRegister(0x20,0x77); //wake up LSM303 (max speed, all accel channels)
	lsm303_ll_writeRegister(0x23,0x08); //enable  high resolution mode +/- 2g
	
	return 0;
}

int lsm303_ll_readAccelY() // returns Temperature * 100
{
	int16_t accel;
	uint8_t buf[2];
	buf[0] = 0x80+0x2a;	
	i2c_burst_read(i2c,LSM303_ACCEL_ADDRESS,0xaa, buf,2);
	accel = buf[1];
	accel = accel << 8;
	accel = accel + buf[0];
	accel = accel / 16; // must shift right 4 bits as this is a left justified 12 bit result
	// now scale to m^3/s * 100.
	// +2047 = +2g
	int accel_32bit = accel; // go to be wary of numeric overflow
	accel_32bit = accel_32bit * 2*981 / 2047;
    return accel_32bit;    
}

int lsm303_ll_readRegister(uint8_t RegNum, uint8_t *Value)
{
	    //reads a series of bytes, starting from a specific register
    int nack;   
	nack=i2c_reg_read_byte(i2c,LSM303_ACCEL_ADDRESS,RegNum,Value);
	return nack;
}
int lsm303_ll_writeRegister(uint8_t RegNum, uint8_t Value)
{
	//sends a byte to a specific register
    uint8_t Buffer[2];    
    Buffer[0]= Value;    
    int nack;    
	nack=i2c_reg_write_byte(i2c,LSM303_ACCEL_ADDRESS,RegNum,Value);
    return nack;
}

These functions can be used as follows to send the Y acceleration to the serial port

void main(void)
{
	int ret;
		
	ret = lsm303_ll_begin();
	if (ret < 0)
	{
		printf("\nError initializing lsm303.  Error code = %d\n",ret);	
		while(1);
	}
	while(1)
	{    
        printf("Accel Y (x100) = %d\n",lsm303_ll_readAccelY());
         k_msleep(100);

	}
}

This particular example illustrates a low level (hence the “ll” in the function names) transaction with the LSM303 over the I2C bus. Zephyr includes a driver for the LSM303 (and may other I2C devices) which performs initialization and scaling.

The following modifications have to be made to prj.conf to trigger the inclusion of the I2C driver into the program:

CONFIG_STDOUT_CONSOLE=y
CONFIG_GPIO=y
CONFIG_I2C=y

Also, app.overlay needs to be modified so that the I2C interface is enabled and pins are assigned to it as follows:

&i2c1 {
        compatible = "nordic,nrf-twim";
        status = "okay";
        sda-pin = < 16 >;   // P0.16 = I2C_INT_SDA
        scl-pin = < 8 >;    // P0.8 = I2C_INT_SCL
};

Full code for this example can be found over here on github.

Zephyr and the BBC Microbit V2 Tutorial Part 2 : Analogue input and output

The NRF52833 has a 12 bit, 8 channel Analogue to Digital Converter (ADC) which allows it to convert signals from analogue sensors into numbers that can be used in calculations. The NRF52833 does not have a Digital to Analogue Converter (DAC) (this is quite common for microcontrollers). Instead it fakes an analogue output capability by doing Pulse Width Modulation (PWM) i.e. by sending a square wave to an output pin and varying the percentage time the pin is high. This allows it to control the average output voltage on that pin. A simple RC filter can be used to filter out the pulses and leave a variable output voltage.

Reading an analogue input

In this example an analogue input is sent to RING0 (there are 5 holes on the Microbit that are designed to accept banana plugs. Ring 0 is the left-most hole when the board is viewed from the speaker side). The RING0 input is connected to Port 0 bit 2 (P0.2) which is also referred to as AIN0 (analogue input 0) in the NRF52833 data sheet. This pin must be configured for operation as an analogue input. This is done using an adc_channel_cfg as shown below.

/*
 * The internal voltage reference is 0.6V
 * The permissable gains are 1/6, 1/5, 1/4, 1/3, 1/2, 1, 2 and 4
 * If a gain of 1/5 is selected then the ADC range becomes 0 to 3V
 */
static const struct device *adc;
// Will read from analog input on P0.2 which is RING 0 on the microbit v2
#define ADC_PORT_BIT 2
struct adc_channel_cfg channel_cfg = {
		/* gain of 1/5 */
		.gain = ADC_GAIN_1_5,
		/* 0.6V reference */
		.reference = ADC_REF_INTERNAL,
		/* sample and hold time of 3us is the default (0 setting) on the NRF52833 */
		.acquisition_time = ADC_ACQ_TIME_DEFAULT,
		/* Channel 0 */
		.channel_id = 0,
		/* AIN0 is specified by setting input_positive to 0+1 i.e. an offset of 1  */
		/* This is as a result of the way the PSELP and PSELN registers work in the NRF52 series of devices */
		/* see page 375 of the NRF52 product specificatoin version : 4452_021 v1.3 */
		.input_positive = 1,
		/* Using single ended conversions */
        .differential = 0
};

int adc_begin()
{
	int ret;
	// Configure the GPIO's 	
	adc=device_get_binding("ADC_0");
	if (adc == NULL)
	{
		printf("Error acquiring ADC \n");
		return -1;
	}
	ret = adc_channel_setup(adc, &channel_cfg);
	if (ret < 0)
	{
		printf("Error configuring ADC channel 0\n");
		return -2;
	}		
	return 0;
}

The NRF52833 can use an internal voltage reference of 0.6V as a basis for ADC conversions as well as fractions of the supply voltage. We will use 0.6V as this is independent of the power supply voltage. Each ADC channel can be scaled by an amplifier. This scaling factor is called “gain” and allows us to control the measurable input voltage range. For example, with a gain of 1 and a voltage reference of 0.6 the ADC will produce its maximum digital output value ((2^12) -1 = 4095) when the input is just 0.6V. If we apply a gain of 1/5 then the measurable input voltage range extends to 3V.

The ADC is of the successive-approximation variety and as such, it requires a stable input voltage during the conversion process. A sample-and-hold circuit (a little capacitor) is used to take a snapshot of the input voltage which is then converted. Capacitors take time to charge and it can happen that insufficient time is allowed for this in which case the snapshot will be different to the actual input voltage at that instant. We can avoid this by allowing a long charging period however this reduces the maximum sampling rate. The acquisition_time field of the adc_channel_config structure allows you control this charging period. It is set to the default of 3 microseconds above.

The channel_id field of the adc_channel_config is used to “name” a particular ADC channel. It is a logical name as opposed to a physical channel in the case of the NRF52833. We associate this adc channel with a particular analogue input using the input_positive field. If we want to use AIN0 this field should be set to ‘1’, for AIN1 this should be 2 etc. i.e. one more than the analogue input channel number as described in the NRF52833 datasheet. The reason for the addition of ‘1’ is to do with the way registers are programmed in this particular microcontroller.

The adc_begin function gets a device structure pointer for the ADC and configures a single channel for use.

To make a reading from the ADC we have to pass an adc_sequence structure to the adc_read

static int16_t channel_0_data;  // This will hold the adc result

struct adc_sequence sequence = {        
		/* This is a bitmask that tells the driver which channels to convert : bit n = 1 for channel n */		
		.channels    = (1 << 0),
		/* Where will the data be stored (could be an array if there are multiple channels to convert */
		.buffer      = &channel_0_data,
		/* buffer size in bytes, not number of samples */
		.buffer_size = sizeof(channel_0_data),
		/* 12 bit resolution */
		.resolution  = 12,
		/* nulls for the rest of the fields */
		.options = NULL,
		.calibrate = 0,
		.oversampling = 0,        
};
int adcread()
{
	int ret;
	ret = adc_read(adc, &sequence);	
	return channel_0_data;
}

In our case we are doing a sequence of 1 conversion so a single 16 bit result is stored to the channel_0_data variable. The address and size of an array can be passed here instead if multiple samples are to be taken.

Analogue output

As mentioned above, the NRF52833 does not have a DAC so it uses PWM instead to simulate a continuously variable analogue output. This requires us to add a couple of elements to our project. We need C functions to initialize the PWM output and also to send values to it as shown below

static const struct device *pwm;
int pwm_begin()
{
	int ret;
	// Configure the GPIO's 	
	pwm=device_get_binding("PWM_0");
	if (pwm == NULL)
	{
		printf("Error acquiring PWM interface \n");
		return -1;
	}
	return 0;
}
int pwm_write(uint16_t value)
{
	
	return pwm_pin_set_usec(pwm,3,PWM_PERIOD_US,value,0);
}

The pwm_begin function acquires a pointer to the device structure for the PWM_0 device. The pwm_write function takes a single argument which is the number of microseconds the associated output pin should be high in each PWM cycle. The constant PWM_PERIOD_US in this example is set to 100 so the incoming parameter to this function should be in the range 0 to 100. The pwm_pin_set_usec function takes 5 arguments:

A pointer to the PWM device structure

The pin number that is to be controlled

The PWM period expressed in microseconds

The PWM high-time expressed in microseconds

A “flags” value which can be used to set the PWM output polarity (0 works fine here)

I have chosen to use P0.3 as the PWM output pin. This is connected to RING1 on the BBC microbit which makes it easy to use with banana plugs. The PWM output can be routed to other pins but I have found that not all of them work (probably due to being configured for use with other peripherals by the OS).

The pwm_begin function acquires a pointer to the device structure for the PWM_0 device. The pwm_write function takes a single argument which is the number of microseconds the associated output pin should be high in each PWM cycle. The constant PWM_PERIOD_US in this example is set to 100 so the incoming parameter to this function should be in the range 0 to 100. The pwm_pin_set_usec function takes 5 arguments:

A pointer to the PWM device structure

The pin number that is to be controlled

The PWM period expressed in microseconds

The PWM high-time expressed in microseconds

A “flags” value which can be used to set the PWM output polarity (0 works fine here)

I have chosen to use P0.3 as the PWM output pin. This is connected to RING1 on the BBC microbit which makes it easy to use with banana plugs. The PWM output can be routed to other pins but I have found that not all of them work (probably due to being configured for use with other peripherals by the OS).

The app.overlay file.

The analogue input and output routines shown above require an additional file be created in the project directory: app.overlay. This file can override and add to settings in the default device tree (dts) file for this device which is to be found in zephyr/boards/arm/bbc_microbit_v2/bbc_microbit_v2.dts. In this file, the adc and pwm devices are disabled. Also, there are no pins assigned to the PWM subsystem. We can fix all of this with the following app.overlay file:

&adc {
	status = "okay";
};
&pwm0 {
	status = "okay";
	ch0-pin = <3>; // P0.3 is labelled RING1 on the microbit. (connected to pin 1 in breakout board)
};

Putting it all together

The following main function reads a value from the ADC and writes a proportional value to the PWM system. The average output voltage should therefore track the input voltage (it will be a little higher because the output switches between 0 and 3.3V. If the input voltage is 3V then the duty will be 100% resulting in an output voltage of 3.3V)

void main(void)
{
	int ret;
	ret = adc_begin();	
	if (ret < 0)
	{
		printf("\nError initializing adc.  Error code = %d\n",ret);	
		while(1);
	}
	ret = pwm_begin();	
	if (ret < 0)
	{
		printf("\nError initializing PWM.  Error code = %d\n",ret);	
		while(1);
	}
	while(1)
	{       
		uint32_t adcvalue = adc_readDigital();
		printf("ADC Digital = %u\n",adcvalue);
		/* The default version of printf does not support floating point numbers so scale up to an integer */
		printf("ADC Voltage (mV) = %d\n",(int)(1000*adc_readVoltage()));
		pwm_write((adcvalue * PWM_PERIOD_US)/4095);
		k_msleep(100);
	}
}

Full source code is available over here on github

Zephyr and the BBC Microbit V2 Tutorial Part 1 : GPIO

Note: all examples used in this tutorial can be found in full over here on github

When should you use an operating system?

There is no simple answer here other than this : “When the value it provides is greater than the cost of learning and using it”.

Among the value offerings of operating systems is hardware abstraction, complex library support, communications protocols and security. Developing these features/libraries from scratch is error prone and time consuming. There is no doubt that you will bring a product to market faster by using a good existing OS and it is likely that your maintenance burden will be reduced. You may also find it easier to recruit developers for such an OS in contrast to using a home-grown solution. That said, using an OS, even a free one, is not cost free. You will have to set up a development environment, learn about it’s libraries and API’s and possibly live with a bigger memory footprint, I/O timing jitter, and a higher CPU load. This may then cause you to raise the hardware specification of your MCU. Elicia White, author of Embedded Systems advises that you should consider using an OS for your MCU project once you get into the realms of networking and/or USB. This application domain is IoT so we will take that advice and base our application on an existing embedded operating system.

Choosing an OS

Factors affecting your choice: Cost, Code size (Flash memory), RAM usage, Hardware support, ongoing support and updates, licensing, value added features such as integration with IoT services such as remote firmware update and messaging. In the case of the BBC Microbit V2 there are not that many options for an embedded OS. The MCU at the heart of the Microbit-V2 is an NRF52833 from Nordic semiconductors. Nordic provides a “binary blob” to manage the radio interface and other hardware elements (this is referred to as a soft-device). In many ways this resembles an operating system. Application developers link this blob with their code and interact with it using an API. Embedded operating systems on this platform also interact with the soft-device and provide an additional range of services. Embedded OS options for the NRF52833 include:

FreeRTOS, Zephyr, and Riot OS (there may be more). Of these Zephyr stood out as having a very active development community. It is licensed using the Apache 2.0 license which is quite permissive. Nordic Semiconductors also seem to be actively supporting this OS so for these reasons, Zephyr was chosen.

What is Zephyr?

Zephyr is a designed to run on microcontrollers with a limited amount of ROM, RAM and CPU resources. It targets a range of MCU cores including various ARM devices, Intel x86, RISC-V and ESP32. This means that application development skills you acquire on one hardware platform can be transferred to other devices.

When we use the phrase “Operating System” we may be inclined to think of desktop operating systems such as Windows, OS-X, Linux etc. Desktop OS’s allow you load and run programs dynamically. Embedded operating systems such as Zephyr do not work like this. The OS and application are compiled together into one single file which is programmed on the target device. When the system starts up, the OS is booted and your application runs. Typically, your application is the only one running on the target system (it may have multiple threads but that’s another story). In this sense, you can consider OS’s such as Zephyr to be like a library that you might link with your own code.

Setting up a working environment.

In order to build applications for Zephyr you need to set up a compiler, libraries, header files and a host of other tools. This environment is sometimes referred to as a toolchain. Detailed instructions for setting up Zephyr on your computer are available here: https://docs.zephyrproject.org/latest/getting_started/index.html

Note: At the end of the installation instructions you are told to test your toolchain and board by compiling a simple blinking LED example. This will not work with the Microbit-V2 as there is no “simple” user LED on the board. You can however build the hello world example as follows:

west build -p auto -b bbc_microbit_v2 samples/hello_world –pristine

The output from this program is sent to your PC using a built-in USB-Serial converter in the Microbit. On Linux this will appear as device /dev/ttyACM0 typically. On Windows this will appear as COM3 or similar. Run a dumb terminal application with a baud-rate of 115200bps, 8 data bits and no parity and you should hopefully see the output on you PC screen.

The BBC Microbit (V2) hardware

The Microbit V2 has a number of built-in peripherals that are accessible by the programmer. These are shown above. The LED matrix is a arranged in a 5 row by 5 column matrix with one GPIO (GPIO= General Purpose Input Output port) row pin supplying (sourcing) current and a GPIO column pin absorbing (sinking) current. There are also two push buttons which are pulled high via 10k resistors; when a button is pressed it pulls a GPIO pin low. The edge connector provides access to GPIO pins some of which are also used by the onboard peripherals. So, if you plan to use an edge connector pin be sure that it does not interfere with an onboard peripheral that you also intend to use.

The onboard LSM303AGR is a 3 axis accelerometer and 3 axis magnetometer. It is used for motion sensing. It is connected to the NRF52833 via an I2C bus (signals can be viewed on board test points)

Zephyr and I/O pins.

Zephyr uses the a system called devicetree to identify GPIO pins, I2C devices and other peripherals. It is quite confusing for beginners (like me) to use and makes extensive use of C macros. In an effort to avoid turning this into a tutorial on devicetree the example projects will make minimal use of devicetree and will instead use Zephyr API’s to access I/O where possible.

Making patterns on the LED matrix

The full code for this example is in the project led_matrix

The LED matrix is wired as shown above. The Input/Output list is as follows:

SignalPortBitSource/Sink
ROW1GPIO021Source
ROW2GPIO022Source
ROW3GPIO015Source
ROW4GPIO024Source
ROW5GPIO019Source
COL1GPIO028Sink
COL2GPIO011Sink
COL3GPIO031Sink
COL4GPIO15Sink
COL5GPIO030Sink

All of these pins must be configured as outputs (because your program will set them high or low). The Source pins must be a High to light an LED and the Sink pins must be Low.

The matrix can be configured in code as follows:

#define ROW1_PORT_BIT 21
#define ROW2_PORT_BIT 22
#define ROW3_PORT_BIT 15
#define ROW4_PORT_BIT 24
#define ROW5_PORT_BIT 19
 
#define COL1_PORT_BIT 28
#define COL2_PORT_BIT 11
#define COL3_PORT_BIT 31
#define COL4_PORT_BIT 5
#define COL5_PORT_BIT 30
 
static const struct device *gpio0, *gpio1;
int matrix_begin()
{
    int ret;
    // Configure the GPIO's     
    gpio0=device_get_binding("GPIO_0");
    if (gpio0 == NULL)
    {
        printf("Error acquiring GPIO 0 interface\n");
        return -1;
    }
    gpio1=device_get_binding("GPIO_1");
    if (gpio0 == NULL)
    {
        printf("Error acquiring GPIO 1 interface\n");
        return -2;
    }
    ret = gpio_pin_configure(gpio0,ROW1_PORT_BIT,GPIO_OUTPUT);
    ret = gpio_pin_configure(gpio0,ROW2_PORT_BIT,GPIO_OUTPUT);
    ret = gpio_pin_configure(gpio0,ROW3_PORT_BIT,GPIO_OUTPUT);
    ret = gpio_pin_configure(gpio0,ROW4_PORT_BIT,GPIO_OUTPUT);
    ret = gpio_pin_configure(gpio0,ROW5_PORT_BIT,GPIO_OUTPUT);
     
    ret = gpio_pin_configure(gpio0,COL1_PORT_BIT,GPIO_OUTPUT);
    ret = gpio_pin_configure(gpio0,COL2_PORT_BIT,GPIO_OUTPUT);
    ret = gpio_pin_configure(gpio0,COL3_PORT_BIT,GPIO_OUTPUT);
    ret = gpio_pin_configure(gpio1,COL4_PORT_BIT,GPIO_OUTPUT);
    ret = gpio_pin_configure(gpio0,COL5_PORT_BIT,GPIO_OUTPUT);
     
    matrix_all_off();   
    return 0;   
}

The Zephyr API calls used are:

get_device_binding and gpio_pin_configure

The first of these get_device_binding returns a pointer to a Zephyr device structure. The behavior is similar to the file open function fopen in C. If the device can’t be found a null is returned otherwise you use the returned value for future operations on that device. The argument passed to get_device_binding is GPIO_0 (and later GPIO_1). This string is used to search the device tree and if a matching device label is found the function returns a pointer to it’s device structure. So the first few lines of matrix_begin retrieve pointers to the gpio0 and gpio1 devices.

The gpio_pin_configure function takes three arguments:

A pointer to the device structure for that GPIO port

The bit number being configured

The bit mode. In our case this is GPIO_OUTPUT . You could specify GPIO_INPUT for input pins. See https://docs.zephyrproject.org/latest/reference/peripherals/gpio.html for further information about port pin configuration options.

In order to make these GPIO pins go high or low you should call the gpio_pin_set function as shown in the following function:

void set_row1(int state)
{
    gpio_pin_set(gpio0,ROW1_PORT_BIT,state);
}

Once again, three arguments are required: you must tell it the port, the bit number and whether the pin is to be High (1) or Low (0).

The function below can be used to put a pattern on the matrix. The row and column states are passed as the 5 least significant bits of the rows and cols parameters.

void matrix_put_pattern(uint8_t rows, uint8_t cols)
{
    set_row1(rows & 1);
    rows = rows >> 1;
    set_row2(rows & 1);
    rows = rows >> 1;
    set_row3(rows & 1);
    rows = rows >> 1;
    set_row4(rows & 1);
    rows = rows >> 1;
    set_row5(rows & 1);
     
    set_col1(cols & 1);
    cols = cols >> 1;
    set_col2(cols & 1);
    cols = cols >> 1;
    set_col3(cols & 1);
    cols = cols >> 1;
    set_col4(cols & 1);
    cols = cols >> 1;
    set_col5(cols & 1);
}

Finally, here is the main function that generates the matrix pattern. Note the inversion of the cols variable in the call to matrix_put_pattern. This is because column bits are active low (they sink current).

void main(void)
{
    int ret;
    uint8_t rows = 1;
    uint8_t cols = 1;
    ret = matrix_begin();
    if (ret < 0)
    {
        printf("\nError initializing LED matrix.  Error code = %d\n",ret);  
        while(1);
    }
    while(1)
    {       
        matrix_put_pattern(rows, ~cols);
        cols = cols << 1;
        if (cols > 16)
        {
            cols = 1;
            rows = rows << 1;
            if (rows > 16)
            {
                rows = 1;
            }
        }
        k_msleep(100);
    }
}

Other bits on the edge connector can be used in a similar way. For example looking at the extract from the Microbit V2 schematic below we can see that P2 or Ring 0 is connected to GPIO port 0, bit 2.

Reading inputs.

The Microbit has two push buttons that pull low when pushed. The configuration of these inputs follows the same pattern as the outputs above. First identify the port and bit number involved and use gpio_pin_configure to configure them as digital inputs (GPIO_INPUT). To read a pin state we call on gpio_pin_get passing two arguments: a pointer to the port device structure and the bit number in question. The function will return 0 or 1 depending on the pin state or and error code (negative value ) if something went wrong.

// Both buttons are on GPIO0
#define BUTTON_A_PORT_BIT 14
#define BUTTON_B_PORT_BIT 23
static const struct device *gpio0;
int get_buttonA()
{
    return gpio_pin_get(gpio0,BUTTON_A_PORT_BIT);
}
int get_buttonB()
{
    return gpio_pin_get(gpio0,BUTTON_B_PORT_BIT);
}
int buttons_begin()
{
    int ret;
    // Configure the GPIO's     
    gpio0=device_get_binding("GPIO_0");
    if (gpio0 == NULL)
    {
        printf("Error acquiring GPIO 0 interface\n");
        return -1;
    }
    ret = gpio_pin_configure(gpio0,BUTTON_A_PORT_BIT,GPIO_INPUT);
    ret = gpio_pin_configure(gpio0,BUTTON_B_PORT_BIT,GPIO_INPUT);
    return 0;
}

void main(void)
{
    int ret;
    uint8_t rows = 1;
    uint8_t cols = 1;
    ret = matrix_begin();
    if (ret < 0)
    {
        printf("\nError initializing LED matrix.  Error code = %d\n",ret);  
        while(1);
    }
    ret = buttons_begin();  
    if (ret < 0)
    {
        printf("\nError initializing buttons.  Error code = %d\n",ret); 
        while(1);
    }
    while(1)
    {       
        matrix_put_pattern(rows, ~cols);
        if (get_buttonA()==0)
        {
            cols = cols << 1; // only change pattern when button A pressed
        }
        if (get_buttonB()==0)
        {
            rows = cols = 1; // reset pattern to start condition
        }
        if (cols > 16)
        {
            cols = 1;
            rows = rows << 1;
            if (rows > 16)
            {
                rows = 1;
            }
        }
        k_msleep(100);
    }
}

The full code for this example is in the project buttons_with_matrix

The above example uses “polling” i.e. continuous reading of the state of the inputs to decide what to do. This is inefficient as the CPU is running at full speed and may miss inputs if it is doing some other task. An alternative approach is to use interrupts which will trigger the execution of a particular function (the interrupt handler) when a hardware event occurs. If the CPU is busy doing something else this task will be suspended while the interrupt handler executes (so long as interrupts have been enabled. The example below is a modification of the above button code.

static fptr button_a_user_handler = NULL;
static struct gpio_callback button_a_cb;
static void button_a_handler(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
{
    printk("interrupt\n");
    if (button_a_user_handler)
    {
        button_a_user_handler();
    }
}
int attach_callback_to_button_a(fptr callback_function)
{
    if (gpio_pin_interrupt_configure(gpio0,BUTTON_A_PORT_BIT,GPIO_INT_EDGE_FALLING) < 0)
    {
        printk("Error configuring interrupt for button A\n");
    }
    gpio_init_callback(&button_a_cb, button_a_handler, (1 << BUTTON_A_PORT_BIT) );    
    if (gpio_add_callback(gpio0, &button_a_cb) < 0)
    {
        printk("Error adding callback for button A\n");
    }
    else
    {
        // success so far so use the user supplied callback function
        button_a_user_handler = callback_function;
    } 
    return 0;
}

The function gpio_pin_interrupt_configure takes three arguments: a reference to the GPIO port, the bit number in question and a flag to indicate whether you want to be interrupted on falling or rising edges etc.

The function gpio_init_callback is used to prepare a structure of type gpio_callback which will be used in a follow-on call to the gpio_add_callback function. gpio_init_callback takes three arguments: the address of the structure that will be prepared, the address of the function that will handle the callback and bitmask identifying which pins will trigger the callback (note the difference between the third parameter here and the second parameter for gpio_pin_interrupt_configure).

The function gpio_add_callback is the last step in setting up interrupts. It takes two arguments : a reference to the GPIO port and the structure that was prepared by gpio_init_callback.

Following all of this, when a falling edge (a button press) happens the function button_a_handler will be called automatically. The declaration of this function is a little complex so I have decided to hide this from the typical user. Instead, the user calls on attach_callback_to_button_a and passes the address of a function they would like called when the button is pressed. This address is stored and when button_a_handler is activated it will call the user function there.

The main function matching this example is shown below.

volatile uint8_t rows = 1;
volatile uint8_t cols = 1;
void button_a_pressed(void)
{
    cols = cols << 1;
    if (cols > 16)
    {
        cols = 1;
        rows = rows << 1;
        if (rows > 16)
        {
            rows = 1;
        }
    }
}
void main(void)
{
    int ret;
    ret = matrix_begin();
    if (ret < 0)
    {
        printf("\nError initializing LED matrix.  Error code = %d\n",ret);  
        while(1);
    }
    ret = buttons_begin();  
    if (ret < 0)
    {
        printf("\nError initializing buttons.  Error code = %d\n",ret); 
        while(1);
    }   
    attach_callback_to_button_a(button_a_pressed);
    while(1)
    {       
        matrix_put_pattern(rows, ~cols);
        k_msleep(100);
         
    }
}

The full code for this example is in the project buttons_with_matrix_with_interrupts

Note the use call to attach_callback_to_button_a takes a single argument – the address of the function you would like to run. In this case, the callback function moves the led matrix pattern variables along a step each time the button is pressed. Also note the use of the volatile keyword.