iforce2d Chris / Mbed 2 deprecated ubxDistanceMeter

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers u8g_com_arduino_sw_spi.c Source File

u8g_com_arduino_sw_spi.c

00001 /*
00002   
00003   u8g_arduino_sw_spi.c
00004 
00005   Universal 8bit Graphics Library
00006   
00007   Copyright (c) 2011, olikraus@gmail.com
00008   All rights reserved.
00009 
00010   Redistribution and use in source and binary forms, with or without modification, 
00011   are permitted provided that the following conditions are met:
00012 
00013   * Redistributions of source code must retain the above copyright notice, this list 
00014     of conditions and the following disclaimer.
00015     
00016   * Redistributions in binary form must reproduce the above copyright notice, this 
00017     list of conditions and the following disclaimer in the documentation and/or other 
00018     materials provided with the distribution.
00019 
00020   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
00021   CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
00022   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00023   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00024   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
00025   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
00026   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
00027   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
00028   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
00029   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
00030   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
00031   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00032   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
00033   
00034   Update for ATOMIC operation done (01 Jun 2013)
00035     U8G_ATOMIC_OR(ptr, val)
00036     U8G_ATOMIC_AND(ptr, val)
00037     U8G_ATOMIC_START();
00038     U8G_ATOMIC_END();
00039  
00040 
00041 */
00042 
00043 #include "u8g.h"
00044 
00045 #if defined(ARDUINO)
00046 
00047 #if ARDUINO < 100 
00048 #include <WProgram.h>    
00049 #include "wiring_private.h"
00050 #include "pins_arduino.h"
00051 
00052 #else 
00053 #include <Arduino.h> 
00054 #include "wiring_private.h"
00055 #endif
00056 
00057 /*=========================================================*/
00058 /* Arduino, AVR */
00059 
00060 #if defined(__AVR__)
00061 
00062 uint8_t u8g_bitData, u8g_bitNotData;
00063 uint8_t u8g_bitClock, u8g_bitNotClock;
00064 volatile uint8_t *u8g_outData;
00065 volatile uint8_t *u8g_outClock;
00066 
00067 static void u8g_com_arduino_init_shift_out(uint8_t dataPin, uint8_t clockPin)
00068 {
00069   u8g_outData = portOutputRegister(digitalPinToPort(dataPin));
00070   u8g_outClock = portOutputRegister(digitalPinToPort(clockPin));
00071   u8g_bitData = digitalPinToBitMask(dataPin);
00072   u8g_bitClock = digitalPinToBitMask(clockPin);
00073 
00074   u8g_bitNotClock = u8g_bitClock;
00075   u8g_bitNotClock ^= 0x0ff;
00076 
00077   u8g_bitNotData = u8g_bitData;
00078   u8g_bitNotData ^= 0x0ff;
00079 }
00080 
00081 static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val) U8G_NOINLINE;
00082 static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val)
00083 {
00084   uint8_t cnt = 8;
00085   uint8_t bitData = u8g_bitData;
00086   uint8_t bitNotData = u8g_bitNotData;
00087   uint8_t bitClock = u8g_bitClock;
00088   uint8_t bitNotClock = u8g_bitNotClock;
00089   volatile uint8_t *outData = u8g_outData;
00090   volatile uint8_t *outClock = u8g_outClock;
00091   U8G_ATOMIC_START();
00092   do
00093   {
00094     if ( val & 128 )
00095       *outData |= bitData;
00096     else
00097       *outData &= bitNotData;
00098    
00099     *outClock |= bitClock;
00100     val <<= 1;
00101     cnt--;
00102     *outClock &= bitNotClock;
00103   } while( cnt != 0 );
00104   U8G_ATOMIC_END();
00105 }
00106 
00107 /*=========================================================*/
00108 /* Arduino, Chipkit */
00109 #elif defined(__18CXX) || defined(__PIC32MX)
00110 
00111 uint16_t dog_bitData, dog_bitNotData;
00112 uint16_t dog_bitClock, dog_bitNotClock;
00113 volatile uint32_t *dog_outData;
00114 volatile uint32_t *dog_outClock;
00115 volatile uint32_t dog_pic32_spi_tmp;
00116 
00117 static void u8g_com_arduino_init_shift_out(uint8_t dataPin, uint8_t clockPin)
00118 {
00119   dog_outData = portOutputRegister(digitalPinToPort(dataPin));
00120   dog_outClock = portOutputRegister(digitalPinToPort(clockPin));
00121   dog_bitData = digitalPinToBitMask(dataPin);
00122   dog_bitClock = digitalPinToBitMask(clockPin);
00123 
00124   dog_bitNotClock = dog_bitClock;
00125   dog_bitNotClock ^= 0x0ffff;
00126 
00127   dog_bitNotData = dog_bitData;
00128   dog_bitNotData ^= 0x0ffff;
00129 }
00130 
00131 static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val)
00132 {
00133   uint8_t cnt = 8;
00134   U8G_ATOMIC_START();
00135   do
00136   {
00137     if ( val & 128 )
00138     *dog_outData |= dog_bitData;
00139     else
00140     *dog_outData &= dog_bitNotData;    
00141     val <<= 1;
00142     /*
00143     There must be some delay here. However
00144     fetching the adress dog_outClock is enough delay, so
00145     do not place dog_outClock in a local variable. This will
00146     break the procedure
00147     */
00148     *dog_outClock |= dog_bitClock;
00149     cnt--;
00150     *dog_outClock &= dog_bitNotClock;
00151     /* 
00152     little additional delay after clk pulse, done by 3x32bit reads 
00153     from I/O. Optimized for PIC32 with 80 MHz.
00154     */
00155     dog_pic32_spi_tmp = *dog_outClock;
00156     dog_pic32_spi_tmp = *dog_outClock;
00157     dog_pic32_spi_tmp = *dog_outClock;
00158   } while( cnt != 0 );
00159   U8G_ATOMIC_END();
00160 }
00161 
00162 /*=========================================================*/
00163 /* Arduino Due */
00164 #elif defined(__arm__)
00165 
00166 /* Due */
00167 
00168 void u8g_digital_write_sam_high(uint8_t pin)
00169 {
00170     PIO_Set( g_APinDescription[pin].pPort, g_APinDescription[pin].ulPin) ;
00171 }
00172 
00173 void u8g_digital_write_sam_low(uint8_t pin)
00174 {
00175     PIO_Clear( g_APinDescription[pin].pPort, g_APinDescription[pin].ulPin) ;
00176 }
00177 
00178 static uint8_t u8g_sam_data_pin;
00179 static uint8_t u8g_sam_clock_pin;
00180 
00181 static void u8g_com_arduino_init_shift_out(uint8_t dataPin, uint8_t clockPin)
00182 {
00183   u8g_sam_data_pin = dataPin;
00184   u8g_sam_clock_pin = clockPin;
00185 }
00186 
00187 static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val)
00188 {
00189   uint8_t i = 8;
00190   do
00191   {
00192     if ( val & 128 )
00193       u8g_digital_write_sam_high(u8g_sam_data_pin);
00194     else
00195       u8g_digital_write_sam_low(u8g_sam_data_pin);
00196     val <<= 1;
00197     //u8g_MicroDelay(); 
00198     u8g_digital_write_sam_high(u8g_sam_clock_pin);
00199     u8g_MicroDelay();   
00200     u8g_digital_write_sam_low(u8g_sam_clock_pin);
00201     u8g_MicroDelay();   
00202     i--;
00203   } while( i != 0 );
00204 }
00205 
00206 
00207 #else
00208 /* empty interface */
00209 
00210 static void u8g_com_arduino_init_shift_out(uint8_t dataPin, uint8_t clockPin)
00211 {
00212 }
00213 
00214 static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val)
00215 {
00216 }
00217 
00218 #endif 
00219 
00220 
00221 uint8_t u8g_com_arduino_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
00222 {
00223   switch(msg)
00224   {
00225     case U8G_COM_MSG_INIT:
00226       u8g_com_arduino_assign_pin_output_high(u8g);
00227       u8g_com_arduino_digital_write(u8g, U8G_PI_SCK, LOW);
00228       u8g_com_arduino_digital_write(u8g, U8G_PI_MOSI, LOW);
00229       u8g_com_arduino_init_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK]);
00230       break;
00231     
00232     case U8G_COM_MSG_STOP:
00233       break;
00234 
00235     case U8G_COM_MSG_RESET:
00236       if ( u8g->pin_list[U8G_PI_RESET] != U8G_PIN_NONE )
00237         u8g_com_arduino_digital_write(u8g, U8G_PI_RESET, arg_val);
00238       break;
00239       
00240     case U8G_COM_MSG_CHIP_SELECT:
00241       if ( arg_val == 0 )
00242       {
00243         /* disable */
00244         u8g_com_arduino_digital_write(u8g, U8G_PI_CS, HIGH);
00245       }
00246       else
00247       {
00248         /* enable */
00249         u8g_com_arduino_digital_write(u8g, U8G_PI_SCK, LOW);
00250         u8g_com_arduino_digital_write(u8g, U8G_PI_CS, LOW);
00251       }
00252       break;
00253 
00254     case U8G_COM_MSG_WRITE_BYTE:
00255       u8g_com_arduino_do_shift_out_msb_first( arg_val );
00256       //u8g_arduino_sw_spi_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK], arg_val);
00257       break;
00258     
00259     case U8G_COM_MSG_WRITE_SEQ:
00260       {
00261         register uint8_t *ptr = arg_ptr;
00262         while( arg_val > 0 )
00263         {
00264           u8g_com_arduino_do_shift_out_msb_first(*ptr++);
00265           // u8g_arduino_sw_spi_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK], *ptr++);
00266           arg_val--;
00267         }
00268       }
00269       break;
00270 
00271       case U8G_COM_MSG_WRITE_SEQ_P:
00272       {
00273         register uint8_t *ptr = arg_ptr;
00274         while( arg_val > 0 )
00275         {
00276           u8g_com_arduino_do_shift_out_msb_first( u8g_pgm_read(ptr) );
00277           //u8g_arduino_sw_spi_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK], u8g_pgm_read(ptr));
00278           ptr++;
00279           arg_val--;
00280         }
00281       }
00282       break;
00283       
00284     case U8G_COM_MSG_ADDRESS:                     /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
00285       u8g_com_arduino_digital_write(u8g, U8G_PI_A0, arg_val);
00286       break;
00287   }
00288   return 1;
00289 }
00290 
00291 #else /* ARDUINO */
00292 
00293 uint8_t u8g_com_arduino_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
00294 {
00295   return 1;
00296 }
00297 
00298 #endif /* ARDUINO */
00299 
00300