iforce2d Chris / Mbed 2 deprecated ubxDistanceMeter

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers u8g_com_io.c Source File

u8g_com_io.c

00001 /*
00002   
00003   u8g_com_io.c
00004   
00005   abstraction layer for low level i/o 
00006 
00007   Universal 8bit Graphics Library
00008   
00009   Copyright (c) 2012, olikraus@gmail.com
00010   All rights reserved.
00011 
00012   Redistribution and use in source and binary forms, with or without modification, 
00013   are permitted provided that the following conditions are met:
00014 
00015   * Redistributions of source code must retain the above copyright notice, this list 
00016     of conditions and the following disclaimer.
00017     
00018   * Redistributions in binary form must reproduce the above copyright notice, this 
00019     list of conditions and the following disclaimer in the documentation and/or other 
00020     materials provided with the distribution.
00021 
00022   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
00023   CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
00024   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00025   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00026   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
00027   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
00028   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
00029   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
00030   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
00031   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
00032   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
00033   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00034   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
00035 
00036   Update for ATOMIC operation done (01 Jun 2013)
00037     U8G_ATOMIC_OR(ptr, val)
00038     U8G_ATOMIC_AND(ptr, val)
00039     U8G_ATOMIC_START();
00040     U8G_ATOMIC_END();
00041 
00042   uint8_t u8g_Pin(uint8_t port, uint8_t bitpos)                     Convert to internal number: AVR: port*8+bitpos, ARM: port*16+bitpos
00043   void u8g_SetPinOutput(uint8_t internal_pin_number)
00044   void u8g_SetPinInput(uint8_t internal_pin_number)
00045   void u8g_SetPinLevel(uint8_t internal_pin_number, uint8_t level)
00046   uint8_t u8g_GetPinLevel(uint8_t internal_pin_number)
00047 
00048 
00049 */
00050 
00051 #include "u8g.h"
00052 
00053 #if defined(__AVR__)
00054 
00055 #include <avr/interrupt.h>
00056 #include <avr/io.h>
00057 
00058 typedef volatile uint8_t * IO_PTR;
00059 
00060 /* create internal pin number */
00061 uint8_t u8g_Pin(uint8_t port, uint8_t bitpos)
00062 {
00063   port <<= 3;
00064   port += bitpos;
00065   return port;
00066 }
00067 
00068 const IO_PTR u8g_avr_ddr_P[] PROGMEM = {
00069 #ifdef DDRA
00070   &DDRA,
00071 #else
00072   0,
00073 #endif
00074   &DDRB,
00075 #ifdef DDRC
00076   &DDRC,
00077 #ifdef DDRD
00078   &DDRD,
00079 #ifdef DDRE
00080   &DDRE,
00081 #ifdef DDRF
00082   &DDRF,
00083 #ifdef DDRG
00084   &DDRG,
00085 #ifdef DDRH
00086   &DDRH,
00087 #endif
00088 #endif
00089 #endif
00090 #endif
00091 #endif
00092 #endif
00093 };
00094 
00095 
00096 const IO_PTR u8g_avr_port_P[] PROGMEM = {
00097 #ifdef PORTA
00098   &PORTA,
00099 #else
00100   0,
00101 #endif
00102   &PORTB,
00103 #ifdef PORTC
00104   &PORTC,
00105 #ifdef PORTD
00106   &PORTD,
00107 #ifdef PORTE
00108   &PORTE,
00109 #ifdef PORTF
00110   &PORTF,
00111 #ifdef PORTG
00112   &PORTG,
00113 #ifdef PORTH
00114   &PORTH,
00115 #endif
00116 #endif
00117 #endif
00118 #endif
00119 #endif
00120 #endif
00121 };
00122 
00123 const IO_PTR u8g_avr_pin_P[] PROGMEM = {
00124 #ifdef PINA
00125   &PINA,
00126 #else
00127   0,
00128 #endif
00129   &PINB,
00130 #ifdef PINC
00131   &PINC,
00132 #ifdef PIND
00133   &PIND,
00134 #ifdef PINE
00135   &PINE,
00136 #ifdef PINF
00137   &PINF,
00138 #ifdef PING
00139   &PING,
00140 #ifdef PINH
00141   &PINH,
00142 #endif
00143 #endif
00144 #endif
00145 #endif
00146 #endif
00147 #endif
00148 };
00149 
00150 static volatile uint8_t *u8g_get_avr_io_ptr(const IO_PTR *base, uint8_t offset)
00151 {
00152   volatile uint8_t * tmp;
00153   base += offset;
00154   memcpy_P(&tmp, base, sizeof(volatile uint8_t * PROGMEM));
00155   return tmp; 
00156 }
00157 
00158 /* set direction to output of the specified pin (internal pin number) */
00159 void u8g_SetPinOutput(uint8_t internal_pin_number)
00160 {
00161   *u8g_get_avr_io_ptr(u8g_avr_ddr_P, internal_pin_number>>3) |= _BV(internal_pin_number&7);
00162 }
00163 
00164 void u8g_SetPinInput(uint8_t internal_pin_number)
00165 {
00166   *u8g_get_avr_io_ptr(u8g_avr_ddr_P, internal_pin_number>>3) &= ~_BV(internal_pin_number&7);
00167 }
00168 
00169 void u8g_SetPinLevel(uint8_t internal_pin_number, uint8_t level)
00170 {
00171   volatile uint8_t * tmp = u8g_get_avr_io_ptr(u8g_avr_port_P, internal_pin_number>>3);
00172   
00173   if ( level == 0 )
00174   {
00175     U8G_ATOMIC_AND(tmp, ~_BV(internal_pin_number&7));
00176    // *tmp &= ~_BV(internal_pin_number&7);
00177   }
00178   else
00179   {
00180     U8G_ATOMIC_OR(tmp, _BV(internal_pin_number&7));
00181     //*tmp |= _BV(internal_pin_number&7);
00182   }
00183   
00184 }
00185 
00186 uint8_t u8g_GetPinLevel(uint8_t internal_pin_number)
00187 {
00188   volatile uint8_t * tmp = u8g_get_avr_io_ptr(u8g_avr_pin_P, internal_pin_number>>3);
00189   if ( ((*tmp) & _BV(internal_pin_number&7))  != 0 )
00190     return 1;
00191   return 0;
00192 }
00193 
00194 #else
00195 
00196 /* convert "port" and "bitpos" to internal pin number */
00197 uint8_t u8g_Pin(uint8_t port, uint8_t bitpos)
00198 {
00199   port <<= 3;
00200   port += bitpos;
00201   return port;
00202 }
00203 
00204 void u8g_SetPinOutput(uint8_t internal_pin_number)
00205 {
00206 }
00207 
00208 void u8g_SetPinInput(uint8_t internal_pin_number)
00209 {
00210 }
00211 
00212 void u8g_SetPinLevel(uint8_t internal_pin_number, uint8_t level)
00213 {
00214 }
00215 
00216 uint8_t u8g_GetPinLevel(uint8_t internal_pin_number)
00217 {
00218   return 0;
00219 }
00220 
00221 #endif
00222 
00223 
00224 #if defined(U8G_WITH_PINLIST)
00225 
00226 void u8g_SetPIOutput(u8g_t *u8g, uint8_t pi)
00227 {
00228   uint8_t pin;
00229   pin = u8g->pin_list[pi];
00230   if ( pin != U8G_PIN_NONE )
00231     u8g_SetPinOutput(pin);
00232 }
00233 
00234 void u8g_SetPILevel(u8g_t *u8g, uint8_t pi, uint8_t level)
00235 {
00236   uint8_t pin;
00237   pin = u8g->pin_list[pi];
00238   if ( pin != U8G_PIN_NONE )
00239     u8g_SetPinLevel(pin, level);
00240 }
00241 
00242 #else  /* defined(U8G_WITH_PINLIST) */
00243 void u8g_SetPIOutput(u8g_t *u8g, uint8_t pi)
00244 {
00245 }
00246 
00247 void u8g_SetPILevel(u8g_t *u8g, uint8_t pi, uint8_t level)
00248 {
00249 }
00250 
00251 #endif /* defined(U8G_WITH_PINLIST) */
00252