From 499e575528f0e5a12babf1ee02e7eec370bcac81 Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Thu, 14 Dec 2017 19:04:01 +0900 Subject: [PATCH] Fix bb. --- bb.c | 23 +++++++++++++++++------ bb.h | 5 +++-- contrib/usart-stm32f103.c | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 contrib/usart-stm32f103.c diff --git a/bb.c b/bb.c index 9ca91ce..b975ba4 100644 --- a/bb.c +++ b/bb.c @@ -32,9 +32,10 @@ #include void -bb_init (struct bb *bb, uint32_t items) +bb_init (struct bb *bb, uint32_t max_item) { - bb->items = items; + bb->items = 0; + bb->max_items = max_item; chopstx_cond_init (&bb->cond); chopstx_mutex_init (&bb->mutex); } @@ -53,26 +54,36 @@ void bb_put (struct bb *bb) { chopstx_mutex_lock (&bb->mutex); + while (bb->items == bb->max_item) + chopstx_cond_wait (&bb->cond, &bb->mutex); bb->items++; chopstx_cond_signal (&bb->cond); chopstx_mutex_unlock (&bb->mutex); } static int -bb_check (void *arg) +bb_check_empty (void *arg) { struct bb *bb = arg; return bb->items != 0; } +static int +bb_check_full (void *arg) +{ + struct bb *bb = arg; + + return bb->items != bb->max_item; +} + void -bb_prepare_poll (struct bb *bb, chopstx_poll_cond_t *p) +bb_prepare_poll (struct bb *bb, chopstx_poll_cond_t *p, int full) { poll_desc->type = CHOPSTX_POLL_COND; poll_desc->ready = 0; poll_desc->cond = &bb->cond; poll_desc->mutex = &bb->mutex; - poll_desc->check = bb_check; - poll_desc->arg = ev; + poll_desc->check = full? bb_check_full: bb_check_empty; + poll_desc->arg = bb; } diff --git a/bb.h b/bb.h index 1251b3f..39d3980 100644 --- a/bb.h +++ b/bb.h @@ -2,10 +2,11 @@ struct bb { chopstx_mutex_t mutex; chopstx_cond_t cond; uint32_t items; + uint32_t max_item; }; -void bb_init (struct bb *bb, uint32_t items); +void bb_init (struct bb *bb, uint32_t max_item); void bb_get (struct bb *bb); void bb_put (struct bb *bb); -void bb_prepare_poll (struct bb *bb, chopstx_poll_cond_t *p); +void bb_prepare_poll (struct bb *bb, chopstx_poll_cond_t *p, int full); diff --git a/contrib/usart-stm32f103.c b/contrib/usart-stm32f103.c new file mode 100644 index 0000000..3fc79d8 --- /dev/null +++ b/contrib/usart-stm32f103.c @@ -0,0 +1,33 @@ +/* + * usart-stm32f103.c - USART driver for STM32F103 + * + * Copyright (C) 2017 g10 Code GmbH + * Author: NIIBE Yutaka + * + * This file is a part of Chopstx, a thread library for embedded. + * + * Chopstx is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Chopstx is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * As additional permission under GNU GPL version 3 section 7, you may + * distribute non-source form of the Program without the copy of the + * GNU GPL normally required by section 4, provided you inform the + * receipents of GNU GPL by a written offer. + * + */ + +#include +#include +#include +#include +#include