Other Parts Discussed in Thread: TMDSCNCD263, TMDSHSECDOCK, AM2634, SYSCONFIG
Tool/software:
Hi Team,
I am using this TMDSCNCD263 board and this is the IO Extender TMDSHSECDOCK.
I am having one requirement, have 2 controllers one is AM2634 and second one is STM32 Nucleo Board i need to configure AM2534 board as a Master and STM32 as a Slave Now i have to transfer data from Master to slave and receive data from salve to master
I write down one code but not working can anyone help me to figure out what's the issue
AM2634 (Master Code)
#include <kernel/dpl/DebugP.h>
#include "ti_drivers_config.h"
#include "ti_drivers_open_close.h"
#include "ti_board_open_close.h"
#include <drivers/hw_include/tistdtypes.h>
#define APP_MCSPI_MSGSIZE (128U)
#define APP_MCSPI_TRANSFER_LOOPCOUNT (10U)
uint8_t gMcspiTxBuffer[APP_MCSPI_MSGSIZE];
uint8_t gMcspiRxBuffer[APP_MCSPI_MSGSIZE];
void *mcspi_transmit_receive(void *args)
{
// int32_t status = SystemP_SUCCESS;
uint32_t i, j;
int32_t transferOK;
MCSPI_Transaction spiTransaction;
uint32_t startTimeInUSec, elapsedTimeInUsecs;
Drivers_open();
Board_driversOpen();
DebugP_log("[MCSPI] Transmit and Receive example started ...\r\n");
/* Memfill buffers */
for(i = 0U; i < APP_MCSPI_MSGSIZE; i++)
{
gMcspiTxBuffer[i] = 0x01;
gMcspiRxBuffer[i] = 0U;
}
/* Print transmitted data */
DebugP_log("Transmitted data on SPI0:\r\n");
for(i = 0U; i < APP_MCSPI_MSGSIZE; i++)
{
DebugP_log("%d ", gMcspiTxBuffer[i]);
}
DebugP_log("\r\n");
/* Initiate transfer */
MCSPI_Transaction_init(&spiTransaction);
spiTransaction.channel = gConfigMcspi0ChCfg[0].chNum; // SPI0 as transmitter
spiTransaction.dataSize = 8;
spiTransaction.csDisable = TRUE;
spiTransaction.count = APP_MCSPI_MSGSIZE / (spiTransaction.dataSize/8);
spiTransaction.txBuf = (void *)gMcspiTxBuffer;
spiTransaction.rxBuf = NULL; // No need to receive on SPI0
spiTransaction.args = NULL;
startTimeInUSec = ClockP_getTimeUsec();
for(j = 0U; j < APP_MCSPI_TRANSFER_LOOPCOUNT; j++)
{
transferOK = MCSPI_transfer(gMcspiHandle[CONFIG_MCSPI0], &spiTransaction);
}
elapsedTimeInUsecs = ClockP_getTimeUsec() - startTimeInUSec;
DebugP_log("----------------------------------------------------------\r\n");
DebugP_log("McSPI Clock %d Hz\r\n", gConfigMcspi0ChCfg[0U].bitRate);
DebugP_log("----------------------------------------------------------\r\n");
DebugP_log("Data Width \tData Length \tTransfer Time (micro sec)\r\n");
DebugP_log("%u\t\t%u\t\t%5.2f\r\n", spiTransaction.dataSize, APP_MCSPI_MSGSIZE,
(Float32)(elapsedTimeInUsecs / (uint32_t)APP_MCSPI_TRANSFER_LOOPCOUNT));
DebugP_log("----------------------------------------------------------\r\n\n");
if(SystemP_SUCCESS != transferOK)
{
DebugP_assert(FALSE); /* MCSPI transfer failed!! */
}
// Now, receive data on SPI1
MCSPI_Transaction_init(&spiTransaction);
spiTransaction.channel = gConfigMcspi1ChCfg[0].chNum; // SPI1 as receiver
spiTransaction.dataSize = 8;
spiTransaction.csDisable = TRUE;
spiTransaction.count = APP_MCSPI_MSGSIZE / (spiTransaction.dataSize/8);
spiTransaction.txBuf = NULL; // No need to transmit on SPI1
spiTransaction.rxBuf = (void *)gMcspiRxBuffer;
spiTransaction.args = NULL;
transferOK = MCSPI_transfer(gMcspiHandle[CONFIG_MCSPI1], &spiTransaction);
if(SystemP_SUCCESS != transferOK)
{
DebugP_assert(FALSE); /* MCSPI transfer failed!! */
}
else
{
/* Print received data */
DebugP_log("Received data on SPI1:\r\n");
for(i = 0U; i < APP_MCSPI_MSGSIZE; i++)
{
DebugP_log("%d ", gMcspiRxBuffer[i]);
}
DebugP_log("\r\n");
}
Board_driversClose();
Drivers_close();
return NULL;
}
STM32 (Slave Code)
#ifndef HSEM_ID_0
#define HSEM_ID_0 (0U) /* HW semaphore 0*/
#endif
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
#define CS_PIN GPIO_PIN_4
#define CS_GPIO_PORT GPIOA
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
#if defined ( __ICCARM__ ) /*!< IAR Compiler */
#pragma location=0x30000000
ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT]; /* Ethernet Rx DMA Descriptors */
#pragma location=0x30000200
ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT]; /* Ethernet Tx DMA Descriptors */
#elif defined ( __CC_ARM ) /* MDK ARM Compiler */
__attribute__((at(0x30000000))) ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT]; /* Ethernet Rx DMA Descriptors */
__attribute__((at(0x30000200))) ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT]; /* Ethernet Tx DMA Descriptors */
#elif defined ( __GNUC__ ) /* GNU Compiler */
ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT] __attribute__((section(".RxDecripSection"))); /* Ethernet Rx DMA Descriptors */
ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT] __attribute__((section(".TxDecripSection"))); /* Ethernet Tx DMA Descriptors */
#endif
ETH_TxPacketConfig TxConfig;
ETH_HandleTypeDef heth;
SPI_HandleTypeDef hspi1;
UART_HandleTypeDef huart3;
PCD_HandleTypeDef hpcd_USB_OTG_FS;
/* USER CODE BEGIN PV */
// Buffer for received data
#define BUFFER_SIZE 100
uint8_t rxBuffer[BUFFER_SIZE];
uint8_t txBuffer[BUFFER_SIZE] = "Data received and sent back"; // Example response data
// Flag to indicate data reception
volatile uint8_t dataReceivedFlag = 0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART3_UART_Init(void);
static void MX_USB_OTG_FS_PCD_Init(void);
static void MX_SPI1_Init(void);
static void MX_ETH_Init(void);
/* USER CODE BEGIN PFP */
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi);
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi);
void StartSPICommunication(void);
void SetCSHigh(void);
void SetCSLow(void);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int __io_putchar(int ch) {
// Transmit one character to the UART bit by bit
// Transmit one character to the UART
HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
return ch;
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* USER CODE BEGIN Boot_Mode_Sequence_0 */
int32_t timeout;
/* USER CODE END Boot_Mode_Sequence_0 */
/* USER CODE BEGIN Boot_Mode_Sequence_1 */
/* Wait until CPU2 boots and enters in stop mode or timeout*/
timeout = 0xFFFF;
while((__HAL_RCC_GET_FLAG(RCC_FLAG_D2CKRDY) != RESET) && (timeout-- > 0));
if ( timeout < 0 )
{
Error_Handler();
}
/* USER CODE END Boot_Mode_Sequence_1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN Boot_Mode_Sequence_2 */
/* When system initialization is finished, Cortex-M7 will release Cortex-M4 by means of
HSEM notification */
/*HW semaphore Clock enable*/
__HAL_RCC_HSEM_CLK_ENABLE();
/*Take HSEM */
HAL_HSEM_FastTake(HSEM_ID_0);
/*Release HSEM in order to notify the CPU2(CM4)*/
HAL_HSEM_Release(HSEM_ID_0,0);
/* wait until CPU2 wakes up from stop mode */
timeout = 0xFFFF;
while((__HAL_RCC_GET_FLAG(RCC_FLAG_D2CKRDY) == RESET) && (timeout-- > 0));
if ( timeout < 0 )
{
Error_Handler();
}
/* USER CODE END Boot_Mode_Sequence_2 */
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART3_UART_Init();
MX_USB_OTG_FS_PCD_Init();
MX_SPI1_Init();
MX_ETH_Init();
/* USER CODE BEGIN 2 */
// Start SPI communication
// StartSPICommunication();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
timeout = HAL_SPI_Receive(&hspi1, rxBuffer, 5, 100);
if(timeout == 0 )
{
printf("Received data");
}
if (dataReceivedFlag)
{
// Reset flag
dataReceivedFlag = 0;
// Set CS pin low to select the SPI device
SetCSLow();
// Echo back received data
if (HAL_SPI_Transmit_IT(&hspi1, rxBuffer, BUFFER_SIZE) != HAL_OK)
{
Error_Handler();
}
// Set CS pin high to deselect the SPI device
// SetCSHigh();
}
}
/* USER CODE END 3 */
}
/**
* @brief SPI1 Initialization Function
* @param None
* @retval None
*/
static void MX_SPI1_Init(void)
{
/* USER CODE BEGIN SPI1_Init 0 */
/* USER CODE END SPI1_Init 0 */
/* USER CODE BEGIN SPI1_Init 1 */
/* USER CODE END SPI1_Init 1 */
/* SPI1 parameter configuration*/
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_SLAVE;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 0x0;
hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
hspi1.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
hspi1.Init.FifoThreshold = SPI_FIFO_THRESHOLD_16DATA;
hspi1.Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
hspi1.Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
hspi1.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;
hspi1.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
hspi1.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
hspi1.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_DISABLE;
hspi1.Init.IOSwap = SPI_IO_SWAP_DISABLE;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN SPI1_Init 2 */
/* USER CODE END SPI1_Init 2 */
}
/* USER CODE BEGIN 4 */
void SetCSHigh(void)
{
HAL_GPIO_WritePin(CS_GPIO_PORT, CS_PIN, GPIO_PIN_SET);
}
void SetCSLow(void)
{
HAL_GPIO_WritePin(CS_GPIO_PORT, CS_PIN, GPIO_PIN_RESET);
}
void StartSPICommunication(void)
{
// Start SPI reception in interrupt mode
if (HAL_SPI_Receive_IT(&hspi1, rxBuffer, BUFFER_SIZE) != HAL_OK)
{
Error_Handler();
}
}
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
{
if (hspi->Instance == SPI1)
{
dataReceivedFlag = 1;
// Transmit received data over UART3
printf("Received data");
HAL_UART_Transmit(&huart3, rxBuffer, BUFFER_SIZE, HAL_MAX_DELAY);
// Restart SPI reception
if (HAL_SPI_Receive_IT(&hspi1, rxBuffer, BUFFER_SIZE) != HAL_OK)
{
Error_Handler();
}
}
}
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
{
if (hspi->Instance == SPI1)
{
// Transmission complete callback
// You can add any additional code here if needed
}
}
/* USER CODE END 4 */