基于RT-Thread的铁电芯片驱动
  zFo7n0gjfPLL 2023年11月19日 15 0
#include "fm25cl64.h"
#include "drv_spi.h"
/*
• function Name      : fm25cl64_erase
• description        : erase fm25cl64 memory area
• @param device      : point of the fm25cl64 device
• @param start_addr  : start address of the memory to erase
• @param end_addr    : end address of the memory to erase
• return             : RT_EOK
*/
rt_err_t fm25cl64_erase(struct rt_spi_device *device, rt_uint16_t start_addr, rt_uint16_t end_addr)
{
rt_uint8_t cmd[5] = {WREN, WRITE, start_addr >> 8, start_addr, WRDI};
rt_spi_send(device, &cmd[0], 1);
rt_spi_send_then_send(device, &cmd[1], 3, RT_NULL, end_addr - start_addr);
rt_spi_send(device, &cmd[4], 1);
return RT_EOK;
}
/*
• function Name     : fm25cl64_RDSR
• description       : read fm25cl64 status register
• @param    device  : point of the fm25cl64 device
• @param   *buffer  : point of the store buffer
• return            : RT_EOK
*/
rt_err_t fm25cl64_RDSR(struct rt_spi_device *device, void *buffer) //读 FM25CL64 状态寄存器
{
rt_uint8_t cmd = RDSR;
rt_spi_send_then_recv(device, &cmd, 1, buffer, 1);
return RT_EOK;
}
/*
• function Name     : fm25cl64_WRSR
• description       : write fm25cl64 status register
• @param  device    : point of the fm25cl64 device
• @param  *buffer   : point of the value buffer
• return            : RT_EOK
*/
rt_err_t fm25cl64_WRSR(struct rt_spi_device *device, void *buffer) //写 FM25CL64 状态寄存器
{
rt_uint8_t cmd = WREN;
rt_spi_send(device, &cmd, 1);
cmd = WRSR;
rt_spi_send_then_send(device, &cmd, 1, buffer, 1);
cmd = WRDI;
rt_spi_send(device, &cmd, 1);
return RT_EOK;
}
/*
• function Name     : fm25cl64_MemoryRead
• description       : read fm25cl64 memory area data
• @param  device    : point of the fm25cl64 device
• @param  addr      : address of the memory area
• @param  *data     : point of the store buffer
• @param  length    : nbytes to read
• return            : RT_EOK
*/
rt_err_t fm25cl64_MemoryRead(struct rt_spi_device *device, rt_uint16_t addr, void *data, rt_size_t length)
{
rt_uint8_t cmd[3] = {READ, addr >> 8, addr};
rt_spi_send_then_recv(device, cmd, 3, data, length);
return RT_EOK;
}
/*
• function Name     : fm25cl64_MemoryWrite
• description       : write fm25cl64 memory area data
• @param  device    : point of the fm25cl64 device
• @param  addr      : address of the memory area
• @param  *data     : point of the value write
• @param  length    : nbytes to write
• return            : RT_EOK
*/
rt_err_t fm25cl64_MemoryWrite(struct rt_spi_device *device, rt_uint16_t addr, void *data, rt_size_t length)
{
rt_uint8_t cmd[5] = {WREN, WRITE, addr >> 8, addr, WRDI};
rt_spi_send(device, &cmd[0], 1);
rt_spi_send_then_send(device, &cmd[1], 3, data, length);
rt_spi_send(device, &cmd[4], 1);
return RT_EOK;
}
/*
• function Name         : fm25cl64_init
• description           : Initialize the F_RAM device
• @param    device      : point of fm25cl64 device
• @param    cs_gpiox    : GPIO peripheral of the cs_pin
• @param    cs_gpio_pin : pin number of the cs_pin
• return    : the resault of initialization
*/
static int rt_hw_spi_fm25cl64_init(void)
{
rt_err_t res;
static struct rt_spi_device spi_dev_fm25;
static struct gd32_spi_cs  spi_cs;
spi_cs.GPIOx = FM25_SPI_CS_GPIOX;
spi_cs.GPIO_Pin = FM25_SPI_CS_GPIOX_PIN_X;//    rt_pin_mode(FRAM_CS, PIN_MODE_OUTPUT);
rcu_periph_clock_enable(RCU_GPIOE);
rcu_periph_clock_enable(RCU_SPI3);
/* SPI5_CLK(PG13), SPI5_MISO(PG12), SPI5_MOSI(PG14),SPI5_IO2(PG10) and SPI5_IO3(PG11) GPIO pin configuration */
    gpio_af_set(GPIOE, GPIO_AF_5, GPIO_PIN_2|GPIO_PIN_5| GPIO_PIN_6);
    gpio_mode_set(GPIOE, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_2|GPIO_PIN_5| GPIO_PIN_6);
    gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, GPIO_PIN_2|GPIO_PIN_5|GPIO_PIN_6);

gpio_mode_set(spi_cs.GPIOx, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, spi_cs.GPIO_Pin);
gpio_output_options_set(spi_cs.GPIOx, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, spi_cs.GPIO_Pin);

gpio_bit_set(spi_cs.GPIOx, spi_cs.GPIO_Pin);

res = rt_spi_bus_attach_device(&spi_dev_fm25, SPI_FRAM_DEVICE_NAME, SPI_FRAM_BUS_NAME, (void*)&spi_cs);

if (res != RT_EOK)
{
    rt_kprintf("rt_spi_bus_attach_device() run failed!\n");
    return res;
}

return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_hw_spi_fm25cl64_init);
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月19日 0

暂无评论

推荐阅读