Skip to main content
MPasz.2
Associate
February 6, 2020
Question

Nucleo-F103RB and Adafruit C1188 SPI doesn't work

  • February 6, 2020
  • 2 replies
  • 765 views

Hi,

I have small problem with connecting external sensor to my F103RB NUCLEO board. Im trying to connect Adafruit CAP1188 module through SPI normal mode. I send example messages and dont seem to get the right answer(I get 0). I just want to check if everything works correctly and then I want to start writing the proper program. I think there might be a problem with my spi config. Im begginer at programming STM32. Can anyone check my code and help me fix potential problems? Im steering CS pin through PIN_0 on my NUCLEO.

According documentation reading byte 51h should return C8h

#include "stm32f10x.h"
#include <stdio.h>
 
uint8_t spi_sendrecv(uint8_t byte)
{
 // wait for input buffer
 while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
 SPI_I2S_SendData(SPI1, byte);
 
 // wait for output buffer
 while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
 return SPI_I2S_ReceiveData(SPI1);
}
 
uint8_t read_reg()
{
 GPIO_ResetBits(GPIOC, GPIO_Pin_0);
 spi_sendrecv(0x7A); //Reset
 spi_sendrecv(0x7A); //Reset
 spi_sendrecv(0x7D); // SET ADDRESS POINTER
 spi_sendrecv(0x51); //ADDRES
 spi_sendrecv(0x7F); // READ
 uint8_t value = spi_sendrecv(0xff); //send random message to recieve answer
 GPIO_SetBits(GPIOC, GPIO_Pin_0);
 return value;
}
 
int main(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
 
	//SCK MOSI
	GPIO_InitTypeDef gpio;
	GPIO_StructInit(&gpio);
	gpio.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7; //SCK, MOSI
	gpio.GPIO_Mode = GPIO_Mode_AF_PP; //output push pull
	gpio.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &gpio);
 
	/ MISO
	gpio.GPIO_Pin = GPIO_Pin_6;
	gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING; //input
	GPIO_Init(GPIOA, &gpio);
 
	gpio.GPIO_Pin = GPIO_Pin_0; // CS -> PIN_0
	gpio.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Init(GPIOC, &gpio);
	GPIO_SetBits(GPIOC, GPIO_Pin_0);
 
 
	SPI_InitTypeDef spi;
	SPI_StructInit(&spi);
	spi.SPI_Mode = SPI_Mode_Master; 
	spi.SPI_NSS = SPI_NSS_Soft; // NSS = CS
	spi.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; 
	SPI_Init(SPI1, &spi);
 
	SPI_Cmd(SPI1, ENABLE); 
 
 
	while(1){
		uint8_t result1 = read_reg(); //Checking debug, I see 0x00
	}
}

This topic has been closed for replies.

2 replies

Ozone
Principal
February 6, 2020

As it seems in the schematics at the Adafruit website, the SPI interface is not broken out to the connectors.

You would have to use I2C.

Edit:

At least not on the same connector.

And the thing about the mode-dependant value for R13 is confusing.

MPasz.2
MPasz.2Author
Associate
February 6, 2020

At the website it seems that SPI is avaiable, I used this guide - https://learn.adafruit.com/adafruit-cap1188-breakout/wiring

I also tried using CubeMx and HAL libraries, and same result - nothing. Maybe problem with wiring?