Xavier Geerinck
Published © GPL3+

An E2E Azure Sphere Project in the Area of Smart Retail

Smart Retail with the Avnet MT3620 Starter Kit and an NFC scanner.

IntermediateFull instructions provided4 hours789

Things used in this project

Story

Read more

Schematics

Connections

Code

mt3620.h

C/C++
Error opening file.

Code snippet #9

Plain text
// INCLUDES

void delay(int s)
{
	sleep(s);
}

int main(void)
{
	Log_Debug("IPC RFID RC522 Application Starting\n");

	// Start the RFID Scanner
	if (mfrc522_init())
	{
		Log_Debug("RFID Scanner not found!\n");
	}
	else
	{
		Log_Debug("RFID Scanner found!\n");
		return -1;
	}

	// Look for a card
	while (1)
	{
		Log_Debug("Trying to get version\n");
		// Check version of the reader
		// Can be 0x91 for 1.0 or 0x92 for 2.0 -> https://www.nxp.com/docs/en/data-sheet/MFRC522.pdf (p66 - VersionReg register)
		uint8_t byte = mfrc522_read(VersionReg);

		Log_Debug("Detected version %d (Hex: %x)\n", byte, byte);

		delay(5);
	}

	return 0;
}

Code snippet #10

Plain text
// INCLUDES

void delay(int s)
{
	sleep(s);
}

int main(void)
{
	Log_Debug("IPC RFID RC522 Application Starting\n");

	// Start the RFID Scanner
	if (mfrc522_init())
	{
		Log_Debug("RFID Scanner not found!\n");
	}
	else
	{
		Log_Debug("RFID Scanner found!\n");
		return -1;
	}

	// Look for a card
	while (1)
	{
		Log_Debug("Trying to get version\n");
		// Check version of the reader
		// Can be 0x91 for 1.0 or 0x92 for 2.0 -> https://www.nxp.com/docs/en/data-sheet/MFRC522.pdf (p66 - VersionReg register)
		uint8_t byte = mfrc522_read(VersionReg);

		Log_Debug("Detected version %d (Hex: %x)\n", byte, byte);

		delay(5);
	}

	return 0;
}

Code snippet #13

Plain text
// Azure IoT SDK
#include <azureiot/iothub_client_core_common.h>
#include <azureiot/iothub_device_client_ll.h>
#include <azureiot/iothub_client_options.h>
#include <azureiot/iothubtransportmqtt.h>
#include <azureiot/iothub.h>
#include <azureiot/azure_sphere_provisioning.h>

Code snippet #14

Plain text
// Azure IoT SDK
#include <azureiot/iothub_client_core_common.h>
#include <azureiot/iothub_device_client_ll.h>
#include <azureiot/iothub_client_options.h>
#include <azureiot/iothubtransportmqtt.h>
#include <azureiot/iothub.h>
#include <azureiot/azure_sphere_provisioning.h>

Code snippet #15

Plain text
Remote debugging from host 192.168.35.1
Application Starting
Setting Azure Scope ID 0ne000777D8
Opening GREEN LED as output
[Azure IoT] Using HSM cert at /run/daa/aca3da0d-5493-45e6-aea2-85c47962770d
[IoTHub][INFO] IoTHubDeviceClient_LL_CreateWithAzureSphereDeviceAuthProvisioning returned 'AZURE_SPHERE_PROV_RESULT_OK'.
[IoTHub][INFO] Configuring Device Twin Callback and Connection Status Callback
[IoTHub][INFO] Sending IoT Hub Message: { "Test": "Hello World" }
[IoTHub][INFO] IoTHubClient accepted the message for delivery
[IoTHub][INFO] Received IoT Twin Update from IoT Hub
[IoTHub][INFO] Changing Status LED to true
[IoTHub][INFO] Reported state for 'MyGreenLED' to value 'true'.
[IoTHub][INFO] Sending IoT Hub Message: { "Test": "Hello World" }
[IoTHub][INFO] IoTHubClient accepted the message for delivery
[IoTHub][INFO] Message received by IoT Hub. Result is: 0

Code snippet #16

Plain text
Remote debugging from host 192.168.35.1
Application Starting
Setting Azure Scope ID 0ne000777D8
Opening GREEN LED as output
[Azure IoT] Using HSM cert at /run/daa/aca3da0d-5493-45e6-aea2-85c47962770d
[IoTHub][INFO] IoTHubDeviceClient_LL_CreateWithAzureSphereDeviceAuthProvisioning returned 'AZURE_SPHERE_PROV_RESULT_OK'.
[IoTHub][INFO] Configuring Device Twin Callback and Connection Status Callback
[IoTHub][INFO] Sending IoT Hub Message: { "Test": "Hello World" }
[IoTHub][INFO] IoTHubClient accepted the message for delivery
[IoTHub][INFO] Received IoT Twin Update from IoT Hub
[IoTHub][INFO] Changing Status LED to true
[IoTHub][INFO] Reported state for 'MyGreenLED' to value 'true'.
[IoTHub][INFO] Sending IoT Hub Message: { "Test": "Hello World" }
[IoTHub][INFO] IoTHubClient accepted the message for delivery
[IoTHub][INFO] Message received by IoT Hub. Result is: 0

Code snippet #17

Plain text
// INCLUDES...

// DECLARE FUNCTIONS...

void delay(int s)
{
	sleep(s);
}

uint8_t InitPeripherals()
{
	Log_Debug("[OLED] Initializing\n");
	if (oled_init())
	{
		Log_Debug("OLED not found!\n");
	}
	else
	{
		Log_Debug("OLED found!\n");
	}

	Log_Debug("[MFRC522] Initializing\n");
	if (mfrc522_init())
	{
		Log_Debug("RFID Scanner not found!\n");
	}
	else
	{
		Log_Debug("RFID Scanner found!\n");
		return -1;
	}
}

int main(void)
{
	int res = InitPeripherals();
	if (res < 0) {
		Log_Debug("Error, exiting!\n");
		return -1;
	}

	oled_template_waiting_for_rfc();

	// Get Card Version
	Log_Debug("Trying to get version\n"); // 0x91 = 1.0, 0x92 = 0.2 -> https://www.nxp.com/docs/en/data-sheet/MFRC522.pdf (p66 - VersionReg register)
	uint8_t readerVersion = mfrc522_read(VersionReg);

	Log_Debug("Detected version %d (Hex: %x)\n", readerVersion, readerVersion);
	oled_template_waiting_for_rfc_with_version(readerVersion);

	// Prepare for reading tags
	uint8_t byte;
	byte = mfrc522_read(ComIEnReg);
	mfrc522_write(ComIEnReg, byte | 0x20);
	byte = mfrc522_read(DivIEnReg);
	mfrc522_write(DivIEnReg, byte | 0x80);

	delay(2);

	// Look for a card
	// Commands: https://www.nxp.com/docs/en/data-sheet/MFRC522.pdf P36
	uint8_t str[MAX_LEN];

	while (1)
	{
		byte = mfrc522_request(PICC_REQALL, str); // Find all the cards antenna area

		if (byte == CARD_FOUND)
		{
			Log_Debug("[MFRC522] Found a card: %x\n", byte);

			byte = mfrc522_get_card_serial(str);

			if (byte == CARD_FOUND)
			{
				for (byte = 0; byte < 8; byte++)
				{
					Log_Debug("[MFRC522] Dumping: %x\n", str[byte]);
				}

				// Convert the byte array to a string of bytes
				char hexstr[8];
				btox(hexstr, str, 8);
				hexstr[8] = 0;
				Log_Debug("%s\n", hexstr);
				oled_template_show_serial(hexstr);

				delay(3);
			}
			else
			{
				Log_Debug("[MFRC522] Error while reading card\n");
			}
		}

		delay(1);
	}

	// Todo: close SPI here

	return 0;
}

void btox(char* xp, const char* bb, int n)
{
	const char xx[] = "0123456789ABCDEF";
	while (--n >= 0) xp[n] = xx[(bb[n >> 1] >> ((1 - (n & 1)) << 2)) & 0xF];
}

void oled_template_waiting_for_rfc(void)
{
	// Clear the buffer
	oled_buffer_clear();

	// Draw the strings
	sd1306_draw_string(0, 0, "Information", FONT_SIZE_TITLE, white_pixel);
	sd1306_draw_string(OLED_LINE_1_X, OLED_LINE_1_Y, "Waiting for tag to", FONT_SIZE_LINE, white_pixel);
	sd1306_draw_string(OLED_LINE_2_X, OLED_LINE_2_Y, "be detected", FONT_SIZE_LINE, white_pixel);

	// Send the buffer to OLED RAM
	sd1306_refresh();
}

void oled_template_waiting_for_rfc_with_version(uint8_t version)
{
	// Clear the buffer
	oled_buffer_clear();

	// Draw the strings
	sd1306_draw_string(0, 0, "Information", FONT_SIZE_TITLE, white_pixel);
	sd1306_draw_string(OLED_LINE_1_X, OLED_LINE_1_Y, "Waiting for tag to", FONT_SIZE_LINE, white_pixel);
	sd1306_draw_string(OLED_LINE_2_X, OLED_LINE_2_Y, "be detected", FONT_SIZE_LINE, white_pixel);

	char versionBuffer[20];
	sprintf(versionBuffer, "MIFARE Version = %x", version);
	sd1306_draw_string(OLED_LINE_3_X, OLED_LINE_3_Y, versionBuffer, FONT_SIZE_LINE, white_pixel);

	// Send the buffer to OLED RAM
	sd1306_refresh();
}

void oled_template_show_serial(char* serial)
{
	// Clear the buffer
	oled_buffer_clear();

	// Draw the strings
	sd1306_draw_string(0, 0, "Information", FONT_SIZE_TITLE, white_pixel);
	sd1306_draw_string(OLED_LINE_1_X, OLED_LINE_1_Y, "Serial:", FONT_SIZE_LINE, white_pixel);
	sd1306_draw_string(OLED_LINE_2_X, OLED_LINE_2_Y, serial, FONT_SIZE_LINE, white_pixel);

	// Send the buffer to OLED RAM
	sd1306_refresh();
}

Code snippet #18

Plain text
// INCLUDES...

// DECLARE FUNCTIONS...

void delay(int s)
{
	sleep(s);
}

uint8_t InitPeripherals()
{
	Log_Debug("[OLED] Initializing\n");
	if (oled_init())
	{
		Log_Debug("OLED not found!\n");
	}
	else
	{
		Log_Debug("OLED found!\n");
	}

	Log_Debug("[MFRC522] Initializing\n");
	if (mfrc522_init())
	{
		Log_Debug("RFID Scanner not found!\n");
	}
	else
	{
		Log_Debug("RFID Scanner found!\n");
		return -1;
	}
}

int main(void)
{
	int res = InitPeripherals();
	if (res < 0) {
		Log_Debug("Error, exiting!\n");
		return -1;
	}

	oled_template_waiting_for_rfc();

	// Get Card Version
	Log_Debug("Trying to get version\n"); // 0x91 = 1.0, 0x92 = 0.2 -> https://www.nxp.com/docs/en/data-sheet/MFRC522.pdf (p66 - VersionReg register)
	uint8_t readerVersion = mfrc522_read(VersionReg);

	Log_Debug("Detected version %d (Hex: %x)\n", readerVersion, readerVersion);
	oled_template_waiting_for_rfc_with_version(readerVersion);

	// Prepare for reading tags
	uint8_t byte;
	byte = mfrc522_read(ComIEnReg);
	mfrc522_write(ComIEnReg, byte | 0x20);
	byte = mfrc522_read(DivIEnReg);
	mfrc522_write(DivIEnReg, byte | 0x80);

	delay(2);

	// Look for a card
	// Commands: https://www.nxp.com/docs/en/data-sheet/MFRC522.pdf P36
	uint8_t str[MAX_LEN];

	while (1)
	{
		byte = mfrc522_request(PICC_REQALL, str); // Find all the cards antenna area

		if (byte == CARD_FOUND)
		{
			Log_Debug("[MFRC522] Found a card: %x\n", byte);

			byte = mfrc522_get_card_serial(str);

			if (byte == CARD_FOUND)
			{
				for (byte = 0; byte < 8; byte++)
				{
					Log_Debug("[MFRC522] Dumping: %x\n", str[byte]);
				}

				// Convert the byte array to a string of bytes
				char hexstr[8];
				btox(hexstr, str, 8);
				hexstr[8] = 0;
				Log_Debug("%s\n", hexstr);
				oled_template_show_serial(hexstr);

				delay(3);
			}
			else
			{
				Log_Debug("[MFRC522] Error while reading card\n");
			}
		}

		delay(1);
	}

	// Todo: close SPI here

	return 0;
}

void btox(char* xp, const char* bb, int n)
{
	const char xx[] = "0123456789ABCDEF";
	while (--n >= 0) xp[n] = xx[(bb[n >> 1] >> ((1 - (n & 1)) << 2)) & 0xF];
}

void oled_template_waiting_for_rfc(void)
{
	// Clear the buffer
	oled_buffer_clear();

	// Draw the strings
	sd1306_draw_string(0, 0, "Information", FONT_SIZE_TITLE, white_pixel);
	sd1306_draw_string(OLED_LINE_1_X, OLED_LINE_1_Y, "Waiting for tag to", FONT_SIZE_LINE, white_pixel);
	sd1306_draw_string(OLED_LINE_2_X, OLED_LINE_2_Y, "be detected", FONT_SIZE_LINE, white_pixel);

	// Send the buffer to OLED RAM
	sd1306_refresh();
}

void oled_template_waiting_for_rfc_with_version(uint8_t version)
{
	// Clear the buffer
	oled_buffer_clear();

	// Draw the strings
	sd1306_draw_string(0, 0, "Information", FONT_SIZE_TITLE, white_pixel);
	sd1306_draw_string(OLED_LINE_1_X, OLED_LINE_1_Y, "Waiting for tag to", FONT_SIZE_LINE, white_pixel);
	sd1306_draw_string(OLED_LINE_2_X, OLED_LINE_2_Y, "be detected", FONT_SIZE_LINE, white_pixel);

	char versionBuffer[20];
	sprintf(versionBuffer, "MIFARE Version = %x", version);
	sd1306_draw_string(OLED_LINE_3_X, OLED_LINE_3_Y, versionBuffer, FONT_SIZE_LINE, white_pixel);

	// Send the buffer to OLED RAM
	sd1306_refresh();
}

void oled_template_show_serial(char* serial)
{
	// Clear the buffer
	oled_buffer_clear();

	// Draw the strings
	sd1306_draw_string(0, 0, "Information", FONT_SIZE_TITLE, white_pixel);
	sd1306_draw_string(OLED_LINE_1_X, OLED_LINE_1_Y, "Serial:", FONT_SIZE_LINE, white_pixel);
	sd1306_draw_string(OLED_LINE_2_X, OLED_LINE_2_Y, serial, FONT_SIZE_LINE, white_pixel);

	// Send the buffer to OLED RAM
	sd1306_refresh();
}

Code snippet #19

Plain text
Remote debugging from host 192.168.35.1
# 1. Initializing the application
[Application][INFO] Starting
[Application][INFO] Setting Azure Scope ID <MASKED>
[OLED][INFO] Initializing
[OLED][INFO] OLED found!
[MFRC522][INFO] Initializing
[MFRC522][SPI][INFO] Initializing
[MFRC522][SPI][INFO] Opened SPI Interface
[MFRC522][SPI][INFO] BusSpeed = 4000000
[MFRC522][SPI][INFO] SPIMode = 1
[MFRC522][SPI][INFO] BitOrder = SPI_BitOrder_MsbFirst
[MFRC522][SPI][INFO] FD Set on 4
[MFRC522][SPI][INFO] Initialized
[MFRC522][INFO] RFID Scanner found!
[ePoll][INFO] Initializing
# 2. Detecting the scanner version
[MFRC522][INFO] Trying to get version
[MFRC522][INFO] Detected version 146 (Hex: 92)
[MFRC522][INFO] Waiting for IoTHub Connection
# ...
[MFRC522][INFO] Waiting for IoTHub Connection
# 3. Configuring our IoT Hub Connection
[Azure IoT] Using HSM cert at /run/daa/<MASKED>
[IoTHub][INFO] IoTHubDeviceClient_LL_CreateWithAzureSphereDeviceAuthProvisioning returned 'AZURE_SPHERE_PROV_RESULT_OK'.
[IoTHub][INFO] Configuring Device Twin Callback and Connection Status Callback
[IoTHub][INFO] IoT Hub Authenticated: IOTHUB_CLIENT_CONNECTION_OK
# 4. Handling the Azure Device Twin Update
[IoTHub][INFO] Received IoT Twin Update from IoT Hub
[IoTHub][Twin][INFO] Updating PriceMap
[IoTHub][Twin][INFO] 8804399D -> Zombie: $15.50
[IoTHub][Twin][INFO] Updating price for 7908C820 to Normal: $14.11
[IoTHub][ERROR] Reported state for '7908C820' to value 'Normal: $14.11'.
[IoTHub][Twin][INFO] 88041B9D -> Skeleton: $20.00
[IoTHub][INFO] Device Twin reported properties update result: HTTP status code 400
# 5. Reading our card and displaying it to the screen
[MFRC522][INFO] Found a card: 1
[MFRC522][INFO] Dumping: 79
[MFRC522][INFO] Dumping: 8
[MFRC522][INFO] Dumping: c8
[MFRC522][INFO] Dumping: 20
[MFRC522][INFO] Dumping: 99
[MFRC522][INFO] Dumping: 0
[MFRC522][INFO] Dumping: 0
[MFRC522][INFO] Dumping: 0
[MFRC522][INFO] Serial: 7908C820
[MAP][INFO] Map Price: Normal: $14.11

Code snippet #20

Plain text
Remote debugging from host 192.168.35.1
# 1. Initializing the application
[Application][INFO] Starting
[Application][INFO] Setting Azure Scope ID <MASKED>
[OLED][INFO] Initializing
[OLED][INFO] OLED found!
[MFRC522][INFO] Initializing
[MFRC522][SPI][INFO] Initializing
[MFRC522][SPI][INFO] Opened SPI Interface
[MFRC522][SPI][INFO] BusSpeed = 4000000
[MFRC522][SPI][INFO] SPIMode = 1
[MFRC522][SPI][INFO] BitOrder = SPI_BitOrder_MsbFirst
[MFRC522][SPI][INFO] FD Set on 4
[MFRC522][SPI][INFO] Initialized
[MFRC522][INFO] RFID Scanner found!
[ePoll][INFO] Initializing
# 2. Detecting the scanner version
[MFRC522][INFO] Trying to get version
[MFRC522][INFO] Detected version 146 (Hex: 92)
[MFRC522][INFO] Waiting for IoTHub Connection
# ...
[MFRC522][INFO] Waiting for IoTHub Connection
# 3. Configuring our IoT Hub Connection
[Azure IoT] Using HSM cert at /run/daa/<MASKED>
[IoTHub][INFO] IoTHubDeviceClient_LL_CreateWithAzureSphereDeviceAuthProvisioning returned 'AZURE_SPHERE_PROV_RESULT_OK'.
[IoTHub][INFO] Configuring Device Twin Callback and Connection Status Callback
[IoTHub][INFO] IoT Hub Authenticated: IOTHUB_CLIENT_CONNECTION_OK
# 4. Handling the Azure Device Twin Update
[IoTHub][INFO] Received IoT Twin Update from IoT Hub
[IoTHub][Twin][INFO] Updating PriceMap
[IoTHub][Twin][INFO] 8804399D -> Zombie: $15.50
[IoTHub][Twin][INFO] Updating price for 7908C820 to Normal: $14.11
[IoTHub][ERROR] Reported state for '7908C820' to value 'Normal: $14.11'.
[IoTHub][Twin][INFO] 88041B9D -> Skeleton: $20.00
[IoTHub][INFO] Device Twin reported properties update result: HTTP status code 400
# 5. Reading our card and displaying it to the screen
[MFRC522][INFO] Found a card: 1
[MFRC522][INFO] Dumping: 79
[MFRC522][INFO] Dumping: 8
[MFRC522][INFO] Dumping: c8
[MFRC522][INFO] Dumping: 20
[MFRC522][INFO] Dumping: 99
[MFRC522][INFO] Dumping: 0
[MFRC522][INFO] Dumping: 0
[MFRC522][INFO] Dumping: 0
[MFRC522][INFO] Serial: 7908C820
[MAP][INFO] Map Price: Normal: $14.11

Code snippet #1

Plain text
# Show the status of the WiFi connection
azsphere device wifi show-status

# Connect the device to WiFi
azsphere device wifi add --ssid <yourSSID> --key <yourKey>

Code snippet #21

Plain text
const config = require('./config');

const { Client } = require('azure-event-hubs');
const connectionString = "<YOUR_IOTHUB_CONNECTION_STRING>";
const consumerGroup = '$Default';

const printError = (err) => {
  console.log(err.message);
};

const client = Client.fromConnectionString(connectionString);
client.open()
.then(client.getPartitionIds.bind(client))
.then((partitionIds) => {
  return partitionIds.map((partitionId) => {
    client.createReceiver(consumerGroup, partitionId, { 'startAfterTime': Date.now() })
    .then((receiver) => {
      console.log(`[Receiver] Created partition receiver: [${partitionId}] for consumerGroup [${consumerGroup}]`);
      receiver.on('errorReceived', printError);
      receiver.on('message', (message) => {
        console.log(`[Receiver][${consumerGroup}][${partitionId}] Message received: ${JSON.stringify(message.body).toString()}`);
      });
    })
  })
})
.catch(printError);

Code snippet #22

Plain text
const config = require('./config');

const { Client } = require('azure-event-hubs');
const connectionString = "<YOUR_IOTHUB_CONNECTION_STRING>";
const consumerGroup = '$Default';

const printError = (err) => {
  console.log(err.message);
};

const client = Client.fromConnectionString(connectionString);
client.open()
.then(client.getPartitionIds.bind(client))
.then((partitionIds) => {
  return partitionIds.map((partitionId) => {
    client.createReceiver(consumerGroup, partitionId, { 'startAfterTime': Date.now() })
    .then((receiver) => {
      console.log(`[Receiver] Created partition receiver: [${partitionId}] for consumerGroup [${consumerGroup}]`);
      receiver.on('errorReceived', printError);
      receiver.on('message', (message) => {
        console.log(`[Receiver][${consumerGroup}][${partitionId}] Message received: ${JSON.stringify(message.body).toString()}`);
      });
    })
  })
})
.catch(printError);

Code snippet #2

Plain text
# Show the status of the WiFi connection
azsphere device wifi show-status

# Connect the device to WiFi
azsphere device wifi add --ssid <yourSSID> --key <yourKey>

Code snippet #3

Plain text
# Login to Azure and see the tenants
azsphere login

# Select the tenant (XavierAzureSphereTenant)
azsphere tenant select -i aca3da0d-5493-45e6-aea2-85c47962770d

# Claim the device
azsphere device claim

Code snippet #4

Plain text
# Login to Azure and see the tenants
azsphere login

# Select the tenant (XavierAzureSphereTenant)
azsphere tenant select -i aca3da0d-5493-45e6-aea2-85c47962770d

# Claim the device
azsphere device claim

Code snippet #5

Plain text
// OUR IMPORTS

int main(int argc, char* argv[])
{
	// Start the OLED Screen
	if (oled_init())
	{
		Log_Debug("OLED not found!\n");
	}
	else
	{
		Log_Debug("OLED found!\n");
	}

	// Clear the buffer
	oled_buffer_clear();

	// Draw the strings
	sd1306_draw_string(0, 0, "Test", FONT_SIZE_TITLE, white_pixel);
	sd1306_draw_string(OLED_LINE_1_X, OLED_LINE_1_Y, "Hello World", FONT_SIZE_LINE, white_pixel);
	sd1306_draw_string(OLED_LINE_2_X, OLED_LINE_2_Y, "Hello World", FONT_SIZE_LINE, white_pixel);
	sd1306_draw_string(OLED_LINE_3_X, OLED_LINE_3_Y, "Hello World", FONT_SIZE_LINE, white_pixel);
	sd1306_draw_string(OLED_LINE_4_X, OLED_LINE_4_Y, "Hello World", FONT_SIZE_LINE, white_pixel);

	// Send the buffer to OLED RAM
	sd1306_refresh();

	return 0;
}

Code snippet #6

Plain text
// OUR IMPORTS

int main(int argc, char* argv[])
{
	// Start the OLED Screen
	if (oled_init())
	{
		Log_Debug("OLED not found!\n");
	}
	else
	{
		Log_Debug("OLED found!\n");
	}

	// Clear the buffer
	oled_buffer_clear();

	// Draw the strings
	sd1306_draw_string(0, 0, "Test", FONT_SIZE_TITLE, white_pixel);
	sd1306_draw_string(OLED_LINE_1_X, OLED_LINE_1_Y, "Hello World", FONT_SIZE_LINE, white_pixel);
	sd1306_draw_string(OLED_LINE_2_X, OLED_LINE_2_Y, "Hello World", FONT_SIZE_LINE, white_pixel);
	sd1306_draw_string(OLED_LINE_3_X, OLED_LINE_3_Y, "Hello World", FONT_SIZE_LINE, white_pixel);
	sd1306_draw_string(OLED_LINE_4_X, OLED_LINE_4_Y, "Hello World", FONT_SIZE_LINE, white_pixel);

	// Send the buffer to OLED RAM
	sd1306_refresh();

	return 0;
}

Code snippet #7

Plain text
void mfrc522_write(uint8_t reg, uint8_t data)
{
	const size_t transferCount = 1;
	SPIMaster_Transfer transfer;

	int result = SPIMaster_InitTransfers(&transfer, transferCount);
	if (result != 0) {
		return;
	}

	//const uint8_t command[] = { (reg << 1) & 0x7E, data };
	const uint8_t command[] = { (reg << 1) & 0x7E, data };
	transfer.flags = SPI_TransferFlags_Write;
	transfer.writeData = command;
	transfer.length = sizeof(command);

	ssize_t transferredBytes = SPIMaster_TransferSequential(spiFd, &transfer, transferCount);

	if (!CheckTransferSize("SPIMaster_TransferSequential (CTRL3_C)", transfer.length, transferredBytes)) {
		Log_Debug("Transfer size is not correct");
		return;
	}
}

uint8_t mfrc522_read(uint8_t reg)
{
	uint8_t readDataResult;
	//uint8_t readCmd = ((reg << 1) & 0x7E) | 0x80; // Set bit 7 indicating it's a read command -> 0x80
	uint8_t readCmd = ((reg << 1) & 0x7E | 0x80); // Set bit 7 indicating it's a read command -> 0x80
	ssize_t transferredBytes = SPIMaster_WriteThenRead(spiFd, &readCmd, sizeof(readCmd), &readDataResult, sizeof(readDataResult));

	if (!CheckTransferSize("SPIMaster_WriteThenRead (CTRL3_C)", sizeof(readCmd) + sizeof(readDataResult), transferredBytes)) {
		Log_Debug("Transfer size is not correct");
		return -1;
	}

	Log_Debug("INFO: READ=0x%02x (SPIMaster_WriteThenRead)\n", readDataResult);

	return readDataResult;
}

Code snippet #8

Plain text
void mfrc522_write(uint8_t reg, uint8_t data)
{
	const size_t transferCount = 1;
	SPIMaster_Transfer transfer;

	int result = SPIMaster_InitTransfers(&transfer, transferCount);
	if (result != 0) {
		return;
	}

	//const uint8_t command[] = { (reg << 1) & 0x7E, data };
	const uint8_t command[] = { (reg << 1) & 0x7E, data };
	transfer.flags = SPI_TransferFlags_Write;
	transfer.writeData = command;
	transfer.length = sizeof(command);

	ssize_t transferredBytes = SPIMaster_TransferSequential(spiFd, &transfer, transferCount);

	if (!CheckTransferSize("SPIMaster_TransferSequential (CTRL3_C)", transfer.length, transferredBytes)) {
		Log_Debug("Transfer size is not correct");
		return;
	}
}

uint8_t mfrc522_read(uint8_t reg)
{
	uint8_t readDataResult;
	//uint8_t readCmd = ((reg << 1) & 0x7E) | 0x80; // Set bit 7 indicating it's a read command -> 0x80
	uint8_t readCmd = ((reg << 1) & 0x7E | 0x80); // Set bit 7 indicating it's a read command -> 0x80
	ssize_t transferredBytes = SPIMaster_WriteThenRead(spiFd, &readCmd, sizeof(readCmd), &readDataResult, sizeof(readDataResult));

	if (!CheckTransferSize("SPIMaster_WriteThenRead (CTRL3_C)", sizeof(readCmd) + sizeof(readDataResult), transferredBytes)) {
		Log_Debug("Transfer size is not correct");
		return -1;
	}

	Log_Debug("INFO: READ=0x%02x (SPIMaster_WriteThenRead)\n", readDataResult);

	return readDataResult;
}

Credits

Xavier Geerinck

Xavier Geerinck

1 project • 1 follower

Comments