git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@1497 35acf78f-673a-0410-8e92-d51de3d6d3f4

master
gdisirio 2010-01-02 18:52:30 +00:00
parent 6cb3190f38
commit edfe4ce441
3 changed files with 100 additions and 94 deletions

View File

@ -38,7 +38,7 @@
* settings file. This file can be used to include platform specific
* header files or to override the low level drivers settings.
*/
#include "mcuconf.h"
/*#include "mcuconf.h"*/
/*===========================================================================*/
/* PAL driver related settings. */

View File

@ -54,7 +54,7 @@ SerialDriver SD2;
/*===========================================================================*/
/** @brief Driver default configuration.*/
static const SerialDriverConfig default_config = {
static const SerialConfig default_config = {
};
static u_long nb = 1;
@ -68,18 +68,18 @@ static void init(SerialDriver *sdp, uint16_t port) {
struct protoent *prtp;
if ((prtp = getprotobyname("tcp")) == NULL) {
printf("%s: Error mapping protocol name to protocol number\n", sdp->d2.com_name);
printf("%s: Error mapping protocol name to protocol number\n", sdp->sd.com_name);
goto abort;
}
sdp->d2.com_listen = socket(PF_INET, SOCK_STREAM, prtp->p_proto);
if (sdp->d2.com_listen == INVALID_SOCKET) {
printf("%s: Error creating simulator socket\n", sdp->d2.com_name);
sdp->sd.com_listen = socket(PF_INET, SOCK_STREAM, prtp->p_proto);
if (sdp->sd.com_listen == INVALID_SOCKET) {
printf("%s: Error creating simulator socket\n", sdp->sd.com_name);
goto abort;
}
if (ioctl(sdp->d2.com_listen, FIONBIO, &nb) != 0) {
printf("%s: Unable to setup non blocking mode on socket\n", sdp->d2.com_name);
if (ioctl(sdp->sd.com_listen, FIONBIO, &nb) != 0) {
printf("%s: Unable to setup non blocking mode on socket\n", sdp->sd.com_name);
goto abort;
}
@ -87,35 +87,35 @@ static void init(SerialDriver *sdp, uint16_t port) {
sad.sin_family = AF_INET;
sad.sin_addr.s_addr = INADDR_ANY;
sad.sin_port = htons(port);
if (bind(sdp->d2.com_listen, (struct sockaddr *)&sad, sizeof(sad))) {
printf("%s: Error binding socket\n", sdp->d2.com_name);
if (bind(sdp->sd.com_listen, (struct sockaddr *)&sad, sizeof(sad))) {
printf("%s: Error binding socket\n", sdp->sd.com_name);
goto abort;
}
if (listen(sdp->d2.com_listen, 1) != 0) {
printf("%s: Error listening socket\n", sdp->d2.com_name);
if (listen(sdp->sd.com_listen, 1) != 0) {
printf("%s: Error listening socket\n", sdp->sd.com_name);
goto abort;
}
printf("Full Duplex Channel %s listening on port %d\n", sdp->d2.com_name, port);
printf("Full Duplex Channel %s listening on port %d\n", sdp->sd.com_name, port);
return;
abort:
if (sdp->d2.com_listen != INVALID_SOCKET)
close(sdp->d2.com_listen);
if (sdp->sd.com_listen != INVALID_SOCKET)
close(sdp->sd.com_listen);
exit(1);
}
static bool_t connint(SerialDriver *sdp) {
if (sdp->d2.com_data == INVALID_SOCKET) {
if (sdp->sd.com_data == INVALID_SOCKET) {
struct sockaddr addr;
socklen_t addrlen = sizeof(addr);
if ((sdp->d2.com_data = accept(sdp->d2.com_listen, &addr, &addrlen)) == INVALID_SOCKET)
if ((sdp->sd.com_data = accept(sdp->sd.com_listen, &addr, &addrlen)) == INVALID_SOCKET)
return FALSE;
if (ioctl(sdp->d2.com_data, FIONBIO, &nb) != 0) {
printf("%s: Unable to setup non blocking mode on data socket\n", sdp->d2.com_name);
if (ioctl(sdp->sd.com_data, FIONBIO, &nb) != 0) {
printf("%s: Unable to setup non blocking mode on data socket\n", sdp->sd.com_name);
goto abort;
}
sdAddFlagsI(sdp, SD_CONNECTED);
@ -123,34 +123,34 @@ static bool_t connint(SerialDriver *sdp) {
}
return FALSE;
abort:
if (sdp->d2.com_listen != INVALID_SOCKET)
close(sdp->d2.com_listen);
if (sdp->d2.com_data != INVALID_SOCKET)
close(sdp->d2.com_data);
if (sdp->sd.com_listen != INVALID_SOCKET)
close(sdp->sd.com_listen);
if (sdp->sd.com_data != INVALID_SOCKET)
close(sdp->sd.com_data);
exit(1);
}
static bool_t inint(SerialDriver *sdp) {
if (sdp->d2.com_data != INVALID_SOCKET) {
if (sdp->sd.com_data != INVALID_SOCKET) {
int i;
uint8_t data[32];
/*
* Input.
*/
int n = recv(sdp->d2.com_data, data, sizeof(data), 0);
int n = recv(sdp->sd.com_data, data, sizeof(data), 0);
switch (n) {
case 0:
close(sdp->d2.com_data);
sdp->d2.com_data = INVALID_SOCKET;
close(sdp->sd.com_data);
sdp->sd.com_data = INVALID_SOCKET;
sdAddFlagsI(sdp, SD_DISCONNECTED);
return FALSE;
case INVALID_SOCKET:
if (errno == EWOULDBLOCK)
return FALSE;
close(sdp->d2.com_data);
sdp->d2.com_data = INVALID_SOCKET;
close(sdp->sd.com_data);
sdp->sd.com_data = INVALID_SOCKET;
return FALSE;
}
for (i = 0; i < n; i++)
@ -162,7 +162,7 @@ static bool_t inint(SerialDriver *sdp) {
static bool_t outint(SerialDriver *sdp) {
if (sdp->d2.com_data != INVALID_SOCKET) {
if (sdp->sd.com_data != INVALID_SOCKET) {
int n;
uint8_t data[1];
@ -173,18 +173,18 @@ static bool_t outint(SerialDriver *sdp) {
if (n < 0)
return FALSE;
data[0] = (uint8_t)n;
n = send(sdp->d2.com_data, data, sizeof(data), 0);
n = send(sdp->sd.com_data, data, sizeof(data), 0);
switch (n) {
case 0:
close(sdp->d2.com_data);
sdp->d2.com_data = INVALID_SOCKET;
close(sdp->sd.com_data);
sdp->sd.com_data = INVALID_SOCKET;
sdAddFlagsI(sdp, SD_DISCONNECTED);
return FALSE;
case INVALID_SOCKET:
if (errno == EWOULDBLOCK)
return FALSE;
close(sdp->d2.com_data);
sdp->d2.com_data = INVALID_SOCKET;
close(sdp->sd.com_data);
sdp->sd.com_data = INVALID_SOCKET;
return FALSE;
}
return TRUE;
@ -207,16 +207,16 @@ void sd_lld_init(void) {
#if USE_SIM_SERIAL1
sdObjectInit(&SD1, NULL, NULL);
SD1.d2.com_listen = INVALID_SOCKET;
SD1.d2.com_data = INVALID_SOCKET;
SD1.d2.com_name = "SD1";
SD1.sd.com_listen = INVALID_SOCKET;
SD1.sd.com_data = INVALID_SOCKET;
SD1.sd.com_name = "SD1";
#endif
#if USE_SIM_SERIAL2
sdObjectInit(&SD2, NULL, NULL);
SD2.d2.com_listen = INVALID_SOCKET;
SD2.d2.com_data = INVALID_SOCKET;
SD2.d2.com_name = "SD2";
SD2.sd.com_listen = INVALID_SOCKET;
SD2.sd.com_data = INVALID_SOCKET;
SD2.sd.com_name = "SD2";
#endif
}
@ -224,14 +224,11 @@ void sd_lld_init(void) {
* @brief Low level serial driver configuration and (re)start.
*
* @param[in] sdp pointer to a @p SerialDriver object
* @param[in] config the architecture-dependent serial driver configuration.
* If this parameter is set to @p NULL then a default
* configuration is used.
*/
void sd_lld_start(SerialDriver *sdp, const SerialDriverConfig *config) {
void sd_lld_start(SerialDriver *sdp) {
if (config == NULL)
config = &default_config;
if (sdp->sd.config == NULL)
sdp->sd.config = &default_config;
#if USE_SIM_SERIAL1
if (sdp == &SD1)

View File

@ -91,51 +91,6 @@
*/
typedef uint32_t sdflags_t;
/**
* @brief @p SerialDriver specific data.
*/
struct _serial_driver_data {
/**
* Input queue, incoming data can be read from this input queue by
* using the queues APIs.
*/
InputQueue iqueue;
/**
* Output queue, outgoing data can be written to this output queue by
* using the queues APIs.
*/
OutputQueue oqueue;
/**
* Status Change @p EventSource. This event is generated when one or more
* condition flags change.
*/
EventSource sevent;
/**
* I/O driver status flags.
*/
sdflags_t flags;
/**
* Input circular buffer.
*/
uint8_t ib[SERIAL_BUFFERS_SIZE];
/**
* Output circular buffer.
*/
uint8_t ob[SERIAL_BUFFERS_SIZE];
/**
* Listen socket for simulated serial port.
*/
SOCKET com_listen;
/**
* Data socket for simulated serial port.
*/
SOCKET com_data;
/**
* Port readable name.
*/
const char *com_name;
};
/**
* @brief Generic Serial Driver configuration structure.
* @details An instance of this structure must be passed to @p sdStart()
@ -146,7 +101,61 @@ struct _serial_driver_data {
* initializers.
*/
typedef struct {
} SerialDriverConfig;
} SerialConfig;
/**
* @brief @p SerialDriver specific data.
*/
struct _serial_driver_data {
/**
* @brief Driver state.
*/
sdstate_t state;
/**
* @brief Current configuration data.
*/
const SerialConfig *config;
/**
* @brief Input queue, incoming data can be read from this input queue by
* using the queues APIs.
*/
InputQueue iqueue;
/**
* @brief Output queue, outgoing data can be written to this output queue by
* using the queues APIs.
*/
OutputQueue oqueue;
/**
* @brief Status Change @p EventSource. This event is generated when one or
* more condition flags change.
*/
EventSource sevent;
/**
* @brief I/O driver status flags.
*/
sdflags_t flags;
/**
* @brief Input circular buffer.
*/
uint8_t ib[SERIAL_BUFFERS_SIZE];
/**
* @brief Output circular buffer.
*/
uint8_t ob[SERIAL_BUFFERS_SIZE];
/* End of the mandatory fields.*/
/**
* Listen socket for simulated serial port.
*/
SOCKET com_listen;
/**
* Data socket for simulated serial port.
*/
SOCKET com_data;
/**
* Port readable name.
*/
const char *com_name;
};
/*===========================================================================*/
/* Driver macros. */
@ -168,7 +177,7 @@ extern SerialDriver SD2;
extern "C" {
#endif
void sd_lld_init(void);
void sd_lld_start(SerialDriver *sdp, const SerialDriverConfig *config);
void sd_lld_start(SerialDriver *sdp);
void sd_lld_stop(SerialDriver *sdp);
bool_t sd_lld_interrupt_pending(void);
#ifdef __cplusplus