Program an AVR microcontroller using mbed.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
aberk
Date:
Thu Sep 09 11:11:37 2010 +0000
Parent:
1:276f6df4be7a
Commit message:
Improved support and documentation for non-paged memory devices.

Changed in this revision

AVR910.cpp Show annotated file Show diff for this revision Revisions of this file
AVR910.h Show annotated file Show diff for this revision Revisions of this file
--- a/AVR910.cpp	Wed Sep 01 10:22:51 2010 +0000
+++ b/AVR910.cpp	Thu Sep 09 11:11:37 2010 +0000
@@ -91,6 +91,7 @@
 
     char pageOffset = 0;
     int  pageNumber = 0;
+    int  address    = 0;
     int  c          = 0;
     int  highLow    = 0;
 
@@ -132,16 +133,19 @@
 
             //Write low byte.
             if (highLow == 0) {
-                loadMemoryPage(WRITE_LOW_BYTE, pageOffset, c);
+                writeFlashMemoryByte(WRITE_LOW_FLASH_BYTE, address, c);
                 highLow = 1;
             }
             //Write high byte.
             else {
-                loadMemoryPage(WRITE_HIGH_BYTE, pageOffset, c);
+                writeFlashMemoryByte(WRITE_HIGH_FLASH_BYTE, address, c);
                 highLow = 0;
-                pageOffset++;
+                address++;
 
-                if (pageOffset > pageSize) {
+                //Page size is our memory size in the non-paged memory case.
+                //Therefore if we've gone beyond our size break because we
+                //don't have any more room.
+                if (address > pageSize) {
                     break;
                 }
 
@@ -280,6 +284,15 @@
 
 }
 
+void AVR910::writeFlashMemoryByte(int highLow, int address, char data) {
+
+    spi_.write(highLow);
+    spi_.write(address & 0xFF00 >> 8);
+    spi_.write(address & 0x00FF);
+    spi_.write(data);
+
+}
+
 void AVR910::writeFlashMemoryPage(char pageNumber) {
 
     spi_.write(0x4C);
--- a/AVR910.h	Wed Sep 01 10:22:51 2010 +0000
+++ b/AVR910.h	Thu Sep 09 11:11:37 2010 +0000
@@ -48,12 +48,14 @@
 #define PATH_TO_BINARY "/local/AVRCODE.bin"
 
 //Commands
-#define ATMEL_VENDOR_CODE 0x1E
-#define DEVICE_LOCKED     0x00
-#define WRITE_HIGH_BYTE   0x48
-#define WRITE_LOW_BYTE    0x40
-#define READ_HIGH_BYTE    0x28
-#define READ_LOW_BYTE     0x20
+#define ATMEL_VENDOR_CODE     0x1E
+#define DEVICE_LOCKED         0x00
+#define WRITE_HIGH_BYTE       0x48
+#define WRITE_LOW_BYTE        0x40
+#define READ_HIGH_BYTE        0x28
+#define READ_LOW_BYTE         0x20
+#define WRITE_HIGH_FLASH_BYTE 0x68
+#define WRITE_LOW_FLASH_BYTE  0x60
 
 #define PAGE_SIZE         ATMEGA328P_PAGESIZE
 #define NUM_PAGES         ATMEGA328P_NUM_PAGES
@@ -91,9 +93,9 @@
      *
      * @param binary File pointer to the binary file to be loaded onto the
      *               AVR microcontroller.
-     * @param pageSize The size of one page on the device. If the device does
-     *                 not use paged memory, set this as the size of memory of
-     *                 the device.
+     * @param pageSize The size of one page on the device in words. If the
+     *                 device does not use paged memory, set this as the size
+     *                 of memory of the device in words.
      * @param numPages The number of pages on the device. If the device does
      *                 not use paged memory, set this to 1 (default).
      *
@@ -110,7 +112,7 @@
      * @param frequency Frequency of the SPI communication in hertz.
      */
     void setFrequency(int frequency);
-    
+
     /**
      * Read the vendor code of the device.
      *
@@ -161,13 +163,23 @@
     /**
      * Load a byte into the memory page buffer.
      *
-     * @param highLow Indicate whether the byte being loaded is a high or low 
+     * @param highLow Indicate whether the byte being loaded is a high or low
      *                byte.
      * @param data The data byte to load.
      */
     void loadMemoryPage(int highLow, char address, char data);
 
     /**
+     * Write a byte into the flash memory.
+     *
+     * @param highLow Indicate whether the byte being loaded is a high or low
+     *                byte.
+     * @param address The address to load the byte at.
+     * @param data The data byte to load.
+     */
+    void writeFlashMemoryByte(int highLow, int address, char data);
+
+    /**
      * Write the memory page buffer to flash memory.
      *
      * @param pageNumber The page number to write to in flash memory.
@@ -190,15 +202,15 @@
      *
      * @param numPages The number of pages written to the AVR microcontroller.
      * @param binary File pointer to the binary used.
-     * 
+     *
      * @return  0 -> No inconsistencies between binary file and AVR flash memory.
      *         -1 -> Binary file was not written correctly.
      */
     int checkMemory(int numPages, FILE* binary);
-    
+
     SPI        spi_;
     DigitalOut nReset_;
-    
+
 };
 
 #endif /* AVR910_H */