你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

ARM CMSIS Driver 学习 之 SPI

[复制链接]
XinLiYF 发布时间:2018-4-14 18:00
ARM CMSIS Driver 学习 之 SPI

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

SPI 发送与接收
  1. /**
  2.   ******************************************************************************
  3.   * @file    main.c
  4.   * @author  XinLi
  5.   * @version v1.0
  6.   * @date    20-March-2018
  7.   * @brief   Main program body.
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * <h2><center>Copyright © 2018 XinLi</center></h2>
  12.   *
  13.   * This program is free software: you can redistribute it and/or modify
  14.   * it under the terms of the GNU General Public License as published by
  15.   * the Free Software Foundation, either version 3 of the License, or
  16.   * (at your option) any later version.
  17.   *
  18.   * This program is distributed in the hope that it will be useful,
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.   * GNU General Public License for more details.
  22.   *
  23.   * You should have received a copy of the GNU General Public License
  24.   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  25.   *
  26.   ******************************************************************************
  27.   */

  28. /* Header includes -----------------------------------------------------------*/
  29. #include "stm32f4xx.h"
  30. #include "Driver_SPI.h"
  31. #include <string.h>

  32. /* Macro definitions ---------------------------------------------------------*/
  33. /* Type definitions ----------------------------------------------------------*/
  34. /* Variable declarations -----------------------------------------------------*/
  35. extern ARM_DRIVER_SPI Driver_SPI1;

  36. /* Variable definitions ------------------------------------------------------*/
  37. static uint8_t txBuffer[5]   = {0xAB};
  38. static uint8_t rxBuffer[5]   = {0};
  39. static uint8_t dataBuffer[5] = {0};

  40. /* Function declarations -----------------------------------------------------*/
  41. static void SPI1_Callback(uint32_t event);
  42. static void SPI1_CS_Init(void);
  43. static void SPI1_CS_Low(void);
  44. static void SPI1_CS_High(void);
  45. static void SystemClock_Config(void);

  46. /* Function definitions ------------------------------------------------------*/

  47. /**
  48.   * @brief  Main program.
  49.   * @param  None.
  50.   * @return None.
  51.   */
  52. int main(void)
  53. {
  54.   /* STM32F4xx HAL library initialization:
  55.        - Configure the Flash prefetch, instruction and Data caches
  56.        - Configure the Systick to generate an interrupt each 1 msec
  57.        - Set NVIC Group Priority to 4
  58.        - Global MSP (MCU Support Package) initialization
  59.      */
  60.   HAL_Init();
  61.   
  62.   /* Configure the system clock to 168 MHz */
  63.   SystemClock_Config();
  64.   
  65.   SPI1_CS_Init();
  66.   
  67.   Driver_SPI1.Initialize(SPI1_Callback);
  68.   Driver_SPI1.PowerControl(ARM_POWER_FULL);
  69.   Driver_SPI1.Control(ARM_SPI_MODE_MASTER |
  70.                       ARM_SPI_CPOL0_CPHA0 |
  71.                       ARM_SPI_MSB_LSB |
  72.                       ARM_SPI_SS_MASTER_UNUSED |
  73.                       ARM_SPI_DATA_BITS(8), 10000000);
  74.   
  75.   SPI1_CS_Low();
  76.   Driver_SPI1.Transfer(txBuffer, rxBuffer, sizeof(txBuffer));
  77.   
  78.   for(;;)
  79.   {
  80.    
  81.   }
  82. }

  83. /**
  84.   * @brief  SPI1 callback function.
  85.   * @param  event: SPI events notification mask.
  86.   * @return None.
  87.   */
  88. static void SPI1_Callback(uint32_t event)
  89. {
  90.   if(event & ARM_SPI_EVENT_TRANSFER_COMPLETE)
  91.   {
  92.     SPI1_CS_High();
  93.     memcpy(dataBuffer, rxBuffer, sizeof(rxBuffer));
  94.   }
  95. }

  96. /**
  97.   * @brief  SPI1 CS pin initialize.
  98.   * @param  None.
  99.   * @return None.
  100.   */
  101. static void SPI1_CS_Init(void)
  102. {
  103.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  104.   
  105.   /* GPIO Ports Clock Enable */
  106.   __HAL_RCC_GPIOB_CLK_ENABLE();
  107.   
  108.   /* Configure GPIO pin : PtPin */
  109.   GPIO_InitStruct.Pin   = GPIO_PIN_14;
  110.   GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;
  111.   GPIO_InitStruct.Pull  = GPIO_PULLUP;
  112.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  113.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  114.   
  115.   /* Configure GPIO pin Output Level */
  116.   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET);
  117. }

  118. /**
  119.   * @brief  SPI1 CS pin level pull low.
  120.   * @param  None.
  121.   * @return None.
  122.   */
  123. static void SPI1_CS_Low(void)
  124. {
  125.   /* Configure GPIO pin Output Level */
  126.   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET);
  127. }

  128. /**
  129.   * @brief  SPI1 CS pin level pull high.
  130.   * @param  None.
  131.   * @return None.
  132.   */
  133. static void SPI1_CS_High(void)
  134. {
  135.   /* Configure GPIO pin Output Level */
  136.   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET);
  137. }

  138. /**
  139.   * @brief  System Clock Configuration
  140.   *         The system Clock is configured as follow :
  141.   *            System Clock source            = PLL (HSE)
  142.   *            SYSCLK(Hz)                     = 168000000
  143.   *            HCLK(Hz)                       = 168000000
  144.   *            AHB Prescaler                  = 1
  145.   *            APB1 Prescaler                 = 4
  146.   *            APB2 Prescaler                 = 2
  147.   *            HSE Frequency(Hz)              = 8000000
  148.   *            PLL_M                          = 8
  149.   *            PLL_N                          = 336
  150.   *            PLL_P                          = 2
  151.   *            PLL_Q                          = 7
  152.   *            VDD(V)                         = 3.3
  153.   *            Main regulator output voltage  = Scale1 mode
  154.   *            Flash Latency(WS)              = 5
  155.   * @param  None
  156.   * @retval None
  157.   */
  158. static void SystemClock_Config(void)
  159. {
  160.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  161.   RCC_OscInitTypeDef RCC_OscInitStruct;

  162.   /* Enable Power Control clock */
  163.   __HAL_RCC_PWR_CLK_ENABLE();

  164.   /* The voltage scaling allows optimizing the power consumption when the device is
  165.      clocked below the maximum system frequency, to update the voltage scaling value
  166.      regarding system frequency refer to product datasheet.  */
  167.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  168.   /* Enable HSE Oscillator and activate PLL with HSE as source */
  169.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  170.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  171.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  172.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  173.   RCC_OscInitStruct.PLL.PLLM = 8;
  174.   RCC_OscInitStruct.PLL.PLLN = 336;
  175.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  176.   RCC_OscInitStruct.PLL.PLLQ = 7;
  177.   HAL_RCC_OscConfig(&RCC_OscInitStruct);
  178.   
  179.   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  180.      clocks dividers */
  181.   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  182.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  183.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  184.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;  
  185.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;  
  186.   HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);

  187.   /* STM32F405x/407x/415x/417x Revision Z devices: prefetch is supported  */
  188.   if (HAL_GetREVID() == 0x1001)
  189.   {
  190.     /* Enable the Flash prefetch */
  191.     __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  192.   }
  193. }
复制代码

归档链接
ARM CMSIS Driver 学习 之 USART
收藏 评论0 发布时间:2018-4-14 18:00

举报

0个回答

所属标签

STM32团队

意法半导体微控制器和微处理器拥有广泛的产品线,包含低成本的8位单片机和基于ARM® Cortex®-M0、M0+、M3、M4、M33、M7及A7内核并具备丰富外设选择的32位微控制器及微处理器


最新内容

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版