0
\$\begingroup\$

Sorry for my broken English in advance.

I am trying to make my atsam4ls2b communicate with an mcp23s17. However, I can not get it to work. I am sending what i think is the correct data over the Mosi pin to the mcp23s17, and when checking the pins with a multimeter I can see that it arrives at the mcp23s17. however it seems like it does nothing with the data. I am hoping anyone can help me out with this problem. Below is my code which is in a loop. it should set all gpio pins to output 3.3v, but it outputs nothing.

SPICon->StartTransaction();
SPICon->Transceive(0x40);//select standard first 4 bits(0100) with opcode 000(A2-A1-A0)(opcode is not used for now) and R/W bit on Write (0) (0100-000-0)
SPICon->Transceive(0x0A);//select IO control register
SPICon->Transceive(0x38);//disable slew rate, sequential and enables address pins
SPICon->EndTransaction();

SPICon->StartTransaction();
SPICon->Transceive(0x40);//select standard first 4 bits(0100) with opcode 000(A2-A1-A0)(opcode is not used for now) and R/W bit on Write (0) (0100-000-0)
SPICon->Transceive(0x0B);//select IO control register
SPICon->Transceive(0x38);//disable slew rate, sequential and enables address pins
SPICon->EndTransaction();

SPICon->StartTransaction();
SPICon->Transceive(0x40);//select standard first 4 bits(0100) with opcode 000(A2-A1-A0)(opcode is not used for now) and R/W bit on Write (0) (0100-000-0)
SPICon->Transceive(0x00);//select IO direction register A
SPICon->Transceive(0x00);//set all pins to output
SPICon->EndTransaction();

SPICon->StartTransaction();
SPICon->Transceive(0x40);//select standard first 4 bits(0100) with opcode 000(A2-A1-A0)(opcode is not used for now) and R/W bit on Write (0) (0100-000-0)
SPICon->Transceive(0x01);//select IO direction register B
SPICon->Transceive(0x00);//set all pins to output
SPICon->EndTransaction();

SPICon->StartTransaction();
SPICon->Transceive(0x40);//select standard first 4 bits(0100) with opcode 000(A2-A1-A0)(opcode is not used for now) and R/W bit on Write (0) (0100-000-0)
SPICon->Transceive(0x14);//select Latch A
SPICon->Transceive(0xFF);//set pins to high
SPICon->EndTransaction();

SPICon->StartTransaction();
SPICon->Transceive(0x40);//select standard first 4 bits(0100) with opcode 000(A2-A1-A0)(opcode is not used for now) and R/W bit on Write (0) (0100-000-0)
SPICon->Transceive(0x15);//select Latch B
SPICon->Transceive(0xFF);//set pins to high
SPICon->EndTransaction();

where the transceive function is:

uint8_t SoftwareSPI::Transceive(uint8_t DataOut)
{
    uint8_t DataIn = 0;

    for(int i = 0; i < 8; i++)
    {
        MoSiPin->SetValue((DataOut >> (7 - i) & 0x01) ? GpioValue::High : GpioValue::Low);
        ClockPin->SetValue(GpioValue::High);

        DelayUs(20);

        GpioValue MisoValue = MiSoPin->GetValue();

        DataIn |= (((uint8_t)MisoValue) << (7 - i));
        ClockPin->SetValue(GpioValue::Low);

        DelayUs(20);
    }

    return DataIn;
}

which basicly does: set the mosi pin to the the value of bit i in the byte DataOut -> set the clock pin to high -> delay to make sure changes go through -> put the value of the miso in in bit i of the byte DataIn -> set the clock pin to low -> delay again to make sure changes go through

this happens 8x so it goes through the whole byte.

the StartTransaction and EndTransaction just pull the CS down and up.

the address pins are all 3 connected to the ground, the mosi pin is connected to pinA22, miso to pinA21, clock to pinA20, chipselect to pinB04, vdd and vss are connected to 3.3V and ground. all gpios are just connected to a led for now.

I am using Atmel Studio 7 as compiler. the program uses some custom made libraries which have been tested correct.

Can anyone see what is wrong here?

Thank you in advance.

\$\endgroup\$
10
  • 1
    \$\begingroup\$ Could you describe "can not get it to work" and also the environment a bit? Currently we have a bit of code that uses some unknown libraries, unknown compiler, unknown hardware setup, we don't know what is it supposed to do (or, what the problem is), and neither what have you tried to solve the problem. \$\endgroup\$
    – domen
    Commented Sep 25, 2018 at 8:10
  • \$\begingroup\$ I added some extra info explaining it a bit more \$\endgroup\$
    – user199409
    Commented Sep 25, 2018 at 8:59
  • 1
    \$\begingroup\$ It's a bit tricky because you have two unknowns - your bitbanged SPI implementation and communication with mcp23s17. Connect MISO to MOSI (without gpio extender), and see if SPI works fine when loopbacked. Have you got an oscilloscope to check what exactly happens on the wire? Have you got a simpler SPI device to try (temperature sensor for example)? Have you checked your clock polarity is correct? \$\endgroup\$
    – domen
    Commented Sep 25, 2018 at 9:15
  • \$\begingroup\$ unfortunately i do not have an oscilloscope to check the wires, nor do i have another spi device. when the MISO and MOSI are connected together it works just like you'd expect. im pretty sure the clock polarity is correct, i switched it around anyway to try but that didn't help. \$\endgroup\$
    – user199409
    Commented Sep 25, 2018 at 9:37
  • 1
    \$\begingroup\$ Check the errata, I remember something funky with the address pins. These parts do work, I use a lot of them in a commercial product. \$\endgroup\$
    – Dan Mills
    Commented Sep 25, 2018 at 9:53

1 Answer 1

1
\$\begingroup\$

apparently I had to connect the nReset pin to 3.3V aswell, which was not connected before. after connecting the pin everything works perfectly, thanks for the quick responses :D

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.