9 years, 3 months ago.

Same port for input and otuput in SPI

I'm trying to use a periferic that uses SPI (Hitachi tri-axis accelerometer H48C). It uses only one pin for input and output (DIO). How should I use SPI library in this case? I can't assign the same port for MISO and MOSI... thank you

2 Answers

9 years, 3 months ago.

Here are the pin defines for SPI, but I don't think that device is true SPI with only one Digital I/O, Check the data sheet. But you will probably need to use a DigitalInOut pin.

F030R8

//*** SPI ***
 
const PinMap PinMap_SPI_MOSI[] = {
    {PA_7,  SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF0_SPI1)},
    {PB_5,  SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF0_SPI1)},
    {PB_15, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF0_SPI2)},
    {NC,    NC,    0}
};
 
const PinMap PinMap_SPI_MISO[] = {
    {PA_6,  SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF0_SPI1)},
    {PB_4,  SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF0_SPI1)},
    {PB_14, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF0_SPI2)},
    {NC,    NC,    0}
};
 
const PinMap PinMap_SPI_SCLK[] = {
    {PA_5,  SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF0_SPI1)},
    {PB_3,  SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF0_SPI1)},
    {PB_13, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLDOWN, GPIO_AF0_SPI2)},
    {NC,    NC,    0}
};
 
const PinMap PinMap_SPI_SSEL[] = {
    {PA_4,  SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF0_SPI1)},
    {PA_15, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF0_SPI1)},
    {PB_12, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF0_SPI2)},
    {NC,    NC,    0}
};
 

More details can be found here:

https://developer.mbed.org/users/mbed_official/code/mbed-src/file/3312ed629f01/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/PeripheralPins.c

Edit...

Here you go, this has example code as well:

https://solarbotics.com/download.php?file=528

9 years, 3 months ago.

The H48C sensor is actually connected to an ADC converter MCP3204 that uses the single DIO pin. Internally the MCP3204 device has separate input and output hardware. The shared DIO pin can only work because the transmitter output part is in high impedance when the device is not transmitting. It should not be at high or low level when the device is receiving or it would short out the master that tries to send data to the MCP3204.

Connecting this device to an mbed SPI with separate MOSI and MISO pins can be done as follows:

  • MOSI to one end of a resistor (eg 2K2), other end of the resistor to DIO
  • MISO to DIO
  • SCLK to SCLK
  • CS out to CS in

You can send data by using

spi.write(my data);

The MCP will not short the data on the DIO pin since it is in high impedance. Even when it does try to transmit at the same time as the mbed nothing bad will happen since the resistor protects the MOSI pin.

Reading data will done by sending an 0xFF. This means that the resistor is pulled high at the mbed end and the MCP3204 can pull it low as needed. The MISO pin will read the data from DIO.

data = spi.write(0xFF);