Sivusto ei tue käyttämääsi selainta. Suosittelemme selaimen päivittämistä uudempaan versioon.

Miscap for PiZero

PiZero is the cheapest Raspberry Pi that came on the cover of the MagPi magazine. It is also available for 5€ so it is a very nice and capable processor for controlling lights and sounds.

The PiZero has no Ethernet socket on board and no 3.5mm headphone socket. These missing features were a bit problematic for developing and testing applications for PiZero.

The solution was to build Miscap to get the Missing Capabilities for PiZero.

As the DiscoCap needs the SPI0 for controlling RGP panels I decided to add switches for moving the Ethernet to use the secondary SPI1 bus.

On the other hand the standard Raspbian OS has support for the ENC28j60 module when it sits on SPI0. That is why there is DIP switches on the card to support both.

Miscap also has an eeprom on board. Normally the eeprom is not important. But when you want to use the SPI1 bus together with DiscoCap then the eeprom can be used to set up the Device Tree in the Linux kernel. This should provide plug-and-play functionality of the Miscap/DiscoCap combo. As a side note: DiscoCap had too little board area for the eeprom. So it is not HAT compliant by itself.

You can use Miscap on its own just to get Ethernet and headphones to your PiZero.

Schematics

Here is the schematics of the board.

The protecting diodes for the headphone audio is missing in the proto series. I may also move the chip selects to other pins later.

Assembly

The ENC28J60 comes in different setups. The module I managed to buy from ebay is a bit too wide so it goes over one screw as seen here.

I found out the if you put in the screw and bend the module a bit then the Ethernet goes perfectly in the middle of the board. The forces of bending it a bit are very small so I assume that it does not harm the setup.

HAT complient Device-tree

The current Raspbian has a very cool support that lets add on hardware configure itself automatically. Miscap has two special features. One is SPI based enc28j60 Ethernet controller and the other is PWM audio to non-standards pins. In the "old" system this required editing /boot/config.txt and issuing commands by wiringPi to change the use of the audio pins. This is now history.

All the stuff described below is already programmed into your Miscap. This is here if you have a desire to change things.

For normal operations you have to do nothing. The Ethernet and the audio just works automatically when you plug it into PiZero.

If you want/need to change something I have added instructions of how to create the eeprom blob below. And also how to program the eeprom in Miscap.

-------------------------- setting up the tools ----------------------------------

You need some tools and data files for working with Miscap.

You could start by enabling the Miscap eeprom for editing. Add this line to /boot/config.txt:

dtparam=i2c_vc=on

and reboot the PiZero.

The you also need some extra software

sudo apt-get update

sudo apt-get install git device-tree-compiler

git clone https://github.com/raspberrypi/hats.git

cd hats/eepromutils

make

-------------------------- creating the device-tree file ----------------------------------

The first thing when creating a Device tree is to make the DTS file. This was created by adun, one of my first beta testers. Thank you! It is very well written and functional.

miscap-overlay.dts (http://discohat.com/data/documents/miscap-overlay.dts):
 
/*
Overlay for Miscap pHAT for the Microchip ENC28J60 Ethernet Controller
and 2-channel PWM Audio on GPIO 12,13
Note: the left and right channels are ok if you put Miscap on a RPi2 or RPi3
on a RPi1 or RPi0 the channels are swapped. There is still work being done on the
firmware and dt-blobs that hopefully will find a good solution.
Enables SPI0 using spi-bcm2835 driver with additional CE3 on GPIO 5 and CE2 on GPIO 6
*/

/dts-v1/;
/plugin/;

/ {

compatible = "brcm,bcm2835", "brcm,bcm2836", "brcm,bcm2708", "brcm,bcm2709";

/* SPI0 */
fragment@0 {
  target = <&gpio>;
  __overlay__ {
    spi0_pins: spi0_pins {
      brcm,pins = <7 8 9 10 11>;
      brcm,function = <4>; /* alt0 */
    };     
  };
};
fragment@1 {
  target = <&spi0>;
  __overlay__ {
    #address-cells = <1>;
    #size-cells = <0>;
    pinctrl-0 = <&spi0_pins &spi0_cs_pins>;
    status = "okay";
    cs-gpios = <0>, <0>, <&gpio 6 1>, <&gpio 5 1>;
           
    /*The first two <0>s request that native CS's 0 and 1 are used
      (even though the driver will convert them to GPIO CS's),
      and GPIO's 6 and 5 are used as active-low chip selects 2 and 3. */

    spidev@0{
      compatible = "spidev";
      reg = <0>;   /* CE0 */
      #address-cells = <1>;
      #size-cells = <0>;
      spi-max-frequency = <500000>;
    };

    spidev@1{
      compatible = "spidev";
      reg = <1>;   /* CE1 */
      #address-cells = <1>;
      #size-cells = <0>;
      spi-max-frequency = <500000>;
    };
         
    spidev@2{
      compatible = "spidev";
      reg = <2>;   /* CE2 */
      #address-cells = <1>;
      #size-cells = <0>;
      spi-max-frequency = <500000>;
    };

    spidev@3{
      compatible = "spidev";
      reg = <3>;   /* CE3 */
      #address-cells = <1>;
      #size-cells = <0>;
      spi-max-frequency = <500000>;
    };
  };
};

/*reserve the extra GPIO 5 and 6 so no other drivers can claim them + mark them as outputs */

fragment@2 {
  target = <&gpio>;
  __overlay__ {
    spi0_cs_pins: spi0_cs_pins {
      brcm,pins = <5 6>;
      brcm,function = <1>; /* out */
    };
  };
};
 
/* enc28j60 enthernet */
 
fragment@3 {
  target = <&spi0>;
  __overlay__ {
    /* needed to avoid dtc warning */
    #address-cells = <1>;
    #size-cells = <0>;

    status = "okay";

    spidev@0{
      status = "disabled";
    };

    eth1: enc28j60@0{
      compatible = "microchip,enc28j60";
      reg = <0>; /* CE0 */
      pinctrl-names = "default";
      pinctrl-0 = <&eth1_pins>;
      interrupt-parent = <&gpio>;
      interrupts = <25 0x2>; /* falling edge */
      spi-max-frequency = <12000000>;
      status = "okay";
    };
  };
};

fragment@4 {
  target = <&gpio>;
  __overlay__ {
    eth1_pins: eth1_pins {
      brcm,pins = <25>;
      brcm,function = <0>; /* in */
      brcm,pull = <0>; /* none */
    };
  };
};


/* Audio */

fragment@5 {
  target = <&audio_pins>;
  __overlay__ {
    brcm,pins = <12 13>;
    brcm,function = <4>; /* alt0 */
  };
};

};

 

Once the Miscap-overlay.dts file is ready you need to compile it to binary form.


dtc -@ -I dts -O dtb -o miscap-overlay.dtbo miscap-overlay.dts

 

-------------------------- creating the settings file ----------------------------------

After this the next step is to prepare the information blob to the eeprom.

miscap-settings.txt:

########################################################################
# EEPROM settings text file
#
# Edit this file for your particular board and run through eepmake tool,
# then use eepflash tool to write to attached HAT ID EEPROM
#
# Tools available:
#  eepmake   Parses EEPROM text file and creates binary .eep file
#  eepdump   Dumps a binary .eep file as human readable text (for debug)
#  eepflash  Write or read .eep binary image to/from HAT EEPROM
#
########################################################################

########################################################################
# Vendor info

# 128 bit UUID. If left at zero eepmake tool will auto-generate
# RFC 4122 compliant UUID
product_uuid 00000000-0000-0000-0000-000000000000

# 16 bit product id
product_id 0x0006

# 16 bit product version
product_ver 0x0100

# ASCII vendor string  (max 255 characters)
vendor "Kertatuote Oy"

# ASCII product string (max 255 characters)
product "Miscap"


########################################################################
# GPIO bank settings, set to nonzero to change from the default.
# NOTE these setting can only be set per BANK, uncommenting any of
# these will force the bank to use the custom setting.

# drive strength, 0=default, 1-8=2,4,6,8,10,12,14,16mA, 9-15=reserved
gpio_drive 0

# 0=default, 1=slew rate limiting, 2=no slew limiting, 3=reserved
gpio_slew 0

# 0=default, 1=hysteresis disabled, 2=hysteresis enabled, 3=reserved
gpio_hysteresis 0

# If board back-powers Pi via 5V GPIO header pins:
# 0 = board does not back-power
# 1 = board back-powers and can supply the Pi with a minimum of 1.3A
# 2 = board back-powers and can supply the Pi with a minimum of 2A
# 3 = reserved
# If back_power=2 then USB high current mode will be automatically
# enabled on the Pi
back_power 0

########################################################################
# GPIO pins, uncomment for GPIOs used on board
# Options for FUNCTION: INPUT, OUTPUT, ALT0-ALT5
# Options for PULL: DEFAULT, UP, DOWN, NONE
# NB GPIO0 and GPIO1 are reserved for ID EEPROM so cannot be set

#         GPIO  FUNCTION  PULL
#         ----  --------  ----
#setgpio  2     INPUT     DEFAULT
#setgpio  3     INPUT     DEFAULT
#setgpio  4     INPUT     DEFAULT
#setgpio  5     INPUT     DEFAULT
#setgpio  6     INPUT     DEFAULT
#setgpio  7     INPUT     DEFAULT
setgpio  8     ALT0     DEFAULT
setgpio  9     ALT0     DEFAULT
setgpio  10    ALT0     DEFAULT
setgpio  11    ALT0     DEFAULT
setgpio  12    ALT4     DEFAULT
setgpio  13    ALT4     DEFAULT
#setgpio  14    INPUT     DEFAULT
#setgpio  15    INPUT     DEFAULT
#setgpio  16    INPUT     DEFAULT
#setgpio  17    INPUT     DEFAULT
#setgpio  18    INPUT     DEFAULT
#setgpio  19    INPUT     DEFAULT
#setgpio  20    INPUT     DEFAULT
#setgpio  21    INPUT     DEFAULT
#setgpio  22    INPUT     DEFAULT
#setgpio  23    INPUT     DEFAULT
#setgpio  24    INPUT     DEFAULT
setgpio  25    INPUT     DEFAULT
#setgpio  26    INPUT     DEFAULT
#setgpio  27    INPUT     DEFAULT

 

The next step is to combine the miscap-settings.txt with the miscap-overlay.dtb into a file that can be flashed to the eeprom.

 

./eepmake miscap_settings.txt miscap.eep miscap-overlay.dtbo

 

-------------------------- programming the eeprom ----------------------------------

After all these steps we end up with miscap.eep (http://discohat.com/data/documents/miscap.eep) that cam be flashed to the Miscap eeprom.

I usually want to erase the eeprom first. In order to do this I create a file containing nothing and burn that first.

 

dd if=/dev/zero ibs=1k count=4 of=blank.eep

sudo ./eepflash.sh -t=24c32 -w -f=blank.eep

 

sudo ./eepflash.sh -t=24c32 -w -f=miscap.eep

 

After this everything is programmed into Miscap and you can just reboot to enjoy sound and Ethernet.

 

-------------------------- loading the kernel module enc28j60 ----------------------------------

 

You still need to add a line to /boot/config.txt like this

 

dtoverlay = enc28j60

 

One handy way to test the audio on the headphone jack is to use omxplayer.

 

sudo apt-get install omxplayer

wget https://goo.gl/XJuOUW -O example.mp3 --no-check-certificate
omxplayer -o local example.mp3

Developer

Karri Kaksonen

info@DiscoHAT.com