Exemple #1
0
/**
 * \brief Allocate the device.
 * \param dev Device to allocate.
 * \param tmo Time-out
 * \return 0 success, -1 otherwise.
 * 
 * The device will be locked for other threads. If the device is already locked,
 * it will wait for max. It waits for tmo milli seconds if the device is already
 * locked.
 */
PUBLIC int BermudaDeviceAlloc(DEVICE *dev, unsigned int tmo)
{
	int rc = -1;
	if(dev != NULL) {
#ifdef __EVENTS__
		if(BermudaEventWait((volatile THREAD**)dev->mutex, tmo) == 0) {
			rc = 0;
		}
#else
		rc = 0;
#endif
	}

	return rc;
}
Exemple #2
0
/**
 * \brief Request an I2C I/O file.
 * \param client I2C driver client.
 * \param flags File flags.
 * \return The file descriptor associated with this client.
 */
PUBLIC int i2cdev_socket(struct i2c_client *client, uint16_t flags)
{
	FILE *socket;
	i2c_features_t features;
	struct i2c_shared_info *shinfo = i2c_shinfo(client);
	int rc = -1;
	
	if(client == NULL) {
		goto out;
	}
	
	if(BermudaEventWait(event(&(shinfo->mutex)), I2C_MASTER_TMO) != 0) {
		goto out;
	}
	
	socket = BermudaHeapAlloc(sizeof(*socket));
	if(!socket) {
		rc = -1;
		BermudaEventSignal(event(&(shinfo->mutex)));
		goto out;
	}
	
	rc = iob_add(socket);
	if(rc < 0) {
		BermudaHeapFree(socket);
		BermudaEventSignal(event(&(shinfo->mutex)));
		goto out;
	}
	
	features = i2c_client_features(client);
	features |= I2C_CLIENT_HAS_LOCK_FLAG;
	i2c_client_set_features(client, features);
	shinfo->socket = socket;
	
	socket->data = client;
	socket->name = "I2C";
	socket->write = &i2cdev_write;
	socket->read = &i2cdev_read;
	socket->flush = &i2cdev_flush;
	socket->close = &i2cdev_close;
	socket->flags = flags;
	
	out:
	return rc;
}
Exemple #3
0
PUBLIC int printf_P(const char *fmt, ...)
{
    int i;
#ifdef __EVENTS__
    if(BermudaEventWait((volatile THREAD**)USART0->mutex, 500) == -1) {
        i = -1;
        goto out;
    }
#endif

    va_list va;
    va_start(va, fmt);
    vfprintf_P(stdout, fmt, va);
    va_end(va);

#ifdef __EVENTS__
    BermudaEventSignal((volatile THREAD**)USART0->mutex);
out:
#endif

    return i;
}