|
发表于 2016-9-29 09:14:41
|
显示全部楼层
#include <sfud.h>
#include <stdarg.h>
#include <rtthread.h>
#include <drivers/spi.h>
typedef struct {
struct rt_semaphore lock;
struct rt_spi_device * rt_spi_device;
} spi_user_data, *spi_user_data_t;
static spi_user_data spi2 = { .rt_spi_device = 0 };
static char log_buf[256];
void sfud_log_debug(const char *file, const long line, const char *format, ...);
static void spi_lock(const sfud_spi *spi) {
spi_user_data_t spi_dev = (spi_user_data_t) spi->user_data;
rt_sem_take(&spi_dev->lock, RT_WAITING_FOREVER);
}
static void spi_unlock(const sfud_spi *spi) {
spi_user_data_t spi_dev = (spi_user_data_t) spi->user_data;
rt_sem_release(&spi_dev->lock);
}
static void retry_delay_ms(void) {
/* millisecond delay */
rt_tick_from_millisecond(1);
}
static void retry_delay_100us(void) {
/* 100 microsecond delay */
rt_thread_delay((RT_TICK_PER_SECOND * 1 + 9999) / 10000);
}
/**
* SPI write data then read data
*/
static sfud_err spi_write_read(const sfud_spi *spi, const uint8_t *write_buf, size_t write_size, uint8_t *read_buf,
size_t read_size) {
struct rt_spi_device * rt_spi_device;
sfud_err result = SFUD_SUCCESS;
spi_user_data_t spi_dev = (spi_user_data_t) spi->user_data;
rt_spi_send_then_recv(spi_dev->rt_spi_device, write_buf,write_size, read_buf, read_size);
return result;
}
sfud_err sfud_spi_port_init(sfud_flash *flash) {
sfud_err result = SFUD_SUCCESS;
struct rt_spi_device * rt_spi_device;
rt_spi_device = (struct rt_spi_device *)rt_device_find("spi20");
if(rt_spi_device == RT_NULL)
{
rt_kprintf("spi device %s not found!\r\n", "spi20");
return -RT_ENOSYS;
}
spi2.rt_spi_device = rt_spi_device;
/* config spi */
{
struct rt_spi_configuration cfg;
cfg.data_width = 8;
cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible: Mode 0 and Mode 3 */
cfg.max_hz = 50 * 1000 * 1000; /* 50M */
rt_spi_configure(spi2.rt_spi_device, &cfg);
}
flash->spi.wr = spi_write_read;
flash->spi.lock = spi_lock;
flash->spi.unlock = spi_unlock;
flash->spi.user_data = &spi2;
/* 100 microsecond delay */
flash->retry.delay = retry_delay_100us;
/* 60 seconds timeout */
flash->retry.times = 60 * 10000;
rt_sem_init(&spi2.lock, "spi2 lock", 1, RT_IPC_FLAG_PRIO);
return result;
}
/**
* This function is print debug info.
*
* @param file the file which has call this function
* @param line the line number which has call this function
* @param format output format
* @param ... args
*/
void sfud_log_debug(const char *file, const long line, const char *format, ...) {
va_list args;
/* args point to the first variable parameter */
va_start(args, format);
rt_kprintf("[SFUD](%s:%ld) ", file, line);
/* must use vprintf to print */
vsnprintf(log_buf, sizeof(log_buf), format, args);
rt_kprintf("%s\n", log_buf);
va_end(args);
}
/**
* This function is print routine info.
*
* @param format output format
* @param ... args
*/
void sfud_log_info(const char *format, ...) {
va_list args;
/* args point to the first variable parameter */
va_start(args, format);
rt_kprintf("[SFUD]");
/* must use vprintf to print */
vsnprintf(log_buf, sizeof(log_buf), format, args);
rt_kprintf("%s\n", log_buf);
va_end(args);
} |
|