cancel
Showing results for 
Search instead for 
Did you mean: 

newbie problem timer / UART

matthiasjunge9
Associate II
Posted on July 09, 2008 at 07:41

newbie problem timer / UART

5 REPLIES 5
matthiasjunge9
Associate II
Posted on May 17, 2011 at 12:39

Hello,

I'm using the Keil STM32 eval board and try to convert my software to the STM32 controller. My program is based on an example project. Now I have 3 questions:

1) In my program I have a little loop waiting for a flag:

unsigned char dbgactive;

...

void debugByteOut(void)

{

while (dbgactive);

....

}

The compiler cannot compile this unless the line is disabled:

sys_main.c: Internal fault: [0xe35e83:310914] in debugByteOut

sys_main.c: Please contact your supplier.

2) The systick timer is running, now I want to use the TIM1 for a delay:

void wait100MicroSec(unsigned char value)

{

TIM1_TimeBaseInitTypeDef timerinitvals;

TIM1_DeInit();

//TIM1 frequency is defined as follow (Prescaler = 0):

//TIM1 frequency = TIM1 counter clock / (TIM1_Period + 1) = 10 KHz.

timerinitvals.TIM1_Period = (72000000 / 10000) - 1;//Period = 100 µsec

timerinitvals.TIM1_Prescaler = 0;

timerinitvals.TIM1_ClockDivision = TIM1_CKD_DIV1;

timerinitvals.TIM1_CounterMode = TIM1_CounterMode_Down;

timerinitvals.TIM1_RepetitionCounter = 0;

TIM1_TimeBaseInit(&timerinitvals);

TIM1_Cmd(ENABLE);

while (value--)

{

while (TIM1_GetFlagStatus(TIM1_FLAG_Update) == RESET);

TIM1_ClearFlag(TIM1_FLAG_Update);

}

TIM1_DeInit();

TIM1_Cmd(DISABLE);

}

The TIM1_FLAG_Update never changes, so the timer is not running, what is wrong with the initialisation?

3) I want to use UART with tx and rx interrupts and configure the following way:

RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA, ENABLE);

/* Configure USART1 Rx (PA10) as input floating */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Configure USART1 Tx (PA9) as alternate function push-pull */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOA, &GPIO_InitStructure);

USART_InitStructure.USART_BaudRate = 115200;

USART_InitStructure.USART_WordLength = USART_WordLength_8b;

USART_InitStructure.USART_StopBits = USART_StopBits_1;

USART_InitStructure.USART_Parity = USART_Parity_No ;

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_InitStructure.USART_Clock = USART_Clock_Disable;

USART_InitStructure.USART_CPOL = USART_CPOL_Low;

USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;

USART_InitStructure.USART_LastBit = USART_LastBit_Disable;

USART_Init(USART1, &USART_InitStructure);

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

//Enable the USART1 Interrupt

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

//Enable the USART Transmit interrupt: this interrupt is generated when the USART transmit data register is empty

USART_ITConfig(USART1, USART_IT_TXE, ENABLE);

//Enable the USART Receive interrupt: this interrupt is generated when the USART receive data register is not empty

USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

USART_Cmd(USART1, ENABLE);

...

void USART1_IRQHandler(void)

{

if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)

{

i = USART_ReceiveData(USART1);

/* Clear the USART1 Receive interrupt */

USART_ClearITPendingBit(USART1, USART_IT_RXNE);

}

if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)

{

USART_SendData(USART1, c);

/* Clear the USART1 transmit interrupt */

USART_ClearITPendingBit(USART1, USART_IT_TXE);

}

}

If I want to run this program it crashed the emulator, any idea what is wrong?

best regards,

matt

st3
Associate II
Posted on May 17, 2011 at 12:39

Quote:

sys_main.c: Please contact your supplier.

So it's asking you to contact your supplier - did you do that?

lanchon
Associate III
Posted on May 17, 2011 at 12:39

1) that's a mistake. ''while (dbgactive);'' can cache the value in dbgactive and enter an infinite loop (it *should* do it). that may be triggering the error. try:

volatile unsigned char dbgactive;

and see if it compiles. but mind you, the rest of the code (which I didn't read) will probably be buggy since you apparently didn't take theading issues into account when you wrote it.

>If I want to run this program it crashed the emulator, any idea what is wrong?

I thought it didn't compile.

matthiasjunge9
Associate II
Posted on May 17, 2011 at 12:39

You are right, it is code from a different controller where no optimization is used, if I declare volatile or disable optimization the compiler works.

Still the timer and UART does not work yet, no idea what is wrong with the configuration.

matthiasjunge9
Associate II
Posted on May 17, 2011 at 12:39

OK, UART problem is the same from the other thread (tx irq constantly triggering when no data available.

Now to the timer: I just want a delay function using timer 1 (without interrupt), can't be that hard, right?

I probably forgot to enable the update event (TIM1_GenerateEvent(TIM1_EventSource_Update);) but what else, it is just a simple downcounter or do I have to do it with interrupts?