-/* Copyright (C) 2013 Czech Technical University in Prague
+/* Copyright (C) 2013, 2016 Czech Technical University in Prague
*
* Authors:
* - Carlos Jenkins <carlos@jenkins.co.cr>
*
- * This program 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 2 of the License, or
- * (at your option) any later version.
- *
- * This program 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 <http://www.gnu.org/licenses/>.
+ * This document contains proprietary information belonging to Czech
+ * Technical University in Prague. Passing on and copying of this
+ * document, and communication of its contents is not permitted
+ * without prior written authorization.
*
* File : mout.c
* Abstract:
#include "rpp/rpp.h"
+#include "rpp/mutex.h"
+
+RPP_MUTEX_DEFINE(mutex_mout);
-#if rppCONFIG_INCLUDE_MOUT == 1
+#ifndef FREERTOS_POSIX
+#include "drv/mout.h"
+#endif
static boolean_t initialized = FALSE;
int8_t rpp_mout_init()
{
- if(initialized) {
- return FAILURE;
- }
- initialized = TRUE;
+ if (initialized)
+ return FAILURE;
+ if (!RPP_MUTEX_INIT(mutex_mout))
+ return FAILURE;
+ initialized = TRUE;
+#ifndef FREERTOS_POSIX
+ dmmInit();
+ gioInit();
+ hetInit();
+#endif
- // FIXME: Implement.
- return SUCCESS;
+ return SUCCESS;
}
-#endif /* rppCONFIG_INCLUDE_MOUT */
+static uint8_t cache[] = {
+ LOW, LOW, LOW, LOW, LOW, LOW
+};
+
+int8_t rpp_mout_set(uint8_t pin, uint8_t val)
+{
+ int ret;
+ // Check range
+ if ((pin < 1) || (pin > 6))
+ return -1;
+
+ // Check val
+ if ((val != HIGH) && (val != LOW))
+ return -2;
+
+ uint8_t idx = pin - 1;
+
+#ifndef FREERTOS_POSIX
+ // Set and store value
+ RPP_MUTEX_LOCK(mutex_mout);
+ ret = drv_mout_set(idx, val);
+ if (ret == SUCCESS)
+ cache[idx] = val;
+ RPP_MUTEX_UNLOCK(mutex_mout);
+ if (ret != SUCCESS)
+ return -3;
+
+ // FIXME Wait some time for synchronization
+ // Don't use vTaskDelay() here because the minimum time to wait is one tick,
+ // and depending of the configuration of the user model one tick can overrun
+ // the program.
+ int wait;
+ for (wait = 0; wait < 10; wait++) {
+ asm (" nop");
+ }
+ RPP_MUTEX_LOCK(mutex_mout);
+ // Get value back and compare
+ ret = drv_mout_diag(idx);
+ RPP_MUTEX_UNLOCK(mutex_mout);
+ if (ret < 0) {
+ return -4;
+ }
+
+#else
+ RPP_MUTEX_LOCK(mutex_mout);
+ cache[idx] = val;
+ RPP_MUTEX_UNLOCK(mutex_mout);
+#endif
+
+ return SUCCESS;
+}
+
+
+int8_t rpp_mout_get(uint8_t pin)
+{
+ // Check range
+ if ((pin < 1) || (pin > 6))
+ return -1;
+
+ RPP_MUTEX_LOCK(mutex_mout);
+ int8_t ret_val = cache[pin - 1];
+ RPP_MUTEX_UNLOCK(mutex_mout);
+
+ return ret_val;
+}
+
+
+int8_t rpp_mout_diag(uint8_t pin)
+{
+ int ret;
+ // Check range
+ if ((pin < 1) || (pin > 6))
+ return -RPP_EINVAL;
+
+#ifndef FREERTOS_POSIX
+ RPP_MUTEX_LOCK(mutex_mout);
+ ret = drv_mout_diag(pin - 1);
+ RPP_MUTEX_UNLOCK(mutex_mout);
+#endif
+ return ret;
+}