Tool/software: Code Composer Studio
Hi,
I need some help in using SPI in CC1352R1 on the Launchpad. I want to read/write from/to registers in an E Paper Driver (S1D13541) by Epson.
Background
I am following a project done for MSP430 by the E Paper manufacturers. I need to drive the EPD using CC1352R1. So far I have written the code for SPI Init and Write.
I will paste the code snippets below.
Requirement
Have I implemented the spi_read(..) correctly? Can someone guide me on how to read from a register from a SPI Slave?
void init_s1d135xx_spi() { SPI_init(); SPI_Params_init(&spiParams); spiParams.frameFormat = SPI_POL0_PHA0; spiParams.bitRate = 4000000; spiParams.mode = SPI_MASTER; spiParams.dataSize = 8; masterSpi = SPI_open(CONFIG_SPI_0, &spiParams); if (masterSpi == NULL) { //Display_printf(display, 0, 0, "Error initializing master SPI\n"); while (1); } else { //Display_printf(display, 0, 0, "Master SPI initialized\n"); } }
void s1d135xx_write_reg(uint16_t reg, uint16_t val) { uint16_t params[] = { reg, val }; GPIO_write(SPI_CS,0); // Pull Down CS send_cmd(S1D135XX_CMD_WRITE_REG); send_params(params, ARRAY_SIZE(params)); GPIO_write(SPI_CS,1); // Pull UP & release CS Display_printf(display, 0, 0, "Wrote 0x%x to 0x%x", val, reg); } uint8_t s1d135xx_read_reg(uint16_t reg) { uint8_t readRegVal1,readRegVal2; GPIO_write(SPI_CS,0); // Pull Down CS send_cmd(S1D135XX_CMD_READ_REG); send_param(reg); readRegVal1 = spi_read(0); readRegVal2 = spi_read(0); GPIO_write(SPI_CS,1); // Pull UP CS readRegVal1 = swap_uint16(readRegVal1); return readRegVal1; }
static void send_cmd(uint16_t cmd) { cmd = swap_uint16(cmd); GPIO_write(HDC,0); // Pull Down HDC //spi_write((uint8_t *)&cmd, sizeof(uint16_t)); spi_write(cmd); GPIO_write(HDC,1); // Pull UP & release HDC } static void send_params(uint16_t *paramsPtr, size_t n) { size_t i; for(i = 0; i < n; i++) { send_param(paramsPtr[i]); } } static void send_param( uint16_t param) { Display_printf(display, 0, 0, "Sending param 0x%x", param); param = swap_uint16(param); //spi_write((uint8_t *)¶m, sizeof(uint16_t)); spi_write(param); }
void spi_write(uint8_t reg) { masterTxBuffer[0] = reg; spiTransaction.count = SPI_MSG_LENGTH; spiTransaction.txBuf = (void *) masterTxBuffer;; spiTransaction.rxBuf = (void *) masterRxBuffer; transferOK = SPI_transfer(masterSpi, &spiTransaction); if (!transferOK){ Display_printf(display, 0, 0, "Unsuccessful SPI transmit!\n"); while(1); } Display_printf(display, 0, 0, "Successful SPI transmit!\n"); } uint8_t spi_read(uint8_t readFromReg) { masterTxBuffer[0] = readFromReg; masterTxBuffer[1] = 0; memset((void *) masterRxBuffer, 0, SPI_MSG_LENGTH); spiTransaction.count = SPI_MSG_LENGTH; spiTransaction.txBuf = (void *) masterTxBuffer; spiTransaction.rxBuf = (void *) masterRxBuffer; transferOK = SPI_transfer(masterSpi, &spiTransaction); if (!transferOK){ Display_printf(display, 0, 0, "Unsuccessful SPI read!\n"); while(1); } return masterRxBuffer[0]; //Forget the return element. spiTransaction.rxBuf does not gets populated. }