add mqttclient examples for EVB_LN882x
add mqttclient examples for EVB_LN882x
This commit is contained in:
64
board/EVB_LN882x/BSP/Src/mqtt_task.c
Normal file
64
board/EVB_LN882x/BSP/Src/mqtt_task.c
Normal file
@@ -0,0 +1,64 @@
|
||||
#include "lwip/api.h"
|
||||
#include <lwip/sockets.h>
|
||||
#include <lwip/err.h>
|
||||
#include <lwip/sys.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "mqttclient.h"
|
||||
|
||||
static void tos_topic_handler(void* client, message_data_t* msg)
|
||||
{
|
||||
(void) client;
|
||||
LOG_I("-----------------------------------------------------------------------------------");
|
||||
LOG_I("%s:%d %s()...\ntopic: %s, qos: %d. \nmessage:\n\t%s\n", __FILE__, __LINE__, __FUNCTION__,
|
||||
msg->topic_name, msg->message->qos, (char*)msg->message->payload);
|
||||
LOG_I("-----------------------------------------------------------------------------------\n");
|
||||
}
|
||||
|
||||
|
||||
mqtt_client_t client;
|
||||
client_init_params_t init_params;
|
||||
|
||||
extern void TCPIP_Init(void);
|
||||
|
||||
void mqtt_iothub(void)
|
||||
{
|
||||
int error;
|
||||
char buf[80] = { 0 };
|
||||
mqtt_message_t msg;
|
||||
memset(&msg, 0, sizeof(msg));
|
||||
|
||||
sprintf(buf, "welcome to mqttclient, this is a publish test...");
|
||||
|
||||
init_params.read_buf_size = 1024;
|
||||
init_params.write_buf_size = 1024;
|
||||
init_params.connect_params.network_params.addr = "iotcloud-mqtt.gz.tencentdevices.com";
|
||||
init_params.connect_params.network_params.port = "1883";
|
||||
init_params.connect_params.user_name = "RUAP1R610Vsupowang;12010126;FTDC9;1623108655";
|
||||
init_params.connect_params.password = "29e68d809ac4ac0e41bf30cc77cd377d30e7af03f3c76b0c50fa27a2c59e174b;hmacsha256";
|
||||
init_params.connect_params.client_id = "RUAP1R610Vsupowang";
|
||||
init_params.connect_params.clean_session = 1;
|
||||
|
||||
log_init();
|
||||
|
||||
mqtt_init(&client, &init_params);
|
||||
|
||||
error = mqtt_connect(&client);
|
||||
|
||||
LOG_D("mqtt connect error is %#x", error);
|
||||
|
||||
mqtt_subscribe(&client, "RUAP1R610V/supowang/data", QOS0, tos_topic_handler);
|
||||
|
||||
memset(&msg, 0, sizeof(msg));
|
||||
|
||||
for (;;) {
|
||||
|
||||
msg.qos = QOS0;
|
||||
msg.payload = (void *) buf;
|
||||
|
||||
mqtt_publish(&client, "RUAP1R610V/supowang/data", &msg);
|
||||
|
||||
tos_task_delay(4000);
|
||||
}
|
||||
|
||||
}
|
200
board/EVB_LN882x/BSP/Src/mqttclient_examples.c
Normal file
200
board/EVB_LN882x/BSP/Src/mqttclient_examples.c
Normal file
@@ -0,0 +1,200 @@
|
||||
#include "cmsis_os.h"
|
||||
#include "osal/osal.h"
|
||||
#include "utils/debug/log.h"
|
||||
#include "utils/debug/art_assert.h"
|
||||
#include "utils/art_string.h"
|
||||
#include "wifi/wifi.h"
|
||||
#include "ping/ping.h"
|
||||
#include "netif/ethernetif.h"
|
||||
#include "wifi_manager/wifi_manager.h"
|
||||
#include "lwip/tcpip.h"
|
||||
#include "drv/drv_adc_measure.h"
|
||||
#include "utils/system_parameter.h"
|
||||
#include "hal/hal_adc.h"
|
||||
|
||||
static OS_Thread_t g_temp_cal_thread;
|
||||
#define TEMP_APP_TASK_STACK_SIZE 4*256 //Byte
|
||||
|
||||
volatile uint8_t dhcp_get_ip = 0;
|
||||
|
||||
void wifi_init_sta(void)
|
||||
{
|
||||
uint8_t macaddr[6] = {0}, macaddr_default[6] = {0};
|
||||
wifi_config_t wifi_config = {
|
||||
.sta = {
|
||||
.ssid = "Supowang",
|
||||
.password = "13975426138",
|
||||
0,
|
||||
},
|
||||
};
|
||||
|
||||
wifi_config_t temp_config = {0};
|
||||
|
||||
wifi_init_type_t init_param = {
|
||||
.wifi_mode = WIFI_MODE_STATION,
|
||||
.sta_ps_mode = WIFI_NO_POWERSAVE,
|
||||
#if 1
|
||||
.dhcp_mode = WLAN_DHCP_CLIENT,
|
||||
#else
|
||||
.dhcp_mode = WLAN_STATIC_IP,
|
||||
.local_ip_addr = "192.168.1.110",
|
||||
.net_mask = "255.255.255.0",
|
||||
.gateway_ip_addr = "192.168.1.1",
|
||||
#endif
|
||||
//In station mode, define the length of the AP list scanned.
|
||||
.scanned_ap_list_size = SCANNED_AP_LIST_SIZE,
|
||||
};
|
||||
|
||||
|
||||
//Set wifi mode
|
||||
wifi_set_mode(init_param.wifi_mode);
|
||||
|
||||
//Check mac address
|
||||
system_parameter_get_wifi_macaddr_default(STATION_IF, macaddr_default);
|
||||
wifi_get_macaddr(STATION_IF, macaddr);
|
||||
if(is_valid_mac((const char *)macaddr) && memcmp(macaddr, macaddr_default, 6) != 0){
|
||||
//If there is a valid MAC in flash, use it
|
||||
wifi_set_macaddr_current(STATION_IF, macaddr);
|
||||
}else{
|
||||
//generate random macaddr
|
||||
generate_mac_randomly(macaddr);
|
||||
wifi_set_macaddr(STATION_IF, macaddr);
|
||||
}
|
||||
|
||||
//Check config
|
||||
wifi_get_config(STATION_IF, &temp_config);
|
||||
if(strlen((const char *)temp_config.sta.ssid) > 0){
|
||||
//If there is a valid config in flash, use it;
|
||||
wifi_set_config_current(STATION_IF, &temp_config);
|
||||
}else{
|
||||
//else, use the prev wifi_config and save it to flash.
|
||||
wifi_set_config(STATION_IF, &wifi_config);
|
||||
}
|
||||
|
||||
//Startup WiFi.
|
||||
if(!wifi_start(&init_param)){//WIFI_MAX_POWERSAVE
|
||||
LOG(LOG_LVL_ERROR, "[%s, %d]wifi_start() fail.\r\n", __func__, __LINE__);
|
||||
}
|
||||
}
|
||||
|
||||
void wifi_init_ap(void)
|
||||
{
|
||||
uint8_t macaddr[6] = {0}, macaddr_default[6] = {0};
|
||||
wifi_config_t wifi_config = {
|
||||
.ap = {
|
||||
.ssid = "Supowang",
|
||||
.ssid_len = strlen("Supowang"),
|
||||
.password = "Supowang",
|
||||
.channel = 1,
|
||||
.authmode = WIFI_AUTH_OPEN,
|
||||
.ssid_hidden = 0,
|
||||
.max_connection = 4,
|
||||
.beacon_interval = 100,
|
||||
.reserved = 0,
|
||||
},
|
||||
};
|
||||
|
||||
wifi_config_t temp_config = {0};
|
||||
|
||||
wifi_init_type_t init_param = {
|
||||
.wifi_mode = WIFI_MODE_AP,
|
||||
.sta_ps_mode = WIFI_NO_POWERSAVE,
|
||||
.dhcp_mode = WLAN_DHCP_SERVER,
|
||||
};
|
||||
|
||||
//Set wifi mode
|
||||
wifi_set_mode(init_param.wifi_mode);
|
||||
|
||||
//Check mac address
|
||||
system_parameter_get_wifi_macaddr_default(SOFT_AP_IF, macaddr_default);
|
||||
wifi_get_macaddr(SOFT_AP_IF, macaddr);
|
||||
if(is_valid_mac((const char *)macaddr) && memcmp(macaddr, macaddr_default, 6) != 0){
|
||||
//If there is a valid MAC in flash, use it
|
||||
wifi_set_macaddr_current(SOFT_AP_IF, macaddr);
|
||||
}else{
|
||||
//generate random macaddr
|
||||
generate_mac_randomly(macaddr);
|
||||
wifi_set_macaddr(SOFT_AP_IF, macaddr);
|
||||
}
|
||||
|
||||
//Check config
|
||||
wifi_get_config(SOFT_AP_IF, &temp_config);
|
||||
if(strlen((const char *)temp_config.sta.ssid) > 0){
|
||||
//If there is a valid config in flash, use it;
|
||||
wifi_set_config_current(SOFT_AP_IF, &temp_config);
|
||||
}else{
|
||||
//else, use the prev wifi_config and save it to flash.
|
||||
wifi_set_config(SOFT_AP_IF, &wifi_config);
|
||||
}
|
||||
|
||||
//Startup WiFi.
|
||||
if(!wifi_start(&init_param)){
|
||||
LOG(LOG_LVL_ERROR, "[%s, %d]wifi_start() fail.\r\n", __func__, __LINE__);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void temp_cal_app_task_entry(void *params)
|
||||
{
|
||||
drv_adc_init();
|
||||
OS_MsDelay(1);
|
||||
wifi_temp_cal_init(drv_adc_read(ADC_CHAN_0));
|
||||
|
||||
while(1)
|
||||
{
|
||||
OS_MsDelay(1);
|
||||
wifi_do_temp_cal_period(drv_adc_read(ADC_CHAN_0));
|
||||
OS_MsDelay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
void wifi_event_sta_got_ip_cb(wifi_msg_t * msg)
|
||||
{
|
||||
dhcp_get_ip = 1;
|
||||
};
|
||||
|
||||
extern void mqtt_iothub(void);
|
||||
void application_entry(void *arg)
|
||||
{
|
||||
//wifi chip temperature calibration.
|
||||
if(OS_OK != OS_ThreadCreate(&g_temp_cal_thread, "TempAPP", temp_cal_app_task_entry, NULL, OS_PRIORITY_BELOW_NORMAL, TEMP_APP_TASK_STACK_SIZE)) {
|
||||
ART_ASSERT(1);
|
||||
}
|
||||
|
||||
reg_wifi_msg_callbcak(wifi_manager_get_handle(), WIFI_MSG_ID_STA_DHCP_GOT_IP,wifi_event_sta_got_ip_cb);
|
||||
|
||||
wifi_mode_enum_t wifi_mode = WIFI_MODE_STATION;
|
||||
|
||||
tcpip_ip_info_t ip_info = {0};
|
||||
wifi_interface_enum_t if_index;
|
||||
|
||||
if(wifi_mode == WIFI_MODE_STATION){
|
||||
if_index = STATION_IF;
|
||||
wifi_init_sta();
|
||||
}else if(wifi_mode == WIFI_MODE_AP){
|
||||
if_index = SOFT_AP_IF;
|
||||
wifi_init_ap();
|
||||
}else if(wifi_mode == WIFI_MODE_MONITOR){
|
||||
if_index = MONITOR_IF;
|
||||
}else{
|
||||
LOG(LOG_LVL_ERROR, "wifi interface error!\r\n");
|
||||
}
|
||||
|
||||
if((wifi_mode == WIFI_MODE_STATION)||(wifi_mode == WIFI_MODE_AP)) {
|
||||
//Wait for network link up
|
||||
while(LINK_UP != ethernetif_get_link_state()){
|
||||
OS_MsDelay(1000);
|
||||
}
|
||||
|
||||
ethernetif_get_ip_info(if_index, &ip_info);
|
||||
}
|
||||
|
||||
while(!dhcp_get_ip){
|
||||
OS_MsDelay(1000);
|
||||
};
|
||||
|
||||
mqtt_iothub();
|
||||
}
|
||||
|
||||
|
||||
|
39
board/EVB_LN882x/KEIL/mqtt_client/JLinkSettings.ini
Normal file
39
board/EVB_LN882x/KEIL/mqtt_client/JLinkSettings.ini
Normal file
@@ -0,0 +1,39 @@
|
||||
[BREAKPOINTS]
|
||||
ForceImpTypeAny = 0
|
||||
ShowInfoWin = 1
|
||||
EnableFlashBP = 2
|
||||
BPDuringExecution = 0
|
||||
[CFI]
|
||||
CFISize = 0x00
|
||||
CFIAddr = 0x00
|
||||
[CPU]
|
||||
MonModeVTableAddr = 0xFFFFFFFF
|
||||
MonModeDebug = 0
|
||||
MaxNumAPs = 0
|
||||
LowPowerHandlingMode = 0
|
||||
OverrideMemMap = 0
|
||||
AllowSimulation = 1
|
||||
ScriptFile=""
|
||||
[FLASH]
|
||||
CacheExcludeSize = 0x00
|
||||
CacheExcludeAddr = 0x00
|
||||
MinNumBytesFlashDL = 0
|
||||
SkipProgOnCRCMatch = 1
|
||||
VerifyDownload = 1
|
||||
AllowCaching = 1
|
||||
EnableFlashDL = 2
|
||||
Override = 1
|
||||
Device="Cortex-M4"
|
||||
[GENERAL]
|
||||
WorkRAMSize = 0x00
|
||||
WorkRAMAddr = 0x00
|
||||
RAMUsageLimit = 0x00
|
||||
[SWO]
|
||||
SWOLogFile=""
|
||||
[MEM]
|
||||
RdOverrideOrMask = 0x00
|
||||
RdOverrideAndMask = 0xFFFFFFFF
|
||||
RdOverrideAddr = 0xFFFFFFFF
|
||||
WrOverrideOrMask = 0x00
|
||||
WrOverrideAndMask = 0xFFFFFFFF
|
||||
WrOverrideAddr = 0xFFFFFFFF
|
2341
board/EVB_LN882x/KEIL/mqtt_client/TencentOS_tiny.uvoptx
Normal file
2341
board/EVB_LN882x/KEIL/mqtt_client/TencentOS_tiny.uvoptx
Normal file
File diff suppressed because it is too large
Load Diff
1419
board/EVB_LN882x/KEIL/mqtt_client/TencentOS_tiny.uvprojx
Normal file
1419
board/EVB_LN882x/KEIL/mqtt_client/TencentOS_tiny.uvprojx
Normal file
File diff suppressed because it is too large
Load Diff
8
board/EVB_LN882x/KEIL/mqtt_client/after_build_ln882x.bat
Normal file
8
board/EVB_LN882x/KEIL/mqtt_client/after_build_ln882x.bat
Normal file
@@ -0,0 +1,8 @@
|
||||
|
||||
python ..\..\..\..\platform\vendor_bsp\LN\ln882x\tools\user_cmd\after_build_soc.py firmware_XIP
|
||||
|
||||
..\..\..\..\platform\vendor_bsp\LN\ln882x\tools\bin\mkimage.exe ln882x flashimage ..\..\..\..\platform\vendor_bsp\LN\ln882x\lib\boot_ram_ln882x.bin firmware_XIP.bin flashimage.bin release=1 crp_enable=0 app_version=10 hw_version=0
|
||||
|
||||
..\..\..\..\platform\vendor_bsp\LN\ln882x\tools\bin\fpu_patch.exe .\firmware_XIP.asm .\flashimage.bin
|
||||
|
||||
|
78
board/EVB_LN882x/KEIL/mqtt_client/firmware_XIP_ln882x.sct
Normal file
78
board/EVB_LN882x/KEIL/mqtt_client/firmware_XIP_ln882x.sct
Normal file
@@ -0,0 +1,78 @@
|
||||
#! armcc -E
|
||||
#include ".\flash_partition_table.h"
|
||||
#include ".\..\..\..\..\platform\vendor_bsp\LN\ln882x\include\cpu\ARM_CM4F\ln88xx\mem_map_ln882x.h"
|
||||
|
||||
|
||||
LR_FLASH FLASH_BASE+PRIMARY_PARTITION_OFFSET PRIMARY_PATTITION_SIZE
|
||||
{; load region size_region
|
||||
|
||||
ISR_VECTOR RAM_BASE ALIGN 0x100
|
||||
{
|
||||
startup_*.o (RESET, +First)
|
||||
}
|
||||
|
||||
ER_FLASH FLASH_BASE+PRIMARY_PARTITION_OFFSET + ImageLength(ISR_VECTOR) PRIMARY_PATTITION_SIZE
|
||||
{; load address = execution address
|
||||
*(InRoot$$Sections)
|
||||
.ANY (+RO)
|
||||
}
|
||||
|
||||
ER_CODE ImageLimit(ISR_VECTOR)
|
||||
{
|
||||
;mandatory item
|
||||
ll_qspi.o(+RO)
|
||||
ll_cache.o(+RO)
|
||||
ll_dma.o(+RO)
|
||||
ll_syscon.o(+RO)
|
||||
ll_sleep.o(+RO)
|
||||
ll_rtc.o(+RO)
|
||||
cache.o(+RO)
|
||||
qspi.o(+RO)
|
||||
flash.o(+RO)
|
||||
hal_dma.o(+RO)
|
||||
hal_syscon.o(+RO)
|
||||
hal_sleep.o(+RO)
|
||||
hal_rtc.o(+RO)
|
||||
;port.o(XIR)
|
||||
|
||||
;optional item
|
||||
;ll_uart.o(+RO)
|
||||
;hal_uart.o(+RO)
|
||||
;log.o(+RO)
|
||||
;serial.o(+RO)
|
||||
;serial_art2000.o(+RO)
|
||||
;fifobuf.o(+RO)
|
||||
}
|
||||
|
||||
ER_STACK +0
|
||||
{
|
||||
startup_ln*(+RW +ZI)
|
||||
}
|
||||
|
||||
ER_DATA +0
|
||||
{
|
||||
*(.ARM.__at_0x1FFFFFFC)
|
||||
.ANY (+RW +ZI)
|
||||
}
|
||||
|
||||
;IQ Data area cannot be used by ER_DATA region
|
||||
;ScatterAssert(ImageLimit(ER_DATA) < (0x20020300))
|
||||
|
||||
HEAP_SPACE +0 EMPTY RETENTION_MEM_BASE-ImageLimit(ER_DATA)
|
||||
{; Heap region
|
||||
}
|
||||
|
||||
NO_INIT_DATA RETENTION_MEM_BASE UNINIT 0x80
|
||||
{
|
||||
*(no_init_data)
|
||||
}
|
||||
|
||||
RETENTION RETENTION_MEM_BASE+0x80 ANY_SIZE 0x2000-0x80-16 RETENTION_MEM_SIZE
|
||||
{
|
||||
*(retention_data)
|
||||
phy_LN.o(+RW +ZI)
|
||||
ch_info.o(+RW +ZI)
|
||||
}
|
||||
}
|
||||
|
||||
|
32
board/EVB_LN882x/KEIL/mqtt_client/flash_partition_table.h
Normal file
32
board/EVB_LN882x/KEIL/mqtt_client/flash_partition_table.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef __FLASH_PARTITION_TABLE_H__
|
||||
#define __FLASH_PARTITION_TABLE_H__
|
||||
|
||||
//#define RETENTION_MEM_BASE 0x20028000
|
||||
//#define RETENTION_MEM_SIZE 0x2000
|
||||
|
||||
#ifndef FLASH_BASE
|
||||
#define FLASH_BASE (0x10000000)
|
||||
#endif
|
||||
|
||||
#ifndef FLASH_SIZE
|
||||
#define FLASH_SIZE (0x00200000)
|
||||
#endif
|
||||
|
||||
#ifndef PRIMARY_PARTITION_OFFSET
|
||||
#define PRIMARY_PARTITION_OFFSET (0x0000F000)
|
||||
#endif
|
||||
|
||||
#define PRIMARY_PATTITION_START 0x0000F000
|
||||
#define PRIMARY_PATTITION_SIZE 0xE1000
|
||||
//#define USERAPP_SRAM_BASE 0x1FFF0000
|
||||
#define BACKUP_PATTITION_START 0x000F0000
|
||||
#define BACKUP_PATTITION_SIZE 0xE1000
|
||||
#define USERDATA_PATTITION_START 0x001D1000
|
||||
#define USERDATA_PATTITION_SIZE 0x23000
|
||||
|
||||
//memory offset check
|
||||
#if ((PRIMARY_PATTITION_START < PRIMARY_PARTITION_OFFSET) || (BACKUP_PATTITION_START < PRIMARY_PATTITION_START) || (USERDATA_PATTITION_START < BACKUP_PATTITION_START) || (PRIMARY_PATTITION_START + PRIMARY_PATTITION_SIZE > BACKUP_PATTITION_START) || (BACKUP_PATTITION_START + BACKUP_PATTITION_SIZE > USERDATA_PATTITION_START) || (USERDATA_PATTITION_START + USERDATA_PATTITION_SIZE > FLASH_SIZE))
|
||||
#error "flash partition define error!"
|
||||
#endif
|
||||
|
||||
#endif /* __FLASH_PARTITION_TABLE_H__ */
|
47
board/EVB_LN882x/TOS-CONFIG/mqtt_config.h
Normal file
47
board/EVB_LN882x/TOS-CONFIG/mqtt_config.h
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-15 00:42:16
|
||||
* @LastEditTime: 2020-02-25 09:25:35
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#ifndef _MQTT_CONFIG_H_
|
||||
#define _MQTT_CONFIG_H_
|
||||
|
||||
//#define LOG_IS_SALOF
|
||||
|
||||
#define LOG_LEVEL DEBUG_LEVEL //WARN_LEVEL DEBUG_LEVEL
|
||||
|
||||
#ifdef LOG_IS_SALOF
|
||||
#define USE_LOG (1U)
|
||||
#define USE_SALOF (1U)
|
||||
#define SALOF_OS USE_TENCENTOS
|
||||
#define USE_IDLE_HOOK (0U)
|
||||
#define LOG_COLOR (0U)
|
||||
#define LOG_TS (0U)
|
||||
#define LOG_TAR (0U)
|
||||
#define SALOF_BUFF_SIZE (512U)
|
||||
#define SALOF_FIFO_SIZE (1024U)
|
||||
#define SALOF_TASK_STACK_SIZE (1024U)
|
||||
#define SALOF_TASK_TICK (50U)
|
||||
#endif
|
||||
|
||||
|
||||
#define MQTT_MAX_PACKET_ID (0xFFFF - 1)
|
||||
#define MQTT_TOPIC_LEN_MAX 64
|
||||
#define MQTT_ACK_HANDLER_NUM_MAX 64
|
||||
#define MQTT_DEFAULT_BUF_SIZE 1024
|
||||
#define MQTT_DEFAULT_CMD_TIMEOUT 4000
|
||||
#define MQTT_MAX_CMD_TIMEOUT 20000
|
||||
#define MQTT_MIN_CMD_TIMEOUT 1000
|
||||
#define MQTT_KEEP_ALIVE_INTERVAL 100 // unit: second
|
||||
#define MQTT_VERSION 4 // 4 is mqtt 3.1.1
|
||||
#define MQTT_RECONNECT_DEFAULT_DURATION 1000
|
||||
#define MQTT_THREAD_STACK_SIZE 2048
|
||||
#define MQTT_THREAD_PRIO 5
|
||||
#define MQTT_THREAD_TICK 50
|
||||
|
||||
|
||||
//#define MQTT_NETWORK_TYPE_TLS
|
||||
|
||||
#endif /* _MQTT_CONFIG_H_ */
|
Reference in New Issue
Block a user