gnea\grbl-Mega  1.0f
Source Code Documentation ( Internal Workings )
eeprom.c
Go to the documentation of this file.
1 // This file has been prepared for Doxygen automatic documentation generation.
24 #include <avr/io.h>
25 #include <avr/interrupt.h>
26 
28 #ifndef EEPE
29  #define EEPE EEWE
30  #define EEMPE EEMWE
31 #endif
32 
34 #define EEPM1 5
35 #define EEPM0 4
36 
37 
38 #define EEPROM_IGNORE_SELFPROG
39 
40 
49 unsigned char eeprom_get_char( unsigned int addr )
50 {
51  do {} while( EECR & (1<<EEPE) );
52  EEAR = addr;
53  EECR = (1<<EERE);
54  return EEDR;
55 }
56 
74 void eeprom_put_char( unsigned int addr, unsigned char new_value )
75 {
76  char old_value;
77  char diff_mask;
78 
79  cli();
80 
81  do {} while( EECR & (1<<EEPE) );
82  #ifndef EEPROM_IGNORE_SELFPROG
83  do {} while( SPMCSR & (1<<SELFPRGEN) );
84  #endif
85 
86  EEAR = addr;
87  EECR = (1<<EERE);
88  old_value = EEDR;
89  diff_mask = old_value ^ new_value;
90 
91  // Check if any bits are changed to '1' in the new value.
92  if( diff_mask & new_value ) {
93  // Now we know that _some_ bits need to be erased to '1'.
94 
95  // Check if any bits in the new value are '0'.
96  if( new_value != 0xff ) {
97  // Now we know that some bits need to be programmed to '0' also.
98 
99  EEDR = new_value;
100  EECR = (1<<EEMPE) |
101  (0<<EEPM1) | (0<<EEPM0);
102  EECR |= (1<<EEPE);
103  } else {
104  // Now we know that all bits should be erased.
105 
106  EECR = (1<<EEMPE) |
107  (1<<EEPM0);
108  EECR |= (1<<EEPE);
109  }
110  } else {
111  // Now we know that _no_ bits need to be erased to '1'.
112 
113  // Check if any bits are changed from '1' in the old value.
114  if( diff_mask ) {
115  // Now we know that _some_ bits need to the programmed to '0'.
116 
117  EEDR = new_value;
118  EECR = (1<<EEMPE) |
119  (1<<EEPM1);
120  EECR |= (1<<EEPE);
121  }
122  }
123 
124  sei();
125 }
127 
128 
129 void memcpy_to_eeprom_with_checksum(unsigned int destination, char *source, unsigned int size) {
130  unsigned char checksum = 0;
131  for(; size > 0; size--) {
132  checksum = (checksum << 1) || (checksum >> 7);
133  checksum += *source;
134  eeprom_put_char(destination++, *(source++));
135  }
136  eeprom_put_char(destination, checksum);
137 }
138 
139 int memcpy_from_eeprom_with_checksum(char *destination, unsigned int source, unsigned int size) {
140  unsigned char data, checksum = 0;
141  for(; size > 0; size--) {
142  data = eeprom_get_char(source++);
143  checksum = (checksum << 1) || (checksum >> 7);
144  checksum += data;
145  *(destination++) = data;
146  }
147  return(checksum == eeprom_get_char(source));
148 }
#define EEPM1
These two are unfortunately not defined in the device include files.
Definition: eeprom.c:34
#define EEPM0
EEPROM Programming Mode Bit 0.
Definition: eeprom.c:35
#define EEMPE
EEPROM master program/write enable.
Definition: eeprom.c:30
unsigned char eeprom_get_char(unsigned int addr)
Read byte from EEPROM.
Definition: eeprom.c:49
int memcpy_from_eeprom_with_checksum(char *destination, unsigned int source, unsigned int size)
Definition: eeprom.c:139
void eeprom_put_char(unsigned int addr, unsigned char new_value)
Write byte to EEPROM.
Definition: eeprom.c:74
#define EEPE
These EEPROM bits have different names on different devices.
Definition: eeprom.c:29
void memcpy_to_eeprom_with_checksum(unsigned int destination, char *source, unsigned int size)
Extensions added as part of Grbl.
Definition: eeprom.c:129