From 07fc57b2c8e748cacfc80a319127224dbba4c3a5 Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Thu, 2 Feb 2017 11:28:02 +0000 Subject: Simulator compiles, to be tested. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@10078 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/hal/ports/simulator/console.c | 20 ++-- os/hal/ports/simulator/posix/hal_lld.c | 41 +++----- os/hal/ports/simulator/posix/hal_lld.h | 12 +++ os/hal/ports/simulator/posix/hal_serial_lld.c | 144 ++++++++++++++------------ os/hal/ports/simulator/posix/hal_serial_lld.h | 20 ++-- 5 files changed, 129 insertions(+), 108 deletions(-) (limited to 'os/hal') diff --git a/os/hal/ports/simulator/console.c b/os/hal/ports/simulator/console.c index 1be915a66..c4ff21f9d 100644 --- a/os/hal/ports/simulator/console.c +++ b/os/hal/ports/simulator/console.c @@ -43,7 +43,7 @@ BaseChannel CD1; /* Driver local functions. */ /*===========================================================================*/ -static size_t write(void *ip, const uint8_t *bp, size_t n) { +static size_t _write(void *ip, const uint8_t *bp, size_t n) { size_t ret; (void)ip; @@ -53,14 +53,14 @@ static size_t write(void *ip, const uint8_t *bp, size_t n) { return ret; } -static size_t read(void *ip, uint8_t *bp, size_t n) { +static size_t _read(void *ip, uint8_t *bp, size_t n) { (void)ip; return fread(bp, 1, n, stdin); } -static msg_t put(void *ip, uint8_t b) { +static msg_t _put(void *ip, uint8_t b) { (void)ip; @@ -69,14 +69,14 @@ static msg_t put(void *ip, uint8_t b) { return MSG_OK; } -static msg_t get(void *ip) { +static msg_t _get(void *ip) { (void)ip; return fgetc(stdin); } -static msg_t putt(void *ip, uint8_t b, systime_t time) { +static msg_t _putt(void *ip, uint8_t b, systime_t time) { (void)ip; (void)time; @@ -86,7 +86,7 @@ static msg_t putt(void *ip, uint8_t b, systime_t time) { return MSG_OK; } -static msg_t gett(void *ip, systime_t time) { +static msg_t _gett(void *ip, systime_t time) { (void)ip; (void)time; @@ -94,7 +94,7 @@ static msg_t gett(void *ip, systime_t time) { return fgetc(stdin); } -static size_t writet(void *ip, const uint8_t *bp, size_t n, systime_t time) { +static size_t _writet(void *ip, const uint8_t *bp, size_t n, systime_t time) { size_t ret; (void)ip; @@ -105,7 +105,7 @@ static size_t writet(void *ip, const uint8_t *bp, size_t n, systime_t time) { return ret; } -static size_t readt(void *ip, uint8_t *bp, size_t n, systime_t time) { +static size_t _readt(void *ip, uint8_t *bp, size_t n, systime_t time) { (void)ip; (void)time; @@ -114,8 +114,8 @@ static size_t readt(void *ip, uint8_t *bp, size_t n, systime_t time) { } static const struct BaseChannelVMT vmt = { - write, read, put, get, - putt, gett, writet, readt + _write, _read, _put, _get, + _putt, _gett, _writet, _readt }; /*===========================================================================*/ diff --git a/os/hal/ports/simulator/posix/hal_lld.c b/os/hal/ports/simulator/posix/hal_lld.c index 52adb4e4c..e6a7ec3e5 100755 --- a/os/hal/ports/simulator/posix/hal_lld.c +++ b/os/hal/ports/simulator/posix/hal_lld.c @@ -22,6 +22,10 @@ * @{ */ +#include +#include +#include + #include "hal.h" /*===========================================================================*/ @@ -32,8 +36,8 @@ /* Driver local variables and types. */ /*===========================================================================*/ -static LARGE_INTEGER nextcnt; -static LARGE_INTEGER slice; +static struct timeval nextcnt; +static struct timeval tick = {0UL, 1000000UL / OSAL_ST_FREQUENCY}; /*===========================================================================*/ /* Driver local functions. */ @@ -51,31 +55,21 @@ static LARGE_INTEGER slice; * @brief Low level HAL driver initialization. */ void hal_lld_init(void) { - WSADATA wsaData; - - /* Initialization.*/ - if (WSAStartup(2, &wsaData) != 0) { - printf("Unable to locate a winsock DLL\n"); - exit(1); - } - printf("ChibiOS/RT simulator (Win32)\n"); - if (!QueryPerformanceFrequency(&slice)) { - printf("QueryPerformanceFrequency() error"); - exit(1); - } - slice.QuadPart /= CH_CFG_ST_FREQUENCY; - QueryPerformanceCounter(&nextcnt); - nextcnt.QuadPart += slice.QuadPart; - - fflush(stdout); +#if defined(__APPLE__) + puts("ChibiOS/RT simulator (OS X)\n"); +#else + puts("ChibiOS/RT simulator (Linux)\n"); +#endif + gettimeofday(&nextcnt, NULL); + timeradd(&nextcnt, &tick, &nextcnt); } /** * @brief Interrupt simulation. */ void _sim_check_for_interrupts(void) { - LARGE_INTEGER n; + struct timeval tv; #if HAL_USE_SERIAL if (sd_lld_interrupt_pending()) { @@ -87,10 +81,9 @@ void _sim_check_for_interrupts(void) { } #endif - /* Interrupt Timer simulation (10ms interval).*/ - QueryPerformanceCounter(&n); - if (n.QuadPart > nextcnt.QuadPart) { - nextcnt.QuadPart += slice.QuadPart; + gettimeofday(&tv, NULL); + if (timercmp(&tv, &nextcnt, >=)) { + timeradd(&nextcnt, &tick, &nextcnt); CH_IRQ_PROLOGUE(); diff --git a/os/hal/ports/simulator/posix/hal_lld.h b/os/hal/ports/simulator/posix/hal_lld.h index 2a1dbf933..08dc51047 100755 --- a/os/hal/ports/simulator/posix/hal_lld.h +++ b/os/hal/ports/simulator/posix/hal_lld.h @@ -25,7 +25,15 @@ #ifndef HAL_LLD_H #define HAL_LLD_H +#if defined(WIN32) #include +#else +#include +#include +#include +#include +#include +#endif #include /*===========================================================================*/ @@ -35,7 +43,11 @@ /** * @brief Platform name. */ +#if defined(WIN32) #define PLATFORM_NAME "Win32 Simulator" +#else +#define PLATFORM_NAME "Posix Simulator" +#endif /*===========================================================================*/ /* Driver pre-compile time settings. */ diff --git a/os/hal/ports/simulator/posix/hal_serial_lld.c b/os/hal/ports/simulator/posix/hal_serial_lld.c index 99a75199a..8ef404850 100755 --- a/os/hal/ports/simulator/posix/hal_serial_lld.c +++ b/os/hal/ports/simulator/posix/hal_serial_lld.c @@ -22,6 +22,10 @@ * @{ */ +#include +#include +#include + #include "hal.h" #if HAL_USE_SERIAL || defined(__DOXYGEN__) @@ -31,11 +35,12 @@ /*===========================================================================*/ /** @brief Serial driver 1 identifier.*/ -#if USE_WIN32_SERIAL1 || defined(__DOXYGEN__) +#if USE_SIM_SERIAL1 || defined(__DOXYGEN__) SerialDriver SD1; #endif + /** @brief Serial driver 2 identifier.*/ -#if USE_WIN32_SERIAL2 || defined(__DOXYGEN__) +#if USE_SIM_SERIAL2 || defined(__DOXYGEN__) SerialDriver SD2; #endif @@ -47,8 +52,6 @@ SerialDriver SD2; static const SerialConfig default_config = { }; -static u_long nb = 1; - /*===========================================================================*/ /* Driver local functions. */ /*===========================================================================*/ @@ -56,6 +59,8 @@ static u_long nb = 1; static void init(SerialDriver *sdp, uint16_t port) { struct sockaddr_in sad; struct protoent *prtp; + int sockval = 1; + socklen_t socklen = sizeof(sockval); if ((prtp = getprotobyname("tcp")) == NULL) { printf("%s: Error mapping protocol name to protocol number\n", sdp->com_name); @@ -63,12 +68,21 @@ static void init(SerialDriver *sdp, uint16_t port) { } sdp->com_listen = socket(PF_INET, SOCK_STREAM, prtp->p_proto); - if (sdp->com_listen == INVALID_SOCKET) { + if (sdp->com_listen == -1) { printf("%s: Error creating simulator socket\n", sdp->com_name); goto abort; } - if (ioctlsocket(sdp->com_listen, FIONBIO, &nb) != 0) { + setsockopt(sdp->com_listen, SOL_SOCKET, SO_REUSEADDR, &sockval, socklen); + +#if 0 + if (ioctl(sdp->com_listen, FIONBIO, &nb) != 0) { + printf("%s: Unable to setup non blocking mode on socket\n", sdp->com_name); + goto abort; + } +#endif + int flags = fcntl(sdp->com_listen, F_GETFL, 0); + if (fcntl(sdp->com_listen, F_SETFL, flags | O_NONBLOCK) != 0) { printf("%s: Unable to setup non blocking mode on socket\n", sdp->com_name); goto abort; } @@ -90,69 +104,75 @@ static void init(SerialDriver *sdp, uint16_t port) { return; abort: - if (sdp->com_listen != INVALID_SOCKET) - closesocket(sdp->com_listen); - WSACleanup(); + if (sdp->com_listen != -1) + close(sdp->com_listen); exit(1); } static bool connint(SerialDriver *sdp) { - if (sdp->com_data == INVALID_SOCKET) { + if (sdp->com_data == -1) { struct sockaddr addr; - int addrlen = sizeof(addr); + socklen_t addrlen = sizeof(addr); - if ((sdp->com_data = accept(sdp->com_listen, &addr, &addrlen)) == INVALID_SOCKET) + if ((sdp->com_data = accept(sdp->com_listen, &addr, &addrlen)) == -1) return FALSE; - if (ioctlsocket(sdp->com_data, FIONBIO, &nb) != 0) { +#if 0 + if (ioctl(sdp->com_data, FIONBIO, &nb) != 0) { printf("%s: Unable to setup non blocking mode on data socket\n", sdp->com_name); goto abort; } - chSysLockFromISR(); +#endif + int flags = fcntl(sdp->com_data, F_GETFL, 0); + if (fcntl(sdp->com_data, F_SETFL, flags | O_NONBLOCK) != 0) { + printf("%s: Unable to setup non blocking mode on data socket\n", sdp->com_name); + goto abort; + } + + osalSysLockFromISR(); chnAddFlagsI(sdp, CHN_CONNECTED); - chSysUnlockFromISR(); + osalSysUnlockFromISR(); return TRUE; } return FALSE; abort: - if (sdp->com_listen != INVALID_SOCKET) - closesocket(sdp->com_listen); - if (sdp->com_data != INVALID_SOCKET) - closesocket(sdp->com_data); - WSACleanup(); + if (sdp->com_listen != -1) + close(sdp->com_listen); + if (sdp->com_data != -1) + close(sdp->com_data); exit(1); } static bool inint(SerialDriver *sdp) { - if (sdp->com_data != INVALID_SOCKET) { + if (sdp->com_data != -1) { int i; uint8_t data[32]; /* * Input. */ - int n = recv(sdp->com_data, (char *)data, sizeof(data), 0); + int n = recv(sdp->com_data, data, sizeof(data), 0); switch (n) { case 0: - closesocket(sdp->com_data); - sdp->com_data = INVALID_SOCKET; - chSysLockFromISR(); + close(sdp->com_data); + sdp->com_data = -1; + osalSysLockFromISR(); chnAddFlagsI(sdp, CHN_DISCONNECTED); - chSysUnlockFromISR(); + osalSysUnlockFromISR(); return FALSE; - case SOCKET_ERROR: - if (WSAGetLastError() == WSAEWOULDBLOCK) + case -1: + if (errno == EWOULDBLOCK) return FALSE; - closesocket(sdp->com_data); - sdp->com_data = INVALID_SOCKET; + close(sdp->com_data); + sdp->com_data = -1; return FALSE; } for (i = 0; i < n; i++) { - chSysLockFromISR(); + osalSysLockFromISR(); sdIncomingDataI(sdp, data[i]); - chSysUnlockFromISR(); + osalSysUnlockFromISR(); } return TRUE; } @@ -161,33 +181,33 @@ static bool inint(SerialDriver *sdp) { static bool outint(SerialDriver *sdp) { - if (sdp->com_data != INVALID_SOCKET) { + if (sdp->com_data != -1) { int n; uint8_t data[1]; /* * Input. */ - chSysLockFromISR(); + osalSysLockFromISR(); n = sdRequestDataI(sdp); - chSysUnlockFromISR(); + osalSysUnlockFromISR(); if (n < 0) return FALSE; data[0] = (uint8_t)n; - n = send(sdp->com_data, (char *)data, sizeof(data), 0); + n = send(sdp->com_data, data, sizeof(data), 0); switch (n) { case 0: - closesocket(sdp->com_data); - sdp->com_data = INVALID_SOCKET; - chSysLockFromISR(); + close(sdp->com_data); + sdp->com_data = -1; + osalSysLockFromISR(); chnAddFlagsI(sdp, CHN_DISCONNECTED); - chSysUnlockFromISR(); + osalSysUnlockFromISR(); return FALSE; - case SOCKET_ERROR: - if (WSAGetLastError() == WSAEWOULDBLOCK) + case -1: + if (errno == EWOULDBLOCK) return FALSE; - closesocket(sdp->com_data); - sdp->com_data = INVALID_SOCKET; + close(sdp->com_data); + sdp->com_data = -1; return FALSE; } return TRUE; @@ -208,17 +228,17 @@ static bool outint(SerialDriver *sdp) { */ void sd_lld_init(void) { -#if USE_WIN32_SERIAL1 +#if USE_SIM_SERIAL1 sdObjectInit(&SD1, NULL, NULL); - SD1.com_listen = INVALID_SOCKET; - SD1.com_data = INVALID_SOCKET; + SD1.com_listen = -1; + SD1.com_data = -1; SD1.com_name = "SD1"; #endif -#if USE_WIN32_SERIAL2 +#if USE_SIM_SERIAL2 sdObjectInit(&SD2, NULL, NULL); - SD2.com_listen = INVALID_SOCKET; - SD2.com_data = INVALID_SOCKET; + SD2.com_listen = -1; + SD2.com_data = -1; SD2.com_name = "SD2"; #endif } @@ -236,14 +256,14 @@ void sd_lld_start(SerialDriver *sdp, const SerialConfig *config) { if (config == NULL) config = &default_config; -#if USE_WIN32_SERIAL1 +#if USE_SIM_SERIAL1 if (sdp == &SD1) - init(&SD1, SD1_PORT); + init(&SD1, SIM_SD1_PORT); #endif -#if USE_WIN32_SERIAL2 +#if USE_SIM_SERIAL2 if (sdp == &SD2) - init(&SD2, SD2_PORT); + init(&SD2, SIM_SD2_PORT); #endif } @@ -260,19 +280,15 @@ void sd_lld_stop(SerialDriver *sdp) { } bool sd_lld_interrupt_pending(void) { - bool b = false; - - CH_IRQ_PROLOGUE(); + bool b; -#if USE_WIN32_SERIAL1 - b |= connint(&SD1) || inint(&SD1) || outint(&SD1); -#endif + OSAL_IRQ_PROLOGUE(); -#if USE_WIN32_SERIAL2 - b |= connint(&SD2) || inint(&SD2) || outint(&SD2); -#endif + b = connint(&SD1) || connint(&SD2) || + inint(&SD1) || inint(&SD2) || + outint(&SD1) || outint(&SD2); - CH_IRQ_EPILOGUE(); + OSAL_IRQ_EPILOGUE(); return b; } diff --git a/os/hal/ports/simulator/posix/hal_serial_lld.h b/os/hal/ports/simulator/posix/hal_serial_lld.h index de228111f..f3bd3fff1 100755 --- a/os/hal/ports/simulator/posix/hal_serial_lld.h +++ b/os/hal/ports/simulator/posix/hal_serial_lld.h @@ -45,8 +45,8 @@ * @details If set to @p TRUE the support for SD1 is included. * @note The default is @p TRUE. */ -#if !defined(USE_WIN32_SERIAL1) || defined(__DOXYGEN__) -#define USE_WIN32_SERIAL1 TRUE +#if !defined(USE_SIM_SERIAL1) || defined(__DOXYGEN__) +#define USE_SIM_SERIAL1 TRUE #endif /** @@ -54,22 +54,22 @@ * @details If set to @p TRUE the support for SD2 is included. * @note The default is @p TRUE. */ -#if !defined(USE_WIN32_SERIAL2) || defined(__DOXYGEN__) -#define USE_WIN32_SERIAL2 TRUE +#if !defined(USE_SIM_SERIAL2) || defined(__DOXYGEN__) +#define USE_SIM_SERIAL2 TRUE #endif /** * @brief Listen port for SD1. */ #if !defined(SD1_PORT) || defined(__DOXYGEN__) -#define SD1_PORT 29001 +#define SIM_SD1_PORT 29001 #endif /** * @brief Listen port for SD2. */ #if !defined(SD2_PORT) || defined(__DOXYGEN__) -#define SD2_PORT 29002 +#define SIM_SD2_PORT 29002 #endif /*===========================================================================*/ @@ -108,9 +108,9 @@ typedef struct { uint8_t ob[SERIAL_BUFFERS_SIZE]; \ /* End of the mandatory fields.*/ \ /* Listen socket for simulated serial port.*/ \ - SOCKET com_listen; \ + int com_listen; \ /* Data socket for simulated serial port.*/ \ - SOCKET com_data; \ + int com_data; \ /* Port readable name.*/ \ const char *com_name; @@ -118,10 +118,10 @@ typedef struct { /* External declarations. */ /*===========================================================================*/ -#if USE_WIN32_SERIAL1 && !defined(__DOXYGEN__) +#if USE_SIM_SERIAL1 && !defined(__DOXYGEN__) extern SerialDriver SD1; #endif -#if USE_WIN32_SERIAL2 && !defined(__DOXYGEN__) +#if USE_SIM_SERIAL2 && !defined(__DOXYGEN__) extern SerialDriver SD2; #endif -- cgit v1.2.3