BabyStep3: PRINTF with UART [Check]

The PRINTF with UART is working now with Interrupt Handler.
Even the Interrupt Handler in this case is not so complex, however, together with the procedure for registering Interrupt Handler requires some document reading.
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
/* Enable the USARTy Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
Another approach is to redirect the printf/scanf to use USART* by means of implementing fputc and fgetc as following:
int fputc(int ch, FILE *f){
	/* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(USART1, (uint8_t) ch);
  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) ;
  return ch;

int fgetc(FILE *f){
	int c = 0;
	while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET) ;
	c = (int) USART_ReceiveData(USART1);
	fputc(c, f);
	return c;
1. Checking Status Flag (TC, RXNE) is still required
2. Neither TXE nor RXNE interrupt should be enabled in this way
3. scanf, printf functions are not as full-feature as standard C library function in other platforms
    e.g. with test code
int a = 0;
int b = 0;
int c = 0;
scanf("%d", &a);
scanf("%d", &b);
c = a + b;
printf("a+b = %d\r\n", c);
the output from picocom application looks like:

Step4. PWM Outputs, Timer etc [____]

Step4. USART with DMA [____]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s