Browse Source

Merge pull request #5155 from OlegHahm/diskio_cleanup

doc diskio: RIOTized style and added doxygen
pr/gpio
Peter Kietzmann 7 years ago
parent
commit
3fa8a267b3
  1. 8
      cpu/lpc2387/asmfunc.s
  2. 92
      cpu/lpc2387/mci/lpc2387-mci.c
  3. 195
      drivers/include/diskio.h
  4. 2
      sys/auto_init/auto_init.c
  5. 12
      sys/shell/commands/sc_disk.c

8
cpu/lpc2387/asmfunc.s

@ -10,9 +10,9 @@
@ Fast Block Copy (declared in diskio.h)
@-----------------------------------------------------------@
.global Copy_un2al
.global copy_un2al
.arm
Copy_un2al:
copy_un2al:
STMFD SP!, {R4-R8}
ANDS IP, R1, #3
BEQ lb_align
@ -46,9 +46,9 @@ lb_align:
BX LR
.global Copy_al2un
.global copy_al2un
.arm
Copy_al2un:
copy_al2un:
STMFD SP!, {R4-R8}
ANDS IP, R0, #3
BEQ sb_align

92
cpu/lpc2387/mci/lpc2387-mci.c

@ -80,7 +80,7 @@ extern unsigned long xtimer_now(void);
---------------------------------------------------------------------------*/
static volatile DSTATUS Stat = STA_NOINIT; /* Disk status */
static volatile diskio_sta_t Stat = DISKIO_STA_NOINIT; /* Disk status */
static unsigned short CardRCA; /* Assigned RCA */
static unsigned char CardType, /* Card type flag */
@ -363,7 +363,7 @@ static void power_off(void)
PINSEL4 &= ~((BIT22 | BIT23) | (BIT24 | BIT25) | (BIT26 | BIT27));
// Pins should be now configured as standard input (see board_init.c if you accidentally reconfigured them)
Stat |= STA_NOINIT;
Stat |= DISKIO_STA_NOINIT;
}
@ -523,13 +523,13 @@ static void bswap_cp(unsigned char *dst, const unsigned long *src)
/*-----------------------------------------------------------------------*/
/* Initialize Disk Drive */
/*-----------------------------------------------------------------------*/
DSTATUS MCI_initialize(void)
diskio_sta_t mci_initialize(void)
{
unsigned int cmd, n;
unsigned long resp[4];
unsigned char ty;
if (Stat & STA_NODISK) {
if (Stat & DISKIO_STA_NODISK) {
return Stat; /* No card in the socket */
}
@ -666,12 +666,12 @@ DSTATUS MCI_initialize(void)
MCI_CLOCK = (MCI_CLOCK & 0xF00) | 0x200 | (PCLK / MCLK_RW / 2 - 1); /* Set MCICLK = MCLK_RW, power-save mode */
Stat &= ~STA_NOINIT; /* Clear STA_NOINIT */
Stat &= ~DISKIO_STA_NOINIT; /* Clear DISKIO_STA_NOINIT */
return Stat;
di_fail:
power_off();
Stat |= STA_NOINIT; /* Set STA_NOINIT */
Stat |= DISKIO_STA_NOINIT; /* Set DISKIO_STA_NOINIT */
return Stat;
}
@ -682,7 +682,7 @@ di_fail:
/* Get Disk Status */
/*-----------------------------------------------------------------------*/
DSTATUS MCI_status(void)
diskio_sta_t mci_status(void)
{
return Stat;
}
@ -699,17 +699,17 @@ DSTATUS MCI_status(void)
* @param sector Start sector number (LBA)
* @param count Sector count (1..127)
*/
DRESULT MCI_read(unsigned char *buff, unsigned long sector, unsigned char count)
diskio_result_t mci_read(unsigned char *buff, unsigned long sector, unsigned char count)
{
unsigned long resp;
unsigned int cmd;
if (count < 1 || count > 127) {
return RES_PARERR; /* Check parameter */
return DISKIO_RES_PARERR; /* Check parameter */
}
if (Stat & STA_NOINIT) {
return RES_NOTRDY; /* Check drive status */
if (Stat & DISKIO_STA_NOINIT) {
return DISKIO_RES_NOTRDY; /* Check drive status */
}
if (!(CardType & CT_BLOCK)) {
@ -717,7 +717,7 @@ DRESULT MCI_read(unsigned char *buff, unsigned long sector, unsigned char count)
}
if (!wait_ready(500)) {
return RES_ERROR; /* Make sure that card is tran state */
return DISKIO_RES_ERROR; /* Make sure that card is tran state */
}
ready_reception(count, 512); /* Ready to receive data blocks */
@ -738,7 +738,7 @@ DRESULT MCI_read(unsigned char *buff, unsigned long sector, unsigned char count)
break; /* Abort if any error has occured */
}
Copy_al2un(buff, DmaBuff[rp], 512); /* Pop an block */
copy_al2un(buff, DmaBuff[rp], 512); /* Pop an block */
XferRp = rp = (rp + 1) % N_BUF; /* Next DMA buffer */
@ -756,7 +756,7 @@ DRESULT MCI_read(unsigned char *buff, unsigned long sector, unsigned char count)
stop_transfer(); /* Close data path */
return count ? RES_ERROR : RES_OK;
return count ? DISKIO_RES_ERROR : DISKIO_RES_OK;
}
@ -771,22 +771,22 @@ DRESULT MCI_read(unsigned char *buff, unsigned long sector, unsigned char count)
* @param sector Start sector number (LBA)
* @param count Sector count (1..127)
* */
DRESULT MCI_write(const unsigned char *buff, unsigned long sector, unsigned char count)
diskio_result_t mci_write(const unsigned char *buff, unsigned long sector, unsigned char count)
{
unsigned long rc;
unsigned int cmd;
unsigned char wp, xc;
if (count < 1 || count > 127) {
return RES_PARERR; /* Check parameter */
return DISKIO_RES_PARERR; /* Check parameter */
}
if (Stat & STA_NOINIT) {
return RES_NOTRDY; /* Check drive status */
if (Stat & DISKIO_STA_NOINIT) {
return DISKIO_RES_NOTRDY; /* Check drive status */
}
if (Stat & STA_PROTECT) {
return RES_WRPRT; /* Check write protection */
if (Stat & DISKIO_STA_PROTECT) {
return DISKIO_RES_WRPRT; /* Check write protection */
}
if (!(CardType & CT_BLOCK)) {
@ -794,7 +794,7 @@ DRESULT MCI_write(const unsigned char *buff, unsigned long sector, unsigned char
}
if (!wait_ready(500)) {
return RES_ERROR; /* Make sure that card is tran state */
return DISKIO_RES_ERROR; /* Make sure that card is tran state */
}
if (count == 1) { /* Single block write */
@ -805,7 +805,7 @@ DRESULT MCI_write(const unsigned char *buff, unsigned long sector, unsigned char
if (!send_cmd(cmd, count, 1, &rc) /* Preset number of blocks to write */
|| (rc & 0xC0580000)) {
return RES_ERROR;
return DISKIO_RES_ERROR;
}
cmd = CMD25;
@ -813,14 +813,14 @@ DRESULT MCI_write(const unsigned char *buff, unsigned long sector, unsigned char
if (!send_cmd(cmd, sector, 1, &rc) /* Send a write command */
|| (rc & 0xC0580000)) {
return RES_ERROR;
return DISKIO_RES_ERROR;
}
wp = 0;
xc = count;
do { /* Fill block FIFO */
Copy_un2al(DmaBuff[wp], (unsigned char *)(unsigned int)buff, 512); /* Push a block */
copy_un2al(DmaBuff[wp], (unsigned char *)(unsigned int)buff, 512); /* Push a block */
wp++; /* Next DMA buffer */
count--;
buff += 512; /* Next user buffer address */
@ -839,7 +839,7 @@ DRESULT MCI_write(const unsigned char *buff, unsigned long sector, unsigned char
break; /* Abort if block underrun or any MCI error has occured */
}
Copy_un2al(DmaBuff[wp], (unsigned char *)(unsigned int)buff, 512); /* Push a block */
copy_un2al(DmaBuff[wp], (unsigned char *)(unsigned int)buff, 512); /* Push a block */
XferWp = wp = (wp + 1) % N_BUF; /* Next DMA buffer */
if (XferStat & 0xC) {
@ -862,7 +862,7 @@ DRESULT MCI_write(const unsigned char *buff, unsigned long sector, unsigned char
send_cmd(CMD12, 0, 1, &rc);
}
return count ? RES_ERROR : RES_OK;
return count ? DISKIO_RES_ERROR : DISKIO_RES_OK;
}
#endif /* _READONLY */
@ -873,26 +873,26 @@ DRESULT MCI_write(const unsigned char *buff, unsigned long sector, unsigned char
/* Miscellaneous Functions */
/*-----------------------------------------------------------------------*/
DRESULT MCI_ioctl(
diskio_result_t mci_ioctl(
unsigned char ctrl, /* Control code */
void *buff /* Buffer to send/receive data block */
)
{
DRESULT res;
diskio_result_t res;
unsigned char *ptr = (unsigned char *)buff;
unsigned long resp[4], d, *dp, st, ed;
if (Stat & STA_NOINIT) {
return RES_NOTRDY;
if (Stat & DISKIO_STA_NOINIT) {
return DISKIO_RES_NOTRDY;
}
res = RES_ERROR;
res = DISKIO_RES_ERROR;
switch(ctrl) {
case CTRL_SYNC : /* Make sure that all data has been written on the media */
if (wait_ready(500)) { /* Wait for card enters tarn state */
res = RES_OK;
res = DISKIO_RES_OK;
}
break;
@ -908,12 +908,12 @@ DRESULT MCI_ioctl(
*(unsigned long *)buff = d << (b - 9);
}
res = RES_OK;
res = DISKIO_RES_OK;
break;
case GET_SECTOR_SIZE : /* Get sectors on the disk (unsigned short) */
*(unsigned short *)buff = 512;
res = RES_OK;
res = DISKIO_RES_OK;
break;
case GET_BLOCK_SIZE : /* Get erase block size in unit of sectors (unsigned long) */
@ -929,7 +929,7 @@ DRESULT MCI_ioctl(
}
}
res = RES_OK;
res = DISKIO_RES_OK;
break;
case CTRL_ERASE_SECTOR : /* Erase a block of sectors */
@ -947,7 +947,7 @@ DRESULT MCI_ioctl(
}
if (send_cmd(CMD32, st, 1, resp) && send_cmd(CMD33, ed, 1, resp) && send_cmd(CMD38, 0, 1, resp) && wait_ready(30000)) {
res = RES_OK;
res = DISKIO_RES_OK;
}
break;
@ -956,38 +956,38 @@ DRESULT MCI_ioctl(
switch(ptr[0]) {
case 0: /* Sub control code == 0 (POWER_OFF) */
power_off(); /* Power off */
res = RES_OK;
res = DISKIO_RES_OK;
break;
case 1: /* Sub control code == 1 (POWER_GET) */
ptr[1] = (unsigned char)power_status();
res = RES_OK;
res = DISKIO_RES_OK;
break;
default :
res = RES_PARERR;
res = DISKIO_RES_PARERR;
}
break;
case MMC_GET_TYPE : /* Get card type flags (1 byte) */
*ptr = CardType;
res = RES_OK;
res = DISKIO_RES_OK;
break;
case MMC_GET_CSD : /* Get CSD (16 bytes) */
memcpy(buff, &CardInfo[0], 16);
res = RES_OK;
res = DISKIO_RES_OK;
break;
case MMC_GET_CID : /* Get CID (16 bytes) */
memcpy(buff, &CardInfo[16], 16);
res = RES_OK;
res = DISKIO_RES_OK;
break;
case MMC_GET_OCR : /* Get OCR (4 bytes) */
memcpy(buff, &CardInfo[32], 4);
res = RES_OK;
res = DISKIO_RES_OK;
break;
case MMC_GET_SDSTAT : /* Receive SD status as a data block (64 bytes) */
@ -1000,8 +1000,8 @@ DRESULT MCI_ioctl(
while ((XferWp == 0) && !(XferStat & 0xC)) {}
if (!(XferStat & 0xC)) {
Copy_al2un((unsigned char *)buff, DmaBuff[0], 64);
res = RES_OK;
copy_al2un((unsigned char *)buff, DmaBuff[0], 64);
res = DISKIO_RES_OK;
}
}
}
@ -1012,7 +1012,7 @@ DRESULT MCI_ioctl(
break;
default:
res = RES_PARERR;
res = DISKIO_RES_PARERR;
}
return res;

195
drivers/include/diskio.h

@ -1,5 +1,6 @@
/*
* Copyright 2010 ChaN
* Copyright 2016 INRIA
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
@ -17,11 +18,11 @@
*
* @file
*
* @author unknown
* @author Oliver Hahm <oliver.hahm@inria.fr>
*/
#ifndef DEF_DISKIO
#define DEF_DISKIO
#ifndef DISKIO_H_
#define DISKIO_H_
#include <stdint.h>
@ -29,105 +30,139 @@
extern "C" {
#endif
#define DN_MCI 0 /* Physical drive number for MCI */
#define DN_NAND 1 /* Physical drive number for NAND flash */
/* These functions are defined in asmfunc.S */
/**
* @def MCI_PWRSAVE
* @ingroup conf
* @brief Powerdown mode to use between mci operations
* @brief Copy aligned to unaligned
*
* @param[out] dst Pointer to unaligned destination address
* @param[in] src Pointer to aligned source address
* @param[in] count Number of bytes to copy
*/
#ifndef MCI_PWRSAVE
#define MCI_PWRSAVE 0
#endif
/* These functions are defined in asmfunc.S */
void Copy_al2un(unsigned char *dst, const unsigned long *src, int count); /* Copy aligned to unaligned. */
void Copy_un2al(unsigned long *dst, const unsigned char *src, int count); /* Copy unaligned to aligned. */
void copy_al2un(unsigned char *dst, const unsigned long *src, int count);
/**
* @brief Copy unaligned to aligned
*
* @param[out] dst Pointer to unaligned destination address
* @param[in] src Pointer to aligned source address
* @param[in] count Number of bytes to copy
*/
void copy_un2al(unsigned long *dst, const unsigned char *src, int count);
/* Status of Disk Functions */
typedef unsigned char DSTATUS;
/* Results of Disk Functions */
/** Results of Disk Functions */
typedef enum {
RES_OK = 0, /* 0: Successful */
RES_ERROR, /* 1: R/W Error */
RES_WRPRT, /* 2: Write Protected */
RES_NOTRDY, /* 3: Not Ready */
RES_PARERR /* 4: Invalid Parameter */
} DRESULT;
/*---------------------------------------*/
/* Prototypes for disk control functions */
DSTATUS disk_initialize(unsigned char);
DSTATUS disk_status(unsigned char);
DRESULT disk_read(unsigned char, unsigned char *, unsigned long, unsigned char);
DRESULT disk_write(unsigned char, const unsigned char *, unsigned long, unsigned char);
DRESULT disk_ioctl(unsigned char, unsigned char, void *);
/* Disk Status Bits (DSTATUS) */
DISKIO_RES_OK = 0, /**< 0: Successful */
DISKIO_RES_ERROR, /**< 1: R/W Error */
DISKIO_RES_WRPRT, /**< 2: Write Protected */
DISKIO_RES_NOTRDY, /**< 3: Not Ready */
DISKIO_RES_PARERR /**< 4: Invalid Parameter */
} diskio_result_t;
#define STA_NOINIT 0x01 /* Drive not initialized */
#define STA_NODISK 0x02 /* No medium in the drive */
#define STA_PROTECT 0x04 /* Write protected */
/* Command code for disk_ioctrl fucntion */
/**
* @name Disk Status Bits
* @{
*/
typedef enum {
DISKIO_STA_NOINIT = 0x01, /**< Drive not initialized */
DISKIO_STA_NODISK = 0x02, /**< No medium in the drive */
DISKIO_STA_PROTECT = 0x04 /**< Write protected */
} diskio_sta_t;
/** @} */
/* Generic ioctl command (defined for FatFs) */
#define CTRL_SYNC 0 /* Flush disk cache (for write functions) */
#define GET_SECTOR_COUNT 1 /* Get media size (for only f_mkfs()) */
#define GET_SECTOR_SIZE 2 /* Get sector size (for multiple sector size (_MAX_SS >= 1024)) */
#define GET_BLOCK_SIZE 3 /* Get erase block size (for only f_mkfs()) */
#define CTRL_ERASE_SECTOR 4 /* Force erased a block of sectors (for only _USE_ERASE) */
/**
* @name Command code for disk_ioctrl fucntion
* @{
*/
/* Generic ioctl command */
#define CTRL_POWER 5 /* Get/Set power status */
#define CTRL_LOCK 6 /* Lock/Unlock media removal */
#define CTRL_EJECT 7 /* Eject media */
/**
* @name Generic ioctl command (defined for FatFs)
* @{
*/
#define CTRL_SYNC 0 /**< Flush disk cache (for write functions) */
#define GET_SECTOR_COUNT 1 /**< Get media size (for only f_mkfs()) */
#define GET_SECTOR_SIZE 2 /**< Get sector size (for multiple sector size (_MAX_SS >= 1024)) */
#define GET_BLOCK_SIZE 3 /**< Get erase block size (for only f_mkfs()) */
#define CTRL_ERASE_SECTOR 4 /**< Force erased a block of sectors (for only _USE_ERASE) */
/** @} */
/* MMC/SDC specific ioctl command */
#define MMC_GET_TYPE 10 /* Get card type */
#define MMC_GET_CSD 11 /* Get CSD */
#define MMC_GET_CID 12 /* Get CID */
#define MMC_GET_OCR 13 /* Get OCR */
#define MMC_GET_SDSTAT 14 /* Get SD status */
/**
* @name Generic ioctl command
* @{
*/
#define CTRL_POWER 5 /**< Get/Set power status */
#define CTRL_LOCK 6 /**< Lock/Unlock media removal */
#define CTRL_EJECT 7 /**< Eject media */
/** @} */
/* ATA/CF specific ioctl command */
#define ATA_GET_REV 20 /* Get F/W revision */
#define ATA_GET_MODEL 21 /* Get model name */
#define ATA_GET_SN 22 /* Get serial number */
/**
* @name MMC/SDC specific ioctl command
* @{
*/
#define MMC_GET_TYPE 10 /**< Get card type */
#define MMC_GET_CSD 11 /**< Get CSD */
#define MMC_GET_CID 12 /**< Get CID */
#define MMC_GET_OCR 13 /**< Get OCR */
#define MMC_GET_SDSTAT 14 /**< Get SD status */
/** @} */
/* NAND specific ioctl command */
#define NAND_FORMAT 30 /* Create physical format */
/** @} */
/**
* @brief Initialize media control interface (MCI)
*
* @returns 0 on success
* @returns a @ref diskio_sta_t value on error
*/
diskio_sta_t mci_initialize(void);
/*---------------------------------------------*/
/* Prototypes for each physical disk functions */
/**
* @brief Get the status of the media control interface (MCI)
*
* @returns 0 on success
* @returns a @ref diskio_sta_t value on error
*/
diskio_sta_t mci_status(void);
/**
* @brief Read sectors over the media control interface (MCI)
*
* @param[out] buff Pointer to the data buffer to store read data
* @param[in] sector Start sector number (LBA)
* @param[in] count Sector count (1..127)
*
* @return @ref DISKIO_RES_OK on success
* @return any other @ref diskio_result_t value on error
*/
diskio_result_t mci_read(unsigned char *buff, unsigned long sector, unsigned char count);
DSTATUS NAND_initialize(void);
DSTATUS NAND_status(void);
DRESULT NAND_read(unsigned char *, unsigned long, unsigned char);
DRESULT NAND_write(const unsigned char *, unsigned long, unsigned char);
DRESULT NAND_ioctl(unsigned char, void *);
/**
* @brief Write sectors over the media control interface (MCI)
* @param[in] buff Pointer to the data to be written
* @param[in] sector Start sector number (LBA)
* @param[in] count Sector count (1..127)
*
* @return @ref DISKIO_RES_OK on success
* @return any other @ref diskio_result_t value on error
*/
diskio_result_t mci_write(const unsigned char *buff, unsigned long sector, unsigned char count);
DSTATUS MCI_initialize(void);
DSTATUS MCI_status(void);
DRESULT MCI_read(unsigned char *, unsigned long, unsigned char);
DRESULT MCI_write(const unsigned char *, unsigned long, unsigned char);
DRESULT MCI_ioctl(unsigned char, void *);
void MCI_timerproc(void);
/**
* @brief IOCTL functions for the media control interface (MCI)
*
* @param[in] ctrl Control code
* @param[in,out] buff Buffer to send/receive data block
*
* @return @ref DISKIO_RES_OK on success
* @return any other @ref diskio_result_t value on error
*/
diskio_result_t mci_ioctl(unsigned char ctrl, void *buff);
#ifdef __cplusplus
}
#endif
/** @} */
#endif
#endif /* DISKIO_H_ */

2
sys/auto_init/auto_init.c

@ -119,7 +119,7 @@ void auto_init(void)
#endif
#ifdef MODULE_MCI
DEBUG("Auto init mci module.\n");
MCI_initialize();
mci_initialize();
#endif
#ifdef MODULE_PROFILING
extern void profiling_init(void);

12
sys/shell/commands/sc_disk.c

@ -27,7 +27,7 @@
static inline uint8_t sector_read(unsigned char *read_buf, unsigned long sector, unsigned long length, unsigned long offset)
{
if (MCI_read(read_buf, sector, 1) == RES_OK) {
if (mci_read(read_buf, sector, 1) == DISKIO_RES_OK) {
printf("[disk] Read sector %lu (%lu):\n", sector, offset);
for (unsigned long i = offset + 1; i <= offset + length; i++) {
@ -51,7 +51,7 @@ int _get_sectorsize(int argc, char **argv)
(void) argv;
unsigned short ssize;
if (MCI_ioctl(GET_SECTOR_SIZE, &ssize) == RES_OK) {
if (mci_ioctl(GET_SECTOR_SIZE, &ssize) == DISKIO_RES_OK) {
printf("[disk] sector size is %u\n", ssize);
return 0;
@ -69,7 +69,7 @@ int _get_blocksize(int argc, char **argv)
(void) argv;
unsigned long bsize;
if (MCI_ioctl(GET_BLOCK_SIZE, &bsize) == RES_OK) {
if (mci_ioctl(GET_BLOCK_SIZE, &bsize) == DISKIO_RES_OK) {
printf("[disk] block size is %lu\n", bsize);
return 0;
@ -87,7 +87,7 @@ int _get_sectorcount(int argc, char **argv)
(void) argv;
unsigned long scount;
if (MCI_ioctl(GET_SECTOR_COUNT, &scount) == RES_OK) {
if (mci_ioctl(GET_SECTOR_COUNT, &scount) == DISKIO_RES_OK) {
printf("[disk] sector count is %lu\n", scount);
return 0;
@ -107,7 +107,7 @@ int _read_sector(int argc, char **argv)
unsigned long sectornr = atol(argv[1]);
if ((MCI_ioctl(GET_SECTOR_COUNT, &scount) == RES_OK) && (MCI_ioctl(GET_SECTOR_SIZE, &ssize) == RES_OK)) {
if ((mci_ioctl(GET_SECTOR_COUNT, &scount) == DISKIO_RES_OK) && (mci_ioctl(GET_SECTOR_SIZE, &ssize) == DISKIO_RES_OK)) {
unsigned char read_buf[ssize];
if (sector_read(read_buf, sectornr, ssize, 0)) {
@ -138,7 +138,7 @@ int _read_bytes(int argc, char **argv)
length = atoi(argv[2]);
/* get card info */
if ((MCI_ioctl(GET_SECTOR_COUNT, &scount) == RES_OK) && (MCI_ioctl(GET_SECTOR_SIZE, &ssize) == RES_OK)) {
if ((mci_ioctl(GET_SECTOR_COUNT, &scount) == DISKIO_RES_OK) && (mci_ioctl(GET_SECTOR_SIZE, &ssize) == DISKIO_RES_OK)) {
/* calculate sector and offset position */
sector = (offset / ssize) + 1;
offset = (offset % ssize);

Loading…
Cancel
Save