diff --git a/core/esp_spi.c b/core/esp_spi.c index 7a4c359..720da18 100644 --- a/core/esp_spi.c +++ b/core/esp_spi.c @@ -248,33 +248,33 @@ size_t spi_transfer(uint8_t bus, const void *out_data, void *in_data, size_t len static void _repeat_send(uint8_t bus, uint32_t* dword,int32_t* repeats,spi_word_size_t size) { - uint8_t i = 0 ; - while(*repeats > 0) - { - uint16_t bytes_to_transfer = __min(*repeats * size , _SPI_BUF_SIZE); - _wait(bus); - _set_size(bus,bytes_to_transfer); - for(i = 0; i < (bytes_to_transfer + 3) / 4;i++) - SPI(bus).W[i] = *dword; //need test with memcpy ! - _start(bus); - *repeats -= (bytes_to_transfer / size ) ; - } - _wait(bus); + uint8_t i = 0 ; + while(*repeats > 0) + { + uint16_t bytes_to_transfer = __min(*repeats * size , _SPI_BUF_SIZE); + _wait(bus); + _set_size(bus,bytes_to_transfer); + for(i = 0; i < (bytes_to_transfer + 3) / 4;i++) + SPI(bus).W[i] = *dword; //need test with memcpy ! + _start(bus); + *repeats -= (bytes_to_transfer / size ) ; + } + _wait(bus); } void spi_repeat_send_8(uint8_t bus, uint8_t data,int32_t repeats) { - uint32_t dword = data << 24 | data << 16 | data << 8 | data; - _repeat_send(bus,&dword,&repeats, SPI_8BIT); + uint32_t dword = data << 24 | data << 16 | data << 8 | data; + _repeat_send(bus,&dword,&repeats, SPI_8BIT); } void spi_repeat_send_16(uint8_t bus, uint16_t data,int32_t repeats) { - uint32_t dword = data << 16 | data; - _repeat_send(bus,&dword,&repeats, SPI_16BIT); + uint32_t dword = data << 16 | data; + _repeat_send(bus,&dword,&repeats, SPI_16BIT); } void spi_repeat_send_32(uint8_t bus, uint32_t data,int32_t repeats) { - _repeat_send(bus,&data,&repeats, SPI_32BIT); + _repeat_send(bus,&data,&repeats, SPI_32BIT); } diff --git a/core/include/esp/spi.h b/core/include/esp/spi.h index d825cb1..4b31144 100644 --- a/core/include/esp/spi.h +++ b/core/include/esp/spi.h @@ -186,7 +186,7 @@ void spi_set_frequency_div(uint8_t bus, uint32_t divider); inline uint32_t spi_get_frequency_div(uint8_t bus) { return (FIELD2VAL(SPI_CLOCK_DIV_PRE, SPI(bus).CLOCK) + 1) | - (FIELD2VAL(SPI_CLOCK_COUNT_NUM, SPI(bus).CLOCK) + 1); + (FIELD2VAL(SPI_CLOCK_COUNT_NUM, SPI(bus).CLOCK) + 1); } /** * \brief Get SPI bus frequency in Hz @@ -196,8 +196,8 @@ inline uint32_t spi_get_frequency_div(uint8_t bus) inline uint32_t spi_get_frequency_hz(uint8_t bus) { return APB_CLK_FREQ / - (FIELD2VAL(SPI_CLOCK_DIV_PRE, SPI(bus).CLOCK) + 1) / - (FIELD2VAL(SPI_CLOCK_COUNT_NUM, SPI(bus).CLOCK) + 1); + (FIELD2VAL(SPI_CLOCK_DIV_PRE, SPI(bus).CLOCK) + 1) / + (FIELD2VAL(SPI_CLOCK_COUNT_NUM, SPI(bus).CLOCK) + 1); } /** @@ -230,8 +230,8 @@ void spi_set_endianness(uint8_t bus, spi_endianness_t endianness); inline spi_endianness_t spi_get_endianness(uint8_t bus) { return SPI(bus).USER0 & (SPI_USER0_WR_BYTE_ORDER | SPI_USER0_RD_BYTE_ORDER) - ? SPI_BIG_ENDIAN - : SPI_LITTLE_ENDIAN; + ? SPI_BIG_ENDIAN + : SPI_LITTLE_ENDIAN; } /** @@ -291,12 +291,12 @@ size_t spi_transfer(uint8_t bus, const void *out_data, void *in_data, size_t len */ static inline void spi_set_command(uint8_t bus,uint8_t bits, uint16_t data) { - if(!bits) return ; - SPI(bus).USER0 |= SPI_USER0_COMMAND ; //enable COMMAND function in SPI module - uint16_t command = data << (16-bits); //align command data to high bits - command = ((command>>8)&0xff) | ((command<<8)&0xff00); //swap byte order - SPI(bus).USER2 = SET_FIELD(SPI(bus).USER2, SPI_USER2_COMMAND_BITLEN, --bits); - SPI(bus).USER2 = SET_FIELD(SPI(bus).USER2, SPI_USER2_COMMAND_VALUE, command); + if(!bits) return ; + SPI(bus).USER0 |= SPI_USER0_COMMAND ; //enable COMMAND function in SPI module + uint16_t command = data << (16-bits); //align command data to high bits + command = ((command>>8)&0xff) | ((command<<8)&0xff00); //swap byte order + SPI(bus).USER2 = SET_FIELD(SPI(bus).USER2, SPI_USER2_COMMAND_BITLEN, --bits); + SPI(bus).USER2 = SET_FIELD(SPI(bus).USER2, SPI_USER2_COMMAND_VALUE, command); } /** @@ -316,10 +316,10 @@ static inline void spi_set_command(uint8_t bus,uint8_t bits, uint16_t data) */ static inline void spi_set_address(uint8_t bus,uint8_t bits, uint32_t data) { - if(!bits) return ; - SPI(bus).USER1 = SET_FIELD(SPI(bus).USER1, SPI_USER1_ADDR_BITLEN, --bits); - SPI(bus).USER0 |= SPI_USER0_ADDR ; //enable ADDRess function in SPI module - SPI(bus).ADDR = data<<(32-bits) ; //align address data to high bits + if(!bits) return ; + SPI(bus).USER1 = SET_FIELD(SPI(bus).USER1, SPI_USER1_ADDR_BITLEN, --bits); + SPI(bus).USER0 |= SPI_USER0_ADDR ; //enable ADDRess function in SPI module + SPI(bus).ADDR = data<<(32-bits) ; //align address data to high bits } /** @@ -335,14 +335,14 @@ static inline void spi_set_address(uint8_t bus,uint8_t bits, uint32_t data) * * \param bus Bus ID: 0 - system, 1 - user * \param bits Number of bits - * \param pos Address to send for each transfert. + * \param pos Position of dummy bit, between Dout and Din if true. */ static inline void spi_set_dummy_bits(uint8_t bus, uint8_t bits, bool pos) { - if(!bits) return ; - if(pos) { SPI(bus).USER0 |= SPI_USER0_MISO; } // Dummy bit will be between Dout and Din data if set - SPI(bus).USER0 |= SPI_USER0_DUMMY; //enable dummy bits - SPI(bus).USER1 = SET_FIELD(SPI(bus).USER1, SPI_USER1_DUMMY_CYCLELEN, --bits); + if(!bits) return ; + if(pos) { SPI(bus).USER0 |= SPI_USER0_MISO; } // Dummy bit will be between Dout and Din data if set + SPI(bus).USER0 |= SPI_USER0_DUMMY; //enable dummy bits + SPI(bus).USER1 = SET_FIELD(SPI(bus).USER1, SPI_USER1_DUMMY_CYCLELEN, --bits); } /** @@ -351,7 +351,7 @@ static inline void spi_set_dummy_bits(uint8_t bus, uint8_t bits, bool pos) */ static inline void spi_clear_address(uint8_t bus) { - SPI(bus).USER0 &= ~(SPI_USER0_ADDR) ; + SPI(bus).USER0 &= ~(SPI_USER0_ADDR) ; } /** @@ -361,7 +361,7 @@ static inline void spi_clear_address(uint8_t bus) static inline void spi_clear_command(uint8_t bus) { - SPI(bus).USER0 &= ~(SPI_USER0_COMMAND) ; + SPI(bus).USER0 &= ~(SPI_USER0_COMMAND) ; } /** @@ -370,7 +370,7 @@ static inline void spi_clear_command(uint8_t bus) */ static inline void spi_clear_dummy(uint8_t bus) { - SPI(bus).USER0 &= ~(SPI_USER0_DUMMY | SPI_USER0_MISO ) ; + SPI(bus).USER0 &= ~(SPI_USER0_DUMMY | SPI_USER0_MISO); } /** diff --git a/examples/spi_test/spi_test.c b/examples/spi_test/spi_test.c index a746ba2..0baccc3 100644 --- a/examples/spi_test/spi_test.c +++ b/examples/spi_test/spi_test.c @@ -22,43 +22,43 @@ void loop(void *pvParameters) { - uint32_t time = 0 ; // SPI transmission time - float avr_time = 0 ; // Average of SPI transmission - float u = 0 ; + uint32_t time = 0 ; // SPI transmission time + float avr_time = 0 ; // Average of SPI transmission + float u = 0 ; - spi_init(1, SPI_MODE0, SPI_FREQ_DIV_1M, 1, SPI_LITTLE_ENDIAN, false); // init SPI module + spi_init(1, SPI_MODE0, SPI_FREQ_DIV_1M, 1, SPI_LITTLE_ENDIAN, false); // init SPI module - while(1) { + while(1) { - time = sdk_system_get_time(); + time = sdk_system_get_time(); - spi_set_command(1,1,1) ; // Set one command bit to 1 - spi_set_address(1,4,8) ; // Set 4 address bits to 8 - spi_set_dummy_bits(1,4,false); // Set 4 dummy bit before Dout + spi_set_command(1,1,1) ; // Set one command bit to 1 + spi_set_address(1,4,8) ; // Set 4 address bits to 8 + spi_set_dummy_bits(1,4,false); // Set 4 dummy bit before Dout - spi_repeat_send_16(1,0xC584,10); // Send 1 bit command + 4 bits address + 4 bits dummy + 160 bits data + spi_repeat_send_16(1,0xC584,10); // Send 1 bit command + 4 bits address + 4 bits dummy + 160 bits data - spi_clear_address(1); // remove address - spi_clear_command(1); // remove command - spi_clear_dummy(1); // remove dummy + spi_clear_address(1); // remove address + spi_clear_command(1); // remove command + spi_clear_dummy(1); // remove dummy - time = sdk_system_get_time() -time ; - avr_time = ((avr_time * (float)u ) + (float)time)/((float)u+1.0) ; // compute average - u++; - if (u==100) { - u=0 ; - printf("Time: %f\n",avr_time); - } - vTaskDelay(100/portTICK_PERIOD_MS); - } + time = sdk_system_get_time() -time ; + avr_time = ((avr_time * (float)u ) + (float)time)/((float)u+1.0) ; // compute average + u++; + if (u==100) { + u=0 ; + printf("Time: %f\n",avr_time); + } + vTaskDelay(100/portTICK_PERIOD_MS); + } } void user_init(void) { - uart_set_baud(0, 115200); - printf("SDK version:%s\n", sdk_system_get_sdk_version()); - xTaskCreate(loop, "loop", 1024, NULL, 2, NULL); + uart_set_baud(0, 115200); + printf("SDK version:%s\n", sdk_system_get_sdk_version()); + xTaskCreate(loop, "loop", 1024, NULL, 2, NULL); }