Hello!
I am trying to make a simple UART Echo application for a processor from family SKEAZN64. I simply want to get back any character send from a terminal via serial port.
My code consists of the following three files:
1. main.c
#include <stdint.h>
#include "SKEAZN642.h"
#include "UART.h"
void Clk_Init()
{
ICS_C1 |= ICS_C1_IRCLKEN_MASK;
ICS_C3 = 0x50;
while ( !(ICS_S & ICS_S_LOCK_MASK) );
ICS_C2 |= ICS_C2_BDIV( 1 );
????????
}
void Uart_Interrupt(uint8_t data)
{
Uart_SendChar( data );
}
int main(void)
{
Clk_Init();
UART_Init();
Uart_SetCallback( Uart_Interrupt );
NVIC_EnableIRQ( UART2_IRQn );
Uart_SendChar( 0x57 );
Uart_SendChar( 0x45 );
Uart_SendChar( 0x4c );
Uart_SendChar( 0x43 );
Uart_SendChar( 0x4f );
Uart_SendChar( 0x4d );
Uart_SendChar( 0x45 );
Uart_SendChar( 0xD );
Uart_SendChar( 0xA );
for ( ;; )
{
static int i = 0;
i++;
}
return 0;
}
2. UART.h
#ifndef UART_H_
#define UART_H_
#include <stdint.h>
#include "SKEAZN642.h"
typedef void(*pt2Func)(void);
typedef void(*pt2FuncU8)(uint8_t);
void UART_Init(void);
void Uart_SendChar(uint8_t send);
uint8_t Uart_GetChar(void);
void Uart_SetCallback(pt2FuncU8 ptr);
#endif
3. UART.c
pt2FuncU8 Uart_Callback;
void UART2_IRQHandler(void);
void UART_Init()
{
SIM_SCGC |= SIM_SCGC_UART2_MASK;
UART2_BDH = 0;
UART2_BDL = 128;
UART2_C1 = 0;
UART2_C2 |= UART_C2_TE_MASK;
UART2_C2 |= UART_C2_RE_MASK;
UART2_C2 |= UART_C2_RIE_MASK;
}
void Uart_SetCallback(pt2FuncU8 ptr)
{
Uart_Callback = ptr;
}
void Uart_SendChar(uint8_t send)
{
while ( (UART2_S1 & UART_S1_TDRE_MASK) == 0 )
;
(void)UART2_S1;
UART2_D = send;
}
uint8_t Uart_GetChar()
{
uint8_t recieve;
while ( ( UART2_S1 & UART_S1_RDRF_MASK) == 0 )
;
(void) UART2_S1;
recieve = UART2_D;
return recieve;
}
void UART2_IRQHandler()
{
(void)UART2_S1;
Uart_Callback( Uart_GetChar() );
}
Now the code have some big problems:
1. Instead of sending initially "WELCOME\r\n" it just send "WEL_and_some_garbage". My guess is that it is not waiting for transmission buffer to be empty! But why not?
This is the loop which should block execution until another char can be send:
while ( (UART2_S1 & UART_S1_TDRE_MASK) == 0 );
But what's wrong with it?
2. The clock initialization
The function was extracted from official documentation AN4942:
void Clk_Init()
{
ICS_C1 |= ICS_C1_IRCLKEN_MASK;
ICS_C3 = 0x50;
while ( !(ICS_S & ICS_S_LOCK_MASK) );
ICS_C2 |= ICS_C2_BDIV( 1 );
????????
}
However, there is an error on the last commented line: error: assignment of read-only member 'S'
Have no clue why so I just commented that line!
3. The callback function is not called when a character is send from UART0.
PS: The UART2 port works fine with processor Expert.
PS2: Most of the code is from: https://www.nxp.com/docs/en/application-note/AN4942.pdf
I also have attached the project archive in case you may want to give a try!