spi_slave: add HAL support
This commit is contained in:
committed by
bot
parent
595d702e97
commit
33db6d608e
@@ -20,4 +20,6 @@ This layer should depend on the operating system as little as possible. It's a w
|
||||
layer can combine basic steps into different working ways (polling, non-polling, interrupt, etc.). Without using
|
||||
queues/locks/delay/loop/etc., this layer can be easily port to other os or simulation systems.
|
||||
|
||||
To get better performance and better porting ability, ``context``s are used to hold sustainable data and pass the parameters.
|
||||
|
||||
To develop your own driver, it is suggested to copy the HAL layer to your own code and keep them until manual update.
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
* See readme.md in soc/include/hal/readme.md
|
||||
******************************************************************************/
|
||||
|
||||
// The HAL layer for SPI (common part)
|
||||
// The HAL layer for SPI master (common part)
|
||||
|
||||
// SPI HAL usages:
|
||||
// 1. initialize the bus
|
||||
@@ -53,7 +53,9 @@ typedef struct {
|
||||
* Context that should be maintained by both the driver and the HAL.
|
||||
*/
|
||||
typedef struct {
|
||||
/* configured by driver at initialization */
|
||||
/* configured by driver at initialization, don't touch */
|
||||
spi_dev_t *hw; ///< Beginning address of the peripheral registers.
|
||||
/* should be configured by driver at initialization */
|
||||
lldesc_t *dmadesc_tx; /**< Array of DMA descriptor used by the TX DMA.
|
||||
* The amount should be larger than dmadesc_n. The driver should ensure that
|
||||
* the data to be sent is shorter than the descriptors can hold.
|
||||
@@ -102,8 +104,6 @@ typedef struct {
|
||||
uint8_t *rcv_buffer; ///< Buffer to hold the receive data.
|
||||
spi_ll_io_mode_t io_mode; ///< IO mode of the master
|
||||
|
||||
/* auto generated at initialization, don't touch */
|
||||
spi_dev_t *hw; ///< Beginning address of the peripheral registers.
|
||||
} spi_hal_context_t;
|
||||
|
||||
/**
|
||||
|
||||
@@ -60,7 +60,7 @@ typedef enum {
|
||||
*
|
||||
* @param hw Beginning address of the peripheral registers.
|
||||
*/
|
||||
static inline void spi_ll_init(spi_dev_t *hw)
|
||||
static inline void spi_ll_master_init(spi_dev_t *hw)
|
||||
{
|
||||
//Reset DMA
|
||||
hw->dma_conf.val |= SPI_LL_RST_MASK;
|
||||
@@ -70,7 +70,36 @@ static inline void spi_ll_init(spi_dev_t *hw)
|
||||
//Reset timing
|
||||
hw->ctrl2.val = 0;
|
||||
|
||||
//master use all 64 bytes of the buffer
|
||||
//use all 64 bytes of the buffer
|
||||
hw->user.usr_miso_highpart = 0;
|
||||
hw->user.usr_mosi_highpart = 0;
|
||||
|
||||
//Disable unneeded ints
|
||||
hw->slave.val &= ~SPI_LL_UNUSED_INT_MASK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize SPI peripheral (slave).
|
||||
*
|
||||
* @param hw Beginning address of the peripheral registers.
|
||||
*/
|
||||
static inline void spi_ll_slave_init(spi_dev_t *hw)
|
||||
{
|
||||
//Configure slave
|
||||
hw->clock.val = 0;
|
||||
hw->user.val = 0;
|
||||
hw->ctrl.val = 0;
|
||||
hw->slave.wr_rd_buf_en = 1; //no sure if needed
|
||||
hw->user.doutdin = 1; //we only support full duplex
|
||||
hw->user.sio = 0;
|
||||
hw->slave.slave_mode = 1;
|
||||
hw->dma_conf.val |= SPI_LL_RST_MASK;
|
||||
hw->dma_out_link.start = 0;
|
||||
hw->dma_in_link.start = 0;
|
||||
hw->dma_conf.val &= ~SPI_LL_RST_MASK;
|
||||
hw->slave.sync_reset = 1;
|
||||
hw->slave.sync_reset = 0;
|
||||
//use all 64 bytes of the buffer
|
||||
hw->user.usr_miso_highpart = 0;
|
||||
hw->user.usr_mosi_highpart = 0;
|
||||
|
||||
@@ -296,6 +325,69 @@ static inline void spi_ll_master_set_mode(spi_dev_t *hw, uint8_t mode)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set SPI mode for the peripheral as slave.
|
||||
*
|
||||
* @param hw Beginning address of the peripheral registers.
|
||||
* @param mode SPI mode to work at, 0-3.
|
||||
*/
|
||||
static inline void spi_ll_slave_set_mode(spi_dev_t *hw, const int mode, bool dma_used)
|
||||
{
|
||||
if (mode == 0) {
|
||||
//The timing needs to be fixed to meet the requirements of DMA
|
||||
hw->pin.ck_idle_edge = 1;
|
||||
hw->user.ck_i_edge = 0;
|
||||
hw->ctrl2.miso_delay_mode = 0;
|
||||
hw->ctrl2.miso_delay_num = 0;
|
||||
hw->ctrl2.mosi_delay_mode = 2;
|
||||
hw->ctrl2.mosi_delay_num = 2;
|
||||
} else if (mode == 1) {
|
||||
hw->pin.ck_idle_edge = 1;
|
||||
hw->user.ck_i_edge = 1;
|
||||
hw->ctrl2.miso_delay_mode = 2;
|
||||
hw->ctrl2.miso_delay_num = 0;
|
||||
hw->ctrl2.mosi_delay_mode = 0;
|
||||
hw->ctrl2.mosi_delay_num = 0;
|
||||
} else if (mode == 2) {
|
||||
//The timing needs to be fixed to meet the requirements of DMA
|
||||
hw->pin.ck_idle_edge = 0;
|
||||
hw->user.ck_i_edge = 1;
|
||||
hw->ctrl2.miso_delay_mode = 0;
|
||||
hw->ctrl2.miso_delay_num = 0;
|
||||
hw->ctrl2.mosi_delay_mode = 1;
|
||||
hw->ctrl2.mosi_delay_num = 2;
|
||||
} else if (mode == 3) {
|
||||
hw->pin.ck_idle_edge = 0;
|
||||
hw->user.ck_i_edge = 0;
|
||||
hw->ctrl2.miso_delay_mode = 1;
|
||||
hw->ctrl2.miso_delay_num = 0;
|
||||
hw->ctrl2.mosi_delay_mode = 0;
|
||||
hw->ctrl2.mosi_delay_num = 0;
|
||||
}
|
||||
|
||||
/* Silicon issues exists in mode 0 and 2 with DMA, change clock phase to
|
||||
* avoid dma issue. This will cause slave output to appear at most half a
|
||||
* spi clock before
|
||||
*/
|
||||
if (dma_used) {
|
||||
if (mode == 0) {
|
||||
hw->pin.ck_idle_edge = 0;
|
||||
hw->user.ck_i_edge = 1;
|
||||
hw->ctrl2.miso_delay_mode = 0;
|
||||
hw->ctrl2.miso_delay_num = 2;
|
||||
hw->ctrl2.mosi_delay_mode = 0;
|
||||
hw->ctrl2.mosi_delay_num = 3;
|
||||
} else if (mode == 2) {
|
||||
hw->pin.ck_idle_edge = 1;
|
||||
hw->user.ck_i_edge = 0;
|
||||
hw->ctrl2.miso_delay_mode = 0;
|
||||
hw->ctrl2.miso_delay_num = 2;
|
||||
hw->ctrl2.mosi_delay_mode = 0;
|
||||
hw->ctrl2.mosi_delay_num = 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set SPI to work in full duplex or half duplex mode.
|
||||
*
|
||||
@@ -587,7 +679,7 @@ static inline void spi_ll_master_set_cs_setup(spi_dev_t *hw, uint8_t setup)
|
||||
* Configs: data
|
||||
*----------------------------------------------------------------------------*/
|
||||
/**
|
||||
* Set the input length.
|
||||
* Set the input length (master).
|
||||
*
|
||||
* @param hw Beginning address of the peripheral registers.
|
||||
* @param bitlen input length, in bits.
|
||||
@@ -598,7 +690,7 @@ static inline void spi_ll_set_miso_bitlen(spi_dev_t *hw, size_t bitlen)
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the output length.
|
||||
* Set the output length (master).
|
||||
*
|
||||
* @param hw Beginning address of the peripheral registers.
|
||||
* @param bitlen output length, in bits.
|
||||
@@ -608,6 +700,28 @@ static inline void spi_ll_set_mosi_bitlen(spi_dev_t *hw, size_t bitlen)
|
||||
hw->mosi_dlen.usr_mosi_dbitlen = bitlen - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the maximum input length (slave).
|
||||
*
|
||||
* @param hw Beginning address of the peripheral registers.
|
||||
* @param bitlen input length, in bits.
|
||||
*/
|
||||
static inline void spi_ll_slave_set_rx_bitlen(spi_dev_t *hw, size_t bitlen)
|
||||
{
|
||||
hw->slv_wrbuf_dlen.bit_len = bitlen - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the maximum output length (slave).
|
||||
*
|
||||
* @param hw Beginning address of the peripheral registers.
|
||||
* @param bitlen output length, in bits.
|
||||
*/
|
||||
static inline void spi_ll_slave_set_tx_bitlen(spi_dev_t *hw, size_t bitlen)
|
||||
{
|
||||
hw->slv_rdbuf_dlen.bit_len = bitlen - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the length of command phase.
|
||||
*
|
||||
@@ -722,6 +836,29 @@ static inline void spi_ll_enable_mosi(spi_dev_t *hw, int enable)
|
||||
hw->user.usr_mosi = enable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the slave peripheral before next transaction.
|
||||
*
|
||||
* @param hw Beginning address of the peripheral registers.
|
||||
*/
|
||||
static inline void spi_ll_slave_reset(spi_dev_t *hw)
|
||||
{
|
||||
hw->slave.sync_reset = 1;
|
||||
hw->slave.sync_reset = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the received bit length of the slave.
|
||||
*
|
||||
* @param hw Beginning address of the peripheral registers.
|
||||
*
|
||||
* @return Received bits of the slave.
|
||||
*/
|
||||
static inline uint32_t spi_ll_slave_get_rcv_bitlen(spi_dev_t *hw)
|
||||
{
|
||||
return hw->slv_rd_bit.slv_rdata_bit;
|
||||
}
|
||||
|
||||
|
||||
#undef SPI_LL_RST_MASK
|
||||
#undef SPI_LL_UNUSED_INT_MASK
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
/*******************************************************************************
|
||||
* NOTICE
|
||||
* The hal is not public api, don't use in application code.
|
||||
* See readme.md in soc/include/hal/readme.md
|
||||
******************************************************************************/
|
||||
|
||||
// The HAL layer for SPI slave (common part)
|
||||
|
||||
// SPI slave HAL usages:
|
||||
// 1. initialize the bus
|
||||
// 2. initialize the DMA descriptors if DMA used
|
||||
// 3. call setup_device to update parameters for the device
|
||||
// 4. prepare data to send, and prepare the receiving buffer
|
||||
// 5. trigger user defined SPI transaction to start
|
||||
// 6. wait until the user transaction is done
|
||||
// 7. store the received data and get the length
|
||||
// 8. check and reset the DMA (if needed) before the next transaction
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "soc/lldesc.h"
|
||||
#include "soc/spi_struct.h"
|
||||
#include <esp_types.h>
|
||||
|
||||
/**
|
||||
* Context that should be maintained by both the driver and the HAL.
|
||||
*/
|
||||
typedef struct {
|
||||
/* configured by driver at initialization, don't touch */
|
||||
spi_dev_t *hw; ///< Beginning address of the peripheral registers.
|
||||
/* should be configured by driver at initialization */
|
||||
lldesc_t *dmadesc_rx; /**< Array of DMA descriptor used by the TX DMA.
|
||||
* The amount should be larger than dmadesc_n. The driver should ensure that
|
||||
* the data to be sent is shorter than the descriptors can hold.
|
||||
*/
|
||||
lldesc_t *dmadesc_tx; /**< Array of DMA descriptor used by the RX DMA.
|
||||
* The amount should be larger than dmadesc_n. The driver should ensure that
|
||||
* the data to be sent is shorter than the descriptors can hold.
|
||||
*/
|
||||
int dmadesc_n; ///< The amount of descriptors of both ``dmadesc_tx`` and ``dmadesc_rx`` that the HAL can use.
|
||||
|
||||
/*
|
||||
* configurations to be filled after ``spi_slave_hal_init``. Updated to
|
||||
* peripheral registers when ``spi_slave_hal_setup_device`` is called.
|
||||
*/
|
||||
struct {
|
||||
uint32_t rx_lsbfirst : 1;
|
||||
uint32_t tx_lsbfirst : 1;
|
||||
uint32_t use_dma : 1;
|
||||
};
|
||||
int mode;
|
||||
|
||||
/*
|
||||
* Transaction specific (data), all these parameters will be updated to the
|
||||
* peripheral every transaction.
|
||||
*/
|
||||
uint32_t bitlen; ///< Expected maximum length of the transaction, in bits.
|
||||
const void *tx_buffer; ///< Data to be sent
|
||||
void *rx_buffer; ///< Buffer to hold the received data.
|
||||
|
||||
/* Other transaction result after one transaction */
|
||||
uint32_t rcv_bitlen; ///< Length of the last transaction, in bits.
|
||||
} spi_slave_hal_context_t;
|
||||
|
||||
/**
|
||||
* Init the peripheral and the context.
|
||||
*
|
||||
* @param hal Context of the HAL layer.
|
||||
* @param host_id Index of the SPI peripheral. 0 for SPI1, 1 for HSPI (SPI2) and 2 for VSPI (SPI3).
|
||||
*/
|
||||
void spi_slave_hal_init(spi_slave_hal_context_t *hal, int host_id);
|
||||
|
||||
/**
|
||||
* Deinit the peripheral (and the context if needed).
|
||||
*
|
||||
* @param hal Context of the HAL layer.
|
||||
*/
|
||||
void spi_slave_hal_deinit(spi_slave_hal_context_t *hal);
|
||||
|
||||
/**
|
||||
* Setup device-related configurations according to the settings in the context.
|
||||
*
|
||||
* @param hal Context of the HAL layer.
|
||||
*/
|
||||
void spi_slave_hal_setup_device(const spi_slave_hal_context_t *hal);
|
||||
|
||||
/**
|
||||
* Prepare the data for the current transaction.
|
||||
*
|
||||
* @param hal Context of the HAL layer.
|
||||
*/
|
||||
void spi_slave_hal_prepare_data(const spi_slave_hal_context_t *hal);
|
||||
|
||||
/**
|
||||
* Trigger start a user-defined transaction.
|
||||
*
|
||||
* @param hal Context of the HAL layer.
|
||||
*/
|
||||
void spi_slave_hal_user_start(const spi_slave_hal_context_t *hal);
|
||||
|
||||
/**
|
||||
* Check whether the transaction is done (trans_done is set).
|
||||
*
|
||||
* @param hal Context of the HAL layer.
|
||||
*/
|
||||
bool spi_slave_hal_usr_is_done(spi_slave_hal_context_t* hal);
|
||||
|
||||
/**
|
||||
* Post transaction operations, fetch data from the buffer and recored the length.
|
||||
*
|
||||
* @param hal Context of the HAL layer.
|
||||
*/
|
||||
void spi_slave_hal_store_result(spi_slave_hal_context_t *hal);
|
||||
|
||||
/**
|
||||
* Get the length of last transaction, in bits. Should be called after ``spi_slave_hal_store_result``.
|
||||
*
|
||||
* Note that if last transaction is longer than configured before, the return
|
||||
* value will be truncated to the configured length.
|
||||
*
|
||||
* @param hal Context of the HAL layer.
|
||||
*
|
||||
* @return Length of the last transaction, in bits.
|
||||
*/
|
||||
uint32_t spi_slave_hal_get_rcv_bitlen(spi_slave_hal_context_t *hal);
|
||||
|
||||
/**
|
||||
* Check whether we need to reset the DMA according to the status of last transactions.
|
||||
*
|
||||
* In ESP32, sometimes we may need to reset the DMA for the slave before the
|
||||
* next transaction. Call this to check it.
|
||||
*
|
||||
* @param hal Context of the HAL layer.
|
||||
*
|
||||
* @return true if reset is needed, else false.
|
||||
*/
|
||||
bool spi_slave_hal_dma_need_reset(const spi_slave_hal_context_t *hal);
|
||||
Reference in New Issue
Block a user