Skip to main content
Visitor II
January 30, 2021
Question

MCU STM8 as SPI slave

  • January 30, 2021
  • 0 replies
  • 604 views

Hello. I want to make the controller a slave for the SPI. I understand the hardware activation Support leg stm8 at present, as it was then activated receiving \ transmitting?

/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"
 
/* Private defines -----------------------------------------------------------*/
#define TIM4_PERIOD 124
#define USER_TIME_CYCLE 10
 
unsigned int system_plc_time = USER_TIME_CYCLE;
bool Start_SPI = FALSE;
//bool Start_SPI = TRUE;
uint8_t system_buff_mb_msg_rx[10];
 
/* Private function prototypes -----------------------------------------------*/
static void CLK_Config(void);
static void TIM4_Config(void);
static void SPI_Config(void);
static void EXTI_Config(void);
 
/* Private functions ---------------------------------------------------------*/
static void CLK_Config(void)
{
 CLK_DeInit();
 CLK_HSICmd(ENABLE);
 CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
 CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
 //CLK_CCOConfig(CLK_OUTPUT_MASTER);
}
 
static void TIM4_Config(void)
{
 TIM4_DeInit();
 TIM4_TimeBaseInit(TIM4_PRESCALER_128, TIM4_PERIOD);
 TIM4_ClearFlag(TIM4_FLAG_UPDATE);
 TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
 TIM4_Cmd(ENABLE);
 TIM4_ClearITPendingBit(TIM4_IT_UPDATE);
}
 
static void SPI_Config(void)
{
 SPI_DeInit();
 SPI_Init(SPI_FIRSTBIT_MSB,
 SPI_BAUDRATEPRESCALER_2,
 SPI_MODE_SLAVE,
 SPI_CLOCKPOLARITY_LOW,
 SPI_CLOCKPHASE_1EDGE,
 SPI_DATADIRECTION_2LINES_FULLDUPLEX,
 SPI_NSS_SOFT,
 0x07);
 SPI_NSSInternalSoftwareCmd(DISABLE);
 SPI_ITConfig(SPI_IT_RXNE, ENABLE);
 //SPI_ITConfig(SPI_IT_WKUP, ENABLE);
 SPI_Cmd(ENABLE);
}
 
static void EXTI_Config(void)
{
 EXTI_DeInit();
 GPIO_Init(GPIOA, GPIO_PIN_3, GPIO_MODE_IN_PU_IT);
 EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOA, EXTI_SENSITIVITY_FALL_LOW);
 
}
 
INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
{
 disableInterrupts();
 system_plc_time--;
 TIM4_ClearITPendingBit(TIM4_IT_UPDATE);
 enableInterrupts();
}
 
INTERRUPT_HANDLER(SPI_IRQHandler, 10)
{
 disableInterrupts();
 //if (Start_SPI == TRUE)
 //{
 Start_SPI = FALSE;
 //SPI_ITConfig(SPI_IT_RXNE, DISABLE);
 system_buff_mb_msg_rx[0] = SPI_ReceiveData();
 SPI_SendData(system_buff_mb_msg_rx[0]);
 //}
 enableInterrupts();
}
 
INTERRUPT_HANDLER(EXTI_PORTA_IRQHandler, 3)
{
 disableInterrupts();
 Start_SPI = TRUE;
 //SPI_ITConfig(SPI_IT_RXNE, ENABLE);
 enableInterrupts();
}
 
void main(void)
{
 CLK_Config();
 TIM4_Config();
 SPI_Config();
 EXTI_Config();
 enableInterrupts();
 
 /* Infinite loop */
 while (1)
 { 
 if(!system_plc_time)
 {
 //SPI_SendData(127);
 system_plc_time = USER_TIME_CYCLE;
 }
 }
 
}
 
 
#ifdef USE_FULL_ASSERT
 
/**
 * @brief Reports the name of the source file and the source line number
 * where the assert_param error has occurred.
 * @param file: pointer to the source file name
 * @param line: assert_param error line source number
 * @retval : None
 */
void assert_failed(u8* file, u32 line)
{ 
 /* User can add his own implementation to report the file name and line number,
 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 
 /* Infinite loop */
 while (1)
 {
 }
}
#endif

in code i rx/tx one byte for test

    This topic has been closed for replies.