re-format the coding style in spi.h
1, Tabs to Spaces 2, File Format(CR/LF) using UNIX style 3, maximum line length = 80 git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2275 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
parent
20de2ffa18
commit
fd425e792d
|
@ -1,213 +1,250 @@
|
||||||
#ifndef __SPI_H__
|
/*
|
||||||
#define __SPI_H__
|
* File : spi.h
|
||||||
|
* This file is part of RT-Thread RTOS
|
||||||
#include <rtthread.h>
|
* COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
|
||||||
|
*
|
||||||
|
* The license and distribution terms for this file may be
|
||||||
#define RT_SPI_CPHA (1<<0) /* bit[0]:CPHA, clock phase */
|
* found in the file LICENSE in this distribution or at
|
||||||
#define RT_SPI_CPOL (1<<1) /* bit[1]:CPOL, clock polarity */
|
* http://www.rt-thread.org/license/LICENSE
|
||||||
/**
|
*
|
||||||
* At CPOL=0 the base value of the clock is zero
|
* Change Logs:
|
||||||
* - For CPHA=0, data are captured on the clock's rising edge (low¡úhigh transition)
|
* Date Author Notes
|
||||||
* and data are propagated on a falling edge (high¡úlow clock transition).
|
*/
|
||||||
* - For CPHA=1, data are captured on the clock's falling edge and data are
|
|
||||||
* propagated on a rising edge.
|
#ifndef __SPI_H__
|
||||||
* At CPOL=1 the base value of the clock is one (inversion of CPOL=0)
|
#define __SPI_H__
|
||||||
* - For CPHA=0, data are captured on clock's falling edge and data are propagated
|
|
||||||
* on a rising edge.
|
#include <rtthread.h>
|
||||||
* - For CPHA=1, data are captured on clock's rising edge and data are propagated
|
|
||||||
* on a falling edge.
|
#define RT_SPI_CPHA (1<<0) /* bit[0]:CPHA, clock phase */
|
||||||
*/
|
#define RT_SPI_CPOL (1<<1) /* bit[1]:CPOL, clock polarity */
|
||||||
#define RT_SPI_LSB (0<<2) /* bit[2]: 0-LSB */
|
/**
|
||||||
#define RT_SPI_MSB (1<<2) /* bit[2]: 1-MSB */
|
* At CPOL=0 the base value of the clock is zero
|
||||||
|
* - For CPHA=0, data are captured on the clock's rising edge (low¡úhigh transition)
|
||||||
#define RT_SPI_MASTER (0<<3) /* SPI master device */
|
* and data are propagated on a falling edge (high¡úlow clock transition).
|
||||||
#define RT_SPI_SLAVE (1<<3) /* SPI slave device */
|
* - For CPHA=1, data are captured on the clock's falling edge and data are
|
||||||
|
* propagated on a rising edge.
|
||||||
#define RT_SPI_MODE_0 (0 | 0) /* CPOL = 0, CPHA = 0 */
|
* At CPOL=1 the base value of the clock is one (inversion of CPOL=0)
|
||||||
#define RT_SPI_MODE_1 (0 | RT_SPI_CPHA) /* CPOL = 0, CPHA = 1 */
|
* - For CPHA=0, data are captured on clock's falling edge and data are propagated
|
||||||
#define RT_SPI_MODE_2 (RT_SPI_CPOL | 0) /* CPOL = 1, CPHA = 0 */
|
* on a rising edge.
|
||||||
#define RT_SPI_MODE_3 (RT_SPI_CPOL | RT_SPI_CPHA) /* CPOL = 1, CPHA = 1 */
|
* - For CPHA=1, data are captured on clock's rising edge and data are propagated
|
||||||
|
* on a falling edge.
|
||||||
#define RT_SPI_MODE_MASK (RT_SPI_CPHA | RT_SPI_CPOL | RT_SPI_MSB)
|
*/
|
||||||
|
#define RT_SPI_LSB (0<<2) /* bit[2]: 0-LSB */
|
||||||
/**
|
#define RT_SPI_MSB (1<<2) /* bit[2]: 1-MSB */
|
||||||
* SPI message structure
|
|
||||||
*/
|
#define RT_SPI_MASTER (0<<3) /* SPI master device */
|
||||||
struct rt_spi_message
|
#define RT_SPI_SLAVE (1<<3) /* SPI slave device */
|
||||||
{
|
|
||||||
const void* send_buf;
|
#define RT_SPI_MODE_0 (0 | 0) /* CPOL = 0, CPHA = 0 */
|
||||||
void* recv_buf;
|
#define RT_SPI_MODE_1 (0 | RT_SPI_CPHA) /* CPOL = 0, CPHA = 1 */
|
||||||
rt_size_t length;
|
#define RT_SPI_MODE_2 (RT_SPI_CPOL | 0) /* CPOL = 1, CPHA = 0 */
|
||||||
struct rt_spi_message* next;
|
#define RT_SPI_MODE_3 (RT_SPI_CPOL | RT_SPI_CPHA) /* CPOL = 1, CPHA = 1 */
|
||||||
|
|
||||||
unsigned cs_take:1;
|
#define RT_SPI_MODE_MASK (RT_SPI_CPHA | RT_SPI_CPOL | RT_SPI_MSB)
|
||||||
unsigned cs_release:1;
|
|
||||||
};
|
/**
|
||||||
|
* SPI message structure
|
||||||
/**
|
*/
|
||||||
* SPI configuration structure
|
struct rt_spi_message
|
||||||
*/
|
{
|
||||||
struct rt_spi_configuration
|
const void *send_buf;
|
||||||
{
|
void *recv_buf;
|
||||||
rt_uint8_t mode;
|
rt_size_t length;
|
||||||
rt_uint8_t data_width;
|
struct rt_spi_message *next;
|
||||||
rt_uint16_t reserved;
|
|
||||||
|
unsigned cs_take : 1;
|
||||||
rt_uint32_t max_hz;
|
unsigned cs_release : 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct rt_spi_ops;
|
/**
|
||||||
struct rt_spi_bus
|
* SPI configuration structure
|
||||||
{
|
*/
|
||||||
struct rt_device parent;
|
struct rt_spi_configuration
|
||||||
const struct rt_spi_ops *ops;
|
{
|
||||||
|
rt_uint8_t mode;
|
||||||
struct rt_mutex lock;
|
rt_uint8_t data_width;
|
||||||
struct rt_spi_device* owner;
|
rt_uint16_t reserved;
|
||||||
};
|
|
||||||
|
rt_uint32_t max_hz;
|
||||||
/**
|
};
|
||||||
* SPI operators
|
|
||||||
*/
|
struct rt_spi_ops;
|
||||||
struct rt_spi_ops
|
struct rt_spi_bus
|
||||||
{
|
{
|
||||||
rt_err_t (*configure)(struct rt_spi_device* device, struct rt_spi_configuration* configuration);
|
struct rt_device parent;
|
||||||
rt_uint32_t (*xfer)(struct rt_spi_device* device, struct rt_spi_message* message);
|
const struct rt_spi_ops *ops;
|
||||||
};
|
|
||||||
|
struct rt_mutex lock;
|
||||||
/**
|
struct rt_spi_device *owner;
|
||||||
* SPI Virtual BUS, one device must connected to a virtual BUS
|
};
|
||||||
*/
|
|
||||||
struct rt_spi_device
|
/**
|
||||||
{
|
* SPI operators
|
||||||
struct rt_device parent;
|
*/
|
||||||
struct rt_spi_bus *bus;
|
struct rt_spi_ops
|
||||||
|
{
|
||||||
struct rt_spi_configuration config;
|
rt_err_t (*configure)(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
|
||||||
};
|
rt_uint32_t (*xfer)(struct rt_spi_device *device, struct rt_spi_message *message);
|
||||||
#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,
|
* SPI Virtual BUS, one device must connected to a virtual BUS
|
||||||
const struct rt_spi_ops* ops);
|
*/
|
||||||
/* attach a device on SPI bus */
|
struct rt_spi_device
|
||||||
rt_err_t rt_spi_bus_attach_device(struct rt_spi_device* device, const char* name,
|
{
|
||||||
const char* bus_name, void* user_data);
|
struct rt_device parent;
|
||||||
/**
|
struct rt_spi_bus *bus;
|
||||||
* This function takes SPI bus.
|
|
||||||
*
|
struct rt_spi_configuration config;
|
||||||
* @param device the SPI device attached to SPI bus
|
};
|
||||||
*
|
#define SPI_DEVICE(dev) ((struct rt_spi_device *)(dev))
|
||||||
* @return RT_EOK on taken SPI bus successfully. others on taken SPI bus failed.
|
|
||||||
*/
|
/* register a SPI bus */
|
||||||
rt_err_t rt_spi_take_bus(struct rt_spi_device* device);
|
rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus,
|
||||||
/**
|
const char *name,
|
||||||
* This function releases SPI bus.
|
const struct rt_spi_ops *ops);
|
||||||
*
|
|
||||||
* @param device the SPI device attached to SPI bus
|
/* attach a device on SPI bus */
|
||||||
*
|
rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device,
|
||||||
* @return RT_EOK on release SPI bus successfully.
|
const char *name,
|
||||||
*/
|
const char *bus_name,
|
||||||
rt_err_t rt_spi_release_bus(struct rt_spi_device* device);
|
void *user_data);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function take SPI device (takes CS of SPI device).
|
* This function takes SPI bus.
|
||||||
*
|
*
|
||||||
* @param device the SPI device attached to SPI bus
|
* @param device the SPI device attached to SPI bus
|
||||||
*
|
*
|
||||||
* @return RT_EOK on release SPI bus successfully. others on taken SPI bus failed.
|
* @return RT_EOK on taken SPI bus successfully. others on taken SPI bus failed.
|
||||||
*/
|
*/
|
||||||
rt_err_t rt_spi_take(struct rt_spi_device* device);
|
rt_err_t rt_spi_take_bus(struct rt_spi_device *device);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function releases SPI device (releases CS of SPI device).
|
* This function releases SPI bus.
|
||||||
*
|
*
|
||||||
* @param device the SPI device attached to SPI bus
|
* @param device the SPI device attached to SPI bus
|
||||||
*
|
*
|
||||||
* @return RT_EOK on release SPI device successfully.
|
* @return RT_EOK on release SPI bus successfully.
|
||||||
*/
|
*/
|
||||||
rt_err_t rt_spi_release(struct rt_spi_device* device);
|
rt_err_t rt_spi_release_bus(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);
|
* This function take SPI device (takes CS of SPI device).
|
||||||
|
*
|
||||||
/* send data then receive data from SPI device */
|
* @param device the SPI device attached to SPI bus
|
||||||
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);
|
* @return RT_EOK on release SPI bus successfully. others on taken SPI bus failed.
|
||||||
|
*/
|
||||||
rt_err_t rt_spi_send_then_send(struct rt_spi_device* device, const void *send_buf1, rt_size_t send_length1,
|
rt_err_t rt_spi_take(struct rt_spi_device *device);
|
||||||
const void* send_buf2, rt_size_t send_length2);
|
|
||||||
|
/**
|
||||||
/**
|
* This function releases SPI device (releases CS of SPI device).
|
||||||
* This function transmits data to SPI device.
|
*
|
||||||
*
|
* @param device the SPI device attached to SPI bus
|
||||||
* @param device the SPI device attached to SPI bus
|
*
|
||||||
* @param send_buf the buffer to be transmitted to SPI device.
|
* @return RT_EOK on release SPI device successfully.
|
||||||
* @param recv_buf the buffer to save received data from SPI device.
|
*/
|
||||||
* @param length the length of transmitted data.
|
rt_err_t rt_spi_release(struct rt_spi_device *device);
|
||||||
*
|
|
||||||
* @return the actual length of transmitted.
|
/* set configuration on SPI device */
|
||||||
*/
|
rt_err_t rt_spi_configure(struct rt_spi_device *device,
|
||||||
rt_size_t rt_spi_transfer(struct rt_spi_device* device, const void *send_buf,
|
struct rt_spi_configuration *cfg);
|
||||||
void* recv_buf, rt_size_t length);
|
|
||||||
|
/* send data then receive data from SPI device */
|
||||||
/**
|
rt_err_t rt_spi_send_then_recv(struct rt_spi_device *device,
|
||||||
* This function transfers a message list to the SPI device.
|
const void *send_buf,
|
||||||
*
|
rt_size_t send_length,
|
||||||
* @param device the SPI device attached to SPI bus
|
void *recv_buf,
|
||||||
* @param message the message list to be transmitted to SPI device
|
rt_size_t recv_length);
|
||||||
*
|
|
||||||
* @return RT_NULL if transmits message list successfully,
|
rt_err_t rt_spi_send_then_send(struct rt_spi_device *device,
|
||||||
* SPI message which be transmitted failed.
|
const void *send_buf1,
|
||||||
*/
|
rt_size_t send_length1,
|
||||||
struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device* device,
|
const void *send_buf2,
|
||||||
struct rt_spi_message *message);
|
rt_size_t send_length2);
|
||||||
|
|
||||||
rt_inline rt_size_t rt_spi_recv(struct rt_spi_device* device, void* recv_buf, rt_size_t length)
|
/**
|
||||||
{
|
* This function transmits data to SPI device.
|
||||||
return rt_spi_transfer(device, RT_NULL, recv_buf, length);
|
*
|
||||||
}
|
* @param device the SPI device attached to SPI bus
|
||||||
|
* @param send_buf the buffer to be transmitted to SPI device.
|
||||||
rt_inline rt_size_t rt_spi_send(struct rt_spi_device* device, const void* send_buf, rt_size_t length)
|
* @param recv_buf the buffer to save received data from SPI device.
|
||||||
{
|
* @param length the length of transmitted data.
|
||||||
return rt_spi_transfer(device, send_buf, RT_NULL, length);
|
*
|
||||||
}
|
* @return the actual length of transmitted.
|
||||||
|
*/
|
||||||
rt_inline rt_uint8_t rt_spi_sendrecv8(struct rt_spi_device* device, rt_uint8_t data)
|
rt_size_t rt_spi_transfer(struct rt_spi_device *device,
|
||||||
{
|
const void *send_buf,
|
||||||
rt_uint8_t value;
|
void *recv_buf,
|
||||||
|
rt_size_t length);
|
||||||
rt_spi_send_then_recv(device, &data, 1, &value, 1);
|
|
||||||
return value;
|
/**
|
||||||
}
|
* This function transfers a message list to the SPI device.
|
||||||
|
*
|
||||||
rt_inline rt_uint16_t rt_spi_sendrecv16(struct rt_spi_device* device, rt_uint16_t data)
|
* @param device the SPI device attached to SPI bus
|
||||||
{
|
* @param message the message list to be transmitted to SPI device
|
||||||
rt_uint16_t value;
|
*
|
||||||
|
* @return RT_NULL if transmits message list successfully,
|
||||||
rt_spi_send_then_recv(device, &data, 2, &value, 2);
|
* SPI message which be transmitted failed.
|
||||||
return value;
|
*/
|
||||||
}
|
struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device,
|
||||||
|
struct rt_spi_message *message);
|
||||||
/**
|
|
||||||
* This function appends a message to the SPI message list.
|
rt_inline rt_size_t rt_spi_recv(struct rt_spi_device *device,
|
||||||
*
|
void *recv_buf,
|
||||||
* @param list the SPI message list header.
|
rt_size_t length)
|
||||||
* @param message the message pointer to be appended to the message list.
|
{
|
||||||
*/
|
return rt_spi_transfer(device, RT_NULL, recv_buf, length);
|
||||||
rt_inline void rt_spi_message_append(struct rt_spi_message* list, struct rt_spi_message* message)
|
}
|
||||||
{
|
|
||||||
RT_ASSERT(list != RT_NULL);
|
rt_inline rt_size_t rt_spi_send(struct rt_spi_device *device,
|
||||||
if (message == RT_NULL) return; /* not append */
|
const void *send_buf,
|
||||||
|
rt_size_t length)
|
||||||
while (list->next != RT_NULL)
|
{
|
||||||
{
|
return rt_spi_transfer(device, send_buf, RT_NULL, length);
|
||||||
list = list->next;
|
}
|
||||||
}
|
|
||||||
|
rt_inline rt_uint8_t rt_spi_sendrecv8(struct rt_spi_device *device,
|
||||||
list->next = message;
|
rt_uint8_t data)
|
||||||
message->next = RT_NULL;
|
{
|
||||||
}
|
rt_uint8_t value;
|
||||||
|
|
||||||
#endif
|
rt_spi_send_then_recv(device, &data, 1, &value, 1);
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
rt_inline rt_uint16_t rt_spi_sendrecv16(struct rt_spi_device *device,
|
||||||
|
rt_uint16_t data)
|
||||||
|
{
|
||||||
|
rt_uint16_t value;
|
||||||
|
|
||||||
|
rt_spi_send_then_recv(device, &data, 2, &value, 2);
|
||||||
|
|
||||||
|
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
|
||||||
|
|
Loading…
Reference in New Issue