ZHDA101 July 2025 TXE8116 , TXE8116-Q1 , TXE8124 , TXE8124-Q1 , TXE8148
以下示例代码可直接覆盖替换 MSPM0 SDK 中的 spi_controller_internal_loopback_poll.c 示例代码。此 .c 文件已经过修改,包含适配 TXE81XX 器件 24 位字方案的 SPI 读写函数。
/*SPI Connections:
VCC = 3.3V
CPOL = 0
CPHA = 0
Clock Frequency = 500kHz
SCLK = PA11
MISO = PA16
MOSI = PA18
/CS = PA2
*/
#include "ti_msp_dl_config.h" //MSP driver library
//Define the TXE81xx Register Map from the TXE81xx d
#define scratch_reg (0x00)
#define device_ID (0x01)
#define input_reg (0x02)
#define output_reg (0x03)
#define config_reg (0x04)
#define pol_reg (0x05)
#define pp_od_sel (0x06)
#define od_conf (0x07)
#define pu_pd_en (0x08)
#define pu_pd_sel (0x09)
#define bus_holder (0x0A)
#define smart_int (0x0B)
#define int_mask (0x0C)
#define glitch_filter_en (0x0D)
#define int_flag_status (0x0E)
#define int_port_status (0x0F)
#define fail_safe_en_reg_1 (0x12)
#define fail_safe_en_reg_2 (0x13)
#define fail_safe_dir_config1 (0x14)
#define fail_safe_dir_config2 (0x15)
#define fail_safe_out1 (0x16)
#define fail_safe_out2 (0x17)
#define fail_safe_redun (0x18)
#define fail_safe_fault (0x19)
#define software_reset (0x1A)
//define each port
//TXE8116 - Port 0 / Port 1
//TXE8124 - Port 0 / Port 1 / Port 2
#define port0 (0x00)
#define port1 (0x01)
#define port2 (0x02)
#define DELAY_500khz (50) //delay cycles to tune /CS for SPI comms at 500kHz
//Function Definitions
uint8_t SPI_Transfer(uint8_t feat_addr, uint8_t port, uint8_t data_byte, bool multi_port);
uint8_t SPI_read (uint8_t feat_addr, uint8_t port);
int main(void)
{
volatile uint8_t read_val = 0x00;
SYSCFG_DL_init(); //initalize SPI driver
read_val = SPI_Transfer(config_reg, port0, 0xFF, false); //write to configuration register 0 - set all output
//write outputs every second on port 0
while(1){
read_val = SPI_Transfer(output_reg, port0, 0xFF, false); //0xFF - LED's off
read_val = SPI_read(output_reg, port0);
delay_cycles(24000000);
read_val = SPI_Transfer(output_reg, port0, 0xAA, false); //0xAA - alternate LED's
read_val = SPI_read(output_reg, port0);
delay_cycles(24000000);
read_val = SPI_Transfer(output_reg, port0, 0x55, false); //0x55 - alternate the other way
read_val = SPI_read(output_reg, port0);
delay_cycles(24000000);
read_val = SPI_Transfer(output_reg, port0, 0x00, false); //0x00 - LED's ON
read_val = SPI_read(output_reg, port0);
delay_cycles(24000000);
}
}
//The SPI_Transfer both writes and returns the previous byte set in the 8-bit register
uint8_t SPI_Transfer(uint8_t feat_addr, uint8_t port, uint8_t data_byte, bool multi_port){
uint8_t write_byte = feat_addr & 0x1F; //only keep B20 - B16
uint8_t port_byte = (port << 4) & 0x70; //only keep B14 - B12, first shift port number to upper nibble
uint8_t m_bit;
uint8_t status_byte1 = 0x00; //first 2 bits are 2'b11 (indicating status segment), next 6 bits are D5 to D0 bits of the fault flag register
uint8_t status_byte2 = 0x00; //8 bits are all 0
uint8_t reg_data_out = 0x00; //the current register data
if (multi_port == true){
m_bit = 0x01; // multi-port = 1 = enabled
} else {
m_bit = 0x00; // multi-port = 0 = disabled
}
DL_GPIO_clearPins(GPIO_LEDS_PORT, GPIO_LEDS_USER_LED_1_PIN | GPIO_LEDS_USER_TEST_PIN); //set /CS LOW
DL_SPI_transmitData8(SPI_0_INST, write_byte); //Transmit B23 - B16
DL_SPI_transmitData8(SPI_0_INST, port_byte + m_bit); //Transmit B15 - B8
DL_SPI_transmitData8(SPI_0_INST, data_byte); //Transmit B7 - B0
status_byte1 = DL_SPI_receiveDataBlocking8 (SPI_0_INST); //reading data on SDO 1st byte
status_byte2 = DL_SPI_receiveDataBlocking8 (SPI_0_INST); //reading data on SDO 2nd byte
reg_data_out = DL_SPI_receiveDataBlocking8 (SPI_0_INST); //reading data on SDO 3rd byte - this is the register data returned (previously set byte in register)
delay_cycles(DELAY_500khz); //delay to time chip select
DL_GPIO_setPins(GPIO_LEDS_PORT, GPIO_LEDS_USER_LED_1_PIN | GPIO_LEDS_USER_TEST_PIN); //set /CS HIGH
return reg_data_out; //return the previous register byte
}
//create a function to directly read a register and the status bits for the fault flag register
uint8_t SPI_read (uint8_t feat_addr, uint8_t port){
uint8_t read_byte = (feat_addr & 0x1F) + 0x80; //only keep B20 - B16 and make B23 = 1 for a "READ"
uint8_t port_byte = (port << 4) & 0x70; //only keep B14-B12, first shift port number to upper nibble
uint8_t status_byte1 = 0x00; //first 2 bits are 2'b11 (indicating status segment), next 6 bits are D5 to D0 bits of the fault flag register
uint8_t status_byte2 = 0x00; //8 bits are all 0
uint8_t reg_data_out = 0x00; //the current register data
DL_GPIO_clearPins(GPIO_LEDS_PORT, GPIO_LEDS_USER_LED_1_PIN | GPIO_LEDS_USER_TEST_PIN); //set CS LOW
DL_SPI_transmitData8(SPI_0_INST, read_byte); //send read command byte
DL_SPI_transmitData8(SPI_0_INST, port_byte); //specify the port selection
DL_SPI_transmitData8(SPI_0_INST, 0x00); //0x00 dummy data to shift out 8 bits of register data on MISO
status_byte1 = DL_SPI_receiveDataBlocking8 (SPI_0_INST); //first 8 bits of status
status_byte2 = DL_SPI_receiveDataBlocking8 (SPI_0_INST); //second 8 bits of status
reg_data_out = DL_SPI_receiveDataBlocking8 (SPI_0_INST); //register data byte
delay_cycles(DELAY_500khz); //delay to time chip select
DL_GPIO_setPins(GPIO_LEDS_PORT, GPIO_LEDS_USER_LED_1_PIN | GPIO_LEDS_USER_TEST_PIN); //set CS HIGH
return reg_data_out; //return register byte
}