> 19.7.3 | Transmitting Data over AT Interface

> This section provides information on the protocol used for data transmission to or from I²C/SPI devices and explains the data mode. It is assumed that you are familiar with the I²C specification.
> Throughout this section the following document conventions are used to mark the direction of send and receive:
> Transfer Message: AT → I²C or SPI device driver
> Response Message: AT ← I²C or SPI device driver
> Channel Open / Close:
> To change from command mode to data mode use the AT^SSPI write command. To close down the channel and return to command mode send the ASCII code # (0x23).
> ASCII Protocol for I²C or SPI communication over AT interface:
> The protocol allows using a Terminal program for the communication between the module and the I²C slave device/SPI device. To visualize transferred characters and response data it is recommended to run the Terminal program in chat mode or to switch on local echo.
> For transfer and response, special characters are defined, such as Start and Stop to mark a single message and Close to disconnect the data channel. All valid special characters are listed below:

> Table 19.3: | Special characters for ASCII coding

> Direction AT - Driver | Function in protocol | Special character | Hex value | Description
> → | Start Transfer Message | < | 0x3C | Special character sent to the I²C/SPI driver to start sending.
> → | Stop Transfer Message | > | 0x3E | Special character sent to the I²C/SPI driver to stop sending.
> → | Channel Close | # | 0x23 | For signalling. Channel Close can be sent any time inside or outside a transmit or receive message. Causes the transfer to be aborted and takes the ME back to AT command mode.
> ← | Start Response Message | { | 0x7B | Special character sent to the I²C/SPI driver to mark the beginning of a Response Message.
> ← | Stop Response Message | } | 0x7D | Special character sent to the I²C/SPI driver to mark the end of a Response Message.
> ← | Protocol error | ! | 0x21 | For signalling. Reports to the AT interface that the Transfer Frame does not comply with the protocol definition (syntax error).
> ← | Transmission OK (in I²C protocol referred to as ACK) | + | 0x2B | Notifies the AT interface that data were successfully transmitted or, on the I²C bus, the I²C Slave Address was recognized.
> On I²C bus only:
> ← | Transmission error (in I²C protocol referred to as NAK) | - | 0x2D | Notifies the AT interface that data transmission failed or the I²C Slave Address was not recognized. On the I²C bus, the faulty data byte (16 bit byte counter) is identified as well.

> Message syntax:
> Each Message consists of a Start and Stop character, a Message-ID, further protocol data and user data. The notation of all elements is explained below:
> Notation of Message-ID:

> • | All ASCII characters between 0x00...0x7F. It is recommended to use only the characters 0...9, A...Z, a...z.

> • | Length of the Message-ID: only one character

> Notation of protocol data (except Message-ID) and user data:

> • | Hex (0...9, a...f, A...F)

> • | Without "0x" (0x01 →01)

> • | Each hex value consists of 2 characters (1 →01)

> • | Without delimiters such as comma, semicolon, space etc. (0xAE 0x01 0xA5 →AE01A5)

> • | In a Transfer Message, the number of all characters after the Message ID shall be even. If it is odd, a protocol error will be reported. On the I²C bus, this applies to the I²C Slave Address and all subsequent written user data. On the SPI this applies to the Read Offset Byte and the Read Length and, if available, all written user data. (Keep in mind that the number of all characters transmitted between Start "<" and Stop ">" of the Transfer Frame shall always be odd because the ID is one character only.)

> • | Length of I²C Slave Address and user data: < 2048 bytes

> The first element of each message is the Start character ("<" for Transfer, "{" for the Response). Accordingly, the last character of a message is the Stop character (">" for Transfer, "}" for the Response).
> The second element of each message is the Message ID (1 character). The Message ID serves the user to distinguish between different messages. It is only relevant on protocol level (between AT interface and I²C device driver), i.e. it is not sent to the I²C slave device.
> Each transfer to the device is followed by a Response Message sent from the driver to the AT interface. The response includes the Message ID and either OK ("+") or error characters ("-" or "!"). A successful response to a Read Message contains the OK character and the read data. If an error occurs on the I²C bus, the response consists of an error character followed by a 16 bit code specifying the faulty byte.
> After each Transfer Message, wait for the Response Message before sending the next Transfer Message.
> All characters entered outside a valid message (i.e. not input between Start character "<" and Stop character ">") are ignored.

> 19.7.3.1 | Structure of Messages on the I²C Bus

> Table 19.4: | Structure of Transfer and Response Messages on the I²C bus

> Frame | Format
> Write Transfer Message | < ID SlaveAddress Data > Maximum length: 2048 bytes for I²C Slave Address and written data. LSB of I²C Slave Address = "0".
> Read Transfer Message | < ID SlaveAddress ReadLength > Read Length < 2048 bytes. LSB I²C of Slave Address = "1".
> Response Message Write OK Read of x bytes OK NAK for xth byte if Read or Write Protocol error in xth byte | {ID + } {ID + Data } {ID - xxxx } {ID ! xxxx }

> On the I²C bus, read and write data are handled in two separate frames transmitted one after the other. This is because the I²C bus has only two bus lines, I2CDAT (I2CDAT_SPIDO) for the serial data and I2CCLK (I2CCLK_SPICLK) for the serial clock. Write data are packed into a Transfer Frame. Read data are packed into a Response Frame. The Transfer Frame contains a Receive or Transmit Request (R/W Request) for the I²C master.
> In a Transfer Message (Read or Write), the third element is the 7-bit I²C Slave Address (2 characters) that identifies each single device connected to the bus. The 8th bit of this byte is the LSB that determines the direction of the message. If the LSB is "0" the master will write information to the selected slave. If the LSB is "1" the master will read information sent from the slave.
> In a Read Transfer Message on the I²C bus, the size of the expected data must be specified explicitly. This is an element of 4 characters stating the number of bytes to be read. It must be placed after the I²C Slave Address.

> 19.7.3.2 | Structure of Messages on the SPI

> Table 19.5: | Structure of Transfer and Response Messages for SPI

> Message | Format
> Transfer Message | < ID ReadOffset ReadLength Data > Read Offset = 8 bits Read Length = 16 bits Read Offset + Read Length < 2048 bytes Max. length of read data: 2048 bytes
> Response Message Write OK Reading x bytes was OK Protocol error in xth byte | {ID +} {ID + Data } {ID ! xxxx }

> The SPI has two serial data lines, DO (I2CDAT_SPIDO) for sending data from the master to the slave, and DI (SPIDI) for receiving data sent from the slave to the master. Both data lines are controlled by one serial clock line CLK (I2CCLK_SPICLK). TC65 acts as master providing the clock. Write and read data are handled in the same Transfer Messages and Response Messages. In a Transfer Message, the next two elements after the ID are the Read Offset and the Read Length, both required to enable reading data from the slave. The Read Offset specifies where to start reading, i.e. which byte is the first to start reading from. If the Read Offset is zero then reading starts from the first byte. The Read Length specifies the number of expected bytes. If the Read Offset is zero and the Read Length does not equal zero, the master reads the specified number of bytes, starting from the first byte. If the Read Length is zero, the Read Offset is ignored, meaning that the master will not read data from the slave. To transmit data from the master to the slave all data can be entered after the Read Length.
> In a Response Message the ID is followed by a special character to indicate the result of reading. If successful, "+" is given, followed by the read data. If reading fails only "!" is received.