196 lines
3.4 KiB
C
196 lines
3.4 KiB
C
#include <nuvoton/functions.h>
|
|
#include <nuvoton/N76E003.h>
|
|
#include <nuvoton/Common.h>
|
|
#include <nuvoton/SFR_Macro.h>
|
|
#include <string.h>
|
|
|
|
#define RELAY2 P01
|
|
#define RELAY1 P13
|
|
|
|
#define IN1 P05
|
|
#define IN2 P15
|
|
|
|
#define MAXCMD 64
|
|
|
|
unsigned char crc;
|
|
|
|
unsigned char getchar1(void)
|
|
{
|
|
UINT8 c;
|
|
while (!RI);
|
|
c = SBUF;
|
|
RI = 0;
|
|
return (c);
|
|
}
|
|
|
|
int putchar1 (unsigned char c)
|
|
{
|
|
crc += c;
|
|
TI = 0;
|
|
SBUF = c;
|
|
while(TI==0);
|
|
return 0;
|
|
}
|
|
|
|
void sendpkt(unsigned char cmd, unsigned int len, unsigned char *buffer) {
|
|
unsigned int i;
|
|
|
|
crc = 0;
|
|
putchar1(0x55);
|
|
putchar1(0xAA);
|
|
putchar1(0x03);
|
|
putchar1(cmd);
|
|
putchar1(len >> 8);
|
|
putchar1(len & 0xFF);
|
|
|
|
for (i=0; i<len; i++) {
|
|
putchar1(buffer[i]);
|
|
}
|
|
putchar1(crc);
|
|
}
|
|
|
|
void tuya_receive (unsigned int len, unsigned char *buffer) {
|
|
unsigned char dpid;
|
|
unsigned char dtype;
|
|
unsigned int dlen;
|
|
|
|
if (len < 4) return;
|
|
dpid = buffer[0];
|
|
dtype = buffer[1];
|
|
dlen = buffer[2] << 8 + buffer[3];
|
|
|
|
switch(dpid) {
|
|
case 1:
|
|
RELAY1 = buffer[4] ? 1 : 0;
|
|
break;
|
|
case 2:
|
|
RELAY2 = buffer[4] ? 1 : 0;
|
|
break;
|
|
}
|
|
|
|
sendpkt(7, len, buffer);
|
|
}
|
|
|
|
void process(unsigned char cmd, unsigned int len, unsigned char *buffer) {
|
|
static unsigned char restart = 0;
|
|
|
|
switch (cmd) {
|
|
case 0: /* Heartbeat */
|
|
buffer[0] = restart;
|
|
restart = 1;
|
|
sendpkt(0, 1, buffer);
|
|
break;
|
|
case 1: /* Identify */
|
|
strcpy(buffer,"Nuovo");
|
|
len = strlen(buffer);
|
|
sendpkt(1, len, buffer);
|
|
break;
|
|
case 3: /* WIFI State */
|
|
sendpkt(3, 0, NULL);
|
|
break;
|
|
case 6: /* Set Command */
|
|
tuya_receive(len, buffer);
|
|
break;
|
|
case 8: /* Query Command */
|
|
break;
|
|
}
|
|
}
|
|
|
|
void recvpkt(void)
|
|
{
|
|
static unsigned char state = 0;
|
|
static unsigned char cmd;
|
|
static unsigned int len;
|
|
static unsigned char rcrc;
|
|
static unsigned char i;
|
|
static unsigned char command[MAXCMD];
|
|
|
|
if (RI) {
|
|
int inByte = getchar1();
|
|
switch (state) {
|
|
case 0:
|
|
if (inByte == 0x55) state++;
|
|
rcrc = 0;
|
|
break;
|
|
case 1:
|
|
if (inByte == 0xaa) state++; else state = 0;
|
|
break;
|
|
case 2:
|
|
if (inByte == 0) state++; else state = 0;
|
|
break;
|
|
case 3:
|
|
cmd = inByte;
|
|
state++;
|
|
break;
|
|
case 4:
|
|
len = inByte << 8;
|
|
state++;
|
|
break;
|
|
case 5:
|
|
len += inByte;
|
|
if (len < MAXCMD) state++; else state = 0;
|
|
if (len == 0) state++;
|
|
i = 0;
|
|
break;
|
|
case 6:
|
|
command[i] = inByte;
|
|
i++;
|
|
if (len == i) state++;
|
|
break;
|
|
case 7:
|
|
if (inByte == rcrc)
|
|
process(cmd, len, command);
|
|
state = 0;
|
|
break;
|
|
}
|
|
rcrc = rcrc + inByte;
|
|
}
|
|
}
|
|
|
|
void uart_loop() {
|
|
recvpkt();
|
|
}
|
|
|
|
void uart_init(UINT32 u32Baudrate) //T1M = 1, SMOD = 1
|
|
{
|
|
P06_PushPull_Mode;
|
|
P07_Input_Mode;
|
|
|
|
SCON = 0x50; //UART0 Mode1,REN=1,TI=1
|
|
TMOD |= 0x20; //Timer1 Mode1
|
|
|
|
set_SMOD; //UART0 Double Rate Enable
|
|
set_T1M;
|
|
clr_BRCK; //Serial port 0 baud rate clock source = Timer1
|
|
|
|
#ifdef FOSC_160000
|
|
TH1 = 256 - (1000000 / u32Baudrate + 1); /*16 MHz */
|
|
#endif
|
|
#ifdef FOSC_166000
|
|
TH1 = 256 - (1037500 / u32Baudrate); /*16.6 MHz */
|
|
#endif
|
|
set_TR1;
|
|
set_TI; //For printf function must setting TI = 1
|
|
}
|
|
|
|
int main()
|
|
{
|
|
uart_init(9600);
|
|
|
|
/* Relays */
|
|
RELAY1 = 0;
|
|
RELAY2 = 0;
|
|
|
|
P01_PushPull_Mode;
|
|
P13_PushPull_Mode;
|
|
|
|
/* Inputs */
|
|
P05_Input_Mode;
|
|
P15_Input_Mode;
|
|
|
|
while (1)
|
|
{
|
|
uart_loop();
|
|
}
|
|
}
|
|
|