PULP Community
Sending data to flash only sends 8 Bit - Printable Version

+- PULP Community (https://pulp-platform.org/community)
+-- Forum: PULP's Community forum (https://pulp-platform.org/community/forumdisplay.php?fid=1)
+--- Forum: PULP General questions (https://pulp-platform.org/community/forumdisplay.php?fid=2)
+--- Thread: Sending data to flash only sends 8 Bit (/showthread.php?tid=171)



Sending data to flash only sends 8 Bit - LPLA - 02-13-2020

Hello,

i tried sending data to a flash (namely S25FS256S) in Pulpissimo and looked at the send example, as well as an example that writes an array of chars and then reads & prints them.
Now i tried to adjust the code so that i can write integer into the flash.
But for some reason i can never write more than 8 bit.

Here is my code:

Code:
#include <stdio.h>
#include <rt/rt_api.h>
#include <stdint.h>
#include <stdarg.h>

#define BUFFER_SIZE 4

typedef struct {
   rt_spim_t *device;
   int *tx_buffer;
   int *rx_buffer;
} spi_flash_t;

rt_spim_conf_t* spi_flash_init_cfg(){
 rt_spim_conf_t *conf = malloc(sizeof(rt_spim_conf_t));
 rt_spim_conf_init(conf);
 conf->max_baudrate = 1000000;
 conf->id = 0;
 conf->cs = 0;
 conf->phase = 1;
 conf->polarity = 1;
 conf->big_endian = 1;
 conf->wordsize = RT_SPIM_WORDSIZE_32;
 return conf;
}

spi_flash_t* spi_flash_open(rt_spim_conf_t *conf) {
 rt_spim_t *device = rt_spim_open(NULL, conf, NULL);
 spi_flash_t *flash = malloc(sizeof(spi_flash_t));
 flash->device = device;
 flash->tx_buffer = rt_alloc(RT_ALLOC_PERIPH, BUFFER_SIZE);
 flash->rx_buffer = rt_alloc(RT_ALLOC_PERIPH, BUFFER_SIZE);
 return flash;
}

void spi_flash_write_enable(spi_flash_t *flash){
 flash->tx_buffer[0] = 0x06;
 rt_spim_send(flash->device, flash->tx_buffer, 8, RT_SPIM_CS_AUTO, NULL);
}

void spi_flash_close(spi_flash_t *flash){
 rt_spim_close(flash->device, NULL);
}

char spi_flash_read_sr1(spi_flash_t *flash){
 flash->tx_buffer[0] = 0x05;
 rt_spim_send(flash->device, flash->tx_buffer, 8, RT_SPIM_CS_KEEP, NULL);
 rt_spim_receive(flash->device, flash->rx_buffer, 8, RT_SPIM_CS_AUTO, NULL);
 return flash->rx_buffer[0];
}

void spi_flash_wait_finish(spi_flash_t *flash){
 while(spi_flash_read_sr1(flash) & 0x1){
     // active wait for WIP going down to 0
 }
}

void spi_flash_write_test1(spi_flash_t *flash){
 flash->tx_buffer[0] = 0x02;
 rt_spim_send(flash->device, flash->tx_buffer, 32, RT_SPIM_CS_KEEP, NULL);
 flash->tx_buffer[0] = 0x00FFEEDD;
 rt_spim_send(flash->device, flash->tx_buffer, BUFFER_SIZE*8, RT_SPIM_CS_AUTO, NULL);
 // 000000DD
 // 000000EE
 // 000000FF
 // 00000000
}

void spi_flash_write_test2(spi_flash_t *flash){
 flash->tx_buffer[0] = 0x02;
 rt_spim_send(flash->device, flash->tx_buffer, 32, RT_SPIM_CS_KEEP, NULL);
 flash->tx_buffer[0] = 0x00FFEEDD;
 flash->tx_buffer[1] = 0xCCBBAA99;
 flash->tx_buffer[2] = 0x88776655;
 flash->tx_buffer[3] = 0x44332211;
 rt_spim_send(flash->device, flash->tx_buffer, BUFFER_SIZE*8*4, RT_SPIM_CS_AUTO, NULL);
 // 000000DD
 // 000000EE
 // 000000FF
 // 00000000
 // 00000099
 // 000000AA
 // 000000BB
 // 000000CC
 // 00000055
 // 00000066
 // 00000077
 // 00000088
 // 00000011
 // 00000022
 // 00000033
 // 00000044
}

void spi_flash_write_test3(spi_flash_t *flash){
 flash->tx_buffer[0] = 0x02;
 rt_spim_send(flash->device, flash->tx_buffer, 32, RT_SPIM_CS_KEEP, NULL);
 flash->tx_buffer[0] = 0xDD;
 flash->tx_buffer[1] = 0xEE;
 flash->tx_buffer[2] = 0xFF;
 flash->tx_buffer[3] = 0x00;
 rt_spim_send(flash->device, flash->tx_buffer, BUFFER_SIZE*8, RT_SPIM_CS_AUTO, NULL);
 // 000000DD
 // 00000000
 // 00000000
 // 00000000
}

void spi_flash_write_test4(spi_flash_t *flash){
 flash->tx_buffer[0] = 0x02;
 rt_spim_send(flash->device, flash->tx_buffer, 32, RT_SPIM_CS_KEEP, NULL);
 flash->tx_buffer[0] = 0xDD;
 flash->tx_buffer[1] = 0xEE;
 flash->tx_buffer[2] = 0xFF;
 flash->tx_buffer[3] = 0x00;
 rt_spim_send(flash->device, flash->tx_buffer, 8, RT_SPIM_CS_AUTO, NULL);
 // 000000DD
}

int main()
{
 rt_spim_conf_t *conf = spi_flash_init_cfg();

 spi_flash_t *flash = spi_flash_open(conf);

 spi_flash_write_enable(flash);

//  spi_flash_write_test1(flash);
//  spi_flash_write_test2(flash);
//  spi_flash_write_test3(flash);
 spi_flash_write_test4(flash);

 spi_flash_wait_finish(flash);

 spi_flash_close(flash);

 return 0;
}
You can see the data in the registers under each function.
I tried many more functions and settings but never was i able to write 32 bit.
Does anyone know what i am doing wrong?

Thank you,
LPLA

Also: Is there a way to highlight code syntax in this forum?