主题中讨论的其他器件: CC3220SF
尊敬的社区:
我正在处理基于 SPI 的 EEPROM 写读操作。
使用 CC3220SF-LAUNCHXL Rev.A 在 simplelink cc32xx SDK 6.10.00.05的帮助下对基于25LC1024 SPI 的 EEPROM 进行写入和读取操作
在基于 SPI 的 EEPROM 操作的 NORTOS 示例代码中搜索、未找到。如果找到 SimpleLink CC3220SF 至 EEPROM 读取写入操作的任何示例代码、请分享。
因此、我根据要求修改了 TI RTOS 基础 SPI 主设备示例代码。
在尝试将数据0xAA 写入 EEPROM 并从相同地址(0x0010FF)读回时进行此修改后、我将0xFF 作为输出、我将地址更改为0x001000、然后也是相同的输出。 有人能帮助我解决这个问题吗?
修改后的代码如下:
/*
* ======== spi_eeprom.c ========
*/
#include <stdint.h>
#include <stdio.h>
#include <stddef.h>
#include <unistd.h>
#include <string.h>
/* Driver Header files */
#include <ti/drivers/GPIO.h>
#include <ti/drivers/SPI.h>
/* Driver configuration */
#include "ti_drivers_config.h"
#define SPI_MSG_LENGTH (8)
unsigned char controllerRxBuffer[SPI_MSG_LENGTH];
unsigned char controllerTxBuffer[SPI_MSG_LENGTH];
/* EEPROM instructions */
#define EEPROM_WRITE_ENABLE (0x06)
#define EEPROM_WRITE_DISABLE (0x04)
#define EEPROM_READ (0x03)
#define EEPROM_WRITE (0x02)
#define EEPROM_ID (0xAB)
SPI_Handle controllerSpi;
SPI_Handle Init_SPI(void){
SPI_Params spiParams;
//printf("SPI initiating\n");
SPI_Params_init(&spiParams); // Initialize SPI parameters
spiParams.dataSize = 8; // 8-bit data size
spiParams.frameFormat = SPI_POL0_PHA0;
spiParams.bitRate = 1000000;
controllerSpi = SPI_open(CONFIG_SPI_0, &spiParams);
if (controllerSpi == NULL)
{
printf("Error initializing controller SPI\n");
}
else
{
printf("SPI initialized\n");
}
return controllerSpi;
}
int writeToEEPROM(uint32_t address, uint8_t* data, uint32_t dataSize)
{
SPI_Transaction spiTransaction;
bool transferOK;
printf("To write\n");
uint8_t tmp_txBuffer[SPI_MSG_LENGTH];
tmp_txBuffer[0] = EEPROM_WRITE_ENABLE; // EEPROM write enable latch
tmp_txBuffer[1] = EEPROM_WRITE; // EEPROM write command
tmp_txBuffer[2] = (address >> 16) & 0xFF; // Address MSB
tmp_txBuffer[3] = (address >> 8) & 0xFF; // Address middle byte
tmp_txBuffer[4] = address & 0xFF; // Address LSB
tmp_txBuffer[5] = data; // data to be written to 25LC1024
strncpy((char *)controllerTxBuffer, tmp_txBuffer, sizeof(SPI_MSG_LENGTH));
memset((void *)controllerRxBuffer, 0, SPI_MSG_LENGTH);
spiTransaction.count = SPI_MSG_LENGTH;
spiTransaction.txBuf = (void *)controllerTxBuffer;
spiTransaction.rxBuf = (void *)controllerRxBuffer;
/* Toggle user LED, indicating a SPI transfer is in progress */
GPIO_toggle(CONFIG_GPIO_LED_1);
/* Perform SPI transfer */
transferOK = SPI_transfer(controllerSpi, &spiTransaction);
if (transferOK)
{
printf("Master transmitted data: %x\n", *(char *)controllerTxBuffer);
//printf("bufr address of transmitted data: %#x\n", controllerTxBuffer);
}
else
{
printf("Unsuccessful SPI transfer");
return -1;
}
return 0;
}
int readFromEEPROM(uint32_t address, uint8_t* receiveBuffer, uint32_t dataSize)
{
printf("To read\n");
// Prepare the read command and address
SPI_Transaction spiTransaction;
bool transferOK;
uint8_t tmp_txBuffer[SPI_MSG_LENGTH];
tmp_txBuffer[0] = EEPROM_READ; // Read command
tmp_txBuffer[1] = (address >> 16) & 0xFF; // Address MSB
tmp_txBuffer[2] = (address >> 8) & 0xFF; // Address middle byte
tmp_txBuffer[3] = address & 0xFF; // Address LSB
tmp_txBuffer[4] = 0xBB; // dummy data
strncpy((char *)controllerTxBuffer, tmp_txBuffer, sizeof(SPI_MSG_LENGTH-1));
memset((void *)controllerRxBuffer, 0, SPI_MSG_LENGTH-1);
spiTransaction.count = SPI_MSG_LENGTH - 1; // Read command + address + dummy with dataSize
spiTransaction.txBuf = (void *)controllerTxBuffer;
spiTransaction.rxBuf = (void *)controllerRxBuffer;
/* Toggle user LED, indicating a SPI transfer is in progress */
GPIO_toggle(CONFIG_GPIO_LED_1);
/* Perform SPI transfer */
transferOK = SPI_transfer(controllerSpi, &spiTransaction);
if (transferOK)
{
printf("Master received data: %x\n", *(char *)controllerRxBuffer);
//printf("bufr address of received data : %#x\n", controllerRxBuffer);
}
else
{
printf("Unsuccessful SPI transfer");
return -1;
}
return 0;
}
void *mainThread(void *arg0)
{
uint8_t writeData[] = {0xAA}; // Data to write to the EEPROM
uint32_t address = 0x000010FF; // Address to write to and read from
uint32_t dataSize = sizeof(writeData); // Size of data to write and read
uint8_t receiveBuffer[dataSize];
/* Call driver init functions. */
GPIO_init();
SPI_init();
/* Configure the LED pins */
GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
GPIO_setConfig(CONFIG_GPIO_LED_1, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
Init_SPI();
/* Turn on user LED */
GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);
// Write data to the EEPROM
writeToEEPROM(address, writeData, dataSize);
// Read data from the EEPROM
readFromEEPROM(address, receiveBuffer, dataSize);
SPI_close(controllerSpi);
printf("\nDone");
}
我针对上述代码获得的输出:
写入
主机发送的数据:6
读取
主机接收的数据:FF
这是 SimpleLink 板的 SPI 引脚配置

硬件配置如下:
| 简单链接 | 25个 LC1024 |
| SCLK | SCK |
| MISO | 因此 |
| 穆西 | Si |
| SS | CS 条 |
| 第3版 | VCC 电压 |
| 接地 | VSS |
谢谢。此致、
Ajaykumar V
