I found why the system hangs when I run the SSP_SSP0PinsInit() function. The reason is that this function assigns a couple of pins include the MOSI P09. But I already assign it once earlier to blink a LED. The CooCox seems confused by the double assignment of MOSI and goes crazy. I changed the LED pin from P09 to P03 and the system hang problem disappeared.
But still found the SPI thing did not transmit anything. I used the scope to find clock not ticking. I googled CooCox to find that I need to reset the SSP peripheral and also to set SPI0 clock divider.
// Reset SSP0
SYSCON_PeriphResetCmd(SYSCON_RSTPeriph_SSP0, ENABLE);
SYSCON_PeriphResetCmd(SYSCON_RSTPeriph_SSP0, DISABLE);
SYSCON_SetSPI0ClockDiv(10);
Then I found the clock is ticking happily.
Problems solved. Lunch time.
.END
// ***********************************************************************
// Program - Fong EKG v5.0
// Function - Olimex EKG Board Evaluation
// Author - TL Fong
// Build - 2013.10.19.02
// Date - 2013oct19hkt1257
// Hardware - Olimex/CooCox/MagicBlue/WHUT/Somy LPC1114/C14/301/FN28
// Olimex EKG board (SHIELD-EKG/EMG Rev B, Bulgaria 2011)
// Software - GCC ARM 4.7, CoIDE 1.7.4, CoLinkEx 1.1, Flash Magic v7.51
// ***********************************************************************
#include "test050.h" // main tests
// ***********************************************************************
// Main Function
// ***********************************************************************
int main()
{
...
testSpi01();
return 0;
}
// ***********************************************************************
// End
// ***********************************************************************
// ***********************************************************************
// test050.h 2013oct19hkt1257
// ***********************************************************************
#include "led050.h"
#include "key050.h"
#include "spi050.h"
void testSpi01() // 2013oct19
{
// *** Blink LED at P03 ***
setupGpioPinOutputLow050(PortPinArrayP03); // setup GPIO pin as output
blinkLed0501(PortPinArrayP03); // blink led
// *** Test SPI loopback ***
loopBackSpi01();
}
...
}
// ***********************************************************************
// End
// ***********************************************************************
// ***********************************************************************
// spi050.h 2013oct19hkt1108
// ***********************************************************************
#include "lpc11xx_ssp.h"
#include "led050.h"
void loopBackSpi01()
{
// Setup and initialize buffers ***
#define BUFFER_SIZE 0x04
uint8_t Tx_Buf[BUFFER_SIZE];
uint8_t Rx_Buf[BUFFER_SIZE];
bufferInit(Tx_Buf, Rx_Buf);
// Enable SSP0 block clock
SYSCON_AHBPeriphClockCmd(SYSCON_AHBPeriph_SSP0, ENABLE);
// Reset SSP0
SYSCON_PeriphResetCmd(SYSCON_RSTPeriph_SSP0, ENABLE);
SYSCON_PeriphResetCmd(SYSCON_RSTPeriph_SSP0, DISABLE);
SYSCON_SetSPI0ClockDiv(10);
// Assign GPIO pins for SPI
SSP_SSP0PinsInit(SCK0_PIO0_6, ENABLE);
// Initialize SSP with default configuration (Master mode, 8 bit data)
SSP_CFG_Type SSP_ConfigStruct;
SSP_ConfigStructInit(&SSP_ConfigStruct);
SSP_Init(LPC_SSP0, &SSP_ConfigStruct);
// Enable SSP peripheral
SSP_Cmd(LPC_SSP0, ENABLE);
// *** Test loop back using SSP_ReadWrite ********************************
// Setup xfer data configuration struct
SSP_DATA_SETUP_Type xferConfig;
xferConfig.tx_data = Tx_Buf;
xferConfig.rx_data = Rx_Buf;
xferConfig.length = BUFFER_SIZE;
// Testing transfer
int i;
for (i = 0; i < 100000; i++)
{
delayMilliSecond(1);
setGpioDataPinHigh01(PortPinArrayP03);
SSP_ReadWrite(LPC_SSP0, &xferConfig, SSP_TRANSFER_POLLING);
setGpioDataPinLow01(PortPinArrayP03);
}
// End of SSP_ReadWrite loopback *****************************************
// Check if SPI transfer OK
int xferStatus;
xferStatus = xferCheck(Tx_Buf, Rx_Buf);
if (xferStatus == 1)
blinkOneLed050(PortPinArrayP03, ONE_FIFTH_SECOND, ONE_HALF_SECOND, BLINK_6_TIMES);
else
blinkOneLed050(PortPinArrayP03, ONE_FIFTH_SECOND, ONE_HALF_SECOND, BLINK_8_TIMES);
}
// *** Private SPI test functions ***
void bufferInit(uint8_t Tx_Buf[BUFFER_SIZE], uint8_t Rx_Buf[BUFFER_SIZE] )
{
uint8_t i;
for (i = 0; i < BUFFER_SIZE; i++)
{
Tx_Buf[i] = i;
Rx_Buf[i] = 0;
}
}
int xferCheck(uint8_t Tx_Buf[BUFFER_SIZE], uint8_t Rx_Buf[BUFFER_SIZE])
{
uint8_t i;
uint8_t *src_addr = (int *) &Tx_Buf[0];
uint8_t *dest_addr = (int *) &Rx_Buf[0];
for ( i = 0; i < BUFFER_SIZE; i++ )
{
if ( *src_addr++ != *dest_addr++ )
{
return 0;
}
}
return 1;
}
// ***********************************************************************
// End
// ***********************************************************************
.END
No comments:
Post a Comment