历史上的今天
今天是:2025年04月23日(星期三)
2018年04月23日 | ARM CMSIS Driver 学习 之 SPI
2018-04-23 来源:eefocus
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();
}
}
史海拾趣
|
1 引言 20世纪末,全球范围内兴起的信息革命浪潮,为汽车工业的突破性发展提供了千载难逢的机遇,信息技术的广泛应用是解决汽车带来的诸如交通拥挤、交通安全、环境污染、能源枯竭等问题的最佳途径。同时,随着汽车电子技术的发展,电子组件 ...… 查看全部问答> |
|
最近用164做了个倒计时,我用的是定时器1的方式1,倒计时可以跑起来,而且完全正常,我现在想用两个外部中断来调整时和分,但是一加上外部中断定时中断都不在响应了,我把外部中断的入口屏蔽了,倒计时正常了。如果我把定时器换为定时器0所有中断都 ...… 查看全部问答> |
|
串口0为片内自带的,初始化的时候,U0FCR这个寄存器总是写不进去值,请教高手~~~ 附代码:[code] INT8U UART0_Init( INT32U baudrate) { INT32U Fdiv; PINSEL0 &= 0 ...… 查看全部问答> |
|
ndis5.1中对应的函数为 VOID NdisInitializeWorkItem( IN PNDIS_WORK_ITEM WorkItem, IN NDIS_PROC Routine, IN PVOID Context ); n ...… 查看全部问答> |
|
;*******************************; 主 程 序;*******************************MAINPG: ANL PCA0MD,#0 ...… 查看全部问答> |
|
MSP430: Trouble Writing Memory Block at 0x0 on Page 0 of Length 0x4: Could not perform access, BSL memory segments are protected. MSP430: GEL: File: D:\\workspace_v5_2\\12864\\Debug\\12864.out: Load failed.… 查看全部问答> |




