I am trying to run a very simple UART test with Necto but I cannot make the UART communicate (it fails on uart_open).
I followed the SDK2 examples, but I suspect that some initialization is missing (pin direction/mapping?).
I am quite sure that all physical connections are ok since I have the same setting working with the legacy SDK, MikroC and similar code.
Here LEDs are used only for debugging (to help the user understand where we are in the code).
The Setup (clock etc.) should be ok too, since I copied it from the old MikroC project and LED sleep times seems correct.
I hope someone can guide me and people in the same situation
Code: Select all
#include "drv_uart.h"
#include "board.h"
#define UART_RX_PIN RD5
#define UART_TX_PIN RD6
#define UART_BAUD_RATE 9600
static uart_t uart; // UART driver context structure.
static uart_config_t uart_cfg; // UART driver configuration structure.
static uint8_t uart_rx_buffer[ 128 ]; // UART RX buffer.
static uint8_t uart_tx_buffer[ 128 ]; // UART TX buffer.
// pin definitions for LEDs
sbit LD1 at LATA0_bit;
sbit LD2 at LATA1_bit;
sbit LD1_Direction at TRISA0_bit;
sbit LD2_Direction at TRISA1_bit;
//blink led1 n times
void blink1(int n) {
for(int i=0; i<n; i++){
LD1 ^= 1;
Delay_ms(50);
LD1 ^= 1;
Delay_ms(50);
}
}
//blink led2 n times
void blink2(int n) {
for(int i=0; i<n; i++){
LD2 ^= 1;
Delay_ms(50);
LD2 ^= 1;
Delay_ms(50);
}
}
void init_leds(){
LD1_Direction = 0;
LD2_Direction = 0;
LD1 = 0;
LD2 = 0;
}
// NOT SURE IF NECESSARY
void init_pins(){
ANCON0 = 0;
ANCON1 = 0;
TRISD5_bit = 1; // declare pin as input
TRISD6_bit = 0; // declare pin as output
LATD = 0; // 0 value in register (latch)
}
void init_my_uart()
{
uart_configure_default( &uart_cfg );
uart.tx_ring_buffer = uart_tx_buffer;
uart.rx_ring_buffer = uart_rx_buffer;
uart_cfg.rx_pin = UART_RX_PIN;
uart_cfg.tx_pin = UART_TX_PIN;
uart_cfg.tx_ring_size = sizeof( uart_tx_buffer );
uart_cfg.rx_ring_size = sizeof( uart_rx_buffer );
Delay_ms(1000);
if ( uart_open( &uart, &uart_cfg ) == UART_ERROR )
{
blink2(1);
return;
}
if ( uart_set_baud( &uart, UART_BAUD_RATE ) == UART_ERROR )
{
blink2(2);
return;
}
while(1){
Delay_ms(1000);
blink1(1);
uart_print( &uart, "Hello World!" );
}
}
void main(){
init_leds();
blink1(3);
blink2(3);
init_pins();
init_my_uart();
}
https://drive.google.com/file/d/1vEj-Ox ... qclfr/view
Thank you for your help.
Luca