Changeset 42bf1b9 in rtems for c/src/libchip


Ignore:
Timestamp:
May 15, 2008, 3:10:38 PM (12 years ago)
Author:
Thomas Doerfler <Thomas.Doerfler@…>
Branches:
4.10, 4.11, 4.9, master
Children:
e0abba9
Parents:
8ac748c
Message:

adapted gen83xx to new board

Location:
c/src/libchip
Files:
4 added
4 edited

Legend:

Unmodified
Added
Removed
  • c/src/libchip/Makefile.am

    r8ac748c r42bf1b9  
    7575if LIBCHIP
    7676include_libchip_HEADERS += i2c/i2c-ds1621.h i2c/i2c-2b-eeprom.h \
    77                            i2c/spi-flash-m25p40.h
     77                           i2c/spi-memdrv.h                     \
     78                           i2c/spi-flash-m25p40.h i2c/spi-fram-fm25l256.h
    7879
    7980noinst_LIBRARIES += libi2cio.a
     
    8182libi2cio_a_SOURCES  = i2c/i2c-ds1621.c i2c/i2c-2b-eeprom.c \
    8283    i2c/i2c-ds1621.h i2c/i2c-2b-eeprom.h \
    83     i2c/spi-flash-m25p40.c i2c/spi-flash-m25p40.h
     84    i2c/spi-memdrv.c i2c/spi-memdrv.h \
     85    i2c/spi-flash-m25p40.c i2c/spi-flash-m25p40.h \
     86    i2c/spi-fram-fm25l256.c i2c/spi-fram-fm25l256.h
    8487endif
    8588
  • c/src/libchip/i2c/spi-flash-m25p40.c

    r8ac748c r42bf1b9  
    1616+-----------------------------------------------------------------+
    1717\*===============================================================*/
    18 /*
    19  * FIXME: currently, this driver only supports read/write accesses
    20  * erase accesses are to be completed
    21  */
    22 
    2318
    2419#include <rtems.h>
     
    2823#include <rtems/libio.h>
    2924
    30 #define FLASH_M25P40_CMD_WREN  0x06
    31 #define FLASH_M25P40_CMD_WRDIS 0x04
    32 #define FLASH_M25P40_CMD_RDID  0x9F
    33 #define FLASH_M25P40_CMD_RDSR  0x05
    34 #define FLASH_M25P40_CMD_WRSR  0x01
    35 #define FLASH_M25P40_CMD_READ  0x03
    36 #define FLASH_M25P40_CMD_PP    0x02  /* page program                       */
    37 #define FLASH_M25P40_CMD_SE    0xD8  /* sector erase                       */
    38 #define FLASH_M25P40_CMD_BE    0xC7  /* bulk erase                         */
    39 #define FLASH_M25P40_CMD_DP    0xB9  /* deep power down                    */
    40 #define FLASH_M25P40_CMD_RES   0xAB  /* release from deep power down       */
    4125
    42 #define M25P40_PAGE_SIZE 256
    43 #define M25P40_TOTAL_SIZE (512*1024)
    44 
    45 const rtems_libi2c_tfr_mode_t spi_flash_m25p40_tfr_mode = {
    46   baudrate:      20000000, /* maximum bits per second                   */
    47   bits_per_char: 8,        /* how many bits per byte/word/longword?     */
    48   lsb_first:      FALSE,   /* FALSE: send MSB first                     */
    49   clock_inv:      FALSE,   /* FALSE: non-inverted clock (high active)   */
    50   clock_phs:      FALSE    /* FALSE: clock starts in middle of data tfr */
    51 } ;
    52 
    53 /*=========================================================================*\
    54 | Function:                                                                 |
    55 \*-------------------------------------------------------------------------*/
    56 rtems_status_code spi_flash_m25p40_wait_ms
    57 (
    58 /*-------------------------------------------------------------------------*\
    59 | Purpose:                                                                  |
    60 |   wait a certain interval given in ms                                     |
    61 +---------------------------------------------------------------------------+
    62 | Input Parameters:                                                         |
    63 \*-------------------------------------------------------------------------*/
    64  int ms                                  /* time to wait in milliseconds   */
    65 )
    66 /*-------------------------------------------------------------------------*\
    67 | Return Value:                                                             |
    68 |    o = ok or error code                                                   |
    69 \*=========================================================================*/
    70 {
    71   rtems_status_code          rc = RTEMS_SUCCESSFUL;
    72   rtems_interval ticks_per_second;
    73 
    74   rc = rtems_clock_get(RTEMS_CLOCK_GET_TICKS_PER_SECOND,&ticks_per_second);
    75   if (rc == RTEMS_SUCCESSFUL) {
    76     rc = rtems_task_wake_after(ticks_per_second * ms / 1000);
     26static spi_memdrv_t spi_flash_m25p40_rw_drv_t = {
     27  {/* public fields */
     28  ops:         &spi_memdrv_rw_ops, /* operations of general memdrv */
     29  size:        sizeof (spi_flash_m25p40_rw_drv_t),
     30  },
     31  { /* our private fields */
     32  baudrate:             2000000,
     33  erase_before_program: TRUE,
     34  empty_state:          0xff,
     35  page_size:            256, /* programming page size in byte */
     36  sector_size:          64*1024, /* erase sector size in byte */
     37  mem_size:             512*1024 /* total capacity in byte    */
    7738  }
    78   return rc;
    79 }
    80 
    81 /*=========================================================================*\
    82 | Function:                                                                 |
    83 \*-------------------------------------------------------------------------*/
    84 rtems_status_code spi_flash_m25p40_write
    85 (
    86 /*-------------------------------------------------------------------------*\
    87 | Purpose:                                                                  |
    88 |   write a block of data to flash                                          |
    89 +---------------------------------------------------------------------------+
    90 | Input Parameters:                                                         |
    91 \*-------------------------------------------------------------------------*/
    92  rtems_device_major_number major,        /* major device number            */
    93  rtems_device_major_number minor,        /* minor device number            */
    94  void                      *arg          /* ptr to write argument struct   */
    95 )
    96 /*-------------------------------------------------------------------------*\
    97 | Return Value:                                                             |
    98 |    o = ok or error code                                                   |
    99 \*=========================================================================*/
    100 {
    101   rtems_status_code          rc = RTEMS_SUCCESSFUL;
    102   rtems_libio_rw_args_t *rwargs = arg;
    103   unsigned                  off = rwargs->offset;
    104   int                       cnt = rwargs->count;
    105   unsigned char            *buf = (unsigned char *)rwargs->buffer;
    106   int                bytes_sent = 0;
    107   int                  curr_cnt;
    108   unsigned char       cmdbuf[4];
    109   int                   ret_cnt = 0;
    110   /*
    111    * check arguments
    112    */
    113   if (rc == RTEMS_SUCCESSFUL) {
    114     if ((cnt <= 0)                      ||
    115         (cnt > M25P40_TOTAL_SIZE)       ||
    116         (off > (M25P40_TOTAL_SIZE-cnt))) {
    117       rc = RTEMS_INVALID_SIZE;
    118     }
    119     else if (buf == NULL) {
    120       rc = RTEMS_INVALID_ADDRESS;
    121     }
    122   }
    123   /*
    124    * select device, set transfer mode, address device
    125    */
    126   if (rc == RTEMS_SUCCESSFUL) {
    127     rc = rtems_libi2c_send_start(minor);
    128   }
    129   /*
    130    * set transfer mode
    131    */
    132   if (rc == RTEMS_SUCCESSFUL) {
    133     rc = -rtems_libi2c_ioctl(minor,
    134                              RTEMS_LIBI2C_IOCTL_SET_TFRMODE,
    135                              &spi_flash_m25p40_tfr_mode);
    136   }
    137 
    138   /*
    139    * address device
    140    */
    141   if (rc == RTEMS_SUCCESSFUL) {
    142     rc = rtems_libi2c_send_addr(minor,TRUE);
    143   }
    144 
    145   /*
    146    * send write_enable command
    147    */
    148   if (rc == RTEMS_SUCCESSFUL) {
    149     cmdbuf[0] = FLASH_M25P40_CMD_WREN;
    150     ret_cnt = rtems_libi2c_write_bytes(minor,cmdbuf,1);
    151     if (ret_cnt < 0) {
    152       rc = -ret_cnt;
    153     }
    154   }
    155   /*
    156    * terminate transfer
    157    */
    158   if (rc == RTEMS_SUCCESSFUL) {
    159     rc = rtems_libi2c_send_stop(minor);
    160   }
    161   while ((rc == RTEMS_SUCCESSFUL) &&
    162          (cnt > bytes_sent)) {
    163     curr_cnt = cnt;
    164     if ((off              / M25P40_PAGE_SIZE) !=
    165         ((off+curr_cnt+1) / M25P40_PAGE_SIZE)) {
    166       curr_cnt = M25P40_PAGE_SIZE - (off % M25P40_PAGE_SIZE);
    167     }
    168     /*
    169      * select device, set transfer mode
    170      */
    171     if (rc == RTEMS_SUCCESSFUL) {
    172       rc = rtems_libi2c_send_start(minor);
    173     }
    174 
    175     /*
    176      * address device
    177      */
    178     if (rc == RTEMS_SUCCESSFUL) {
    179       rc = rtems_libi2c_send_addr(minor,TRUE);
    180     }
    181 
    182     /*
    183      * set transfer mode
    184      */
    185     if (rc == RTEMS_SUCCESSFUL) {
    186       rc = -rtems_libi2c_ioctl(minor,
    187                                RTEMS_LIBI2C_IOCTL_SET_TFRMODE,
    188                                &spi_flash_m25p40_tfr_mode);
    189     }
    190     /*
    191      * send "page program" command and address
    192      */
    193     if (rc == RTEMS_SUCCESSFUL) {
    194       cmdbuf[0] = FLASH_M25P40_CMD_PP;
    195       cmdbuf[1] = (off >> 16) & 0xff;
    196       cmdbuf[2] = (off >>  8) & 0xff;
    197       cmdbuf[3] = (off >>  0) & 0xff;
    198       ret_cnt = rtems_libi2c_write_bytes(minor,cmdbuf,4);
    199       if (ret_cnt < 0) {
    200         rc = -ret_cnt;
    201       }
    202     }
    203     /*
    204      * send write data
    205      */
    206     if (rc == RTEMS_SUCCESSFUL) {
    207       ret_cnt = rtems_libi2c_write_bytes(minor,buf,curr_cnt);
    208       if (ret_cnt < 0) {
    209         rc = -ret_cnt;
    210       }
    211     }
    212     /*
    213      * terminate transfer
    214      */
    215     if (rc == RTEMS_SUCCESSFUL) {
    216       rc = rtems_libi2c_send_stop(minor);
    217     }
    218     /*
    219      * wait proper time for data to store: 5ms
    220      */
    221     if (rc == RTEMS_SUCCESSFUL) {
    222       rc = spi_flash_m25p40_wait_ms(5);
    223     }
    224     /*
    225      * adjust bytecount to be sent and pointers
    226      */
    227     bytes_sent += curr_cnt;
    228     off        += curr_cnt;
    229     buf        += curr_cnt;
    230   }
    231   rwargs->bytes_moved = bytes_sent;
    232   return rc;
    233 }
    234 
    235 /*=========================================================================*\
    236 | Function:                                                                 |
    237 \*-------------------------------------------------------------------------*/
    238 rtems_status_code spi_flash_m25p40_read
    239 (
    240 /*-------------------------------------------------------------------------*\
    241 | Purpose:                                                                  |
    242 |   read a block of data from flash                                         |
    243 +---------------------------------------------------------------------------+
    244 | Input Parameters:                                                         |
    245 \*-------------------------------------------------------------------------*/
    246  rtems_device_major_number major,        /* major device number            */
    247  rtems_device_major_number minor,        /* minor device number            */
    248  void                      *arg          /* ptr to read argument struct    */
    249 )
    250 /*-------------------------------------------------------------------------*\
    251 | Return Value:                                                             |
    252 |    o = ok or error code                                                   |
    253 \*=========================================================================*/
    254 {
    255   rtems_status_code rc = RTEMS_SUCCESSFUL;
    256   rtems_libio_rw_args_t *rwargs = arg;
    257   unsigned                  off = rwargs->offset;
    258   int                       cnt = rwargs->count;
    259   unsigned char            *buf = (unsigned char *)rwargs->buffer;
    260   unsigned char       cmdbuf[4];
    261   int                   ret_cnt = 0;
    262   /*
    263    * check arguments
    264    */
    265   if (rc == RTEMS_SUCCESSFUL) {
    266     if ((cnt <= 0)                      ||
    267         (cnt > M25P40_TOTAL_SIZE)       ||
    268         (off > (M25P40_TOTAL_SIZE-cnt))) {
    269       rc = RTEMS_INVALID_SIZE;
    270     }
    271     else if (buf == NULL) {
    272       rc = RTEMS_INVALID_ADDRESS;
    273     }
    274   }
    275   /*
    276    * select device, set transfer mode, address device
    277    */
    278   if (rc == RTEMS_SUCCESSFUL) {
    279     rc = rtems_libi2c_send_start(minor);
    280   }
    281   /*
    282    * set transfer mode
    283    */
    284   if (rc == RTEMS_SUCCESSFUL) {
    285     rc = -rtems_libi2c_ioctl(minor,
    286                              RTEMS_LIBI2C_IOCTL_SET_TFRMODE,
    287                              &spi_flash_m25p40_tfr_mode);
    288   }
    289   /*
    290    * address device
    291    */
    292   if (rc == RTEMS_SUCCESSFUL) {
    293     rc = rtems_libi2c_send_addr(minor,TRUE);
    294   }
    295 
    296   if (off >= M25P40_TOTAL_SIZE) {
    297     /*
    298      * HACK: beyond size of Flash array? then read status register instead
    299      */
    300     /*
    301      * send read status register command
    302      */
    303     if (rc == RTEMS_SUCCESSFUL) {
    304       cmdbuf[0] = FLASH_M25P40_CMD_RDSR;
    305       ret_cnt = rtems_libi2c_write_bytes(minor,cmdbuf,1);
    306       if (ret_cnt < 0) {
    307         rc = -ret_cnt;
    308       }
    309     }
    310   }
    311   else {
    312     /*
    313      * send read command and address
    314      */
    315     if (rc == RTEMS_SUCCESSFUL) {
    316       cmdbuf[0] = FLASH_M25P40_CMD_READ;
    317       cmdbuf[1] = (off >> 16) & 0xff;
    318       cmdbuf[2] = (off >>  8) & 0xff;
    319       cmdbuf[3] = (off >>  0) & 0xff;
    320       ret_cnt = rtems_libi2c_write_bytes(minor,cmdbuf,4);
    321       if (ret_cnt < 0) {
    322         rc = -ret_cnt;
    323       }
    324     }
    325   }
    326   /*
    327    * fetch read data
    328    */
    329   if (rc == RTEMS_SUCCESSFUL) {
    330     ret_cnt = rtems_libi2c_read_bytes (minor,buf,cnt);
    331     if (ret_cnt < 0) {
    332       rc = -ret_cnt;
    333     }
    334   }
    335    
    336   /*
    337    * terminate transfer
    338    */
    339   if (rc == RTEMS_SUCCESSFUL) {
    340     rc = rtems_libi2c_send_stop(minor);
    341   }
    342   rwargs->bytes_moved = (rc == RTEMS_SUCCESSFUL) ? ret_cnt : 0;
    343 
    344   return rc;
    345 }
    346 
    347 static rtems_driver_address_table spi_flash_m25p40_rw_ops = {
    348   read_entry:  spi_flash_m25p40_read,
    349   write_entry: spi_flash_m25p40_write
    350 };
    351 
    352 static rtems_libi2c_drv_t spi_flash_m25p40_rw_drv_tbl = {
    353   ops:         &spi_flash_m25p40_rw_ops,
    354   size:        sizeof (spi_flash_m25p40_rw_drv_tbl),
    35539};
    35640
    35741rtems_libi2c_drv_t *spi_flash_m25p40_rw_driver_descriptor =
    358 &spi_flash_m25p40_rw_drv_tbl;
     42&spi_flash_m25p40_rw_drv_t.libi2c_drv_entry;
    35943
    360 static rtems_driver_address_table spi_flash_m25p40_ro_ops = {
    361   read_entry:  spi_flash_m25p40_read,
    362 };
    363 
    364 static rtems_libi2c_drv_t spi_flash_m25p40_ro_drv_tbl = {
    365   ops:         &spi_flash_m25p40_ro_ops,
    366   size:        sizeof (spi_flash_m25p40_ro_drv_tbl),
     44static spi_memdrv_t spi_flash_m25p40_ro_drv_t = {
     45  {/* public fields */
     46  ops:         &spi_memdrv_ro_ops, /* operations of general memdrv */
     47  size:        sizeof (spi_flash_m25p40_ro_drv_t),
     48  },
     49  { /* our private fields */
     50  baudrate:             2000000,
     51  erase_before_program: TRUE,
     52  empty_state:          0xff,
     53  page_size:            256, /* programming page size in byte */
     54  sector_size:          64*1024, /* erase sector size in byte */
     55  mem_size:             512*1024 /* total capacity in byte    */
     56  }
    36757};
    36858
    36959rtems_libi2c_drv_t *spi_flash_m25p40_ro_driver_descriptor =
    370 &spi_flash_m25p40_ro_drv_tbl;
     60&spi_flash_m25p40_ro_drv_t.libi2c_drv_entry;
  • c/src/libchip/i2c/spi-flash-m25p40.h

    r8ac748c r42bf1b9  
    2525#define _LIBCHIP_SPI_FLASH_M25P40_H
    2626
    27 #include <rtems/libi2c.h>
     27#include <libchip/spi-memdrv.h>
    2828
    2929#ifdef __cplusplus
  • c/src/libchip/preinstall.am

    r8ac748c r42bf1b9  
    126126PREINSTALL_FILES += $(PROJECT_INCLUDE)/libchip/i2c-2b-eeprom.h
    127127
     128$(PROJECT_INCLUDE)/libchip/spi-memdrv.h: i2c/spi-memdrv.h $(PROJECT_INCLUDE)/libchip/$(dirstamp)
     129        $(INSTALL_DATA) $< $(PROJECT_INCLUDE)/libchip/spi-memdrv.h
     130PREINSTALL_FILES += $(PROJECT_INCLUDE)/libchip/spi-memdrv.h
     131
    128132$(PROJECT_INCLUDE)/libchip/spi-flash-m25p40.h: i2c/spi-flash-m25p40.h $(PROJECT_INCLUDE)/libchip/$(dirstamp)
    129133        $(INSTALL_DATA) $< $(PROJECT_INCLUDE)/libchip/spi-flash-m25p40.h
    130134PREINSTALL_FILES += $(PROJECT_INCLUDE)/libchip/spi-flash-m25p40.h
     135
     136$(PROJECT_INCLUDE)/libchip/spi-fram-fm25l256.h: i2c/spi-fram-fm25l256.h $(PROJECT_INCLUDE)/libchip/$(dirstamp)
     137        $(INSTALL_DATA) $< $(PROJECT_INCLUDE)/libchip/spi-fram-fm25l256.h
     138PREINSTALL_FILES += $(PROJECT_INCLUDE)/libchip/spi-fram-fm25l256.h
    131139endif
    132140if LIBCHIP
Note: See TracChangeset for help on using the changeset viewer.