g0b1vetx-board/middleware/sfud/port/sfud_port.c
2023-04-29 09:09:12 +08:00

183 lines
4.6 KiB
C

/*
* This file is part of the Serial Flash Universal Driver Library.
*
* Copyright (c) 2018, zylx, <qgyhd1234@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* 'Software'), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Function: Portable interface for each platform.
* Created on: 2018-11-23
*/
#include <sfud.h>
#include <stdarg.h>
#include <stdio.h>
#include "main.h"
#include <string.h>
#include "FreeRTOS.h"
void sfud_log_info (const char *format, ...);
sfud_err qspi_send_then_recv (const void *send_buf, size_t send_length, void *recv_buf, size_t recv_length);
extern SPI_HandleTypeDef hspi2;
typedef struct
{
SPI_HandleTypeDef *spix;
GPIO_TypeDef *cs_gpiox;
uint16_t cs_gpio_pin;
} spi_user_data, *spi_user_data_t;
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)
{
portENTER_CRITICAL();
//__disable_irq();
}
static void spi_unlock (const sfud_spi *spi)
{
portEXIT_CRITICAL();
//__enable_irq();
}
/**
* 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)
{
sfud_err result = SFUD_SUCCESS;
//int index = 0;
spi_user_data_t spi_dev = (spi_user_data_t) spi->user_data;
uint8_t send_data, read_data;
if (write_size)
{
SFUD_ASSERT (write_buf);
}
if (read_size)
{
SFUD_ASSERT (read_buf);
}
/* reset cs pin */
if (spi_dev->cs_gpiox != NULL)
HAL_GPIO_WritePin (spi_dev->cs_gpiox, spi_dev->cs_gpio_pin, GPIO_PIN_RESET);
for (int i = 0; i < write_size + read_size; i++)
{
if (i < write_size)
{
send_data = *write_buf++;
}
else
{
send_data = SFUD_DUMMY_DATA;
}
HAL_SPI_TransmitReceive (&hspi2, &send_data, &read_data, 1, 1000);
if (i >= write_size)
{
*read_buf++ = read_data;
}
}
/* set cs pin */
if (spi_dev->cs_gpiox != NULL)
HAL_GPIO_WritePin (spi_dev->cs_gpiox, spi_dev->cs_gpio_pin, GPIO_PIN_SET);
return result;
}
/* about 100 microsecond delay */
static void retry_delay_100us (void)
{
uint32_t delay = 2400;
while (delay--);
}
static spi_user_data spi1 = { .spix = &hspi2, .cs_gpiox = SPI2_NSS_GPIO_Port, .cs_gpio_pin = SPI2_NSS_Pin };
sfud_err sfud_spi_port_init (sfud_flash *flash)
{
sfud_err result = SFUD_SUCCESS;
switch (flash->index)
{
case SFUD_W25Q_DEVICE_INDEX:
{
/* set the interfaces and data */
flash->spi.wr = spi_write_read;
// flash->spi.qspi_read = qspi_read;
flash->spi.lock = spi_lock;
flash->spi.unlock = spi_unlock;
flash->spi.user_data = &spi1;
/* about 100 microsecond delay */
flash->retry.delay = retry_delay_100us;
/* adout 60 seconds timeout */
flash->retry.times = 60 * 10000;
break;
}
}
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);
printf ("[SFUD](%s:%ld) ", file, line);
/* must use vprintf to print */
vsnprintf (log_buf, sizeof (log_buf), format, args);
printf ("%s\r\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);
printf ("[SFUD]");
/* must use vprintf to print */
vsnprintf (log_buf, sizeof (log_buf), format, args);
printf ("%s\r\n", log_buf);
va_end (args);
}