Subsection 10.3.6 Part 6: The Pins, and Why They Must Be Open-Drain
Now we’ll look at the STM32C0 peripheral which sits at the other end of the two wires. A peripheral inside the chip has to reach the outside world through pins, and which pins it can use is not up to us (Figure 10.3.19). Even though we only have one I2C peripheral on our STM32C0 chip, it’s referred to as I2C1 for reasons of software compatibility with more capable chips. I2C1’s SCL and SDA signals are available on several pin pairs, and the pair we use is PB8 for SCL and PB9 for SDA, because those two are the Nucleo’s Arduino-standard I2C pins (D15 and D14 on the header) and every Arduino-shaped board puts I2C there.
Getting a peripheral’s signal onto a pin is the alternate-function mechanism you first used for the UART: put the pin in alternate function mode with
MODER, then choose which alternate function with AFR. The number to choose is in the datasheet, not the reference manual: Table 15 of the STM32C031C6 datasheet (DS13867), Port B alternate function mapping, where the PB8 row and the PB9 row both name I2C1 under AF6 (Figure 10.3.20).
/stm32_af6_pb8_pb9_annotated.png)
I2C1_SCL and I2C1_SDA are what is written where they meet. That is where the 6 in GPIO_AF6 comes from. PB6 and PB7 carry I2C1 on AF6 too, which is the “several pin pairs” of Figure 10.3.19 seen from the table’s side. The reference manual does not carry this table; only the datasheet does.One thing that is new is that rather than being in push-pull mode, both pins have to be configured open-drain. This is accomplished via the GPIO output type register
GPIOx_OTYPER (RM0490 §6.4.2), where a 1 in bit \(y\) means “pin \(y\) is open-drain” and 0, the reset value, means push-pull. A push-pull output has two transistors, one pulling the pin up to 3.3 V and one pulling it down, and it always drives the pin to one rail or the other. We made use of this type of behavior when driving LEDs, for example. An open-drain output has only the pull-down: writing a 0 pulls the pin LOW, and writing a 1 turns the transistor off and leaves the pin floating, so the pull-up sets the level.
This behavior is necessary on the I2C bus: SDA changes hands every ninth bit: on a write the controller drives eight bits then lets go of the data line and the device drives the acknowledge; on a read it is the other way round. Letting go of the data line means to let the line float, not to force it to ground or 3.3 V. Figure 10.3.21 is what that moment looks like. If both ends were push-pull, one driving HIGH and one driving LOW would put a transistor from 3.3 V and a transistor to ground both switched on across the same wire, a short circuit, limited only by what the two output stages can survive. With open-drain outputs there is no such path: the strongest thing any device can do is pull the line LOW, the only route from 3.3 V is through the pull-up resistor, and the worst case of two devices disagreeing is that the line reads LOW.
This is also why the pull-up matters. The ACK works because of the pull-up and the open-drain configuration, and so do two other features: several controllers can share one bus (we will see this soon), and a device can hold SCL LOW to ask for more time (we will not be using this feature). You can read more about this in Subsection 10.4.10.
The pull-ups themselves are typically 5 kΩ to 10 kΩ, and for the devices we use in this class we do not need to install any since both come on breakout boards that carry their own. That is worth checking on the datasheet of anything you add.
i2c1_init() also turns on the STM32C031C6’s internal pull-ups, about 40 kΩ, on both lines. They are too weak to be the bus’s only pull-up once a device and its wiring are attached, but they keep the bus HIGH when no device is attached. If there were no pull-up anywhere then the line could never be released HIGH: nothing on an open-drain bus drives HIGH, as the paragraph above explains, so the level is simply undefined. The reason that the value for the pull-up resistor is a range rather than a number is that it is squeezed from both ends: too small and the device pulling LOW has to sink more current than it can while still holding the line below the receiver’s threshold; too large and the resistor charges the bus’s own capacitance too slowly for the line to get back up before the next clock edge. If you are interested in reading about finding a good value for your setup, go on to Subsection 10.4.10.
The pin configurations happen in
i2c1_init(), which we used yesterday already. In that function, we configure the pins first, then the peripheral, with one ordering detail in the middle: the timing register may only be written while the peripheral is disabled, so PE, which enables the peripheral, is cleared before TIMINGR is touched and set again afterwards.
void i2c1_init(void){
// Configure pins PB8 and PB9
// Enable clock access to GPIOB (for PB8 and PB9)
RCC->IOPENR |= RCC_IOPENR_GPIOBEN;
// Set PB8 and PB9 to alternate function mode
GPIOB->MODER = (GPIOB->MODER & ~GPIO_MODER_MODE8_Msk) | (GPIO_ALTERNATE << GPIO_MODER_MODE8_Pos);
GPIOB->MODER = (GPIOB->MODER & ~GPIO_MODER_MODE9_Msk) | (GPIO_ALTERNATE << GPIO_MODER_MODE9_Pos);
// Next, select AF6 for these pins.
GPIOB->AFR[1] = ((GPIOB->AFR[1]) & ~GPIO_AFRH_AFSEL8_Msk) | (GPIO_AF6 << GPIO_AFRH_AFSEL8_Pos);
GPIOB->AFR[1] = ((GPIOB->AFR[1]) & ~GPIO_AFRH_AFSEL9_Msk) | (GPIO_AF6 << GPIO_AFRH_AFSEL9_Pos);
// Set PB8 and PB9 to open drain
GPIOB->OTYPER |= (GPIO_OTYPER_OT8 | GPIO_OTYPER_OT9);
// Enable the internal pull-ups on PB8 and PB9
GPIOB->PUPDR = (GPIOB->PUPDR & ~GPIO_PUPDR_PUPD8_Msk) | (GPIO_PULLUP << GPIO_PUPDR_PUPD8_Pos);
GPIOB->PUPDR = (GPIOB->PUPDR & ~GPIO_PUPDR_PUPD9_Msk) | (GPIO_PULLUP << GPIO_PUPDR_PUPD9_Pos);
// Configure the i2c module
// Enable clock access to I2C1
RCC->APBENR1 |= RCC_APBENR1_I2C1EN;
// Enter reset mode (disable peripheral)
I2C1->CR1 &= ~I2C_CR1_PE;
// Timing settings: Standard mode (100kHz) with 12MHz I2CCLK
I2C1->TIMINGR &= 0x0; // clear timing register
I2C1->TIMINGR |= (0x2 << I2C_TIMINGR_PRESC_Pos);
I2C1->TIMINGR |= (0x4 << I2C_TIMINGR_SCLDEL_Pos) | (0x2 << I2C_TIMINGR_SDADEL_Pos);
I2C1->TIMINGR |= (0xf << I2C_TIMINGR_SCLH_Pos) | (0x13 << I2C_TIMINGR_SCLL_Pos);
// Exit reset mode (enable peripheral)
I2C1->CR1 |= I2C_CR1_PE;
}
The alternate function selection consists of two 32-bit registers (
AFR[0] and AFR[1]), the first one for pins 0–7 and the second one for pins 8–15. So PB8 and PB9 are configured in AFR[1]. The reference manual calls this register for the higher-numbered pins GPIOx_AFRH, which is why the field names read GPIO_AFRH_AFSEL8.
The two
PUPDR lines turn on the STM32C031C6’s internal pull-ups on PB8 and PB9, through the same GPIOx_PUPDR register that turned on the pull-up for the pushbutton on D5 (PB4). With no device attached they keep both lines HIGH, so that a transaction still ends in a NACK instead of hanging.
The four
TIMINGR lines we will leave alone. They set how long SCL spends HIGH, how long it spends LOW, and how long the peripheral waits around the edges before it changes or samples SDA, and the values are the ones this board needs at 100 kHz. Subsection 10.4.6 works out where PRESC comes from and checks SCLL and SCLH against the bus speed, which is worth reading the day you move this code to a board whose clock runs at a different speed.
