MK22FN512VLH12单片机使用官方库的fsl_dspi.h文件和fsl_port.h文件配置PORTC5、PORTC6、PORTC7引脚为SPI0,来读写W25Q128(PORTC4为使能引脚)
时间: 2024-03-08 07:50:28 浏览: 218
好的,您的问题是如何使用MK22FN512VLH12单片机的官方库文件配置SPI0接口以及W25Q128的读写操作。
首先,您需要在代码中包含fsl_dspi.h和fsl_port.h头文件。接下来,您需要配置SPI0的引脚,即PORTC5、PORTC6和PORTC7引脚。可以使用PORT_SetPinMux函数将这些引脚配置为SPI0的SCK、MOSI和MISO引脚。同时,也需要配置PORTC4引脚为W25Q128的使能引脚。
下面是具体的代码示例:
```c
#include "fsl_dspi.h"
#include "fsl_port.h"
#define DSPI_MASTER_BASEADDR SPI0
#define DSPI_MASTER_CLK_SRC DSPI0_CLK_SRC
#define DSPI_MASTER_CLK_FREQ CLOCK_GetFreq(DSPI0_CLK_SRC)
#define DSPI_MASTER_PCS_FOR_INIT kDSPI_Pcs0
#define DSPI_MASTER_PCS_FOR_TRANSFER kDSPI_MasterPcs0
#define TRANSFER_SIZE 256U /*! Transfer dataSize. */
uint8_t txData[TRANSFER_SIZE];
uint8_t rxData[TRANSFER_SIZE];
void SPI_Init(void)
{
/* Initialize SPI master */
dspi_master_config_t masterConfig;
DSPI_MasterGetDefaultConfig(&masterConfig);
masterConfig.ctarConfig.baudRate = 5000000U;
DSPI_MasterInit(DSPI_MASTER_BASEADDR, &masterConfig, DSPI_MASTER_CLK_FREQ);
}
void GPIO_Init(void)
{
/* Define the init structure for the output LED pin */
gpio_pin_config_t output_config = {
kGPIO_DigitalOutput, 0,
};
/* Initialize SPI0 SCK/MOSI/MISO pins */
PORT_SetPinMux(PORTC, 5U, kPORT_MuxAlt2);
PORT_SetPinMux(PORTC, 6U, kPORT_MuxAlt2);
PORT_SetPinMux(PORTC, 7U, kPORT_MuxAlt2);
/* Initialize W25Q128 CS pin */
PORT_SetPinMux(PORTC, 4U, kPORT_MuxAsGpio);
GPIO_PinInit(GPIOC, 4U, &output_config);
}
void W25Q128_WriteEnable(void)
{
/* Set W25Q128 CS pin to low */
GPIO_ClearPinsOutput(GPIOC, 1U << 4U);
/* Send "Write Enable" command */
txData[0] = 0x06;
DSPI_MasterTransferBlocking(DSPI_MASTER_BASEADDR, &txData[0], NULL, 1U, kDSPI_MasterPcs0, NULL);
/* Set W25Q128 CS pin to high */
GPIO_SetPinsOutput(GPIOC, 1U << 4U);
}
void W25Q128_WriteData(uint32_t address, uint8_t *data, uint32_t dataSize)
{
/* Set W25Q128 CS pin to low */
GPIO_ClearPinsOutput(GPIOC, 1U << 4U);
/* Send "Page Program" command */
txData[0] = 0x02;
txData[1] = (address >> 16U) & 0xFFU;
txData[2] = (address >> 8U) & 0xFFU;
txData[3] = address & 0xFFU;
DSPI_MasterTransferBlocking(DSPI_MASTER_BASEADDR, &txData[0], NULL, 4U, kDSPI_MasterPcs0, NULL);
/* Send data */
DSPI_MasterTransferBlocking(DSPI_MASTER_BASEADDR, &data[0], NULL, dataSize, kDSPI_MasterPcs0, NULL);
/* Set W25Q128 CS pin to high */
GPIO_SetPinsOutput(GPIOC, 1U << 4U);
}
void W25Q128_ReadData(uint32_t address, uint8_t *data, uint32_t dataSize)
{
/* Set W25Q128 CS pin to low */
GPIO_ClearPinsOutput(GPIOC, 1U << 4U);
/* Send "Read Data" command */
txData[0] = 0x03;
txData[1] = (address >> 16U) & 0xFFU;
txData[2] = (address >> 8U) & 0xFFU;
txData[3] = address & 0xFFU;
DSPI_MasterTransferBlocking(DSPI_MASTER_BASEADDR, &txData[0], NULL, 4U, kDSPI_MasterPcs0, NULL);
/* Receive data */
DSPI_MasterTransferBlocking(DSPI_MASTER_BASEADDR, NULL, &data[0], dataSize, kDSPI_MasterPcs0, NULL);
/* Set W25Q128 CS pin to high */
GPIO_SetPinsOutput(GPIOC, 1U << 4U);
}
int main(void)
{
/* Init board hardware */
BOARD_InitPins();
BOARD_BootClockRUN();
BOARD_InitDebugConsole();
/* Initialize GPIO */
GPIO_Init();
/* Initialize SPI */
SPI_Init();
/* Enable writing to W25Q128 */
W25Q128_WriteEnable();
/* Write data to W25Q128 */
uint8_t writeData[TRANSFER_SIZE] = {0};
for (uint32_t i = 0; i < TRANSFER_SIZE; i++)
{
writeData[i] = i & 0xFFU;
}
W25Q128_WriteData(0x00000000U, writeData, TRANSFER_SIZE);
/* Read data from W25Q128 */
uint8_t readData[TRANSFER_SIZE] = {0};
W25Q128_ReadData(0x00000000U, readData, TRANSFER_SIZE);
/* Verify data */
for (uint32_t i = 0; i < TRANSFER_SIZE; i++)
{
if (readData[i] != (i & 0xFFU))
{
PRINTF("Data verification failed!\r\n");
break;
}
}
while (1)
{
}
}
```
在以上代码中,程序首先调用GPIO_Init函数配置SPI0的引脚和W25Q128的使能引脚。然后,程序调用SPI_Init函数初始化SPI0的主模式,并设置其波特率为5MHz。
接下来,程序调用W25Q128_WriteEnable函数向W25Q128发送“写使能”命令。然后,程序调用W25Q128_WriteData函数将256字节的数据写入W25Q128的地址0x00000000处。最后,程序调用W25Q128_ReadData函数从W25Q128的地址0x00000000处读取256字节的数据,并对读取的数据进行验证。如果数据验证失败,则程序将输出错误消息。
希望这个示例可以帮到您,如果您还有其他问题,请随时问我。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)