Tool/software: TI-RTOS
hi everyone
Recently I cofig SPI master and SPI slave on the MSP_EXP430F5529LP develop board, under TI-RTOS two TASK, I found that two spi interact with each other. When I turn off one of the tasks and the other spi works ok , I try to add semaphores to protect SPI_transfer() funtion, and the problem remains, I put the breakpoint and found that the problem appeared in the following figure, resulting in SPI transfer return false, and spi slave can only send the first byte out. Who can teach me how to do it, or give me a similar example, thank you!
best regards
xc.mo
2.code
#define spiA1_TASKSTACKSIZE 512 SPI_Handle spiA1_master; SPI_Params spiA1_master_Params; Task_Struct spiA1_master_taskStruct; Char spiA1_master_taskStack[spiA1_TASKSTACKSIZE]; GateMutex_Handle gatemutex_handle; GateMutex_Params gatemutex_params; #define spiB1_TASKSTACKSIZE 512 SPI_Handle spiB1_slave; SPI_Params spiB1_slave_Params; Task_Struct spiB1_slave_taskStruct; Char spiB1_slave_taskStack[spiB1_TASKSTACKSIZE]; //Semaphore_Handle spiB1_semHandle; Semaphore_Params spiB1_semParams; Semaphore_Struct spiB1_semStructP; //Semaphore_Handle protect SPI_transfer() Semaphore_Params two_spi_semParams; Semaphore_Struct two_spi_semStructP; // // void spiA1transferCallbackFxn(SPI_Handle handle, SPI_Transaction* transaction) { Board_SPIA1_MASTER_CS_HIGH; Semaphore_post(Semaphore_handle(&two_spi_semStructP)); System_printf("spiA1 transferCallbackFxn inter!\n"); System_flush(); } /* * ======== uscia1_spi taskFxn ======== * Task for this function is created statically. See the project's .cfg file. */ Void spiA1_master_taskFxn(UArg arg0, UArg arg1) { /* Create I2C for usage */ SPI_Params_init(&spiA1_master_Params); spiA1_master_Params.transferMode = SPI_MODE_CALLBACK; spiA1_master_Params.transferCallbackFxn = spiA1transferCallbackFxn; spiA1_master_Params.mode = SPI_MASTER; spiA1_master_Params.bitRate = 500000, /* bitRate Hz*/ spiA1_master_Params.dataSize = 8; spiA1_master_Params.frameFormat = SPI_POL1_PHA1; spiA1_master = SPI_open(Board_SPIA1_MASTER, &spiA1_master_Params); if (spiA1_master == NULL) { System_abort("Error Initializing SPI\n"); } else { System_printf("spiA1 Initialized!\n"); } System_flush(); //gatemute GateMutex_Params_init(&gatemutex_params); gatemutex_handle = GateMutex_create(&gatemutex_params, NULL); if (gatemutex_handle == NULL) { System_abort("Error GateMutex_create\n"); } else { System_printf("GateMutex_create OK!\n"); } char txbuf[5] = {0x21,0x22,0x23,0x24,0x25}; char rxbuf[6]; uint8_t count ; SPI_Transaction spiTransaction; while(1){ //cs low Board_SPIA1_MASTER_CS_LOW; Semaphore_pend(Semaphore_handle(&two_spi_semStructP),BIOS_WAIT_FOREVER); spiTransaction.txBuf = txbuf; spiTransaction.rxBuf = NULL; spiTransaction.count = 5; if (SPI_transfer(spiA1_master, &spiTransaction) == NULL) { System_printf("spiA1 Bus fault\n"); System_flush(); } Task_sleep(1000); GPIO_toggle(Board_LED_P1_0);; } } void spiB1_CS_Fxn1(UArg arg) { Semaphore_post(Semaphore_handle(&spiB1_semStructP)); //System_printf("spiB1 CS P2_4 interrupt callback!\n"); //System_flush(); } Void spiB1_slave_taskFxn(UArg arg0, UArg arg1) { /* Create SPI for usage */ SPI_Params_init(&spiB1_slave_Params); spiB1_slave_Params.transferMode = SPI_MODE_CALLBACK; spiB1_slave_Params.transferCallbackFxn = spiB1_transferCallbackFxn; spiB1_slave_Params.mode = SPI_SLAVE; spiB1_slave_Params.dataSize = 8; spiB1_slave = SPI_open(Board_SPIB1_SLAVE, &spiB1_slave_Params); if (spiB1_slave == NULL) { System_abort("Error Initializing SPI B1\n"); } else { System_printf("SPI B1 Initialized!\n"); } System_flush(); //gatemute GateMutex_Params_init(&gatemutex_spiB1_params); gatemutex_spiB1_handle = GateMutex_create(&gatemutex_spiB1_params, NULL); if (gatemutex_spiB1_handle == NULL) { System_abort("Error gatemutex_spiB1_create\n"); } else { System_printf("gatemutex_spiB1_create OK!\n"); } System_flush(); //CS Semaphore Semaphore_Params_init(&spiB1_semParams); spiB1_semParams.mode = Semaphore_Mode_BINARY; Semaphore_construct(&spiB1_semStructP, 0, &spiB1_semParams); //enable CS(P2_4) interrupt GPIO_setCallback(Board_SPIB1_SLAVE_CS_P2_4, (GPIO_CallbackFxn)spiB1_CS_Fxn1);//much be before GPIO_enableInt() GPIO_enableInt(Board_SPIB1_SLAVE_CS_P2_4); char txbuf[5] = {0x12,0x13,0x14,0x15,0x16}; SPI_Transaction spiTransaction; Task_sleep(100); while(1){ //judge CS by Gpio Interrupt Semaphore_pend(Semaphore_handle(&spiB1_semStructP),BIOS_WAIT_FOREVER); // protect SPI_transfer() Semaphore_pend(Semaphore_handle(&two_spi_semStructP),BIOS_WAIT_FOREVER); spiTransaction.txBuf = txbuf; spiTransaction.rxBuf = NULL; spiTransaction.count = 5; if (SPI_transfer(spiB1_slave, &spiTransaction) == NULL) { System_printf("spiB1 Bus fault\n"); System_flush(); } Semaphore_post(Semaphore_handle(&two_spi_semStructP)); Task_sleep(1000); GPIO_toggle(Board_LED_P4_7); } } int main(void) { /* Call board init functions */ Board_initGeneral(); Board_initGPIO(); Board_initSPI(); //spi master taskParams.arg0 = 1000; taskParams.stackSize = spiA1_TASKSTACKSIZE; taskParams.stack = &spiA1_master_taskStack; Task_construct(&spiA1_master_taskStruct, (Task_FuncPtr)spiA1_master_taskFxn, &taskParams, NULL); //spiB1 slave taskParams.arg0 = 1000; taskParams.stackSize = spiB1_TASKSTACKSIZE; taskParams.stack = &spiB1_slave_taskStack; Task_construct(&spiB1_slave_taskStruct, (Task_FuncPtr)spiB1_slave_taskFxn, &taskParams, NULL); //Two_SPI Semaphore protected SPI_transfer() Semaphore_Params_init(&two_spi_semParams); spiB1_semParams.mode = Semaphore_Mode_BINARY; Semaphore_construct(&two_spi_semStructP, 1, &two_spi_semParams); /* Start BIOS */ BIOS_start(); return (0); }