?lang_form? ?lang_select? ?lang_submit? ?lang_endform?
{HEADER END}
{BLAME START}

library

?curdirlinks? -

Blame information for rev 6

Line No. Rev Author Line
1 6 kaklik <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
3 <title>Procyon AVRlib: spiflash.c Source File</title>
4 <link href="dox.css" rel="stylesheet" type="text/css">
5 </head><body>
6 <!-- Generated by Doxygen 1.4.2 -->
7 <div class="qindex"><a class="qindex" href="main.html">Main&nbsp;Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data&nbsp;Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Data&nbsp;Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a></div>
8 <h1>spiflash.c</h1><a href="spiflash_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file spiflash.c \brief SPI Flash Memory Driver (M25Pxx/AT25Fxxx/etc). */</span>
9 00002 <span class="comment">//*****************************************************************************</span>
10 00003 <span class="comment">//</span>
11 00004 <span class="comment">// File Name : 'spiflash.c'</span>
12 00005 <span class="comment">// Title : SPI Flash Memory Driver (M25Pxx/AT25Fxxx/etc)</span>
13 00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2006</span>
14 00007 <span class="comment">// Created : 2006-04-15</span>
15 00008 <span class="comment">// Revised : 2006-07-02</span>
16 00009 <span class="comment">// Version : 0.1</span>
17 00010 <span class="comment">// Target MCU : AVR processors</span>
18 00011 <span class="comment">// Editor Tabs : 4</span>
19 00012 <span class="comment">//</span>
20 00013 <span class="comment">// NOTE: This code is currently below version 1.0, and therefore is considered</span>
21 00014 <span class="comment">// to be lacking in some functionality or documentation, or may not be fully</span>
22 00015 <span class="comment">// tested. Nonetheless, you can expect most functions to work.</span>
23 00016 <span class="comment">//</span>
24 00017 <span class="comment">//*****************************************************************************</span>
25 00018
26 00019 <span class="comment">// system includes</span>
27 00020 <span class="preprocessor">#include "avr/io.h"</span>
28 00021 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>
29 00022
30 00023 <span class="comment">// library includes</span>
31 00024 <span class="preprocessor">#include "<a class="code" href="spi_8h.html">spi.h</a>"</span>
32 00025 <span class="preprocessor">#include "<a class="code" href="spiflash_8h.html">spiflash.h</a>"</span>
33 00026
34 00027 <span class="preprocessor">#define SPIFLASH_CONFIG_CS DDRB |= (1&lt;&lt;0)</span>
35 00028 <span class="preprocessor"></span><span class="preprocessor">#define SPIFLASH_ASSERT_CS PORTB &amp;= (1&lt;&lt;0)</span>
36 00029 <span class="preprocessor"></span><span class="preprocessor">#define SPIFLASH_RELEASE_CS PORTB |= (1&lt;&lt;0)</span>
37 00030 <span class="preprocessor"></span>
38 00031 <span class="comment">// functions</span>
39 00032 <span class="keywordtype">void</span> spiflashInit(<span class="keywordtype">void</span>)
40 00033 {
41 00034 <span class="comment">// initialize spi</span>
42 00035 spiInit();
43 00036 <span class="comment">// initialize chip select</span>
44 00037 SPIFLASH_RELEASE_CS;
45 00038 SPIFLASH_CONFIG_CS;
46 00039 }
47 00040
48 00041 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> spiflashGetID(<span class="keywordtype">void</span>)
49 00042 {
50 00043 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> id;
51 00044
52 00045 SPIFLASH_ASSERT_CS;
53 00046 spiByte(0, SPIFLASH_CMD_RDID, 0);
54 00047 <span class="keywordtype">id</span> = spiByte(0, 0x00, 0)&lt;&lt;8;
55 00048 <span class="keywordtype">id</span> |= spiByte(0, 0x00, 1);
56 00049 SPIFLASH_RELEASE_CS;
57 00050
58 00051 <span class="keywordflow">return</span> id;
59 00052 }
60 00053
61 00054 <span class="keywordtype">void</span> spiflashChipErase(<span class="keywordtype">void</span>)
62 00055 {
63 00056 <span class="comment">// enable write</span>
64 00057 SPIFLASH_ASSERT_CS;
65 00058 spiByte(0, SPIFLASH_CMD_WREN, 1);
66 00059 SPIFLASH_RELEASE_CS;
67 00060
68 00061 <span class="comment">// clock out dummy byte to waste time</span>
69 00062 spiByte(0, 0x00, 1);
70 00063
71 00064 <span class="comment">// do chip erase</span>
72 00065 SPIFLASH_ASSERT_CS;
73 00066 spiByte(0, SPIFLASH_CMD_CHIPERASE, 1);
74 00067 SPIFLASH_RELEASE_CS;
75 00068
76 00069 <span class="comment">// clock out dummy byte to waste time</span>
77 00070 spiByte(0, 0x00, 1);
78 00071
79 00072 <span class="comment">// wait until write is done</span>
80 00073 SPIFLASH_ASSERT_CS;
81 00074 spiByte(0, SPIFLASH_CMD_RDSR, 0);
82 00075 <span class="keywordflow">while</span>(spiByte(0, 0x00, 0) &amp; SPIFLASH_STATUS_BUSY);
83 00076 SPIFLASH_RELEASE_CS;
84 00077 }
85 00078
86 00079 <span class="keywordtype">void</span> spiflashRead(<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> addr, <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> nbytes, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *data)
87 00080 {
88 00081 <span class="comment">// begin read</span>
89 00082 SPIFLASH_ASSERT_CS;
90 00083 <span class="comment">// issue read command</span>
91 00084 spiByte(0, SPIFLASH_CMD_READ, 0);
92 00085 <span class="comment">// send address</span>
93 00086 spiByte(0, addr&gt;&gt;16, 0);
94 00087 spiByte(0, addr&gt;&gt;8, 0);
95 00088 spiByte(0, addr&gt;&gt;0, 0);
96 00089 <span class="comment">// read data</span>
97 00090 <span class="keywordflow">while</span>(nbytes--)
98 00091 *data++ = spiByte(0, 0x00, 0);
99 00092 <span class="comment">// end read</span>
100 00093 SPIFLASH_RELEASE_CS;
101 00094 }
102 00095
103 00096 <span class="keywordtype">void</span> spiflashWrite(<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> addr, <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> nbytes, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *data)
104 00097 {
105 00098 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> page;
106 00099 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i;
107 00100 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> pagelen;
108 00101
109 00102 <span class="comment">// loop through pages to be programmed</span>
110 00103 <span class="keywordflow">for</span>(page=0; page&lt;((nbytes+SPIFLASH_PAGESIZE-1)&gt;&gt;8); page++)
111 00104 {
112 00105 <span class="comment">// program this page</span>
113 00106
114 00107 <span class="comment">// enable write</span>
115 00108 SPIFLASH_ASSERT_CS;
116 00109 spiByte(0, SPIFLASH_CMD_WREN, 1);
117 00110 SPIFLASH_RELEASE_CS;
118 00111
119 00112 <span class="comment">// clock out dummy byte to waste time</span>
120 00113 spiByte(0, 0x00, 1);
121 00114
122 00115 <span class="comment">// begin write</span>
123 00116 SPIFLASH_ASSERT_CS;
124 00117 <span class="comment">// issue write command</span>
125 00118 spiByte(0, SPIFLASH_CMD_PAGEPROG, 0);
126 00119 <span class="comment">// send address</span>
127 00120 spiByte(0, addr&gt;&gt;16, 0);
128 00121 spiByte(0, addr&gt;&gt;8, 0);
129 00122 spiByte(0, addr&gt;&gt;0, 0);
130 00123 <span class="comment">// program exactly the number of bytes requested</span>
131 00124 <span class="keywordflow">if</span>( ((page&lt;&lt;8)+SPIFLASH_PAGESIZE) &lt;= nbytes)
132 00125 pagelen = SPIFLASH_PAGESIZE;
133 00126 <span class="keywordflow">else</span>
134 00127 pagelen = nbytes-(page&lt;&lt;8);
135 00128 <span class="comment">// transfer data</span>
136 00129 <span class="keywordflow">for</span>(i=0; i&lt;pagelen; i++)
137 00130 spiByte(0, *data++, 0);
138 00131 <span class="comment">// end write</span>
139 00132 SPIFLASH_RELEASE_CS;
140 00133
141 00134 <span class="comment">// clock out dummy byte to waste time</span>
142 00135 spiByte(0, 0x00, 1);
143 00136
144 00137 <span class="comment">// wait until write is done</span>
145 00138 SPIFLASH_ASSERT_CS;
146 00139 spiByte(0, SPIFLASH_CMD_RDSR, 0);
147 00140 <span class="keywordflow">while</span>(spiByte(0, 0x00, 0) &amp; SPIFLASH_STATUS_BUSY);
148 00141 SPIFLASH_RELEASE_CS;
149 00142
150 00143 <span class="comment">// clock out dummy byte to waste time</span>
151 00144 spiByte(0, 0x00, 1);
152 00145 }
153 00146 }
154 </pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:07 2006 for Procyon AVRlib by&nbsp;
155 <a href="http://www.doxygen.org/index.html">
156 <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address>
157 </body>
158 </html>
{BLAME END}
{FOOTER START}

Powered by WebSVN v2.8.3