Fix to have load pin working with SPI1.

Dependencies:   mbed

Fork of Max7221 by Dwayne Dilbeck

Files at this revision

API Documentation at this revision

Comitter:
ky3orr
Date:
Thu Nov 19 23:58:07 2015 +0000
Parent:
5:1f3dbf38d027
Child:
7:a160cb7cdd86
Commit message:
1st refactor to have library working with single SPI only.

Changed in this revision

Max7221.cpp Show annotated file Show diff for this revision Revisions of this file
Max7221.h Show annotated file Show diff for this revision Revisions of this file
--- a/Max7221.cpp	Thu Nov 19 23:27:53 2015 +0000
+++ b/Max7221.cpp	Thu Nov 19 23:58:07 2015 +0000
@@ -11,14 +11,11 @@
 
 ///Intialize the Class static members
 ///Devices used per SPI bus are set to ZERO
-int Max7221::maxInUseSPI1 = 0;
-int Max7221::maxInUseSPI2 = 0;
+int Max7221::maxInUseSPI = 0;
 ///Set the static pointers to the SPI busses to NULL
-SPI *Max7221::spi1=NULL;
-SPI *Max7221::spi2=NULL;
+SPI *Max7221::spi=NULL;
 ///Set the static pointers to the load signals to NULL
-DigitalOut *Max7221::load1=NULL;
-DigitalOut *Max7221::load2=NULL;
+DigitalOut *Max7221::cs=NULL;
 
 /**
 *  Constructor. This is the default constructor
@@ -30,42 +27,16 @@
 *  @date 8/6/2013
 */
 Max7221::Max7221(PinName msoi, PinName mclk, PinName load){
-   ///Determine which bus we are connecting the device to based on the MSOI pin name
-   switch (msoi) {
-     ///If using SPI bus #1
-     case PTC6: maxInUseSPI1++; 
-              ///Set this insctance to pointers to the correct static pointers
-              this->id=maxInUseSPI1;
-              this->maxInUse=&maxInUseSPI1;
-              ///If no devices have been assigned to the SPI bus it must be initialized
-              if (spi1 ==NULL) {
-                 spi1 = new SPI(msoi, NC, mclk);
-                 load1 = new DigitalOut(load);
-              } else {
-                ///TODO: Check that load pin is the same for all SP2 
-              }
-              this->max72_spi=spi1;
-              this->load = load1;
-              break;
-     ///If using SPI bus #2
-     case PTD7: maxInUseSPI2++;  
-              ///Set this insctance to pointers to the correct static pointers
-              this->id=maxInUseSPI2;
-              this->maxInUse=&maxInUseSPI2;
-              ///If no devices have been assigned to the SPI bus it must be initialized
-              if (spi2 ==NULL) {
-                 spi2 = new SPI(msoi, NC, mclk);
-                 load2 = new DigitalOut(load);
-              } else {
-                ///TODO: Check that load pin is the same for all SP2 
-              }
-              this->max72_spi=spi2;
-              this->load = load2;
-              break; 
-     ///If a pin not belonging to a SPI bus is used, Throw error
-     default: error("Not a SPI port");
-   }
-   
+  ///Set this insctance to pointers to the correct static pointers
+  this->id=maxInUseSPI;
+  this->maxInUse=&maxInUseSPI;
+  ///If no devices have been assigned to the SPI bus it must be initialized
+  if (spi ==NULL) {
+     spi = new SPI(msoi, NC, mclk);
+     cs = new DigitalOut(load);
+  }
+  this->max7221_spi= spi;
+  this->max7221_cs = cs;
 }
 
 /**
@@ -83,23 +54,23 @@
 */
 void Max7221::Write( unsigned int reg, unsigned int data) {
     int c = 0;
-    *load = LOW;
+    *cs = LOW;
 
     ///if there are multiple devices sharing the SPI buss and they preceed the current device in the cascade Write a NOOP to them
     for ( c = *maxInUse; c > this->id; c--) {
-        max72_spi->write(0);  // no-op
-        max72_spi->write(0);  // no-op
+        max7221_spi->write(0);  // no-op
+        max7221_spi->write(0);  // no-op
     }
     ///Write to this device registers
-    max72_spi->write(reg);  // specify register
-    max72_spi->write(data);  // put data
+    max7221_spi->write(reg);  // specify register
+    max7221_spi->write(data);  // put data
 
     ///if there are multiple devices sharing the SPI buss and they follow the current device in the cascade Write a NOOP to them
     for ( c=this->id-1; c >= 1; c--) {
-        max72_spi->write(0);  // no-op
-        max72_spi->write(0);  // no-op
+        max7221_spi->write(0);  // no-op
+        max7221_spi->write(0);  // no-op
     }
-    *load = HIGH;
+    *cs = HIGH;
 }
 
 /**
@@ -110,10 +81,10 @@
 void Max7221::Setup () {
     // initiation of the max 7221
     // SPI setup: 8 bits, mode 0
-    max72_spi->format(8, 0);
+    max7221_spi->format(8, 0);
     
     // going by the datasheet, min clk is 100ns so theoretically 10MHz should work...
-    max72_spi->frequency(10*MHZ);
+    max7221_spi->frequency(10*MHZ);
     
     Write(max7219_reg_scanLimit, 0x07);   ///ENABLE all 8 digits
     Write(max7219_reg_decodeMode, 0xff);  // Turn on Code B font decode for all digits
@@ -135,22 +106,13 @@
 */   
 void Max7221::WriteAll (unsigned int reg, unsigned int data) {   
     ///Write to all the devices on SPI Bus #1 first
-    if(load1 !=NULL) {
-        *load1 = LOW;                    // begin
-        for ( int c=1; c<= maxInUseSPI1; c++) {
-            spi1->write(reg);  // specify register
-            spi1->write(data);  // put data
+    if(cs !=NULL) {
+        *cs = LOW;                    // begin
+        for ( int c=1; c<= maxInUseSPI; c++) {
+            spi->write(reg);  // specify register
+            spi->write(data);  // put data
         }
-        *load1 = HIGH;
-    }
-    ///Write to all the devices on SPI Bus #2
-    if(load2 !=NULL) {
-        *load2 = LOW;
-        for ( int c=1; c<= maxInUseSPI2; c++) {
-            spi2->write(reg);  // specify register
-            spi2->write(data);  // put data
-        }
-        *load2 = HIGH;
+        *cs = HIGH;
     }
 }
 /**
@@ -251,14 +213,10 @@
 void Max7221::SetupAll () {
     // initiation of the max 7219
     // SPI setup: 8 bits, mode 0
-    if(spi1!=NULL) {
-       spi1->format(8, 0);
-       spi1->frequency(10*MHZ);    
-    }        
-    if(spi2!=NULL) {
-       spi2->format(8, 0);
-       spi2->frequency(10*MHZ);    
-    }        
+    if(spi!=NULL) {
+       spi->format(8, 0);
+       spi->frequency(10*MHZ);    
+    }               
     WriteAll(max7219_reg_scanLimit, 0x07);
     WriteAll(max7219_reg_decodeMode, 0xff);  // using an led matrix (not digits)
     WriteAll(max7219_reg_shutdown, 0x01);    // not in shutdown mode
--- a/Max7221.h	Thu Nov 19 23:27:53 2015 +0000
+++ b/Max7221.h	Thu Nov 19 23:58:07 2015 +0000
@@ -159,21 +159,21 @@
    
 private:
    /// Pointer for the class instance to a particular SPI bus
-   SPI *max72_spi;
+   SPI *max7221_spi;
    /// pointer for the class instance of a particular load signal
-   DigitalOut *load;
+   DigitalOut *max7221_cs;
    ///  id of the class instance
    int id;
    /// pointer to the number of devices connected to the SPI bus instance this device instance is connected to.
    int *maxInUse;
    
    ///For the class we have a static set of values.  There are 2 SPI buses, 2 load signals, and 2 counters of the number of devices connected to a SPI bus.
-   static SPI *spi1;
-   static SPI *spi2;
-   static DigitalOut *load1;
-   static DigitalOut *load2;
-   static int maxInUseSPI1;
-   static int maxInUseSPI2;
+   static SPI *spi;
+//   static SPI *spi2;
+   static DigitalOut *cs;
+//   static DigitalOut *load2;
+   static int maxInUseSPI;
+//   static int maxInUseSPI2;
 };
 
 #endif
\ No newline at end of file