Clion开发STM32之W5500系列(综合实验)

说明

  1. 此为w5500模块的综合实验测试模块,包含dhcp、dns、ntp
  2. 以上三个模块的驱动参考之前的文章,本篇不做说明.
  3. 使用的开发芯片 stm32f103vet6系列,外设接口使用的spi2

实验内容:

  1. 通过dhcp动态获取ip,
  2. 通过dns解析NTP服务域名的ip
  3. 通过NTP服务ip获取时间

w5500配置驱动

/*******************************************************************************
 *  Copyright (c) [scl]。保留所有权利。
 *     本文仅供个人学习和研究使用,禁止用于商业用途。
 ******************************************************************************/

#include "app_conf.h"
#include "w5500_config.h"


#if APP_CONFIG_W5500
#define  APP_CONFIG_W5500_DHCP      (1) /*W5500 应用层--DHCP*/
#define  APP_CONFIG_W5500_DNS       (1) /*W5500 应用层--DNS*/
#define  APP_CONFIG_W5500_NTP       (1) /*W5500 应用层--NTP*/

#define DBG_ENABLE
#define DBG_SECTION_NAME "w5500"
#define DBG_LEVEL W5500_DBG_LEVEL

#include "sys_dbg.h"
#include "w5500_dhcp.h"
#include "w5500_dns.h"
#include "w5500_ntp.h"

#define W5500_CS stm_port_define(B,12)
#define W5500_RST stm_port_define(C,7)


static SPI_HandleTypeDef *w5500_spi = NULL;

static void send_and_rec_bytes(uint8_t *in_dat, uint8_t *out_data, uint16_t len) {
    while (HAL_SPI_GetState(w5500_spi) != HAL_SPI_STATE_READY);
    HAL_SPI_TransmitReceive(w5500_spi, in_dat, out_data, len, 1000);
    while (HAL_SPI_GetState(w5500_spi) != HAL_SPI_STATE_READY);
}

static void send_only(uint8_t *in_data, uint16_t len) {
    HAL_SPI_Transmit(w5500_spi, in_data, len, 1000);
}

static void W5500_RST_HIGH(void) { stm_pin_high(W5500_RST); }

static void W5500_RST_LOW(void) { stm_pin_low(W5500_RST); }

static void W5500_CS_LOW(void) { stm_pin_low(W5500_CS); }

static void W5500_CS_HIGH(void) { stm_pin_high(W5500_CS); }

static bool w5500_dhcp_after_init(SOCKET s, uint8_t try_cnt, uint32_t wait_ms);

static bool w5500_dns_after_init(SOCKET s, uint8_t *domain, uint8_t try_cnt, uint32_t wait_ms);

static bool w5500_ntp_after_init(SOCKET s, uint8_t try_cnt, uint32_t wait_ms);

static void W5500_Driver_MspInit(void) {
    stm32_pin_mode(W5500_CS, pin_mode_output);  /*CS*/
    stm32_pin_mode(W5500_RST, pin_mode_output); /*RST*/
    stm_pin_low(W5500_RST);
    stm_pin_low(W5500_CS);
    /*初始化SPI外设*/
    /*W5500 支持 SPI 模式 0 及模式 3..MOSI 和 MISO 信号无论是接收或发送,均遵从从最高标志位(MSB)到最低标志位(LSB)的传输序列。*/
    bsp_SpiHandleInit(w5500_spi, SPI_BAUDRATEPRESCALER_2, spi_mode_3);
}

module_w5500_t w5500_conf = {
        .base_conf={
                .socket_num = 4,
                .rx_size={4, 4, 4, 4},
                .tx_size={4, 4, 4, 4},
        },
        .net_conf={
                .ip={192, 168, 199, 12},
                .gw={192, 168, 199, 1},
                .sub={255, 255, 255, 0},
        },
        .driver={
                .cs_high = W5500_CS_HIGH,
                .cs_low = W5500_CS_LOW,
                .rst_high= W5500_RST_HIGH,
                .rst_low=W5500_RST_LOW,
                .delay = HAL_Delay,
                .send_and_rec_bytes = send_and_rec_bytes,
                .send_only =send_only
        },
        .api = {
                .msp_init=W5500_Driver_MspInit,
        }
};
uint8_t ntp_domain[] = {"ntp.ntsc.ac.cn"}; /*ntp域名*/
static struct dns_conf net_dns_cnf = { /*dns服务配置*/
        .dns_server_ip={114, 114, 114, 114},
        .dns_port = 53,
        .delay_ms_cb = HAL_Delay
};
static struct ntp_conf net_ntp_conf = {
//        .ntp_server={114, 118, 7, 163},
        .ntp_port = 123,
        .delay_ms_cb = HAL_Delay
};


static void w5500_pre_init(void) {
    /*一般做数据加载,此时系统时钟使用的是内部时钟,如需要使用系统时钟的外设不在此进行初始化*/
    w5500_spi = conv_spi_handle_ptr(handle_get_by_id(spi2_id));
    /*初始化资源*/
    module_w5500_init(&w5500_conf);
    uint32_t uid0 = HAL_GetUIDw0();
    uint32_t uid1 = HAL_GetUIDw1();
    uint32_t uid2 = HAL_GetUIDw2();
    uint8_t mac[6] = {0, uid0 >> 8, uid1, uid1 >> 8, uid2, uid2 >> 8};
    memcpy(w5500_conf.net_conf.mac, mac, sizeof(mac));
#if APP_CONFIG_W5500_DHCP /*使用dhcp*/
    dhcp_config_registry(&w5500_conf);
    w5500_conf.net_conf_init = dhcp_init;/*使用dhcp init*/
#endif
}

static void w5500_init(void) {

    w5500_conf.api.msp_init();/*初始化*/
    w5500_conf.net_conf_init();

}


static void w5500_after_init(void) {
    uint8_t try_cnt;
    SOCKET sn = 1;/*使用的socket*/
#if APP_CONFIG_W5500_DHCP
    try_cnt = 10;
    if (!w5500_dhcp_after_init(sn, try_cnt, 500)) {
        LOG_D("w5500_dhcp_after_init try cnt over:%d", try_cnt);
        return;
    }
#endif
    uint8_t ip[4];
    w5500_reg_ip_read(ip);
    LOG_D("w5500_reg_ip_read:%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
    w5500_reg_gw_read(ip);
    LOG_D("w5500_reg_gw_read:%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
#if APP_CONFIG_W5500_DNS
    try_cnt = 5;
    dns_config_set(&net_dns_cnf);
    if (!w5500_dns_after_init(sn, ntp_domain, try_cnt, 500)) {
        LOG_D("w5500_dns_after_init try cnt over:%d", try_cnt);
        return;
    }

#endif

#if APP_CONFIG_W5500_NTP
    try_cnt = 5;
    ntp_config_set(&net_ntp_conf);
    if (!w5500_ntp_after_init(sn, try_cnt, 500)) {
        LOG_D("w5500_ntp_after_init try cnt over:%d", try_cnt);
        return;
    }
#endif
}

app_init_export(w5500_net_conf, w5500_pre_init, w5500_init, w5500_after_init);

#if APP_CONFIG_W5500_DHCP

static bool w5500_dhcp_after_init(SOCKET s, uint8_t try_cnt, uint32_t wait_ms) {
    for (uint8_t i = 0; i < try_cnt; ++i) {
        if (do_dhcp(s, wait_ms)) {
            return true;
        }
    }
    return false;
}

#endif
#if APP_CONFIG_W5500_DNS

static bool w5500_dns_after_init(SOCKET s, uint8_t *domain, uint8_t try_cnt, uint32_t wait_ms) {
    for (int i = 0; i < try_cnt; ++i) {
        if (do_dns(s, domain, net_ntp_conf.ntp_server, wait_ms)) {
            LOG_D("dns parse result ip:%d.%d.%d.%d",
                  net_ntp_conf.ntp_server[0], net_ntp_conf.ntp_server[1],
                  net_ntp_conf.ntp_server[2], net_ntp_conf.ntp_server[3]
            );
            return true;
        }
    }
    return false;
}

#endif

#if APP_CONFIG_W5500_NTP

static bool w5500_ntp_after_init(SOCKET s, uint8_t try_cnt, uint32_t wait_ms) {
    struct net_date_time *nt_tm = NULL;
    for (int i = 0; i < try_cnt; ++i) {
        nt_tm = ntp_date_time_get(s, wait_ms);
        if (nt_tm != NULL) {
            LOG_D("NTP TIME:%d-%02d-%02d %02d:%02d:%02d",
                  nt_tm->year, nt_tm->month, nt_tm->day,
                  nt_tm->hour, nt_tm->min, nt_tm->sec
            );
            HAL_TIM_Base_Start_IT(handle_get_by_id(tim6_id));
            return true;
        }
    }
    return false;
}

#endif
#endif





测试结果

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/45560.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

国密SSL优势及应用场景

国密SSL的优势主要有以下几点&#xff1a; 更高的安全性&#xff1a;国密算法采用的是国家密码管理局推荐的算法&#xff0c;相对于传统的SSL协议更加安全。 更好的性能&#xff1a;国密算法是国家密码管理局推荐的算法&#xff0c;其加密效率与密钥长度相比传统算法更高。 更…

微服务架构演变

微服务架构筑基 软件架构演进 软件架构的发展经历了从单体结构、垂直架构、SOA架构到微服务架构的过程. 什么是微服务&#xff1f;&#xff1f; Spring Cloud Netfilx Spring Cloud Alibaba service Mesh 架构的发展&#xff1a;基于某一种因素 技术是服务于业务的 业务又是…

Vue mixin 混入

可以复用的组件&#xff0c;我们一般会抽离&#xff0c;写成公共的模块。 可以复用的方法&#xff0c;我们一般会抽离&#xff0c;写成公共的函数。 那么 在 Vue 中&#xff0c;如果 某几个组件实例 VueComponent 中、或者 整个 Vue 项目中 都存在相同的配置&#xff0c;那就…

数据结构(二)

目录 Trie树 并查集 堆 Trie树 作用:用来高效地存储和查找字符串集合的数据结构 基本形式: 模板代码如下: #include<iostream> using namespace std;const int N 100010;//idx代表当前用到哪个下标 //既是根节点&#xff0c;又是空节点 //cnt存储的是以当前点结尾的…

Mac 系统钥匙串证书不受信任

Mac 系统钥匙串证书不受信任 解决办法 通过尝试安装 Apple PKI 的 Worldwide Developer Relations - G4 (Expiring 12/10/2030 00:00:00 UTC) 解决该异常问题 以上便是此次分享的全部内容&#xff0c;希望能对大家有所帮助!

移动端商品详情页设计

效果图 代码如下 页面设计 <div class"container"><!--商品详情 start--><van-image class"goods-item-image" :src"goods.goodsHeadImg"></van-image><div class"goods-price">&#xffe5;<span&…

【VUE】npm打包报错 Syntax Error: Error: Cannot find module ‘imagemin-gifsicle‘

一. Syntax Error: Error: Cannot find module ‘imagemin-gifsicle’ npm run build 报错&#xff0c;报错如下 原因 这个错误消息显示缺少了 imagemin-gifsicle 模块&#xff0c;而它是 image-webpack-loader 的依赖项&#xff0c;导致构建失败。解决 &#xff08;1&#xf…

安全—01day

文章目录 1. 编码1.1 ASCLL编码1.2 URL编码1.3 Unicode编码1.4 HTML编码1.5 Base64编码 2. form表单2.1 php接收form表单2.2 python接收form表单 1. 编码 1.1 ASCLL编码 ASCII 是基于拉丁字母的一套电脑编码系统&#xff0c;主要用于显示现代英语和其他西欧语言。它是最通用的…

电容触摸屏(TP)的工艺结构

液晶显示屏(LCM),触摸屏(TP) “GG、GP、GF”这是结构分类&#xff0c;第一个字母表面材质&#xff08;又称为上层&#xff09;&#xff0c;第二个字母是触摸屏的材质&#xff08;又称为下层&#xff09;&#xff0c;两者贴合在一起。 G玻璃&#xff0c;FFILM&#xff0c;“”贴…

Stable Diffusion - 扩展 SegmentAnything 和 GroundingDINO 实例分割算法 插件的配置与使用

欢迎关注我的CSDN&#xff1a;https://spike.blog.csdn.net/ 本文地址&#xff1a;https://blog.csdn.net/caroline_wendy/article/details/131918652 Paper and GitHub&#xff1a; Segment Anything: SAM - Segment Anything GitHub: https://github.com/facebookresearch/s…

蓝桥杯专题-真题版含答案-【牌型种数】【煤球数目】【寒假作业】【奖券数目】

点击跳转专栏>Unity3D特效百例点击跳转专栏>案例项目实战源码点击跳转专栏>游戏脚本-辅助自动化点击跳转专栏>Android控件全解手册点击跳转专栏>Scratch编程案例点击跳转>软考全系列点击跳转>蓝桥系列 &#x1f449;关于作者 专注于Android/Unity和各种游…

MySQL 数据库约束

目录 一、数据库约束 1、约束类型 二、NULL 约束 三、unique 约束 四、default 约束 五、primary key 约束 自增主键 六、foreign key 外键约束 七、check 约束 一、数据库约束 我们使用数据库来存储数据&#xff0c;一般是希望这里存储的数据是靠谱的&#xff0c;…

There has been an error.Error running C:\WINDOWS\System32\icacls

目前网上有两种有效的解决方案&#xff1a; windows用户名含中文的创建一个新用户&#xff0c;链接 安装其他版本的PostgreSQL(可优先考虑&#xff0c;我使用该方法解决的问题)&#xff0c;链接

java项目之人才公寓管理系统(ssm+mysql+jsp)

风定落花生&#xff0c;歌声逐流水&#xff0c;大家好我是风歌&#xff0c;混迹在java圈的辛苦码农。今天要和大家聊的是一款基于ssm的人才公寓管理系统。技术交流和部署相关看文章末尾&#xff01; 开发环境&#xff1a; 后端&#xff1a; 开发语言&#xff1a;Java 框架&…

【产品实习评审】对于高校跑腿的任务模型和价格模型设计比较到位

大家好&#xff0c;本篇文章分享【校招VIP】商业在线实习项目“跑个腿”第一期需求发布模块产品同学的脑图周最佳作品&#xff0c;该同学来自江苏师范大学社会学专业。 本项目亮点&#xff1a; 1 跑腿需求发布模块—构建项目数据模型&#xff0c;包括时效、常用地址和联系 2…

卤味行业数据分析报告

在一个炎热的夏日午后&#xff0c;热气蒸腾的城市街头弥漫着一股令人垂涎欲滴的香气。这股香气源自一家招牌醒目的卤味小吃摊位&#xff0c;摊主技巧娴熟地将各式美味的食材浸泡在独特的卤汁中。这里没有花哨的招牌&#xff0c;却吸引了无数食客的目光和嘴巴。 卤制食品在中国烹…

Rust vs Go:常用语法对比(四)

题图来自 Go vs. Rust performance comparison: The basics 61. Get current date 获取当前时间 package mainimport ( "fmt" "time")func main() { d : time.Now() fmt.Println("Now is", d) // The Playground has a special sandbox, so you …

上手 SpringBoot

简介 SpringBoot设计的目的是简化 Spring应用的初始搭建以及 开发过程。 SpringBoot概述 parent 继承父pom文件&#xff0c;方便管理依赖的版本。此处涉及maven的使用 作用&#xff1a; 继承parent的形式可以采用引入依赖的形式实现效果 starter(原理是依赖传递) 包含了若…

Mac电脑文件夹无权限问题

sudo cp 16.5.zip /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport 走到之前的folder &#xff0c;右键选择get info更改權限, 再應用到所有子文件夹 右下解鎖再加自己Read & Write, -右邊拉下應該可以應用到所有子文件 这样就可以…
最新文章