/* Copyright (C) 2012-2013, 2015 Czech Technical University in Prague
*
- * 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.
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
*
* File : spi.c
*/
-//#include "ul/ul_list.h"
-//#include "drv/spi.h"
-//#include "cpu_def.h"
-//#include "ul/ul_list.h"
#include "drv/spi.h"
+#include "drv/spi_tms570.h"
-int spi_msg_rq_ins(spi_drv_t *ifc, spi_msg_t *msg)
+static boolean_t spi_initialized = FALSE;
+
+int spi_init()
{
- spi_isr_lock_level_t saveif;
+ if (spi_initialized == TRUE)
+ return FAILURE;
+ spi_initialized = TRUE;
- if (!ifc)
- return -1;
+ spi_tms570_init();
+ return SUCCESS;
+}
- if (!(ifc->flags & SPI_IFC_ON))
- return -1;
+static int spi_transfer_callback(struct spi_drv *ifc, int code, struct spi_msg *msg)
+{
+ if (msg->private)
+ msg->private = 0;
+ return 0;
+}
+
+/*
+ * Send SPI message asynchronously
+ *
+ * This function is thread safe.
+ */
+int spi_msg_rq_ins(spi_msg_t *msg)
+{
+ spi_isr_lock_level_t saveif;
+ spi_drv_t *ifc = spi_tms570_get_iface(msg->dev);
spi_isr_lock(saveif);
spi_rq_queue_insert(ifc, msg);
return 0;
}
-int spi_transfer_callback(struct spi_drv *ifc, int code, struct spi_msg *msg)
-{
- if (msg->private)
- msg->private = 0;
- return 0;
-}
-
/*
+ * Send SPI message synchronously
+ *
* This function is thread safe.
*/
-int spi_transfer(spi_drv_t *ifc, int addr, int rq_len, const void *tx_buf, void *rx_buf)
+int spi_transfer(enum spi_device dev, int rq_len, const void *tx_buf, void *rx_buf)
{
spi_msg_t msg;
msg.flags = 0;
//msg.ifc = NULL;
spi_rq_queue_init_detached(&msg);
- msg.addr = addr;
+ msg.dev = dev;
msg.rq_len = rq_len;
msg.tx_buf = tx_buf;
msg.rx_buf = rx_buf;
msg.callback = spi_transfer_callback;
msg.private = 1;
- if (spi_msg_rq_ins(ifc, &msg) < 0)
+ if (spi_msg_rq_ins(&msg) < 0)
return -1;
/* Wait for the request completion */
int8_t port_spi_set(const struct port_desc *port, void *values, size_t size)
{
- spi_drv_t *ifc;
- uint8_t rx[4];
+ uint8_t rx[24];
- assert(port->numchn == 1);
- assert(size == port->bpch/8);
+ assert(size == port->numchn * port->bpch / 8);
assert(size <= sizeof(rx));
- ifc = spi_find_drv(NULL, port->cfg.spi.ifc);
- if (ifc == NULL)
- return FAILURE;
-
- if (!(ifc->flags & SPI_IFC_ON))
- return FAILURE;
-
- spi_transfer(ifc, port->cfg.spi.cs, size, values, rx);
+ spi_transfer(port->cfg.spi.dev, size, values, rx);
memcpy(values, rx, MIN(size, port->numchn * port->bpch/8));
return SUCCESS;