-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheeprom.h
105 lines (95 loc) · 3.68 KB
/
eeprom.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* eeprom.h
*
* Created: 31-03-2012 18:13:29
* Author: Hussain
*/
/**
* @file eeprom.h
* @brief Functions to read and write to/from EEPROM
*/
#include <stdint.h>
#ifndef EEPROM_H_
#define EEPROM_H_
/// EEPROM Max Address
#define MAX_ADDR 131072
#define HALF_ADDR 65536
/**
* Wrapper around eeprom_write_page() that repeats calling this
* function until either an error has been returned, or all bytes
* have been written.
*/
int eeprom_write_bytes(uint32_t eeaddr, int len, uint8_t *buf);
/**
* Write "len" bytes into EEPROM starting at "eeaddr" from "buf".
*
* This is a bit simpler than the previous function since both, the
* address and the data bytes will be transfered in master transmitter
* mode, thus no reselection of the device is necessary. However, the
* EEPROMs are only capable of writing one "page" simultaneously, so
* care must be taken to not cross a page boundary within one write
* cycle. The amount of data one page consists of varies from
* manufacturer to manufacturer: some vendors only use 8-byte pages
* for the smaller devices, and 16-byte pages for the larger devices,
* while other vendors generally use 16-byte pages. We thus use the
* smallest common denominator of 8 bytes per page, declared by the
* macro PAGE_SIZE above.
*
* The function simply returns after writing one page, returning the
* actual number of data byte written. It is up to the caller to
* re-invoke it in order to write further data.
*/
int eeprom_write_page(uint32_t eeaddr, int len, uint8_t *buf);
/**
* Note [7]
*
* Read "len" bytes from EEPROM starting at "eeaddr" into "buf".
*
* This requires two bus cycles: during the first cycle, the device
* will be selected (master transmitter mode), and the address
* transfered. Address bits exceeding 256 are transfered in the
* E2/E1/E0 bits (subaddress bits) of the device selector.
*
* The second bus cycle will reselect the device (repeated start
* condition, going into master receiver mode), and transfer the data
* from the device to the TWI master. Multiple bytes can be
* transfered by ACKing the client's transfer. The last transfer will
* be NACKed, which the client will take as an indication to not
* initiate further transfers.
*/
int eeprom_read_bytes(uint32_t eeaddr, int len, uint8_t *buf);
int eeprom_read_bytes_part(uint32_t eeaddr, int len, uint8_t *buf);
void write_frame_to_eeprom(uint8_t *frame);
void read_frame_from_eeprom(uint8_t *frame);
/**
* Do all the startup-time peripheral initializations: UART (for our
* debug/test output), and TWI clock.
*/
void ioinit(void);
/**
* Maximal number of iterations to wait for a device to respond for a
* selection. Should be large enough to allow for a pending write to
* complete, but low enough to properly abort an infinite loop in case
* a slave is broken or not present at all. With 100 kHz TWI clock,
* transfering the start condition and SLA+R/W packet takes about 10
* µs. The longest write period is supposed to not exceed ~ 10 ms.
* Thus, normal operation should not require more than 100 iterations
* to get the device to respond to a selection.
*/
#define MAX_ITER 200
/**
* Number of bytes that can be written in a row, see comments for
* eeprom_write_page() below. Some vendor's devices would accept 16,
* but 8 seems to be the lowest common denominator.
*
* Note that the page size must be a power of two, this simplifies the
* page boundary calculations below.
*/
#define PAGE_SIZE 128
/**
* Saved TWI status register, for error messages only. We need to
* save it in a variable, since the datasheet only guarantees the TWSR
* register to have valid contents while the TWINT bit in TWCR is set.
*/
uint8_t twst;
#endif /* EEPROM_H_ */