Posts with «spi» label

Learning to Reverse Engineer on a Broken Printer

When a Lexmark inkjet printer stopped working, [Mojobobo] was able to claim it as his own. He quickly realized that the machine was flooded with ink and not worth repairing, but that didn’t mean he couldn’t still find a use for it. When he learned that the printer’s firmware was not only upgradable but also unprotected, he knew he should be able to get the printer to do his own bidding.

[Mojobobo] started his journey with the motherboard. The unit still powered up, but it was asking to insert a “duplex module” before it would boot any further. [Mojobobo] first tried to find a way to trick the duplex module sensor, but was unsuccessful. His next step was to search for some kind of serial communications port. He didn’t have an oscilloscope, so instead he used a speaker with a wire probe. In theory, if the wire was pressed against an active serial port, he would be able to hear varying tones through the speaker. Sure enough, he found some interesting tones after probing around some ports next to a “JTAG” label. He looked up some information about the nearby chip and found that it included an SPI bus.

After some internet research, [Mojobobo] learned enough about SPI to have a rough idea of how to use it. Having limited tools available to him, he decided to use his Arduino to try to communicate with the motherboard. After wiring up a simple circuit, (and then re-wiring it) he was able to dump the first 4096 bytes of the motherboard’s boot loader to the Arduino via the SPI interface.

[Mojobobo's] next steps will be to find a faster way to dump the boot loader. At 9600 baud, he grew tired of waiting after three hours. Once he has the full boot loader he intends to search for a way to bypass the duplex sensor and get the board to finish booting. Then he may just use the printer for its scanning functions, or he might find other interesting uses for it.


Filed under: peripherals hacks

Tutorial – pcDuino GPIO with Arduino IDE

Introduction

In this tutorial we’ll explain how to use the GPIO pins of the Arduino implementation in the pcDuino v2 and v3. As the v3 is now available you can use it as well, and it’s interchangeable with the v2. Although the pcDuino v2 is Arduino-compatible, there are a few differences that you need to be aware of – in order to make your projects a success and also to avoid any costly mistakes.

This tutorial builds on the knowledge from the initial review, so if pcDuino v2 is new to you please review this article before moving on. In this instalment we’ll run through the following:

  • ADC (analogue to digital)
  • Digital input and outputs
  • PWM (pulse-width modulation)
  • I2C bus
  • SPI bus

Using ADC pins

Just like an Arduino Uno or compatible, the pcDuino v2 has six ADC pins, in the expected locations:

Using the pcDuino v2’s ADC pins is quite straight forward, however you just need to remember a few things about the hardware – that the maximum input voltage on A0 and A1 is 2V – and 3.3V for A2~A5.

Although there is an AREF pin on the board, this function isn’t supported at the time of writing. From the software perspective A0 and A1’s values have a 6-bit resolution and can fall between 0 and 63 (0~2V), otherwise the others have a 12-bit resolution and thus return values between 0 and 4095 (0~3.3V). Using the ADC pins is simple, and demonstrated in the following sketch:

// pcDuino v2 ADC demonstration

#include <core.h> // for pcDuino

int a0, a1, a2, a3, a4, a5;

void setup() 
{
}

void loop() 
{
  // read all the ADCs
  a0 = analogRead(0);
  a1 = analogRead(1);
  a2 = analogRead(2);
  a3 = analogRead(3);
  a4 = analogRead(4);
  a5 = analogRead(5);
  // display ADC values to console
  printf(A0, A1,   A2,   A3,   A4,   A5\n);
  printf(%d  %d  %d  %d  %d  %d\n, a0, a1, a2, a3, a4, a5);
  printf(n);
  delay(1000);
}

… which results with the following in the console:

Digital outputs

The pcDuino v2’s implementation of digital outputs aren’t anything out of the ordinary – except that you are limited to a maximum voltage of 3.3V instead of the usual 5V. Furthermore you can only source 4mA from each pin. However if you have some 5V-only shields that you must use with your pcDuino v2 – there is a Voltage Translation board that can be used to solve the problem:

However using 3.3V for new designs shouldn’t be an issue – new sensors, ICs and so on should be 3.3V-compatible. And with the pcDuino v2 you get an extra four digital I/O pins, located next to the SPI grouping as shown below:

These are simply addressed as D14~D17. Now back for a quick demonstration with the typical LEDs. As the current sourced from each GPIO pin cannot exceed 4mA, you need to use a resistor to keep things under control. Using the LED wizard, by entering a 3.3V supply, 2.1V forward voltage for our LEDs and a 4mA current – the resistor value to use is 330Ω.

If you’re having a lazy attack and use 560Ω, the current will be around 2.5mA with acceptable results. We’ve done just that with the following demonstration sketch:

// pcDuino v2 digital output demonstration

#include <core.h> // for pcDuino

void setup() 
{
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);  
  digitalWrite(4, LOW);  
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);  
}

void loop() 
{
  for (int i = 4; i < 8; i++)
  {
    digitalWrite(i, HIGH);
    delay(250);
    digitalWrite(i, LOW);
  }
}

… and the results in this video.

Digital inputs

When using the digital pins as inputs, just treat them as normal except they have a maximum input voltage of 3.3V for HIGH. Again – just keep thinking “3.3V”.

Using the I2C data bus

The I2C bus (or “two wire interface”) is a common serial data bus used for interfacing all manner of devices with a microcontroller. You can find a background on the I2C bus and Arduino tutorial here. Just like an Arduino Uno R3, the I2C bus pins are both A4 and A5 (for SCL and SDA) and can also be found up near D13, for example.

The limitations for the pcDuino v2’s version of I2C bus are few – the maximum speed is 200 kHz, it only uses 7-bit addresses and you can’t use the pcDuino in slave mode. However there are 2.2kΩ pullup resistors which can save using them with external circuitry.

We demonstrate the I2C bus by writing data to and reading it from a Microchip 24LC256 EEPROM (which is handy in itself as there isn’t any EEPROM function on the pcDuino v2). This is demonstrated with an Arduino Uno in part two of our I2C tutorials.

Connection is very easy – pins 1 to 4 of the EEPROM are connected to GND, pin 5 to SDA, pin 6 to SCL, pin 7 to GND and pin 8 to 3.3V. Finally a 0.1uF capacitor is placed across 3.3V and GND.

The sketch to read and write values to the EEPROM is simple, and apart from the #include <core.h> for the pcDuino all the other functions operate as normal.

// pcDuino I2C demonstration

#include <core.h> // for pcDuino
#include <Wire.h>   for I2C
#define chip1 0x50  device bus address for EEPROM

// always have your values in variables
unsigned int pointer = 69;  // we need this to be unsigned, as you may have an address  32767
byte d=0;  // example variable to handle data going in and out of EERPROMS

void setup()
{
  Wire.begin();  // wake up, I2C!
}

void writeData(int device, unsigned int add, byte data) 
// writes a byte of data 'data' to the chip at I2C address 'device', in memory location 'add'
{
  Wire.beginTransmission(device);
  Wire.write((int)(add  8)); // left-part of pointer address
  Wire.write((int)(add & 0xFF)); // and the right
  Wire.write(data);
  Wire.endTransmission();
  delay(10);
}

byte readData(int device, unsigned int add) 
// reads a byte of data from memory location 'add' in chip at I2C address 'device' 
{
  byte result;  // returned value
  Wire.beginTransmission(device);  // these three lines set the pointer position in the EEPROM
  Wire.write((int)(add  8));  // left-part of pointer address
  Wire.write((int)(add & 0xFF)); // and the right
  Wire.endTransmission();
  Wire.requestFrom(device,1); // now get the byte of data...
  result = Wire.read();
  return result;  // and return it as a result of the function readData
}

void loop()
{
  printf(Writing data...\n);
  for (int a=0; a10; a++)
  {
    writeData(chip1,a,a);
  }
  printf(Reading data...\n);
  for (int a=0; a10; a++)
  {
    d=readData(chip1,a);    
    printf(Pointer %d holds %d.\n,a,d);
  }
}

… which results with the following output in the console:

As you now know, using I2C isn’t hard at all. A lot of beginners shy away from it – or run screaming for the nearest library for their part. You don’t need libraries – spend a little time now learning about I2C and you’re set for life.

Using the SPI data bus

Again we have some SPI tutorials for Arduino, so check them out first if the concept is new to you. Writing to an SPI device with the pcDuino v2 isn’t tricky at all, you have the 3.3V hardware limitation and the SPI pins are in the same location (D10~D13) or in a separate group on the board:

Furthermore the maximum SPI speed is 12 MHz and the pcDuino v2’s  implementation of SPI can only work as a master. However in the sketch there are a few differences to note. To demonstrate this we’ll control a Microchip MCP4162 digital rheostat via SPI to control the brightness of an LED. Here is the circuit:

And now for the sketch. Take note of the fourth line in void setup() –  this is used to set the SPI bus speed to 12 MHz. You can also reduce the speed with other multipliers such as 32, 64 and 128 to slow it down even further. The other point to note is the use of SPI.transfer(). With the pcDuino v2 there are two parameters – the first is the data to send to the SPI device, and the second is either

SPI_CONTINUE

if there is another byte of data following immediately, or

SPI_LAST

if that is the last byte for that immediate transfer. You can see this use of the paramters within the function setValue() in the demonstration sketch below.

// pcDuino SPI demonstration

#include <core.h>  // for pcDuino
#include <SPI.h>
int ss = 10;
int del = 1000;

void setup()
{
  SPI.begin();
  SPI.setDataMode(SPI_MODE3);
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV16);
  pinMode(ss, OUTPUT);
  digitalWrite(ss, HIGH);
}

void setValue(int value)
{
  digitalWrite(ss, LOW);
  SPI.transfer(0, SPI_CONTINUE);
  SPI.transfer(value, SPI_LAST);
  digitalWrite(ss, HIGH);
}

void loop()
{
  setValue(255);
  delay(del);
  setValue(223);
  delay(del);
  setValue(191);
  delay(del);
  setValue(159);
  delay(del);
  setValue(127);
  delay(del);
  setValue(95);
  delay(del);
  setValue(63);
  delay(del);
  setValue(31);
  delay(del);
  setValue(0);
  delay(del);
}

When using the SPI bus, relevant data will appear in the console, for example:

And finally the demonstration video to show you it really works – you can see the output voltage from the rheostat and the matching LED brightness.

Receiving data from the SPI bus is equally as simple, however at the time of writing we don’t have an SPI device to demonstrate this, so please refer the SPI part of the pcDuino guide. Finally, you can’t use PWM on D10 or D11 when using the SPI bus in your sketch.

Pulse-width modulation

You can simulate analogue output using PWM with a pcDuino v2 – however there are two types of PWM pins available. The first is found on digital pins D3, D9, D10 and D11 – they are simulated PWM – and have a low range of zero to twenty at 5 Hz. There are two hardware PWM pins – D5 and D6, which  run at 520Hz and have the full range of 0~255 available in analogWrite(). Once again – they output 3.3V. Furthermore, you can’t use pinMode() functions or the SPI bus if using D10 and/or D11 for PWM.

Conclusion

Now you should have an understanding of the features and limitations of using GPIO pins with your pcDuino v2 Arduino sketches. And finally a plug for my own store – tronixlabs.com – offering a growing range and Australia’s best value for supported hobbyist electronics from adafruit, DFRobot, Freetronics, Seeed Studio and much more.

Have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column, or join our forum – dedicated to the projects and related items on this website.

The post Tutorial – pcDuino GPIO with Arduino IDE appeared first on tronixstuff.

Tronixstuff 29 Jan 04:12
adc  arduino  gpio  i2c  input  output  pcduino  pwm  review  spi  tronixlabs  tronixstuff  tutorial  

A custom Pi shield or an Arduino?

As more and more people get a Pi they are asking how to interface it to their robot. I do not own a Pi but I looked at the GPIO pins available for interfacing. Apart from general digital I/O pins you have I2C, SPI and Serial interfacing available. I assume there is a library or something that allows these pins to be easily access from within the Linux operating system.

So the question becomes do you just use another MCU such as an Arduino to provide the necessary I/O functionality or do you use a custom shield?

read more

Let's Make Robots 04 Oct 13:38
arduino  i2c  ideas  interface  pi  raspberry  serial  shield  spi  

TicTocTrac: track your perception of time

Brian Schiffer and Sima Mitra, from Cornell University, propose a very nice wristwatch that allows you to keep track of your time perception, using a method known as duration production: TicTocTrac.

Human perception of time is typically distorted, due to the different amount of information and experiences acquired everyday. TicTocTrac lets you to estimate your own perception, first by signaling the perceived duration of a given event and, then, by comparing it with the actual event duration. Finally, all the information can easily be saved to a micro SD card.

The hardware is based on a Atmega32u4, a DS3234S real-time clock and several leds to display time, while the software part is mostly based on Arduino’s DS3234S RTC library.

More information can be found here.

[Via: TicTocTrac]

NFR24L01 bluetooth module and SPI

Hi

 

I am working on my first robot. Ibased it on an ArduinoMega128 and can make it drive a pre-programmed pattern. I now want to control it via bluetooth from my android phone. For this I was given a small BT module yesterday. It is based on the NFR24L01 chip, and contains no serial interface, but rather the default SPI that the chip communicates with. I know the Arduino has an SPI interface on leg 50-53. However, away from connecting, I have no idea on how to begin.

 

I know I need to add the #include spi.h to my Arduino sketch.

read more