1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/sys/isrpipe/isrpipe.c

49 lines
1.1 KiB
C
Raw Normal View History

2016-12-07 20:32:00 +01:00
/*
* Copyright (C) 2016 Kaspar Schleiser <kaspar@schleiser.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup sys
* @{
* @file
* @brief ISR -> userspace pipe implementation
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
*
* @}
*/
#include "isrpipe.h"
void isrpipe_init(isrpipe_t *isrpipe, char *buf, size_t bufsize)
{
mutex_init(&isrpipe->mutex);
tsrb_init(&isrpipe->tsrb, (uint8_t *)buf, bufsize);
2016-12-07 20:32:00 +01:00
}
int isrpipe_write_one(isrpipe_t *isrpipe, char c)
{
int res = tsrb_add_one(&isrpipe->tsrb, c);
/* `res` is either 0 on success or -1 when the buffer is full. Either way,
* unlocking the mutex is fine.
*/
mutex_unlock(&isrpipe->mutex);
return res;
}
int isrpipe_read(isrpipe_t *isrpipe, char *buffer, size_t count)
{
int res;
while (!(res = tsrb_get(&isrpipe->tsrb, (uint8_t *)buffer, count))) {
2016-12-07 20:32:00 +01:00
mutex_lock(&isrpipe->mutex);
}
return res;
}