1 | #define MPU6050 ((uint8_t) 0x68)
|
2 |
|
3 | void RCC_Configuration(void) {
|
4 | /* GPIOB clock enable */
|
5 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
|
6 | /* I2C clock enable */
|
7 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
|
8 | }
|
9 |
|
10 |
|
11 | void GPIO_Configuration(void) {
|
12 | GPIO_InitTypeDef GPIO_InitStructure;
|
13 |
|
14 | /* I2C1 PB6 = SCL, PB7 = SDA Pin Configuration */
|
15 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
|
16 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
|
17 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
|
18 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
19 | }
|
20 |
|
21 | void I2C_Configuration(void) {
|
22 | I2C_InitTypeDef I2C_InitStructure;
|
23 |
|
24 | I2C_DeInit(I2C1);
|
25 |
|
26 | I2C_InitStructure.I2C_ClockSpeed = 400000; // 400kHz
|
27 | I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
|
28 | I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
|
29 | I2C_InitStructure.I2C_OwnAddress1 = 0x00;
|
30 | I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
|
31 | I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
|
32 | I2C_Init(I2C1, &I2C_InitStructure);
|
33 |
|
34 | I2C_Cmd(I2C1, ENABLE);
|
35 | }
|
36 |
|
37 |
|
38 | void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction) {
|
39 | trace_puts("i2c start\n");
|
40 | while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
|
41 |
|
42 | I2C_GenerateSTART(I2Cx, ENABLE);
|
43 | while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
|
44 | I2C_Send7bitAddress(I2Cx, address, direction);
|
45 |
|
46 | if (direction == I2C_Direction_Transmitter) {
|
47 | while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
|
48 | }
|
49 | else if (direction == I2C_Direction_Receiver) {
|
50 | while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
|
51 | }
|
52 | }
|
53 |
|
54 | void I2C_write(I2C_TypeDef* I2Cx, uint8_t data) {
|
55 | trace_puts("i2c write\n");
|
56 | I2C_SendData(I2Cx, data);
|
57 | while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
|
58 | }
|
59 |
|
60 | uint8_t I2C_read_nack(I2C_TypeDef* I2Cx) {
|
61 | trace_puts("i2c read nack\n");
|
62 |
|
63 | I2C_AcknowledgeConfig(I2Cx, DISABLE);
|
64 | while ( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED) ) {
|
65 | trace_printf("i2c read nack stuck\n");
|
66 | }
|
67 | uint8_t data = I2C_ReceiveData(I2Cx);
|
68 |
|
69 | return data;
|
70 | }
|
71 |
|
72 | void I2C_stop(I2C_TypeDef* I2Cx) {
|
73 | trace_puts("i2c stop\n");
|
74 |
|
75 | I2C_GenerateSTOP(I2Cx, ENABLE);
|
76 |
|
77 | // Wait until I2C stop condition is finished
|
78 | while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_STOPF));
|
79 | }
|
80 |
|
81 | /* Single-Byte Read Sequence*/
|
82 | uint8_t Read_Reg(uint8_t reg_adr) {
|
83 | I2C_start(I2C1, MPU6050 << 1, I2C_Direction_Transmitter);
|
84 | I2C_write(I2C1, reg_adr);
|
85 | I2C_stop(I2C1);
|
86 |
|
87 | I2C_start(I2C1, MPU6050 << 1, I2C_Direction_Receiver);
|
88 | uint8_t received_data = I2C_read_nack(I2C1);
|
89 | I2C_stop(I2C1);
|
90 | return received_data;
|
91 | }
|
92 |
|
93 | int main(void) {
|
94 | System_Clock_Setup();
|
95 |
|
96 | RCC_Configuration();
|
97 | GPIO_Configuration();
|
98 | I2C_Configuration();
|
99 |
|
100 | trace_puts("Hello\n");
|
101 | Delay(1e5);
|
102 | while (1) {
|
103 | trace_puts("loop begin\n");
|
104 |
|
105 | uint8_t addr = Read_Reg(0x75);
|
106 | trace_printf("addr: %i\n", addr);
|
107 | Delay(1e4);
|
108 | }
|
109 | }
|