2022-05-11 10:30 AM
Hello, I'm having some problems with UART1 in STM8S001J3. I'm using the simple code below (the compilation is ok), but when I try to programming the MCU in STVP, a error happen and the MCU doesn't work anymore, even though I try to programming with a funciotonal firmware (like only GPIO port set). Anyone have any idea how to fix this? Or if there is another way to use UART1 in this MCU.
If I put this contidion in DISABLE, the programming process works normally, but UART doesn't work.
CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1, ENABLE);
#include "STM8S.h"
void clock_setup(void);
void GPIO_setup(void);
void UART1_setup(void);
void main(void)
{
unsigned char i = 0;
char ch = 0;
clock_setup();
GPIO_setup();
UART1_setup();
while(TRUE)
{
if(UART1_GetFlagStatus(UART1_FLAG_RXNE) == TRUE)
{
ch = UART1_ReceiveData8();
UART1_ClearFlag(UART1_FLAG_RXNE);
UART1_SendData8(i + 0x30);
}
if(UART1_GetFlagStatus(UART1_FLAG_TXE) == FALSE)
{
i++;
}
};
}
void clock_setup(void)
{
CLK_DeInit();
CLK_HSECmd(DISABLE);
CLK_LSICmd(DISABLE);
CLK_HSICmd(ENABLE);
while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == FALSE);
CLK_ClockSwitchCmd(ENABLE);
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV8);
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI,
DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_I2C, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_SPI, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_ADC, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_AWU, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1, ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, DISABLE);
}
void GPIO_setup(void)
{
GPIO_DeInit(GPIOD);
GPIO_Init(GPIOD, GPIO_PIN_5, GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_DeInit(GPIOA);
GPIO_Init(GPIOA, GPIO_PIN_3, GPIO_MODE_IN_PU_NO_IT);
}
void UART1_setup(void)
{
UART1_DeInit();
UART1_Init(9600,
UART1_WORDLENGTH_8D,
UART1_STOPBITS_1,
UART1_PARITY_NO,
UART1_SYNCMODE_CLOCK_DISABLE,
UART1_MODE_TXRX_ENABLE);
UART1_Cmd(ENABLE);
}
When this error happen, MCU doesn't work anymore:
Solved! Go to Solution.
2022-05-12 07:03 AM
A simple delay in the start of main void solved the problem. The flash of MCU and UART are working now.
2022-05-11 10:49 AM
>>When this error happen, MCU doesn't work anymore
Odd that when you reconfigure the pin that SWIM is using, it doesn't work subsequently.. Perhaps have some delay between reset, and reassigning it, so the debugger has a window to connect.
PD5 and PA3 (REMAPPED) are UART1_TX pins
PD6 would be UART1_RX
2022-05-11 11:09 AM
Hi, sorry for that. I was trying to change the TX port to see if this was the problem and I forgot to change back. But the error still happen with correct ports.
GPIO_DeInit(GPIOD);
GPIO_Init(GPIOD, GPIO_PIN_5, GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_Init(GPIOD, GPIO_PIN_6, GPIO_MODE_IN_PU_NO_IT);
2022-05-11 12:10 PM
>>But the error still happen with correct ports.
This is the 8-pin STM8 device right? What else uses PIN 8 ?
2022-05-11 12:35 PM
Yes, STM8S001 is a 8-PIN MCU. PIN 8 is a default SWIM of this MCU e and I only use as TX pin. I tried to use PA3 as TX but also doesn't work.
If I put this contidion in DISABLE, the programming process works normally, but UART doesn't work.
CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1, ENABLE);
2022-05-11 12:46 PM
PA3 would require a remapped configuration, (AFR0=1 and AFR1=1)
Pin 8 is bonded to 4 nodes on the internal IC, if you put one in Push-Pull mode it's going to preclude others being used as bidirectional, and you'll be fighting over who can sink the most current.
2022-05-12 07:03 AM
A simple delay in the start of main void solved the problem. The flash of MCU and UART are working now.
2022-11-09 05:45 AM
Hi,
How to apply the delay to main function?
i applied delay by using Tim4 still i am getting the problem controllers are unable to reprogram and
by using remap also i am unable to use UART.