Added SPI Flash example
authorKevin Townsend <kevin@ktownsend.com>
Sun, 2 Oct 2011 18:03:01 +0000 (20:03 +0200)
committerKevin Townsend <kevin@ktownsend.com>
Sun, 2 Oct 2011 18:03:01 +0000 (20:03 +0200)
tools/examples/spiflash/erase_write_read/main.c [new file with mode: 0644]
tools/examples/spiflash/erase_write_read/readme.txt [new file with mode: 0644]

diff --git a/tools/examples/spiflash/erase_write_read/main.c b/tools/examples/spiflash/erase_write_read/main.c
new file mode 100644 (file)
index 0000000..76fb3c4
--- /dev/null
@@ -0,0 +1,160 @@
+/**************************************************************************/
+/*! 
+    @file     main.c
+    @author   K. Townsend (microBuilder.eu)
+
+    @section LICENSE
+
+    Software License Agreement (BSD License)
+
+    Copyright (c) 2011, microBuilder SARL
+    All rights reserved.
+
+    Redistribution and use in source and binary forms, with or without
+    modification, are permitted provided that the following conditions are met:
+    1. Redistributions of source code must retain the above copyright
+    notice, this list of conditions and the following disclaimer.
+    2. Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in the
+    documentation and/or other materials provided with the distribution.
+    3. Neither the name of the copyright holders nor the
+    names of its contributors may be used to endorse or promote products
+    derived from this software without specific prior written permission.
+
+    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
+    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
+    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+/**************************************************************************/
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "projectconfig.h"
+#include "sysinit.h"
+
+#include "core/gpio/gpio.h"
+#include "core/systick/systick.h"
+#include "drivers/spiflash/spiflash.h"
+
+#ifdef CFG_INTERFACE
+  #include "core/cmd/cmd.h"
+#endif
+
+/**************************************************************************/
+/*! 
+    Main program entry point.  After reset, normal code execution will
+    begin here.
+*/
+/**************************************************************************/
+int main(void)
+{
+  // Configure cpu and mandatory peripherals
+  systemInit();
+
+  spiflashError_e error;
+  uint8_t buffer[256];
+
+  // Wait a few seconds to let people connect to USB CDC to see the output
+  systickDelay(5000);
+
+  // Initialise SPI, etc.
+  printf("Initialising flash\r\n");
+  spiflashInit();
+
+  printf("Erasing sector 0 (page 0..15)\r\n");
+  error = spiflashEraseSector (0);
+  if (error)
+  {
+    // Check what went wrong
+    switch (error)
+    {
+      case SPIFLASH_ERROR_ADDROUTOFRANGE:
+        // Specified starting address is out of range
+        break;
+      case SPIFLASH_ERROR_TIMEOUT_READY:
+        // Timeout waiting for ready status (can be pre or post write)
+        break;
+      case SPIFLASH_ERROR_PROTECTIONERR:
+        // Flash is write protected
+        break;
+    }
+  }
+
+  printf("Writing 8 bytes to page 0 (bytes 0..7)\r\n");
+  buffer[0] = 0x12;
+  buffer[1] = 0x34;
+  buffer[2] = 0x56;
+  buffer[3] = 0x78;
+  buffer[4] = 0xAB;
+  buffer[5] = 0xCD;
+  buffer[6] = 0xEF;
+  buffer[7] = 0xAA;
+  error = spiflashWritePage (0, buffer, 8);
+  if (error)
+  {
+    // Check what went wrong
+    switch (error)
+    {
+      case SPIFLASH_ERROR_ADDROUTOFRANGE:
+        // Specified starting address is out of range
+        break;
+      case SPIFLASH_ERROR_DATAEXCEEDSPAGESIZE:
+        // Supplied data exceeds max page size
+        break;
+      case SPIFLASH_ERROR_PAGEWRITEOVERFLOW:
+        // The data length plus the start address offset exceeeds page limits
+        break;
+      case SPIFLASH_ERROR_TIMEOUT_READY:
+        // Timeout waiting for ready status (can be pre or post write)
+        break;
+      case SPIFLASH_ERROR_PROTECTIONERR:
+        // Unable to set write latch
+        break;
+    }
+  }
+
+  printf("Reading 16 bytes from page 1 (bytes 0..15)\r\n");
+  // Clear buffer just to prove the point
+  buffer[0] = 0;
+  buffer[1] = 0;
+  buffer[2] = 0;
+  buffer[3] = 0;
+  buffer[4] = 0;
+  buffer[5] = 0;
+  buffer[6] = 0;
+  buffer[7] = 0;
+  error = spiflashReadBuffer (0, buffer, 16);
+  if (error)
+  {
+    // Check what went wrong
+    switch (error)
+    {
+      case SPIFLASH_ERROR_ADDROUTOFRANGE:
+        // Specified starting address is out of range
+        break;
+      case SPIFLASH_ERROR_TIMEOUT_READY:
+        // Timed out waiting for flash to return ready state
+        break;
+      case SPIFLASH_ERROR_ADDROVERFLOW:
+        // Ran over the upper address during read
+        break;
+    }
+  }
+
+  // Display read results
+  printf("0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X %s", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7], buffer[8], buffer[9], buffer[10], buffer[11], buffer[12], buffer[13], buffer[14], buffer[15], CFG_PRINTF_NEWLINE);
+
+  while(1)
+  {
+  }
+
+  return 0;
+}
diff --git a/tools/examples/spiflash/erase_write_read/readme.txt b/tools/examples/spiflash/erase_write_read/readme.txt
new file mode 100644 (file)
index 0000000..0d4ed3b
--- /dev/null
@@ -0,0 +1,17 @@
+OVERVIEW
+============================================================
+This example will erae one 4KB page of SPI Flash, write
+8 bytes worth of data, and then read back 16 bytes of
+data to show how all three operations work.
+
+HOW TO USE THIS EXAMPLE
+============================================================
+1.) Connect the SPI Flash to SPI on the LPC1343
+
+2.) Configure your terminal software to open the USB COM
+    port at 115K.
+
+3.) When the application starts, there is a 5 second delay
+    (to allow you time to connect via USB CDC), after which 
+    point the device will run through the erase, write, read
+    sequence once and then wait in an infinite loop.
This page took 0.032012 seconds and 4 git commands to generate.