Merge branch 'prepare_for_master' of https://gitlink.org.cn/xuos/xiuos into prepare_for_master

This commit is contained in:
huang
2023-09-11 11:19:52 +08:00
112 changed files with 8457 additions and 2366 deletions
@@ -281,7 +281,7 @@ struct pbuf* low_level_input(struct netif* netif)
extern void LwipSetIPTest(int argc, char* argv[]);
int HwEthInit(void)
{
// lwip_config_tcp(0, lwip_ipaddr, lwip_netmask, lwip_gwaddr);
// lwip_config_tcp(0, lwip_ipaddr, lwip_netmask, lwip_gwaddr);
LwipSetIPTest(1, NULL);
return EOK;
}
@@ -263,7 +263,7 @@ err_t ethernetif_init(struct netif* netif)
if (EOK != lwip_netdev_add(netif)) {
SYS_KDEBUG_LOG(NETDEV_DEBUG, ("[%s] LWIP add netdev failed.\n", __func__));
} else {
printf("[%s] Add Netdev successful\n", __func__);
// printf("[%s] Add Netdev successful\n", __func__);
}
return LL_OK;
}
@@ -191,8 +191,8 @@ static uint32 SpiLoraWrite(void *dev, struct BusBlockWriteParam *write_param)
KPrintf("SpiLoraWrite ERROR:The message is too long!\n");
return ERROR;
} else {
SX1276SetTxPacket(write_param->buffer, write_param->size);
while(SX1276Process() != RF_TX_DONE);
SX1276SetTx(write_param->buffer, write_param->size);
KPrintf("SpiLoraWrite success!\n");
}
@@ -210,24 +210,12 @@ static uint32 SpiLoraRead(void *dev, struct BusBlockReadParam *read_param)
NULL_PARAM_CHECK(dev);
NULL_PARAM_CHECK(read_param);
int read_times = 100;
SX1276StartRx();
KPrintf("SpiLoraRead Ready!\n");
while (read_times) {
if (SX1276Process() != RF_RX_DONE) {
read_times --;
MdelayKTask(500);
} else {
break;
}
}
int ret = SX1276GetRx(read_param->buffer, (uint16 *)&read_param->read_length);
if (read_times > 0) {
SX1276GetRxPacket(read_param->buffer, (uint16 *)&read_param->read_length);
} else {
read_param->read_length = 0;
if (ret < 0) {
return 0;
}
return read_param->read_length;
@@ -494,6 +494,7 @@ int HwSpiInit(void)
return ret;
}
#ifdef TEST_SPI
/*Just for lora test*/
static struct Bus *bus;
static struct HardwareDev *dev;
@@ -578,4 +579,4 @@ void TestLoraOpen(void)
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN),
TestLoraOpen, TestLoraOpen, open lora device and read parameters);
#endif
@@ -50,6 +50,9 @@ uint8_t SX1276Regs[0x70];
static bool LoRaOn = true;
static bool LoRaOnState = false;
static int sx1276_tx_sem, sx1276_rx_sem;
static int sx1276_radio_task;
void SX1276Reset(void)
{
uint32_t startTick;
@@ -200,6 +203,20 @@ void SX1276GetRxPacket(void *buffer, uint16_t *size)
}
}
int SX1276GetRx(void *buffer, uint16_t *size)
{
int ret = -1;
SX1276StartRx();
//receive timeout 10s
ret = KSemaphoreObtain(sx1276_rx_sem, 10000);
if (0 == ret) {
SX1276LoRaSetRFState(RFLR_STATE_IDLE);
SX1276GetRxPacket(buffer, size);
}
return ret;
}
void SX1276SetTxPacket(const void *buffer, uint16_t size)
{
if(LoRaOn == false) {
@@ -209,6 +226,14 @@ void SX1276SetTxPacket(const void *buffer, uint16_t size)
}
}
void SX1276SetTx(const void *buffer, uint16_t size)
{
SX1276SetTxPacket(buffer, size);
KSemaphoreObtain(sx1276_tx_sem, WAITING_FOREVER);
SX1276StartRx();
}
uint8_t SX1276GetRFState(void)
{
if(LoRaOn == false) {
@@ -236,6 +261,20 @@ uint32_t SX1276Process(void)
}
}
static void Sx1276RadioEntry(void *parameter)
{
uint32_t result;
while(1) {
result = SX1276Process();
if (RF_RX_DONE == result) {
KSemaphoreAbandon(sx1276_rx_sem);
}
if (RF_TX_DONE == result) {
KSemaphoreAbandon(sx1276_tx_sem);
}
}
}
uint32_t SX1276ChannelEmpty(void)
{
if(LoRaOn == false) {
@@ -277,6 +316,12 @@ void SX1276Init(void)
SX1276_SetLoRaOn(LoRaOn);
SX1276LoRaInit();
#endif
sx1276_rx_sem = KSemaphoreCreate(0);
sx1276_tx_sem = KSemaphoreCreate(0);
sx1276_radio_task = KTaskCreate("radio", Sx1276RadioEntry , NONE, 2048, 20);
StartupKTask(sx1276_radio_task);
}
#endif
@@ -80,8 +80,12 @@ void SX1276StartRx( void ); //开始接收
void SX1276GetRxPacket( void *buffer, uint16_t *size ); //得到接收的数据
int SX1276GetRx(void *buffer, uint16_t *size); //应用接收数据,无数据时阻塞
void SX1276SetTxPacket( const void *buffer, uint16_t size ); //发送数据
void SX1276SetTx( const void *buffer, uint16_t size ); //应用发送数据
uint8_t SX1276GetRFState( void ); //得到RFLRState状态
void SX1276SetRFState( uint8_t state ); //设置RFLRState状态,RFLRState的值决定了下面的函数处理哪一步的代码
@@ -13,6 +13,21 @@ menuconfig BSP_USING_UART3
default "usart3_dev3"
endif
menuconfig BSP_USING_UART4
bool "Enable USART4 for RS485"
default y
if BSP_USING_UART4
config SERIAL_BUS_NAME_4
string "serial bus 4 name"
default "usart4"
config SERIAL_DRV_NAME_4
string "serial bus 4 driver name"
default "usart4_drv"
config SERIAL_4_DEVICE_NAME_0
string "serial bus 4 device 0 name"
default "usart4_dev4"
endif
menuconfig BSP_USING_UART6
bool "Enable USART6"
default n
@@ -50,6 +50,14 @@ Modification:
#define USART3_TX_PIN (GPIO_PIN_10)
#endif
#if defined(BSP_USING_UART4)
#define USART4_RX_PORT (GPIO_PORT_E)
#define USART4_RX_PIN (GPIO_PIN_07)
#define USART4_TX_PORT (GPIO_PORT_G)
#define USART4_TX_PIN (GPIO_PIN_00)
#endif
#if defined(BSP_USING_UART6)
#define USART6_RX_PORT (GPIO_PORT_H)
#define USART6_RX_PIN (GPIO_PIN_06)
@@ -72,6 +80,12 @@ static x_err_t UartGpioInit(CM_USART_TypeDef *USARTx)
GPIO_SetFunc(USART3_TX_PORT, USART3_TX_PIN, GPIO_FUNC_32);
break;
#endif
#ifdef BSP_USING_UART4
case (uint32)CM_USART4:
GPIO_SetFunc(USART4_RX_PORT, USART4_RX_PIN, GPIO_FUNC_33);
GPIO_SetFunc(USART4_TX_PORT, USART4_TX_PIN, GPIO_FUNC_32);
break;
#endif
#ifdef BSP_USING_UART6
case (uint32)CM_USART6:
GPIO_SetFunc(USART6_RX_PORT, USART6_RX_PIN, GPIO_FUNC_37);
@@ -123,6 +137,32 @@ void Uart3RxErrIrqHandler(void)
}
#endif
#ifdef BSP_USING_UART4
struct SerialBus serial_bus_4;
struct SerialDriver serial_driver_4;
struct SerialHardwareDevice serial_device_4;
void Uart4RxIrqHandler(void)
{
x_base lock = 0;
lock = DISABLE_INTERRUPT();
SerialSetIsr(&serial_device_4, SERIAL_EVENT_RX_IND);
ENABLE_INTERRUPT(lock);
}
void Uart4RxErrIrqHandler(void)
{
x_base lock = 0;
lock = DISABLE_INTERRUPT();
UartRxErrIsr(&serial_bus_4, &serial_driver_4, &serial_device_4);
ENABLE_INTERRUPT(lock);
}
#endif
#ifdef BSP_USING_UART6
struct SerialBus serial_bus_6;
struct SerialDriver serial_driver_6;
@@ -499,6 +539,57 @@ int HwUsartInit(void)
}
#endif
#ifdef BSP_USING_UART4
static struct SerialCfgParam serial_cfg_4;
memset(&serial_cfg_4, 0, sizeof(struct SerialCfgParam));
static struct SerialDevParam serial_dev_param_4;
memset(&serial_dev_param_4, 0, sizeof(struct SerialDevParam));
static struct UsartHwCfg serial_hw_cfg_4;
memset(&serial_hw_cfg_4, 0, sizeof(struct UsartHwCfg));
serial_driver_4.drv_done = &drv_done;
serial_driver_4.configure = SerialDrvConfigure;
serial_device_4.hwdev_done = &hwdev_done;
serial_cfg_4.data_cfg = data_cfg_init;
//default irq configure
serial_hw_cfg_4.uart_device = CM_USART4;
serial_hw_cfg_4.usart_clock = FCG3_PERIPH_USART4;
serial_hw_cfg_4.rx_err_irq.irq_config.irq_num = BSP_UART4_RXERR_IRQ_NUM;
serial_hw_cfg_4.rx_err_irq.irq_config.irq_prio = BSP_UART4_RXERR_IRQ_PRIO;
serial_hw_cfg_4.rx_err_irq.irq_config.int_src = INT_SRC_USART4_EI;
serial_hw_cfg_4.rx_irq.irq_config.irq_num = BSP_UART4_RX_IRQ_NUM;
serial_hw_cfg_4.rx_irq.irq_config.irq_prio = BSP_UART4_RX_IRQ_PRIO;
serial_hw_cfg_4.rx_irq.irq_config.int_src = INT_SRC_USART4_RI;
serial_hw_cfg_4.rx_err_irq.irq_callback = Uart4RxErrIrqHandler;
serial_hw_cfg_4.rx_irq.irq_callback = Uart4RxIrqHandler;
hc32_install_irq_handler(&serial_hw_cfg_4.rx_err_irq.irq_config, serial_hw_cfg_4.rx_err_irq.irq_callback, 0);
serial_cfg_4.hw_cfg.private_data = (void *)&serial_hw_cfg_4;
serial_driver_4.private_data = (void *)&serial_cfg_4;
serial_dev_param_4.serial_work_mode = SIGN_OPER_INT_RX;
serial_device_4.haldev.private_data = (void *)&serial_dev_param_4;
ret = BoardSerialBusInit(&serial_bus_4, &serial_driver_4, SERIAL_BUS_NAME_4, SERIAL_DRV_NAME_4);
if (EOK != ret) {
KPrintf("HwUartInit uart4 error ret %u\n", ret);
return ERROR;
}
ret = BoardSerialDevBend(&serial_device_4, (void *)&serial_cfg_4, SERIAL_BUS_NAME_4, SERIAL_4_DEVICE_NAME_0);
if (EOK != ret) {
KPrintf("HwUartInit uart4 error ret %u\n", ret);
return ERROR;
}
#endif
#ifdef BSP_USING_UART6
static struct SerialCfgParam serial_cfg_6;
memset(&serial_cfg_6, 0, sizeof(struct SerialCfgParam));
@@ -1,4 +1,5 @@
export CFLAGS := -mcmodel=medany -march=rv64imafdc -mabi=lp64d -fno-common -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -O0 -ggdb -fgnu89-inline -Werror
export CFLAGS := -mcmodel=medany -march=rv64imafdc -mabi=lp64d -fno-common -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -O1 -fgnu89-inline -Wformat -Wuninitialized
# export CFLAGS := -mcmodel=medany -march=rv64imafdc -mabi=lp64d -fno-common -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -O0 -ggdb -fgnu89-inline -Werror -Wformat -Wuninitialized
export AFLAGS := -c -mcmodel=medany -march=rv64imafdc -mabi=lp64d -x assembler-with-cpp -ggdb
export LFLAGS := -mcmodel=medany -march=rv64imafdc -mabi=lp64d -nostartfiles -Wl,--gc-sections,-Map=XiZi-edu-riscv64.map,-cref,-u,_start -T $(BSP_ROOT)/link.lds
@@ -14,12 +15,12 @@ endif
export APPLFLAGS := -mcmodel=medany -march=rv64imafdc -mabi=lp64d -nostartfiles -Wl,--gc-sections,-Map=XiZi-edu-riscv64.map,-cref,-u, -T $(BSP_ROOT)/link_userspace.lds
export CXXFLAGS := -mcmodel=medany -march=rv64imafdc -mabi=lp64d -fno-common -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -O0 -ggdb -Werror
export CXXFLAGS := -mcmodel=medany -march=rv64imafdc -mabi=lp64d -fno-common -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -O0 -ggdb -Werror -std=gnu++11
export CROSS_COMPILE ?=/opt/gnu-mcu-eclipse/riscv-none-gcc/8.2.0-2.1-20190425-1021/bin/riscv-none-embed-
export DEFINES := -DHAVE_CCONFIG_H -DHAVE_SIGINFO
export DEFINES := -DHAVE_CCONFIG_H -DHAVE_SIGINFO -DRISCV_LWIP
export ARCH = risc-v
export MCU = k210
@@ -25,11 +25,12 @@
#ifndef _BSP_ATOMIC_H
#define _BSP_ATOMIC_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#define SPINLOCK_INIT \
{ \
0 \
@@ -63,7 +64,7 @@ extern "C" {
typedef struct _spinlock
{
int lock;
int32_t lock;
} spinlock_t;
typedef struct _semaphore
@@ -2,6 +2,7 @@
config BSP_USING_W5500
bool "Using w5500"
select BSP_USING_LWIP
default y
config BSP_WIZ_RST_PIN
@@ -15,3 +16,8 @@ config BSP_WIZ_INT_PIN
config BSP_WIZ_USE_IPERF
bool "Using iperf"
default y
menuconfig BSP_USING_LWIP
bool "Using LwIP device"
default n
select RESOURCES_LWIP
@@ -1,3 +1,4 @@
SRC_FILES := socket.c connect_w5500.c w5500.c wizchip_conf.c spi_interface.c wiz_ping.c connect_w5500_test.c wiz_iperf.c
# SRC_FILES := socket.c connect_w5500.c w5500.c wizchip_conf.c spi_interface.c wiz_ping.c connect_w5500_test.c wiz_iperf.c
SRC_FILES := socket.c connect_w5500.c w5500.c wizchip_conf.c spi_interface.c wiz_ping.c connect_w5500_test.c w5x00_lwip.c wiz_iperf.c
include $(KERNEL_ROOT)/compiler.mk
@@ -5,8 +5,8 @@
#include <dev_pin.h>
#include <drv_io_config.h>
#include <fpioa.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <xs_base.h>
#include "gpio_common.h"
@@ -14,6 +14,10 @@
#include "socket.h"
#include "w5500.h"
#include "connect_ethernet.h"
#include <sys.h>
#define SPI_LORA_FREQUENCY 10000000
// spi operations
@@ -23,242 +27,329 @@ extern void spi_select_cs(void);
extern void spi_deselete_cs(void);
// global configurations for w5500 tcp connection
uint32_t get_gbuf_size() {
static const uint32_t g_wiznet_buf_size = 2048;
return g_wiznet_buf_size;
uint32_t get_gbuf_size()
{
static const uint32_t g_wiznet_buf_size = 2048;
return g_wiznet_buf_size;
}
wiz_NetInfo *get_gnetinfo() {
static wiz_NetInfo g_wiz_netinfo = {.mac = {0x00, 0x08, 0xdc, 0x11, 0x11, 0x11},
.ip = {192, 168, 130, 77},
.sn = {255, 255, 254, 0},
.gw = {192, 168, 130, 1},
.dns = {0, 0, 0, 0},
.dhcp = NETINFO_STATIC};
return &g_wiz_netinfo;
wiz_NetInfo* get_gnetinfo()
{
static wiz_NetInfo g_wiz_netinfo = { .mac = { 0x00, 0x08, 0xdc, 0x11, 0x11, 0x11 },
.ip = { 192, 168, 130, 77 },
.sn = { 255, 255, 254, 0 },
.gw = { 192, 168, 130, 1 },
.dns = { 0, 0, 0, 0 },
.dhcp = NETINFO_STATIC };
return &g_wiz_netinfo;
}
int network_init() {
wiz_NetInfo check_wiz_netinfo;
check_wiz_netinfo.dhcp = NETINFO_STATIC;
ctlnetwork(CN_SET_NETINFO, (void *)get_gnetinfo());
ctlnetwork(CN_GET_NETINFO, (void *)&check_wiz_netinfo);
int network_init()
{
wiz_NetInfo check_wiz_netinfo;
check_wiz_netinfo.dhcp = NETINFO_STATIC;
ctlnetwork(CN_SET_NETINFO, (void*)get_gnetinfo());
ctlnetwork(CN_GET_NETINFO, (void*)&check_wiz_netinfo);
if (memcmp(get_gnetinfo(), &check_wiz_netinfo, sizeof(wiz_NetInfo)) != 0) {
KPrintf(
"mac: %d; ip: %d; gw: %d; sn: %d; dns: %d; dhcp: %d;\n",
memcmp(&get_gnetinfo()->mac, &check_wiz_netinfo.mac, sizeof(uint8_t) * 6),
memcmp(&get_gnetinfo()->ip, &check_wiz_netinfo.ip, sizeof(uint8_t) * 4),
memcmp(&get_gnetinfo()->sn, &check_wiz_netinfo.sn, sizeof(uint8_t) * 4),
memcmp(&get_gnetinfo()->gw, &check_wiz_netinfo.gw, sizeof(uint8_t) * 4),
memcmp(&get_gnetinfo()->dns, &check_wiz_netinfo.dns, sizeof(uint8_t) * 4),
memcmp(&get_gnetinfo()->dhcp, &check_wiz_netinfo.dhcp, sizeof(uint8_t)));
KPrintf("WIZCHIP set network information fail.\n");
return ERROR;
}
uint8_t tmpstr[6];
ctlwizchip(CW_GET_ID, (void *)tmpstr);
KPrintf("=== %s NET CONF ===\r\n", (char *)tmpstr);
KPrintf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n", get_gnetinfo()->mac[0],
get_gnetinfo()->mac[1], get_gnetinfo()->mac[2], get_gnetinfo()->mac[3],
get_gnetinfo()->mac[4], get_gnetinfo()->mac[5]);
KPrintf("SIP: %d.%d.%d.%d\r\n", get_gnetinfo()->ip[0], get_gnetinfo()->ip[1],
get_gnetinfo()->ip[2], get_gnetinfo()->ip[3]);
KPrintf("GAR: %d.%d.%d.%d\r\n", get_gnetinfo()->gw[0], get_gnetinfo()->gw[1],
get_gnetinfo()->gw[2], get_gnetinfo()->gw[3]);
KPrintf("SUB: %d.%d.%d.%d\r\n", get_gnetinfo()->sn[0], get_gnetinfo()->sn[1],
get_gnetinfo()->sn[2], get_gnetinfo()->sn[3]);
KPrintf("DNS: %d.%d.%d.%d\r\n", get_gnetinfo()->dns[0], get_gnetinfo()->dns[1],
get_gnetinfo()->dns[2], get_gnetinfo()->dns[3]);
KPrintf("======================\r\n");
if (memcmp(get_gnetinfo(), &check_wiz_netinfo, sizeof(wiz_NetInfo)) != 0) {
KPrintf(
"mac: %d; ip: %d; gw: %d; sn: %d; dns: %d; dhcp: %d;\n",
memcmp(&get_gnetinfo()->mac, &check_wiz_netinfo.mac, sizeof(uint8_t) * 6),
memcmp(&get_gnetinfo()->ip, &check_wiz_netinfo.ip, sizeof(uint8_t) * 4),
memcmp(&get_gnetinfo()->sn, &check_wiz_netinfo.sn, sizeof(uint8_t) * 4),
memcmp(&get_gnetinfo()->gw, &check_wiz_netinfo.gw, sizeof(uint8_t) * 4),
memcmp(&get_gnetinfo()->dns, &check_wiz_netinfo.dns, sizeof(uint8_t) * 4),
memcmp(&get_gnetinfo()->dhcp, &check_wiz_netinfo.dhcp, sizeof(uint8_t)));
KPrintf("WIZCHIP set network information fail.\n");
return ERROR;
}
uint8_t tmpstr[6];
ctlwizchip(CW_GET_ID, (void*)tmpstr);
KPrintf("=== %s NET CONF ===\r\n", (char*)tmpstr);
KPrintf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n", get_gnetinfo()->mac[0],
get_gnetinfo()->mac[1], get_gnetinfo()->mac[2], get_gnetinfo()->mac[3],
get_gnetinfo()->mac[4], get_gnetinfo()->mac[5]);
KPrintf("SIP: %d.%d.%d.%d\r\n", get_gnetinfo()->ip[0], get_gnetinfo()->ip[1],
get_gnetinfo()->ip[2], get_gnetinfo()->ip[3]);
KPrintf("GAR: %d.%d.%d.%d\r\n", get_gnetinfo()->gw[0], get_gnetinfo()->gw[1],
get_gnetinfo()->gw[2], get_gnetinfo()->gw[3]);
KPrintf("SUB: %d.%d.%d.%d\r\n", get_gnetinfo()->sn[0], get_gnetinfo()->sn[1],
get_gnetinfo()->sn[2], get_gnetinfo()->sn[3]);
KPrintf("DNS: %d.%d.%d.%d\r\n", get_gnetinfo()->dns[0], get_gnetinfo()->dns[1],
get_gnetinfo()->dns[2], get_gnetinfo()->dns[3]);
KPrintf("======================\r\n");
return EOK;
return EOK;
}
/****************** spi init ******************/
static struct Bus *w5500_spi_bus;
int w5500_spi_init() {
x_err_t ret = EOK;
static struct Bus* w5500_spi_bus;
int w5500_spi_init()
{
x_err_t ret = EOK;
w5500_spi_bus = BusFind(SPI_BUS_NAME_1);
w5500_spi_bus->owner_haldev =
BusFindDevice(w5500_spi_bus, SPI_1_DEVICE_NAME_0);
w5500_spi_bus->owner_driver = BusFindDriver(w5500_spi_bus, SPI_1_DRV_NAME);
w5500_spi_bus = BusFind(SPI_BUS_NAME_1);
w5500_spi_bus->owner_haldev = BusFindDevice(w5500_spi_bus, SPI_1_DEVICE_NAME_0);
w5500_spi_bus->owner_driver = BusFindDriver(w5500_spi_bus, SPI_1_DRV_NAME);
w5500_spi_bus->match(w5500_spi_bus->owner_driver,
w5500_spi_bus->owner_haldev);
w5500_spi_bus->match(w5500_spi_bus->owner_driver,
w5500_spi_bus->owner_haldev);
struct BusConfigureInfo configure_info;
struct SpiMasterParam spi_master_param;
spi_master_param.spi_data_bit_width = 8;
spi_master_param.spi_work_mode = SPI_MODE_0 | SPI_MSB;
spi_master_param.spi_maxfrequency = SPI_LORA_FREQUENCY;
spi_master_param.spi_data_endian = 0;
struct BusConfigureInfo configure_info;
struct SpiMasterParam spi_master_param;
spi_master_param.spi_data_bit_width = 8;
spi_master_param.spi_work_mode = SPI_MODE_0 | SPI_MSB;
spi_master_param.spi_maxfrequency = SPI_LORA_FREQUENCY;
spi_master_param.spi_data_endian = 0;
configure_info.configure_cmd = OPE_CFG;
configure_info.private_data = (void *)&spi_master_param;
ret = BusDrvConfigure(w5500_spi_bus->owner_driver, &configure_info);
if (ret) {
KPrintf("spi drv OPE_CFG error drv %8p cfg %8p\n",
configure_info.configure_cmd = OPE_CFG;
configure_info.private_data = (void*)&spi_master_param;
ret = BusDrvConfigure(w5500_spi_bus->owner_driver, &configure_info);
if (ret) {
KPrintf("spi drv OPE_CFG error drv %8p cfg %8p\n",
w5500_spi_bus->owner_driver, &spi_master_param);
return ERROR;
}
return ERROR;
}
configure_info.configure_cmd = OPE_INT;
ret = BusDrvConfigure(w5500_spi_bus->owner_driver, &configure_info);
if (ret) {
KPrintf("spi drv OPE_INT error drv %8p\n", w5500_spi_bus->owner_driver);
return ERROR;
}
configure_info.configure_cmd = OPE_INT;
ret = BusDrvConfigure(w5500_spi_bus->owner_driver, &configure_info);
if (ret) {
KPrintf("spi drv OPE_INT error drv %8p\n", w5500_spi_bus->owner_driver);
return ERROR;
}
return EOK;
return EOK;
}
void spi_write_byte(uint8_t tx_data) {
struct BusBlockWriteParam write_param;
write_param.buffer = &tx_data;
write_param.size = 1;
BusDevWriteData(w5500_spi_bus->owner_haldev, &write_param);
void spi_write_byte(uint8_t tx_data)
{
struct BusBlockWriteParam write_param;
uint8_t data = tx_data;
write_param.buffer = &data;
write_param.size = 1;
BusDevWriteData(w5500_spi_bus->owner_haldev, &write_param);
}
uint8_t spi_read_byte(void) {
uint8_t result = 0;
struct BusBlockReadParam read_param;
read_param.buffer = &result;
read_param.size = 1;
BusDevReadData(w5500_spi_bus->owner_haldev, &read_param);
return result;
uint8_t spi_read_byte(void)
{
uint8_t result = 0;
struct BusBlockReadParam read_param;
read_param.buffer = &result;
read_param.size = 1;
BusDevReadData(w5500_spi_bus->owner_haldev, &read_param);
return result;
}
void spi_write_burst(uint8_t *tx_buf, uint16_t len) {
struct BusBlockWriteParam write_param;
write_param.buffer = tx_buf;
write_param.size = len;
BusDevWriteData(w5500_spi_bus->owner_haldev, &write_param);
void spi_write_burst(uint8_t* tx_buf, uint16_t len)
{
struct BusBlockWriteParam write_param;
write_param.buffer = tx_buf;
write_param.size = len;
BusDevWriteData(w5500_spi_bus->owner_haldev, &write_param);
}
void spi_read_burst(uint8_t *rx_buf, uint16_t len) {
struct BusBlockReadParam read_param;
read_param.buffer = rx_buf;
read_param.size = len;
BusDevReadData(w5500_spi_bus->owner_haldev, &read_param);
void spi_read_burst(uint8_t* rx_buf, uint16_t len)
{
struct BusBlockReadParam read_param;
read_param.buffer = rx_buf;
read_param.size = len;
BusDevReadData(w5500_spi_bus->owner_haldev, &read_param);
}
/****************** chip init ******************/
void wiz_reset() {
gpiohs_set_drive_mode(WIZ_RST_PIN, GPIO_DM_OUTPUT);
gpiohs_set_pin(WIZ_RST_PIN, GPIO_PV_LOW);
MdelayKTask(20);
void wiz_reset()
{
gpiohs_set_drive_mode(WIZ_RST_PIN, GPIO_DM_OUTPUT);
gpiohs_set_pin(WIZ_RST_PIN, GPIO_PV_LOW);
MdelayKTask(20);
gpiohs_set_pin(WIZ_RST_PIN, GPIO_PV_HIGH);
MdelayKTask(20);
gpiohs_set_pin(WIZ_RST_PIN, GPIO_PV_HIGH);
MdelayKTask(20);
}
void wiz_spi_handler_reg() {
// spi ops registration
#if (_WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_VDM_) || \
(_WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_FDM_)
/* register SPI device CS select callback function */
gpiohs_set_drive_mode(SPI1_CS0_PIN, GPIO_DM_OUTPUT);
reg_wizchip_cs_cbfunc(spi_select_cs, spi_deselete_cs);
void wiz_spi_handler_reg()
{
// spi ops registration
#if (_WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_VDM_) || (_WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_FDM_)
/* register SPI device CS select callback function */
gpiohs_set_drive_mode(SPI1_CS0_PIN, GPIO_DM_OUTPUT);
reg_wizchip_cs_cbfunc(spi_select_cs, spi_deselete_cs);
#else
#if (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SIP_) != _WIZCHIP_IO_MODE_SIP_
#error "Unknown _WIZCHIP_IO_MODE_"
#else
reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect);
reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect);
#endif
#endif
reg_wizchip_spi_cbfunc(spi_read_byte, spi_write_byte);
reg_wizchip_cris_cbfunc(spi_enter_cris, spi_exit_cris);
reg_wizchip_spiburst_cbfunc(spi_read_burst, spi_write_burst);
reg_wizchip_spi_cbfunc(spi_read_byte, spi_write_byte);
reg_wizchip_cris_cbfunc(spi_enter_cris, spi_exit_cris);
reg_wizchip_spiburst_cbfunc(spi_read_burst, spi_write_burst);
}
int wiz_chip_cfg_init() {
uint8_t mem_size[2][8] = {{2, 2, 2, 2, 2, 2, 2, 2}, {2, 2, 2, 2, 2, 2, 2, 2}};
int wiz_chip_cfg_init()
{
uint8_t mem_size[2][8] = { { 2, 2, 2, 2, 2, 2, 2, 2 }, { 2, 2, 2, 2, 2, 2, 2, 2 } };
/* reset WIZnet chip internal PHY, configures PHY mode. */
if (ctlwizchip(CW_INIT_WIZCHIP, (void *)mem_size) == -1) {
KPrintf("WIZCHIP initialize failed.");
return ERROR;
}
/* reset WIZnet chip internal PHY, configures PHY mode. */
if (ctlwizchip(CW_INIT_WIZCHIP, (void*)mem_size) == -1) {
KPrintf("WIZCHIP initialize failed.");
return ERROR;
}
struct wiz_NetTimeout_t net_timeout;
net_timeout.retry_cnt = 5;
net_timeout.time_100us = 20000;
ctlnetwork(CN_SET_TIMEOUT, (void *)&net_timeout);
struct wiz_NetTimeout_t net_timeout;
net_timeout.retry_cnt = 5;
net_timeout.time_100us = 20000;
ctlnetwork(CN_SET_TIMEOUT, (void*)&net_timeout);
return EOK;
return EOK;
}
/****************** interrupt handle ******************/
void wiz_irq_handler() {}
int wiz_interrupt_init() {
int32_t ret = -ERROR;
struct Bus *pin_bus = PinBusInitGet();
struct PinParam pin_param;
struct BusConfigureInfo pin_configure_info;
pin_configure_info.configure_cmd = OPE_CFG;
pin_configure_info.private_data = (void *)&pin_param;
pin_param.cmd = GPIO_CONFIG_MODE;
pin_param.pin = BSP_WIZ_INT_PIN;
pin_param.mode = GPIO_CFG_INPUT_PULLUP;
ret = BusDrvConfigure(pin_bus->owner_driver, &pin_configure_info);
if (ret != EOK) {
KPrintf("config pin_param %d input failed!\n", pin_param.pin);
return -ERROR;
}
pin_param.cmd = GPIO_IRQ_REGISTER;
pin_param.pin = BSP_WIZ_INT_PIN;
pin_param.irq_set.irq_mode = GPIO_IRQ_EDGE_FALLING;
pin_param.irq_set.hdr = wiz_irq_handler;
pin_param.irq_set.args = NONE;
ret = BusDrvConfigure(pin_bus->owner_driver, &pin_configure_info);
if (ret != EOK) {
KPrintf("register pin_param %d irq failed!\n", pin_param.pin);
return -ERROR;
}
pin_param.cmd = GPIO_IRQ_DISABLE;
pin_param.pin = BSP_WIZ_INT_PIN;
ret = BusDrvConfigure(pin_bus->owner_driver, &pin_configure_info);
if (ret != EOK) {
KPrintf("disable pin_param %d irq failed!\n", pin_param.pin);
return -ERROR;
}
// 4. enable interuption
pin_param.cmd = GPIO_IRQ_ENABLE;
pin_param.pin = BSP_WIZ_INT_PIN;
ret = BusDrvConfigure(pin_bus->owner_driver, &pin_configure_info);
if (ret != EOK) {
KPrintf("enable pin_param %d irq failed!\n", pin_param.pin);
return -ERROR;
}
return EOK;
return EOK;
#ifdef BSP_USING_LWIP
#include <sys.h>
static inline void spi_if_clr(void)
{
setSn_IR(0, 0x1F);
setSIR(0);
}
int HwWiznetInit(void) {
wiz_reset();
void wiz_irq_handler()
{
static x_base eth_irq_lock;
eth_irq_lock = DISABLE_INTERRUPT();
if (EOK != w5500_spi_init()) {
return ERROR;
}
if (*get_eth_recv_sem() > 0) {
sys_sem_signal(get_eth_recv_sem());
}
wiz_spi_handler_reg();
ENABLE_INTERRUPT(eth_irq_lock);
}
#else
void wiz_irq_handler()
{
static x_base eth_irq_lock;
eth_irq_lock = DISABLE_INTERRUPT();
if (EOK != wiz_chip_cfg_init()) {
return ERROR;
}
printf("=");
uint8_t ir = getIR();
setSIR(0x00);
setIR(0x00);
network_init();
ENABLE_INTERRUPT(eth_irq_lock);
}
#endif
return EOK;
void wizchip_interrupt_init(uint8_t socket, void (*callback)(void*))
{
int ret_val;
uint8_t reg_val1;
reg_val1 = (SIK_CONNECTED | SIK_DISCONNECTED | SIK_RECEIVED | SIK_TIMEOUT); // except SendOK
ret_val = wiz_ctlsocket(socket, CS_SET_INTMASK, (void*)&reg_val1);
#if (_WIZCHIP_ == W5100S)
reg_val = (1 << socket);
#elif (_WIZCHIP_ == W5500)
uint16_t reg_val2 = ((1 << socket) << 8);
#endif
ret_val = ctlwizchip(CW_SET_INTRMASK, (void*)&reg_val2);
(void)ret_val;
}
int wiz_interrupt_init()
{
int32_t ret = -ERROR;
wizchip_interrupt_init(0, wiz_irq_handler);
struct Bus* pin_bus = PinBusInitGet();
struct PinParam pin_param;
struct BusConfigureInfo pin_configure_info;
pin_configure_info.configure_cmd = OPE_CFG;
pin_configure_info.private_data = (void*)&pin_param;
pin_param.cmd = GPIO_CONFIG_MODE;
pin_param.pin = BSP_WIZ_INT_PIN;
pin_param.mode = GPIO_CFG_INPUT_PULLUP;
ret = BusDrvConfigure(pin_bus->owner_driver, &pin_configure_info);
if (ret != EOK) {
KPrintf("config pin_param %d input failed!\n", pin_param.pin);
return -ERROR;
}
pin_param.cmd = GPIO_IRQ_REGISTER;
pin_param.pin = BSP_WIZ_INT_PIN;
pin_param.irq_set.irq_mode = GPIO_IRQ_EDGE_FALLING;
pin_param.irq_set.hdr = wiz_irq_handler;
pin_param.irq_set.args = NONE;
ret = BusDrvConfigure(pin_bus->owner_driver, &pin_configure_info);
if (ret != EOK) {
KPrintf("register pin_param %d irq failed!\n", pin_param.pin);
return -ERROR;
}
pin_param.cmd = GPIO_IRQ_DISABLE;
pin_param.pin = BSP_WIZ_INT_PIN;
ret = BusDrvConfigure(pin_bus->owner_driver, &pin_configure_info);
if (ret != EOK) {
KPrintf("disable pin_param %d irq failed!\n", pin_param.pin);
return -ERROR;
}
// 4. enable interuption
pin_param.cmd = GPIO_IRQ_ENABLE;
pin_param.pin = BSP_WIZ_INT_PIN;
ret = BusDrvConfigure(pin_bus->owner_driver, &pin_configure_info);
if (ret != EOK) {
KPrintf("enable pin_param %d irq failed!\n", pin_param.pin);
return -ERROR;
}
return EOK;
}
int HwWiznetInit(void)
{
wiz_reset();
if (EOK != w5500_spi_init()) {
return ERROR;
}
wiz_spi_handler_reg();
if (EOK != wiz_chip_cfg_init()) {
return ERROR;
}
extern uint8_t wiz_mac[6];
setSHAR(wiz_mac);
ctlwizchip(CW_RESET_PHY, 0);
wiz_interrupt_init(0, wiz_irq_handler);
setSn_RXBUF_SIZE(0, 16);
setSn_TXBUF_SIZE(0, 16);
#define SOCK_ANY_PORT_NUM 0xC000
setSn_MR(0, Sn_MR_MFEN | Sn_MR_MACRAW | Sn_MR_MIP6B | Sn_MR_MMB);
wiz_socket(0, Sn_MR_MACRAW, SOCK_ANY_PORT_NUM, SOCK_IO_NONBLOCK);
uint8_t sock_iomode = SOCK_IO_NONBLOCK;
wiz_ctlsocket(0, CS_SET_IOMODE, &sock_iomode);
uint8_t sock_sr = 0;
while (1) {
sock_sr = getSn_SR(0);
SYS_KDEBUG_LOG(WIZNET_DEBUG, ("[%s] sock_sr: %x, MACRAW: %x\n", __func__, sock_sr, SOCK_MACRAW));
if (sock_sr == SOCK_MACRAW) {
SYS_KDEBUG_LOG(WIZNET_DEBUG, ("Socket 0 MACRAW mode established\r\n"));
break;
}
}
network_init();
return EOK;
}
File diff suppressed because it is too large Load Diff
@@ -54,7 +54,7 @@
//! THE POSSIBILITY OF SUCH DAMAGE.
//
//*****************************************************************************
//#include <stdio.h>
// #include <stdio.h>
#include "w5500.h"
#define _W5500_SPI_VDM_OP_ 0x00
@@ -65,191 +65,198 @@
#if (_WIZCHIP_ == 5500)
////////////////////////////////////////////////////
uint8_t WIZCHIP_READ(uint32_t AddrSel) {
uint8_t ret;
uint8_t spi_data[3];
uint8_t WIZCHIP_READ(uint32_t AddrSel)
{
uint8_t ret = 0;
uint8_t spi_data[3];
WIZCHIP_CRITICAL_ENTER();
WIZCHIP.CS._select();
WIZCHIP_CRITICAL_ENTER();
WIZCHIP.CS._select();
AddrSel |= (_W5500_SPI_READ_ | _W5500_SPI_VDM_OP_);
AddrSel |= (_W5500_SPI_READ_ | _W5500_SPI_VDM_OP_);
if (!WIZCHIP.IF.SPI._read_burst ||
!WIZCHIP.IF.SPI._write_burst) // byte operation
{
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16);
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8);
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0);
} else // burst operation
{
spi_data[0] = (AddrSel & 0x00FF0000) >> 16;
spi_data[1] = (AddrSel & 0x0000FF00) >> 8;
spi_data[2] = (AddrSel & 0x000000FF) >> 0;
WIZCHIP.IF.SPI._write_burst(spi_data, 3);
}
ret = WIZCHIP.IF.SPI._read_byte();
WIZCHIP.CS._deselect();
WIZCHIP_CRITICAL_EXIT();
return ret;
}
void WIZCHIP_WRITE(uint32_t AddrSel, uint8_t wb) {
uint8_t spi_data[4];
WIZCHIP_CRITICAL_ENTER();
WIZCHIP.CS._select();
AddrSel |= (_W5500_SPI_WRITE_ | _W5500_SPI_VDM_OP_);
// if(!WIZCHIP.IF.SPI._read_burst || !WIZCHIP.IF.SPI._write_burst) // byte
// operation
if (!WIZCHIP.IF.SPI._write_burst) // byte operation
{
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16);
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8);
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0);
WIZCHIP.IF.SPI._write_byte(wb);
} else // burst operation
{
spi_data[0] = (AddrSel & 0x00FF0000) >> 16;
spi_data[1] = (AddrSel & 0x0000FF00) >> 8;
spi_data[2] = (AddrSel & 0x000000FF) >> 0;
spi_data[3] = wb;
WIZCHIP.IF.SPI._write_burst(spi_data, 4);
}
WIZCHIP.CS._deselect();
WIZCHIP_CRITICAL_EXIT();
}
void WIZCHIP_READ_BUF(uint32_t AddrSel, uint8_t *pBuf, uint16_t len) {
uint8_t spi_data[3];
uint16_t i;
WIZCHIP_CRITICAL_ENTER();
WIZCHIP.CS._select();
AddrSel |= (_W5500_SPI_READ_ | _W5500_SPI_VDM_OP_);
if (!WIZCHIP.IF.SPI._read_burst ||
!WIZCHIP.IF.SPI._write_burst) // byte operation
{
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16);
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8);
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0);
for (i = 0; i < len; i++)
pBuf[i] = WIZCHIP.IF.SPI._read_byte();
} else // burst operation
{
spi_data[0] = (AddrSel & 0x00FF0000) >> 16;
spi_data[1] = (AddrSel & 0x0000FF00) >> 8;
spi_data[2] = (AddrSel & 0x000000FF) >> 0;
WIZCHIP.IF.SPI._write_burst(spi_data, 3);
WIZCHIP.IF.SPI._read_burst(pBuf, len);
}
WIZCHIP.CS._deselect();
WIZCHIP_CRITICAL_EXIT();
}
void WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t *pBuf, uint16_t len) {
uint8_t spi_data[3];
uint16_t i;
WIZCHIP_CRITICAL_ENTER();
WIZCHIP.CS._select();
AddrSel |= (_W5500_SPI_WRITE_ | _W5500_SPI_VDM_OP_);
if (!WIZCHIP.IF.SPI._write_burst) // byte operation
{
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16);
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8);
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0);
for (i = 0; i < len; i++)
WIZCHIP.IF.SPI._write_byte(pBuf[i]);
} else // burst operation
{
spi_data[0] = (AddrSel & 0x00FF0000) >> 16;
spi_data[1] = (AddrSel & 0x0000FF00) >> 8;
spi_data[2] = (AddrSel & 0x000000FF) >> 0;
WIZCHIP.IF.SPI._write_burst(spi_data, 3);
WIZCHIP.IF.SPI._write_burst(pBuf, len);
}
WIZCHIP.CS._deselect();
WIZCHIP_CRITICAL_EXIT();
}
uint16_t getSn_TX_FSR(uint8_t sn) {
uint16_t val = 0, val1 = 0;
do {
val1 = WIZCHIP_READ(Sn_TX_FSR(sn));
val1 = (val1 << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_FSR(sn), 1));
if (val1 != 0) {
val = WIZCHIP_READ(Sn_TX_FSR(sn));
val = (val << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_FSR(sn), 1));
if (!WIZCHIP.IF.SPI._read_burst || !WIZCHIP.IF.SPI._write_burst) // byte operation
{
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16);
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8);
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0);
} else // burst operation
{
spi_data[0] = (AddrSel & 0x00FF0000) >> 16;
spi_data[1] = (AddrSel & 0x0000FF00) >> 8;
spi_data[2] = (AddrSel & 0x000000FF) >> 0;
WIZCHIP.IF.SPI._write_burst(spi_data, 3);
}
} while (val != val1);
return val;
ret = WIZCHIP.IF.SPI._read_byte();
WIZCHIP.CS._deselect();
WIZCHIP_CRITICAL_EXIT();
return ret;
}
uint16_t getSn_RX_RSR(uint8_t sn) {
uint16_t val = 0, val1 = 0;
void WIZCHIP_WRITE(uint32_t AddrSel, uint8_t wb)
{
uint8_t spi_data[4];
do {
val1 = WIZCHIP_READ(Sn_RX_RSR(sn));
val1 = (val1 << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_RSR(sn), 1));
if (val1 != 0) {
val = WIZCHIP_READ(Sn_RX_RSR(sn));
val = (val << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_RSR(sn), 1));
WIZCHIP_CRITICAL_ENTER();
WIZCHIP.CS._select();
AddrSel |= (_W5500_SPI_WRITE_ | _W5500_SPI_VDM_OP_);
// if(!WIZCHIP.IF.SPI._read_burst || !WIZCHIP.IF.SPI._write_burst) // byte
// operation
if (!WIZCHIP.IF.SPI._write_burst) // byte operation
{
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16);
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8);
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0);
WIZCHIP.IF.SPI._write_byte(wb);
} else // burst operation
{
spi_data[0] = (AddrSel & 0x00FF0000) >> 16;
spi_data[1] = (AddrSel & 0x0000FF00) >> 8;
spi_data[2] = (AddrSel & 0x000000FF) >> 0;
spi_data[3] = wb;
WIZCHIP.IF.SPI._write_burst(spi_data, 4);
}
} while (val != val1);
return val;
WIZCHIP.CS._deselect();
WIZCHIP_CRITICAL_EXIT();
}
void wiz_send_data(uint8_t sn, uint8_t *wizdata, uint16_t len) {
uint16_t ptr = 0;
uint32_t addrsel = 0;
void WIZCHIP_READ_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len)
{
uint8_t spi_data[3];
uint16_t i;
if (len == 0)
return;
ptr = getSn_TX_WR(sn);
// M20140501 : implict type casting -> explict type casting
// addrsel = (ptr << 8) + (WIZCHIP_TXBUF_BLOCK(sn) << 3);
addrsel = ((uint32_t)ptr << 8) + (WIZCHIP_TXBUF_BLOCK(sn) << 3);
//
WIZCHIP_WRITE_BUF(addrsel, wizdata, len);
WIZCHIP_CRITICAL_ENTER();
WIZCHIP.CS._select();
ptr += len;
setSn_TX_WR(sn, ptr);
AddrSel |= (_W5500_SPI_READ_ | _W5500_SPI_VDM_OP_);
if (!WIZCHIP.IF.SPI._read_burst || !WIZCHIP.IF.SPI._write_burst) // byte operation
{
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16);
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8);
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0);
for (i = 0; i < len; i++)
pBuf[i] = WIZCHIP.IF.SPI._read_byte();
} else // burst operation
{
spi_data[0] = (AddrSel & 0x00FF0000) >> 16;
spi_data[1] = (AddrSel & 0x0000FF00) >> 8;
spi_data[2] = (AddrSel & 0x000000FF) >> 0;
WIZCHIP.IF.SPI._write_burst(spi_data, 3);
WIZCHIP.IF.SPI._read_burst(pBuf, len);
}
WIZCHIP.CS._deselect();
WIZCHIP_CRITICAL_EXIT();
}
void wiz_recv_data(uint8_t sn, uint8_t *wizdata, uint16_t len) {
uint16_t ptr = 0;
uint32_t addrsel = 0;
void WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len)
{
uint8_t spi_data[3];
uint16_t i;
if (len == 0)
return;
ptr = getSn_RX_RD(sn);
// M20140501 : implict type casting -> explict type casting
// addrsel = ((ptr << 8) + (WIZCHIP_RXBUF_BLOCK(sn) << 3);
addrsel = ((uint32_t)ptr << 8) + (WIZCHIP_RXBUF_BLOCK(sn) << 3);
//
WIZCHIP_READ_BUF(addrsel, wizdata, len);
ptr += len;
WIZCHIP_CRITICAL_ENTER();
WIZCHIP.CS._select();
setSn_RX_RD(sn, ptr);
AddrSel |= (_W5500_SPI_WRITE_ | _W5500_SPI_VDM_OP_);
if (!WIZCHIP.IF.SPI._write_burst) // byte operation
{
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16);
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8);
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0);
for (i = 0; i < len; i++)
WIZCHIP.IF.SPI._write_byte(pBuf[i]);
} else // burst operation
{
spi_data[0] = (AddrSel & 0x00FF0000) >> 16;
spi_data[1] = (AddrSel & 0x0000FF00) >> 8;
spi_data[2] = (AddrSel & 0x000000FF) >> 0;
WIZCHIP.IF.SPI._write_burst(spi_data, 3);
WIZCHIP.IF.SPI._write_burst(pBuf, len);
}
WIZCHIP.CS._deselect();
WIZCHIP_CRITICAL_EXIT();
}
void wiz_recv_ignore(uint8_t sn, uint16_t len) {
uint16_t ptr = 0;
uint16_t getSn_TX_FSR(uint8_t sn)
{
uint16_t val = 0, val1 = 0;
ptr = getSn_RX_RD(sn);
ptr += len;
setSn_RX_RD(sn, ptr);
do {
val1 = WIZCHIP_READ(Sn_TX_FSR(sn));
val1 = (val1 << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_FSR(sn), 1));
if (val1 != 0) {
val = WIZCHIP_READ(Sn_TX_FSR(sn));
val = (val << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_FSR(sn), 1));
}
} while (val != val1);
return val;
}
uint16_t getSn_RX_RSR(uint8_t sn)
{
uint16_t val = 0, val1 = 0;
do {
val1 = WIZCHIP_READ(Sn_RX_RSR(sn));
val1 = (val1 << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_RSR(sn), 1));
if (val1 != 0) {
val = WIZCHIP_READ(Sn_RX_RSR(sn));
val = (val << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_RSR(sn), 1));
}
} while (val != val1);
return val;
}
void wiz_send_data(uint8_t sn, uint8_t* wizdata, uint16_t len)
{
uint16_t ptr = 0;
uint32_t addrsel = 0;
if (len == 0)
return;
ptr = getSn_TX_WR(sn);
// M20140501 : implict type casting -> explict type casting
// addrsel = (ptr << 8) + (WIZCHIP_TXBUF_BLOCK(sn) << 3);
addrsel = ((uint32_t)ptr << 8) + (WIZCHIP_TXBUF_BLOCK(sn) << 3);
//
WIZCHIP_WRITE_BUF(addrsel, wizdata, len);
ptr += len;
setSn_TX_WR(sn, ptr);
}
void wiz_recv_data(uint8_t sn, uint8_t* wizdata, uint16_t len)
{
uint16_t ptr = 0;
uint32_t addrsel = 0;
if (len == 0)
return;
ptr = getSn_RX_RD(sn);
// M20140501 : implict type casting -> explict type casting
// addrsel = ((ptr << 8) + (WIZCHIP_RXBUF_BLOCK(sn) << 3);
addrsel = ((uint32_t)ptr << 8) + (WIZCHIP_RXBUF_BLOCK(sn) << 3);
//
WIZCHIP_READ_BUF(addrsel, wizdata, len);
ptr += len;
setSn_RX_RD(sn, ptr);
}
void wiz_recv_ignore(uint8_t sn, uint16_t len)
{
uint16_t ptr = 0;
ptr = getSn_RX_RD(sn);
ptr += len;
setSn_RX_RD(sn, ptr);
}
#endif
@@ -0,0 +1,248 @@
/**
* Copyright (c) 2022 WIZnet Co.,Ltd
*
* SPDX-License-Identifier: BSD-3-Clause
*/
/**
* ----------------------------------------------------------------------------------------------------
* Includes
* ----------------------------------------------------------------------------------------------------
*/
#include <stdio.h>
#include "connect_ethernet.h"
#include "socket.h"
#include "netif/etharp.h"
#include <sys.h>
#include <xs_kdbg.h>
#include <xizi.h>
/**
* ----------------------------------------------------------------------------------------------------
* Macros
* ----------------------------------------------------------------------------------------------------
*/
/**
* ----------------------------------------------------------------------------------------------------
* Variables
* ----------------------------------------------------------------------------------------------------
*/
uint8_t wiz_mac[6] = { 0x00, 0x08, 0xDC, 0x12, 0x34, 0x56 };
static const uint32_t ethernet_polynomial_le = 0xedb88320U;
static sys_mutex_t wiz_trans_mtx;
/**
* ----------------------------------------------------------------------------------------------------
* Functions
* ----------------------------------------------------------------------------------------------------
*/
void* ethernetif_config_enet_set(uint8_t enet_port)
{
return NONE;
}
void Time_Update_LwIP(void)
{
// no need to do
}
static inline void spi_if_clr(void)
{
setSn_IR(0, 0x1F);
setSIR(0);
}
// macraw func
uint16_t macraw_send(const uint8_t* buf, uint16_t len)
{
uint8_t sock_num = 0;
uint16_t ret = 0;
if (len > getSn_TxMAX(sock_num)) {
ret = getSn_TxMAX(sock_num);
} else {
ret = len;
}
wiz_send_data(sock_num, (uint8_t*)buf, len);
WIZCHIP_WRITE(Sn_CR(sock_num), Sn_CR_SEND);
while (WIZCHIP_READ(Sn_CR(sock_num))) {
}
while (WIZCHIP_READ(Sn_IR(sock_num)) & Sn_IR_SENDOK != Sn_IR_SENDOK) {
}
WIZCHIP_WRITE(Sn_IR(sock_num), Sn_IR_SENDOK);
return ret;
}
uint16_t macraw_recv(uint8_t* buf, uint16_t len)
{
uint8_t sock_num = 0;
uint16_t lowlevel_len = getSn_RX_RSR(sock_num);
if (lowlevel_len <= 0 || len <= 0) {
return 0;
}
uint16_t data_len = 0;
uint8_t macraw_head[2];
uint16_t ptr = getSn_RX_RD(sock_num);
uint32_t addrsel = ((uint32_t)ptr << 8) + (WIZCHIP_RXBUF_BLOCK(sock_num) << 3);
WIZCHIP_READ_BUF(addrsel, macraw_head, 2);
ptr += 2;
data_len = (macraw_head[0] << 8) + macraw_head[1] - 2;
if (data_len > 1514) {
KPrintf("[%s:%d] Info: data recved oversize: %d\n", __func__, __LINE__, data_len);
wiz_recv_ignore(sock_num, data_len);
// wiz_sock_close(sock_num);
// wiz_socket(sock_num, Sn_MR_MACRAW, 0xC000, 0);
return 0;
}
addrsel = ((uint32_t)ptr << 8) + (WIZCHIP_RXBUF_BLOCK(sock_num) << 3);
WIZCHIP_READ_BUF(addrsel, buf, data_len);
ptr += data_len;
setSn_RX_RD(sock_num, ptr);
WIZCHIP_WRITE(Sn_CR(sock_num), Sn_CR_RECV);
while (WIZCHIP_READ(Sn_CR(sock_num)))
;
return data_len;
}
void netif_link_callback(struct netif* netif)
{
printf("netif link status changed %s\n", netif_is_link_up(netif) ? "up" : "down");
}
void netif_status_callback(struct netif* netif)
{
printf("netif status changed %s\n", ip4addr_ntoa(netif_ip4_addr(netif)));
}
static int32_t wiz_transmit_pbuf(struct pbuf* p)
{
static uint8_t addr[4] = { 0xFF, 0xFF, 0xFF, 0xFF };
int32_t send_ret = 0;
while (1) {
spi_if_clr();
if ((send_ret = wiz_sock_sendto(0, (uint8_t*)p->payload, p->len, addr, 0)) <= 0) {
SYS_KDEBUG_LOG(WIZNET_DEBUG, ("[%s] data send failed: %d, sock: %x\n", __func__, send_ret, getSn_SR(0)));
return -ERROR;
}
if (p->len == p->tot_len) {
break;
}
p = p->next;
}
return EOK;
}
static struct pbuf* wiz_read_receive_pbuf(struct pbuf* buf)
{
#define RX_FRAME_SIZE 1542
static uint8_t rx_frame[RX_FRAME_SIZE];
static uint8_t addr[4] = { 0xFF, 0xFF, 0xFF, 0xFF };
uint16_t port = 0;
uint16_t lowlevel_len = getSn_RX_RSR(0);
if (lowlevel_len <= 0) {
return NULL;
}
int32_t data_len = wiz_sock_recvfrom(0, rx_frame, RX_FRAME_SIZE, addr, &port);
if (data_len > 0 && data_len <= RX_FRAME_SIZE) {
buf = pbuf_alloc(PBUF_RAW, data_len, PBUF_POOL);
if (buf == NULL) {
return NULL;
}
memcpy(buf->payload, rx_frame, data_len);
} else {
return NULL;
}
return buf;
}
void ethernetif_input(void* netif_arg)
{
struct netif* netif = (struct netif*)netif_arg;
struct pbuf* p = NULL;
for (;;) {
sys_arch_sem_wait(get_eth_recv_sem(), WAITING_FOREVER);
while (1) {
sys_mutex_lock(&wiz_trans_mtx);
spi_if_clr();
p = NULL;
p = wiz_read_receive_pbuf(p);
sys_mutex_unlock(&wiz_trans_mtx);
if (p != NULL) {
// SYS_KDEBUG_LOG(WIZNET_DEBUG, ("[%s:%d] Info Recved package with size %d\n", __func__, __LINE__, p->len));
if (ERR_OK != netif->input(p, netif)) {
pbuf_free(p);
}
p = NULL;
} else {
break;
}
}
}
}
static err_t spi_if_linkoutput(struct netif* netif, struct pbuf* p)
{
sys_mutex_lock(&wiz_trans_mtx);
if (!(getSn_SR(0) & SOCK_MACRAW)) {
SYS_KDEBUG_LOG(WIZNET_DEBUG, ("[%s:%d] err socket state %d\n", __func__, __LINE__, p->len));
wiz_sock_close(0);
setSn_MR(0, Sn_MR_MFEN | Sn_MR_MACRAW | Sn_MR_MIP6B | Sn_MR_MMB);
wiz_socket(0, Sn_MR_MACRAW, 0, SOCK_IO_NONBLOCK);
}
int32_t ret = wiz_transmit_pbuf(p);
sys_mutex_unlock(&wiz_trans_mtx);
if (ret != EOK) {
return ERR_USE;
}
/* TODO: Set up result value */
return ERR_OK;
}
err_t netif_initialize(struct netif* netif)
{
sys_mutex_new(&wiz_trans_mtx);
netif->linkoutput = spi_if_linkoutput;
// netif->linkoutput = netif_output;
netif->output = etharp_output;
netif->mtu = ETHERNET_MTU;
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6;
netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
SMEMCPY(netif->hwaddr, wiz_mac, sizeof(netif->hwaddr));
netif->hwaddr_len = sizeof(netif->hwaddr);
netif->name[0] = 'e';
netif->name[1] = '0';
return ERR_OK;
}
@@ -304,7 +304,7 @@ __exit:
if (recv_data) free(recv_data);
}
void iperf_usage(void)
static void iperf_usage(void)
{
KPrintf("Usage: iperf [-s|-c host] [options] [multi-threaded]\n");
KPrintf(" iperf [-h|--stop]\n");
@@ -326,7 +326,7 @@ void iperf_usage(void)
return;
}
int iperf(int argc, char **argv)
int wiz_iperf(int argc, char** argv)
{
int mode = 0; /* server mode */
char *host = NULL;
@@ -460,6 +460,6 @@ __usage:
return 0;
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN),
iperf, iperf,
iperf throughput test);
wiz_iperf, wiz_iperf,
iperf throughput test);
#endif
@@ -12,8 +12,8 @@
#define PING_BIND_PORT 3000
PINGMSGR PingRequest = {0};
PINGMSGR PingReply = {0};
PINGMSGR PingRequest = { 0 };
PINGMSGR PingReply = { 0 };
static uint16_t ping_RandomID = 0x1234;
static uint16_t ping_RandomSeqNum = 0x4321;
@@ -21,7 +21,7 @@ uint8_t ping_reply_received = 0;
uint8_t ping_req = 0;
uint8_t ping_rep = 0;
uint8_t ping_cnt = 0;
uint8_t ping_rep_buf[150] = {0};
uint8_t ping_rep_buf[150] = { 0 };
// ping状态机
#define PING_STA_FREE 0
@@ -32,41 +32,43 @@ uint8_t ping_rep_buf[150] = {0};
uint8_t ping_sta = PING_STA_FREE;
//当前ping的设备的序号
// 当前ping的设备的序号
uint8_t ping_socket = 0;
#define bswap_16(A) ((((uint16)(A)&0xff00) >> 8) | (((uint16)(A)&0x00ff) << 8))
#define bswap_16(A) ((((uint16)(A) & 0xff00) >> 8) | (((uint16)(A) & 0x00ff) << 8))
uint16_t htons(uint16_t n) {
union {
int i;
char c;
} u = {1};
return u.c ? bswap_16(n) : n;
uint16_t htons(uint16_t n)
{
union {
int i;
char c;
} u = { 1 };
return u.c ? bswap_16(n) : n;
}
uint16_t checksum(uint8_t *src, uint32_t len) {
uint16_t sum, tsum, i, j;
uint32_t lsum;
uint16_t checksum(uint8_t* src, uint32_t len)
{
uint16_t sum, tsum, i, j;
uint32_t lsum;
j = len >> 1;
lsum = 0;
j = len >> 1;
lsum = 0;
for (i = 0; i < j; i++) {
tsum = src[i * 2];
tsum = tsum << 8;
tsum += src[i * 2 + 1];
lsum += tsum;
}
for (i = 0; i < j; i++) {
tsum = src[i * 2];
tsum = tsum << 8;
tsum += src[i * 2 + 1];
lsum += tsum;
}
if (len % 2) {
tsum = src[i * 2];
lsum += (tsum << 8);
}
if (len % 2) {
tsum = src[i * 2];
lsum += (tsum << 8);
}
sum = lsum;
sum = ~(sum + (lsum >> 16));
return (uint16_t)sum;
sum = lsum;
sum = ~(sum + (lsum >> 16));
return (uint16_t)sum;
}
/**
@@ -76,64 +78,64 @@ uint16_t checksum(uint8_t *src, uint32_t len) {
*@param pCount- ping的次数
*@return ping成功次数
*/
uint8_t ping_count(uint8_t sn, uint16_t pCount, uint8_t *addr) {
uint16_t rlen, cnt, i;
uint8_t ping_count(uint8_t sn, uint16_t pCount, uint8_t* addr)
{
uint16_t rlen, cnt, i;
ping_reply_received = 0;
ping_req = 0;
ping_rep = 0;
KPrintf("Ping:%d.%d.%d.%d\r\n", (addr[0]), (addr[1]), (addr[2]), (addr[3]));
ping_reply_received = 0;
ping_req = 0;
ping_rep = 0;
KPrintf("Ping:%d.%d.%d.%d, socket state: %x\r\n", (addr[0]), (addr[1]), (addr[2]), (addr[3]), getSn_SR(sn));
for (i = 0; i < pCount + 1; i++) /*循环ping pCount次*/
{
switch (getSn_SR(sn)) /*获取socket状态*/
for (i = 0; i < pCount + 1; i++) /*循环ping pCount次*/
{
case SOCK_CLOSED: /*socket关闭状态*/
{
wiz_sock_close(sn);
/* Create Socket */
IINCHIP_WRITE(Sn_PROTO(sn), IPPROTO_ICMP); /*设置ICMP 协议*/
if (wiz_socket(sn, Sn_MR_IPRAW, PING_BIND_PORT, 0) !=
0) /*判断ip raw模式socket是否开启*/
switch (getSn_SR(sn)) /*获取socket状态*/
{
}
/* Check socket register */
while (getSn_SR(sn) != SOCK_IPRAW) {
MdelayKTask(50);
};
break;
}
case SOCK_IPRAW: /*ip raw模式*/
{
cnt = 0;
ping_request(sn, addr); /*发送Ping请求*/
ping_req++;
while (1) {
if ((rlen = getSn_RX_RSR(sn)) > 0) {
rlen = ping_reply(sn, addr, rlen); /*获取回复信息*/
ping_rep++;
if (ping_reply_received) {
break;
case SOCK_CLOSED: /*socket关闭状态*/
{
wiz_sock_close(sn);
/* Create Socket */
IINCHIP_WRITE(Sn_PROTO(sn), IPPROTO_ICMP); /*设置ICMP 协议*/
if (wiz_socket(sn, Sn_MR_IPRAW, PING_BIND_PORT, 0) != 0) /*判断ip raw模式socket是否开启*/
{
}
}
if ((cnt > 300)) {
cnt = 0;
/* Check socket register */
while (getSn_SR(sn) != SOCK_IPRAW) {
MdelayKTask(50);
};
break;
} else {
cnt++;
MdelayKTask(10);
}
}
break;
}
default:
break;
case SOCK_IPRAW: /*ip raw模式*/
{
cnt = 0;
ping_request(sn, addr); /*发送Ping请求*/
ping_req++;
while (1) {
if ((rlen = getSn_RX_RSR(sn)) > 0) {
rlen = ping_reply(sn, addr, rlen); /*获取回复信息*/
ping_rep++;
if (ping_reply_received) {
break;
}
}
if ((cnt > 300)) {
cnt = 0;
break;
} else {
cnt++;
MdelayKTask(10);
}
}
break;
}
default:
break;
}
if (ping_req >= pCount) {
wiz_sock_close(sn);
}
}
if (ping_req >= pCount) {
wiz_sock_close(sn);
}
}
return ping_rep;
return ping_rep;
}
/**
@@ -142,31 +144,30 @@ uint8_t ping_count(uint8_t sn, uint16_t pCount, uint8_t *addr) {
*@param addr- P地址
*@return 无
*/
uint8_t ping_request(uint8_t sn, uint8_t *addr) {
uint8_t *buffer;
uint16_t i, temp_len = 0;
ping_reply_received = 0; /*ping 回复初始化标志位*/
PingRequest.Type = PING_REQUEST; /*Ping-Request*/
PingRequest.Code = CODE_ZERO; /*总是 '0'*/
PingRequest.ID = htons(ping_RandomID++); /*设置ping响应ID为随机的整型变量*/
PingRequest.SeqNum =
htons(ping_RandomSeqNum++); /*设置ping响应的序列号为随机整形变量*/
for (i = 0; i < PING_BUF_LEN; i++) {
PingRequest.Data[i] = (i) % 8; /*ping相应的数在'0'~'8*/
}
PingRequest.CheckSum = 0;
/* 计算响应次数*/
PingRequest.CheckSum =
htons(checksum((uint8_t *)&PingRequest, sizeof(PingRequest)));
uint8_t ping_request(uint8_t sn, uint8_t* addr)
{
uint8_t* buffer;
/*发送ping响应到目的方 */
if (wiz_sock_sendto(sn, (uint8_t *)&PingRequest, sizeof(PingRequest), addr,
PING_BIND_PORT) == 0) {
KPrintf("Fail to send ping-reply packet\r\n");
} else {
KPrintf("ping send\n");
}
return 0;
uint16_t i, temp_len = 0;
ping_reply_received = 0; /*ping 回复初始化标志位*/
PingRequest.Type = PING_REQUEST; /*Ping-Request*/
PingRequest.Code = CODE_ZERO; /*总是 '0'*/
PingRequest.ID = htons(ping_RandomID++); /*设置ping响应ID为随机的整型变量*/
PingRequest.SeqNum = htons(ping_RandomSeqNum++); /*设置ping响应的序列号为随机整形变量*/
for (i = 0; i < PING_BUF_LEN; i++) {
PingRequest.Data[i] = (i) % 8; /*ping相应的数在'0'~'8*/
}
PingRequest.CheckSum = 0;
/* 计算响应次数*/
PingRequest.CheckSum = htons(checksum((uint8_t*)&PingRequest, sizeof(PingRequest)));
/*发送ping响应到目的方 */
if (wiz_sock_sendto(sn, (uint8_t*)&PingRequest, sizeof(PingRequest), addr, PING_BIND_PORT) == 0) {
KPrintf("Fail to send ping-reply packet\r\n");
} else {
KPrintf("ping send\n");
}
return 0;
}
/**
@@ -175,82 +176,84 @@ uint8_t ping_request(uint8_t sn, uint8_t *addr) {
*@param addr- Ping地址
*@return 无
*/
uint8_t ping_reply(uint8_t sn, uint8_t *addr, uint16_t rlen) {
uint16_t tmp_checksum;
uint16_t len;
uint16_t i;
uint8_t ping_reply(uint8_t sn, uint8_t* addr, uint16_t rlen)
{
uint16_t tmp_checksum;
uint16_t len;
uint16_t i;
uint16_t port = PING_BIND_PORT;
PINGMSGR PingReply;
uint16_t port = PING_BIND_PORT;
PINGMSGR PingReply;
memset(ping_rep_buf, 0, sizeof(ping_rep_buf));
len = wiz_sock_recvfrom(sn, ping_rep_buf, rlen, addr,
&port); /*从目的端接收数据*/
memset(ping_rep_buf, 0, sizeof(ping_rep_buf));
len = wiz_sock_recvfrom(sn, ping_rep_buf, rlen, addr,
&port); /*从目的端接收数据*/
if (ping_rep_buf[0] == PING_REPLY) {
PingReply.Type = ping_rep_buf[0];
PingReply.Code = ping_rep_buf[1];
PingReply.CheckSum = (ping_rep_buf[3] << 8) + ping_rep_buf[2];
PingReply.ID = (ping_rep_buf[5] << 8) + ping_rep_buf[4];
PingReply.SeqNum = (ping_rep_buf[7] << 8) + ping_rep_buf[6];
if (ping_rep_buf[0] == PING_REPLY) {
PingReply.Type = ping_rep_buf[0];
PingReply.Code = ping_rep_buf[1];
PingReply.CheckSum = (ping_rep_buf[3] << 8) + ping_rep_buf[2];
PingReply.ID = (ping_rep_buf[5] << 8) + ping_rep_buf[4];
PingReply.SeqNum = (ping_rep_buf[7] << 8) + ping_rep_buf[6];
for (i = 0; i < len - 8; i++) {
PingReply.Data[i] = ping_rep_buf[8 + i];
}
tmp_checksum = ~checksum(ping_rep_buf, len); /*检查ping回复的次数*/
if (tmp_checksum != 0xffff) {
KPrintf("tmp_checksum = %x\r\n", tmp_checksum);
for (i = 0; i < len - 8; i++) {
PingReply.Data[i] = ping_rep_buf[8 + i];
}
tmp_checksum = ~checksum(ping_rep_buf, len); /*检查ping回复的次数*/
if (tmp_checksum != 0xffff) {
KPrintf("tmp_checksum = %x\r\n", tmp_checksum);
} else {
KPrintf("Reply from %3d.%3d.%3d.%3d ID=%x Byte=%d\r\n\r\n", (addr[0]),
(addr[1]), (addr[2]), (addr[3]), htons(PingReply.ID), (rlen + 6));
ping_reply_received = 1; /*当退出ping回复循环时,设置ping回复标志为1*/
}
} else if (ping_rep_buf[0] == PING_REQUEST) {
PingReply.Code = ping_rep_buf[1];
PingReply.Type = ping_rep_buf[2];
PingReply.CheckSum = (ping_rep_buf[3] << 8) + ping_rep_buf[2];
PingReply.ID = (ping_rep_buf[5] << 8) + ping_rep_buf[4];
PingReply.SeqNum = (ping_rep_buf[7] << 8) + ping_rep_buf[6];
for (i = 0; i < len - 8; i++) {
PingReply.Data[i] = ping_rep_buf[8 + i];
}
tmp_checksum = PingReply.CheckSum; /*检查ping回复次数*/
PingReply.CheckSum = 0;
if (tmp_checksum != PingReply.CheckSum) {
KPrintf(" \n CheckSum is in correct %x shold be %x \n", (tmp_checksum),
htons(PingReply.CheckSum));
} else {
}
KPrintf(
" Request from %d.%d.%d.%d ID:%x SeqNum:%x :data size %d bytes\r\n",
(addr[0]), (addr[1]), (addr[2]), (addr[3]), (PingReply.ID),
(PingReply.SeqNum), (rlen + 6));
ping_reply_received = 1; /* 当退出ping回复循环时,设置ping回复标志为1
*/
} else {
KPrintf("Reply from %3d.%3d.%3d.%3d ID=%x Byte=%d\r\n\r\n", (addr[0]),
(addr[1]), (addr[2]), (addr[3]), htons(PingReply.ID), (rlen + 6));
ping_reply_received = 1; /*当退出ping回复循环时,设置ping回复标志为1*/
KPrintf(" Unkonwn msg. \n");
}
} else if (ping_rep_buf[0] == PING_REQUEST) {
PingReply.Code = ping_rep_buf[1];
PingReply.Type = ping_rep_buf[2];
PingReply.CheckSum = (ping_rep_buf[3] << 8) + ping_rep_buf[2];
PingReply.ID = (ping_rep_buf[5] << 8) + ping_rep_buf[4];
PingReply.SeqNum = (ping_rep_buf[7] << 8) + ping_rep_buf[6];
for (i = 0; i < len - 8; i++) {
PingReply.Data[i] = ping_rep_buf[8 + i];
}
tmp_checksum = PingReply.CheckSum; /*检查ping回复次数*/
PingReply.CheckSum = 0;
if (tmp_checksum != PingReply.CheckSum) {
KPrintf(" \n CheckSum is in correct %x shold be %x \n", (tmp_checksum),
htons(PingReply.CheckSum));
} else {
}
KPrintf(
" Request from %d.%d.%d.%d ID:%x SeqNum:%x :data size %d bytes\r\n",
(addr[0]), (addr[1]), (addr[2]), (addr[3]), (PingReply.ID),
(PingReply.SeqNum), (rlen + 6));
ping_reply_received = 1; /* 当退出ping回复循环时,设置ping回复标志为1
*/
} else {
KPrintf(" Unkonwn msg. \n");
}
return 0;
return 0;
}
void wiz_ping_test(int argc, char *argv[]) {
uint32_t tmp_ip[4];
uint8_t target_ip[4];
uint16_t pCount = 5; //默认ping 5次
void wiz_ping_test(int argc, char* argv[])
{
uint32_t tmp_ip[4];
uint8_t target_ip[4];
uint16_t pCount = 5; // 默认ping 5次
if (argc >= 2) {
KPrintf("This is a Ping test: %s\n", argv[1]);
sscanf(argv[1], "%d.%d.%d.%d", &tmp_ip[0], &tmp_ip[1], &tmp_ip[2],
&tmp_ip[3]);
target_ip[0] = (uint8_t)tmp_ip[0];
target_ip[1] = (uint8_t)tmp_ip[1];
target_ip[2] = (uint8_t)tmp_ip[2];
target_ip[3] = (uint8_t)tmp_ip[3];
if (argc >= 3){
pCount = atoi(argv[2]); //如果ip后面跟具体的数字,代表ping的次数
if (argc >= 2) {
KPrintf("This is a Ping test: %s\n", argv[1]);
sscanf(argv[1], "%d.%d.%d.%d", &tmp_ip[0], &tmp_ip[1], &tmp_ip[2],
&tmp_ip[3]);
target_ip[0] = (uint8_t)tmp_ip[0];
target_ip[1] = (uint8_t)tmp_ip[1];
target_ip[2] = (uint8_t)tmp_ip[2];
target_ip[3] = (uint8_t)tmp_ip[3];
if (argc >= 3) {
pCount = atoi(argv[2]); // 如果ip后面跟具体的数字,代表ping的次数
}
ping_count(ping_socket, pCount, target_ip);
}
ping_count(ping_socket, pCount, target_ip);
}
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN),
ping, wiz_ping_test, ping to given addr);
wiz_ping, wiz_ping_test, ping to given addr);
@@ -0,0 +1,118 @@
/**
* Copyright (c) 2022 WIZnet Co.,Ltd
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef _W5x00_LWIP_H_
#define _W5x00_LWIP_H_
/**
* ----------------------------------------------------------------------------------------------------
* Includes
* ----------------------------------------------------------------------------------------------------
*/
#include "lwip/netif.h"
/**
* ----------------------------------------------------------------------------------------------------
* Macros
* ----------------------------------------------------------------------------------------------------
*/
/* LWIP */
#define ETHERNET_MTU 1500
#define SOCKET_MACRAW 0
/**
* ----------------------------------------------------------------------------------------------------
* Variables
* ----------------------------------------------------------------------------------------------------
*/
/**
* ----------------------------------------------------------------------------------------------------
* Functions
* ----------------------------------------------------------------------------------------------------
*/
/*! \brief send an ethernet packet
* \ingroup w5x00_lwip
*
* It is used to send outgoing data to the socket.
*
* \param sn socket number
* \param buf a pointer to the data to send
* \param len the length of data in packet
* \return he sent data size
*/
int32_t send_lwip(uint8_t sn, uint8_t* buf, uint16_t len);
/*! \brief read an ethernet packet
* \ingroup w5x00_lwip
*
* It is used to read incoming data from the socket.
*
* \param sn socket number
* \param buf a pointer buffer to read incoming data
* \param len the length of the data in the packet
* \return the real received data size
*/
int32_t recv_lwip(uint8_t sn, uint8_t* buf, uint16_t len);
/*! \brief callback function
* \ingroup w5x00_lwip
*
* This function is called by ethernet_output() when it wants
* to send a packet on the interface. This function outputs
* the pbuf as-is on the link medium.
*
* \param netif a pre-allocated netif structure
* \param p main packet buffer struct
* \return ERR_OK if data was sent.
*/
err_t netif_output(struct netif* netif, struct pbuf* p);
/*! \brief callback function
* \ingroup w5x00_lwip
*
* Callback function for link.
*
* \param netif a pre-allocated netif structure
*/
void netif_link_callback(struct netif* netif);
/*! \brief callback function
* \ingroup w5x00_lwip
*
* Callback function for status.
*
* \param netif a pre-allocated netif structure
*/
void netif_status_callback(struct netif* netif);
/*! \brief callback function
* \ingroup w5x00_lwip
*
* Callback function that initializes the interface.
*
* \param netif a pre-allocated netif structure
* \return ERR_OK if Network interface initialized
*/
err_t netif_initialize(struct netif* netif);
/*! \brief ethernet frame cyclic redundancy check
* \ingroup w5x00_lwip
*
* Perform cyclic redundancy check on ethernet frame
*
* \param data a pointer to the ethernet frame
* \param length the total length of ethernet frame
* \return an ethernet frame cyclic redundancy check result value
*/
static uint32_t ethernet_frame_crc(const uint8_t* data, int length);
void ethernetif_input(void* netif_arg);
void* ethernetif_config_enet_set(uint8_t enet_port);
void Time_Update_LwIP(void);
#define NETIF_ENET0_INIT_FUNC netif_initialize
#endif /* _W5x00_LWIP_H_ */
@@ -191,8 +191,8 @@ static uint32 SpiWriteData(struct SpiHardwareDevice *spi_dev, struct SpiDataStan
for (i = 0; i < spi_datacfg->length; i++) {
tx_buff[i] = ((uint8_t *)spi_datacfg->tx_buff)[i];
}
dmac_set_single_mode(dev_param->spi_dma_param->spi_dmac_txchannel, tx_buff, (void *)(&spi_instance[device_master_id]->dr[0]), DMAC_ADDR_INCREMENT, DMAC_ADDR_NOCHANGE,
DMAC_MSIZE_4, DMAC_TRANS_WIDTH_32, spi_datacfg->length);
dmac_set_single_mode(dev_param->spi_dma_param->spi_dmac_txchannel, tx_buff, (void*)(&spi_instance[device_master_id]->dr[0]), DMAC_ADDR_INCREMENT, DMAC_ADDR_NOCHANGE,
DMAC_MSIZE_4, DMAC_TRANS_WIDTH_32, spi_datacfg->length);
}
spi_instance[device_master_id]->ser = 1U << dev_param->spi_slave_param->spi_cs_select_id;
@@ -202,9 +202,9 @@ static uint32 SpiWriteData(struct SpiHardwareDevice *spi_dev, struct SpiDataStan
spi_instance[device_master_id]->ssienr = 0x00;
transfer_done:
if (tx_buff) {
x_free(tx_buff);
}
if (tx_buff != NULL) {
x_free(tx_buff);
}
}
if (spi_datacfg->spi_cs_release) {
@@ -1,7 +1,7 @@
if BSP_USING_HWTIMER
config HWTIMER_BUS_NAME_1
string "hwtimer bus name"
default "hwtim1"
default "timer0"
menuconfig ENABLE_TIM1
bool "enable TIM1"
@@ -10,10 +10,10 @@ if BSP_USING_HWTIMER
if ENABLE_TIM1
config HWTIMER_1_DEVICE_NAME_1
string "TIM1 dev name"
default "hwtim1_dev1"
default "timer0_dev0"
config HWTIMER_DRIVER_NAME_1
string "TIM1 drv name"
default "hwtim1_drv"
default "timer0_drv"
endif
endif
@@ -42,16 +42,10 @@ uint32 HwtimerOpen(void *dev)
{
struct HwtimerHardwareDevice *hwtimer_dev = dev;
ptim2_cb_info = &hwtimer_dev->hwtimer_param.cb_info;
plic_init();
sysctl_enable_irq();
timer_init(TIMER_DEVICE_1);
size_t real_time = timer_set_interval(TIMER_DEVICE_1, TIMER_CHANNEL_1, hwtimer_dev->hwtimer_param.period_millisecond *1000);
KPrintf("timer_set_interval -- real_time : %ld\n", real_time);
timer_irq_register(TIMER_DEVICE_1, TIMER_CHANNEL_1, !hwtimer_dev->hwtimer_param.repeat, 1, timer_callback, NULL);
timer_set_enable(TIMER_DEVICE_1, TIMER_CHANNEL_1, 1);
return EOK;
@@ -73,6 +67,34 @@ static const struct HwtimerDevDone dev_done =
.read = NONE,
};
static uint32 HwTimerDrvConfigure(void *drv, struct BusConfigureInfo *configure_info)
{
NULL_PARAM_CHECK(drv);
NULL_PARAM_CHECK(configure_info);
x_err_t ret = EOK;
void* param;
switch (configure_info->configure_cmd)
{
case OPE_CFG: {
size_t real_time = timer_set_interval(TIMER_DEVICE_1, TIMER_CHANNEL_1, *(x_ticks_t *)configure_info->private_data *1000000);
KPrintf("timer_set_interval -- real_time : %ld\n", real_time);
break;
}
case OPE_INT: {
ptim2_cb_info = (struct HwtimerCallBackInfo *)malloc(sizeof(struct HwtimerCallBackInfo));
ptim2_cb_info->timeout_callback = configure_info->private_data;
timer_irq_register(TIMER_DEVICE_1, TIMER_CHANNEL_1, 0, 1, timer_callback, NULL);
break;
}
default:
break;
}
return ret;
}
/*Init hwtimer bus*/
static int BoardHwtimerBusInit(struct HwtimerBus *hwtimer_bus, struct HwtimerDriver *hwtimer_driver)
{
@@ -86,7 +108,7 @@ static int BoardHwtimerBusInit(struct HwtimerBus *hwtimer_bus, struct HwtimerDri
}
/*Init the hwtimer driver*/
hwtimer_driver->configure = NONE;
hwtimer_driver->configure = &HwTimerDrvConfigure;
ret = HwtimerDriverInit(hwtimer_driver, HWTIMER_DRIVER_NAME_1);
if (EOK != ret) {
KPrintf("board_hwtimer_init HwtimerDriverInit error %d\n", ret);
@@ -111,7 +111,7 @@ uint32_t wdt_init(wdt_device_number_t id, uint64_t time_out_ms, plic_irq_callbac
plic_irq_enable(id ? IRQN_WDT1_INTERRUPT : IRQN_WDT0_INTERRUPT);
plic_irq_register(id ? IRQN_WDT1_INTERRUPT : IRQN_WDT0_INTERRUPT, on_irq, ctx);
wdt_response_mode(id, WDT_CR_RMOD_INTERRUPT);
wdt_response_mode(id, WDT_CR_RMOD_RESET);
uint8_t m_top = wdt_get_top(id, time_out_ms);
wdt_set_timeout(id, m_top);
wdt_enable(id);
@@ -21,8 +21,12 @@ CONFIG_BSP_USING_LPUART3=y
CONFIG_SERIAL_BUS_NAME_3="uart3"
CONFIG_SERIAL_DRV_NAME_3="uart3_drv"
CONFIG_SERIAL_3_DEVICE_NAME_0="uart3_dev3"
# CONFIG_BSP_USING_LPUART4 is not set
# CONFIG_BSP_USING_LPUART8 is not set
CONFIG_BSP_USING_LPUART8=y
CONFIG_SERIAL_BUS_NAME_8="uart8"
CONFIG_SERIAL_DRV_NAME_8="uart8_drv"
CONFIG_SERIAL_8_DEVICE_NAME_0="uart8_dev8"
# CONFIG_BSP_USING_CH438 is not set
CONFIG_BSP_USING_GPIO=y
CONFIG_PIN_BUS_NAME="pin"
@@ -63,7 +67,7 @@ CONFIG___STACKSIZE__=4096
#
CONFIG_RESOURCES_SERIAL=y
CONFIG_SERIAL_USING_DMA=y
CONFIG_SERIAL_RB_BUFSZ=128
CONFIG_SERIAL_RB_BUFSZ=256
CONFIG_RESOURCES_PIN=y
#
@@ -125,7 +129,7 @@ CONFIG_ZOMBIE_KTASK_STACKSIZE=2048
#
CONFIG_KERNEL_CONSOLE=y
CONFIG_KERNEL_BANNER=y
CONFIG_KERNEL_CONSOLEBUF_SIZE=128
CONFIG_KERNEL_CONSOLEBUF_SIZE=256
#
# Kernel Hook
@@ -231,6 +235,7 @@ CONFIG_ADD_XIZI_FEATURES=y
# CONFIG_SUPPORT_KNOWING_FRAMEWORK is not set
# CONFIG_SUPPORT_CONTROL_FRAMEWORK is not set
#
# Security
#
@@ -507,7 +507,7 @@ static status_t flexspi_config_mcr1(uint32_t instance, flexspi_mem_config_t *con
// Configure MCR1
FLEXSPI->MCR1 = FLEXSPI_MCR1_SEQWAIT(seqWaitTicks) | FLEXSPI_MCR1_AHBBUSWAIT(ahbBusWaitTicks);
return kStatus_Success;
return (status_t)kStatus_Success;
}
@@ -647,14 +647,14 @@ uint8_t FLASH_WritePage(uint32_t addr, const uint32_t *buf, uint32_t len)
/*******************************************************************************
* : FLASH_Read
* : FLASH_ReadBuf
* : Flash内容
* : addr:
buf:
len:
* : kStatus_Success
*******************************************************************************/
status_t FLASH_Read(uint32_t addr, uint32_t *buf, uint32_t len)
status_t FLASH_ReadBuf(uint32_t addr, uint32_t *buf, uint32_t len)
{
status_t status;
flexspi_xfer_t flashXfer;
@@ -678,206 +678,28 @@ status_t FLASH_Read(uint32_t addr, uint32_t *buf, uint32_t len)
}
/*******************************************************************************
* : flash_erase
* : Flash指定长度的空间
* : addr:
byte_cnt:,4k字节为最小擦除单位
* : kStatus_Success
* : 4k字节的4k字节
*******************************************************************************/
status_t flash_erase(uint32_t start_addr, uint32_t byte_cnt)
{
uint32_t addr;
status_t status;
addr = start_addr;
while(addr < (byte_cnt + start_addr))
{
status = FLASH_EraseSector(addr);
if(status != kStatus_Success)
{
return status;
}
addr += FLASH_GetSectorSize();
}
return status;
}
/*******************************************************************************
* : flash_write
* : flash起始地址写入指定长度的数据
* : addr:
buf:
byte_cnt:
* : kStatus_Success
*******************************************************************************/
status_t flash_write(uint32_t start_addr, uint8_t *buf, uint32_t byte_cnt)
{
uint32_t size;
status_t status;
while(byte_cnt > 0)
{
size = byte_cnt > FLASH_PAGE_SIZE ? FLASH_PAGE_SIZE : byte_cnt;
status = FLASH_WritePage(start_addr, (void *)buf, size);
if(status != kStatus_Success)
{
return status;
}
start_addr += size;
buf += size;
byte_cnt -= size;
}
return kStatus_Success;
}
/*******************************************************************************
* : flash_read
* : Flash内容
* : addr:
buf:
len:
* : kStatus_Success
*******************************************************************************/
status_t flash_read(uint32_t addr, uint8_t *buf, uint32_t len)
{
/* For FlexSPI Memory ReadBack, use IP Command instead of AXI command for security */
if((addr >= 0x60000000) && (addr < 0x61000000))
{
return FLASH_Read(addr, (void *)buf, len);
}
else
{
void* result = memcpy(buf, (void*)addr, len);
if(result == NULL)
{
return (status_t)kStatus_Fail;
}
else
{
return (status_t)kStatus_Success;
}
}
}
/*******************************************************************************
* : flash_copy
* : flash数据在分区之间的拷贝
* : srcAddr:flash的起始地址
dstAddr:flash的起始地址;
imageSize:flash空间大小,
* : kStatus_Success
*******************************************************************************/
status_t flash_copy(uint32_t srcAddr,uint32_t dstAddr, uint32_t imageSize)
{
uint32_t PageNum, Remain, i;
status_t status;
if((srcAddr == dstAddr) || imageSize > APP_FLASH_SIZE)
{
return (status_t)kStatus_Fail;
}
status = flash_erase(dstAddr,imageSize);
if(status != kStatus_Success)
{
KPrintf("Erase flash 0x%08x failure !\r\n",dstAddr);
return status;
}
PageNum = imageSize/FLASH_PAGE_SIZE;
Remain = imageSize%FLASH_PAGE_SIZE;
for(i=0;i<PageNum;i++)
{
memset(buffer, 0, sizeof(buffer));
status = flash_read(srcAddr + i*FLASH_PAGE_SIZE, buffer, sizeof(buffer));
if(status != kStatus_Success)
{
KPrintf("Read flash 0x%08x failure !\r\n", srcAddr + i*FLASH_PAGE_SIZE);
return status;
}
status = flash_write(dstAddr+ i*FLASH_PAGE_SIZE, buffer, FLASH_PAGE_SIZE);
if(status != kStatus_Success)
{
KPrintf("Write flash 0x%08x failure !\r\n", dstAddr + i*FLASH_PAGE_SIZE);
return status;
}
}
if(Remain)
{
memset(buffer, 0, sizeof(buffer));
status = flash_read(srcAddr + i*FLASH_PAGE_SIZE, buffer, Remain);
if(status != kStatus_Success)
{
KPrintf("Read flash 0x%08x failure !\r\n", srcAddr + i*FLASH_PAGE_SIZE);
return status;
}
status = flash_write(dstAddr+ i*FLASH_PAGE_SIZE, buffer, Remain);
if(status != kStatus_Success)
{
KPrintf("Write flash 0x%08x failure !\r\n", dstAddr + i*FLASH_PAGE_SIZE);
return status;
}
}
return (status_t)kStatus_Success;
}
/*******************************************************************************
* : NOR_FLASH_Erase
* : Flash指定长度的空间,imageSize
* : addr:
imageSize:
* : None
*******************************************************************************/
status_t NOR_FLASH_Erase(uint32_t app_base_addr,uint32_t imageSize)
{
uint16_t i;
uint32_t sectorNum = (imageSize%SECTOR_SIZE != 0)? (imageSize/SECTOR_SIZE + 1):(imageSize/SECTOR_SIZE);
for(i=0;i<sectorNum;i++)
{
status_t status = FLASH_EraseSector(app_base_addr+i*SECTOR_SIZE);
if (status != kStatus_Success)
{
KPrintf("Erase_Sector 0x%x faild!\r\n",i*SECTOR_SIZE);
return status;
}
}
return kStatus_Success;
}
/*******************************************************************************
* : NorFlash_Write_PageProgram
* : Flash指定长度的数据
* : pBuffer:
WriteAddr:
NumByteToWrite:(256)
* : kStatus_Success
* : , kStatus_Success,
* : 256
*******************************************************************************/
void NorFlash_Write_PageProgram(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
status_t NorFlash_Write_PageProgram(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
{
uint8_t temp_data[256] = {0xff};
memcpy(temp_data,pBuffer,NumByteToWrite);
status_t status = FLASH_WritePage(WriteAddr,(void *)temp_data,FLASH_PAGE_SIZE);
if (status != kStatus_Success)
if(status != kStatus_Success)
{
KPrintf("Write_PageProgram 0x%x faild!\r\n",WriteAddr);
}
return (status_t)kStatus_Success;
}
@@ -887,13 +709,14 @@ void NorFlash_Write_PageProgram(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t Num
* : pBuffer:
WriteAddr:(24bit)
NumByteToWrite:(65535)
* :
* : , kStatus_Success,
* : 0XFF,0XFF处写入的数据将失败!
,,!
*******************************************************************************/
void NorFlash_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
status_t NorFlash_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
{
uint16_t pageRemain;
uint16_t pageRemain;
status_t status;
pageRemain = 256 - WriteAddr%256;//单页剩余的字节数
@@ -904,7 +727,12 @@ void NorFlash_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByte
while(1)
{
NorFlash_Write_PageProgram(pBuffer,WriteAddr,pageRemain);
status = NorFlash_Write_PageProgram(pBuffer,WriteAddr,pageRemain);
if(status != kStatus_Success)
{
KPrintf("Write_PageProgram 0x%x faild!\r\n",WriteAddr);
return status;
}
if(NumByteToWrite == pageRemain)
{
break;//写入结束了
@@ -925,25 +753,54 @@ void NorFlash_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByte
}
}
}
return (status_t)kStatus_Success;
}
/*******************************************************************************
* : NorFlash_Write
* : Flash_Erase
* : Flash指定长度的空间,imageSize
* : start_addr:
imageSize:
* : , kStatus_Success,
*******************************************************************************/
status_t Flash_Erase(uint32_t start_addr, uint32_t imageSize)
{
uint16_t i;
status_t status;
uint32_t sectorNum = (imageSize%SECTOR_SIZE != 0)? (imageSize/SECTOR_SIZE + 1):(imageSize/SECTOR_SIZE);
for(i=0;i<sectorNum;i++)
{
status = FLASH_EraseSector(start_addr+i*SECTOR_SIZE);
if (status != kStatus_Success)
{
KPrintf("Erase_Sector 0x%x faild!\r\n",i*SECTOR_SIZE);
return status;
}
}
return (status_t)kStatus_Success;
}
/*******************************************************************************
* : Flash_Write
* : W25QXX在指定地址开始写入指定长度的数据
* : pBuffer:
WriteAddr:(24bit)
WriteAddr:
NumByteToWrite:(65535)
* : None
* : , kStatus_Success,
* :
*******************************************************************************/
void NorFlash_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
status_t Flash_Write(uint32_t WriteAddr, uint8_t *pBuffer, uint32_t NumByteToWrite)
{
uint32_t secPos;
uint16_t secOff;
uint16_t secRemain;
uint16_t i;
uint8_t *NorFlash_BUF = 0;
status_t status;
NorFlash_BUF = NorFlash_BUFFER;//RAM缓冲区4K
@@ -959,7 +816,11 @@ void NorFlash_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
}
while(1)
{
FLASH_Read(CHIP_FLAH_BASE + secPos*SECTOR_SIZE, (void *)NorFlash_BUF, SECTOR_SIZE);//读出整个扇区的内容
status = FLASH_ReadBuf(CHIP_FLAH_BASE + secPos*SECTOR_SIZE, (void *)NorFlash_BUF, SECTOR_SIZE);//读出整个扇区的内容
if (status != kStatus_Success)
{
return status;
}
for(i=0;i<secRemain;i++)//校验数据
{
if(NorFlash_BUF[secOff+i] != 0xFF)
@@ -969,16 +830,28 @@ void NorFlash_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
}
if(i < secRemain)//需要擦除
{
FLASH_EraseSector(CHIP_FLAH_BASE + secPos*SECTOR_SIZE);
status = FLASH_EraseSector(CHIP_FLAH_BASE + secPos*SECTOR_SIZE);
if (status != kStatus_Success)
{
return status;
}
for(i=0;i<secRemain;i++)//复制
{
NorFlash_BUF[i+secOff] = pBuffer[i];
}
NorFlash_Write_NoCheck(NorFlash_BUF,CHIP_FLAH_BASE + secPos*SECTOR_SIZE,SECTOR_SIZE);//写入整个扇区
status = NorFlash_Write_NoCheck(NorFlash_BUF,CHIP_FLAH_BASE + secPos*SECTOR_SIZE,SECTOR_SIZE);//写入整个扇区
if (status != kStatus_Success)
{
return status;
}
}
else
{
NorFlash_Write_NoCheck(pBuffer,CHIP_FLAH_BASE + WriteAddr,secRemain);//写已经擦除了的,直接写入扇区剩余区间.
status = NorFlash_Write_NoCheck(pBuffer,CHIP_FLAH_BASE + WriteAddr,secRemain);//写已经擦除了的,直接写入扇区剩余区间.
if (status != kStatus_Success)
{
return status;
}
}
if(NumByteToWrite == secRemain)
@@ -1003,33 +876,138 @@ void NorFlash_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
}
}
}
return (status_t)kStatus_Success;
}
/*******************************************************************************
* : Flash_Read
* : Flash内容
* : addr:
buf:
len:
* : kStatus_Success
*******************************************************************************/
status_t Flash_Read(uint32_t addr, uint8_t *buf, uint32_t len)
{
/* For FlexSPI Memory ReadBack, use IP Command instead of AXI command for security */
if((addr >= 0x60000000) && (addr < 0x61000000))
{
return FLASH_ReadBuf(addr, (void *)buf, len);
}
else
{
void* result = memcpy(buf, (void*)addr, len);
if(result == NULL)
{
return (status_t)kStatus_Fail;
}
else
{
return (status_t)kStatus_Success;
}
}
}
/*******************************************************************************
* : Flash_Copy
* : flash数据在分区之间的拷贝
* : srcAddr:flash的起始地址
dstAddr:flash的起始地址;
imageSize:flash空间大小,
* : kStatus_Success
*******************************************************************************/
status_t Flash_Copy(uint32_t srcAddr,uint32_t dstAddr, uint32_t imageSize)
{
uint32_t PageNum, Remain, i;
status_t status;
if((srcAddr == dstAddr) || imageSize > APP_FLASH_SIZE)
{
return (status_t)kStatus_Fail;
}
status = Flash_Erase(dstAddr,imageSize);
if(status != kStatus_Success)
{
KPrintf("Erase flash 0x%08x failure !\r\n",dstAddr);
return status;
}
PageNum = imageSize/FLASH_PAGE_SIZE;
Remain = imageSize%FLASH_PAGE_SIZE;
for(i=0;i<PageNum;i++)
{
memset(buffer, 0, sizeof(buffer));
status = Flash_Read(srcAddr + i*FLASH_PAGE_SIZE, buffer, sizeof(buffer));
if(status != kStatus_Success)
{
KPrintf("Read flash 0x%08x failure !\r\n", srcAddr + i*FLASH_PAGE_SIZE);
return status;
}
status = Flash_Write(dstAddr+ i*FLASH_PAGE_SIZE, buffer, FLASH_PAGE_SIZE);
if(status != kStatus_Success)
{
KPrintf("Write flash 0x%08x failure !\r\n", dstAddr + i*FLASH_PAGE_SIZE);
return status;
}
}
if(Remain)
{
memset(buffer, 0, sizeof(buffer));
status = Flash_Read(srcAddr + i*FLASH_PAGE_SIZE, buffer, Remain);
if(status != kStatus_Success)
{
KPrintf("Read flash 0x%08x failure !\r\n", srcAddr + i*FLASH_PAGE_SIZE);
return status;
}
status = Flash_Write(dstAddr+ i*FLASH_PAGE_SIZE, buffer, Remain);
if(status != kStatus_Success)
{
KPrintf("Write flash 0x%08x failure !\r\n", dstAddr + i*FLASH_PAGE_SIZE);
return status;
}
}
return (status_t)kStatus_Success;
}
/*******************************************************************************
* : NOR_FLASH_Write
* : W25QXX在指定地址开始写入指定长度的数据
* : FlashAddress:Flash地址的指针
Data:
DataLength:
* : 0
* : , kStatus_Success,
*******************************************************************************/
#ifndef USE_HIGHT_SPEED_TRANS
uint32_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength)
status_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength)
{
status_t status;
uint32_t WriteAddr;
WriteAddr = *FlashAddress;
NorFlash_Write(Data,WriteAddr,DataLength);
status = Flash_Write(WriteAddr,Data,DataLength);
if (status != kStatus_Success)
{
return status;
}
*FlashAddress += DataLength;
return 0;
return (status_t)kStatus_Success;
}
#else
uint8_t packetNum = 0;
uint32_t dataLen = 0;
uint32_t WriteAddr;
uint8_t dataBuff[5*1024];
uint32_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength,uint8_t doneFlag)
status_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength,uint8_t doneFlag)
{
status_t status;
if(!doneFlag)
{
memcpy(&dataBuff[dataLen],Data,DataLength);
@@ -1042,7 +1020,11 @@ uint32_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLen
if(dataLen>=SECTOR_SIZE)
{
NorFlash_Write(dataBuff,WriteAddr,dataLen);
status = Flash_Write(WriteAddr,dataBuff,dataLen);
if (status != kStatus_Success)
{
return status;
}
packetNum = 0;
dataLen = 0;
}
@@ -1050,10 +1032,14 @@ uint32_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLen
}
else
{
NorFlash_Write(dataBuff,WriteAddr,dataLen);
status = Flash_Write(WriteAddr,dataBuff,dataLen);
if (status != kStatus_Success)
{
return status;
}
packetNum = 0;
dataLen = 0;
}
return (0);
return (status_t)kStatus_Success;;
}
#endif
@@ -280,7 +280,7 @@ int32_t Ymodem_Receive(uint8_t *buf, const uint32_t addr)
}
/* erase user application area */
NOR_FLASH_Erase(addr,size);
Flash_Erase(addr,size);
Send_Byte(ACK);
Send_Byte(CRC16);
}
@@ -300,9 +300,9 @@ int32_t Ymodem_Receive(uint8_t *buf, const uint32_t addr)
/* Write received data in Flash */
#ifndef USE_HIGHT_SPEED_TRANS
if(NOR_FLASH_Write(&flashdestination, buf, (uint16_t)packet_length) == 0)
if(NOR_FLASH_Write(&flashdestination, buf, (uint16_t)packet_length) == kStatus_Success)
#else
if(NOR_FLASH_Write(&flashdestination, buf, (uint16_t)packet_length, 0) == 0)
if(NOR_FLASH_Write(&flashdestination, buf, (uint16_t)packet_length, 0) == kStatus_Success)
#endif
{
Send_Byte(ACK);
@@ -349,7 +349,10 @@ int32_t Ymodem_Receive(uint8_t *buf, const uint32_t addr)
}
}
#ifdef USE_HIGHT_SPEED_TRANS
NOR_FLASH_Write(&flashdestination, buf, (uint16_t) packet_length,1);
if(NOR_FLASH_Write(&flashdestination, buf, (uint16_t) packet_length,1) != kStatus_Success)
{
return -4;
}
#endif
return (int32_t)size;
}
@@ -370,13 +373,12 @@ int32_t SerialDownload(const uint32_t addr)
Size = Ymodem_Receive(&tab_1024[0], addr);
if(Size > 0)
{
Serial_PutString("\n\n\r Programming Completed Successfully!\n\r--------------------------------\r\n Name: ");
Serial_PutString("\n\n\rProgramming Completed Successfully!\n\r\r\nName: ");
Serial_PutString(FileName);
Int2Str(Number, Size);
Serial_PutString("\n\r Size: ");
Serial_PutString("\n\rSize: ");
Serial_PutString(Number);
Serial_PutString(" Bytes\r\n");
Serial_PutString("-------------------\n");
}
else if(Size == -1)
{
@@ -63,21 +63,19 @@ void FLASH_Init(void);
void FLASH_DeInit(void);
uint8_t FLASH_EraseSector(uint32_t addr);
uint8_t FLASH_WritePage(uint32_t addr, const uint32_t *buf, uint32_t len);
status_t FLASH_Read(uint32_t addr, uint32_t *buf, uint32_t len);
status_t flash_erase(uint32_t start_addr, uint32_t byte_cnt);
status_t flash_write(uint32_t start_addr, uint8_t *buf, uint32_t byte_cnt);
status_t flash_read(uint32_t addr, uint8_t *buf, uint32_t len);
status_t flash_copy(uint32_t srcAddr,uint32_t dstAddr, uint32_t imageSize);
status_t FLASH_ReadBuf(uint32_t addr, uint32_t *buf, uint32_t len);
status_t NorFlash_Write_PageProgram(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);
status_t NorFlash_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);
status_t NOR_FLASH_Erase(uint32_t app_base_addr,uint32_t imageSize);
void NorFlash_Write_PageProgram(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);
void NorFlash_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);
void NorFlash_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);
status_t Flash_Erase(uint32_t start_addr, uint32_t imageSize);
status_t Flash_Write(uint32_t WriteAddr, uint8_t *pBuffer, uint32_t NumByteToWrite);
status_t Flash_Read(uint32_t addr, uint8_t *buf, uint32_t len);
status_t Flash_Copy(uint32_t srcAddr,uint32_t dstAddr, uint32_t imageSize);
#ifndef USE_HIGHT_SPEED_TRANS
uint32_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength);
status_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength);
#else
uint32_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength,uint8_t doneFlag);
status_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength,uint8_t doneFlag);
#endif
#endif