Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

hanzibal's Content

There have been 386 items by hanzibal (Search limited from 24-May 23)


By content type

See this member's


Sort by                Order  

#57567 MIP tcp/ip stack running on Netduino mini !!

Posted by hanzibal on 14 April 2014 - 02:04 PM in Project Showcase

Great, looking forward too see the vid!



#56563 MIP tcp/ip stack running on Netduino mini !!

Posted by hanzibal on 01 March 2014 - 09:05 PM in Project Showcase

If you got a Netduino with networking, you should probably stick to that since it's faster and has a smaller memory footprint but yes, you can flash non-network firmware and rely on mip alone for tcp/ip.

Mip is 100% managed code which is great in many ways but at the same time makes it less effective in terms of system resources than the stack compiled into the firmware of the networked Netduino models.



#56343 Extensive driver for the MCP23S17 I/O expander

Posted by hanzibal on 24 February 2014 - 07:16 PM in Project Showcase

Hmm, thats odd - it's the multi SPI mgr reporting an attempt to add the same object twice. Let me get back to you shortly...



#56341 Extensive driver for the MCP23S17 I/O expander

Posted by hanzibal on 24 February 2014 - 07:02 PM in Project Showcase

Ah, I see it now, in the inner for loop, you should replace pinCnt with icPins. Sorry, I think that was my fault, I misinterpreted your variable naming. Hopefully, it will work after that mod.



#56336 Extensive driver for the MCP23S17 I/O expander

Posted by hanzibal on 24 February 2014 - 10:16 AM in Project Showcase

Regarding LEDs of Bank 1 lighting up unexpectedly, I went back to the information you got about your board:

 

..."[color=rgb(0,0,0);font-family:arial, sans-serif;font-size:13px;]On the left-hand chip (Bank 0) A0 is pulled to ground through 10K?. On the right hand chip (Bank 1) A0 is pulled up to Vcc.[/color]

[color=rgb(0,0,0);font-family:arial, sans-serif;font-size:13px;]On both chips A1 and A2 are both pulled down to ground through 10K?, and the two solder jumpers BA0 and BA1 can be shorted to link A1 and A2 to Vcc respectively"....[/color]

 

As the board manufacturer describes it, this would result in the first chip (Bank 0) getting {A0,A1,A2} = {0,0,0} corresponding to hwAddr 0 (zero) which would be correct in reference to your code.

 

It shouldn't matter if I have got the bit order wrong in my code because we're dealing with all zeroes for Bank 0.

 

However, I think we should concentrate on getting rid of any exceptions first before investigating this any further.




#56344 Extensive driver for the MCP23S17 I/O expander

Posted by hanzibal on 24 February 2014 - 07:31 PM in Project Showcase

Problem ought to be the Object.GetHashCode method returning the same value for two different instances which I never anticipated in the context of how it's being used here. Among other things, MSDN says this about GetHashCode:

...The GetHashCode method can be overridden by a derived type. If GetHashCode is not overridden, hash codes for reference types are computed by calling the Object.GetHashCode method of the base class, which computes a hash code based on an object's reference; for more information, see RuntimeHelpers.GetHashCode. In other words, two objects for which the ReferenceEquals method returns true have identical hash codes...

I'll figure something out - meanwhile you could try using only Bank 0 and see if you can get that working.



#56330 Extensive driver for the MCP23S17 I/O expander

Posted by hanzibal on 24 February 2014 - 08:46 AM in Project Showcase

I'm sorry it's not working out as hoped. It's a bit strange since "port0000" would be the GPIO_NONE pin and that an attempt has been made to use it more than once. I'm thinking maybe the cast returns zero for some reason? This occurs while in the loop creating one of the output ports right? EDIT #1: Pin numbers can be given in any numeric format, the binary value will be the same either way.

 

EDIT #2: Made a few small updates to the code, probably nothing but you could try updating.

 

EDIT #3: What does your main chip and port creation code look like at this point?




#56348 Extensive driver for the MCP23S17 I/O expander

Posted by hanzibal on 24 February 2014 - 08:28 PM in Project Showcase

That is truly great news Andy, I'm really glad to hear that - wonderful !! Perhaps photos or even a short video clip later when you got both banks running on the (hopefully) corrected version now available? Way da go - persistance pays!



#56604 Extensive driver for the MCP23S17 I/O expander

Posted by hanzibal on 03 March 2014 - 08:58 AM in Project Showcase

Carrying the multi SPI conversation on here...
Do you think it is possible to incorporate Steffan's multi spi mgr in to your MCP23S17 driver?

Yes but can this really be achieved with mIP 100% unchanged?

Am I correct in saying the use of a bus is to allow for multiple port changes to be made in one write, thus increasing speed and efficiency?

If this is correct, then I also understand that I can't make a bus spanning multiple chips, however if for example I wanted to change the state of 10 pins 5 on each chip, then I could create a class that identifies which pins are on which bus and perform 2 writes one for each bus?

Both correct and the best way to do this is probably for you to wrap the driver in another class of your own.



#56351 Extensive driver for the MCP23S17 I/O expander

Posted by hanzibal on 24 February 2014 - 11:07 PM in Project Showcase

That's great too and thanks for sharing the photos and everything on your blog. From what I understand, you now got 32 digital ports all nicely lined up in a single array that you can index from 0 to 31. If you'd want to light up, say pin at index 23, you'd just go like so:
Outs[23].Value = true;
Am I right? In the context of this example, you don't have to care about the internal pin numbers being 2's complement. However, when working with buses, it's very conveniant becuase bit masking then becomes very easy. Note that buses cannot span multiple chips and also note that your array is not a bus, it's an array of pins which is not quite the same thing.



#56200 Extensive driver for the MCP23S17 I/O expander

Posted by hanzibal on 19 February 2014 - 10:54 AM in Project Showcase

What's this?
This thread is about a software driver for a chip called MCP23S17 that provides 16 additional digital IO pins to a micro controller such as the one on your Netduino.

Posted Image

The MCP23S17 gives you 16 digital input, output and interrupt enabled pins over SPI. Up to eight chips can be combined on a single SPI bus to provide a maximum of 128 pins.

Background
I was originally looking to make a dual ] board (Arduino shield form factor) for this driver to be used with. As so often however, I haven't found the time to complete the board so I decided to release the driver code for others to use.

About the software
The driver class is actually quite powerful and has a lot of features. The whole interface somewhat mimics that of native Netduino GPIOs in how they are created and used. Full source code is attached to this post.

You can create individual pins (input, output, interrupt) and also buses to represent a collection of pins in parallell.

The buses are very powerful - for example, you can easily create a 4 pin bus to be used in controlling an LCD or something like that.

You can also read and write pins or buses in a so called "burst mode" - this will result in bit streams in either direction at about a tenth of the configured SPI speed.

The driver can also be used in a special "direct mode" where all the built-in code for thread safe pin management etc is bypassed in favor of more speed.

How to use it
Simply download the attached code file and add it to your project and start coding with it - these lines of code the only ones needed for an output port:
// create driver to use SPI bus #1 and Netduino pin 17 for chip selectvar chip = new MCP23S17(var io = new MCP23S17(SPI.SPI_module.SPI1, Pins.GPIO_PIN_17);// create a port for pin 1var outport = chip.CreateOutputPort(MCP23S17.Pins.GPIO_1);// drive pin 1 highoutport.Value = true;
There are more examples abailable in posts ahead. There's currently no separate documentation for the software but it's easy to adopt and there are lots of intelli-comments in the code to help guide you through.

I'd love for you guys to try it out and then hear about your experiences!

Hardware
Obviously, you need one or more MCP23S17 chips as well. The chip is available in a breadboard friendly DIP28 package and breakout boards are available on eBay and other places.

You wire the chip just as you would any other SPI slave device - connect power, ground, miso, mosi, clock, chip select and optionally an additional pin to receive interrupts.

NOTE: This driver is for the SPI version of the chip, i.e. MCP23S17 and not the regular MCP23017 which uses I2C. I was planning on isolating the transport layer to support both but I will probably never get around doing that so if you're up for it...feel free!

Code revision history:
v1.1 - corrected a bug involved in cascading
v1.2 - added internal multi SPI mgr
V1.3 - made a few insignificant clean ups
v1.4 - corrected a nasty bug in the multi SPI mgr

Attached File  MCP23S17.cs   57.85KB   62 downloads

Attached Thumbnails

  • image.jpg



#56346 Extensive driver for the MCP23S17 I/O expander

Posted by hanzibal on 24 February 2014 - 08:10 PM in Project Showcase

Found an embarrassing bug in the multi SPI mgr - code is updated and hopefully problem is gone now...

 

Btw, your blinker loop looks suspicious - shouldn't you have a delay during the OFF period as well?




#56319 Extensive driver for the MCP23S17 I/O expander

Posted by hanzibal on 23 February 2014 - 10:24 PM in Project Showcase

....Any ideas?...

I just updated the driver code in post #1 - it now contains an internal light weight multiple SPI manager so you shouldn't be getting that exception anymore. Please try it when you can and report back since I haven't actually tested it myself yet :unsure: because all my Netduinos are occupied :) This also means sharing a single SPI bus among multiple driver instances should work, both when cascading using a shared CS line and when using separate per chip CS lines. EDIT: Sorry for messing with your code, but may I suggest:
for (int ic = 0; ic < icCnt; ic++){    chip[ic] = new MCP23S17(SPI_Devices.SPI1, Pins.GPIO_PIN_D10, Pins.GPIO_NONE, ic, 7000);    // Define all outputs    for (int pin = 0; pin < pinCnt; pin++)        Outs[16 * ic + pin] = chip[ic].CreateOutputPort((MCP23S17.Pins)(1 << pin), false);}
Really anxious to know if the driver works with your board!



#56232 Extensive driver for the MCP23S17 I/O expander

Posted by hanzibal on 20 February 2014 - 08:00 PM in Project Showcase

I guess I could add another constructor to better suit your needs, but it turns out its a little more complicated than that.

 

For now you can specify Cpu.Pin.GPIO_NONE for the IRQ pin like so for the first chip using hwAddr=0:

var chip0 = new MCP23S17(SPI.SPI_module.SPI1, Pins.GPIO_PIN_17, Cpu.Pin.GPIO_NONE, 0, 7000);

and like so for the second chip using hwAddr=1:

var chip1 = new MCP23S17(SPI.SPI_module.SPI1, Pins.GPIO_PIN_17, Cpu.Pin.GPIO_NONE, 1, 7000);

In cascaded mode, these chips can, not only, share the same SPI bus but also use the same chip select (!CS) line because they (as with your board) can use the hardwired address pins (A0,A1,A2) to differentiate bus messages as belonging to one or the another chip. This is a feature to reduce the number of IO lines required meaning that you can have up to 8 chips connected to the same four pin SPI bus.

 

However, because I never got around to testing the driver in cascaded mode with multiple chips, it turns out it currently doesn't support running two instances simultaneously. This means that, until further, you'll have to settle for using one of your chips at a time.

 

I'm afraid you have to bare with me until I've had the time to add support for sharing the same SPI module between multiple instances.




#56229 Extensive driver for the MCP23S17 I/O expander

Posted by hanzibal on 20 February 2014 - 05:48 PM in Project Showcase

Ok, I've fixed the hwAddr bug and updated the driver source code file of the first post in this thread.

 

About your board, I can't find any schematics and can't tell from just looking at the pictures how the A0,A1,A2 pins of the chips are connected but looking into their source code, it seems one chip has h/w address 0 (zero) and the other has h/w address 1 so you should just pass in those values and it should work.

 

This means you have to create one driver instance for each of the chips.

 

At a first glance, it seems they haven't broken out the IRQ pins of the chips which is too bad because then you cannot use the interrupt functionality of the driver.

 

The driver class has multiple constructors, normally you simply choose the one that fits your needs, e.g. the one without the IRQ pin parameter but when come to think of it, I'm uncertain as of there is a constructor that 1) has the hwAddr parameter and 2) does not have the IRQ pin parameter. I'll have to look into this and will get back to you.




#56217 Extensive driver for the MCP23S17 I/O expander

Posted by hanzibal on 20 February 2014 - 07:29 AM in Project Showcase

Yes, there's an optional constructor parameter hwAddr to be used when cascading multiple chips. The IRQ pin is optional, you only need it if you require interrupts on input pins. Regarding pin assignents, could you point me to the board you've got so that I can have a look? EDIT: To calculate what value of hwAddr to use, you must first figure out how pins A0,A1,A2 are wired on your expander board - they will be tied to gnd or vcc in any contellation forming a binary number for you to specify for the hwAddr parameter. Oh and I just discovered a bug regarding how the driver treats hwAddr so please wait for me to update the code file attached to the original post above!



#56205 Extensive driver for the MCP23S17 I/O expander

Posted by hanzibal on 19 February 2014 - 02:07 PM in Project Showcase

Here's a series of example on how to use the driver.

using MCP23S17Lib;// print code versionDebug.Print(MCP23S17.Version);// create a new instance of the driver class using pin 17 for !CS and pin 18 for IRQvar io = new MCP23S17(SPI.SPI_module.SPI1, Pins.GPIO_PIN_17, Pins.GPIO_PIN_18);// create an input port for MCP23S17 pin 0var p0 = io.CreateInputPort(MCP23S17.Pins.GPIO_0);// create an input port for MCP23S17 pin 1var p1 = io.CreateInputPort(MCP23S17.Pins.GPIO_1);// create an output port for MCP23S17 pin 2var p2 = io.CreateOutputPort(MCP23S17.Pins.GPIO_2);// set p2 to the logical AND between p0 and p1p2.Value = p0.Value && p1.Value;// toggle p2 a few times (really quickly)p2.BurstWrite(new bool[]{true, false, true, false, true, false});// create an interrupt port for pin 12. Apply a weak pull up and have it fire interrupts on low edgesvar ip12 = io.CreateInterruptPort(MCP23S17.Pins.GPIO_12, MCP23S17.PullupMode.Pullup, MCP23S17.InterruptMode.EdgeLow);// attach an interrupt handler to pin 12ip12.OnInterrupt += (MCP23S17.Pins pin, bool value, DateTime time) =>    {        Debug.Print("Interrupt occurred on p12!");    };// quick and dirty single line interrupt handler on pin 14 with defaults (no pull up, both edges)io.CreateInterruptPort(MCP23S17.Pins.GPIO_14).OnInterrupt += (MCP23S17.Pins pin, bool value, DateTime time) =>    {        Debug.Print("Interrupt occurred on pin " + pin.ToString());    };// create a 4 pin output bus (MSB first)var bus = io.CreateBus (    io.CreateOutputPort(MCP23S17.Pins.GPIO_6),    io.CreateOutputPort(MCP23S17.Pins.GPIO_5),    io.CreateOutputPort(MCP23S17.Pins.GPIO_4),    io.CreateOutputPort(MCP23S17.Pins.GPIO_3));// set pins 6 and 4 to logic highbus.Value = 10;// create a 5 bit mixed bus of both inputs and outputs// which must be consecutive (e.g. not scarse)var mixbus = io.CreateBus(    io.CreateInputPort(MCP23S17.Pins.GPIO_11),    io.CreateOutputPort(MCP23S17.Pins.GPIO_10),    io.CreateInputPort(MCP23S17.Pins.GPIO_9),    io.CreateOutputPort(MCP23S17.Pins.GPIO_8),    io.CreateOutputPort(MCP23S17.Pins.GPIO_7));// this will only effect output pinsmixbus.Value = 0xffff;// this will read all five pins ushort mixbusvalue = mixbus.Value;// attach an interrupt handler to the mixed bus mixbus.OnInterrupt += (ushort interruptmask, ushort value, DateTime time) =>{    Debug.Print("Interrupt occurred!nBus value is " + value.ToString());

The above example uses SPI module #1 and pins 17 and 18 on a Netduino mini but you can easily change it to work on other pins and on any Netduino model, just change the first lines.




#56234 Extensive driver for the MCP23S17 I/O expander

Posted by hanzibal on 20 February 2014 - 08:23 PM in Project Showcase

Yes, see my last post on how to use one of the chips. Good luck and please be sure to report back on how it worked out for you!



#56236 Extensive driver for the MCP23S17 I/O expander

Posted by hanzibal on 20 February 2014 - 09:23 PM in Project Showcase

Oh and even though its probably obvious, I should maybe point out that the driver presents the two banks (A0...A7 and B0...B7) as one consecutive pin array 0...15. Microchip tend to use "bank switching" due to legacy tech in how registers of their MCUs are mapped to memory. Most of their chips are probably PIC 18's running custom read-only firmware :-)



#56312 Extensive driver for the MCP23S17 I/O expander

Posted by hanzibal on 23 February 2014 - 09:05 PM in Project Showcase

Yes it is because you are passing in the same SPI module to multiple driver instances.

 

As I said earlier, the driver does not yet support the same SPI module being shared among multiple driver instances.

 

I have to make a few adjustments for that first.

 

Also, I think your pin numbering might still be wrong - you must use values that are integer power of two, e.g. 1,2,4,8,16, etc. Have a look at the example I provided in post #15 above where I'm bit shifting like this "1 << Counter".

 

While waiting for me to update the code, you'd probably benefit more from verifying the driver works in getting a single single chip up and running.




#56306 Extensive driver for the MCP23S17 I/O expander

Posted by hanzibal on 23 February 2014 - 05:32 PM in Project Showcase

You should avoid reading the code :-) I should probably supply more examples to make things clearer but basically you create input, output and interrupt ports in much in the same way you create any Netduino digital port. Furthermore, you can create buses that are collections of multiple pins that you can use in parallell. To answer your question in the other thread regarding using the driver along side of other SPI slaves on the same SPI bus - yes, it is possible to share the SPI bus with another slave but you will be facing the same problem that you always do with multiple SPI slave devices on the same bus. You need some software mechanism to change between different SPI configurations in run time between communication with one or the other slave devices. There have been a number of such very similar "multi SPI" implementations avaialable and I would gladely add support for one. Unfortunately, there's no standard - naturally, everybody who's had the problem have also developed their own solution for it. I think I will make an attempt at adding some kind of generic multi SPI support and will return to that later.



#56315 Extensive driver for the MCP23S17 I/O expander

Posted by hanzibal on 23 February 2014 - 10:04 PM in Project Showcase

Cool, so you "only" need to write your own LcdProvider class where you would then make internal use of the MCP23S17 driver in creating and using a bus from the pins you supply in the parameter list above - correct?




#56262 Extensive driver for the MCP23S17 I/O expander

Posted by hanzibal on 22 February 2014 - 01:30 AM in Project Showcase

Looking great! A couple of things though: 1. You should connect D12 to MISO (assuming you got a Netduino 2). 2. There's a bug in your code - when looping to create your 16 output ports, you cannot pass in counter values 0,1,2,3,4,5,6,...etc casted as pins since these need be integer powers of two, e.g. 1,2,4,8,16,32,64,...etc. You can achieve this like so:
for (uint Counter = 0; Counter < pinCount; ++Counter)    Outs[Counter] = chip1.CreateOutputPort((MCP23S17.Pins)(1 << Counter), false);
However, I really think you should be using the enums, then your code will be easier to read and will work even if I change the implementation. For example, you would have avoided the above bug :-) I see your expander board has the IRQ pins broken out after all which is great news but you might want to add that later when you got the basics working. Hope this helps!



#56313 Extensive driver for the MCP23S17 I/O expander

Posted by hanzibal on 23 February 2014 - 09:08 PM in Project Showcase

ok.. another great work Andy. so... actually i dont need to drive LCD display using M74HC595. I think it should be easier to drive it using MCP2317.... :rolleyes: thanks in advance for hanz and andy.

Yes, that's an excellent idea - and it would be really cool to see the software being used to drive an LCD too! TIP: Try using BurstWrite on a bus... Thanks to the pins A0,A1,A2 of each chip (reflected through the hwAddr constuctor parameter), you can cascade up to 8 chips on the same SPI bus producing a maximum of 128 GPIOs all with the same CS line - but, I need to make a few adjustments to allow the same SPI module to be shared among multiple driver instances. At present, such an attempt would result in an exception of type "resource already in use" or something like that - just like mcinnes01 just experienced. NOTE: The driver is for the SPI version, e.g. the MCP23S17 with an "S" in the middle - not the regular MCP23017 which runs on I2C.



#56500 Dive Computer and Rebreather Controller Based on a Netduino Mini

Posted by hanzibal on 27 February 2014 - 03:29 PM in Project Showcase

Very impressive, it requires quite some skill.

Partly reflow soldered or everything by hand?

What are the differences compared to Chris's version?




home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.