Configure a sensor over SPI from its datasheet
Pick the SPI mode and the fastest clock the sensor allows, then set its data rate and range from the register map, including a non-obvious field encoding. A…
hard · Reading datasheets and schematics · datasheets · driver · spi
Bring up the motion sensor on SPI1, working from the Datasheet excerpt below. Write imu_init(), which must:
- Make PA4 an output that deselects the sensor (high) before it starts driving the pin.
- Configure SPI1 as master in one of the sensor's modes, most significant bit first, with software slave management (SSM and SSI set), at the fastest SCK the sensor allows; then enable it.
- Read WHO_AM_I and return −1 if it is not 0x6B.
- Write CTRL1 for a 104 Hz output data rate and a ±4 g range, then return 0.
The helpers cs() and spi_xfer() under Provided do the byte transfers.
Datasheet excerpt
SPI interface (sensor datasheet)
| Parameter | Value |
|---|---|
| SPI modes | 0 (CPOL 0, CPHA 0) or 3 (CPOL 1, CPHA 1) |
| SCK frequency | 10 MHz maximum |
| Bit order | most significant bit first |
| First byte | bit 7: 1 = read, 0 = write; bits 6:0: register address |
| Following bytes | data, for consecutive registers |
CTRL1 (0x10), read/write
| Bits | Field | Values |
|---|---|---|
| 7:4 | ODR | 0000 power-down, 0001 12.5 Hz, 0010 26 Hz, 0011 52 Hz, 0100 104 Hz, 0101 208 Hz |
| 3:2 | FS | full scale: 00 ±2 g, 01 ±16 g, 10 ±4 g, 11 ±8 g |
| 1:0 | — | reserved, write 00 |
WHO_AM_I (0x0F), read-only: 0x6B.
SPI1 (microcontroller reference manual)
SPI1 is on APB2, which runs at 84 MHz on this board. CR1 bits 5:3 BR: SCK = PCLK2 / 2^(BR+1), so 000 = /2, 001 = /4, 010 = /8, 011 = /16, 100 = /32.
Starter code
int imu_init(void) {
GPIOA->BSRR = 1u << CS_PIN;
GPIOA->MODER = (GPIOA->MODER & ~(3u << (CS_PIN * 2))) | (1u << (CS_PIN * 2));
SPI1->CR1 = SPI_CR1_MSTR | SPI_CR1_BR(2) | SPI_CR1_SSM | SPI_CR1_SSI;
SPI1->CR1 |= SPI_CR1_SPE;
cs(1);
spi_xfer(0x0F);
uint8_t id = spi_xfer(0x00);
cs(0);
if (id != 0x6B) return -1;
cs(1);
spi_xfer(0x10);
spi_xfer(0x44);
cs(0);
return 0;
}Example tests
Bring-up
printf("imu_init() = %d\n", imu_init());
show_clock();Expected output:
gpioa: pin 4 high
gpioa: pin 4 low
gpioa: pin 4 high
gpioa: pin 4 low
imu: write 0x10 = 0x48
gpioa: pin 4 high
imu_init() = 0
SCK 5250000 HzLearn it first
- Reading datasheets and schematics · Where the answers are, min/typ/max, calculating from the tables and reading register maps.
More Reading datasheets and schematics problems
- Build a register from its datasheet table · Assemble the ADS1115 ADC's 16-bit config register from its fields.
- Baud rate divider from the reference manual · Turn the reference manual's baud rate formula into a BRR register value, in both oversampling modes.
- ADC timing from the electrical characteristics · Pick the ADC prescaler and sampling time from the datasheet's limits, and work out the conversion time.
- Bit-bang SPI from a timing diagram · Read the clock polarity and phase off a sensor's timing diagram and drive the pins to match.
- A sensor frame, its CRC and its transfer function · Check the CRC the datasheet specifies, then convert raw readings to temperature and humidity.
- Apply the errata sheet's workarounds · An ADC driver that follows the reference manual still reads wrong values. The errata sheet explains why.
- Route a UART to its pins from the alternate-function table · Find USART2's pins and function number in the datasheet, then set MODER, AFRL, OSPEEDR and PUPDR without disturbing other pins.
- An open-drain interrupt line, from the sensor's pin description · The datasheet says INT is open-drain and active low. Configure the pin so it reads correctly, and report when the sensor asserts it.
- A 10 ms tick, from the clock tree · Work out the timer's input clock from the clock tree, including the APB timer rule, then set the prescaler and auto-reload for a 10 ms period.
- Fast-mode I2C and the EEPROM's address, from three documents · Compose TIMINGR from the reference manual's timing table, work out the EEPROM's address from its datasheet and the schematic, and probe for it.
- Receive from a GPS module, from its interface description · Set the baud rate divider and frame format the module's datasheet gives, with only the receiver enabled, and read one NMEA sentence.
- An LED and a button, from the board schematic · The schematic decides which level lights the LED and which level means pressed. Write the three functions that hide it.
- Battery voltage through a resistor divider · Read the divider ratio and the ADC's reference voltage off the schematic, and turn a raw reading into millivolts.
- Trace an expansion header to the microcontroller's pins · Follow each header pin's net through resistors, a level shifter and solder bridges to the pin it ends at.
- A relay and a sensor's power switch, from the transistors · One transistor turns its load on with a high pin, the other with a low pin. Work out which from the schematic, and keep both off from reset.
- Route a pin's interrupt, from the reference manual · Use the SYSCFG and EXTI register descriptions and the vector table to send any pin's edges to its interrupt.
- Choose a DMA stream from the request mapping table · Find a free stream and channel for a peripheral's DMA request in the reference manual's table, and compose the stream's control register.