Added new high level TWI packet read/write commands, altered behaviour of the TWI_StartTransmission() function.
Spell check source code files.
This commit is contained in:
parent
9c7594e7db
commit
70d55f6e0c
13 changed files with 225 additions and 92 deletions
|
|
@ -87,13 +87,13 @@
|
|||
* for (uint16_t i = 0; i < DATAFLASH_PAGE_SIZE; i++)
|
||||
* Dataflash_SendByte(WriteBuffer[i]);
|
||||
*
|
||||
* // Commit the Dataflash's first memory buffer to the non-voltatile FLASH memory
|
||||
* // Commit the Dataflash's first memory buffer to the non-volatile FLASH memory
|
||||
* printf("Committing page to non-volatile memory page index 5:\r\n");
|
||||
* Dataflash_SendByte(DF_CMD_BUFF1TOMAINMEMWITHERASE);
|
||||
* Dataflash_SendAddressBytes(5, 0);
|
||||
* Dataflash_WaitWhileBusy();
|
||||
*
|
||||
* // Read the page from non-voltatile FLASH memory into the Dataflash's second memory buffer
|
||||
* // Read the page from non-volatile FLASH memory into the Dataflash's second memory buffer
|
||||
* printf("Reading data into second dataflash buffer:\r\n");
|
||||
* Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF2);
|
||||
* Dataflash_SendAddressBytes(5, 0);
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@
|
|||
* ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_128);
|
||||
* Temperature_Init();
|
||||
*
|
||||
* // Display converted temperature in degrees Celcius
|
||||
* // Display converted temperature in degrees Celsius
|
||||
* printf("Current Temperature: %d Degrees\r\n", Temperature_GetTemperature());
|
||||
* \endcode
|
||||
*
|
||||
|
|
|
|||
|
|
@ -35,9 +35,9 @@
|
|||
* different sizes to suit different needs.
|
||||
*
|
||||
* Note that for each buffer, insertion and removal operations may occur at the same time (via
|
||||
* a multithreaded ISR based system) however the same kind of operation (two or more insertions
|
||||
* a multi-threaded ISR based system) however the same kind of operation (two or more insertions
|
||||
* or deletions) must not overlap. If there is possibility of two or more of the same kind of
|
||||
* operating occuring at the same point in time, atomic (mutex) locking should be used.
|
||||
* operating occurring at the same point in time, atomic (mutex) locking should be used.
|
||||
*/
|
||||
|
||||
/** \ingroup Group_MiscDrivers
|
||||
|
|
@ -52,9 +52,9 @@
|
|||
* different sizes to suit different needs.
|
||||
*
|
||||
* Note that for each buffer, insertion and removal operations may occur at the same time (via
|
||||
* a multithreaded ISR based system) however the same kind of operation (two or more insertions
|
||||
* a multi-threaded ISR based system) however the same kind of operation (two or more insertions
|
||||
* or deletions) must not overlap. If there is possibility of two or more of the same kind of
|
||||
* operating occuring at the same point in time, atomic (mutex) locking should be used.
|
||||
* operating occurring at the same point in time, atomic (mutex) locking should be used.
|
||||
*
|
||||
* \section Sec_ExampleUsage Example Usage
|
||||
* The following snippet is an example of how this module may be used within a typical
|
||||
|
|
|
|||
|
|
@ -49,13 +49,16 @@
|
|||
* The following snippet is an example of how this module may be used within a typical
|
||||
* application.
|
||||
*
|
||||
* <b>Low Level API Example:</b>
|
||||
* \code
|
||||
* // Initialise the TWI driver before first use
|
||||
* TWI_Init();
|
||||
*
|
||||
* // Start a write session to device at address 0xA0 with a 10ms timeout
|
||||
* // Start a write session to device at device address 0xA0, internal address 0xDC with a 10ms timeout
|
||||
* if (TWI_StartTransmission(0xA0 | TWI_ADDRESS_WRITE, 10))
|
||||
* {
|
||||
* TWI_SendByte(0xDC);
|
||||
*
|
||||
* TWI_SendByte(0x01);
|
||||
* TWI_SendByte(0x02);
|
||||
* TWI_SendByte(0x03);
|
||||
|
|
@ -64,21 +67,47 @@
|
|||
* TWI_StopTransmission();
|
||||
* }
|
||||
*
|
||||
* // Start a read session to device at address 0xA0 with a 10ms timeout
|
||||
* if (TWI_StartTransmission(0xA0 | TWI_ADDRESS_READ, 10))
|
||||
* // Start a read session to device at address 0xA0, internal address 0xDC with a 10ms timeout
|
||||
* if (TWI_StartTransmission(0xA0 | TWI_ADDRESS_WRITE, 10))
|
||||
* {
|
||||
* uint8_t Byte1, Byte2, Byte3;
|
||||
*
|
||||
* // Read three bytes, acknowledge after the third byte is received
|
||||
* TWI_ReceiveByte(&Byte1, false);
|
||||
* TWI_ReceiveByte(&Byte2, false);
|
||||
* TWI_ReceiveByte(&Byte3, true);
|
||||
*
|
||||
* // Must stop transmission afterwards to release the bus
|
||||
* TWI_SendByte(0xDC);
|
||||
* TWI_StopTransmission();
|
||||
*
|
||||
* if (TWI_StartTransmission(0xA0 | TWI_ADDRESS_READ, 10))
|
||||
* {
|
||||
* uint8_t Byte1, Byte2, Byte3;
|
||||
*
|
||||
* // Read three bytes, acknowledge after the third byte is received
|
||||
* TWI_ReceiveByte(&Byte1, false);
|
||||
* TWI_ReceiveByte(&Byte2, false);
|
||||
* TWI_ReceiveByte(&Byte3, true);
|
||||
*
|
||||
* // Must stop transmission afterwards to release the bus
|
||||
* TWI_StopTransmission();
|
||||
* }
|
||||
* }
|
||||
* \endcode
|
||||
*
|
||||
* <b>High Level API Example:</b>
|
||||
* \code
|
||||
* // Initialise the TWI driver before first use
|
||||
* TWI_Init();
|
||||
*
|
||||
* // Start a write session to device at device address 0xA0, internal address 0xDC with a 10ms timeout
|
||||
* uint8_t InternalWriteAddress = 0xDC;
|
||||
* uint8_t WritePacket[3] = {0x01, 0x02, 0x03};
|
||||
*
|
||||
* TWI_WritePacket(0xA0, 10, &InternalWriteAddress, sizeof(InternalWriteAddress),
|
||||
* &WritePacket, sizeof(WritePacket);
|
||||
*
|
||||
* // Start a read session to device at address 0xA0, internal address 0xDC with a 10ms timeout
|
||||
* uint8_t InternalReadAddress = 0xDC;
|
||||
* uint8_t ReadPacket[3];
|
||||
*
|
||||
* TWI_ReadPacket(0xA0, 10, &InternalReadAddress, sizeof(InternalReadAddress),
|
||||
* &ReadPacket, sizeof(ReadPacket);
|
||||
* \endcode
|
||||
*
|
||||
* @{
|
||||
*/
|
||||
|
||||
|
|
@ -90,6 +119,7 @@
|
|||
|
||||
#include <avr/io.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <util/twi.h>
|
||||
#include <util/delay.h>
|
||||
|
||||
|
|
@ -108,12 +138,29 @@
|
|||
/** TWI slave device address mask for a read session. Mask with a slave device base address to obtain
|
||||
* the correct TWI bus address for the slave device when reading data from it.
|
||||
*/
|
||||
#define TWI_ADDRESS_READ 0x00
|
||||
#define TWI_ADDRESS_READ 0x00
|
||||
|
||||
/** TWI slave device address mask for a write session. Mask with a slave device base address to obtain
|
||||
* the correct TWI bus address for the slave device when writing data to it.
|
||||
*/
|
||||
#define TWI_ADDRESS_WRITE 0x01
|
||||
#define TWI_ADDRESS_WRITE 0x01
|
||||
|
||||
/** Mask to retrieve the base address for a TWI device, which can then be ORed with \ref TWI_ADDRESS_READ
|
||||
* or \ref TWI_ADDRESS_WRITE to obtain the device's read and write address respectively.
|
||||
*/
|
||||
#define TWI_DEVICE_ADDRESS_MASK 0xFE
|
||||
|
||||
/* Enums: */
|
||||
/** Enum for the possible return codes of the TWI transfer start routine and other dependant TWI functions. */
|
||||
enum TWI_ErrorCodes_t
|
||||
{
|
||||
TWI_ERROR_NoError = 0, /**< Indicates that the command completed sucessfully. */
|
||||
TWI_ERROR_BusFault = 1, /**< A TWI bus fault occurred while attempting to capture the bus. */
|
||||
TWI_ERROR_BusCaptureTimeout = 2, /**< A timeout occurred whilst waiting for the bus to be ready. */
|
||||
TWI_ERROR_SlaveResponseTimeout = 3, /**< No ACK received at the nominated slave address within the timeout period. */
|
||||
TWI_ERROR_SlaveNotReady = 4, /**< Slave NAKed the TWI bus START condition. */
|
||||
TWI_ERROR_SlaveNAK = 5, /**< Slave NAKed whilst attempting to send data to the device. */
|
||||
};
|
||||
|
||||
/* Inline Functions: */
|
||||
/** Initialises the TWI hardware into master mode, ready for data transmission and reception. This must be
|
||||
|
|
@ -163,8 +210,8 @@
|
|||
*
|
||||
* \return Boolean \c true if the byte reception successfully completed, \c false otherwise
|
||||
*/
|
||||
static inline bool TWI_ReceiveByte(uint8_t* const Byte,
|
||||
const bool LastByte)
|
||||
static inline uint8_t TWI_ReceiveByte(uint8_t* const Byte,
|
||||
const bool LastByte)
|
||||
{
|
||||
uint8_t TWCRMask = ((1 << TWINT) | (1 << TWEN));
|
||||
|
||||
|
|
@ -184,10 +231,44 @@
|
|||
* \param[in] SlaveAddress Address of the slave TWI device to communicate with
|
||||
* \param[in] TimeoutMS Timeout period within which the slave must respond, in milliseconds
|
||||
*
|
||||
* \return Boolean \c true if the device is ready for data, \c false otherwise
|
||||
* \return A value from the \ref TWI_ErrorCodes_t enum
|
||||
*/
|
||||
bool TWI_StartTransmission(const uint8_t SlaveAddress,
|
||||
const uint8_t TimeoutMS);
|
||||
uint8_t TWI_StartTransmission(const uint8_t SlaveAddress,
|
||||
const uint8_t TimeoutMS);
|
||||
|
||||
/** High level function to perform a complete packet transfer over the TWI bus to the specified
|
||||
* device.
|
||||
*
|
||||
* \param[in] SlaveAddress Base address of the TWI slave device to communicate with
|
||||
* \param[in] TimeoutMS Timeout for bus capture and slave START ACK, in milliseconds
|
||||
* \param[in] InternalAddress Pointer to a location where the internal slave read start address is stored
|
||||
* \param[in] InternalAddressLen Size of the internal device address, in bytes
|
||||
* \param[in] Buffer Pointer to a buffer where the read packet data is to be stored
|
||||
* \param[in] Length Size of the packet to read, in bytes
|
||||
*/
|
||||
uint8_t TWI_ReadPacket(const uint8_t SlaveAddress,
|
||||
const uint8_t TimeoutMS,
|
||||
const uint8_t* InternalAddress,
|
||||
uint8_t InternalAddressLen,
|
||||
uint8_t* Buffer,
|
||||
uint8_t Length);
|
||||
|
||||
/** High level function to perform a complete packet transfer over the TWI bus from the specified
|
||||
* device.
|
||||
*
|
||||
* \param[in] SlaveAddress Base address of the TWI slave device to communicate with
|
||||
* \param[in] TimeoutMS Timeout for bus capture and slave START ACK, in milliseconds
|
||||
* \param[in] InternalAddress Pointer to a location where the internal slave write start address is stored
|
||||
* \param[in] InternalAddressLen Size of the internal device address, in bytes
|
||||
* \param[in] Buffer Pointer to a buffer where the packet data to send is stored
|
||||
* \param[in] Length Size of the packet to send, in bytes
|
||||
*/
|
||||
uint8_t TWI_WritePacket(const uint8_t SlaveAddress,
|
||||
const uint8_t TimeoutMS,
|
||||
const uint8_t* InternalAddress,
|
||||
uint8_t InternalAddressLen,
|
||||
const uint8_t* Buffer,
|
||||
uint8_t Length);
|
||||
|
||||
/* Disable C linkage for C++ Compilers: */
|
||||
#if defined(__cplusplus)
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@
|
|||
|
||||
#include "TWI.h"
|
||||
|
||||
bool TWI_StartTransmission(const uint8_t SlaveAddress,
|
||||
const uint8_t TimeoutMS)
|
||||
uint8_t TWI_StartTransmission(const uint8_t SlaveAddress,
|
||||
const uint8_t TimeoutMS)
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
|
|
@ -33,17 +33,17 @@ bool TWI_StartTransmission(const uint8_t SlaveAddress,
|
|||
continue;
|
||||
default:
|
||||
TWCR = (1 << TWEN);
|
||||
return false;
|
||||
return TWI_ERROR_BusFault;
|
||||
}
|
||||
}
|
||||
|
||||
_delay_us(10);
|
||||
}
|
||||
|
||||
if (!(BusCaptured))
|
||||
if (!(TimeoutRemaining))
|
||||
{
|
||||
TWCR = (1 << TWEN);
|
||||
return false;
|
||||
return TWI_ERROR_BusCaptureTimeout;
|
||||
}
|
||||
|
||||
TWDR = SlaveAddress;
|
||||
|
|
@ -59,17 +59,85 @@ bool TWI_StartTransmission(const uint8_t SlaveAddress,
|
|||
}
|
||||
|
||||
if (!(TimeoutRemaining))
|
||||
return false;
|
||||
return TWI_ERROR_SlaveResponseTimeout;
|
||||
|
||||
switch (TWSR & TW_STATUS_MASK)
|
||||
{
|
||||
case TW_MT_SLA_ACK:
|
||||
case TW_MR_SLA_ACK:
|
||||
return true;
|
||||
return TWI_ERROR_NoError;
|
||||
default:
|
||||
TWCR = ((1 << TWINT) | (1 << TWSTO) | (1 << TWEN));
|
||||
return false;
|
||||
return TWI_ERROR_SlaveNotReady;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t TWI_ReadPacket(const uint8_t SlaveAddress,
|
||||
const uint8_t TimeoutMS,
|
||||
const uint8_t* InternalAddress,
|
||||
uint8_t InternalAddressLen,
|
||||
uint8_t* Buffer,
|
||||
uint8_t Length)
|
||||
{
|
||||
uint8_t ErrorCode;
|
||||
|
||||
if ((ErrorCode = TWI_WritePacket(SlaveAddress, TimeoutMS, InternalAddress, InternalAddressLen,
|
||||
NULL, 0)) != TWI_ERROR_NoError)
|
||||
{
|
||||
return ErrorCode;
|
||||
}
|
||||
|
||||
if ((ErrorCode = TWI_StartTransmission((SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_READ,
|
||||
TimeoutMS)) == TWI_ERROR_NoError)
|
||||
{
|
||||
while (Length--)
|
||||
{
|
||||
if (!(TWI_ReceiveByte(Buffer++, (Length == 0))))
|
||||
{
|
||||
ErrorCode = TWI_ERROR_SlaveNAK;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
TWI_StopTransmission();
|
||||
}
|
||||
|
||||
return ErrorCode;
|
||||
}
|
||||
|
||||
uint8_t TWI_WritePacket(const uint8_t SlaveAddress,
|
||||
const uint8_t TimeoutMS,
|
||||
const uint8_t* InternalAddress,
|
||||
uint8_t InternalAddressLen,
|
||||
const uint8_t* Buffer,
|
||||
uint8_t Length)
|
||||
{
|
||||
uint8_t ErrorCode;
|
||||
|
||||
if ((ErrorCode = TWI_StartTransmission((SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_WRITE,
|
||||
TimeoutMS)) == TWI_ERROR_NoError)
|
||||
{
|
||||
while (InternalAddressLen--)
|
||||
{
|
||||
if (!(TWI_SendByte(*(InternalAddress++))))
|
||||
{
|
||||
ErrorCode = TWI_ERROR_SlaveNAK;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
while (Length--)
|
||||
{
|
||||
if (!(TWI_SendByte(*(Buffer++))))
|
||||
{
|
||||
ErrorCode = TWI_ERROR_SlaveNAK;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
TWI_StopTransmission();
|
||||
}
|
||||
|
||||
return ErrorCode;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -189,7 +189,7 @@
|
|||
enum CDC_DescriptorSubtypes_t
|
||||
{
|
||||
CDC_DSUBTYPE_CSInterface_Header = 0x00, /**< CDC class-specific Header functional descriptor. */
|
||||
CDC_DSUBTYPE_CSInterface_CallManagement = 0x01, /**< CDC class-specific Call Managment functional descriptor. */
|
||||
CDC_DSUBTYPE_CSInterface_CallManagement = 0x01, /**< CDC class-specific Call Management functional descriptor. */
|
||||
CDC_DSUBTYPE_CSInterface_ACM = 0x02, /**< CDC class-specific Abstract Control Model functional descriptor. */
|
||||
CDC_DSUBTYPE_CSInterface_DirectLine = 0x03, /**< CDC class-specific Direct Line functional descriptor. */
|
||||
CDC_DSUBTYPE_CSInterface_TelephoneRinger = 0x04, /**< CDC class-specific Telephone Ringer functional descriptor. */
|
||||
|
|
@ -280,7 +280,7 @@
|
|||
* must be \ref CDC_DSUBTYPE_CSInterface_ACM.
|
||||
*/
|
||||
uint8_t Capabilities; /**< Capabilities of the ACM interface, given as a bit mask. For most devices,
|
||||
* this should be set to a fixed value of 0x06 - for other capabiltiies, refer
|
||||
* this should be set to a fixed value of 0x06 - for other capabilities, refer
|
||||
* to the CDC ACM specification.
|
||||
*/
|
||||
} USB_CDC_Descriptor_FunctionalACM_t;
|
||||
|
|
@ -303,7 +303,7 @@
|
|||
* must be \ref CDC_DSUBTYPE_CSInterface_ACM.
|
||||
*/
|
||||
uint8_t bmCapabilities; /**< Capabilities of the ACM interface, given as a bit mask. For most devices,
|
||||
* this should be set to a fixed value of 0x06 - for other capabiltiies, refer
|
||||
* this should be set to a fixed value of 0x06 - for other capabilities, refer
|
||||
* to the CDC ACM specification.
|
||||
*/
|
||||
} USB_CDC_StdDescriptor_FunctionalACM_t;
|
||||
|
|
|
|||
|
|
@ -95,8 +95,8 @@
|
|||
* after connection before the enumeration process can start or incorrect operation will
|
||||
* occur.
|
||||
*
|
||||
* The default delay value may be overridden in the user project makefile by definining the
|
||||
* \c HOST_DEVICE_SETTLE_DELAY_MS token to tbe required delay in milliseconds, and passed to the
|
||||
* The default delay value may be overridden in the user project makefile by defining the
|
||||
* \c HOST_DEVICE_SETTLE_DELAY_MS token to the required delay in milliseconds, and passed to the
|
||||
* compiler using the -D switch.
|
||||
*/
|
||||
#define HOST_DEVICE_SETTLE_DELAY_MS 1000
|
||||
|
|
|
|||
|
|
@ -308,7 +308,7 @@
|
|||
* changed in value.
|
||||
* \n\n
|
||||
*
|
||||
* \note When the controller is initialized into UID autodetection mode, this variable will hold the
|
||||
* \note When the controller is initialized into UID auto-detection mode, this variable will hold the
|
||||
* currently selected USB mode (i.e. \ref USB_MODE_Device or \ref USB_MODE_Host). If the controller
|
||||
* is fixed into a specific mode (either through the \c USB_DEVICE_ONLY or \c USB_HOST_ONLY compile time
|
||||
* options, or a limitation of the USB controller in the chosen device model) this will evaluate to
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue