static uint8_t u8g_com_arduino_t6963_read(u8g_t *u8g) { uint8_t val = 0; u8g_com_arduino_digital_write(u8g, U8G_PI_RD, 0); u8g_MicroDelay(); /* 150ns, reference: t6963 datasheet */ U8G_ATOMIC_START(); /* only read bits 0, 1 and 3 */ if ( (*u8g_input_data_port[3] & u8g_data_mask[3]) != 0 ) val++; val <<= 1; val <<= 1; if ( (*u8g_input_data_port[1] & u8g_data_mask[1]) != 0 ) val++; val <<= 1; if ( (*u8g_input_data_port[0] & u8g_data_mask[0]) != 0 ) val++; U8G_ATOMIC_END(); u8g_com_arduino_digital_write(u8g, U8G_PI_RD, 1); u8g_MicroDelay(); /* 10ns, reference: t6963 datasheet */ return val; }
static void u8g_com_arduino_t6963_write(u8g_t *u8g, uint8_t val) { U8G_ATOMIC_START(); u8g_com_arduino_t6963_write_data_pin( 0, val&1 ); val >>= 1; u8g_com_arduino_t6963_write_data_pin( 1, val&1 ); val >>= 1; u8g_com_arduino_t6963_write_data_pin( 2, val&1 ); val >>= 1; u8g_com_arduino_t6963_write_data_pin( 3, val&1 ); val >>= 1; u8g_com_arduino_t6963_write_data_pin( 4, val&1 ); val >>= 1; u8g_com_arduino_t6963_write_data_pin( 5, val&1 ); val >>= 1; u8g_com_arduino_t6963_write_data_pin( 6, val&1 ); val >>= 1; u8g_com_arduino_t6963_write_data_pin( 7, val&1 ); val >>= 1; U8G_ATOMIC_END(); u8g_com_arduino_digital_write(u8g, U8G_PI_WR, 0); u8g_MicroDelay(); /* 80ns, reference: t6963 datasheet */ u8g_com_arduino_digital_write(u8g, U8G_PI_WR, 1); u8g_MicroDelay(); /* 10ns, reference: t6963 datasheet */ }
static void i2c_100KHz_delay(void) { /* 1x u8g_MicroDelay() ca. 130KHz 2x u8g_MicroDelay() ca. 80KHz */ u8g_MicroDelay(); u8g_MicroDelay(); }
uint8_t u8g_dev_ssd1322_nhd31oled_bw_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg) { switch(msg) { case U8G_DEV_MSG_INIT: u8g_InitCom(u8g, dev, U8G_SPI_CLK_CYCLE_300NS); u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd1322_1bit_nhd_312_init_seq); break; case U8G_DEV_MSG_STOP: break; case U8G_DEV_MSG_PAGE_NEXT: { uint8_t i; u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem); uint8_t *p = pb->buf; u8g_uint_t cnt; cnt = pb->width; cnt >>= 3; for( i = 0; i < pb->p.page_height; i++ ) { u8g_dev_ssd1322_1bit_prepare_row(u8g, dev, i); /* this will also enable chip select */ #if !defined(U8G_16BIT) u8g_WriteByte(u8g, dev, 0x0ff); u8g_WriteByte(u8g, dev, 0x0ff); #endif u8g_WriteSequenceBWTo16GrDevice(u8g, dev, cnt, p); #if !defined(U8G_16BIT) u8g_WriteByte(u8g, dev, 0x0ff); u8g_WriteByte(u8g, dev, 0x0ff); #endif u8g_MicroDelay(); // for DUE? u8g_SetChipSelect(u8g, dev, 0); p+=cnt; } } break; case U8G_DEV_MSG_CONTRAST: u8g_SetChipSelect(u8g, dev, 1); u8g_SetAddress(u8g, dev, 0); /* instruction mode */ u8g_WriteByte(u8g, dev, 0x081); u8g_SetAddress(u8g, dev, 1); /* data mode */ u8g_WriteByte(u8g, dev, (*(uint8_t *)arg) >> 1); u8g_MicroDelay(); // for DUE? u8g_SetChipSelect(u8g, dev, 0); break; case U8G_DEV_MSG_SLEEP_ON: u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd13xx_sleep_on); return 1; case U8G_DEV_MSG_SLEEP_OFF: u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd13xx_sleep_off); return 1; } return u8g_dev_pb8h1_base_fn(u8g, dev, msg, arg); }
void u8g_com_arduino_parallel_write(u8g_t *u8g, uint8_t val) { u8g_com_arduino_digital_write(u8g, U8G_PI_D0, val&1); val >>= 1; u8g_com_arduino_digital_write(u8g, U8G_PI_D1, val&1); val >>= 1; u8g_com_arduino_digital_write(u8g, U8G_PI_D2, val&1); val >>= 1; u8g_com_arduino_digital_write(u8g, U8G_PI_D3, val&1); val >>= 1; u8g_com_arduino_digital_write(u8g, U8G_PI_D4, val&1); val >>= 1; u8g_com_arduino_digital_write(u8g, U8G_PI_D5, val&1); val >>= 1; u8g_com_arduino_digital_write(u8g, U8G_PI_D6, val&1); val >>= 1; u8g_com_arduino_digital_write(u8g, U8G_PI_D7, val&1); /* EN cycle time must be 1 micro second, digitalWrite is slow enough to do this */ u8g_Delay(1); u8g_com_arduino_digital_write(u8g, U8G_PI_EN, HIGH); u8g_Delay(1); u8g_MicroDelay(); /* delay by 1000ns, reference: ST7920: 140ns, SBN1661: 100ns */ u8g_com_arduino_digital_write(u8g, U8G_PI_EN, LOW); u8g_10MicroDelay(); /* ST7920 commands: 72us */ u8g_Delay(2); }
void u8g_com_arduino_fast_parallel_write(u8g_t *u8g, uint8_t val) { U8G_ATOMIC_START(); u8g_com_arduino_fast_write_data_pin( 0, val&1 ); val >>= 1; u8g_com_arduino_fast_write_data_pin( 1, val&1 ); val >>= 1; u8g_com_arduino_fast_write_data_pin( 2, val&1 ); val >>= 1; u8g_com_arduino_fast_write_data_pin( 3, val&1 ); val >>= 1; u8g_com_arduino_fast_write_data_pin( 4, val&1 ); val >>= 1; u8g_com_arduino_fast_write_data_pin( 5, val&1 ); val >>= 1; u8g_com_arduino_fast_write_data_pin( 6, val&1 ); val >>= 1; u8g_com_arduino_fast_write_data_pin( 7, val&1 ); val >>= 1; U8G_ATOMIC_END(); /* EN cycle time must be 1 micro second */ u8g_com_arduino_digital_write(u8g, U8G_PI_EN, HIGH); u8g_MicroDelay(); /* delay by 1000ns, reference: ST7920: 140ns, SBN1661: 100ns */ u8g_com_arduino_digital_write(u8g, U8G_PI_EN, LOW); u8g_10MicroDelay(); /* ST7920 commands: 72us */ u8g_10MicroDelay(); /* ST7920 commands: 72us */ }
//************************************************************************************* //************************************************************************************* uint8_t u8g_com_uart_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) { switch(msg) { case U8G_COM_MSG_STOP: //STOP THE DEVICE break; case U8G_COM_MSG_INIT: //INIT HARDWARE INTERFACES, TIMERS, GPIOS... u8g_Delay(500); init_UART3(); //init_LCD(); //set 8 bit etc break; case U8G_COM_MSG_ADDRESS: //SWITCH FROM DATA TO COMMAND MODE (arg_val == 0 for command mode) // define cmd (arg_val = 0) or data mode (arg_val = 1) // cmd - if (arg_val == 0) { control = ST7920_CMD; } else { control = ST7920_DATA; } break; case U8G_COM_MSG_RESET: //TOGGLE THE RESET PIN ON THE DISPLAY BY THE VALUE IN arg_val break; case U8G_COM_MSG_WRITE_BYTE: //WRITE BYTE TO DEVICE UART_write_byte(control << 1); //shift RS left, RW == 0 == write UART_write_byte(arg_val); u8g_MicroDelay(); break; case U8G_COM_MSG_WRITE_SEQ: case U8G_COM_MSG_WRITE_SEQ_P: //WRITE A SEQUENCE OF BYTES TO THE DEVICE { register uint8_t *ptr = arg_ptr; //UART_write_byte((1 << 2) | (control << 1)); //0x04 for initialization of byte sequence, shift RS left, RW == 0 == write while( arg_val > 0 ) { UART_write_byte(control << 1); //shift RS left, RW == 0 == write UART_write_byte( *ptr); ptr++; arg_val--; } } break; } return 1; }
uint8_t u8g_com_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) { switch (msg) { case U8G_COM_MSG_STOP: break; case U8G_COM_MSG_INIT: // init spi and ports u8g_MicroDelay(); break; case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */ //u8g_10MicroDelay(); GPIO_SET_PIN_VAL(LCD_A0, arg_val); u8g_MicroDelay(); break; case U8G_COM_MSG_CHIP_SELECT: GPIO_SET_PIN_VAL(LCD_CSB, !arg_val); u8g_MicroDelay(); break; case U8G_COM_MSG_RESET: GPIO_SET_PIN_VAL(LCD_RSTB, arg_val); u8g_MicroDelay(); break; case U8G_COM_MSG_WRITE_BYTE: spi_out(arg_val); u8g_MicroDelay(); break; case U8G_COM_MSG_WRITE_SEQ: case U8G_COM_MSG_WRITE_SEQ_P: { register uint8_t *ptr = arg_ptr; while (arg_val > 0) { spi_out(*ptr++); arg_val--; } } break; } return 1; }
static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val) { uint8_t cnt = 8; do { if ( val & 128 ) digitalWrite(u8g_data_pin, HIGH); else digitalWrite(u8g_data_pin, LOW); val <<= 1; //u8g_MicroDelay(); digitalWrite(u8g_clock_pin, LOW); cnt--; u8g_MicroDelay(); digitalWrite(u8g_clock_pin, HIGH); u8g_MicroDelay(); } while( cnt != 0 ); }
static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val) { uint8_t i = 8; do { if ( val & 128 ) u8g_digital_write_sam_high(u8g_sam_data_pin); else u8g_digital_write_sam_low(u8g_sam_data_pin); val <<= 1; //u8g_MicroDelay(); u8g_digital_write_sam_high(u8g_sam_clock_pin); u8g_MicroDelay(); u8g_digital_write_sam_low(u8g_sam_clock_pin); u8g_MicroDelay(); i--; } while( i != 0 ); }
void u8g_arduino_sw_spi_shift_out(uint8_t dataPin, uint8_t clockPin, uint8_t val) { uint8_t i = 8; do { if ( val & 128 ) digitalWrite(dataPin, HIGH); else digitalWrite(dataPin, LOW); val <<= 1; u8g_MicroDelay(); /* 23 Sep 2012 */ //delay(1); digitalWrite(clockPin, HIGH); u8g_MicroDelay(); /* 23 Sep 2012 */ //delay(1); digitalWrite(clockPin, LOW); u8g_MicroDelay(); /* 23 Sep 2012 */ //delay(1); i--; } while( i != 0 ); }
static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val) { uint8_t cnt = 8; U8G_ATOMIC_START(); do { if ( val & 128 ) *dog_outData |= dog_bitData; else *dog_outData &= dog_bitNotData; val <<= 1; //u8g_MicroDelay(); //*dog_outClock |= dog_bitClock; *dog_outClock &= dog_bitNotClock; cnt--; u8g_MicroDelay(); //*dog_outClock &= dog_bitNotClock; *dog_outClock |= dog_bitClock; u8g_MicroDelay(); } while( cnt != 0 ); U8G_ATOMIC_END(); }
uint8_t u8g_com_hw_i2c_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) { switch(msg) { case U8G_COM_MSG_STOP: break; case U8G_COM_MSG_INIT: u8g_MicroDelay(); break; case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */ u8g_10MicroDelay(); if (arg_val == 0) { control = 0; } else { control = 0x40; } break; case U8G_COM_MSG_WRITE_BYTE: { uint8_t buffer[2]; buffer[0] = control; buffer[1] = arg_val; HAL_I2C_Master_Transmit(&I2C_HANDLER, DEVICE_ADDRESS, (uint8_t*) buffer, 2, I2C_TIMEOUT); } break; case U8G_COM_MSG_WRITE_SEQ: case U8G_COM_MSG_WRITE_SEQ_P: { uint8_t buffer[DATA_BUFFER_SIZE]; uint8_t *ptr = arg_ptr; buffer[0] = control; for (int i = 1; i <= arg_val; i++) { buffer[i] = *(ptr++); } HAL_I2C_Master_Transmit(&I2C_HANDLER, DEVICE_ADDRESS, (uint8_t *)buffer, arg_val, I2C_TIMEOUT); } break; } return 1; }
uint8_t u8g_com_hw_i2c_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) { register uint8_t *ptr = arg_ptr; switch(msg) { case U8G_COM_MSG_STOP: break; case U8G_COM_MSG_INIT: init_i2c(); //I2C_WrReg(0x80, 0xAE); //I2C_WrReg(0x80, 0x8D); //I2C_WrReg(0x80, 0x14); //I2C_WrReg(0x80, 0xAF); u8g_MicroDelay(); break; case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */ if(arg_val == 0){ control = 0; //Send a Command (D/C# pin is low) } else{ control = 0x40; //send data } u8g_10MicroDelay(); break; case U8G_COM_MSG_CHIP_SELECT: break; case U8G_COM_MSG_RESET: break; case U8G_COM_MSG_WRITE_BYTE: I2C_WrReg(control, arg_val); //u8g_i2c_start(0x78); //u8g_i2c_send_byte(arg_val); //u8g_i2c_stop(); u8g_MicroDelay(); break; case U8G_COM_MSG_WRITE_SEQ: case U8G_COM_MSG_WRITE_SEQ_P: while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY) == SET); I2C_TransferHandling(I2C1, 0x78, 1, I2C_Reload_Mode, I2C_Generate_Start_Write); //Ensure the transmit interrupted flag is set while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TXIS) == RESET); //Send the address of the register we wish to write to I2C_SendData(I2C1, control); //Ensure that the transfer complete reload flag is Set, essentially a standard //TC flag while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TCR) == RESET); I2C_TransferHandling(I2C1, 0x78, arg_val+1, I2C_AutoEnd_Mode, I2C_No_StartStop); //Again, wait until the transmit interrupted flag is set while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TXIS) == RESET); //Send the value you wish you write to the register while( arg_val > 0 ) { I2C_SendData(I2C1, *ptr++); arg_val--; while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TXIS) == RESET); //Wait for the stop flag to be set indicating a stop condition has been sent } I2C_TransferHandling(I2C1, 0x78, 0, I2C_AutoEnd_Mode, I2C_Generate_Stop); //while(I2C_GetFlagStatus(I2C1,I2C_FLAG_STOPF) == RESET); //I2C_ClearFlag(I2C1, I2C_FLAG_STOPF); delay_micro_seconds(100); //u8g_MicroDelay(); break; } return 1; }
uint8_t u8g_com_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) { switch(msg) { case U8G_COM_MSG_STOP: break; case U8G_COM_MSG_INIT: if ( arg_val <= U8G_SPI_CLK_CYCLE_50NS ) { spi_init(SPI_BAUDRATEPRESCALER_2); } else if ( arg_val <= U8G_SPI_CLK_CYCLE_300NS ) { spi_init(SPI_BAUDRATEPRESCALER_4); } else if ( arg_val <= U8G_SPI_CLK_CYCLE_400NS ) { spi_init(SPI_BAUDRATEPRESCALER_4); } else { spi_init(SPI_BAUDRATEPRESCALER_8); } u8g_MicroDelay(); break; case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */ u8g_10MicroDelay(); set_gpio_level(LCD_CD_PIN, arg_val); u8g_10MicroDelay(); break; case U8G_COM_MSG_CHIP_SELECT: if ( arg_val == 0 ) { /* disable */ uint8_t i; /* this delay is required to avoid that the display is switched off too early --> DOGS102 with LPC1114 */ for( i = 0; i < 5; i++ )u8g_10MicroDelay(); set_gpio_level(LCD_CS_PIN, 1); } else { /* enable */ set_gpio_level(LCD_CS_PIN, 0); } u8g_MicroDelay(); break; case U8G_COM_MSG_RESET: set_gpio_level(LCD_RST_PIN, arg_val); u8g_10MicroDelay(); break; case U8G_COM_MSG_WRITE_BYTE: spi_out(arg_val); u8g_MicroDelay(); break; case U8G_COM_MSG_WRITE_SEQ: case U8G_COM_MSG_WRITE_SEQ_P: { register uint8_t *ptr = arg_ptr; while( arg_val > 0 ) { spi_out(*ptr++); arg_val--; } } break; } return 1; }