Hi xiangjun.rong,
Based on your comments i did use delays in the appropriate instance and now the code works just fine.
Here is the code:
#include <string.h>
#include "fsl_spi_master_driver.h"
#include "fsl_clock_manager.h"
#include "fsl_debug_console.h"
#include "board.h"
#include <stdio.h>
// MCP23S17 SPI Slave Device
#define SPI_SLAVE_ID 0x40
#define SPI_SLAVE_ADDR 0x07 // A2=0,A1=0,A0=0
#define SPI_SLAVE_WRITE 0x00
#define SPI_SLAVE_READ 0x01
// MCP23S17 Registers Definition for BANK=0 (default)
#define IODIRA 0x00
#define IODIRB 0x10
#define IOCONA 0x0A
#define GPPUA 0x06
#define GPPUB 0x16
#define GPIOA_Exp 0x09
#define GPIOB_Exp 0x19
/*******************************************************************************
* Definitions
******************************************************************************/
#define SPI_MASTER_INSTANCE (0) /*! User change define to choose SPI instance */
#define TRANSFER_SIZE (64)
#define TRANSFER_BAUDRATE (1000000U) /*! Transfer baudrate - 500k */
#define MASTER_TRANSFER_TIMEOUT (5000U) /*! Transfer timeout of master - 5s */
void wait_tcy(unsigned int wait);
/* Setup the board as a spi master */
int main (void)
{
uint32_t spiSourceClock;
SPI_Type * spiBaseAddr = g_spiBase[SPI_MASTER_INSTANCE];;
/* init the hardware, this also sets up up the SPI pins for each specific SoC */
hardware_init();
OSA_Init();
// Enable clock for SPI
CLOCK_SYS_EnableSpiClock(SPI_MASTER_INSTANCE);
// configure the run-time state struct with the source clock value
spiSourceClock = CLOCK_SYS_GetSpiFreq(SPI_MASTER_INSTANCE);
// Reset the SPI module to it's default state, which includes SPI disabled
SPI_HAL_Init(spiBaseAddr);
// Set SPI to master mode
SPI_HAL_SetMasterSlave(spiBaseAddr, kSpiMaster);
// Set slave select to GPIO output mode
SPI_HAL_SetSlaveSelectOutputMode(spiBaseAddr, kSpiSlaveSelect_AsGpio); /*Change introduced by me since the data need to be sent 3bytes at a time, i.e. 1)SlaveID, 2)Register,3)value*/
// Set the SPI pin mode to normal mode
SPI_HAL_SetPinMode(spiBaseAddr, kSpiPinMode_Normal);
Usr_SPI0_EN; //Defined and configured in gpio_pins.h,pin_mux.c and board.h
Usr_SPI_OFF;
// SPI system Enable
SPI_HAL_Enable(spiBaseAddr);
// Configure the bus to access the provided device.
SPI_HAL_SetBaud(spiBaseAddr, TRANSFER_BAUDRATE, spiSourceClock);
// Setup format as same as slave
SPI_HAL_SetDataFormat(spiBaseAddr, kSpiClockPolarity_ActiveHigh, kSpiClockPhase_FirstEdge, kSpiMsbFirst);
// Disable module to clear the shift register
SPI_HAL_Disable(spiBaseAddr);
SPI_HAL_Enable(spiBaseAddr);
//IOCONA register is set to 0x88
Usr_SPI_ON;
SPI_HAL_WriteDataBlocking(spiBaseAddr, SPI_SLAVE_ID);
SPI_HAL_WriteDataBlocking(spiBaseAddr, IOCONA);
SPI_HAL_WriteDataBlocking(spiBaseAddr, 0x88); //Tried 0x80 as well for Address mode disbaled
OSA_TimeDelay(1); //wait for 1ms
Usr_SPI_OFF;
//PORTA of IO-expander is set to Output mode
Usr_SPI_ON;
SPI_HAL_WriteDataBlocking(spiBaseAddr,SPI_SLAVE_ID);
SPI_HAL_WriteDataBlocking(spiBaseAddr, IODIRA);
SPI_HAL_WriteDataBlocking(spiBaseAddr, 0x00);
OSA_TimeDelay(1); //wait for 1ms
Usr_SPI_OFF;
//Set all pins of PORTA high
Usr_SPI_ON;
SPI_HAL_WriteDataBlocking(spiBaseAddr, SPI_SLAVE_ID);
SPI_HAL_WriteDataBlocking(spiBaseAddr, GPIOA_Exp);
SPI_HAL_WriteDataBlocking(spiBaseAddr, 0x05);
OSA_TimeDelay(1); //wait for 1ms
Usr_SPI_OFF;
while(1)
{
//Set all pins of PORTA high
Usr_SPI_ON;
SPI_HAL_WriteDataBlocking(spiBaseAddr, SPI_SLAVE_ID);
SPI_HAL_WriteDataBlocking(spiBaseAddr, GPIOA_Exp);
SPI_HAL_WriteDataBlocking(spiBaseAddr, 0x05);
OSA_TimeDelay(1); //wait for 1ms
Usr_SPI_OFF;
OSA_TimeDelay(1000); //wait for 1sec
//Set all pins of PORTA high
Usr_SPI_ON;
SPI_HAL_WriteDataBlocking(spiBaseAddr, SPI_SLAVE_ID);
SPI_HAL_WriteDataBlocking(spiBaseAddr, GPIOA_Exp);
SPI_HAL_WriteDataBlocking(spiBaseAddr, 0x00);
OSA_TimeDelay(1); //wait for 1ms
Usr_SPI_OFF;
OSA_TimeDelay(1000); //wait for 1sec
}
}
void wait_tcy(unsigned int wait)
{
do
{
;
}while(--wait!=0);
}
/*******************************************************************************
* EOF
******************************************************************************/
I did try waiting for 100 time cycles instead of 1ms which according to the datasheet should be sufficient but doesn't work always,
This isn't the case in PIC18f controllers wherein you could toggle the GPIO to disable SPI immediately. Am i missing something???
Regards,
Nelson Lobo