Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need SPI documentation #31

Open
ghost opened this issue Sep 11, 2017 · 4 comments
Open

Need SPI documentation #31

ghost opened this issue Sep 11, 2017 · 4 comments

Comments

@ghost
Copy link

ghost commented Sep 11, 2017

I'm trying to code up an spi-read function. I guess it needs to be implemented using the spi-send function but there is no documentation on how the function works. Also I would like to know how to set the spi speed correctly for the spi-init function.

If you can either provide me with an spi-read function or point me at some documentation so I can code one myself I would appreciate it.

@zeroflag
Copy link
Owner

zeroflag commented Sep 11, 2017

The spi functions are based on the spi.c from esp-open-rtos.

There is a thin wrapper:

https://github.com/zeroflag/punyforth/blob/feab7a0fea1c815a5dc441085251fdea44c7089e/arch/esp8266/rtos/user/forth_spi.c

And the Forth words are here:

defprimitive "spi-init",8,spi_init,REGULAR

There isn't any user friendly documentation yet, but you can check the stack usage.

For example spi-send8 is defined as follows:

defprimitive "spi-send8",9,spi_send8,REGULAR
    DPOP a2                    // bus
    DPOP a3                    // data
    CCALL forth_spi_send8
    DPUSH a2
    NEXT

So the stack effect should be

: spi-send8 ( data bus -- result )

I think you're right saying that the read should be implemented by using the send. But I didn't need to use read so far.

There are some examples of using spi-init and send in

https://github.com/zeroflag/punyforth/blob/master/arch/esp8266/forth/ssd1306-spi.forth

@ghost
Copy link
Author

ghost commented Sep 11, 2017 via email

@zeroflag
Copy link
Owner

Here is the documentation of spi_transfer from https://github.com/SuperHouse/esp-open-rtos/blob/61c3d509e5b930c6c1b269ceff99e2d1f60d2010/core/include/esp/spi.h#L275

 * \param bus Bus ID: 0 - system, 1 - user
 * \param out_data Data to send.
 * \param in_data Receive buffer. If NULL, received data will be lost.
 * \param len Buffer size in words
 * \param word_size Size of the word
 * \return Transmitted/received words count
 */
size_t spi_transfer(uint8_t bus, const void *out_data, void *in_data, size_t len, spi_word_size_t word_size);

So the out_data buffer contains the data to be sent and the other one is the receive buffer. I didn't need the receive buffer when I used this (I just passed 0) so I don't have more information on this beside what's in the C comment.

This may help regarding the spi-get-freq-div constant.

/**
 * Macro for use with spi_init and spi_set_frequency_div.
 * SPI frequency = 80000000 / divider / count
 * dvider must be in 1..8192 and count in 1..64
 */
#define SPI_GET_FREQ_DIV(divider, count) (((count) << 16) | ((divider) & 0xffff))

/**
 * Predefinded SPI frequency dividers
 */
#define SPI_FREQ_DIV_125K SPI_GET_FREQ_DIV(64, 10) ///< 125kHz
#define SPI_FREQ_DIV_250K SPI_GET_FREQ_DIV(32, 10) ///< 250kHz
#define SPI_FREQ_DIV_500K SPI_GET_FREQ_DIV(16, 10) ///< 500kHz
#define SPI_FREQ_DIV_1M   SPI_GET_FREQ_DIV(8, 10)  ///< 1MHz
#define SPI_FREQ_DIV_2M   SPI_GET_FREQ_DIV(4, 10)  ///< 2MHz
#define SPI_FREQ_DIV_4M   SPI_GET_FREQ_DIV(2, 10)  ///< 4MHz
#define SPI_FREQ_DIV_8M   SPI_GET_FREQ_DIV(5,  2)  ///< 8MHz
#define SPI_FREQ_DIV_10M  SPI_GET_FREQ_DIV(4,  2)  ///< 10MHz
#define SPI_FREQ_DIV_20M  SPI_GET_FREQ_DIV(2,  2)  ///< 20MHz
#define SPI_FREQ_DIV_40M  SPI_GET_FREQ_DIV(1,  2)  ///< 40MHz
#define SPI_FREQ_DIV_80M  SPI_GET_FREQ_DIV(1,  1)  ///< 80MHz

@ghost
Copy link
Author

ghost commented Sep 11, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant