Skip to content

Instantly share code, notes, and snippets.

@kopanitsa
Created June 10, 2014 12:46
Show Gist options
  • Save kopanitsa/29386b5600634fb9b902 to your computer and use it in GitHub Desktop.
Save kopanitsa/29386b5600634fb9b902 to your computer and use it in GitHub Desktop.
Itead SPI sample
/*
* (C) Copyright 2014
* okada.takahiro111@gmail.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* Sample to read/write "mbed Geta SPI PRAM 128Mbit" from Iteaduino A20.
* reference URL:
* http://www.galileo-7.com/?pid=53257954
* http://mbed.org/users/okini3939/code/SPIRAM_NP8P128A13TSM60E/file/3a2094fa7c4c/main.cpp
*
*
* PIN ASSIGNMENT
* [SRAM] [ITEAD]
* GND ---- GND
* 40 ---- 3V3
* 11 ---- 112 (SPI0 MOSI)
* 12 ---- 113 (SPI0 MISO)
* 13 ---- 110 (SPI SCK)
* 14 ---- 109 (SPI CS)
* 15 ---- HIGH (3V3)
* 16 ---- HIGH (3V3)
*
*/
#include <itead.h>
#include <string.h>
#define CMD_WREN 0x06 // Write enable
#define CMD_READ 0x03 // Read data bytes
#define CMD_PP_BA 0x22 // Page program (bit-alterable write)
#define CMD_RDID 0x9f
const int cs = 109;
int SPIWrite(int addr, char *buf, int len) {
int i;
digitalWrite(cs, LOW);
SPItransfer(0, CMD_WREN);
digitalWrite(cs, HIGH);
delay(10);
digitalWrite(cs, LOW);
SPItransfer(0, CMD_PP_BA);
SPItransfer(0, (addr >> 16) & 0xff);
SPItransfer(0, (addr >> 8) & 0xff);
SPItransfer(0, addr & 0xff);
len = len - (addr & 0x3f);
for (i = 0; i < len; i ++) {
SPItransfer(0,buf[i]);
}
digitalWrite(cs, HIGH);
return i;
}
int SPIRead(int addr, char *buf, int len) {
int i;
digitalWrite(cs, LOW);
SPItransfer(0, CMD_READ);
SPItransfer(0, (addr >> 16) & 0xff);
SPItransfer(0, (addr >> 8) & 0xff);
SPItransfer(0, addr & 0xff);
for (i = 0; i < len; i ++) {
buf[i] = SPItransfer(0, 0);
}
digitalWrite(cs, HIGH);
return i;
}
int main(){
int i;
// initialize
pinMode(cs, OUTPUT);
digitalWrite(cs, HIGH);
SPIbegin(0);
delay(50);
//test to write
char buf[6];
strcpy(buf, "abcdef");
SPIWrite(0, buf, 6);
//test to read
delay(1000);
memset(buf, 0, 6);
SPIRead(0, buf, 6);
for (i = 0; i < 6; i ++) {
printf(" %02x", buf[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment