中级
20 分钟

使用DA16200MOD-AA和PIC32MZ1024EFH064重新构想连接世界的方式

随时随地通过超低功耗Wi-Fi创新连接!

LP WiFi Click with PIC32MZ clicker

已发布 6月 24, 2024

点击板

LP WiFi Click

开发板

PIC32MZ clicker

编译器

NECTO Studio

微控制器单元

PIC32MZ1024EFH064

随着我们引入创新的超低功耗Wi-Fi解决方案,您将体验到无缝连接的新时代,重新定义您在移动时保持连接的方式。

A

A

硬件概览

它是如何工作的?

LP WiFi Click基于瑞萨的DA16200MOD-AA,这是一款高度集成的超低功耗WiFi模块,具有最佳的射频性能和舒适的开发环境。它由DA16200 SoC、4MB闪存存储器、射频组件、晶体振荡器、射频集成滤波器和一枚内置的2.4GHz芯片天线组成。这种低功耗操作可以延长电池寿命一年甚至更长,具体取决于应用场景,即使板上持续连接到WiFi网络。该模块还具有强大的物联网安全性,包括用于WiFi和更高堆栈层的身份验证和加密的WPA3和TLS。DA16200MOD具有集成的RTC块(36位实时计数器),带有32.768kHz时钟源,用于SoC的RTC块中的自由运行计数器,

提供电源管理和功能控制以进行低功耗操作。当正常操作期间板上标记为POWER的开关设置为适当的ON位置时,RTC块始终处于通电状态。这样可以优化功耗,并用于开关电源的目的。此外,板的左上角标记为RTC块Pads的标头连接到内部RTC块,用于连接并从外部设备(如传感器)接收外部事件信号。LP WiFi Click使用UART接口作为其默认通信协议与MCU通信。用户可以使用其他接口,如SPI和I2C,以改进和与外围设备通信。值得注意的是,DA16200模块附带的固件仅支持与主机微控制器的UART通信和与传感器等外围设备的I2C通信(SPI接口默认不支持,

可以通过固件更新启用)。此板还具有某些按钮和标头。板载的按钮标记为RESET和WPS,分别表示恢复出厂设置按钮和WiFi Protected Setup(WPS)按钮。至于标头,第一个标有TX和RX信号的标头适用于使用UART接口进行调试,而第二个标头可以通过将其连接到带有I2C信号的右上标头来向设计中添加一个小而明亮、清晰的OLED显示屏。此Click board™只能在3.3V逻辑电压级别下运行。在使用具有不同逻辑电压级别的MCU之前,板必须执行适当的逻辑电压级别转换。此外,它配备了一个包含函数和示例代码的库,可用作进一步开发的参考。

LP WiFi Click hardware overview image

功能概述

开发板

PIC32MZ Clicker 是一款紧凑型入门开发板,它将 Click 板™的灵活性带给您喜爱的微控制器,使其成为实现您想法的完美入门套件。它配备了一款板载 32 位带有浮点单元的 Microchip PIC32MZ 微控制器,一个 USB 连接器,LED 指示灯,按钮,一个 mikroProg 连接器,以及一个用于与外部电子设备接口的头部。得益于其紧凑的设计和清晰易识别的丝网标记,它提供了流畅且沉浸式的工作体验,允许在任

何情况下、任何地方都能访问。PIC32MZ Clicker 开 发套件的每个部分都包含了使同一板块运行最高效的必要组件。除了可以选择 PIC32MZ Clicker 的编程方式,使用 USB HID mikroBootloader 或通过外部 mikroProg 连接器为 PIC,dsPIC 或 PIC32 编程外,Clicker 板还包括一个干净且调节过的开发套件电源供应模块。USB Micro-B 连接可以提供多达 500mA 的电流,这足以操作所有板载和附加模块。所有 

mikroBUS™ 本身支持的通信方法都在这块板上,包 括已经建立良好的 mikroBUS™ 插槽、重置按钮以及若干按钮和 LED 指示灯。PIC32MZ Clicker 是 Mikroe 生态系统的一个组成部分,允许您在几分钟内创建新的应用程序。它由 Mikroe 软件工具原生支持,得益于大量不同的 Click 板™(超过一千块板),其数量每天都在增长,它涵盖了原型制作的许多方面。

PIC32MZ clicker double side image

微控制器概述 

MCU卡片 / MCU

default

建筑

PIC32

MCU 内存 (KB)

1024

硅供应商

Microchip

引脚数

64

RAM (字节)

524288

使用的MCU引脚

mikroBUS™映射器

NC
NC
AN
Wake-Up
RE5
RST
SPI Chip Select
RG9
CS
SPI Clock
RG6
SCK
SPI Data OUT
RG7
MISO
SPI Data IN
RG8
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
PWM Signal
RB3
PWM
Interrupt
RB5
INT
UART TX
RB2
TX
UART RX
RB0
RX
I2C Clock
RD10
SCL
I2C Data
RD9
SDA
NC
NC
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

LP WiFi Click Schematic schematic

一步一步来

项目组装

PIC32MZ clicker front image hardware assembly

从选择您的开发板和Click板™开始。以PIC32MZ clicker作为您的开发板开始。

PIC32MZ clicker front image hardware assembly
GNSS2 Click front image hardware assembly
Prog-cut hardware assembly
Board mapper by product7 hardware assembly
Necto image step 2 hardware assembly
Necto image step 3 hardware assembly
Necto image step 4 hardware assembly
Necto image step 5 hardware assembly
Necto image step 6 hardware assembly
Flip&Click PIC32MZ MCU step hardware assembly
Necto No Display image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Debug Image Necto Step hardware assembly

软件支持

库描述

该库包含 LP WiFi Click 驱动程序的 API。

关键功能:

  • lpwifi_send_cmd - 发送命令函数。

  • lpwifi_connect_to_ap - 连接到AP函数。

  • lpwifi_factory_reset_device - 设备恢复出厂设置函数。

开源

代码示例

完整的应用程序代码和一个现成的项目可以通过NECTO Studio包管理器直接安装到NECTO Studio 应用程序代码也可以在MIKROE的GitHub账户中找到。

/*!
 * @file main.c
 * @brief LP WiFi Click Example.
 *
 * # Description
 * This example reads and processes data from LP WiFi Clicks.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes the driver and powers up the module, then connects to the desired AP
 * and creates TCP and UDP servers on the desired local port.
 *
 * ## Application Task
 * Logs all the data received from TCP/UDP clients on the USB UART.
 *
 * ## Additional Function
 * - static void lpwifi_clear_app_buf ( void )
 * - static err_t lpwifi_process ( void )
 * - static void lpwifi_error_check( err_t error_flag )
 * - static void lpwifi_log_app_buf ( void )
 * - static err_t lpwifi_rsp_check ( void )
 * - static void lpwifi_check_connection( void )
 *
 * @note
 * In order for the example to work, user needs to set the AP SSID, password, and Local port
 * on which the TCP server and UDP socket will be created.
 * Enter valid data for the following macros: AP_SSID, AP_PASSWORD and LOCAL_PORT.
 *
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "lpwifi.h"
#include "string.h"
#include "generic_pointer.h"

// Set AP SSID
#define AP_SSID                     ""

// Set AP password - if the AP is OPEN remain this NULL
#define AP_PASSWORD                 ""

// Set Local port on which the TCP server and UDP socket will be created.
#define LOCAL_PORT                  1

#define APP_OK                      0
#define APP_ERROR_DRIVER            -1
#define APP_ERROR_OVERFLOW          -2
#define APP_ERROR_TIMEOUT           -3

#define RSP_OK                      "OK"
#define RSP_ERROR                   "ERROR"

#define PROCESS_BUFFER_SIZE         400

#define WAIT_FOR_CONNECTION         0
#define CONNECTED_TO_AP             1
#define NOT_CONNECTED_TO_AP         2

static lpwifi_t lpwifi;
static log_t logger;

static char app_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
static int32_t app_buf_len                 = 0;
static int32_t app_buf_cnt                 = 0;

static char assigned_ip_address[ 25 ]      = { 0 };
static uint8_t app_connection_status       = WAIT_FOR_CONNECTION;

static err_t app_error_flag;

/**
 * @brief LP WiFi clearing application buffer.
 * @details This function clears memory of application buffer and reset it's length and counter.
 * @note None.
 */
static void lpwifi_clear_app_buf ( void );

/**
 * @brief LP WiFi data reading function.
 * @details This function reads data from device and concatenates data to application buffer.
 *
 * @return @li @c  0 - Read some data.
 *         @li @c -1 - Nothing is read.
 *         @li @c -2 - Application buffer overflow.
 *
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t lpwifi_process ( void );

/**
 * @brief LP WiFi check for errors.
 * @details This function checks for different types of errors and logs them on UART.
 * @note None.
 */
static void lpwifi_error_check( err_t error_flag );

/**
 * @brief LP WiFi logs application buffer.
 * @details This function logs data from application buffer.
 * @note None.
 */
static void lpwifi_log_app_buf ( void );

/**
 * @brief LP WiFi response check.
 * @details This function checks for response and returns the status of response.
 *
 * @return application status.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t lpwifi_rsp_check ( void );

/**
 * @brief LP WiFi check connection.
 * @details This function checks connection to the AP, and fills the assigned_ip_address buffer and
 *          logs the response on the USB UART if it is connected successfully.
 *
 * @note None.
 */
static void lpwifi_check_connection( void );

/**
 * @brief LP WiFi str cut chr function.
 * @details This function removes all selected characters from string str,
 * and returns it to the same str without those characters.
 * @param str Address of string.
 * @param chr Character to cut.
 */
static void lpwifi_str_cut_chr ( uint8_t *str, uint8_t chr );

void application_init ( void )
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    lpwifi_cfg_t lpwifi_cfg;  /**< Click config object. */

    /**
     * Logger initialization.
     * Default baud rate: 115200
     * Default log level: LOG_LEVEL_DEBUG
     * @note If USB_UART_RX and USB_UART_TX
     * are defined as HAL_PIN_NC, you will
     * need to define them manually for log to work.
     * See @b LOG_MAP_USB_UART macro definition for detailed explanation.
     */
    LOG_MAP_USB_UART( log_cfg );
    log_init( &logger, &log_cfg );
    log_info( &logger, " Application Init " );
    Delay_ms ( 100 );

    // Click initialization.

    lpwifi_cfg_setup( &lpwifi_cfg );
    LPWIFI_MAP_MIKROBUS( lpwifi_cfg, MIKROBUS_1 );
    err_t init_flag = lpwifi_init( &lpwifi, &lpwifi_cfg );
    if ( UART_ERROR == init_flag )
    {
        log_error( &logger, " Application Init Error. " );
        log_info( &logger, " Please, run program again... " );

        for ( ; ; );
    }

    lpwifi_default_cfg( &lpwifi );
    Delay_ms ( 500 );

    // Initiate the communication
    lpwifi_send_cmd( &lpwifi, LPWIFI_CMD_AT );
    Delay_ms ( 1000 );

    // Dummy read
    lpwifi_process( );
    lpwifi_clear_app_buf( );

    log_printf( &logger, "\r\n --- Factory reset --- \r\n" );
    lpwifi_factory_reset_device ( &lpwifi );
    Delay_ms ( 500 );
    // Enable Echo
    lpwifi_send_cmd( &lpwifi, LPWIFI_CMD_ATE );
    app_error_flag = lpwifi_rsp_check( );
    lpwifi_error_check( app_error_flag );
    Delay_ms ( 500 );
    log_printf( &logger, " ----------------------------------------------- \r\n" );

    log_printf( &logger, "\r\n --- Connecting to the access point --- \r\n" );
    // Connect to AP
    lpwifi_connect_to_ap( &lpwifi, AP_SSID, AP_PASSWORD );
    app_error_flag = lpwifi_rsp_check( );
    lpwifi_error_check( app_error_flag );
    lpwifi_check_connection();
    while ( CONNECTED_TO_AP != app_connection_status )
    {
        lpwifi_check_connection();
        if ( NOT_CONNECTED_TO_AP == app_connection_status )
        {
            Delay_ms ( 500 );
            app_connection_status = WAIT_FOR_CONNECTION;

            // Connect to AP
            lpwifi_connect_to_ap( &lpwifi, AP_SSID, AP_PASSWORD );
            app_error_flag = lpwifi_rsp_check( );
            lpwifi_error_check( app_error_flag );
        }
    }
    log_printf( &logger, " ----------------------------------------------- \r\n" );
    log_printf( &logger, "\r\n --- Creating a TCP server --- \r\n" );
    // Create TCP Server
    lpwifi_create_tcp_server( &lpwifi, LOCAL_PORT );
    app_error_flag = lpwifi_rsp_check( );
    lpwifi_error_check( app_error_flag );
    Delay_ms ( 500 );

    log_printf( &logger, " ----------------------------------------------- \r\n" );
    log_printf( &logger, "\r\n --- Creating a UDP socket --- \r\n" );
    // Create UDP socket
    lpwifi_create_udp_socket( &lpwifi, LOCAL_PORT );
    app_error_flag = lpwifi_rsp_check( );
    lpwifi_error_check( app_error_flag );
    Delay_ms ( 500 );

    log_printf( &logger, " ----------------------------------------------- \r\n" );
    log_printf( &logger, " TCP server and UDP socket are available at: \r\n" );
    log_printf( &logger, "    SSID: \"%s\"\r\n    IP: %s\r\n    Port: %u", ( char * ) AP_SSID,
                                                                           ( char * ) assigned_ip_address,
                                                                         ( uint16_t ) LOCAL_PORT );
    log_printf( &logger, "\r\n ----------------------------------------------- \r\n" );
    log_printf( &logger, " You can connect to it via a TCP/UDP client." );
    log_printf( &logger, "\r\n ----------------------------------------------- \r\n" );
}

void application_task ( void )
{
    lpwifi_process( );
    lpwifi_log_app_buf( );
}

int main ( void ) 
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif
    
    application_init( );
    
    for ( ; ; ) 
    {
        application_task( );
    }

    return 0;
}

static void lpwifi_clear_app_buf ( void )
{
    memset( app_buf, 0, app_buf_len );
    app_buf_len = 0;
    app_buf_cnt = 0;
}

static err_t lpwifi_process ( void )
{
    int32_t rx_size;
    char rx_buff[ PROCESS_BUFFER_SIZE ] = { 0 };

    rx_size = lpwifi_generic_read( &lpwifi, rx_buff, PROCESS_BUFFER_SIZE );

    if ( rx_size > 0 )
    {
        int32_t buf_cnt = 0;

        if ( app_buf_len + rx_size >= PROCESS_BUFFER_SIZE )
        {
            lpwifi_clear_app_buf(  );
            return LPWIFI_ERROR;
        }
        else
        {
            buf_cnt = app_buf_len;
            app_buf_len += rx_size;
        }

        for ( int32_t rx_cnt = 0; rx_cnt < rx_size; rx_cnt++ )
        {
            if ( rx_buff[ rx_cnt ] != 0 )
            {
                app_buf[ ( buf_cnt + rx_cnt ) ] = rx_buff[ rx_cnt ];
            }
            else
            {
                app_buf_len--;
                buf_cnt--;
            }

        }
        return LPWIFI_OK;
    }
    return LPWIFI_ERROR;
}

static err_t lpwifi_rsp_check ( void )
{
    uint16_t timeout_cnt = 0;
    uint16_t timeout = 10000;

    err_t error_flag = lpwifi_process(  );

    if ( ( error_flag != 0 ) && ( error_flag != -1 ) )
    {
        return error_flag;
    }

    while ( ( strstr( app_buf, RSP_OK ) == 0 ) && ( strstr( app_buf, RSP_ERROR ) == 0 ) )
    {
        error_flag = lpwifi_process(  );
        if ( ( error_flag != 0 ) && ( error_flag != -1 ) )
        {
            return error_flag;
        }

        timeout_cnt++;
        if ( timeout_cnt > timeout )
        {
            // Initialize AT command
            while ( ( 0 == strstr( app_buf, RSP_OK ) ) && ( 0 == strstr( app_buf, RSP_ERROR ) ) )
            {
                lpwifi_send_cmd( &lpwifi, LPWIFI_CMD_ATZ );
                lpwifi_process(  );
                Delay_ms ( 100 );
            }
            lpwifi_clear_app_buf(  );
            // Enable Echo
            while ( ( 0 == strstr( app_buf, RSP_OK ) ) && ( 0 == strstr( app_buf, RSP_ERROR ) ) )
            {
                lpwifi_send_cmd( &lpwifi, LPWIFI_CMD_ATE );
                lpwifi_process(  );
                Delay_ms ( 100 );
            }
            lpwifi_clear_app_buf(  );
            return APP_ERROR_TIMEOUT;
        }

        Delay_ms ( 1 );
    }

    lpwifi_log_app_buf();

    return APP_OK;
}

static void lpwifi_error_check( err_t error_flag )
{
    if ( ( error_flag != APP_OK ) && ( error_flag != APP_ERROR_DRIVER ) )
    {
        switch ( error_flag )
        {
            case APP_ERROR_OVERFLOW:
                log_error( &logger, " Overflow!" );
                break;
            case APP_ERROR_TIMEOUT:
                log_error( &logger, " Timeout!" );
                break;
            default:
                break;
        }
    }
}

static void lpwifi_log_app_buf ( void )
{
    for ( int32_t buf_cnt = 0; buf_cnt < app_buf_len; buf_cnt++ )
    {
        log_printf( &logger, "%c", app_buf[ buf_cnt ] );
    }

    lpwifi_clear_app_buf(  );
}

static void lpwifi_check_connection( void )
{
    #define CONNECTED     "+WFJAP:1"
    #define NOT_CONNECTED "+WFJAP:0"

    lpwifi_process( );
    if ( 0 != strstr( app_buf, CONNECTED ) )
    {
        #define IP_DELIMITER "',"
        char * __generic_ptr app_buf_ptr;
        Delay_ms ( 200 );
        lpwifi_process( );
        app_buf_ptr = strstr( app_buf, IP_DELIMITER );
        strcpy( assigned_ip_address, app_buf_ptr );
        lpwifi_str_cut_chr( assigned_ip_address, '\'' );
        lpwifi_str_cut_chr( assigned_ip_address, ',' );
        lpwifi_str_cut_chr( assigned_ip_address, '\r' );
        lpwifi_str_cut_chr( assigned_ip_address, '\n' );
        lpwifi_log_app_buf( );
        app_connection_status = CONNECTED_TO_AP;
    }
    else if ( 0 != strstr( app_buf, NOT_CONNECTED ) )
    {
        app_connection_status = NOT_CONNECTED_TO_AP;
    }
}

static void lpwifi_str_cut_chr ( uint8_t *str, uint8_t chr )
{
    uint16_t cnt_0, cnt_1;
    for ( cnt_0 = 0; cnt_0 < strlen( str ); cnt_0++ )
    {
        if ( str[ cnt_0 ] == chr )
        {
            for ( cnt_1 = cnt_0; cnt_1 < strlen( str ); cnt_1++ )
            {
                str[ cnt_1 ] = str[ cnt_1 + 1 ];
            }
        }
    }
}

// ------------------------------------------------------------------------ END

额外支持

资源

喜欢这个项目吗?

'购买此套件' 按钮会直接带您进入购物车,您可以在购物车中轻松添加或移除产品。