Program an AVR microcontroller using mbed.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
aberk
Date:
Wed Sep 01 10:22:51 2010 +0000
Parent:
0:3066745764a5
Child:
2:99c56829a2a8
Commit message:
Added support for non-paged memory AVRs and changed parameters for the program(...) method to include page size (memory size if non-paged memory) and number of pages (=1 if non-paged memory).

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
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/AVR910.cpp	Tue Aug 31 14:09:53 2010 +0000
+++ b/AVR910.cpp	Wed Sep 01 10:22:51 2010 +0000
@@ -60,7 +60,7 @@
 
     //Issue a programming enable command.
     response = enableProgramming();
-    
+
     //Simple debugging to see if we get trapped
     //in an infinite loop.
     DigitalOut working(LED1);
@@ -79,12 +79,12 @@
         response = enableProgramming();
 
     }
-    
+
     working = 0;
 
 }
 
-int AVR910::program(FILE* binary) {
+int AVR910::program(FILE* binary, int pageSize, int numPages) {
 
     //Clear memory contents.
     chipErase();
@@ -93,27 +93,60 @@
     int  pageNumber = 0;
     int  c          = 0;
     int  highLow    = 0;
-    
-    while ((c = getc(binary)) != EOF) {
+
+    //We're dealing with paged memory.
+    if (numPages > 1) {
+
+        while ((c = getc(binary)) != EOF) {
+
+            //Page is fully loaded, time to write it to flash.
+            if (pageOffset == (pageSize)) {
+                writeFlashMemoryPage(pageNumber);
 
-        //Page is fully loaded, time to write it to flash.
-        if (pageOffset == (PAGE_SIZE)) {
-            writeFlashMemoryPage(pageNumber);
+                pageNumber++;
+                if (pageNumber > numPages) {
+                    break;
+                }
+                pageOffset = 0;
+            }
 
-            pageNumber++;
-            pageOffset = 0;
+            //Write low byte.
+            if (highLow == 0) {
+                loadMemoryPage(WRITE_LOW_BYTE, pageOffset, c);
+                highLow = 1;
+            }
+            //Write high byte.
+            else {
+                loadMemoryPage(WRITE_HIGH_BYTE, pageOffset, c);
+                highLow = 0;
+                pageOffset++;
+            }
+
         }
 
-        //Write low byte.
-        if (highLow == 0) {
-            loadMemoryPage(WRITE_LOW_BYTE, pageOffset, c);
-            highLow = 1;
-        }
-        //Write high byte.
-        else {
-            loadMemoryPage(WRITE_HIGH_BYTE, pageOffset, c);
-            highLow = 0;
-            pageOffset++;
+    }
+    //We're dealing with non-paged memory.
+    else {
+
+        while ((c = getc(binary)) != EOF) {
+
+            //Write low byte.
+            if (highLow == 0) {
+                loadMemoryPage(WRITE_LOW_BYTE, pageOffset, c);
+                highLow = 1;
+            }
+            //Write high byte.
+            else {
+                loadMemoryPage(WRITE_HIGH_BYTE, pageOffset, c);
+                highLow = 0;
+                pageOffset++;
+
+                if (pageOffset > pageSize) {
+                    break;
+                }
+
+            }
+
         }
 
     }
--- a/AVR910.h	Tue Aug 31 14:09:53 2010 +0000
+++ b/AVR910.h	Wed Sep 01 10:22:51 2010 +0000
@@ -91,11 +91,16 @@
      *
      * @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 numPages The number of pages on the device. If the device does
+     *                 not use paged memory, set this to 1 (default).
      *
      * @return  0 => AVR microcontroller programmed successfully.
      *         -1 => Problem during programming.
      */
-    int program(FILE* binary);
+    int program(FILE* binary, int pageSize, int numPages = 1);
 
     /**
      * Set the frequency of the SPI communication.
--- a/main.cpp	Tue Aug 31 14:09:53 2010 +0000
+++ b/main.cpp	Wed Sep 01 10:22:51 2010 +0000
@@ -12,7 +12,7 @@
 
     int success  = -1;
     int response =  0;
-    
+
     //Read the vendor code [0x1E == Atmel].
     response = mAVRISP.readVendorCode();
 
@@ -25,7 +25,7 @@
         pc.printf("Microcontroller is not an Atmel\n");
         return -1;
     }
-    
+
     //Read part family and flash size - see datasheet for code meaning.
     response = mAVRISP.readPartFamilyAndFlashSize();
 
@@ -43,33 +43,32 @@
 
     if (response == 0xFF) {
         pc.printf("Device code erased or target missing\n");
-        return -1;
     } else if (response == 0x02) {
         pc.printf("Device locked\n");
         return -1;
     } else {
         pc.printf("Part number code is: 0x%02x\n", response);
     }
-    
+
     //Open binary file to write to AVR.
     FILE *fp = fopen(PATH_TO_BINARY, "rb");
-    
-    if(fp == NULL){
+
+    if (fp == NULL) {
         pc.printf("Failed to open binary. Please check the file path\n");
         return -1;
-    }
-    else{
+    } else {
         //Program it!
         pc.printf("Binary file opened successfully\n");
-        success = mAVRISP.program(fp);
+        success = mAVRISP.program(fp,
+                                  ATMEGA328P_PAGESIZE,
+                                  ATMEGA328P_NUM_PAGES);
         fclose(fp);
     }
-    
-    if(success < 0){
+
+    if (success < 0) {
         printf("Programming failed.\n");
-    }
-    else{
+    } else {
         printf("Programming was successful!\n");
     }
-    
+
 }