×
单片机 > 单片机程序设计 > 详情

ARM CMSIS Driver 学习 之 SPI

发布时间:2020-08-31 发布时间:
|

CMSIS Driver 都有着相似的 API 函数和相似的调用方法,它是在 ST HAL 库的基础上又进一步的封装,使用和配置起来都要比 ST HAL 库要方便和简单许多,并且还是跨平台的,非常有学习和使用的价值。今天学习 SPI API 的使用,详细介绍见 CMSIS Driver SPI API

SPI 发送与接收

/** 

  ****************************************************************************** 

  * @file    main.c 

  * @author  XinLi 

  * @version v1.0 

  * @date    20-March-2018 

  * @brief   Main program body. 

  ****************************************************************************** 

  * @attention 

  * 

  *

Copyright © 2018 XinLi

 

  * 

  * This program is free software: you can redistribute it and/or modify 

  * it under the terms of the GNU General Public License as published by 

  * the Free Software Foundation, either version 3 of the License, or 

  * (at your option) any later version. 

  * 

  * This program is distributed in the hope that it will be useful, 

  * but WITHOUT ANY WARRANTY; without even the implied warranty of 

  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

  * GNU General Public License for more details. 

  * 

  * You should have received a copy of the GNU General Public License 

  * along with this program.  If not, see

  * 

  ****************************************************************************** 

  */  

  

/* Header includes -----------------------------------------------------------*/  

#include "stm32f4xx.h"  

#include "Driver_SPI.h"  

#include  

  

/* Macro definitions ---------------------------------------------------------*/  

/* Type definitions ----------------------------------------------------------*/  

/* Variable declarations -----------------------------------------------------*/  

extern ARM_DRIVER_SPI Driver_SPI1;  

  

/* Variable definitions ------------------------------------------------------*/  

static uint8_t txBuffer[5]   = {0xAB};  

static uint8_t rxBuffer[5]   = {0};  

static uint8_t dataBuffer[5] = {0};  

  

/* Function declarations -----------------------------------------------------*/  

static void SPI1_Callback(uint32_t event);  

static void SPI1_CS_Init(void);  

static void SPI1_CS_Low(void);  

static void SPI1_CS_High(void);  

static void SystemClock_Config(void);  

  

/* Function definitions ------------------------------------------------------*/  

  

/** 

  * @brief  Main program. 

  * @param  None. 

  * @return None. 

  */  

int main(void)  

{  

  /* STM32F4xx HAL library initialization: 

       - Configure the Flash prefetch, instruction and Data caches 

       - Configure the Systick to generate an interrupt each 1 msec 

       - Set NVIC Group Priority to 4 

       - Global MSP (MCU Support Package) initialization 

     */  

  HAL_Init();  

    

  /* Configure the system clock to 168 MHz */  

  SystemClock_Config();  

    

  SPI1_CS_Init();  

    

  Driver_SPI1.Initialize(SPI1_Callback);  

  Driver_SPI1.PowerControl(ARM_POWER_FULL);  

  Driver_SPI1.Control(ARM_SPI_MODE_MASTER |  

                      ARM_SPI_CPOL0_CPHA0 |  

                      ARM_SPI_MSB_LSB |  

                      ARM_SPI_SS_MASTER_UNUSED |  

                      ARM_SPI_DATA_BITS(8), 10000000);  

    

  SPI1_CS_Low();  

  Driver_SPI1.Transfer(txBuffer, rxBuffer, sizeof(txBuffer));  

    

  for(;;)  

  {  

      

  }  

}  

  

/** 

  * @brief  SPI1 callback function. 

  * @param  event: SPI events notification mask. 

  * @return None. 

  */  

static void SPI1_Callback(uint32_t event)  

{  

  if(event & ARM_SPI_EVENT_TRANSFER_COMPLETE)  

  {  

    SPI1_CS_High();  

    memcpy(dataBuffer, rxBuffer, sizeof(rxBuffer));  

  }  

}  

  

/** 

  * @brief  SPI1 CS pin initialize. 

  * @param  None. 

  * @return None. 

  */  

static void SPI1_CS_Init(void)  

{  

  GPIO_InitTypeDef GPIO_InitStruct = {0};  

    

  /* GPIO Ports Clock Enable */  

  __HAL_RCC_GPIOB_CLK_ENABLE();  

    

  /* Configure GPIO pin : PtPin */  

  GPIO_InitStruct.Pin   = GPIO_PIN_14;  

  GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;  

  GPIO_InitStruct.Pull  = GPIO_PULLUP;  

  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;  

  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);  

    

  /* Configure GPIO pin Output Level */  

  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET);  

}  

  

/** 

  * @brief  SPI1 CS pin level pull low. 

  * @param  None. 

  * @return None. 

  */  

static void SPI1_CS_Low(void)  

{  

  /* Configure GPIO pin Output Level */  

  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET);  

}  

  

/** 

  * @brief  SPI1 CS pin level pull high. 

  * @param  None. 

  * @return None. 

  */  

static void SPI1_CS_High(void)  

{  

  /* Configure GPIO pin Output Level */  

  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET);  

}  

  

/** 

  * @brief  System Clock Configuration 

  *         The system Clock is configured as follow :  

  *            System Clock source            = PLL (HSE) 

  *            SYSCLK(Hz)                     = 168000000 

  *            HCLK(Hz)                       = 168000000 

  *            AHB Prescaler                  = 1 

  *            APB1 Prescaler                 = 4 

  *            APB2 Prescaler                 = 2 

  *            HSE Frequency(Hz)              = 8000000 

  *            PLL_M                          = 8 

  *            PLL_N                          = 336 

  *            PLL_P                          = 2 

  *            PLL_Q                          = 7 

  *            VDD(V)                         = 3.3 

  *            Main regulator output voltage  = Scale1 mode 

  *            Flash Latency(WS)              = 5 

  * @param  None 

  * @retval None 

  */  

static void SystemClock_Config(void)  

{  

  RCC_ClkInitTypeDef RCC_ClkInitStruct;  

  RCC_OscInitTypeDef RCC_OscInitStruct;  

  

  /* Enable Power Control clock */  

  __HAL_RCC_PWR_CLK_ENABLE();  

  

  /* The voltage scaling allows optimizing the power consumption when the device is  

     clocked below the maximum system frequency, to update the voltage scaling value  

     regarding system frequency refer to product datasheet.  */  

  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);  

  

  /* Enable HSE Oscillator and activate PLL with HSE as source */  

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;  

  RCC_OscInitStruct.HSEState = RCC_HSE_ON;  

  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;  

  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;  

  RCC_OscInitStruct.PLL.PLLM = 8;  

  RCC_OscInitStruct.PLL.PLLN = 336;  

  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;  

  RCC_OscInitStruct.PLL.PLLQ = 7;  

  HAL_RCC_OscConfig(&RCC_OscInitStruct);  

    

  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2  

     clocks dividers */  

  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);  

  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;  

  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;  

  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;    

  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;    

  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);  

  

  /* STM32F405x/407x/415x/417x Revision Z devices: prefetch is supported  */  

  if (HAL_GetREVID() == 0x1001)  

  {  

    /* Enable the Flash prefetch */  

    __HAL_FLASH_PREFETCH_BUFFER_ENABLE();  

  }  

}  



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
基于arm的指纹识别门禁系统是如何设计的