• AVR Freaks

Hot!Basic SPI Lib

Author
_edx_
New Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2016/09/21 11:56:28
  • Location: 0
  • Status: offline
2019/10/25 16:05:28 (permalink)
0

Basic SPI Lib

Hi there, it's me again,
As before i noticed that there're too much issues using this protocol, so i made a basic (write and read) SPI harmony compatible library.
 
What you can do:
Write & Read (Useful for R&W external FLASH & EEPROM memories)
Write (Useful for communicating with modules that doesn't send )
FastRead (Useful for reading data above 10 Mhz)
 
Prototypes:
void spi_write(SPI_MODULE_ID index, uint8_t data);
void spi_fast_read(SPI_MODULE_ID index, uint8_t *write_buffer, size_t write_num, uint8_t *read_buffer, uint32_t read_num);
void spi_write_read(SPI_MODULE_ID index, uint8_t *write_buffer, size_t write_num, uint8_t *read_buffer, uint32_t read_num);

 
How to use it:
Configure the harmony as always, select spi driver and configure it according to you needs (setup_spi.png)
 
Writing and Reading to/from external flash memory (IS25LP032D)
 
/*Enable Write*/
 
static const uint8_t Enable[] = { WREN };
// spi_write_read(SPI_ID_3,Enable,sizeof(Enable),NULL,0);
 
/*Writing*/
 
static const uint8_t datos[] = { PP, 0x00,0x00,0x00, 0xDA,0xED,0xAE,0xEA,WRDI };
// spi_write_read(SPI_ID_3,datos,sizeof(datos),NULL,0);
/*Reading*/
static const uint8_t leer[] = { NORD,0x00,0x00,0x00 };
static uint8_t rx_buffer[4];
spi_write_read(SPI_ID_3,leer,sizeof(leer),rx_buffer,sizeof(rx_buffer));

 
Logic analyzer output: spi_flash.png
Mplabx simulation output: mplab_wr.png
 
Reading data @ 25 Mhz from external flash (IS25LP032D)
 
/*When use fast read you must control de CS line*/
PLIB_PORTS_PinClear(PORTS_ID_0,PORT_CHANNEL_D, PORTS_BIT_POS_9);
spi_fast_read(SPI_ID_3,dato,sizeof(dato),rx_buffer, sizeof(rx_buffer));
PLIB_PORTS_PinSet(PORTS_ID_0,PORT_CHANNEL_D, PORTS_BIT_POS_9);

 
Logic analyzer output: spi_fastread.png
 
Reading data from microSdcard (Sandisk 8Gb)
 
/*You must provide the FS*/
FS_mount(fs);
FS_open(fs,"file.txt");
//saving data into array
FS_read(fs,rx_buffer,sizeof(rxbuffer));
for(k=0;k!=sizeof(rx_buffer);k++){
spi_write(SPI_ID_3, rx_buffer[k]); } //writing data to display

 
Logic analyzer ouput: spi_sdcard.png
 
Due to the limit of 5 files you should download the library from my webpage.
So this is all for now any comments or suggestion please don't hesistate to contact me,
And as always do not forget to check out my website for more pic related projects: https://edx-twin3.org
 
Regards :1
post edited by _edx_ - 2019/10/26 17:08:33

Attached Image(s)

#1
_edx_
New Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2016/09/21 11:56:28
  • Location: 0
  • Status: offline
Re: Basic SPI Lib 2019/10/25 16:08:52 (permalink)
0
I don't know why but won't let me put more than one URL, so anyways, also i've an I2C Library too (https://www.microchip.com/forums/m1078279.aspx)
 
post edited by _edx_ - 2019/10/27 14:18:47
#2
Jump to:
© 2020 APG vNext Commercial Version 4.5