Hi,
I am trying to develop a simple application which uses SPI2 to connect the STM32 to an external chip over the extension connector of the Primer2.
I am not using the hardware chip select, but pin PC4 instead (CX_ADC1 on extension connector).
I am seeing two problems:
1) PC4 GPIO is not responding on GPIO_WriteBit functions (The pin goes high, but only for a short time)
2) SPI is nog working (no clock signal and data generated when SPI_I2S_SendData function is called).
To further test this, i have made a very basic application which only initializes the SPI and Pin PC4, then tries to make pin PC4 high (does not happen), and then when the orange button is pressed sends a data byte over SPI (nothing happens on SCK or MOSI lines).
Please find my code for the application below.
Does anyone has some ideas on what i might be doing wrong?
Kind regards,
Tim
/*******************************************************************************
* Function Name : Application_Ini
* Description : Initialization function of Circle_App. This function will
* be called only once by CircleOS.
* Input : None
* Return : MENU_CONTINUE_COMMAND
*******************************************************************************/
enum MENU_code Application_Ini ( void )
{
if(strcmp(UTIL_GetVersion(), NEEDEDVERSION) < 0)
{
return MsgVersion();
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
SPI_InitTypeDef SPI_InitStructure;
SPI_StructInit(&SPI_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; /* min 8 at 72Mhz (step 5)*/
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI2, ENABLE);
/* SSn Config (PC4) -------------------------------------------------------------*/
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_WriteBit(GPIOC, GPIO_Pin_4,Bit_SET);
return MENU_CONTINUE_COMMAND;
}
/*******************************************************************************
* Function Name : Application_Handler
* Description : Management of the Circle_App.
*
* Input : None
* Return : MENU_CONTINUE
*******************************************************************************/
enum MENU_code Application_Handler ( void )
{
// TODO: Write your application handling here.
// This routine will get called repeatedly by CircleOS, until we
// return MENU_LEAVE
if( BUTTON_GetState() == BUTTON_PUSHED )
{
BUTTON_WaitForRelease();
BUTTON_SetMode( BUTTON_ONOFF_FORMAIN );
while ((SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) != SET)&&(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET));
GPIO_WriteBit(GPIOC, GPIO_Pin_4,Bit_RESET);
SPI_I2S_SendData(SPI2, 0x05);
GPIO_WriteBit(GPIOC, GPIO_Pin_4,Bit_SET);
return MENU_Quit();
}
return MENU_CONTINUE; // Returning MENU_LEAVE will quit to CircleOS
}
/*******************************************************************************
* Function Name : MsgVersion
* Description : Display the current CircleOS version and the version needed
* exit to main menu after 4 secondes
*
* Input : None
* Return : MENU_CONTINUE
*******************************************************************************/
enum MENU_code MsgVersion(void)
{
int hh,mm,ss,ss2;
DRAW_DisplayString(5,60,"CircleOS",17);
DRAW_DisplayString(80,60,UTIL_GetVersion(),3);
DRAW_DisplayString(5,34,NEEDEDVERSION,3);
DRAW_DisplayString(40,34," required",12);
RTC_GetTime(&hh,&mm,&ss);
ss = ss + 4; // 4 secondes
ss = ss%60;
do
{
RTC_GetTime(&hh,&mm,&ss2);
}while (ss2 != ss); // do while < 4 secondes
return MENU_REFRESH;
}