modified client.c

This commit is contained in:
eddyem
2014-09-23 12:17:35 +04:00
parent 298d842352
commit 4f7419c715
10 changed files with 495 additions and 146 deletions

View File

@@ -213,21 +213,25 @@ uint32_t read_AD7794(uint8_t channel){
uint8_t dr;
switch (N){
case 0: // start: set channel
//P("A0 ", uart1_send);
if(!AD7794_set_channel(channel)){
return 0; // 0 in return means error
}
break;
case 1: // put ADC to a single conversion mode
//P("A1 ", uart1_send);
sendWord(MODE_REGISTER, SINGLE_MODE |
U16(0x0f)); // the lowest speed;
check_errR();
break;
case 2: // wait for data reading & check errors
//P("A2", uart1_send);
dr = check_data_ready();
check_errR();
if(!dr) return AD7794_NOTRDY;
break;
default: // last step -> return readed data
//P("\r\n", uart1_send);
N = 0;
return read_ADC_data();
}

View File

@@ -33,4 +33,13 @@ void SysTick_init();
void ADC_init();
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__

Binary file not shown.

View File

@@ -35,6 +35,7 @@ uint32_t ad7794_values[TRD_NO];
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_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
* function calls from anywhere
@@ -44,13 +45,16 @@ uint8_t doubleconv = 1; // ==0 to single conversion; 1 to double (with currents
* @param doubleconv == 1 to do double conversion (with currents reversing)
*/
void read_next_TRD(){
static uint8_t step = 0; // step of operation
static uint8_t N = 0; // number of current device
static uint8_t step = 0; // step of operation
static uint8_t N = 0; // number of current device
static uint32_t val0 = 0, val1 = 0; // readed values
// "default" in switch will process last step
switch (step){ // now we should do something depending on current step value
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) = GPIO10; // enable com
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
if(!val0){ // 1st value isn't ready yet
if(!ADC_direct()){ // error: we can't setup reading
ad7794_values[N] = 0;
ad7794_on = 0;
step++;
break;
// ad7794_values[N] = 0;
RESET_ADC(); // reset steps counter to re-initialize + mark ad7794_on = 0
}
val0 = AD7794_NOTRDY;
val0 = read_AD7794(0);
if(!val0) RESET_ADC();
break;
}
if(val0 == AD7794_NOTRDY){ // data not ready yet
val0 = read_AD7794(0);
@@ -74,21 +78,21 @@ void read_next_TRD(){
// All OK, prepare second reading
if(doubleconv){ // double conversion -> prepare
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{
val1 = read_AD7794(0); // process it later
}
}else{ // simply copy value instead of multiplying by 2
val1 = val0;
}
}else{ // val0 ready, check val1
if(doubleconv && !val1){ // error
ad7794_values[N] = 0;
ad7794_on = 0;
ad7794_values[N] = val0 << 1;
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);
}else{ // all OK, we can put sum of val0 & val1 into array
ad7794_values[N] = val0 + val1;
@@ -149,6 +153,8 @@ int main(){
SPI1_init();
OW_Init();
// wait a little and then turn on USB pullup
for (i = 0; i < 0x800000; i++)
__asm__("nop");
@@ -161,17 +167,17 @@ int main(){
parce_incoming_buf(usbdatabuf, usbdatalen, usb_send);
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(Timer != lastTRDread){ // run this not more than once in 1ms
lastTRDread = Timer;
read_next_TRD();
}
}
if(Timer - Old_timer > 999){ // write out time in seconds
if(Timer - Old_timer > 999){ // one-second cycle
Old_timer += 1000;
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);
}else if(Timer < Old_timer){ // Timer overflow
Old_timer = 0;

View File

@@ -39,6 +39,7 @@
#include "user_proto.h"
#include "AD7794.h"
#include "onewire.h"
#define _U_ __attribute__((__unused__))
#define U8(x) ((uint8_t) x)

196
with_opencm3/onewire.c Normal file
View 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
View 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__

View File

@@ -24,15 +24,9 @@
#include "cdcacm.h"
// 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 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);
/**
@@ -242,32 +236,49 @@ void uart3_send(uint8_t 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
* @param UART - device to check
*/
void check_and_parce_UART(){
int i;
void check_and_parce_UART(uint32_t UART){
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"
for(i = 0; i < 3; i++){
curbuff = &RX_buffer[i];
datalen = curbuff->end;
if(!datalen) continue; // buffer is empty
// buffer isn't empty: process data in it
switch (i){
case 0:
sf = uart1_send;
break;
case 1:
sf = uart2_send;
break;
default:
sf = uart3_send;
}
parce_incoming_buf((char*)curbuff->buf, datalen, sf); // process data
curbuff->end = 0; // and zero counter
if(!curbuff) return;
switch(UART){
case USART1:
sf = uart1_send;
break;
case USART2:
sf = uart2_send;
break;
case USART3:
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
curbuff->end = 0; // and zero counter
}
/**

View File

@@ -26,14 +26,23 @@
// Size of buffers
#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_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 uart2_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__