Merge pull request #56 from thelittlehan/master

linux port更新
This commit is contained in:
Supowang1989
2019-10-30 19:16:36 +08:00
committed by GitHub
4 changed files with 49 additions and 13 deletions

View File

@@ -78,6 +78,7 @@ __API__ void tos_cpu_cpsr_restore(cpu_cpsr_t cpsr)
__KERNEL__ void cpu_init(void) __KERNEL__ void cpu_init(void)
{ {
port_init();
k_cpu_cycle_per_tick = TOS_CFG_CPU_CLOCK / k_cpu_tick_per_second; k_cpu_cycle_per_tick = TOS_CFG_CPU_CLOCK / k_cpu_tick_per_second;
cpu_systick_init(k_cpu_cycle_per_tick); cpu_systick_init(k_cpu_cycle_per_tick);
@@ -108,7 +109,6 @@ __KERNEL__ void cpu_irq_context_switch(void)
__KERNEL__ void cpu_systick_init(k_cycle_t cycle_per_tick) __KERNEL__ void cpu_systick_init(k_cycle_t cycle_per_tick)
{ {
port_systick_priority_set(TOS_CFG_CPU_SYSTICK_PRIO);
port_systick_config(cycle_per_tick); port_systick_config(cycle_per_tick);
} }

View File

@@ -81,18 +81,24 @@ interrupt_manager _int_manager={
}; };
static uint64_t tick_ms = 0; static uint64_t tick_ms = 0;
static pthread_t main_thread_id;
static pthread_mutex_t cpsr_mutex;
#define CHECK_IS_MAIN_THREAD(main_thread_id) \
if(main_thread_id != pthread_self()) return
__PORT__ void port_int_disable(void) __PORT__ void port_int_disable(void)
{ {
sigset_t signal_mask,*manager_mask=NULL; sigset_t signal_mask,*manager_mask=NULL;
if(_int_manager.count == 0){ if(_int_manager.count == 0){
pthread_mutex_lock(&cpsr_mutex);
manager_mask = &(_int_manager.signal_mask); manager_mask = &(_int_manager.signal_mask);
}
_int_manager.count ++;
sigfillset(&signal_mask); sigfillset(&signal_mask);
_filter_signal(&signal_mask); _filter_signal(&signal_mask);
pthread_sigmask(SIG_BLOCK,&signal_mask,manager_mask); pthread_sigmask(SIG_BLOCK,&signal_mask,manager_mask);
} }
_int_manager.count ++;
}
__PORT__ void port_int_enable(void) __PORT__ void port_int_enable(void)
{ {
@@ -102,6 +108,7 @@ __PORT__ void port_int_enable(void)
if(_int_manager.count == 0){ if(_int_manager.count == 0){
sigaddset(&(_int_manager.signal_mask),SIG_RESUME); //ensure SIG_RESUME is blocked sigaddset(&(_int_manager.signal_mask),SIG_RESUME); //ensure SIG_RESUME is blocked
_filter_signal(&(_int_manager.signal_mask)); _filter_signal(&(_int_manager.signal_mask));
pthread_mutex_unlock(&cpsr_mutex);
pthread_sigmask(SIG_SETMASK,&(_int_manager.signal_mask),NULL); pthread_sigmask(SIG_SETMASK,&(_int_manager.signal_mask),NULL);
} }
} }
@@ -126,10 +133,6 @@ __PORT__ pthread_t port_create_thread(void *arg)
__PORT__ void port_sched_start(void) __PORT__ void port_sched_start(void)
{ {
_install_signal(SIG_SUSPEND, _handle_suspend_thread);
_install_signal(SIG_RESUME, _handle_resume_thread);
_install_signal(SIG_CONTEXT_SWITCH, _handle_context_switch);
k_curr_task = k_next_task; k_curr_task = k_next_task;
_resume_task(k_curr_task); _resume_task(k_curr_task);
@@ -184,8 +187,13 @@ __PORT__ void port_systick_config(uint32_t cycle_per_tick)
} }
} }
__PORT__ void port_systick_priority_set(uint32_t prio) __PORT__ void port_init(void)
{ {
main_thread_id = pthread_self();
pthread_mutex_init(&cpsr_mutex,NULL);
_install_signal(SIG_SUSPEND, _handle_suspend_thread);
_install_signal(SIG_RESUME, _handle_resume_thread);
_install_signal(SIG_CONTEXT_SWITCH, _handle_context_switch);
} }
__PORT__ void port_delay_ms(uint32_t ms) __PORT__ void port_delay_ms(uint32_t ms)
@@ -224,6 +232,7 @@ __PORT__ void _install_signal(int sig,void (*func)(int))
__PORT__ void _handle_tick_signal() __PORT__ void _handle_tick_signal()
{ {
CHECK_IS_MAIN_THREAD(main_thread_id);
tick_ms ++; tick_ms ++;
if(tos_knl_is_running()) { if(tos_knl_is_running()) {
tos_knl_irq_enter(); tos_knl_irq_enter();

View File

@@ -89,7 +89,7 @@ __PORT__ void port_irq_context_switch(void);
__PORT__ void port_systick_config(uint32_t cycle_per_tick); __PORT__ void port_systick_config(uint32_t cycle_per_tick);
__PORT__ void port_systick_priority_set(uint32_t prio); //ok __PORT__ void port_init(void);
__PORT__ pthread_t port_create_thread(void *arg); __PORT__ pthread_t port_create_thread(void *arg);

View File

@@ -12,12 +12,25 @@ static osMutexId socket_send_lock, socket_recv_lock;
osMutexDef(socket_send_lock); osMutexDef(socket_send_lock);
osMutexDef(socket_recv_lock); osMutexDef(socket_recv_lock);
static osSemaphoreId socket_recv_event;
osSemaphoreDef(socket_recv_event);
int socket_init(void) int socket_init(void)
{ {
socket_send_lock = osMutexCreate(osMutex(socket_send_lock)); socket_send_lock = osMutexCreate(osMutex(socket_send_lock));
socket_recv_lock = osMutexCreate(osMutex(socket_recv_lock)); socket_recv_lock = osMutexCreate(osMutex(socket_recv_lock));
socket_recv_event = osSemaphoreCreate(osSemaphore(socket_recv_event),0);
return ((socket_recv_lock != NULL) && (socket_send_lock != NULL)); return (
(socket_recv_event != NULL) &&
(socket_recv_lock != NULL) &&
(socket_send_lock != NULL)
);
}
void io_signal_handle(int signal)
{
osSemaphoreRelease(socket_recv_event);
} }
int socket_connect(const char *ip, const char *port, sal_proto_t proto) int socket_connect(const char *ip, const char *port, sal_proto_t proto)
@@ -26,6 +39,7 @@ int socket_connect(const char *ip, const char *port, sal_proto_t proto)
.sin_family = AF_INET, .sin_family = AF_INET,
.sin_port = htons(atoi(port))}; .sin_port = htons(atoi(port))};
int socket_proto = 0; int socket_proto = 0;
struct sigaction sig_install;
inet_pton(AF_INET, ip, &addr.sin_addr); inet_pton(AF_INET, ip, &addr.sin_addr);
@@ -44,6 +58,19 @@ int socket_connect(const char *ip, const char *port, sal_proto_t proto)
int socket_id = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); int socket_id = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
fcntl( socket_id, __F_SETOWN, getpid() );
fcntl( socket_id, __F_SETSIG, SIGIO );
fcntl( socket_id, F_SETFL, O_ASYNC );
sig_install.sa_flags = 0;
sig_install.sa_handler = io_signal_handle;
sigfillset( &sig_install.sa_mask );
if ( 0 != sigaction( SIGIO, &sig_install, NULL ) )
{
printf( "socket problem installing %d\n",SIGIO );
}
connect(socket_id, &addr, sizeof(addr)); connect(socket_id, &addr, sizeof(addr));
return socket_id; return socket_id;
} }
@@ -63,7 +90,7 @@ int socket_send(int sock, const void *buf, size_t len)
} }
if (send_len != len) if (send_len != len)
{ {
osDelay(5); osSemaphoreWait(socket_recv_event,100);
} }
} while (len != send_len); } while (len != send_len);
osMutexRelease(socket_send_lock); osMutexRelease(socket_send_lock);
@@ -85,7 +112,7 @@ int socket_recv(int sock, void *buf, size_t len)
} }
if (recv_len != len) if (recv_len != len)
{ {
osDelay(5); osSemaphoreWait(socket_recv_event,100);
} }
} while (len != recv_len); } while (len != recv_len);
osMutexRelease(socket_recv_lock); osMutexRelease(socket_recv_lock);