📄 stm32f10x_i2c.lst
字号:
###############################################################################
# #
# 30/Jul/2008 11:02:20 #
# IAR ARM ANSI C/C++ Compiler V5.11.0.20622/W32 EVALUATION #
# Copyright 1999-2007 IAR Systems. All rights reserved. #
# #
# Cpu mode = thumb #
# Endian = little #
# Source file = E:\library\src\stm32f10x_i2c.c #
# Command line = E:\library\src\stm32f10x_i2c.c -D EMB_FLASH -lcN #
# E:\ELE\yten\pro\Release\List\ -o #
# E:\ELE\yten\pro\Release\Obj\ --no_cse --no_unroll #
# --no_inline --no_code_motion --no_tbaa --no_clustering #
# --no_scheduling --debug --endian little --cpu Cortex-M3 #
# -e --fpu None --dlib_config "C:\Program Files\IAR #
# Systems\Embedded Workbench 5.0 #
# Evaluation\ARM\INC\DLib_Config_Normal.h" -I #
# E:\ELE\yten\pro\ -I E:\ELE\yten\pro\..\LIBRARY\INC\ -I #
# "C:\Program Files\IAR Systems\Embedded Workbench 5.0 #
# Evaluation\ARM\INC\" -On #
# List file = E:\ELE\yten\pro\Release\List\stm32f10x_i2c.lst #
# Object file = E:\ELE\yten\pro\Release\Obj\stm32f10x_i2c.o #
# #
# #
###############################################################################
E:\library\src\stm32f10x_i2c.c
1 /******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
2 * File Name : stm32f10x_i2c.c
3 * Author : MCD Application Team
4 * Version : V1.0
5 * Date : 10/08/2007
6 * Description : This file provides all the I2C firmware functions.
7 ********************************************************************************
8 * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
9 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
10 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
11 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
12 * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
13 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
14 *******************************************************************************/
15
16 /* Includes ------------------------------------------------------------------*/
17 #include "stm32f10x_i2c.h"
18 #include "stm32f10x_rcc.h"
19
20 /* Private typedef -----------------------------------------------------------*/
21 /* Private define ------------------------------------------------------------*/
22 /* I2C SPE mask */
23 #define CR1_PE_Set ((u16)0x0001)
24 #define CR1_PE_Reset ((u16)0xFFFE)
25
26 /* I2C DMAEN mask */
27 #define CR2_DMAEN_Set ((u16)0x0800)
28 #define CR2_DMAEN_Reset ((u16)0xF7FF)
29
30 /* I2C LAST mask */
31 #define CR2_LAST_Set ((u16)0x1000)
32 #define CR2_LAST_Reset ((u16)0xEFFF)
33
34 /* I2C START mask */
35 #define CR1_START_Set ((u16)0x0100)
36 #define CR1_START_Reset ((u16)0xFEFF)
37
38 /* I2C STOP mask */
39 #define CR1_STOP_Set ((u16)0x0200)
40 #define CR1_STOP_Reset ((u16)0xFDFF)
41
42 /* I2C ACK mask */
43 #define CR1_ACK_Set ((u16)0x0400)
44 #define CR1_ACK_Reset ((u16)0xFBFF)
45
46 /* I2C ENGC mask */
47 #define CR1_ENGC_Set ((u16)0x0040)
48 #define CR1_ENGC_Reset ((u16)0xFFBF)
49
50 /* I2C ADD0 mask */
51 #define OAR1_ADD0_Set ((u16)0x0001)
52 #define OAR1_ADD0_Reset ((u16)0xFFFE)
53
54 /* I2C SWRST mask */
55 #define CR1_SWRST_Set ((u16)0x8000)
56 #define CR1_SWRST_Reset ((u16)0x7FFF)
57
58 /* I2C PEC mask */
59 #define CR1_PEC_Set ((u16)0x1000)
60 #define CR1_PEC_Reset ((u16)0xEFFF)
61
62 /* I2C ENPEC mask */
63 #define CR1_ENPEC_Set ((u16)0x0020)
64 #define CR1_ENPEC_Reset ((u16)0xFFDF)
65
66 /* I2C ENARP mask */
67 #define CR1_ENARP_Set ((u16)0x0010)
68 #define CR1_ENARP_Reset ((u16)0xFFEF)
69
70 /* I2C NOSTRETCH mask */
71 #define CR1_NOSTRETCH_Set ((u16)0x0080)
72 #define CR1_NOSTRETCH_Reset ((u16)0xFF7F)
73
74 /* I2C ENDUAL mask */
75 #define OAR2_ENDUAL_Set ((u16)0x0001)
76 #define OAR2_ENDUAL_Reset ((u16)0xFFFE)
77
78 /* I2C F/S mask */
79 #define CCR_FS_Set ((u16)0x8000)
80
81 /* I2C ADD2 mask */
82 #define OAR2_ADD2_Reset ((u16)0xFF01)
83
84 /* I2C FREQ mask */
85 #define CR2_FREQ_Reset ((u16)0xFFC0)
86
87 /* I2C CCR mask */
88 #define CCR_CCR_Set ((u16)0x0FFF)
89
90 /* I2C FLAG mask */
91 #define I2C_FLAG_Mask ((u32)0x00FFFFFF)
92
93 /* I2C registers Masks */
94 #define CR1_CLEAR_Mask ((u16)0xFBF5)
95
96 /* Private macro -------------------------------------------------------------*/
97 /* Private variables ---------------------------------------------------------*/
98 /* Private function prototypes -----------------------------------------------*/
99 /* Private functions ---------------------------------------------------------*/
100
101 /*******************************************************************************
102 * Function Name : I2C_DeInit
103 * Description : Deinitializes the I2Cx peripheral registers to their default
104 * reset values.
105 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
106 * Output : None
107 * Return : None
108 *******************************************************************************/
109 void I2C_DeInit(I2C_TypeDef* I2Cx)
110 {
111 switch (*(u32*)&I2Cx)
112 {
113 case I2C1_BASE:
114 /* Enable I2C1 reset state */
115 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
116 /* Release I2C1 from reset state */
117 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
118 break;
119
120 case I2C2_BASE:
121 /* Enable I2C2 reset state */
122 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
123 /* Release I2C2 from reset state */
124 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
125 break;
126
127 default:
128 break;
129 }
130 }
131
132 /*******************************************************************************
133 * Function Name : I2C_Init
134 * Description : Initializes the I2Cx peripheral according to the specified
135 * parameters in the I2C_InitStruct.
136 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
137 * - I2C_InitStruct: pointer to a I2C_InitTypeDef structure that
138 * contains the configuration information for the specified
139 * I2C peripheral.
140 * Output : None
141 * Return : None
142 ******************************************************************************/
143 void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct)
144 {
145 u16 tmpreg = 0, freqrange = 0;
146 u16 result = 0x04;
147 u32 pclk1clock = 12000000;
148 RCC_ClocksTypeDef RCC_Clocks;
149
150 /* Check the parameters */
151 assert_param(IS_I2C_MODE(I2C_InitStruct->I2C_Mode));
152 assert_param(IS_I2C_DUTY_CYCLE(I2C_InitStruct->I2C_DutyCycle));
153 assert_param(IS_I2C_OWN_ADDRESS1(I2C_InitStruct->I2C_OwnAddress1));
154 assert_param(IS_I2C_ACK_STATE(I2C_InitStruct->I2C_Ack));
155 assert_param(IS_I2C_ACKNOWLEDGE_ADDRESS(I2C_InitStruct->I2C_AcknowledgedAddress));
156 assert_param(IS_I2C_CLOCK_SPEED(I2C_InitStruct->I2C_ClockSpeed));
157
158 /*---------------------------- I2Cx CR2 Configuration ------------------------*/
159 /* Get the I2Cx CR2 value */
160 tmpreg = I2Cx->CR2;
161 /* Clear frequency FREQ[5:0] bits */
162 tmpreg &= CR2_FREQ_Reset;
163 /* Get PCLK1Clock frequency value */
164 RCC_GetClocksFreq(&RCC_Clocks);
165 pclk1clock = RCC_Clocks.PCLK1_Frequency;
166 /* Set frequency bits depending on PCLK1Clock value */
167 freqrange = (u16)(pclk1clock / 1000000);
168 tmpreg |= freqrange;
169 /* Write to I2Cx CR2 */
170 I2Cx->CR2 = tmpreg;
171
172 /*---------------------------- I2Cx CCR Configuration ------------------------*/
173 /* Disable I2Cx to configure TRISE */
174 I2C_Cmd(I2Cx, DISABLE);
175
176 /* Reset tmpreg value */
177 /* Clear F/S, DUTY and CCR[11:0] bits */
178 tmpreg = 0;
179
180 /* Configure speed in standard mode */
181 if (I2C_InitStruct->I2C_ClockSpeed <= 100000)
182 {
183 /* Standard mode speed calculate */
184 result = (u16)(pclk1clock / (I2C_InitStruct->I2C_ClockSpeed << 1));
185 /* Test if CCR value is under 0x4*/
186 if (result < 0x04)
187 {
188 /* Set minimum allowed value */
189 result = 0x04;
190 }
191 /* Set speed value for standard mode */
192 tmpreg |= result;
193 /* Set Maximum Rise Time: ((1000/(1000000000/pclk1clock))+1 */
194 I2Cx->TRISE = freqrange + 1;
195 }
196 /* Configure speed in fast mode */
197 else /*(I2C_InitStruct->I2C_ClockSpeed <= 400000)*/
198 {
199 if (I2C_InitStruct->I2C_DutyCycle == I2C_DutyCycle_2)
200 {
201 /* Fast mode speed calculate: Tlow/Thigh = 2 */
202 result = (u16)(pclk1clock / (I2C_InitStruct->I2C_ClockSpeed * 3));
203 }
204 else /*I2C_InitStruct->I2C_DutyCycle == I2C_DutyCycle_16_9*/
205 {
206 /* Fast mode speed calculate: Tlow/Thigh = 16/9 */
207 result = (u16)(pclk1clock / (I2C_InitStruct->I2C_ClockSpeed * 25));
208 /* Set DUTY bit */
209 result |= I2C_DutyCycle_16_9;
210 }
211 /* Test if CCR value is under 0x1*/
212 if ((result & CCR_CCR_Set) == 0)
213 {
214 /* Set minimum allowed value */
215 result |= (u16)0x0001;
216 }
217 /* Set speed value and set F/S bit for fast mode */
218 tmpreg |= result | CCR_FS_Set;
219 /* Set Maximum Rise Time: ((300/(1000000000/pclk1clock))+1 */
220 I2Cx->TRISE = (u16)(((freqrange * 300) / 1000) + 1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -