欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

drv_spi.c

最新版IAR FOR ARM(EWARM)5.11中的代码例子
C
字号:
/**************************************************************************/
/*                                                                        */
/*     Copyright (C) 2006 Oki Electric Industry Co., LTD.                 */
/*                                                                        */
/*     System Name  :  ML675050 series                                    */
/*     Module Name  :  ML675050 spi driver program                        */
/*     File   Name  :  drv_spi.c                                          */
/*     Revision     :  01.00                                              */
/*     Date         :  2006/1/11 initial version                          */
/*                                                                        */
/**************************************************************************/

#include "common.h"
#if defined(__arm)
#include "ml675050.h"
#else
#include "ml675050sim.h"
#endif
#include "hal_api.h"
#include "drv_common.h"
#include "drv_spi.h"
#include "intrinsics.h"


#define SPI_PORT2_SEL_MASK 0xff00ffff
#define SPI_PORT2_SEL_SET  0x00550000

/******************************/
/*     Private defines        */
/******************************/
/*--------- variable ---------*/
static volatile int8_t transmit_flag;

/*--------- function ---------*/


/************************************************************************/
/*                                                                      */
/*  Function Name   : smpDrv_OpenSpi                                    */
/*  Input           : struct ML675050_SpiParam *pInit                   */
/*  Output          : int16_t DRV_OK(1)                                 */
/*                            DRV_PARAM_ERROR(-2)                       */
/*                                                                      */
/*  Note : open driver of spi.                                          */
/*                                                                      */
/************************************************************************/
int16_t smpDrv_OpenSpi(struct ML675050_SpiParam *pInit) {
    int16_t rtnVal = DRV_OK;
    uPLAT_PioFunctionParam port_param;
    uPLAT_InterruptParam param;

    rtnVal = uplat7dHAL_PioGetFunction(&port_param);
    if (rtnVal != DRV_OK) {
        return rtnVal;
    }

    port_param.port_sel2 = (port_param.port_sel2 & SPI_PORT2_SEL_MASK)
                           | SPI_PORT2_SEL_SET;
    rtnVal = uplat7dHAL_PioSetFunction(&port_param);
    if (rtnVal != DRV_OK) {
        return rtnVal;
    }

    if ((pInit->device_select != SPI_DEVICE_MASTER)
        && (pInit->device_select !=SPI_DEVICE_SLAVE)) {
        rtnVal = DRV_PARAM_ERROR;
    }

    if (pInit->device_select == SPI_DEVICE_MASTER) {
        switch (pInit->baudrate) {
        case SPI_BAUDRATE:
            break;
        default:
            rtnVal = DRV_PARAM_ERROR;
            break;
        }
    }

    if(pInit->send_size != SPI_SEND_SIZE) {
        rtnVal = DRV_PARAM_ERROR;
    }

    if(pInit->send_order != SPI_LSB_FIRST) {
        rtnVal = DRV_PARAM_ERROR;
    }

    if (rtnVal == DRV_OK) {
        transmit_flag = 0;

        /* Registration Callback function */
        param.primary = DRV_INT_SPI0;             /* SPI interrupt */
        param.level = EXILCC_ILC52 & INT_LV5;     /* level */
        param.callback = DrvSPI_spi_handler;      /* Call back function for SPI driver */
        rtnVal = uplat7dHAL_InterruptSetPrimary(&param);
        if (rtnVal == DRV_OK) {
           /* Initialize SPI */
            rtnVal =ml675050HAL_SpiInit(pInit);
            /* Enable irq interrupt. */
            __enable_interrupt(); //irq_en();
        }
    }
    return rtnVal;
}

/************************************************************************/
/*                                                                      */
/*  Function Name   : smpDrv_CloseSpi                                   */
/*  Input           : void                                              */
/*  Output          : void                                              */
/*                                                                      */
/*                                                                      */
/*  Note : open driver of spi.                                          */
/*                                                                      */
/************************************************************************/
int16_t smpDrv_CloseSpi(void) {
    int16_t rtnVal = DRV_OK;
    
    /* Disable irq interrupt. */
    __disable_interrupt(); //irq_dis();                              /* Disable Interrupt */
    
    return rtnVal;
}

/************************************************************************/
/*                                                                      */
/*  Function Name   : smpDrv_IoctlSpi                                   */
/*  Input           : uint16_t cmd, uint32_t arg                        */
/*  Output          : int16_t DRV_OK(1)                                 */
/*                            DRV_PARAM_ERROR(-2)                       */
/*                                                                      */
/*  Note :   spi input/output control.                                  */
/*                                                                      */
/************************************************************************/
int16_t smpDrv_IoctlSpi(uint16_t cmd, uint32_t arg) {
    int16_t rtnVal = DRV_OK;

    switch (cmd) {
    case ML675050_HAL_SPI_MASTER_TX_DATA:
        transmit_flag =0;
        rtnVal = ml675050HAL_SpiMasterTxData();
        if (rtnVal == DRV_OK) {
            /* Waiting for Spi Master transfer finishing */
            while (transmit_flag == 0) {
                ;
            }
        }
        break;
    case ML675050_HAL_SPI_SLAVE_TX_DATA:
        transmit_flag =0;
        rtnVal = ml675050HAL_SpiSlaveTxData();
        if (rtnVal == DRV_OK) {
            /* Waiting for Spi Slave transfer finishing */
            while (transmit_flag == 0) {
                ;
            }
        }
        break;
    case ML675050_HAL_SPI_READ_STATUS:
        rtnVal = ml675050HAL_SpiReadStatus((uint32_t *)arg);
        break;
    default :
        rtnVal = DRV_PARAM_ERROR;
        break;
    }

    return rtnVal;
}
/************************************************************************/
/*                                                                      */
/*  Function Name   : DrvSPI_spi_handler                                */
/*  Input           : uint16_t state                                    */
/*  Output          : int16_t DRV_OK(1)                                 */
/*                            DRV_PARAM_ERROR(-2)                       */
/*                                                                      */
/*  Note : spi handler.                                                 */
/*                                                                      */
/************************************************************************/
void DrvSPI_spi_handler(uint16_t state)
{
    transmit_flag = 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -