Witam, Mógł by ktoś sprawdzić czy mój kod jest prawidłowy? Ponieważ nie mogę odebrać danych z Esp przez protokół UART.
[/i][/i]
Dodano po 1 [minuty]:
*
* uart.c
*
* Created on: 11 lut 2021
* Author: admin
*/
// uart.c
#include "prj.h"
volatile char uart_buf[MAX_UART_BUF_SIZE];
volatile char uart_rsp[MAX_UART_BUF_SIZE];
volatile int uart_buf_idx =0;
volatile int uart_char_size =0;
volatile u8 command_ready = FALSE;
volatile u8 http_get = 0;
volatile u8 rsp_greater = 0;
volatile u8 rsp_sendok=0;
volatile u8 rsp_control=0;
volatile u8 led;
volatile u8 led_value;
volatile u8 rsp_got_ip;
volatile u8 rsp_found=0;
void uart_init(uint32_t baud)
{
// fosc = 8000000 Hz
// baud=9600.0,
UBRRL = 0x33;
UBRRH = 0x00;
// enable uart N81
UCSRB = _BV(RXEN) | _BV(TXEN) | _BV(RXCIE) ;
UCSRC = _BV(URSEL) | _BV(UCSZ1) | _BV(UCSZ0);
memset( (void *) &uart_buf[0], 0, MAX_UART_BUF_SIZE);
}
void uart_send_char(unsigned char ch)
{
while (! (UCSRA & _BV(UDRE)) );
UDR = ch;
}
void uart_sendString(char *s)
{
unsigned int i=0;
while (s[i] != '\x0')
{
uart_send_char(s[i++]);
};
}
void uart_sendString_hex(char *s)
{
unsigned int i=0;
while (s[i] != '\x0')
{
uart_send_hex(s[i++]);
uart_send_char(' ');
};
}
void uart_send_hex(unsigned char ch)
{
unsigned char i,temp;
for (i=0; i<2; i++)
{
temp = (ch & 0xF0)>>4;
if ( temp <= 9)
uart_send_char ( '0' + temp);
else
uart_send_char( 'A' + temp - 10);
ch = ch << 4;
}
}
//=================================
ISR (USART_RXC_vect)
{
char status ,ch;
static int idx=0;
static int crlf_idx = 0;
BOOL bGET_flag = FALSE;
char *pBuf = (char *) &uart_buf[0];
status = UCSRA;
ch = UDR;
idx = idx % (MAX_UART_BUF_SIZE-1);
uart_buf[idx] = ch;
idx++;
if (crlf_idx==3 && ch=='\n')
{
// Debug("htpp ");
idx= 0;
crlf_idx=0;
http_get= 1;
}
if (crlf_idx==2)
{
if (ch=='\r') crlf_idx++;
else crlf_idx =0;
}
if (crlf_idx==1 && ch=='\n')
{
if (bGET_flag == FALSE)
{
crlf_idx= 0;
uart_buf[idx] = '\x0';
// kopiuj
if (idx != 2)
{
memcpy( (void *) &uart_rsp[0] , (void *) &uart_buf[0],idx+1);
rsp_found = 1;
}
// "+CIFSR:STAIP"
//===========================
if (strstr(pBuf, "STAIP") != NULL)
{
get_ip(pBuf,MAX_UART_BUF_SIZE);
}
idx = 0;
}
else // bGet_flag == 1
crlf_idx++;
}
if (crlf_idx ==0 && ch =='\r')
crlf_idx++;
if (ch == '>')
rsp_greater = 1;
}
Dodano po 1 [minuty]:
/*
* uart.h
*
* Created on: 11 lut 2021
* Author: admin
*/
#ifndef _UART_H
#define _UART_H
#include "prj.h"
void uart_init();
void uart_send_char(unsigned char ch);
void uart_sendString(char *s);
void uart_send_hex(unsigned char ch);
void uart_sendString_hex(char *s);
void copy_command();
#define MAX_UART_BUF_SIZE 64
extern volatile char cmd_buf[MAX_UART_BUF_SIZE];
extern volatile char uart_rsp[MAX_UART_BUF_SIZE];
extern volatile unsigned char command_ready;
extern volatile int buf_index;
extern volatile int uart_char_size;
extern volatile u8 http_get ;
extern volatile u8 rsp_greater;
extern volatile u8 rsp_sendok;
extern volatile u8 rsp_control;
extern volatile u8 led;
extern volatile u8 led_value;
extern volatile u8 rsp_got_ip;
extern volatile u8 rsp_found;
#endif
