1 | I2C_HandleTypeDef hi2c1;
|
2 | DMA_HandleTypeDef hdma_i2c1_rx;
|
3 | DMA_HandleTypeDef hdma_i2c1_tx;
|
4 |
|
5 | static void MX_GPIO_Init(void);
|
6 | static void MX_DMA_Init(void);
|
7 | static void MX_I2C1_Init(void);
|
8 |
|
9 | int main(void)
|
10 | {
|
11 |
|
12 | HAL_Init();
|
13 |
|
14 | SystemClock_Config();
|
15 |
|
16 | MX_GPIO_Init();
|
17 | MX_DMA_Init();
|
18 | MX_I2C1_Init();
|
19 |
|
20 | // Enable MPU6050 and use Gyro PLL as clock source
|
21 | uint8_t buf = MPU6050_CLOCK_PLL_XGYRO;
|
22 | if (HAL_I2C_Mem_Write_DMA(&hi2c1, MPU6050_ADDRESS_AD0_LOW << 1, MPU6050_RA_PWR_MGMT_1, I2C_MEMADD_SIZE_8BIT, &buf, 1) != HAL_OK) {
|
23 | Error_Handler();
|
24 | }
|
25 |
|
26 | // (Hier kommt noch mehr Code...)
|
27 | }
|
28 |
|
29 | /* I2C1 init function */
|
30 | static void MX_I2C1_Init(void)
|
31 | {
|
32 | hi2c1.Instance = I2C1;
|
33 | hi2c1.Init.ClockSpeed = 100000;
|
34 | hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
|
35 | hi2c1.Init.OwnAddress1 = 0;
|
36 | hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
37 | hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
|
38 | hi2c1.Init.OwnAddress2 = 0;
|
39 | hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
|
40 | hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
41 |
|
42 | /* Peripheral clock enable */
|
43 | if (HAL_I2C_Init(&hi2c1) != HAL_OK)
|
44 | {
|
45 | Error_Handler();
|
46 | }
|
47 |
|
48 | }
|
49 |
|
50 | static void MX_DMA_Init(void)
|
51 | {
|
52 | __HAL_RCC_DMA1_CLK_ENABLE();
|
53 |
|
54 | /* DMA interrupt init */
|
55 | /* DMA1_Channel6_IRQn interrupt configuration */
|
56 | HAL_NVIC_SetPriority(DMA1_Channel6_IRQn, 0, 0);
|
57 | HAL_NVIC_EnableIRQ(DMA1_Channel6_IRQn);
|
58 |
|
59 | /* DMA1_Channel7_IRQn interrupt configuration */
|
60 | HAL_NVIC_SetPriority(DMA1_Channel7_IRQn, 0, 0);
|
61 | HAL_NVIC_EnableIRQ(DMA1_Channel7_IRQn);
|
62 | }
|
63 |
|
64 | void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
|
65 | {
|
66 |
|
67 | GPIO_InitTypeDef GPIO_InitStruct;
|
68 | if(hi2c->Instance==I2C1)
|
69 | {
|
70 | /**I2C1 GPIO Configuration
|
71 | PB6 ------> I2C1_SCL
|
72 | PB7 ------> I2C1_SDA
|
73 | */
|
74 | GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
|
75 | GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
|
76 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
77 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
78 |
|
79 | /* Peripheral clock enable */
|
80 |
|
81 | __HAL_RCC_I2C1_CLK_ENABLE();
|
82 | hdma_i2c1_rx.Instance = DMA1_Channel7;
|
83 | hdma_i2c1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
|
84 | hdma_i2c1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
|
85 | hdma_i2c1_rx.Init.MemInc = DMA_MINC_ENABLE;
|
86 | hdma_i2c1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
87 | hdma_i2c1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
88 | hdma_i2c1_rx.Init.Mode = DMA_NORMAL;
|
89 | hdma_i2c1_rx.Init.Priority = DMA_PRIORITY_LOW;
|
90 |
|
91 | if (HAL_DMA_Init(&hdma_i2c1_rx) != HAL_OK) {
|
92 | Error_Handler();
|
93 | }
|
94 |
|
95 | __HAL_LINKDMA(hi2c,hdmarx,hdma_i2c1_rx);
|
96 |
|
97 | hdma_i2c1_tx.Instance = DMA1_Channel6;
|
98 | hdma_i2c1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
|
99 | hdma_i2c1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
|
100 | hdma_i2c1_tx.Init.MemInc = DMA_MINC_ENABLE;
|
101 | hdma_i2c1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
102 | hdma_i2c1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
103 | hdma_i2c1_tx.Init.Mode = DMA_NORMAL;
|
104 | hdma_i2c1_tx.Init.Priority = DMA_PRIORITY_LOW;
|
105 |
|
106 | if (HAL_DMA_Init(&hdma_i2c1_tx) != HAL_OK) {
|
107 | Error_Handler();
|
108 | }
|
109 |
|
110 | __HAL_LINKDMA(hi2c,hdmatx,hdma_i2c1_tx);
|
111 |
|
112 | }
|
113 |
|
114 | }
|