| 1 |
608 |
jeremybenn |
/* ----------------------------------------------------------------------------
|
| 2 |
|
|
* ATMEL Microcontroller Software Support
|
| 3 |
|
|
* ----------------------------------------------------------------------------
|
| 4 |
|
|
* Copyright (c) 2008, Atmel Corporation
|
| 5 |
|
|
*
|
| 6 |
|
|
* All rights reserved.
|
| 7 |
|
|
*
|
| 8 |
|
|
* Redistribution and use in source and binary forms, with or without
|
| 9 |
|
|
* modification, are permitted provided that the following conditions are met:
|
| 10 |
|
|
*
|
| 11 |
|
|
* - Redistributions of source code must retain the above copyright notice,
|
| 12 |
|
|
* this list of conditions and the disclaimer below.
|
| 13 |
|
|
*
|
| 14 |
|
|
* Atmel's name may not be used to endorse or promote products derived from
|
| 15 |
|
|
* this software without specific prior written permission.
|
| 16 |
|
|
*
|
| 17 |
|
|
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
|
| 18 |
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
| 19 |
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
|
| 20 |
|
|
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
|
| 21 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
| 22 |
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
| 23 |
|
|
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
| 24 |
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
| 25 |
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
| 26 |
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 27 |
|
|
* ----------------------------------------------------------------------------
|
| 28 |
|
|
*/
|
| 29 |
|
|
|
| 30 |
|
|
#ifndef trace_LEVEL
|
| 31 |
|
|
#define trace_LEVEL 1
|
| 32 |
|
|
#endif
|
| 33 |
|
|
|
| 34 |
|
|
//------------------------------------------------------------------------------
|
| 35 |
|
|
// Headers
|
| 36 |
|
|
//------------------------------------------------------------------------------
|
| 37 |
|
|
|
| 38 |
|
|
#include "twi.h"
|
| 39 |
|
|
#include <utility/math.h>
|
| 40 |
|
|
#include <utility/assert.h>
|
| 41 |
|
|
#include <utility/trace.h>
|
| 42 |
|
|
|
| 43 |
|
|
//------------------------------------------------------------------------------
|
| 44 |
|
|
// Global functions
|
| 45 |
|
|
//------------------------------------------------------------------------------
|
| 46 |
|
|
|
| 47 |
|
|
//------------------------------------------------------------------------------
|
| 48 |
|
|
/// Configures a TWI peripheral to operate in master mode, at the given
|
| 49 |
|
|
/// frequency (in Hz). The duty cycle of the TWI clock is set to 50%.
|
| 50 |
|
|
/// \param pTwi Pointer to an AT91S_TWI instance.
|
| 51 |
|
|
/// \param twck Desired TWI clock frequency.
|
| 52 |
|
|
/// \param mck Master clock frequency.
|
| 53 |
|
|
//------------------------------------------------------------------------------
|
| 54 |
|
|
void TWI_Configure(AT91S_TWI *pTwi, unsigned int twck, unsigned int mck)
|
| 55 |
|
|
{
|
| 56 |
|
|
unsigned int ckdiv = 0;
|
| 57 |
|
|
unsigned int cldiv;
|
| 58 |
|
|
unsigned char ok = 0;
|
| 59 |
|
|
|
| 60 |
|
|
trace_LOG(trace_DEBUG, "-D- TWI_Configure()\n\r");
|
| 61 |
|
|
SANITY_CHECK(pTwi);
|
| 62 |
|
|
|
| 63 |
|
|
// Reset the TWI
|
| 64 |
|
|
pTwi->TWI_CR = AT91C_TWI_SWRST;
|
| 65 |
|
|
|
| 66 |
|
|
// Set master mode
|
| 67 |
|
|
pTwi->TWI_CR = AT91C_TWI_MSEN;
|
| 68 |
|
|
|
| 69 |
|
|
// Configure clock
|
| 70 |
|
|
while (!ok) {
|
| 71 |
|
|
|
| 72 |
|
|
cldiv = ((mck / (2 * twck)) - 3) / power(2, ckdiv);
|
| 73 |
|
|
if (cldiv <= 255) {
|
| 74 |
|
|
|
| 75 |
|
|
ok = 1;
|
| 76 |
|
|
}
|
| 77 |
|
|
else {
|
| 78 |
|
|
|
| 79 |
|
|
ckdiv++;
|
| 80 |
|
|
}
|
| 81 |
|
|
}
|
| 82 |
|
|
|
| 83 |
|
|
ASSERT(ckdiv < 8, "-F- Cannot find valid TWI clock parameters\n\r");
|
| 84 |
|
|
trace_LOG(trace_INFO, "-D- Using CKDIV = %u and CLDIV/CHDIV = %u\n\r", ckdiv, cldiv);
|
| 85 |
|
|
pTwi->TWI_CWGR = (ckdiv << 16) | (cldiv << 8) | cldiv;
|
| 86 |
|
|
}
|
| 87 |
|
|
|
| 88 |
|
|
//------------------------------------------------------------------------------
|
| 89 |
|
|
/// Sends a STOP condition on the TWI.
|
| 90 |
|
|
/// \param pTwi Pointer to an AT91S_TWI instance.
|
| 91 |
|
|
//------------------------------------------------------------------------------
|
| 92 |
|
|
void TWI_Stop(AT91S_TWI *pTwi)
|
| 93 |
|
|
{
|
| 94 |
|
|
SANITY_CHECK(pTwi);
|
| 95 |
|
|
|
| 96 |
|
|
pTwi->TWI_CR = AT91C_TWI_STOP;
|
| 97 |
|
|
}
|
| 98 |
|
|
|
| 99 |
|
|
//------------------------------------------------------------------------------
|
| 100 |
|
|
/// Starts a read operation on the TWI bus with the specified slave, and returns
|
| 101 |
|
|
/// immediately. Data must then be read using TWI_ReadByte() whenever a byte is
|
| 102 |
|
|
/// available (poll using TWI_ByteReceived()).
|
| 103 |
|
|
/// \param pTwi Pointer to an AT91S_TWI instance.
|
| 104 |
|
|
/// \param address Slave address on the bus.
|
| 105 |
|
|
/// \param iaddress Optional internal address bytes.
|
| 106 |
|
|
/// \param isize Number of internal address bytes.
|
| 107 |
|
|
//-----------------------------------------------------------------------------
|
| 108 |
|
|
void TWI_StartRead(
|
| 109 |
|
|
AT91S_TWI *pTwi,
|
| 110 |
|
|
unsigned char address,
|
| 111 |
|
|
unsigned int iaddress,
|
| 112 |
|
|
unsigned char isize)
|
| 113 |
|
|
{
|
| 114 |
|
|
trace_LOG(trace_DEBUG, "-D- TWI_StartRead()\n\r");
|
| 115 |
|
|
SANITY_CHECK(pTwi);
|
| 116 |
|
|
SANITY_CHECK((address & 0x80) == 0);
|
| 117 |
|
|
SANITY_CHECK((iaddress & 0xFF000000) == 0);
|
| 118 |
|
|
SANITY_CHECK(isize < 4);
|
| 119 |
|
|
|
| 120 |
|
|
// Set slave address and number of internal address bytes
|
| 121 |
|
|
pTwi->TWI_MMR = (isize << 8) | AT91C_TWI_MREAD | (address << 16);
|
| 122 |
|
|
|
| 123 |
|
|
// Set internal address bytes
|
| 124 |
|
|
pTwi->TWI_IADR = iaddress;
|
| 125 |
|
|
|
| 126 |
|
|
// Send START condition
|
| 127 |
|
|
pTwi->TWI_CR = AT91C_TWI_START;
|
| 128 |
|
|
}
|
| 129 |
|
|
|
| 130 |
|
|
//-----------------------------------------------------------------------------
|
| 131 |
|
|
/// Reads a byte from the TWI bus. The read operation must have been started
|
| 132 |
|
|
/// using TWI_StartRead() and a byte must be available (check with
|
| 133 |
|
|
/// TWI_ByteReceived()).
|
| 134 |
|
|
/// Returns the byte read.
|
| 135 |
|
|
/// \param pTwi Pointer to an AT91S_TWI instance.
|
| 136 |
|
|
//-----------------------------------------------------------------------------
|
| 137 |
|
|
unsigned char TWI_ReadByte(AT91S_TWI *pTwi)
|
| 138 |
|
|
{
|
| 139 |
|
|
SANITY_CHECK(pTwi);
|
| 140 |
|
|
|
| 141 |
|
|
return pTwi->TWI_RHR;
|
| 142 |
|
|
}
|
| 143 |
|
|
|
| 144 |
|
|
//-----------------------------------------------------------------------------
|
| 145 |
|
|
/// Sends a byte of data to one of the TWI slaves on the bus. This function
|
| 146 |
|
|
/// must be called once before TWI_StartWrite() with the first byte of data
|
| 147 |
|
|
/// to send, then it shall be called repeatedly after that to send the
|
| 148 |
|
|
/// remaining bytes.
|
| 149 |
|
|
/// \param pTwi Pointer to an AT91S_TWI instance.
|
| 150 |
|
|
/// \param byte Byte to send.
|
| 151 |
|
|
//-----------------------------------------------------------------------------
|
| 152 |
|
|
void TWI_WriteByte(AT91S_TWI *pTwi, unsigned char byte)
|
| 153 |
|
|
{
|
| 154 |
|
|
SANITY_CHECK(pTwi);
|
| 155 |
|
|
|
| 156 |
|
|
pTwi->TWI_THR = byte;
|
| 157 |
|
|
}
|
| 158 |
|
|
|
| 159 |
|
|
//-----------------------------------------------------------------------------
|
| 160 |
|
|
/// Starts a write operation on the TWI to access the selected slave, then
|
| 161 |
|
|
/// returns immediately. A byte of data must be provided to start the write;
|
| 162 |
|
|
/// other bytes are written next.
|
| 163 |
|
|
/// \param pTwi Pointer to an AT91S_TWI instance.
|
| 164 |
|
|
/// \param address Address of slave to acccess on the bus.
|
| 165 |
|
|
/// \param iaddress Optional slave internal address.
|
| 166 |
|
|
/// \param isize Number of internal address bytes.
|
| 167 |
|
|
/// \param byte First byte to send.
|
| 168 |
|
|
//-----------------------------------------------------------------------------
|
| 169 |
|
|
void TWI_StartWrite(
|
| 170 |
|
|
AT91S_TWI *pTwi,
|
| 171 |
|
|
unsigned char address,
|
| 172 |
|
|
unsigned int iaddress,
|
| 173 |
|
|
unsigned char isize,
|
| 174 |
|
|
unsigned char byte)
|
| 175 |
|
|
{
|
| 176 |
|
|
trace_LOG(trace_DEBUG, "-D- TWI_StartWrite()\n\r");
|
| 177 |
|
|
SANITY_CHECK(pTwi);
|
| 178 |
|
|
SANITY_CHECK((address & 0x80) == 0);
|
| 179 |
|
|
SANITY_CHECK((iaddress & 0xFF000000) == 0);
|
| 180 |
|
|
SANITY_CHECK(isize < 4);
|
| 181 |
|
|
|
| 182 |
|
|
// Set slave address and number of internal address bytes
|
| 183 |
|
|
pTwi->TWI_MMR = (isize << 8) | (address << 16);
|
| 184 |
|
|
|
| 185 |
|
|
// Set internal address bytes
|
| 186 |
|
|
pTwi->TWI_IADR = iaddress;
|
| 187 |
|
|
|
| 188 |
|
|
// Write first byte to send
|
| 189 |
|
|
TWI_WriteByte(pTwi, byte);
|
| 190 |
|
|
}
|
| 191 |
|
|
|
| 192 |
|
|
//-----------------------------------------------------------------------------
|
| 193 |
|
|
/// Returns 1 if a byte has been received and can be read on the given TWI
|
| 194 |
|
|
/// peripheral; otherwise, returns 0. This function resets the status register
|
| 195 |
|
|
/// of the TWI.
|
| 196 |
|
|
/// \param pTwi Pointer to an AT91S_TWI instance.
|
| 197 |
|
|
//-----------------------------------------------------------------------------
|
| 198 |
|
|
unsigned char TWI_ByteReceived(AT91S_TWI *pTwi)
|
| 199 |
|
|
{
|
| 200 |
|
|
return ((pTwi->TWI_SR & AT91C_TWI_RXRDY) == AT91C_TWI_RXRDY);
|
| 201 |
|
|
}
|
| 202 |
|
|
|
| 203 |
|
|
//-----------------------------------------------------------------------------
|
| 204 |
|
|
/// Returns 1 if a byte has been sent, so another one can be stored for
|
| 205 |
|
|
/// transmission; otherwise returns 0. This function clears the status register
|
| 206 |
|
|
/// of the TWI.
|
| 207 |
|
|
/// \param pTwi Pointer to an AT91S_TWI instance.
|
| 208 |
|
|
//-----------------------------------------------------------------------------
|
| 209 |
|
|
unsigned char TWI_ByteSent(AT91S_TWI *pTwi)
|
| 210 |
|
|
{
|
| 211 |
|
|
return ((pTwi->TWI_SR & AT91C_TWI_TXRDY) == AT91C_TWI_TXRDY);
|
| 212 |
|
|
}
|
| 213 |
|
|
|
| 214 |
|
|
//-----------------------------------------------------------------------------
|
| 215 |
|
|
/// Returns 1 if the current transmission is complete (the STOP has been sent);
|
| 216 |
|
|
/// otherwise returns 0.
|
| 217 |
|
|
/// \param pTwi Pointer to an AT91S_TWI instance.
|
| 218 |
|
|
//-----------------------------------------------------------------------------
|
| 219 |
|
|
unsigned char TWI_TransferComplete(AT91S_TWI *pTwi)
|
| 220 |
|
|
{
|
| 221 |
|
|
return ((pTwi->TWI_SR & AT91C_TWI_TXCOMP) == AT91C_TWI_TXCOMP);
|
| 222 |
|
|
}
|
| 223 |
|
|
|
| 224 |
|
|
//-----------------------------------------------------------------------------
|
| 225 |
|
|
/// Enables the selected interrupts sources on a TWI peripheral.
|
| 226 |
|
|
/// \param pTwi Pointer to an AT91S_TWI instance.
|
| 227 |
|
|
/// \param sources Bitwise OR of selected interrupt sources.
|
| 228 |
|
|
//-----------------------------------------------------------------------------
|
| 229 |
|
|
void TWI_EnableIt(AT91S_TWI *pTwi, unsigned int sources)
|
| 230 |
|
|
{
|
| 231 |
|
|
SANITY_CHECK(pTwi);
|
| 232 |
|
|
SANITY_CHECK((sources & 0xFFFFFEF8) == 0);
|
| 233 |
|
|
|
| 234 |
|
|
pTwi->TWI_IER = sources;
|
| 235 |
|
|
}
|
| 236 |
|
|
|
| 237 |
|
|
//-----------------------------------------------------------------------------
|
| 238 |
|
|
/// Disables the selected interrupts sources on a TWI peripheral.
|
| 239 |
|
|
/// \param pTwi Pointer to an AT91S_TWI instance.
|
| 240 |
|
|
/// \param sources Bitwise OR of selected interrupt sources.
|
| 241 |
|
|
//-----------------------------------------------------------------------------
|
| 242 |
|
|
void TWI_DisableIt(AT91S_TWI *pTwi, unsigned int sources)
|
| 243 |
|
|
{
|
| 244 |
|
|
SANITY_CHECK(pTwi);
|
| 245 |
|
|
SANITY_CHECK((sources & 0xFFFFFEF8) == 0);
|
| 246 |
|
|
|
| 247 |
|
|
pTwi->TWI_IDR = sources;
|
| 248 |
|
|
}
|
| 249 |
|
|
|
| 250 |
|
|
//-----------------------------------------------------------------------------
|
| 251 |
|
|
/// Returns the current status register of the given TWI peripheral. This
|
| 252 |
|
|
/// resets the internal value of the status register, so further read may yield
|
| 253 |
|
|
/// different values.
|
| 254 |
|
|
/// \param pTwi Pointer to an AT91S_TWI instance.
|
| 255 |
|
|
//-----------------------------------------------------------------------------
|
| 256 |
|
|
unsigned int TWI_GetStatus(AT91S_TWI *pTwi)
|
| 257 |
|
|
{
|
| 258 |
|
|
SANITY_CHECK(pTwi);
|
| 259 |
|
|
|
| 260 |
|
|
return pTwi->TWI_SR;
|
| 261 |
|
|
}
|
| 262 |
|
|
|
| 263 |
|
|
//-----------------------------------------------------------------------------
|
| 264 |
|
|
/// Returns the current status register of the given TWI peripheral, but
|
| 265 |
|
|
/// masking interrupt sources which are not currently enabled.
|
| 266 |
|
|
/// This resets the internal value of the status register, so further read may
|
| 267 |
|
|
/// yield different values.
|
| 268 |
|
|
/// \param pTwi Pointer to an AT91S_TWI instance.
|
| 269 |
|
|
//-----------------------------------------------------------------------------
|
| 270 |
|
|
unsigned int TWI_GetMaskedStatus(AT91S_TWI *pTwi)
|
| 271 |
|
|
{
|
| 272 |
|
|
unsigned int status;
|
| 273 |
|
|
|
| 274 |
|
|
SANITY_CHECK(pTwi);
|
| 275 |
|
|
|
| 276 |
|
|
status = pTwi->TWI_SR;
|
| 277 |
|
|
status &= pTwi->TWI_IMR;
|
| 278 |
|
|
|
| 279 |
|
|
return status;
|
| 280 |
|
|
}
|
| 281 |
|
|
|