mirror of
https://github.com/eddyem/IR-controller.git
synced 2025-12-06 10:45:15 +03:00
modified client.c
This commit is contained in:
parent
298d842352
commit
4f7419c715
@ -31,7 +31,8 @@
|
|||||||
#include <stdint.h> // int types
|
#include <stdint.h> // int types
|
||||||
#include <sys/time.h> // gettimeofday
|
#include <sys/time.h> // gettimeofday
|
||||||
|
|
||||||
int zmon_period = 30; // monitor temperature each 30s
|
#define BUFLEN 1024
|
||||||
|
|
||||||
double t0; // start time
|
double t0; // start time
|
||||||
|
|
||||||
FILE *fout = NULL; // file for messages duplicating
|
FILE *fout = NULL; // file for messages duplicating
|
||||||
@ -70,6 +71,11 @@ void quit(int ex_stat){
|
|||||||
* Open & setup TTY, terminal
|
* Open & setup TTY, terminal
|
||||||
*/
|
*/
|
||||||
void tty_init(){
|
void tty_init(){
|
||||||
|
// terminal without echo
|
||||||
|
tcgetattr(STDIN_FILENO, &oldt);
|
||||||
|
newt = oldt;
|
||||||
|
newt.c_lflag &= ~(ICANON | ECHO);
|
||||||
|
if(tcsetattr(STDIN_FILENO, TCSANOW, &newt) < 0) quit(-2);
|
||||||
printf("\nOpen port...\n");
|
printf("\nOpen port...\n");
|
||||||
if ((comfd = open(comdev,O_RDWR|O_NOCTTY|O_NONBLOCK)) < 0){
|
if ((comfd = open(comdev,O_RDWR|O_NOCTTY|O_NONBLOCK)) < 0){
|
||||||
fprintf(stderr,"Can't use port %s\n",comdev);
|
fprintf(stderr,"Can't use port %s\n",comdev);
|
||||||
@ -84,107 +90,86 @@ void tty_init(){
|
|||||||
tty.c_cc[VMIN] = 0; // non-canonical mode
|
tty.c_cc[VMIN] = 0; // non-canonical mode
|
||||||
tty.c_cc[VTIME] = 5;
|
tty.c_cc[VTIME] = 5;
|
||||||
if(ioctl(comfd,TCSETA,&tty) < 0) exit(-1); // set new mode
|
if(ioctl(comfd,TCSETA,&tty) < 0) exit(-1); // set new mode
|
||||||
// terminal without echo
|
|
||||||
tcgetattr(STDIN_FILENO, &oldt);
|
|
||||||
newt = oldt;
|
|
||||||
newt.c_lflag &= ~(ICANON | ECHO);
|
|
||||||
if(tcsetattr(STDIN_FILENO, TCSANOW, &newt) < 0) quit(-2);
|
|
||||||
printf(" OK\n");
|
printf(" OK\n");
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Read character from console without echo
|
|
||||||
* @return char readed
|
|
||||||
*/
|
|
||||||
int read_console(){
|
|
||||||
int rb;
|
|
||||||
struct timeval tv;
|
|
||||||
int retval;
|
|
||||||
fd_set rfds;
|
|
||||||
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
|
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
|
||||||
FD_ZERO(&rfds);
|
|
||||||
FD_SET(STDIN_FILENO, &rfds);
|
|
||||||
tv.tv_sec = 0; tv.tv_usec = 10000;
|
|
||||||
retval = select(1, &rfds, NULL, NULL, &tv);
|
|
||||||
if(!retval) rb = 0;
|
|
||||||
else {
|
|
||||||
if(FD_ISSET(STDIN_FILENO, &rfds)) rb = getchar();
|
|
||||||
else rb = 0;
|
|
||||||
}
|
|
||||||
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
|
|
||||||
return rb;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* getchar() without echo
|
* getchar() without echo
|
||||||
* wait until at least one character pressed
|
* wait until at least one character pressed
|
||||||
* @return character readed
|
* @return character readed
|
||||||
*/
|
*
|
||||||
int mygetchar(){ // аналог getchar() без необходимости жать Enter
|
int mygetchar(){
|
||||||
int ret;
|
int ret;
|
||||||
do ret = read_console();
|
do ret = read_console();
|
||||||
while(ret == 0);
|
while(ret == 0);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read data from TTY
|
* read both tty & console
|
||||||
* @param buff (o) - buffer for data read
|
* @param buff (o) - buffer for messages readed from tty
|
||||||
* @param length - buffer len
|
* @param length (io) - buff's length (return readed len or 0)
|
||||||
* @return amount of readed bytes
|
* @param rb (o) - byte readed from console or -1
|
||||||
|
* @return 1 if something was readed here or there
|
||||||
*/
|
*/
|
||||||
size_t read_tty(uint8_t *buff, size_t length){
|
int read_tty_and_console(char *buff, size_t *length, int *rb){
|
||||||
ssize_t L = 0;
|
ssize_t L;
|
||||||
fd_set rfds;
|
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
int retval;
|
int sel, retval = 0;
|
||||||
|
fd_set rfds;
|
||||||
FD_ZERO(&rfds);
|
FD_ZERO(&rfds);
|
||||||
|
FD_SET(STDIN_FILENO, &rfds);
|
||||||
FD_SET(comfd, &rfds);
|
FD_SET(comfd, &rfds);
|
||||||
tv.tv_sec = 0; tv.tv_usec = 500000; // wait for 500ms
|
tv.tv_sec = 0; tv.tv_usec = 10000;
|
||||||
retval = select(comfd + 1, &rfds, NULL, NULL, &tv);
|
sel = select(comfd + 1, &rfds, NULL, NULL, &tv);
|
||||||
if (!retval) return 0;
|
if(sel > 0){
|
||||||
if(FD_ISSET(comfd, &rfds)){
|
if(FD_ISSET(STDIN_FILENO, &rfds)){
|
||||||
if((L = read(comfd, buff, length)) < 1) return 0;
|
*rb = getchar();
|
||||||
|
retval = 1;
|
||||||
|
}else{
|
||||||
|
*rb = -1;
|
||||||
}
|
}
|
||||||
return (size_t)L;
|
if(FD_ISSET(comfd, &rfds)){
|
||||||
|
if((L = read(comfd, buff, *length)) < 1){ // disconnect or other troubles
|
||||||
|
fprintf(stderr, "USB error or disconnected!\n");
|
||||||
|
quit(1);
|
||||||
|
}else{
|
||||||
|
if(L == 0){ // USB disconnected
|
||||||
|
fprintf(stderr, "USB disconnected!\n");
|
||||||
|
quit(1);
|
||||||
|
}
|
||||||
|
*length = (size_t) L;
|
||||||
|
retval = 1;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
*length = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
void help(){
|
void help(){
|
||||||
printf("Use this commands:\n"
|
printf("Use this commands:\n"
|
||||||
"h\tShow this help\n"
|
"h\tShow this help\n"
|
||||||
"q\tQuit\n"
|
"q\tQuit\n"
|
||||||
"t\tMonitor temperature on ZakWire\n"
|
|
||||||
"v\tNon-verbose\n"
|
|
||||||
"z\tGet ZakWire data\n"
|
|
||||||
"V\tVerbose\n"
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define dup_pr(...) do{printf(__VA_ARGS__); if(fout) fprintf(fout, __VA_ARGS__);}while(0)
|
|
||||||
|
|
||||||
int zflag = 0; // monitor zakwire
|
|
||||||
void con_sig(int rb){
|
void con_sig(int rb){
|
||||||
uint8_t cmd;
|
char cmd;
|
||||||
if(rb < 1) return;
|
if(rb < 1) return;
|
||||||
if(rb == 'q') quit(0); // q == exit
|
if(rb == 'q') quit(0); // q == exit
|
||||||
switch(rb){
|
cmd = (char) rb;
|
||||||
|
write(comfd, &cmd, 1);
|
||||||
|
/*switch(rb){
|
||||||
case 'h':
|
case 'h':
|
||||||
help();
|
help();
|
||||||
break;
|
break;
|
||||||
case 's':
|
|
||||||
dup_pr("Stop ZakWire\n");
|
|
||||||
zflag = 0;
|
|
||||||
break;
|
|
||||||
case 't':
|
|
||||||
dup_pr("ZakWire thermal monitoring\n");
|
|
||||||
zflag = 1;
|
|
||||||
write(comfd, "v", 1);
|
|
||||||
t0 = dtime() - zmon_period;
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
cmd = (uint8_t) rb;
|
cmd = (uint8_t) rb;
|
||||||
write(comfd, &cmd, 1);
|
write(comfd, &cmd, 1);
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -193,11 +178,7 @@ void con_sig(int rb){
|
|||||||
* @param len - length of data in buffer (could be 2 or 4)
|
* @param len - length of data in buffer (could be 2 or 4)
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
uint32_t get_int(uint8_t *buff, size_t len){
|
uint32_t get_int(char *buff, size_t len){
|
||||||
/* int i;
|
|
||||||
printf("read %zd bytes: ", len);
|
|
||||||
for(i = 0; i < len; i++) printf("0x%x ", buff[i]);
|
|
||||||
printf("\n");*/
|
|
||||||
if(len != 2 && len != 4){
|
if(len != 2 && len != 4){
|
||||||
fprintf(stdout, "Bad data length!\n");
|
fprintf(stdout, "Bad data length!\n");
|
||||||
return 0xffffffff;
|
return 0xffffffff;
|
||||||
@ -209,9 +190,42 @@ uint32_t get_int(uint8_t *buff, size_t len){
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copy line by line buffer buff to file removing cmd starting from newline
|
||||||
|
* @param buffer - data to put into file
|
||||||
|
* @param cmd - symbol to remove from line startint (if found, change *cmd to (-1)
|
||||||
|
* or NULL, (-1) if no command to remove
|
||||||
|
*/
|
||||||
|
void copy_buf_to_file(char *buffer, int *cmd){
|
||||||
|
char *buff, *line, *ptr;
|
||||||
|
if(!cmd || *cmd < 0){
|
||||||
|
fprintf(fout, "%s", buffer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
buff = strdup(buffer), ptr = buff;
|
||||||
|
do{
|
||||||
|
if(!*ptr) break;
|
||||||
|
if(ptr[0] == (char)*cmd){
|
||||||
|
*cmd = -1;
|
||||||
|
ptr++;
|
||||||
|
if(ptr[0] == '\n') ptr++;
|
||||||
|
if(!*ptr) break;
|
||||||
|
}
|
||||||
|
line = ptr;
|
||||||
|
ptr = strchr(buff, '\n');
|
||||||
|
if(ptr){
|
||||||
|
*ptr++ = 0;
|
||||||
|
//fprintf(fout, "%s\n", line);
|
||||||
|
}//else
|
||||||
|
//fprintf(fout, "%s", line); // no newline found in buffer
|
||||||
|
fprintf(fout, "%s\n", line);
|
||||||
|
}while(ptr);
|
||||||
|
free(buff);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]){
|
int main(int argc, char *argv[]){
|
||||||
int rb;
|
int rb, oldcmd = -1;
|
||||||
uint8_t buff[128];
|
char buff[BUFLEN+1];
|
||||||
size_t L;
|
size_t L;
|
||||||
if(argc == 2){
|
if(argc == 2){
|
||||||
fout = fopen(argv[1], "a");
|
fout = fopen(argv[1], "a");
|
||||||
@ -229,34 +243,19 @@ int main(int argc, char *argv[]){
|
|||||||
setbuf(stdout, NULL);
|
setbuf(stdout, NULL);
|
||||||
t0 = dtime();
|
t0 = dtime();
|
||||||
while(1){
|
while(1){
|
||||||
rb = read_console();
|
L = BUFLEN;
|
||||||
if(rb > 0) con_sig(rb);
|
if(read_tty_and_console(buff, &L, &rb)){
|
||||||
L = read_tty(buff, 127);
|
if(rb > 0){
|
||||||
|
con_sig(rb);
|
||||||
|
oldcmd = rb;
|
||||||
|
}
|
||||||
if(L){
|
if(L){
|
||||||
buff[L] = 0;
|
buff[L] = 0;
|
||||||
printf("TTY: %s\n", buff);
|
printf("%s", buff);
|
||||||
if(fout) fprintf(fout, "%zd\t%s\n", time(NULL), buff);
|
if(fout){
|
||||||
}
|
copy_buf_to_file(buff, &oldcmd);
|
||||||
if(zflag && dtime() - t0 > zmon_period){ // thermal monitoring
|
}
|
||||||
t0 += zmon_period;
|
}
|
||||||
char NUM[] = {'6', '5', '3'};
|
|
||||||
char obuf[] = {'N', 0, 'z'};
|
|
||||||
int i;
|
|
||||||
double temper[3];
|
|
||||||
for(i = 0; i < 3; i++){
|
|
||||||
obuf[1] = NUM[i];
|
|
||||||
if(3 != write(comfd, &obuf, 3)){
|
|
||||||
perror("Can't write");
|
|
||||||
quit(-1);
|
|
||||||
}
|
|
||||||
L = read_tty(buff, 127);
|
|
||||||
//for(j = 0; j < 10; j++) // 10 tries to read data from USB
|
|
||||||
// if((L = read_tty(buff, 127)) < 1) continue;
|
|
||||||
uint32_t ans = get_int(buff, L);
|
|
||||||
if(ans == 0xffffffff) temper[i] = -274.;
|
|
||||||
else temper[i] = ((double)ans)/2047.*70. - 10.;
|
|
||||||
}
|
|
||||||
dup_pr("%zd\t%.2f\t%.2f\t%.2f\n", time(NULL), temper[0], temper[1], temper[2]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -213,21 +213,25 @@ uint32_t read_AD7794(uint8_t channel){
|
|||||||
uint8_t dr;
|
uint8_t dr;
|
||||||
switch (N){
|
switch (N){
|
||||||
case 0: // start: set channel
|
case 0: // start: set channel
|
||||||
|
//P("A0 ", uart1_send);
|
||||||
if(!AD7794_set_channel(channel)){
|
if(!AD7794_set_channel(channel)){
|
||||||
return 0; // 0 in return means error
|
return 0; // 0 in return means error
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 1: // put ADC to a single conversion mode
|
case 1: // put ADC to a single conversion mode
|
||||||
|
//P("A1 ", uart1_send);
|
||||||
sendWord(MODE_REGISTER, SINGLE_MODE |
|
sendWord(MODE_REGISTER, SINGLE_MODE |
|
||||||
U16(0x0f)); // the lowest speed;
|
U16(0x0f)); // the lowest speed;
|
||||||
check_errR();
|
check_errR();
|
||||||
break;
|
break;
|
||||||
case 2: // wait for data reading & check errors
|
case 2: // wait for data reading & check errors
|
||||||
|
//P("A2", uart1_send);
|
||||||
dr = check_data_ready();
|
dr = check_data_ready();
|
||||||
check_errR();
|
check_errR();
|
||||||
if(!dr) return AD7794_NOTRDY;
|
if(!dr) return AD7794_NOTRDY;
|
||||||
break;
|
break;
|
||||||
default: // last step -> return readed data
|
default: // last step -> return readed data
|
||||||
|
//P("\r\n", uart1_send);
|
||||||
N = 0;
|
N = 0;
|
||||||
return read_ADC_data();
|
return read_ADC_data();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -33,4 +33,13 @@ void SysTick_init();
|
|||||||
void ADC_init();
|
void ADC_init();
|
||||||
void ADC_calibrate_and_start();
|
void ADC_calibrate_and_start();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* One Wire interface
|
||||||
|
*/
|
||||||
|
// In case of using USART2 for 1-wire port, make corresponding change
|
||||||
|
// and redefine pins in OW_Init
|
||||||
|
//#define OW_USART_X USART2
|
||||||
|
#define OW_USART_X USART3
|
||||||
|
|
||||||
|
|
||||||
#endif // __HARDWARE_INI_H__
|
#endif // __HARDWARE_INI_H__
|
||||||
|
|||||||
Binary file not shown.
@ -35,6 +35,7 @@ uint32_t ad7794_values[TRD_NO];
|
|||||||
uint8_t doubleconv = 1; // ==0 to single conversion; 1 to double (with currents reversing)
|
uint8_t doubleconv = 1; // ==0 to single conversion; 1 to double (with currents reversing)
|
||||||
#define ADC_direct() setup_AD7794(EXTREFIN_1 | REF_DETECTION | UNIPOLAR_CODING, IEXC_DIRECT | IEXC_1MA)
|
#define ADC_direct() setup_AD7794(EXTREFIN_1 | REF_DETECTION | UNIPOLAR_CODING, IEXC_DIRECT | IEXC_1MA)
|
||||||
#define ADC_reverse() setup_AD7794(EXTREFIN_1 | REF_DETECTION | UNIPOLAR_CODING, IEXC_SWAPPED | IEXC_1MA)
|
#define ADC_reverse() setup_AD7794(EXTREFIN_1 | REF_DETECTION | UNIPOLAR_CODING, IEXC_SWAPPED | IEXC_1MA)
|
||||||
|
#define RESET_ADC() do{N = 0; ad7794_on = 0; step++; return; }while(0)
|
||||||
/**
|
/**
|
||||||
* reads next value of voltage on TRD
|
* reads next value of voltage on TRD
|
||||||
* function calls from anywhere
|
* function calls from anywhere
|
||||||
@ -50,7 +51,10 @@ void read_next_TRD(){
|
|||||||
// "default" in switch will process last step
|
// "default" in switch will process last step
|
||||||
switch (step){ // now we should do something depending on current step value
|
switch (step){ // now we should do something depending on current step value
|
||||||
case 0: // step 0: set address
|
case 0: // step 0: set address
|
||||||
if(N == 0) AD7794_init(); // reset ADC in beginning of each set
|
if(N == 0 || !ad7794_on) AD7794_init(); // reset ADC in beginning of each set
|
||||||
|
//P("Step 0: read_next_TRD, N=", uart1_send);
|
||||||
|
//print_int(N, uart1_send);
|
||||||
|
//newline(uart1_send);
|
||||||
GPIO_BSRR(GPIOC) = N << 6; // set address
|
GPIO_BSRR(GPIOC) = N << 6; // set address
|
||||||
GPIO_BSRR(GPIOC) = GPIO10; // enable com
|
GPIO_BSRR(GPIOC) = GPIO10; // enable com
|
||||||
N++; // and increment TRD counter
|
N++; // and increment TRD counter
|
||||||
@ -61,12 +65,12 @@ void read_next_TRD(){
|
|||||||
case 1: // step 1: prepare reading in 1st current direction or single reading
|
case 1: // step 1: prepare reading in 1st current direction or single reading
|
||||||
if(!val0){ // 1st value isn't ready yet
|
if(!val0){ // 1st value isn't ready yet
|
||||||
if(!ADC_direct()){ // error: we can't setup reading
|
if(!ADC_direct()){ // error: we can't setup reading
|
||||||
ad7794_values[N] = 0;
|
// ad7794_values[N] = 0;
|
||||||
ad7794_on = 0;
|
RESET_ADC(); // reset steps counter to re-initialize + mark ad7794_on = 0
|
||||||
step++;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
val0 = AD7794_NOTRDY;
|
val0 = read_AD7794(0);
|
||||||
|
if(!val0) RESET_ADC();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
if(val0 == AD7794_NOTRDY){ // data not ready yet
|
if(val0 == AD7794_NOTRDY){ // data not ready yet
|
||||||
val0 = read_AD7794(0);
|
val0 = read_AD7794(0);
|
||||||
@ -74,21 +78,21 @@ void read_next_TRD(){
|
|||||||
// All OK, prepare second reading
|
// All OK, prepare second reading
|
||||||
if(doubleconv){ // double conversion -> prepare
|
if(doubleconv){ // double conversion -> prepare
|
||||||
if(!ADC_reverse()){ // we can't setup it, but we have 1st value
|
if(!ADC_reverse()){ // we can't setup it, but we have 1st value
|
||||||
val1 = val0;
|
//ad7794_values[N] = val0 << 1; // double val0
|
||||||
|
RESET_ADC();
|
||||||
}else{
|
}else{
|
||||||
val1 = read_AD7794(0); // process it later
|
val1 = read_AD7794(0); // process it later
|
||||||
}
|
}
|
||||||
}else{ // simply copy value instead of multiplying by 2
|
}else{ // simply copy value instead of multiplying by 2
|
||||||
val1 = val0;
|
ad7794_values[N] = val0 << 1;
|
||||||
}
|
|
||||||
}else{ // val0 ready, check val1
|
|
||||||
if(doubleconv && !val1){ // error
|
|
||||||
ad7794_values[N] = 0;
|
|
||||||
ad7794_on = 0;
|
|
||||||
step++;
|
step++;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
if(doubleconv && (val1 == AD7794_NOTRDY)){ // val1 not ready yet
|
}else{ // val0 ready, check val1; we can go here only when doubleconv != 0
|
||||||
|
if(!val1){ // error: on previous step we had to get some value or AD7794_NOTRDY
|
||||||
|
//ad7794_values[N] = 0;
|
||||||
|
RESET_ADC();
|
||||||
|
}
|
||||||
|
if(val1 == AD7794_NOTRDY){ // val1 not ready yet
|
||||||
val1 = read_AD7794(0);
|
val1 = read_AD7794(0);
|
||||||
}else{ // all OK, we can put sum of val0 & val1 into array
|
}else{ // all OK, we can put sum of val0 & val1 into array
|
||||||
ad7794_values[N] = val0 + val1;
|
ad7794_values[N] = val0 + val1;
|
||||||
@ -149,6 +153,8 @@ int main(){
|
|||||||
|
|
||||||
SPI1_init();
|
SPI1_init();
|
||||||
|
|
||||||
|
OW_Init();
|
||||||
|
|
||||||
// wait a little and then turn on USB pullup
|
// wait a little and then turn on USB pullup
|
||||||
for (i = 0; i < 0x800000; i++)
|
for (i = 0; i < 0x800000; i++)
|
||||||
__asm__("nop");
|
__asm__("nop");
|
||||||
@ -161,17 +167,17 @@ int main(){
|
|||||||
parce_incoming_buf(usbdatabuf, usbdatalen, usb_send);
|
parce_incoming_buf(usbdatabuf, usbdatalen, usb_send);
|
||||||
usbdatalen = 0; // all data have been processed - prepare to get new portion
|
usbdatalen = 0; // all data have been processed - prepare to get new portion
|
||||||
}
|
}
|
||||||
check_and_parce_UART(); // also check data in UART buffers
|
check_and_parce_UART(USART1); // also check data in UART buffers
|
||||||
if(ad7794_on){
|
if(ad7794_on){
|
||||||
if(Timer != lastTRDread){ // run this not more than once in 1ms
|
if(Timer != lastTRDread){ // run this not more than once in 1ms
|
||||||
lastTRDread = Timer;
|
lastTRDread = Timer;
|
||||||
read_next_TRD();
|
read_next_TRD();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(Timer - Old_timer > 999){ // write out time in seconds
|
if(Timer - Old_timer > 999){ // one-second cycle
|
||||||
Old_timer += 1000;
|
Old_timer += 1000;
|
||||||
gpio_toggle(GPIOC, GPIO12); // toggle LED
|
gpio_toggle(GPIOC, GPIO12); // toggle LED
|
||||||
if(!ad7794_on) AD7794_init(); // try to init ADC
|
if(!ad7794_on) AD7794_init(); // try to init ADC if it doesn't work
|
||||||
//print_int(Timer/1000, usb_send);
|
//print_int(Timer/1000, usb_send);
|
||||||
}else if(Timer < Old_timer){ // Timer overflow
|
}else if(Timer < Old_timer){ // Timer overflow
|
||||||
Old_timer = 0;
|
Old_timer = 0;
|
||||||
|
|||||||
@ -39,6 +39,7 @@
|
|||||||
|
|
||||||
#include "user_proto.h"
|
#include "user_proto.h"
|
||||||
#include "AD7794.h"
|
#include "AD7794.h"
|
||||||
|
#include "onewire.h"
|
||||||
|
|
||||||
#define _U_ __attribute__((__unused__))
|
#define _U_ __attribute__((__unused__))
|
||||||
#define U8(x) ((uint8_t) x)
|
#define U8(x) ((uint8_t) x)
|
||||||
|
|||||||
196
with_opencm3/onewire.c
Normal file
196
with_opencm3/onewire.c
Normal file
@ -0,0 +1,196 @@
|
|||||||
|
/*
|
||||||
|
* onewire.c - functions to work with 1-wire devices
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
|
* MA 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
#include "onewire.h"
|
||||||
|
|
||||||
|
#define OW_0 0x00
|
||||||
|
#define OW_1 0xff
|
||||||
|
#define OW_R 0xff
|
||||||
|
#define OW_RST 0xf0
|
||||||
|
|
||||||
|
|
||||||
|
// In/Out buffer
|
||||||
|
uint8_t ow_buf[8];
|
||||||
|
/**
|
||||||
|
* this function sends bits of ow_byte (LSB first) to 1-wire line
|
||||||
|
* @param ow_byte - byte to convert
|
||||||
|
* @param Nbits - number of bits to send
|
||||||
|
*/
|
||||||
|
void OW_SendBits(uint8_t ow_byte, uint8_t Nbits){
|
||||||
|
uint8_t i, byte;
|
||||||
|
if(Nbits == 0) return;
|
||||||
|
if(Nbits > 8) Nbits = 8;
|
||||||
|
for(i = 0; i < Nbits; i++){
|
||||||
|
if(ow_byte & 0x01){
|
||||||
|
byte = OW_1;
|
||||||
|
}else{
|
||||||
|
byte = OW_0;
|
||||||
|
}
|
||||||
|
fill_uart_buff(OW_USART_X, byte); // send next "bit"
|
||||||
|
ow_byte = ow_byte >> 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Inverce conversion - read data (not more than 8 b
|
||||||
|
*/
|
||||||
|
uint8_t OW_ReadByte(){
|
||||||
|
UART_buff *curbuff = get_uart_buffer(OW_USART_X);
|
||||||
|
uint8_t ow_byte = 0, i, L, *buf;
|
||||||
|
if(!curbuff || !(L = curbuff->end)) return 0; // no data?
|
||||||
|
if(L > 8) L = 8; // forget all other data
|
||||||
|
buf = curbuff->buf;
|
||||||
|
for(i = 0; i < L; i++, buf++){
|
||||||
|
ow_byte = ow_byte >> 1; // prepare for next bit filling
|
||||||
|
if(*buf == OW_1){
|
||||||
|
ow_byte |= 0x80; // MSB = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ow_byte >> (8 - L); // shift to the end: L could be != 8 ???
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Configure peripherial ports (USART2) for 1-wire
|
||||||
|
*/
|
||||||
|
void OW_Init(){
|
||||||
|
struct usb_cdc_line_coding owlc = {
|
||||||
|
.dwDTERate = 115200,
|
||||||
|
.bCharFormat = USB_CDC_1_STOP_BITS,
|
||||||
|
.bParityType = USB_CDC_NO_PARITY,
|
||||||
|
.bDataBits = 8,
|
||||||
|
};
|
||||||
|
UART_init(OW_USART_X);
|
||||||
|
UART_setspeed(OW_USART_X, &owlc);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 1-wire reset
|
||||||
|
* Reset procedure: USART settings are 9600,8,n,1,
|
||||||
|
* send 0xf0 then check what we get
|
||||||
|
* if not 0xf0 line is busy.
|
||||||
|
* Other operations work with next USART settings: 115200,8,n,1
|
||||||
|
*
|
||||||
|
* return 1 in case of 1-wire devices present; otherwise return 0
|
||||||
|
*/
|
||||||
|
uint8_t OW_Reset() {
|
||||||
|
uint8_t ow_presence;
|
||||||
|
UART_buff *curbuff;
|
||||||
|
// change speed to 9600
|
||||||
|
usart_set_baudrate(OW_USART_X, 9600);
|
||||||
|
//USART_ClearFlag(OW_USART_X, USART_FLAG_TC);
|
||||||
|
fill_uart_buff(OW_USART_X, OW_RST); // send 1 byte data
|
||||||
|
// wait for end of transmission
|
||||||
|
while(!(USART_SR(OW_USART_X) & USART_SR_TC));
|
||||||
|
curbuff = get_uart_buffer(OW_USART_X);
|
||||||
|
if(!curbuff || !(curbuff->end)) return 0; // error reading
|
||||||
|
curbuff->end = 0; // zero counter
|
||||||
|
ow_presence = curbuff->buf[0];
|
||||||
|
// change speed back
|
||||||
|
usart_set_baudrate(OW_USART_X, 115200);
|
||||||
|
// if there is any device on bus, it will pull it, so we'll get not 0xf0
|
||||||
|
if(ow_presence != OW_RST){
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
// we get 0xf0 -> there's nothing on the bus
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Procedure of 1-wire communications
|
||||||
|
* variables:
|
||||||
|
* sendReset - send RESET before transmission
|
||||||
|
* command - bytes sent to the bus (if we want to read, send OW_READ_SLOT)
|
||||||
|
* cLen - command buffer length (how many bytes to send)
|
||||||
|
* data - pointer for reading buffer (if reading needed)
|
||||||
|
* readStart - first byte to read (starts from 0) or OW_NO_READ (not read)
|
||||||
|
*
|
||||||
|
* return 1 if succeed, 0 if failure
|
||||||
|
*/
|
||||||
|
uint8_t OW_Send(uint8_t sendReset, uint8_t *command, uint8_t cLen,
|
||||||
|
uint8_t *data, uint8_t dLen, uint8_t readStart) {
|
||||||
|
// if reset needed - send RESET and check bus
|
||||||
|
if(sendReset){
|
||||||
|
if(OW_Reset() == 0){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while(cLen > 0){
|
||||||
|
OW_SendBits(*command, 8);
|
||||||
|
command++;
|
||||||
|
cLen--;
|
||||||
|
// wait for EOT
|
||||||
|
while(!(USART_SR(OW_USART_X) & USART_SR_TC));
|
||||||
|
// put data from bus into user buffer
|
||||||
|
if(readStart == 0 && dLen > 0){
|
||||||
|
*data = OW_ReadByte();
|
||||||
|
data++;
|
||||||
|
dLen--;
|
||||||
|
}else{
|
||||||
|
if(readStart != OW_NO_READ){
|
||||||
|
readStart--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* scan 1-wire bus
|
||||||
|
* num - max number of devices
|
||||||
|
* buf - array for devices' ID's (8*num bytes)
|
||||||
|
* return amount of founded devices
|
||||||
|
*/
|
||||||
|
uint8_t OW_Scan(uint8_t *buf, uint8_t num) {
|
||||||
|
unsigned long path,next,pos;
|
||||||
|
uint8_t bit,chk;
|
||||||
|
uint8_t cnt_bit, cnt_byte, cnt_num;
|
||||||
|
path=0;
|
||||||
|
cnt_num=0;
|
||||||
|
do{
|
||||||
|
//(issue the 'ROM search' command)
|
||||||
|
if( 0 == OW_WriteCmd(OW_SEARCH_ROM) ) return 0;
|
||||||
|
next=0; // next path to follow
|
||||||
|
pos=1; // path bit pointer
|
||||||
|
for(cnt_byte = 0; cnt_byte != 8; cnt_byte++){
|
||||||
|
buf[cnt_num*8 + cnt_byte] = 0;
|
||||||
|
for(cnt_bit = 0; cnt_bit != 8; cnt_bit++){
|
||||||
|
//(read two bits, 'bit' and 'chk', from the 1-wire bus)
|
||||||
|
OW_SendBits(OW_R, 2);
|
||||||
|
bit = OW_ReadByte();
|
||||||
|
chk = bit & 0x02; // bit 1
|
||||||
|
bit = bit & 0x01; // bit 0
|
||||||
|
//bit = (ow_buf[0] == OW_1); chk = (ow_buf[1] == OW_1);
|
||||||
|
if(bit && chk) return 0; // error
|
||||||
|
if(!bit && !chk){ // collision, both are zero
|
||||||
|
if (pos & path) bit = 1; // if we've been here before
|
||||||
|
else next = (path&(pos-1)) | pos; // else, new branch for next
|
||||||
|
pos <<= 1;
|
||||||
|
}
|
||||||
|
//(save this bit as part of the current ROM value)
|
||||||
|
if (bit) buf[cnt_num*8 + cnt_byte]|=(1<<cnt_bit);
|
||||||
|
//(write 'bit' to the 1-wire bus)
|
||||||
|
OW_SendBits(bit, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//(output the just-completed ROM value)
|
||||||
|
path=next;
|
||||||
|
cnt_num++;
|
||||||
|
}while(path && cnt_num < num);
|
||||||
|
return cnt_num;
|
||||||
|
}
|
||||||
114
with_opencm3/onewire.h
Normal file
114
with_opencm3/onewire.h
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
/*
|
||||||
|
* onewire.h
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
|
* MA 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#ifndef __ONEWIRE_H__
|
||||||
|
#define __ONEWIRE_H__
|
||||||
|
|
||||||
|
#include "main.h"
|
||||||
|
#include "hardware_ini.h"
|
||||||
|
|
||||||
|
// 1-wire status
|
||||||
|
#define OW_OK (1)
|
||||||
|
#define OW_ERROR (2)
|
||||||
|
#define OW_NO_DEVICE (3)
|
||||||
|
|
||||||
|
#define OW_NO_READ (0xff)
|
||||||
|
|
||||||
|
#define OW_READ_SLOT (uint8_t*)"0xff"
|
||||||
|
|
||||||
|
void OW_Init();
|
||||||
|
uint8_t OW_Send(uint8_t sendReset, uint8_t *command, uint8_t cLen,
|
||||||
|
uint8_t *data, uint8_t dLen, uint8_t readStart);
|
||||||
|
uint8_t OW_Scan(uint8_t *buf, uint8_t num);
|
||||||
|
|
||||||
|
// shortcuts for functions
|
||||||
|
// only send message b wich length is c with RESET flag a
|
||||||
|
#define OW_SendOnly(a,b,c) OW_Send(a, b, c, (void*)0, 0, OW_NO_READ)
|
||||||
|
// send 1 command (with bus reset)
|
||||||
|
#define OW_WriteCmd(cmd) OW_Send(1, cmd, 1, (void*)0, 0, OW_NO_READ)
|
||||||
|
// send 1 function (without bus reset)
|
||||||
|
#define OW_WriteFn(cmd) OW_Send(0, cmd, 1, (void*)0, 0, OW_NO_READ)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* thermometer identificator is: 8bits CRC, 48bits serial, 8bits device code (10h)
|
||||||
|
* Critical temperatures is T_H and T_L
|
||||||
|
* T_L is lowest allowed temperature
|
||||||
|
* T_H is highest -//-
|
||||||
|
* format T_H and T_L: 1bit sigh + 7bits of data
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* thermometer commands (DS18S20)\
|
||||||
|
* send them with bus reset!
|
||||||
|
*/
|
||||||
|
// find devices
|
||||||
|
#define T_SEARCH_ROM (0xf0)
|
||||||
|
#define OW_SEARCH_ROM (uint8_t*)"\xf0"
|
||||||
|
// read device (when it is alone on the bus)
|
||||||
|
#define T_READ_ROM (0x33)
|
||||||
|
#define OW_READ_ROM (uint8_t*)"\x33"
|
||||||
|
// send device ID (after this command - 8 bytes of ID)
|
||||||
|
#define T_MATCH_ROM (0x55)
|
||||||
|
#define OW_MATCH_ROM (uint8_t*)"\x55"
|
||||||
|
// broadcast command
|
||||||
|
#define T_SKIP_ROM (0xcc)
|
||||||
|
#define OW_SKIP_ROM (uint8_t*)"\xcc"
|
||||||
|
// find devices with critical conditions
|
||||||
|
#define T_ALARM_SEARCH (0xec)
|
||||||
|
#define OW_ALARM_SEARCH (uint8_t*)"\xec"
|
||||||
|
/*
|
||||||
|
* thermometer functions
|
||||||
|
* send them without bus reset!
|
||||||
|
*/
|
||||||
|
// start themperature reading
|
||||||
|
#define T_CONVERT_T (0x44)
|
||||||
|
#define OW_CONVERT_T (uint8_t*)"\x44"
|
||||||
|
// write critical temperature to device's RAM
|
||||||
|
#define T_SCRATCHPAD (0x4e)
|
||||||
|
#define OW_SCRATCHPAD (uint8_t*)"\x4e"
|
||||||
|
// read whole device flash
|
||||||
|
#define T_READ_SCRATCHPAD (0xbe)
|
||||||
|
#define OW_READ_SCRATCHPAD (uint8_t*)"\xbe"
|
||||||
|
// copy critical themperature from device's RAM to its EEPROM
|
||||||
|
#define T_COPY_SCRATCHPAD (0x48)
|
||||||
|
#define OW_COPY_SCRATCHPAD (uint8_t*)"\x48"
|
||||||
|
// copy critical themperature from EEPROM to RAM (when power on this operation runs automatically)
|
||||||
|
#define T_RECALL_E2 (0xb8)
|
||||||
|
#define OW_RECALL_E2 (uint8_t*)"\xb8"
|
||||||
|
// check whether there is devices wich power up from bus
|
||||||
|
#define T_READ_POWER_SUPPLY (0xb4)
|
||||||
|
#define OW_READ_POWER_SUPPLY (uint8_t*)"\xb4"
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* RAM register:
|
||||||
|
* 0 - themperature: 1 ADU == 0.5 degrC
|
||||||
|
* 1 - sign (0 - T>0 degrC ==> T=byte0; 1 - T<0 degrC ==> T=byte0-0xff+1)
|
||||||
|
* 2 - T_H
|
||||||
|
* 3 - T_L
|
||||||
|
* 4 - 0xff (reserved)
|
||||||
|
* 5 - 0xff (reserved)
|
||||||
|
* 6 - COUNT_REMAIN (0x0c)
|
||||||
|
* 7 - COUNT PER DEGR (0x10)
|
||||||
|
* 8 - CRC
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#endif // __ONEWIRE_H__
|
||||||
@ -24,15 +24,9 @@
|
|||||||
#include "cdcacm.h"
|
#include "cdcacm.h"
|
||||||
|
|
||||||
// Buffers for Tx
|
// Buffers for Tx
|
||||||
typedef struct {
|
|
||||||
uint8_t buf[UART_TX_DATA_SIZE];
|
|
||||||
uint8_t start; // index from where to start reading
|
|
||||||
uint8_t end; // index from where to start writing
|
|
||||||
} UART_buff;
|
|
||||||
static UART_buff TX_buffer[3]; // Tx buffers for all three ports
|
static UART_buff TX_buffer[3]; // Tx buffers for all three ports
|
||||||
static UART_buff RX_buffer[3]; // Rx buffers for all three ports
|
static UART_buff RX_buffer[3]; // Rx buffers for all three ports
|
||||||
|
|
||||||
void fill_uart_buff(uint32_t UART, uint8_t byte);
|
|
||||||
void fill_uart_RXbuff(uint32_t UART, uint8_t byte);
|
void fill_uart_RXbuff(uint32_t UART, uint8_t byte);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -242,33 +236,50 @@ void uart3_send(uint8_t byte){
|
|||||||
fill_uart_buff(USART3, byte);
|
fill_uart_buff(USART3, byte);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UART_buff *get_uart_buffer(uint32_t UART){
|
||||||
|
switch(UART){
|
||||||
|
case USART1:
|
||||||
|
return &RX_buffer[0];
|
||||||
|
break;
|
||||||
|
case USART2:
|
||||||
|
return &RX_buffer[1];
|
||||||
|
break;
|
||||||
|
case USART3:
|
||||||
|
return &RX_buffer[2];
|
||||||
|
break;
|
||||||
|
default: // error - return
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check buffers for non-empty & run parsing function
|
* Check buffers for non-empty & run parsing function
|
||||||
|
* @param UART - device to check
|
||||||
*/
|
*/
|
||||||
void check_and_parce_UART(){
|
void check_and_parce_UART(uint32_t UART){
|
||||||
int i;
|
|
||||||
sendfun sf;
|
sendfun sf;
|
||||||
UART_buff *curbuff;
|
UART_buff *curbuff = get_uart_buffer(UART);
|
||||||
uint8_t datalen; // length of data in buffer - here we use param "end"
|
uint8_t datalen; // length of data in buffer - here we use param "end"
|
||||||
for(i = 0; i < 3; i++){
|
if(!curbuff) return;
|
||||||
curbuff = &RX_buffer[i];
|
switch(UART){
|
||||||
datalen = curbuff->end;
|
case USART1:
|
||||||
if(!datalen) continue; // buffer is empty
|
|
||||||
// buffer isn't empty: process data in it
|
|
||||||
switch (i){
|
|
||||||
case 0:
|
|
||||||
sf = uart1_send;
|
sf = uart1_send;
|
||||||
break;
|
break;
|
||||||
case 1:
|
case USART2:
|
||||||
sf = uart2_send;
|
sf = uart2_send;
|
||||||
break;
|
break;
|
||||||
default:
|
case USART3:
|
||||||
sf = uart3_send;
|
sf = uart3_send;
|
||||||
|
break;
|
||||||
|
default: // error - return
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
datalen = curbuff->end;
|
||||||
|
if(!datalen) return; // buffer is empty
|
||||||
parce_incoming_buf((char*)curbuff->buf, datalen, sf); // process data
|
parce_incoming_buf((char*)curbuff->buf, datalen, sf); // process data
|
||||||
curbuff->end = 0; // and zero counter
|
curbuff->end = 0; // and zero counter
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fill data in RX buffer to prepare it for further work
|
* Fill data in RX buffer to prepare it for further work
|
||||||
|
|||||||
@ -26,14 +26,23 @@
|
|||||||
// Size of buffers
|
// Size of buffers
|
||||||
#define UART_TX_DATA_SIZE 64
|
#define UART_TX_DATA_SIZE 64
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t buf[UART_TX_DATA_SIZE];
|
||||||
|
uint8_t start; // index from where to start reading
|
||||||
|
uint8_t end; // index from where to start writing
|
||||||
|
} UART_buff;
|
||||||
|
|
||||||
void UART_init(uint32_t UART);
|
void UART_init(uint32_t UART);
|
||||||
void UART_setspeed(uint32_t UART, struct usb_cdc_line_coding *linecoding);
|
void UART_setspeed(uint32_t UART, struct usb_cdc_line_coding *linecoding);
|
||||||
|
|
||||||
|
void fill_uart_buff(uint32_t UART, uint8_t byte);
|
||||||
void uart1_send(uint8_t byte);
|
void uart1_send(uint8_t byte);
|
||||||
void uart2_send(uint8_t byte);
|
void uart2_send(uint8_t byte);
|
||||||
void uart3_send(uint8_t byte);
|
void uart3_send(uint8_t byte);
|
||||||
|
|
||||||
void check_and_parce_UART();
|
void check_and_parce_UART(uint32_t UART);
|
||||||
|
|
||||||
|
UART_buff *get_uart_buffer(uint32_t UART);
|
||||||
|
|
||||||
|
|
||||||
#endif // __UART_H__
|
#endif // __UART_H__
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user