1 | #include <stdint.h>
|
2 |
|
3 | #include <avr/io.h>
|
4 |
|
5 | #include <avr/pgmspace.h>
|
6 |
|
7 | //mtE
|
8 |
|
9 |
|
10 |
|
11 | #include "dataflash.h"
|
12 |
|
13 |
|
14 |
|
15 | const uint8_t DF_pagebits[] PROGMEM ={ 9, 9, 9, 9, 9, 10, 10, 11}; //index of internal page address bits
|
16 |
|
17 | const uint16_t DF_pagesize[] PROGMEM ={264,264, 264, 264, 264, 528, 528,1056}; //index of pagesizes
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | unsigned char PageBits = 0;
|
26 |
|
27 | unsigned int PageSize = 0;
|
28 |
|
29 |
|
30 |
|
31 | void DF_SPI_init (void)
|
32 |
|
33 | {
|
34 |
|
35 | PORTB |= (1<<PB1) | (1<<PB3) | (1<<PB4) | (1<<PB5);
|
36 |
|
37 | DDRB |= (1<<DDB1) | (1<<DDB3) | (1<<DDB5); //Set MOSI, SCK AND SS as outputs
|
38 |
|
39 | // mtE
|
40 |
|
41 | SPSR = (1<<SPI2X); //SPI double speed settings
|
42 |
|
43 | SPCR = (1<<SPE) | (1<<MSTR) | (1<<CPHA) | (1<<CPOL); //Enable SPI in Master mode, mode 3, Fosc/4
|
44 |
|
45 | }
|
46 |
|
47 |
|
48 |
|
49 | unsigned char DF_SPI_RW (unsigned char output)
|
50 |
|
51 | {
|
52 |
|
53 | unsigned char input;
|
54 |
|
55 |
|
56 |
|
57 | SPDR = output; //put byte 'output' in SPI data register
|
58 |
|
59 | while(!(SPSR & 0x80)); //wait for transfer complete, poll SPIF-flag
|
60 |
|
61 | input = SPDR; //read value in SPI data reg.
|
62 |
|
63 |
|
64 |
|
65 | return input; //return the byte clocked in from SPI slave
|
66 |
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | unsigned char Read_DF_status (void)
|
76 |
|
77 | {
|
78 |
|
79 | unsigned char result,index_copy;
|
80 |
|
81 | DF_CS_inactive; //make sure to toggle CS signal in order
|
82 |
|
83 | DF_CS_active; //to reset dataflash command decoder
|
84 |
|
85 | result = DF_SPI_RW(0x57); //send status register read op-code
|
86 |
|
87 | result = DF_SPI_RW(0x00); //dummy write to get result ### Hier hängt er###
|
88 |
|
89 |
|
90 |
|
91 | index_copy = ((result & 0x38) >> 3); //get the size info from status register
|
92 |
|
93 |
|
94 |
|
95 | PageBits = pgm_read_byte(&DF_pagebits[index_copy]); //get number of internal page address bits from look-up table
|
96 |
|
97 | PageSize = pgm_read_word(&DF_pagesize[index_copy]); //get the size of the page (in bytes)
|
98 |
|
99 |
|
100 |
|
101 | return result; //return the read status register value
|
102 |
|
103 | }
|
104 |
|
105 | int main(void)
|
106 |
|
107 | {
|
108 |
|
109 | unsigned char puffer[10];
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | uart_init();
|
116 |
|
117 | uart_puts("Hallo");
|
118 |
|
119 |
|
120 |
|
121 | DF_SPI_init(); // init the SPI interface to communicate with the DataFlash
|
122 |
|
123 |
|
124 |
|
125 | char result = Read_DF_status();
|
126 |
|
127 |
|
128 | sprintf(puffer,"%.2x\r\n",result);
|
129 |
|
130 | uart_puts("DF-Status 0x");
|
131 | uart_puts(puffer);
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | while(1) {
|
138 |
|
139 |
|
140 |
|
141 | }
|
142 |
|
143 |
|
144 |
|
145 | return 0;
|
146 |
|
147 | }
|