Skip to content

Instantly share code, notes, and snippets.

@Azq2
Created March 17, 2019 11:05
Show Gist options
  • Select an option

  • Save Azq2/21183e12c40c1da96707ae30bbc1d438 to your computer and use it in GitHub Desktop.

Select an option

Save Azq2/21183e12c40c1da96707ae30bbc1d438 to your computer and use it in GitHub Desktop.
#include <stm32f10x.h>
#include <math.h>
#include <stdlib.h>
#include "printf.h"
#define EDID_I2C_ADDR 0x50
#define EDID_I2C_CLOCK 100000
static uint8_t monitor_edid[] = {
0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x09, 0xd1, 0x43, 0x78, 0x45, 0x54, 0x00, 0x00, 0x2d, 0x14, 0x01, 0x03, 0x0e, 0x29, 0x17, 0x78, 0x2e, 0x3d, 0x85, 0xa6, 0x56, 0x4a, 0x9a, 0x24, 0x12, 0x50, 0x54, 0xa5, 0x6a, 0x80, 0x71, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x66, 0x21, 0x56, 0xaa, 0x51, 0x00, 0x1e, 0x30, 0x46, 0x8f, 0x33, 0x00, 0x9a, 0xe6, 0x10, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0xff, 0x00, 0x38, 0x42, 0x41, 0x30, 0x32, 0x30, 0x30, 0x31, 0x30, 0x31, 0x39, 0x0a, 0x20, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x32, 0x4c, 0x1e, 0x3f, 0x0e, 0x00, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x42, 0x65, 0x6e, 0x51, 0x20, 0x47, 0x39, 0x32, 0x35, 0x48, 0x44, 0x41, 0x0a, 0x00, 0xd8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x09, 0xd1, 0x43, 0x78, 0x45, 0x54, 0x00, 0x00, 0x2d, 0x14, 0x01, 0x03, 0x0e, 0x29, 0x17, 0x78, 0x2e, 0x3d, 0x85, 0xa6, 0x56, 0x4a, 0x9a, 0x24, 0x12, 0x50, 0x54, 0xa5, 0x6a, 0x80, 0x71, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x66, 0x21, 0x56, 0xaa, 0x51, 0x00, 0x1e, 0x30, 0x46, 0x8f, 0x33, 0x00, 0x9a, 0xe6, 0x10, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0xff, 0x00, 0x38, 0x42, 0x41, 0x30, 0x32, 0x30, 0x30, 0x31, 0x30, 0x31, 0x39, 0x0a, 0x20, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x32, 0x4c, 0x1e, 0x3f, 0x0e, 0x00, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x42, 0x65, 0x6e, 0x51, 0x20, 0x47, 0x39, 0x32, 0x35, 0x48, 0x44, 0x41, 0x0a, 0x00, 0xd8,
};
static int monitor_edid_offset = 0;
static void usart1_init(uint32_t baudrate);
static void init_i2c_slave();
static void uart_putc(void *p, char c);
void main() {
usart1_init(115200);
init_printf(NULL, uart_putc);
init_i2c_slave();
tfp_printf("ohuenno!\r\n");
while (1);
}
void I2C2_EV_IRQHandler() {
switch (I2C_GetLastEvent(I2C2)) {
case I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED:
case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED:
I2C2 ->SR1;
I2C2 ->SR2;
break;
// seek edid eeprom
case I2C_EVENT_SLAVE_BYTE_RECEIVED:
monitor_edid_offset = I2C_ReceiveData(I2C2);
break;
case I2C_EVENT_SLAVE_BYTE_TRANSMITTING:
case I2C_EVENT_SLAVE_BYTE_TRANSMITTED:
I2C_SendData(I2C2, monitor_edid[monitor_edid_offset]);
++monitor_edid_offset;
break;
case I2C_EVENT_SLAVE_ACK_FAILURE:
I2C2 ->SR1 &= 0x00FF;
break;
case I2C_EVENT_SLAVE_STOP_DETECTED:
I2C2 ->SR1;
I2C2->CR1 |= 0x1;
break;
}
}
void I2C2_ER_IRQHandler() {
if ((I2C_ReadRegister(I2C2, I2C_Register_SR1) & 0xFF00) != 0x00)
I2C2->SR1 &= 0x00FF;
}
static void usart1_init(uint32_t baudrate) {
GPIO_InitTypeDef gpio;
USART_InitTypeDef uart;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
gpio.GPIO_Pin = GPIO_Pin_9;
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &gpio);
gpio.GPIO_Pin = GPIO_Pin_10;
gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &gpio);
uart.USART_BaudRate = baudrate;
uart.USART_WordLength = USART_WordLength_8b;
uart.USART_StopBits = USART_StopBits_1;
uart.USART_Parity = USART_Parity_No;
uart.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
uart.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART1, &uart);
USART_Cmd(USART1, ENABLE);
}
static void uart_putc(void *p, char c) {
USART_SendData(USART1, c);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
static void init_i2c_slave() {
GPIO_InitTypeDef gpio;
NVIC_InitTypeDef nvic;
I2C_InitTypeDef i2c;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
gpio.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
gpio.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &gpio);
nvic.NVIC_IRQChannel = I2C2_EV_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 0;
nvic.NVIC_IRQChannelSubPriority = 0;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
nvic.NVIC_IRQChannel = I2C2_ER_IRQn;
NVIC_Init(&nvic);
I2C_ITConfig(I2C2, I2C_IT_EVT, ENABLE);
I2C_ITConfig(I2C2, I2C_IT_BUF, ENABLE);
I2C_ITConfig(I2C2, I2C_IT_ERR, ENABLE);
i2c.I2C_Mode = I2C_Mode_I2C;
i2c.I2C_DutyCycle = I2C_DutyCycle_2;
i2c.I2C_OwnAddress1 = EDID_I2C_ADDR << 1;
i2c.I2C_Ack = I2C_Ack_Enable;
i2c.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
i2c.I2C_ClockSpeed = EDID_I2C_CLOCK;
I2C_Init(I2C2, &i2c);
I2C_StretchClockCmd(I2C2, ENABLE);
I2C_Cmd(I2C2, ENABLE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment