mirror of
https://github.com/olikraus/U8g2_Arduino.git
synced 2026-09-15 11:13:03 +00:00
2.22.17
This commit is contained in:
+406
@@ -160,6 +160,191 @@ extern "C" uint8_t u8x8_gpio_and_delay_arduino(u8x8_t *u8x8, uint8_t msg, uint8_
|
||||
#endif // U8X8_USE_PINS
|
||||
|
||||
|
||||
|
||||
|
||||
/*=============================================*/
|
||||
|
||||
/*
|
||||
replacement for a more faster u8x8_byte_3wire_sw_spi
|
||||
in general u8x8_byte_3wire_sw_spi could be a fallback:
|
||||
|
||||
uint8_t u8x8_byte_arduino_3wire_sw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
|
||||
{
|
||||
return u8x8_byte_3wire_sw_spi(u8x8, msg,arg_int, arg_ptr);
|
||||
}
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
#ifndef __AVR_ARCH__
|
||||
#define __AVR_ARCH__ 0
|
||||
#endif
|
||||
|
||||
#if !defined(U8X8_USE_PINS)
|
||||
/* no pin information (very strange), so fallback */
|
||||
uint8_t u8x8_byte_arduino_3wire_sw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
|
||||
{
|
||||
return u8x8_byte_3wire_sw_spi(u8x8, msg,arg_int, arg_ptr);
|
||||
}
|
||||
|
||||
#elif __AVR_ARCH__ == 4 || __AVR_ARCH__ == 5 || __AVR_ARCH__ == 51 || __AVR_ARCH__ == 6
|
||||
|
||||
/* this function completly replaces u8x8_byte_4wire_sw_spi*/
|
||||
extern "C" uint8_t u8x8_byte_arduino_3wire_sw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
|
||||
{
|
||||
uint8_t i;
|
||||
uint8_t takeover_edge = u8x8_GetSPIClockPhase(u8x8);
|
||||
uint16_t b;
|
||||
uint8_t *data;
|
||||
|
||||
/* the following static vars are recalculated in U8X8_MSG_BYTE_START_TRANSFER */
|
||||
/* so, it should be possible to use multiple displays with different pins */
|
||||
|
||||
static volatile uint8_t *arduino_clock_port;
|
||||
|
||||
static uint8_t arduino_clock_mask;
|
||||
static uint8_t arduino_clock_n_mask;
|
||||
|
||||
static volatile uint8_t *arduino_data_port;
|
||||
static uint8_t arduino_data_mask;
|
||||
static uint8_t arduino_data_n_mask;
|
||||
|
||||
static uint8_t last_dc;
|
||||
|
||||
|
||||
switch(msg)
|
||||
{
|
||||
case U8X8_MSG_BYTE_SEND:
|
||||
|
||||
data = (uint8_t *)arg_ptr;
|
||||
if ( takeover_edge == 0 )
|
||||
{
|
||||
while( arg_int > 0 )
|
||||
{
|
||||
b = *data;
|
||||
if ( last_dc != 0 )
|
||||
b |= 256;
|
||||
data++;
|
||||
arg_int--;
|
||||
/* issue 156, check for speed */
|
||||
#if F_CPU <= 17000000
|
||||
if ( b == 0 )
|
||||
{
|
||||
*arduino_data_port &= arduino_data_n_mask;
|
||||
for( i = 0; i < 9; i++ )
|
||||
{
|
||||
*arduino_clock_port |= arduino_clock_mask;
|
||||
*arduino_clock_port &= arduino_clock_n_mask;
|
||||
}
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
for( i = 0; i < 9; i++ )
|
||||
{
|
||||
if ( b & 256 )
|
||||
*arduino_data_port |= arduino_data_mask;
|
||||
else
|
||||
*arduino_data_port &= arduino_data_n_mask;
|
||||
|
||||
*arduino_clock_port |= arduino_clock_mask;
|
||||
b <<= 1;
|
||||
*arduino_clock_port &= arduino_clock_n_mask;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while( arg_int > 0 )
|
||||
{
|
||||
b = *data;
|
||||
if ( last_dc != 0 )
|
||||
b |= 256;
|
||||
data++;
|
||||
arg_int--;
|
||||
/* issue 156, check for speed */
|
||||
#if F_CPU <= 17000000
|
||||
if ( b == 0 )
|
||||
{
|
||||
*arduino_data_port &= arduino_data_n_mask;
|
||||
for( i = 0; i < 9; i++ )
|
||||
{
|
||||
*arduino_clock_port &= arduino_clock_n_mask;
|
||||
*arduino_clock_port |= arduino_clock_mask;
|
||||
}
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
for( i = 0; i < 9; i++ )
|
||||
{
|
||||
if ( b & 256 )
|
||||
*arduino_data_port |= arduino_data_mask;
|
||||
else
|
||||
*arduino_data_port &= arduino_data_n_mask;
|
||||
|
||||
*arduino_clock_port &= arduino_clock_n_mask;
|
||||
b <<= 1;
|
||||
*arduino_clock_port |= arduino_clock_mask;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case U8X8_MSG_BYTE_INIT:
|
||||
/* disable chipselect */
|
||||
u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
|
||||
/* no wait required here */
|
||||
|
||||
/* for SPI: setup correct level of the clock signal */
|
||||
u8x8_gpio_SetSPIClock(u8x8, u8x8_GetSPIClockPhase(u8x8));
|
||||
break;
|
||||
case U8X8_MSG_BYTE_SET_DC:
|
||||
last_dc = arg_int;
|
||||
break;
|
||||
case U8X8_MSG_BYTE_START_TRANSFER:
|
||||
u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level);
|
||||
u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->post_chip_enable_wait_ns, NULL);
|
||||
|
||||
/* there is no consistency checking for u8x8->pins[U8X8_PIN_SPI_CLOCK] */
|
||||
|
||||
arduino_clock_port = portOutputRegister(digitalPinToPort(u8x8->pins[U8X8_PIN_SPI_CLOCK]));
|
||||
arduino_clock_mask = digitalPinToBitMask(u8x8->pins[U8X8_PIN_SPI_CLOCK]);
|
||||
arduino_clock_n_mask = ~arduino_clock_mask;
|
||||
|
||||
/* there is no consistency checking for u8x8->pins[U8X8_PIN_SPI_DATA] */
|
||||
|
||||
arduino_data_port = portOutputRegister(digitalPinToPort(u8x8->pins[U8X8_PIN_SPI_DATA]));
|
||||
arduino_data_mask = digitalPinToBitMask(u8x8->pins[U8X8_PIN_SPI_DATA]);
|
||||
arduino_data_n_mask = ~arduino_data_mask;
|
||||
|
||||
break;
|
||||
case U8X8_MSG_BYTE_END_TRANSFER:
|
||||
u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->pre_chip_disable_wait_ns, NULL);
|
||||
u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
#else
|
||||
/* fallback */
|
||||
uint8_t u8x8_byte_arduino_3wire_sw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
|
||||
{
|
||||
return u8x8_byte_3wire_sw_spi(u8x8, msg,arg_int, arg_ptr);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*=============================================*/
|
||||
|
||||
/*
|
||||
@@ -552,6 +737,227 @@ extern "C" uint8_t u8x8_byte_arduino_2nd_hw_spi(U8X8_UNUSED u8x8_t *u8x8, U8X8_U
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*=============================================*/
|
||||
/* fast SW I2C for AVR uC */
|
||||
|
||||
|
||||
#if !defined(U8X8_USE_PINS)
|
||||
/* no pin information (very strange), so fallback */
|
||||
extern "C" uint8_t u8x8_byte_arduino_sw_i2c(U8X8_UNUSED u8x8_t *u8x8, U8X8_UNUSED uint8_t msg, U8X8_UNUSED uint8_t arg_int, U8X8_UNUSED void *arg_ptr)
|
||||
{
|
||||
return u8x8_byte_sw_i2c(u8x8, msg,arg_int, arg_ptr);
|
||||
}
|
||||
|
||||
#elif !defined(U8X8_USE_ARDUINO_AVR_SW_I2C_OPTIMIZATION)
|
||||
|
||||
extern "C" uint8_t u8x8_byte_arduino_sw_i2c(U8X8_UNUSED u8x8_t *u8x8, U8X8_UNUSED uint8_t msg, U8X8_UNUSED uint8_t arg_int, U8X8_UNUSED void *arg_ptr)
|
||||
{
|
||||
return u8x8_byte_sw_i2c(u8x8, msg,arg_int, arg_ptr);
|
||||
}
|
||||
|
||||
#elif __AVR_ARCH__ == 4 || __AVR_ARCH__ == 5 || __AVR_ARCH__ == 51 || __AVR_ARCH__ == 6
|
||||
|
||||
|
||||
/* the following static vars are recalculated in U8X8_MSG_BYTE_START_TRANSFER */
|
||||
/* so, it should be possible to use multiple displays with different pins */
|
||||
|
||||
static volatile uint8_t *arduino_i2c_clock_port;
|
||||
|
||||
static uint8_t arduino_i2c_clock_mask;
|
||||
static uint8_t arduino_i2c_clock_n_mask;
|
||||
|
||||
static volatile uint8_t *arduino_i2c_data_port;
|
||||
static uint8_t arduino_i2c_data_mask;
|
||||
static uint8_t arduino_i2c_data_n_mask;
|
||||
|
||||
/*
|
||||
software i2c,
|
||||
ignores ACK response (which is anyway not provided by some displays)
|
||||
also does not allow reading from the device
|
||||
*/
|
||||
static void i2c_delay(u8x8_t *u8x8) U8X8_NOINLINE;
|
||||
static void i2c_delay(u8x8_t *u8x8)
|
||||
{
|
||||
//u8x8_gpio_Delay(u8x8, U8X8_MSG_DELAY_10MICRO, u8x8->display_info->i2c_bus_clock_100kHz);
|
||||
u8x8_gpio_Delay(u8x8, U8X8_MSG_DELAY_I2C, u8x8->display_info->i2c_bus_clock_100kHz);
|
||||
}
|
||||
|
||||
static void i2c_init(u8x8_t *u8x8)
|
||||
{
|
||||
*arduino_i2c_clock_port |= arduino_i2c_clock_mask;
|
||||
*arduino_i2c_data_port |= arduino_i2c_data_mask;
|
||||
i2c_delay(u8x8);
|
||||
}
|
||||
|
||||
/* actually, the scl line is not observed, so this procedure does not return a value */
|
||||
|
||||
static void i2c_read_scl_and_delay(u8x8_t *u8x8)
|
||||
{
|
||||
/* set as input (line will be high) */
|
||||
*arduino_i2c_clock_port |= arduino_i2c_clock_mask;
|
||||
|
||||
i2c_delay(u8x8);
|
||||
}
|
||||
|
||||
static void i2c_clear_scl(u8x8_t *u8x8)
|
||||
{
|
||||
*arduino_i2c_clock_port &= arduino_i2c_clock_n_mask;
|
||||
}
|
||||
|
||||
static void i2c_read_sda(u8x8_t *u8x8)
|
||||
{
|
||||
/* set as input (line will be high) */
|
||||
*arduino_i2c_data_port |= arduino_i2c_data_mask;
|
||||
}
|
||||
|
||||
static void i2c_clear_sda(u8x8_t *u8x8)
|
||||
{
|
||||
/* set open collector and drive low */
|
||||
*arduino_i2c_data_port &= arduino_i2c_data_n_mask;
|
||||
}
|
||||
|
||||
static void i2c_start(u8x8_t *u8x8)
|
||||
{
|
||||
if ( u8x8->i2c_started != 0 )
|
||||
{
|
||||
/* if already started: do restart */
|
||||
i2c_read_sda(u8x8); /* SDA = 1 */
|
||||
i2c_delay(u8x8);
|
||||
i2c_read_scl_and_delay(u8x8);
|
||||
}
|
||||
i2c_read_sda(u8x8);
|
||||
/* send the start condition, both lines go from 1 to 0 */
|
||||
i2c_clear_sda(u8x8);
|
||||
i2c_delay(u8x8);
|
||||
i2c_clear_scl(u8x8);
|
||||
u8x8->i2c_started = 1;
|
||||
}
|
||||
|
||||
|
||||
static void i2c_stop(u8x8_t *u8x8)
|
||||
{
|
||||
/* set SDA to 0 */
|
||||
i2c_clear_sda(u8x8);
|
||||
i2c_delay(u8x8);
|
||||
|
||||
/* now release all lines */
|
||||
i2c_read_scl_and_delay(u8x8);
|
||||
|
||||
/* set SDA to 1 */
|
||||
i2c_read_sda(u8x8);
|
||||
i2c_delay(u8x8);
|
||||
u8x8->i2c_started = 0;
|
||||
}
|
||||
|
||||
static void i2c_write_bit(u8x8_t *u8x8, uint8_t val)
|
||||
{
|
||||
if (val)
|
||||
i2c_read_sda(u8x8);
|
||||
else
|
||||
i2c_clear_sda(u8x8);
|
||||
|
||||
i2c_delay(u8x8);
|
||||
i2c_read_scl_and_delay(u8x8);
|
||||
i2c_clear_scl(u8x8);
|
||||
}
|
||||
|
||||
static void i2c_read_bit(u8x8_t *u8x8)
|
||||
{
|
||||
//uint8_t val;
|
||||
/* do not drive SDA */
|
||||
i2c_read_sda(u8x8);
|
||||
i2c_delay(u8x8);
|
||||
i2c_read_scl_and_delay(u8x8);
|
||||
i2c_read_sda(u8x8);
|
||||
i2c_delay(u8x8);
|
||||
i2c_clear_scl(u8x8);
|
||||
//return val;
|
||||
}
|
||||
|
||||
static void i2c_write_byte(u8x8_t *u8x8, uint8_t b)
|
||||
{
|
||||
i2c_write_bit(u8x8, b & 128);
|
||||
i2c_write_bit(u8x8, b & 64);
|
||||
i2c_write_bit(u8x8, b & 32);
|
||||
i2c_write_bit(u8x8, b & 16);
|
||||
i2c_write_bit(u8x8, b & 8);
|
||||
i2c_write_bit(u8x8, b & 4);
|
||||
i2c_write_bit(u8x8, b & 2);
|
||||
i2c_write_bit(u8x8, b & 1);
|
||||
|
||||
/* read ack from client */
|
||||
/* 0: ack was given by client */
|
||||
/* 1: nothing happend during ack cycle */
|
||||
i2c_read_bit(u8x8);
|
||||
}
|
||||
|
||||
|
||||
extern "C" uint8_t u8x8_byte_arduino_sw_i2c(U8X8_UNUSED u8x8_t *u8x8, U8X8_UNUSED uint8_t msg, U8X8_UNUSED uint8_t arg_int, U8X8_UNUSED void *arg_ptr)
|
||||
{
|
||||
uint8_t *data;
|
||||
|
||||
switch(msg)
|
||||
{
|
||||
case U8X8_MSG_BYTE_SEND:
|
||||
data = (uint8_t *)arg_ptr;
|
||||
|
||||
while( arg_int > 0 )
|
||||
{
|
||||
i2c_write_byte(u8x8, *data);
|
||||
data++;
|
||||
arg_int--;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case U8X8_MSG_BYTE_INIT:
|
||||
pinMode(u8x8->pins[U8X8_PIN_I2C_CLOCK], OUTPUT);
|
||||
digitalWrite(u8x8->pins[U8X8_PIN_I2C_CLOCK], 1);
|
||||
|
||||
pinMode(u8x8->pins[U8X8_PIN_I2C_DATA], OUTPUT);
|
||||
digitalWrite(u8x8->pins[U8X8_PIN_I2C_DATA], 1);
|
||||
|
||||
i2c_init(u8x8);
|
||||
break;
|
||||
case U8X8_MSG_BYTE_SET_DC:
|
||||
break;
|
||||
case U8X8_MSG_BYTE_START_TRANSFER:
|
||||
|
||||
/* there is no consistency checking for u8x8->pins[U8X8_PIN_I2C_CLOCK] */
|
||||
|
||||
arduino_i2c_clock_port = portOutputRegister(digitalPinToPort(u8x8->pins[U8X8_PIN_I2C_CLOCK]));
|
||||
arduino_i2c_clock_mask = digitalPinToBitMask(u8x8->pins[U8X8_PIN_I2C_CLOCK]);
|
||||
arduino_i2c_clock_n_mask = ~arduino_i2c_clock_mask;
|
||||
|
||||
/* there is no consistency checking for u8x8->pins[U8X8_PIN_I2C_DATA] */
|
||||
|
||||
arduino_i2c_data_port = portOutputRegister(digitalPinToPort(u8x8->pins[U8X8_PIN_I2C_DATA]));
|
||||
arduino_i2c_data_mask = digitalPinToBitMask(u8x8->pins[U8X8_PIN_I2C_DATA]);
|
||||
arduino_i2c_data_n_mask = ~arduino_i2c_data_mask;
|
||||
|
||||
i2c_start(u8x8);
|
||||
i2c_write_byte(u8x8, u8x8_GetI2CAddress(u8x8));
|
||||
|
||||
break;
|
||||
case U8X8_MSG_BYTE_END_TRANSFER:
|
||||
i2c_stop(u8x8);
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
/* not AVR architecture, fallback */
|
||||
extern "C" uint8_t u8x8_byte_arduino_sw_i2c(U8X8_UNUSED u8x8_t *u8x8, U8X8_UNUSED uint8_t msg, U8X8_UNUSED uint8_t arg_int, U8X8_UNUSED void *arg_ptr)
|
||||
{
|
||||
return u8x8_byte_sw_i2c(u8x8, msg,arg_int, arg_ptr);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/*=============================================*/
|
||||
|
||||
|
||||
Reference in New Issue
Block a user