comparison libs/STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/I2C/EEPROM/main.c @ 0:c59513fd84fb

Initial commit of STM32 test code.
author Daniel O'Connor <darius@dons.net.au>
date Mon, 03 Oct 2011 21:19:15 +1030
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c59513fd84fb
1 /**
2 ******************************************************************************
3 * @file I2C/EEPROM/main.c
4 * @author MCD Application Team
5 * @version V3.5.0
6 * @date 08-April-2011
7 * @brief Main program body
8 ******************************************************************************
9 * @attention
10 *
11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
13 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
17 *
18 * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
19 ******************************************************************************
20 */
21
22 /* Includes ------------------------------------------------------------------*/
23 #include "stm32_eval_i2c_ee.h"
24
25
26 #ifdef USE_STM3210E_EVAL
27 #include "stm3210e_eval_lcd.h"
28 #elif defined(USE_STM3210B_EVAL)
29 #include "stm3210b_eval_lcd.h"
30 #elif defined(USE_STM3210C_EVAL)
31 #include "stm3210c_eval_lcd.h"
32 #elif defined(USE_STM32100B_EVAL)
33 #include "stm32100b_eval_lcd.h"
34 #elif defined(USE_STM32100E_EVAL)
35 #include "stm32100e_eval_lcd.h"
36 #endif /* USE_STM3210E_EVAL */
37
38 /** @addtogroup STM32F10x_StdPeriph_Examples
39 * @{
40 */
41
42 /** @addtogroup I2C_EEPROM
43 * @{
44 */
45
46 /* Private typedef -----------------------------------------------------------*/
47 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
48
49 /* Private define ------------------------------------------------------------*/
50 /* Uncomment the following line to enable using LCD screen for messages display */
51 #define ENABLE_LCD_MSG_DISPLAY
52
53 #define sEE_WRITE_ADDRESS1 0x50
54 #define sEE_READ_ADDRESS1 0x50
55 #define BUFFER_SIZE1 (countof(Tx1_Buffer)-1)
56 #define BUFFER_SIZE2 (countof(Tx2_Buffer)-1)
57 #define sEE_WRITE_ADDRESS2 (sEE_WRITE_ADDRESS1 + BUFFER_SIZE1)
58 #define sEE_READ_ADDRESS2 (sEE_READ_ADDRESS1 + BUFFER_SIZE1)
59
60 /* Private macro -------------------------------------------------------------*/
61 #define countof(a) (sizeof(a) / sizeof(*(a)))
62
63 /* Private variables ---------------------------------------------------------*/
64 uint8_t Tx1_Buffer[] = "/* STM32F10xx I2C Firmware Library EEPROM driver example: \
65 buffer 1 transfer into address sEE_WRITE_ADDRESS1 */ \
66 Example Description \
67 This firmware provides a basic example of how to use the I2C firmware library and\
68 an associate I2C EEPROM driver to communicate with an I2C EEPROM device (here the\
69 example is interfacing with M24C64 EEPROM)\
70 \
71 I2C peripheral is configured in Master transmitter during write operation and in\
72 Master receiver during read operation from I2C EEPROM. \
73 \
74 The peripheral used is I2C1 but can be configured by modifying the defines values\
75 in stm32xxxx_eval.h file. The speed is set to 200kHz and can be configured by \
76 modifying the relative define in stm32_eval_i2c_ee.h file.\
77 \
78 For M24C64 devices all the memory is accessible through the two-bytes \
79 addressing mode and need to define block addresses. In this case, only the physical \
80 address has to be defined (according to the address pins (E0,E1 and E2) connection).\
81 This address is defined in i2c_ee.h (default is 0xA0: E0, E1 and E2 tied to ground).\
82 The EEPROM addresses where the program start the write and the read operations \
83 is defined in the main.c file. \
84 \
85 First, the content of Tx1_Buffer is written to the EEPROM_WriteAddress1 and the\
86 written data are read. The written and the read buffers data are then compared.\
87 Following the read operation, the program waits that the EEPROM reverts to its \
88 Standby state. A second write operation is, then, performed and this time, Tx2_Buffer\
89 is written to EEPROM_WriteAddress2, which represents the address just after the last \
90 written one in the first write. After completion of the second write operation, the \
91 written data are read. The contents of the written and the read buffers are compared.\
92 \
93 All transfers are managed in DMA mode (except when 1-byte read/write operation is\
94 required). Once sEE_ReadBuffer() or sEE_WriteBuffer() function is called, the \
95 use application may perform other tasks in parallel while Read/Write operation is\
96 managed by DMA.\
97 \
98 This example provides the possibility to use the STM32XXXX-EVAL LCD screen for\
99 messages display (transfer status: Ongoing, PASSED, FAILED).\
100 To enable this option uncomment the define ENABLE_LCD_MSG_DISPLAY in the main.c\
101 file. ";
102 uint8_t Tx2_Buffer[] = "/* STM32F10xx I2C Firmware Library EEPROM driver example: \
103 buffer 2 transfer into address sEE_WRITE_ADDRESS2 */";
104 uint8_t Rx1_Buffer[BUFFER_SIZE1], Rx2_Buffer[BUFFER_SIZE2];
105 volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED;
106 volatile uint16_t NumDataRead = 0;
107
108 /* Private functions ---------------------------------------------------------*/
109 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
110
111 /**
112 * @brief Main program
113 * @param None
114 * @retval None
115 */
116 int main(void)
117 {
118 /*!< At this stage the microcontroller clock setting is already configured,
119 this is done through SystemInit() function which is called from startup
120 file (startup_stm32f10x_xx.s) before to branch to application main.
121 To reconfigure the default setting of SystemInit() function, refer to
122 system_stm32f10x.c file
123 */
124
125 #ifdef ENABLE_LCD_MSG_DISPLAY
126 /* Initialize the LCD screen for information display */
127 #ifdef USE_STM3210E_EVAL
128 STM3210E_LCD_Init();
129 #elif defined(USE_STM3210B_EVAL)
130 STM3210B_LCD_Init();
131 #elif defined(USE_STM3210C_EVAL)
132 STM3210C_LCD_Init();
133 #elif defined(USE_STM32100B_EVAL)
134 STM32100B_LCD_Init();
135 #elif defined(USE_STM32100E_EVAL)
136 STM32100E_LCD_Init();
137 #endif /* USE_STM3210E_EVAL */
138
139 /* Display application information */
140 LCD_Clear(LCD_COLOR_BLUE);
141 LCD_SetBackColor(LCD_COLOR_BLUE);
142 LCD_SetTextColor(LCD_COLOR_WHITE);
143 LCD_DisplayStringLine(LCD_LINE_0, "SMT32F1xx FW Library");
144 LCD_DisplayStringLine(LCD_LINE_1, " EEPROM Example ");
145 #endif /* ENABLE_LCD_MSG_DISPLAY */
146
147 /* Initialize the I2C EEPROM driver ----------------------------------------*/
148 sEE_Init();
149
150 /* First write in the memory followed by a read of the written data --------*/
151 /* Write on I2C EEPROM from sEE_WRITE_ADDRESS1 */
152 sEE_WriteBuffer(Tx1_Buffer, sEE_WRITE_ADDRESS1, BUFFER_SIZE1);
153
154 /* Set the Number of data to be read */
155 NumDataRead = BUFFER_SIZE1;
156
157 /* Read from I2C EEPROM from sEE_READ_ADDRESS1 */
158 sEE_ReadBuffer(Rx1_Buffer, sEE_READ_ADDRESS1, (uint16_t *)(&NumDataRead));
159
160 #ifdef ENABLE_LCD_MSG_DISPLAY
161 LCD_DisplayStringLine(LCD_LINE_3, " Transfer 1 Ongoing ");
162 #endif /* ENABLE_LCD_MSG_DISPLAY */
163
164 /* Wait till DMA transfer is complete (Transfer complete interrupt handler
165 resets the variable holding the number of data to be read) */
166 while (NumDataRead > 0)
167 {
168 /* Starting from this point, if the requested number of data is higher than 1,
169 then only the DMA is managing the data transfer. Meanwhile, CPU is free to
170 perform other tasks:
171
172 // Add your code here:
173 //...
174 //...
175
176 For simplicity reasons, this example is just waiting till the end of the
177 transfer. */
178 }
179
180 /* Check if the data written to the memory is read correctly */
181 TransferStatus1 = Buffercmp(Tx1_Buffer, Rx1_Buffer, BUFFER_SIZE1);
182 /* TransferStatus1 = PASSED, if the transmitted and received data
183 to/from the EEPROM are the same */
184 /* TransferStatus1 = FAILED, if the transmitted and received data
185 to/from the EEPROM are different */
186 #ifdef ENABLE_LCD_MSG_DISPLAY
187 if (TransferStatus1 == PASSED)
188 {
189 LCD_DisplayStringLine(LCD_LINE_3, " Transfer 1 PASSED ");
190 }
191 else
192 {
193 LCD_DisplayStringLine(LCD_LINE_3, " Transfer 1 FAILED ");
194 }
195 #endif /* ENABLE_LCD_MSG_DISPLAY */
196
197 /*----------------------------------
198
199 ------------------------------------------*/
200
201 /* Second write in the memory followed by a read of the written data -------*/
202 /* Write on I2C EEPROM from sEE_WRITE_ADDRESS2 */
203 sEE_WriteBuffer(Tx2_Buffer, sEE_WRITE_ADDRESS2, BUFFER_SIZE2);
204
205 /* Set the Number of data to be read */
206 NumDataRead = BUFFER_SIZE2;
207
208 /* Read from I2C EEPROM from sEE_READ_ADDRESS2 */
209 sEE_ReadBuffer(Rx2_Buffer, sEE_READ_ADDRESS2, (uint16_t *)(&NumDataRead));
210
211 #ifdef ENABLE_LCD_MSG_DISPLAY
212 LCD_DisplayStringLine(LCD_LINE_5, " Transfer 2 Ongoing ");
213 #endif /* ENABLE_LCD_MSG_DISPLAY */
214
215 /* Wait till DMA transfer is complete (Transfer complete interrupt handler
216 resets the variable holding the number of data to be read) */
217 while (NumDataRead > 0)
218 {
219 /* Starting from this point, if the requested number of data is higher than 1,
220 then only the DMA is managing the data transfer. Meanwhile, CPU is free to
221 perform other tasks:
222
223 // Add your code here:
224 //...
225 //...
226
227 For simplicity reasons, this example is just waiting till the end of the
228 transfer. */
229 }
230
231 /* Check if the data written to the memory is read correctly */
232 TransferStatus2 = Buffercmp(Tx2_Buffer, Rx2_Buffer, BUFFER_SIZE2);
233 /* TransferStatus2 = PASSED, if the transmitted and received data
234 to/from the EEPROM are the same */
235 /* TransferStatus2 = FAILED, if the transmitted and received data
236 to/from the EEPROM are different */
237 #ifdef ENABLE_LCD_MSG_DISPLAY
238 if (TransferStatus1 == PASSED)
239 {
240 LCD_DisplayStringLine(LCD_LINE_5, " Transfer 2 PASSED ");
241 }
242 else
243 {
244 LCD_DisplayStringLine(LCD_LINE_5, " Transfer 2 FAILED ");
245 }
246 #endif /* ENABLE_LCD_MSG_DISPLAY */
247
248 /* Free all used resources */
249 sEE_DeInit();
250
251 #ifdef ENABLE_LCD_MSG_DISPLAY
252 /* Display end of example information */
253 LCD_DisplayStringLine(LCD_LINE_7, "---End Of Example---");
254 #endif /* ENABLE_LCD_MSG_DISPLAY */
255
256 while (1)
257 {
258 }
259 }
260
261 #ifndef USE_DEFAULT_TIMEOUT_CALLBACK
262 /**
263 * @brief Example of timeout situation management.
264 * @param None.
265 * @retval None.
266 */
267 uint32_t sEE_TIMEOUT_UserCallback(void)
268 {
269 /* Use application may try to recover the communication by resetting I2C
270 peripheral (calling the function I2C_SoftwareResetCmd()) then re-start
271 the transmission/reception from a previously stored recover point.
272 For simplicity reasons, this example only shows a basic way for errors
273 managements which consists of stopping all the process and requiring system
274 reset. */
275
276 #ifdef ENABLE_LCD_MSG_DISPLAY
277 /* Display error message on screen */
278 LCD_Clear(LCD_COLOR_RED);
279 LCD_DisplayStringLine(LCD_LINE_4, "Communication ERROR!");
280 LCD_DisplayStringLine(LCD_LINE_5, "Try again after res-");
281 LCD_DisplayStringLine(LCD_LINE_6, " etting the Board ");
282 #endif /* ENABLE_LCD_MSG_DISPLAY */
283
284 /* Block communication and all processes */
285 while (1)
286 {
287 }
288 }
289
290 #endif /* USE_DEFAULT_TIMEOUT_CALLBACK */
291
292 /**
293 * @brief Compares two buffers.
294 * @param pBuffer1, pBuffer2: buffers to be compared.
295 * @param BufferLength: buffer's length
296 * @retval PASSED: pBuffer1 identical to pBuffer2
297 * FAILED: pBuffer1 differs from pBuffer2
298 */
299 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
300 {
301 while(BufferLength--)
302 {
303 if(*pBuffer1 != *pBuffer2)
304 {
305 return FAILED;
306 }
307
308 pBuffer1++;
309 pBuffer2++;
310 }
311
312 return PASSED;
313 }
314
315 #ifdef USE_FULL_ASSERT
316
317 /**
318 * @brief Reports the name of the source file and the source line number
319 * where the assert_param error has occurred.
320 * @param file: pointer to the source file name
321 * @param line: assert_param error line source number
322 * @retval None
323 */
324 void assert_failed(uint8_t* file, uint32_t line)
325 {
326 /* User can add his own implementation to report the file name and line number,
327 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
328
329 /* Infinite loop */
330 while (1)
331 {
332 }
333 }
334
335 #endif
336
337 /**
338 * @}
339 */
340
341 /**
342 * @}
343 */
344
345 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/