iforce2d Chris / Mbed 2 deprecated ubxDistanceMeter

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers u8g_com_atmega_st7920_spi.c Source File

u8g_com_atmega_st7920_spi.c

00001 /*
00002   
00003   u8g_com_atmega_st7920_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   A special SPI interface for ST7920 controller
00035 
00036 */
00037 
00038 #include "u8g.h"
00039 
00040 #if defined(__AVR__)
00041 
00042 static void u8g_atmega_st7920_sw_spi_shift_out(u8g_t *u8g, uint8_t val) U8G_NOINLINE;
00043 static void u8g_atmega_st7920_sw_spi_shift_out(u8g_t *u8g, uint8_t val)
00044 {
00045   uint8_t i = 8;
00046   do
00047   {
00048     u8g_SetPILevel(u8g, U8G_PI_MOSI, val & 128 );
00049     val <<= 1;
00050     u8g_SetPILevel(u8g, U8G_PI_SCK, 1 );
00051     u8g_MicroDelay();       /* 15 Aug 2012: added for high speed uC */
00052     u8g_SetPILevel(u8g, U8G_PI_SCK, 0 );
00053     u8g_MicroDelay();       /* 15 Aug 2012: added for high speed uC */
00054     i--;
00055   } while( i != 0 );
00056 }
00057 
00058 static void u8g_com_atmega_st7920_write_byte(u8g_t *u8g, uint8_t rs, uint8_t val) U8G_NOINLINE;
00059 static void u8g_com_atmega_st7920_write_byte(u8g_t *u8g, uint8_t rs, uint8_t val)
00060 {
00061   uint8_t i;
00062   
00063   if ( rs == 0 )
00064   {
00065     /* command */
00066     u8g_atmega_st7920_sw_spi_shift_out(u8g, 0x0f8);
00067   }
00068   else if ( rs == 1 )
00069   {
00070     /* data */
00071     u8g_atmega_st7920_sw_spi_shift_out(u8g, 0x0fa);
00072   }
00073   
00074   u8g_atmega_st7920_sw_spi_shift_out(u8g, val & 0x0f0);
00075   u8g_atmega_st7920_sw_spi_shift_out(u8g, val << 4);
00076 
00077   for( i = 0; i < 4; i++ )
00078     u8g_10MicroDelay();
00079 }
00080 
00081 
00082 uint8_t u8g_com_atmega_st7920_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
00083 {
00084   switch(msg)
00085   {
00086     case U8G_COM_MSG_INIT:
00087       u8g_SetPIOutput(u8g, U8G_PI_SCK);
00088       u8g_SetPIOutput(u8g, U8G_PI_MOSI);
00089       /* u8g_SetPIOutput(u8g, U8G_PI_A0); */
00090       u8g_SetPIOutput(u8g, U8G_PI_CS);
00091       u8g_SetPIOutput(u8g, U8G_PI_RESET);
00092       
00093       u8g_SetPILevel(u8g, U8G_PI_SCK, 0 );
00094       u8g_SetPILevel(u8g, U8G_PI_MOSI, 0 );
00095       u8g_SetPILevel(u8g, U8G_PI_CS, 0 );
00096       /* u8g_SetPILevel(u8g, U8G_PI_A0, 0); */
00097     
00098       u8g->pin_list[U8G_PI_A0_STATE] = 0;       /* inital RS state: command mode */
00099       break;
00100     
00101     case U8G_COM_MSG_STOP:
00102       break;
00103 
00104     case U8G_COM_MSG_RESET:
00105       u8g_SetPILevel(u8g, U8G_PI_RESET, arg_val);
00106       break;
00107     
00108     case U8G_COM_MSG_ADDRESS:                     /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
00109       u8g->pin_list[U8G_PI_A0_STATE] = arg_val;
00110       break;
00111 
00112     case U8G_COM_MSG_CHIP_SELECT:      
00113       if ( arg_val == 0 )
00114       {
00115         /* disable, note: the st7920 has an active high chip select */
00116         u8g_SetPILevel(u8g, U8G_PI_CS, 0);
00117       }
00118       else
00119       {
00120         /* u8g_SetPILevel(u8g, U8G_PI_SCK, 0 ); */
00121         /* enable */
00122         u8g_SetPILevel(u8g, U8G_PI_CS, 1); /* CS = 1 (high active) */
00123       }
00124       break;
00125       
00126 
00127     case U8G_COM_MSG_WRITE_BYTE:
00128       u8g_com_atmega_st7920_write_byte(u8g, u8g->pin_list[U8G_PI_A0_STATE], arg_val);
00129       u8g->pin_list[U8G_PI_A0_STATE] = 2; 
00130       break;
00131     
00132     case U8G_COM_MSG_WRITE_SEQ:
00133       {
00134         register uint8_t *ptr = arg_ptr;
00135         while( arg_val > 0 )
00136         {
00137           u8g_com_atmega_st7920_write_byte(u8g, u8g->pin_list[U8G_PI_A0_STATE], *ptr++);
00138       u8g->pin_list[U8G_PI_A0_STATE] = 2; 
00139           arg_val--;
00140         }
00141       }
00142       break;
00143 
00144       case U8G_COM_MSG_WRITE_SEQ_P:
00145       {
00146         register uint8_t *ptr = arg_ptr;
00147         while( arg_val > 0 )
00148         {
00149           u8g_com_atmega_st7920_write_byte(u8g, u8g->pin_list[U8G_PI_A0_STATE], u8g_pgm_read(ptr));
00150       u8g->pin_list[U8G_PI_A0_STATE] = 2; 
00151           ptr++;
00152           arg_val--;
00153         }
00154       }
00155       break;
00156   }
00157   return 1;
00158 }
00159 
00160 #else
00161 
00162 
00163 uint8_t u8g_com_atmega_st7920_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
00164 {
00165   return 1;
00166 }
00167 
00168 
00169 #endif
00170 
00171