summaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
author周传福 <[email protected]>2019-05-16 12:22:53 +0800
committerGitHub <[email protected]>2019-05-16 12:22:53 +0800
commit9329b62beff829b7e6fcfe285b4267011e6f4d5c (patch)
tree5de4f47d6267a119bd9125c41a3f882c0a8b7b70 /components
parentb844496ac9459a16044abe13785b355853526304 (diff)
parent4539172f5c97306a322ac1768c5c8eb4f878ea3c (diff)
Merge branch 'master' into stm32_uart_tx_dma
Diffstat (limited to 'components')
-rw-r--r--components/drivers/include/drivers/pm.h171
-rw-r--r--components/drivers/pm/pm.c424
-rw-r--r--components/drivers/sdio/sd.c2
-rwxr-xr-xcomponents/drivers/sensors/sensor.h2
-rwxr-xr-xcomponents/drivers/sensors/sensor_cmd.c8
-rw-r--r--components/drivers/serial/serial.c1
-rw-r--r--components/libc/Kconfig6
-rw-r--r--components/libc/pthreads/pthread.c301
-rw-r--r--components/libc/pthreads/pthread.h30
-rw-r--r--components/libc/pthreads/pthread_attr.c29
-rw-r--r--components/libc/pthreads/pthread_cond.c2
-rw-r--r--components/libc/pthreads/pthread_internal.h16
-rw-r--r--components/libc/pthreads/pthread_tls.c10
-rw-r--r--components/net/Kconfig5
-rw-r--r--components/net/freemodbus/port/portserial.c29
-rw-r--r--components/net/freemodbus/port/portserial_m.c31
-rw-r--r--components/net/lwip-2.0.2/src/api/api_lib.c2
-rw-r--r--components/net/lwip-2.1.0/src/api/api_lib.c2
-rw-r--r--components/net/netdev/include/netdev.h2
-rw-r--r--components/net/netdev/src/netdev.c39
-rw-r--r--components/net/sal_socket/dfs_net/dfs_net.c4
-rw-r--r--components/net/sal_socket/impl/af_inet_lwip.c15
-rw-r--r--components/net/sal_socket/include/sal_socket.h29
-rw-r--r--components/net/sal_socket/src/sal_socket.c49
-rw-r--r--components/utilities/ulog/ulog.c8
25 files changed, 708 insertions, 509 deletions
diff --git a/components/drivers/include/drivers/pm.h b/components/drivers/include/drivers/pm.h
index 94d4c8559b..e35b21cd48 100644
--- a/components/drivers/include/drivers/pm.h
+++ b/components/drivers/include/drivers/pm.h
@@ -7,6 +7,7 @@
* Date Author Notes
* 2012-06-02 Bernard the first version
* 2018-08-02 Tanek split run and sleep modes, support custom mode
+ * 2019-04-28 Zero-Free improve PM mode and device ops interface
*/
#ifndef __PM_H__
@@ -16,96 +17,67 @@
#ifndef PM_HAS_CUSTOM_CONFIG
-/* All modes used for rt_pm_request() adn rt_pm_release() */
+/* All modes used for rt_pm_request() and rt_pm_release() */
enum
{
- /* run modes */
- PM_RUN_MODE_NORMAL = 0,
-
/* sleep modes */
- PM_SLEEP_MODE_SLEEP,
- PM_SLEEP_MODE_TIMER,
+ PM_SLEEP_MODE_NONE = 0,
+ PM_SLEEP_MODE_IDLE,
+ PM_SLEEP_MODE_LIGHT,
+ PM_SLEEP_MODE_DEEP,
+ PM_SLEEP_MODE_STANDBY,
PM_SLEEP_MODE_SHUTDOWN,
+ PM_SLEEP_MODE_MAX,
+};
+
+enum
+{
+ /* run modes*/
+ PM_RUN_MODE_HIGH_SPEED = 0,
+ PM_RUN_MODE_NORMAL_SPEED,
+ PM_RUN_MODE_MEDIUM_SPEED,
+ PM_RUN_MODE_LOW_SPEED,
+ PM_RUN_MODE_MAX,
+};
+
+enum
+{
+ RT_PM_FREQUENCY_PENDING = 0x01,
};
+#define RT_PM_DEFAULT_SLEEP_MODE PM_SLEEP_MODE_IDLE
+#define RT_PM_DEFAULT_RUN_MODE PM_RUN_MODE_NORMAL_SPEED
+
/* The name of all modes used in the msh command "pm_dump" */
-#define PM_MODE_NAMES \
+#define PM_SLEEP_MODE_NAMES \
{ \
- "Running Mode", \
- \
- "Sleep Mode", \
- "Timer Mode", \
+ "None Mode", \
+ "Idle Mode", \
+ "LightSleep Mode", \
+ "DeepSleep Mode", \
+ "Standby Mode", \
"Shutdown Mode", \
}
-/* run mode count : 1 */
-#define PM_RUN_MODE_COUNT 1
-/* sleep mode count : 3 */
-#define PM_SLEEP_MODE_COUNT 3
-
-/* support redefining default run mode */
-#ifndef PM_RUN_MODE_DEFAULT
-#define PM_RUN_MODE_DEFAULT 0
-#endif
-
-/* support redefining default sleep mode */
-#ifndef PM_SLEEP_MODE_DEFAULT
-#define PM_SLEEP_MODE_DEFAULT (PM_SLEEP_MODE_START)
-#endif
-
-/* support redefining the minimum tick into sleep mode */
-#ifndef PM_MIN_ENTER_SLEEP_TICK
-#define PM_MIN_ENTER_SLEEP_TICK (1)
-#endif
+#define PM_RUN_MODE_NAMES \
+{ \
+ "High Speed", \
+ "Normal Speed", \
+ "Medium Speed", \
+ "Low Mode", \
+}
#else /* PM_HAS_CUSTOM_CONFIG */
#include <pm_cfg.h>
-#ifndef PM_RUN_MODE_COUNT
-#error "You must defined PM_RUN_MODE_COUNT on pm_cfg.h"
-#endif
-
-#ifndef PM_SLEEP_MODE_COUNT
-#error "You must defined PM_SLEEP_MODE_COUNT on pm_cfg.h"
-#endif
-
-#ifndef PM_MODE_DEFAULT
-#error "You must defined PM_MODE_DEFAULT on pm_cfg.h"
-#endif
-
-#ifndef PM_MODE_NAMES
-#error "You must defined PM_MODE_NAMES on pm_cfg.h"
-#endif
-
-#ifndef PM_RUN_MODE_DEFAULT
-#error "You must defined PM_RUN_MODE_DEFAULT on pm_cfg.h"
-#endif
-
-/* The default sleep mode(PM_SLEEP_MODE_DEFAULT) are not required.
- * If the default mode is defined, it is requested once in rt_system_pm_init()
- */
-
#endif /* PM_HAS_CUSTOM_CONFIG */
-/* run mode must start at 0 */
-#define PM_RUN_MODE_START 0
-/* the values of the run modes and sleep mode must be consecutive */
-#define PM_SLEEP_MODE_START PM_RUN_MODE_COUNT
-/* all mode count */
-#define PM_MODE_COUNT (PM_RUN_MODE_COUNT + PM_SLEEP_MODE_COUNT)
-/* The last mode, will be request in rt_system_pm_init() */
-#define PM_MODE_MAX (PM_RUN_MODE_COUNT + PM_SLEEP_MODE_COUNT - 1)
-
-#if PM_MODE_COUNT > 32
-#error "The number of modes cannot exceed 32"
-#endif
-
/**
* device control flag to request or release power
*/
#define RT_PM_DEVICE_CTRL_REQUEST 0x01
-#define RT_PM_DEVICE_CTRL_RELEASE 0x02
+#define RT_PM_DEVICE_CTRL_RELEASE 0x00
struct rt_pm;
@@ -114,13 +86,8 @@ struct rt_pm;
*/
struct rt_pm_ops
{
- void (*enter)(struct rt_pm *pm);
- void (*exit)(struct rt_pm *pm);
-
-#if PM_RUN_MODE_COUNT > 1
- void (*frequency_change)(struct rt_pm *pm, rt_uint32_t frequency);
-#endif
-
+ void (*sleep)(struct rt_pm *pm, uint8_t mode);
+ void (*run)(struct rt_pm *pm, uint8_t mode);
void (*timer_start)(struct rt_pm *pm, rt_uint32_t timeout);
void (*timer_stop)(struct rt_pm *pm);
rt_tick_t (*timer_get_tick)(struct rt_pm *pm);
@@ -128,18 +95,15 @@ struct rt_pm_ops
struct rt_device_pm_ops
{
-#if PM_RUN_MODE_COUNT > 1
- void (*frequency_change)(const struct rt_device* device);
-#endif
-
- void (*suspend)(const struct rt_device* device);
- void (*resume) (const struct rt_device* device);
+ int (*suspend)(const struct rt_device *device, uint8_t mode);
+ void (*resume)(const struct rt_device *device, uint8_t mode);
+ int (*frequency_change)(const struct rt_device *device, uint8_t mode);
};
struct rt_device_pm
{
- const struct rt_device* device;
- const struct rt_device_pm_ops* ops;
+ const struct rt_device *device;
+ const struct rt_device_pm_ops *ops;
};
/**
@@ -150,32 +114,45 @@ struct rt_pm
struct rt_device parent;
/* modes */
- rt_uint8_t modes[PM_MODE_COUNT];
- rt_uint8_t current_mode; /* current pm mode */
- rt_uint8_t exit_count;
+ rt_uint8_t modes[PM_SLEEP_MODE_MAX];
+ rt_uint8_t sleep_mode; /* current sleep mode */
+ rt_uint8_t run_mode; /* current running mode */
/* the list of device, which has PM feature */
rt_uint8_t device_pm_number;
- struct rt_device_pm* device_pm;
- struct rt_semaphore device_lock;
+ struct rt_device_pm *device_pm;
/* if the mode has timer, the corresponding bit is 1*/
- rt_uint32_t timer_mask;
+ rt_uint8_t timer_mask;
+ rt_uint8_t flags;
const struct rt_pm_ops *ops;
};
-void rt_pm_enter(void);
-void rt_pm_exit(void);
+enum
+{
+ RT_PM_ENTER_SLEEP = 0,
+ RT_PM_EXIT_SLEEP,
+};
+
+struct rt_pm_notify
+{
+ void (*notify)(uint8_t event, uint8_t mode, void *data);
+ void *data;
+};
+
+void rt_pm_request(uint8_t sleep_mode);
+void rt_pm_release(uint8_t sleep_mode);
+int rt_pm_run_enter(uint8_t run_mode);
-void rt_pm_request(rt_ubase_t mode);
-void rt_pm_release(rt_ubase_t mode);
+void rt_pm_device_register(struct rt_device *device, const struct rt_device_pm_ops *ops);
+void rt_pm_device_unregister(struct rt_device *device);
-void rt_pm_register_device(struct rt_device* device, const struct rt_device_pm_ops* ops);
-void rt_pm_unregister_device(struct rt_device* device);
+void rt_pm_notify_set(void (*notify)(uint8_t event, uint8_t mode, void *data), void *data);
+void rt_pm_default_set(uint8_t sleep_mode);
void rt_system_pm_init(const struct rt_pm_ops *ops,
- rt_uint8_t timer_mask,
- void *user_data);
+ uint8_t timer_mask,
+ void *user_data);
#endif /* __PM_H__ */
diff --git a/components/drivers/pm/pm.c b/components/drivers/pm/pm.c
index c300414444..3e9cf56c76 100644
--- a/components/drivers/pm/pm.c
+++ b/components/drivers/pm/pm.c
@@ -7,6 +7,7 @@
* Date Author Notes
* 2012-06-02 Bernard the first version
* 2018-08-02 Tanek split run and sleep modes, support custom mode
+ * 2019-04-28 Zero-Free improve PM mode and device ops interface
*/
#include <rthw.h>
@@ -16,27 +17,46 @@
#ifdef RT_USING_PM
static struct rt_pm _pm;
+static uint8_t _pm_default_sleep = RT_PM_DEFAULT_SLEEP_MODE;
+static struct rt_pm_notify _pm_notify;
+static uint8_t _pm_init_flag = 0;
+
+#define RT_PM_TICKLESS_THRESH (2)
+
+RT_WEAK uint32_t rt_pm_enter_critical(uint8_t sleep_mode)
+{
+ return rt_hw_interrupt_disable();
+}
+
+RT_WEAK void rt_pm_exit_critical(uint32_t ctx, uint8_t sleep_mode)
+{
+ rt_hw_interrupt_enable(ctx);
+}
/**
* This function will suspend all registered devices
*/
-static void _pm_device_suspend(void)
+static int _pm_device_suspend(uint8_t mode)
{
- int index;
+ int index, ret = RT_EOK;
for (index = 0; index < _pm.device_pm_number; index++)
{
if (_pm.device_pm[index].ops->suspend != RT_NULL)
{
- _pm.device_pm[index].ops->suspend(_pm.device_pm[index].device);
+ ret = _pm.device_pm[index].ops->suspend(_pm.device_pm[index].device, mode);
+ if(ret != RT_EOK)
+ break;
}
}
+
+ return ret;
}
/**
* This function will resume all registered devices
*/
-static void _pm_device_resume(void)
+static void _pm_device_resume(uint8_t mode)
{
int index;
@@ -44,16 +64,15 @@ static void _pm_device_resume(void)
{
if (_pm.device_pm[index].ops->resume != RT_NULL)
{
- _pm.device_pm[index].ops->resume(_pm.device_pm[index].device);
+ _pm.device_pm[index].ops->resume(_pm.device_pm[index].device, mode);
}
}
}
-#if PM_RUN_MODE_COUNT > 1
/**
* This function will update the frequency of all registered devices
*/
-static void _pm_device_frequency_change(void)
+static void _pm_device_frequency_change(uint8_t mode)
{
rt_uint32_t index;
@@ -61,152 +80,151 @@ static void _pm_device_frequency_change(void)
for (index = 0; index < _pm.device_pm_number; index ++)
{
if (_pm.device_pm[index].ops->frequency_change != RT_NULL)
- _pm.device_pm[index].ops->frequency_change(_pm.device_pm[index].device);
+ _pm.device_pm[index].ops->frequency_change(_pm.device_pm[index].device, mode);
}
}
-#endif
/**
- * This function will enter corresponding power mode.
+ * This function will update the system clock frequency when idle
*/
-void rt_pm_enter(void)
+static void _pm_frequency_scaling(struct rt_pm *pm)
{
- rt_ubase_t level;
- struct rt_pm *pm;
- rt_uint32_t index;
- rt_tick_t timeout_tick;
+ rt_base_t level;
- pm = &_pm;
+ if (pm->flags & RT_PM_FREQUENCY_PENDING)
+ {
+ level = rt_hw_interrupt_disable();
+ /* change system runing mode */
+ pm->ops->run(pm, pm->run_mode);
+ /* changer device frequency */
+ _pm_device_frequency_change(pm->run_mode);
+ pm->flags &= ~RT_PM_FREQUENCY_PENDING;
+ rt_hw_interrupt_enable(level);
+ }
+}
- /* disable interrupt before check run modes */
- level = rt_hw_interrupt_disable();
- /* check each run mode, and decide to swithc to run mode or sleep mode */
- for (index = 0; index < PM_RUN_MODE_COUNT; index++)
+/**
+ * This function selects the sleep mode according to the rt_pm_request/rt_pm_release count.
+ */
+static uint8_t _pm_select_sleep_mode(struct rt_pm *pm)
+{
+ int index;
+ uint8_t mode;
+
+ mode = _pm_default_sleep;
+ for (index = PM_SLEEP_MODE_NONE; index < PM_SLEEP_MODE_MAX; index ++)
{
if (pm->modes[index])
{
- if (index > pm->current_mode)
- {
- pm->ops->exit(pm);
- pm->current_mode = index;
- pm->ops->enter(pm);
-#if PM_RUN_MODE_COUNT > 1
- pm->ops->frequency_change(pm, 0);
- _pm_device_frequency_change();
-#endif
- }
-
- rt_hw_interrupt_enable(level);
- /* The current mode is run mode, no need to check sleep mode */
- return ;
+ mode = index;
+ break;
}
}
- /* enable interrupt after check run modes */
- rt_hw_interrupt_enable(level);
+ pm->sleep_mode = mode;
- level = rt_hw_interrupt_disable();
- /* check each sleep mode to decide which mode can system sleep. */
- for (index = PM_SLEEP_MODE_START; index < PM_SLEEP_MODE_START + PM_SLEEP_MODE_COUNT; index++)
+ return mode;
+}
+
+/**
+ * This function changes the power sleep mode base on the result of selection
+ */
+static void _pm_change_sleep_mode(struct rt_pm *pm, uint8_t mode)
+{
+ rt_tick_t timeout_tick, delta_tick;
+ rt_base_t level;
+ int ret = RT_EOK;
+
+ if (mode == PM_SLEEP_MODE_NONE)
{
- if (pm->modes[index])
- {
- /* let mcu sleep when system is idle */
+ pm->sleep_mode = mode;
+ pm->ops->sleep(pm, PM_SLEEP_MODE_NONE);
+ }
+ else
+ {
+ level = rt_pm_enter_critical(mode);
- /* run mode to sleep mode */
- if (pm->current_mode < PM_SLEEP_MODE_START)
- {
- /* exit run mode */
- pm->ops->exit(pm);
- }
+ /* Notify app will enter sleep mode */
+ if (_pm_notify.notify)
+ _pm_notify.notify(RT_PM_ENTER_SLEEP, mode, _pm_notify.data);
- /* set current power mode */
- pm->current_mode = index;
- pm->exit_count = 1;
+ /* Suspend all peripheral device */
+ ret = _pm_device_suspend(mode);
+ if (ret != RT_EOK)
+ {
+ _pm_device_resume(mode);
+ if (_pm_notify.notify)
+ _pm_notify.notify(RT_PM_EXIT_SLEEP, mode, _pm_notify.data);
+ rt_pm_exit_critical(level, mode);
- /* suspend all of devices with PM feature */
- _pm_device_suspend();
+ return;
+ }
- /* should start pm timer */
- if (pm->timer_mask & (1 << index))
+ /* Tickless*/
+ if (pm->timer_mask & (0x01 << mode))
+ {
+ timeout_tick = rt_timer_next_timeout_tick();
+ if (timeout_tick == RT_TICK_MAX)
{
- /* get next os tick */
- timeout_tick = rt_timer_next_timeout_tick();
- if (timeout_tick != RT_TICK_MAX)
+ if (pm->ops->timer_start)
{
- timeout_tick -= rt_tick_get();
-
-#if defined(PM_MIN_ENTER_SLEEP_TICK) && PM_MIN_ENTER_SLEEP_TICK > 0
- if (timeout_tick < PM_MIN_ENTER_SLEEP_TICK)
- {
- rt_hw_interrupt_enable(level);
- /* limit the minimum time to enter timer sleep mode */
- return ;
- }
-#endif
+ pm->ops->timer_start(pm, RT_TICK_MAX);
}
- /* startup pm timer */
- pm->ops->timer_start(pm, timeout_tick);
}
+ else
+ {
+ timeout_tick = timeout_tick - rt_tick_get();
+ if (timeout_tick < RT_PM_TICKLESS_THRESH)
+ {
+ mode = PM_SLEEP_MODE_IDLE;
+ }
+ else
+ {
+ pm->ops->timer_start(pm, timeout_tick);
+ }
+ }
+ }
- /* enter sleep and wait to be waken up */
- pm->ops->enter(pm);
-
- /* exit from low power mode */
- rt_pm_exit();
+ /* enter lower power state */
+ pm->ops->sleep(pm, mode);
- rt_hw_interrupt_enable(level);
- return ;
+ /* wake up from lower power state*/
+ if (pm->timer_mask & (0x01 << mode))
+ {
+ delta_tick = pm->ops->timer_get_tick(pm);
+ pm->ops->timer_stop(pm);
+ if (delta_tick)
+ {
+ rt_tick_set(rt_tick_get() + delta_tick);
+ rt_timer_check();
+ }
}
- }
- rt_hw_interrupt_enable(level);
+ /* resume all device */
+ _pm_device_resume(pm->sleep_mode);
+
+ if (_pm_notify.notify)
+ _pm_notify.notify(RT_PM_EXIT_SLEEP, mode, _pm_notify.data);
+
+ rt_pm_exit_critical(level, mode);
+ }
}
/**
- * This function exits from sleep mode.
+ * This function will enter corresponding power mode.
*/
-void rt_pm_exit(void)
+void rt_system_power_manager(void)
{
- rt_ubase_t level;
- struct rt_pm *pm;
- rt_tick_t delta_tick;
-
- pm = &_pm;
-
- level = rt_hw_interrupt_disable();
-
- if (pm->exit_count)
- {
- pm->exit_count = 0;
-
- if (pm->current_mode >= PM_SLEEP_MODE_START)
- {
- /* sleep mode with timer */
- if (pm->timer_mask & (1 << pm->current_mode))
- {
- /* get the tick of pm timer */
- delta_tick = pm->ops->timer_get_tick(pm);
-
- /* stop pm timer */
- pm->ops->timer_stop(pm);
+ uint8_t mode;
- if (delta_tick)
- {
- /* adjust OS tick */
- rt_tick_set(rt_tick_get() + delta_tick);
- /* check system timer */
- rt_timer_check();
- }
- }
+ if (_pm_init_flag == 0)
+ return;
- /* exit from sleep mode */
- pm->ops->exit(pm);
- /* resume the device with PM feature */
- _pm_device_resume();
- }
- }
+ /* CPU frequency scaling according to the runing mode settings */
+ _pm_frequency_scaling(&_pm);
- rt_hw_interrupt_enable(level);
+ /* Low Power Mode Processing */
+ mode = _pm_select_sleep_mode(&_pm);
+ _pm_change_sleep_mode(&_pm, mode);
}
/**
@@ -215,60 +233,21 @@ void rt_pm_exit(void)
*
* @param parameter the parameter of run mode or sleep mode
*/
-void rt_pm_request(rt_ubase_t mode)
+void rt_pm_request(uint8_t mode)
{
- rt_ubase_t level;
+ rt_base_t level;
struct rt_pm *pm;
- pm = &_pm;
+ if (_pm_init_flag == 0)
+ return;
- if (mode > PM_MODE_MAX)
+ if (mode > (PM_SLEEP_MODE_MAX - 1))
return;
level = rt_hw_interrupt_disable();
-
- /* update pm modes table */
- pm->modes[mode] ++;
-
- /* request higter mode with a smaller mode value*/
- if (mode < pm->current_mode)
- {
- /* the old current mode is RUN mode, need to all pm->ops->exit(),
- * if not, it has already called in rt_pm_exit()
- */
- if (pm->current_mode < PM_SLEEP_MODE_START)
- {
- pm->ops->exit(pm);
- }
- else if (pm->exit_count)
- {
- /* call exeit when global interrupt is disable */
- pm->ops->exit(pm);
- pm->exit_count = 0;
- }
-
- /* update current mode */
- pm->current_mode = mode;
-
- /* current mode is higher run mode */
- if (mode < PM_SLEEP_MODE_START)
- {
- /* enter run mode */
- pm->ops->enter(pm);
-#if PM_RUN_MODE_COUNT > 1
- /* frequency change */
- pm->ops->frequency_change(pm, 0);
- _pm_device_frequency_change();
-#endif
- }
- else
- {
- /* do nothing when request higher sleep mode,
- * and swithc to new sleep mode in rt_pm_enter()
- */
- }
- }
-
+ pm = &_pm;
+ if (pm->modes[mode] < 255)
+ pm->modes[mode] ++;
rt_hw_interrupt_enable(level);
}
@@ -279,21 +258,21 @@ void rt_pm_request(rt_ubase_t mode)
* @param parameter the parameter of run mode or sleep mode
*
*/
-void rt_pm_release(rt_ubase_t mode)
+void rt_pm_release(uint8_t mode)
{
rt_ubase_t level;
struct rt_pm *pm;
- pm = &_pm;
+ if (_pm_init_flag == 0)
+ return;
- if (mode > PM_MODE_MAX)
+ if (mode > (PM_SLEEP_MODE_MAX - 1))
return;
level = rt_hw_interrupt_disable();
-
+ pm = &_pm;
if (pm->modes[mode] > 0)
pm->modes[mode] --;
-
rt_hw_interrupt_enable(level);
}
@@ -303,9 +282,9 @@ void rt_pm_release(rt_ubase_t mode)
* @param device the device with PM feature
* @param ops the PM ops for device
*/
-void rt_pm_register_device(struct rt_device *device, const struct rt_device_pm_ops *ops)
+void rt_pm_device_register(struct rt_device *device, const struct rt_device_pm_ops *ops)
{
- rt_ubase_t level;
+ rt_base_t level;
struct rt_device_pm *device_pm;
RT_DEBUG_NOT_IN_INTERRUPT;
@@ -322,8 +301,6 @@ void rt_pm_register_device(struct rt_device *device, const struct rt_device_pm_o
_pm.device_pm_number += 1;
}
- rt_sem_release(&(_pm.device_lock));
-
rt_hw_interrupt_enable(level);
}
@@ -332,7 +309,7 @@ void rt_pm_register_device(struct rt_device *device, const struct rt_device_pm_o
*
* @param device the device with PM feature
*/
-void rt_pm_unregister_device(struct rt_device *device)
+void rt_pm_device_unregister(struct rt_device *device)
{
rt_ubase_t level;
rt_uint32_t index;
@@ -363,6 +340,23 @@ void rt_pm_unregister_device(struct rt_device *device)
}
/**
+ * This function set notification callback for application
+ */
+void rt_pm_notify_set(void (*notify)(uint8_t event, uint8_t mode, void *data), void *data)
+{
+ _pm_notify.notify = notify;
+ _pm_notify.data = data;
+}
+
+/**
+ * This function set default sleep mode when no pm_request
+ */
+void rt_pm_default_set(uint8_t sleep_mode)
+{
+ _pm_default_sleep = sleep_mode;
+}
+
+/**
* RT-Thread device interface for PM device
*/
static rt_size_t _rt_pm_device_read(rt_device_t dev,
@@ -377,7 +371,7 @@ static rt_size_t _rt_pm_device_read(rt_device_t dev,
pm = (struct rt_pm *)dev;
RT_ASSERT(pm != RT_NULL);
- if (pos <= PM_MODE_MAX)
+ if (pos < PM_SLEEP_MODE_MAX)
{
int mode;
@@ -399,11 +393,11 @@ static rt_size_t _rt_pm_device_write(rt_device_t dev,
{
/* get request */
request = *(unsigned char *)buffer;
- if (request == '1')
+ if (request == 0x01)
{
rt_pm_request(pos);
}
- else if (request == '0')
+ else if (request == 0x00)
{
rt_pm_release(pos);
}
@@ -434,6 +428,36 @@ static rt_err_t _rt_pm_device_control(rt_device_t dev,
return RT_EOK;
}
+int rt_pm_run_enter(uint8_t mode)
+{
+ rt_base_t level;
+ struct rt_pm *pm;
+
+ if (_pm_init_flag == 0)
+ return -RT_EIO;
+
+ if (mode > PM_RUN_MODE_MAX)
+ return -RT_EINVAL;
+
+ level = rt_hw_interrupt_disable();
+ pm = &_pm;
+ if (mode < pm->run_mode)
+ {
+ /* change system runing mode */
+ pm->ops->run(pm, mode);
+ /* changer device frequency */
+ _pm_device_frequency_change(mode);
+ }
+ else
+ {
+ pm->flags |= RT_PM_FREQUENCY_PENDING;
+ }
+ pm->run_mode = mode;
+ rt_hw_interrupt_enable(level);
+
+ return RT_EOK;
+}
+
/**
* This function will initialize power management.
*
@@ -466,12 +490,9 @@ void rt_system_pm_init(const struct rt_pm_ops *ops,
/* register PM device to the system */
rt_device_register(device, "pm", RT_DEVICE_FLAG_RDWR);
- /* todo : add to kernel source code */
- rt_thread_idle_sethook(rt_pm_enter);
-
rt_memset(pm->modes, 0, sizeof(pm->modes));
- pm->current_mode = PM_RUN_MODE_DEFAULT;
-
+ pm->sleep_mode = _pm_default_sleep;
+ pm->run_mode = RT_PM_DEFAULT_RUN_MODE;
pm->timer_mask = timer_mask;
pm->ops = ops;
@@ -479,24 +500,15 @@ void rt_system_pm_init(const struct rt_pm_ops *ops,
pm->device_pm = RT_NULL;
pm->device_pm_number = 0;
- /* initialize semaphore */
- rt_sem_init(&(pm->device_lock), "pm", 1, RT_IPC_FLAG_FIFO);
-
- /* request in default running mode */
- rt_pm_request(PM_RUN_MODE_DEFAULT);
-
-#ifdef PM_SLEEP_MODE_DEFAULT
- /* request in default sleep mode */
- rt_pm_request(PM_SLEEP_MODE_DEFAULT);
-#endif
-
- /* must hold on deep shutdown mode */
- rt_pm_request(PM_MODE_MAX);
+ _pm_init_flag = 1;
}
#ifdef RT_USING_FINSH
#include <finsh.h>
+static const char *_pm_sleep_str[] = PM_SLEEP_MODE_NAMES;
+static const char *_pm_run_str[] = PM_RUN_MODE_NAMES;
+
static void rt_pm_release_mode(int argc, char **argv)
{
int mode = 0;
@@ -521,9 +533,20 @@ static void rt_pm_request_mode(int argc, char **argv)
}
MSH_CMD_EXPORT_ALIAS(rt_pm_request_mode, pm_request, request power management mode);
+static void rt_pm_run_mode_switch(int argc, char **argv)
+{
+ int mode = 0;
+ if (argc >= 2)
+ {
+ mode = atoi(argv[1]);
+ }
+
+ rt_pm_run_enter(mode);
+}
+MSH_CMD_EXPORT_ALIAS(rt_pm_run_mode_switch, pm_run, switch power management run mode);
+
static void rt_pm_dump_status(void)
{
- static const char *pm_str[] = PM_MODE_NAMES;
rt_uint32_t index;
struct rt_pm *pm;
@@ -531,17 +554,18 @@ static void rt_pm_dump_status(void)
rt_kprintf("| Power Management Mode | Counter | Timer |\n");
rt_kprintf("+-----------------------+---------+-------+\n");
- for (index = 0; index <= PM_MODE_MAX; index ++)
+ for (index = 0; index < PM_SLEEP_MODE_MAX; index ++)
{
int has_timer = 0;
if (pm->timer_mask & (1 << index))
has_timer = 1;
- rt_kprintf("| %021s | %7d | %5d |\n", pm_str[index], pm->modes[index], has_timer);
+ rt_kprintf("| %021s | %7d | %5d |\n", _pm_sleep_str[index], pm->modes[index], has_timer);
}
rt_kprintf("+-----------------------+---------+-------+\n");
- rt_kprintf("pm current mode: %s\n", pm_str[pm->current_mode]);
+ rt_kprintf("pm current sleep mode: %s\n", _pm_sleep_str[pm->sleep_mode]);
+ rt_kprintf("pm current run mode: %s\n", _pm_run_str[pm->run_mode]);
}
FINSH_FUNCTION_EXPORT_ALIAS(rt_pm_dump_status, pm_dump, dump power management status);
MSH_CMD_EXPORT_ALIAS(rt_pm_dump_status, pm_dump, dump power management status);
diff --git a/components/drivers/sdio/sd.c b/components/drivers/sdio/sd.c
index 3e7ad1cd16..183eba169d 100644
--- a/components/drivers/sdio/sd.c
+++ b/components/drivers/sdio/sd.c
@@ -264,7 +264,7 @@ static rt_int32_t mmcsd_switch(struct rt_mmcsd_card *card)
if ((buf[16] & 0xF) != 1)
{
- LOG_E("switching card to high speed failed!");
+ LOG_I("switching card to high speed failed!");
goto err;
}
diff --git a/components/drivers/sensors/sensor.h b/components/drivers/sensors/sensor.h
index 4f8ee47eb8..a9b6684457 100755
--- a/components/drivers/sensors/sensor.h
+++ b/components/drivers/sensors/sensor.h
@@ -19,7 +19,7 @@ extern "C" {
#endif
#ifdef RT_USING_RTC
-#define rt_sensor_get_ts() time() /* API for the sensor to get the timestamp */
+#define rt_sensor_get_ts() time(RT_NULL) /* API for the sensor to get the timestamp */
#else
#define rt_sensor_get_ts() rt_tick_get() /* API for the sensor to get the timestamp */
#endif
diff --git a/components/drivers/sensors/sensor_cmd.c b/components/drivers/sensors/sensor_cmd.c
index 2760892cfe..6babf12db3 100755
--- a/components/drivers/sensors/sensor_cmd.c
+++ b/components/drivers/sensors/sensor_cmd.c
@@ -24,13 +24,13 @@ static void sensor_show_data(rt_size_t num, rt_sensor_t sensor, struct rt_sensor
switch (sensor->info.type)
{
case RT_SENSOR_CLASS_ACCE:
- LOG_I("num:%3d, x:%5d, y:%5d, z:%5d, timestamp:%5d", num, sensor_data->data.acce.x, sensor_data->data.acce.y, sensor_data->data.acce.z, sensor_data->timestamp);
+ LOG_I("num:%3d, x:%5d, y:%5d, z:%5d mg, timestamp:%5d", num, sensor_data->data.acce.x, sensor_data->data.acce.y, sensor_data->data.acce.z, sensor_data->timestamp);
break;
case RT_SENSOR_CLASS_GYRO:
- LOG_I("num:%3d, x:%8d, y:%8d, z:%8d, timestamp:%5d", num, sensor_data->data.gyro.x, sensor_data->data.gyro.y, sensor_data->data.gyro.z, sensor_data->timestamp);
+ LOG_I("num:%3d, x:%8d, y:%8d, z:%8d dps, timestamp:%5d", num, sensor_data->data.gyro.x / 1000, sensor_data->data.gyro.y / 1000, sensor_data->data.gyro.z / 1000, sensor_data->timestamp);
break;
case RT_SENSOR_CLASS_MAG:
- LOG_I("num:%3d, x:%5d, y:%5d, z:%5d, timestamp:%5d", num, sensor_data->data.mag.x, sensor_data->data.mag.y, sensor_data->data.mag.z, sensor_data->timestamp);
+ LOG_I("num:%3d, x:%5d, y:%5d, z:%5d mGauss, timestamp:%5d", num, sensor_data->data.mag.x, sensor_data->data.mag.y, sensor_data->data.mag.z, sensor_data->timestamp);
break;
case RT_SENSOR_CLASS_HUMI:
LOG_I("num:%3d, humi:%3d.%d%%, timestamp:%5d", num, sensor_data->data.humi / 10, sensor_data->data.humi % 10, sensor_data->timestamp);
@@ -39,7 +39,7 @@ static void sensor_show_data(rt_size_t num, rt_sensor_t sensor, struct rt_sensor
LOG_I("num:%3d, temp:%3d.%dC, timestamp:%5d", num, sensor_data->data.temp / 10, sensor_data->data.temp % 10, sensor_data->timestamp);
break;
case RT_SENSOR_CLASS_BARO:
- LOG_I("num:%3d, press:%5d, timestamp:%5d", num, sensor_data->data.baro, sensor_data->timestamp);
+ LOG_I("num:%3d, press:%5d pa, timestamp:%5d", num, sensor_data->data.baro, sensor_data->timestamp);
break;
case RT_SENSOR_CLASS_STEP:
LOG_I("num:%3d, step:%5d, timestamp:%5d", num, sensor_data->data.step, sensor_data->timestamp);
diff --git a/components/drivers/serial/serial.c b/components/drivers/serial/serial.c
index cacaf1c9ac..c3e96fc77b 100644
--- a/components/drivers/serial/serial.c
+++ b/components/drivers/serial/serial.c
@@ -675,6 +675,7 @@ static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
serial->serial_tx = tx_dma;
dev->open_flag |= RT_DEVICE_FLAG_DMA_TX;
+ /* configure low level device */
serial->ops->control(serial, RT_DEVICE_CTRL_CONFIG, (void *)RT_DEVICE_FLAG_DMA_TX);
}
#endif /* RT_SERIAL_USING_DMA */
diff --git a/components/libc/Kconfig b/components/libc/Kconfig
index b2abe69850..43d9330102 100644
--- a/components/libc/Kconfig
+++ b/components/libc/Kconfig
@@ -8,6 +8,12 @@ config RT_USING_PTHREADS
bool "Enable pthreads APIs"
default n
+if RT_USING_PTHREADS
+ config PTHREAD_NUM_MAX
+ int "Maximum number of pthreads"
+ default 8
+endif
+
if RT_USING_LIBC && RT_USING_DFS
config RT_USING_POSIX
bool "Enable POSIX layer for poll/select, stdin etc"
diff --git a/components/libc/pthreads/pthread.c b/components/libc/pthreads/pthread.c
index 55f46c0caa..7ecf304038 100644
--- a/components/libc/pthreads/pthread.c
+++ b/components/libc/pthreads/pthread.c
@@ -10,10 +10,121 @@
* 2019-02-07 Bernard Add _pthread_destroy to release pthread resource.
*/
+#include <rthw.h>
#include <pthread.h>
#include <sched.h>
#include "pthread_internal.h"
+RT_DEFINE_SPINLOCK(pth_lock);
+_pthread_data_t *pth_table[PTHREAD_NUM_MAX] = {NULL};
+
+_pthread_data_t *_pthread_get_data(pthread_t thread)
+{
+ RT_DECLARE_SPINLOCK(pth_lock);
+ _pthread_data_t *ptd;
+
+ if (thread >= PTHREAD_NUM_MAX) return NULL;
+
+ rt_hw_spin_lock(&pth_lock);
+ ptd = pth_table[thread];
+ rt_hw_spin_unlock(&pth_lock);
+
+ if (ptd && ptd->magic == PTHREAD_MAGIC) return ptd;
+
+ return NULL;
+}
+
+pthread_t _pthread_data_get_pth(_pthread_data_t *ptd)
+{
+ int index;
+ RT_DECLARE_SPINLOCK(pth_lock);
+
+ rt_hw_spin_lock(&pth_lock);
+ for (index = 0; index < PTHREAD_NUM_MAX; index ++)
+ {
+ if (pth_table[index] == ptd) break;
+ }
+ rt_hw_spin_unlock(&pth_lock);
+
+ return index;
+}
+
+pthread_t _pthread_data_create(void)
+{
+ int index;
+ _pthread_data_t *ptd = NULL;
+ RT_DECLARE_SPINLOCK(pth_lock);
+
+ ptd = (_pthread_data_t*)rt_malloc(sizeof(_pthread_data_t));
+ if (!ptd) return PTHREAD_NUM_MAX;
+
+ memset(ptd, 0x0, sizeof(_pthread_data_t));
+ ptd->canceled = 0;
+ ptd->cancelstate = PTHREAD_CANCEL_DISABLE;
+ ptd->canceltype = PTHREAD_CANCEL_DEFERRED;
+ ptd->magic = PTHREAD_MAGIC;
+
+ rt_hw_spin_lock(&pth_lock);
+ for (index = 0; index < PTHREAD_NUM_MAX; index ++)
+ {
+ if (pth_table[index] == NULL)
+ {
+ pth_table[index] = ptd;
+ break;
+ }
+ }
+ rt_hw_spin_unlock(&pth_lock);
+
+ /* full of pthreads, clean magic and release ptd */
+ if (index == PTHREAD_NUM_MAX)
+ {
+ ptd->magic = 0x0;
+ rt_free(ptd);
+ }
+
+ return index;
+}
+
+void _pthread_data_destroy(pthread_t pth)
+{
+ RT_DECLARE_SPINLOCK(pth_lock);
+
+ _pthread_data_t *ptd = _pthread_get_data(pth);
+ if (ptd)
+ {
+ /* remove from pthread table */
+ rt_hw_spin_lock(&pth_lock);
+ pth_table[pth] = NULL;
+ rt_hw_spin_unlock(&pth_lock);
+
+ /* delete joinable semaphore */
+ if (ptd->joinable_sem != RT_NULL)
+ rt_sem_delete(ptd->joinable_sem);
+
+ /* release thread resource */
+ if (ptd->attr.stackaddr == RT_NULL)
+ {
+ /* release thread allocated stack */
+ rt_free(ptd->tid->stack_addr);
+ }
+ /* clean stack addr pointer */
+ ptd->tid->stack_addr = RT_NULL;
+
+ /*
+ * if this thread create the local thread data,
+ * delete it
+ */
+ if (ptd->tls != RT_NULL) rt_free(ptd->tls);
+ rt_free(ptd->tid);
+
+ /* clean magic */
+ ptd->magic = 0x0;
+
+ /* free ptd */
+ rt_free(ptd);
+ }
+}
+
int pthread_system_init(void)
{
/* initialize key area */
@@ -29,26 +140,11 @@ INIT_COMPONENT_EXPORT(pthread_system_init);
static void _pthread_destroy(_pthread_data_t *ptd)
{
- /* delete joinable semaphore */
- if (ptd->joinable_sem != RT_NULL)
- rt_sem_delete(ptd->joinable_sem);
-
- /* release thread resource */
- if (ptd->attr.stack_base == RT_NULL)
+ pthread_t pth = _pthread_data_get_pth(ptd);
+ if (pth != PTHREAD_NUM_MAX)
{
- /* release thread allocated stack */
- rt_free(ptd->tid->stack_addr);
+ _pthread_data_destroy(pth);
}
- /* clean stack addr pointer */
- ptd->tid->stack_addr = RT_NULL;
-
- /*
- * if this thread create the local thread data,
- * delete it
- */
- if (ptd->tls != RT_NULL) rt_free(ptd->tls);
- rt_free(ptd->tid);
- rt_free(ptd);
return;
}
@@ -56,7 +152,10 @@ static void _pthread_destroy(_pthread_data_t *ptd)
static void _pthread_cleanup(rt_thread_t tid)
{
_pthread_data_t *ptd;
- ptd = _pthread_get_data(tid);
+
+ /* get pthread data from user data of thread */
+ ptd = (_pthread_data_t *)tid->user_data;
+ RT_ASSERT(ptd != RT_NULL);
/* clear cleanup function */
tid->cleanup = RT_NULL;
@@ -84,29 +183,30 @@ static void pthread_entry_stub(void *parameter)
ptd->return_value = value;
}
-int pthread_create(pthread_t *tid,
+int pthread_create(pthread_t *pid,
const pthread_attr_t *attr,
void *(*start)(void *), void *parameter)
{
- int result;
+ int ret = 0;
void *stack;
char name[RT_NAME_MAX];
static rt_uint16_t pthread_number = 0;
+
+ pthread_t pth_id;
_pthread_data_t *ptd;
- /* tid shall be provided */
- RT_ASSERT(tid != RT_NULL);
+ /* pid shall be provided */
+ RT_ASSERT(pid != RT_NULL);
/* allocate posix thread data */
- ptd = (_pthread_data_t *)rt_malloc(sizeof(_pthread_data_t));
- if (ptd == RT_NULL)
- return ENOMEM;
- /* clean posix thread data memory */
- rt_memset(ptd, 0, sizeof(_pthread_data_t));
- ptd->canceled = 0;
- ptd->cancelstate = PTHREAD_CANCEL_DISABLE;
- ptd->canceltype = PTHREAD_CANCEL_DEFERRED;
- ptd->magic = PTHREAD_MAGIC;
+ pth_id = _pthread_data_create();
+ if (pth_id == PTHREAD_NUM_MAX)
+ {
+ ret = ENOMEM;
+ goto __exit;
+ }
+ /* get pthread data */
+ ptd = _pthread_get_data(pth_id);
if (attr != RT_NULL)
{
@@ -119,31 +219,27 @@ int pthread_create(pthread_t *tid,
}
rt_snprintf(name, sizeof(name), "pth%02d", pthread_number ++);
- if (ptd->attr.stack_base == 0)
+ if (ptd->attr.stackaddr == 0)
{
- stack = (void *)rt_malloc(ptd->attr.stack_size);
+ stack = (void *)rt_malloc(ptd->attr.stacksize);
}
else
{
- stack = (void *)(ptd->attr.stack_base);
+ stack = (void *)(ptd->attr.stackaddr);
}
if (stack == RT_NULL)
{
- rt_free(ptd);
-
- return ENOMEM;
+ ret = ENOMEM;
+ goto __exit;
}
/* pthread is a static thread object */
ptd->tid = (rt_thread_t) rt_malloc(sizeof(struct rt_thread));
if (ptd->tid == RT_NULL)
{
- if (ptd->attr.stack_base == 0)
- rt_free(stack);
- rt_free(ptd);
-
- return ENOMEM;
+ ret = ENOMEM;
+ goto __exit;
}
if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
@@ -151,11 +247,8 @@ int pthread_create(pthread_t *tid,
ptd->joinable_sem = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO);
if (ptd->joinable_sem == RT_NULL)
{
- if (ptd->attr.stack_base != 0)
- rt_free(stack);
- rt_free(ptd);
-
- return ENOMEM;
+ ret = ENOMEM;
+ goto __exit;
}
}
else
@@ -169,40 +262,32 @@ int pthread_create(pthread_t *tid,
/* initial this pthread to system */
if (rt_thread_init(ptd->tid, name, pthread_entry_stub, ptd,
- stack, ptd->attr.stack_size,
- ptd->attr.priority, 5) != RT_EOK)
+ stack, ptd->attr.stacksize,
+ ptd->attr.schedparam.sched_priority, 5) != RT_EOK)
{
- if (ptd->attr.stack_base == 0)
- rt_free(stack);
- if (ptd->joinable_sem != RT_NULL)
- rt_sem_delete(ptd->joinable_sem);
- rt_free(ptd);
-
- return EINVAL;
+ ret = EINVAL;
+ goto __exit;
}
/* set pthread id */
- *tid = ptd->tid;
+ *pid = pth_id;
/* set pthread cleanup function and ptd data */
- (*tid)->cleanup = _pthread_cleanup;
- (*tid)->user_data = (rt_uint32_t)ptd;
+ ptd->tid->cleanup = _pthread_cleanup;
+ ptd->tid->user_data = (rt_uint32_t)ptd;
/* start thread */
- result = rt_thread_startup(*tid);
- if (result == RT_EOK)
+ if (rt_thread_startup(ptd->tid) == RT_EOK)
return 0;
/* start thread failed */
rt_thread_detach(ptd->tid);
- if (ptd->attr.stack_base == 0)
- rt_free(stack);
- if (ptd->joinable_sem != RT_NULL)
- rt_sem_delete(ptd->joinable_sem);
-
- rt_free(ptd);
+ ret = EINVAL;
- return EINVAL;
+__exit:
+ if (pth_id != PTHREAD_NUM_MAX)
+ _pthread_data_destroy(pth_id);
+ return ret;
}
RTM_EXPORT(pthread_create);
@@ -221,7 +306,7 @@ int pthread_detach(pthread_t thread)
goto __exit;
}
- if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
+ if ((ptd->tid->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
{
/* this defunct pthread is not handled by idle */
if (rt_sem_trytake(ptd->joinable_sem) != RT_EOK)
@@ -270,13 +355,13 @@ int pthread_join(pthread_t thread, void **value_ptr)
_pthread_data_t *ptd;
rt_err_t result;
- if (thread == rt_thread_self())
+ ptd = _pthread_get_data(thread);
+ if (ptd && ptd->tid == rt_thread_self())
{
/* join self */
return EDEADLK;
}
- ptd = _pthread_get_data(thread);
if (ptd->attr.detachstate == PTHREAD_CREATE_DETACHED)
return EINVAL; /* join on a detached pthread */
@@ -299,13 +384,32 @@ int pthread_join(pthread_t thread, void **value_ptr)
}
RTM_EXPORT(pthread_join);
+pthread_t pthread_self (void)
+{
+ rt_thread_t tid;
+ _pthread_data_t *ptd;
+
+ tid = rt_thread_self();
+ if (tid == NULL) return PTHREAD_NUM_MAX;
+
+ /* get pthread data from user data of thread */
+ ptd = (_pthread_data_t *)rt_thread_self()->user_data;
+ RT_ASSERT(ptd != RT_NULL);
+
+ return _pthread_data_get_pth(ptd);
+}
+RTM_EXPORT(pthread_self);
+
void pthread_exit(void *value)
{
_pthread_data_t *ptd;
_pthread_cleanup_t *cleanup;
extern _pthread_key_data_t _thread_keys[PTHREAD_KEY_MAX];
- ptd = _pthread_get_data(rt_thread_self());
+ if (rt_thread_self() == NULL) return;
+
+ /* get pthread data from user data of thread */
+ ptd = (_pthread_data_t *)rt_thread_self()->user_data;
rt_enter_critical();
/* disable cancel */
@@ -382,7 +486,15 @@ RTM_EXPORT(pthread_atfork);
int pthread_kill(pthread_t thread, int sig)
{
#ifdef RT_USING_SIGNALS
- return rt_thread_kill(thread, sig);
+ _pthread_data_t *ptd;
+
+ ptd = _pthread_get_data(thread);
+ if (ptd)
+ {
+ return rt_thread_kill(ptd->tid, sig);
+ }
+
+ return EINVAL;
#else
return ENOSYS;
#endif
@@ -401,8 +513,10 @@ void pthread_cleanup_pop(int execute)
_pthread_data_t *ptd;
_pthread_cleanup_t *cleanup;
- /* get posix thread data */
- ptd = _pthread_get_data(rt_thread_self());
+ if (rt_thread_self() == NULL) return;
+
+ /* get pthread data from user data of thread */
+ ptd = (_pthread_data_t *)rt_thread_self()->user_data;
RT_ASSERT(ptd != RT_NULL);
if (execute)
@@ -428,8 +542,10 @@ void pthread_cleanup_push(void (*routine)(void *), void *arg)
_pthread_data_t *ptd;
_pthread_cleanup_t *cleanup;
- /* get posix thread data */
- ptd = _pthread_get_data(rt_thread_self());
+ if (rt_thread_self() == NULL) return;
+
+ /* get pthread data from user data of thread */
+ ptd = (_pthread_data_t *)rt_thread_self()->user_data;
RT_ASSERT(ptd != RT_NULL);
cleanup = (_pthread_cleanup_t *)rt_malloc(sizeof(_pthread_cleanup_t));
@@ -478,8 +594,10 @@ int pthread_setcancelstate(int state, int *oldstate)
{
_pthread_data_t *ptd;
- /* get posix thread data */
- ptd = _pthread_get_data(rt_thread_self());
+ if (rt_thread_self() == NULL) return EINVAL;
+
+ /* get pthread data from user data of thread */
+ ptd = (_pthread_data_t *)rt_thread_self()->user_data;
RT_ASSERT(ptd != RT_NULL);
if ((state == PTHREAD_CANCEL_ENABLE) || (state == PTHREAD_CANCEL_DISABLE))
@@ -499,8 +617,10 @@ int pthread_setcanceltype(int type, int *oldtype)
{
_pthread_data_t *ptd;
- /* get posix thread data */
- ptd = _pthread_get_data(rt_thread_self());
+ if (rt_thread_self() == NULL) return EINVAL;
+
+ /* get pthread data from user data of thread */
+ ptd = (_pthread_data_t *)rt_thread_self()->user_data;
RT_ASSERT(ptd != RT_NULL);
if ((type != PTHREAD_CANCEL_DEFERRED) && (type != PTHREAD_CANCEL_ASYNCHRONOUS))
@@ -519,8 +639,10 @@ void pthread_testcancel(void)
int cancel = 0;
_pthread_data_t *ptd;
- /* get posix thread data */
- ptd = _pthread_get_data(rt_thread_self());
+ if (rt_thread_self() == NULL) return;
+
+ /* get pthread data from user data of thread */
+ ptd = (_pthread_data_t *)rt_thread_self()->user_data;
RT_ASSERT(ptd != RT_NULL);
if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE)
@@ -534,14 +656,14 @@ int pthread_cancel(pthread_t thread)
{
_pthread_data_t *ptd;
- /* cancel self */
- if (thread == rt_thread_self())
- return 0;
-
/* get posix thread data */
ptd = _pthread_get_data(thread);
RT_ASSERT(ptd != RT_NULL);
+ /* cancel self */
+ if (ptd->tid == rt_thread_self())
+ return 0;
+
/* set canceled */
if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE)
{
@@ -555,10 +677,11 @@ int pthread_cancel(pthread_t thread)
* thread (pthread_cleanup), it will move to defunct
* thread list and wait for handling in idle thread.
*/
- rt_thread_detach(thread);
+ rt_thread_detach(ptd->tid);
}
}
return 0;
}
RTM_EXPORT(pthread_cancel);
+
diff --git a/components/libc/pthreads/pthread.h b/components/libc/pthreads/pthread.h
index f9a0a92b68..7bb72b726c 100644
--- a/components/libc/pthreads/pthread.h
+++ b/components/libc/pthreads/pthread.h
@@ -32,7 +32,7 @@ extern "C" {
#define PTHREAD_EXPLICIT_SCHED 0
#define PTHREAD_INHERIT_SCHED 1
-typedef rt_thread_t pthread_t;
+typedef long pthread_t;
typedef long pthread_condattr_t;
typedef long pthread_rwlockattr_t;
typedef long pthread_mutexattr_t;
@@ -76,15 +76,21 @@ enum
#define PTHREAD_SCOPE_PROCESS 0
#define PTHREAD_SCOPE_SYSTEM 1
+struct sched_param
+{
+ int sched_priority;
+};
+
struct pthread_attr
{
- void* stack_base;
- rt_uint32_t stack_size; /* stack size of thread */
+ void* stackaddr; /* stack address of thread */
+ int stacksize; /* stack size of thread */
+
+ int inheritsched; /* Inherit parent prio/policy */
+ int schedpolicy; /* scheduler policy */
+ struct sched_param schedparam; /* sched parameter */
- rt_uint8_t priority; /* priority of thread */
- rt_uint8_t detachstate; /* detach state */
- rt_uint8_t policy; /* scheduler policy */
- rt_uint8_t inheritsched; /* Inherit parent prio/policy */
+ int detachstate; /* detach state */
};
typedef struct pthread_attr pthread_attr_t;
@@ -131,11 +137,6 @@ struct pthread_barrier
};
typedef struct pthread_barrier pthread_barrier_t;
-struct sched_param
-{
- int sched_priority;
-};
-
/* pthread thread interface */
int pthread_attr_destroy(pthread_attr_t *attr);
int pthread_attr_init(pthread_attr_t *attr);
@@ -171,10 +172,7 @@ rt_inline int pthread_equal (pthread_t t1, pthread_t t2)
return t1 == t2;
}
-rt_inline pthread_t pthread_self (void)
-{
- return rt_thread_self();
-}
+pthread_t pthread_self (void);
void pthread_exit (void *value_ptr);
int pthread_once(pthread_once_t * once_control, void (*init_routine) (void));
diff --git a/components/libc/pthreads/pthread_attr.c b/components/libc/pthreads/pthread_attr.c
index 8eed35932f..2294599bf3 100644
--- a/components/libc/pthreads/pthread_attr.c
+++ b/components/libc/pthreads/pthread_attr.c
@@ -20,10 +20,13 @@ const pthread_attr_t pthread_default_attr =
{
0, /* stack base */
DEFAULT_STACK_SIZE, /* stack size */
- DEFAULT_PRIORITY, /* priority */
- PTHREAD_CREATE_JOINABLE, /* detach state */
+
+ PTHREAD_INHERIT_SCHED, /* Inherit parent prio/policy */
SCHED_FIFO, /* scheduler policy */
- PTHREAD_INHERIT_SCHED /* Inherit parent prio/policy */
+ {
+ DEFAULT_PRIORITY, /* scheduler priority */
+ },
+ PTHREAD_CREATE_JOINABLE, /* detach state */
};
int pthread_attr_init(pthread_attr_t *attr)
@@ -73,7 +76,7 @@ int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
{
RT_ASSERT(attr != RT_NULL);
- attr->policy = policy;
+ attr->schedpolicy = policy;
return 0;
}
@@ -83,7 +86,7 @@ int pthread_attr_getschedpolicy(pthread_attr_t const *attr, int *policy)
{
RT_ASSERT(attr != RT_NULL);
- *policy = (int)attr->policy;
+ *policy = (int)attr->schedpolicy;
return 0;
}
@@ -95,7 +98,7 @@ int pthread_attr_setschedparam(pthread_attr_t *attr,
RT_ASSERT(attr != RT_NULL);
RT_ASSERT(param != RT_NULL);
- attr->priority = param->sched_priority;
+ attr->schedparam.sched_priority = param->sched_priority;
return 0;
}
@@ -107,7 +110,7 @@ int pthread_attr_getschedparam(pthread_attr_t const *attr,
RT_ASSERT(attr != RT_NULL);
RT_ASSERT(param != RT_NULL);
- param->sched_priority = attr->priority;
+ param->sched_priority = attr->schedparam.sched_priority;
return 0;
}
@@ -117,7 +120,7 @@ int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stack_size)
{
RT_ASSERT(attr != RT_NULL);
- attr->stack_size = stack_size;
+ attr->stacksize = stack_size;
return 0;
}
@@ -127,7 +130,7 @@ int pthread_attr_getstacksize(pthread_attr_t const *attr, size_t *stack_size)
{
RT_ASSERT(attr != RT_NULL);
- *stack_size = attr->stack_size;
+ *stack_size = attr->stacksize;
return 0;
}
@@ -155,8 +158,8 @@ int pthread_attr_setstack(pthread_attr_t *attr,
{
RT_ASSERT(attr != RT_NULL);
- attr->stack_base = stack_base;
- attr->stack_size = RT_ALIGN_DOWN(stack_size, RT_ALIGN_SIZE);
+ attr->stackaddr = stack_base;
+ attr->stacksize = RT_ALIGN_DOWN(stack_size, RT_ALIGN_SIZE);
return 0;
}
@@ -168,8 +171,8 @@ int pthread_attr_getstack(pthread_attr_t const *attr,
{
RT_ASSERT(attr != RT_NULL);
- *stack_base = attr->stack_base;
- *stack_size = attr->stack_size;
+ *stack_base = attr->stackaddr;
+ *stack_size = attr->stacksize;
return 0;
}
diff --git a/components/libc/pthreads/pthread_cond.c b/components/libc/pthreads/pthread_cond.c
index d3c4ac7637..38dc697469 100644
--- a/components/libc/pthreads/pthread_cond.c
+++ b/components/libc/pthreads/pthread_cond.c
@@ -189,7 +189,7 @@ rt_err_t _pthread_cond_timedwait(pthread_cond_t *cond,
pthread_cond_init(cond, RT_NULL);
/* The mutex was not owned by the current thread at the time of the call. */
- if (mutex->lock.owner != pthread_self())
+ if (mutex->lock.owner != rt_thread_self())
return -RT_ERROR;
/* unlock a mutex failed */
if (pthread_mutex_unlock(mutex) != 0)
diff --git a/components/libc/pthreads/pthread_internal.h b/components/libc/pthreads/pthread_internal.h
index 3e94e867d7..a6706ca091 100644
--- a/components/libc/pthreads/pthread_internal.h
+++ b/components/libc/pthreads/pthread_internal.h
@@ -30,6 +30,10 @@ struct _pthread_key_data
};
typedef struct _pthread_key_data _pthread_key_data_t;
+#ifndef PTHREAD_NUM_MAX
+#define PTHREAD_NUM_MAX 32
+#endif
+
#define PTHREAD_MAGIC 0x70746873
struct _pthread_data
{
@@ -56,17 +60,7 @@ struct _pthread_data
};
typedef struct _pthread_data _pthread_data_t;
-rt_inline _pthread_data_t *_pthread_get_data(pthread_t thread)
-{
- _pthread_data_t *ptd;
- RT_ASSERT(thread != RT_NULL);
-
- ptd = (_pthread_data_t *)thread->user_data;
- RT_ASSERT(ptd != RT_NULL);
- RT_ASSERT(ptd->magic == PTHREAD_MAGIC);
-
- return ptd;
-}
+_pthread_data_t *_pthread_get_data(pthread_t thread);
int clock_time_to_tick(const struct timespec *time);
diff --git a/components/libc/pthreads/pthread_tls.c b/components/libc/pthreads/pthread_tls.c
index c6d0bca155..4b97d796d9 100644
--- a/components/libc/pthreads/pthread_tls.c
+++ b/components/libc/pthreads/pthread_tls.c
@@ -22,7 +22,10 @@ void *pthread_getspecific(pthread_key_t key)
{
struct _pthread_data* ptd;
- ptd = _pthread_get_data(rt_thread_self());
+ if (rt_thread_self() == NULL) return NULL;
+
+ /* get pthread data from user data of thread */
+ ptd = (_pthread_data_t *)rt_thread_self()->user_data;
RT_ASSERT(ptd != NULL);
if (ptd->tls == NULL)
@@ -39,7 +42,10 @@ int pthread_setspecific(pthread_key_t key, const void *value)
{
struct _pthread_data* ptd;
- ptd = _pthread_get_data(rt_thread_self());
+ if (rt_thread_self() == NULL) return EINVAL;
+
+ /* get pthread data from user data of thread */
+ ptd = (_pthread_data_t *)rt_thread_self()->user_data;
RT_ASSERT(ptd != NULL);
/* check tls area */
diff --git a/components/net/Kconfig b/components/net/Kconfig
index a719f7be6f..68737176ed 100644
--- a/components/net/Kconfig
+++ b/components/net/Kconfig
@@ -5,6 +5,7 @@ menu "Socket abstraction layer"
config RT_USING_SAL
bool "Enable socket abstraction layer"
select RT_USING_NETDEV
+ select RT_USING_LIBC
select RT_USING_SYSTEM_WORKQUEUE
default n
@@ -36,7 +37,6 @@ config RT_USING_SAL
bool "Enable BSD socket operated by file system API"
default n
select RT_USING_DFS
- select RT_USING_LIBC
select RT_USING_POSIX
help
Let BSD socket operated by file system API, such as read/write and involveed in select/poll POSIX APIs.
@@ -73,6 +73,9 @@ config RT_USING_NETDEV
bool "Enable netstat features"
default y
+ config NETDEV_USING_AUTO_DEFAULT
+ bool "Enable default netdev automatic change features"
+ default y
endif
endmenu
diff --git a/components/net/freemodbus/port/portserial.c b/components/net/freemodbus/port/portserial.c
index 7c546a1691..cbd846d7ff 100644
--- a/components/net/freemodbus/port/portserial.c
+++ b/components/net/freemodbus/port/portserial.c
@@ -52,6 +52,8 @@ static void serial_soft_trans_irq(void* parameter);
BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
eMBParity eParity)
{
+ rt_device_t dev = RT_NULL;
+ char uart_name[20];
/**
* set 485 mode receive and transmit control IO
* @note MODBUS_SLAVE_RT_CONTROL_PIN_INDEX need be defined by user
@@ -60,22 +62,19 @@ BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
rt_pin_mode(MODBUS_SLAVE_RT_CONTROL_PIN_INDEX, PIN_MODE_OUTPUT);
#endif
/* set serial name */
- if (ucPORT == 1) {
-#if defined(RT_USING_UART1) || defined(RT_USING_REMAP_UART1)
- extern struct rt_serial_device serial1;
- serial = &serial1;
-#endif
- } else if (ucPORT == 2) {
-#if defined(RT_USING_UART2)
- extern struct rt_serial_device serial2;
- serial = &serial2;
-#endif
- } else if (ucPORT == 3) {
-#if defined(RT_USING_UART3)
- extern struct rt_serial_device serial3;
- serial = &serial3;
-#endif
+ rt_snprintf(uart_name,sizeof(uart_name), "uart%d", ucPORT);
+
+ dev = rt_device_find(uart_name);
+ if(dev == RT_NULL)
+ {
+ /* can not find uart */
+ return FALSE;
}
+ else
+ {
+ serial = (struct rt_serial_device*)dev;
+ }
+
/* set serial configure parameter */
serial->config.baud_rate = ulBaudRate;
serial->config.stop_bits = STOP_BITS_1;
diff --git a/components/net/freemodbus/port/portserial_m.c b/components/net/freemodbus/port/portserial_m.c
index f68794a617..47a70dd905 100644
--- a/components/net/freemodbus/port/portserial_m.c
+++ b/components/net/freemodbus/port/portserial_m.c
@@ -53,6 +53,9 @@ static void serial_soft_trans_irq(void* parameter);
BOOL xMBMasterPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
eMBParity eParity)
{
+ rt_device_t dev = RT_NULL;
+ char uart_name[20];
+
/**
* set 485 mode receive and transmit control IO
* @note MODBUS_MASTER_RT_CONTROL_PIN_INDEX need be defined by user
@@ -60,24 +63,20 @@ BOOL xMBMasterPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
#if defined(RT_MODBUS_MASTER_USE_CONTROL_PIN)
rt_pin_mode(MODBUS_MASTER_RT_CONTROL_PIN_INDEX, PIN_MODE_OUTPUT);
#endif
-
/* set serial name */
- if (ucPORT == 1) {
-#if defined(RT_USING_UART1) || defined(RT_USING_REMAP_UART1)
- extern struct rt_serial_device serial1;
- serial = &serial1;
-#endif
- } else if (ucPORT == 2) {
-#if defined(RT_USING_UART2)
- extern struct rt_serial_device serial2;
- serial = &serial2;
-#endif
- } else if (ucPORT == 3) {
-#if defined(RT_USING_UART3)
- extern struct rt_serial_device serial3;
- serial = &serial3;
-#endif
+ rt_snprintf(uart_name,sizeof(uart_name), "uart%d", ucPORT);
+
+ dev = rt_device_find(uart_name);
+ if(dev == RT_NULL)
+ {
+ /* can not find uart */
+ return FALSE;
+ }
+ else
+ {
+ serial = (struct rt_serial_device*)dev;
}
+
/* set serial configure parameter */
serial->config.baud_rate = ulBaudRate;
serial->config.stop_bits = STOP_BITS_1;
diff --git a/components/net/lwip-2.0.2/src/api/api_lib.c b/components/net/lwip-2.0.2/src/api/api_lib.c
index 3c1d6a6c27..7d9c12768d 100644
--- a/components/net/lwip-2.0.2/src/api/api_lib.c
+++ b/components/net/lwip-2.0.2/src/api/api_lib.c
@@ -175,12 +175,12 @@ netconn_delete(struct netconn *conn)
API_MSG_VAR_ALLOC(msg);
API_MSG_VAR_REF(msg).conn = conn;
+#if LWIP_TCP
#if LWIP_SO_SNDTIMEO || LWIP_SO_LINGER
/* get the time we started, which is later compared to
sys_now() + conn->send_timeout */
API_MSG_VAR_REF(msg).msg.sd.time_started = sys_now();
#else /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */
-#if LWIP_TCP
API_MSG_VAR_REF(msg).msg.sd.polls_left =
((LWIP_TCP_CLOSE_TIMEOUT_MS_DEFAULT + TCP_SLOW_INTERVAL - 1) / TCP_SLOW_INTERVAL) + 1;
#endif /* LWIP_TCP */
diff --git a/components/net/lwip-2.1.0/src/api/api_lib.c b/components/net/lwip-2.1.0/src/api/api_lib.c
index e03b8b7451..5e9ceafa53 100644
--- a/components/net/lwip-2.1.0/src/api/api_lib.c
+++ b/components/net/lwip-2.1.0/src/api/api_lib.c
@@ -201,12 +201,12 @@ netconn_prepare_delete(struct netconn *conn)
API_MSG_VAR_ALLOC(msg);
API_MSG_VAR_REF(msg).conn = conn;
+#if LWIP_TCP
#if LWIP_SO_SNDTIMEO || LWIP_SO_LINGER
/* get the time we started, which is later compared to
sys_now() + conn->send_timeout */
API_MSG_VAR_REF(msg).msg.sd.time_started = sys_now();
#else /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */
-#if LWIP_TCP
API_MSG_VAR_REF(msg).msg.sd.polls_left =
((LWIP_TCP_CLOSE_TIMEOUT_MS_DEFAULT + TCP_SLOW_INTERVAL - 1) / TCP_SLOW_INTERVAL) + 1;
#endif /* LWIP_TCP */
diff --git a/components/net/netdev/include/netdev.h b/components/net/netdev/include/netdev.h
index 5ea8e96080..e9d8e38658 100644
--- a/components/net/netdev/include/netdev.h
+++ b/components/net/netdev/include/netdev.h
@@ -133,7 +133,7 @@ int netdev_register(struct netdev *netdev, const char *name, void *user_data);
int netdev_unregister(struct netdev *netdev);
/* Get network interface device object */
-struct netdev *netdev_get_first_link_up(void);
+struct netdev *netdev_get_first_by_flags(uint16_t flags);
struct netdev *netdev_get_by_ipaddr(ip_addr_t *ip_addr);
struct netdev *netdev_get_by_name(const char *name);
#ifdef RT_USING_SAL
diff --git a/components/net/netdev/src/netdev.c b/components/net/netdev/src/netdev.c
index bfefc2d1de..71b1ec7ccd 100644
--- a/components/net/netdev/src/netdev.c
+++ b/components/net/netdev/src/netdev.c
@@ -134,12 +134,14 @@ int netdev_unregister(struct netdev *netdev)
/**
* This function will get the first network interface device
- * with the link_up status in network interface device list.
+ * with the flags in network interface device list.
+ *
+ * @param flags the network interface device flags
*
* @return != NULL: network interface device object
* NULL: get failed
*/
-struct netdev *netdev_get_first_link_up(void)
+struct netdev *netdev_get_first_by_flags(uint16_t flags)
{
rt_base_t level;
rt_slist_t *node = RT_NULL;
@@ -155,7 +157,7 @@ struct netdev *netdev_get_first_link_up(void)
for (node = &(netdev_list->list); node; node = rt_slist_next(node))
{
netdev = rt_slist_entry(node, struct netdev, list);
- if (netdev && netdev_is_up(netdev) && netdev_is_link_up(netdev))
+ if (netdev && (netdev->flags & flags) != 0)
{
rt_hw_interrupt_enable(level);
return netdev;
@@ -269,7 +271,7 @@ struct netdev *netdev_get_by_family(int family)
{
netdev = rt_slist_entry(node, struct netdev, list);
pf = (struct sal_proto_family *) netdev->sal_user_data;
- if (pf && pf->skt_ops && pf->family == family && netdev_is_up(netdev) && netdev_is_link_up(netdev))
+ if (pf && pf->skt_ops && pf->family == family && netdev_is_up(netdev))
{
rt_hw_interrupt_enable(level);
return netdev;
@@ -671,6 +673,23 @@ void netdev_low_level_set_dns_server(struct netdev *netdev, uint8_t dns_num, con
}
}
+#ifdef NETDEV_USING_AUTO_DEFAULT
+/* Change to the first link_up network interface device automatically */
+static void netdev_auto_change_default(struct netdev *netdev)
+{
+ struct netdev *new_netdev = RT_NULL;
+
+ if (rt_memcmp(netdev, netdev_default, sizeof(struct netdev)) == 0)
+ {
+ new_netdev = netdev_get_first_by_flags(NETDEV_FLAG_LINK_UP);
+ if (new_netdev)
+ {
+ netdev_set_default(new_netdev);
+ }
+ }
+}
+#endif /* NETDEV_USING_AUTO_DEFAULT */
+
/**
* This function will set network interface device status.
* @NOTE it can only be called in the network interface device driver.
@@ -689,6 +708,11 @@ void netdev_low_level_set_status(struct netdev *netdev, rt_bool_t is_up)
else
{
netdev->flags &= ~NETDEV_FLAG_UP;
+
+#ifdef NETDEV_USING_AUTO_DEFAULT
+ /* change to the first link_up network interface device automatically */
+ netdev_auto_change_default(netdev);
+#endif /* NETDEV_USING_AUTO_DEFAULT */
}
/* execute network interface device status change callback function */
@@ -728,6 +752,11 @@ void netdev_low_level_set_link_status(struct netdev *netdev, rt_bool_t is_up)
/* set network interface device flags to internet down */
netdev->flags &= ~NETDEV_FLAG_INTERNET_UP;
+
+#ifdef NETDEV_USING_AUTO_DEFAULT
+ /* change to the first link_up network interface device automatically */
+ netdev_auto_change_default(netdev);
+#endif /* NETDEV_USING_AUTO_DEFAULT */
}
/* execute link status change callback function */
@@ -937,7 +966,7 @@ static int netdev_cmd_ping(char* target_name, rt_uint32_t times, rt_size_t size)
else
{
/* using first internet up status network interface device */
- netdev = netdev_get_first_link_up();
+ netdev = netdev_get_first_by_flags(NETDEV_FLAG_LINK_UP);
if (netdev == RT_NULL || NETDEV_PING_IS_COMMONICABLE(netdev) == 0)
{
rt_kprintf("ping: network interface device get error.\n");
diff --git a/components/net/sal_socket/dfs_net/dfs_net.c b/components/net/sal_socket/dfs_net/dfs_net.c
index 37db186eff..f1a3d777a1 100644
--- a/components/net/sal_socket/dfs_net/dfs_net.c
+++ b/components/net/sal_socket/dfs_net/dfs_net.c
@@ -35,7 +35,9 @@ int dfs_net_getsocket(int fd)
static int dfs_net_ioctl(struct dfs_fd* file, int cmd, void* args)
{
- return -EIO;
+ int socket = (int) file->data;
+
+ return sal_ioctlsocket(socket, cmd, args);
}
static int dfs_net_read(struct dfs_fd* file, void *buf, size_t count)
diff --git a/components/net/sal_socket/impl/af_inet_lwip.c b/components/net/sal_socket/impl/af_inet_lwip.c
index cb271a55e0..95e7633635 100644
--- a/components/net/sal_socket/impl/af_inet_lwip.c
+++ b/components/net/sal_socket/impl/af_inet_lwip.c
@@ -217,6 +217,19 @@ static int inet_getsockname(int socket, struct sockaddr *name, socklen_t *namele
return lwip_getsockname(socket, name, namelen);
}
+int inet_ioctlsocket(int socket, long cmd, void *arg)
+{
+ switch (cmd)
+ {
+ case F_GETFL:
+ case F_SETFL:
+ return lwip_fcntl(socket, cmd, (int) arg);
+
+ default:
+ return lwip_ioctl(socket, cmd, arg);
+ }
+}
+
#ifdef SAL_USING_POSIX
static int inet_poll(struct dfs_fd *file, struct rt_pollreq *req)
{
@@ -278,7 +291,7 @@ static const struct sal_socket_ops lwip_socket_ops =
lwip_shutdown,
lwip_getpeername,
inet_getsockname,
- lwip_ioctl,
+ inet_ioctlsocket,
#ifdef SAL_USING_POSIX
inet_poll,
#endif
diff --git a/components/net/sal_socket/include/sal_socket.h b/components/net/sal_socket/include/sal_socket.h
index 066258eac9..d72f1e4c3c 100644
--- a/components/net/sal_socket/include/sal_socket.h
+++ b/components/net/sal_socket/include/sal_socket.h
@@ -97,6 +97,10 @@ typedef uint16_t in_port_t;
#define MSG_DONTWAIT 0x08 /* Nonblocking i/o for this operation only */
#define MSG_MORE 0x10 /* Sender will send more */
+/* Options for level IPPROTO_IP */
+#define IP_TOS 1
+#define IP_TTL 2
+
/* Options for level IPPROTO_TCP */
#define TCP_NODELAY 0x01 /* don't delay send to coalesce packets */
#define TCP_KEEPALIVE 0x02 /* send KEEPALIVE probes when idle for pcb->keep_idle milliseconds */
@@ -107,6 +111,10 @@ typedef uint16_t in_port_t;
/* Options and types related to multicast membership */
#define IP_ADD_MEMBERSHIP 3
#define IP_DROP_MEMBERSHIP 4
+/* Options and types for UDP multicast traffic handling */
+#define IP_MULTICAST_TTL 5
+#define IP_MULTICAST_IF 6
+#define IP_MULTICAST_LOOP 7
typedef struct ip_mreq
{
@@ -114,6 +122,27 @@ typedef struct ip_mreq
struct in_addr imr_interface; /* local IP address of interface */
} ip_mreq;
+/* The Type of Service provides an indication of the abstract parameters of the quality of service desired */
+#define IPTOS_TOS_MASK 0x1E
+#define IPTOS_TOS(tos) ((tos) & IPTOS_TOS_MASK)
+#define IPTOS_LOWDELAY 0x10
+#define IPTOS_THROUGHPUT 0x08
+#define IPTOS_RELIABILITY 0x04
+#define IPTOS_LOWCOST 0x02
+#define IPTOS_MINCOST IPTOS_LOWCOST
+
+/* The Network Control precedence designation is intended to be used within a network only */
+#define IPTOS_PREC_MASK 0xe0
+#define IPTOS_PREC(tos) ((tos) & IPTOS_PREC_MASK)
+#define IPTOS_PREC_NETCONTROL 0xe0
+#define IPTOS_PREC_INTERNETCONTROL 0xc0
+#define IPTOS_PREC_CRITIC_ECP 0xa0
+#define IPTOS_PREC_FLASHOVERRIDE 0x80
+#define IPTOS_PREC_FLASH 0x60
+#define IPTOS_PREC_IMMEDIATE 0x40
+#define IPTOS_PREC_PRIORITY 0x20
+#define IPTOS_PREC_ROUTINE 0x00
+
/* Options for shatdown type */
#ifndef SHUT_RD
#define SHUT_RD 0
diff --git a/components/net/sal_socket/src/sal_socket.c b/components/net/sal_socket/src/sal_socket.c
index 008a29e252..8f3149abd5 100644
--- a/components/net/sal_socket/src/sal_socket.c
+++ b/components/net/sal_socket/src/sal_socket.c
@@ -70,9 +70,9 @@ do {
} \
}while(0) \
-#define SAL_NETDEV_IS_COMMONICABLE(netdev) \
+#define SAL_NETDEV_IS_UP(netdev) \
do { \
- if (!netdev_is_up(netdev) || !netdev_is_link_up(netdev)){ \
+ if (!netdev_is_up(netdev)) { \
return -1; \
} \
}while(0) \
@@ -86,9 +86,9 @@ do {
}while(0) \
#define SAL_NETDEV_NETDBOPS_VALID(netdev, pf, ops) \
-((netdev) && netdev_is_up(netdev) && netdev_is_link_up(netdev) && \
+ ((netdev) && netdev_is_up(netdev) && \
((pf) = (struct sal_proto_family *) (netdev)->sal_user_data) != RT_NULL && \
- (pf)->netdb_ops->ops) \
+ (pf)->netdb_ops->ops) \
/**
* SAL (Socket Abstraction Layer) initialize.
@@ -387,14 +387,7 @@ static int socket_init(int family, int type, int protocol, struct sal_socket **r
sock->type = type;
sock->protocol = protocol;
- /* get socket operations from network interface device */
- if (netdv_def == RT_NULL)
- {
- LOG_E("not find default network interface device for socket create.");
- return -3;
- }
-
- if (netdev_is_up(netdv_def) && netdev_is_link_up(netdv_def))
+ if (netdv_def && netdev_is_up(netdv_def))
{
/* check default network interface device protocol family */
pf = (struct sal_proto_family *) netdv_def->sal_user_data;
@@ -755,8 +748,8 @@ int sal_connect(int socket, const struct sockaddr *name, socklen_t namelen)
/* get the socket object by socket descriptor */
SAL_SOCKET_OBJ_GET(sock, socket);
- /* check the network interface is commonicable */
- SAL_NETDEV_IS_COMMONICABLE(sock->netdev);
+ /* check the network interface is up status */
+ SAL_NETDEV_IS_UP(sock->netdev);
/* check the network interface socket opreation */
SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, connect);
@@ -799,8 +792,8 @@ int sal_recvfrom(int socket, void *mem, size_t len, int flags,
/* get the socket object by socket descriptor */
SAL_SOCKET_OBJ_GET(sock, socket);
- /* check the network interface is commonicable */
- SAL_NETDEV_IS_COMMONICABLE(sock->netdev);
+ /* check the network interface is up status */
+ SAL_NETDEV_IS_UP(sock->netdev);
/* check the network interface socket opreation */
SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, recvfrom);
@@ -833,8 +826,8 @@ int sal_sendto(int socket, const void *dataptr, size_t size, int flags,
/* get the socket object by socket descriptor */
SAL_SOCKET_OBJ_GET(sock, socket);
- /* check the network interface is commonicable */
- SAL_NETDEV_IS_COMMONICABLE(sock->netdev);
+ /* check the network interface is up status */
+ SAL_NETDEV_IS_UP(sock->netdev);
/* check the network interface socket opreation */
SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, sendto);
@@ -972,8 +965,8 @@ int sal_poll(struct dfs_fd *file, struct rt_pollreq *req)
/* get the socket object by socket descriptor */
SAL_SOCKET_OBJ_GET(sock, socket);
- /* check the network interface is commonicable */
- SAL_NETDEV_IS_COMMONICABLE(sock->netdev);
+ /* check the network interface is up status */
+ SAL_NETDEV_IS_UP(sock->netdev);
/* check the network interface socket opreation */
SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, poll);
@@ -992,8 +985,8 @@ struct hostent *sal_gethostbyname(const char *name)
}
else
{
- /* get the first network interface device with the link up status */
- netdev = netdev_get_first_link_up();
+ /* get the first network interface device with up status */
+ netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP);
if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, gethostbyname))
{
return pf->netdb_ops->gethostbyname(name);
@@ -1015,8 +1008,8 @@ int sal_gethostbyname_r(const char *name, struct hostent *ret, char *buf,
}
else
{
- /* get the first network interface device with the link up status */
- netdev = netdev_get_first_link_up();
+ /* get the first network interface device with up status */
+ netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP);
if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, gethostbyname_r))
{
return pf->netdb_ops->gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
@@ -1040,8 +1033,8 @@ int sal_getaddrinfo(const char *nodename,
}
else
{
- /* get the first network interface device with the link up status */
- netdev = netdev_get_first_link_up();
+ /* get the first network interface device with up status */
+ netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP);
if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, getaddrinfo))
{
return pf->netdb_ops->getaddrinfo(nodename, servname, hints, res);
@@ -1062,8 +1055,8 @@ void sal_freeaddrinfo(struct addrinfo *ai)
}
else
{
- /* get the first network interface device with the link up status */
- netdev = netdev_get_first_link_up();
+ /* get the first network interface device with up status */
+ netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP);
if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, freeaddrinfo))
{
pf->netdb_ops->freeaddrinfo(ai);
diff --git a/components/utilities/ulog/ulog.c b/components/utilities/ulog/ulog.c
index 87d0932506..36d5ac275f 100644
--- a/components/utilities/ulog/ulog.c
+++ b/components/utilities/ulog/ulog.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -403,15 +403,15 @@ void ulog_output_to_all_backend(rt_uint32_t level, const char *tag, rt_bool_t is
else
{
/* recalculate the log start address and log size when backend not supported color */
- rt_size_t color_info_len = rt_strlen(color_output_info[level]);
+ rt_size_t color_info_len = rt_strlen(color_output_info[level]), output_size = size;
if (color_info_len)
{
rt_size_t color_hdr_len = rt_strlen(CSI_START) + color_info_len;
log += color_hdr_len;
- size -= (color_hdr_len + (sizeof(CSI_END) - 1));
+ output_size -= (color_hdr_len + (sizeof(CSI_END) - 1));
}
- backend->output(backend, level, tag, is_raw, log, size);
+ backend->output(backend, level, tag, is_raw, log, output_size);
}
#endif /* !defined(ULOG_USING_COLOR) || defined(ULOG_USING_SYSLOG) */
}