drv_spi.c 18.4 KB
Newer Older
1
/*
2
 * Copyright (c) 2006-2018, RT-Thread Development Team
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
 *
 * Change Logs:
 * Date           Author       Notes
 * 2017-06-05     tanek        first implementation.
 */
 
#include "drv_spi.h"

#include <board.h>
#include <finsh.h>

//#define DEBUG

#ifdef DEBUG
#define DEBUG_PRINTF(...)   rt_kprintf(__VA_ARGS__)
#else
#define DEBUG_PRINTF(...)   
#endif

/* private rt-thread spi ops function */
static rt_err_t configure(struct rt_spi_device* device, struct rt_spi_configuration* configuration);
static rt_uint32_t xfer(struct rt_spi_device* device, struct rt_spi_message* message);

static struct rt_spi_ops stm32_spi_ops =
{
    configure,
    xfer
};

#ifdef SPI_USE_DMA
static uint8_t dummy = 0xFF;
static void DMA_RxConfiguration(struct rt_spi_bus * spi_bus,
                                struct rt_spi_message* message)
{
    struct stm32f4_spi *f4_spi = (struct stm32f4_spi *)spi_bus->parent.user_data;
    
    DMA_HandleTypeDef * hdma_tx = &f4_spi->hdma_tx;
    DMA_HandleTypeDef * hdma_rx = &f4_spi->hdma_rx;
     
    HAL_DMA_DeInit(hdma_tx);
    HAL_DMA_DeInit(hdma_rx);
        
    /* Check if the DMA Stream is disabled before enabling it.
       Note that this step is useful when the same Stream is used multiple times:
       enabled, then disabled then re-enabled... In this case, the DMA Stream disable
       will be effective only at the end of the ongoing data transfer and it will
       not be possible to re-configure it before making sure that the Enable bit
       has been cleared by hardware. If the Stream is used only once, this step might
       be bypassed. */    
    while (hdma_tx->Instance->CR & DMA_SxCR_EN);
    while (hdma_rx->Instance->CR & DMA_SxCR_EN);

    if(message->recv_buf != RT_NULL)
    {
        hdma_rx->Init.MemInc = DMA_MINC_ENABLE;
    }
    else
    {
        message->recv_buf = &dummy;
        hdma_rx->Init.MemInc = DMA_MINC_DISABLE;
    }
    HAL_DMA_Init(hdma_rx);
    
    __HAL_LINKDMA(&f4_spi->spi_handle, hdmarx, f4_spi->hdma_rx);
        
    if(message->send_buf != RT_NULL)
    {
        hdma_tx->Init.MemInc = DMA_MINC_ENABLE;
    }
    else
    {
        dummy = 0xFF;
        message->send_buf = &dummy;
        hdma_tx->Init.MemInc = DMA_MINC_DISABLE;
    }
    HAL_DMA_Init(hdma_tx);
    
    __HAL_LINKDMA(&f4_spi->spi_handle, hdmatx, f4_spi->hdma_tx);
    
    /* NVIC configuration for DMA transfer complete interrupt*/
    HAL_NVIC_SetPriority(f4_spi->hdma_tx_irq, 0, 1);
    HAL_NVIC_EnableIRQ(f4_spi->hdma_tx_irq);

    /* NVIC configuration for DMA transfer complete interrupt*/
    HAL_NVIC_SetPriority(f4_spi->hdma_rx_irq, 0, 0);   
    HAL_NVIC_EnableIRQ(f4_spi->hdma_rx_irq);
    
}
#endif

static rt_err_t configure(struct rt_spi_device* device,
                          struct rt_spi_configuration* configuration)
{
    struct rt_spi_bus * spi_bus = (struct rt_spi_bus *)device->bus;	
    struct stm32f4_spi *f4_spi = (struct stm32f4_spi *)spi_bus->parent.user_data;
	SPI_HandleTypeDef * SpiHandle = &f4_spi->spi_handle;

	RT_ASSERT(device != RT_NULL);
	RT_ASSERT(configuration != RT_NULL);

    /* data_width */
    if(configuration->data_width <= 8)
    {
        SpiHandle->Init.DataSize = SPI_DATASIZE_8BIT;
    }
    else if(configuration->data_width <= 16)
    {
        SpiHandle->Init.DataSize = SPI_DATASIZE_16BIT;
    }
    else
    {
        return RT_EIO;
    }

    /* baudrate */
    {
        uint32_t SPI_APB_CLOCK;
        uint32_t max_hz;

        max_hz = configuration->max_hz;

        DEBUG_PRINTF("sys   freq: %d\n", HAL_RCC_GetSysClockFreq());
        DEBUG_PRINTF("pclk2 freq: %d\n", HAL_RCC_GetPCLK2Freq());

        SPI_APB_CLOCK = HAL_RCC_GetPCLK2Freq();

        if(max_hz >= SPI_APB_CLOCK/2)
        {
            SpiHandle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
        }
        else if(max_hz >= SPI_APB_CLOCK/4)
        {
            SpiHandle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
        }
        else if(max_hz >= SPI_APB_CLOCK/8)
        {
            SpiHandle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
        }
        else if(max_hz >= SPI_APB_CLOCK/16)
        {
            SpiHandle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
        }
        else if(max_hz >= SPI_APB_CLOCK/32)
        {
            SpiHandle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
        }
        else if(max_hz >= SPI_APB_CLOCK/64)
        {
            SpiHandle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
        }
        else if(max_hz >= SPI_APB_CLOCK/128)
        {
            SpiHandle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;
        }
        else
        {
            /*  min prescaler 256 */
            SpiHandle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
        }
    } /* baudrate */

    /* CPOL */
    if(configuration->mode & RT_SPI_CPOL)
    {
        SpiHandle->Init.CLKPolarity = SPI_POLARITY_HIGH;
    }
    else
    {
        SpiHandle->Init.CLKPolarity = SPI_POLARITY_LOW;
    }
    /* CPHA */
    if(configuration->mode & RT_SPI_CPHA)
    {
		SpiHandle->Init.CLKPhase = SPI_PHASE_2EDGE;
    }
    else
    {
		SpiHandle->Init.CLKPhase = SPI_PHASE_1EDGE;
    }
    /* MSB or LSB */
    if(configuration->mode & RT_SPI_MSB)
    {
        SpiHandle->Init.FirstBit = SPI_FIRSTBIT_MSB;
    }
    else
    {
        SpiHandle->Init.FirstBit = SPI_FIRSTBIT_LSB;
    }
    SpiHandle->Init.Direction = SPI_DIRECTION_2LINES;
    SpiHandle->Init.Mode = SPI_MODE_MASTER;
    SpiHandle->Init.NSS  = SPI_NSS_SOFT;
	SpiHandle->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
	SpiHandle->Init.TIMode = SPI_TIMODE_DISABLE;

    /* init SPI */
    if (HAL_SPI_Init(SpiHandle) != HAL_OK)
	{
		return RT_ERROR;
	}
    /* Enable SPI_MASTER */
	__HAL_SPI_ENABLE(SpiHandle);
    
    DEBUG_PRINTF("spi configuration\n");

    return RT_EOK;
};

static rt_uint32_t xfer(struct rt_spi_device* device, struct rt_spi_message* message)
{
    struct rt_spi_bus * stm32_spi_bus = (struct rt_spi_bus *)device->bus;
    struct stm32f4_spi *f4_spi = (struct stm32f4_spi *)stm32_spi_bus->parent.user_data;
    struct rt_spi_configuration * config = &device->config;
    SPI_TypeDef * SPI = f4_spi->spi_handle.Instance;
    struct stm32_spi_cs * stm32_spi_cs = device->parent.user_data;
    rt_uint32_t size = message->length;

	RT_ASSERT(device != NULL);
	RT_ASSERT(message != NULL);
	
    /* take CS */
    if(message->cs_take)
    {
        HAL_GPIO_WritePin(stm32_spi_cs->GPIOx, stm32_spi_cs->GPIO_Pin, GPIO_PIN_RESET);
    }

#ifdef SPI_USE_DMA
    if(message->length > 32)
    {
        if(config->data_width <= 8)
        {
            HAL_StatusTypeDef state; 
            DEBUG_PRINTF("spi dma transfer start\n");
            DMA_RxConfiguration(stm32_spi_bus, message);
            DEBUG_PRINTF("dma configuration finish , send buf %X, rec buf %X, length: %d\n", 
                    (uint32_t)message->send_buf, (uint32_t)message->recv_buf, message->length);
            
            state = HAL_SPI_TransmitReceive_DMA(&f4_spi->spi_handle, 
                                                (uint8_t*)message->send_buf, 
                                                (uint8_t*)message->recv_buf, 
                                                message->length);
            if (state != HAL_OK) 
            {
                DEBUG_PRINTF("spi flash configuration error : %d\n", state);
                message->length = 0;
                //while(1);
            }
            else
            {
                DEBUG_PRINTF("spi dma transfer finish\n");            
            }
            
            while (HAL_SPI_GetState(&f4_spi->spi_handle) != HAL_SPI_STATE_READY);
            DEBUG_PRINTF("spi get state finish\n");
        }
        else
        {
            // Todo 
        }
    }
    else
#endif
    {
        if(config->data_width <= 8)
        {
            const rt_uint8_t * send_ptr = message->send_buf;
            rt_uint8_t * recv_ptr = message->recv_buf;

            while(size--)
            {
                rt_uint8_t data = 0xFF;

                if(send_ptr != RT_NULL)
                {
                    data = *send_ptr++;
                }
                
                // Todo: replace register read/write by stm32f4 lib
                //Wait until the transmit buffer is empty
                while ((SPI->SR & SPI_FLAG_TXE) == RESET);
                // Send the byte
				SPI->DR = data;

                //Wait until a data is received
                while ((SPI->SR & SPI_FLAG_RXNE) == RESET);
                // Get the received data
                data = SPI->DR;

                if(recv_ptr != RT_NULL)
                {
                    *recv_ptr++ = data;
                }
            }
        }
        else if(config->data_width <= 16)
        {
            const rt_uint16_t * send_ptr = message->send_buf;
            rt_uint16_t * recv_ptr = message->recv_buf;

            while(size--)
            {
                rt_uint16_t data = 0xFF;

                if(send_ptr != RT_NULL)
                {
                    data = *send_ptr++;
                }

                //Wait until the transmit buffer is empty
                while ((SPI->SR & SPI_FLAG_TXE) == RESET);
                // Send the byte
                SPI->DR = data;

                //Wait until a data is received
                while ((SPI->SR & SPI_FLAG_RXNE) == RESET);
                // Get the received data
                data = SPI->DR;

                if(recv_ptr != RT_NULL)
                {
                    *recv_ptr++ = data;
                }
            }
        }
    }

    /* release CS */
    if(message->cs_release)
    {
        //GPIO_SetBits(stm32_spi_cs->GPIOx, stm32_spi_cs->GPIO_Pin);
		HAL_GPIO_WritePin(stm32_spi_cs->GPIOx, stm32_spi_cs->GPIO_Pin, GPIO_PIN_SET);
    }

    return message->length;
};





#ifdef RT_USING_SPI1

static struct stm32f4_spi stm32f4_spi1 = 
{
    /* .spi_handle = */{
        /* .Instance = */ SPI1,
    },
    /* .hdma_rx = */ {   
        DMA2_Stream2,
        DMA_CHANNEL_3,
    },
    /* .hdma_rx_irq = */ DMA2_Stream2_IRQn,

    /* .hdma_tx = */{
        DMA2_Stream3,
        DMA_CHANNEL_3,
    },
    /* .hdma_tx_irq = */ DMA2_Stream3_IRQn,
};

static struct rt_spi_bus spi1_bus;

/**
  * @brief  This function handles DMA Rx interrupt request.
  * @param  None
  * @retval None  
  */
void DMA2_Stream2_IRQHandler(void)
{
  HAL_DMA_IRQHandler(stm32f4_spi1.spi_handle.hdmarx);
}    
/**
  * @brief  This function handles DMA Tx interrupt request.  
  * @param  None
  * @retval None    
  */
void DMA2_Stream3_IRQHandler(void)
{
    HAL_DMA_IRQHandler(stm32f4_spi1.spi_handle.hdmatx);
}

#endif

#ifdef RT_USING_SPI2

struct stm32f4_spi stm32f4_spi2 = 
{
    /* .spi_handle = */{
        /* .Instance = */ SPI2,
    },
    /* .hdma_rx = */ {   
        DMA1_Stream3,
        DMA_CHANNEL_0,
    },
    /* .hdma_rx_irq = */ DMA1_Stream3_IRQn,

    /* .hdma_tx = */{
        DMA1_Stream4,
        DMA_CHANNEL_0,
    },
    /* .hdma_tx_irq = */ DMA1_Stream4_IRQn,
};

static struct rt_spi_bus spi2_bus;

/**
  * @brief  This function handles DMA Rx interrupt request.
  * @param  None
  * @retval None  
  */
void DMA1_Stream3_IRQHandler(void)
{
  HAL_DMA_IRQHandler(stm32f4_spi2.spi_handle.hdmarx);
}    
/**
  * @brief  This function handles DMA Tx interrupt request.  
  * @param  None
  * @retval None    
  */
void DMA1_Stream4_IRQHandler(void)
{
    HAL_DMA_IRQHandler(stm32f4_spi2.spi_handle.hdmatx);
}

#endif

#ifdef RT_USING_SPI3

struct stm32f4_spi stm32f4_spi3 = 
{
    /* .spi_handle = */{
        /* .Instance = */ SPI3,
    },
    /* .hdma_rx = */ {   
        DMA1_Stream0,
        DMA_CHANNEL_0,
    },
    /* .hdma_rx_irq = */ DMA1_Stream0_IRQn,

    /* .hdma_tx = */{
        DMA1_Stream2,
        DMA_CHANNEL_0,
    },
    /* .hdma_tx_irq = */ DMA1_Stream2_IRQn,
};

static struct rt_spi_bus spi3_bus;


/**
  * @brief  This function handles DMA Rx interrupt request.
  * @param  None
  * @retval None  
  */
void DMA1_Stream0_IRQHandler(void)
{
  HAL_DMA_IRQHandler(stm32f4_spi3.spi_handle.hdmarx);
}    
/**
  * @brief  This function handles DMA Tx interrupt request.  
  * @param  None
  * @retval None    
  */
void DMA1_Stream2_IRQHandler(void)
{
    HAL_DMA_IRQHandler(stm32f4_spi3.spi_handle.hdmatx);
}

#endif

#ifdef RT_USING_SPI4

struct stm32f4_spi stm32f4_spi4 = 
{
    /* .spi_handle = */{
L
liang yongxiang 已提交
479
        /* .Instance = */ SPI4,
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
    },
    /* .hdma_rx = */ {   
        DMA2_Stream0,
        DMA_CHANNEL_4,
    },
    /* .hdma_rx_irq = */ DMA2_Stream0_IRQn,

    /* .hdma_tx = */{
        DMA2_Stream1,
        DMA_CHANNEL_4,
    },
    /* .hdma_tx_irq = */ DMA2_Stream1_IRQn,
};

static struct rt_spi_bus spi4_bus;


/**
  * @brief  This function handles DMA Rx interrupt request.
  * @param  None
  * @retval None  
  */
void DMA2_Stream0_IRQHandler(void)
{
  HAL_DMA_IRQHandler(stm32f4_spi4.spi_handle.hdmarx);
}    
/**
  * @brief  This function handles DMA Tx interrupt request.  
  * @param  None
  * @retval None    
  */
void DMA2_Stream1_IRQHandler(void)
{
    HAL_DMA_IRQHandler(stm32f4_spi4.spi_handle.hdmatx);
}

#endif

#ifdef RT_USING_SPI5

struct stm32f4_spi stm32f4_spi5 = 
{
    /* .spi_handle = */{
        /* .Instance = */ SPI5,
    },
525
#ifdef SPI_USE_DMA
526 527 528 529 530 531 532 533 534 535 536
    /* .hdma_rx = */ {   
        DMA2_Stream3,
        DMA_CHANNEL_2,
    },
    /* .hdma_rx_irq = */ DMA2_Stream3_IRQn,

    /* .hdma_tx = */{
        DMA2_Stream4,
        DMA_CHANNEL_2,
    },
    /* .hdma_tx_irq = */ DMA2_Stream4_IRQn,
537
#endif /* SPI_USE_DMA */
538 539 540 541
};

static struct rt_spi_bus spi5_bus;

542
#ifdef SPI_USE_DMA
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
/**
  * @brief  This function handles DMA Rx interrupt request.
  * @param  None
  * @retval None  
  */
void DMA2_Stream3_IRQHandler(void)
{
  HAL_DMA_IRQHandler(stm32f4_spi5.spi_handle.hdmarx);
}    
/**
  * @brief  This function handles DMA Tx interrupt request.  
  * @param  None
  * @retval None    
  */
void DMA2_Stream4_IRQHandler(void)
{
    HAL_DMA_IRQHandler(stm32f4_spi5.spi_handle.hdmatx);
}
561
#endif /* SPI_USE_DMA */
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704

#endif

#ifdef RT_USING_SPI6

struct stm32f4_spi stm32f4_spi6 = 
{
    /* .spi_handle = */{
        /* .Instance = */ SPI5,
    },
    /* .hdma_rx = */ {   
        DMA2_Stream6,
        DMA_CHANNEL_2,
    },
    /* .hdma_rx_irq = */ DMA2_Stream6_IRQn,

    /* .hdma_tx = */{
        DMA2_Stream5,
        DMA_CHANNEL_2,
    },
    /* .hdma_tx_irq = */ DMA2_Stream5_IRQn,
};

static struct rt_spi_bus spi6_bus;


/**
  * @brief  This function handles DMA Rx interrupt request.
  * @param  None
  * @retval None  
  */
void DMA2_Stream6_IRQHandler(void)
{
  HAL_DMA_IRQHandler(stm32f4_spi6.spi_handle.hdmarx);
}    
/**
  * @brief  This function handles DMA Tx interrupt request.  
  * @param  None
  * @retval None    
  */
void DMA2_Stream5_IRQHandler(void)
{
    HAL_DMA_IRQHandler(stm32f4_spi6.spi_handle.hdmatx);
}

#endif

/** \brief init and register stm32 spi bus.
 *
 * \param SPI: STM32 SPI, e.g: SPI1,SPI2,SPI3.
 * \param spi_bus_name: spi bus name, e.g: "spi1"
 * \return
 *
 */
rt_err_t stm32_spi_bus_register(SPI_TypeDef * SPI,
                            //struct stm32_spi_bus * stm32_spi,
                            const char * spi_bus_name)
{
    struct stm32f4_spi * p_spi_bus;
    struct rt_spi_bus *  spi_bus;
    
    RT_ASSERT(SPI != RT_NULL);
    //RT_ASSERT(stm32_spi != RT_NULL);
    RT_ASSERT(spi_bus_name != RT_NULL);
        
#ifdef RT_USING_SPI1
    if(SPI == SPI1)
    {
    #ifdef SPI_USE_DMA
        __HAL_RCC_DMA2_CLK_ENABLE();
        p_spi_bus = &stm32f4_spi1;
    #endif
        __HAL_RCC_SPI1_CLK_ENABLE();
        spi_bus = &spi1_bus;
    }
#endif
    
#ifdef RT_USING_SPI2
    if(SPI == SPI2)
    {
    #ifdef SPI_USE_DMA
        __HAL_RCC_DMA1_CLK_ENABLE();
        p_spi_bus = &stm32f4_spi2;
    #endif
        __HAL_RCC_SPI2_CLK_ENABLE();
        spi_bus = &spi2_bus;
    }
#endif

#ifdef RT_USING_SPI3
    if(SPI == SPI3)
    {
    	//stm32_spi->spi_handle.Instance = SPI3;
    #ifdef SPI_USE_DMA
        __HAL_RCC_DMA1_CLK_ENABLE();
        p_spi_bus = &stm32f4_spi3;
    #endif
		__HAL_RCC_SPI3_CLK_ENABLE();
        spi_bus = &spi3_bus;
    }
#endif
    
#ifdef RT_USING_SPI4
    if(SPI == SPI4)
    {
#ifdef SPI_USE_DMA
        __HAL_RCC_DMA2_CLK_ENABLE();
#endif
		__HAL_RCC_SPI4_CLK_ENABLE();
        spi_bus = &spi4_bus;
    }
#endif

#ifdef RT_USING_SPI5
    if(SPI == SPI5)
    {
    #ifdef SPI_USE_DMA
        __HAL_RCC_DMA2_CLK_ENABLE();
        p_spi_bus = &stm32f4_spi5;		
    #endif
		__HAL_RCC_SPI5_CLK_ENABLE();
        spi_bus = &spi5_bus;
    }
#endif
 
#ifdef RT_USING_SPI6
    if(SPI == SPI6)
    {
    #ifdef SPI_USE_DMA
        __HAL_RCC_DMA2_CLK_ENABLE();
        p_spi_bus = &stm32f4_spi5;
    #endif
		__HAL_RCC_SPI6_CLK_ENABLE();
        spi_bus = &spi6_bus;
    }
#endif

    if (    (SPI != SPI1) && (SPI != SPI2) && (SPI != SPI3)
        &&  (SPI != SPI4) && (SPI != SPI5) && (SPI != SPI6))
    {
        return RT_ENOSYS;
    }
    
705
#ifdef SPI_USE_DMA
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
    /* Configure the DMA handler for Transmission process */
    p_spi_bus->hdma_tx.Init.Direction           = DMA_MEMORY_TO_PERIPH;
    p_spi_bus->hdma_tx.Init.PeriphInc           = DMA_PINC_DISABLE;
    //p_spi_bus->hdma_tx.Init.MemInc              = DMA_MINC_ENABLE;
    p_spi_bus->hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    p_spi_bus->hdma_tx.Init.MemDataAlignment    = DMA_MDATAALIGN_BYTE;
    p_spi_bus->hdma_tx.Init.Mode                = DMA_NORMAL;
    p_spi_bus->hdma_tx.Init.Priority            = DMA_PRIORITY_LOW;
    p_spi_bus->hdma_tx.Init.FIFOMode            = DMA_FIFOMODE_DISABLE;         
    p_spi_bus->hdma_tx.Init.FIFOThreshold       = DMA_FIFO_THRESHOLD_FULL;
    p_spi_bus->hdma_tx.Init.MemBurst            = DMA_MBURST_INC4;
    p_spi_bus->hdma_tx.Init.PeriphBurst         = DMA_PBURST_INC4;
    
    p_spi_bus->hdma_rx.Init.Direction           = DMA_PERIPH_TO_MEMORY;
    p_spi_bus->hdma_rx.Init.PeriphInc           = DMA_PINC_DISABLE;
    //p_spi_bus->hdma_rx.Init.MemInc              = DMA_MINC_ENABLE;
    p_spi_bus->hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    p_spi_bus->hdma_rx.Init.MemDataAlignment    = DMA_MDATAALIGN_BYTE;
    p_spi_bus->hdma_rx.Init.Mode                = DMA_NORMAL;
    p_spi_bus->hdma_rx.Init.Priority            = DMA_PRIORITY_HIGH;
    p_spi_bus->hdma_rx.Init.FIFOMode            = DMA_FIFOMODE_DISABLE;         
    p_spi_bus->hdma_rx.Init.FIFOThreshold       = DMA_FIFO_THRESHOLD_FULL;
    p_spi_bus->hdma_rx.Init.MemBurst            = DMA_MBURST_INC4;
    p_spi_bus->hdma_rx.Init.PeriphBurst         = DMA_PBURST_INC4;
730
#endif /* SPI_USE_DMA */
731 732 733 734 735

    spi_bus->parent.user_data = &stm32f4_spi5;
    
    return rt_spi_bus_register(spi_bus, spi_bus_name, &stm32_spi_ops);
}