浏览代码

HAL eeprom cleanup

Scott Lahteine 4 年前
父节点
当前提交
38b44e3fc9

+ 1
- 1
Marlin/src/HAL/STM32F1/eeprom_if_iic.cpp 查看文件

40
 // Public functions
40
 // Public functions
41
 // ------------------------
41
 // ------------------------
42
 
42
 
43
-void eeprom_write_byte(uint8_t *pos, unsigned char value) {
43
+void eeprom_write_byte(uint8_t *pos, uint8_t value) {
44
   const unsigned eeprom_address = (unsigned)pos;
44
   const unsigned eeprom_address = (unsigned)pos;
45
   return BL24CXX::writeOneByte(eeprom_address, value);
45
   return BL24CXX::writeOneByte(eeprom_address, value);
46
 }
46
 }

+ 1
- 1
Marlin/src/HAL/shared/eeprom_if.h 查看文件

25
 // EEPROM
25
 // EEPROM
26
 //
26
 //
27
 void eeprom_init();
27
 void eeprom_init();
28
-void eeprom_write_byte(uint8_t *pos, unsigned char value);
28
+void eeprom_write_byte(uint8_t *pos, uint8_t value);
29
 uint8_t eeprom_read_byte(uint8_t *pos);
29
 uint8_t eeprom_read_byte(uint8_t *pos);

+ 8
- 9
Marlin/src/HAL/shared/eeprom_if_i2c.cpp 查看文件

55
 // Public functions
55
 // Public functions
56
 // ------------------------
56
 // ------------------------
57
 
57
 
58
-void eeprom_write_byte(uint8_t *pos, unsigned char value) {
58
+static void _eeprom_begin(uint8_t * const pos) {
59
   const unsigned eeprom_address = (unsigned)pos;
59
   const unsigned eeprom_address = (unsigned)pos;
60
-
61
   Wire.beginTransmission(eeprom_device_address);
60
   Wire.beginTransmission(eeprom_device_address);
62
-  Wire.write(int(eeprom_address >> 8));   // MSB
63
-  Wire.write(int(eeprom_address & 0xFF)); // LSB
61
+  Wire.write(int(eeprom_address >> 8));   // Address High
62
+  Wire.write(int(eeprom_address & 0xFF)); // Address Low
63
+}
64
+
65
+void eeprom_write_byte(uint8_t *pos, uint8_t value) {
66
+  _eeprom_begin(pos);
64
   Wire.write(value);
67
   Wire.write(value);
65
   Wire.endTransmission();
68
   Wire.endTransmission();
66
 
69
 
70
 }
73
 }
71
 
74
 
72
 uint8_t eeprom_read_byte(uint8_t *pos) {
75
 uint8_t eeprom_read_byte(uint8_t *pos) {
73
-  const unsigned eeprom_address = (unsigned)pos;
74
-
75
-  Wire.beginTransmission(eeprom_device_address);
76
-  Wire.write(int(eeprom_address >> 8));   // MSB
77
-  Wire.write(int(eeprom_address & 0xFF)); // LSB
76
+  _eeprom_begin(pos);
78
   Wire.endTransmission();
77
   Wire.endTransmission();
79
   Wire.requestFrom(eeprom_device_address, (byte)1);
78
   Wire.requestFrom(eeprom_device_address, (byte)1);
80
   return Wire.available() ? Wire.read() : 0xFF;
79
   return Wire.available() ? Wire.read() : 0xFF;

+ 28
- 31
Marlin/src/HAL/shared/eeprom_if_spi.cpp 查看文件

43
   #define EEPROM_WRITE_DELAY    7
43
   #define EEPROM_WRITE_DELAY    7
44
 #endif
44
 #endif
45
 
45
 
46
-uint8_t eeprom_read_byte(uint8_t* pos) {
47
-  uint8_t v;
48
-  uint8_t eeprom_temp[3];
49
-
50
-  // set read location
51
-  // begin transmission from device
52
-  eeprom_temp[0] = CMD_READ;
53
-  eeprom_temp[1] = ((unsigned)pos>>8) & 0xFF; // addr High
54
-  eeprom_temp[2] = (unsigned)pos& 0xFF;       // addr Low
55
-  WRITE(SPI_EEPROM1_CS, HIGH);
56
-  WRITE(SPI_EEPROM1_CS, LOW);
46
+static void _eeprom_begin(uint8_t * const pos, const uint8_t cmd) {
47
+  const uint8_t eeprom_temp[3] = {
48
+    cmd,
49
+    (unsigned(pos) >> 8) & 0xFF,  // Address High
50
+     unsigned(pos)       & 0xFF   // Address Low
51
+  };
52
+  WRITE(SPI_EEPROM1_CS, HIGH);    // Usually free already
53
+  WRITE(SPI_EEPROM1_CS, LOW);     // Activate the Bus
57
   spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 3);
54
   spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 3);
55
+                                  // Leave the Bus in-use
56
+}
57
+
58
+uint8_t eeprom_read_byte(uint8_t* pos) {
59
+  _eeprom_begin(pos, CMD_READ);   // Set read location and begin transmission
60
+
61
+  const uint8_t v = spiRec(SPI_CHAN_EEPROM1); // After READ a value sits on the Bus
62
+
63
+  WRITE(SPI_EEPROM1_CS, HIGH);    // Done with device
58
 
64
 
59
-  v = spiRec(SPI_CHAN_EEPROM1);
60
-  WRITE(SPI_EEPROM1_CS, HIGH);
61
   return v;
65
   return v;
62
 }
66
 }
63
 
67
 
64
-void eeprom_write_byte(uint8_t* pos, uint8_t value) {
65
-  uint8_t eeprom_temp[3];
66
-
67
-  /*write enable*/
68
-  eeprom_temp[0] = CMD_WREN;
69
-  WRITE(SPI_EEPROM1_CS, LOW);
70
-  spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 1);
71
-  WRITE(SPI_EEPROM1_CS, HIGH);
72
-  delay(1);
73
-
74
-  /*write addr*/
75
-  eeprom_temp[0] = CMD_WRITE;
76
-  eeprom_temp[1] = ((unsigned)pos>>8) & 0xFF;  //addr High
77
-  eeprom_temp[2] = (unsigned)pos & 0xFF;       //addr Low
68
+void eeprom_write_byte(uint8_t *pos, uint8_t value) {
69
+  const uint8_t eeprom_temp = CMD_WREN;
78
   WRITE(SPI_EEPROM1_CS, LOW);
70
   WRITE(SPI_EEPROM1_CS, LOW);
79
-  spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 3);
71
+  spiSend(SPI_CHAN_EEPROM1, &eeprom_temp, 1); // Write Enable
72
+
73
+  WRITE(SPI_EEPROM1_CS, HIGH);      // Done with the Bus
74
+  delay(1);                         // For a small amount of time
75
+
76
+  _eeprom_begin(pos, CMD_WRITE);    // Set write address and begin transmission
80
 
77
 
81
-  spiSend(SPI_CHAN_EEPROM1, value);
82
-  WRITE(SPI_EEPROM1_CS, HIGH);
83
-  delay(EEPROM_WRITE_DELAY);   // wait for page write to complete
78
+  spiSend(SPI_CHAN_EEPROM1, value); // Send the value to be written
79
+  WRITE(SPI_EEPROM1_CS, HIGH);      // Done with the Bus
80
+  delay(EEPROM_WRITE_DELAY);        // Give page write time to complete
84
 }
81
 }
85
 
82
 
86
 #endif // USE_SHARED_EEPROM
83
 #endif // USE_SHARED_EEPROM

正在加载...
取消
保存