You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
269 lines
6.9 KiB
C
269 lines
6.9 KiB
C
4 weeks ago
|
/*
|
||
|
* FRAMInterface.h
|
||
|
*
|
||
|
* Author: Aleksey Gerasimenko
|
||
|
* gerasimenko.aleksey.n@gmail.com
|
||
|
*/
|
||
|
|
||
|
#include <stdint.h>
|
||
|
|
||
|
|
||
|
#include "F28335/DSP28x_Project.h"
|
||
|
|
||
|
#include "DSP28335/GPIO.h"
|
||
|
#include "RUDRIVEFRAMEWORK/DataType.h"
|
||
|
#include "RUDRIVEFRAMEWORK/SystemDefinitions.h"
|
||
|
|
||
|
|
||
|
#ifndef PERIPHERY_FRAM_H_
|
||
|
#define PERIPHERY_FRAM_H_
|
||
|
|
||
|
|
||
|
|
||
|
#ifndef SPIA_GPIO_SETUP_DEFAULT_DEFINES
|
||
|
#define SPIA_GPIO_SETUP_DEFAULT (&DSP28335::GPIO::gpio_spia_setup)
|
||
|
#define SPIA_GPIO_SETUP_DEFAULT_DEFINES
|
||
|
#endif
|
||
|
|
||
|
#ifndef SPIA_GPIO_WRITE_PROTECT_SET_DEFINES
|
||
|
#define SPIA_GPIO_WRITE_PROTECT_SET (&DSP28335::GPIO::gpio_spia_write_protect_set)
|
||
|
#define SPIA_GPIO_WRITE_PROTECT_SET_DEFINES
|
||
|
#endif
|
||
|
//
|
||
|
#ifndef SPIA_GPIO_WRITE_PROTECT_CLEAR_DEFINES
|
||
|
#define SPIA_GPIO_WRITE_PROTECT_CLEAR (&DSP28335::GPIO::gpio_spia_write_protect_clear)
|
||
|
#define SPIA_GPIO_WRITE_PROTECT_CLEAR_DEFINES
|
||
|
#endif
|
||
|
//
|
||
|
#ifndef FRAM_OPCODE_WREN_DEFINES
|
||
|
#define FRAM_OPCODE_WREN (uint16_t)0x0600
|
||
|
#define FRAM_OPCODE_WREN_DEFINES
|
||
|
#endif
|
||
|
//
|
||
|
#ifndef FRAM_OPCODE_WRDI_DEFINES
|
||
|
#define FRAM_OPCODE_WRDI (uint16_t)0x0400
|
||
|
#define FRAM_OPCODE_WRDI_DEFINES
|
||
|
#endif
|
||
|
//
|
||
|
#ifndef FRAM_OPCODE_RDSR_DEFINES
|
||
|
#define FRAM_OPCODE_RDSR (uint16_t)0x0500
|
||
|
#define FRAM_OPCODE_RDSR_DEFINES
|
||
|
#endif
|
||
|
//
|
||
|
#ifndef FRAM_OPCODE_WRSR_DEFINES
|
||
|
#define FRAM_OPCODE_WRSR (uint16_t)0x0100
|
||
|
#define FRAM_OPCODE_WRSR_DEFINES
|
||
|
#endif
|
||
|
//
|
||
|
#ifndef FRAM_OPCODE_READ_DEFINES
|
||
|
#define FRAM_OPCODE_READ (uint16_t)0x0300
|
||
|
#define FRAM_OPCODE_READ_DEFINES
|
||
|
#endif
|
||
|
//
|
||
|
#ifndef FRAM_OPCODE_WRITE_DEFINES
|
||
|
#define FRAM_OPCODE_WRITE (uint16_t)0x0200
|
||
|
#define FRAM_OPCODE_WRITE_DEFINES
|
||
|
#endif
|
||
|
//
|
||
|
#ifndef FRAM_OPCODE_DUMMY_DEFINES
|
||
|
#define FRAM_OPCODE_DUMMY (uint16_t)0x5500
|
||
|
#define FRAM_OPCODE_DUMMY_DEFINES
|
||
|
#endif
|
||
|
//
|
||
|
#ifndef FRAM_OPCODE_ERASE_DEFINES
|
||
|
#define FRAM_OPCODE_ERASE (uint16_t)0xFF00
|
||
|
#define FRAM_OPCODE_ERASE_DEFINES
|
||
|
#endif
|
||
|
//
|
||
|
|
||
|
|
||
|
|
||
|
namespace PERIPHERY
|
||
|
{
|
||
|
|
||
|
|
||
|
struct FRAMSetup
|
||
|
{
|
||
|
pGPIO_FUNCTION gpio_setup;
|
||
|
pGPIO_FUNCTION write_protect_set;
|
||
|
pGPIO_FUNCTION write_protect_clear;
|
||
|
FRAMSetup():
|
||
|
gpio_setup(SPIA_GPIO_SETUP_DEFAULT),
|
||
|
write_protect_set(SPIA_GPIO_WRITE_PROTECT_SET),
|
||
|
write_protect_clear(SPIA_GPIO_WRITE_PROTECT_CLEAR)
|
||
|
{}
|
||
|
};//
|
||
|
|
||
|
|
||
|
struct FRAMDATAWordField
|
||
|
{
|
||
|
uint16_t low :8;
|
||
|
uint16_t high :8;
|
||
|
};//
|
||
|
|
||
|
union FRAMDATAWord
|
||
|
{
|
||
|
uint16_t all;
|
||
|
FRAMDATAWordField byte;
|
||
|
};//
|
||
|
|
||
|
|
||
|
struct FRAMDATALongWord
|
||
|
{
|
||
|
FRAMDATAWord wL;
|
||
|
FRAMDATAWord wH;
|
||
|
};//
|
||
|
|
||
|
|
||
|
|
||
|
union FRAMDataVariant
|
||
|
{
|
||
|
int16_t i16;
|
||
|
uint16_t u16;
|
||
|
int32_t i32;
|
||
|
uint32_t u32;
|
||
|
float f;
|
||
|
bool b;
|
||
|
FRAMDATALongWord lw;
|
||
|
FRAMDataVariant():
|
||
|
u32((uint32_t)0)
|
||
|
{}
|
||
|
};//
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
class FRAMInterface
|
||
|
{
|
||
|
public:
|
||
|
enum mode_t {WAIT, READ, WRITE, ERASE, VERIFY, READY, RESTORE};
|
||
|
protected:
|
||
|
mode_t m_mode;
|
||
|
private:
|
||
|
//Uint16 m_fifo_tx_status;
|
||
|
//Uint16 m_fifo_rx_status;
|
||
|
private:
|
||
|
Uint16 m_fifo_tx[16];
|
||
|
Uint16 m_fifo_rx[16];
|
||
|
private:
|
||
|
uint16_t *m_buffer_pointer;
|
||
|
uint16_t m_buffer_size;
|
||
|
uint16_t m_buffer_index;
|
||
|
uint16_t m_fram_start_addr;
|
||
|
uint16_t m_fram_addr;
|
||
|
uint16_t m_data_fram;
|
||
|
uint16_t m_data_buffer;
|
||
|
FRAMDataVariant m_data_variant;
|
||
|
bool m_verify_status;
|
||
|
bool *m_p_verify_status;
|
||
|
uint16_t m_delay;
|
||
|
void *m_destination;
|
||
|
public:
|
||
|
FRAMInterface();
|
||
|
void setup();
|
||
|
public:
|
||
|
FRAMInterface::mode_t get_mode();
|
||
|
bool compare_mode(FRAMInterface::mode_t mode);
|
||
|
public:
|
||
|
void break_fram();
|
||
|
void write_buffer (uint16_t addr, uint16_t *buffer_pointer, uint16_t buffer_size);
|
||
|
void read_buffer (uint16_t addr, uint16_t *buffer_pointer, uint16_t buffer_size);
|
||
|
void erase_buffer (uint16_t addr, uint16_t *buffer_pointer, uint16_t buffer_size);
|
||
|
void verify_buffer(uint16_t addr, uint16_t *buffer_pointer, uint16_t buffer_size, bool *verify_status);
|
||
|
public:
|
||
|
void write_slow_buffer (uint16_t addr, uint16_t *buffer_pointer, uint16_t buffer_size);
|
||
|
void read_slow_buffer (uint16_t addr, uint16_t *buffer_pointer, uint16_t buffer_size);
|
||
|
void erase_slow_buffer (uint16_t addr, uint16_t *buffer_pointer, uint16_t buffer_size);
|
||
|
public:
|
||
|
void write_int16 (uint16_t addr, int16_t data);
|
||
|
void write_uint16(uint16_t addr, uint16_t data);
|
||
|
void write_int32 (uint16_t addr, int32_t data);
|
||
|
void write_uint32(uint16_t addr, uint32_t data);
|
||
|
void write_float (uint16_t addr, float data);
|
||
|
void write_bool (uint16_t addr, bool data);
|
||
|
public:
|
||
|
void read_int16 (uint16_t addr, int16_t *destination);
|
||
|
void read_uint16(uint16_t addr, uint16_t *destination);
|
||
|
void read_int32 (uint16_t addr, int32_t *destination);
|
||
|
void read_uint32(uint16_t addr, uint32_t *destination);
|
||
|
void read_float (uint16_t addr, float *destination);
|
||
|
void read_bool (uint16_t addr, bool *destination);
|
||
|
private:
|
||
|
void (*_gpio_setup)();
|
||
|
public:
|
||
|
void set_wp();
|
||
|
void clear_wp();
|
||
|
private:
|
||
|
void (*_set_wp)();
|
||
|
void (*_clear_wp)();
|
||
|
public:
|
||
|
void execute();
|
||
|
private:
|
||
|
void (FRAMInterface::*_execute)();
|
||
|
void _execute_free();
|
||
|
void _execute_ready();
|
||
|
void _execute_read_buffer_get_data();
|
||
|
void _execute_read_status_register();
|
||
|
void _write_buffer();
|
||
|
void _read_buffer_send_opcode();
|
||
|
void _erase_buffer();
|
||
|
void _verify_buffer_send_opcode();
|
||
|
void _execute_verify_buffer_data();
|
||
|
inline void _break_fram();
|
||
|
private:
|
||
|
void _execute_ready_wren_write_buffer();
|
||
|
void _execute_ready_wren_erase_buffer();
|
||
|
void _execute_ready_write_buffer();
|
||
|
void _execute_ready_erase_buffer();
|
||
|
private:
|
||
|
void _execute_ready_wren_write_int16();
|
||
|
void _execute_ready_wren_write_uint16();
|
||
|
void _execute_ready_wren_write_int32();
|
||
|
void _execute_ready_wren_write_uint32();
|
||
|
void _execute_ready_wren_write_float();
|
||
|
void _execute_ready_wren_write_bool();
|
||
|
private:
|
||
|
void _execute_ready_write_int16();
|
||
|
void _execute_ready_write_uint16();
|
||
|
void _execute_ready_write_int32();
|
||
|
void _execute_ready_write_uint32();
|
||
|
void _execute_ready_write_float();
|
||
|
void _execute_ready_write_bool();
|
||
|
private:
|
||
|
void _execute_ready_read_buffer();
|
||
|
void _execute_ready_read_int16();
|
||
|
void _execute_ready_read_uint16();
|
||
|
void _execute_ready_read_int32();
|
||
|
void _execute_ready_read_uint32();
|
||
|
void _execute_ready_read_float();
|
||
|
void _execute_ready_read_bool();
|
||
|
private:
|
||
|
void _execute_write_register();
|
||
|
void _execute_write_16();
|
||
|
void _execute_ready_opcode_wren();
|
||
|
void _execute_ready_opcode_wrsr();
|
||
|
private:
|
||
|
inline void _prepare_execute(uint16_t addr, uint16_t *buffer_pointer, uint16_t buffer_size);
|
||
|
//
|
||
|
inline void _spi_opcode_wren();
|
||
|
inline void _spi_opcode_wrsr();
|
||
|
//
|
||
|
inline void _spi_write_16();
|
||
|
inline void _spi_erase_16();
|
||
|
inline void _spi_read_16();
|
||
|
//
|
||
|
inline void _spi_write_32();
|
||
|
inline void _spi_erase_32();
|
||
|
inline void _spi_read_32();
|
||
|
//
|
||
|
inline void _spi_get_read_data_16();
|
||
|
inline void _spi_get_read_data_32();
|
||
|
inline void _spi_get_status_register();
|
||
|
inline uint16_t _spi_get_fifo_tx_status();
|
||
|
//
|
||
|
};//
|
||
|
|
||
|
} /* namespace PERIPHERY */
|
||
|
|
||
|
#endif /* PERIPHERY_FRAM_H_ */
|