stm32f4-discoveryの作法 その7

#概要

stm32f4-discoveryの作法、調べてみた。
i2c scaner、やってみた。
i2c1で、pb6(scl)とpb9(sda)をテスト。

#結果

```
0
I2C CHECK EVENT TRANS MODE TIMEOUT
1
I2C CHECK EVENT TRANS MODE TIMEOUT
2
I2C CHECK EVENT TRANS MODE TIMEOUT
3
I2C CHECK EVENT TRANS MODE TIMEOUT
4
I2C CHECK EVENT TRANS MODE TIMEOUT
5
I2C CHECK EVENT TRANS MODE TIMEOUT
6
I2C CHECK EVENT TRANS MODE TIMEOUT
7
I2C CHECK EVENT TRANS MODE TIMEOUT
8
I2C CHECK EVENT TRANS MODE TIMEOUT
9
I2C CHECK EVENT TRANS MODE TIMEOUT
10
I2C CHECK EVENT TRANS MODE TIMEOUT
11
I2C CHECK EVENT TRANS MODE TIMEOUT
12
I2C CHECK EVENT TRANS MODE TIMEOUT
13
I2C CHECK EVENT TRANS MODE TIMEOUT
14
I2C CHECK EVENT TRANS MODE TIMEOUT
15
I2C CHECK EVENT TRANS MODE TIMEOUT
16
I2C CHECK EVENT TRANS MODE TIMEOUT
17
I2C CHECK EVENT TRANS MODE TIMEOUT
18
I2C CHECK EVENT TRANS MODE TIMEOUT
19
I2C CHECK EVENT TRANS MODE TIMEOUT
20
I2C CHECK EVENT TRANS MODE TIMEOUT
21
I2C CHECK EVENT TRANS MODE TIMEOUT
22
I2C CHECK EVENT TRANS MODE TIMEOUT
23
I2C CHECK EVENT TRANS MODE TIMEOUT
24
I2C CHECK EVENT TRANS MODE TIMEOUT
25
I2C CHECK EVENT TRANS MODE TIMEOUT
26
27
I2C BUSY TIMEOUT
28
I2C CHECK EVENT TRANS MODE TIMEOUT
29
I2C CHECK EVENT TRANS MODE TIMEOUT
30
I2C CHECK EVENT TRANS MODE TIMEOUT
31
I2C CHECK EVENT TRANS MODE TIMEOUT
32
I2C CHECK EVENT TRANS MODE TIMEOUT
33
I2C CHECK EVENT TRANS MODE TIMEOUT
34
I2C CHECK EVENT TRANS MODE TIMEOUT
35
I2C CHECK EVENT TRANS MODE TIMEOUT
36
I2C CHECK EVENT TRANS MODE TIMEOUT
37
I2C CHECK EVENT TRANS MODE TIMEOUT
38
I2C CHECK EVENT TRANS MODE TIMEOUT
39
I2C CHECK EVENT TRANS MODE TIMEOUT
40
I2C CHECK EVENT TRANS MODE TIMEOUT
41
I2C CHECK EVENT TRANS MODE TIMEOUT
42
I2C CHECK EVENT TRANS MODE TIMEOUT
43
I2C CHECK EVENT TRANS MODE TIMEOUT
44
I2C CHECK EVENT TRANS MODE TIMEOUT
45
I2C CHECK EVENT TRANS MODE TIMEOUT
46
I2C CHECK EVENT TRANS MODE TIMEOUT
47
I2C CHECK EVENT TRANS MODE TIMEOUT
48
I2C CHECK EVENT TRANS MODE TIMEOUT
49
I2C CHECK EVENT TRANS MODE TIMEOUT
50
I2C CHECK EVENT TRANS MODE TIMEOUT
51
I2C CHECK EVENT TRANS MODE TIMEOUT
52
I2C CHECK EVENT TRANS MODE TIMEOUT
53
I2C CHECK EVENT TRANS MODE TIMEOUT
54
I2C CHECK EVENT TRANS MODE TIMEOUT
55
I2C CHECK EVENT TRANS MODE TIMEOUT
56
I2C CHECK EVENT TRANS MODE TIMEOUT
57
I2C CHECK EVENT TRANS MODE TIMEOUT
58
I2C CHECK EVENT TRANS MODE TIMEOUT
59
I2C CHECK EVENT TRANS MODE TIMEOUT
60
I2C CHECK EVENT TRANS MODE TIMEOUT
61
I2C CHECK EVENT TRANS MODE TIMEOUT
62
I2C CHECK EVENT TRANS MODE TIMEOUT
63
I2C CHECK EVENT TRANS MODE TIMEOUT
64
I2C CHECK EVENT TRANS MODE TIMEOUT
65
I2C CHECK EVENT TRANS MODE TIMEOUT
66
I2C CHECK EVENT TRANS MODE TIMEOUT
67
I2C CHECK EVENT TRANS MODE TIMEOUT
68
I2C CHECK EVENT TRANS MODE TIMEOUT
69
I2C CHECK EVENT TRANS MODE TIMEOUT
70
I2C CHECK EVENT TRANS MODE TIMEOUT
71
I2C CHECK EVENT TRANS MODE TIMEOUT
72
I2C CHECK EVENT TRANS MODE TIMEOUT
73
I2C CHECK EVENT TRANS MODE TIMEOUT
74
75
I2C BUSY TIMEOUT
76
I2C CHECK EVENT TRANS MODE TIMEOUT
77
I2C CHECK EVENT TRANS MODE TIMEOUT
78
I2C CHECK EVENT TRANS MODE TIMEOUT
79
I2C CHECK EVENT TRANS MODE TIMEOUT
80
I2C CHECK EVENT TRANS MODE TIMEOUT
81
I2C CHECK EVENT TRANS MODE TIMEOUT
82
I2C CHECK EVENT TRANS MODE TIMEOUT
83
I2C CHECK EVENT TRANS MODE TIMEOUT
84
I2C CHECK EVENT TRANS MODE TIMEOUT
85
I2C CHECK EVENT TRANS MODE TIMEOUT
86
I2C CHECK EVENT TRANS MODE TIMEOUT
87
I2C CHECK EVENT TRANS MODE TIMEOUT
88
I2C CHECK EVENT TRANS MODE TIMEOUT
89
I2C CHECK EVENT TRANS MODE TIMEOUT
90
I2C CHECK EVENT TRANS MODE TIMEOUT
91
I2C CHECK EVENT TRANS MODE TIMEOUT
92
I2C CHECK EVENT TRANS MODE TIMEOUT
93
I2C CHECK EVENT TRANS MODE TIMEOUT
94
I2C CHECK EVENT TRANS MODE TIMEOUT
95
I2C CHECK EVENT TRANS MODE TIMEOUT
96
I2C CHECK EVENT TRANS MODE TIMEOUT
97
I2C CHECK EVENT TRANS MODE TIMEOUT
98
I2C CHECK EVENT TRANS MODE TIMEOUT
99
I2C CHECK EVENT TRANS MODE TIMEOUT
100
I2C CHECK EVENT TRANS MODE TIMEOUT
101
I2C CHECK EVENT TRANS MODE TIMEOUT
102
I2C CHECK EVENT TRANS MODE TIMEOUT
103
I2C CHECK EVENT TRANS MODE TIMEOUT
104
I2C CHECK EVENT TRANS MODE TIMEOUT
105
I2C CHECK EVENT TRANS MODE TIMEOUT
106
I2C CHECK EVENT TRANS MODE TIMEOUT
107
I2C CHECK EVENT TRANS MODE TIMEOUT
108
I2C CHECK EVENT TRANS MODE TIMEOUT
109
I2C CHECK EVENT TRANS MODE TIMEOUT
110
I2C CHECK EVENT TRANS MODE TIMEOUT
111
I2C CHECK EVENT TRANS MODE TIMEOUT
112
I2C CHECK EVENT TRANS MODE TIMEOUT
113
I2C CHECK EVENT TRANS MODE TIMEOUT
114
I2C CHECK EVENT TRANS MODE TIMEOUT
115
I2C CHECK EVENT TRANS MODE TIMEOUT
116
I2C CHECK EVENT TRANS MODE TIMEOUT
117
I2C CHECK EVENT TRANS MODE TIMEOUT
118
I2C CHECK EVENT TRANS MODE TIMEOUT
119
I2C CHECK EVENT TRANS MODE TIMEOUT
120
I2C CHECK EVENT TRANS MODE TIMEOUT
121
I2C CHECK EVENT TRANS MODE TIMEOUT
122
I2C CHECK EVENT TRANS MODE TIMEOUT
123
I2C CHECK EVENT TRANS MODE TIMEOUT
124
I2C CHECK EVENT TRANS MODE TIMEOUT
125
I2C CHECK EVENT TRANS MODE TIMEOUT
126
I2C CHECK EVENT TRANS MODE TIMEOUT
127
I2C CHECK EVENT TRANS MODE TIMEOUT


```

#サンプルコード

```
#include <stdio.h>
#include "stm32f4_discovery.h"
#include "printk.h"

#define FLAG_TIMEOUT				 		((uint32_t)0x1000)
#define LONG_TIMEOUT				 		((uint32_t)(10 * FLAG_TIMEOUT))

void delay(__IO uint32_t nCount)
{
	while(nCount--)
	{
	}
}
uint16_t I2C_test(uint16_t d_address)
{
	uint16_t r = 0;
	__IO uint32_t Timeout; 
	Timeout = LONG_TIMEOUT;
	while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)) 
	{
		if ((Timeout--) == 0) 
		{
			I2C_GenerateSTOP(I2C1, ENABLE);
			printk("I2C BUSY TIMEOUT\r\n");
			return 0xffff;
		}
	}
	I2C_GenerateSTART(I2C1, ENABLE);
	Timeout = FLAG_TIMEOUT;
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) 
	{
		if ((Timeout--) == 0) 
		{
			I2C_GenerateSTOP(I2C1, ENABLE);
			printk("I2C CHECK EVENT MASTER MODE TIMEOUT\r\n");
			return 0xffff;
		}
	}		
	I2C_Send7bitAddress(I2C1, (d_address << 1), I2C_Direction_Transmitter);
	Timeout = FLAG_TIMEOUT;
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) 
	{
		if ((Timeout--) == 0) 
		{
			I2C_GenerateSTOP(I2C1, ENABLE);
			printk("I2C CHECK EVENT TRANS MODE TIMEOUT\r\n");
			return 0xffff;
		}
	}
	return r;
}
int main(void)
{
	GPIO_InitTypeDef GPIO_InitStructure2; 
	I2C_InitTypeDef I2C_InitStructure;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
	GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_6;
	GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure2.GPIO_OType = GPIO_OType_OD;
	GPIO_InitStructure2.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(GPIOB, &GPIO_InitStructure2);
	GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_9;
	GPIO_Init(GPIOB, &GPIO_InitStructure2);
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);	
	I2C_DeInit(I2C1);
	I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
	I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
	I2C_InitStructure.I2C_OwnAddress1 = 0x33;
	I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
	I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	I2C_InitStructure.I2C_ClockSpeed = 100000;
	I2C_Cmd(I2C1, ENABLE);
	I2C_Init(I2C1, &I2C_InitStructure);
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 |GPIO_Pin_15;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_Init(GPIOD, &GPIO_InitStructure);
	GPIO_ResetBits(GPIOD, GPIO_Pin_4 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 |GPIO_Pin_15);
	GPIO_SetBits(GPIOD, GPIO_Pin_4);
	static char out[20];
	RCC_ClocksTypeDef RCC_Clocks;
	RCC_GetClocksFreq(&RCC_Clocks);
	SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000000);
	init_usart();
	uint16_t i;
	uint16_t j;
	for (i = 0; i < 128; i++)
	{
		sprintf(out, "%d\r\n", i);
		printk(out);
		j = I2C_test(i);
		delay(0xFFFFFF);
	}
}


```


以上。