[bsp] added Ethernet driver and lwip in stm32f429-apollo board

* use uart1 as default console device
    * add pcf8574 support(a general-purpose remote I/O expansion chip)
This commit is contained in:
tanek liang
2017-06-09 14:58:33 +08:00
parent 269a7683b5
commit a157b25f55
9 changed files with 875 additions and 6 deletions
+1 -1
View File
@@ -18,7 +18,7 @@ drv_mpu.c
# add Ethernet drivers.
if GetDepend('RT_USING_LWIP'):
src += ['drv_eth.c']
src += ['drv_eth.c', 'drv_iic.c', 'drv_pcf8574.c']
# add gpio drivers.
if GetDepend('RT_USING_PIN'):
+1 -1
View File
@@ -61,7 +61,7 @@ extern int __bss_end;
// <o> Console on USART: <0=> no console <1=>USART 1 <2=>USART 2 <3=> USART 3
// <i>Default: 1
#define STM32_CONSOLE_USART 2
#define STM32_CONSOLE_USART 1
void rt_hw_board_init(void);
File diff suppressed because it is too large Load Diff
+6
View File
@@ -0,0 +1,6 @@
#ifndef __DRV_ETH_H__
#define __DRV_ETH_H__
void rt_hw_stm32_eth_init(void);
#endif
+113
View File
@@ -0,0 +1,113 @@
/*
* File : drv_iic.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2017 RT-Thread Develop Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2017-06-05 tanek first implementation.
*/
#include "drv_iic.h"
#include <board.h>
#include <finsh.h>
#include <rtdevice.h>
#include <rthw.h>
#define DEBUG
#ifdef DEBUG
#define DEBUG_PRINTF(...) rt_kprintf(__VA_ARGS__)
#else
#define DEBUG_PRINTF(...)
#endif
static void stm32f4_i2c_gpio_init()
{
GPIO_InitTypeDef GPIO_Initure;
__HAL_RCC_GPIOH_CLK_ENABLE();
GPIO_Initure.Pin = GPIO_PIN_4 | GPIO_PIN_5;
GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_Initure.Pull = GPIO_PULLUP;
GPIO_Initure.Speed = GPIO_SPEED_FAST;
HAL_GPIO_Init(GPIOH, &GPIO_Initure);
HAL_GPIO_WritePin(GPIOH, GPIO_PIN_5, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOH, GPIO_PIN_4, GPIO_PIN_SET);
}
static void stm32f4_set_sda(void *data, rt_int32_t state)
{
HAL_GPIO_WritePin(GPIOH, GPIO_PIN_5, (GPIO_PinState)state);
}
static void stm32f4_set_scl(void *data, rt_int32_t state)
{
HAL_GPIO_WritePin(GPIOH, GPIO_PIN_4, (GPIO_PinState)state);
}
static rt_int32_t stm32f4_get_sda(void *data)
{
return (rt_int32_t)HAL_GPIO_ReadPin(GPIOH, GPIO_PIN_5);
}
static rt_int32_t stm32f4_get_scl(void *data)
{
return (rt_int32_t)HAL_GPIO_ReadPin(GPIOH, GPIO_PIN_4);
}
static void stm32f4_udelay(rt_uint32_t us)
{
rt_int32_t i;
for (; us > 0; us--)
{
i = 50;
while (i > 0)
{
i--;
}
}
}
static const struct rt_i2c_bit_ops bit_ops = {
RT_NULL,
stm32f4_set_sda,
stm32f4_set_scl,
stm32f4_get_sda,
stm32f4_get_scl,
stm32f4_udelay,
5,
100
};
int stm32f4_i2c_init(void)
{
struct rt_i2c_bus_device *bus;
bus = rt_malloc(sizeof(struct rt_i2c_bus_device));
if (bus == RT_NULL)
{
rt_kprintf("rt_malloc failed\n");
return -RT_ENOMEM;
}
rt_memset((void *)bus, 0, sizeof(struct rt_i2c_bus_device));
bus->priv = (void *)&bit_ops;
stm32f4_i2c_gpio_init();
rt_i2c_bit_add_bus(bus, "i2c0");
return RT_EOK;
}
INIT_DEVICE_EXPORT(stm32f4_i2c_init);
+24
View File
@@ -0,0 +1,24 @@
/*
* File : drv_iic.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2017 RT-Thread Develop Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2017-06-05 tanek first implementation.
*/
#ifndef STM32F4XX_IIC_INCLUDED
#define STM32F4XX_IIC_INCLUDED
#include <rtthread.h>
#include <drivers/spi.h>
#include "stm32f4xx_hal.h"
#endif // STM32F20X_40X_SPI_H_INCLUDED
+100
View File
@@ -0,0 +1,100 @@
/*
* File : drv_iic.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2017 RT-Thread Develop Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2017-06-09 tanek first implementation.
*/
#include "drv_pcf8574.h"
#include <board.h>
#include <rthw.h>
#include <finsh.h>
//#define DEBUG
#ifdef DEBUG
#define DEBUG_PRINTF(...) rt_kprintf(__VA_ARGS__)
#else
#define DEBUG_PRINTF(...)
#endif
#define I2C_BUS_NAME "i2c0"
#define PCF8574_ADDR 0x20
static uint8_t rt_pcf8574_read_onebyte(void);
static void rt_pcf8574_write_onebyte(rt_uint8_t value);
static struct rt_i2c_bus_device * i2c_bus;
rt_err_t rt_pcf8574_init(void)
{
rt_uint8_t buffer[] = { 0xFF };
rt_off_t pos = PCF8574_ADDR;
__HAL_RCC_GPIOB_CLK_ENABLE();
i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(I2C_BUS_NAME);
if (i2c_bus == RT_NULL)
{
DEBUG_PRINTF("\ni2c_bus %s for PCF8574 not found!\n", I2C_BUS_NAME);
return -RT_ENOSYS;
}
if (rt_device_open(&i2c_bus->parent, RT_NULL) != RT_EOK)
{
DEBUG_PRINTF("\ni2c_bus %s for cs43l22 opened failed!\n", I2C_BUS_NAME);
return -RT_EEMPTY;
}
rt_device_write(&i2c_bus->parent, pos, &buffer, 1);
return RT_EOK;
}
static uint8_t rt_pcf8574_read_onebyte(void)
{
rt_uint8_t value;
rt_device_read(&i2c_bus->parent, PCF8574_ADDR, &value, 1);
return value;
}
static void rt_pcf8574_write_onebyte(rt_uint8_t value)
{
rt_device_write(&i2c_bus->parent, PCF8574_ADDR, &value, 1);
}
void rt_pcf8574_write_bit(rt_uint8_t bit, rt_uint8_t state)
{
rt_uint8_t data;
data = rt_pcf8574_read_onebyte();
if (state == 0)
data &= ~(1 << bit);
else
data |= 1 << bit;
rt_pcf8574_write_onebyte(data);
}
rt_uint8_t rt_pcf8574_read_bit(rt_uint8_t bit)
{
rt_uint8_t data;
data = rt_pcf8574_read_onebyte();
if (data&(1 << bit))
return 1;
else
return 0;
}
@@ -0,0 +1,25 @@
#ifndef __DRV_PCF8574_H
#define __DRV_PCF8574_H
#include <board.h>
#include <finsh.h>
#include <rtdevice.h>
#include <rthw.h>
//PCF8574各个IO的功能
#define BEEP_IO 0 //蜂鸣器控制引脚 P0
#define AP_INT_IO 1 //AP3216C中断引脚 P1
#define DCMI_PWDN_IO 2 //DCMI的电源控制引脚 P2
#define USB_PWR_IO 3 //USB电源控制引脚 P3
#define EX_IO 4 //扩展IO,自定义使用 P4
#define MPU_INT_IO 5 //MPU9250中断引脚 P5
#define RS485_RE_IO 6 //RS485_RE引脚 P6
#define ETH_RESET_IO 7 //以太网复位引脚 P7
rt_err_t rt_pcf8574_init(void);
void rt_pcf8574_write_bit(rt_uint8_t bit, rt_uint8_t state);
rt_uint8_t rt_pcf8574_read_bit(rt_uint8_t bit);
#endif
+9 -4
View File
@@ -186,7 +186,9 @@
/* Enable DNS */
#define RT_LWIP_DNS
/* Enable DHCP */
//#define RT_LWIP_DHCP
#define RT_LWIP_DHCP
/* Enable DEBUG */
//#define RT_LWIP_DEBUG
/* the number of simulatenously active TCP connections*/
#define RT_LWIP_TCP_PCB_NUM 5
@@ -228,9 +230,9 @@
#define CHECKSUM_CHECK_IP 0
#define CHECKSUM_CHECK_UDP 0
#define CHECKSUM_GEN_TCP 0
#define CHECKSUM_GEN_IP 0
#define CHECKSUM_GEN_UDP 0
//#define CHECKSUM_GEN_TCP 0
//#define CHECKSUM_GEN_IP 0
//#define CHECKSUM_GEN_UDP 0
/* RT_GDB_STUB */
//#define RT_USING_GDB
@@ -246,4 +248,7 @@
/* serial flash discoverable parameters by JEDEC standard */
#define RT_SFUD_USING_SFDP
#define RT_USING_I2C
#define RT_USING_I2C_BITOPS
#endif