Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

extEEPROM.h 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*-----------------------------------------------------------------------------*
  2. * extEEPROM.h - Arduino library to support external I2C EEPROMs. *
  3. * *
  4. * This library will work with most I2C serial EEPROM chips between 2k bits *
  5. * and 2048k bits (2M bits) in size. Multiple EEPROMs on the bus are supported *
  6. * as a single address space. I/O across block, page and device boundaries *
  7. * is supported. Certain assumptions are made regarding the EEPROM *
  8. * device addressing. These assumptions should be true for most EEPROMs *
  9. * but there are exceptions, so read the datasheet and know your hardware. *
  10. * *
  11. * The library should also work for EEPROMs smaller than 2k bits, assuming *
  12. * that there is only one EEPROM on the bus and also that the user is careful *
  13. * to not exceed the maximum address for the EEPROM. *
  14. * *
  15. * Library tested with: *
  16. * Microchip 24AA02E48 (2k bit) *
  17. * 24xx32 (32k bit, thanks to Richard M) *
  18. * Microchip 24LC256 (256k bit) *
  19. * Microchip 24FC1026 (1M bit, thanks to Gabriele B on the Arduino forum) *
  20. * ST Micro M24M02 (2M bit) *
  21. * *
  22. * Library will NOT work with Microchip 24xx1025 as its control byte does not *
  23. * conform to the following assumptions. *
  24. * *
  25. * Device addressing assumptions: *
  26. * 1. The I2C address sequence consists of a control byte followed by one *
  27. * address byte (for EEPROMs <= 16k bits) or two address bytes (for *
  28. * EEPROMs > 16k bits). *
  29. * 2. The three least-significant bits in the control byte (excluding the R/W *
  30. * bit) comprise the three most-significant bits for the entire address *
  31. * space, i.e. all chips on the bus. As such, these may be chip-select *
  32. * bits or block-select bits (for individual chips that have an internal *
  33. * block organization), or a combination of both (in which case the *
  34. * block-select bits must be of lesser significance than the chip-select *
  35. * bits). *
  36. * 3. Regardless of the number of bits needed to address the entire address *
  37. * space, the three most-significant bits always go in the control byte. *
  38. * Depending on EEPROM device size, this may result in one or more of the *
  39. * most significant bits in the I2C address bytes being unused (or "don't *
  40. * care"). *
  41. * 4. An EEPROM contains an integral number of pages. *
  42. * *
  43. * To use the extEEPROM library, the Arduino Wire library must also *
  44. * be included. *
  45. * *
  46. * Jack Christensen 23Mar2013 v1 *
  47. * 29Mar2013 v2 - Updated to span page boundaries (and therefore also *
  48. * device boundaries, assuming an integral number of pages per device) *
  49. * 08Jul2014 v3 - Generalized for 2kb - 2Mb EEPROMs. *
  50. * *
  51. * Paolo Paolucci 22-10-2015 v3.2 *
  52. * 09-01-2016 v3.2 Add update function. *
  53. * *
  54. * External EEPROM Library by Jack Christensen is licensed under CC BY-SA 4.0, *
  55. * http://creativecommons.org/licenses/by-sa/4.0/ *
  56. *-----------------------------------------------------------------------------*/
  57. /*
  58. * tekka 2018:
  59. * Re-implementing extEEPROM::update(unsigned long addr, byte value);
  60. */
  61. #ifndef extEEPROM_h
  62. #define extEEPROM_h
  63. #include <Arduino.h>
  64. #include <Wire.h>
  65. //EEPROM size in kilobits. EEPROM part numbers are usually designated in k-bits.
  66. enum eeprom_size_t {
  67. kbits_2 = 2,
  68. kbits_4 = 4,
  69. kbits_8 = 8,
  70. kbits_16 = 16,
  71. kbits_32 = 32,
  72. kbits_64 = 64,
  73. kbits_128 = 128,
  74. kbits_256 = 256,
  75. kbits_512 = 512,
  76. kbits_1024 = 1024,
  77. kbits_2048 = 2048
  78. };
  79. //EEPROM addressing error, returned by write() or read() if upper address bound is exceeded
  80. const uint8_t EEPROM_ADDR_ERR = 9;
  81. /** extEEPROM class */
  82. class extEEPROM
  83. {
  84. private:
  85. // the private attribute used to comunicate with the correct I2C SERCOM
  86. TwoWire *communication;
  87. public:
  88. /**
  89. * I2C clock frequencies
  90. */
  91. enum twiClockFreq_t {
  92. twiClock100kHz = 100000, //!< twiClock100kHz
  93. twiClock400kHz = 400000 //!< twiClock400kHz
  94. };
  95. /**
  96. * @brief Constructor
  97. * @param deviceCapacity
  98. * @param nDevice
  99. * @param pageSize
  100. * @param eepromAddr
  101. */
  102. extEEPROM(eeprom_size_t deviceCapacity, byte nDevice, unsigned int pageSize,
  103. byte eepromAddr = 0x50);
  104. // It is ready for every I2C Sercom, by default use the main Wire
  105. byte begin(twiClockFreq_t twiFreq = twiClock100kHz, TwoWire *_comm=&Wire); //!< begin()
  106. byte write(unsigned long addr, byte *values, unsigned int nBytes); //!< write()
  107. byte write(unsigned long addr, byte value); //!< write()
  108. byte read(unsigned long addr, byte *values, unsigned int nBytes); //!< read()
  109. int read(unsigned long addr); //!< read()
  110. byte update(unsigned long addr, byte *values, unsigned int nBytes); //!< update()
  111. byte update(unsigned long addr, byte value); //!< update()
  112. unsigned long length(); //!< length()
  113. private:
  114. uint8_t _eepromAddr; //eeprom i2c address
  115. uint16_t _dvcCapacity; //capacity of one EEPROM device, in kbits
  116. uint8_t _nDevice; //number of devices on the bus
  117. uint16_t _pageSize; //page size in bytes
  118. uint8_t _csShift; //number of bits to shift address for chip select bits in control byte
  119. uint16_t _nAddrBytes; //number of address bytes (1 or 2)
  120. unsigned long _totalCapacity; //capacity of all EEPROM devices on the bus, in bytes
  121. };
  122. #endif