1 #include <twr_tag_nfc.h>
4 #define _TWR_TAG_NFC_TNF_WELL_KNOWN 0x01
6 #define _TWR_TAG_NFC_BLOCK_SIZE 16
8 static bool _twr_tag_nfc_ndef_add_record_head(
twr_tag_nfc_ndef_t *
self,
size_t payload_length);
12 memset(
self, 0,
sizeof(*
self));
14 self->_i2c_channel = i2c_channel;
16 self->_i2c_address = i2c_address;
24 uint8_t config[_TWR_TAG_NFC_BLOCK_SIZE];
32 transfer.
length =
sizeof(config);
39 if ((config[12] | config[13] | config[14] | config[15]) == 0x00 )
41 config[0] =
self->_i2c_address << 1;
68 if ((length % _TWR_TAG_NFC_BLOCK_SIZE != 0) || (length > TWR_TAG_NFC_BUFFER_SIZE))
73 size_t read_length = 0;
75 uint8_t address = 0x01;
81 transfer.
length = _TWR_TAG_NFC_BLOCK_SIZE;
83 while ((read_length < length) && (address < 0x37))
87 transfer.
buffer = (uint8_t *) buffer + read_length;
96 read_length += _TWR_TAG_NFC_BLOCK_SIZE;
104 if ((length % _TWR_TAG_NFC_BLOCK_SIZE != 0) || (length > TWR_TAG_NFC_BUFFER_SIZE))
109 size_t write_length = 0;
111 uint8_t address = 0x01;
117 transfer.
length = _TWR_TAG_NFC_BLOCK_SIZE;
119 while ((write_length < length) && (address < 0x37))
123 transfer.
buffer = (uint8_t *) buffer + write_length;
138 write_length += _TWR_TAG_NFC_BLOCK_SIZE;
146 size_t length = ndef->_length;
148 if (((length % _TWR_TAG_NFC_BLOCK_SIZE) != 0))
150 length = ((length / _TWR_TAG_NFC_BLOCK_SIZE) + 1) * _TWR_TAG_NFC_BLOCK_SIZE;
158 self->_buffer[0] = 0x03;
160 self->_buffer[1] = 0x00;
162 self->_buffer[2] = 0xFE;
166 self->_encoded_size = 0;
168 self->_last_tnf_pos = 0;
173 size_t text_length = strlen(text);
175 size_t encoding_length = strlen(encoding);
177 if (!_twr_tag_nfc_ndef_add_record_head(
self, encoding_length + 1 + text_length))
182 self->_buffer[
self->_length++] = 0x54;
184 self->_buffer[
self->_length++] = encoding_length;
186 memcpy(self->_buffer + self->_length, encoding, encoding_length);
188 self->_length += encoding_length;
190 memcpy(self->_buffer + self->_length, text, text_length);
192 self->_length += text_length;
194 self->_buffer[
self->_length++] = 0xFE;
201 size_t uri_length = strlen(uri);
203 if (!_twr_tag_nfc_ndef_add_record_head(
self, 1 + uri_length))
207 self->_buffer[
self->_length++] = 0x55;
209 self->_buffer[
self->_length++] = 0;
211 memcpy(self->_buffer + self->_length, uri, uri_length);
213 self->_length += uri_length;
215 self->_buffer[
self->_length++] = 0xFE;
220 static bool _twr_tag_nfc_ndef_add_record_head(
twr_tag_nfc_ndef_t *
self,
size_t payload_length)
222 size_t head_length = 2 + (payload_length > 0xff ? 4 : 1) + 1;
224 if ((head_length + payload_length > TWR_TAG_NFC_BUFFER_SIZE))
229 if (self->_last_tnf_pos != 0)
231 self->_buffer[
self->_last_tnf_pos] &= ~0x40;
234 if (self->_encoded_size == 0)
236 self->_buffer[1] = 0xff;
245 self->_encoded_size += head_length + payload_length;
247 self->_buffer[2] =
self->_encoded_size >> 8;
249 self->_buffer[3] =
self->_encoded_size;
251 self->_buffer[
self->_length] = _TWR_TAG_NFC_TNF_WELL_KNOWN | 0x40;
253 if (self->_length == 4)
255 self->_buffer[
self->_length] |= 0x80;
258 if (payload_length <= 0xff)
260 self->_buffer[
self->_length] |= 0x10;
263 self->_last_tnf_pos =
self->_length;
267 self->_buffer[
self->_length++] = 1;
269 if (payload_length <= 0xFF)
271 self->_buffer[
self->_length++] = payload_length;
275 self->_buffer[
self->_length++] = payload_length >> 24;
277 self->_buffer[
self->_length++] = payload_length >> 16;
279 self->_buffer[
self->_length++] = payload_length >> 8;
281 self->_buffer[
self->_length++] = payload_length & 0xff;
void twr_i2c_init(twr_i2c_channel_t channel, twr_i2c_speed_t speed)
Initialize I2C channel.
bool twr_i2c_memory_write(twr_i2c_channel_t channel, const twr_i2c_memory_transfer_t *transfer)
Memory write to I2C channel.
bool twr_i2c_memory_read(twr_i2c_channel_t channel, const twr_i2c_memory_transfer_t *transfer)
Memory read from I2C channel.
twr_i2c_channel_t
I2C channels.
@ TWR_I2C_SPEED_400_KHZ
I2C communication speed is 400 kHz.
void twr_tag_nfc_ndef_init(twr_tag_nfc_ndef_t *self)
Initialize NDEF.
bool twr_tag_nfc_init(twr_tag_nfc_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize NFC Tag.
bool twr_tag_nfc_ndef_add_text(twr_tag_nfc_ndef_t *self, const char *text, const char *encoding)
Add ndef text record.
bool twr_tag_nfc_memory_read(twr_tag_nfc_t *self, void *buffer, size_t length)
Read from memory.
bool twr_tag_nfc_memory_write(twr_tag_nfc_t *self, void *buffer, size_t length)
Write to memory.
bool twr_tag_nfc_memory_write_ndef(twr_tag_nfc_t *self, twr_tag_nfc_ndef_t *ndef)
Write to memory.
bool twr_tag_nfc_ndef_add_uri(twr_tag_nfc_ndef_t *self, const char *uri)
Add ndef uri record.
void twr_timer_init(void)
Initialize timer.
void twr_timer_delay(uint16_t microseconds)
Relative delay.
void twr_timer_stop(void)
Stop timer.
void twr_timer_start(void)
Start timer.
I2C memory transfer parameters.
uint32_t memory_address
8-bit I2C memory address (it can be extended to 16-bit format if OR-ed with TWR_I2C_MEMORY_ADDRESS_16...
uint8_t device_address
7-bit I2C device address
size_t length
Length of buffer which is being written or read.
void * buffer
Pointer to buffer which is being written or read.