ARM CMSIS Driver Learning SPI

Publisher:sheng44Latest update time:2018-04-23 Source: eefocusKeywords:ARM  CMSIS  Driver  SPI Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

CMSIS Driver has similar API functions and similar calling methods. It is a further encapsulation based on the ST HAL library. It is much more convenient and simpler to use and configure than the ST HAL library. It is also cross-platform and very valuable to learn and use. Today we will learn how to use the SPI API. For detailed introduction, see  CMSIS Driver SPI API

SPI Transmit and Receive

/** 

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

  * @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();  

  }  

}  


Keywords:ARM  CMSIS  Driver  SPI Reference address:ARM CMSIS Driver Learning SPI

Previous article:STM32F103C8T6 reads PT100 resistance value through MAX31865
Next article:ARM CMSIS Driver Learning - USART

Recommended ReadingLatest update time:2024-11-16 23:43

Watch the design of ARM controlled inverter power supply circuit
System overall plan As shown in Figure 1,  the inverter system consists of a boost circuit, an inverter circuit, a control circuit, and a feedback circuit. The low-voltage DC power supply DC12V is boosted, rectified , and filtered by the boost circuit to obtain a high-voltage DC power of about DC170V, and then conver
[Power Management]
Watch the design of ARM controlled inverter power supply circuit
Pi Zi Heng Embedded: ARM Cortex-M Core (4) - Performance Indicators
1. Processor performance indicators   There are many performance indicators used to evaluate CPUs. Test standards with different performance focuses may produce different indicator values. The following introduces two classic test standards widely used in the embedded industry. 1.1 Dhrystone Standard   Dhrystone is a
[Microcontroller]
Pi Zi Heng Embedded: ARM Cortex-M Core (4) - Performance Indicators
Linux platform device driver - button device driver
A previous blog briefly introduced the platform device driver model (http://www.cnblogs.com/ape-ming/p/5107641.html). Now, based on the template listed in that blog, we will modify the example in the previous blog (http://www.cnblogs.com/ape-ming/p/5110996.html) into the platform device driver model. 1. P
[Microcontroller]
13. Knowledge of ARM coprocessor
There are coprocessors in the processor to assist the processor in completing some functions, mainly for assisting purposes. Coprocessor: Coprocessors are used to perform specific processing tasks, such as math coprocessors that can control digital processing to reduce the burden on the processor. ARM can
[Microcontroller]
13. Knowledge of ARM coprocessor
Design of SoC Voice Processing System Based on ARM7 TDMI
introduction With the rapid development of microelectronics and computer technology, many embedded application systems have emerged. Among them, various speech processing systems have been continuously developed and widely used in various industries, such as voice station announcers, automatic interpretatio
[Microcontroller]
Design of SoC Voice Processing System Based on ARM7 TDMI
Simple open-circuit protection for boost converters in LED driver applications
introduction One approach to driving high-brightness LEDs is to modify the standard boost converter topology to drive a constant current through the load. However, this implementation presents a serious problem because an open-circuit fault in the LED string removes the path for the load current. This can p
[Power Management]
Simple open-circuit protection for boost converters in LED driver applications
164 drive 8-bit LED display CVAVR program
//164 driver digital tube display //Chip ATMEGA16L  //Clock 4MHz internal //Written in CVAVR, using the system's own delay function //PD0 PD1 simulate 164 timing #include     #include    #define hc164_data PORTD.0 #define hc164_clk PORTD.1   void led164_display (void); // Digital tube display void hc164_send_
[Microcontroller]
ARM external Flash memory IAP solution
Embedded application systems with ARM chips as processor cores have gained more and more popularity due to their small size, low power consumption, low cost, high performance, rich on-chip resources and wide support for operating systems. In-Application Programming (IAP ) is such a self-modify
[Power Management]
ARM external Flash memory IAP solution
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号