Browse Source

refine spi api interface

Signed-off-by: surenyi <surenyi82@qq.com>
bfsk
surenyi 3 years ago
parent
commit
074033f842
  1. 10
      include/spi.h
  2. 7
      platforms/dsk/tlv320aic23.c
  3. 8
      src/platform.c
  4. 13
      src/platform_internal.h
  5. 50
      src/spi.c

10
include/spi.h

@ -5,22 +5,22 @@
extern "C" {
#endif
typedef struct __spi_handle *spi_t;
#define SPI_MODE_0 0
#define SPI_MODE_1 1
#define SPI_MODE_2 2
#define SPI_MODE_3 3
spi_t spi_open(int dev_id, int mode, int freq, int bits);
int spi_open(int dev_id, int mode, int freq, int bits);
int spi_write(spi_t spi, const void *buf, int len);
int spi_read(spi_t spi, void *buf, int len);
int spi_write(int dev_id, const void *buf, int len);
int spi_read(int dev_id, void *buf, int len);
void spi_close(spi_t);
void spi_close(int dev_id);
#ifdef __cplusplus
}
#endif
#endif

7
platforms/dsk/tlv320aic23.c

@ -111,15 +111,14 @@ void aic23_init()
0x9, 0x1,
};
Uint16 i, data;
spi_t spi_dev;
spi_dev = spi_open(MCBSP_DEV1, SPI_MODE_3, 2000000, 16);
if (!spi_dev)
if (spi_open(MCBSP_DEV1, SPI_MODE_3, 2000000, 16)) {
return;
}
for (i = 0; i < ARRAY_SIZE(aic23); i += 2) {
data = aic23[i] << 9 | aic23[i+1];
spi_write(spi_dev, &data, sizeof data);
spi_write(MCBSP_DEV1, &data, sizeof data);
}
/* spi_close(spi_dev); */
}

8
src/platform.c

@ -1,10 +1,4 @@
#include "platform.h"
extern far void vectors();
int tick_init();
extern void irq_lowlevel_init();
extern void spi_lowlevel_init();
#include "platform_internal.h"
int platform_init()
{

13
src/platform_internal.h

@ -0,0 +1,13 @@
#ifndef __PLATFORM_INTERNAL_H__
#define __PLATFORM_INTERNAL_H__
#include "platform.h"
far void vectors();
int tick_init();
void irq_lowlevel_init();
void spi_lowlevel_init();
void spi_lowlevel_init();
#endif

50
src/spi.c

@ -1,7 +1,8 @@
#include <string.h>
#include "csl/csl.h"
#include "csl/csl_mcbsp.h"
#include "platform_internal.h"
#include "spi.h"
typedef struct __spi_handle *spi_t;
struct __spi_handle {
MCBSP_Handle spi;
int bits;
@ -9,14 +10,12 @@ struct __spi_handle {
static struct __spi_handle __spi_devs[_MCBSP_PORT_CNT];
unsigned long platform_cpu_freq();
void spi_lowlevel_init()
{
memset(__spi_devs, 0, sizeof __spi_devs);
}
spi_t spi_open(int dev_id, int mode, int freq, int bits)
int spi_open(int dev_id, int mode, int freq, int bits)
{
MCBSP_Config mbconf;
MCBSP_Handle handle = 0;
@ -45,7 +44,7 @@ spi_t spi_open(int dev_id, int mode, int freq, int bits)
dlen1 = MCBSP_XCR_XWDLEN1_32BIT;
break;
default:
return NULL;
return -1;
}
switch (mode) {
@ -178,7 +177,7 @@ spi_t spi_open(int dev_id, int mode, int freq, int bits)
MCBSP_SRGR_DEFAULT_DELAY);
spi->spi = handle;
return spi;
return 0;
}
static int __write_u8(spi_t spi, const Uint8 *buf, int len)
@ -214,11 +213,30 @@ static int __write_u32(spi_t spi, const Uint32 *buf, int len)
return (len);
}
int spi_write(spi_t spi, const void *buf, int len)
static spi_t __get_active_device(int dev_id)
{
spi_t spi;
if (dev_id < 0 || dev_id >= ARRAY_SIZE(__spi_devs))
return NULL;
spi = &__spi_devs[dev_id];
return spi->spi ? spi : NULL;
}
int spi_write(int dev_id, const void *buf, int len)
{
const Uint8 *u8;
const Uint16 *u16;
const Uint32 *u32;
spi_t spi;
if ((spi = __get_active_device(dev_id)) == NULL) {
return -1;
}
spi = &__spi_devs[dev_id];
switch (spi->bits) {
case 8:
@ -275,11 +293,16 @@ static int __read_u32(spi_t spi, Uint32 *buf, int len)
return (len);
}
int spi_read(spi_t spi, void *buf, int len)
int spi_read(int dev_id, void *buf, int len)
{
Uint8 *u8;
Uint16 *u16;
Uint32 *u32;
spi_t spi;
if ((spi = __get_active_device(dev_id)) == NULL) {
return -1;
}
switch (spi->bits) {
case 8:
@ -303,9 +326,16 @@ int spi_read(spi_t spi, void *buf, int len)
return (len);
}
void spi_close(spi_t spi)
void spi_close(int dev_id)
{
spi_t spi;
if ((spi = __get_active_device(dev_id)) == NULL) {
return;
}
MCBSP_close(spi->spi);
spi->spi = NULL;
spi->bits = 0;
}

Loading…
Cancel
Save