MCUXpresso_MIMXRT1052xxxxB/boards/evkbimxrt1050/azure_rtos_examples/azure_iot_mqtt/main.c
Yilin Sun 75f32185d2
Updated to v2.14.0
Signed-off-by: Yilin Sun <imi415@imi.moe>
2023-11-30 20:55:00 +08:00

389 lines
12 KiB
C

/**************************************************************************/
/* */
/* Copyright (c) Microsoft Corporation. All rights reserved. */
/* */
/* This software is licensed under the Microsoft Software License */
/* Terms for Microsoft Azure RTOS. Full text of the license can be */
/* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */
/* and in the root directory of this software. */
/* */
/**************************************************************************/
#include "fsl_common.h"
#include "fsl_debug_console.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "board.h"
#include "nx_api.h"
#ifndef SAMPLE_DHCP_DISABLE
#include "nxd_dhcp_client.h"
#endif /* SAMPLE_DHCP_DISABLE */
#include "nxd_dns.h"
#include "nx_secure_tls_api.h"
#include "fsl_iomuxc.h"
/*******************************************************************************
* Definitions
******************************************************************************/
/* Define the helper thread for running Azure SDK on ThreadX (THREADX IoT Platform). */
#ifndef SAMPLE_HELPER_STACK_SIZE
#define SAMPLE_HELPER_STACK_SIZE (4096)
#endif /* SAMPLE_HELPER_STACK_SIZE */
#ifndef SAMPLE_HELPER_THREAD_PRIORITY
#define SAMPLE_HELPER_THREAD_PRIORITY (4)
#endif /* SAMPLE_HELPER_THREAD_PRIORITY */
/* Define user configurable symbols. */
#ifndef SAMPLE_IP_STACK_SIZE
#define SAMPLE_IP_STACK_SIZE (2048)
#endif /* SAMPLE_IP_STACK_SIZE */
#ifndef SAMPLE_PACKET_COUNT
#define SAMPLE_PACKET_COUNT (32)
#endif /* SAMPLE_PACKET_COUNT */
#ifndef SAMPLE_PACKET_SIZE
#define SAMPLE_PACKET_SIZE (1536)
#endif /* SAMPLE_PACKET_SIZE */
#define SAMPLE_POOL_SIZE ((SAMPLE_PACKET_SIZE + sizeof(NX_PACKET)) * SAMPLE_PACKET_COUNT)
#ifndef SAMPLE_ARP_CACHE_SIZE
#define SAMPLE_ARP_CACHE_SIZE (512)
#endif /* SAMPLE_ARP_CACHE_SIZE */
#ifndef SAMPLE_IP_THREAD_PRIORITY
#define SAMPLE_IP_THREAD_PRIORITY (1)
#endif /* SAMPLE_IP_THREAD_PRIORITY */
#ifdef SAMPLE_DHCP_DISABLE
#ifndef SAMPLE_IPV4_ADDRESS
/*#define SAMPLE_IPV4_ADDRESS IP_ADDRESS(192, 168, 100, 33)*/
#error "SYMBOL SAMPLE_IPV4_ADDRESS must be defined. This symbol specifies the IP address of device. "
#endif /* SAMPLE_IPV4_ADDRESS */
#ifndef SAMPLE_IPV4_MASK
/*#define SAMPLE_IPV4_MASK 0xFFFFFF00UL*/
#error "SYMBOL SAMPLE_IPV4_MASK must be defined. This symbol specifies the IP address mask of device. "
#endif /* SAMPLE_IPV4_MASK */
#ifndef SAMPLE_GATEWAY_ADDRESS
/*#define SAMPLE_GATEWAY_ADDRESS IP_ADDRESS(192, 168, 100, 1)*/
#error "SYMBOL SAMPLE_GATEWAY_ADDRESS must be defined. This symbol specifies the gateway address for routing. "
#endif /* SAMPLE_GATEWAY_ADDRESS */
#ifndef SAMPLE_DNS_SERVER_ADDRESS
/*#define SAMPLE_DNS_SERVER_ADDRESS IP_ADDRESS(192, 168, 100, 1)*/
#error "SYMBOL SAMPLE_DNS_SERVER_ADDRESS must be defined. This symbol specifies the dns server address for routing. "
#endif /* SAMPLE_DNS_SERVER_ADDRESS */
#else
#define SAMPLE_IPV4_ADDRESS IP_ADDRESS(0, 0, 0, 0)
#define SAMPLE_IPV4_MASK IP_ADDRESS(0, 0, 0, 0)
#endif /* SAMPLE_DHCP_DISABLE */
/*******************************************************************************
* Variables
******************************************************************************/
static TX_THREAD sample_helper_thread;
static NX_PACKET_POOL pool_0;
static NX_IP ip_0;
static NX_DNS dns_0;
#ifndef SAMPLE_DHCP_DISABLE
AT_NONCACHEABLE_SECTION_ALIGN(NX_DHCP dhcp_0, 64);
#endif /* SAMPLE_DHCP_DISABLE */
/* Define the stack/cache for ThreadX. */
static ULONG sample_ip_stack[SAMPLE_IP_STACK_SIZE / sizeof(ULONG)];
#ifndef SAMPLE_POOL_STACK_USER
AT_NONCACHEABLE_SECTION_ALIGN(ULONG sample_pool_stack[SAMPLE_POOL_SIZE / sizeof(ULONG)], 64);
static ULONG sample_pool_stack_size = sizeof(sample_pool_stack);
#else
extern ULONG sample_pool_stack[];
extern ULONG sample_pool_stack_size;
#endif
static ULONG sample_arp_cache_area[SAMPLE_ARP_CACHE_SIZE / sizeof(ULONG)];
static ULONG sample_helper_thread_stack[SAMPLE_HELPER_STACK_SIZE / sizeof(ULONG)];
/*******************************************************************************
* Prototypes
******************************************************************************/
/* Define the prototypes for sample thread. */
static void sample_helper_thread_entry(ULONG parameter);
#ifndef SAMPLE_DHCP_DISABLE
static void dhcp_wait();
#endif /* SAMPLE_DHCP_DISABLE */
static UINT dns_create();
extern VOID sample_entry(NX_IP *ip_ptr, NX_PACKET_POOL *pool_ptr, NX_DNS *dns_ptr);
/* Include the platform IP driver. */
extern VOID nx_link_driver(NX_IP_DRIVER *driver_req_ptr);
extern uint32_t get_seed(void);
/*******************************************************************************
* Code
******************************************************************************/
static void delay(void)
{
volatile uint32_t i = 0;
for (i = 0; i < 1000000; ++i)
{
__asm("NOP"); /* delay */
}
}
/* return the ENET MDIO interface clock frequency */
uint32_t BOARD_GetMDIOClock(void)
{
return CLOCK_GetFreq(kCLOCK_IpgClk);
}
/* Define main entry point. */
int main(void)
{
/* Init board hardware. */
gpio_pin_config_t gpio_config = {kGPIO_DigitalOutput, 0, kGPIO_NoIntmode};
const clock_enet_pll_config_t config = {
.enableClkOutput = true,
.enableClkOutput25M = false,
.loopDivider = 1
};
/* Enable cycle counter for tx_exection_profile.c use */
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
DWT->LAR = 0xC5ACCE55;
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
BOARD_ConfigMPU();
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitDebugConsole();
CLOCK_InitEnetPll(&config);
delay();
IOMUXC_EnableMode(IOMUXC_GPR, kIOMUXC_GPR_ENET1TxClkOutputDir, true);
GPIO_PinInit(GPIO1, 9, &gpio_config);
GPIO_PinInit(GPIO1, 10, &gpio_config);
/* pull up the ENET_INT before RESET. */
GPIO_WritePinOutput(GPIO1, 10, 1);
GPIO_WritePinOutput(GPIO1, 9, 0);
delay();
GPIO_WritePinOutput(GPIO1, 9, 1);
PRINTF("Start the azure_iot_mqtt example...\r\n");
/* Enter the ThreadX kernel. */
tx_kernel_enter();
return 0;
}
/* Define what the initial system looks like. */
void tx_application_define(void *first_unused_memory)
{
UINT status;
/* Initialize the NetX system. */
nx_system_initialize();
/* Create a packet pool. */
status = nx_packet_pool_create(&pool_0, "NetX Main Packet Pool", SAMPLE_PACKET_SIZE, (UCHAR *)sample_pool_stack,
sample_pool_stack_size);
/* Check for pool creation error. */
if (status)
{
PRINTF("nx_packet_pool_create fail: %u\r\n", status);
return;
}
/* Create an IP instance. */
status = nx_ip_create(&ip_0, "NetX IP Instance 0", SAMPLE_IPV4_ADDRESS, SAMPLE_IPV4_MASK, &pool_0, nx_link_driver,
(UCHAR *)sample_ip_stack, sizeof(sample_ip_stack), SAMPLE_IP_THREAD_PRIORITY);
/* Check for IP create errors. */
if (status)
{
PRINTF("nx_ip_create fail: %u\r\n", status);
return;
}
/* Enable ARP and supply ARP cache memory for IP Instance 0. */
status = nx_arp_enable(&ip_0, (VOID *)sample_arp_cache_area, sizeof(sample_arp_cache_area));
/* Check for ARP enable errors. */
if (status)
{
PRINTF("nx_arp_enable fail: %u\r\n", status);
return;
}
/* Enable ICMP traffic. */
status = nx_icmp_enable(&ip_0);
/* Check for ICMP enable errors. */
if (status)
{
PRINTF("nx_icmp_enable fail: %u\r\n", status);
return;
}
/* Enable TCP traffic. */
status = nx_tcp_enable(&ip_0);
/* Check for TCP enable errors. */
if (status)
{
PRINTF("nx_tcp_enable fail: %u\r\n", status);
return;
}
/* Enable UDP traffic. */
status = nx_udp_enable(&ip_0);
/* Check for UDP enable errors. */
if (status)
{
PRINTF("nx_udp_enable fail: %u\r\n", status);
return;
}
/* Initialize TLS. */
nx_secure_tls_initialize();
/* Create sample helper thread. */
status = tx_thread_create(&sample_helper_thread, "Demo Thread", sample_helper_thread_entry, 0,
sample_helper_thread_stack, SAMPLE_HELPER_STACK_SIZE, SAMPLE_HELPER_THREAD_PRIORITY,
SAMPLE_HELPER_THREAD_PRIORITY, TX_NO_TIME_SLICE, TX_AUTO_START);
/* Check status. */
if (status)
{
PRINTF("Demo helper thread creation fail: %u\r\n", status);
return;
}
}
/* Define sample helper thread entry. */
static void sample_helper_thread_entry(ULONG parameter)
{
UINT status;
ULONG ip_address = 0;
ULONG network_mask = 0;
ULONG gateway_address = 0;
/* Use a random number to init the seed. */
srand(get_seed());
#ifndef AZRTOS_IOT_CLIENT_DHCP_DISABLE
dhcp_wait();
#else
nx_ip_gateway_address_set(&ip_0, SAMPLE_GATEWAY_ADDRESS);
#endif /* AZRTOS_IOT_CLIENT_DHCP_DISABLE */
/* Get IP address and gateway address. */
nx_ip_address_get(&ip_0, &ip_address, &network_mask);
nx_ip_gateway_address_get(&ip_0, &gateway_address);
/* Output IP address and gateway address. */
PRINTF("IP address: %lu.%lu.%lu.%lu\r\n", (ip_address >> 24), (ip_address >> 16 & 0xFF), (ip_address >> 8 & 0xFF),
(ip_address & 0xFF));
PRINTF("Mask: %lu.%lu.%lu.%lu\r\n", (network_mask >> 24), (network_mask >> 16 & 0xFF), (network_mask >> 8 & 0xFF),
(network_mask & 0xFF));
PRINTF("Gateway: %lu.%lu.%lu.%lu\r\n", (gateway_address >> 24), (gateway_address >> 16 & 0xFF),
(gateway_address >> 8 & 0xFF), (gateway_address & 0xFF));
/* Create DNS. */
status = dns_create();
/* Check for DNS create errors. */
if (status)
{
PRINTF("dns_create fail: %u\r\n", status);
return;
}
/* Start sample. */
sample_entry(&ip_0, &pool_0, &dns_0);
}
#ifndef SAMPLE_DHCP_DISABLE
static void dhcp_wait()
{
ULONG actual_status;
PRINTF("DHCP In Progress...\r\n");
/* Create the DHCP instance. */
nx_dhcp_create(&dhcp_0, &ip_0, "DHCP Client");
/* Start the DHCP Client. */
nx_dhcp_start(&dhcp_0);
/* Wait util address is solved. */
nx_ip_status_check(&ip_0, NX_IP_ADDRESS_RESOLVED, &actual_status, NX_WAIT_FOREVER);
}
#endif /* SAMPLE_DHCP_DISABLE */
static UINT dns_create()
{
UINT status;
ULONG dns_server_address[3];
UINT dns_server_address_size = 12;
/* Create a DNS instance for the Client. Note this function will create
the DNS Client packet pool for creating DNS message packets intended
for querying its DNS server. */
status = nx_dns_create(&dns_0, &ip_0, (UCHAR *)"DNS Client");
if (status)
{
return (status);
}
/* Is the DNS client configured for the host application to create the pecket pool? */
#ifdef NX_DNS_CLIENT_USER_CREATE_PACKET_POOL
/* Yes, use the packet pool created above which has appropriate payload size
for DNS messages. */
status = nx_dns_packet_pool_set(&dns_0, ip_0.nx_ip_default_packet_pool);
if (status)
{
nx_dns_delete(&dns_0);
return (status);
}
#endif /* NX_DNS_CLIENT_USER_CREATE_PACKET_POOL */
#ifndef SAMPLE_DHCP_DISABLE
/* Retrieve DNS server address. */
nx_dhcp_interface_user_option_retrieve(&dhcp_0, 0, NX_DHCP_OPTION_DNS_SVR, (UCHAR *)(dns_server_address),
&dns_server_address_size);
#else
dns_server_address[0] = SAMPLE_DNS_SERVER_ADDRESS;
#endif /* AZRTOS_IOT_CLIENT_DHCP_DISABLE */
/* Add an IPv4 server address to the Client list. */
status = nx_dns_server_add(&dns_0, dns_server_address[0]);
if (status)
{
nx_dns_delete(&dns_0);
return (status);
}
/* Output DNS Server address. */
PRINTF("DNS Server address: %lu.%lu.%lu.%lu\r\n", (dns_server_address[0] >> 24),
(dns_server_address[0] >> 16 & 0xFF), (dns_server_address[0] >> 8 & 0xFF), (dns_server_address[0] & 0xFF));
return (NX_SUCCESS);
}