iforce2d Chris / Mbed 2 deprecated ubxDistanceMeter

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers u8g_com_arduino_st7920_custom.c Source File

u8g_com_arduino_st7920_custom.c

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