2 \author EiNSTeiN_ <einstein@g3nius.org>
3 \brief Atmel 2-wire EEPROM
16 //! Handles a monitor command.
17 void spi_handle_fn( uint8_t const app,
21 // define the spi app's app_t
22 app_t const twe_app = {
34 "\tThis app handles Atmel's 2-wire EEPROM protocol.\n"
37 // Transmit the START condition
45 // high to low transision of SDA while SCL is high
56 // transmit the STOP condition
63 // low to high transision of SDA while SCL is high
71 //! Write 8 bits and read 1
72 unsigned char twe_tx(unsigned char byte){
75 for (bit = 0; bit < 8; bit++) {
78 /* write SDA on trailing edge of previous clock */
92 SPIREN |= SDA; /* as per datasheet, SDA must be pulled high externally */
96 bit = (SPIIN & SDA) ? 1 : 0;
105 debugstr("not acked :s");
111 //! Read 8 bits, optionally acking it
112 unsigned char twe_rx(unsigned int ack){
114 unsigned int bit = 0;
117 SPIREN |= SDA; /* as per datasheet, SDA must be pulled high externally */
120 for (bit = 0; bit < 8; bit++) {
147 //! Read a block to a buffer.
148 void twe_peekblock(uint8_t const app,
155 // start command / write
157 twe_tx(0xa0); // preamble=1010, device adr=000, write=0
159 // output address bytes
160 twe_tx((adr >> 8) & 0xff);
163 // start command / read
165 twe_tx(0xa1); // preamble=1010, device adr=000, read=1
168 txhead(app, verb, len);
171 serial_tx(twe_rx(i != (len-1)));
178 //! Read a block to a buffer.
179 void twe_pokeblock(uint16_t adr,
184 // start command / write
186 twe_tx(0xa0); // preamble=1010, device adr=000, write=0
188 // output address bytes
189 twe_tx((adr >> 8) & 0xFF);
200 //! Set up the pins for SPI mode.
205 SETSDA; // normal position
206 CLRSCL; // normal position
223 //! Handles a monitor command.
224 void twe_handle_fn( uint8_t const app,
231 case PEEK: //Grab 128 bytes from an SPI Flash ROM
232 adr = cmddataword[0];
234 twe_peekblock(app, verb, adr, 128);
237 //~ case POKE: //Grab 128 bytes from an SPI Flash ROM
238 //~ debugstr("reading");
239 //~ twe_pokeblock(cmddata, len);