void i2c_delay(void) { /* should be at least 4 */ /* should be 5 for 100KHz transfer speed */ delay_micro_seconds(5); }
int __attribute__ ((noinline)) main(void) { /* set systick and start systick interrupt */ SysTick_Config(SYS_CORE_CLOCK/1000UL*(unsigned long)SYS_TICK_PERIOD_IN_MS); /* turn on GPIO */ Chip_GPIO_Init(LPC_GPIO_PORT); /* disable SWCLK and SWDIO, after reset, boot code may activate this */ Chip_SWM_DisableFixedPin(2); Chip_SWM_DisableFixedPin(3); /* turn on IOCON */ Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_IOCON); /* turn on switch matrix */ Chip_SWM_Init(); /* activate analog comperator */ Chip_ACMP_Init(LPC_CMP); /* let LED on pin 4 of the DIP8 blink */ Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, 0, 2); for(;;) { Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 0, 2); delay_micro_seconds(500000UL); Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 0, 2); delay_micro_seconds(500000UL); } /* enter sleep mode: Reduce from 1.4mA to 0.8mA with 12MHz */ while (1) { SCB->SCR |= (1UL << SCB_SCR_SLEEPONEXIT_Pos); /* enter sleep mode after interrupt */ Chip_PMU_SleepState(LPC_PMU); /* enter sleep mode now */ } }
void u8g_10MicroDelay(void) { delay_micro_seconds(10); }
void u8g_MicroDelay(void) { delay_micro_seconds(1); }
void u8g_Delay(uint16_t val) { delay_micro_seconds(1000UL*(uint32_t)val); }
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 u8x8_gpio_and_delay_stm32l0(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) { switch(msg) { case U8X8_MSG_GPIO_AND_DELAY_INIT: /* only support for software I2C*/ RCC->IOPENR |= RCC_IOPENR_IOPAEN; /* Enable clock for GPIO Port A */ __NOP(); __NOP(); GPIOA->MODER &= ~GPIO_MODER_MODE14; /* clear mode for PA10 */ //GPIOA->MODER |= GPIO_MODER_MODE14_0; /* Output mode for PA10 */ GPIOA->OTYPER &= ~GPIO_OTYPER_OT_14; /* no open drain for PA10 */ GPIOA->OSPEEDR &= ~GPIO_OSPEEDER_OSPEED14; /* low speed for PA10 */ GPIOA->PUPDR &= ~GPIO_PUPDR_PUPD14; /* no pullup/pulldown for PA10 */ //GPIOA->BSRR = GPIO_BSRR_BS_14; /* atomic set PA10 */ GPIOA->MODER &= ~GPIO_MODER_MODE13; /* clear mode for PA9 */ //GPIOA->MODER |= GPIO_MODER_MODE13_0; /* Output mode for PA9 */ GPIOA->OTYPER &= ~GPIO_OTYPER_OT_13; /* no open drain for PA9 */ GPIOA->OSPEEDR &= ~GPIO_OSPEEDER_OSPEED13; /* low speed for PA9 */ GPIOA->PUPDR &= ~GPIO_PUPDR_PUPD13; /* no pullup/pulldown for PA9 */ //GPIOA->BSRR = GPIO_BSRR_BS_13; /* atomic set PA9 */ break; case U8X8_MSG_DELAY_NANO: /* not required for SW I2C */ break; case U8X8_MSG_DELAY_10MICRO: /* not used at the moment */ break; case U8X8_MSG_DELAY_100NANO: /* not used at the moment */ break; case U8X8_MSG_DELAY_MILLI: delay_micro_seconds(arg_int*1000UL); break; case U8X8_MSG_DELAY_I2C: /* arg_int is 1 or 4: 100KHz (5us) or 400KHz (1.25us) */ delay_micro_seconds(arg_int<=2?5:1); break; case U8X8_MSG_GPIO_I2C_CLOCK: if ( arg_int == 0 ) { GPIOA->MODER &= ~GPIO_MODER_MODE13; /* clear mode for PA10 */ GPIOA->MODER |= GPIO_MODER_MODE13_0; /* Output mode for PA10 */ GPIOA->BSRR = GPIO_BSRR_BR_3; /* atomic clr PA9 */ } else { //GPIOA->BSRR = GPIO_BSRR_BS_13; /* atomic set PA9 */ GPIOA->MODER &= ~GPIO_MODER_MODE13; /* clear mode for PA9: input mode */ } break; case U8X8_MSG_GPIO_I2C_DATA: if ( arg_int == 0 ) { GPIOA->MODER &= ~GPIO_MODER_MODE14; /* clear mode for PA10 */ GPIOA->MODER |= GPIO_MODER_MODE14_0; /* Output mode for PA10 */ GPIOA->BSRR = GPIO_BSRR_BR_14; /* atomic clr PA10 */ } else { //GPIOA->BSRR = GPIO_BSRR_BS_14; /* atomic set PA10 */ // input mode GPIOA->MODER &= ~GPIO_MODER_MODE14; /* clear mode for PA10: input mode */ } break; /* case U8X8_MSG_GPIO_MENU_SELECT: u8x8_SetGPIOResult(u8x8, Chip_GPIO_GetPinState(LPC_GPIO, KEY_SELECT_PORT, KEY_SELECT_PIN)); break; case U8X8_MSG_GPIO_MENU_NEXT: u8x8_SetGPIOResult(u8x8, Chip_GPIO_GetPinState(LPC_GPIO, KEY_NEXT_PORT, KEY_NEXT_PIN)); break; case U8X8_MSG_GPIO_MENU_PREV: u8x8_SetGPIOResult(u8x8, Chip_GPIO_GetPinState(LPC_GPIO, KEY_PREV_PORT, KEY_PREV_PIN)); break; case U8X8_MSG_GPIO_MENU_HOME: u8x8_SetGPIOResult(u8x8, Chip_GPIO_GetPinState(LPC_GPIO, KEY_HOME_PORT, KEY_HOME_PIN)); break; */ default: u8x8_SetGPIOResult(u8x8, 1); break; } return 1; }