pinballsp
Posts: 17
Joined: Fri Mar 03, 2017 11:32 pm
Contact: Website Facebook Skype YouTube

MCP23S17, Wiringpi or another library

Thu Jul 27, 2017 12:22 pm

Hello.

I need connect two MCP23S17 to Raspberry. I have already designed the hardware, both chips share same SPI bus, each one with a different Chip select.

I have check WiringPi library, but seem this do not allow select two different chip select pin, but access by two SPi ports and if several chips share same port then each chip must to be wired with a different address.

So, my question is if its possible work with WiringPi, two MCP23S17 chips, sharing same SPI bus, same address, but different Chip Select.

Or if its not possible, if somebody know another MCP23S17 library that allow work with two different chip select pins to share two chips in same SPI bus. Im testing now the Piface library, also for MCP23s17, I hope this allow.

Kind Regards

User avatar
DougieLawson
Posts: 38883
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: MCP23S17, Wiringpi or another library

Thu Jul 27, 2017 2:10 pm

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

pinballsp
Posts: 17
Joined: Fri Mar 03, 2017 11:32 pm
Contact: Website Facebook Skype YouTube

Re: MCP23S17, Wiringpi or another library

Thu Jul 27, 2017 3:31 pm

 
Thanks DougieLawson, but with WiringPi I do not see how may I config two MCP23s17, both with same address but different Chip Select, in same SPI bus.

 

User avatar
mikronauts
Posts: 2779
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: MCP23S17, Wiringpi or another library

Thu Jul 27, 2017 4:09 pm

Why on earth would you want to do that?

Save the other CS for another peripheral, you can use up to eight MCP23SXX on one CS.
pinballsp wrote:
Thu Jul 27, 2017 3:31 pm
 
Thanks DougieLawson, but with WiringPi I do not see how may I config two MCP23s17, both with same address but different Chip Select, in same SPI bus.

 
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

User avatar
DougieLawson
Posts: 38883
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: MCP23S17, Wiringpi or another library

Thu Jul 27, 2017 4:12 pm

Code: Select all

#include <wiringPi.h>
#include <mcp23s17.h>

mcp23s17Setup (int pinBase, int spiPort, int devId) ;
The devId defines which MCP23S17 you're addressing your commands to.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

pinballsp
Posts: 17
Joined: Fri Mar 03, 2017 11:32 pm
Contact: Website Facebook Skype YouTube

Re: MCP23S17, Wiringpi or another library

Thu Jul 27, 2017 5:29 pm

mikronauts wrote:
Thu Jul 27, 2017 4:09 pm
Why on earth would you want to do that?
 

Because of hardware PCB is already developed and manufactured, so I can not change it now. I thought was easier share same SPI and control each MCP with a different Chip Select pin.

I have another developments with ARM microcontrollers, and when need connect several devices by SPI, always share same port and select each device with a different chip select pin.

 
Last edited by pinballsp on Thu Jul 27, 2017 5:46 pm, edited 1 time in total.

pinballsp
Posts: 17
Joined: Fri Mar 03, 2017 11:32 pm
Contact: Website Facebook Skype YouTube

Re: MCP23S17, Wiringpi or another library

Thu Jul 27, 2017 5:32 pm

DougieLawson wrote:
Thu Jul 27, 2017 4:12 pm

Code: Select all

#include <wiringPi.h>
#include <mcp23s17.h>

mcp23s17Setup (int pinBase, int spiPort, int devId) ;
The devId defines which MCP23S17 you're addressing your commands to.
 

Yes, I know, but my two MCP23S17 share same address (and SPI port), and I can not change it now, because of my PCB is already manufactured. I only may access to each MCP with a different Raspberry Chip Select pin.

Image

 
Last edited by pinballsp on Sun Jul 30, 2017 12:45 pm, edited 2 times in total.

User avatar
DougieLawson
Posts: 38883
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: MCP23S17, Wiringpi or another library

Thu Jul 27, 2017 5:44 pm

I bet you can with a wire between pin#15, #16 or #17 and GND or VCC (as appropriate). You can always cut the track for those pins on the circuit board with a scalpel or xacto knife.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

pinballsp
Posts: 17
Joined: Fri Mar 03, 2017 11:32 pm
Contact: Website Facebook Skype YouTube

Re: MCP23S17, Wiringpi or another library

Thu Jul 27, 2017 5:50 pm

DougieLawson wrote:
Thu Jul 27, 2017 5:44 pm
I bet you can with a wire between pin#15, #16 or #17 and GND or VCC (as appropriate). You can always cut the track for those pins on the circuit board with a scalpel or xacto knife.
 
Yes, I thought about it too, but only to test one unit now. I have a good microscope, and I may remove one pin of one MCP to connect to + for a different address, and also change also the Chip Select pin. My MCP are both SMD.

If I must change address, then need modify and manufacture new PCB. May be I will do, if no possible control each MCP with a different Chip Select by firmware, sharing same SPI port.

Image

 

User avatar
DougieLawson
Posts: 38883
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: MCP23S17, Wiringpi or another library

Thu Jul 27, 2017 6:11 pm

If you need to use CE0/CE1 you're going to have to change your PCB for pin#11 on one of the MCP23S17s anyway.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

pinballsp
Posts: 17
Joined: Fri Mar 03, 2017 11:32 pm
Contact: Website Facebook Skype YouTube

Re: MCP23S17, Wiringpi or another library

Fri Jul 28, 2017 7:43 am

DougieLawson wrote:
Thu Jul 27, 2017 6:11 pm
If you need to use CE0/CE1 you're going to have to change your PCB for pin#11 on one of the MCP23S17s anyway.
 

My current PCB already connect CE0 and CE1, each one to each MCP, but I can not use with WinringPi because of I use two Chip Select and share same MCP address and same SPI port.

Probably I will modify PCB to work with different addresses for each MCP and only one Chip Select, so will be compatible with Wiringpi.

Also I may modify Wiringpi library or SPI library to disable Chip Select, and activate CE0 or CE1 from my software to access to each MCP with current PCB design.

I made my PCB to share same SPI port and different Chip Select, because its habitual when develop with microcontrollers, and need connect several SPI devices, activate each one with a different Chip Select sharing same SPI port. I did not know that MCP23s17 allow configure a different address for each one, I thought that address was only for I2C.

User avatar
gordon@drogon.net
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: MCP23S17, Wiringpi or another library

Sat Jul 29, 2017 6:16 pm

wiringPi can handle 16 mcp12s17's on the standard SPI port on a Raspberry Pi.

I'm not seeing where your issue is.

In mcp23s17Setup(), there are 3 parameters: pinBase, spiPort and devId.

The pinBase is the new pin number of the first pin of the device - any number >= 64 that you like.
The spiPort is the CE line - 0 or 1
The devId is the devuce sub-address 0 through 7.

So if you have 2 mcp23s17's on the same SPI bus, then you initialise the first one with

mcp23s17Setup (base1, 0, 0);

and the second with

mcp23s17Setup (base2, 1, 0);

Simples.

-Gordon
--
Gordons projects: https://projects.drogon.net/

pinballsp
Posts: 17
Joined: Fri Mar 03, 2017 11:32 pm
Contact: Website Facebook Skype YouTube

Re: MCP23S17, Wiringpi or another library

Sun Jul 30, 2017 1:22 pm

 

OK, thanks, I understand.
Then after config both MCP in same program, I access to status of each pin with the base1 and base2 fields ?

Do not understand yet how work this of BASE field when setup MCP. I see one example where assign #define BASE 123, and seem that access to each pin with BASE + pin number.

Then If I config two MCP, with same SPI port, same address, different chip select, to access to each pin in each MCP, I simply must define different BASE for each MCP ??.

For example I assign BASE1 123 to first MCP, and BASE2 153 to second MCP, then the 16 pins of first MCP are 123 to 138 and pins to second MCP are 153 to 168 ?.

The if I want read first pin of each MCP, I must to do ?
digitalRead (BASE1 + 1)
digitalRead (BASE2 + 1)

And to write ?
digitalWrite (BASE1 + 1 , value)
digitalWrite (BASE2 + 1, value)

I can only read or write one pin by instruction, can not read or write all the 16 pins in one instruction ?.

Thanks.
Last edited by pinballsp on Mon Jul 31, 2017 9:34 pm, edited 1 time in total.

User avatar
gordon@drogon.net
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: MCP23S17, Wiringpi or another library

Sun Jul 30, 2017 9:20 pm

pinballsp wrote:
Sun Jul 30, 2017 1:22 pm
 

OK, thanks, I understand.
Then after config both MCP in same program, I access to status of each pin with the base1 and base2 fields ?

Do not understand yet how work this of BASE field when setup MCP. I see one example where assign #define BASE 123, and seem that access to each pin with BASE + pin number.

Then If I config two MCP, with same SPI port, same address, different chip select, to access to each pin in each MCP, I simply must define different BASE for each MCP ??.

For example I assign BASE1 123 to first MCP, and BASE2 153 to second MCP, then the 16 pins of first MCP are 123 to 138 and pins to second MCP are 153 to 168 ?.

The if I want read first pin of each MCP, I must to do ?
digitalRead (BASE1 + 1)
digitalRead (BASE2 + 1)
The first pin, port A, bit 0, is BASE1 + 0 The last pin, port B, bit 7 is BASE1 + 15 and so on.
And to write ?
digitalWrite (BASE1 + 1 , value)
digitalWrite (BASE2 + 1, value)

I can only read of write one pin by instruction, can not read or write all the 16 pins in one instruction ?.
wiringPi is a pin-based GPIO library, not a port based one.

There is a way to read/write the ports via wiringPi, but you need to do extra code, or ignore wiringPi completely.

-Gordon
--
Gordons projects: https://projects.drogon.net/

pinballsp
Posts: 17
Joined: Fri Mar 03, 2017 11:32 pm
Contact: Website Facebook Skype YouTube

Re: MCP23S17, Wiringpi or another library

Mon Jul 31, 2017 9:18 pm

 

Ok, thank you Gordon.

I will try with libmcp23s17 for Piface, it seems that this allow read/write a full port, GPIOA or GPIOB. Because of in my application I need read in loop two MCP23s17, always all the 16 inputs for each one, so I suppose will be faster read by port (4 read operations) than read pin by pin (32 read operations).

 

Return to “C/C++”