// Changes OLED power (brightness) void oled_power(uint8_t level) { uint8_t p[6]; if(level > 90) level = 90; p[0] = p[2] = p[4] = level >> 4; p[1] = p[3] = p[5] = level & 15; oled_cmd(0x0E, 6, p); }
// Sets the memory window void oled_window(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) { uint8_t window[8]; y0 += 0x1C; // TODO: Pretty sure it's possible to configure the display y1 += 0x1C; // to get rid of this offset... but at least it works now! window[0] = x0 >> 4; window[1] = x0 & 15; window[2] = x1 >> 4; window[3] = x1 & 15; window[4] = y0 >> 4; window[5] = y0 & 15; window[6] = y1 >> 4; window[7] = y1 & 15; oled_cmd(0x0A, 0x08, window); }
/******************************************************************* * Function: void USBCBInitEP(void) * * Overview: This function is called when the device becomes * initialized, which occurs after the host sends a * SET_CONFIGURATION (wValue not = 0) request. This * callback function should initialize the endpoints * for the device's usage according to the current * configuration. *******************************************************************/ void USBCBInitEP(void) { #if (MSD_DATA_IN_EP == MSD_DATA_OUT_EP) USBEnableEndpoint(MSD_DATA_IN_EP, USB_IN_ENABLED | USB_OUT_ENABLED | USB_HANDSHAKE_ENABLED| USB_DISALLOW_SETUP); #else USBEnableEndpoint(MSD_DATA_IN_EP, USB_IN_ENABLED | USB_HANDSHAKE_ENABLED | USB_DISALLOW_SETUP); USBEnableEndpoint(MSD_DATA_OUT_EP, USB_OUT_ENABLED | USB_HANDSHAKE_ENABLED| USB_DISALLOW_SETUP); #endif #if defined(DISPLAY_ENABLED) oled_cmd(OLEDREG_DISPLAY_OFF); // to avoid interference disp_start_usb(); #endif USBMSDInit(); // init Mass Sstorage Device over USB CDCInitEP(); // init serial line over USB }
// Initializes and turns on screen void oled_init(void) { // Set initial pin states pin_clear(OLED_VEN); pin_set(ENABLE_2V8); pin_set(OLED_RESET); pin_set(OLED_A0); // SPI config { // Enable peripheral clock RCC_AHB1PeriphClockCmd(OLED_SPI_SCK->clock | OLED_SPI_MOSI->clock, ENABLE); // Enable SPI clock RCC_APB2PeriphClockCmd(OLED_SPI_CLK, ENABLE); //pinInit(OLED_SPI_SCK); //pinInit(OLED_SPI_MOSI); SPI_I2S_DeInit(OLED_SPI); SPI_InitTypeDef ConfigSPI; ConfigSPI.SPI_Direction = SPI_Direction_2Lines_FullDuplex; ConfigSPI.SPI_Mode = SPI_Mode_Master; ConfigSPI.SPI_DataSize = SPI_DataSize_8b; ConfigSPI.SPI_CPOL = SPI_CPOL_High; ConfigSPI.SPI_CPHA = SPI_CPHA_2Edge; ConfigSPI.SPI_NSS = SPI_NSS_Soft; ConfigSPI.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; ConfigSPI.SPI_FirstBit = SPI_FirstBit_MSB; SPI_Init(OLED_SPI, &ConfigSPI); SPI_Cmd(OLED_SPI, ENABLE); } // DMA config { RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); DMA_InitTypeDef ConfigDMA; ConfigDMA.DMA_Channel = DMA_Channel_3; ConfigDMA.DMA_PeripheralBaseAddr = (uint32_t)&SPI1->DR; ConfigDMA.DMA_Memory0BaseAddr = 0; ConfigDMA.DMA_DIR = DMA_DIR_MemoryToPeripheral; ConfigDMA.DMA_BufferSize = 0; ConfigDMA.DMA_PeripheralInc = DMA_PeripheralInc_Disable; ConfigDMA.DMA_MemoryInc = DMA_MemoryInc_Enable; ConfigDMA.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; ConfigDMA.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; ConfigDMA.DMA_Mode = DMA_Mode_Normal; ConfigDMA.DMA_Priority = DMA_Priority_High; ConfigDMA.DMA_FIFOMode = DMA_FIFOMode_Enable; ConfigDMA.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull; ConfigDMA.DMA_MemoryBurst = DMA_MemoryBurst_Single; ConfigDMA.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream3, &ConfigDMA); stream_cr = DMA2_Stream3->CR; repeat_cr = DMA2_Stream3->CR ^ (DMA_MemoryInc_Enable | DMA_MemoryDataSize_HalfWord); } // Cycle reset pin_clear(OLED_RESET); delay(10); pin_set(OLED_RESET); delay(10); // Do software reset oled_cmd(0x01, 0, 0); delay(10); // Initialization command list uint8_t init_cmds[] = { 0x03, 0x01, 0x00, // Stand-by OFF 0x07, 0x08, 0x00, 0x00, 0x07, 0x0F, 0x01, 0x0C, 0x09, 0x0C, // Set display size 0x05, 0x01, 0x0A, // Set RAM write "direction" 0x06, 0x01, 0x01, // Set RAM scanning "direction" 0x04, 0x01, 0x02, // Set internal oscillator to 90Hz 0x1F, 0x01, 0x30, // Set row scan operations = ALL 0x30, 0x01, 0x12, // Set regulator to ??? 0x1C, 0x01, 0x0F, // Set pre-charge width 0x1E, 0x01, 0x00, // Set peak pulse delay 0x0F, 0x03, 0x0A, 0x0A, 0x0A, // Set peak currents (RGB) 0x1D, 0x03, 0x05, 0x05, 0x05, // Set peak pulse widths (RGB) 0x08, 0x01, 0x01, // Set interface type to 8-bit 0x00 // END }; // Send initializiation commands uint8_t *p_cmds = init_cmds; while(*p_cmds != 0x00) { uint8_t cmd = *p_cmds++; uint8_t args = *p_cmds++; oled_cmd(cmd, args, p_cmds); p_cmds += args; } // Set initial power oled_power(40); // Clear screen uint16_t blank_line[128]; memset(blank_line, 0, sizeof(blank_line)); for(uint8_t y = 0; y < 128; y++) { oled_blit(0, y, 128, 1, blank_line); } // Turn on display uint8_t init_on[] = {0x01}; oled_cmd(0x02, 1, init_on); // Power up LEDs oled_vramp(); }