1 | void
|
2 | spi2_config (void) {
|
3 |
|
4 | GPIO_InitTypeDef GPIO_InitStructure;
|
5 |
|
6 | /* Configure SPI2 pins: SCK, MISO and MOSI ---------------------------------*/
|
7 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
|
8 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
9 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
10 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
11 |
|
12 | /* Configure Slave Select Pin --------------------------------------------*/
|
13 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
|
14 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
15 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
16 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
17 |
|
18 | // set slave select to high
|
19 | GPIO_WriteBit(GPIOB, GPIO_Pin_12, 1);
|
20 | }
|
21 |
|
22 | void
|
23 | spi2_init (void) {
|
24 |
|
25 | SPI_I2S_DeInit(SPI2);
|
26 | spi2_config ();
|
27 |
|
28 | SPI_InitTypeDef SPI_InitStructure;
|
29 |
|
30 | /* SPI2 configuration ------------------------------------------------------*/
|
31 | SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
32 | SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
|
33 | SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
|
34 | SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
|
35 | SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
|
36 | SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
|
37 | SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
|
38 | SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
|
39 | SPI_Init(SPI2, &SPI_InitStructure);
|
40 |
|
41 | SPI_CalculateCRC(SPI2, DISABLE);
|
42 | SPI_Cmd(SPI2, ENABLE);
|
43 | }
|
44 |
|
45 |
|
46 | ///////////////////////////////////////////////////////////////////////////////
|
47 | // enable FRAM
|
48 | ///////////////////////////////////////////////////////////////////////////////
|
49 | void
|
50 | FRAM_enable (void) {
|
51 | //SPI_SSOutputCmd(SPI2, ENABLE);
|
52 | GPIO_WriteBit(GPIOB, GPIO_Pin_12, 0);
|
53 | }
|
54 |
|
55 | ///////////////////////////////////////////////////////////////////////////////
|
56 | // disable FRAM
|
57 | ///////////////////////////////////////////////////////////////////////////////
|
58 | void
|
59 | FRAM_disable (void) {
|
60 | //SPI_SSOutputCmd(SPI2, DISABLE);
|
61 | GPIO_WriteBit(GPIOB, GPIO_Pin_12, 1);
|
62 | }
|
63 |
|
64 | ///////////////////////////////////////////////////////////////////////////////
|
65 | // initialize FRAM
|
66 | ///////////////////////////////////////////////////////////////////////////////
|
67 | void
|
68 | FRAM_init (void) {
|
69 |
|
70 | // set write enable latch
|
71 | while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE)==RESET);
|
72 |
|
73 | FRAM_enable();
|
74 | SPI_I2S_SendData(SPI2, 0x06);
|
75 | FRAM_disable();
|
76 | }
|
77 |
|
78 | ///////////////////////////////////////////////////////////////////////////////
|
79 | // Write Byte to Fram
|
80 | ///////////////////////////////////////////////////////////////////////////////
|
81 | void
|
82 | FRAM_write_byte (unsigned char b) {
|
83 |
|
84 | while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE)==RESET);
|
85 | SPI_I2S_SendData(SPI2, b);
|
86 | }
|
87 |
|
88 | ///////////////////////////////////////////////////////////////////////////////
|
89 | // Write Byte to Fram
|
90 | ///////////////////////////////////////////////////////////////////////////////
|
91 | unsigned char
|
92 | FRAM_read_byte (void) {
|
93 |
|
94 | while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE)==RESET);
|
95 | return SPI_I2S_ReceiveData(SPI2);
|
96 | }
|
97 |
|
98 |
|
99 |
|
100 | void
|
101 | FRAM_Test () {
|
102 |
|
103 | unsigned char mod_byte= 2;
|
104 | unsigned int limit = 0;
|
105 | unsigned char byte = 0;
|
106 |
|
107 | // write data to fram
|
108 | FRAM_enable();
|
109 |
|
110 | // write address -> start address is 0
|
111 | FRAM_write_byte (0x02); // write memory data command
|
112 | FRAM_write_byte (0x00); // addr
|
113 | FRAM_write_byte (0x00); // addr
|
114 | FRAM_write_byte (0x00); // addr
|
115 |
|
116 | for (limit = 0; limit < 200; limit++) {
|
117 | if (mod_byte % 2) {
|
118 | FRAM_write_byte (0xAA);
|
119 | }
|
120 | else {
|
121 | FRAM_write_byte (0x55);
|
122 | }
|
123 | mod_byte++;
|
124 | }
|
125 |
|
126 | FRAM_disable();
|
127 |
|
128 | // read data from fram
|
129 | FRAM_enable();
|
130 | printf("\r\nread data from FRAM\r\n");
|
131 | FRAM_write_byte (0x03); // read memory data command
|
132 | FRAM_write_byte (0x00); // addr
|
133 | FRAM_write_byte (0x00); // addr
|
134 | FRAM_write_byte (0x00); // addr
|
135 |
|
136 | for (limit = 0; limit < 200; limit++) {
|
137 | byte = FRAM_read_byte();
|
138 | printf("%#X ", byte); // nach 0x55 0x55 0x55 läuft der uC nicht weiter
|
139 | }
|
140 | FRAM_disable();
|
141 | /**/
|
142 | }
|