Raspberry Pi SPI Loopback Testing

This post explains how to do SPI loopback testing in RaspberryPi.
This is the pinout of RaspbeyyPi GPIO header.

Next short the MOSI and MISO pins (GPIO10 and GPIO09)

Now download SPI test code from below link


Compile it

gcc -o spidev_test spidev_test.c

Run the code using spi dev

./spidev_test -D /dev/spidev0.0

If you are getting the hex characters as shown in screenshot, the SPI loopback is working.


Enabling SPI on Raspberry Pi

The Serial Peripheral Interface bus (SPI) is a synchronous serial communication interface specification used for short distance communication, primarily in embedded systems. The interface was developed by Motorola.

SPI devices communicate in full duplex mode using a master-slave architecture with a single master. The master device originates the frame for reading and writing. Multiple slave devices are supported through selection with individual slave select (SS) lines.

Read more details on Wikipedia

The SPI bus specifies five logic signals:

SCLK: Serial Clock (output from master).
MOSI: Master Out Slave In (data output from master).
MISO: Master In Slave Out (data output from slave).
SDIO: Serial Data I/O (bidirectional I/O)
SS: Slave Select (often active low, output from master).

I am using RaspberryPi Model B. First i turned on the Pi, i used NOOBS Raspbian operating system (https://www.raspberrypi.org/downloads/noobs/). I don’t have a HDMI display, so i connected it to network found out the ip of the Pi and did ssh using the following command

ssh pi@ip-address

The default password for ssh is “raspberry” if you are using NOOBS debian distro.

The SPI is not enabled by default. To enable SPI issue the following command

sudo raspi-config

Then Select Interfacing Options

Select SPI

It will ask for Enable or Disable, Enable SPI

Finish the config and reboot

After reboot you can see two spidev’s by issuing the command

ls /dev/spi*

This mean SPI is enabled on your Raspberry Pi

Increase USBFS memory limit in Ubuntu

When using USB3 camera on Linux you may face this USB memory limit issue.
By default, Linux limits image capture to 2 MB. To capture images over 2 MB, extend the USBFS limit on how many buffers can be locked into the driver.

sudo sh -c 'echo 1000 > /sys/module/usbcore/parameters/usbfs_memory_mb'

To confirm that you have successfully updated the memory limit, run the following command:

cat /sys/module/usbcore/parameters/usbfs_memory_mb

To set the maximum usbfs memory limit permanently:
Open the /etc/default/grub file in any text editor. Find and replace:


with this:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash usbcore.usbfs_memory_mb=1000"

Update grub with these settings:

$ sudo update-grub

You need to reboot the system after updating grub.

Fail2ban – Prevent Postfix Brute Force


# apt-get install fail2ban

To limit memory usage, add to /etc/default/fail2ban:

+ulimit -s 256

Local config file
Create a local config file /etc/fail2ban/jail.local to override settings in jail.conf:

To ban authentication via SSH after 3 failed attempts add the following to /etc/fail2ban/jail.local:

enabled   = true
port      = ssh
filter    = sshd
logpath   = /var/log/auth.log
maxretry  = 3


# /etc/init.d/fail2ban restart

Postfix and Dovecot
To ban authentication to Postfix and Dovecot after 3 failed attempts add the following to /etc/fail2ban/jail.local:

enabled = true
port = pop3,pop3s,imap,imaps
filter = dovecot
logpath = /var/log/mail.log
maxretry  = 3

enabled  = true
port     = smtp,ssmtp
filter   = postfix
logpath  = /var/log/mail.log
maxretry  = 3

enabled   = true
port      = smtp,ssmtp,imap2,imap3,imaps,pop3,pop3s
filter    = sasl
logpath   = /var/log/mail.log
maxretry  = 3

Fail2ban (Debian Squeeze) isn’t shipped with a configuration for Dovecot, so create /etc/fail2ban/filter.d/dovecot.conf:

failregex = (?: pop3-login|imap-login): .*(?:Authentication failure|Aborted login \(auth failed|Aborted login \(tried to use disabled|Disconnected \(auth failed).*rip=(?P\S*),.*
ignoreregex =

Restart fail2ban:

# /etc/init.d/fail2ban restart

To ban IPs after 3 failed login attempts add the following to /etc/fail2ban/jail.local:

enabled = true
port    = http,https
filter  = apache-auth
logpath = /var/log/apache*/*error.log
maxretry = 3

Copy Large amount of data using rsync

Copying large amount of data,like 300 or 400GB’s from one hdd to another using nautilus or any file manager may fail on some occasions. Using the command rsync will be a better idea to copy large piles of data. This will show the progress percentage and the amount of data copied.

rsync -ah --info=progress2 SOURCE_PATH DESTINATION_PATH

change SOURCE and DESTINATION path according to your needs.

Install and Configure OpenCV in Code Blocks

First install dependency packages

Install Code Blocks IDE

sudo apt-get install codeblocks

Required packages

sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

Optional packages

sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

Clone the latest OpenCV from github

git clone https://github.com/opencv/opencv.git

Compile and Install

cd ~/opencv
mkdir release
cd release

For using with QT and OpenGL and for sample code snippets use this cmake


Screenshot from 2016-08-02 23:45:46

Screenshot from 2016-08-03 00:17:29

sudo make install

Screenshot from 2016-08-03 00:17:58

Now open CodeBlocks.Now go to settings –>compiler
Under the compiler settings tab select the other options tab and copy the following command into the pane.

`pkg-config opencv --cflags`

Screenshot from 2016-08-09 21:01:57

Now open the Linker Settings Tab and copy the following command int the other linker options pane:

`pkg-config opencv --libs`

Screenshot from 2016-08-09 21:02:48

Click OK to save the settings.

Happy Hacking 🙂

Syncing Heroku and GitHub repos

First add them:

git remote add origin 
git remote add herkou git@heroku.com:.git

In case if you want to clone and add

git clone https://github.com//.git
git remote add origin  https://github.com//.git

Push and Pull in Github

git remote -v
git pull origin master -- pull from Github Master branch
git push origin master -- push to Github Master branch

Pushing to Heroku

git remote add origin 
git remote add herkou git@heroku.com:.git
git push heroku master -- push to Heroku