cancel
Showing results for 
Search instead for 
Did you mean: 

Moving on from blinky, how?

rithers
Associate II
Posted on July 19, 2016 at 09:36

Hi my name is Etienne, I am new to microcontrollers and I'm still banging my head against the wall.  

I would like to run a simple USART printf function to get things going.  

To keep it short,  Ive got a Nucleo F103, Keil uVision5, using the 'pack installer' to load up a blinky program works fine,  but as soon as I try to use some of the library functions like

USART_StructInit();,  i get:

warning: implicit declaration invalid in C99,

then I go into RTE manager, add and resolve USART StdPeriph Drivers,

now USART_StructInit(); has too few arguments,  and USARTInit() is undefined.   

Is there any resource which explains how to use Peripheral Library functions, step by step?

I am having such a hard time finding code examples that work!

...spent a few hours trying to get the MCBSTM discovery examples to run, to no avail.  

...spent another few hours trying to step-by-step with the data sheet, still no luck.
11 REPLIES 11
Nesrine M_O
Lead II
Posted on July 19, 2016 at 11:34

Hi stehelin.etenne,

Welcome in the STM32 Community!

“I am having such a hard time finding code examples that work!�

1- if you are SPL user ,I suggest you to review the examples available under the STM32F1 standard peripherals library “STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Examples\USART\Printf�

2- If you are a Cube user, I suggest you to review the examples available in the STM32CubeF1 package. You may find USART examples under «STM32Cube_FW_F1_V1.4.0\Projects\STM32F103RB-Nucleo\Examples\UART\UART_Printf''.

-Syrine-

Nesrine M_O
Lead II
Posted on July 19, 2016 at 11:42

Hi stehelin.etenne,

I'd highly recommend you also to have a look to these two documents, they provide guidelines to novice users on how to build and run a sample application and to create and build their own application around the STM32F103 Nucleo:

•

http://www.st.com/content/ccc/resource/technical/document/user_manual/1b/03/1b/b4/88/20/4e/cd/DM00105928.pdf/files/DM00105928.pdf/jcr:content/translations/en.DM00105928.pdf

•

http://www.st.com/content/ccc/resource/technical/document/user_manual/98/2e/fa/4b/e0/82/43/b7/DM00105823.pdf/files/DM00105823.pdf/jcr:content/translations/en.DM00105823.pdf

-Syrine-

rithers
Associate II
Posted on July 19, 2016 at 20:26

Any ideas on how to Port the projects from STM32F10x_Examples to a Nucleo or more generic format?     It seems like they all have this for a starting point:

#include ''stm32_eval.h''

#ifdef USE_STM32100B_EVAL

 #include ''stm32100b_eval_lcd.h''

#elif defined USE_STM3210B_EVAL

…

and on and on,  for the peripherals.   

Im not using any of the EVAL boards.   

I'd really like to get into the StdPeripheral Examples as there are enough of them to learn quite a lot from;   however I don't feel like investing in a $300 eval board just yet.

Is there a simpler, more generic approach?

thanks so much

/Etienne

Posted on July 19, 2016 at 21:13

Porting requires an appreciation of the hardware attached to each board and the pins.

The NUCLEO boards really don't have anything. The SPL has examples for EVAL boards, and there was a library dealing with the original DISCOVERY-VL boards. The root directory of the SPL had a Windows Help file as I recall, but the STM32 library is similar to ones for prior ST parts. Outputting to the ST-LINK VCP, you could do this

// STM32 Keil printf USART2 (Tx PA.2, Rx PA.3) STM32F103RB NUCLEO - sourcer32@gmail.com
#include ''stm32f10x.h''
#include <
stdio.h
>
#include <
stdlib.h
>
//****************************************************************************
void USART2_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIO and USART2 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USARTx configured as follow:
- BaudRate = 9600 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 9600;
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 configuration */
USART_Init(USART2, &USART_InitStructure);
/* Enable USART */
USART_Cmd(USART2, ENABLE);
}
//****************************************************************************
void OutString(char *s)
{
while(*s)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART2, *s++); // Send Char
}
}
//****************************************************************************
int main(void)
{
RCC_ClocksTypeDef RCC_Clocks;
/*!< 
At
this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f10x_xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f10x.c file
*/
RCC_GetClocksFreq(&RCC_Clocks);
USART2_Configuration();
OutString(''Welcome to Nucleo F103RB

'');
printf(''Hello World!
'');
printf(''Running at %d MHz
'', RCC_Clocks.SYSCLK_Frequency / 1000000);
while(1); // Don't want to exit
}
//****************************************************************************
// Hosting of stdio functionality through USART2
//****************************************************************************
#include <rt_misc.h>
#pragma import(__use_no_semihosting_swi)
struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
FILE __stdin;
int fputc(int ch, FILE *f)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2, ch);
return(ch);
}
int fgetc(FILE *f)
{
char ch;
while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);
ch = USART_ReceiveData(USART2);
return((int)ch);
}
int ferror(FILE *f)
{
/* Your implementation of ferror */
return EOF;
}
void _ttywrch(int ch)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2, ch);
}
void _sys_exit(int return_code)
{
label: goto label; /* endless loop */
}
//****************************************************************************
#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(uint8_t* file, uint32_t 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

'', file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
//****************************************************************************

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
rithers
Associate II
Posted on July 20, 2016 at 00:46

Clive thank you so much for the example.

I must be missing an include, because now all of the Library functions bug:

error: use of undeclared identifier 'USART_InitTypeDef'... and so on. 

Is there a 'master' library header I need to find, containing all the declarations ?

I know this is a very rookie issue,  but I feel like once I get this part,  a lot can happen.

thanks for the help

/etienne

Posted on July 20, 2016 at 01:19

Make sure you have the full F1 v3.5.0 SPL

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32-standard-peripheral-libraries/stsw-stm32054.html

Review the Template Projects. The specific area of concern would be the Include Paths used to find the files, and the Defines passed to compiler.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
AvaTar
Lead
Posted on July 20, 2016 at 08:19

For SPL code, you need to define ''USE_STDPERIPH_DRIVER'' to pull all the required headers in.

This is usually done via including ''stm32fxxx_conf.h'' from within ''stm32fxxx.h'' - if the mentioned macro is defined. (File names depend on MCU family).

Best place would be the project settings (or the make file).

AvaTar
Lead
Posted on July 20, 2016 at 08:23

> Best place would be the project settings (or the make file).

 

If you are about that, add a proper definition for HSE_VALUE if the default value in ''stm32fxxx.h'' doesn't match the quartz on your board.

rithers
Associate II
Posted on July 28, 2016 at 01:40

Thanks everybody for your help.  

After all, I never could get the SPL Examples up & running on my boards. 

Maybe after some time and programming experience, those will be easier to navigate.

I did, however, get CubeMX up & running, and it is fantastic!

Yes, im really a beginner,   and GUI gratification is huge.  

/etienne