MT2625 SPI学习

https://blog.csdn.net/qq_38410730/article/details/80318821

https://zhuanlan.zhihu.com/p/37506796

https://wenku.baidu.com/view/cf7de1dcfd0a79563d1e7220.html


DMA
Direct Memory Access. DMA is a feature of computer systems that allows certain hardware subsystems to access main system memory independent from the central processing unit (CPU).

FIFO First In, First Out. FIFO is a method for organizing and manipulating a data buffer, where the first entry, or 'head' of the queue, is processed first.

GPIO General Purpose Inputs-Outputs.

NVIC Nested Vectored Interrupt Controller. NVIC is the interrupt controller of ARM Cortex-M series processors.

IRQ Interrupt Request. For more information

MISO Master Input, Slave Output. Output from the SPI slave.

MOSI Master Output, Slave Input. Output from the SPI master.

SCLK Serial Clock. Output from the SPI master.

SS Slave Select. Output from the SPI master, active low.

SPI 串行外设接口是一种高速的,全双工,同步的通信总线

Serial Peripheral Interface. The Serial Peripheral Interface bus is a synchronous serial communication interface specification used for short distance communication.

mt2625 的 spi 有polling 、dma 、dma blocking模式,其中dma在初始化完成后需要注册回调函数,sample code如下:

 

  1 // Callback function sample code. Pass this function to the driver while calling #hal_spi_master_register_callback().
  2 void user_spi_callback (hal_spi_master_callback_event_t event,void *user_data)
  3 {
  4     rin_log("\r\n user_spi_callback event=%d \r\n",event);
  5     
  6     if (HAL_SPI_MASTER_EVENT_SEND_FINISHED == event)
  7     {
  8         // Send finish event handler;
  9         // User code;
 10         hal_spi_master_deinit(MASTER_SPI_PORT); // Deinitialize, if no longer in use.
 11     }
 12     else if (HAL_SPI_MASTER_EVENT_RECEIVE_FINISHED == event)
 13     {
 14         // Receive finish event handler;
 15         // User code;
 16         hal_spi_master_deinit(MASTER_SPI_PORT); // Deinitialize, if no longer in use.
 17     }
 18 }
 19 
 20 
 21 void SPIInitMasterDMAMode(void)
 22 {
 23     hal_spi_master_config_t spi_config_master;
 24     hal_spi_master_send_and_receive_config_t spi_send_and_receive_config;
 25     uint8_t status_cmd = 0x10;//SPIS_STATUS_CMD;
 26     
 27     hal_spi_master_status_t err = HAL_SPI_MASTER_STATUS_OK;
 28     
 29      // SPI init
 30     //hal_spi_master_send_and_receive_config_t spi_send_and_receive_config;
 31     uint8_t status_receive[2];
 32     uint8_t data[2] = {0x7E, 0x55};
 33     uint32_t size = 2;
 34 
 35     rin_log("\r\nSPIInitMasterMode \r\n");
 36     
 37     spi_config_master.bit_order = HAL_SPI_MASTER_LSB_FIRST;
 38     spi_config_master.slave_port = HAL_SPI_MASTER_SLAVE_0;
 39     spi_config_master.clock_frequency = 1000000;
 40     spi_config_master.phase = HAL_SPI_MASTER_CLOCK_PHASE0;
 41     spi_config_master.polarity = HAL_SPI_MASTER_CLOCK_POLARITY0;
 42     status_receive[1] = 0;
 43 
 44     spi_send_and_receive_config.receive_length = 2;
 45     spi_send_and_receive_config.send_length = 1;
 46     spi_send_and_receive_config.send_data = &status_cmd;
 47     spi_send_and_receive_config.receive_buffer = status_receive;
 48 
 49     // Step 1. Call hal_gpio_init() to inite the pins,
 50     // if EPT tool hasn't been used to configure the related pinmux.
 51     hal_gpio_init(SPI_CS);
 52     hal_gpio_init(SPI_CLK);
 53     hal_gpio_init(SPI_MOSI);
 54     hal_gpio_init(SPI_MISO);
 55     
 56     // Step 2. Call hal_pinmux_set_function() to set GPIO pinmux,
 57     // if EPT tool hasn't been used to configure the related pinmux.
 58     hal_pinmux_set_function(SPI_CS, HAL_GPIO_19_SPI_MST1_CS);// Set the pin to be used as CS signal of SPI.
 59     hal_pinmux_set_function(SPI_CLK, HAL_GPIO_22_SPI_MST1_SCK);// Set the pin to be used as SCK signal of SPI.
 60     hal_pinmux_set_function(SPI_MISO, HAL_GPIO_20_SPI_MST1_MISO);// Set the pin to be used as MOSI signal of SPI.
 61     hal_pinmux_set_function(SPI_MOSI, HAL_GPIO_21_SPI_MST1_MOSI);// Set the pin to be used as MISO signal of SPI.
 62 
 63     // Step 3. Call hal_spi_master_init() to initialize one SPI master.
 64     // If the SPI master is already initialized by another user, user will get HAL_SPI_MASTER_STATUS_ERROR_BUSY.
 65     err = hal_spi_master_init(MASTER_SPI_PORT,&spi_config_master);
 66     
 67     if (HAL_SPI_MASTER_STATUS_OK == err)
 68     {
 69         rin_log("\r\nHAL_SPI_MASTER_STATUS_OK \r\n");
 70 
 71         // Step 4. Call hal_spi_master_register_callback() to register a user callback.
 72         err = hal_spi_master_register_callback(MASTER_SPI_PORT, (hal_spi_master_callback_t)user_spi_callback, NULL);
 73         
 74         if (HAL_SPI_MASTER_STATUS_OK != err)
 75         {
 76             // Error handler;
 77             rin_log("\r\n reg cb fail err=%d \r\n",err);
 78         }
 79         rin_log("\r\n reg cb HAL_SPI_MASTER_STATUS_OK \r\n");
 80         
 81         TaskDelay(24);
 82         
 83         // Step 5. Call hal_spi_master_send_dma() to send data in the DMA mode.
 84         // Step 6. Call hal_spi_master_send_and_receive_dma() to send and receive data in the DMA mode.
 85         err = hal_spi_master_send_dma(MASTER_SPI_PORT,data,size);//Half-duplex
 86 
 87         TaskDelay(1000);
 88 
 89         // Call hal_spi_master_send_and_receive_dma() to send and receive data in the DMA mode.
 90         //if (HAL_SPI_MASTER_STATUS_OK != hal_spi_master_send_and_receive_dma(MASTER_SPI_PORT,&spi_send_and_receive_config))
 91         //{
 92             // Error handler;
 93             //rin_log("\r\n send and recv fail err=%d \r\n",err);
 94         //}
 95     
 96         if (HAL_SPI_MASTER_STATUS_OK != err)
 97         {
 98             // Error handler;
 99             rin_log("\r\n send fail err=%d \r\n",err);
100         }
101         else
102         {
103             rin_log("\r\n send suc data=0x%x 0x%x \r\n",data[0],data[1]);
104         }
105     }
106     else
107     {    
108         // Error handler;
109         rin_log("\r\n init fail err=%d \r\n",err);
110     }
111 
112     // Step 6. Call hal_spi_master_deinit() to deinitialize the SPI master, if it's no longer in use.
113     //err = hal_spi_master_deinit();//deinit in cb func
114     rin_log("\r\n hal_spi_master_deinit err=%d \r\n",err);
115     //vTaskDelete(g_HLSpi_task_handle);
116     
117 }

 

posted @ 2019-09-24 16:28  LevinKai  阅读(467)  评论(0编辑  收藏  举报