1 | #include "stm32f10x.h"
|
2 |
|
3 | //Konfig fuer Firmware Lib:
|
4 | #include "Firmware Lib\stm32f10x_conf.h"
|
5 |
|
6 | void rcc_config (void)
|
7 | {
|
8 | /* Enable AlternativFunktion */
|
9 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
|
10 | /* Enable GPIOB */
|
11 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
|
12 |
|
13 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
|
14 | }
|
15 |
|
16 | /*=============================================================*/
|
17 |
|
18 | void I2C_config(void){
|
19 |
|
20 | GPIO_InitTypeDef GPIO_InitStructure;
|
21 | NVIC_InitTypeDef NVIC_InitStructure;
|
22 | I2C_InitTypeDef I2C_InitStructure;
|
23 |
|
24 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
|
25 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
|
26 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
27 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
28 |
|
29 | NVIC_InitStructure.NVIC_IRQChannel = I2C1_EV_IRQn;
|
30 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
31 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
32 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
33 | NVIC_Init(&NVIC_InitStructure);
|
34 |
|
35 | NVIC_InitStructure.NVIC_IRQChannel = I2C1_ER_IRQn;
|
36 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
37 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
38 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
39 | NVIC_Init(&NVIC_InitStructure);
|
40 |
|
41 | I2C_DeInit(I2C1);
|
42 |
|
43 | I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
|
44 | I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
|
45 | I2C_InitStructure.I2C_ClockSpeed = 100000;
|
46 | I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
|
47 | I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
|
48 | I2C_InitStructure.I2C_OwnAddress1 = 0;
|
49 | I2C_Init(I2C1, &I2C_InitStructure);
|
50 |
|
51 | I2C_ITConfig(I2C1, I2C_IT_EVT, ENABLE);
|
52 | I2C_ITConfig(I2C1, I2C_IT_BUF, ENABLE);
|
53 | I2C_ITConfig(I2C1, I2C_IT_ERR, ENABLE);
|
54 |
|
55 | I2C_Cmd(I2C1, ENABLE);
|
56 | }
|
57 |
|
58 |
|
59 | /*===============*/
|
60 |
|
61 | I2C_TypeDef * I2C_Module = I2C1;
|
62 | volatile uint8_t deviceAddress;
|
63 | volatile uint8_t i2cBusyFlag=0;
|
64 | volatile uint8_t dataByte0;
|
65 | volatile uint8_t receivedDataByte0;
|
66 | volatile uint8_t i2cDirectionWrite;
|
67 |
|
68 | void i2c_writeByte(uint8_t address, uint8_t byte)
|
69 | {
|
70 | while(i2cBusyFlag){}
|
71 | while(I2C_GetFlagStatus(I2C_Module,I2C_FLAG_BUSY)){}
|
72 |
|
73 | deviceAddress = address;
|
74 | dataByte0 = byte;
|
75 | i2cDirectionWrite = 1;
|
76 | i2cBusyFlag = 1;
|
77 | I2C_GenerateSTART(I2C_Module, ENABLE);
|
78 | }
|
79 |
|
80 |
|
81 | void i2c_readByte(uint8_t address)
|
82 | {
|
83 | while(i2cBusyFlag){}
|
84 | while(I2C_GetFlagStatus(I2C_Module,I2C_FLAG_BUSY)){}
|
85 |
|
86 | deviceAddress = address;
|
87 | i2cDirectionWrite = 0;
|
88 | i2cBusyFlag = 1;
|
89 | I2C_AcknowledgeConfig(I2C_Module, ENABLE);
|
90 | I2C_GenerateSTART(I2C_Module, ENABLE);
|
91 | }
|
92 |
|
93 |
|
94 | // ISR
|
95 | void i2c_handleEventInterrupt(void){
|
96 |
|
97 | if(I2C_GetFlagStatus(I2C_Module, I2C_FLAG_SB) == SET)
|
98 | {
|
99 | if(i2cDirectionWrite){
|
100 | // STM32 Transmitter
|
101 | I2C_Send7bitAddress(I2C_Module, deviceAddress << 1, I2C_Direction_Transmitter); // !!DAS SCHICKT ER NOCH WEG
|
102 |
|
103 | }else{
|
104 | // STM32 Receiver
|
105 | I2C_Send7bitAddress(I2C_Module, deviceAddress << 1, I2C_Direction_Receiver);
|
106 | I2C_AcknowledgeConfig(I2C_Module, DISABLE);
|
107 | I2C_GenerateSTOP(I2C_Module, ENABLE);
|
108 | }
|
109 | }
|
110 |
|
111 | else if(I2C_GetFlagStatus(I2C_Module, I2C_FLAG_ADDR) == SET || I2C_GetFlagStatus(I2C_Module, I2C_FLAG_BTF) == SET)
|
112 | {
|
113 | I2C_ReadRegister(I2C_Module, I2C_Register_SR1);
|
114 | I2C_ReadRegister(I2C_Module, I2C_Register_SR2);
|
115 |
|
116 | if(i2cDirectionWrite==1)
|
117 | {
|
118 | // STM32 Transmitter
|
119 | I2C_SendData(I2C_Module, dataByte0);
|
120 | i2cBusyFlag = 0;
|
121 | i2cDirectionWrite=0;
|
122 | }
|
123 | else
|
124 | {
|
125 | I2C_GenerateSTOP(I2C_Module, ENABLE);
|
126 | i2cBusyFlag = 0;
|
127 | }
|
128 | }
|
129 |
|
130 | else if(I2C_GetFlagStatus(I2C_Module, I2C_FLAG_RXNE) == SET){
|
131 | // STM32 Receiver
|
132 | I2C_ReadRegister(I2C_Module, I2C_Register_SR1);
|
133 | I2C_ReadRegister(I2C_Module, I2C_Register_SR2);
|
134 |
|
135 | receivedDataByte0 = I2C_ReceiveData(I2C_Module);
|
136 | i2cBusyFlag = 0;
|
137 | }
|
138 | }
|
139 |
|
140 |
|
141 |
|
142 | // ISR
|
143 | void i2c_handleErrorInterrupt(void){
|
144 | I2C_GenerateSTOP(I2C_Module, ENABLE);
|
145 | i2cBusyFlag = 0;
|
146 |
|
147 | I2C_ReadRegister(I2C_Module, I2C_Register_SR1);
|
148 | I2C_ReadRegister(I2C_Module, I2C_Register_SR2);
|
149 | I2C_ClearFlag(I2C_Module, I2C_FLAG_AF);
|
150 | I2C_ClearFlag(I2C_Module, I2C_FLAG_ARLO);
|
151 | I2C_ClearFlag(I2C_Module, I2C_FLAG_BERR);
|
152 | I2C_GenerateSTOP(I2C_Module, DISABLE);
|
153 | }
|
154 |
|
155 |
|
156 | void I2C1_EV_IRQHandler(void){
|
157 | i2c_handleEventInterrupt();
|
158 | }
|
159 |
|
160 | void I2C1_ER_IRQHandler(void){
|
161 | i2c_handleErrorInterrupt();
|
162 | }
|
163 |
|
164 |
|
165 |
|
166 | void read (uint8_t gyro, uint8_t SUB)
|
167 | {
|
168 | i2c_writeByte(gyro, SUB); //Schreiben - welches Register gelesen werden soll
|
169 |
|
170 | i2c_readByte(gyro); //Lesen - Byte vom Register auslesen
|
171 | }
|
172 |
|
173 |
|
174 | /*======================== MAIN =======================*/
|
175 | int main (void)
|
176 | {
|
177 | rcc_config();
|
178 | I2C_config();
|
179 |
|
180 | read (0x69,0x0F); //Test - Anfrage: Who_am_i
|
181 |
|
182 | while(1)
|
183 | {}
|
184 | }
|