Commits

F Malpartida committed b82429e

Added speed improvements and alignment with PICKit.

Comments (0)

Files changed (4)

 //
 // @author Florian Fida -
 //
+// 2012-03-16 bperrybap updated fio_shiftout() to be smaller & faster
+//
 // @todo:
 //  support chipkit:
 // (https://github.com/chipKIT32/chipKIT32-MAX/blob/master/hardware/pic32/
 // ---------------------------------------------------------------------------
 #include "FastIO.h"
 
+
 fio_register fio_pinToOutputRegister(uint8_t pin, uint8_t initial_state)
 {
 	pinMode(pin, OUTPUT);
 #endif
 }
 
+
 fio_register fio_pinToInputRegister(uint8_t pin)
 {
 	pinMode(pin, INPUT);
 #endif
 }
 
-uint8_t fio_pinToBit(uint8_t pin)
+
+fio_bit fio_pinToBit(uint8_t pin)
 {
 #ifdef FIO_FALLBACK
 	// (ab)use the bit variable to store the pin
 #endif
 }
 
-void fio_digitalWrite(fio_register pinRegister, uint8_t pinBit, uint8_t value) 
+
+void fio_digitalWrite(fio_register pinRegister, fio_bit pinBit, uint8_t value) 
 {
 #ifdef FIO_FALLBACK
 	digitalWrite(pinBit, value);
 	// # disable interrupts
 	int8_t i;
    
-   
-   for(i = 0; i < 8; i++)
-   {
-      if (bitOrder == LSBFIRST)
-      {
-         fio_digitalWrite(dataRegister, dataBit, !!(value & (1 << i)));
-      }
-      else
-      {
-         fio_digitalWrite(dataRegister, dataBit, !!(value & (1 << (7 - i))));
-      }
-      ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
-      {
-         fio_digitalWrite_HIGH (clockRegister, clockBit);
-         
-         // Switching is a little bit faster
-         fio_digitalWrite_SWITCH (clockRegister,clockBit);
-      } // end critical section
+	if(bitOrder == LSBFIRST)
+	{
+		for(i = 0; i < 8; i++)
+		{
+			ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
+			{
+				if(value & 1)
+            {
+               fio_digitalWrite_HIGH(dataRegister, dataBit);
+				}
+            else
+            {
+               fio_digitalWrite_LOW(dataRegister, dataBit);
+            }
+            value >>= 1;
+				fio_digitalWrite_HIGH (clockRegister, clockBit);
+				fio_digitalWrite_LOW (clockRegister,clockBit);
+			}
+		}
       
-   } 
+	}
+	else
+	{
+		for(i = 0; i < 8; i++)
+		{
+			ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
+			{
+				if(value & 0x80)
+            {
+               fio_digitalWrite_HIGH(dataRegister, dataBit);
+				}
+            else
+            {
+               fio_digitalWrite_LOW(dataRegister, dataBit);
+            }
+				value <<= 1;
+				fio_digitalWrite_HIGH (clockRegister, clockBit);
+				fio_digitalWrite_LOW (clockRegister,clockBit);
+			}
+		}
+	}
 }
 
-void fio_shiftOut(fio_register dataRegister, uint8_t dataBit, 
-                  fio_register clockRegister, uint8_t clockBit)
+
+void fio_shiftOut(fio_register dataRegister, fio_bit dataBit, 
+                  fio_register clockRegister, fio_bit clockBit)
 {
    ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
    {
       }
    }
 }
+
+
 void fio_shiftOut1_init(uint8_t pin)
 {
 	fio_shiftOut1_init(fio_pinToOutputRegister(pin,HIGH),fio_pinToBit(pin));
 	fio_digitalWrite(shift1Register,shift1Bit,HIGH);
 	delayMicroseconds(300);
 }
+
+
 void fio_shiftOut1(fio_register shift1Register, fio_bit shift1Bit, uint8_t value, 
                    boolean noLatch)
 {
 // @version API 1.0.0
 //
 // @author Florian Fida -
-//
-// @todo:
+// 2012-03-16 bperrybap mods for chipkit32 (pic32) Arduino
 //  support chipkit:
 // (https://github.com/chipKIT32/chipKIT32-MAX/blob/master/hardware/pic32/
 //   cores/pic32/wiring_digital.c)
 #include <pins_arduino.h> // pleasing sanguino core
 #include <inttypes.h>
 
-#ifdef __AVR__
+
+#define SKIP 0x23
+
+#if defined (__AVR__)
 #include <util/atomic.h> // for critical section management
-#endif
+typedef uint8_t fio_bit;
+typedef volatile uint8_t *fio_register;
 
 
-/*!
- @defined 
- @abstract   Enables IO digitalRead/digitalWrite fall back for non-AVR
-             architectures.
- */
-#ifndef __AVR__
+#elif defined(__PIC32MX__)
+typedef uint32_t fio_bit;
+typedef volatile uint32_t *fio_register;
+
+
+#else
+// fallback to Arduino standard digital i/o routines
 #define FIO_FALLBACK
 #define ATOMIC_BLOCK(dummy) if(true)
 #define ATOMIC_RESTORESTATE
+typedef uint8_t fio_bit;
+typedef uint8_t fio_register;
 #endif
 
-// PUBLIC CONSTANTS DEFINITIONS
-// ---------------------------------------------------------------------------
-/*!
- @defined 
- @abstract   Skip setting IO outputs during IO configuration or when getting
-             the output register associated to an IO pin.k
+
+
+#if !defined(FIO_FALLBACK) && !defined(ATOMIC_BLOCK)
+/*
+ * Define an ATOMIC_BLOCK that implements ATOMIC_FORCEON type
+ * Using the portable Arduino interrupts() and noInterrupts()
  */
-#define SKIP 0x23
+#define ATOMIC_RESTORESTATE ATOMIC_FORCEON // sorry, no support for save/restore yet.
+#define ATOMIC_FORCEON uint8_t sreg_save \
+              __attribute__((__cleanup__(__iSeiParam))) = 0
 
-// PUBLIC TYPE DEFINITIONS
-// ---------------------------------------------------------------------------
-typedef uint8_t fio_bit;
+static __inline__ uint8_t __iCliRetVal(void)
+{
+	noInterrupts();
+	return(1);
+}
+static __inline__ void __iSeiParam(const uint8_t *__s)
+{
+	interrupts();
+}
+#define ATOMIC_BLOCK(type) for(type,  __Todo = __iCliRetVal(); __Todo; __Todo = 0)
 
-/*!
-    @typedef 
-    @abstract   Type definitions for FAST_IOs o NATIVE IDE IO
-    @field      fio_register: IO control register
-*/
+#endif // end of block to create compatible ATOMIC_BLOCK()
 
-#ifndef FIO_FALLBACK
-typedef volatile uint8_t *fio_register;
-#else
-// remove volatile to give optimizer a chance
-typedef uint8_t fio_register;
-#endif
+
 
 /*!
  @function
  @param bitOrder[in] bit order
  */
 void fio_shiftOut( fio_register dataRegister, fio_bit dataBit, fio_register clockRegister, 
-                   fio_bit clockBit, uint8_t value, uint8_t bitOrder );
+                  fio_bit clockBit, uint8_t value, uint8_t bitOrder );
 
 /*!
  @method
     NOTE: The prefered methods to control the backlight are "backlight" and
     "noBacklight".
     
-    @param      0..255 - the value is very dependent on the LCD, however, 0
-    will be interpreted as off.
+    @param      0..255 - the value is very dependent on the LCD. However, 
+    BACKLIGHT_OFF will be interpreted as off and BACKLIGHT_ON will drive the
+    backlight on.
     */
    virtual void setBacklight ( uint8_t value ) { };
    
     This command shouldn't be used to drive the LCD, only to implement any other
     feature that is not available on this library.
     
-    @param      value[in] Command value to send to the LCD.
+    @param      value[in] Command value to send to the LCD (COMMAND, DATA or
+    FOUR_BITS).
     */
    void command(uint8_t value);
 
    // to read the Enable pulse, and then reads the new contents of the SR.
    ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
    {
-      fio_digitalWrite_HIGH(_srEnableRegister, _srEnableBit);            
+      fio_digitalWrite_HIGH(_srEnableRegister, _srEnableBit);
+      waitUsec (1);         // enable pulse must be >450ns               
       fio_digitalWrite_SWITCHTO(_srEnableRegister, _srEnableBit, LOW);
    } // end critical section
-   waitUsec ( 37 );      // commands need > 37us to settle
 }
 
 // PUBLIC METHODS