Statistics
| Branch: | Tag: | Revision:

amiro-lld / drivers / AT24C01B / v1 / alld_AT24C01B.c @ e7af07e7

History | View | Annotate | Download (6.468 KB)

1
/*
2
AMiRo-LLD is a compilation of low-level hardware drivers for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2019  Thomas Schöpping et al.
4

5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU Lesser General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9

10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU Lesser General Public License for more details.
14

15
You should have received a copy of the GNU Lesser General Public License
16
along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
18

    
19
/**
20
 * @file    alld_AT24C01B.c
21
 * @brief   EEPROM function implementations.
22
 *
23
 * @addtogroup lld_eeprom
24
 * @{
25
 */
26

    
27
#include <alld_AT24C01B.h>
28
#include <string.h>
29

    
30
/******************************************************************************/
31
/* LOCAL DEFINITIONS                                                          */
32
/******************************************************************************/
33

    
34
/******************************************************************************/
35
/* EXPORTED VARIABLES                                                         */
36
/******************************************************************************/
37

    
38
/******************************************************************************/
39
/* LOCAL TYPES                                                                */
40
/******************************************************************************/
41

    
42
/******************************************************************************/
43
/* LOCAL VARIABLES                                                            */
44
/******************************************************************************/
45

    
46
/******************************************************************************/
47
/* LOCAL FUNCTIONS                                                            */
48
/******************************************************************************/
49

    
50
/******************************************************************************/
51
/* EXPORTED FUNCTIONS                                                         */
52
/******************************************************************************/
53

    
54
/**
55
 * @brief Acknowledgement polling to check if a write operation finished.
56
 * @param[in]   at24c01b  The eeprom driver to use.
57
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
58
 *
59
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
60
 */
61
apalExitStatus_t at24c01b_lld_poll_ack(const AT24C01BDriver* const at24c01b, const apalTime_t timeout)
62
{
63
  apalDbgAssert(at24c01b != NULL);
64

    
65
  uint8_t tmp = 0;
66
  return apalI2CMasterReceive(at24c01b->i2cd, (AT24C01B_LLD_I2C_ADDR_FIXED | at24c01b->addr), &tmp, 1, timeout);
67
}
68

    
69
/**
70
 * @brief Read the data from the eeprom starting at the current address.
71
 * @param[in]   at24c01b  The eeprom driver to use.
72
 * @param[out]  data      The data read from the AT24C01B.
73
 * @param[in]   num       The amount of bytes to read.
74
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
75
 *
76
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
77
 */
78
apalExitStatus_t at24c01b_lld_read_current_address(const AT24C01BDriver* const at24c01b, uint8_t* const data, const uint8_t num, const apalTime_t timeout)
79
{
80
  apalDbgAssert(at24c01b != NULL);
81
  apalDbgAssert(data != NULL);
82

    
83
  return apalI2CMasterReceive(at24c01b->i2cd, (AT24C01B_LLD_I2C_ADDR_FIXED | at24c01b->addr), data, num, timeout);
84
}
85

    
86
/**
87
 * @brief Read the data from the eeprom starting at a given address.
88
 * @param[in]   at24c01b  The eeprom driver to use.
89
 * @param[in]   addr      The address to read.
90
 * @param[out]  data      The data read from the AT24C01B.
91
 * @param[in]   num       The amount of bytes to read.
92
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
93
 *
94
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
95
 */
96
apalExitStatus_t at24c01b_lld_read(const AT24C01BDriver* const at24c01b, const uint8_t addr, uint8_t* const data, const uint8_t num, const apalTime_t timeout)
97
{
98
  apalDbgAssert(at24c01b != NULL);
99
  apalDbgAssert(data != NULL);
100
  apalDbgAssert(addr + num <= AT24C01B_LLD_SIZE_BYTES);
101

    
102
  apalExitStatus_t status = apalI2CMasterTransmit(at24c01b->i2cd, (AT24C01B_LLD_I2C_ADDR_FIXED | at24c01b->addr), &addr, 1, NULL, 0, timeout);
103
  if (status == APAL_STATUS_OK || status == APAL_STATUS_WARNING) {
104
    return apalI2CMasterReceive(at24c01b->i2cd, (AT24C01B_LLD_I2C_ADDR_FIXED | at24c01b->addr), data, num, timeout);
105
  } else {
106
    return status;
107
  }
108
}
109

    
110
/**
111
 * @brief Write a byte to the eeprom.
112
 * @param[in]   at24c01b  The eeprom driver to use.
113
 * @param[in]   addr      The address to start writing.
114
 * @param[out]  data      The data to be written to the AT24C01BN.
115
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
116
 *
117
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
118
 */
119
apalExitStatus_t at24c01b_lld_write_byte(const AT24C01BDriver* const at24c01b, const uint8_t addr, const uint8_t data, const apalTime_t timeout)
120
{
121
  apalDbgAssert(at24c01b != NULL);
122

    
123
  const uint8_t writedata[2] = {addr, data};
124
  return apalI2CMasterTransmit(at24c01b->i2cd, (AT24C01B_LLD_I2C_ADDR_FIXED | at24c01b->addr), writedata, 2, NULL, 0, timeout);
125
}
126

    
127
/**
128
 * @brief Write a page to the eeprom.
129
 * @param[in]   at24c01b  The eeprom driver to use.
130
 * @param[in]   addr      The address to start writing.
131
 * @param[in]   data      The data to be written to the AT24C01BN.
132
 * @param[in]   num       The amount of bytes to write.
133
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
134
 *
135
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
136
 */
137
apalExitStatus_t at24c01b_lld_write_page(const AT24C01BDriver* const at24c01b, const uint8_t addr, const uint8_t* const data, const uint8_t num, const apalTime_t timeout)
138
{
139
  apalDbgAssert(at24c01b != NULL);
140
  apalDbgAssert(data != NULL);
141

    
142
  uint8_t writedata[num+1];
143
  writedata[0] = addr;
144
  memcpy(&writedata[1], data, num);
145
  return apalI2CMasterTransmit(at24c01b->i2cd, (AT24C01B_LLD_I2C_ADDR_FIXED | at24c01b->addr), writedata, num+1, NULL, 0, timeout);
146
}
147

    
148
/** @} */
149