Guest User

Untitled

a guest
Feb 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. /*
  2. * Author(s): Min-Hua Chen (Embedded Platform Lab, NTHU)
  3. * Copyright (c) 2008 National Tsing Hua University (NTHU)
  4. * Permission to copy, modify, and distribute this program is granted
  5. * for noncommercial purposes, provided the author(s) and copyright
  6. * notice are retained. All other uses require explicit written
  7. * permission from NTHU.
  8. *
  9. * eeprom driver
  10. * Min-Hua Chen <orca.chen@gmail.com>
  11. * 2007/8/23
  12. * 2008/3/6
  13. */
  14.  
  15. #include <eco/reg24e1.h>
  16. #include <spi/spi.h>
  17. #include "eeprom.h"
  18.  
  19. /* Eco programmer mode, issue output signal as EE_CS signal
  20. * to another Eco debugger board. With correct wire connection,
  21. * we can write date to another debugging board, check the manual
  22. * for the connection part
  23. */
  24. #include <eco/config.h>
  25. #include <serial/serial.h>
  26.  
  27. #ifdef ECO_PROG_MODE
  28. #undef EE_CS
  29. /* I/O port as CS for Eco sensor node*/
  30. #define EE_CS INT0_N
  31. #endif
  32.  
  33. #ifdef ECO_DEV_PROG_MODE
  34. #undef EE_CS
  35. /* I/O port as CS for new debugging boad */
  36. #define EE_CS DIO7
  37. #endif
  38.  
  39. /* eeproom_init - init the eeprom, connect eeprom to the SPI
  40. * interface */
  41. void eeprom_init()
  42. {
  43. /* connect spi to eeprom and setup clock rate to 1/8 of CPU */
  44. spi_init(SPI_CONN_EEPROM, SPI_CLK_D8);
  45. /* set p0.0(EEPROM CSN) to output mode */
  46. P0_DIR &= ~0x01;
  47. }
  48.  
  49. /* eeprom_status - read the status register */
  50. char eeprom_status()
  51. {
  52. char byte;
  53. EE_CS = 0; /* active eeprom */
  54. spi_write_then_read(EE_RDSR); /* send read-status-register
  55. instruction to the eeprom */
  56. byte = spi_write_then_read(0);
  57. EE_CS = 1; /* inactive eeprom */
  58.  
  59. return byte;
  60. }
  61.  
  62. #if 0
  63. /* eeprom_write - write a single byte to specified address
  64. * @addr: target address
  65. * @byte: writting byte of data
  66. */
  67. void eeprom_write(unsigned int addr, char byte)
  68. {
  69. while (eeprom_status() & 0x01) /* wait until write cycle done */
  70. ;
  71. puts("ee_write:");
  72. int_print((unsigned int)addr);
  73. puts(", ee_write_byte:");
  74. int_print((unsigned int)byte);
  75. puts("\n\r");
  76. EE_CS = 0; /* active eeprom */
  77. spi_write_then_read(EE_WREN); /* write-enable instruction */
  78. EE_CS = 1; /* inactive eeprom */
  79. EE_CS = 0; /* active eeprom */
  80. spi_write_then_read(EE_WRITE); /* write instruction */
  81. spi_write_then_read(addr >> 8); /* higher byte of addr */
  82. spi_write_then_read(addr & 0xff); /* lower byte */
  83. spi_write_then_read(byte); /* write data */
  84. EE_CS = 1; /* inactive eeprom */
  85. EE_CS = 0; /* active eeprom */
  86. spi_write_then_read(EE_WRDI); /* write-disable instruction */
  87. EE_CS = 1; /* inactive eeprom */
  88. }
  89. #endif
  90.  
  91. /* eeprom_read - read single byte from specified address
  92. * @addr: target address
  93. */
  94. char eeprom_read(unsigned int addr)
  95. {
  96. char byte = 0;
  97. while (eeprom_status() & 0x01) /* wait until write cycle done */
  98. ;
  99. EE_CS = 0; /* active eeprom */
  100. spi_write_then_read(EE_READ); /* read instruction */
  101. spi_write_then_read(addr >> 8); /* higher byte of addr */
  102. spi_write_then_read(addr & 0xff); /* lower byte */
  103. byte = spi_write_then_read(0); /* read data */
  104. EE_CS = 1; /* inactive eeprom */
  105. return byte;
  106. }
  107.  
  108. /* flash_erase_all - erase all pages on flash memory */
  109. void flash_erase_all()
  110. {
  111. while (eeprom_status() & 0x01) /* wait until write cycle done */
  112. ;
  113. EE_CS = 0; /* enable SPI slave */
  114. spi_write_then_read(EE_WREN); /* write-enable instruction */
  115. EE_CS = 1; /* start erase operation */
  116. EE_CS = 0; /* start erase operation */
  117. spi_write_then_read(ERASE_ALL); /* read instruction */
  118. EE_CS = 1; /* start erase operation */
  119. while (eeprom_status() & 0x00) /* wait until erase done */
  120. ;
  121. /* re-enable flash write operation */
  122. EE_CS = 0; /* enable SPI slave */
  123. spi_write_then_read(EE_WREN); /* write-enable instruction */
  124. EE_CS = 1; /* start erase operation */
  125. }
Add Comment
Please, Sign In to add comment