bsps: Move libchip to bsps

This patch is a part of the BSP source reorganization.

Update #3285.
This commit is contained in:
Sebastian Huber
2018-04-04 10:13:28 +02:00
parent 8621ed3806
commit 27de4e1fb8
83 changed files with 55 additions and 91 deletions
+177
View File
@@ -0,0 +1,177 @@
/* Trivial i2c driver for reading "2-byte eeproms".
* On 'open' the read-pointer is reset to 0, subsequent
* read operations slurp data from there...
*/
/*
* Authorship
* ----------
* This software was created by
* Till Straumann <strauman@slac.stanford.edu>, 2005,
* Stanford Linear Accelerator Center, Stanford University.
*
* Acknowledgement of sponsorship
* ------------------------------
* This software was produced by
* the Stanford Linear Accelerator Center, Stanford University,
* under Contract DE-AC03-76SFO0515 with the Department of Energy.
*
* Government disclaimer of liability
* ----------------------------------
* Neither the United States nor the United States Department of Energy,
* nor any of their employees, makes any warranty, express or implied, or
* assumes any legal liability or responsibility for the accuracy,
* completeness, or usefulness of any data, apparatus, product, or process
* disclosed, or represents that its use would not infringe privately owned
* rights.
*
* Stanford disclaimer of liability
* --------------------------------
* Stanford University makes no representations or warranties, express or
* implied, nor assumes any liability for the use of this software.
*
* Stanford disclaimer of copyright
* --------------------------------
* Stanford University, owner of the copyright, hereby disclaims its
* copyright and all other rights in this software. Hence, anyone may
* freely use it for any purpose without restriction.
*
* Maintenance of notices
* ----------------------
* In the interest of clarity regarding the origin and status of this
* SLAC software, this and all the preceding Stanford University notices
* are to remain affixed to any copy or derivative of this software made
* or distributed by the recipient and are to be affixed to any copy of
* software made or distributed by the recipient that contains a copy or
* derivative of this software.
*
* ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03
*/
#include <rtems.h>
#include <rtems/libi2c.h>
#include <libchip/i2c-2b-eeprom.h>
#include <rtems/libio.h>
#define EEPROM_PG_SZ 32
#define ALGN(x) (((uint32_t)(x) + EEPROM_PG_SZ) & ~(EEPROM_PG_SZ-1))
static rtems_status_code
send_file_ptr (rtems_device_minor_number minor, unsigned pos, int tout)
{
int sc;
unsigned char bytes[2];
bytes[0] = (pos >> 8) & 0xff;
bytes[1] = (pos) & 0xff;
/* poll addressing the next page; if 'tout' is <=0 we only try once
* and return the status. If 'tout' is positive, we try 'tout' times
* and return RTEMS_TIMEOUT if it didnt work
*/
while ((sc = rtems_libi2c_start_write_bytes (minor, bytes, 2)) < 0) {
if (--tout <= 0)
return tout ? -sc : RTEMS_TIMEOUT;
rtems_task_wake_after (1);
}
return RTEMS_SUCCESSFUL;
}
static rtems_status_code
i2c_2b_eeprom_write (rtems_device_major_number major,
rtems_device_minor_number minor, void *arg)
{
rtems_libio_rw_args_t *rwargs = arg;
unsigned off = rwargs->offset;
int cnt = rwargs->count;
unsigned char *buf = (unsigned char *)rwargs->buffer;
int sc;
unsigned end;
int l;
if (cnt <= 0)
return RTEMS_SUCCESSFUL;
if ((sc = send_file_ptr (minor, off, 0)))
return sc;
do {
/* write up to next page boundary */
end = ALGN (off);
l = end - off;
if (l > cnt)
l = cnt;
sc = rtems_libi2c_write_bytes (minor, buf, l);
if (sc < 0)
return -sc;
sc = rtems_libi2c_send_stop (minor);
if (sc)
return sc;
rwargs->bytes_moved += l;
buf += l;
cnt -= l;
off += l;
/* poll addressing the next page */
if ((sc = send_file_ptr (minor, off, 100)))
return sc;
} while (cnt > 0);
return rtems_libi2c_send_stop (minor);
}
static rtems_status_code
i2c_2b_eeprom_read (rtems_device_major_number major,
rtems_device_minor_number minor, void *arg)
{
int sc;
rtems_libio_rw_args_t *rwargs = arg;
if (RTEMS_SUCCESSFUL != (sc = send_file_ptr (minor, rwargs->offset, 0)))
return -sc;
sc = rtems_libi2c_start_read_bytes(
minor,
(unsigned char *)rwargs->buffer,
rwargs->count
);
if (sc < 0) {
rwargs->bytes_moved = 0;
return -sc;
}
rwargs->bytes_moved = sc;
return rtems_libi2c_send_stop (minor);
}
static rtems_driver_address_table myops = {
.read_entry = i2c_2b_eeprom_read,
.write_entry = i2c_2b_eeprom_write,
};
static rtems_libi2c_drv_t my_drv_tbl = {
.ops = &myops,
.size = sizeof (my_drv_tbl),
};
/* provide a second table for R/O access */
static rtems_driver_address_table my_ro_ops = {
.read_entry = i2c_2b_eeprom_read,
};
static rtems_libi2c_drv_t my_ro_drv_tbl = {
.ops = &my_ro_ops,
.size = sizeof (my_ro_drv_tbl),
};
rtems_libi2c_drv_t *i2c_2b_eeprom_driver_descriptor = &my_drv_tbl;
rtems_libi2c_drv_t *i2c_2b_eeprom_ro_driver_descriptor = &my_ro_drv_tbl;
+128
View File
@@ -0,0 +1,128 @@
/* Trivial i2c driver for the maxim DS1621 temperature sensor;
* just implements reading constant conversions with 8-bit
* resolution.
* Demonstrates the implementation of a i2c high-level driver.
*/
/*
* Authorship
* ----------
* This software was created by
* Till Straumann <strauman@slac.stanford.edu>, 2005,
* Stanford Linear Accelerator Center, Stanford University.
*
* Acknowledgement of sponsorship
* ------------------------------
* This software was produced by
* the Stanford Linear Accelerator Center, Stanford University,
* under Contract DE-AC03-76SFO0515 with the Department of Energy.
*
* Government disclaimer of liability
* ----------------------------------
* Neither the United States nor the United States Department of Energy,
* nor any of their employees, makes any warranty, express or implied, or
* assumes any legal liability or responsibility for the accuracy,
* completeness, or usefulness of any data, apparatus, product, or process
* disclosed, or represents that its use would not infringe privately owned
* rights.
*
* Stanford disclaimer of liability
* --------------------------------
* Stanford University makes no representations or warranties, express or
* implied, nor assumes any liability for the use of this software.
*
* Stanford disclaimer of copyright
* --------------------------------
* Stanford University, owner of the copyright, hereby disclaims its
* copyright and all other rights in this software. Hence, anyone may
* freely use it for any purpose without restriction.
*
* Maintenance of notices
* ----------------------
* In the interest of clarity regarding the origin and status of this
* SLAC software, this and all the preceding Stanford University notices
* are to remain affixed to any copy or derivative of this software made
* or distributed by the recipient and are to be affixed to any copy of
* software made or distributed by the recipient that contains a copy or
* derivative of this software.
*
* ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03
*/
#include <rtems.h>
#include <rtems/libi2c.h>
#include <libchip/i2c-ds1621.h>
#include <rtems/libio.h>
static rtems_status_code
ds1621_init (rtems_device_major_number major, rtems_device_minor_number minor,
void *arg)
{
int sc;
unsigned char csr[2] = { DS1621_CMD_CSR_ACCESS, 0 }, cmd;
/* First start command acquires a lock for the bus */
/* Initialize; switch continuous conversion on */
sc = rtems_libi2c_start_write_bytes (minor, csr, 1);
if (sc < 0)
return -sc;
sc = rtems_libi2c_start_read_bytes (minor, csr + 1, 1);
if (sc < 0)
return -sc;
csr[1] &= ~DS1621_CSR_1SHOT;
sc = rtems_libi2c_start_write_bytes (minor, csr, 2);
if (sc < 0)
return -sc;
/* Start conversion */
cmd = DS1621_CMD_START_CONV;
sc = rtems_libi2c_start_write_bytes (minor, &cmd, 1);
if (sc < 0)
return -sc;
/* sending 'stop' relinquishes the bus mutex -- don't hold it
* across system calls!
*/
return rtems_libi2c_send_stop (minor);
}
static rtems_status_code
ds1621_read (rtems_device_major_number major, rtems_device_minor_number minor,
void *arg)
{
int sc;
rtems_libio_rw_args_t *rwargs = arg;
unsigned char cmd = DS1621_CMD_READ_TEMP;
sc = rtems_libi2c_start_write_bytes (minor, &cmd, 1);
if (sc < 0)
return -sc;
if (sc < 1)
return RTEMS_IO_ERROR;
sc = rtems_libi2c_start_read_bytes(minor, (unsigned char *)rwargs->buffer, 1);
if (sc < 0) {
rwargs->bytes_moved = 0;
return -sc;
}
rwargs->bytes_moved = 1;
return rtems_libi2c_send_stop (minor);
}
static rtems_driver_address_table myops = {
.initialization_entry = ds1621_init,
.read_entry = ds1621_read,
};
static rtems_libi2c_drv_t my_drv_tbl = {
.ops = &myops,
.size = sizeof (my_drv_tbl),
};
rtems_libi2c_drv_t *i2c_ds1621_driver_descriptor = &my_drv_tbl;
+91
View File
@@ -0,0 +1,91 @@
/**
* @file
*
* @brief I2C Driver for SEMTECH SC620 Octal LED Driver
*/
/*
* Copyright (c) 2013 embedded brains GmbH. All rights reserved.
*
* embedded brains GmbH
* Obere Lagerstr. 30
* 82178 Puchheim
* Germany
* <rtems@embedded-brains.de>
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rtems.org/license/LICENSE.
*/
#include <libchip/i2c-sc620.h>
#include <rtems/libio.h>
#define SC620_REG_COUNT 10
static rtems_status_code i2c_sc620_write(
rtems_device_major_number major,
rtems_device_minor_number minor,
void *arg
)
{
rtems_status_code sc = RTEMS_IO_ERROR;
rtems_libio_rw_args_t *rw = arg;
unsigned char *buf = (unsigned char *) &rw->buffer[0];
if (rw->count == 2 && buf[0] < SC620_REG_COUNT) {
int rv;
rv = rtems_libi2c_start_write_bytes(
minor, buf, 2
);
if (rv == 2) {
sc = rtems_libi2c_send_stop(minor);
}
}
rw->bytes_moved = sc == RTEMS_SUCCESSFUL ? 2 : 0;
return sc;
}
static rtems_status_code i2c_sc620_read(
rtems_device_major_number major,
rtems_device_minor_number minor,
void *arg
)
{
rtems_status_code sc = RTEMS_IO_ERROR;
rtems_libio_rw_args_t *rw = arg;
unsigned char *buf = (unsigned char *) &rw->buffer[0];
if (rw->count == 1 && buf[0] < SC620_REG_COUNT) {
int rv;
rv = rtems_libi2c_start_write_bytes(minor, buf, 1);
if (rv == 1) {
sc = rtems_libi2c_send_addr(minor, 0);
if (sc == RTEMS_SUCCESSFUL) {
rv = rtems_libi2c_read_bytes(minor, buf, 1);
if (rv == 1) {
sc = rtems_libi2c_send_stop(minor);
}
}
}
}
rw->bytes_moved = sc == RTEMS_SUCCESSFUL ? 1 : 0;
return sc;
}
static rtems_driver_address_table i2c_sc620_ops = {
.read_entry = i2c_sc620_read,
.write_entry = i2c_sc620_write
};
rtems_libi2c_drv_t i2c_sc620_driver = {
.ops = &i2c_sc620_ops,
.size = sizeof(i2c_sc620_driver)
};
+60
View File
@@ -0,0 +1,60 @@
/*===============================================================*\
| Project: SPI driver for M25P40 like spi flash device |
+-----------------------------------------------------------------+
| Copyright (c) 2007 |
| Embedded Brains GmbH |
| Obere Lagerstr. 30 |
| D-82178 Puchheim |
| Germany |
| rtems@embedded-brains.de |
+-----------------------------------------------------------------+
| The license and distribution terms for this file may be |
| found in the file LICENSE in this distribution or at |
| |
| http://www.rtems.org/license/LICENSE. |
| |
+-----------------------------------------------------------------+
\*===============================================================*/
#include <rtems.h>
#include <rtems/libi2c.h>
#include <libchip/spi-flash-m25p40.h>
#include <rtems/libio.h>
static spi_memdrv_t spi_flash_m25p40_rw_drv_t = {
{/* public fields */
.ops = &spi_memdrv_rw_ops, /* operations of general memdrv */
.size = sizeof (spi_flash_m25p40_rw_drv_t),
},
{ /* our private fields */
.baudrate = 2000000,
.erase_before_program = true,
.empty_state = 0xff,
.page_size = 256, /* programming page size in bytes */
.sector_size = 0x10000, /* 64K - erase sector size in bytes */
.mem_size = 0x80000, /* 512K - total capacity in bytes */
}
};
rtems_libi2c_drv_t *spi_flash_m25p40_rw_driver_descriptor =
&spi_flash_m25p40_rw_drv_t.libi2c_drv_entry;
static spi_memdrv_t spi_flash_m25p40_ro_drv_t = {
{/* public fields */
.ops = &spi_memdrv_ro_ops, /* operations of general memdrv */
.size = sizeof (spi_flash_m25p40_ro_drv_t),
},
{ /* our private fields */
.baudrate = 2000000,
.erase_before_program = true,
.empty_state = 0xff,
.page_size = 256, /* programming page size in bytes */
.sector_size = 0x10000, /* 64K erase sector size in bytes */
.mem_size = 0x80000, /* 512K total capacity in bytes */
}
};
rtems_libi2c_drv_t *spi_flash_m25p40_ro_driver_descriptor =
&spi_flash_m25p40_ro_drv_t.libi2c_drv_entry;
+60
View File
@@ -0,0 +1,60 @@
/*===============================================================*\
| Project: SPI driver for FM25L256 like spi fram device |
+-----------------------------------------------------------------+
| Copyright (c) 2008 |
| Embedded Brains GmbH |
| Obere Lagerstr. 30 |
| D-82178 Puchheim |
| Germany |
| rtems@embedded-brains.de |
+-----------------------------------------------------------------+
| The license and distribution terms for this file may be |
| found in the file LICENSE in this distribution or at |
| |
| http://www.rtems.org/license/LICENSE. |
| |
+-----------------------------------------------------------------+
\*===============================================================*/
#include <rtems.h>
#include <rtems/libi2c.h>
#include <libchip/spi-fram-fm25l256.h>
#include <rtems/libio.h>
static spi_memdrv_t spi_fram_fm25l256_rw_drv_t = {
{/* public fields */
.ops = &spi_memdrv_rw_ops, /* operations of general memdrv */
.size = sizeof (spi_fram_fm25l256_rw_drv_t),
},
{ /* our private fields */
.baudrate = 2000000,
.erase_before_program = false,
.empty_state = 0xff,
.page_size = 0x8000, /* 32K programming page size in bytes */
.sector_size = 1, /* erase sector size in bytes */
.mem_size = 0x8000, /* 32K total capacity in bytes */
}
};
rtems_libi2c_drv_t *spi_fram_fm25l256_rw_driver_descriptor =
&spi_fram_fm25l256_rw_drv_t.libi2c_drv_entry;
static spi_memdrv_t spi_fram_fm25l256_ro_drv_t = {
{/* public fields */
.ops = &spi_memdrv_ro_ops, /* operations of general memdrv */
.size = sizeof (spi_fram_fm25l256_ro_drv_t),
},
{ /* our private fields */
.baudrate = 2000000,
.erase_before_program = false,
.empty_state = 0xff,
.page_size = 0x8000, /* 32k programming page size in bytes */
.sector_size = 1, /* erase sector size in bytes */
.mem_size = 0x8000, /* 32k total capacity in bytes */
}
};
rtems_libi2c_drv_t *spi_fram_fm25l256_ro_driver_descriptor =
&spi_fram_fm25l256_ro_drv_t.libi2c_drv_entry;
+442
View File
@@ -0,0 +1,442 @@
/*===============================================================*\
| Project: SPI driver for spi memory devices |
+-----------------------------------------------------------------+
| Copyright (c) 2008 |
| Embedded Brains GmbH |
| Obere Lagerstr. 30 |
| D-82178 Puchheim |
| Germany |
| rtems@embedded-brains.de |
+-----------------------------------------------------------------+
| The license and distribution terms for this file may be |
| found in the file LICENSE in this distribution or at |
| |
| http://www.rtems.org/license/LICENSE. |
| |
+-----------------------------------------------------------------+
\*===============================================================*/
/*
* FIXME: currently, this driver only supports read/write accesses
* erase accesses are to be completed
*/
#include <rtems.h>
#include <rtems/libi2c.h>
#include <libchip/spi-memdrv.h>
#include <rtems/libio.h>
#define SPI_MEM_CMD_WREN 0x06
#define SPI_MEM_CMD_WRDIS 0x04
#define SPI_MEM_CMD_RDID 0x9F
#define SPI_MEM_CMD_RDSR 0x05
#define SPI_MEM_CMD_WRSR 0x01
#define SPI_MEM_CMD_READ 0x03
#define SPI_MEM_CMD_PP 0x02 /* page program */
#define SPI_MEM_CMD_SE 0xD8 /* sector erase */
#define SPI_MEM_CMD_BE 0xC7 /* bulk erase */
#define SPI_MEM_CMD_DP 0xB9 /* deep power down */
#define SPI_MEM_CMD_RES 0xAB /* release from deep power down */
/*=========================================================================*\
| Function: |
\*-------------------------------------------------------------------------*/
static rtems_status_code spi_memdrv_minor2param_ptr
(
/*-------------------------------------------------------------------------*\
| Purpose: |
| translate given minor device number to param pointer |
+---------------------------------------------------------------------------+
| Input Parameters: |
\*-------------------------------------------------------------------------*/
rtems_device_minor_number minor, /* minor number of device */
spi_memdrv_param_t **param_ptr /* ptr to param ptr */
)
/*-------------------------------------------------------------------------*\
| Return Value: |
| o = ok or error code |
\*=========================================================================*/
{
rtems_status_code rc = RTEMS_SUCCESSFUL;
spi_memdrv_t *drv_ptr;
if (rc == RTEMS_SUCCESSFUL) {
rc = -rtems_libi2c_ioctl(minor,
RTEMS_LIBI2C_IOCTL_GET_DRV_T,
&drv_ptr);
}
if ((rc == RTEMS_SUCCESSFUL) &&
(drv_ptr->libi2c_drv_entry.size != sizeof(spi_memdrv_t))) {
rc = RTEMS_INVALID_SIZE;
}
if (rc == RTEMS_SUCCESSFUL) {
*param_ptr = &(drv_ptr->spi_memdrv_param);
}
return rc;
}
/*=========================================================================*\
| Function: |
\*-------------------------------------------------------------------------*/
static rtems_status_code spi_memdrv_wait_ms
(
/*-------------------------------------------------------------------------*\
| Purpose: |
| wait a certain interval given in ms |
+---------------------------------------------------------------------------+
| Input Parameters: |
\*-------------------------------------------------------------------------*/
int ms /* time to wait in milliseconds */
)
/*-------------------------------------------------------------------------*\
| Return Value: |
| o = ok or error code |
\*=========================================================================*/
{
rtems_interval ticks_per_second;
ticks_per_second = rtems_clock_get_ticks_per_second();
(void) rtems_task_wake_after(ticks_per_second * ms / 1000);
return 0;
}
/*=========================================================================*\
| Function: |
\*-------------------------------------------------------------------------*/
rtems_status_code spi_memdrv_write
(
/*-------------------------------------------------------------------------*\
| Purpose: |
| write a block of data to flash |
+---------------------------------------------------------------------------+
| Input Parameters: |
\*-------------------------------------------------------------------------*/
rtems_device_major_number major, /* major device number */
rtems_device_minor_number minor, /* minor device number */
void *arg /* ptr to write argument struct */
)
/*-------------------------------------------------------------------------*\
| Return Value: |
| o = ok or error code |
\*=========================================================================*/
{
rtems_status_code rc = RTEMS_SUCCESSFUL;
rtems_libio_rw_args_t *rwargs = arg;
off_t off = rwargs->offset;
int cnt = rwargs->count;
unsigned char *buf = (unsigned char *)rwargs->buffer;
int bytes_sent = 0;
int curr_cnt;
unsigned char cmdbuf[4];
int ret_cnt = 0;
int cmd_size;
spi_memdrv_param_t *mem_param_ptr;
rtems_libi2c_tfr_mode_t tfr_mode = {
.baudrate = 20000000, /* maximum bits per second */
.bits_per_char = 8, /* how many bits per byte/word/longword? */
.lsb_first = FALSE, /* FALSE: send MSB first */
.clock_inv = FALSE, /* FALSE: non-inverted clock (high active) */
.clock_phs = FALSE /* FALSE: clock starts in middle of data tfr */
} ;
/*
* get mem parameters
*/
if (rc == RTEMS_SUCCESSFUL) {
rc = spi_memdrv_minor2param_ptr(minor,&mem_param_ptr);
}
/*
* check arguments
*/
if (rc == RTEMS_SUCCESSFUL) {
if ((cnt <= 0) ||
(cnt > mem_param_ptr->mem_size) ||
(off > (mem_param_ptr->mem_size-cnt))) {
rc = RTEMS_INVALID_SIZE;
}
else if (buf == NULL) {
rc = RTEMS_INVALID_ADDRESS;
}
}
while ((rc == RTEMS_SUCCESSFUL) &&
(cnt > bytes_sent)) {
curr_cnt = cnt - bytes_sent;
if ((mem_param_ptr->page_size > 0) &&
(off / mem_param_ptr->page_size) !=
((off+curr_cnt+1) / mem_param_ptr->page_size)) {
curr_cnt = mem_param_ptr->page_size - (off % mem_param_ptr->page_size);
}
/*
* select device, set transfer mode, address device
*/
if (rc == RTEMS_SUCCESSFUL) {
rc = rtems_libi2c_send_start(minor);
}
/*
* set transfer mode
*/
if (rc == RTEMS_SUCCESSFUL) {
tfr_mode.baudrate = mem_param_ptr->baudrate;
rc = -rtems_libi2c_ioctl(minor,
RTEMS_LIBI2C_IOCTL_SET_TFRMODE,
&tfr_mode);
}
/*
* address device
*/
if (rc == RTEMS_SUCCESSFUL) {
rc = rtems_libi2c_send_addr(minor,TRUE);
}
/*
* send write_enable command
*/
if (rc == RTEMS_SUCCESSFUL) {
cmdbuf[0] = SPI_MEM_CMD_WREN;
ret_cnt = rtems_libi2c_write_bytes(minor,cmdbuf,1);
if (ret_cnt < 0) {
rc = -ret_cnt;
}
}
/*
* terminate transfer
*/
if (rc == RTEMS_SUCCESSFUL) {
rc = rtems_libi2c_send_stop(minor);
}
/*
* select device, set transfer mode
*/
if (rc == RTEMS_SUCCESSFUL) {
rc = rtems_libi2c_send_start(minor);
}
/*
* address device
*/
if (rc == RTEMS_SUCCESSFUL) {
rc = rtems_libi2c_send_addr(minor,TRUE);
}
/*
* set transfer mode
*/
if (rc == RTEMS_SUCCESSFUL) {
rc = -rtems_libi2c_ioctl(minor,
RTEMS_LIBI2C_IOCTL_SET_TFRMODE,
&tfr_mode);
}
/*
* send "page program" command and address
*/
if (rc == RTEMS_SUCCESSFUL) {
cmdbuf[0] = SPI_MEM_CMD_PP;
if (mem_param_ptr->mem_size > 0x10000 /* 256*256 */) {
cmdbuf[1] = (off >> 16) & 0xff;
cmdbuf[2] = (off >> 8) & 0xff;
cmdbuf[3] = (off >> 0) & 0xff;
cmd_size = 4;
}
else if (mem_param_ptr->mem_size > 256) {
cmdbuf[1] = (off >> 8) & 0xff;
cmdbuf[2] = (off >> 0) & 0xff;
cmd_size = 3;
}
else {
cmdbuf[1] = (off >> 0) & 0xff;
cmd_size = 1;
}
ret_cnt = rtems_libi2c_write_bytes(minor,cmdbuf,cmd_size);
if (ret_cnt < 0) {
rc = -ret_cnt;
}
}
/*
* send write data
*/
if (rc == RTEMS_SUCCESSFUL) {
ret_cnt = rtems_libi2c_write_bytes(minor,buf,curr_cnt);
if (ret_cnt < 0) {
rc = -ret_cnt;
}
}
/*
* terminate transfer
*/
if (rc == RTEMS_SUCCESSFUL) {
rc = rtems_libi2c_send_stop(minor);
}
/*
* wait proper time for data to store: 5ms
* FIXME: select proper interval or poll, until device is finished
*/
if (rc == RTEMS_SUCCESSFUL) {
rc = spi_memdrv_wait_ms(5);
}
/*
* adjust bytecount to be sent and pointers
*/
bytes_sent += curr_cnt;
off += curr_cnt;
buf += curr_cnt;
}
rwargs->bytes_moved = bytes_sent;
return rc;
}
/*=========================================================================*\
| Function: |
\*-------------------------------------------------------------------------*/
rtems_status_code spi_memdrv_read
(
/*-------------------------------------------------------------------------*\
| Purpose: |
| read a block of data from flash |
+---------------------------------------------------------------------------+
| Input Parameters: |
\*-------------------------------------------------------------------------*/
rtems_device_major_number major, /* major device number */
rtems_device_minor_number minor, /* minor device number */
void *arg /* ptr to read argument struct */
)
/*-------------------------------------------------------------------------*\
| Return Value: |
| o = ok or error code |
\*=========================================================================*/
{
rtems_status_code rc = RTEMS_SUCCESSFUL;
rtems_libio_rw_args_t *rwargs = arg;
off_t off = rwargs->offset;
int cnt = rwargs->count;
unsigned char *buf = (unsigned char *)rwargs->buffer;
unsigned char cmdbuf[4];
int ret_cnt = 0;
int cmd_size;
spi_memdrv_param_t *mem_param_ptr;
rtems_libi2c_tfr_mode_t tfr_mode = {
.baudrate = 20000000, /* maximum bits per second */
.bits_per_char = 8, /* how many bits per byte/word/longword? */
.lsb_first = FALSE, /* FALSE: send MSB first */
.clock_inv = FALSE, /* FALSE: non-inverted clock (high active) */
.clock_phs = FALSE /* FALSE: clock starts in middle of data tfr */
};
/*
* get mem parameters
*/
if (rc == RTEMS_SUCCESSFUL) {
rc = spi_memdrv_minor2param_ptr(minor,&mem_param_ptr);
}
/*
* check arguments
*/
if (rc == RTEMS_SUCCESSFUL) {
if ((cnt <= 0) ||
(cnt > mem_param_ptr->mem_size) ||
(off > (mem_param_ptr->mem_size-cnt))) {
rc = RTEMS_INVALID_SIZE;
}
else if (buf == NULL) {
rc = RTEMS_INVALID_ADDRESS;
}
}
/*
* select device, set transfer mode, address device
*/
if (rc == RTEMS_SUCCESSFUL) {
rc = rtems_libi2c_send_start(minor);
}
/*
* set transfer mode
*/
if (rc == RTEMS_SUCCESSFUL) {
tfr_mode.baudrate = mem_param_ptr->baudrate;
rc = -rtems_libi2c_ioctl(minor,
RTEMS_LIBI2C_IOCTL_SET_TFRMODE,
&tfr_mode);
}
/*
* address device
*/
if (rc == RTEMS_SUCCESSFUL) {
rc = rtems_libi2c_send_addr(minor,TRUE);
}
if (off >= mem_param_ptr->mem_size) {
/*
* HACK: beyond size of memory array? then read status register instead
*/
/*
* send read status register command
*/
if (rc == RTEMS_SUCCESSFUL) {
cmdbuf[0] = SPI_MEM_CMD_RDSR;
ret_cnt = rtems_libi2c_write_bytes(minor,cmdbuf,1);
if (ret_cnt < 0) {
rc = -ret_cnt;
}
}
}
else {
/*
* send read command and address
*/
if (rc == RTEMS_SUCCESSFUL) {
cmdbuf[0] = SPI_MEM_CMD_READ;
if (mem_param_ptr->mem_size > 0x10000 /* 256*256 */) {
cmdbuf[1] = (off >> 16) & 0xff;
cmdbuf[2] = (off >> 8) & 0xff;
cmdbuf[3] = (off >> 0) & 0xff;
cmd_size = 4;
}
else if (mem_param_ptr->mem_size > 256) {
cmdbuf[1] = (off >> 8) & 0xff;
cmdbuf[2] = (off >> 0) & 0xff;
cmd_size = 3;
}
else {
cmdbuf[1] = (off >> 0) & 0xff;
cmd_size = 1;
}
ret_cnt = rtems_libi2c_write_bytes(minor,cmdbuf,cmd_size);
if (ret_cnt < 0) {
rc = -ret_cnt;
}
}
}
/*
* fetch read data
*/
if (rc == RTEMS_SUCCESSFUL) {
ret_cnt = rtems_libi2c_read_bytes (minor,buf,cnt);
if (ret_cnt < 0) {
rc = -ret_cnt;
}
}
/*
* terminate transfer
*/
if (rc == RTEMS_SUCCESSFUL) {
rc = rtems_libi2c_send_stop(minor);
}
rwargs->bytes_moved = (rc == RTEMS_SUCCESSFUL) ? ret_cnt : 0;
return rc;
}
/*
* driver operation tables
*/
rtems_driver_address_table spi_memdrv_rw_ops = {
.read_entry = spi_memdrv_read,
.write_entry = spi_memdrv_write
};
rtems_driver_address_table spi_memdrv_ro_ops = {
.read_entry = spi_memdrv_read,
};
File diff suppressed because it is too large Load Diff