├── AT24C02-IIC ├── at24c02_i2c_drv.c └── at24c02_i2c_drv.h ├── BH1750-IIC ├── bh1750_i2c_drv.c └── bh1750_i2c_drv.h ├── DS18B20 ├── core_delay │ ├── core_delay.c │ └── core_delay.h ├── ds18b20.c └── ds18b20.h ├── DW1000 ├── decadriver │ ├── deca_device.c │ ├── deca_device_api.h │ ├── deca_param_types.h │ ├── deca_params_init.c │ ├── deca_regs.h │ ├── deca_types.h │ └── deca_version.h ├── dw1000.c ├── dw1000.h └── platform │ ├── deca_mutex.c │ ├── deca_sleep.c │ ├── deca_spi.c │ └── deca_spi.h ├── HC-SR04 ├── HC_SR04.c ├── HC_SR04.h ├── core_delay │ ├── core_delay.c │ └── core_delay.h └── readme.md ├── HTS221 ├── HTS221.c └── HTS221.h ├── KEY ├── key.c └── key.h ├── LCD-FMC ├── lcd_fmc.c └── lcd_fmc.h ├── LCD-FSMC ├── lcd_fsmc.c └── lcd_fsmc.h ├── LCD-RGB-LTDC ├── font │ ├── font_ascii.h │ ├── font_hz.h │ ├── hz16.h │ ├── hz24.h │ └── hz32.h ├── lcd_rgb_ltdc_drv.c └── lcd_rgb_ltdc_drv.h ├── LCD-SPI ├── bear.h ├── font.h ├── lcd_spi_drv.c ├── lcd_spi_drv.h └── readme.md ├── LED ├── led_drv.c └── led_drv.h ├── LICENSE ├── OLED ├── bmp.h ├── oled.c ├── oled.h └── oledfont.h ├── SDRAM ├── doc │ └── W9825G6KH.pdf ├── readme.md ├── sdram_fmc_drv.c └── sdram_fmc_drv.h ├── SGP30 ├── doc │ ├── Sensirion_Gas_Sensors_SGP30_Datasheet_EN.PDF.pdf │ └── sgp30.jpg ├── readme.md ├── sgp30.c └── sgp30.h ├── W25Q64-QSPI ├── bsp_w25q64.c └── bsp_w25q64.h ├── W25QXX-SPI ├── doc │ └── DS_W25Q64.pdf ├── readme.md ├── w25q64.c └── w25q64.h ├── W5500 ├── Application │ ├── loopback │ │ ├── loopback.c │ │ └── loopback.h │ └── multicast │ │ ├── multicast.c │ │ └── multicast.h ├── Ethernet │ ├── Socket_APIs_V3.0.3.chm │ ├── W5500 │ │ ├── w5500.c │ │ └── w5500.h │ ├── socket.c │ ├── socket.h │ ├── wizchip_conf.c │ └── wizchip_conf.h ├── Internet │ ├── DHCP │ │ ├── dhcp.c │ │ └── dhcp.h │ ├── DNS │ │ ├── dns.c │ │ └── dns.h │ ├── FTPClient │ │ ├── ftpc.c │ │ ├── ftpc.h │ │ └── stdio_private.h │ ├── FTPServer │ │ ├── ftpd.c │ │ ├── ftpd.h │ │ └── stdio_private.h │ ├── MQTT │ │ ├── MQTTClient.c │ │ ├── MQTTClient.h │ │ ├── MQTTPacket │ │ │ └── src │ │ │ │ ├── MQTTConnect.h │ │ │ │ ├── MQTTConnectClient.c │ │ │ │ ├── MQTTConnectServer.c │ │ │ │ ├── MQTTDeserializePublish.c │ │ │ │ ├── MQTTFormat.c │ │ │ │ ├── MQTTFormat.h │ │ │ │ ├── MQTTPacket.c │ │ │ │ ├── MQTTPacket.h │ │ │ │ ├── MQTTPublish.h │ │ │ │ ├── MQTTSerializePublish.c │ │ │ │ ├── MQTTSubscribe.h │ │ │ │ ├── MQTTSubscribeClient.c │ │ │ │ ├── MQTTSubscribeServer.c │ │ │ │ ├── MQTTUnsubscribe.h │ │ │ │ ├── MQTTUnsubscribeClient.c │ │ │ │ ├── MQTTUnsubscribeServer.c │ │ │ │ └── StackTrace.h │ │ ├── mqtt_interface.c │ │ └── mqtt_interface.h │ ├── SNMP │ │ ├── snmp.c │ │ ├── snmp.h │ │ ├── snmp_custom.c │ │ ├── snmp_custom.h │ │ └── tools │ │ │ ├── OID_Converter │ │ │ └── Readme.txt │ │ │ └── net-snmp-5.7(win32-bin) │ │ │ └── snmptrapd.conf │ ├── SNTP │ │ ├── sntp.c │ │ └── sntp.h │ ├── TFTP │ │ ├── netutil.c │ │ ├── netutil.h │ │ ├── tftp.c │ │ └── tftp.h │ └── httpServer │ │ ├── httpParser.c │ │ ├── httpParser.h │ │ ├── httpServer.c │ │ ├── httpServer.h │ │ ├── httpUtil.c │ │ └── httpUtil.h └── Port │ ├── w5500_port_hal.c │ └── w5500_port_hal.h └── tools ├── Image2Lcd.zip ├── MakeDot_V1_1.zip ├── PCtoLCD2002.zip └── sockettoolv25.zip /AT24C02-IIC/at24c02_i2c_drv.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @Copyright (c) 2019,mculover666 All rights reserved 3 | * @filename at24C02_i2c_drv.c 4 | * @breif Drive AT24C02(EEPROM) based on stm32 iic peripheral 5 | * @changelog v1.0 mculover666 2019/7/15 6 | * v1.1 mculover666 2021/4/15 (add WRITE_CYCLE_TIME_MS) 7 | * v1.2 mculover666 2021/4/28 (add value operation) 8 | */ 9 | 10 | #include "AT24C02_i2c_drv.h" 11 | #include "i2c.h" 12 | 13 | /** 14 | * @brief write a byte to any address 15 | * @param[in] addr address to write 16 | * @param[in] data the pointer of data to write 17 | * @return errcode 18 | * @retval 0 success 19 | * @retval -1 fail 20 | */ 21 | int AT24C02_Write_Byte(uint16_t addr, uint8_t* dat) 22 | { 23 | int ret; 24 | 25 | HAL_Delay(WRITE_CYCLE_TIME_MS); 26 | 27 | ret = HAL_I2C_Mem_Write(&hi2c1, AT24C02_ADDR_WRITE, addr, I2C_MEMADD_SIZE_8BIT, dat, 1, 100); 28 | 29 | return ret == HAL_OK ? 0 : -1; 30 | } 31 | 32 | /** 33 | * @brief read a byte from any address 34 | * @param[in] addr address to rad 35 | * @param[out] read_buf the pointer of buffer to save read data 36 | * @return errcode 37 | * @retval 0 success 38 | * @retval -1 fail 39 | */ 40 | int AT24C02_Read_Byte(uint16_t addr, uint8_t* read_buf) 41 | { 42 | int ret; 43 | 44 | HAL_Delay(WRITE_CYCLE_TIME_MS); 45 | 46 | ret = HAL_I2C_Mem_Read(&hi2c1, AT24C02_ADDR_READ, addr, I2C_MEMADD_SIZE_8BIT, read_buf, 1, 100); 47 | 48 | return ret == HAL_OK ? 0 : -1; 49 | } 50 | 51 | /** 52 | * @brief write amount bytes to any address 53 | * @param[in] addr address to write 54 | * @param[in] data the pointer of data buffer to write 55 | * @return errcode 56 | * @retval 0 success 57 | * @retval else fail 58 | */ 59 | int AT24C02_Write_Amount_Byte(uint16_t addr, uint8_t* dat, uint16_t size) 60 | { 61 | int ret; 62 | uint16_t i = 0; 63 | uint16_t cnt = 0; 64 | uint16_t page = 0; 65 | 66 | if (0 == addr % 8) { // whether the addr is the starting address of the page 67 | if (size <= 8) { //whether the number of bytes to be written is less than one page 68 | HAL_Delay(WRITE_CYCLE_TIME_MS); 69 | ret = HAL_I2C_Mem_Write(&hi2c1, AT24C02_ADDR_WRITE, addr, I2C_MEMADD_SIZE_8BIT, dat, size, 100); 70 | if (ret != HAL_OK) { 71 | return -1; 72 | } 73 | } else { 74 | page = size / 8; 75 | for (i = 0;i < page; i++) { 76 | HAL_Delay(WRITE_CYCLE_TIME_MS); 77 | ret = HAL_I2C_Mem_Write(&hi2c1, AT24C02_ADDR_WRITE, addr, I2C_MEMADD_SIZE_8BIT, &dat[cnt], 8, 100); 78 | if (ret != HAL_OK) { 79 | return -2; 80 | } 81 | addr += 8; 82 | cnt += 8; 83 | } 84 | if (cnt < size) { //write remain bytes 85 | HAL_Delay(WRITE_CYCLE_TIME_MS); 86 | ret = HAL_I2C_Mem_Write(&hi2c1, AT24C02_ADDR_WRITE, addr, I2C_MEMADD_SIZE_8BIT, &dat[cnt], size - cnt, 100); 87 | if (ret != HAL_OK) { 88 | return -3; 89 | } 90 | } 91 | } 92 | } else { //the address deviates from the starting page address 93 | if (size <= (8 - addr % 8)) { //whether we can finish writing on this page 94 | HAL_Delay(WRITE_CYCLE_TIME_MS); 95 | ret = HAL_I2C_Mem_Write(&hi2c1, AT24C02_ADDR_WRITE, addr, I2C_MEMADD_SIZE_8BIT, dat, size, 100); 96 | if (ret != HAL_OK) { 97 | return -4; 98 | } 99 | } else { //can't finish on this page 100 | //finish the page first 101 | cnt += 8 - addr % 8; 102 | HAL_Delay(WRITE_CYCLE_TIME_MS); 103 | ret = HAL_I2C_Mem_Write(&hi2c1, AT24C02_ADDR_WRITE, addr, I2C_MEMADD_SIZE_8BIT, dat, cnt, 100); 104 | if (ret != HAL_OK) { 105 | return -5; 106 | } 107 | addr += cnt; 108 | 109 | //loops to write the entire page of data 110 | page = (size - cnt) / 8; 111 | for (i = 0;i < page; i++) { 112 | HAL_Delay(WRITE_CYCLE_TIME_MS); 113 | ret = HAL_I2C_Mem_Write(&hi2c1, AT24C02_ADDR_WRITE, addr, I2C_MEMADD_SIZE_8BIT, &dat[cnt], 8, 100); 114 | if (ret != HAL_OK) { 115 | return -6; 116 | } 117 | addr += 8; 118 | cnt += 8; 119 | } 120 | if (cnt < size) { //write remain bytes 121 | HAL_Delay(WRITE_CYCLE_TIME_MS); 122 | ret = HAL_I2C_Mem_Write(&hi2c1, AT24C02_ADDR_WRITE, addr, I2C_MEMADD_SIZE_8BIT, &dat[cnt], size - cnt, 100); 123 | if (ret != HAL_OK) { 124 | return -7; 125 | } 126 | } 127 | } 128 | } 129 | 130 | return 0; 131 | } 132 | 133 | /** 134 | * @brief read amount bytes from any address 135 | * @param[in] addr address to read 136 | * @param[out] read_buf the pointer of buffer to save read data 137 | * @return errcode 138 | * @retval 0 success 139 | * @retval -1 fail 140 | */ 141 | int AT24C02_Read_Amount_Byte(uint16_t addr, uint8_t* recv_buf, uint16_t size) 142 | { 143 | int ret; 144 | 145 | HAL_Delay(WRITE_CYCLE_TIME_MS); 146 | 147 | ret = HAL_I2C_Mem_Read(&hi2c1, AT24C02_ADDR_READ, addr, I2C_MEMADD_SIZE_8BIT, recv_buf, size, 100); 148 | 149 | return ret == HAL_OK ? 0 : -1; 150 | } 151 | 152 | /** 153 | * @brief write value to any address 154 | * @param[in] addr address to write 155 | * @param[in] ptr the pointer of value to write 156 | * @return errcode 157 | * @retval 0 success 158 | * @retval else fail 159 | */ 160 | int AT24C02_Write_Value(uint16_t addr, void *ptr, uint16_t size) 161 | { 162 | int ret; 163 | 164 | if (!ptr) { 165 | return -1; 166 | } 167 | 168 | ret = AT24C02_Write_Amount_Byte(addr, (uint8_t*)ptr, size); 169 | 170 | if (ret < 0) { 171 | return ret; 172 | } 173 | 174 | return 0; 175 | } 176 | 177 | /** 178 | * @brief read value from any address 179 | * @param[in] addr address to write 180 | * @param[in] ptr the pointer of value to read 181 | * @return errcode 182 | * @retval 0 success 183 | * @retval else fail 184 | */ 185 | int AT24C02_Read_Value(uint16_t addr, void *ptr, uint16_t size) 186 | { 187 | int ret; 188 | 189 | if (!ptr) { 190 | return -1; 191 | } 192 | 193 | ret = AT24C02_Read_Amount_Byte(addr, (uint8_t*)ptr, size); 194 | 195 | if (ret < 0) { 196 | return ret; 197 | } 198 | 199 | return 0; 200 | } 201 | -------------------------------------------------------------------------------- /AT24C02-IIC/at24c02_i2c_drv.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @Copyright (c) 2019,mculover666 All rights reserved 3 | * @filename at24C02_i2c_drv.c 4 | * @breif Drive AT24C02(EEPROM) based on stm32 iic peripheral 5 | * @changelog v1.0 mculover666 2019/7/15 6 | * v1.1 mculover666 2021/4/15 (add WRITE_CYCLE_TIME_MS) 7 | * v1.2 mculover666 2021/4/28 (add value operation) 8 | */ 9 | 10 | #ifndef _AT24C02_I2C_DRV_H_ 11 | #define _AT24C02_I2C_DRV_H_ 12 | 13 | #include "stm32l4xx_hal.h" 14 | 15 | /* slave device address of eeprom */ 16 | #define AT24C02_ADDR_WRITE 0xA0 17 | #define AT24C02_ADDR_READ 0xA1 18 | 19 | /* delay time between write and write or between write and read, value is 5-10ms generally */ 20 | #define WRITE_CYCLE_TIME_MS 5 21 | 22 | 23 | int AT24C02_Write_Byte(uint16_t addr, uint8_t* dat); 24 | int AT24C02_Read_Byte(uint16_t addr, uint8_t* read_buf); 25 | int AT24C02_Write_Amount_Byte(uint16_t addr, uint8_t* dat, uint16_t size); 26 | int AT24C02_Read_Amount_Byte(uint16_t addr, uint8_t* recv_buf, uint16_t size); 27 | int AT24C02_Write_Value(uint16_t addr, void *ptr, uint16_t size); 28 | int AT24C02_Read_Value(uint16_t addr, void *ptr, uint16_t size); 29 | 30 | #endif /* _AT24C02_I2C_DRV_H_ */ 31 | -------------------------------------------------------------------------------- /BH1750-IIC/bh1750_i2c_drv.c: -------------------------------------------------------------------------------- 1 | #include "bh1750_i2c_drv.h" 2 | 3 | #if defined(USE_ST_HAL) 4 | static void bh1750_i2c_init(void) 5 | { 6 | // it will be called in main. 7 | // MX_I2C1_Init(); 8 | } 9 | 10 | static int bh1750_send_cmd(uint8_t cmd) 11 | { 12 | return HAL_I2C_Master_Transmit(&BH1750_I2C_HANDLE, BH1750_ADDR_WRITE, &cmd, 1, 0xFFFF); 13 | } 14 | 15 | static int bh1750_read_dat(uint8_t* dat) 16 | { 17 | return HAL_I2C_Master_Receive(&BH1750_I2C_HANDLE, BH1750_ADDR_READ, dat, 2, 0xFFFF); 18 | } 19 | 20 | #elif defined(USE_NXP_FSL) 21 | static void bh1750_i2c_init(void) 22 | { 23 | lpi2c_master_config_t masterConfig; 24 | 25 | /*Clock setting for LPI2C */ 26 | CLOCK_SetMux(kCLOCK_Lpi2cMux, LPI2C_CLOCK_SOURCE_SELECT); 27 | CLOCK_SetDiv(kCLOCK_Lpi2cDiv, LPI2C_CLOCK_SOURCE_DIVIDER); 28 | 29 | /* 30 | * masterConfig.debugEnable = false; 31 | * masterConfig.ignoreAck = false; 32 | * masterConfig.pinConfig = kLPI2C_2PinOpenDrain; 33 | * masterConfig.baudRate_Hz = 100000U; 34 | * masterConfig.busIdleTimeout_ns = 0; 35 | * masterConfig.pinLowTimeout_ns = 0; 36 | * masterConfig.sdaGlitchFilterWidth_ns = 0; 37 | * masterConfig.sclGlitchFilterWidth_ns = 0; 38 | */ 39 | LPI2C_MasterGetDefaultConfig(&masterConfig); 40 | 41 | /* Change the default baudrate configuration */ 42 | masterConfig.baudRate_Hz = LPI2C_BAUDRATE; 43 | 44 | /* Initialize the LPI2C master peripheral */ 45 | LPI2C_MasterInit(BH1750_I2C_MASTER, &masterConfig, LPI2C_MASTER_CLOCK_FREQUENCY); 46 | } 47 | 48 | static int bh1750_send_cmd(uint8_t cmd) 49 | { 50 | status_t reVal = kStatus_Fail; 51 | 52 | /* Send master blocking data to slave */ 53 | if (kStatus_Success != LPI2C_MasterStart(BH1750_I2C_MASTER, BH1750_DEVICE_ADDR_7BIT, kLPI2C_Write)) 54 | { 55 | return -1; 56 | } 57 | 58 | /* Check communicate with slave successful or not */ 59 | if (LPI2C_MasterGetStatusFlags(BH1750_I2C_MASTER) & kLPI2C_MasterNackDetectFlag) 60 | { 61 | return kStatus_LPI2C_Nak; 62 | } 63 | 64 | reVal = LPI2C_MasterSend(BH1750_I2C_MASTER, (uint8_t *)&cmd, 1); 65 | if (reVal != kStatus_Success) 66 | { 67 | if (reVal == kStatus_LPI2C_Nak) 68 | { 69 | LPI2C_MasterStop(BH1750_I2C_MASTER); 70 | } 71 | return -1; 72 | } 73 | 74 | reVal = LPI2C_MasterStop(BH1750_I2C_MASTER); 75 | if (reVal != kStatus_Success) 76 | { 77 | return -1; 78 | } 79 | 80 | return 0; 81 | } 82 | 83 | static int bh1750_read_dat(uint8_t* dat) 84 | { 85 | status_t reVal = kStatus_Fail; 86 | 87 | if (kStatus_Success != LPI2C_MasterStart(BH1750_I2C_MASTER, BH1750_DEVICE_ADDR_7BIT, kLPI2C_Read)) { 88 | return -1; 89 | } 90 | 91 | reVal = LPI2C_MasterReceive(BH1750_I2C_MASTER, dat, 2); 92 | if (reVal != kStatus_Success) 93 | { 94 | if (reVal == kStatus_LPI2C_Nak) 95 | { 96 | LPI2C_MasterStop(BH1750_I2C_MASTER); 97 | } 98 | return -1; 99 | } 100 | 101 | reVal = LPI2C_MasterStop(BH1750_I2C_MASTER); 102 | if (reVal != kStatus_Success) 103 | { 104 | return -1; 105 | } 106 | 107 | return 0; 108 | } 109 | #endif /* USE_ST_HAL or USE_NXP_FSL */ 110 | 111 | static uint16_t bh1750_dat2lux(uint8_t* dat) 112 | { 113 | uint16_t lux = 0; 114 | 115 | lux = dat[0]; 116 | lux <<= 8; 117 | lux += dat[1]; 118 | lux = (int)(lux / 1.2); 119 | 120 | return lux; 121 | } 122 | 123 | int bh1750_init(void) 124 | { 125 | bh1750_i2c_init(); 126 | return 0; 127 | } 128 | 129 | int bh1750_start(bh1750_mode_t mode) 130 | { 131 | return bh1750_send_cmd(mode); 132 | } 133 | 134 | int bh1750_read_lux(uint16_t *lux) 135 | { 136 | uint8_t dat[2] = {0}; 137 | 138 | if (bh1750_read_dat(dat) != 0) { 139 | return -1; 140 | } 141 | 142 | *lux = bh1750_dat2lux(dat); 143 | 144 | return 0; 145 | } 146 | -------------------------------------------------------------------------------- /BH1750-IIC/bh1750_i2c_drv.h: -------------------------------------------------------------------------------- 1 | #ifndef _BH1750_I2C_DRV_H_ 2 | #define _BH1750_I2C_DRV_H_ 3 | 4 | //#define USE_ST_HAL 5 | #define USE_NXP_FSL 6 | 7 | #if defined(USE_ST_HAL) 8 | 9 | #include "stm32l4xx_hal.h" 10 | 11 | // address(7 bit) + read or write(1 bit) 12 | #define BH1750_ADDR_WRITE 0x46 13 | #define BH1750_ADDR_READ 0x47 14 | #define BH1750_I2C_HANDLE hi2c1 15 | 16 | #elif defined(USE_NXP_FSL) 17 | 18 | #include "fsl_lpi2c.h" 19 | 20 | #define BH1750_DEVICE_ADDR_7BIT 0x23 21 | #define BH1750_I2C_MASTER LPI2C1 22 | /* Select USB1 PLL (480 MHz) as master lpi2c clock source */ 23 | #define LPI2C_CLOCK_SOURCE_SELECT (0U) 24 | /* Clock divider for master lpi2c clock source */ 25 | #define LPI2C_CLOCK_SOURCE_DIVIDER (5U) 26 | /* Get frequency of lpi2c clock */ 27 | #define LPI2C_CLOCK_FREQUENCY ((CLOCK_GetFreq(kCLOCK_Usb1PllClk) / 8) / (LPI2C_CLOCK_SOURCE_DIVIDER + 1U)) 28 | #define LPI2C_MASTER_CLOCK_FREQUENCY LPI2C_CLOCK_FREQUENCY 29 | #define LPI2C_BAUDRATE 100000U 30 | 31 | #endif /* USE_ST_HAL or USE_NXP_FSL */ 32 | 33 | typedef enum 34 | { 35 | POWER_OFF_CMD = 0x00, // Power off 36 | POWER_ON_CMD = 0x01, // Power on 37 | RESET_REGISTER = 0x07, // Reset digital register 38 | CONT_H_MODE = 0x10, // Continuous high resolution mode, measurement time 120ms 39 | CONT_H_MODE2 = 0x11, // Continuous high resolution mode2, measurement time 120ms 40 | CONT_L_MODE = 0x13, // Continuous low resolution mode, measurement time 16ms 41 | ONCE_H_MODE = 0x20, // Once high resolution mode, measurement time 120ms 42 | ONCE_H_MODE2 = 0x21, // Once high resolution mode2, measurement time 120ms 43 | ONCE_L_MODE = 0x23 // Once low resolution mode2, measurement time 120ms 44 | } bh1750_mode_t; 45 | 46 | int bh1750_init(void); 47 | int bh1750_start(bh1750_mode_t mode); 48 | int bh1750_read_lux(uint16_t *lux); 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /DS18B20/core_delay/core_delay.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/DS18B20/core_delay/core_delay.c -------------------------------------------------------------------------------- /DS18B20/core_delay/core_delay.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/DS18B20/core_delay/core_delay.h -------------------------------------------------------------------------------- /DS18B20/ds18b20.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/DS18B20/ds18b20.c -------------------------------------------------------------------------------- /DS18B20/ds18b20.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/DS18B20/ds18b20.h -------------------------------------------------------------------------------- /DW1000/decadriver/deca_device.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/DW1000/decadriver/deca_device.c -------------------------------------------------------------------------------- /DW1000/decadriver/deca_device_api.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/DW1000/decadriver/deca_device_api.h -------------------------------------------------------------------------------- /DW1000/decadriver/deca_param_types.h: -------------------------------------------------------------------------------- 1 | /*! ---------------------------------------------------------------------------- 2 | * @file deca_param_types.h 3 | * @brief Decawave general type definitions for configuration structures 4 | * 5 | * @attention 6 | * 7 | * Copyright 2013 (c) Decawave Ltd, Dublin, Ireland. 8 | * 9 | * All rights reserved. 10 | * 11 | */ 12 | #ifndef _DECA_PARAM_TYPES_H_ 13 | #define _DECA_PARAM_TYPES_H_ 14 | 15 | #ifdef __cplusplus 16 | extern "C" { 17 | #endif 18 | #include "deca_types.h" 19 | 20 | #define NUM_BR 3 21 | #define NUM_PRF 2 22 | #define NUM_PACS 4 23 | #define NUM_BW 2 //2 bandwidths are supported 24 | #define NUM_SFD 2 //supported number of SFDs - standard = 0, non-standard = 1 25 | #define NUM_CH 6 //supported channels are 1, 2, 3, 4, 5, 7 26 | #define NUM_CH_SUPPORTED 8 //supported channels are '0', 1, 2, 3, 4, 5, '6', 7 27 | #define PCODES 25 //supported preamble codes 28 | 29 | 30 | typedef struct { 31 | uint32 lo32; 32 | uint16 target[NUM_PRF]; 33 | } agc_cfg_struct ; 34 | 35 | extern const agc_cfg_struct agc_config ; 36 | 37 | //SFD threshold settings for 110k, 850k, 6.8Mb standard and non-standard 38 | extern const uint16 sftsh[NUM_BR][NUM_SFD]; 39 | 40 | extern const uint16 dtune1[NUM_PRF]; 41 | 42 | #define XMLPARAMS_VERSION (1.17f) 43 | 44 | extern const uint32 fs_pll_cfg[NUM_CH]; 45 | extern const uint8 fs_pll_tune[NUM_CH]; 46 | extern const uint8 rx_config[NUM_BW]; 47 | extern const uint32 tx_config[NUM_CH]; 48 | extern const uint8 dwnsSFDlen[NUM_BR]; //length of SFD for each of the bitrates 49 | extern const uint32 digital_bb_config[NUM_PRF][NUM_PACS]; 50 | extern const uint8 chan_idx[NUM_CH_SUPPORTED]; 51 | extern const double txpwr_compensation[NUM_CH]; 52 | 53 | #define PEAK_MULTPLIER (0x60) //3 -> (0x3 * 32) & 0x00E0 54 | #define N_STD_FACTOR (13) 55 | #define LDE_PARAM1 (PEAK_MULTPLIER | N_STD_FACTOR) 56 | 57 | #define LDE_PARAM3_16 (0x1607) 58 | #define LDE_PARAM3_64 (0x0607) 59 | 60 | #define MIXER_GAIN_STEP (0.5) 61 | #define DA_ATTN_STEP (2.5) 62 | 63 | extern const uint16 lde_replicaCoeff[PCODES]; 64 | 65 | #ifdef __cplusplus 66 | } 67 | #endif 68 | 69 | #endif 70 | 71 | 72 | -------------------------------------------------------------------------------- /DW1000/decadriver/deca_params_init.c: -------------------------------------------------------------------------------- 1 | /*! ---------------------------------------------------------------------------- 2 | * @file deca_params_init.c 3 | * @brief DW1000 configuration parameters 4 | * 5 | * @attention 6 | * 7 | * Copyright 2013 (c) Decawave Ltd, Dublin, Ireland. 8 | * 9 | * All rights reserved. 10 | * 11 | * 12 | * ------------------------------------------------------------------------------------------------------------------- 13 | **/ 14 | #include 15 | #include 16 | 17 | #include "deca_regs.h" 18 | #include "deca_device_api.h" 19 | #include "deca_param_types.h" 20 | 21 | 22 | //----------------------------------------- 23 | // map the channel number to the index in the configuration arrays below 24 | // 0th element is chan 1, 1st is chan 2, 2nd is chan 3, 3rd is chan 4, 4th is chan 5, 5th is chan 7 25 | const uint8 chan_idx[NUM_CH_SUPPORTED] = {0, 0, 1, 2, 3, 4, 0, 5}; 26 | 27 | //----------------------------------------- 28 | const uint32 tx_config[NUM_CH] = 29 | { 30 | RF_TXCTRL_CH1, 31 | RF_TXCTRL_CH2, 32 | RF_TXCTRL_CH3, 33 | RF_TXCTRL_CH4, 34 | RF_TXCTRL_CH5, 35 | RF_TXCTRL_CH7, 36 | }; 37 | 38 | //Frequency Synthesiser - PLL configuration 39 | const uint32 fs_pll_cfg[NUM_CH] = 40 | { 41 | FS_PLLCFG_CH1, 42 | FS_PLLCFG_CH2, 43 | FS_PLLCFG_CH3, 44 | FS_PLLCFG_CH4, 45 | FS_PLLCFG_CH5, 46 | FS_PLLCFG_CH7 47 | }; 48 | 49 | //Frequency Synthesiser - PLL tuning 50 | const uint8 fs_pll_tune[NUM_CH] = 51 | { 52 | FS_PLLTUNE_CH1, 53 | FS_PLLTUNE_CH2, 54 | FS_PLLTUNE_CH3, 55 | FS_PLLTUNE_CH4, 56 | FS_PLLTUNE_CH5, 57 | FS_PLLTUNE_CH7 58 | }; 59 | 60 | //bandwidth configuration 61 | const uint8 rx_config[NUM_BW] = 62 | { 63 | RF_RXCTRLH_NBW, 64 | RF_RXCTRLH_WBW 65 | }; 66 | 67 | 68 | const agc_cfg_struct agc_config = 69 | { 70 | AGC_TUNE2_VAL, 71 | { AGC_TUNE1_16M , AGC_TUNE1_64M } //adc target 72 | }; 73 | 74 | //DW non-standard SFD length for 110k, 850k and 6.81M 75 | const uint8 dwnsSFDlen[NUM_BR] = 76 | { 77 | DW_NS_SFD_LEN_110K, 78 | DW_NS_SFD_LEN_850K, 79 | DW_NS_SFD_LEN_6M8 80 | }; 81 | 82 | // SFD Threshold 83 | const uint16 sftsh[NUM_BR][NUM_SFD] = 84 | { 85 | { 86 | DRX_TUNE0b_110K_STD, 87 | DRX_TUNE0b_110K_NSTD 88 | }, 89 | { 90 | DRX_TUNE0b_850K_STD, 91 | DRX_TUNE0b_850K_NSTD 92 | }, 93 | { 94 | DRX_TUNE0b_6M8_STD, 95 | DRX_TUNE0b_6M8_NSTD 96 | } 97 | }; 98 | 99 | const uint16 dtune1[NUM_PRF] = 100 | { 101 | DRX_TUNE1a_PRF16, 102 | DRX_TUNE1a_PRF64 103 | }; 104 | 105 | const uint32 digital_bb_config[NUM_PRF][NUM_PACS] = 106 | { 107 | { 108 | DRX_TUNE2_PRF16_PAC8, 109 | DRX_TUNE2_PRF16_PAC16, 110 | DRX_TUNE2_PRF16_PAC32, 111 | DRX_TUNE2_PRF16_PAC64 112 | }, 113 | { 114 | DRX_TUNE2_PRF64_PAC8, 115 | DRX_TUNE2_PRF64_PAC16, 116 | DRX_TUNE2_PRF64_PAC32, 117 | DRX_TUNE2_PRF64_PAC64 118 | } 119 | }; 120 | 121 | const uint16 lde_replicaCoeff[PCODES] = 122 | { 123 | 0, // No preamble code 0 124 | LDE_REPC_PCODE_1, 125 | LDE_REPC_PCODE_2, 126 | LDE_REPC_PCODE_3, 127 | LDE_REPC_PCODE_4, 128 | LDE_REPC_PCODE_5, 129 | LDE_REPC_PCODE_6, 130 | LDE_REPC_PCODE_7, 131 | LDE_REPC_PCODE_8, 132 | LDE_REPC_PCODE_9, 133 | LDE_REPC_PCODE_10, 134 | LDE_REPC_PCODE_11, 135 | LDE_REPC_PCODE_12, 136 | LDE_REPC_PCODE_13, 137 | LDE_REPC_PCODE_14, 138 | LDE_REPC_PCODE_15, 139 | LDE_REPC_PCODE_16, 140 | LDE_REPC_PCODE_17, 141 | LDE_REPC_PCODE_18, 142 | LDE_REPC_PCODE_19, 143 | LDE_REPC_PCODE_20, 144 | LDE_REPC_PCODE_21, 145 | LDE_REPC_PCODE_22, 146 | LDE_REPC_PCODE_23, 147 | LDE_REPC_PCODE_24 148 | }; 149 | 150 | const double txpwr_compensation[NUM_CH] = { 151 | 0.0, 152 | 0.035, 153 | 0.0, 154 | 0.0, 155 | 0.065, 156 | 0.0 157 | }; 158 | 159 | -------------------------------------------------------------------------------- /DW1000/decadriver/deca_regs.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/DW1000/decadriver/deca_regs.h -------------------------------------------------------------------------------- /DW1000/decadriver/deca_types.h: -------------------------------------------------------------------------------- 1 | /*! ---------------------------------------------------------------------------- 2 | * @file deca_types.h 3 | * @brief Decawave general type definitions 4 | * 5 | * @attention 6 | * 7 | * Copyright 2013 (c) Decawave Ltd, Dublin, Ireland. 8 | * 9 | * All rights reserved. 10 | * 11 | */ 12 | 13 | #ifndef _DECA_TYPES_H_ 14 | #define _DECA_TYPES_H_ 15 | 16 | #ifdef __cplusplus 17 | extern "C" { 18 | #endif 19 | 20 | #ifndef uint8 21 | #ifndef _DECA_UINT8_ 22 | #define _DECA_UINT8_ 23 | typedef unsigned char uint8; 24 | #endif 25 | #endif 26 | 27 | #ifndef uint16 28 | #ifndef _DECA_UINT16_ 29 | #define _DECA_UINT16_ 30 | typedef unsigned short uint16; 31 | #endif 32 | #endif 33 | 34 | #ifndef uint32 35 | #ifndef _DECA_UINT32_ 36 | #define _DECA_UINT32_ 37 | typedef unsigned long uint32; 38 | #endif 39 | #endif 40 | 41 | #ifndef int8 42 | #ifndef _DECA_INT8_ 43 | #define _DECA_INT8_ 44 | typedef signed char int8; 45 | #endif 46 | #endif 47 | 48 | #ifndef int16 49 | #ifndef _DECA_INT16_ 50 | #define _DECA_INT16_ 51 | typedef signed short int16; 52 | #endif 53 | #endif 54 | 55 | #ifndef int32 56 | #ifndef _DECA_INT32_ 57 | #define _DECA_INT32_ 58 | typedef signed long int32; 59 | #endif 60 | #endif 61 | 62 | #ifndef NULL 63 | #define NULL ((void *)0UL) 64 | #endif 65 | 66 | #ifdef __cplusplus 67 | } 68 | #endif 69 | 70 | #endif /* DECA_TYPES_H_ */ 71 | 72 | 73 | -------------------------------------------------------------------------------- /DW1000/decadriver/deca_version.h: -------------------------------------------------------------------------------- 1 | /*! ---------------------------------------------------------------------------- 2 | * @file deca_version.h 3 | * @brief Defines the version info for the DW1000 device driver including its API 4 | * 5 | * @attention 6 | * 7 | * Copyright 2013 (c) Decawave Ltd, Dublin, Ireland. 8 | * 9 | * All rights reserved. 10 | * 11 | */ 12 | 13 | #ifndef _DECA_VERSION_H_ 14 | #define _DECA_VERSION_H_ 15 | 16 | // 17 | // The DW1000 device driver is separately version numbered to any version the application using it may have 18 | // 19 | // Two symbols are defined here: one hexadecimal value and one string that includes the hex bytes. 20 | // Both should be updated together in a consistent way when the software is being modified. 21 | // 22 | // The format of the hex version is 0xAABBCC and the string ends with AA.BB.CC, where... 23 | // 24 | // Quantity CC is updated for minor changes/bug fixes that should not need user code changes 25 | // Quantity BB is updated for changes/bug fixes that may need user code changes 26 | // Quantity AA is updated for major changes that will need user code changes 27 | // 28 | 29 | #define DW1000_DRIVER_VERSION 0x040006 30 | #define DW1000_DEVICE_DRIVER_VER_STRING "DW1000 Device Driver Version 04.00.06" 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /DW1000/dw1000.c: -------------------------------------------------------------------------------- 1 | #include "dw1000.h" 2 | 3 | void reset_DW1000(void) 4 | { 5 | GPIO_InitTypeDef GPIO_InitStruct = {0}; 6 | 7 | // Enable GPIO used for DW1000 reset 8 | // done by main.c(MX_GPIO_Init) 9 | 10 | //drive the RSTn pin low 11 | HAL_GPIO_WritePin(DW1000_RST_PORT, DW1000_RST_PIN, GPIO_PIN_RESET); 12 | 13 | //put the pin back to tri-state ... as input 14 | GPIO_InitStruct.Pin = DW1000_RST_PIN; 15 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 16 | GPIO_InitStruct.Pull = GPIO_NOPULL; 17 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 18 | HAL_GPIO_Init(DW1000_RST_PORT, &GPIO_InitStruct); 19 | 20 | HAL_Delay(2); 21 | } 22 | 23 | static void SPI_ChangeRate(SPI_TypeDef *SPIx, uint16_t scalingfactor) 24 | { 25 | uint16_t tmpreg = 0; 26 | 27 | /* Get the SPIx CR1 value */ 28 | tmpreg = SPIx->CR1; 29 | 30 | /*clear the scaling bits*/ 31 | tmpreg &= 0xFFC7; 32 | 33 | /*set the scaling bits*/ 34 | tmpreg |= scalingfactor; 35 | 36 | /* Write to SPIx CR1 */ 37 | SPIx->CR1 = tmpreg; 38 | } 39 | 40 | void spi_set_rate_low(void) 41 | { 42 | SPI_ChangeRate(DW1000_SPI_Handle.Instance, SPI_BAUDRATEPRESCALER_32); 43 | } 44 | 45 | void spi_set_rate_high(void) 46 | { 47 | SPI_ChangeRate(DW1000_SPI_Handle.Instance, SPI_BAUDRATEPRESCALER_4); 48 | } 49 | -------------------------------------------------------------------------------- /DW1000/dw1000.h: -------------------------------------------------------------------------------- 1 | #ifndef _DW1000_H_ 2 | #define _DW1000_H_ 3 | 4 | #include "stm32l4xx_hal.h" 5 | 6 | /* 7 | CS <------------- PA4 8 | RST <------------- PB9 9 | WAKEUP <---------- PA8 10 | */ 11 | #define DW1000_CS_PORT GPIOA 12 | #define DW1000_CS_PIN GPIO_PIN_4 13 | #define DW1000_RST_PORT GPIOB 14 | #define DW1000_RST_PIN GPIO_PIN_9 15 | #define DW1000_WAKEUP_PORT GPIOA 16 | #define DW10000_WAKEUP_PIN GPIO_PIN_8 17 | 18 | /* 19 | IRQ --------------> PC9 20 | */ 21 | #define DW1000_IRQn_TYPE EXTI9_5_IRQn 22 | #define DW1000_IRQ_PORT GPIOC 23 | #define DW1000_IRQ_PIN GPIO_PIN_9 24 | 25 | /* 26 | SPI Interface <---> SPI1 27 | SPI_CS <----------> PA4 28 | SPI_CLK <---------> PA1 29 | SPI_MISO <--------> PA6 30 | SPI_MOSI <--------> PA12 31 | */ 32 | extern SPI_HandleTypeDef hspi1; 33 | #define DW1000_SPI_Handle hspi1 34 | 35 | void reset_DW1000(void); 36 | void spi_set_rate_low(void); 37 | void spi_set_rate_high(void); 38 | 39 | #endif /* _DW1000_H_ */ 40 | -------------------------------------------------------------------------------- /DW1000/platform/deca_mutex.c: -------------------------------------------------------------------------------- 1 | /*! ---------------------------------------------------------------------------- 2 | * @file deca_mutex.c 3 | * @brief IRQ interface / mutex implementation 4 | * 5 | * @attention 6 | * 7 | * Copyright 2013 (c) DecaWave Ltd, Dublin, Ireland. 8 | * 9 | * All rights reserved. 10 | * 11 | */ 12 | 13 | #include "deca_device_api.h" 14 | #include "dw1000.h" 15 | // --------------------------------------------------------------------------- 16 | // 17 | // NB: The purpose of this file is to provide for microprocessor interrupt enable/disable, this is used for 18 | // controlling mutual exclusion from critical sections in the code where interrupts and background 19 | // processing may interact. The code using this is kept to a minimum and the disabling time is also 20 | // kept to a minimum, so blanket interrupt disable may be the easiest way to provide this. But at a 21 | // minimum those interrupts coming from the decawave device should be disabled/re-enabled by this activity. 22 | // 23 | // In porting this to a particular microprocessor, the implementer may choose to use #defines in the 24 | // deca_irq.h include file to map these calls transparently to the target system. Alternatively the 25 | // appropriate code may be embedded in the functions provided below. 26 | // 27 | // This mutex dependent on HW port. 28 | // If HW port uses EXT_IRQ line to receive ready/busy status from DW1000 then mutex should use this signal 29 | // If HW port not use EXT_IRQ line (i.e. SW polling) then no necessary for decamutex(on/off) 30 | // 31 | // For critical section use this mutex instead 32 | // __save_intstate() 33 | // __restore_intstate() 34 | // --------------------------------------------------------------------------- 35 | 36 | 37 | /*! ------------------------------------------------------------------------------------------------------------------ 38 | * Function: decamutexon() 39 | * 40 | * Description: This function should disable interrupts. This is called at the start of a critical section 41 | * It returns the irq state before disable, this value is used to re-enable in decamutexoff call 42 | * 43 | * Note: The body of this function is defined in deca_mutex.c and is platform specific 44 | * 45 | * input parameters: 46 | * 47 | * output parameters 48 | * 49 | * returns the state of the DW1000 interrupt 50 | */ 51 | decaIrqStatus_t decamutexon(void) 52 | { 53 | decaIrqStatus_t s = __HAL_GPIO_EXTI_GET_FLAG(DW1000_IRQ_PIN); 54 | 55 | if(s) { 56 | HAL_NVIC_DisableIRQ(DW1000_IRQn_TYPE); //disable the external interrupt line 57 | } 58 | return s ; // return state before disable, value is used to re-enable in decamutexoff call 59 | } 60 | 61 | /*! ------------------------------------------------------------------------------------------------------------------ 62 | * Function: decamutexoff() 63 | * 64 | * Description: This function should re-enable interrupts, or at least restore their state as returned(&saved) by decamutexon 65 | * This is called at the end of a critical section 66 | * 67 | * Note: The body of this function is defined in deca_mutex.c and is platform specific 68 | * 69 | * input parameters: 70 | * @param s - the state of the DW1000 interrupt as returned by decamutexon 71 | * 72 | * output parameters 73 | * 74 | * returns the state of the DW1000 interrupt 75 | */ 76 | void decamutexoff(decaIrqStatus_t s) // put a function here that re-enables the interrupt at the end of the critical section 77 | { 78 | if(s) { //need to check the port state as we can't use level sensitive interrupt on the STM ARM 79 | HAL_NVIC_EnableIRQ(DW1000_IRQn_TYPE); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /DW1000/platform/deca_sleep.c: -------------------------------------------------------------------------------- 1 | /*! ---------------------------------------------------------------------------- 2 | * @file deca_sleep.c 3 | * @brief platform dependent sleep implementation 4 | * 5 | * @attention 6 | * 7 | * Copyright 2015 (c) DecaWave Ltd, Dublin, Ireland. 8 | * 9 | * All rights reserved. 10 | * 11 | * @author DecaWave 12 | */ 13 | 14 | #include "deca_device_api.h" 15 | #include "dw1000.h" 16 | 17 | /* Wrapper function to be used by decadriver. Declared in deca_device_api.h */ 18 | void deca_sleep(unsigned int time_ms) 19 | { 20 | HAL_Delay(time_ms); 21 | } 22 | -------------------------------------------------------------------------------- /DW1000/platform/deca_spi.c: -------------------------------------------------------------------------------- 1 | /*! ---------------------------------------------------------------------------- 2 | * @file deca_spi.c 3 | * @brief SPI access functions 4 | * 5 | * @attention 6 | * 7 | * Copyright 2013 (c) DecaWave Ltd, Dublin, Ireland. 8 | * 9 | * All rights reserved. 10 | * 11 | * @author DecaWave 12 | */ 13 | #include 14 | 15 | #include "deca_spi.h" 16 | #include "deca_device_api.h" 17 | #include "dw1000.h" 18 | 19 | /*! ------------------------------------------------------------------------------------------------------------------ 20 | * Function: openspi() 21 | * 22 | * Low level abstract function to open and initialise access to the SPI device. 23 | * returns 0 for success, or -1 for error 24 | */ 25 | int openspi(/*SPI_TypeDef* SPIx*/) 26 | { 27 | // done by main.c, default SPI used is SPI1 28 | 29 | return 0; 30 | 31 | } // end openspi() 32 | 33 | /*! ------------------------------------------------------------------------------------------------------------------ 34 | * Function: closespi() 35 | * 36 | * Low level abstract function to close the the SPI device. 37 | * returns 0 for success, or -1 for error 38 | */ 39 | int closespi(void) 40 | { 41 | 42 | return 0; 43 | 44 | } // end closespi() 45 | 46 | /*! ------------------------------------------------------------------------------------------------------------------ 47 | * Function: writetospi() 48 | * 49 | * Low level abstract function to write to the SPI 50 | * Takes two separate byte buffers for write header and write data 51 | * returns 0 for success, or -1 for error 52 | */ 53 | int writetospi(uint16 headerLength, const uint8 *headerBuffer, uint32 bodylength, const uint8 *bodyBuffer) 54 | { 55 | HAL_StatusTypeDef status; 56 | decaIrqStatus_t stat; 57 | 58 | stat = decamutexon() ; 59 | 60 | /* Enable DW1000_CS */ 61 | HAL_GPIO_WritePin(DW1000_CS_PORT, DW1000_CS_PIN, GPIO_PIN_RESET); 62 | 63 | /* Send Header */ 64 | status = HAL_SPI_Transmit(&DW1000_SPI_Handle, (uint8_t *)headerBuffer, headerLength, 0xFFFF); 65 | if (status != HAL_OK) { 66 | return -1; 67 | } 68 | 69 | /* Send Body */ 70 | status = HAL_SPI_Transmit(&DW1000_SPI_Handle, (uint8_t *)bodyBuffer, bodylength, 0xFFFF); 71 | if (status != HAL_OK) { 72 | return -1; 73 | } 74 | 75 | /* Disable DW1000_CS */ 76 | HAL_GPIO_WritePin(DW1000_CS_PORT, DW1000_CS_PIN, GPIO_PIN_SET); 77 | 78 | decamutexoff(stat) ; 79 | 80 | return 0; 81 | } // end writetospi() 82 | 83 | 84 | /*! ------------------------------------------------------------------------------------------------------------------ 85 | * Function: readfromspi() 86 | * 87 | * Low level abstract function to read from the SPI 88 | * Takes two separate byte buffers for write header and read data 89 | * returns the offset into read buffer where first byte of read data may be found, 90 | * or returns -1 if there was an error 91 | */ 92 | int readfromspi(uint16 headerLength, const uint8 *headerBuffer, uint32 readlength, uint8 *readBuffer) 93 | { 94 | HAL_StatusTypeDef status; 95 | decaIrqStatus_t stat; 96 | 97 | stat = decamutexon() ; 98 | 99 | /* Enable DW1000_CS */ 100 | HAL_GPIO_WritePin(DW1000_CS_PORT, DW1000_CS_PIN, GPIO_PIN_RESET); 101 | 102 | /* Send Header */ 103 | status = HAL_SPI_Transmit(&DW1000_SPI_Handle, (uint8_t *)headerBuffer, headerLength, 0xFFFF); 104 | if (status != HAL_OK) { 105 | return -1; 106 | } 107 | 108 | /* Receive Body */ 109 | status = HAL_SPI_Receive(&DW1000_SPI_Handle, (uint8_t *)readBuffer, readlength, 0xFFFF); 110 | if (status != HAL_OK) { 111 | return -1; 112 | } 113 | 114 | /* Disable DW1000_CS */ 115 | HAL_GPIO_WritePin(DW1000_CS_PORT, DW1000_CS_PIN, GPIO_PIN_SET); 116 | 117 | decamutexoff(stat) ; 118 | 119 | return 0; 120 | } // end readfromspi() 121 | -------------------------------------------------------------------------------- /DW1000/platform/deca_spi.h: -------------------------------------------------------------------------------- 1 | /*! ---------------------------------------------------------------------------- 2 | * @file deca_spi.h 3 | * @brief SPI access functions 4 | * 5 | * @attention 6 | * 7 | * Copyright 2013 (c) DecaWave Ltd, Dublin, Ireland. 8 | * 9 | * All rights reserved. 10 | * 11 | * @author DecaWave 12 | */ 13 | 14 | #ifndef _DECA_SPI_H_ 15 | #define _DECA_SPI_H_ 16 | 17 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | #include "deca_types.h" 22 | 23 | #define DECA_MAX_SPI_HEADER_LENGTH (3) // max number of bytes in header (for formating & sizing) 24 | 25 | /*! ------------------------------------------------------------------------------------------------------------------ 26 | * Function: openspi() 27 | * 28 | * Low level abstract function to open and initialise access to the SPI device. 29 | * returns 0 for success, or -1 for error 30 | */ 31 | int openspi(void) ; 32 | 33 | /*! ------------------------------------------------------------------------------------------------------------------ 34 | * Function: closespi() 35 | * 36 | * Low level abstract function to close the the SPI device. 37 | * returns 0 for success, or -1 for error 38 | */ 39 | int closespi(void) ; 40 | 41 | #ifdef __cplusplus 42 | } 43 | #endif 44 | 45 | #endif /* _DECA_SPI_H_ */ 46 | -------------------------------------------------------------------------------- /HC-SR04/HC_SR04.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @Copyright (c) 2021,mculover666 All rights reserved 3 | * @filename HC_SR04.c 4 | * @breif Drive HC_SR04 based on stm32 tim peripheral 5 | * @changelog v1.0 mculover666 2021/5/9 6 | * v1.1 mculover666 2021/5/12 add object design 7 | */ 8 | 9 | #include "HC_SR04.h" 10 | #include 11 | 12 | /** 13 | * @brief pend the mutex to protect tim. 14 | * @param none 15 | * @return none 16 | * @note it will be need if you use rtos 17 | */ 18 | static void HC_SRO4_Mutex_Pend() 19 | { 20 | //add your code here 21 | } 22 | 23 | /** 24 | * @brief post the mutex to protect tim. 25 | * @param none 26 | * @return none 27 | * @note it will be need if you use rtos 28 | */ 29 | static void HC_SRO4_Mutex_Post() 30 | { 31 | //add your code here 32 | } 33 | 34 | /** 35 | * @brief hc_sr04_device object initialization. 36 | * @param none 37 | * @return none 38 | */ 39 | void HC_SR04_Init(hc_sr04_device_t *hc_sr04_device) 40 | { 41 | // the gpio and tim is initialized in main 42 | } 43 | 44 | /** 45 | * @brief Send trig signal. 46 | * @param none 47 | * @return none 48 | */ 49 | static void HC_SR04_Start(hc_sr04_device_t *hc_sr04_device) 50 | { 51 | /* output high level */ 52 | HAL_GPIO_WritePin(hc_sr04_device->trig_port, hc_sr04_device->trig_pin, GPIO_PIN_SET); 53 | 54 | /* maintain high level at least 10us */ 55 | CPU_TS_Tmr_Delay_US(10); 56 | 57 | /* resume low level */ 58 | HAL_GPIO_WritePin(hc_sr04_device->trig_port, hc_sr04_device->trig_pin, GPIO_PIN_RESET); 59 | } 60 | 61 | /** 62 | * @brief Measure the high level time of the echo signal. 63 | * @param hc_sr04_device the pointer of the hc_sr04_device_t object 64 | * @return errcode 65 | * @retval 0 success 66 | * @retval -1 fail 67 | */ 68 | int HC_SR04_Measure(hc_sr04_device_t *hc_sr04_device) 69 | { 70 | uint32_t tick_us; 71 | 72 | HC_SRO4_Mutex_Pend(); 73 | 74 | HC_SR04_Start(hc_sr04_device); 75 | 76 | __HAL_TIM_SetCounter(hc_sr04_device->tim, 0); 77 | 78 | /* waitting for start of the high level through echo pin */ 79 | while (HAL_GPIO_ReadPin(hc_sr04_device->echo_port, hc_sr04_device->echo_pin) == GPIO_PIN_RESET); 80 | 81 | /* start the tim and enable the interrupt */ 82 | HAL_TIM_Base_Start(hc_sr04_device->tim); 83 | 84 | /* waitting for end of the high level through echo pin */ 85 | while (HAL_GPIO_ReadPin(hc_sr04_device->echo_port, hc_sr04_device->echo_pin) == GPIO_PIN_SET); 86 | 87 | /* stop the tim */ 88 | HAL_TIM_Base_Stop(hc_sr04_device->tim); 89 | 90 | /* get the time of high level */ 91 | tick_us = __HAL_TIM_GetCounter(hc_sr04_device->tim); 92 | 93 | /* calc distance in unit cm */ 94 | hc_sr04_device->distance = (double)(tick_us/1000000.0) * 340.0 / 2.0 *100.0; 95 | 96 | HC_SRO4_Mutex_Post(); 97 | 98 | return 0; 99 | } 100 | -------------------------------------------------------------------------------- /HC-SR04/HC_SR04.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/HC-SR04/HC_SR04.h -------------------------------------------------------------------------------- /HC-SR04/core_delay/core_delay.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/HC-SR04/core_delay/core_delay.c -------------------------------------------------------------------------------- /HC-SR04/core_delay/core_delay.h: -------------------------------------------------------------------------------- 1 | #ifndef __CORE_DELAY_H 2 | #define __CORE_DELAY_H 3 | 4 | #include "stm32l4xx.h" 5 | 6 | /* ��ȡ�ں�ʱ��Ƶ�� */ 7 | #define GET_CPU_ClkFreq() HAL_RCC_GetSysClockFreq() 8 | #define SysClockFreq (80000000) 9 | 10 | /* Ϊ����ʹ�ã�����ʱ�����ڲ�����CPU_TS_TmrInit������ʼ��ʱ����Ĵ����� 11 | ����ÿ�ε��ú��������ʼ��һ�顣 12 | �ѱ���ֵ����Ϊ0��Ȼ����main����������ʱ����CPU_TS_TmrInit�ɱ���ÿ�ζ���ʼ�� */ 13 | #define CPU_TS_INIT_IN_DELAY_FUNCTION 1 14 | 15 | /******************************************************************************* 16 | * �������� 17 | ******************************************************************************/ 18 | uint32_t CPU_TS_TmrRd(void); 19 | HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority); 20 | 21 | //ʹ�����º���ǰ�����ȵ���CPU_TS_TmrInit����ʹ�ܼ���������ʹ�ܺ�CPU_TS_INIT_IN_DELAY_FUNCTION 22 | //�����ʱֵΪ8�� 23 | void CPU_TS_Tmr_Delay_US(uint32_t us); 24 | #define HAL_Delay(ms) CPU_TS_Tmr_Delay_US(ms*1000) 25 | #define CPU_TS_Tmr_Delay_S(s) CPU_TS_Tmr_Delay_MS(s*1000) 26 | 27 | 28 | #endif /* __CORE_DELAY_H */ 29 | 30 | -------------------------------------------------------------------------------- /HC-SR04/readme.md: -------------------------------------------------------------------------------- 1 | # 超声波模块驱动 2 | 3 | 本驱动支持同时多个超时波模块,底层需要至少一个us级硬件定时器支持 4 | 5 | # 示例 6 | 7 | 创建设备对象: 8 | ```c 9 | hc_sr04_device_t hc_sr04_device1; 10 | ``` 11 | 12 | 初始化设备对象: 13 | ```c 14 | hc_sr04_device1.trig_port = GPIOB; 15 | hc_sr04_device1.trig_pin = GPIO_PIN_8; 16 | hc_sr04_device1.echo_port = GPIOB; 17 | hc_sr04_device1.echo_pin = GPIO_PIN_9; 18 | hc_sr04_device1.tim = &htim2; 19 | HC_SR04_Init(&hc_sr04_device1); 20 | ``` 21 | 22 | 获取该设备的数据: 23 | ```c 24 | ret = HC_SR04_Measure(&hc_sr04_device1); 25 | if (ret < 0) { 26 | printf("measure fail\r\n"); 27 | } 28 | printf("distance:%.2f cm\r\n", hc_sr04_device1.distance); 29 | ``` -------------------------------------------------------------------------------- /HTS221/HTS221.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/HTS221/HTS221.c -------------------------------------------------------------------------------- /HTS221/HTS221.h: -------------------------------------------------------------------------------- 1 | #ifndef _HTS221_H_ 2 | #define _HTS221_H_ 3 | 4 | #include 5 | 6 | #define HTS221_ADDR_WR 0xBE 7 | #define HTS221_ADDR_RD 0xBF 8 | 9 | #define HTS221_CTRL_REG1 0x20 10 | #define HTS221_CTRL_REG2 0x21 11 | #define HTS221_CTRL_REG3 0x22 12 | 13 | #define HTS221_STATUS_REG 0x27 14 | 15 | #define HTS221_HUMIDITY_OUT_L 0x28 16 | #define HTS221_HUMIDITY_OUT_H 0x29 17 | #define HTS221_TEMP_OUT_L 0x2A 18 | #define HTS221_TEMP_OUT_H 0x2B 19 | 20 | void HTS221_Init(void); 21 | 22 | uint8_t HTS221_Get_Temperature(int16_t* temperature); 23 | uint8_t HTS221_Get_Humidity(int16_t* humidity); 24 | 25 | #endif /* _HTS221_H_ */ 26 | -------------------------------------------------------------------------------- /KEY/key.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/KEY/key.c -------------------------------------------------------------------------------- /KEY/key.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @Copyright (c) 2019,mculover666 All rights reserved 3 | * @filename key.h 4 | * @breif independent KEY driver file 5 | * @version 6 | * v1.0 mculover666 2019/7/15 7 | */ 8 | #ifndef _KEY_H_ 9 | #define _KEY_H_ 10 | 11 | #include "stm32l4xx_hal.h" 12 | 13 | #define KEY1_CLK() __HAL_RCC_GPIOB_CLK_ENABLE(); 14 | #define KEY1_Pin GPIO_PIN_2 15 | #define KEY1_GPIO_Port GPIOB 16 | #define KEY2_CLK() __HAL_RCC_GPIOB_CLK_ENABLE(); 17 | #define KEY2_Pin GPIO_PIN_3 18 | #define KEY2_GPIO_Port GPIOB 19 | 20 | 21 | void KEY_GPIO_Init(void); 22 | 23 | #endif /* _KEY_H_ */ 24 | -------------------------------------------------------------------------------- /LCD-FMC/lcd_fmc.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/LCD-FMC/lcd_fmc.c -------------------------------------------------------------------------------- /LCD-FMC/lcd_fmc.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/LCD-FMC/lcd_fmc.h -------------------------------------------------------------------------------- /LCD-FSMC/lcd_fsmc.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/LCD-FSMC/lcd_fsmc.c -------------------------------------------------------------------------------- /LCD-FSMC/lcd_fsmc.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/LCD-FSMC/lcd_fsmc.h -------------------------------------------------------------------------------- /LCD-RGB-LTDC/font/font_ascii.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/LCD-RGB-LTDC/font/font_ascii.h -------------------------------------------------------------------------------- /LCD-RGB-LTDC/font/font_hz.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/LCD-RGB-LTDC/font/font_hz.h -------------------------------------------------------------------------------- /LCD-RGB-LTDC/lcd_rgb_ltdc_drv.h: -------------------------------------------------------------------------------- 1 | #ifndef _LCD_RGB_LTDC_DRV_H_ 2 | #define _LCD_RGB_LTDC_DRV_H_ 3 | 4 | #include "ltdc.h" 5 | 6 | /** 7 | * @brief Windows size on lcd. 8 | */ 9 | #define LCD_WIDTH 1024 10 | #define LCD_HEIGHT 600 11 | 12 | /** 13 | * @brief Backlight control pin of lcd. 14 | */ 15 | #define LCD_BL_GPIO_PORT GPIOB 16 | #define LCD_BL_GPIO_PIN GPIO_PIN_5 17 | 18 | /** 19 | * @brief start address of lcd framebuffer. 20 | */ 21 | #define LCD_FRAME_BUFFER 0xc0000000 22 | 23 | /** 24 | * @brief whether use dma2d to transfer data to lcd framebuffer. 25 | */ 26 | #define USE_DMA2D_EN 0 27 | 28 | /** 29 | * @brief whether enable ASCII char display. 30 | */ 31 | #define USE_ASCII_EN 0 32 | 33 | /** 34 | * @brief whether enable chinese char display. 35 | */ 36 | #define USE_CHINESE_EN 0 37 | 38 | /** 39 | * @brief whether enable chinese full library(GB2312). 40 | */ 41 | #define USE_CHINESE_FULL_LIB 0 42 | #define CHINESE_FULL_LIB_16_EN 1 43 | #define CHINESE_FULL_LIB_24_EN 0 44 | #define CHINESE_FULL_LIB_32_EN 0 45 | /** 46 | * @brief color 47 | * @note rgb565 48 | */ 49 | #define BLACK 0x0000 50 | #define BLUE 0x001F 51 | #define GREEN 0x07E0 52 | #define GBLUE 0X07FF 53 | #define GRAY 0X8430 54 | #define BROWN 0XBC40 55 | #define RED 0xF800 56 | #define PINK 0XF81F 57 | #define BRRED 0XFC07 58 | #define YELLOW 0xFFE0 59 | #define WHITE 0xFFFF 60 | 61 | /** 62 | * @brief Control the lcd backlight. 63 | * @param[in] brightness the value of lcd backlight. 64 | * @return None 65 | */ 66 | void lcd_backlight_control(uint8_t bightness); 67 | 68 | /** 69 | * @brief LCD initialization. 70 | * @param None 71 | * @return None 72 | */ 73 | void lcd_init(void); 74 | 75 | /** 76 | * @brief Clear lcd. 77 | * @param[in] color rgb565 78 | * @return None 79 | */ 80 | void lcd_clear(uint16_t color); 81 | 82 | /** 83 | * @brief Draw point. 84 | * @param[in] x horizontal position. 85 | * @param[in] y vertical position. 86 | * @param[in] color rgb565 87 | * @return None 88 | */ 89 | void lcd_draw_point(uint16_t x, uint16_t y, uint16_t color); 90 | 91 | /** 92 | * @brief Read point. 93 | * @param[in] x horizontal position. 94 | * @param[in] y vertical position. 95 | * @return color rgb565 96 | */ 97 | uint16_t lcd_read_point(uint16_t x, uint16_t y); 98 | 99 | /** 100 | * @brief Draw line. 101 | * @param[in] x1 horizontal start position. 102 | * @param[in] y1 vertical start position. 103 | * @param[in] x2 horizontal end position. 104 | * @param[in] y2 vertical end position. 105 | * @param[in] color rgb565 106 | * @return None 107 | * @note Bresenham algorithm. 108 | */ 109 | void lcd_draw_line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color); 110 | 111 | /** 112 | * @brief Draw rect. 113 | * @param[in] x1 horizontal start position. 114 | * @param[in] y1 vertical start position. 115 | * @param[in] x2 horizontal end position. 116 | * @param[in] y2 vertical end position. 117 | * @param[in] color rgb565 118 | * @return None 119 | */ 120 | void lcd_draw_rect(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color); 121 | 122 | /** 123 | * @brief Fill rect. 124 | * @param[in] x1 horizontal start position. 125 | * @param[in] y1 vertical start position. 126 | * @param[in] x2 horizontal end position. 127 | * @param[in] y2 vertical end position. 128 | * @param[in] color rgb565 129 | * @return None 130 | */ 131 | void lcd_fill(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color); 132 | 133 | 134 | /** 135 | * @brief Fill rect. 136 | * @param[in] x1 horizontal start position. 137 | * @param[in] y1 vertical start position. 138 | * @param[in] x2 horizontal end position. 139 | * @param[in] y2 vertical end position. 140 | * @param[in] color pointer to color buffer. 141 | * @return None 142 | */ 143 | void lcd_fill_with_buffer(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t *color); 144 | 145 | #if USE_ASCII_EN 146 | /** 147 | * @brief Show a ASCII char. 148 | * @param[in] x1 horizontal start position. 149 | * @param[in] y1 vertical start position. 150 | * @param[in] x2 horizontal end position. 151 | * @param[in] y2 vertical end position. 152 | * @param[in] ch ascii char. 153 | * @param[in] back_color rgb565 154 | * @param[in] font_color rgb565 155 | * @param[in] font_size support 12/16/24/32. 156 | * @return None 157 | * @note This function need ascii library(font.h). 158 | */ 159 | void lcd_show_char(uint16_t x, uint16_t y, char ch, uint16_t back_color, uint16_t font_color, uint8_t font_size); 160 | 161 | /** 162 | * @brief Show a ASCII string. 163 | * @param[in] x1 horizontal start position. 164 | * @param[in] y1 vertical start position. 165 | * @param[in] x2 horizontal end position. 166 | * @param[in] y2 vertical end position. 167 | * @param[in] str ascii string. 168 | * @param[in] back_color rgb565 169 | * @param[in] font_color rgb565 170 | * @param[in] font_size support 12/16/24/32. 171 | * @return None 172 | * @note This function need ascii library(font.h). 173 | */ 174 | void lcd_show_str(uint16_t x, uint16_t y, char *str, uint16_t back_color, uint16_t font_color, uint8_t font_size); 175 | 176 | #endif /* USE_ASCII_EN */ 177 | 178 | #if USE_CHINESE_EN 179 | /** 180 | * @brief Show a chinese char. 181 | * @param[in] x1 horizontal start position. 182 | * @param[in] y1 vertical start position. 183 | * @param[in] x2 horizontal end position. 184 | * @param[in] y2 vertical end position. 185 | * @param[in] ch offset in hz library. 186 | * @param[in] back_color rgb565 187 | * @param[in] font_color rgb565 188 | * @param[in] font_size support 16/24/32. 189 | * @return None 190 | * @note This function need hz library. 191 | */ 192 | void lcd_show_chinese(uint16_t x, uint16_t y, uint32_t offset, uint16_t back_color, uint16_t font_color, uint8_t font_size); 193 | 194 | #endif /* USE_CHINESE_EN */ 195 | 196 | #endif /* _LCD_RGB_LTDC_DRV_H_ */ 197 | -------------------------------------------------------------------------------- /LCD-SPI/font.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/LCD-SPI/font.h -------------------------------------------------------------------------------- /LCD-SPI/readme.md: -------------------------------------------------------------------------------- 1 | # 示例 2 | ## 基本使用 3 | 包含头文件: 4 | ```c 5 | #include "lcd_spi_drv.h" 6 | ``` 7 | 初始化屏幕: 8 | ```c 9 | lcd_init(); 10 | ``` 11 | 清屏: 12 | ```c 13 | lcd_clear(WHITE); 14 | ``` 15 | 16 | ## 画线示例 17 | ```c 18 | lcd_draw_color_line(0,120,240,120,RED); //画水平线 19 | lcd_draw_color_line(0,0,240,240,BLUE); //画斜线(从左到右,45°) 20 | lcd_draw_color_line(0,240,240,0,GREEN); //画斜线(从右到左,45°) 21 | lcd_draw_color_line(120,0,120,240,YELLOW); //画垂直线 22 | lcd_draw_color_line(180,0,60,240,RED); //画斜线(从左到右,120°) 23 | lcd_draw_color_line(60,0,180,240,RED); //画斜线(从右到左,60°) 24 | lcd_draw_color_line(0,60,240,180,RED); //画斜线(从左到右,180°) 25 | lcd_draw_color_line(0,180,240,60,RED); //画斜线(从左到右,30°) 26 | LCD_Draw_ColorRect(60,60,180,180,PINK); //画矩形 27 | LCD_Draw_ColorCircle(120,120,85, GBLUE); //画圆 28 | ``` 29 | 30 | ## 字符示例 31 | 需要开启字符显示: 32 | ```c 33 | #define USE_ASCII_FONT_LIB 1 // Whether to enable character display 34 | ``` 35 | 测试代码: 36 | ```c 37 | //以下9行测试12/16/24/32四种字符和字符串显示 38 | lcd_show_char(6,12,'B',BLACK,YELLOW,16); 39 | lcd_show_char(14,28,'C',BLACK,GREEN,24); 40 | lcd_show_char(0,0,'A',BLACK,BLUE,12); 41 | lcd_show_char(26,52,'D',BLACK,PINK,32); 42 | lcd_show_str(60,240-32-24-24-24,"Powerd BY",BLACK,GREEN,24); 43 | lcd_show_str(36,240-32-24-24,"TencentOS-tiny",BLACK,YELLOW,24); 44 | lcd_show_str(28,240-32-24,"Mculover666",BLACK,BLUE,32); 45 | lcd_show_str(12,240-24,"Copyright (c) 2019",BLACK,PINK,24);= 46 | ``` 47 | ## 六芒星示例 48 | ```c 49 | lcd_draw_color_sixpointstar(150,65,40,RED); 50 | ``` 51 | 52 | ## 图片示例 53 | 需要开启字符显示: 54 | ```c 55 | #define USE_ASCII_FONT_LIB 1 // Whether to enable character display 56 | ``` 57 | 包含图片头文件: 58 | ```c 59 | #include "bear.h" 60 | ``` 61 | 图片显示测试: 62 | ```c 63 | //以下2行测试图片显示,需要bear.h头文件的支持 64 | lcd_show_image(0,0,240,240,gImage_bear); 65 | lcd_show_str(70,240-24,"Starting...",WHITE,BLUE,24); 66 | ``` 67 | 68 | ## 刷屏示例 69 | ```c 70 | start_time = HAL_GetTick(); 71 | lcd_color_set(GREEN, WHITE); 72 | lcd_clear(); 73 | end_time = HAL_GetTick(); 74 | printf("clear time is %d ms\r\n", end_time - start_time); 75 | ``` -------------------------------------------------------------------------------- /LED/led_drv.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @Copyright (c) 2019,mculover666 All rights reserved 3 | * @filename led.c 4 | * @breif independent LED driver file 5 | * @version 6 | * 1.0 mculover666 2019/7/10 7 | */ 8 | 9 | #include "led_drv.h" 10 | 11 | /** 12 | * @brief LED GPIO Initialization Function 13 | * @param None 14 | * @retval None 15 | */ 16 | void LED_GPIO_init(void) 17 | { 18 | 19 | GPIO_InitTypeDef GPIO_InitStruct = {0}; 20 | 21 | /* GPIO Ports Clock Enable */ 22 | LED_CLK(); 23 | 24 | /*Configure GPIO pin*/ 25 | GPIO_InitStruct.Pin = LED_PIN; 26 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 27 | GPIO_InitStruct.Pull = GPIO_NOPULL; 28 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 29 | HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct); 30 | 31 | /*Configure GPIO pin Output Level */ 32 | HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_RESET); 33 | } 34 | -------------------------------------------------------------------------------- /LED/led_drv.h: -------------------------------------------------------------------------------- 1 | #ifndef _LED_DRV_H_ 2 | #define _LED_DRV_H_ 3 | /** 4 | * @Copyright (c) 2019,mculover666 5 | All rights reserved 6 | * @filename led.h 7 | * @breif independent LED driver file 8 | * @version 9 | * 1.0 mculover666 2019/7/10 10 | */ 11 | 12 | #include "stm32l4xx_hal.h" 13 | 14 | #define LED_CLK() __HAL_RCC_GPIOC_CLK_ENABLE(); 15 | #define LED_PORT GPIOC 16 | #define LED_PIN GPIO_PIN_13 17 | 18 | #define LED(n) (n?HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_SET):HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_RESET)); 19 | 20 | void LED_GPIO_init(void); 21 | 22 | #endif /* _LED_DRV_H_ */ 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2022, Mculover666 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | 3. Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /OLED/bmp.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/OLED/bmp.h -------------------------------------------------------------------------------- /OLED/oled.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/OLED/oled.c -------------------------------------------------------------------------------- /OLED/oled.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/OLED/oled.h -------------------------------------------------------------------------------- /OLED/oledfont.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/OLED/oledfont.h -------------------------------------------------------------------------------- /SDRAM/doc/W9825G6KH.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/SDRAM/doc/W9825G6KH.pdf -------------------------------------------------------------------------------- /SDRAM/readme.md: -------------------------------------------------------------------------------- 1 | 详细的SDRAM驱动文件请阅读: 2 | 3 | - [STM32CubeMX | 31-使用硬件FMC读写SDRAM(W9825G6KH)](https://mculover666.blog.csdn.net/article/details/108221735) -------------------------------------------------------------------------------- /SDRAM/sdram_fmc_drv.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/SDRAM/sdram_fmc_drv.c -------------------------------------------------------------------------------- /SDRAM/sdram_fmc_drv.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/SDRAM/sdram_fmc_drv.h -------------------------------------------------------------------------------- /SGP30/doc/Sensirion_Gas_Sensors_SGP30_Datasheet_EN.PDF.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/SGP30/doc/Sensirion_Gas_Sensors_SGP30_Datasheet_EN.PDF.pdf -------------------------------------------------------------------------------- /SGP30/doc/sgp30.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/SGP30/doc/sgp30.jpg -------------------------------------------------------------------------------- /SGP30/readme.md: -------------------------------------------------------------------------------- 1 | 驱动编写和使用文章: 2 | 3 | - [STM32CubeMX | 29-使用硬件I2C读取甲醛传感器SGP30](https://editor.csdn.net/md?articleId=107536250) 4 | 5 | sgp30传感器长下面这样: 6 | 7 | ![](./doc/sgp30.jpg) 8 | 9 | sgp30英文数据手册(我阅读时加入了一点注释)在doc目录下。 -------------------------------------------------------------------------------- /SGP30/sgp30.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/SGP30/sgp30.c -------------------------------------------------------------------------------- /SGP30/sgp30.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/SGP30/sgp30.h -------------------------------------------------------------------------------- /W25Q64-QSPI/bsp_w25q64.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/W25Q64-QSPI/bsp_w25q64.c -------------------------------------------------------------------------------- /W25Q64-QSPI/bsp_w25q64.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/W25Q64-QSPI/bsp_w25q64.h -------------------------------------------------------------------------------- /W25QXX-SPI/doc/DS_W25Q64.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/W25QXX-SPI/doc/DS_W25Q64.pdf -------------------------------------------------------------------------------- /W25QXX-SPI/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/W25QXX-SPI/readme.md -------------------------------------------------------------------------------- /W25QXX-SPI/w25q64.c: -------------------------------------------------------------------------------- 1 | #include "w25q64.h" 2 | 3 | #if defined(USE_ST_HAL) 4 | static void spi_init(void) 5 | { 6 | // it will be called in main. 7 | // MX_SPIx_Init(); 8 | } 9 | 10 | static void spi_cs_select(void) 11 | { 12 | HAL_GPIO_WritePin(SPI_CS_PORT, SPI_CS_PIN, GPIO_PIN_RESET); 13 | 14 | /* tCHSH: /CS Active Hold Time relative to CLK, min is 5 ns. */ 15 | for (int i = 0; i < 50; i++) { 16 | __NOP(); 17 | } 18 | } 19 | 20 | static void spi_cs_deselect(void) 21 | { 22 | HAL_GPIO_WritePin(SPI_CS_PORT, SPI_CS_PIN, GPIO_PIN_SET); 23 | 24 | /* tSHSL: /CS Deselect Time (for Array Read -> Array Read -> 25 | -> Erase or Program -> Read Status Registers), min is 10 ns. */ 26 | for (int i = 0; i < 100; i++) { 27 | __NOP(); 28 | } 29 | } 30 | 31 | static int spi_transmit(uint8_t *buf, uint16_t size) 32 | { 33 | HAL_StatusTypeDef status; 34 | 35 | status = HAL_SPI_Transmit(&SPI_Handle, buf, size, 100); 36 | 37 | return status == HAL_OK ? 0 : -1; 38 | } 39 | 40 | static int spi_receive(uint8_t *buf, uint16_t size) 41 | { 42 | HAL_StatusTypeDef status; 43 | 44 | status = HAL_SPI_Receive(&SPI_Handle, buf, size, 100); 45 | 46 | return status == HAL_OK ? 0 : -1; 47 | } 48 | 49 | #elif defined(USE_NXP_FSL) 50 | static void spi_init(void) 51 | { 52 | lpspi_master_config_t masterConfig; 53 | uint32_t srcClock_Hz; 54 | 55 | /* Set clock source for LPSPI */ 56 | CLOCK_SetMux(kCLOCK_LpspiMux, LPSPI_CLOCK_SOURCE_SELECT); 57 | CLOCK_SetDiv(kCLOCK_LpspiDiv, LPSPI_CLOCK_SOURCE_DIVIDER); 58 | 59 | /* Master config */ 60 | LPSPI_MasterGetDefaultConfig(&masterConfig); 61 | masterConfig.baudRate = TRANSFER_BAUDRATE; 62 | 63 | srcClock_Hz = LPSPI_MASTER_CLK_FREQ; 64 | LPSPI_MasterInit(SPI_Handle, &masterConfig, srcClock_Hz); 65 | } 66 | 67 | static void spi_cs_select(void) 68 | { 69 | GPIO_PinWrite(SPI_CS_PORT, SPI_CS_PIN, 0); 70 | 71 | /* tCHSH: /CS Active Hold Time relative to CLK, min is 5 ns. */ 72 | for (int i = 0; i < 500; i++) { 73 | __NOP(); 74 | } 75 | } 76 | 77 | static void spi_cs_deselect(void) 78 | { 79 | GPIO_PinWrite(SPI_CS_PORT, SPI_CS_PIN, 1); 80 | 81 | /* tSHSL: /CS Deselect Time (for Array Read -> Array Read -> 82 | -> Erase or Program -> Read Status Registers), min is 10 ns. */ 83 | for (int i = 0; i < 1000; i++) { 84 | __NOP(); 85 | } 86 | } 87 | 88 | static int spi_transmit(uint8_t *buf, uint16_t size) 89 | { 90 | status_t status; 91 | lpspi_transfer_t masterXfer; 92 | 93 | /* Start master transfer, transfer data to slave. */ 94 | masterXfer.txData = buf; 95 | masterXfer.rxData = NULL; 96 | masterXfer.dataSize = size; 97 | masterXfer.configFlags = kLPSPI_MasterByteSwap; 98 | 99 | status = LPSPI_MasterTransferBlocking(SPI_Handle, &masterXfer); 100 | 101 | return status == kStatus_Success ? 0 : -1; 102 | } 103 | 104 | static int spi_receive(uint8_t *buf, uint16_t size) 105 | { 106 | status_t status; 107 | lpspi_transfer_t masterXfer; 108 | 109 | /* Start master transfer, receive data from slave */ 110 | masterXfer.txData = NULL; 111 | masterXfer.rxData = buf; 112 | masterXfer.dataSize = size; 113 | masterXfer.configFlags = kLPSPI_MasterByteSwap; 114 | 115 | status = LPSPI_MasterTransferBlocking(SPI_Handle, &masterXfer); 116 | 117 | return status == kStatus_Success ? 0 : -1; 118 | } 119 | #endif /* USE_ST_HAL or USE_NXP_FSL */ 120 | 121 | int w25qxx_init(void) 122 | { 123 | spi_init(); 124 | 125 | return 0; 126 | } 127 | 128 | uint16_t w25qxx_read_deviceid(void) 129 | { 130 | uint8_t recv_buf[2] = {0}; 131 | uint16_t device_id = 0; 132 | uint8_t send_data[4] = {ManufactDeviceID_CMD, 0x00, 0x00, 0x00}; 133 | 134 | spi_cs_select(); 135 | if (spi_transmit(send_data, 4) == 0) { 136 | if (spi_receive(recv_buf, 2) == 0) { 137 | device_id = (recv_buf[0] << 8) | recv_buf[1]; 138 | } 139 | } 140 | spi_cs_deselect(); 141 | 142 | return device_id; 143 | } 144 | 145 | static void w25qxx_wait_busy(void) 146 | { 147 | uint8_t cmd; 148 | uint8_t result; 149 | 150 | cmd = READ_STATU_REGISTER_1; 151 | 152 | spi_cs_select(); 153 | spi_transmit(&cmd, 1); 154 | while (1) { 155 | spi_receive(&result, 1); 156 | if ((result & 0x01) != 0x01) { 157 | break; 158 | } 159 | } 160 | spi_cs_deselect(); 161 | 162 | return; 163 | } 164 | 165 | int w25qxx_read(uint8_t* buffer, uint32_t start_addr, uint16_t nbytes) 166 | { 167 | uint8_t cmd[4]; 168 | 169 | cmd[0] = READ_DATA_CMD; 170 | cmd[1] = (uint8_t)(start_addr >> 16); 171 | cmd[2] = (uint8_t)(start_addr >> 8); 172 | cmd[3] = (uint8_t)(start_addr); 173 | 174 | spi_cs_select(); 175 | if (spi_transmit(cmd, 4) == 0) { 176 | if (spi_receive(buffer, nbytes) == 0) { 177 | spi_cs_deselect(); 178 | return 0; 179 | } 180 | } 181 | spi_cs_deselect(); 182 | 183 | return -1; 184 | } 185 | 186 | void w25qxx_write_enable(void) 187 | { 188 | uint8_t cmd = WRITE_ENABLE_CMD; 189 | 190 | spi_cs_select(); 191 | spi_transmit(&cmd, 1); 192 | spi_cs_deselect(); 193 | } 194 | 195 | void w25qxx_write_disable(void) 196 | { 197 | uint8_t cmd = WRITE_DISABLE_CMD; 198 | 199 | spi_cs_select(); 200 | spi_transmit(&cmd, 1); 201 | spi_cs_deselect(); 202 | } 203 | 204 | int w25qxx_erase_sector(uint32_t sector_addr) 205 | { 206 | uint8_t cmd[4]; 207 | 208 | cmd[0] = SECTOR_ERASE_CMD; 209 | cmd[1] = (uint8_t)(sector_addr>>16); 210 | cmd[2] = (uint8_t)(sector_addr>>8); 211 | cmd[3] = (uint8_t)(sector_addr); 212 | 213 | w25qxx_wait_busy(); 214 | 215 | w25qxx_write_enable(); 216 | 217 | spi_cs_select(); 218 | if (spi_transmit(cmd, 4) != 0) { 219 | spi_cs_deselect(); 220 | return -1; 221 | } 222 | spi_cs_deselect(); 223 | 224 | w25qxx_wait_busy(); 225 | 226 | return 0; 227 | } 228 | 229 | int w25qxx_page_program(uint8_t* dat, uint32_t write_addr, uint16_t nbytes) 230 | { 231 | uint8_t cmd[4]; 232 | 233 | cmd[0] = PAGE_PROGRAM_CMD; 234 | cmd[1] = (uint8_t)(write_addr >> 16); 235 | cmd[2] = (uint8_t)(write_addr >> 8); 236 | cmd[3] = (uint8_t)(write_addr); 237 | 238 | w25qxx_wait_busy(); 239 | 240 | w25qxx_write_enable(); 241 | 242 | spi_cs_select(); 243 | if (spi_transmit(cmd, 4) != 0) { 244 | spi_cs_deselect(); 245 | return -1; 246 | } 247 | if (spi_transmit(dat, nbytes) != 0) { 248 | spi_cs_deselect(); 249 | return -1; 250 | } 251 | 252 | spi_cs_deselect(); 253 | 254 | w25qxx_wait_busy(); 255 | 256 | return 0; 257 | } 258 | -------------------------------------------------------------------------------- /W25QXX-SPI/w25q64.h: -------------------------------------------------------------------------------- 1 | #ifndef _W25Q64_H_ 2 | #define _W25Q64_H_ 3 | 4 | //#define USE_ST_HAL 5 | #define USE_NXP_FSL 6 | 7 | #if defined(USE_ST_HAL) 8 | #include "spi.h" 9 | 10 | #define SPI_Handle hspi1 11 | #define SPI_CS_PORT GPIOA 12 | #define SPI_CS_PIN GPIO_PIN_4 13 | 14 | #elif defined(USE_NXP_FSL) 15 | #include "fsl_gpio.h" 16 | #include "fsl_lpspi.h" 17 | 18 | #define SPI_Handle LPSPI3 19 | #define SPI_CS_PORT GPIO1 20 | #define SPI_CS_PIN 3 21 | /* Select USB1 PLL PFD0 (720 MHz) as lpspi clock source */ 22 | #define LPSPI_CLOCK_SOURCE_SELECT (1U) 23 | /* Clock divider for master lpspi clock source */ 24 | #define LPSPI_CLOCK_SOURCE_DIVIDER (7U) 25 | /* 90MHz */ 26 | #define LPSPI_MASTER_CLK_FREQ (CLOCK_GetFreq(kCLOCK_Usb1PllPfd0Clk) / (LPSPI_CLOCK_SOURCE_DIVIDER + 1U)) 27 | /*! Transfer baudrate - 80M */ 28 | #define TRANSFER_BAUDRATE 80000000U 29 | #endif /* USE_ST_HAL or USE_NXP_FSL */ 30 | 31 | enum { 32 | ManufactDeviceID_CMD = 0x90, 33 | READ_STATU_REGISTER_1 = 0x05, 34 | READ_STATU_REGISTER_2 = 0x35, 35 | READ_DATA_CMD = 0x03, 36 | WRITE_ENABLE_CMD = 0x06, 37 | WRITE_DISABLE_CMD = 0x04, 38 | SECTOR_ERASE_CMD = 0x20, 39 | CHIP_ERASE_CMD = 0xc7, 40 | PAGE_PROGRAM_CMD = 0x02, 41 | }; 42 | 43 | int w25qxx_init(void); 44 | uint16_t w25qxx_read_deviceid(void); 45 | int w25qxx_read(uint8_t* buffer, uint32_t start_addr, uint16_t nbytes); 46 | int w25qxx_erase_sector(uint32_t sector_addr); 47 | int w25qxx_page_program(uint8_t* dat, uint32_t write_addr, uint16_t nbytes); 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /W5500/Application/loopback/loopback.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "loopback.h" 3 | #include "socket.h" 4 | #include "wizchip_conf.h" 5 | 6 | #if LOOPBACK_MODE == LOOPBACK_MAIN_NOBLCOK 7 | 8 | int32_t loopback_tcps(uint8_t sn, uint8_t* buf, uint16_t port) 9 | { 10 | int32_t ret; 11 | uint16_t size = 0, sentsize=0; 12 | 13 | #ifdef _LOOPBACK_DEBUG_ 14 | uint8_t destip[4]; 15 | uint16_t destport; 16 | #endif 17 | 18 | switch(getSn_SR(sn)) 19 | { 20 | case SOCK_ESTABLISHED : 21 | if(getSn_IR(sn) & Sn_IR_CON) 22 | { 23 | #ifdef _LOOPBACK_DEBUG_ 24 | getSn_DIPR(sn, destip); 25 | destport = getSn_DPORT(sn); 26 | 27 | printf("%d:Connected - %d.%d.%d.%d : %d\r\n",sn, destip[0], destip[1], destip[2], destip[3], destport); 28 | #endif 29 | setSn_IR(sn,Sn_IR_CON); 30 | } 31 | if((size = getSn_RX_RSR(sn)) > 0) // Don't need to check SOCKERR_BUSY because it doesn't not occur. 32 | { 33 | if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE; 34 | ret = recv(sn, buf, size); 35 | 36 | if(ret <= 0) return ret; // check SOCKERR_BUSY & SOCKERR_XXX. For showing the occurrence of SOCKERR_BUSY. 37 | size = (uint16_t) ret; 38 | sentsize = 0; 39 | 40 | while(size != sentsize) 41 | { 42 | ret = send(sn, buf+sentsize, size-sentsize); 43 | if(ret < 0) 44 | { 45 | close(sn); 46 | return ret; 47 | } 48 | sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero. 49 | } 50 | } 51 | break; 52 | case SOCK_CLOSE_WAIT : 53 | #ifdef _LOOPBACK_DEBUG_ 54 | //printf("%d:CloseWait\r\n",sn); 55 | #endif 56 | if((ret = disconnect(sn)) != SOCK_OK) return ret; 57 | #ifdef _LOOPBACK_DEBUG_ 58 | printf("%d:Socket Closed\r\n", sn); 59 | #endif 60 | break; 61 | case SOCK_INIT : 62 | #ifdef _LOOPBACK_DEBUG_ 63 | printf("%d:Listen, TCP server loopback, port [%d]\r\n", sn, port); 64 | #endif 65 | if( (ret = listen(sn)) != SOCK_OK) return ret; 66 | break; 67 | case SOCK_CLOSED: 68 | #ifdef _LOOPBACK_DEBUG_ 69 | //printf("%d:TCP server loopback start\r\n",sn); 70 | #endif 71 | if((ret = socket(sn, Sn_MR_TCP, port, 0x00)) != sn) return ret; 72 | #ifdef _LOOPBACK_DEBUG_ 73 | //printf("%d:Socket opened\r\n",sn); 74 | #endif 75 | break; 76 | default: 77 | break; 78 | } 79 | return 1; 80 | } 81 | 82 | 83 | int32_t loopback_tcpc(uint8_t sn, uint8_t* buf, uint8_t* destip, uint16_t destport) 84 | { 85 | int32_t ret; // return value for SOCK_ERRORs 86 | uint16_t size = 0, sentsize=0; 87 | 88 | // Destination (TCP Server) IP info (will be connected) 89 | // >> loopback_tcpc() function parameter 90 | // >> Ex) 91 | // uint8_t destip[4] = {192, 168, 0, 214}; 92 | // uint16_t destport = 5000; 93 | 94 | // Port number for TCP client (will be increased) 95 | static uint16_t any_port = 50000; 96 | 97 | // Socket Status Transitions 98 | // Check the W5500 Socket n status register (Sn_SR, The 'Sn_SR' controlled by Sn_CR command or Packet send/recv status) 99 | switch(getSn_SR(sn)) 100 | { 101 | case SOCK_ESTABLISHED : 102 | if(getSn_IR(sn) & Sn_IR_CON) // Socket n interrupt register mask; TCP CON interrupt = connection with peer is successful 103 | { 104 | #ifdef _LOOPBACK_DEBUG_ 105 | printf("%d:Connected to - %d.%d.%d.%d : %d\r\n",sn, destip[0], destip[1], destip[2], destip[3], destport); 106 | #endif 107 | setSn_IR(sn, Sn_IR_CON); // this interrupt should be write the bit cleared to '1' 108 | } 109 | 110 | ////////////////////////////////////////////////////////////////////////////////////////////// 111 | // Data Transaction Parts; Handle the [data receive and send] process 112 | ////////////////////////////////////////////////////////////////////////////////////////////// 113 | if((size = getSn_RX_RSR(sn)) > 0) // Sn_RX_RSR: Socket n Received Size Register, Receiving data length 114 | { 115 | if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE; // DATA_BUF_SIZE means user defined buffer size (array) 116 | ret = recv(sn, buf, size); // Data Receive process (H/W Rx socket buffer -> User's buffer) 117 | 118 | if(ret <= 0) return ret; // If the received data length <= 0, receive failed and process end 119 | size = (uint16_t) ret; 120 | sentsize = 0; 121 | 122 | // Data sentsize control 123 | while(size != sentsize) 124 | { 125 | ret = send(sn, buf+sentsize, size-sentsize); // Data send process (User's buffer -> Destination through H/W Tx socket buffer) 126 | if(ret < 0) // Send Error occurred (sent data length < 0) 127 | { 128 | close(sn); // socket close 129 | return ret; 130 | } 131 | sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero. 132 | } 133 | } 134 | ////////////////////////////////////////////////////////////////////////////////////////////// 135 | break; 136 | 137 | case SOCK_CLOSE_WAIT : 138 | #ifdef _LOOPBACK_DEBUG_ 139 | //printf("%d:CloseWait\r\n",sn); 140 | #endif 141 | if((ret=disconnect(sn)) != SOCK_OK) return ret; 142 | #ifdef _LOOPBACK_DEBUG_ 143 | printf("%d:Socket Closed\r\n", sn); 144 | #endif 145 | break; 146 | 147 | case SOCK_INIT : 148 | #ifdef _LOOPBACK_DEBUG_ 149 | printf("%d:Try to connect to the %d.%d.%d.%d : %d\r\n", sn, destip[0], destip[1], destip[2], destip[3], destport); 150 | #endif 151 | if( (ret = connect(sn, destip, destport)) != SOCK_OK) return ret; // Try to TCP connect to the TCP server (destination) 152 | break; 153 | 154 | case SOCK_CLOSED: 155 | close(sn); 156 | if((ret=socket(sn, Sn_MR_TCP, any_port++, 0x00)) != sn){ 157 | if(any_port == 0xffff) any_port = 50000; 158 | return ret; // TCP socket open with 'any_port' port number 159 | } 160 | #ifdef _LOOPBACK_DEBUG_ 161 | //printf("%d:TCP client loopback start\r\n",sn); 162 | //printf("%d:Socket opened\r\n",sn); 163 | #endif 164 | break; 165 | default: 166 | break; 167 | } 168 | return 1; 169 | } 170 | 171 | 172 | int32_t loopback_udps(uint8_t sn, uint8_t* buf, uint16_t port) 173 | { 174 | int32_t ret; 175 | uint16_t size, sentsize; 176 | uint8_t destip[4]; 177 | uint16_t destport; 178 | 179 | switch(getSn_SR(sn)) 180 | { 181 | case SOCK_UDP : 182 | if((size = getSn_RX_RSR(sn)) > 0) 183 | { 184 | if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE; 185 | ret = recvfrom(sn, buf, size, destip, (uint16_t*)&destport); 186 | if(ret <= 0) 187 | { 188 | #ifdef _LOOPBACK_DEBUG_ 189 | printf("%d: recvfrom error. %ld\r\n",sn,ret); 190 | #endif 191 | return ret; 192 | } 193 | size = (uint16_t) ret; 194 | sentsize = 0; 195 | while(sentsize != size) 196 | { 197 | ret = sendto(sn, buf+sentsize, size-sentsize, destip, destport); 198 | if(ret < 0) 199 | { 200 | #ifdef _LOOPBACK_DEBUG_ 201 | printf("%d: sendto error. %ld\r\n",sn,ret); 202 | #endif 203 | return ret; 204 | } 205 | sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero. 206 | } 207 | } 208 | break; 209 | case SOCK_CLOSED: 210 | #ifdef _LOOPBACK_DEBUG_ 211 | //printf("%d:UDP loopback start\r\n",sn); 212 | #endif 213 | if((ret = socket(sn, Sn_MR_UDP, port, 0x00)) != sn) 214 | return ret; 215 | #ifdef _LOOPBACK_DEBUG_ 216 | printf("%d:Opened, UDP loopback, port [%d]\r\n", sn, port); 217 | #endif 218 | break; 219 | default : 220 | break; 221 | } 222 | return 1; 223 | } 224 | 225 | #endif 226 | -------------------------------------------------------------------------------- /W5500/Application/loopback/loopback.h: -------------------------------------------------------------------------------- 1 | #ifndef _LOOPBACK_H_ 2 | #define _LOOPBACK_H_ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | 10 | /* Loopback test debug message printout enable */ 11 | #define _LOOPBACK_DEBUG_ 12 | 13 | /* DATA_BUF_SIZE define for Loopback example */ 14 | #ifndef DATA_BUF_SIZE 15 | #define DATA_BUF_SIZE 2048 16 | #endif 17 | 18 | /************************/ 19 | /* Select LOOPBACK_MODE */ 20 | /************************/ 21 | #define LOOPBACK_MAIN_NOBLOCK 0 22 | #define LOOPBACK_MODE LOOPBACK_MAIN_NOBLOCK 23 | 24 | 25 | /* TCP server Loopback test example */ 26 | int32_t loopback_tcps(uint8_t sn, uint8_t* buf, uint16_t port); 27 | 28 | /* TCP client Loopback test example */ 29 | int32_t loopback_tcpc(uint8_t sn, uint8_t* buf, uint8_t* destip, uint16_t destport); 30 | 31 | /* UDP Loopback test example */ 32 | int32_t loopback_udps(uint8_t sn, uint8_t* buf, uint16_t port); 33 | 34 | #ifdef __cplusplus 35 | } 36 | #endif 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /W5500/Application/multicast/multicast.c: -------------------------------------------------------------------------------- 1 | #include "multicast.h" 2 | #include 3 | #include "socket.h" 4 | #include "wizchip_conf.h" 5 | 6 | 7 | int32_t multicast_loopback(uint8_t sn, uint8_t* buf, uint8_t* multicast_ip, uint16_t multicast_port) 8 | { 9 | int32_t ret; 10 | uint16_t size, sentsize; 11 | uint8_t destip[4]; 12 | uint16_t destport, port=3000; 13 | 14 | switch(getSn_SR(sn)) 15 | { 16 | case SOCK_UDP : 17 | if((size = getSn_RX_RSR(sn)) > 0) 18 | { 19 | if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE; 20 | ret = recvfrom(sn, buf, size, destip, (uint16_t*)&destport); 21 | if(ret <= 0) 22 | { 23 | #ifdef _MULTICAST_DEBUG_ 24 | printf("%d: recvfrom error. %ld\r\n",sn,ret); 25 | #endif 26 | return ret; 27 | } 28 | size = (uint16_t) ret; 29 | sentsize = 0; 30 | while(sentsize != size) 31 | { 32 | ret = sendto(sn, buf+sentsize, size-sentsize, destip, destport); 33 | if(ret < 0) 34 | { 35 | #ifdef _MULTICAST_DEBUG_ 36 | printf("%d: sendto error. %ld\r\n",sn,ret); 37 | #endif 38 | return ret; 39 | } 40 | sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero. 41 | } 42 | } 43 | 44 | break; 45 | case SOCK_CLOSED: 46 | #ifdef _MULTICAST_DEBUG_ 47 | printf("%d:Multicast Loopback start\r\n",sn); 48 | #endif 49 | setSn_DIPR(0, multicast_ip); 50 | setSn_DPORT(0, multicast_port); 51 | if((ret = socket(sn, Sn_MR_UDP, port, Sn_MR_MULTI)) != sn) 52 | return ret; 53 | #ifdef _MULTICAST_DEBUG_ 54 | printf("%d:Opened, UDP Multicast Socket\r\n", sn); 55 | printf("%d:Multicast Group IP - %d.%d.%d.%d\r\n", sn, multicast_ip[0], multicast_ip[1], multicast_ip[2], multicast_ip[3]); 56 | printf("%d:Multicast Group Port - %d\r\n", sn, multicast_port); 57 | #endif 58 | break; 59 | default : 60 | break; 61 | } 62 | return 1; 63 | } 64 | 65 | int32_t multicast_recv(uint8_t sn, uint8_t* buf, uint8_t* multicast_ip, uint16_t multicast_port) 66 | { 67 | int32_t ret; 68 | uint16_t size, port=3000; 69 | uint8_t destip[4]; 70 | uint16_t destport; 71 | 72 | switch(getSn_SR(sn)) 73 | { 74 | case SOCK_UDP : 75 | if((size = getSn_RX_RSR(sn)) > 0) 76 | { 77 | if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE; 78 | ret = recvfrom(sn, buf, size, destip, (uint16_t*)&destport); 79 | if(ret <= 0) 80 | { 81 | #ifdef _MULTICAST_DEBUG_ 82 | printf("%d: recvfrom error. %ld\r\n",sn,ret); 83 | #endif 84 | return ret; 85 | } 86 | size = (uint16_t) ret; 87 | #ifdef _MULTICAST_DEBUG_ 88 | printf("\r\nrecv size : %d\r\n", size); 89 | for(int i=0; i 9 | 10 | /* Multicast test debug message printout enable */ 11 | #define _MULTICAST_DEBUG_ 12 | 13 | #ifndef DATA_BUF_SIZE 14 | #define DATA_BUF_SIZE 2048 15 | #endif 16 | 17 | /* UDP Multicast Loopback test example */ 18 | int32_t multicast_loopback(uint8_t sn, uint8_t* buf, uint8_t* multicast_ip, uint16_t multicast_port); 19 | 20 | /* UDP Multicast Recv test example */ 21 | int32_t multicast_recv(uint8_t sn, uint8_t* buf, uint8_t* multicast_ip, uint16_t multicast_port); 22 | 23 | 24 | #ifdef __cplusplus 25 | } 26 | #endif 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /W5500/Ethernet/Socket_APIs_V3.0.3.chm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/W5500/Ethernet/Socket_APIs_V3.0.3.chm -------------------------------------------------------------------------------- /W5500/Internet/DHCP/dhcp.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // 3 | //! \file dhcp.h 4 | //! \brief DHCP APIs Header file. 5 | //! \details Processig DHCP protocol as DISCOVER, OFFER, REQUEST, ACK, NACK and DECLINE. 6 | //! \version 1.1.1 7 | //! \date 2019/10/08 8 | //! \par Revision history 9 | //! <2019/10/08> compare DHCP server ip address 10 | //! <2013/11/18> 1st Release 11 | //! <2012/12/20> V1.1.0 12 | //! 1. Move unreferenced DEFINE to dhcp.c 13 | //! <2012/12/26> V1.1.1 14 | //! \author Eric Jung & MidnightCow 15 | //! \copyright 16 | //! 17 | //! Copyright (c) 2013, WIZnet Co., LTD. 18 | //! All rights reserved. 19 | //! 20 | //! Redistribution and use in source and binary forms, with or without 21 | //! modification, are permitted provided that the following conditions 22 | //! are met: 23 | //! 24 | //! * Redistributions of source code must retain the above copyright 25 | //! notice, this list of conditions and the following disclaimer. 26 | //! * Redistributions in binary form must reproduce the above copyright 27 | //! notice, this list of conditions and the following disclaimer in the 28 | //! documentation and/or other materials provided with the distribution. 29 | //! * Neither the name of the nor the names of its 30 | //! contributors may be used to endorse or promote products derived 31 | //! from this software without specific prior written permission. 32 | //! 33 | //! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 34 | //! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 35 | //! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 36 | //! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 37 | //! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 38 | //! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 39 | //! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 40 | //! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 41 | //! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 42 | //! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 43 | //! THE POSSIBILITY OF SUCH DAMAGE. 44 | // 45 | //***************************************************************************** 46 | #ifndef _DHCP_H_ 47 | #define _DHCP_H_ 48 | 49 | #ifdef __cplusplus 50 | extern "C" { 51 | #endif 52 | 53 | /* 54 | * @brief 55 | * @details If you want to display debug & processing message, Define _DHCP_DEBUG_ 56 | * @note If defined, it depends on 57 | */ 58 | //#define _DHCP_DEBUG_ 59 | 60 | 61 | /* Retry to processing DHCP */ 62 | #define MAX_DHCP_RETRY 2 ///< Maximum retry count 63 | #define DHCP_WAIT_TIME 10 ///< Wait Time 10s 64 | 65 | 66 | /* UDP port numbers for DHCP */ 67 | #define DHCP_SERVER_PORT 67 ///< DHCP server port number 68 | #define DHCP_CLIENT_PORT 68 ///< DHCP client port number 69 | 70 | 71 | #define MAGIC_COOKIE 0x63825363 ///< You should not modify it number. 72 | 73 | #define DCHP_HOST_NAME "WIZnet\0" 74 | 75 | /* 76 | * @brief return value of @ref DHCP_run() 77 | */ 78 | enum 79 | { 80 | DHCP_FAILED = 0, ///< Processing Fail 81 | DHCP_RUNNING, ///< Processing DHCP protocol 82 | DHCP_IP_ASSIGN, ///< First Occupy IP from DHPC server (if cbfunc == null, act as default default_ip_assign) 83 | DHCP_IP_CHANGED, ///< Change IP address by new ip from DHCP (if cbfunc == null, act as default default_ip_update) 84 | DHCP_IP_LEASED, ///< Stand by 85 | DHCP_STOPPED ///< Stop processing DHCP protocol 86 | }; 87 | 88 | /* 89 | * @brief DHCP client initialization (outside of the main loop) 90 | * @param s - socket number 91 | * @param buf - buffer for processing DHCP message 92 | */ 93 | void DHCP_init(uint8_t s, uint8_t * buf); 94 | 95 | /* 96 | * @brief DHCP 1s Tick Timer handler 97 | * @note SHOULD BE register to your system 1s Tick timer handler 98 | */ 99 | void DHCP_time_handler(void); 100 | 101 | /* 102 | * @brief Register call back function 103 | * @param ip_assign - callback func when IP is assigned from DHCP server first 104 | * @param ip_update - callback func when IP is changed 105 | * @param ip_conflict - callback func when the assigned IP is conflict with others. 106 | */ 107 | void reg_dhcp_cbfunc(void(*ip_assign)(void), void(*ip_update)(void), void(*ip_conflict)(void)); 108 | 109 | /* 110 | * @brief DHCP client in the main loop 111 | * @return The value is as the follow \n 112 | * @ref DHCP_FAILED \n 113 | * @ref DHCP_RUNNING \n 114 | * @ref DHCP_IP_ASSIGN \n 115 | * @ref DHCP_IP_CHANGED \n 116 | * @ref DHCP_IP_LEASED \n 117 | * @ref DHCP_STOPPED \n 118 | * 119 | * @note This function is always called by you main task. 120 | */ 121 | uint8_t DHCP_run(void); 122 | 123 | /* 124 | * @brief Stop DHCP processing 125 | * @note If you want to restart. call DHCP_init() and DHCP_run() 126 | */ 127 | void DHCP_stop(void); 128 | 129 | /* Get Network information assigned from DHCP server */ 130 | /* 131 | * @brief Get IP address 132 | * @param ip - IP address to be returned 133 | */ 134 | void getIPfromDHCP(uint8_t* ip); 135 | /* 136 | * @brief Get Gateway address 137 | * @param ip - Gateway address to be returned 138 | */ 139 | void getGWfromDHCP(uint8_t* ip); 140 | /* 141 | * @brief Get Subnet mask value 142 | * @param ip - Subnet mask to be returned 143 | */ 144 | void getSNfromDHCP(uint8_t* ip); 145 | /* 146 | * @brief Get DNS address 147 | * @param ip - DNS address to be returned 148 | */ 149 | void getDNSfromDHCP(uint8_t* ip); 150 | 151 | /* 152 | * @brief Get the leased time by DHCP sever 153 | * @return unit 1s 154 | */ 155 | uint32_t getDHCPLeasetime(void); 156 | 157 | #ifdef __cplusplus 158 | } 159 | #endif 160 | 161 | #endif /* _DHCP_H_ */ 162 | -------------------------------------------------------------------------------- /W5500/Internet/DNS/dns.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // 3 | //! \file dns.h 4 | //! \brief DNS APIs Header file. 5 | //! \details Send DNS query & Receive DNS reponse. 6 | //! \version 1.1.0 7 | //! \date 2013/11/18 8 | //! \par Revision history 9 | //! <2013/10/21> 1st Release 10 | //! <2013/12/20> V1.1.0 11 | //! 1. Remove secondary DNS server in DNS_run 12 | //! If 1st DNS_run failed, call DNS_run with 2nd DNS again 13 | //! 2. DNS_timerHandler -> DNS_time_handler 14 | //! 3. Move the no reference define to dns.c 15 | //! 4. Integrated dns.h dns.c & dns_parse.h dns_parse.c into dns.h & dns.c 16 | //! <2013/12/20> V1.1.0 17 | //! 18 | //! \author Eric Jung & MidnightCow 19 | //! \copyright 20 | //! 21 | //! Copyright (c) 2013, WIZnet Co., LTD. 22 | //! All rights reserved. 23 | //! 24 | //! Redistribution and use in source and binary forms, with or without 25 | //! modification, are permitted provided that the following conditions 26 | //! are met: 27 | //! 28 | //! * Redistributions of source code must retain the above copyright 29 | //! notice, this list of conditions and the following disclaimer. 30 | //! * Redistributions in binary form must reproduce the above copyright 31 | //! notice, this list of conditions and the following disclaimer in the 32 | //! documentation and/or other materials provided with the distribution. 33 | //! * Neither the name of the nor the names of its 34 | //! contributors may be used to endorse or promote products derived 35 | //! from this software without specific prior written permission. 36 | //! 37 | //! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 38 | //! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 39 | //! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 40 | //! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 41 | //! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 42 | //! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 43 | //! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 44 | //! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 45 | //! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 46 | //! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 47 | //! THE POSSIBILITY OF SUCH DAMAGE. 48 | // 49 | //***************************************************************************** 50 | 51 | #ifndef _DNS_H_ 52 | #define _DNS_H_ 53 | 54 | #ifdef __cplusplus 55 | extern "C" { 56 | #endif 57 | 58 | #include 59 | /* 60 | * @brief Define it for Debug & Monitor DNS processing. 61 | * @note If defined, it dependens on 62 | */ 63 | //#define _DNS_DEBUG_ 64 | 65 | #define MAX_DNS_BUF_SIZE 256 ///< maximum size of DNS buffer. */ 66 | /* 67 | * @brief Maxium length of your queried Domain name 68 | * @todo SHOULD BE defined it equal as or greater than your Domain name lenght + null character(1) 69 | * @note SHOULD BE careful to stack overflow because it is allocated 1.5 times as MAX_DOMAIN_NAME in stack. 70 | */ 71 | #define MAX_DOMAIN_NAME 16 // for example "www.google.com" 72 | 73 | #define MAX_DNS_RETRY 2 ///< Requery Count 74 | #define DNS_WAIT_TIME 3 ///< Wait response time. unit 1s. 75 | 76 | #define IPPORT_DOMAIN 53 ///< DNS server port number 77 | 78 | #define DNS_MSG_ID 0x1122 ///< ID for DNS message. You can be modifyed it any number 79 | /* 80 | * @brief DNS process initialize 81 | * @param s : Socket number for DNS 82 | * @param buf : Buffer for DNS message 83 | */ 84 | void DNS_init(uint8_t s, uint8_t * buf); 85 | 86 | /* 87 | * @brief DNS process 88 | * @details Send DNS query and receive DNS response 89 | * @param dns_ip : DNS server ip 90 | * @param name : Domain name to be queryed 91 | * @param ip_from_dns : IP address from DNS server 92 | * @return -1 : failed. @ref MAX_DOMIN_NAME is too small \n 93 | * 0 : failed (Timeout or Parse error)\n 94 | * 1 : success 95 | * @note This funtion blocks until success or fail. max time = @ref MAX_DNS_RETRY * @ref DNS_WAIT_TIME 96 | */ 97 | int8_t DNS_run(uint8_t * dns_ip, uint8_t * name, uint8_t * ip_from_dns); 98 | 99 | /* 100 | * @brief DNS 1s Tick Timer handler 101 | * @note SHOULD BE register to your system 1s Tick timer handler 102 | */ 103 | void DNS_time_handler(void); 104 | 105 | #ifdef __cplusplus 106 | } 107 | #endif 108 | 109 | #endif /* _DNS_H_ */ 110 | -------------------------------------------------------------------------------- /W5500/Internet/FTPClient/ftpc.h: -------------------------------------------------------------------------------- 1 | #ifndef _FTPC_H_ 2 | #define _FTPC_H_ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "stdio_private.h" 15 | #include "socket.h" 16 | 17 | #define F_APP_FTPC 18 | 19 | /* If your target support a file system, you have to activate this feature and implement. */ 20 | //#define F_FILESYSTEM 21 | 22 | /* Change to your Chipset Uart function, you have to activate this feature and implement. 23 | * Change!! -> Board_UARTGetCharBlocking() 24 | * Below is an example of a function of lpc_chip library. */ 25 | //#define ftp_getc() Board_UARTGetCharBlocking() 26 | 27 | #ifdef F_FILESYSTEM 28 | #include "ff.h" 29 | #endif 30 | 31 | #ifndef ftp_getc() 32 | #define Need_UARTGetCharBlocking_func 33 | #else 34 | /* Change library 35 | * Change!! -> board_api.h, 36 | * Below is an example of a function of lpc_chip library. */ 37 | #include "board_api.h" 38 | #endif 39 | 40 | 41 | #define LINELEN 100 42 | #ifndef F_FILESYSTEM 43 | #define _MAX_SS 512 44 | #endif 45 | 46 | #define CTRL_SOCK 2 47 | #define DATA_SOCK 3 48 | 49 | /* FTP Responses */ 50 | #define R_150 150 /* File status ok; opening data conn */ 51 | #define R_200 200 /* 'Generic' command ok */ 52 | #define R_220 220 /* Service ready for new user. */ 53 | #define R_226 226 /* Closing data connection. File transfer/abort successful */ 54 | #define R_227 227 /* Entering passive mode (h1,h2,h3,h4,p1,p2) */ 55 | #define R_230 230 /* User logged in, proceed */ 56 | #define R_331 331 /* User name okay, need password. */ 57 | 58 | #define TransferAscii 'A' 59 | #define TransferBinary 'I' 60 | 61 | enum ftpc_type { 62 | ASCII_TYPE, 63 | IMAGE_TYPE, 64 | }; 65 | 66 | enum ftpc_datasock_state{ 67 | DATASOCK_IDLE, 68 | DATASOCK_READY, 69 | DATASOCK_START 70 | }; 71 | 72 | enum ftpc_datasock_mode{ 73 | PASSIVE_MODE, 74 | ACTIVE_MODE 75 | }; 76 | enum CommandFirst { 77 | f_nocmd, 78 | f_dir, 79 | f_put, 80 | f_get, 81 | }; 82 | enum CommandSecond { 83 | s_nocmd, 84 | s_dir, 85 | s_put, 86 | s_get, 87 | }; 88 | struct Command { 89 | enum CommandFirst First; 90 | enum CommandSecond Second; 91 | }; 92 | struct ftpc { 93 | uint8_t control; /* Control stream */ 94 | uint8_t data; /* Data stream */ 95 | 96 | enum ftpc_type type; /* Transfer type */ 97 | 98 | enum ftpc_datasock_state dsock_state; 99 | enum ftpc_datasock_mode dsock_mode; 100 | 101 | char workingdir[LINELEN]; 102 | char filename[LINELEN]; 103 | 104 | #ifdef F_FILESYSTEM 105 | FIL fil; // FatFs File objects 106 | FRESULT fr; // FatFs function common result code 107 | #endif 108 | }; 109 | 110 | #ifndef un_I2cval 111 | typedef union _un_l2cval { 112 | uint32_t lVal; 113 | uint8_t cVal[4]; 114 | }un_l2cval; 115 | #endif 116 | 117 | void ftpc_init(uint8_t * src_ip); 118 | uint8_t ftpc_run(uint8_t * dbuf); 119 | char proc_ftpc(char * buf); 120 | int pportc(char * arg); 121 | uint8_t* User_Keyboard_MSG(); 122 | 123 | #ifdef __cplusplus 124 | } 125 | #endif 126 | 127 | #endif // _FTPC_H_ 128 | -------------------------------------------------------------------------------- /W5500/Internet/FTPClient/stdio_private.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id: stdio_private.h,v 1.6 2003/01/07 22:17:24 joerg_wunsch Exp $ */ 31 | 32 | #ifdef __cplusplus 33 | extern "C" { 34 | #endif 35 | 36 | #include 37 | #include 38 | 39 | struct __file { 40 | char *buf; /* buffer pointer */ 41 | unsigned char unget; /* ungetc() buffer */ 42 | uint8_t flags; /* flags, see below */ 43 | #define __SRD 0x0001 /* OK to read */ 44 | #define __SWR 0x0002 /* OK to write */ 45 | #define __SSTR 0x0004 /* this is an sprintf/snprintf string */ 46 | #define __SPGM 0x0008 /* fmt string is in progmem */ 47 | #define __SERR 0x0010 /* found error */ 48 | #define __SEOF 0x0020 /* found EOF */ 49 | #define __SUNGET 0x040 /* ungetc() happened */ 50 | #if 0 51 | /* possible future extensions, will require uint16_t flags */ 52 | #define __SRW 0x0080 /* open for reading & writing */ 53 | #define __SLBF 0x0100 /* line buffered */ 54 | #define __SNBF 0x0200 /* unbuffered */ 55 | #define __SMBF 0x0400 /* buf is from malloc */ 56 | #endif 57 | int size; /* size of buffer */ 58 | int len; /* characters read or written so far */ 59 | int (*put)(char); /* function to write one char to device */ 60 | int (*get)(void); /* function to read one char from device */ 61 | }; 62 | 63 | /* values for PRINTF_LEVEL */ 64 | #define PRINTF_MIN 1 65 | #define PRINTF_STD 2 66 | #define PRINTF_FLT 3 67 | 68 | /* values for SCANF_LEVEL */ 69 | #define SCANF_MIN 1 70 | #define SCANF_STD 2 71 | #define SCANF_FLT 3 72 | 73 | #ifdef __cplusplus 74 | } 75 | #endif 76 | -------------------------------------------------------------------------------- /W5500/Internet/FTPServer/ftpd.h: -------------------------------------------------------------------------------- 1 | #ifndef _FTPD_H_ 2 | #define _FTPD_H_ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | /* 9 | * Wiznet. 10 | * (c) Copyright 2002, Wiznet. 11 | * 12 | * Filename : ftpd.h 13 | * Version : 1.0 14 | * Programmer(s) : 15 | * Created : 2003/01/28 16 | * Description : Header file of FTP daemon. (AVR-GCC Compiler) 17 | */ 18 | 19 | #include 20 | 21 | //#define F_FILESYSTEM // If your target support a file system, you have to activate this feature and implement. 22 | 23 | #if defined(F_FILESYSTEM) 24 | #include "ff.h" 25 | #endif 26 | 27 | #define F_APP_FTP 28 | #define _FTP_DEBUG_ 29 | 30 | 31 | #define LINELEN 100 32 | //#define DATA_BUF_SIZE 100 33 | #if !defined(F_FILESYSTEM) 34 | #define _MAX_SS 512 35 | #endif 36 | 37 | #define CTRL_SOCK 2 38 | #define DATA_SOCK 3 39 | 40 | #define IPPORT_FTPD 20 /* FTP Data port */ 41 | #define IPPORT_FTP 21 /* FTP Control port */ 42 | 43 | #define HOSTNAME "iinChip" 44 | #define VERSION "1.0" 45 | 46 | #define FILENAME "a.txt" 47 | 48 | /* FTP commands */ 49 | enum ftp_cmd { 50 | USER_CMD, 51 | ACCT_CMD, 52 | PASS_CMD, 53 | TYPE_CMD, 54 | LIST_CMD, 55 | CWD_CMD, 56 | DELE_CMD, 57 | NAME_CMD, 58 | QUIT_CMD, 59 | RETR_CMD, 60 | STOR_CMD, 61 | PORT_CMD, 62 | NLST_CMD, 63 | PWD_CMD, 64 | XPWD_CMD, 65 | MKD_CMD, 66 | XMKD_CMD, 67 | XRMD_CMD, 68 | RMD_CMD, 69 | STRU_CMD, 70 | MODE_CMD, 71 | SYST_CMD, 72 | XMD5_CMD, 73 | XCWD_CMD, 74 | FEAT_CMD, 75 | PASV_CMD, 76 | SIZE_CMD, 77 | MLSD_CMD, 78 | APPE_CMD, 79 | NO_CMD, 80 | }; 81 | 82 | enum ftp_type { 83 | ASCII_TYPE, 84 | IMAGE_TYPE, 85 | LOGICAL_TYPE 86 | }; 87 | 88 | enum ftp_state { 89 | FTPS_NOT_LOGIN, 90 | FTPS_LOGIN 91 | }; 92 | 93 | enum datasock_state{ 94 | DATASOCK_IDLE, 95 | DATASOCK_READY, 96 | DATASOCK_START 97 | }; 98 | 99 | enum datasock_mode{ 100 | PASSIVE_MODE, 101 | ACTIVE_MODE 102 | }; 103 | 104 | struct ftpd { 105 | uint8_t control; /* Control stream */ 106 | uint8_t data; /* Data stream */ 107 | 108 | enum ftp_type type; /* Transfer type */ 109 | enum ftp_state state; 110 | 111 | enum ftp_cmd current_cmd; 112 | 113 | enum datasock_state dsock_state; 114 | enum datasock_mode dsock_mode; 115 | 116 | char username[LINELEN]; /* Arg to USER command */ 117 | char workingdir[LINELEN]; 118 | char filename[LINELEN]; 119 | 120 | #if defined(F_FILESYSTEM) 121 | FIL fil; // FatFs File objects 122 | FRESULT fr; // FatFs function common result code 123 | #endif 124 | 125 | }; 126 | 127 | #ifndef un_I2cval 128 | typedef union _un_l2cval { 129 | uint32_t lVal; 130 | uint8_t cVal[4]; 131 | }un_l2cval; 132 | #endif 133 | 134 | void ftpd_init(uint8_t * src_ip); 135 | uint8_t ftpd_run(uint8_t * dbuf); 136 | char proc_ftpd(char * buf); 137 | char ftplogin(char * pass); 138 | int pport(char * arg); 139 | 140 | int sendit(char * command); 141 | int recvit(char * command); 142 | 143 | long sendfile(uint8_t s, char * command); 144 | long recvfile(uint8_t s); 145 | 146 | #if defined(F_FILESYSTEM) 147 | void print_filedsc(FIL *fil); 148 | #endif 149 | 150 | #ifdef __cplusplus 151 | } 152 | #endif 153 | 154 | #endif // _FTPD_H_ 155 | -------------------------------------------------------------------------------- /W5500/Internet/FTPServer/stdio_private.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id: stdio_private.h,v 1.6 2003/01/07 22:17:24 joerg_wunsch Exp $ */ 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | #include 36 | #include 37 | 38 | struct __file { 39 | char *buf; /* buffer pointer */ 40 | unsigned char unget; /* ungetc() buffer */ 41 | uint8_t flags; /* flags, see below */ 42 | #define __SRD 0x0001 /* OK to read */ 43 | #define __SWR 0x0002 /* OK to write */ 44 | #define __SSTR 0x0004 /* this is an sprintf/snprintf string */ 45 | #define __SPGM 0x0008 /* fmt string is in progmem */ 46 | #define __SERR 0x0010 /* found error */ 47 | #define __SEOF 0x0020 /* found EOF */ 48 | #define __SUNGET 0x040 /* ungetc() happened */ 49 | #if 0 50 | /* possible future extensions, will require uint16_t flags */ 51 | #define __SRW 0x0080 /* open for reading & writing */ 52 | #define __SLBF 0x0100 /* line buffered */ 53 | #define __SNBF 0x0200 /* unbuffered */ 54 | #define __SMBF 0x0400 /* buf is from malloc */ 55 | #endif 56 | int size; /* size of buffer */ 57 | int len; /* characters read or written so far */ 58 | int (*put)(char); /* function to write one char to device */ 59 | int (*get)(void); /* function to read one char from device */ 60 | }; 61 | 62 | /* values for PRINTF_LEVEL */ 63 | #define PRINTF_MIN 1 64 | #define PRINTF_STD 2 65 | #define PRINTF_FLT 3 66 | 67 | /* values for SCANF_LEVEL */ 68 | #define SCANF_MIN 1 69 | #define SCANF_STD 2 70 | #define SCANF_FLT 3 71 | 72 | #ifdef __cplusplus 73 | } 74 | #endif 75 | -------------------------------------------------------------------------------- /W5500/Internet/MQTT/MQTTClient.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014, 2015 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Allan Stockdill-Mander/Ian Craggs - initial API and implementation and/or initial documentation 15 | * Ian Craggs - documentation and platform specific header 16 | *******************************************************************************/ 17 | 18 | #if !defined(__MQTT_CLIENT_C_) 19 | #define __MQTT_CLIENT_C_ 20 | 21 | #if defined(__cplusplus) 22 | extern "C" { 23 | #endif 24 | 25 | #if defined(WIN32_DLL) || defined(WIN64_DLL) 26 | #define DLLImport __declspec(dllimport) 27 | #define DLLExport __declspec(dllexport) 28 | #elif defined(LINUX_SO) 29 | #define DLLImport extern 30 | #define DLLExport __attribute__ ((visibility ("default"))) 31 | #else 32 | #define DLLImport 33 | #define DLLExport 34 | #endif 35 | 36 | #include "./MQTTPacket/src/MQTTPacket.h" 37 | #include "stdio.h" 38 | #include "mqtt_interface.h" 39 | 40 | #define MAX_PACKET_ID 65535 /* according to the MQTT specification - do not change! */ 41 | 42 | #if !defined(MAX_MESSAGE_HANDLERS) 43 | #define MAX_MESSAGE_HANDLERS 5 /* redefinable - how many subscriptions do you want? */ 44 | #endif 45 | 46 | enum QoS { QOS0, QOS1, QOS2 }; 47 | 48 | /* all failure return codes must be negative */ 49 | enum returnCode { BUFFER_OVERFLOW = -2, FAILURE = -1, SUCCESSS = 0 }; 50 | 51 | /* The Platform specific header must define the Network and Timer structures and functions 52 | * which operate on them. 53 | * 54 | typedef struct Network 55 | { 56 | int (*mqttread)(Network*, unsigned char* read_buffer, int, int); 57 | int (*mqttwrite)(Network*, unsigned char* send_buffer, int, int); 58 | } Network;*/ 59 | 60 | /* The Timer structure must be defined in the platform specific header, 61 | * and have the following functions to operate on it. */ 62 | extern void TimerInit(Timer*); 63 | extern char TimerIsExpired(Timer*); 64 | extern void TimerCountdownMS(Timer*, unsigned int); 65 | extern void TimerCountdown(Timer*, unsigned int); 66 | extern int TimerLeftMS(Timer*); 67 | 68 | typedef struct MQTTMessage 69 | { 70 | enum QoS qos; 71 | unsigned char retained; 72 | unsigned char dup; 73 | unsigned short id; 74 | void *payload; 75 | size_t payloadlen; 76 | } MQTTMessage; 77 | 78 | typedef struct MessageData 79 | { 80 | MQTTMessage* message; 81 | MQTTString* topicName; 82 | } MessageData; 83 | 84 | typedef void (*messageHandler)(MessageData*); 85 | 86 | typedef struct MQTTClient 87 | { 88 | unsigned int next_packetid, 89 | command_timeout_ms; 90 | size_t buf_size, 91 | readbuf_size; 92 | unsigned char *buf, 93 | *readbuf; 94 | unsigned int keepAliveInterval; 95 | char ping_outstanding; 96 | int isconnected; 97 | 98 | struct MessageHandlers 99 | { 100 | const char* topicFilter; 101 | void (*fp) (MessageData*); 102 | } messageHandlers[MAX_MESSAGE_HANDLERS]; /* Message handlers are indexed by subscription topic */ 103 | 104 | void (*defaultMessageHandler) (MessageData*); 105 | 106 | Network* ipstack; 107 | Timer ping_timer; 108 | #if defined(MQTT_TASK) 109 | Mutex mutex; 110 | Thread thread; 111 | #endif 112 | } MQTTClient; 113 | 114 | #define DefaultClient {0, 0, 0, 0, NULL, NULL, 0, 0, 0} 115 | 116 | 117 | /** 118 | * Create an MQTT client object 119 | * @param client 120 | * @param network 121 | * @param command_timeout_ms 122 | * @param 123 | */ 124 | DLLExport void MQTTClientInit(MQTTClient* client, Network* network, unsigned int command_timeout_ms, 125 | unsigned char* sendbuf, size_t sendbuf_size, unsigned char* readbuf, size_t readbuf_size); 126 | 127 | /** MQTT Connect - send an MQTT connect packet down the network and wait for a Connack 128 | * The nework object must be connected to the network endpoint before calling this 129 | * @param options - connect options 130 | * @return success code 131 | */ 132 | DLLExport int MQTTConnect(MQTTClient* client, MQTTPacket_connectData* options); 133 | 134 | /** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs 135 | * @param client - the client object to use 136 | * @param topic - the topic to publish to 137 | * @param message - the message to send 138 | * @return success code 139 | */ 140 | DLLExport int MQTTPublish(MQTTClient* client, const char*, MQTTMessage*); 141 | 142 | /** MQTT Subscribe - send an MQTT subscribe packet and wait for suback before returning. 143 | * @param client - the client object to use 144 | * @param topicFilter - the topic filter to subscribe to 145 | * @param message - the message to send 146 | * @return success code 147 | */ 148 | DLLExport int MQTTSubscribe(MQTTClient* client, const char* topicFilter, enum QoS, messageHandler); 149 | 150 | /** MQTT Subscribe - send an MQTT unsubscribe packet and wait for unsuback before returning. 151 | * @param client - the client object to use 152 | * @param topicFilter - the topic filter to unsubscribe from 153 | * @return success code 154 | */ 155 | DLLExport int MQTTUnsubscribe(MQTTClient* client, const char* topicFilter); 156 | 157 | /** MQTT Disconnect - send an MQTT disconnect packet and close the connection 158 | * @param client - the client object to use 159 | * @return success code 160 | */ 161 | DLLExport int MQTTDisconnect(MQTTClient* client); 162 | 163 | /** MQTT Yield - MQTT background 164 | * @param client - the client object to use 165 | * @param time - the time, in milliseconds, to yield for 166 | * @return success code 167 | */ 168 | DLLExport int MQTTYield(MQTTClient* client, int time); 169 | 170 | #if defined(MQTT_TASK) 171 | /** MQTT start background thread for a client. After this, MQTTYield should not be called. 172 | * @param client - the client object to use 173 | * @return success code 174 | */ 175 | DLLExport int MQTTStartTask(MQTTClient* client); 176 | #endif 177 | 178 | #if defined(__cplusplus) 179 | } 180 | #endif 181 | 182 | #endif 183 | -------------------------------------------------------------------------------- /W5500/Internet/MQTT/MQTTPacket/src/MQTTConnect.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Xiang Rong - 442039 Add makefile to Embedded C client 16 | *******************************************************************************/ 17 | 18 | #ifndef MQTTCONNECT_H_ 19 | #define MQTTCONNECT_H_ 20 | 21 | #if !defined(DLLImport) 22 | #define DLLImport 23 | #endif 24 | #if !defined(DLLExport) 25 | #define DLLExport 26 | #endif 27 | 28 | 29 | typedef union 30 | { 31 | unsigned char all; /**< all connect flags */ 32 | #if defined(REVERSED) 33 | struct 34 | { 35 | unsigned int username : 1; /**< 3.1 user name */ 36 | unsigned int password : 1; /**< 3.1 password */ 37 | unsigned int willRetain : 1; /**< will retain setting */ 38 | unsigned int willQoS : 2; /**< will QoS value */ 39 | unsigned int will : 1; /**< will flag */ 40 | unsigned int cleansession : 1; /**< clean session flag */ 41 | unsigned int : 1; /**< unused */ 42 | } bits; 43 | #else 44 | struct 45 | { 46 | unsigned int : 1; /**< unused */ 47 | unsigned int cleansession : 1; /**< cleansession flag */ 48 | unsigned int will : 1; /**< will flag */ 49 | unsigned int willQoS : 2; /**< will QoS value */ 50 | unsigned int willRetain : 1; /**< will retain setting */ 51 | unsigned int password : 1; /**< 3.1 password */ 52 | unsigned int username : 1; /**< 3.1 user name */ 53 | } bits; 54 | #endif 55 | } MQTTConnectFlags; /**< connect flags byte */ 56 | 57 | 58 | 59 | /** 60 | * Defines the MQTT "Last Will and Testament" (LWT) settings for 61 | * the connect packet. 62 | */ 63 | typedef struct 64 | { 65 | /** The eyecatcher for this structure. must be MQTW. */ 66 | char struct_id[4]; 67 | /** The version number of this structure. Must be 0 */ 68 | int struct_version; 69 | /** The LWT topic to which the LWT message will be published. */ 70 | MQTTString topicName; 71 | /** The LWT payload. */ 72 | MQTTString message; 73 | /** 74 | * The retained flag for the LWT message (see MQTTAsync_message.retained). 75 | */ 76 | unsigned char retained; 77 | /** 78 | * The quality of service setting for the LWT message (see 79 | * MQTTAsync_message.qos and @ref qos). 80 | */ 81 | char qos; 82 | } MQTTPacket_willOptions; 83 | 84 | 85 | #define MQTTPacket_willOptions_initializer { {'M', 'Q', 'T', 'W'}, 0, {NULL, {0, NULL}}, {NULL, {0, NULL}}, 0, 0 } 86 | 87 | 88 | typedef struct 89 | { 90 | /** The eyecatcher for this structure. must be MQTC. */ 91 | char struct_id[4]; 92 | /** The version number of this structure. Must be 0 */ 93 | int struct_version; 94 | /** Version of MQTT to be used. 3 = 3.1 4 = 3.1.1 95 | */ 96 | unsigned char MQTTVersion; 97 | MQTTString clientID; 98 | unsigned short keepAliveInterval; 99 | unsigned char cleansession; 100 | unsigned char willFlag; 101 | MQTTPacket_willOptions will; 102 | MQTTString username; 103 | MQTTString password; 104 | } MQTTPacket_connectData; 105 | 106 | typedef union 107 | { 108 | unsigned char all; /**< all connack flags */ 109 | #if defined(REVERSED) 110 | struct 111 | { 112 | unsigned int sessionpresent : 1; /**< session present flag */ 113 | unsigned int : 7; /**< unused */ 114 | } bits; 115 | #else 116 | struct 117 | { 118 | unsigned int : 7; /**< unused */ 119 | unsigned int sessionpresent : 1; /**< session present flag */ 120 | } bits; 121 | #endif 122 | } MQTTConnackFlags; /**< connack flags byte */ 123 | 124 | #define MQTTPacket_connectData_initializer { {'M', 'Q', 'T', 'C'}, 0, 4, {NULL, {0, NULL}}, 60, 1, 0, \ 125 | MQTTPacket_willOptions_initializer, {NULL, {0, NULL}}, {NULL, {0, NULL}} } 126 | 127 | DLLExport int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options); 128 | DLLExport int MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf, int len); 129 | 130 | DLLExport int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent); 131 | DLLExport int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen); 132 | 133 | DLLExport int MQTTSerialize_disconnect(unsigned char* buf, int buflen); 134 | DLLExport int MQTTSerialize_pingreq(unsigned char* buf, int buflen); 135 | 136 | #endif /* MQTTCONNECT_H_ */ 137 | -------------------------------------------------------------------------------- /W5500/Internet/MQTT/MQTTPacket/src/MQTTConnectClient.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | *******************************************************************************/ 16 | 17 | #include "MQTTPacket.h" 18 | #include "StackTrace.h" 19 | 20 | #include 21 | 22 | /** 23 | * Determines the length of the MQTT connect packet that would be produced using the supplied connect options. 24 | * @param options the options to be used to build the connect packet 25 | * @return the length of buffer needed to contain the serialized version of the packet 26 | */ 27 | int MQTTSerialize_connectLength(MQTTPacket_connectData* options) 28 | { 29 | int len = 0; 30 | 31 | FUNC_ENTRY; 32 | 33 | if (options->MQTTVersion == 3) 34 | len = 12; /* variable depending on MQTT or MQIsdp */ 35 | else if (options->MQTTVersion == 4) 36 | len = 10; 37 | 38 | len += MQTTstrlen(options->clientID)+2; 39 | if (options->willFlag) 40 | len += MQTTstrlen(options->will.topicName)+2 + MQTTstrlen(options->will.message)+2; 41 | if (options->username.cstring || options->username.lenstring.data) 42 | len += MQTTstrlen(options->username)+2; 43 | if (options->password.cstring || options->password.lenstring.data) 44 | len += MQTTstrlen(options->password)+2; 45 | 46 | FUNC_EXIT_RC(len); 47 | return len; 48 | } 49 | 50 | 51 | /** 52 | * Serializes the connect options into the buffer. 53 | * @param buf the buffer into which the packet will be serialized 54 | * @param len the length in bytes of the supplied buffer 55 | * @param options the options to be used to build the connect packet 56 | * @return serialized length, or error if 0 57 | */ 58 | int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options) 59 | { 60 | unsigned char *ptr = buf; 61 | MQTTHeader header = {0}; 62 | MQTTConnectFlags flags = {0}; 63 | int len = 0; 64 | int rc = -1; 65 | 66 | FUNC_ENTRY; 67 | if (MQTTPacket_len(len = MQTTSerialize_connectLength(options)) > buflen) 68 | { 69 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 70 | goto exit; 71 | } 72 | 73 | header.byte = 0; 74 | header.bits.type = CONNECT; 75 | writeChar(&ptr, header.byte); /* write header */ 76 | 77 | ptr += MQTTPacket_encode(ptr, len); /* write remaining length */ 78 | 79 | if (options->MQTTVersion == 4) 80 | { 81 | writeCString(&ptr, "MQTT"); 82 | writeChar(&ptr, (char) 4); 83 | } 84 | else 85 | { 86 | writeCString(&ptr, "MQIsdp"); 87 | writeChar(&ptr, (char) 3); 88 | } 89 | 90 | flags.all = 0; 91 | flags.bits.cleansession = options->cleansession; 92 | flags.bits.will = (options->willFlag) ? 1 : 0; 93 | if (flags.bits.will) 94 | { 95 | flags.bits.willQoS = options->will.qos; 96 | flags.bits.willRetain = options->will.retained; 97 | } 98 | 99 | if (options->username.cstring || options->username.lenstring.data) 100 | flags.bits.username = 1; 101 | if (options->password.cstring || options->password.lenstring.data) 102 | flags.bits.password = 1; 103 | 104 | writeChar(&ptr, flags.all); 105 | writeInt(&ptr, options->keepAliveInterval); 106 | writeMQTTString(&ptr, options->clientID); 107 | if (options->willFlag) 108 | { 109 | writeMQTTString(&ptr, options->will.topicName); 110 | writeMQTTString(&ptr, options->will.message); 111 | } 112 | if (flags.bits.username) 113 | writeMQTTString(&ptr, options->username); 114 | if (flags.bits.password) 115 | writeMQTTString(&ptr, options->password); 116 | 117 | rc = ptr - buf; 118 | 119 | exit: FUNC_EXIT_RC(rc); 120 | return rc; 121 | } 122 | 123 | 124 | /** 125 | * Deserializes the supplied (wire) buffer into connack data - return code 126 | * @param sessionPresent the session present flag returned (only for MQTT 3.1.1) 127 | * @param connack_rc returned integer value of the connack return code 128 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 129 | * @param len the length in bytes of the data in the supplied buffer 130 | * @return error code. 1 is success, 0 is failure 131 | */ 132 | int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen) 133 | { 134 | MQTTHeader header = {0}; 135 | unsigned char* curdata = buf; 136 | unsigned char* enddata = NULL; 137 | int rc = 0; 138 | int mylen; 139 | MQTTConnackFlags flags = {0}; 140 | 141 | FUNC_ENTRY; 142 | header.byte = readChar(&curdata); 143 | if (header.bits.type != CONNACK) 144 | goto exit; 145 | 146 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 147 | enddata = curdata + mylen; 148 | if (enddata - curdata < 2) 149 | goto exit; 150 | 151 | flags.all = readChar(&curdata); 152 | *sessionPresent = flags.bits.sessionpresent; 153 | *connack_rc = readChar(&curdata); 154 | 155 | rc = 1; 156 | exit: 157 | FUNC_EXIT_RC(rc); 158 | return rc; 159 | } 160 | 161 | 162 | /** 163 | * Serializes a 0-length packet into the supplied buffer, ready for writing to a socket 164 | * @param buf the buffer into which the packet will be serialized 165 | * @param buflen the length in bytes of the supplied buffer, to avoid overruns 166 | * @param packettype the message type 167 | * @return serialized length, or error if 0 168 | */ 169 | int MQTTSerialize_zero(unsigned char* buf, int buflen, unsigned char packettype) 170 | { 171 | MQTTHeader header = {0}; 172 | int rc = -1; 173 | unsigned char *ptr = buf; 174 | 175 | FUNC_ENTRY; 176 | if (buflen < 2) 177 | { 178 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 179 | goto exit; 180 | } 181 | header.byte = 0; 182 | header.bits.type = packettype; 183 | writeChar(&ptr, header.byte); /* write header */ 184 | 185 | ptr += MQTTPacket_encode(ptr, 0); /* write remaining length */ 186 | rc = ptr - buf; 187 | exit: 188 | FUNC_EXIT_RC(rc); 189 | return rc; 190 | } 191 | 192 | 193 | /** 194 | * Serializes a disconnect packet into the supplied buffer, ready for writing to a socket 195 | * @param buf the buffer into which the packet will be serialized 196 | * @param buflen the length in bytes of the supplied buffer, to avoid overruns 197 | * @return serialized length, or error if 0 198 | */ 199 | int MQTTSerialize_disconnect(unsigned char* buf, int buflen) 200 | { 201 | return MQTTSerialize_zero(buf, buflen, DISCONNECT); 202 | } 203 | 204 | 205 | /** 206 | * Serializes a disconnect packet into the supplied buffer, ready for writing to a socket 207 | * @param buf the buffer into which the packet will be serialized 208 | * @param buflen the length in bytes of the supplied buffer, to avoid overruns 209 | * @return serialized length, or error if 0 210 | */ 211 | int MQTTSerialize_pingreq(unsigned char* buf, int buflen) 212 | { 213 | return MQTTSerialize_zero(buf, buflen, PINGREQ); 214 | } 215 | -------------------------------------------------------------------------------- /W5500/Internet/MQTT/MQTTPacket/src/MQTTConnectServer.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | *******************************************************************************/ 16 | 17 | #include "StackTrace.h" 18 | #include "MQTTPacket.h" 19 | #include 20 | 21 | #define min(a, b) ((a < b) ? a : b) 22 | 23 | 24 | /** 25 | * Validates MQTT protocol name and version combinations 26 | * @param protocol the MQTT protocol name as an MQTTString 27 | * @param version the MQTT protocol version number, as in the connect packet 28 | * @return correct MQTT combination? 1 is true, 0 is false 29 | */ 30 | int MQTTPacket_checkVersion(MQTTString* protocol, int version) 31 | { 32 | int rc = 0; 33 | 34 | if (version == 3 && memcmp(protocol->lenstring.data, "MQIsdp", 35 | min(6, protocol->lenstring.len)) == 0) 36 | rc = 1; 37 | else if (version == 4 && memcmp(protocol->lenstring.data, "MQTT", 38 | min(4, protocol->lenstring.len)) == 0) 39 | rc = 1; 40 | return rc; 41 | } 42 | 43 | 44 | /** 45 | * Deserializes the supplied (wire) buffer into connect data structure 46 | * @param data the connect data structure to be filled out 47 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 48 | * @param len the length in bytes of the data in the supplied buffer 49 | * @return error code. 1 is success, 0 is failure 50 | */ 51 | int MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf, int len) 52 | { 53 | MQTTHeader header = {0}; 54 | MQTTConnectFlags flags = {0}; 55 | unsigned char* curdata = buf; 56 | unsigned char* enddata = &buf[len]; 57 | int rc = 0; 58 | MQTTString Protocol; 59 | int version; 60 | int mylen = 0; 61 | 62 | FUNC_ENTRY; 63 | header.byte = readChar(&curdata); 64 | if (header.bits.type != CONNECT) 65 | goto exit; 66 | 67 | curdata += MQTTPacket_decodeBuf(curdata, &mylen); /* read remaining length */ 68 | 69 | if (!readMQTTLenString(&Protocol, &curdata, enddata) || 70 | enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */ 71 | goto exit; 72 | 73 | version = (int)readChar(&curdata); /* Protocol version */ 74 | /* If we don't recognize the protocol version, we don't parse the connect packet on the 75 | * basis that we don't know what the format will be. 76 | */ 77 | if (MQTTPacket_checkVersion(&Protocol, version)) 78 | { 79 | flags.all = readChar(&curdata); 80 | data->cleansession = flags.bits.cleansession; 81 | data->keepAliveInterval = readInt(&curdata); 82 | if (!readMQTTLenString(&data->clientID, &curdata, enddata)) 83 | goto exit; 84 | data->willFlag = flags.bits.will; 85 | if (flags.bits.will) 86 | { 87 | data->will.qos = flags.bits.willQoS; 88 | data->will.retained = flags.bits.willRetain; 89 | if (!readMQTTLenString(&data->will.topicName, &curdata, enddata) || 90 | !readMQTTLenString(&data->will.message, &curdata, enddata)) 91 | goto exit; 92 | } 93 | if (flags.bits.username) 94 | { 95 | if (enddata - curdata < 3 || !readMQTTLenString(&data->username, &curdata, enddata)) 96 | goto exit; /* username flag set, but no username supplied - invalid */ 97 | if (flags.bits.password && 98 | (enddata - curdata < 3 || !readMQTTLenString(&data->password, &curdata, enddata))) 99 | goto exit; /* password flag set, but no password supplied - invalid */ 100 | } 101 | else if (flags.bits.password) 102 | goto exit; /* password flag set without username - invalid */ 103 | rc = 1; 104 | } 105 | exit: 106 | FUNC_EXIT_RC(rc); 107 | return rc; 108 | } 109 | 110 | 111 | /** 112 | * Serializes the connack packet into the supplied buffer. 113 | * @param buf the buffer into which the packet will be serialized 114 | * @param buflen the length in bytes of the supplied buffer 115 | * @param connack_rc the integer connack return code to be used 116 | * @param sessionPresent the MQTT 3.1.1 sessionPresent flag 117 | * @return serialized length, or error if 0 118 | */ 119 | int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent) 120 | { 121 | MQTTHeader header = {0}; 122 | int rc = 0; 123 | unsigned char *ptr = buf; 124 | MQTTConnackFlags flags = {0}; 125 | 126 | FUNC_ENTRY; 127 | if (buflen < 2) 128 | { 129 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 130 | goto exit; 131 | } 132 | header.byte = 0; 133 | header.bits.type = CONNACK; 134 | writeChar(&ptr, header.byte); /* write header */ 135 | 136 | ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */ 137 | 138 | flags.all = 0; 139 | flags.bits.sessionpresent = sessionPresent; 140 | writeChar(&ptr, flags.all); 141 | writeChar(&ptr, connack_rc); 142 | 143 | rc = ptr - buf; 144 | exit: 145 | FUNC_EXIT_RC(rc); 146 | return rc; 147 | } 148 | 149 | -------------------------------------------------------------------------------- /W5500/Internet/MQTT/MQTTPacket/src/MQTTDeserializePublish.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | *******************************************************************************/ 16 | 17 | #include "StackTrace.h" 18 | #include "MQTTPacket.h" 19 | #include 20 | 21 | #define min(a, b) ((a < b) ? 1 : 0) 22 | 23 | /** 24 | * Deserializes the supplied (wire) buffer into publish data 25 | * @param dup returned integer - the MQTT dup flag 26 | * @param qos returned integer - the MQTT QoS value 27 | * @param retained returned integer - the MQTT retained flag 28 | * @param packetid returned integer - the MQTT packet identifier 29 | * @param topicName returned MQTTString - the MQTT topic in the publish 30 | * @param payload returned byte buffer - the MQTT publish payload 31 | * @param payloadlen returned integer - the length of the MQTT payload 32 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 33 | * @param buflen the length in bytes of the data in the supplied buffer 34 | * @return error code. 1 is success 35 | */ 36 | int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName, 37 | unsigned char** payload, int* payloadlen, unsigned char* buf, int buflen) 38 | { 39 | MQTTHeader header = {0}; 40 | unsigned char* curdata = buf; 41 | unsigned char* enddata = NULL; 42 | int rc = 0; 43 | int mylen = 0; 44 | 45 | FUNC_ENTRY; 46 | header.byte = readChar(&curdata); 47 | if (header.bits.type != PUBLISH) 48 | goto exit; 49 | *dup = header.bits.dup; 50 | *qos = header.bits.qos; 51 | *retained = header.bits.retain; 52 | 53 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 54 | enddata = curdata + mylen; 55 | 56 | if (!readMQTTLenString(topicName, &curdata, enddata) || 57 | enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */ 58 | goto exit; 59 | 60 | if (*qos > 0) 61 | *packetid = readInt(&curdata); 62 | 63 | *payloadlen = enddata - curdata; 64 | *payload = curdata; 65 | rc = 1; 66 | exit: 67 | FUNC_EXIT_RC(rc); 68 | return rc; 69 | } 70 | 71 | 72 | 73 | /** 74 | * Deserializes the supplied (wire) buffer into an ack 75 | * @param packettype returned integer - the MQTT packet type 76 | * @param dup returned integer - the MQTT dup flag 77 | * @param packetid returned integer - the MQTT packet identifier 78 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 79 | * @param buflen the length in bytes of the data in the supplied buffer 80 | * @return error code. 1 is success, 0 is failure 81 | */ 82 | int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen) 83 | { 84 | MQTTHeader header = {0}; 85 | unsigned char* curdata = buf; 86 | unsigned char* enddata = NULL; 87 | int rc = 0; 88 | int mylen; 89 | 90 | FUNC_ENTRY; 91 | header.byte = readChar(&curdata); 92 | *dup = header.bits.dup; 93 | *packettype = header.bits.type; 94 | 95 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 96 | enddata = curdata + mylen; 97 | 98 | if (enddata - curdata < 2) 99 | goto exit; 100 | *packetid = readInt(&curdata); 101 | 102 | rc = 1; 103 | exit: 104 | FUNC_EXIT_RC(rc); 105 | return rc; 106 | } 107 | 108 | -------------------------------------------------------------------------------- /W5500/Internet/MQTT/MQTTPacket/src/MQTTFormat.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | *******************************************************************************/ 16 | 17 | #if !defined(MQTTFORMAT_H) 18 | #define MQTTFORMAT_H 19 | 20 | #include "StackTrace.h" 21 | #include "MQTTPacket.h" 22 | 23 | const char* MQTTPacket_getName(unsigned short packetid); 24 | int MQTTStringFormat_connect(char* strbuf, int strbuflen, MQTTPacket_connectData* data); 25 | int MQTTStringFormat_connack(char* strbuf, int strbuflen, unsigned char connack_rc, unsigned char sessionPresent); 26 | int MQTTStringFormat_publish(char* strbuf, int strbuflen, unsigned char dup, int qos, unsigned char retained, 27 | unsigned short packetid, MQTTString topicName, unsigned char* payload, int payloadlen); 28 | int MQTTStringFormat_ack(char* strbuf, int strbuflen, unsigned char packettype, unsigned char dup, unsigned short packetid); 29 | int MQTTStringFormat_subscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid, int count, 30 | MQTTString topicFilters[], int requestedQoSs[]); 31 | int MQTTStringFormat_suback(char* strbuf, int strbuflen, unsigned short packetid, int count, int* grantedQoSs); 32 | int MQTTStringFormat_unsubscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid, 33 | int count, MQTTString topicFilters[]); 34 | char* MQTTFormat_toClientString(char* strbuf, int strbuflen, unsigned char* buf, int buflen); 35 | char* MQTTFormat_toServerString(char* strbuf, int strbuflen, unsigned char* buf, int buflen); 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /W5500/Internet/MQTT/MQTTPacket/src/MQTTPacket.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Xiang Rong - 442039 Add makefile to Embedded C client 16 | *******************************************************************************/ 17 | 18 | #ifndef MQTTPACKET_H_ 19 | #define MQTTPACKET_H_ 20 | 21 | #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */ 22 | extern "C" { 23 | #endif 24 | 25 | #if defined(WIN32_DLL) || defined(WIN64_DLL) 26 | #define DLLImport __declspec(dllimport) 27 | #define DLLExport __declspec(dllexport) 28 | #elif defined(LINUX_SO) 29 | #define DLLImport extern 30 | #define DLLExport __attribute__ ((visibility ("default"))) 31 | #else 32 | #define DLLImport 33 | #define DLLExport 34 | #endif 35 | 36 | enum errors 37 | { 38 | MQTTPACKET_BUFFER_TOO_SHORT = -2, 39 | MQTTPACKET_READ_ERROR = -1, 40 | MQTTPACKET_READ_COMPLETE 41 | }; 42 | 43 | enum msgTypes 44 | { 45 | CONNECT = 1, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL, 46 | PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK, 47 | PINGREQ, PINGRESP, DISCONNECT 48 | }; 49 | 50 | /** 51 | * Bitfields for the MQTT header byte. 52 | */ 53 | typedef union 54 | { 55 | unsigned char byte; /**< the whole byte */ 56 | #if defined(REVERSED) 57 | struct 58 | { 59 | unsigned int type : 4; /**< message type nibble */ 60 | unsigned int dup : 1; /**< DUP flag bit */ 61 | unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */ 62 | unsigned int retain : 1; /**< retained flag bit */ 63 | } bits; 64 | #else 65 | struct 66 | { 67 | unsigned int retain : 1; /**< retained flag bit */ 68 | unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */ 69 | unsigned int dup : 1; /**< DUP flag bit */ 70 | unsigned int type : 4; /**< message type nibble */ 71 | } bits; 72 | #endif 73 | } MQTTHeader; 74 | 75 | typedef struct 76 | { 77 | int len; 78 | char* data; 79 | } MQTTLenString; 80 | 81 | typedef struct 82 | { 83 | char* cstring; 84 | MQTTLenString lenstring; 85 | } MQTTString; 86 | 87 | #define MQTTString_initializer {NULL, {0, NULL}} 88 | 89 | int MQTTstrlen(MQTTString mqttstring); 90 | 91 | #include "MQTTConnect.h" 92 | #include "MQTTPublish.h" 93 | #include "MQTTSubscribe.h" 94 | #include "MQTTUnsubscribe.h" 95 | #include "MQTTFormat.h" 96 | 97 | int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char type, unsigned char dup, unsigned short packetid); 98 | int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen); 99 | 100 | int MQTTPacket_len(int rem_len); 101 | int MQTTPacket_equals(MQTTString* a, char* b); 102 | 103 | int MQTTPacket_encode(unsigned char* buf, int length); 104 | int MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int* value); 105 | int MQTTPacket_decodeBuf(unsigned char* buf, int* value); 106 | 107 | int readInt(unsigned char** pptr); 108 | char readChar(unsigned char** pptr); 109 | void writeChar(unsigned char** pptr, char c); 110 | void writeInt(unsigned char** pptr, int anInt); 111 | int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata); 112 | void writeCString(unsigned char** pptr, const char* string); 113 | void writeMQTTString(unsigned char** pptr, MQTTString mqttstring); 114 | 115 | DLLExport int MQTTPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int)); 116 | 117 | typedef struct { 118 | int (*getfn)(void *, unsigned char*, int); /* must return -1 for error, 0 for call again, or the number of bytes read */ 119 | void *sck; /* pointer to whatever the system may use to identify the transport */ 120 | int multiplier; 121 | int rem_len; 122 | int len; 123 | char state; 124 | }MQTTTransport; 125 | 126 | int MQTTPacket_readnb(unsigned char* buf, int buflen, MQTTTransport *trp); 127 | 128 | #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */ 129 | } 130 | #endif 131 | 132 | 133 | #endif /* MQTTPACKET_H_ */ 134 | -------------------------------------------------------------------------------- /W5500/Internet/MQTT/MQTTPacket/src/MQTTPublish.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Xiang Rong - 442039 Add makefile to Embedded C client 16 | *******************************************************************************/ 17 | 18 | #ifndef MQTTPUBLISH_H_ 19 | #define MQTTPUBLISH_H_ 20 | 21 | #if !defined(DLLImport) 22 | #define DLLImport 23 | #endif 24 | #if !defined(DLLExport) 25 | #define DLLExport 26 | #endif 27 | 28 | DLLExport int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid, 29 | MQTTString topicName, unsigned char* payload, int payloadlen); 30 | 31 | DLLExport int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName, 32 | unsigned char** payload, int* payloadlen, unsigned char* buf, int len); 33 | 34 | DLLExport int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid); 35 | DLLExport int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid); 36 | DLLExport int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid); 37 | 38 | #endif /* MQTTPUBLISH_H_ */ 39 | -------------------------------------------------------------------------------- /W5500/Internet/MQTT/MQTTPacket/src/MQTTSerializePublish.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Ian Craggs - fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=453144 16 | *******************************************************************************/ 17 | 18 | #include "MQTTPacket.h" 19 | #include "StackTrace.h" 20 | 21 | #include 22 | 23 | 24 | /** 25 | * Determines the length of the MQTT publish packet that would be produced using the supplied parameters 26 | * @param qos the MQTT QoS of the publish (packetid is omitted for QoS 0) 27 | * @param topicName the topic name to be used in the publish 28 | * @param payloadlen the length of the payload to be sent 29 | * @return the length of buffer needed to contain the serialized version of the packet 30 | */ 31 | int MQTTSerialize_publishLength(int qos, MQTTString topicName, int payloadlen) 32 | { 33 | int len = 0; 34 | 35 | len += 2 + MQTTstrlen(topicName) + payloadlen; 36 | if (qos > 0) 37 | len += 2; /* packetid */ 38 | return len; 39 | } 40 | 41 | 42 | /** 43 | * Serializes the supplied publish data into the supplied buffer, ready for sending 44 | * @param buf the buffer into which the packet will be serialized 45 | * @param buflen the length in bytes of the supplied buffer 46 | * @param dup integer - the MQTT dup flag 47 | * @param qos integer - the MQTT QoS value 48 | * @param retained integer - the MQTT retained flag 49 | * @param packetid integer - the MQTT packet identifier 50 | * @param topicName MQTTString - the MQTT topic in the publish 51 | * @param payload byte buffer - the MQTT publish payload 52 | * @param payloadlen integer - the length of the MQTT payload 53 | * @return the length of the serialized data. <= 0 indicates error 54 | */ 55 | int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid, 56 | MQTTString topicName, unsigned char* payload, int payloadlen) 57 | { 58 | unsigned char *ptr = buf; 59 | MQTTHeader header = {0}; 60 | int rem_len = 0; 61 | int rc = 0; 62 | 63 | FUNC_ENTRY; 64 | if (MQTTPacket_len(rem_len = MQTTSerialize_publishLength(qos, topicName, payloadlen)) > buflen) 65 | { 66 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 67 | goto exit; 68 | } 69 | 70 | header.bits.type = PUBLISH; 71 | header.bits.dup = dup; 72 | header.bits.qos = qos; 73 | header.bits.retain = retained; 74 | writeChar(&ptr, header.byte); /* write header */ 75 | 76 | ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; 77 | 78 | writeMQTTString(&ptr, topicName); 79 | 80 | if (qos > 0) 81 | writeInt(&ptr, packetid); 82 | 83 | memcpy(ptr, payload, payloadlen); 84 | ptr += payloadlen; 85 | 86 | rc = ptr - buf; 87 | 88 | exit: 89 | FUNC_EXIT_RC(rc); 90 | return rc; 91 | } 92 | 93 | 94 | 95 | /** 96 | * Serializes the ack packet into the supplied buffer. 97 | * @param buf the buffer into which the packet will be serialized 98 | * @param buflen the length in bytes of the supplied buffer 99 | * @param type the MQTT packet type 100 | * @param dup the MQTT dup flag 101 | * @param packetid the MQTT packet identifier 102 | * @return serialized length, or error if 0 103 | */ 104 | int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid) 105 | { 106 | MQTTHeader header = {0}; 107 | int rc = 0; 108 | unsigned char *ptr = buf; 109 | 110 | FUNC_ENTRY; 111 | if (buflen < 4) 112 | { 113 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 114 | goto exit; 115 | } 116 | header.bits.type = packettype; 117 | header.bits.dup = dup; 118 | header.bits.qos = (packettype == PUBREL) ? 1 : 0; 119 | writeChar(&ptr, header.byte); /* write header */ 120 | 121 | ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */ 122 | writeInt(&ptr, packetid); 123 | rc = ptr - buf; 124 | exit: 125 | FUNC_EXIT_RC(rc); 126 | return rc; 127 | } 128 | 129 | 130 | /** 131 | * Serializes a puback packet into the supplied buffer. 132 | * @param buf the buffer into which the packet will be serialized 133 | * @param buflen the length in bytes of the supplied buffer 134 | * @param packetid integer - the MQTT packet identifier 135 | * @return serialized length, or error if 0 136 | */ 137 | int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid) 138 | { 139 | return MQTTSerialize_ack(buf, buflen, PUBACK, 0, packetid); 140 | } 141 | 142 | 143 | /** 144 | * Serializes a pubrel packet into the supplied buffer. 145 | * @param buf the buffer into which the packet will be serialized 146 | * @param buflen the length in bytes of the supplied buffer 147 | * @param dup integer - the MQTT dup flag 148 | * @param packetid integer - the MQTT packet identifier 149 | * @return serialized length, or error if 0 150 | */ 151 | int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid) 152 | { 153 | return MQTTSerialize_ack(buf, buflen, PUBREL, dup, packetid); 154 | } 155 | 156 | 157 | /** 158 | * Serializes a pubrel packet into the supplied buffer. 159 | * @param buf the buffer into which the packet will be serialized 160 | * @param buflen the length in bytes of the supplied buffer 161 | * @param packetid integer - the MQTT packet identifier 162 | * @return serialized length, or error if 0 163 | */ 164 | int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid) 165 | { 166 | return MQTTSerialize_ack(buf, buflen, PUBCOMP, 0, packetid); 167 | } 168 | 169 | 170 | -------------------------------------------------------------------------------- /W5500/Internet/MQTT/MQTTPacket/src/MQTTSubscribe.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Xiang Rong - 442039 Add makefile to Embedded C client 16 | *******************************************************************************/ 17 | 18 | #ifndef MQTTSUBSCRIBE_H_ 19 | #define MQTTSUBSCRIBE_H_ 20 | 21 | #if !defined(DLLImport) 22 | #define DLLImport 23 | #endif 24 | #if !defined(DLLExport) 25 | #define DLLExport 26 | #endif 27 | 28 | DLLExport int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, 29 | int count, MQTTString topicFilters[], char requestedQoSs[]); 30 | 31 | DLLExport int MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid, 32 | int maxcount, int* count, MQTTString topicFilters[], int requestedQoSs[], unsigned char* buf, int len); 33 | 34 | DLLExport int MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short packetid, int count, int* grantedQoSs); 35 | 36 | DLLExport int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], unsigned char* buf, int len); 37 | 38 | 39 | #endif /* MQTTSUBSCRIBE_H_ */ 40 | -------------------------------------------------------------------------------- /W5500/Internet/MQTT/MQTTPacket/src/MQTTSubscribeClient.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | *******************************************************************************/ 16 | 17 | #include "MQTTPacket.h" 18 | #include "StackTrace.h" 19 | 20 | #include 21 | 22 | /** 23 | * Determines the length of the MQTT subscribe packet that would be produced using the supplied parameters 24 | * @param count the number of topic filter strings in topicFilters 25 | * @param topicFilters the array of topic filter strings to be used in the publish 26 | * @return the length of buffer needed to contain the serialized version of the packet 27 | */ 28 | int MQTTSerialize_subscribeLength(int count, MQTTString topicFilters[]) 29 | { 30 | int i; 31 | int len = 2; /* packetid */ 32 | 33 | for (i = 0; i < count; ++i) 34 | len += 2 + MQTTstrlen(topicFilters[i]) + 1; /* length + topic + req_qos */ 35 | return len; 36 | } 37 | 38 | 39 | /** 40 | * Serializes the supplied subscribe data into the supplied buffer, ready for sending 41 | * @param buf the buffer into which the packet will be serialized 42 | * @param buflen the length in bytes of the supplied bufferr 43 | * @param dup integer - the MQTT dup flag 44 | * @param packetid integer - the MQTT packet identifier 45 | * @param count - number of members in the topicFilters and reqQos arrays 46 | * @param topicFilters - array of topic filter names 47 | * @param requestedQoSs - array of requested QoS 48 | * @return the length of the serialized data. <= 0 indicates error 49 | */ 50 | int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, int count, 51 | MQTTString topicFilters[], char requestedQoSs[]) 52 | { 53 | unsigned char *ptr = buf; 54 | MQTTHeader header = {0}; 55 | int rem_len = 0; 56 | int rc = 0; 57 | int i = 0; 58 | 59 | FUNC_ENTRY; 60 | if (MQTTPacket_len(rem_len = MQTTSerialize_subscribeLength(count, topicFilters)) > buflen) 61 | { 62 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 63 | goto exit; 64 | } 65 | 66 | header.byte = 0; 67 | header.bits.type = SUBSCRIBE; 68 | header.bits.dup = dup; 69 | header.bits.qos = 1; 70 | writeChar(&ptr, header.byte); /* write header */ 71 | 72 | ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; 73 | 74 | writeInt(&ptr, packetid); 75 | 76 | for (i = 0; i < count; ++i) 77 | { 78 | writeMQTTString(&ptr, topicFilters[i]); 79 | writeChar(&ptr, requestedQoSs[i]); 80 | } 81 | 82 | rc = ptr - buf; 83 | exit: 84 | FUNC_EXIT_RC(rc); 85 | return rc; 86 | } 87 | 88 | 89 | 90 | /** 91 | * Deserializes the supplied (wire) buffer into suback data 92 | * @param packetid returned integer - the MQTT packet identifier 93 | * @param maxcount - the maximum number of members allowed in the grantedQoSs array 94 | * @param count returned integer - number of members in the grantedQoSs array 95 | * @param grantedQoSs returned array of integers - the granted qualities of service 96 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 97 | * @param buflen the length in bytes of the data in the supplied buffer 98 | * @return error code. 1 is success, 0 is failure 99 | */ 100 | int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], unsigned char* buf, int buflen) 101 | { 102 | MQTTHeader header = {0}; 103 | unsigned char* curdata = buf; 104 | unsigned char* enddata = NULL; 105 | int rc = 0; 106 | int mylen; 107 | 108 | FUNC_ENTRY; 109 | header.byte = readChar(&curdata); 110 | if (header.bits.type != SUBACK) 111 | goto exit; 112 | 113 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 114 | enddata = curdata + mylen; 115 | if (enddata - curdata < 2) 116 | goto exit; 117 | 118 | *packetid = readInt(&curdata); 119 | 120 | *count = 0; 121 | while (curdata < enddata) 122 | { 123 | if (*count > maxcount) 124 | { 125 | rc = -1; 126 | goto exit; 127 | } 128 | grantedQoSs[(*count)++] = readChar(&curdata); 129 | } 130 | 131 | rc = 1; 132 | exit: 133 | FUNC_EXIT_RC(rc); 134 | return rc; 135 | } 136 | 137 | 138 | -------------------------------------------------------------------------------- /W5500/Internet/MQTT/MQTTPacket/src/MQTTSubscribeServer.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | *******************************************************************************/ 16 | 17 | #include "MQTTPacket.h" 18 | #include "StackTrace.h" 19 | 20 | #include 21 | 22 | 23 | /** 24 | * Deserializes the supplied (wire) buffer into subscribe data 25 | * @param dup integer returned - the MQTT dup flag 26 | * @param packetid integer returned - the MQTT packet identifier 27 | * @param maxcount - the maximum number of members allowed in the topicFilters and requestedQoSs arrays 28 | * @param count - number of members in the topicFilters and requestedQoSs arrays 29 | * @param topicFilters - array of topic filter names 30 | * @param requestedQoSs - array of requested QoS 31 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 32 | * @param buflen the length in bytes of the data in the supplied buffer 33 | * @return the length of the serialized data. <= 0 indicates error 34 | */ 35 | int MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[], 36 | int requestedQoSs[], unsigned char* buf, int buflen) 37 | { 38 | MQTTHeader header = {0}; 39 | unsigned char* curdata = buf; 40 | unsigned char* enddata = NULL; 41 | int rc = -1; 42 | int mylen = 0; 43 | 44 | FUNC_ENTRY; 45 | header.byte = readChar(&curdata); 46 | if (header.bits.type != SUBSCRIBE) 47 | goto exit; 48 | *dup = header.bits.dup; 49 | 50 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 51 | enddata = curdata + mylen; 52 | 53 | *packetid = readInt(&curdata); 54 | 55 | *count = 0; 56 | while (curdata < enddata) 57 | { 58 | if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata)) 59 | goto exit; 60 | if (curdata >= enddata) /* do we have enough data to read the req_qos version byte? */ 61 | goto exit; 62 | requestedQoSs[*count] = readChar(&curdata); 63 | (*count)++; 64 | } 65 | 66 | rc = 1; 67 | exit: 68 | FUNC_EXIT_RC(rc); 69 | return rc; 70 | } 71 | 72 | 73 | /** 74 | * Serializes the supplied suback data into the supplied buffer, ready for sending 75 | * @param buf the buffer into which the packet will be serialized 76 | * @param buflen the length in bytes of the supplied buffer 77 | * @param packetid integer - the MQTT packet identifier 78 | * @param count - number of members in the grantedQoSs array 79 | * @param grantedQoSs - array of granted QoS 80 | * @return the length of the serialized data. <= 0 indicates error 81 | */ 82 | int MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short packetid, int count, int* grantedQoSs) 83 | { 84 | MQTTHeader header = {0}; 85 | int rc = -1; 86 | unsigned char *ptr = buf; 87 | int i; 88 | 89 | FUNC_ENTRY; 90 | if (buflen < 2 + count) 91 | { 92 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 93 | goto exit; 94 | } 95 | header.byte = 0; 96 | header.bits.type = SUBACK; 97 | writeChar(&ptr, header.byte); /* write header */ 98 | 99 | ptr += MQTTPacket_encode(ptr, 2 + count); /* write remaining length */ 100 | 101 | writeInt(&ptr, packetid); 102 | 103 | for (i = 0; i < count; ++i) 104 | writeChar(&ptr, grantedQoSs[i]); 105 | 106 | rc = ptr - buf; 107 | exit: 108 | FUNC_EXIT_RC(rc); 109 | return rc; 110 | } 111 | 112 | 113 | -------------------------------------------------------------------------------- /W5500/Internet/MQTT/MQTTPacket/src/MQTTUnsubscribe.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Xiang Rong - 442039 Add makefile to Embedded C client 16 | *******************************************************************************/ 17 | 18 | #ifndef MQTTUNSUBSCRIBE_H_ 19 | #define MQTTUNSUBSCRIBE_H_ 20 | 21 | #if !defined(DLLImport) 22 | #define DLLImport 23 | #endif 24 | #if !defined(DLLExport) 25 | #define DLLExport 26 | #endif 27 | 28 | DLLExport int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, 29 | int count, MQTTString topicFilters[]); 30 | 31 | DLLExport int MQTTDeserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, int max_count, int* count, MQTTString topicFilters[], 32 | unsigned char* buf, int len); 33 | 34 | DLLExport int MQTTSerialize_unsuback(unsigned char* buf, int buflen, unsigned short packetid); 35 | 36 | DLLExport int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int len); 37 | 38 | #endif /* MQTTUNSUBSCRIBE_H_ */ 39 | -------------------------------------------------------------------------------- /W5500/Internet/MQTT/MQTTPacket/src/MQTTUnsubscribeClient.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | *******************************************************************************/ 16 | 17 | #include "MQTTPacket.h" 18 | #include "StackTrace.h" 19 | 20 | #include 21 | 22 | /** 23 | * Determines the length of the MQTT unsubscribe packet that would be produced using the supplied parameters 24 | * @param count the number of topic filter strings in topicFilters 25 | * @param topicFilters the array of topic filter strings to be used in the publish 26 | * @return the length of buffer needed to contain the serialized version of the packet 27 | */ 28 | int MQTTSerialize_unsubscribeLength(int count, MQTTString topicFilters[]) 29 | { 30 | int i; 31 | int len = 2; /* packetid */ 32 | 33 | for (i = 0; i < count; ++i) 34 | len += 2 + MQTTstrlen(topicFilters[i]); /* length + topic*/ 35 | return len; 36 | } 37 | 38 | 39 | /** 40 | * Serializes the supplied unsubscribe data into the supplied buffer, ready for sending 41 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 42 | * @param buflen the length in bytes of the data in the supplied buffer 43 | * @param dup integer - the MQTT dup flag 44 | * @param packetid integer - the MQTT packet identifier 45 | * @param count - number of members in the topicFilters array 46 | * @param topicFilters - array of topic filter names 47 | * @return the length of the serialized data. <= 0 indicates error 48 | */ 49 | int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, 50 | int count, MQTTString topicFilters[]) 51 | { 52 | unsigned char *ptr = buf; 53 | MQTTHeader header = {0}; 54 | int rem_len = 0; 55 | int rc = -1; 56 | int i = 0; 57 | 58 | FUNC_ENTRY; 59 | if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(count, topicFilters)) > buflen) 60 | { 61 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 62 | goto exit; 63 | } 64 | 65 | header.byte = 0; 66 | header.bits.type = UNSUBSCRIBE; 67 | header.bits.dup = dup; 68 | header.bits.qos = 1; 69 | writeChar(&ptr, header.byte); /* write header */ 70 | 71 | ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; 72 | 73 | writeInt(&ptr, packetid); 74 | 75 | for (i = 0; i < count; ++i) 76 | writeMQTTString(&ptr, topicFilters[i]); 77 | 78 | rc = ptr - buf; 79 | exit: 80 | FUNC_EXIT_RC(rc); 81 | return rc; 82 | } 83 | 84 | 85 | /** 86 | * Deserializes the supplied (wire) buffer into unsuback data 87 | * @param packetid returned integer - the MQTT packet identifier 88 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 89 | * @param buflen the length in bytes of the data in the supplied buffer 90 | * @return error code. 1 is success, 0 is failure 91 | */ 92 | int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int buflen) 93 | { 94 | unsigned char type = 0; 95 | unsigned char dup = 0; 96 | int rc = 0; 97 | 98 | FUNC_ENTRY; 99 | rc = MQTTDeserialize_ack(&type, &dup, packetid, buf, buflen); 100 | if (type == UNSUBACK) 101 | rc = 1; 102 | FUNC_EXIT_RC(rc); 103 | return rc; 104 | } 105 | 106 | 107 | -------------------------------------------------------------------------------- /W5500/Internet/MQTT/MQTTPacket/src/MQTTUnsubscribeServer.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | *******************************************************************************/ 16 | 17 | #include "MQTTPacket.h" 18 | #include "StackTrace.h" 19 | 20 | #include 21 | 22 | 23 | /** 24 | * Deserializes the supplied (wire) buffer into unsubscribe data 25 | * @param dup integer returned - the MQTT dup flag 26 | * @param packetid integer returned - the MQTT packet identifier 27 | * @param maxcount - the maximum number of members allowed in the topicFilters and requestedQoSs arrays 28 | * @param count - number of members in the topicFilters and requestedQoSs arrays 29 | * @param topicFilters - array of topic filter names 30 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 31 | * @param buflen the length in bytes of the data in the supplied buffer 32 | * @return the length of the serialized data. <= 0 indicates error 33 | */ 34 | int MQTTDeserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[], 35 | unsigned char* buf, int len) 36 | { 37 | MQTTHeader header = {0}; 38 | unsigned char* curdata = buf; 39 | unsigned char* enddata = NULL; 40 | int rc = 0; 41 | int mylen = 0; 42 | 43 | FUNC_ENTRY; 44 | header.byte = readChar(&curdata); 45 | if (header.bits.type != UNSUBSCRIBE) 46 | goto exit; 47 | *dup = header.bits.dup; 48 | 49 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 50 | enddata = curdata + mylen; 51 | 52 | *packetid = readInt(&curdata); 53 | 54 | *count = 0; 55 | while (curdata < enddata) 56 | { 57 | if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata)) 58 | goto exit; 59 | (*count)++; 60 | } 61 | 62 | rc = 1; 63 | exit: 64 | FUNC_EXIT_RC(rc); 65 | return rc; 66 | } 67 | 68 | 69 | /** 70 | * Serializes the supplied unsuback data into the supplied buffer, ready for sending 71 | * @param buf the buffer into which the packet will be serialized 72 | * @param buflen the length in bytes of the supplied buffer 73 | * @param packetid integer - the MQTT packet identifier 74 | * @return the length of the serialized data. <= 0 indicates error 75 | */ 76 | int MQTTSerialize_unsuback(unsigned char* buf, int buflen, unsigned short packetid) 77 | { 78 | MQTTHeader header = {0}; 79 | int rc = 0; 80 | unsigned char *ptr = buf; 81 | 82 | FUNC_ENTRY; 83 | if (buflen < 2) 84 | { 85 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 86 | goto exit; 87 | } 88 | header.byte = 0; 89 | header.bits.type = UNSUBACK; 90 | writeChar(&ptr, header.byte); /* write header */ 91 | 92 | ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */ 93 | 94 | writeInt(&ptr, packetid); 95 | 96 | rc = ptr - buf; 97 | exit: 98 | FUNC_EXIT_RC(rc); 99 | return rc; 100 | } 101 | 102 | 103 | -------------------------------------------------------------------------------- /W5500/Internet/MQTT/MQTTPacket/src/StackTrace.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Ian Craggs - fix for bug #434081 16 | *******************************************************************************/ 17 | 18 | #ifndef STACKTRACE_H_ 19 | #define STACKTRACE_H_ 20 | 21 | #include 22 | #define NOSTACKTRACE 1 23 | 24 | #if defined(NOSTACKTRACE) 25 | #define FUNC_ENTRY 26 | #define FUNC_ENTRY_NOLOG 27 | #define FUNC_ENTRY_MED 28 | #define FUNC_ENTRY_MAX 29 | #define FUNC_EXIT 30 | #define FUNC_EXIT_NOLOG 31 | #define FUNC_EXIT_MED 32 | #define FUNC_EXIT_MAX 33 | #define FUNC_EXIT_RC(x) 34 | #define FUNC_EXIT_MED_RC(x) 35 | #define FUNC_EXIT_MAX_RC(x) 36 | 37 | #else 38 | 39 | #if defined(WIN32) 40 | #define inline __inline 41 | #define FUNC_ENTRY StackTrace_entry(__FUNCTION__, __LINE__, TRACE_MINIMUM) 42 | #define FUNC_ENTRY_NOLOG StackTrace_entry(__FUNCTION__, __LINE__, -1) 43 | #define FUNC_ENTRY_MED StackTrace_entry(__FUNCTION__, __LINE__, TRACE_MEDIUM) 44 | #define FUNC_ENTRY_MAX StackTrace_entry(__FUNCTION__, __LINE__, TRACE_MAXIMUM) 45 | #define FUNC_EXIT StackTrace_exit(__FUNCTION__, __LINE__, NULL, TRACE_MINIMUM) 46 | #define FUNC_EXIT_NOLOG StackTrace_exit(__FUNCTION__, __LINE__, -1) 47 | #define FUNC_EXIT_MED StackTrace_exit(__FUNCTION__, __LINE__, NULL, TRACE_MEDIUM) 48 | #define FUNC_EXIT_MAX StackTrace_exit(__FUNCTION__, __LINE__, NULL, TRACE_MAXIMUM) 49 | #define FUNC_EXIT_RC(x) StackTrace_exit(__FUNCTION__, __LINE__, &x, TRACE_MINIMUM) 50 | #define FUNC_EXIT_MED_RC(x) StackTrace_exit(__FUNCTION__, __LINE__, &x, TRACE_MEDIUM) 51 | #define FUNC_EXIT_MAX_RC(x) StackTrace_exit(__FUNCTION__, __LINE__, &x, TRACE_MAXIMUM) 52 | #else 53 | #define FUNC_ENTRY StackTrace_entry(__func__, __LINE__, TRACE_MINIMUM) 54 | #define FUNC_ENTRY_NOLOG StackTrace_entry(__func__, __LINE__, -1) 55 | #define FUNC_ENTRY_MED StackTrace_entry(__func__, __LINE__, TRACE_MEDIUM) 56 | #define FUNC_ENTRY_MAX StackTrace_entry(__func__, __LINE__, TRACE_MAXIMUM) 57 | #define FUNC_EXIT StackTrace_exit(__func__, __LINE__, NULL, TRACE_MINIMUM) 58 | #define FUNC_EXIT_NOLOG StackTrace_exit(__func__, __LINE__, NULL, -1) 59 | #define FUNC_EXIT_MED StackTrace_exit(__func__, __LINE__, NULL, TRACE_MEDIUM) 60 | #define FUNC_EXIT_MAX StackTrace_exit(__func__, __LINE__, NULL, TRACE_MAXIMUM) 61 | #define FUNC_EXIT_RC(x) StackTrace_exit(__func__, __LINE__, &x, TRACE_MINIMUM) 62 | #define FUNC_EXIT_MED_RC(x) StackTrace_exit(__func__, __LINE__, &x, TRACE_MEDIUM) 63 | #define FUNC_EXIT_MAX_RC(x) StackTrace_exit(__func__, __LINE__, &x, TRACE_MAXIMUM) 64 | 65 | void StackTrace_entry(const char* name, int line, int trace); 66 | void StackTrace_exit(const char* name, int line, void* return_value, int trace); 67 | 68 | void StackTrace_printStack(FILE* dest); 69 | char* StackTrace_get(unsigned long); 70 | 71 | #endif 72 | 73 | #endif 74 | 75 | 76 | 77 | 78 | #endif /* STACKTRACE_H_ */ 79 | -------------------------------------------------------------------------------- /W5500/Internet/MQTT/mqtt_interface.c: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | //! \file mqtt_interface.c 3 | //! \brief Paho MQTT to WIZnet Chip interface implement file. 4 | //! \details The process of porting an interface to use paho MQTT. 5 | //! \version 1.0.0 6 | //! \date 2016/12/06 7 | //! \par Revision history 8 | //! <2016/12/06> 1st Release 9 | //! 10 | //! \author Peter Bang & Justin Kim 11 | //! \copyright 12 | //! 13 | //! Copyright (c) 2016, WIZnet Co., LTD. 14 | //! All rights reserved. 15 | //! 16 | //! Redistribution and use in source and binary forms, with or without 17 | //! modification, are permitted provided that the following conditions 18 | //! are met: 19 | //! 20 | //! * Redistributions of source code must retain the above copyright 21 | //! notice, this list of conditions and the following disclaimer. 22 | //! * Redistributions in binary form must reproduce the above copyright 23 | //! notice, this list of conditions and the following disclaimer in the 24 | //! documentation and/or other materials provided with the distribution. 25 | //! * Neither the name of the nor the names of its 26 | //! contributors may be used to endorse or promote products derived 27 | //! from this software without specific prior written permission. 28 | //! 29 | //! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 30 | //! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 | //! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 | //! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 33 | //! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 34 | //! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 35 | //! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 36 | //! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 37 | //! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 | //! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 39 | //! THE POSSIBILITY OF SUCH DAMAGE. 40 | // 41 | //***************************************************************************** 42 | 43 | #include "mqtt_interface.h" 44 | #include "wizchip_conf.h" 45 | #include "socket.h" 46 | 47 | unsigned long MilliTimer; 48 | 49 | /* 50 | * @brief MQTT MilliTimer handler 51 | * @note MUST BE register to your system 1m Tick timer handler. 52 | */ 53 | void MilliTimer_Handler(void) { 54 | MilliTimer++; 55 | } 56 | 57 | /* 58 | * @brief Timer Initialize 59 | * @param timer : pointer to a Timer structure 60 | * that contains the configuration information for the Timer. 61 | */ 62 | void TimerInit(Timer* timer) { 63 | timer->end_time = 0; 64 | } 65 | 66 | /* 67 | * @brief expired Timer 68 | * @param timer : pointer to a Timer structure 69 | * that contains the configuration information for the Timer. 70 | */ 71 | char TimerIsExpired(Timer* timer) { 72 | long left = timer->end_time - MilliTimer; 73 | return (left < 0); 74 | } 75 | 76 | /* 77 | * @brief Countdown millisecond Timer 78 | * @param timer : pointer to a Timer structure 79 | * that contains the configuration information for the Timer. 80 | * timeout : setting timeout millisecond. 81 | */ 82 | void TimerCountdownMS(Timer* timer, unsigned int timeout) { 83 | timer->end_time = MilliTimer + timeout; 84 | } 85 | 86 | /* 87 | * @brief Countdown second Timer 88 | * @param timer : pointer to a Timer structure 89 | * that contains the configuration information for the Timer. 90 | * timeout : setting timeout millisecond. 91 | */ 92 | void TimerCountdown(Timer* timer, unsigned int timeout) { 93 | timer->end_time = MilliTimer + (timeout * 1000); 94 | } 95 | 96 | /* 97 | * @brief left millisecond Timer 98 | * @param timer : pointer to a Timer structure 99 | * that contains the configuration information for the Timer. 100 | */ 101 | int TimerLeftMS(Timer* timer) { 102 | long left = timer->end_time - MilliTimer; 103 | return (left < 0) ? 0 : left; 104 | } 105 | 106 | /* 107 | * @brief New network setting 108 | * @param n : pointer to a Network structure 109 | * that contains the configuration information for the Network. 110 | * sn : socket number where x can be (0..7). 111 | * @retval None 112 | */ 113 | void NewNetwork(Network* n, int sn) { 114 | n->my_socket = sn; 115 | n->mqttread = w5x00_read; 116 | n->mqttwrite = w5x00_write; 117 | n->disconnect = w5x00_disconnect; 118 | } 119 | 120 | /* 121 | * @brief read function 122 | * @param n : pointer to a Network structure 123 | * that contains the configuration information for the Network. 124 | * buffer : pointer to a read buffer. 125 | * len : buffer length. 126 | * @retval received data length or SOCKERR code 127 | */ 128 | int w5x00_read(Network* n, unsigned char* buffer, int len, long time) 129 | { 130 | 131 | if((getSn_SR(n->my_socket) == SOCK_ESTABLISHED) && (getSn_RX_RSR(n->my_socket)>0)) 132 | return recv(n->my_socket, buffer, len); 133 | 134 | return SOCK_ERROR; 135 | } 136 | 137 | /* 138 | * @brief write function 139 | * @param n : pointer to a Network structure 140 | * that contains the configuration information for the Network. 141 | * buffer : pointer to a read buffer. 142 | * len : buffer length. 143 | * @retval length of data sent or SOCKERR code 144 | */ 145 | int w5x00_write(Network* n, unsigned char* buffer, int len, long time) 146 | { 147 | if(getSn_SR(n->my_socket) == SOCK_ESTABLISHED) 148 | return send(n->my_socket, buffer, len); 149 | 150 | return SOCK_ERROR; 151 | } 152 | 153 | /* 154 | * @brief disconnect function 155 | * @param n : pointer to a Network structure 156 | * that contains the configuration information for the Network. 157 | */ 158 | void w5x00_disconnect(Network* n) 159 | { 160 | disconnect(n->my_socket); 161 | } 162 | 163 | /* 164 | * @brief connect network function 165 | * @param n : pointer to a Network structure 166 | * that contains the configuration information for the Network. 167 | * ip : server iP. 168 | * port : server port. 169 | */ 170 | void ConnectNetwork(Network* n, uint8_t* ip, uint16_t port) 171 | { 172 | uint16_t myport = 12345; 173 | 174 | socket(n->my_socket,Sn_MR_TCP,myport,0); 175 | connect(n->my_socket,ip,port); 176 | } 177 | -------------------------------------------------------------------------------- /W5500/Internet/SNMP/snmp.h: -------------------------------------------------------------------------------- 1 | #ifndef _SNMP_H_ 2 | #define _SNMP_H_ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | // SNMP Debug Message (dump) Enable 9 | #define _SNMP_DEBUG_ 10 | 11 | #define PORT_SNMP_AGENT 161 12 | #define PORT_SNMP_TRAP 162 13 | 14 | #define SNMP_V1 0 15 | 16 | #define MAX_OID 12 17 | #define MAX_STRING 64 18 | #define MAX_SNMPMSG_LEN 512 19 | #define MAX_TRAPMSG_LEN 512 20 | 21 | // SNMP Error code 22 | #define SNMP_SUCCESS 0 23 | #define OID_NOT_FOUND -1 24 | #define TABLE_FULL -2 25 | #define ILLEGAL_LENGTH -3 26 | #define INVALID_ENTRY_ID -4 27 | #define INVALID_DATA_TYPE -5 28 | 29 | #define NO_SUCH_NAME 2 30 | #define BAD_VALUE 3 31 | 32 | // SNMPv1 Commands 33 | #define GET_REQUEST 0xa0 34 | #define GET_NEXT_REQUEST 0xa1 35 | #define GET_RESPONSE 0xa2 36 | #define SET_REQUEST 0xa3 37 | 38 | // Macros: SNMPv1 request validation checker 39 | #define VALID_REQUEST(x) ((x == GET_REQUEST) || (x == GET_NEXT_REQUEST) || (x == SET_REQUEST)) 40 | 41 | // SNMPv1 Return Types 42 | #define SNMPDTYPE_INTEGER 0x02 43 | #define SNMPDTYPE_OCTET_STRING 0x04 44 | #define SNMPDTYPE_NULL_ITEM 0x05 45 | #define SNMPDTYPE_OBJ_ID 0x06 46 | #define SNMPDTYPE_SEQUENCE 0x30 47 | #define SNMPDTYPE_SEQUENCE_OF SNMPDTYPE_SEQUENCE 48 | 49 | #define SNMPDTYPE_COUNTER 0x41 50 | #define SNMPDTYPE_GAUGE 0x42 51 | #define SNMPDTYPE_TIME_TICKS 0x43 52 | #define SNMPDTYPE_OPAQUE 0x44 53 | 54 | // SNMP Trap: Standard Trap Types (Generic) 55 | #define SNMPTRAP_COLDSTART 0x00 // Generic trap-type 0: Cold Start 56 | #define SNMPTRAP_WARMSTART 0x01 // Generic trap-type 1: Warm Start 57 | #define SNMPTRAP_LINKDOWN 0x02 // Generic trap-type 2: Link Down 58 | #define SNMPTRAP_LINKUP 0x03 // Generic trap-type 3: Link Up 59 | #define SNMPTRAP_AUTHENTICATION 0x04 // Generic trap-type 4: Authentication Failure 60 | #define SNMPTRAP_EGPNEIGHBORLOSS 0x05 // Generic trap-type 5: EGP Neighbor Loss 61 | 62 | // Macros 63 | #define COPY_SEGMENT(x) \ 64 | { \ 65 | request_msg.index += seglen; \ 66 | memcpy(&response_msg.buffer[response_msg.index], &request_msg.buffer[x.start], seglen ); \ 67 | response_msg.index += seglen; \ 68 | } 69 | 70 | #ifndef HTONL 71 | #define HTONL(x) \ 72 | ((((x) >> 24) & 0x000000ff) | \ 73 | (((x) >> 8) & 0x0000ff00) | \ 74 | (((x) << 8) & 0x00ff0000) | \ 75 | (((x) << 24) & 0xff000000)) 76 | #endif 77 | 78 | typedef struct { 79 | uint8_t oidlen; 80 | uint8_t oid[MAX_OID]; 81 | uint8_t dataType; 82 | uint8_t dataLen; 83 | union { 84 | uint8_t octetstring[MAX_STRING]; 85 | uint32_t intval; 86 | } u; 87 | void (*getfunction)(void *, uint8_t *); 88 | void (*setfunction)(int32_t); 89 | } dataEntryType; 90 | 91 | struct messageStruct { 92 | uint8_t buffer[MAX_SNMPMSG_LEN]; 93 | int32_t len; 94 | int32_t index; 95 | }; 96 | 97 | typedef struct { 98 | int32_t start; /* Absolute Index of the TLV */ 99 | int32_t len; /* The L value of the TLV */ 100 | int32_t vstart; /* Absolute Index of this TLV's Value */ 101 | int32_t nstart; /* Absolute Index of the next TLV */ 102 | } tlvStructType; 103 | 104 | 105 | /********************************************************************************************/ 106 | /* SNMP : Functions */ 107 | /********************************************************************************************/ 108 | // SNMP Main functions 109 | void snmpd_init(uint8_t * managerIP, uint8_t * agentIP, uint8_t sn_agent, uint8_t sn_trap); 110 | int32_t snmpd_run(void); 111 | int32_t snmp_sendTrap(uint8_t * managerIP, uint8_t * agentIP, int8_t* community, dataEntryType enterprise_oid, uint32_t genericTrap, uint32_t specificTrap, uint32_t va_count, ...); 112 | 113 | // SNMP Time handler functions 114 | void SNMP_time_handler(void); 115 | uint32_t getSNMPTimeTick(void); 116 | void currentUptime(void *ptr, uint8_t *len); 117 | 118 | #ifdef __cplusplus 119 | } 120 | #endif 121 | 122 | #endif 123 | -------------------------------------------------------------------------------- /W5500/Internet/SNMP/snmp_custom.c: -------------------------------------------------------------------------------- 1 | 2 | /******************************************************************************************** 3 | * SNMP : User Customization Part 4 | * - OID Registration 5 | * - User defined functions for OID related 6 | * + Integer value, String 7 | * + I/O control / Chip registers 8 | * + Network Informations 9 | * + Etc. 10 | * 11 | *********************************************************************************************/ 12 | #include "snmp_custom.h" 13 | 14 | #ifdef _USE_WIZNET_W5500_EVB_ 15 | #include "board.h" 16 | #endif 17 | 18 | dataEntryType snmpData[] = 19 | { 20 | // System MIB 21 | // SysDescr Entry 22 | {8, {0x2b, 6, 1, 2, 1, 1, 1, 0}, 23 | SNMPDTYPE_OCTET_STRING, 30, {"WIZnet Embedded SNMP Agent"}, 24 | NULL, NULL}, 25 | 26 | // SysObjectID Entry 27 | {8, {0x2b, 6, 1, 2, 1, 1, 2, 0}, 28 | SNMPDTYPE_OBJ_ID, 8, {"\x2b\x06\x01\x02\x01\x01\x02\x00"}, 29 | NULL, NULL}, 30 | 31 | // SysUptime Entry 32 | {8, {0x2b, 6, 1, 2, 1, 1, 3, 0}, 33 | SNMPDTYPE_TIME_TICKS, 0, {""}, 34 | currentUptime, NULL}, 35 | 36 | // sysContact Entry 37 | {8, {0x2b, 6, 1, 2, 1, 1, 4, 0}, 38 | SNMPDTYPE_OCTET_STRING, 30, {"http://www.wizwiki.net/forum"}, 39 | NULL, NULL}, 40 | 41 | // sysName Entry 42 | {8, {0x2b, 6, 1, 2, 1, 1, 5, 0}, 43 | SNMPDTYPE_OCTET_STRING, 30, {"http://www.wiznet.co.kr"}, 44 | NULL, NULL}, 45 | 46 | // Location Entry 47 | {8, {0x2b, 6, 1, 2, 1, 1, 6, 0}, 48 | SNMPDTYPE_OCTET_STRING, 30, {"4F Humax Village"}, 49 | NULL, NULL}, 50 | 51 | // SysServices 52 | {8, {0x2b, 6, 1, 2, 1, 1, 7, 0}, 53 | SNMPDTYPE_INTEGER, 4, {""}, 54 | NULL, NULL}, 55 | 56 | #ifdef _USE_WIZNET_W5500_EVB_ 57 | // Get the WIZnet W5500-EVB LED Status 58 | {8, {0x2b, 6, 1, 4, 1, 6, 1, 0}, 59 | SNMPDTYPE_OCTET_STRING, 40, {""}, 60 | get_LEDStatus_all, NULL}, 61 | 62 | // Set the LED_R (RGB LED) 63 | {8, {0x2b, 6, 1, 4, 1, 6, 1, 1}, 64 | SNMPDTYPE_INTEGER, 4, {""}, 65 | NULL, set_LEDStatus_R}, 66 | 67 | // Set the LED_G (RGB LED) 68 | {8, {0x2b, 6, 1, 4, 1, 6, 1, 2}, 69 | SNMPDTYPE_INTEGER, 4, {""}, 70 | NULL, set_LEDStatus_G}, 71 | 72 | // Set the LED_B (RGB LED) 73 | {8, {0x2b, 6, 1, 4, 1, 6, 1, 3}, 74 | SNMPDTYPE_INTEGER, 4, {""}, 75 | NULL, set_LEDStatus_B}, 76 | #endif 77 | 78 | // OID Test #1 (long-length OID example, 19865) 79 | {0x0a, {0x2b, 0x06, 0x01, 0x04, 0x01, 0x81, 0x9b, 0x19, 0x01, 0x00}, 80 | SNMPDTYPE_OCTET_STRING, 30, {"long-length OID Test #1"}, 81 | NULL, NULL}, 82 | 83 | // OID Test #2 (long-length OID example, 22210) 84 | {0x0a, {0x2b, 0x06, 0x01, 0x04, 0x01, 0x81, 0xad, 0x42, 0x01, 0x00}, 85 | SNMPDTYPE_OCTET_STRING, 35, {"long-length OID Test #2"}, 86 | NULL, NULL}, 87 | 88 | // OID Test #2: SysObjectID Entry 89 | {0x0a, {0x2b, 0x06, 0x01, 0x04, 0x01, 0x81, 0xad, 0x42, 0x02, 0x00}, 90 | SNMPDTYPE_OBJ_ID, 0x0a, {"\x2b\x06\x01\x04\x01\x81\xad\x42\x02\x00"}, 91 | NULL, NULL}, 92 | }; 93 | 94 | const int32_t maxData = (sizeof(snmpData) / sizeof(dataEntryType)); 95 | 96 | void initTable() 97 | { 98 | // Example integer value for [OID 1.3.6.1.2.1.1.7.0] 99 | snmpData[6].u.intval = -5; 100 | 101 | } 102 | 103 | 104 | // W5500-EVB: LED Control /////////////////////////////////////////////////////////////////////////// 105 | #ifdef _USE_WIZNET_W5500_EVB_ 106 | void get_LEDStatus_all(void *ptr, uint8_t *len) 107 | { 108 | uint8_t led_status[3] = {0, }; 109 | 110 | led_status[LED_R] = (uint8_t)Board_LED_Test(LED_R); 111 | led_status[LED_G] = (uint8_t)Board_LED_Test(LED_G); 112 | led_status[LED_B] = (uint8_t)Board_LED_Test(LED_B); 113 | 114 | *len = sprintf((char *)ptr, "LED R [%s] / G [%s] / B [%s]", led_status[LED_R]?"On":"Off", led_status[LED_G]?"On":"Off", led_status[LED_B]?"On":"Off"); 115 | } 116 | 117 | void set_LEDStatus_R(int32_t val) 118 | { 119 | if(val == 0) Board_LED_Set(LED_R, false); 120 | else Board_LED_Set(LED_R, true); 121 | } 122 | 123 | void set_LEDStatus_G(int32_t val) 124 | { 125 | if(val == 0) Board_LED_Set(LED_G, false); 126 | else Board_LED_Set(LED_G, true); 127 | } 128 | 129 | void set_LEDStatus_B(int32_t val) 130 | { 131 | if(val == 0) Board_LED_Set(LED_B, false); 132 | else Board_LED_Set(LED_B, true); 133 | } 134 | #endif 135 | ///////////////////////////////////////////////////////////////////////////////////////////////////// 136 | 137 | void initial_Trap(uint8_t * managerIP, uint8_t * agentIP) 138 | { 139 | // SNMP Trap: WarmStart(1) Trap 140 | { 141 | dataEntryType enterprise_oid = {0x0a, {0x2b, 0x06, 0x01, 0x04, 0x01, 0x81, 0x9b, 0x19, 0x01, 0x00}, 142 | SNMPDTYPE_OBJ_ID, 0x0a, {"\x2b\x06\x01\x04\x01\x81\x9b\x19\x10\x00"}, NULL, NULL}; 143 | // Generic Trap: warmStart COMMUNITY 144 | snmp_sendTrap(managerIP, agentIP, (void *)COMMUNITY, enterprise_oid, SNMPTRAP_WARMSTART, 0, 0); 145 | } 146 | 147 | } 148 | -------------------------------------------------------------------------------- /W5500/Internet/SNMP/snmp_custom.h: -------------------------------------------------------------------------------- 1 | #ifndef _SNMP_CUSTOM_H_ 2 | #define _SNMP_CUSTOM_H_ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include "snmp.h" 14 | 15 | extern dataEntryType snmpData[]; 16 | extern const int32_t maxData; 17 | 18 | // Define for using W5500-EVB: H/W Dependency (e.g., LEDs...) 19 | //#define _USE_WIZNET_W5500_EVB_ 20 | 21 | #define COMMUNITY "public\0" 22 | #define COMMUNITY_SIZE (strlen(COMMUNITY)) 23 | 24 | /* Predefined function: Response value control */ 25 | void initTable(); 26 | 27 | /* User defined functions: LED control examples */ 28 | #ifdef _USE_WIZNET_W5500_EVB_ 29 | void get_LEDStatus_all(void *ptr, uint8_t *len); 30 | void set_LEDStatus_R(int32_t val); 31 | void set_LEDStatus_G(int32_t val); 32 | void set_LEDStatus_B(int32_t val); 33 | #endif 34 | /* SNMP Trap: warmStart(1) */ 35 | void initial_Trap(uint8_t * managerIP, uint8_t * agentIP); 36 | 37 | #ifdef __cplusplus 38 | } 39 | #endif 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /W5500/Internet/SNMP/tools/OID_Converter/Readme.txt: -------------------------------------------------------------------------------- 1 | ============================================================ 2 | 3 | ============================================================ 4 | 5 | + Example OID for convert 6 | 1.3.6.1.4.1.19865.1.0 7 | 8 | (1) Convert Usage 9 | CMD>>oid 1.3.6.1.4.1.19865.1.0 10 | => 06 0A 2B 06 01 04 01 81 9B 19 01 00 11 | 12 | >> TLV(tag-length-value) Example OID 13 | (06) Tag 14 | (0A) Length 15 | [2B] 1(iso).3(identified-organization) (in ASN.1 BER encoding, i.e. 1*40+3 = 0x2b) 16 | [06] 6(dod) 17 | [01] 1(internet) 18 | [04] 4(private) 19 | [01] 1(enterprise) 20 | [81 9B 19] 19865(Vendor-Specific) 21 | ... 22 | 23 | (2) Add the entry to OID table in source code (DataEntryType, put the converted value to array) 24 | {0x0a, {0x2b, 0x06, 0x01, 0x04, 0x01, 0x81, 0x9b, 0x19, 0x01, 0x00}, 25 | SNMPDTYPE_OCTET_STRING, 30, {"long-length OID Test"}, 26 | NULL, NULL}, 27 | 28 | ============================================================ 29 | OID Converter: OID encoder/decoder 30 | v1.3 - Matthias Gaertner 1999/2001 - Freeware 31 | ============================================================ 32 | 33 | The OID converter is a handy little tool to convert ASN.1 OIDs from readable dotted decimal notation to binary hexadecimal Distinguished Encoding Rules (DER) representation and vice versa. 34 | If you're into x.509 certificates, this may be useful to you, too. 35 | 36 | Usage: 37 | OID [-c|-C] [-o] {-i|1.2.3.4} 38 | converts dotted form to ASCII HEX DER output. 39 | OID -x [-o] {-i|hex-digits} 40 | decodes ASCII HEX DER and gives dotted form. 41 | 42 | If you need more information, please refer to Matthias Gaertner's page, 43 | http://www.rtner.de/software/oid.html 44 | 45 | =============================================================================================== -------------------------------------------------------------------------------- /W5500/Internet/SNMP/tools/net-snmp-5.7(win32-bin)/snmptrapd.conf: -------------------------------------------------------------------------------- 1 | # 2 | # net-snmp (or ucd-snmp) persistent data file. 3 | # 4 | ############################################################################ 5 | # STOP STOP STOP STOP STOP STOP STOP STOP STOP 6 | # 7 | # **** DO NOT EDIT THIS FILE **** 8 | # 9 | # STOP STOP STOP STOP STOP STOP STOP STOP STOP 10 | ############################################################################ 11 | # 12 | # DO NOT STORE CONFIGURATION ENTRIES HERE. 13 | # Please save normal configuration tokens for snmptrapd in SNMPCONFPATH/snmptrapd.conf. 14 | # Only "createUser" tokens should be placed here by snmptrapd administrators. 15 | # (Did I mention: do not edit this file?) 16 | # 17 | 18 | 19 | authCommunity log,execute,net public 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | engineBoots 1 33 | oldEngineID 0x80001f88803d6f00001ba7934e00000000 34 | -------------------------------------------------------------------------------- /W5500/Internet/SNTP/sntp.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/W5500/Internet/SNTP/sntp.c -------------------------------------------------------------------------------- /W5500/Internet/SNTP/sntp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * sntp.h 3 | * 4 | * Created on: 2014. 12. 15. 5 | * Author: Administrator 6 | */ 7 | 8 | #ifndef SNTP_H_ 9 | #define SNTP_H_ 10 | 11 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | #include 16 | 17 | /* 18 | * @brief Define it for Debug & Monitor DNS processing. 19 | * @note If defined, it dependens on 20 | */ 21 | //#define _SNTP_DEBUG_ 22 | 23 | #define MAX_SNTP_BUF_SIZE sizeof(ntpformat) ///< maximum size of DNS buffer. */ 24 | 25 | /* for ntpclient */ 26 | typedef signed char s_char; 27 | typedef unsigned long long tstamp; 28 | typedef unsigned int tdist; 29 | 30 | typedef struct _ntpformat 31 | { 32 | 33 | uint8_t dstaddr[4]; /* destination (local) address */ 34 | char version; /* version number */ 35 | char leap; /* leap indicator */ 36 | char mode; /* mode */ 37 | char stratum; /* stratum */ 38 | char poll; /* poll interval */ 39 | s_char precision; /* precision */ 40 | tdist rootdelay; /* root delay */ 41 | tdist rootdisp; /* root dispersion */ 42 | char refid; /* reference ID */ 43 | tstamp reftime; /* reference time */ 44 | tstamp org; /* origin timestamp */ 45 | tstamp rec; /* receive timestamp */ 46 | tstamp xmt; /* transmit timestamp */ 47 | 48 | 49 | } ntpformat; 50 | 51 | typedef struct _datetime 52 | { 53 | uint16_t yy; 54 | uint8_t mo; 55 | uint8_t dd; 56 | uint8_t hh; 57 | uint8_t mm; 58 | uint8_t ss; 59 | } datetime; 60 | 61 | #define ntp_port 123 //ntp server port number 62 | #define SECS_PERDAY 86400UL // seconds in a day = 60*60*24 63 | #define UTC_ADJ_HRS 9 // SEOUL : GMT+9 64 | #define EPOCH 1900 // NTP start year 65 | 66 | void get_seconds_from_ntp_server(uint8_t *buf, uint16_t idx); 67 | void SNTP_init(uint8_t s, uint8_t *ntp_server, uint8_t tz, uint8_t *buf); 68 | int8_t SNTP_run(datetime *time); 69 | tstamp changedatetime_to_seconds(void); 70 | void calcdatetime(tstamp seconds); 71 | 72 | #ifdef __cplusplus 73 | } 74 | #endif 75 | 76 | #endif /* SNTP_H_ */ 77 | -------------------------------------------------------------------------------- /W5500/Internet/TFTP/netutil.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "netutil.h" 5 | 6 | /** 7 | * Convert a 32bit Address into a Dotted Decimal Format string. 8 | * 9 | * @param addr 32bit address. 10 | * @return Dotted Decimal Format string. 11 | */ 12 | int8_t* inet_ntoa(uint32_t addr) 13 | { 14 | static int8_t addr_str[16]; 15 | memset(addr_str,0,16); 16 | sprintf((char*)addr_str,"%d.%d.%d.%d",(int32_t)(addr>>24 & 0xFF),(int32_t)(addr>>16 & 0xFF),(int32_t)(addr>>8 & 0xFF),(int32_t)(addr & 0xFF)); 17 | return addr_str; 18 | } 19 | 20 | /** 21 | * Convert a 32bit Address into a Dotted Decimal Format string. 22 | * This is differ from inet_ntoa in fixed length. 23 | * 24 | * @param addr 32bit address. 25 | * @return Dotted Decimal Format string. 26 | */ 27 | int8_t* inet_ntoa_pad(uint32_t addr) 28 | { 29 | static int8_t addr_str[16]; 30 | memset(addr_str,0,16); 31 | sprintf((char*)addr_str,"%03d.%03d.%03d.%03d",(int32_t)(addr>>24 & 0xFF),(int32_t)(addr>>16 & 0xFF),(int32_t)(addr>>8 & 0xFF),(int32_t)(addr & 0xFF)); 32 | return addr_str; 33 | } 34 | 35 | /** 36 | * Converts a string containing an (Ipv4) Internet Protocol decimal dotted address into a 32bit address. 37 | * 38 | * @param addr Dotted Decimal Format string. 39 | * @return 32bit address. 40 | */ 41 | uint32_t inet_addr(uint8_t* addr) 42 | { 43 | int8_t i; 44 | uint32_t inetaddr = 0; 45 | int8_t taddr[30]; 46 | int8_t * nexttok; 47 | int32_t num; 48 | strcpy((char*)taddr,(char*)addr); 49 | 50 | nexttok = taddr; 51 | for(i = 0; i < 4 ; i++) 52 | { 53 | nexttok = (int8_t*)strtok((char*)nexttok,"."); 54 | if(nexttok[0] == '0' && nexttok[1] == 'x') num = strtol((char*)nexttok+2, NULL, 16); 55 | else num = strtol((char*)nexttok, NULL, 10); 56 | inetaddr = inetaddr << 8; 57 | inetaddr |= (num & 0xFF); 58 | nexttok = NULL; 59 | } 60 | return inetaddr; 61 | } 62 | 63 | /** 64 | * Swap the byte order of 16bit(short) wide variable. 65 | * 66 | * @param i 16bit value to swap 67 | * @return Swapped value 68 | */ 69 | uint16_t swaps(uint16_t i) 70 | { 71 | uint16_t ret=0; 72 | ret = (i & 0xFF) << 8; 73 | ret |= ((i >> 8)& 0xFF); 74 | return ret; 75 | } 76 | 77 | /** 78 | * Swap the byte order of 32bit(long) wide variable. 79 | * 80 | * @param l 32bit value to convert 81 | * @return Swapped value 82 | */ 83 | uint32_t swapl(uint32_t l) 84 | { 85 | uint32_t ret=0; 86 | ret = (l & 0xFF) << 24; 87 | ret |= ((l >> 8) & 0xFF) << 16; 88 | ret |= ((l >> 16) & 0xFF) << 8; 89 | ret |= ((l >> 24) & 0xFF); 90 | return ret; 91 | } 92 | 93 | /** 94 | * htons function converts a unsigned short from host to TCP/IP network byte order (which is big-endian). 95 | * 96 | * @param hostshort The value to convert. 97 | * @return The value in TCP/IP network byte order. 98 | */ 99 | uint16_t htons(uint16_t hostshort) 100 | { 101 | #ifdef SYSTEM_LITTLE_ENDIAN 102 | return swaps(hostshort); 103 | #else 104 | return hostshort; 105 | #endif 106 | } 107 | 108 | 109 | /** 110 | * htonl function converts a unsigned long from host to TCP/IP network byte order (which is big-endian). 111 | * 112 | * @param hostlong The value to convert. 113 | * @return The value in TCP/IP network byte order. 114 | */ 115 | uint32_t htonl(uint32_t hostlong) 116 | { 117 | #ifdef SYSTEM_LITTLE_ENDIAN 118 | return swapl(hostlong); 119 | #else 120 | return hostlong; 121 | #endif 122 | } 123 | 124 | 125 | /** 126 | * ntohs function converts a unsigned short from TCP/IP network byte order 127 | * to host byte order (which is little-endian on Intel processors). 128 | * 129 | * @param netshort The value to convert. 130 | * @return A 16-bit number in host byte order 131 | */ 132 | uint32_t ntohs(uint16_t netshort) 133 | { 134 | #ifdef SYSTEM_LITTLE_ENDIAN 135 | return htons(netshort); 136 | #else 137 | return netshort; 138 | #endif 139 | } 140 | 141 | /** 142 | * converts a unsigned long from TCP/IP network byte order to host byte order 143 | * (which is little-endian on Intel processors). 144 | * 145 | * @param netlong The value to convert. 146 | * @return A 16-bit number in host byte order 147 | */ 148 | uint32_t ntohl(uint32_t netlong) 149 | { 150 | #ifdef SYSTEM_LITTLE_ENDIAN 151 | return swapl(netlong); 152 | #else 153 | return netlong; 154 | #endif 155 | } 156 | /** 157 | * @} 158 | */ 159 | -------------------------------------------------------------------------------- /W5500/Internet/TFTP/netutil.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __NETUTIL_H__ 3 | #define __NETUTIL_H__ 4 | 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | #include 10 | 11 | #define SYSTEM_LITTLE_ENDIAN 12 | 13 | int8_t* inet_ntoa(uint32_t addr); 14 | int8_t* inet_ntoa_pad(uint32_t addr); 15 | uint32_t inet_addr(uint8_t* addr); 16 | uint16_t swaps(uint16_t i); 17 | uint32_t swapl(uint32_t l); 18 | uint16_t htons(uint16_t hostshort); 19 | uint32_t htonl(uint32_t hostlong); 20 | uint32_t ntohs(uint16_t netshort); 21 | uint32_t ntohl(uint32_t netlong); 22 | 23 | #ifdef __cplusplus 24 | } 25 | #endif 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /W5500/Internet/TFTP/tftp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file tftp.h 3 | * @brief TFTP Header File. 4 | * @version 0.1.0 5 | * @author Sang-sik Kim 6 | */ 7 | #ifndef __TFTP_H__ 8 | #define __TFTP_H__ 9 | 10 | #ifdef __cplusplus 11 | extern "C" { 12 | #endif 13 | 14 | #include 15 | 16 | #define F_APP_TFTP 17 | #define __TFTP_DEBUG__ 18 | 19 | #define F_STORAGE // If your target support a storage, you have to activate this feature and implement. 20 | 21 | #define SOCK_TFTP 1 22 | 23 | #define INFO_DBG 0x01 24 | #define ERROR_DBG 0x02 25 | #define DEBUG_DBG 0x04 26 | #define IPC_DBG 0x08 27 | 28 | #define DBG_PRINT(level, format, args...) { \ 29 | if(dbg_level & level) \ 30 | printf(format, ##args); \ 31 | } 32 | 33 | #define NORMAL_MODE 0 34 | #define TFTP_MODE 1 35 | 36 | extern int dbg_level; 37 | 38 | /* tftp message */ 39 | #define TFTP_RRQ 1 40 | #define TFTP_WRQ 2 41 | #define TFTP_DATA 3 42 | #define TFTP_ACK 4 43 | #define TFTP_ERROR 5 44 | #define TFTP_OACK 6 45 | 46 | /* tftp state */ 47 | #define STATE_NONE 0 48 | #define STATE_RRQ 1 49 | #define STATE_WRQ 2 50 | #define STATE_DATA 3 51 | #define STATE_ACK 4 52 | #define STATE_OACK 5 53 | 54 | /* tftp transfer mode */ 55 | #define TRANS_ASCII "netascii" 56 | #define TRANS_BINARY "octet" 57 | 58 | /* tftp progress state */ 59 | #define TFTP_PROGRESS 0 60 | #define TFTP_FAIL 1 61 | #define TFTP_SUCCESS 2 62 | 63 | /* define */ 64 | #define TFTP_SERVER_PORT 69 65 | #define TFTP_TEMP_PORT 51000 66 | #define TFTP_BLK_SIZE 512 67 | #define MAX_MTU_SIZE 1514 68 | #define FILE_NAME_SIZE 20 69 | 70 | //#define __TFTP_DEBUG__ 71 | 72 | /* typedef */ 73 | typedef struct tftp_data { 74 | uint16_t opcode; 75 | uint16_t block_num; 76 | uint8_t data[0]; 77 | } TFTP_DATA_T; 78 | 79 | typedef struct tftp_error { 80 | uint16_t opcode; 81 | uint16_t error_code; 82 | uint8_t error_msg[0]; 83 | } TFTP_ERROR_T; 84 | 85 | typedef struct tftp_option { 86 | uint8_t *code; 87 | uint8_t *value; 88 | } TFTP_OPTION; 89 | 90 | /* Functions */ 91 | void TFTP_init(uint8_t socket, uint8_t *buf); 92 | void TFTP_exit(void); 93 | int TFTP_run(void); 94 | void TFTP_read_request(uint32_t server_ip, uint8_t *filename); 95 | void tftp_timeout_handler(void); 96 | 97 | #ifdef __cplusplus 98 | } 99 | #endif 100 | 101 | #endif /*__TFTP_H__ */ 102 | -------------------------------------------------------------------------------- /W5500/Internet/httpServer/httpParser.h: -------------------------------------------------------------------------------- 1 | /** 2 | @file httpd.h 3 | @brief Define Constants and fucntions associated with HTTP protocol. 4 | */ 5 | 6 | #include 7 | 8 | #ifndef __HTTPPARSER_H__ 9 | #define __HTTPPARSER_H__ 10 | 11 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | //#define _HTTPPARSER_DEBUG_ 16 | 17 | #define HTTP_SERVER_PORT 80 /**< HTTP server well-known port number */ 18 | 19 | /* HTTP Method */ 20 | #define METHOD_ERR 0 /**< Error Method. */ 21 | #define METHOD_GET 1 /**< GET Method. */ 22 | #define METHOD_HEAD 2 /**< HEAD Method. */ 23 | #define METHOD_POST 3 /**< POST Method. */ 24 | 25 | /* HTTP GET Method */ 26 | #define PTYPE_ERR 0 /**< Error file. */ 27 | #define PTYPE_HTML 1 /**< HTML file. */ 28 | #define PTYPE_GIF 2 /**< GIF file. */ 29 | #define PTYPE_TEXT 3 /**< TEXT file. */ 30 | #define PTYPE_JPEG 4 /**< JPEG file. */ 31 | #define PTYPE_FLASH 5 /**< FLASH file. */ 32 | #define PTYPE_MPEG 6 /**< MPEG file. */ 33 | #define PTYPE_PDF 7 /**< PDF file. */ 34 | #define PTYPE_CGI 8 /**< CGI file. */ 35 | #define PTYPE_XML 9 /**< XML file. */ 36 | #define PTYPE_CSS 10 /**< CSS file. */ 37 | #define PTYPE_JS 11 /**< JavaScript file. */ 38 | #define PTYPE_JSON 12 /**< JSON (JavaScript Standard Object Notation) file. */ 39 | #define PTYPE_PNG 13 /**< PNG file. */ 40 | #define PTYPE_ICO 14 /**< ICON file. */ 41 | 42 | #define PTYPE_TTF 20 /**< Font type: TTF file. */ 43 | #define PTYPE_OTF 21 /**< Font type: OTF file. */ 44 | #define PTYPE_WOFF 22 /**< Font type: WOFF file. */ 45 | #define PTYPE_EOT 23 /**< Font type: EOT file. */ 46 | #define PTYPE_SVG 24 /**< Font type: SVG file. */ 47 | 48 | 49 | /* HTTP response */ 50 | #define STATUS_OK 200 51 | #define STATUS_CREATED 201 52 | #define STATUS_ACCEPTED 202 53 | #define STATUS_NO_CONTENT 204 54 | #define STATUS_MV_PERM 301 55 | #define STATUS_MV_TEMP 302 56 | #define STATUS_NOT_MODIF 304 57 | #define STATUS_BAD_REQ 400 58 | #define STATUS_UNAUTH 401 59 | #define STATUS_FORBIDDEN 403 60 | #define STATUS_NOT_FOUND 404 61 | #define STATUS_INT_SERR 500 62 | #define STATUS_NOT_IMPL 501 63 | #define STATUS_BAD_GATEWAY 502 64 | #define STATUS_SERV_UNAVAIL 503 65 | 66 | /* HTML Doc. for ERROR */ 67 | static const char ERROR_HTML_PAGE[] = "HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\nContent-Length: 78\r\n\r\n\r\n\r\nSorry, the page you requested was not found.\r\n\r\n\r\n\0"; 68 | static const char ERROR_REQUEST_PAGE[] = "HTTP/1.1 400 OK\r\nContent-Type: text/html\r\nContent-Length: 50\r\n\r\n\r\n\r\nInvalid request.\r\n\r\n\r\n\0"; 69 | 70 | /* HTML Doc. for CGI result */ 71 | #define HTML_HEADER "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: " 72 | 73 | /* Response header for HTML*/ 74 | #define RES_HTMLHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: keep-alive\r\nContent-Length: " 75 | 76 | /* Response head for TEXT */ 77 | #define RES_TEXTHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: " 78 | 79 | /* Response head for GIF */ 80 | #define RES_GIFHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: image/gif\r\nContent-Length: " 81 | 82 | /* Response head for JPEG */ 83 | #define RES_JPEGHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: image/jpeg\r\nContent-Length: " 84 | 85 | /* Response head for PNG */ 86 | #define RES_PNGHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: image/png\r\nContent-Length: " 87 | 88 | /* Response head for FLASH */ 89 | #define RES_FLASHHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: application/x-shockwave-flash\r\nContent-Length: " 90 | 91 | /* Response head for XML */ 92 | #define RES_XMLHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: text/xml\r\nConnection: keep-alive\r\nContent-Length: " 93 | 94 | /* Response head for CSS */ 95 | #define RES_CSSHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: text/css\r\nContent-Length: " 96 | 97 | /* Response head for JavaScript */ 98 | #define RES_JSHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: application/javascript\r\nContent-Length: " 99 | 100 | /* Response head for JSON */ 101 | #define RES_JSONHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: " 102 | 103 | /* Response head for ICO */ 104 | #define RES_ICOHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: image/x-icon\r\nContent-Length: " 105 | 106 | /* Response head for CGI */ 107 | #define RES_CGIHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: " 108 | 109 | /* Response head for TTF, Font */ 110 | #define RES_TTFHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: application/x-font-truetype\r\nContent-Length: " 111 | 112 | /* Response head for OTF, Font */ 113 | #define RES_OTFHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: application/x-font-opentype\r\nContent-Length: " 114 | 115 | /* Response head for WOFF, Font */ 116 | #define RES_WOFFHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: application/font-woff\r\nContent-Length: " 117 | 118 | /* Response head for EOT, Font */ 119 | #define RES_EOTHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.ms-fontobject\r\nContent-Length: " 120 | 121 | /* Response head for SVG, Font */ 122 | #define RES_SVGHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: image/svg+xml\r\nContent-Length: " 123 | 124 | /** 125 | @brief Structure of HTTP REQUEST 126 | */ 127 | 128 | //#define MAX_URI_SIZE 1461 129 | #define MAX_URI_SIZE 512 130 | 131 | typedef struct _st_http_request 132 | { 133 | uint8_t METHOD; /**< request method(METHOD_GET...). */ 134 | uint8_t TYPE; /**< request type(PTYPE_HTML...). */ 135 | uint8_t URI[MAX_URI_SIZE]; /**< request file name. */ 136 | }st_http_request; 137 | 138 | // HTTP Parsing functions 139 | void unescape_http_url(char * url); /* convert escape character to ascii */ 140 | void parse_http_request(st_http_request *, uint8_t *); /* parse request from peer */ 141 | void find_http_uri_type(uint8_t *, uint8_t *); /* find MIME type of a file */ 142 | void make_http_response_head(char *, char, uint32_t); /* make response header */ 143 | uint8_t * get_http_param_value(char* uri, char* param_name); /* get the user-specific parameter value */ 144 | uint8_t get_http_uri_name(uint8_t * uri, uint8_t * uri_buf); /* get the requested URI name */ 145 | #ifdef _OLD_ 146 | uint8_t * get_http_uri_name(uint8_t * uri); 147 | #endif 148 | 149 | // Utility functions 150 | uint16_t ATOI(uint8_t * str, uint8_t base); 151 | void mid(char* src, char* s1, char* s2, char* sub); 152 | void inet_addr_(uint8_t * addr, uint8_t * ip); 153 | 154 | #ifdef __cplusplus 155 | } 156 | #endif 157 | 158 | #endif /* end of __HTTPPARSER_H__ */ 159 | -------------------------------------------------------------------------------- /W5500/Internet/httpServer/httpServer.h: -------------------------------------------------------------------------------- 1 | /** 2 | @file httpServer.h 3 | @brief Define constants and functions related HTTP Web server. 4 | */ 5 | 6 | #include 7 | 8 | #ifndef __HTTPSERVER_H__ 9 | #define __HTTPSERVER_H__ 10 | 11 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | // HTTP Server debug message enable 16 | #define _HTTPSERVER_DEBUG_ 17 | 18 | #define INITIAL_WEBPAGE "index.html" 19 | #define M_INITIAL_WEBPAGE "m/index.html" 20 | #define MOBILE_INITIAL_WEBPAGE "mobile/index.html" 21 | 22 | /* Web Server Content Storage Select */ 23 | //#define _USE_SDCARD_ 24 | #ifndef _USE_SDCARD_ 25 | //#define _USE_FLASH_ 26 | #endif 27 | 28 | #if !defined(_USE_SDCARD_) && !defined(_USE_FLASH_) 29 | #define _NOTUSED_STORAGE_ 30 | #endif 31 | 32 | 33 | /* Watchdog timer */ 34 | //#define _USE_WATCHDOG_ 35 | 36 | /********************************************* 37 | * HTTP Process states list 38 | *********************************************/ 39 | #define STATE_HTTP_IDLE 0 /* IDLE, Waiting for data received (TCP established) */ 40 | #define STATE_HTTP_REQ_INPROC 1 /* Received HTTP request from HTTP client */ 41 | #define STATE_HTTP_REQ_DONE 2 /* The end of HTTP request parse */ 42 | #define STATE_HTTP_RES_INPROC 3 /* Sending the HTTP response to HTTP client (in progress) */ 43 | #define STATE_HTTP_RES_DONE 4 /* The end of HTTP response send (HTTP transaction ended) */ 44 | 45 | /********************************************* 46 | * HTTP Simple Return Value 47 | *********************************************/ 48 | #define HTTP_FAILED 0 49 | #define HTTP_OK 1 50 | #define HTTP_RESET 2 51 | 52 | /********************************************* 53 | * HTTP Content NAME length 54 | *********************************************/ 55 | #define MAX_CONTENT_NAME_LEN 128 56 | 57 | /********************************************* 58 | * HTTP Timeout 59 | *********************************************/ 60 | #define HTTP_MAX_TIMEOUT_SEC 3 // Sec. 61 | 62 | typedef enum 63 | { 64 | NONE, ///< Web storage none 65 | CODEFLASH, ///< Code flash memory 66 | SDCARD, ///< SD card 67 | DATAFLASH ///< External data flash memory 68 | }StorageType; 69 | 70 | typedef struct _st_http_socket 71 | { 72 | uint8_t sock_status; 73 | uint8_t file_name[MAX_CONTENT_NAME_LEN]; 74 | uint32_t file_start; 75 | uint32_t file_len; 76 | uint32_t file_offset; // (start addr + sent size...) 77 | uint8_t storage_type; // Storage type; Code flash, SDcard, Data flash ... 78 | }st_http_socket; 79 | 80 | // Web content structure for file in code flash memory 81 | #define MAX_CONTENT_CALLBACK 20 82 | 83 | typedef struct _httpServer_webContent 84 | { 85 | uint8_t * content_name; 86 | uint32_t content_len; 87 | uint8_t * content; 88 | }httpServer_webContent; 89 | 90 | 91 | void httpServer_init(uint8_t * tx_buf, uint8_t * rx_buf, uint8_t cnt, uint8_t * socklist); 92 | void reg_httpServer_cbfunc(void(*mcu_reset)(void), void(*wdt_reset)(void)); 93 | void httpServer_run(uint8_t seqnum); 94 | 95 | void reg_httpServer_webContent(uint8_t * content_name, uint8_t * content); 96 | uint8_t find_userReg_webContent(uint8_t * content_name, uint16_t * content_num, uint32_t * file_len); 97 | uint16_t read_userReg_webContent(uint16_t content_num, uint8_t * buf, uint32_t offset, uint16_t size); 98 | uint8_t display_reg_webContent_list(void); 99 | 100 | /* 101 | * @brief HTTP Server 1sec Tick Timer handler 102 | * @note SHOULD BE register to your system 1s Tick timer handler 103 | */ 104 | void httpServer_time_handler(void); 105 | uint32_t get_httpServer_timecount(void); 106 | 107 | #ifdef __cplusplus 108 | } 109 | #endif 110 | 111 | #endif 112 | -------------------------------------------------------------------------------- /W5500/Internet/httpServer/httpUtil.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file httpUtil.c 3 | * @brief HTTP Server Utilities 4 | * @version 1.0 5 | * @date 2014/07/15 6 | * @par Revision 7 | * 2014/07/15 - 1.0 Release 8 | * @author 9 | * \n\n @par Copyright (C) 1998 - 2014 WIZnet. All rights reserved. 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | #include "httpUtil.h" 16 | 17 | uint8_t http_get_cgi_handler(uint8_t * uri_name, uint8_t * buf, uint32_t * file_len) 18 | { 19 | uint8_t ret = HTTP_OK; 20 | uint16_t len = 0; 21 | 22 | if(predefined_get_cgi_processor(uri_name, buf, &len)) 23 | { 24 | ; 25 | } 26 | else if(strcmp((const char *)uri_name, "example.cgi") == 0) 27 | { 28 | // To do 29 | ; 30 | } 31 | else 32 | { 33 | // CGI file not found 34 | ret = HTTP_FAILED; 35 | } 36 | 37 | if(ret) *file_len = len; 38 | return ret; 39 | } 40 | 41 | uint8_t http_post_cgi_handler(uint8_t * uri_name, st_http_request * p_http_request, uint8_t * buf, uint32_t * file_len) 42 | { 43 | uint8_t ret = HTTP_OK; 44 | uint16_t len = 0; 45 | uint8_t val = 0; 46 | 47 | if(predefined_set_cgi_processor(uri_name, p_http_request->URI, buf, &len)) 48 | { 49 | ; 50 | } 51 | else if(strcmp((const char *)uri_name, "example.cgi") == 0) 52 | { 53 | // To do 54 | val = 1; 55 | len = sprintf((char *)buf, "%d", val); 56 | } 57 | else 58 | { 59 | // CGI file not found 60 | ret = HTTP_FAILED; 61 | } 62 | 63 | if(ret) *file_len = len; 64 | return ret; 65 | } 66 | -------------------------------------------------------------------------------- /W5500/Internet/httpServer/httpUtil.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file httpUtil.h 3 | * @brief Header File for HTTP Server Utilities 4 | * @version 1.0 5 | * @date 2014/07/15 6 | * @par Revision 7 | * 2014/07/15 - 1.0 Release 8 | * @author 9 | * \n\n @par Copyright (C) 1998 - 2014 WIZnet. All rights reserved. 10 | */ 11 | 12 | #ifndef __HTTPUTIL_H__ 13 | #define __HTTPUTIL_H__ 14 | 15 | #ifdef __cplusplus 16 | extern "C" { 17 | #endif 18 | 19 | #include "httpServer.h" 20 | #include "httpParser.h" 21 | 22 | uint8_t http_get_cgi_handler(uint8_t * uri_name, uint8_t * buf, uint32_t * file_len); 23 | uint8_t http_post_cgi_handler(uint8_t * uri_name, st_http_request * p_http_request, uint8_t * buf, uint32_t * file_len); 24 | 25 | uint8_t predefined_get_cgi_processor(uint8_t * uri_name, uint8_t * buf, uint16_t * len); 26 | uint8_t predefined_set_cgi_processor(uint8_t * uri_name, uint8_t * uri, uint8_t * buf, uint16_t * len); 27 | 28 | #ifdef __cplusplus 29 | } 30 | #endif 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /W5500/Port/w5500_port_hal.c: -------------------------------------------------------------------------------- 1 | #include "w5500_port_hal.h" 2 | 3 | /** 4 | * @brief enter critical section 5 | * @param none 6 | * @return none 7 | */ 8 | static void w5500_cris_enter(void) 9 | { 10 | __set_PRIMASK(1); 11 | } 12 | 13 | /** 14 | * @brief exit critical section 15 | * @param none 16 | * @return none 17 | */ 18 | static void w5500_cris_exit(void) 19 | { 20 | __set_PRIMASK(0); 21 | } 22 | 23 | /** 24 | * @brief select chip 25 | * @param none 26 | * @return none 27 | */ 28 | static void w5500_cs_select(void) 29 | { 30 | HAL_GPIO_WritePin(W5500_CS_PORT, W5500_CS_PIN, GPIO_PIN_RESET); 31 | } 32 | 33 | /** 34 | * @brief deselect chip 35 | * @param none 36 | * @return none 37 | */ 38 | static void w5500_cs_deselect(void) 39 | { 40 | HAL_GPIO_WritePin(W5500_CS_PORT, W5500_CS_PIN, GPIO_PIN_SET); 41 | } 42 | 43 | /** 44 | * @brief read byte in SPI interface 45 | * @param none 46 | * @return the value of the byte read 47 | */ 48 | static uint8_t w5500_spi_readbyte(void) 49 | { 50 | uint8_t value; 51 | 52 | if (HAL_SPI_Receive(&W5500_SPI_HANDLE, &value, 1, 1000) != HAL_OK) { 53 | value = 0; 54 | } 55 | 56 | return value; 57 | } 58 | 59 | /** 60 | * @brief write byte in SPI interface 61 | * @param wb the value to write 62 | * @return none 63 | */ 64 | static void w5500_spi_writebyte(uint8_t wb) 65 | { 66 | HAL_SPI_Transmit(&W5500_SPI_HANDLE, &wb, 1, 1000); 67 | } 68 | 69 | /** 70 | * @brief burst read byte in SPI interface 71 | * @param pBuf pointer of data buf 72 | * @param len number of bytes to read 73 | * @return none 74 | */ 75 | static void w5500_spi_readburst(uint8_t* pBuf, uint16_t len) 76 | { 77 | if (!pBuf) { 78 | return; 79 | } 80 | 81 | HAL_SPI_Receive(&W5500_SPI_HANDLE, pBuf, len, 1000); 82 | } 83 | 84 | /** 85 | * @brief burst write byte in SPI interface 86 | * @param pBuf pointer of data buf 87 | * @param len number of bytes to write 88 | * @return none 89 | */ 90 | static void w5500_spi_writeburst(uint8_t* pBuf, uint16_t len) 91 | { 92 | if (!pBuf) { 93 | return; 94 | } 95 | 96 | HAL_SPI_Transmit(&W5500_SPI_HANDLE, pBuf, len, 1000); 97 | } 98 | 99 | /** 100 | * @brief hard reset 101 | * @param none 102 | * @return none 103 | */ 104 | static void w5500_hard_reset(void) 105 | { 106 | HAL_GPIO_WritePin(W5500_RST_PORT, W5500_RST_PIN, GPIO_PIN_RESET); 107 | HAL_Delay(50); 108 | HAL_GPIO_WritePin(W5500_RST_PORT, W5500_RST_PIN, GPIO_PIN_SET); 109 | HAL_Delay(10); 110 | } 111 | 112 | /** 113 | * @brief Initializes WIZCHIP with socket buffer size 114 | * @param none 115 | * @return errcode 116 | * @retval 0 success 117 | * @retval -1 fail 118 | */ 119 | static int w5500_chip_init(void) 120 | { 121 | /* default size is 2KB */ 122 | 123 | return wizchip_init(NULL, NULL); 124 | } 125 | 126 | /** 127 | * @brief set phy config if autonego is disable 128 | * @param none 129 | * @return none 130 | */ 131 | static void w5500_phy_init(void) 132 | { 133 | #ifdef USE_AUTONEGO 134 | // no thing to do 135 | #else 136 | wiz_PhyConf conf; 137 | 138 | conf.by = PHY_CONFBY_SW; 139 | conf.mode = PHY_MODE_MANUAL; 140 | conf.speed = PHY_SPEED_100; 141 | conf.duplex = PHY_DUPLEX_FULL; 142 | 143 | wizphy_setphyconf(&conf); 144 | #endif 145 | } 146 | 147 | /** 148 | * @brief initializes the network infomation 149 | * @param none 150 | * @return none 151 | */ 152 | static void w5500_network_info_init(void) 153 | { 154 | wiz_NetInfo info; 155 | 156 | uint8_t mac[6] = DEFAULT_MAC_ADDR; 157 | uint8_t ip[4] = DEFAULT_IP_ADDR; 158 | uint8_t sn[4] = DEFAULT_SUB_MASK; 159 | uint8_t gw[4] = DEFAULT_GW_ADDR; 160 | uint8_t dns[4] = DEFAULT_DNS_ADDR; 161 | 162 | memcpy(info.mac, mac, 6); 163 | memcpy(info.ip, ip, 4); 164 | memcpy(info.sn, sn, 4); 165 | memcpy(info.gw, gw, 4); 166 | memcpy(info.dns, dns, 4); 167 | 168 | #ifdef USE_DHCP 169 | info.dhcp = NETINFO_DHCP; 170 | #else 171 | info.dhcp = NETINFO_STATIC; 172 | #endif 173 | 174 | wizchip_setnetinfo(&info); 175 | } 176 | 177 | /** 178 | * @brief read and show the network infomation 179 | * @param none 180 | * @return none 181 | */ 182 | void w5500_network_info_show(void) 183 | { 184 | wiz_NetInfo info; 185 | 186 | wizchip_getnetinfo(&info); 187 | 188 | printf("w5500 network infomation:\r\n"); 189 | printf(" -mac:%d:%d:%d:%d:%d:%d\r\n", info.mac[0], info.mac[1], info.mac[2], 190 | info.mac[3], info.mac[4], info.mac[5]); 191 | printf(" -ip:%d.%d.%d.%d\r\n", info.ip[0], info.ip[1], info.ip[2], info.ip[3]); 192 | printf(" -sn:%d.%d.%d.%d\r\n", info.sn[0], info.sn[1], info.sn[2], info.sn[3]); 193 | printf(" -gw:%d.%d.%d.%d\r\n", info.gw[0], info.gw[1], info.gw[2], info.gw[3]); 194 | printf(" -dns:%d.%d.%d.%d\r\n", info.dns[0], info.dns[1], info.dns[2], info.dns[3]); 195 | 196 | if (info.dhcp == NETINFO_DHCP) { 197 | printf(" -dhcp_mode: dhcp\r\n"); 198 | } else { 199 | printf(" -dhcp_mode: static\r\n"); 200 | } 201 | } 202 | 203 | /** 204 | * @brief w5500 init 205 | * @param none 206 | * @return errcode 207 | * @retval 0 success 208 | * @retval -1 chip init fail 209 | */ 210 | int w5500_init(void) 211 | { 212 | /* W5500 hard reset */ 213 | w5500_hard_reset(); 214 | 215 | /* Register spi driver function */ 216 | reg_wizchip_cris_cbfunc(w5500_cris_enter, w5500_cris_exit); 217 | reg_wizchip_cs_cbfunc(w5500_cs_select, w5500_cs_deselect); 218 | reg_wizchip_spi_cbfunc(w5500_spi_readbyte, w5500_spi_writebyte); 219 | reg_wizchip_spiburst_cbfunc(w5500_spi_readburst, w5500_spi_writeburst); 220 | 221 | /* socket buffer size init */ 222 | if (w5500_chip_init() != 0) { 223 | return -1; 224 | } 225 | 226 | /* phy init */ 227 | w5500_phy_init(); 228 | 229 | /* network infomation init */ 230 | w5500_network_info_init(); 231 | 232 | /* show network infomation */ 233 | w5500_network_info_show(); 234 | 235 | return 0; 236 | } 237 | -------------------------------------------------------------------------------- /W5500/Port/w5500_port_hal.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/W5500/Port/w5500_port_hal.h -------------------------------------------------------------------------------- /tools/Image2Lcd.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/tools/Image2Lcd.zip -------------------------------------------------------------------------------- /tools/MakeDot_V1_1.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/tools/MakeDot_V1_1.zip -------------------------------------------------------------------------------- /tools/PCtoLCD2002.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/tools/PCtoLCD2002.zip -------------------------------------------------------------------------------- /tools/sockettoolv25.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mculover666/HAL_Driver_Lib/2c09811e01fed3ec64c85be50a5565f55fee995d/tools/sockettoolv25.zip --------------------------------------------------------------------------------