add 1-wire

This commit is contained in:
2015-04-04 20:27:02 +03:00
parent 1a0095a9ae
commit b861211e29
23 changed files with 2236 additions and 22 deletions

34
1-wire/Makefile Normal file
View File

@@ -0,0 +1,34 @@
NAME=testproj
SDCC=sdcc
CCFLAGS=-DSTM8S105 -I../ -I/usr/share/sdcc/include -mstm8 --out-fmt-ihx
LDFLAGS= -mstm8 --out-fmt-ihx -lstm8
FLASHFLAGS=-cstlinkv2 -pstm8s105
SRC=$(wildcard *.c)
# ATTENTION: FIRST in list should be file with main()
OBJ=$(SRC:%.c=%.rel)
TRASH=$(OBJ) $(SRC:%.c=%.rst) $(SRC:%.c=%.asm) $(SRC:%.c=%.lst)
TRASH+=$(SRC:%.c=%.sym) $(NAME).lk $(NAME).map
INDEPENDENT_HEADERS=../stm8l.h ports_definition.h Makefile
all: $(NAME).ihx
$(SRC) : %.c : %.h $(INDEPENDENT_HEADERS)
@touch $@
%.h: ;
clean:
rm -f $(TRASH)
load: $(NAME).ihx
stm8flash $(FLASHFLAGS) -w $(NAME).ihx
%.rel: %.c
$(SDCC) $(CCFLAGS) -c $<
$(NAME).ihx: $(OBJ)
$(SDCC) $(LDFLAGS) $(OBJ) -o $(NAME).ihx
.PHONY: all

BIN
1-wire/client Executable file

Binary file not shown.

197
1-wire/interrupts.c Normal file
View File

@@ -0,0 +1,197 @@
/*
* interrupts.c
*
* Copyright 2014 Edward V. Emelianoff <eddy@sao.ru>
*
* 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 "ports_definition.h"
#include "uart.h"
#include "onewire.h"
// Top Level Interrupt
INTERRUPT_HANDLER(TLI_IRQHandler, 0){}
// Auto Wake Up Interrupt
INTERRUPT_HANDLER(AWU_IRQHandler, 1){}
// Clock Controller Interrupt
INTERRUPT_HANDLER(CLK_IRQHandler, 2){}
// External Interrupt PORTA
INTERRUPT_HANDLER(EXTI_PORTA_IRQHandler, 3){}
// External Interrupt PORTB
INTERRUPT_HANDLER(EXTI_PORTB_IRQHandler, 4){}
// External Interrupt PORTC
INTERRUPT_HANDLER(EXTI_PORTC_IRQHandler, 5){
}
// External Interrupt PORTD
INTERRUPT_HANDLER(EXTI_PORTD_IRQHandler, 6){
}
// External Interrupt PORTE
INTERRUPT_HANDLER(EXTI_PORTE_IRQHandler, 7){}
#ifdef STM8S903
// External Interrupt PORTF
INTERRUPT_HANDLER(EXTI_PORTF_IRQHandler, 8){}
#endif // STM8S903
#if defined (STM8S208) || defined (STM8AF52Ax)
// CAN RX Interrupt routine.
INTERRUPT_HANDLER(CAN_RX_IRQHandler, 8){}
// CAN TX Interrupt routine.
INTERRUPT_HANDLER(CAN_TX_IRQHandler, 9){}
#endif // STM8S208 || STM8AF52Ax
// SPI Interrupt routine.
INTERRUPT_HANDLER(SPI_IRQHandler, 10){}
// Timer1 Update/Overflow/Trigger/Break Interrupt
INTERRUPT_HANDLER(TIM1_UPD_OVF_TRG_BRK_IRQHandler, 11){
}
// Timer1 Capture/Compare Interrupt routine.
INTERRUPT_HANDLER(TIM1_CAP_COM_IRQHandler, 12){}
#ifdef STM8S903
// Timer5 Update/Overflow/Break/Trigger Interrupt
INTERRUPT_HANDLER(TIM5_UPD_OVF_BRK_TRG_IRQHandler, 13){}
// Timer5 Capture/Compare Interrupt
INTERRUPT_HANDLER(TIM5_CAP_COM_IRQHandler, 14){}
#else // STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF62Ax or STM8AF52Ax or STM8AF626x
// Timer2 Update/Overflow/Break Interrupt
INTERRUPT_HANDLER(TIM2_UPD_OVF_BRK_IRQHandler, 13){
}
// Timer2 Capture/Compare Interrupt
// manage with sending/receiving
INTERRUPT_HANDLER(TIM2_CAP_COM_IRQHandler, 14){
if(TIM2_SR1 & TIM_SR1_CC1IF){
TIM2_SR1 &= ~TIM_SR1_CC1IF;
onewire_gotlen = TIM2_CCR1H << 8;
onewire_gotlen |= TIM2_CCR1L;
if(onewire_tick_ctr){ // there's some more data to transmit / receive
--onewire_tick_ctr;
if(is_receiver){// receive bits
ow_data >>= 1;
if(onewire_gotlen < ONE_ZERO_BARRIER){ // this is 1
ow_data |= 0x80; // LSbit first!
}
// in receiver mode we don't need to send byte after ctr is zero!
if(onewire_tick_ctr == 0){
TIM2_CR1 &= ~TIM_CR1_CEN;
}
}else{// transmit bits
// update CCR2 registers with new values
if(ow_data & 1){ // transmit 1
TIM2REG(CCR2, BIT_ONE_P);
}else{ // transmit 0
TIM2REG(CCR2, BIT_ZERO_P);
}
ow_data >>= 1;
}
}else{ // end: turn off timer
TIM2_CR1 &= ~TIM_CR1_CEN;
}
}
//if(TIM2_SR1 & TIM_SR1_CC2IF)
// TIM2_SR1 &= ~TIM_SR1_CC2IF;
}
#endif // STM8S903
#if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S105) || \
defined(STM8S005) || defined (STM8AF62Ax) || defined (STM8AF52Ax) || defined (STM8AF626x)
// Timer3 Update/Overflow/Break Interrupt
INTERRUPT_HANDLER(TIM3_UPD_OVF_BRK_IRQHandler, 15){}
// Timer3 Capture/Compare Interrupt
INTERRUPT_HANDLER(TIM3_CAP_COM_IRQHandler, 16){}
#endif // STM8S208, STM8S207 or STM8S105 or STM8AF62Ax or STM8AF52Ax or STM8AF626x
#if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S103) || \
defined(STM8S003) || defined (STM8AF62Ax) || defined (STM8AF52Ax) || defined (STM8S903)
// UART1 TX Interrupt
INTERRUPT_HANDLER(UART1_TX_IRQHandler, 17){}
// UART1 RX Interrupt
INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18){}
#endif // STM8S208 or STM8S207 or STM8S103 or STM8S903 or STM8AF62Ax or STM8AF52Ax
// I2C Interrupt
INTERRUPT_HANDLER(I2C_IRQHandler, 19){}
#if defined(STM8S105) || defined(STM8S005) || defined (STM8AF626x)
// UART2 TX interrupt
INTERRUPT_HANDLER(UART2_TX_IRQHandler, 20){}
// UART2 RX interrupt
INTERRUPT_HANDLER(UART2_RX_IRQHandler, 21){
U8 rb;
if(UART2_SR & UART_SR_RXNE){ // data received
rb = UART2_DR; // read received byte & clear RXNE flag
while(!(UART2_SR & UART_SR_TXE));
UART2_DR = rb; // echo received symbol
UART_rx[UART_rx_cur_i++] = rb; // put received byte into cycled buffer
if(UART_rx_cur_i == UART_rx_start_i){ // Oops: buffer overflow! Just forget old data
UART_rx_start_i++;
check_UART_pointer(UART_rx_start_i);
}
check_UART_pointer(UART_rx_cur_i);
}
}
#endif // STM8S105 or STM8AF626x
#if defined(STM8S207) || defined(STM8S007) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax)
// UART3 TX interrupt
INTERRUPT_HANDLER(UART3_TX_IRQHandler, 20){}
// UART3 RX interrupt
INTERRUPT_HANDLER(UART3_RX_IRQHandler, 21){}
#endif // STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax
#if defined(STM8S207) || defined(STM8S007) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax)
// ADC2 interrupt
INTERRUPT_HANDLER(ADC2_IRQHandler, 22){}
#else
// ADC1 interrupt
INTERRUPT_HANDLER(ADC1_IRQHandler, 22){
}
#endif // STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax
#ifdef STM8S903
// Timer6 Update/Overflow/Trigger Interrupt
INTERRUPT_HANDLER(TIM6_UPD_OVF_TRG_IRQHandler, 23){}
#else // STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF52Ax or STM8AF62Ax or STM8AF626x
// Timer4 Update/Overflow Interrupt
INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23){
if(TIM4_SR & TIM_SR1_UIF){ // update interrupt
Global_time++; // increase timer
}
TIM4_SR = 0; // clear all interrupt flags
}
#endif // STM8S903
// Eeprom EEC Interrupt
INTERRUPT_HANDLER(EEPROM_EEC_IRQHandler, 24){}

144
1-wire/interrupts.h Normal file
View File

@@ -0,0 +1,144 @@
/*
* interrupts.h
*
* Copyright 2014 Edward V. Emelianoff <eddy@sao.ru>
*
* 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 __INTERRUPTS_H__
#define __INTERRUPTS_H__
#include "stm8l.h"
// Top Level Interrupt
INTERRUPT_DEFINITION(TLI_IRQHandler, 0);
// Auto Wake Up Interrupt
INTERRUPT_DEFINITION(AWU_IRQHandler, 1);
// Clock Controller Interrupt
INTERRUPT_DEFINITION(CLK_IRQHandler, 2);
// External Interrupt PORTA
INTERRUPT_DEFINITION(EXTI_PORTA_IRQHandler, 3);
// External Interrupt PORTB
INTERRUPT_DEFINITION(EXTI_PORTB_IRQHandler, 4);
// External Interrupt PORTC
INTERRUPT_DEFINITION(EXTI_PORTC_IRQHandler, 5);
// External Interrupt PORTD
INTERRUPT_DEFINITION(EXTI_PORTD_IRQHandler, 6);
// External Interrupt PORTE
INTERRUPT_DEFINITION(EXTI_PORTE_IRQHandler, 7);
#ifdef STM8S903
// External Interrupt PORTF
INTERRUPT_DEFINITION(EXTI_PORTF_IRQHandler, 8);
#endif // STM8S903
#if defined (STM8S208) || defined (STM8AF52Ax)
// CAN RX Interrupt routine.
INTERRUPT_DEFINITION(CAN_RX_IRQHandler, 8);
// CAN TX Interrupt routine.
INTERRUPT_DEFINITION(CAN_TX_IRQHandler, 9);
#endif // STM8S208 || STM8AF52Ax
// SPI Interrupt routine.
INTERRUPT_DEFINITION(SPI_IRQHandler, 10);
// Timer1 Update/Overflow/Trigger/Break Interrupt
INTERRUPT_DEFINITION(TIM1_UPD_OVF_TRG_BRK_IRQHandler, 11);
// Timer1 Capture/Compare Interrupt routine.
INTERRUPT_DEFINITION(TIM1_CAP_COM_IRQHandler, 12);
#ifdef STM8S903
// Timer5 Update/Overflow/Break/Trigger Interrupt
INTERRUPT_DEFINITION(TIM5_UPD_OVF_BRK_TRG_IRQHandler, 13);
// Timer5 Capture/Compare Interrupt
INTERRUPT_DEFINITION(TIM5_CAP_COM_IRQHandler, 14);
#else // STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF62Ax or STM8AF52Ax or STM8AF626x
// Timer2 Update/Overflow/Break Interrupt
INTERRUPT_DEFINITION(TIM2_UPD_OVF_BRK_IRQHandler, 13);
// Timer2 Capture/Compare Interrupt
INTERRUPT_DEFINITION(TIM2_CAP_COM_IRQHandler, 14);
#endif // STM8S903
#if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S105) || \
defined(STM8S005) || defined (STM8AF62Ax) || defined (STM8AF52Ax) || defined (STM8AF626x)
// Timer3 Update/Overflow/Break Interrupt
INTERRUPT_DEFINITION(TIM3_UPD_OVF_BRK_IRQHandler, 15);
// Timer3 Capture/Compare Interrupt
INTERRUPT_DEFINITION(TIM3_CAP_COM_IRQHandler, 16);
#endif // STM8S208, STM8S207 or STM8S105 or STM8AF62Ax or STM8AF52Ax or STM8AF626x
#if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S103) || \
defined(STM8S003) || defined (STM8AF62Ax) || defined (STM8AF52Ax) || defined (STM8S903)
// UART1 TX Interrupt
INTERRUPT_DEFINITION(UART1_TX_IRQHandler, 17);
// UART1 RX Interrupt
INTERRUPT_DEFINITION(UART1_RX_IRQHandler, 18);
#endif // STM8S208 or STM8S207 or STM8S103 or STM8S903 or STM8AF62Ax or STM8AF52Ax
// I2C Interrupt
INTERRUPT_DEFINITION(I2C_IRQHandler, 19);
#if defined(STM8S105) || defined(STM8S005) || defined (STM8AF626x)
// UART2 TX interrupt
INTERRUPT_DEFINITION(UART2_TX_IRQHandler, 20);
// UART2 RX interrupt
INTERRUPT_DEFINITION(UART2_RX_IRQHandler, 21);
#endif // STM8S105 or STM8AF626x
#if defined(STM8S207) || defined(STM8S007) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax)
// UART3 TX interrupt
INTERRUPT_DEFINITION(UART3_TX_IRQHandler, 20);
// UART3 RX interrupt
INTERRUPT_DEFINITION(UART3_RX_IRQHandler, 21);
#endif // STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax
#if defined(STM8S207) || defined(STM8S007) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax)
// ADC2 interrupt
INTERRUPT_DEFINITION(ADC2_IRQHandler, 22);
#else // STM8S105, STM8S103 or STM8S903 or STM8AF626x
// ADC1 interrupt
INTERRUPT_DEFINITION(ADC1_IRQHandler, 22);
#endif // STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax
#ifdef STM8S903
// Timer6 Update/Overflow/Trigger Interrupt
INTERRUPT_DEFINITION(TIM6_UPD_OVF_TRG_IRQHandler, 23);
#else // STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF52Ax or STM8AF62Ax or STM8AF626x
// Timer4 Update/Overflow Interrupt
INTERRUPT_DEFINITION(TIM4_UPD_OVF_IRQHandler, 23);
#endif // STM8S903
// Eeprom EEC Interrupt
INTERRUPT_DEFINITION(EEPROM_EEC_IRQHandler, 24);
#endif // __INTERRUPTS_H__

144
1-wire/main.c Normal file
View File

@@ -0,0 +1,144 @@
/*
* blinky.c
*
* Copyright 2014 Edward V. Emelianoff <eddy@sao.ru>
*
* 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 "ports_definition.h"
#include "interrupts.h"
#include "uart.h"
#include "onewire.h"
volatile unsigned long Global_time = 0L; // global time in ms
U16 paused_val = 500; // interval between LED flashing
volatile U8 waitforread = 0;
// show received scrtchpad
void show_received_data(){
char i;
// uart_write("show_received_data()\n");
for(i = 8; i > -1; i--){
if(i < 8) UART_send_byte(',');
printUHEX(ow_data_array[i]);
}
ow_process_resdata = NULL;
uart_write("\nTemp: ");
print_long(gettemp());
uart_write("dergC\n");
}
// ask to read N bytes after temper request
void send_read_seq(){
// uart_write("send_read_seq()\n");
onewire_receive_bytes(9);
ow_process_resdata = show_received_data;
}
void wait_reading(){
// uart_write("wait_reading()\n");
waitforread = 1;
ow_process_resdata = NULL;
}
int main() {
unsigned long T = 0L;
// int Ival;
U8 rb;
CFG_GCR |= 1; // disable SWIM
// Configure clocking
CLK_CKDIVR = 0; // F_HSI = 16MHz, f_CPU = 16MHz
// Timer 4 (8 bit) used as system tick timer
// prescaler == 128 (2^7), Tfreq = 125kHz
// period = 1ms, so ARR = 125
TIM4_PSCR = 7;
TIM4_ARR = 125;
// interrupts: update
TIM4_IER = TIM_IER_UIE;
// auto-reload + interrupt on overflow + enable
TIM4_CR1 = TIM_CR1_APRE | TIM_CR1_URS | TIM_CR1_CEN;
// PC2 - PP output (on-board LED)
PORT(LED_PORT, DDR) |= LED_PIN;
PORT(LED_PORT, CR1) |= LED_PIN;
uart_init();
onewire_setup();
// enable all interrupts
enableInterrupts();
// Loop
do{
if((Global_time - T > paused_val) || (T > Global_time)){
T = Global_time;
PORT(LED_PORT, ODR) ^= LED_PIN; // blink on-board LED
if(!OW_CONVERSION_DONE) uart_write("conversion in process\n");
}
process_onewire();
if(UART_read_byte(&rb)){ // buffer isn't empty
switch(rb){
case 'h': // help
case 'H':
uart_write("\nPROTO:\n+/-\tLED period\nS/s\tset/get Mspeed\n"
"r: reset 1-wire\n"
"w: read temper\n");
break;
case '+':
paused_val += 100;
if(paused_val > 10000)
paused_val = 500; // but not more than 10s
break;
case '-':
paused_val -= 100;
if(paused_val < 500) // but not less than 0.5s
paused_val = 500;
break;
case 'r':
if(!onewire_reset()) uart_write("not ");
uart_write("found 1-wire devices (echo len: ");
print_long((long)onewire_gotlen);
uart_write(")\n");
break;
case 'w':
if(!onewire_reset()){
uart_write("no devices found!");
break;
}
ow_data_array[0] = OW_CONVERT_T;
ow_data_array[1] = OW_SKIP_ROM;
ow_process_resdata = wait_reading;
onewire_send_bytes(2);
break;
}
}
if(waitforread){
if(OW_CONVERSION_DONE){
if(onewire_reset()){
waitforread = 0;
ow_process_resdata = send_read_seq;
ow_data_array[0] = OW_READ_SCRATCHPAD;
ow_data_array[1] = OW_SKIP_ROM;
onewire_send_bytes(2);
}else uart_write("error reseting!");
}
}
}while(1);
}

257
1-wire/onewire.c Normal file
View File

@@ -0,0 +1,257 @@
/*
* onewire.c
*
* Copyright 2015 Edward V. Emelianoff <eddy@sao.ru, edward.emelianoff@gmail.com>
*
* 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"
#include "ports_definition.h"
#include "uart.h"
// mode of 1-wire
volatile OW_modes ow_mode = OW_MODE_OFF;
// length of zero-pulse (TIM2_CCR1)
volatile U16 onewire_gotlen = 0;
volatile U8 is_receiver = 0;
volatile U8 ow_data;
// remaining ticks counter
volatile U8 onewire_tick_ctr;
// array for sending/receiving data
// DATA STORED IN REVERCE ORDER! FIRST BYTE ID ow_data_array[N] AND LAST IS ow_data_array[0]!!!
U8 ow_data_array[10];
// how much bytes to send/receive
U8 ow_byte_ctr;
// this function will be called after receiving/transmitting of N bytes
// inside this function should be: ow_process_resdata = NULL;
void (*ow_process_resdata)() = NULL;
/*
********************* Timer2 ********************
* prescaler: TIM2_PSCR f = f_{in}/2^{TIM2_PSCR}
* other registers:
* TIM2_CR1 (page 223): | ARPE | reserved[2:0] | OPM | URS | UDIS | CEN |
* ARPE - Auto-reload preload enable (for TIM2_ARR)
* OPM: One-pulse mode (not implemented!)
* URS: Update request source (When enabled by the UDIS bit, 1 - interrupt only on counter overflow/underflow)
* UDIS: Update disable (1 - disable update int)
* CEN: Counter enable (1 - enable)
* TIM2_IER (page 226): | res | TIE | res | res | CC3IE | CC2IE | CC1IE | UIE |
* T - trigger; CC - comp/capt; U - update <--
* TIM2_SR1 (page 227): similar (but instead of IE -> IF)
* interrupt flags
* TIM2_SR2 (page 228): overcapture flags
* TIM2_EGR (page 229): event generation
* TIM2_CCMR1 (page 230):
* OUTPUT: | res | OC1M[2:0] | OC1PE | res | CC1S[1:0] |
* INPUT: | IC1F[3:0] | IC1PSC[1:0] | CC1S[1:0] |
* OC1M: compare mode !!! writeable only when channel is off (CC1E=0) !!!
* 000: no
* 001: set channel 1 to active level on match
* 010: set chan1 to inactive ...
* 011: toggle
* 100: force 0
* 101: force 1
* 110: PWM mode 1 (1->0)
* 111: PWM mode 2 (0->1)
* OC1PE: output compare 1 preload enable (0 - loads immediately)
* CC1S: comp/capt selection
* 00: CC1 is out
* 01: CC1 is in (TI1FP1)
* 10: TI2FP1
* 11: (only for TIM5)
* IC1F: input capture filter (0 - no filter)
* IC1PSC: capture prescaler (0 - no, xx - 2^{xx} events)
* TIM2_CCMRx - the same for channels 2 & 3
* TIM2_CCERx - CC polarity/enable (lowest 2 bytes in each 4): P=1:active high/capture on rising edge;
* TIM2_CNTRH, TIM2_CNTRL - counter value (automatical)
* TIM2_PSCR - prescaler value, lower 4 bits
* TIM1_ARRH, TIM1_ARRL - auto-reload value (while zero, timer is stopped) (page 206)
* TIM2_CCRxL/H - compare/capture registers
*/
void onewire_setup(){
// freq = 1MHz: psc=16 -> TIM2_PSCR = 4
TIM2_PSCR = 4;
// AIN: PD3 / TIM2_CH2 -> CC1 will be input on TI2FP1 & CC2 will be output
// configure pin CC2 (PD3): open-drain output
PORT(PD, DDR) |= GPIO_PIN3; // output & pseudo open-drain
PORT(PD, CR2) |= GPIO_PIN3; // fast
// out: PWM mode 1 (active -> inactive), preload enable
// CCMR2: | 0 | 110 | 1 | 0 | 00 |
TIM2_CCMR2 = 0x68;
// in: TI2FP1
TIM2_CCMR1 = 0x02;
// polarity: out - active LOW, in - capture on rising edge, enable
TIM2_CCER1 = 0x31;
// interrupts: CC1IE
TIM2_IER = TIM_IER_CC1IE;
// enable preload for registers to refresh their values only by UEV
TIM2_CR1 = TIM_CR1_APRE | TIM_CR1_URS;
// ARR values: 1000 for reset, 70 for data in/out
// CCR2 values: 500 for reset, 60 for sending 0 or reading, <15 for sending 1
// CCR1 values: >550 if there's devices on line (on reset), >12 (typ.15) - read 0, < 12 (typ.1) - read 1
// WARN! on reset there would be two CC events generated
}
/**
* reset 1-wire bus
* return 0 if no devices found, else return 1
*/
U8 onewire_reset(){
is_receiver = 0; // send data, not receive
onewire_tick_ctr = 1; // if there's devices on the bus CC1 int would be generated twice!
TIM2REG(ARR, RESET_LEN);
TIM2REG(CCR2, RESET_P);
//TIM2_CCR1H = TIM2_CCR1L = 0;
TIM2_EGR = TIM_EGR_UG; // generate UEV to update values
TIM2_CR1 |= TIM_CR1_CEN; // turn on timer
ow_mode = OW_MODE_RESET;
while(OW_BUSY); // wait until transmission is over
return ((onewire_gotlen > RESET_BARRIER) ? 1 : 0);
}
/**
* Send one byte through 1-wire
*/
void onewire_send_byte(U8 byte){
ow_data = byte;
is_receiver = 0;
onewire_tick_ctr = 7; // 7 bits remain
TIM2REG(ARR, BIT_LEN);
if(ow_data & 1){ // transmit 1
TIM2REG(CCR2, BIT_ONE_P);
}else{ // transmit 0
TIM2REG(CCR2, BIT_ZERO_P);
}
TIM2_EGR = TIM_EGR_UG; // generate UEV to update values
ow_data >>= 1;
TIM2_CR1 |= TIM_CR1_CEN; // turn on timer
}
/**
* get 1 byte through 1-wire
*/
void onewire_wait_for_receive(){
ow_data = 0;
is_receiver = 1;
onewire_tick_ctr = 8; // eight bits remain!
TIM2REG(ARR, BIT_LEN);
TIM2REG(CCR2, BIT_READ_P); // reading length
TIM2_EGR = TIM_EGR_UG; // generate UEV to update values
TIM2_CR1 |= TIM_CR1_CEN; // turn on timer
}
// DATA STORED IN REVERCE ORDER! FIRST BYTE ID ow_data_array[N] AND LAST IS ow_data_array[0]!!!
/**
* wait for receiveing N bytes from 1-wire
* N shoud be not great than 10
*/
void onewire_receive_bytes(U8 N){
ow_byte_ctr = N;
ow_mode = OW_MODE_RECEIVE_N;
onewire_wait_for_receive();
}
void onewire_send_bytes(U8 N){
ow_byte_ctr = N;
ow_mode = OW_MODE_TRANSMIT_N; // first data portion will be send by process_onewire
}
/**
* process 1-wire events
*/
void process_onewire(){
if(OW_BUSY || !OW_CONVERSION_DONE) return; // let data to be processed
switch(ow_mode){
case OW_MODE_RECEIVE_N: // wait for receiving of N bytes -> send next byte
uart_write("receive ");
printUHEX(ow_data);
UART_send_byte(' ');
printUHEX(onewire_gotlen);
UART_send_byte('\n');
ow_data_array[--ow_byte_ctr] = ow_data;
if(ow_byte_ctr){ // there's some more data to receive
onewire_wait_for_receive(); // wait for another byte
return;
}
// we have received all data - process it!
ow_mode = OW_MODE_OFF;
if(ow_process_resdata){
ow_process_resdata();
}
break;
case OW_MODE_TRANSMIT_N:
uart_write("transmit ");
if(ow_byte_ctr){ // we have some more data to transmit
printUHEX(ow_data_array[ow_byte_ctr-1]);
UART_send_byte('\n');
onewire_send_byte(ow_data_array[--ow_byte_ctr]);
return;
}
ow_mode = OW_MODE_OFF;
uart_write("is over\n");
if(ow_process_resdata){
ow_process_resdata();
}
break;
case OW_MODE_RESET:
uart_write("reset done!\n");
ow_mode = OW_MODE_OFF;
break;
default: // OW_MODE_OFF
return;
}
}
/**
* convert temperature from ow_data_array (scratchpad)
* in case of error return 200000
* return value in 1000th degrees centigrade
* don't forget that bytes in ow_data_array have reverce order!!!
* so:
* 8 - themperature LSB
* 7 - themperature MSB (all higher bits are sign)
* 6 - T_H
* 5 - T_L
* 4 - B20: Configuration register (only bits 6/5 valid: 9..12 bits resolution); 0xff for S20
* 3 - 0xff (reserved)
* 2 - (reserved for B20); S20: COUNT_REMAIN (0x0c)
* 1 - COUNT PER DEGR (0x10)
* 0 - CRC
*/
long gettemp(){
// detect DS18S20
long t;
U8 l,m;
char v;
if(ow_data_array[4] == 0xff){ // DS18S20
;
}
else{
m = ow_data_array[7];
l = ow_data_array[8];
v = l>>4 | ((m & 7)<<4);
if(m&0x80){ // minus
v |= 0x80;
}
t = ((long)v) * 1000 + ((long)l&0x0f)*125;
}
return t;
}

123
1-wire/onewire.h Normal file
View File

@@ -0,0 +1,123 @@
/*
* onewire.h
*
* Copyright 2015 Edward V. Emelianoff <eddy@sao.ru, edward.emelianoff@gmail.com>
*
* 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 "stm8l.h"
#define TIM2REGH(reg) TIM2_##reg##H
#define TIM2REGL(reg) TIM2_##reg##L
#define TIM2REG(reg, val) do{TIM2REGH(reg) = val >> 8; TIM2REGL(reg) = val & 0xff;}while(0)
// ARR values: 1000 for reset, 100 for data in/out
// CCR2 values: 500 for reset, 60 for sending 0 or reading, <15 for sending 1
// CCR1 values: >550 if there's devices on line (on reset), >12 (typ.15) - read 0, < 12 (typ.1) - read 1
#define RESET_LEN ((U16)1000)
#define BIT_LEN ((U16)100)
#define RESET_P ((U16)500)
#define BIT_ONE_P ((U16)10)
#define BIT_ZERO_P ((U16)60)
#define BIT_READ_P ((U16)5)
#define RESET_BARRIER ((U16)550)
#define ONE_ZERO_BARRIER ((U16)10)
#define OW_CONVERSION_DONE ((PORT(PD,IDR) & GPIO_PIN3))
#define OW_BUSY ((TIM2_CR1 & TIM_CR1_CEN))
/*
* thermometer commands
* send them with bus reset!
*/
// find devices
#define OW_SEARCH_ROM (0xf0)
// read device (when it is alone on the bus)
#define OW_READ_ROM (0x33)
// send device ID (after this command - 8 bytes of ID)
#define OW_MATCH_ROM (0x55)
// broadcast command
#define OW_SKIP_ROM (0xcc)
// find devices with critical conditions
#define OW_ALARM_SEARCH (0xec)
/*
* thermometer functions
* send them without bus reset!
*/
// start themperature reading
#define OW_CONVERT_T (0x44)
// write critical temperature to device's RAM
#define OW_SCRATCHPAD (0x4e)
// read whole device flash
#define OW_READ_SCRATCHPAD (0xbe)
// copy critical themperature from device's RAM to its EEPROM
#define OW_COPY_SCRATCHPAD (0x48)
// copy critical themperature from EEPROM to RAM (when power on this operation runs automatically)
#define OW_RECALL_E2 (0xb8)
// check whether there is devices wich power up from bus
#define OW_READ_POWER_SUPPLY (0xb4)
/*
* RAM register:
* 0 - themperature LSB
* 1 - themperature MSB (all higher bits are sign)
* 2 - T_H
* 3 - T_L
* 4 - B20: Configuration register (only bits 6/5 valid: 9..12 bits resolution); 0xff for S20
* 5 - 0xff (reserved)
* 6 - (reserved for B20); S20: COUNT_REMAIN (0x0c)
* 7 - COUNT PER DEGR (0x10)
* 8 - CRC
*
* To identify S20/B20 use value of confuguration register: its MSbit is 0
*/
typedef enum{
OW_MODE_OFF, // sleeping
OW_MODE_TRANSMIT_N, // transmit N bytes
OW_MODE_RECEIVE_N, // receive N bytes
OW_MODE_RESET // reset bus
} OW_modes;
extern volatile U8 ow_data; // byte to send/receive
extern volatile U8 onewire_tick_ctr; // tick counter
extern volatile U8 is_receiver;
extern volatile U16 onewire_gotlen; // last captured value
extern U8 ow_data_array[];
extern U8 ow_byte_ctr;
extern volatile OW_modes ow_mode;
extern void (*ow_process_resdata)();
void onewire_setup();
U8 onewire_reset();
void onewire_send_byte(U8 byte);
void onewire_wait_for_receive();
void process_onewire();
void onewire_receive_bytes(U8 N);
void onewire_send_bytes(U8 N);
long gettemp();
#endif // __ONEWIRE_H__

40
1-wire/ports_definition.h Normal file
View File

@@ -0,0 +1,40 @@
/*
* ports_definition.h - definition of ports pins & so on
*
* Copyright 2014 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.com>
*
* 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 __PORTS_DEFINITION_H__
#define __PORTS_DEFINITION_H__
#include "stm8l.h"
// macro for using in port constructions like PORT(LED_PORT, ODR) = xx
#define CONCAT(a, b) a ## _ ## b
#define PORT(a, b) CONCAT(a , b)
// on-board LED
#define LED_PORT PC
#define LED_PIN GPIO_PIN2
// UART2_TX
#define UART_PORT PD
#define UART_TX_PIN GPIO_PIN5
#endif // __PORTS_DEFINITION_H__

123
1-wire/testproj.ihx Normal file
View File

@@ -0,0 +1,123 @@
:2080A0008080808080808080808080805202AE5302F66B017B01A5022603CC81527B016B35
:2080C000027B02A4FDAE5302F7AE530FF65F975858585858585858CF0019AE5310F6974FF5
:2080E000CA0019959FCA001A97CF0019725D000A2759C6000A4AC7000A725D001B2726C6C5
:20810000000944C70009CE0019A3000A2408C60009AA80C70009725D000A2636AE5300F68D
:20812000A4FEF7202D7200000902200CAE53117FAE5312A60AF7200AAE53117FAE5312A6F1
:208140003CF7C6000944C700092007AE5300F6A4FEF75B0280808080805204AE5240F66B7E
:20816000047B04A520274DAE5241F66B017B04A4804D27FDAE52417B01F7AE00011F02C642
:20818000001797C600174CC700174F9572FB027B01F7C60016C10017260FC600164CC7008C
:2081A00016A1082604725F0016C60017A1082604725F00175B048080AE5342F644241B90AC
:2081C000CE002072A90001C6001FA90097C6001EA9009590CF0020CF001EAE53427F808020
:2081E000AE5240F64824F9AE52417B03F781160390F64D2718AE5240F64824F9AE5245F6AC
:20820000AA08F790F6905CAE5241F720E3815202C60017C1001626034F20271605AE0001F6
:208220001F01C6001697C600164CC700164F9572FB01F690F7C60016A1082604725F001641
:20824000A6015B0281521C5F1F031F017B21A1042303CC831D7B21A1032603CC831D0D21B4
:208260002603CC831D961C00051F1B4F5F9772FB1B7F4CA10C25F51E1B1C000AA60AF77B98
:2082800021A101270E7B21A102271C7B21A104272120301E1FF66B1A5F0F171F027B1A6B2D
:2082A000047B176B01201C161F90FE5F17031F0120111E1FE6036B16E602FE6B031F017B5D
:2082C000166B04A6096B114B0A5F894B001E07891E0789CD8B4E5B089F887B126B13840A41
:2082E000115F417B124172FB1BAB30F74B0A5F894B001E07891E0789CD8C7F5B081F03174D
:20830000011E0326041E0127067B11A1FF2CB87B114C5F9772FB1B89CD81EE5B025B1C8145
:2083200052110F01965C5C1F101E101C000B7F1E16A300007B15A2007B14A2002E141616D1
:2083400090504F1215974F12149517161F14A6016B01A60B6B0E4B0A5F894B001E1A891E22
:208360001A89CD8AA95B089F0A0E5F417B0E4172FB10AB30F74B0A5F894B001E1A891E1A06
:2083800089CD8BCB5B081F1617147B0EA1002C034F2002A6011E1626041E1427034D26B61A
:2083A000887B0F6B10844D27140D0127100A0E7B0E6B0F5F7B0F9772FB10A62DF75F7B0F14
:2083C0009772FB1089CD81EE5B025B1181521ACE00201F0DCE001E1F0B5F1F091F070F041E
:2083E0000F020F01961C000389CD820E5B024D2603CC84717B03A12D260E1E09260A1E0731
:208400002606A6016B0420697B03A1302403CC84937B03A1392303CC8493A6016B021E099C
:20842000891E09894B0A5F894B00CD8D1E5B081F11170F7B030F195F90977B19909572F9FA
:20844000119F1910979E190F9572A200309FA2006B149EA20017096B077B146B08AE7FFF4D
:2084600013094F12084F120724075F1F091F070F0190CE002072F20DC6001F120C95C600DA
:208480001E120B9790A327109EA2009FA2002403CC83E40D0126040D0226034F201A7B0948
:2084A000887B0B6B07846B050D0427051E05501F051E1D1605FFA6015B1A81AE853889CDC1
:2084C00081EE5B021E0389CD81EE5B024B0ACD81E084817B03A40F6B037B03887B04A10A36
:2084E000842406AB306B032004AB576B037B0381AE854189CD81EE5B027B034EA40F88CD88
:2085000084D35B0188CD81E0847B0388CD84D35B0188CD81E08481AE5011F6AA20F7AE5069
:2085200012F6AA20F7AE5242A611F7AE5243A606F7AE5245A62CF7810A4552524F523A201F
:04854000003078008F
:028E87000000E9
:20854400AE530CA604F7AE5011F6AA08F7AE5013F6AA08F7AE5306A668F7AE5305A602F75A
:20856400AE5308A631F7AE5301A602F7AE5300A684F781725F001B3501000AAE530DA603F9
:20858400F7AE530EA6E8F7AE5311A601F7AE5312A6F4F7AE5304A601F7AE5300F6AA01F7BC
:2085A40035030018AE5300F64425F9CE0019A302262304A60120014F817B03C70009725F7E
:2085C400001B3507000AAE530D7FAE530EA664F77200000902200CAE53117FAE5312A60A9C
:2085E400F7200AAE53117FAE5312A63CF7AE5304A601F7725400097210530081725F000937
:208604003501001B3508000AAE530D7FAE530EA664F7AE53117FAE5312A605F7AE5304A630
:2086240001F772105300817B03C7001535020018CC86007B03C7001535010018815202AEC2
:208644005300F6442403CC872AAE5010F6A5082603CC872AC60018A1012603CC86C7A10229
:20866400270AA1032603CC871DCC872AAE87B289CD81EE5B023B0009CD84F0844B20CD8140
:20868400E084C6001A88CD84F0844B0ACD81E084AE000B725A00159FCB0015979EA90095B2
:2086A400C60009F7725D00152706CD8600CC872A725F0018CE001C2603CC872ACE001CFDAF
:2086C400CC872AAE87BB89CD81EE5B02725D0015272FAE000B1F01C600154A5F9772FB016B
:2086E400F688CD84F0844B0ACD81E084C600154AC70015974F9572FB01F688CD85BD842011
:2087040025725F0018AE87C589CD81EE5B02CE001C2713CE001CFD200DAE87CE89CD81EE2B
:208724005B02725F00185B02815210AE000B1F0F1E0FE604A1FF2603CC87AE1E0FE6079538
:20874400160F90E6086B017B014EA40F6B0E9EA407484848481A0E979E4824049FAA80977A
:208764009F494FA20095909790958990894BE84B035F89CD8D1E5B081F0C170A7B016B0919
:208784000F085F7B09A40F90974F90954F974F959089894B7D5F894B00CD8D1E5B08517288
:2087A400F90C9F190B979E190A95515B10817265636569766520007472616E736D6974202E
:1787C400006973206F7665720A00726573657420646F6E65210A00C8
:068E8900000000000000E3
:208000008200808382000000820080A0820080A1820080A2820080A3820080A4820080A57E
:20802000820080A6820080A78200000082000000820080A8820080A9820080AA820080AB3D
:20804000820080AC82008155820081568200000082000000820081578200815882008159AC
:20806000820081B7820081B8820081DF82000000820000008200000082000000820000001F
:1D808300AE00152707724F00005A26F9AE000F2709D68E86D700155A26F7CC8080AF
:03808000CC884960
:2087DB005202AE000B1F01A608A1082E08884B2CCD81E084845F9772FB0188F69784888981
:2087FB005B01CD84F084844AA1FF2CDD725F001D725F001CAE89EE89CD81EE5B02CD872D23
:20881B00899089CD83205B04AE89F689CD81EE5B025B02814B09CD862B84AE87DBCF001C4E
:20883B008135010024725F001D725F001C8152095F1F041F0272107F60AE50C67FAE5345FE
:20885B00A607F7AE5346A67DF7AE5341A601F7AE5340A685F7AE500CF6AA04F7AE500DF60F
:20887B00AA04F7CD8517CD85449ACE002072F0041F08C6001F12036B07C6001E1202CE00F2
:20889B0022905F881309909F1208909E12015B012511CE00201304C6001F1203C6001E12F7
:2088BB00022422CE00201F04CE001E1F02AE500AF6A804F7AE5010F6A5082609AE89FD89F9
:2088DB00CD81EE5B02CD8641965C89CD820E5B024D2603CC89A87B01A12B2726A12D273EE0
:2088FB00A1482712A168270EA172274AA1772603CC8981CC89A8AE8A1489CD81EE5B02CC2B
:20891B0089A8CE00221C0064CF0022A327102203CC89A835F4002335010022CC89A8CE003F
:20893B00221D0064CF0022A301F4246135F40023350100222057CD85774D2609AE8A5E89EC
:20895B00CD81EE5B02AE8A6389CD81EE5B0290CE00195F908989CD83205B04AE8A8489CD4D
:20897B0081EE5B022027CD85774D260BAE8A8789CD81EE5B022016AE000BA644F75CA6CC03
:20899B00F7AE883CCF001C4B02CD863784725D00242603CC8885AE5010F6A5082603CC88EA
:2089BB0085CD85774D271D725F0024AE882FCF001CAE000BA6BEF75CA6CCF74B02CD8637C8
:2089DB0084CC8885AE8A9989CD81EE5B02CC88855B09810A54656D703A200064657267438F
:2089FB000A00636F6E76657273696F6E20696E2070726F636573730A000A50524F544F3A14
:208A1B000A2B2F2D094C454420706572696F640A532F73097365742F676574204D737065B1
:208A3B0065640A723A20726573657420312D776972650A773A20726561642074656D706572
:208A5B00720A006E6F742000666F756E6420312D77697265206465766963657320286563AA
:208A7B00686F206C656E3A2000290A006E6F206465766963657320666F756E642100657204
:0E8A9B00726F72207265736574696E672100D8
:078E8F000000000001F400E7
:208AA900521D1E22A300007B21A2007B20A2002F040F0D2004A6016B0D0D0D27151E225068
:208AC9004F12216B1B4F12201F186B167B1B6B1720081622171816201716161817141616BD
:208AE90017121E26A300007B25A2007B24A2002E1E7B27406B114F12266B104F12256B0F2E
:208B09004F12246B0E1610170B160E170920081626170B16241709160B170716091E0789D6
:208B290090891E18891E1889CD8B4E5B0817010D0D270D504F120290974F120190952002A3
:208B490016015B1D8152040F020F017B0B484F494D262E160D1E0B905859170D1F0B1E097C
:208B6900130D7B08120C7B07120B240D160D1E0B549056170D1F0B20080C017B016B022049
:208B8900CA7B026B041E09130D7B08120C7B07120B2513160972F20D7B08120C977B0712FB
:208BA9000B9517091F07160D1E0B549056170D1F0B7B046B030A040D0326CA1E0916075B58
:208BC9000481521E1E23A300007B22A2007B21A2002F040F112004A6016B110D1127151E24
:208BE90023504F12226B1C4F12211F146B127B1C6B13200816231714162117121614171962
:208C0900161217171E27A300007B26A2007B25A2002F040F162004A6016B160D16271E7B01
:208C290028406B104F12276B0F4F12266B0E4F12256B0D160F170B160D1709200816271742
:208C49000B16251709160B170716091E078990891E1D891E1D89CD8C7F5B0817017B111801
:208C6900164D270D504F120290974F12019095200216015B1E8152125F1F051F03A6206B86
:208C8900027B15484F496B0116171E1590585917171F157B036B0F1E04887B076B138408BC
:208CA9001259090F1F047B126B067B0F6B030D01271A7B06AA016B0A7B056B097B046B0839
:208CC9007B036B0716091705160717031E05131B7B04121A7B031219252B160572F21B7B1F
:208CE90004121A6B0C7B03121917056B037B0C6B047B18AA0190977B1790957B16977B15C7
:208D09009517171F150A020D022703CC8C8A1E1716155B128152409096905C961C00431F26
:208D2900051E05E603961C00471F0B1E0B1F0D1E0D1F3F1E3F88E60197844290FF72A90045
:208D4900021E05E6031E0B1F071E071F091E091F0F1E0F88E60397844290FF965C1F151E3D
:208D690015F66B171E05F697160B90E603429F1B171E15F71E15F66B1C1E05E60197160B64
:208D890090E602429F1B1C1E15F79096905C93FE1F1D1E05E6011E0B1F231E231F251E25F4
:208DA9001F271E2788E60397844272FB1D90FF93FE1F291E05E6021E0B1F2B1E2B1F2D1E2E
:208DC9002D1F311E3188E60297844272FB2990FF16051E05E6021E0B1F331E331F351E3533
:208DE9001F371E3788E6019784429F90F71E055C1F391E05E60290971E0BE60390421E3989
:208E0900FF160B1E05E6031E0B1F3D1E3D1F2F1E2F88F69784429F90F71E0B5C1F3B1E059F
:208E2900E60390971E0BE60290421E3BFF1E0B1C00037F1E051C00037F965CE6036B14E616
:208E4900026B13E6016B12F616431718164572F9131721887B13191A6B218419186B1F166C
:1E8E690021EF02161FFFE603E602FE16491E4772F9219F1920979E191F95515B40813F
:00000001FF

16
1-wire/tests Normal file
View File

@@ -0,0 +1,16 @@
0x98,0x01,0x4b,0x46,0x7f,0xff,0x08,0x10,0x22
LSB: 10011000 = 152
MSB: 00000001 = 1
temperature = 11001 + 1/2 = 25.5
TH: 01001011 = 75
TL: 01000110 = 70
conf: 01111111 = 127
res: 0xff - OK
res: 1000 - 8
res: 0x10 - OK
CRC: 100010 = 34

169
1-wire/uart.c Normal file
View File

@@ -0,0 +1,169 @@
/*
* blinky.c
*
* Copyright 2014 Edward V. Emelianoff <eddy@sao.ru>
*
* 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 "ports_definition.h"
#include "uart.h"
#include "interrupts.h"
U8 UART_rx[UART_BUF_LEN]; // cycle buffer for received data
U8 UART_rx_start_i = 0; // started index of received data (from which reading starts)
U8 UART_rx_cur_i = 0; // index of current first byte in rx array (to which data will be written)
/**
* Send one byte through UART
* @param byte - data to send
*/
void UART_send_byte(U8 byte){
while(!(UART2_SR & UART_SR_TXE)); // wait until previous byte transmitted
UART2_DR = byte;
}
void uart_write(char *str){
while(*str){
while(!(UART2_SR & UART_SR_TXE));
UART2_CR2 |= UART_CR2_TEN;
UART2_DR = *str++;
}
}
/**
* Read one byte from Rx buffer
* @param byte - where to store readed data
* @return 1 in case of non-empty buffer
*/
U8 UART_read_byte(U8 *byte){
if(UART_rx_start_i == UART_rx_cur_i) // buffer is empty
return 0;
*byte = UART_rx[UART_rx_start_i++];
check_UART_pointer(UART_rx_start_i);
return 1;
}
void printUint(U8 *val, U8 len){
unsigned long Number = 0;
U8 i = len;
char ch;
U8 decimal_buff[12]; // max len of U32 == 10 + \n + \0
if(len > 4 || len == 3 || len == 0) return;
for(i = 0; i < 12; i++)
decimal_buff[i] = 0;
decimal_buff[10] = '\n';
ch = 9;
switch(len){
case 1:
Number = *((U8*)val);
break;
case 2:
Number = *((U16*)val);
break;
case 4:
Number = *((unsigned long*)val);
break;
}
do{
i = Number % 10L;
decimal_buff[ch--] = i + '0';
Number /= 10L;
}while(Number && ch > -1);
uart_write((char*)&decimal_buff[ch+1]);
}
/**
* print signed long onto terminal
* max len = 10 symbols + 1 for "-" + 1 for '\n' + 1 for 0 = 13
*/
void print_long(long Number){
U8 i, L = 0;
char ch;
char decimal_buff[12];
decimal_buff[11] = 0;
ch = 11;
if(Number < 0){
Number = -Number;
L = 1;
}
do{
i = Number % 10L;
decimal_buff[--ch] = i + '0';
Number /= 10L;
}while(Number && ch > 0);
if(ch > 0 && L) decimal_buff[--ch] = '-';
uart_write(&decimal_buff[ch]);
}
U8 readInt(int *val){
unsigned long T = Global_time;
unsigned long R = 0;
int readed;
U8 sign = 0, rb, ret = 0, bad = 0;
do{
if(!UART_read_byte(&rb)) continue;
if(rb == '-' && R == 0){ // negative number
sign = 1;
continue;
}
if(rb < '0' || rb > '9') break; // number ends with any non-digit symbol that will be omitted
ret = 1; // there's at least one digit
R = R * 10L + rb - '0';
if(R > 0x7fff){ // bad value
R = 0;
bad = 0;
}
}while(Global_time - T < 10000); // wait no longer than 10s
if(bad || !ret) return 0;
readed = (int) R;
if(sign) readed *= -1;
*val = readed;
return 1;
}
void error_msg(char *msg){
uart_write("\nERROR: ");
uart_write(msg);
UART_send_byte('\n');
}
U8 U8toHEX(U8 val){
val &= 0x0f;
if(val < 10) val += '0';
else val += 'a' - 10;
return val;
}
void printUHEX(U8 val){
uart_write("0x");
UART_send_byte(U8toHEX(val>>4)); // MSB
UART_send_byte(U8toHEX(val)); // LSB
}
void uart_init(){
// PD5 - UART2_TX
PORT(UART_PORT, DDR) |= UART_TX_PIN;
PORT(UART_PORT, CR1) |= UART_TX_PIN;
// Configure UART
// 8 bit, no parity, 1 stop (UART_CR1/3 = 0 - reset value)
// 57600 on 16MHz: BRR1=0x11, BRR2=0x06
UART2_BRR1 = 0x11; UART2_BRR2 = 0x06;
UART2_CR2 = UART_CR2_TEN | UART_CR2_REN | UART_CR2_RIEN; // Allow RX/TX, generate ints on rx
}

45
1-wire/uart.h Normal file
View File

@@ -0,0 +1,45 @@
/*
* blinky.h
*
* Copyright 2014 Edward V. Emelianoff <eddy@sao.ru>
*
* 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 __MAIN_H__
#define __MAIN_H__
extern volatile unsigned long Global_time; // global time in ms
#define UART_BUF_LEN 8 // max 7 bytes transmited in on operation
extern U8 UART_rx[];
extern U8 UART_rx_start_i;
extern U8 UART_rx_cur_i;
void UART_send_byte(U8 byte);
void uart_write(char *str);
void printUint(U8 *val, U8 len);
void print_long(long Number);
void error_msg(char *msg);
void uart_init();
U8 UART_read_byte(U8 *byte);
void printUHEX(U8 val);
#define check_UART_pointer(x) do{if(x == UART_BUF_LEN) x = 0;}while(0)
#endif // __MAIN_H__