📄 abl_api.c
字号:
/*********************************************************************** * $Workfile: abl_api.c $ * $Revision: 1.0 $ * $Author: WellsK $ * $Date: Jun 09 2003 12:06:26 $ * * Project: Standard API * * Description: * This file implements non hardware specific I/O system * * Revision History: * $Log: //smaicnt2/pvcs/VM/sharpmcu/archives/sharpmcu/software/abl/source/abl_api.c-arc $ *
* Rev 1.0 Jun 09 2003 12:06:26 WellsK
* Initial revision.
* *********************************************************************** * SHARP MICROELECTRONICS OF THE AMERICAS MAKES NO REPRESENTATION * OR WARRANTIES WITH RESPECT TO THE PERFORMANCE OF THIS SOFTWARE, * AND SPECIFICALLY DISCLAIMS ANY RESPONSIBILITY FOR ANY DAMAGES, * SPECIAL OR CONSEQUENTIAL, CONNECTED WITH THE USE OF THIS SOFTWARE. * * SHARP MICROELECTRONICS OF THE AMERICAS PROVIDES THIS SOFTWARE SOLELY * FOR THE PURPOSE OF SOFTWARE DEVELOPMENT INCORPORATING THE USE OF A * SHARP MICROCONTROLLER OR SYSTEM-ON-CHIP PRODUCT. USE OF THIS SOURCE * FILE IMPLIES ACCEPTANCE OF THESE CONDITIONS. * * COPYRIGHT (C) 2001 SHARP MICROELECTRONICS OF THE AMERICAS, INC. * CAMAS, WA **********************************************************************/#include "abl_api.h"/* Private io system table */STATIC API_TABLE_T api[MAX_API_TABLE];/* Private methods */STATIC STATUS api_remove_device (INT_32 id);STATIC INT_32 api_find_device (INT_32 id);STATIC INT_32 api_find_empty (void);STATIC STATUS api_add_device (INT_32 id, void* open, void* close, void* read, void* write, void* ioctl);/* State variable for init */STATIC INT_32 api_is_init = FALSE;/* Max size of the device table */#define MAX_API_DEVS NELEMENTS(api)/************************************************************************* Function: abl_api_init** Purpose: * To initialize the api system** Processing:* This function clears the api system table and marks it as* initialized. Once the table has been initialized the devices can* be bound to the io system and make use of the common API.* * Parameters: * config - Not used ** Outputs: * None** Returns: * None** Notes: * See abl_api.h for structure definitions*************************************************************************/void abl_api_init (void* cfg){ UNS_32 idx = 0; CHAR* dst = NULL; /* Clear the table on startup */ if (api_is_init == FALSE) { /* Clear the driver table */ dst = (CHAR*)&api[0]; for (idx = 0; idx < (sizeof(API_TABLE_T) * MAX_API_DEVS); idx++) { *dst++ = 0; } } /* Set the init flag */ api_is_init = TRUE;}/************************************************************************* Function: abl_api_register** Purpose: * To register a device with the system** Processing:* This funtion is used to bind a device to the system. Once bound* the device can make use of the common API layer.* * Parameters: * id - device id.* open - driver open method* close - driver close method* read - driver read method* write - driver write method* ioctl - driver io control method** Outputs: * None** Returns: * None** Notes: * See abl_api.h for structure definitions*************************************************************************/STATUS abl_api_register (INT_32 id, void* open, void* close, void* read, void* write, void* ioctl) { INT_32 status = _NO_ERROR; /* Add the device to the io system */ status = api_add_device (id, open, close, read, write, ioctl); /* Return the status of the registration */ return (status);}/************************************************************************* Function: abl_open** Purpose: * Connects to a system device** Processing:* This routine calls the associated open method in the io subsystem * array. If the device asscoiated with the name is not registered an * error -1 is returned. If the device is registered and not already * opened a file descriptor that uniquely identifies this device is * returned. * * Parameters:* id - Device id to open * arg - Options used to open the device** Outputs: * None** Returns: * device file decriptor* -1 if the device does not exist** Notes: * See sma_iosys.h for structure definitions*************************************************************************/INT_32 abl_open (INT_32 id, INT_32 arg){ INT_32 fd = 0; INT_32 devid = 0; /* Get the index for the device */ if ((fd = api_find_device (id)) == -1) { /* The device is not in the table */ return (_ERROR); } /* Open the device - returns param to be passed into driver */ devid = (*(PFI)(api[fd].driver.open)) (id, arg); /* Sanity check */ if (devid == 0) { /* Error opening the device */ return (-1); } /* Save the param to be used by the device driver */ api[fd].devid = devid; /* Update the device state as opened */ api[fd].opened = TRUE; /* Return the device file descriptor */ return (fd);}/************************************************************************* Function: abl_close** Purpose: * closes a session with an device driver** Processing:* This routine marks the device as closed and then calls the * associated close method at the device driver layer to disable* the hardware.* * Parameters:* fd - file descriptor of the device to be closed** Outputs: * None** Returns: * _NO_ERROR if the device has been closed* _ERROR if the device could not be closed** Notes: * See abl_api.h for structure definitions*************************************************************************/STATUS abl_close (INT_32 fd){ STATUS status = _NO_ERROR; /* Sanity check */ if (fd > MAX_API_DEVS) { /* Invalid file descriptor */ return (_ERROR); } /* Is the device opened */ if (api[fd].opened != TRUE) { /* Device is not opened */ return (_ERROR); } /* Close the device at the driver layer */ status = (*(PFI)(api[fd].driver.close)) (api[fd].devid); /* If the device has been closed then mark it as so */ if (status == _NO_ERROR) { /* Mark the device as closed */ api[fd].opened = FALSE; /* Remove the device from the control table */ (void) api_remove_device (fd); } /* Return the close call status */ return (status);}/************************************************************************* Function: abl_read** Purpose: * reads data from a registered api system device.** Processing:* This routine reads data from a registered api device by using the * callback method that has been bound to a driver. If the * device is not registered -1 is returned. If the device is * registered the user can pass in a buffer and a max number of * bytes for the driver to use.* * Parameters:* fd - device file descriptor.* buffer - data buffer.* max_bytes - max number of bytes to read.** Outputs: * None** Returns: * -1 if the device is not registered.* actual number of bytes read.** Notes: * See abl_api.h for structure definitions*************************************************************************/INT_32 abl_read (INT_32 fd, CHAR* buffer, INT_32 max_bytes){ INT_32 n_bytes = 0; /* Sanity check */ if (fd > MAX_API_DEVS) { /* Non valid file descriptor */ return (-1); } /* Method not bound to system */ if (api[fd].driver.read == NULL) { return (0); } /* Get the data from the device */ n_bytes = (*(PFI)(api[fd].driver.read))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -