Reading SPI EEPROM on User Space Level

This forum is for users of Microchip MPUs and who are interested in using Linux OS.

Moderator: nferre

CleberPeter
Location: Brazil
Posts: 20
Joined: Tue May 14, 2019 7:57 pm

Reading SPI EEPROM on User Space Level

Wed May 27, 2020 11:05 pm

I'm using the 48LM01 memory connected to the spi_1 peripheral of sama5d27_som1 on a custom board.

I applied the following configuration to the device tree:

Code: Select all

spi1: spi@fc000000 {
	pinctrl-names = "default";
	pinctrl-0 = <&pinctrl_spi1 &pinctrl_spi1_cs_eeprom &pinctrl_spi1_cs_lcd &pinctrl_spi1_cs_rfid>;
	status = "okay";
				
	eeprom@0 {
		compatible = "microchip,48LM01", "atmel,at25";
		reg = <0>;  /* cs number */
		spi-max-frequency = <5000000>;
		pagesize = <128>;
		size = <131072>; /* 128KB */
		address-width = <24>;
	};
};

Added the at25 driver to the kernel:

Code: Select all

CONFIG_EEPROM_AT25=y

Memory is detected during boot:

Code: Select all

at25 spi0.0: 128 KByte at25 eeprom, pagesize 128

The cat and echo commands on /sys/bus/spi/drivers/at25/spi0.0/eeprom work perfectly.

But i cannot open file in a simple program in c:

Code: Select all

fd = open("/sys/bus/spi/drivers/at25/spi.0.0/eeprom", O_RDONLY);

With the code above fd always has a value of -1.



Does anyone have any idea why the file cannot be opened and if there is any way to access it through standard C functions ?
ylanz
Posts: 24
Joined: Mon May 05, 2014 12:57 pm

Re: Reading SPI EEPROM on User Space Level

Thu May 28, 2020 9:36 am

The -1 stands for permission error (EPERM), did you try to test your C program using sudo (if installed on your OS) ?
CleberPeter
Location: Brazil
Posts: 20
Joined: Tue May 14, 2019 7:57 pm

Re: Reading SPI EEPROM on User Space Level

Thu May 28, 2020 1:54 pm

Thank you for the answer.

The cat command reads the file perfectly without any additional permission, but to make sure I changed the permissions on the file to ensure that all users are allowed to read and write it. Same mistake :(

In my research I found several discussions about EEPROM readings both via spi (driver at25) and i2c (driver at24) result in the same difficulty, the commands cat and echo work, but open(), read() and write() do not.

Inspecting the implementation of the cat command(https://github.com/coreutils/coreutils/ ... /src/cat.c) I found that the function used by it is open(). Being that we are using the same function which would make cat work and open not?

Some forums report that access to eeprom must be done through a /dev/** directory which apparently is not provided by the driver at25.

I accept any suggestion ...
ylanz
Posts: 24
Joined: Mon May 05, 2014 12:57 pm

Re: Reading SPI EEPROM on User Space Level

Thu May 28, 2020 2:53 pm

Your path seems wrong:
fd = open("/sys/bus/spi/drivers/at25/spi.0.0/eeprom", O_RDONLY);
You added a "." between "spi" and "0.0"
CleberPeter
Location: Brazil
Posts: 20
Joined: Tue May 14, 2019 7:57 pm

Re: Reading SPI EEPROM on User Space Level

Thu May 28, 2020 4:52 pm

I would never have seen it, thank you very much.

That was the problem.
ylanz
Posts: 24
Joined: Mon May 05, 2014 12:57 pm

Re: Reading SPI EEPROM on User Space Level

Thu May 28, 2020 6:08 pm

Glad it works :)

Return to “LINUX”

Who is online

Users browsing this forum: Bing [Bot] and 7 guests