Beispiel #1
0
void wav(const char* filename)
{
    int fd;
	rt_size_t block_size;

	block_size = sbuf_get_size();
	block_size = (block_size / 512) * 512;

    fd = open(filename, O_RDONLY, 0);
    if (fd >= 0)
    {
		rt_uint8_t* buf;
		rt_size_t 	len;
		rt_device_t device;

		/* open audio device and set tx done call back */
		device = rt_device_find("snd");
		rt_device_set_tx_complete(device, wav_tx_done);
		rt_device_open(device, RT_DEVICE_OFLAG_WRONLY);

		do
		{
			buf = sbuf_alloc();
			len = read(fd, (char*)buf, block_size);
			if (len > 0) rt_device_write(device, 0, buf, len);
			else sbuf_release(buf);
		} while (len != 0);

		/* close device and file */
		rt_device_close(device);
		close(fd);
    }
}
Beispiel #2
0
void rcu_uart_set_device(char* uartStr)
{
    static int isInited=0;
    rt_device_t dev = RT_NULL;
                                                        
    if(isInited==1)
        return;

    dev = rt_device_find(uartStr);
    if (dev == RT_NULL)
    {
        hclog("finsh: can not find device: %s\n", uartStr);
        return;
    }
    if(dev == rcuDevice)
        return;

    /* open this device and set the new device */
    if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR) == RT_EOK)
    {
        if (rcuDevice != RT_NULL)
        {
            /* close old  device */
            rt_device_close(rcuDevice);
            rt_device_set_rx_indicate(dev, RT_NULL);
        }
        rcuDevice = dev;
        rt_device_set_rx_indicate(dev, rcu_rx_ind);
    }
    isInited = 1;
                        
}
Beispiel #3
0
/**
 * @ingroup finsh
 *
 * This function sets the input device of finsh shell.
 *
 * @param device_name the name of new input device.
 */
void finsh_set_device(const char* device_name)
{
	rt_device_t dev = RT_NULL;

	RT_ASSERT(shell != RT_NULL);
	dev = rt_device_find(device_name);
	if (dev == RT_NULL)
	{
		rt_kprintf("finsh: can not find device: %s\n", device_name);
		return;
	}

	/* check whether it's a same device */
	if (dev == shell->device) return;
	/* open this device and set the new device in finsh shell */
	if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR) == RT_EOK)
	{
		if (shell->device != RT_NULL)
		{
			/* close old finsh device */
			rt_device_close(shell->device);
            rt_device_set_rx_indicate(dev, RT_NULL);
		}

		shell->device = dev;
		rt_device_set_rx_indicate(dev, finsh_rx_ind);
	}
}
Beispiel #4
0
rt_err_t log_trace_set_device(const char *device_name)
{
    struct rt_device *output_device;

    /* find out output device */
    output_device = rt_device_find(device_name);
    if (output_device != RT_NULL)
    {
        rt_err_t result;

        /* open device */
        result = rt_device_open(output_device, RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_RDWR);
        if (result != RT_EOK)
        {
            rt_kprintf("Open trace device failed.\n");
            return -RT_ERROR;
        }
    }

    /* set trace out device */
    if (_traceout_device != RT_NULL)
        rt_device_close(_traceout_device);
    _traceout_device = output_device;

    return RT_EOK;
}
Beispiel #5
0
void uart1_rs485_set_device(void)
{
	    rt_device_t dev = RT_NULL;

    dev = rt_device_find("uart2");
	if (dev == RT_NULL)
    {
        rt_kprintf("finsh: can not find device: %s\n", "uart1");
        return;
    }

    /* check whether it's a same device */
    if (dev == uart1_dev_my->device) return;
		
    if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX |\
                       RT_DEVICE_FLAG_STREAM) == RT_EOK)
    {
        if (uart1_dev_my->device != RT_NULL)
        {
            /* close old finsh device */
            rt_device_close(uart1_dev_my->device);
            rt_device_set_rx_indicate(uart1_dev_my->device, RT_NULL);
        }

        uart1_dev_my->device = dev;
        rt_device_set_rx_indicate(dev, rs485_rx_ind);
    }
}
Beispiel #6
0
int dfs_device_fs_close(struct dfs_fd* file)
{
    rt_err_t result;
    rt_device_t dev_id;

    RT_ASSERT(file != RT_NULL);

    if (file->type == FT_DIRECTORY) {
        struct device_dirent *root_dirent;

        root_dirent = (struct device_dirent*)file->data;
        RT_ASSERT(root_dirent != RT_NULL);

        /* release dirent */
        rt_free(root_dirent);
        return DFS_STATUS_OK;
    }

    /* get device handler */
    dev_id = (rt_device_t)file->data;
    RT_ASSERT(dev_id != RT_NULL);

    /* close device handler */
    result = rt_device_close(dev_id);
    if (result == RT_EOK) {
        file->data = RT_NULL;
        return DFS_STATUS_OK;
    }

    return -DFS_STATUS_EIO;
}
Beispiel #7
0
/**
 * @ingroup finsh
 *
 * This function sets the input device of finsh shell.
 *
 * @param device_name the name of new input device.
 */
void finsh_set_device(const char *device_name)
{
    rt_device_t dev = RT_NULL;

    RT_ASSERT(shell != RT_NULL);
    dev = rt_device_find(device_name);
    if (dev == RT_NULL)
    {
        rt_kprintf("finsh: can not find device: %s\n", device_name);
        return;
    }

    /* check whether it's a same device */
    if (dev == shell->device) return;
    /* open this device and set the new device in finsh shell */
    if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX | \
                       RT_DEVICE_FLAG_STREAM) == RT_EOK)
    {
        if (shell->device != RT_NULL)
        {
            /* close old finsh device */
            rt_device_close(shell->device);
            rt_device_set_rx_indicate(shell->device, RT_NULL);
        }

        /* clear line buffer before switch to new device */
        memset(shell->line, 0, sizeof(shell->line));
        shell->line_curpos = shell->line_position = 0;

        shell->device = dev;
        rt_device_set_rx_indicate(dev, finsh_rx_ind);
    }
}
Beispiel #8
0
void mp3_decoder_detach(struct mp3_decoder* decoder)
{
    RT_ASSERT(decoder != RT_NULL);

	/* close audio device */
	if (decoder->snd_device != RT_NULL)
		rt_device_close(decoder->snd_device);

	/* release mp3 decoder */
    MP3FreeDecoder(decoder->decoder);
}
Beispiel #9
0
static void ftk_source_input_destroy(FtkSource* thiz)
{
    if (thiz != NULL)
    {
        DECL_PRIV(thiz, priv);

        ftk_rtthread_select_fd_free(priv->fd);

        rt_device_close(priv->device);

        FTK_ZFREE(thiz, sizeof(thiz) + sizeof(PrivInfo));
    }
}
Beispiel #10
0
static void ftk_source_touch_destroy(FtkSource* thiz)
{
	if (thiz != NULL)
	{
		DECL_PRIV(thiz, priv);

		ftk_rtthread_select_fd_free(priv->fd);

		rt_mq_detach(&priv->mq);

		rt_device_control(priv->device, RT_TOUCH_EVENTPOST, NULL); 

		rt_device_close(priv->device);

		FTK_ZFREE(thiz, sizeof(thiz) + sizeof(PrivInfo));
	}
}
Beispiel #11
0
int ft5406_hw_init(void)
{
    rt_thread_t tid;
    rt_device_t dev;

    dev = rt_device_find(I2CBUS_NAME);
    if (!dev) return -1;

    if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
        return -1;

    FTDEBUG("ft5406 set i2c bus to %s\n", I2CBUS_NAME);
    _i2c_bus = (struct rt_i2c_bus_device *)dev;

    {
        gpio_pin_config_t pin_config =
        {
            kGPIO_DigitalOutput, 0,
        };

        CLOCK_EnableClock(kCLOCK_Gpio2);

        /* Enable touch panel controller */
        GPIO_PinInit(GPIO, 2, 27, &pin_config);
        GPIO_WritePinOutput(GPIO, 2, 27, 1);
        rt_thread_delay(50);
        GPIO_WritePinOutput(GPIO, 2, 27, 0);
        rt_thread_delay(50);
        GPIO_WritePinOutput(GPIO, 2, 27, 1);
    }

    rt_sem_init(&_tp_sem, "touch", 0, RT_IPC_FLAG_FIFO);
    tid = rt_thread_create("touch", _touch, RT_NULL,
                           2048, 10, 20);
    if (!tid)
    {
        rt_device_close(dev);
        return -1;
    }

    rt_thread_startup(tid);

    return 0;
}
Beispiel #12
0
rt_err_t rym_cat_to_dev(rt_device_t idev, rt_device_t odev)
{
    struct rym_ctx rctx;
    rt_err_t res;
    _odev = odev;

    rt_kprintf("entering RYM mode\n");
    odev->flag &= ~RT_DEVICE_FLAG_STREAM;
    res = rt_device_open(odev, 0);
    if (res != RT_EOK)
    {
        rt_kprintf("open output device error: 0x%x", -res);
        return res;
    }
    res = rym_recv_on_device(&rctx, idev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
                             RT_NULL, _rym_echo_data, RT_NULL, 1000);
    rt_device_close(_odev);
    rt_kprintf("leaving RYM mode with code %X\n", res);
    return res;
}
int
test_tdc_gp21(void)
{
    rt_device_t tdc = RT_NULL;
#if 0
    struct spi_tdc_gp21_tof_data tof_data;
    struct spi_tdc_gp21_temp_scales temp_scales;
#endif

    if(RT_EOK != tdc_gp21_register()) {
        return -RT_ERROR;
    }

    tdc = rt_device_find("tdc1");
    if(RT_NULL == tdc) {
        return -RT_ERROR;
    }
    if(RT_EOK != rt_device_open(tdc, RT_NULL)) {
        return -RT_ERROR;
    }
#if 0
    if(RT_EOK != rt_device_control(tdc,
                                   SPI_TDC_GP21_CTRL_MEASURE_TEMP,
                                   &temp_scales)) {
        return -RT_ERROR;
    }
    if(RT_EOK != rt_device_control(tdc,
                                   SPI_TDC_GP21_CTRL_MEASURE_TOF2,
                                   &tof_data)) {
        return -RT_ERROR;
    }
    if(RT_EOK != rt_device_close(tdc)) {
        return -RT_ERROR;
    }
    if(RT_EOK != rt_device_unregister(tdc)) {
        return -RT_ERROR;
    }
#endif
		return RT_EOK;
}
Beispiel #14
0
/**
 * @ingroup finsh
 *
 * This function sets the input device of finsh shell.
 *
 * @param device_name the name of new input device.
 */
void finsh_set_device(const char* device_name)
{
	rt_device_t dev = RT_NULL;

	RT_ASSERT(shell != RT_NULL);
	dev = rt_device_find(device_name);
	if (dev != RT_NULL && rt_device_open(dev, RT_DEVICE_OFLAG_RDWR) == RT_EOK)
	{
		if (shell->device != RT_NULL)
		{
			/* close old finsh device */
			rt_device_close(shell->device);
		}

		shell->device = dev;
		rt_device_set_rx_indicate(dev, finsh_rx_ind);
	}
	else
	{
		rt_kprintf("finsh: can not find device:%s\n", device_name);
	}
}
Beispiel #15
0
/** \brief set system time(date not modify).
 *
 * \param rt_uint32_t hour   e.g: 0~23.
 * \param rt_uint32_t minute e.g: 0~59.
 * \param rt_uint32_t second e.g: 0~59.
 * \return rt_err_t if set success, return RT_EOK.
 *
 */
rt_err_t i2c(rt_uint8_t wr, rt_uint32_t addr, rt_uint32_t reg, rt_uint32_t data)
{
    struct rt_i2c_priv_data i2c_priv_data;
    struct rt_i2c_msg i2c_msg[2];
    rt_uint8_t i2c_data[10];
    rt_device_t device;
    rt_err_t ret = -RT_ERROR;

    device = rt_device_find("I2C");
    if (device == RT_NULL)
    {
        i2c_err("Can't find I2C device.\n");
        return -RT_ERROR;
    }

    if(!wr)
    {
        // Write Action
        i2c_msg[0].addr=addr;
        i2c_msg[0].flags=RT_I2C_WR ;
        i2c_msg[0].len=2;
        i2c_data[0]=reg;
        i2c_data[1]=data;
        i2c_msg[0].buf=i2c_data;

        i2c_priv_data.msgs=i2c_msg;
        i2c_priv_data.number=1;

        if (rt_device_open(device, 0) == RT_EOK)
        {
            i2c_dbg("write msg addr=0x%x, len=%d, reg=0x%d,  data=0x%x\n",i2c_msg[0].addr, i2c_msg[0].len, i2c_data[0], i2c_data[1]);
            rt_device_control(device, RT_I2C_DEV_CTRL_RW, &i2c_priv_data);
            rt_device_close(device);
        }
        else
        {
            i2c_err("open:w err\n");
            goto l_err;
        }
    }
    else
    {
        // Read Action
        // Write reg addr msg
        i2c_msg[0].addr=addr;
        i2c_msg[0].flags=RT_I2C_WR;
        i2c_msg[0].len=1;
        i2c_data[0]=reg;
        i2c_msg[0].buf=i2c_data;
        // Read data msg
        i2c_msg[1].addr=addr;
        i2c_msg[1].flags=RT_I2C_RD;
        i2c_msg[1].len=1;
        i2c_data[1]=0;
        i2c_msg[1].buf=&i2c_data[1];

        i2c_priv_data.msgs=i2c_msg;
        i2c_priv_data.number=2;

        if (rt_device_open(device, 0) == RT_EOK)
        {
            rt_device_control(device, RT_I2C_DEV_CTRL_RW, &i2c_priv_data);
            i2c_dbg("read msg addr=0x%x, len=%d, addr=0x%d,  data=0x%x\n",i2c_msg[0].addr, i2c_msg[0].len, i2c_data[0], i2c_data[1]);
            rt_device_close(device);
        }
        else
        {

            i2c_err("open:r err\n");
            goto l_err;
        }
    }

l_err:
    return ret;
}
Beispiel #16
0
static void test_IO3V3_thread_entry(void* parameter)
{
    unsigned int count=0;
    int i,j,state,test_times=0, err_times=0;
    rt_device_t f_adc;

    f_adc = rt_device_find("ADC1");
    if (f_adc == RT_NULL)
    {
        rt_kprintf("Can't find ADC1 device.\n");
        return ;
    }
    if (rt_device_open(f_adc, 0) != RT_EOK)
    {
        rt_kprintf("Can't open ADC1 device.\n");
        return ;
    }

    A10_PWR_CTL(A10_PWRENUP);

    while (1)
    { 
//Key GPIOE_14, LED GPIOE_0
if ((!GETKEY2()) || (!test_ret)) //Key Down
{
    test_ret=0;
    test_times++;
    rt_kprintf("Test IO ADC....: Times=%d, err=%d\n", test_times, err_times);
    l_powerup:
    state=0;
    rt_kprintf("Test IO ADC....: power up state=%d and delay 1 min\n", state);
//Power UP
    A10_PWR_CTL(A10_PWRENUP);
// Delay 1 Min
    for(i=0; i<60; i++)
        rt_thread_delay(RT_TICK_PER_SECOND);

//Check IO is 3.3V
    if(check_io_voltage(f_adc) !=1)
    {
//If Not powerup state
        rt_kprintf("ERR: Not Powerup State\n");
        goto l_powerup;
    }    	

    state=1;
    rt_kprintf("Test IO ADC....: power off state=%d and delay 10 sec\n", state);
//Long powerkey shutdown sys
    A10_PWR_CTL(A10_PWRSHUTDOWN);
    rt_thread_delay(RT_TICK_PER_SECOND*2);

//Check IO is 0V
    if(check_io_voltage(f_adc) !=0)
    {
//If Not poweroff state
        rt_kprintf("ERR: Not Poweroff State\n");

        state=2;
        rt_kprintf("Test IO ADC....: bug state=%d\n", state);
        i=10;
        do
        {
            rt_thread_delay(RT_TICK_PER_SECOND);
//Check IO is 0.8-1.8V
            if(check_io_voltage(f_adc)!=-1)
            {
//If Not bug state
                rt_kprintf("ERR: Not Bug State\n");
                goto l_powerup;
            }

        }while(i--);

//OK. There is a BUG now.
        test_ret=1;
        err_times++;
    }
}
else
{
    check_io_voltage(f_adc);
}
rt_thread_delay(RT_TICK_PER_SECOND/2);

}
rt_device_close(f_adc);
}
Beispiel #17
0
static void test_suspend_thread_entry(void* parameter)
{
	    unsigned int count=0;
    int i,j,state,test_times=0, err_times=0;
    rt_device_t f_adc;

    f_adc = rt_device_find("ADC1");
    if (f_adc == RT_NULL)
    {
        rt_kprintf("Can't find ADC1 device.\n");
        return ;
    }
   if (rt_device_open(f_adc, 0) != RT_EOK)
    {
        rt_kprintf("Can't open ADC1 device.\n");
        return ;
    }

    A10_PWR_CTL(A10_PWRENUP);
    while(1)
    {

    	if ((!GETKEY2()) || (!test_ret)) //Key Down
    	{
    		test_ret=0;
    		test_times++;
    		rt_kprintf("Test suspend....: Times=%d, err=%d\n", test_times, err_times);
l_suspend:
    		state=0;
    		rt_kprintf("Test suspend....: suspend state=%d and delay 20 sec\n", state);
    		//To suspend
    		A10_PWR_CTL(A10_SUSPEND);
    		rt_thread_delay(RT_TICK_PER_SECOND*20);
    		//Check is suspend state.
    		if (Is_suspend())
    		{
    			//resume?
    			rt_kprintf("ERR: Not suspend State\n");
               //test_ret++;
                err_times++;
    			goto l_suspend;
    		}
            if(check_io_voltage(f_adc))
            {
                //resume?
                rt_kprintf("ERR: Not suspend State, There are voltage in 3V for A31S.\n");

                //goto l_suspend;
            }

    		// OK, now is suspend. Pls sleep 1 min
    		rt_kprintf("Test suspend....: Now is suspend state and delay 1 min\n");
    		rt_thread_delay(RT_TICK_PER_SECOND*20);
l_resume:
    		//To resume
    		rt_kprintf("Test suspend....: resume state=%d and delay 20 sec\n", state);
    		A10_PWR_CTL(A10_SUSPEND);
    		rt_thread_delay(RT_TICK_PER_SECOND*20);
    		//Check is resume state.
    		if (!Is_suspend())
    		{
    			//resume?
    			rt_kprintf("ERR: Not resume State\n");
                //test_ret++;
                err_times++;
    			goto l_resume;
    		}
    		// OK, now is resume. Pls sleep 1 min
    		rt_kprintf("Test resume....: Now is resume state and delay 1 min\n");
    		rt_thread_delay(RT_TICK_PER_SECOND*20);    		

    	}

		rt_thread_delay(RT_TICK_PER_SECOND/2);

    }
         rt_device_close(f_adc);
}
int test_spi_bus_register(void)
{
    rt_device_t spi_bus = RT_NULL;

#ifdef RT_USING_SPI1
    spi_bus = rt_device_find("spi1");
    if(spi_bus == RT_NULL) {
        return -RT_ERROR;
    }
    if(RT_EOK != rt_device_open(spi_bus, RT_DEVICE_OFLAG_RDWR)) {
        return -RT_ERROR;
    }
    if((spi_bus->write == RT_NULL) ||
       (spi_bus->close == RT_NULL) ||
       (spi_bus->open == RT_NULL) ||
       (spi_bus->read == RT_NULL) ||
       (spi_bus->init == RT_NULL)) {
        return -RT_ERROR;
    }
#if ((defined RT_USING_SPI1_TX_DMA) && (defined RT_USING_SPI1_RX_DMA))
    if(spi_bus->flag != (RT_DEVICE_FLAG_RDWR|
                         RT_DEVICE_FLAG_ACTIVATED|
                         RT_DEVICE_FLAG_DMA_RX|
                         RT_DEVICE_FLAG_DMA_TX)) {
        return -RT_ERROR;
    }
#elif (defined RT_USING_SPI1_TX_DMA)
    if(spi_bus->flag != (RT_DEVICE_FLAG_RDWR|
                         RT_DEVICE_FLAG_ACTIVATED|
                         RT_DEVICE_FLAG_INT_RX|
                         RT_DEVICE_FLAG_DMA_TX)) {
        return -RT_ERROR;
    }
#elif (defined RT_USING_SPI1_RX_DMA)
    if(spi_bus->flag != (RT_DEVICE_FLAG_RDWR|
                         RT_DEVICE_FLAG_ACTIVATED|
                         RT_DEVICE_FLAG_DMA_RX|
                         RT_DEVICE_FLAG_INT_TX)) {
        return -RT_ERROR;
    }
#else
    if(spi_bus->flag != (RT_DEVICE_FLAG_RDWR|
                         RT_DEVICE_FLAG_ACTIVATED|
                         RT_DEVICE_FLAG_INT_RX|
                         RT_DEVICE_FLAG_INT_TX)) {
        return -RT_ERROR;
    }
#endif /* defined(RT_USING_SPI1_TX_DMA)&&defined(RT_USING_SPI1_RX_DMA) */
    if(RT_EOK != rt_device_close(spi_bus)) {
        return -RT_ERROR;
    }
#endif /* RT_USING_SPI1 */


#ifdef RT_USING_SPI2
    spi_bus = rt_device_find("spi2");
    if(spi_bus == RT_NULL) {
        return -RT_ERROR;
    }
    if(RT_EOK != rt_device_open(spi_bus, RT_DEVICE_OFLAG_RDWR)) {
        return -RT_ERROR;
    }
    if((spi_bus->write == RT_NULL) ||
       (spi_bus->close == RT_NULL) ||
       (spi_bus->open == RT_NULL) ||
       (spi_bus->read == RT_NULL) ||
       (spi_bus->init == RT_NULL)) {
        return -RT_ERROR;
    }
#if ((defined RT_USING_SPI2_TX_DMA) && (defined RT_USING_SPI2_RX_DMA))
    if(spi_bus->flag != (RT_DEVICE_FLAG_RDWR|
                         RT_DEVICE_FLAG_ACTIVATED|
                         RT_DEVICE_FLAG_DMA_RX|
                         RT_DEVICE_FLAG_DMA_TX)) {
        return -RT_ERROR;
    }
#elif (defined RT_USING_SPI2_TX_DMA)
    if(spi_bus->flag != (RT_DEVICE_FLAG_RDWR|
                         RT_DEVICE_FLAG_ACTIVATED|
                         RT_DEVICE_FLAG_INT_RX|
                         RT_DEVICE_FLAG_DMA_TX)) {
        return -RT_ERROR;
    }
#elif (defined RT_USING_SPI2_RX_DMA)
    if(spi_bus->flag != (RT_DEVICE_FLAG_RDWR|
                         RT_DEVICE_FLAG_ACTIVATED|
                         RT_DEVICE_FLAG_DMA_RX|
                         RT_DEVICE_FLAG_INT_TX)) {
        return -RT_ERROR;
    }
#else
    if(spi_bus->flag != (RT_DEVICE_FLAG_RDWR|
                         RT_DEVICE_FLAG_ACTIVATED|
                         RT_DEVICE_FLAG_INT_RX|
                         RT_DEVICE_FLAG_INT_TX)) {
        return -RT_ERROR;
    }
#endif /* defined(RT_USING_SPI2_TX_DMA)&&defined(RT_USING_SPI2_RX_DMA) */
    if(RT_EOK != rt_device_close(spi_bus)) {
        return -RT_ERROR;
    }
#endif /* RT_USING_SPI2 */

    return RT_EOK;
}
int test_spi_bus_open_close(void)
{
    rt_device_t spi_bus = RT_NULL;

#ifdef RT_USING_SPI1
    spi_bus = rt_device_find("spi1");
    if(spi_bus == RT_NULL) {
        return -RT_ERROR;
    }
    if(RT_EOK != rt_device_open(spi_bus, RT_DEVICE_OFLAG_RDWR)) {
        return -RT_ERROR;
    }
    if(spi_bus->open_flag != (RT_DEVICE_OFLAG_OPEN | RT_DEVICE_OFLAG_RDWR)) {
        return -RT_ERROR;
    }
    if(spi_bus->ref_count != 1) {
        return -RT_ERROR;
    }
    if(RT_EOK != rt_device_open(spi_bus, RT_DEVICE_OFLAG_RDWR)) {
        return -RT_ERROR;
    }
    if(spi_bus->ref_count != 2) {
        return -RT_ERROR;
    }
    if(RT_EOK != rt_device_close(spi_bus)) {
        return -RT_ERROR;
    }
    if(spi_bus->ref_count != 1) {
        return -RT_ERROR;
    }
    if(spi_bus->open_flag == RT_DEVICE_OFLAG_CLOSE) {
        return -RT_ERROR;
    }
    if(RT_EOK != rt_device_close(spi_bus)) {
        return -RT_ERROR;
    }
    if(spi_bus->open_flag != RT_DEVICE_OFLAG_CLOSE) {
        return -RT_ERROR;
    }
    if(spi_bus->ref_count != 0) {
        return -RT_ERROR;
    }
#endif /* RT_USING_SPI1 */

#ifdef RT_USING_SPI2
    spi_bus = rt_device_find("spi2");
    if(spi_bus == RT_NULL) {
        return -RT_ERROR;
    }
    if(RT_EOK != rt_device_open(spi_bus, RT_DEVICE_OFLAG_RDWR)) {
        return -RT_ERROR;
    }
    if(spi_bus->open_flag != (RT_DEVICE_OFLAG_OPEN | RT_DEVICE_OFLAG_RDWR)) {
        return -RT_ERROR;
    }
    if(spi_bus->ref_count != 1) {
        return -RT_ERROR;
    }
    if(RT_EOK != rt_device_open(spi_bus, RT_DEVICE_OFLAG_RDWR)) {
        return -RT_ERROR;
    }
    if(spi_bus->ref_count != 2) {
        return -RT_ERROR;
    }
    if(RT_EOK != rt_device_close(spi_bus)) {
        return -RT_ERROR;
    }
    if(spi_bus->ref_count != 1) {
        return -RT_ERROR;
    }
    if(spi_bus->open_flag == RT_DEVICE_OFLAG_CLOSE) {
        return -RT_ERROR;
    }
    if(RT_EOK != rt_device_close(spi_bus)) {
        return -RT_ERROR;
    }
    if(spi_bus->open_flag != RT_DEVICE_OFLAG_CLOSE) {
        return -RT_ERROR;
    }
    if(spi_bus->ref_count != 0) {
        return -RT_ERROR;
    }
#endif /* RT_USING_SPI2 */

    return RT_EOK;
}
Beispiel #20
0
static rt_err_t _block_device_test(rt_device_t device)
{
    rt_err_t result;
    struct rt_device_blk_geometry geometry;
    rt_uint8_t * read_buffer  = RT_NULL;
    rt_uint8_t * write_buffer = RT_NULL;

    rt_kprintf("\r\n");

    if( (device->flag & RT_DEVICE_FLAG_RDWR) == RT_DEVICE_FLAG_RDWR )
    {
        // device can read and write.
        // step 1: open device
        result = rt_device_open(device,RT_DEVICE_FLAG_RDWR);
        if( result != RT_EOK )
        {
            return result;
        }

        // step 2: get device info
        rt_memset(&geometry, 0, sizeof(geometry));
        result = rt_device_control(device,
                                   RT_DEVICE_CTRL_BLK_GETGEOME,
                                   &geometry);
        if( result != RT_EOK )
        {
            rt_kprintf("device : %s cmd RT_DEVICE_CTRL_BLK_GETGEOME failed.\r\n");
            return result;
        }
        rt_kprintf("device info:\r\n");
        rt_kprintf("sector  size : %d byte\r\n", geometry.bytes_per_sector);
        rt_kprintf("sector count : %d \r\n", geometry.sector_count);
        rt_kprintf("block   size : %d byte\r\n", geometry.block_size);

        rt_kprintf("\r\n");
        read_buffer = rt_malloc(geometry.bytes_per_sector);
        if( read_buffer == RT_NULL )
        {
            rt_kprintf("no memory for read_buffer!\r\n");
            goto __return;
        }
        write_buffer = rt_malloc(geometry.bytes_per_sector);
        if( write_buffer == RT_NULL )
        {
            rt_kprintf("no memory for write_buffer!\r\n");
            goto __return;
        }

        /* step 3:  R/W test */
        {
            rt_uint32_t i, err_count, sector_no;
            rt_uint8_t * data_point;

            i = rt_device_read(device, 0, read_buffer, 1);
            if(i != 1)
            {
                rt_kprintf("read device :%s ", device->parent.name);
                rt_kprintf("the first sector failed.\r\n");
                goto __return;
            }

            data_point = write_buffer;
            for(i=0; i<geometry.bytes_per_sector; i++)
            {
                *data_point++ = (rt_uint8_t)i;
            }

            /* write first sector */
            sector_no = 0;
            data_point = write_buffer;
            *data_point++ = (rt_uint8_t)sector_no;
            i = rt_device_write(device, sector_no, write_buffer,1);
            if( i != 1 )
            {
                rt_kprintf("read the first sector success!\r\n");
                rt_kprintf("but write device :%s ", device->parent.name);
                rt_kprintf("the first sector failed.\r\n");
                rt_kprintf("maybe readonly!\r\n");
                goto __return;
            }

            /* write the second sector */
            sector_no = 1;
            data_point = write_buffer;
            *data_point++ = (rt_uint8_t)sector_no;
            i = rt_device_write(device,sector_no,write_buffer,1);
            if( i != 1 )
            {
                rt_kprintf("write device :%s ",device->parent.name);
                rt_kprintf("the second sector failed.\r\n");
                goto __return;
            }

            /* write the end sector */
            sector_no = geometry.sector_count-1;
            data_point = write_buffer;
            *data_point++ = (rt_uint8_t)sector_no;
            i = rt_device_write(device,sector_no,write_buffer,1);
            if( i != 1 )
            {
                rt_kprintf("write device :%s ",device->parent.name);
                rt_kprintf("the end sector failed.\r\n");
                goto __return;
            }

            /* verify first sector */
            sector_no = 0;
            i = rt_device_read(device,sector_no,read_buffer,1);
            if( i != 1 )
            {
                rt_kprintf("read device :%s ",device->parent.name);
                rt_kprintf("the first sector failed.\r\n");
                goto __return;
            }
            err_count = 0;
            data_point = read_buffer;
            if( (*data_point++) != (rt_uint8_t)sector_no)
            {
                err_count++;
            }
            for(i=1; i<geometry.bytes_per_sector; i++)
            {
                if( (*data_point++) != (rt_uint8_t)i )
                {
                    err_count++;
                }
            }
            if( err_count > 0 )
            {
                rt_kprintf("verify device :%s ",device->parent.name);
                rt_kprintf("the first sector failed.\r\n");
                goto __return;
            }

            /* verify sector sector */
            sector_no = 1;
            i = rt_device_read(device,sector_no,read_buffer,1);
            if( i != 1 )
            {
                rt_kprintf("read device :%s ",device->parent.name);
                rt_kprintf("the second sector failed.\r\n");
                goto __return;
            }
            err_count = 0;
            data_point = read_buffer;
            if( (*data_point++) != (rt_uint8_t)sector_no)
            {
                err_count++;
            }
            for(i=1; i<geometry.bytes_per_sector; i++)
            {
                if( (*data_point++) != (rt_uint8_t)i )
                {
                    err_count++;
                }
            }
            if( err_count > 0 )
            {
                rt_kprintf("verify device :%s ",device->parent.name);
                rt_kprintf("the second sector failed.\r\n");
                goto __return;
            }

            /* verify the end sector */
            sector_no = geometry.sector_count-1;
            i = rt_device_read(device,sector_no,read_buffer,1);
            if( i != 1 )
            {
                rt_kprintf("read device :%s ",device->parent.name);
                rt_kprintf("the end sector failed.\r\n");
                goto __return;
            }
            err_count = 0;
            data_point = read_buffer;
            if( (*data_point++) != (rt_uint8_t)sector_no)
            {
                err_count++;
            }
            for(i=1; i<geometry.bytes_per_sector; i++)
            {
                if( (*data_point++) != (rt_uint8_t)i )
                {
                    err_count++;
                }
            }
            if( err_count > 0 )
            {
                rt_kprintf("verify device :%s ",device->parent.name);
                rt_kprintf("the end sector failed.\r\n");
                goto __return;
            }
            rt_kprintf("device R/W test pass!\r\n");

        } /* step 3: I/O R/W test */

        rt_kprintf("\r\nRT_TICK_PER_SECOND:%d\r\n", RT_TICK_PER_SECOND);

        // step 4: continuous single sector speed test
        {
            rt_uint32_t tick_start,tick_end;
            rt_uint32_t i;

            rt_kprintf("\r\ncontinuous single sector speed test:\r\n");

            if( geometry.sector_count < 10 )
            {
                rt_kprintf("device sector_count < 10, speed test abort!\r\n");
            }
            else
            {
                unsigned int sector;

                // sign sector write
                rt_kprintf("write: ");
                sector = 0;
                tick_start = rt_tick_get();
                for(i=0; i<200; i++)
                {
                    sector += rt_device_write(device, i, read_buffer, 1);
                    if((i != 0) && ((i%4) == 0) )
                    {
                        if(sector < 4)
                        {
                            rt_kprintf("#");
                        }
                        else
                        {
                            rt_kprintf("<");
                        }
                        sector = 0;
                    }
                }
                tick_end = rt_tick_get();
                rt_kprintf("\r\nwrite 200 sector from %d to %d, ",tick_start,tick_end);
                calculate_speed_print( (geometry.bytes_per_sector*200UL*RT_TICK_PER_SECOND)/(tick_end-tick_start) );
                rt_kprintf("\r\n");

                // sign sector read
                rt_kprintf("read : ");
                sector = 0;
                tick_start = rt_tick_get();
                for(i=0; i<200; i++)
                {
                    sector += rt_device_read(device, i, read_buffer, 1);
                    if((i != 0) && ((i%4) == 0) )
                    {
                        if(sector < 4)
                        {
                            rt_kprintf("#");
                        }
                        else
                        {
                            rt_kprintf(">");
                        }
                        sector = 0;
                    }
                }
                tick_end = rt_tick_get();
                rt_kprintf("\r\nread 200 sector from %d to %d, ",tick_start,tick_end);
                calculate_speed_print( (geometry.bytes_per_sector*200UL*RT_TICK_PER_SECOND)/(tick_end-tick_start) );
                rt_kprintf("\r\n");
            }
        }// step 4: speed test

        // step 5: random single sector speed test
        {
            rt_uint32_t tick_start,tick_end;
            rt_uint32_t i;

            rt_kprintf("\r\nrandom single sector speed test:\r\n");

            if( geometry.sector_count < 10 )
            {
                rt_kprintf("device sector_count < 10, speed test abort!\r\n");
            }
            else
            {
                unsigned int sector;

                // sign sector write
                rt_kprintf("write: ");
                sector = 0;
                tick_start = rt_tick_get();
                for(i=0; i<200; i++)
                {
                    sector += rt_device_write(device, (geometry.sector_count / 10) * (i%10) + (i%10), read_buffer, 1);
                    if((i != 0) && ((i%4) == 0) )
                    {
                        if(sector < 4)
                        {
                            rt_kprintf("#");
                        }
                        else
                        {
                            rt_kprintf("<");
                        }
                        sector = 0;
                    }
                }
                tick_end = rt_tick_get();
                rt_kprintf("\r\nwrite 200 sector from %d to %d, ",tick_start,tick_end);
                calculate_speed_print( (geometry.bytes_per_sector*200UL*RT_TICK_PER_SECOND)/(tick_end-tick_start) );
                rt_kprintf("\r\n");

                // sign sector read
                rt_kprintf("read : ");
                sector = 0;
                tick_start = rt_tick_get();
                for(i=0; i<200; i++)
                {
                    sector += rt_device_read(device, (geometry.sector_count / 10) * (i%10) + (i%10), read_buffer, 1);
                    if((i != 0) && ((i%4) == 0) )
                    {
                        if(sector < 4)
                        {
                            rt_kprintf("#");
                        }
                        else
                        {
                            rt_kprintf(">");
                        }
                        sector = 0;
                    }
                }
                tick_end = rt_tick_get();
                rt_kprintf("\r\nread 200 sector from %d to %d, ",tick_start,tick_end);
                calculate_speed_print( (geometry.bytes_per_sector*200UL*RT_TICK_PER_SECOND)/(tick_end-tick_start) );
                rt_kprintf("\r\n");
            }
        }// step 4: speed test

        /* step 6: multiple sector speed test */
        {
            rt_uint8_t * multiple_buffer;
            rt_uint8_t * ptr;
            rt_uint32_t tick_start,tick_end;
            rt_uint32_t sector,i;

            rt_kprintf("\r\nmultiple sector speed test\r\n");

            for(sector=2; sector<256; sector=sector*2)
            {
                multiple_buffer = rt_malloc(geometry.bytes_per_sector * sector);

                if(multiple_buffer == RT_NULL)
                {
                    rt_kprintf("no memory for %d sector! multiple sector speed test abort!\r\n", sector);
                    break;
                }

                rt_memset(multiple_buffer, sector, geometry.bytes_per_sector * sector);
                rt_kprintf("write: ");
                tick_start = rt_tick_get();
                for(i=0; i<10; i++)
                {
                    rt_size_t n;
                    n = rt_device_write(device, 50, multiple_buffer, sector);
                    if(n == sector)
                    {
                        rt_kprintf("<");
                    }
                    else
                    {
                        rt_kprintf("#");
                    }
                }
                tick_end = rt_tick_get();
                rt_kprintf("\r\n");
                rt_kprintf("multiple write %d sector speed : ", sector);
                calculate_speed_print( (geometry.bytes_per_sector * sector * 10 * RT_TICK_PER_SECOND)/(tick_end-tick_start) );
                rt_kprintf("\r\n");

                rt_memset(multiple_buffer, ~sector, geometry.bytes_per_sector * sector);
                rt_kprintf("read : ");
                tick_start = rt_tick_get();
                for(i=0; i<10; i++)
                {
                    rt_size_t n;
                    n = rt_device_read(device, 50, multiple_buffer, sector);
                    if(n == sector)
                    {
                        rt_kprintf(">");
                    }
                    else
                    {
                        rt_kprintf("#");
                    }
                }
                tick_end = rt_tick_get();
                rt_kprintf("\r\n");
                rt_kprintf("multiple read %d sector speed : ", sector);
                calculate_speed_print( (geometry.bytes_per_sector * sector * 10 * RT_TICK_PER_SECOND)/(tick_end-tick_start) );

                ptr = multiple_buffer;
                for(i=0; i<geometry.bytes_per_sector * sector; i++)
                {
                    if(*ptr != sector)
                    {
                        rt_kprintf(" but data verify fail!");
                        break;
                    }
                    ptr++;
                }
                rt_kprintf("\r\n\r\n");

                rt_free(multiple_buffer);
            }
        } /* step 5: multiple sector speed test */

        rt_device_close(device);
        return RT_EOK;
    }// device can read and write.
    else
    {
        // device read only
        rt_device_close(device);
        return RT_EOK;
    }// device read only

__return:
    if( read_buffer != RT_NULL )
    {
        rt_free(read_buffer);
    }
    if( write_buffer != RT_NULL )
    {
        rt_free(write_buffer);
    }
    rt_device_close(device);
    return RT_ERROR;
}
Beispiel #21
0
int dfs_elm_mkfs(rt_device_t dev_id)
{
#define FSM_STATUS_INIT            0
#define FSM_STATUS_USE_TEMP_DRIVER 1
    FATFS *fat = RT_NULL;
    BYTE *work;
    int flag;
    FRESULT result;
    int index;

    work = rt_malloc(_MAX_SS);
    if(RT_NULL == work) {
        return -DFS_STATUS_ENOMEM;
    }

    if (dev_id == RT_NULL)
        return -DFS_STATUS_EINVAL;

    /* if the device is already mounted, then just do mkfs to the drv,
     * while if it is not mounted yet, then find an empty drive to do mkfs
     */

    flag = FSM_STATUS_INIT;
    index = get_disk(dev_id);
    if (index == -1)
    {
        /* not found the device id */
        index = get_disk(RT_NULL);
        if (index == -1)
        {
            /* no space to store an temp driver */
            rt_kprintf("sorry, there is no space to do mkfs! \n");
            return -DFS_STATUS_ENOSPC;
        }
        else
        {
            fat = rt_malloc(sizeof(FATFS));
            if (fat == RT_NULL)
                return -DFS_STATUS_ENOMEM;

            flag = FSM_STATUS_USE_TEMP_DRIVER;

            disk[index] = dev_id;
            /* try to open device */
            rt_device_open(dev_id, RT_DEVICE_OFLAG_RDWR);

            /* just fill the FatFs[vol] in ff.c, or mkfs will failded!
             * consider this condition: you just umount the elm fat,
             * then the space in FatFs[index] is released, and now do mkfs
             * on the disk, you will get a failure. so we need f_mount here,
             * just fill the FatFS[index] in elm fatfs to make mkfs work.
             */
            f_mount(fat, "", (BYTE)index);
        }
    }

    /* [IN] Logical drive number */
    /* [IN] Format options */
    /* [IN] Size of the allocation unit */
    /* [-]  Working buffer */
    /* [IN] Size of working buffer */
    result = f_mkfs("", FM_ANY, 0, work, _MAX_SS);
    rt_free(work);

    /* check flag status, we need clear the temp driver stored in disk[] */
    if (flag == FSM_STATUS_USE_TEMP_DRIVER)
    {
        rt_free(fat);
        f_mount(RT_NULL, "",(BYTE)index);
        disk[index] = RT_NULL;
        /* close device */
        rt_device_close(dev_id);
    }

    if (result != FR_OK)
    {
        rt_kprintf("format error\n");
        return elm_result_to_dfs(result);
    }

    return DFS_STATUS_OK;
}