comparison hw.c @ 18:afdd22502c2a

Add i2c test code
author Daniel O'Connor <darius@dons.net.au>
date Wed, 14 Nov 2012 12:30:58 +1030
parents 96c345d304af
children 1e2fa7396f98
comparison
equal deleted inserted replaced
17:b62eaa3131b1 18:afdd22502c2a
2 #include <stdio.h> 2 #include <stdio.h>
3 #include "stm32f10x.h" 3 #include "stm32f10x.h"
4 4
5 #include "1wire.h" 5 #include "1wire.h"
6 #include "lcd.h" 6 #include "lcd.h"
7
8 #define I2C_TIMEOUT 10000
7 9
8 static void hw_port_cfg(void); 10 static void hw_port_cfg(void);
9 11
10 /* Wait for cnt microseconds */ 12 /* Wait for cnt microseconds */
11 void 13 void
24 lcd_setpwm(1000); 26 lcd_setpwm(1000);
25 } 27 }
26 28
27 static void 29 static void
28 hw_port_cfg(void) { 30 hw_port_cfg(void) {
29 GPIO_InitTypeDef GPIO_InitStructure;
30 USART_InitTypeDef USART_InitStructure;
31 SPI_InitTypeDef SPI_InitStructure;
32 FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure; 31 FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
33 FSMC_NORSRAMTimingInitTypeDef p; 32 FSMC_NORSRAMTimingInitTypeDef p;
33 GPIO_InitTypeDef GPIO_InitStructure;
34 I2C_InitTypeDef I2C_InitStructure;
35 SPI_InitTypeDef SPI_InitStructure;
34 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 36 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
35 37 USART_InitTypeDef USART_InitStructure;
36 /* RTC stuff */ 38
37 /* Enable PWR and BKP clocks */ 39 /* Enable clocks */
38 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); 40 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP | RCC_APB1Periph_TIM4 | RCC_APB1Periph_I2C1,
41 ENABLE);
42
43 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOB |
44 RCC_APB2Periph_GPIOE | RCC_APB2Periph_SPI1, ENABLE);
45
46 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
39 47
40 /* Allow access to BKP Domain */ 48 /* Allow access to BKP Domain */
41 PWR_BackupAccessCmd(ENABLE); 49 PWR_BackupAccessCmd(ENABLE);
42 50
43 /* Reset Backup Domain 51 /* Reset Backup Domain
72 RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */ 80 RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
73 81
74 /* Wait until last write operation on RTC registers has finished */ 82 /* Wait until last write operation on RTC registers has finished */
75 RTC_WaitForLastTask(); 83 RTC_WaitForLastTask();
76 84
77 /* Clock setup */
78 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
79
80 /* Port configuration */ 85 /* Port configuration */
81 /* Configure USART1 TX (PA.09) as alternate function push-pull */ 86 /* Configure USART1 TX (PA.09) as alternate function push-pull */
82 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; 87 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
83 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 88 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
84 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 89 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
86 91
87 /* Configure USART1 RX (PA.10) as input floating */ 92 /* Configure USART1 RX (PA.10) as input floating */
88 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; 93 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
89 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 94 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
90 GPIO_Init(GPIOA, &GPIO_InitStructure); 95 GPIO_Init(GPIOA, &GPIO_InitStructure);
91
92 /* Enable GPIOB clock */
93 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
94 96
95 /* Configure PB5 as output push-pull for LED */ 97 /* Configure PB5 as output push-pull for LED */
96 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; 98 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
97 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 99 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
98 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 100 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
119 121
120 /* Enable USART */ 122 /* Enable USART */
121 USART_Cmd(USART1, ENABLE); 123 USART_Cmd(USART1, ENABLE);
122 124
123 /* Enable FSMC clock */ 125 /* Enable FSMC clock */
124 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
125
126 /* Enable alternate function IO clock */
127 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
128
129 /* Enable GPIOD clock */
130 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
131
132 /* Enable GPIOD clock */
133 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
134 126
135 /* Configures LCD Control lines (FSMC Pins) in alternate function Push-Pull mode. 127 /* Configures LCD Control lines (FSMC Pins) in alternate function Push-Pull mode.
136 * 128 *
137 * PD0(D2), PD1(D3), PD4(NOE), PD5(NWE), PD7(NE1/CS), PD8(D13), PD9(D14), 129 * PD0(D2), PD1(D3), PD4(NOE), PD5(NWE), PD7(NE1/CS), PD8(D13), PD9(D14),
138 * PD10(D15), PD11(A16/RS) PD14(D0), PD15(D1) 130 * PD10(D15), PD11(A16/RS) PD14(D0), PD15(D1)
151 GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | 143 GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 |
152 GPIO_Pin_15); 144 GPIO_Pin_15);
153 GPIO_Init(GPIOE, &GPIO_InitStructure); 145 GPIO_Init(GPIOE, &GPIO_InitStructure);
154 146
155 /* Configure backlight control (PD13/FSMC_A18 remapped to TIM4_CH2) */ 147 /* Configure backlight control (PD13/FSMC_A18 remapped to TIM4_CH2) */
156 /* Enable TIM4 clock */
157 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
158 148
159 /* Enable timer function 149 /* Enable timer function
160 * Note source clock is SYSCLK / 2 = 36MHz 150 * Note source clock is SYSCLK / 2 = 36MHz
161 */ 151 */
162 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; 152 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
232 * 222 *
233 * The touch screen is selected with PB7. 223 * The touch screen is selected with PB7.
234 * The flash chip is selected with PA4. 224 * The flash chip is selected with PA4.
235 */ 225 */
236 226
237 /* Enable SPI1 clock */
238 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
239
240 /* Configure MOSI, MISO and SCLK as alternate function PP */ 227 /* Configure MOSI, MISO and SCLK as alternate function PP */
241 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; 228 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
242 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 229 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
243 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 230 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
244 GPIO_Init(GPIOA, &GPIO_InitStructure); 231 GPIO_Init(GPIOA, &GPIO_InitStructure);
273 260
274 /* SPI1 enable */ 261 /* SPI1 enable */
275 SPI_Cmd(SPI1, ENABLE); 262 SPI_Cmd(SPI1, ENABLE);
276 263
277 /* Configure TIM6 for interval timing */ 264 /* Configure TIM6 for interval timing */
278 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
279
280 /* Reset TIM6 */ 265 /* Reset TIM6 */
281 TIM_DeInit(TIM6); 266 TIM_DeInit(TIM6);
282 267
283 /* Time Base configuration */ 268 /* Time Base configuration */
284 TIM_TimeBaseStructure.TIM_Period = 0; 269 TIM_TimeBaseStructure.TIM_Period = 0;
289 274
290 /* Setup for single pulse mode clear UDIS */ 275 /* Setup for single pulse mode clear UDIS */
291 TIM_SelectOnePulseMode(TIM6, TIM_OPMode_Single); 276 TIM_SelectOnePulseMode(TIM6, TIM_OPMode_Single);
292 TIM_UpdateDisableConfig(TIM6, DISABLE); 277 TIM_UpdateDisableConfig(TIM6, DISABLE);
293 278
294 /* Setup GPIO for 1-wire */ 279 /* Setup GPIO for delay test & 1-wire
280 * PE2 -> pin 3 on header
281 * PE3 -> pin 4 on header */
295 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; 282 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
296 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 283 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
297 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 284 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
298 GPIO_Init(GPIOE, &GPIO_InitStructure); 285 GPIO_Init(GPIOE, &GPIO_InitStructure);
299 286
308 GPIO_SetBits(GPIOE, GPIO_Pin_2); 295 GPIO_SetBits(GPIOE, GPIO_Pin_2);
309 _usleep16(30); 296 _usleep16(30);
310 GPIO_ResetBits(GPIOE, GPIO_Pin_2); 297 GPIO_ResetBits(GPIOE, GPIO_Pin_2);
311 _usleep16(100); 298 _usleep16(100);
312 } 299 }
313 #endif 300 #endif
301
302 /* Setup I2C bus */
303
304 /* Configure SCL/SDA pins */
305 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
306 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
307 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
308 GPIO_Init(GPIOB, &GPIO_InitStructure);
309
310 /* Reset I2C1 */
311 I2C_DeInit(I2C1);
312
313 /* Setup I2C1 */
314 I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
315 I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
316 I2C_InitStructure.I2C_OwnAddress1 = 0x00;
317 I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
318 I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
319 I2C_InitStructure.I2C_ClockSpeed = 100000;
320 I2C_Init(I2C1, &I2C_InitStructure);
321
322 /* Start I2C controller */
323 I2C_Cmd(I2C1, ENABLE);
324
325 #if 0
326 uint32_t I2C_TimeOut = I2C_TIMEOUT;
327
328 /* Clear the I2C1 AF flag */
329 I2C_ClearFlag(I2C1, I2C_FLAG_AF);
330
331 /* Enable I2C1 acknowledgement if it is already disabled by other function */
332 I2C_AcknowledgeConfig(I2C1, ENABLE);
333
334 /*---------------------------- Transmission Phase ---------------------------*/
335
336 /* Send I2C1 START condition */
337 I2C_GenerateSTART(I2C1, ENABLE);
338
339 /*!< Test on I2C1 EV5 and clear it */
340 while ((!I2C_GetFlagStatus(I2C1, I2C_FLAG_SB)) && I2C_TimeOut) {
341 I2C_TimeOut--;
342 }
343
344 if (I2C_TimeOut == 0) {
345 return ERROR;
346 }
347
348 I2C_TimeOut = I2C_TIMEOUT;
349 /* Send STLM75 slave address for write */
350 I2C_Send7bitAddress(I2C1, LM75_ADDR, I2C_Direction_Transmitter);
351
352 while ((!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) && I2C_TimeOut) {
353 I2C_TimeOut--;
354 }
355
356 if ((I2C_GetFlagStatus(LM75_I2C, I2C_FLAG_AF) != 0x00) || (I2C_TimeOut == 0)) {
357 return ERROR;
358 } else {
359 return SUCCESS;
360 }
361
362 #endif
314 } 363 }