summaryrefslogtreecommitdiff
path: root/components/drivers
diff options
context:
space:
mode:
author[email protected] <[email protected]>2012-05-18 05:37:09 +0000
committer[email protected] <[email protected]>2012-05-18 05:37:09 +0000
commit03668c93b980fd17e21f5c4b994abb311cefaeba (patch)
treea70beb0cf139cc11955cb0f9e0fa385727b292d1 /components/drivers
parentea6d73f14052ffe684092c33b64dfa50f6579843 (diff)
Changed SPI message to message list. Added take/release SPI device/bus interface.
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2123 bbd45198-f89e-11dd-88c7-29a3b14d5316
Diffstat (limited to 'components/drivers')
-rw-r--r--components/drivers/include/drivers/spi.h86
-rw-r--r--components/drivers/spi/spi_core.c149
2 files changed, 232 insertions, 3 deletions
diff --git a/components/drivers/include/drivers/spi.h b/components/drivers/include/drivers/spi.h
index 3d395a7bab..cf0f51cff2 100644
--- a/components/drivers/include/drivers/spi.h
+++ b/components/drivers/include/drivers/spi.h
@@ -39,6 +39,7 @@ struct rt_spi_message
const void* send_buf;
void* recv_buf;
rt_size_t length;
+ struct rt_spi_message* next;
unsigned cs_take:1;
unsigned cs_release:1;
@@ -88,22 +89,81 @@ struct rt_spi_device
#define SPI_DEVICE(dev) ((struct rt_spi_device*)(dev))
/* register a SPI bus */
-rt_err_t rt_spi_bus_register(struct rt_spi_bus* bus, const char* name, const struct rt_spi_ops* ops);
+rt_err_t rt_spi_bus_register(struct rt_spi_bus* bus, const char* name,
+ const struct rt_spi_ops* ops);
/* attach a device on SPI bus */
-rt_err_t rt_spi_bus_attach_device(struct rt_spi_device* device, const char* name, const char* bus_name, void* user_data);
+rt_err_t rt_spi_bus_attach_device(struct rt_spi_device* device, const char* name,
+ const char* bus_name, void* user_data);
+/**
+ * This function takes SPI bus.
+ *
+ * @param device the SPI device attached to SPI bus
+ *
+ * @return RT_EOK on taken SPI bus successfully. others on taken SPI bus failed.
+ */
+rt_err_t rt_spi_take_bus(struct rt_spi_device* device);
+/**
+ * This function releases SPI bus.
+ *
+ * @param device the SPI device attached to SPI bus
+ *
+ * @return RT_EOK on release SPI bus successfully.
+ */
+rt_err_t rt_spi_release_bus(struct rt_spi_device* device);
+
+/**
+ * This function take SPI device (takes CS of SPI device).
+ *
+ * @param device the SPI device attached to SPI bus
+ *
+ * @return RT_EOK on release SPI bus successfully. others on taken SPI bus failed.
+ */
+rt_err_t rt_spi_take(struct rt_spi_device* device);
+
+/**
+ * This function releases SPI device (releases CS of SPI device).
+ *
+ * @param device the SPI device attached to SPI bus
+ *
+ * @return RT_EOK on release SPI device successfully.
+ */
+rt_err_t rt_spi_release(struct rt_spi_device* device);
+
/* set configuration on SPI device */
rt_err_t rt_spi_configure(struct rt_spi_device* device, struct rt_spi_configuration* cfg);
-/* send data then receive data from SPI devicew */
+/* send data then receive data from SPI device */
rt_err_t rt_spi_send_then_recv(struct rt_spi_device* device, const void *send_buf, rt_size_t send_length,
void* recv_buf, rt_size_t recv_length);
rt_err_t rt_spi_send_then_send(struct rt_spi_device* device, const void *send_buf1, rt_size_t send_length1,
const void* send_buf2, rt_size_t send_length2);
+/**
+ * This function transmits data to SPI device.
+ *
+ * @param device the SPI device attached to SPI bus
+ * @param send_buf the buffer to be transmitted to SPI device.
+ * @param recv_buf the buffer to save received data from SPI device.
+ * @param length the length of transmitted data.
+ *
+ * @return the actual length of transmitted.
+ */
rt_size_t rt_spi_transfer(struct rt_spi_device* device, const void *send_buf,
void* recv_buf, rt_size_t length);
+/**
+ * This function transfers a message list to the SPI device.
+ *
+ * @param device the SPI device attached to SPI bus
+ * @param message the message list to be transmitted to SPI device
+ *
+ * @return RT_NULL if transmits message list successfully,
+ * SPI message which be transmitted failed.
+ */
+struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device* device,
+ struct rt_spi_message *message);
+
rt_inline rt_size_t rt_spi_recv(struct rt_spi_device* device, void* recv_buf, rt_size_t length)
{
return rt_spi_transfer(device, RT_NULL, recv_buf, length);
@@ -130,4 +190,24 @@ rt_inline rt_uint16_t rt_spi_sendrecv16(struct rt_spi_device* device, rt_uint16_
return value;
}
+/**
+ * This function appends a message to the SPI message list.
+ *
+ * @param list the SPI message list header.
+ * @param message the message pointer to be appended to the message list.
+ */
+rt_inline void rt_spi_message_append(struct rt_spi_message* list, struct rt_spi_message* message)
+{
+ RT_ASSERT(list != RT_NULL);
+ if (message == RT_NULL) return; /* not append */
+
+ while (list->next != RT_NULL)
+ {
+ list = list->next;
+ }
+
+ list->next = message;
+ message->next = RT_NULL;
+}
+
#endif
diff --git a/components/drivers/spi/spi_core.c b/components/drivers/spi/spi_core.c
index 3425498451..ac1295ee9e 100644
--- a/components/drivers/spi/spi_core.c
+++ b/components/drivers/spi/spi_core.c
@@ -111,6 +111,7 @@ rt_err_t rt_spi_send_then_send(struct rt_spi_device* device, const void *send_bu
message.length = send_length1;
message.cs_take = 1;
message.cs_release = 0;
+ message.next = RT_NULL;
result = device->bus->ops->xfer(device, &message);
if (result == 0)
@@ -125,6 +126,7 @@ rt_err_t rt_spi_send_then_send(struct rt_spi_device* device, const void *send_bu
message.length = send_length2;
message.cs_take = 0;
message.cs_release = 1;
+ message.next = RT_NULL;
result = device->bus->ops->xfer(device, &message);
if (result == 0)
@@ -180,6 +182,7 @@ rt_err_t rt_spi_send_then_recv(struct rt_spi_device* device, const void *send_bu
message.length = send_length;
message.cs_take = 1;
message.cs_release = 0;
+ message.next = RT_NULL;
result = device->bus->ops->xfer(device, &message);
if (result == 0)
@@ -194,6 +197,7 @@ rt_err_t rt_spi_send_then_recv(struct rt_spi_device* device, const void *send_bu
message.length = recv_length;
message.cs_take = 0;
message.cs_release = 1;
+ message.next = RT_NULL;
result = device->bus->ops->xfer(device, &message);
if (result == 0)
@@ -249,6 +253,7 @@ rt_size_t rt_spi_transfer(struct rt_spi_device* device, const void *send_buf,
message.recv_buf = recv_buf;
message.length = length;
message.cs_take = message.cs_release = 1;
+ message.next = RT_NULL;
/* transfer message */
result = device->bus->ops->xfer(device, &message);
@@ -268,3 +273,147 @@ __exit:
rt_mutex_release(&(device->bus->lock));
return result;
}
+
+struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device* device,
+ struct rt_spi_message *message)
+{
+ rt_err_t result;
+ struct rt_spi_message* index;
+
+ RT_ASSERT(device != RT_NULL);
+
+ /* get first message */
+ index = message;
+ if (index == RT_NULL) return index;
+
+ result = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
+ if (result != RT_EOK)
+ {
+ rt_set_errno(-RT_EBUSY);
+ return index;
+ }
+
+ /* reset errno */
+ rt_set_errno(RT_EOK);
+
+ /* configure SPI bus */
+ if (device->bus->owner != device)
+ {
+ /* not the same owner as current, re-configure SPI bus */
+ result = device->bus->ops->configure(device, &device->config);
+ if (result == RT_EOK)
+ {
+ /* set SPI bus owner */
+ device->bus->owner = device;
+ }
+ else
+ {
+ /* configure SPI bus failed */
+ rt_set_errno(-RT_EIO);
+ result = 0;
+ goto __exit;
+ }
+ }
+
+ /* transmit each SPI message */
+ while (index != RT_NULL)
+ {
+ /* transmit SPI message */
+ result = device->bus->ops->xfer(device, index);
+ if (result == 0)
+ {
+ rt_set_errno(-RT_EIO);
+ break;
+ }
+
+ index = index->next;
+ }
+
+__exit:
+ /* release bus lock */
+ rt_mutex_release(&(device->bus->lock));
+ return index;
+}
+
+rt_err_t rt_spi_take_bus(struct rt_spi_device* device)
+{
+ rt_err_t result = RT_EOK;
+
+ RT_ASSERT(device != RT_NULL);
+ RT_ASSERT(device->bus != RT_NULL);
+
+ result = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
+ if (result != RT_EOK)
+ {
+ rt_set_errno(-RT_EBUSY);
+ return -RT_EBUSY;
+ }
+
+ /* reset errno */
+ rt_set_errno(RT_EOK);
+
+ /* configure SPI bus */
+ if (device->bus->owner != device)
+ {
+ /* not the same owner as current, re-configure SPI bus */
+ result = device->bus->ops->configure(device, &device->config);
+ if (result == RT_EOK)
+ {
+ /* set SPI bus owner */
+ device->bus->owner = device;
+ }
+ else
+ {
+ /* configure SPI bus failed */
+ rt_set_errno(-RT_EIO);
+ /* release lock */
+ rt_mutex_release(&(device->bus->lock));
+
+ return -RT_EIO;
+ }
+ }
+
+ return result;
+}
+
+rt_err_t rt_spi_release_bus(struct rt_spi_device* device)
+{
+ RT_ASSERT(device != RT_NULL);
+ RT_ASSERT(device->bus != RT_NULL);
+ RT_ASSERT(device->bus->owner != device);
+
+ /* release lock */
+ rt_mutex_release(&(device->bus->lock));
+
+ return RT_EOK;
+}
+
+rt_err_t rt_spi_take(struct rt_spi_device* device)
+{
+ rt_err_t result;
+ struct rt_spi_message message;
+
+ RT_ASSERT(device != RT_NULL);
+ RT_ASSERT(device->bus != RT_NULL);
+
+ rt_memset(&message, 0, sizeof(message));
+ message.cs_take = 1;
+
+ result = device->bus->ops->xfer(device, &message);
+ return result;
+}
+
+rt_err_t rt_spi_release(struct rt_spi_device* device)
+{
+ rt_err_t result;
+ struct rt_spi_message message;
+
+ RT_ASSERT(device != RT_NULL);
+ RT_ASSERT(device->bus != RT_NULL);
+
+ rt_memset(&message, 0, sizeof(message));
+ message.cs_release = 1;
+
+ result = device->bus->ops->xfer(device, &message);
+ return result;
+}