mirror of
https://github.com/eddyem/STM8_samples.git
synced 2025-12-06 02:35:21 +03:00
I2C (not working yet)
This commit is contained in:
parent
077c50a247
commit
132ebb5787
File diff suppressed because it is too large
Load Diff
34
si7005/Makefile
Normal file
34
si7005/Makefile
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
NAME=uart
|
||||||
|
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)
|
||||||
|
|
||||||
|
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
|
||||||
335
si7005/i2c.c
Normal file
335
si7005/i2c.c
Normal file
@ -0,0 +1,335 @@
|
|||||||
|
/*
|
||||||
|
* i2c.c - functions to work with HW I2C
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* I2C_FREQR - peripherial input clock (>=1MHz - standard, >=4MHz - fast)
|
||||||
|
* + clock control & rise time
|
||||||
|
* I2C_CR1 - enable
|
||||||
|
* I2C_CR2 - start bit (generate + set SB bit + interrupt if ITEVTEN==1)
|
||||||
|
* writing can be in 7 & 10 bit modes
|
||||||
|
*
|
||||||
|
* ** 7bit transmit**
|
||||||
|
* 1) start; 2) read SR1 & write address to DR; 3) check ADDR, if ACK received
|
||||||
|
* it would be 1 - clear it by read SR1 & SR3, wait for TXE==1 & BTF==1 (ready for transmit)
|
||||||
|
* and send data writing to DR; to send EOT set STOP=1
|
||||||
|
*
|
||||||
|
* ** 10bit transmit**
|
||||||
|
* 1) start; 2) read SR1 & write 11110xx0 (xx - MS bits of address) to DR;
|
||||||
|
* 3) ADD10=1, clear it by reading SR1 & write last 8 bits of address to DR;
|
||||||
|
* 4) similar to 3) for 7bits
|
||||||
|
*
|
||||||
|
* ** receive **
|
||||||
|
* 1) clear ADDR after sending address
|
||||||
|
* 2) set ACK if need to generate ACK bit
|
||||||
|
* 3) check RXNE or get data by interrupt (if ITEVTEN==1 & ITBUFEN==1)
|
||||||
|
* in case of byte transfer finished, BTF=1 (to clear it, read SR1 & DR)
|
||||||
|
* in 10bit receive mode send start & 11110xx1 after sending address
|
||||||
|
* in 7bit receive mode send address with LSB set
|
||||||
|
*
|
||||||
|
* TRA bit indicates I2C mode
|
||||||
|
*
|
||||||
|
* ** close **
|
||||||
|
* Method 1 (higher interrupts):
|
||||||
|
* 1) reset ACK=0 to generate NACK; 2) to generate Stop/Restart set STOP/START
|
||||||
|
* all this should be done at second last byte reading (to get a single byte
|
||||||
|
* do this just after ADDR sent); 3) read last byte
|
||||||
|
*
|
||||||
|
* Method 2 (low interrupt priority or polling, valid only for N>2):
|
||||||
|
* 1) not read DR & wait for RXNE==1&&BTF==1
|
||||||
|
* 2) clear ACK
|
||||||
|
* 3) read DR (DATA_N-2) & set STOP/START
|
||||||
|
* 4) read DR (DATA_N-1), read DR (DATA_N)
|
||||||
|
*
|
||||||
|
* to read 1 byte use Method 1;
|
||||||
|
* to read 2 bytes with Method2, set POS&ACK, wait ADDR, clear ADDR&ACK,
|
||||||
|
* wait BTF, set STOP, read DR twice
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* *** REGISTERS ***
|
||||||
|
* I2C_CR1: | NOSTRETCH | ENGC | reserved[5:1] | PE |
|
||||||
|
* NOSTRETCH - ==1 to slave mode
|
||||||
|
* ENGC - generall call EN/DIS
|
||||||
|
* PE - ==1 to enable I2C
|
||||||
|
*
|
||||||
|
* I2C_CR2: | SWRST | res[6:4] | POS | ACK | STOP | START |
|
||||||
|
* SWRST - ==1 to software reset
|
||||||
|
* POS - ACK position (==0 for current byte, ==1 for next byte)
|
||||||
|
* ACK - send ACK after reading byte
|
||||||
|
* STOP - send STOP after reading byte
|
||||||
|
* START - send repeated START
|
||||||
|
* DO NOT make any changes with I2C_CR2 when sending START/STOP
|
||||||
|
* before they'll be cleared by hardware!
|
||||||
|
*
|
||||||
|
* I2C_FREQR: | res[7:6] | FREQ[5:0] |
|
||||||
|
* FREQ - I2C clock frequency (in MHz, from 1 to 24)
|
||||||
|
*
|
||||||
|
* I2C_OARL: | ADD[7:1] | ADD0 | - LSB of address
|
||||||
|
* ADD0 - 0th bit of address in 10bit mode, don't care in 7bit mode
|
||||||
|
*
|
||||||
|
* I2C_OARH: | ADDMODE | ADDCONF | res[5:3] | ADD[9:8] | res |
|
||||||
|
* ADDMODE - 0 for 7bit, 1 for 10bit address
|
||||||
|
* ADDCONF - must always be written as 1 ("address is set")
|
||||||
|
*
|
||||||
|
* I2C_DR - data I/O register
|
||||||
|
*
|
||||||
|
* I2C_SR1: | TXE | RXNE | res | STOPF | ADD10 | BTF | ADDR | SB |
|
||||||
|
* TXE - set when DR is empty for transmission
|
||||||
|
* RXNE - set when DR not empty in receiver mode
|
||||||
|
* STOPF - stop detected (slave mode)
|
||||||
|
* ADD10 - 10 bit header sent (first byte)
|
||||||
|
* BTF - byte transfer finished (set when NOSTRETCH==0)
|
||||||
|
* ADDR - address sent (not set if NACK)
|
||||||
|
* SB - start condition
|
||||||
|
*
|
||||||
|
* I2C_SR2: | res[7:6] | WUFH | res | OVR | AF | ARLO | BERR |
|
||||||
|
* WUFH - wakeup from halt
|
||||||
|
* OVR - overrun/underrun
|
||||||
|
* AF - acknowledge failure (must be cleared by software)
|
||||||
|
* ARLO - arbitration lost (another master on line, clear it by SW)
|
||||||
|
* BERR - bus error (clear by SW)
|
||||||
|
*
|
||||||
|
* I2C_SR3: | res[7:5] | GENCALL | res | TRA | BUSY | MSL |
|
||||||
|
* GENCALL - general call in slave mode
|
||||||
|
* TRA - transmitter(1)/receiver(0) flag
|
||||||
|
* BUSY - bus busy (another communication detected)
|
||||||
|
* MSL - slave(0)/master(1) mode, set/cleared by HW
|
||||||
|
*
|
||||||
|
* I2C_ITR: | res[7:3] | ITBUFEN | ITEVTEN | ITERREN |
|
||||||
|
* ITBUFEN - enable buffer interrupt
|
||||||
|
* ITEVTEN - enable event interrupt
|
||||||
|
* ITERREN - enable error interrupt
|
||||||
|
*
|
||||||
|
* I2C_CCRL: | CCR[7:0] |
|
||||||
|
* SCLH clock in master mode
|
||||||
|
* standard: T = 2*CCR*tmaster, Tlow = Thigh = CCR*tmaster
|
||||||
|
* fast: (DUTY==0) T = 3*CCR*tmaster, Thigh = CCR*tmaster, Tlow = 2*Thigh
|
||||||
|
* (DUTY==1) T = 25*CCR*tmaster, Thigh=9*CCR*tmaster, Tlow=16*CCR*tmaster
|
||||||
|
* fmaster - clock by I2C_FREQR
|
||||||
|
* minimum allowed value is 4 (exept FAST DUTY, when it is 1)
|
||||||
|
*
|
||||||
|
* I2C_CCRH: | F/S | DUTY | res[5:4] | CCR[11:8] |
|
||||||
|
* F/S - == 1 in fast mode
|
||||||
|
* DUTY - (see upper)
|
||||||
|
* IN standard mode 100kHz is: FREQR=8, CCR=0x28
|
||||||
|
*
|
||||||
|
* I2C_TRISER: | res[7:6] | TRISE[5:0] |
|
||||||
|
* TRISE - maximum rise time (0x09 for standard @100kHz)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "i2c.h"
|
||||||
|
#include "ports_definition.h"
|
||||||
|
|
||||||
|
static U8 addr7r = 0, addr7w = 0;
|
||||||
|
|
||||||
|
static U16 _c;
|
||||||
|
#define I2C_WAIT(evt) do{for(_c = 0; _c < 16000; _c++){ \
|
||||||
|
if(evt) break;} if(_c == 16000) return ret;}while(0)
|
||||||
|
|
||||||
|
static U8 _d;
|
||||||
|
#define I2C_LINEWAIT() do{ for(_d = 0; _d < 16; _d++){\
|
||||||
|
if(!(I2C_SR3 & 2)) break; I2C_CR2 |= 2;\
|
||||||
|
for(_c = 0; _c < 1000; _c++) if(!(I2C_CR2 & 2)) break;} \
|
||||||
|
if(_d == 16) return I2C_LINEBUSY; }while(0)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* configure 100kHz speed in standard mode & enable i2c
|
||||||
|
*/
|
||||||
|
void i2c_setup(){
|
||||||
|
// configure pins: PB5 - I2C_SDA; PB4 - I2C_SCL (both opendrain)
|
||||||
|
PORT(PB, ODR) |= GPIO_PIN4|GPIO_PIN5; // set to 1
|
||||||
|
PORT(PB, DDR) |= GPIO_PIN4|GPIO_PIN5;
|
||||||
|
PORT(PB, CR2) |= GPIO_PIN4|GPIO_PIN5; // fast mode
|
||||||
|
// Don't forget to connect pullup resistor to I2C foots
|
||||||
|
I2C_FREQR = 8; // 8MHz fmaster
|
||||||
|
I2C_TRISER = 9; // rise time 1000ns
|
||||||
|
I2C_CCRL = 0x28; // 100kHz
|
||||||
|
I2C_CCRH = 0;
|
||||||
|
I2C_ITR = 0; // disable all I2C interrupts
|
||||||
|
I2C_CR2 |= 4; // ACK
|
||||||
|
I2C_CR1 |= 1; // enable I2C
|
||||||
|
}
|
||||||
|
|
||||||
|
void i2c_set_addr7(U8 addr){
|
||||||
|
addr7w = addr << 1;
|
||||||
|
addr7r = addr7w | 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* send one byte in 7bit address mode
|
||||||
|
* return I2C_OK if success errcode if fails
|
||||||
|
*/
|
||||||
|
i2c_status i2c_7bit_send_onebyte(U8 data){
|
||||||
|
i2c_status ret = I2C_TMOUT;
|
||||||
|
//I2C_CR2 |= 0x80; I2C_CR2 &= ~0x80; // reset I2C
|
||||||
|
I2C_LINEWAIT();
|
||||||
|
//I2C_WAIT(!(I2C_SR3 & 2)); // wait for line released
|
||||||
|
I2C_CR2 |= 1; // send START
|
||||||
|
I2C_WAIT(I2C_SR1 & 1); // wait for SB
|
||||||
|
I2C_DR = addr7w;
|
||||||
|
ret = I2C_NOADDR;
|
||||||
|
I2C_WAIT((I2C_SR1 & 2) || I2C_SR2); // wait for ADDR
|
||||||
|
if(I2C_SR2){ // NACK or other error
|
||||||
|
ret = I2C_NACK;
|
||||||
|
goto endoftransmission;
|
||||||
|
}
|
||||||
|
ret = I2C_HWPROBLEM;
|
||||||
|
if(!(I2C_SR3 & 4)) goto endoftransmission; // interface is in receiver mode
|
||||||
|
I2C_WAIT((I2C_SR1 & 0x80) || I2C_SR2); // wait for TXE
|
||||||
|
if(I2C_SR2){
|
||||||
|
ret = I2C_NACK;
|
||||||
|
goto endoftransmission;
|
||||||
|
}
|
||||||
|
I2C_DR = data; // send data
|
||||||
|
I2C_WAIT((I2C_SR1 & 0x84 == 0x84) || I2C_SR2); // wait for TXE & BTF
|
||||||
|
if(!I2C_SR2) ret = I2C_OK;
|
||||||
|
else ret = I2C_NACK;
|
||||||
|
endoftransmission:
|
||||||
|
I2C_SR2 = 0; // clear all error flagss
|
||||||
|
I2C_CR2 |= 2; // set STOP
|
||||||
|
while(I2C_CR2 & 2); // wait for STOP sent
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* send datalen bytes over I2C
|
||||||
|
* return I2C_OK if OK
|
||||||
|
*/
|
||||||
|
i2c_status i2c_7bit_send(U8 *data, U8 datalen){
|
||||||
|
i2c_status ret = I2C_TMOUT;
|
||||||
|
//I2C_CR2 |= 0x80; I2C_CR2 &= ~0x80; // reset I2C
|
||||||
|
//I2C_WAIT(!(I2C_SR3 & 2)); // wait for line released
|
||||||
|
I2C_LINEWAIT();
|
||||||
|
I2C_CR2 |= 1; // send START
|
||||||
|
ret = I2C_TMOUT;
|
||||||
|
I2C_WAIT(I2C_SR1 & 1); // wait for SB
|
||||||
|
I2C_DR = addr7w;
|
||||||
|
ret = I2C_NOADDR;
|
||||||
|
I2C_WAIT((I2C_SR1 & 2) || I2C_SR2); // wait for ADDR
|
||||||
|
if(I2C_SR2){ // NACK or other error
|
||||||
|
ret = I2C_NACK;
|
||||||
|
goto endoftransmission;
|
||||||
|
}
|
||||||
|
ret = I2C_HWPROBLEM;
|
||||||
|
if(!(I2C_SR3 & 4)) goto endoftransmission; // interface is in receiver mode
|
||||||
|
while(datalen--){
|
||||||
|
I2C_WAIT((I2C_SR1 & 0x80) || I2C_SR2); // wait for TXE
|
||||||
|
if(I2C_SR2){
|
||||||
|
ret = I2C_NACK;
|
||||||
|
goto endoftransmission;
|
||||||
|
}
|
||||||
|
I2C_DR = *data--; // send data
|
||||||
|
}
|
||||||
|
I2C_WAIT((I2C_SR1 & 0x84 == 0x84) || I2C_SR2); // wait for TXE & BTF
|
||||||
|
if(!I2C_SR2) ret = I2C_OK;
|
||||||
|
else ret = I2C_NACK;
|
||||||
|
endoftransmission:
|
||||||
|
I2C_SR2 = 0; // clear all error flags
|
||||||
|
I2C_CR2 |= 2; // set STOP
|
||||||
|
while(I2C_CR2 & 2); // wait for STOP sent
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* get one byte by I2C
|
||||||
|
* return I2C_OK if ok
|
||||||
|
* return I2C_NACK if some error present
|
||||||
|
*/
|
||||||
|
i2c_status i2c_7bit_receive_onebyte(U8 *data){
|
||||||
|
i2c_status ret = I2C_TMOUT;
|
||||||
|
//I2C_CR2 |= 0x80; I2C_CR2 &= ~0x80; // reset I2C
|
||||||
|
//I2C_WAIT(!(I2C_SR3 & 2)); // wait for line released
|
||||||
|
I2C_LINEWAIT();
|
||||||
|
I2C_CR2 |= 1; // send START
|
||||||
|
ret = I2C_TMOUT;
|
||||||
|
I2C_WAIT(I2C_SR1 & 1); // wait for SB
|
||||||
|
I2C_DR = addr7r; // send address & read bit
|
||||||
|
ret = I2C_NOADDR;
|
||||||
|
I2C_WAIT((I2C_SR1 & 2) || I2C_SR2); // wait for ADDR
|
||||||
|
if(I2C_SR2){ // NACK or other error
|
||||||
|
ret = I2C_NACK;
|
||||||
|
goto endofreceiving;
|
||||||
|
}
|
||||||
|
// clear POS|ACK
|
||||||
|
I2C_CR2 &= ~0x0c;
|
||||||
|
ret = I2C_HWPROBLEM;
|
||||||
|
// read again SR1, SR3 to clear ADDR
|
||||||
|
if(!(I2C_SR1 & 2) || (I2C_SR3 & 4)) goto endofreceiving; // interface is in transmitter mode
|
||||||
|
// set STOP
|
||||||
|
I2C_CR2 |= 2;
|
||||||
|
// wait for RxNE
|
||||||
|
I2C_WAIT((I2C_SR1 & 0x40) || I2C_SR2);
|
||||||
|
if(I2C_SR2){
|
||||||
|
ret = I2C_NACK;
|
||||||
|
goto endofreceiving; // error
|
||||||
|
}
|
||||||
|
ret = I2C_OK;
|
||||||
|
// read data clearing RxNE
|
||||||
|
*data = I2C_DR;
|
||||||
|
endofreceiving:
|
||||||
|
I2C_SR2 = 0; // clear all error flags
|
||||||
|
while(I2C_CR2 & 2); // wait for STOP sent
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
i2c_status i2c_7bit_receive_twobyte(U8 *data){
|
||||||
|
i2c_status ret = I2C_TMOUT;
|
||||||
|
//I2C_CR2 |= 0x80; I2C_CR2 &= ~0x80; // reset I2C
|
||||||
|
//I2C_WAIT(!(I2C_SR3 & 2)); // wait for line released
|
||||||
|
I2C_LINEWAIT();
|
||||||
|
I2C_CR2 |= 1; // send START
|
||||||
|
ret = I2C_TMOUT;
|
||||||
|
I2C_WAIT(I2C_SR1 & 1); // wait for SB
|
||||||
|
I2C_DR = addr7r; // send address & read bit
|
||||||
|
// set POS|ACK
|
||||||
|
I2C_CR2 |= 0x0c;
|
||||||
|
ret = I2C_NOADDR;
|
||||||
|
I2C_WAIT((I2C_SR1 & 2) || I2C_SR2); // wait for ADDR
|
||||||
|
if(I2C_SR2){ // NACK or other error
|
||||||
|
ret = I2C_NACK;
|
||||||
|
goto endofreceiving;
|
||||||
|
}
|
||||||
|
ret = I2C_HWPROBLEM;
|
||||||
|
// read again SR1, SR3 to clear ADDR
|
||||||
|
if(!(I2C_SR1 & 2) || (I2C_SR3 & 4)) return 0; // interface is in transmitter mode
|
||||||
|
// clear ACK
|
||||||
|
I2C_CR2 &= ~4;
|
||||||
|
// wait for BTF
|
||||||
|
I2C_WAIT((I2C_SR1 & 4) || I2C_SR2);
|
||||||
|
if(I2C_SR2){
|
||||||
|
ret = I2C_NACK;
|
||||||
|
goto endofreceiving;
|
||||||
|
}
|
||||||
|
// set STOP
|
||||||
|
I2C_CR2 |= 2;
|
||||||
|
ret = 1;
|
||||||
|
// read data
|
||||||
|
data[0] = I2C_DR;
|
||||||
|
data[1] = I2C_DR;
|
||||||
|
endofreceiving:
|
||||||
|
I2C_SR2 = 0; // clear all error flags
|
||||||
|
while(I2C_CR2 & 2); // wait for STOP sent
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
INTERRUPT_HANDLER(I2C_IRQHandler, 19){
|
||||||
|
}
|
||||||
45
si7005/i2c.h
Normal file
45
si7005/i2c.h
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* i2c.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 __I2C_H__
|
||||||
|
#define __I2C_H__
|
||||||
|
|
||||||
|
#include "stm8l.h"
|
||||||
|
|
||||||
|
// flags for i2c_state
|
||||||
|
typedef enum{
|
||||||
|
I2C_OK = 0,
|
||||||
|
I2C_LINEBUSY,
|
||||||
|
I2C_TMOUT,
|
||||||
|
I2C_NOADDR,
|
||||||
|
I2C_NACK,
|
||||||
|
I2C_HWPROBLEM
|
||||||
|
} i2c_status;
|
||||||
|
|
||||||
|
void i2c_setup();
|
||||||
|
void i2c_set_addr7(U8 addr);
|
||||||
|
i2c_status i2c_7bit_send_onebyte(U8 data);
|
||||||
|
i2c_status i2c_7bit_send(U8 *data, U8 datalen);
|
||||||
|
i2c_status i2c_7bit_receive_onebyte(U8 *data);
|
||||||
|
i2c_status i2c_7bit_receive_twobyte(U8 *data);
|
||||||
|
|
||||||
|
#endif // __I2C_H__
|
||||||
194
si7005/interrupts.c
Normal file
194
si7005/interrupts.c
Normal file
@ -0,0 +1,194 @@
|
|||||||
|
/*
|
||||||
|
* 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"
|
||||||
|
|
||||||
|
// 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){
|
||||||
|
/* 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;
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
#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
si7005/interrupts.h
Normal file
144
si7005/interrupts.h
Normal 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__
|
||||||
100
si7005/main.c
Normal file
100
si7005/main.c
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
/*
|
||||||
|
* 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 "si7005.h"
|
||||||
|
|
||||||
|
volatile unsigned long Global_time = 0L; // global time in ms
|
||||||
|
U16 paused_val = 500; // interval between LED flashing
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
unsigned long T = 0L, siT = 0L;
|
||||||
|
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();
|
||||||
|
|
||||||
|
si7005_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(UART_read_byte(&rb)){ // buffer isn't empty
|
||||||
|
switch(rb){
|
||||||
|
case 'h': // help
|
||||||
|
case 'H':
|
||||||
|
uart_write("\nPROTO:\n+/-\tLED period\n"
|
||||||
|
"I\tread Si7005 device id\n"
|
||||||
|
"T\tread themperature\n"
|
||||||
|
"P\tread pressure\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 'I':
|
||||||
|
si7005_read_ID();
|
||||||
|
break;
|
||||||
|
case 'T':
|
||||||
|
si7005_read_T();
|
||||||
|
break;
|
||||||
|
case 'P':
|
||||||
|
si7005_read_P();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(Global_time != siT){
|
||||||
|
siT = Global_time;
|
||||||
|
si7005_process();
|
||||||
|
}
|
||||||
|
}while(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
40
si7005/ports_definition.h
Normal file
40
si7005/ports_definition.h
Normal 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__
|
||||||
146
si7005/si7005.c
Normal file
146
si7005/si7005.c
Normal file
@ -0,0 +1,146 @@
|
|||||||
|
/*
|
||||||
|
* si7005.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 "i2c.h"
|
||||||
|
#include "uart.h"
|
||||||
|
#include "si7005.h"
|
||||||
|
|
||||||
|
#define DEVID (0x40)
|
||||||
|
|
||||||
|
typedef enum{
|
||||||
|
RELAX = 0,
|
||||||
|
WAITFORT,
|
||||||
|
WAITFORP
|
||||||
|
}si7005_state;
|
||||||
|
|
||||||
|
static si7005_state state = RELAX;
|
||||||
|
|
||||||
|
void si7005_setup(){
|
||||||
|
i2c_setup();
|
||||||
|
i2c_set_addr7(DEVID);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* read device ID
|
||||||
|
*/
|
||||||
|
void si7005_read_ID(){
|
||||||
|
U8 ID;
|
||||||
|
i2c_status st = I2C_OK;
|
||||||
|
if(state != RELAX){
|
||||||
|
error_msg("measurements are in process");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if((st = i2c_7bit_send_onebyte(0x11)) == I2C_OK){
|
||||||
|
if((st = i2c_7bit_receive_onebyte(&ID)) == I2C_OK){
|
||||||
|
uart_write("got ID: ");
|
||||||
|
printUHEX(ID);
|
||||||
|
UART_send_byte('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(st != I2C_OK){
|
||||||
|
uart_write("can't write 0x11, errcode: ");
|
||||||
|
printUHEX(st);
|
||||||
|
UART_send_byte('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* start themperature reading
|
||||||
|
*/
|
||||||
|
void si7005_read_T(){
|
||||||
|
const U8 cmd[2] = {0x03, 0x11};
|
||||||
|
if(state != RELAX){
|
||||||
|
error_msg("measurements are in process");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(i2c_7bit_send(cmd, 2) != I2C_OK){
|
||||||
|
error_msg("can't send read sequence");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
state = WAITFORT;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* start pressure reading
|
||||||
|
*/
|
||||||
|
void si7005_read_P(){
|
||||||
|
const U8 cmd[2] = {0x03, 0x01};
|
||||||
|
if(state != RELAX){
|
||||||
|
error_msg("measurements are in process");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(i2c_7bit_send(cmd, 2) != I2C_OK){
|
||||||
|
error_msg("can't send read sequence");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
state = WAITFORP;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* display readed value in tenths
|
||||||
|
*/
|
||||||
|
static void display_data(U8 *d){
|
||||||
|
U16 udata = *((int *)d);
|
||||||
|
long idata = 0L;
|
||||||
|
switch (state){
|
||||||
|
case WAITFORP: // display pressure
|
||||||
|
udata >>=4; // get 12 bit data
|
||||||
|
idata = (udata*10L)/16L - 240L;
|
||||||
|
uart_write("P*10=");
|
||||||
|
break;
|
||||||
|
case WAITFORT: // display themperature
|
||||||
|
udata >>=2; // get 14 bit data
|
||||||
|
idata = (udata*100L)/32L - 5000L;
|
||||||
|
uart_write("T*100=");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
print_long(idata);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* process state machine
|
||||||
|
*/
|
||||||
|
void si7005_process(){
|
||||||
|
U8 T[2], b;
|
||||||
|
i2c_status st;
|
||||||
|
if(state == RELAX) return;
|
||||||
|
if(state == WAITFORP || state == WAITFORT){ // poll RDY
|
||||||
|
if(i2c_7bit_send_onebyte(0) == I2C_OK){
|
||||||
|
if(i2c_7bit_receive_onebyte(&b) == I2C_OK){
|
||||||
|
if(b) return; // !RDY
|
||||||
|
if((st = i2c_7bit_send_onebyte(1)) == I2C_OK)
|
||||||
|
if((st = i2c_7bit_receive_twobyte(T)) == I2C_OK)
|
||||||
|
display_data(T);
|
||||||
|
state = RELAX;
|
||||||
|
if(st){
|
||||||
|
uart_write("can't read value, err: ");
|
||||||
|
printUHEX(st);
|
||||||
|
UART_send_byte('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
error_msg("can't poll !RDY");
|
||||||
|
state = RELAX;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
32
si7005/si7005.h
Normal file
32
si7005/si7005.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* si7005.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 __SI7005_H__
|
||||||
|
#define __SI7005_H__
|
||||||
|
|
||||||
|
void si7005_setup();
|
||||||
|
void si7005_read_ID();
|
||||||
|
void si7005_read_T();
|
||||||
|
void si7005_read_P();
|
||||||
|
void si7005_process();
|
||||||
|
|
||||||
|
#endif // __SI7005_H__
|
||||||
169
si7005/uart.c
Normal file
169
si7005/uart.c
Normal 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
si7005/uart.h
Normal file
45
si7005/uart.h
Normal 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__
|
||||||
154
si7005/uart.ihx
Normal file
154
si7005/uart.ihx
Normal file
@ -0,0 +1,154 @@
|
|||||||
|
:2080A000808080808080808080808080808080805204AE5240F66B047B04A520274DAE520D
|
||||||
|
:2080C00041F66B017B04A4804D27FDAE52417B01F7AE00011F02C6000D97C6000D4CC70015
|
||||||
|
:2080E0000D4F9572FB027B01F7C6000CC1000D260FC6000C4CC7000CA1082604725F000C37
|
||||||
|
:20810000C6000DA1082604725F000D5B048080AE5342F644241B90CE001172A90001C6006F
|
||||||
|
:1781200010A90097C6000FA9009590CF0011CF000FAE53427F8080D5
|
||||||
|
:20813700AE5240F64824F9AE52417B03F781160390F64D2718AE5240F64824F9AE5245F655
|
||||||
|
:20815700AA08F790F6905CAE5241F720E3815202C6000DC1000C26034F20271605AE0001B4
|
||||||
|
:208177001F01C6000C97C6000C4CC7000C4F9572FB01F690F7C6000CA1082604725F000C1D
|
||||||
|
:20819700A6015B0281521C5F1F031F017B21A1042303CC82747B21A1032603CC82740D21B2
|
||||||
|
:2081B7002603CC8274961C00051F1B4F5F9772FB1B7F4CA10C25F51E1B1C000AA60AF77BEC
|
||||||
|
:2081D70021A101270E7B21A102271C7B21A104272120301E1FF66B1A5F0F171F027B1A6BD7
|
||||||
|
:2081F700047B176B01201C161F90FE5F17031F0120111E1FE6036B16E602FE6B031F017B07
|
||||||
|
:20821700166B04A6096B114B0A5F894B001E07891E0789CD8EF25B089F887B126B13840A43
|
||||||
|
:20823700115F417B124172FB1BAB30F74B0A5F894B001E07891E0789CD90235B081F03174E
|
||||||
|
:20825700011E0326041E0127067B11A1FF2CB87B114C5F9772FB1B89CD81455B025B1C8198
|
||||||
|
:2082770052110F01965C5C1F101E101C000B7F1E16A300007B15A2007B14A2002E1416167B
|
||||||
|
:2082970090504F1215974F12149517161F14A6016B01A60B6B0E4B0A5F894B001E1A891ECC
|
||||||
|
:2082B7001A89CD8E4D5B089F0A0E5F417B0E4172FB10AB30F74B0A5F894B001E1A891E1A08
|
||||||
|
:2082D70089CD8F6F5B081F1617147B0EA1002C034F2002A6011E1626041E1427034D26B61C
|
||||||
|
:2082F700887B0F6B10844D27140D0127100A0E7B0E6B0F5F7B0F9772FB10A62DF75F7B0FBE
|
||||||
|
:208317009772FB1089CD81455B025B1181521ACE00111F0DCE000F1F0B5F1F091F070F048E
|
||||||
|
:208337000F020F01961C000389CD81655B024D2603CC83C87B03A12D260E1E09260A1E072E
|
||||||
|
:208357002606A6016B0420697B03A1302403CC83EA7B03A1392303CC83EAA6016B021E099A
|
||||||
|
:20837700891E09894B0A5F894B00CD90C25B081F11170F7B030F195F90977B19909572F9FD
|
||||||
|
:20839700119F1910979E190F9572A200309FA2006B149EA20017096B077B146B08AE7FFFF7
|
||||||
|
:2083B70013094F12084F120724075F1F091F070F0190CE001172F20DC60010120C95C600A2
|
||||||
|
:2083D7000F120B9790A327109EA2009FA2002403CC833B0D0126040D0226034F201A7B09AA
|
||||||
|
:2083F700887B0B6B07846B050D0427051E05501F051E1D1605FFA6015B1A81AE848F89CD15
|
||||||
|
:2084170081455B021E0389CD81455B024B0ACD813784817B03A40F6B037B03887B04A10ADA
|
||||||
|
:20843700842406AB306B032004AB576B037B0381AE849889CD81455B027B034EA40F88CD84
|
||||||
|
:20845700842A5B0188CD8137847B0388CD842A5B0188CD81378481AE5011F6AA20F7AE50B7
|
||||||
|
:2084770012F6AA20F7AE5242A611F7AE5243A606F7AE5245A62CF7810A4552524F523A20C9
|
||||||
|
:048497000030780039
|
||||||
|
:02922B00000041
|
||||||
|
:20849B00CD88B74B40CD88F284815202725D000E270BAE86B189CD84125B0220494B11CDBB
|
||||||
|
:2084BB0088FE5B016B024D2623965C89CD8BD45B026B024D2616AE86CD89CD81455B027BCD
|
||||||
|
:2084DB000188CD8447844B0ACD8137840D022716AE86D689CD81455B027B0288CD8447848E
|
||||||
|
:2084FB004B0ACD8137845B02815202965CA603F79093905CA61190F7725D000E270BAE86AF
|
||||||
|
:20851B00B189CD84125B02201A4B0289CD8A5C5B034D270BAE86F289CD84125B02200435DE
|
||||||
|
:20853B0001000E5B02815202965CA603F79093905CA60190F7725D000E270BAE86B189CDC6
|
||||||
|
:20855B0084125B02201A4B0289CD8A5C5B034D270BAE86F289CD84125B0220043502000E95
|
||||||
|
:20857B005B028152081E0BFEC6000EA101274DC6000EA1022703CC8624A61062905F899060
|
||||||
|
:20859B00894B0A5F894B00CD90C25B084B104B004B004B00899089CD8F6F5B085172A200BC
|
||||||
|
:2085BB00F09FA2006B029EA20017076B057B026B06AE870B89CD81455B0220425454905F94
|
||||||
|
:2085DB008990894B645F894B00CD90C25B084B204B004B004B00899089CD8F6F5B0851729B
|
||||||
|
:2085FB00A213889FA2006B029EA20017076B057B026B06AE871189CD81455B0220001E07B5
|
||||||
|
:20861B00891E0789CD82775B045B08815204725D000E2603CC86AEC6000EA102270AC60035
|
||||||
|
:20863B000EA1012703CC86AE4B00CD88FE5B014D2654965C89CD8BD45B024D26560D012683
|
||||||
|
:20865B00524B01CD88FE5B016B044D2619965C5C9093899089CD8D075B02856B044D260679
|
||||||
|
:20867B0089CD857E5B02725F000E0D042725AE871889CD81455B027B0488CD8447844B0AAF
|
||||||
|
:20869B00CD813784200DAE873089CD84125B02725F000E5B04816D6561737572656D656EEA
|
||||||
|
:2086BB0074732061726520696E2070726F6365737300676F742049443A200063616E27742C
|
||||||
|
:2086DB0020777269746520307831312C20657272636F64653A200063616E27742073656E4D
|
||||||
|
:2086FB006420726561642073657175656E636500502A31303D00542A3130303D0063616E30
|
||||||
|
:20871B00277420726561642076616C75652C206572723A200063616E277420706F6C6C20F7
|
||||||
|
:05873B00215244590029
|
||||||
|
:01922D000040
|
||||||
|
:208000008200808382000000820080A0820080A1820080A2820080A3820080A4820080A57E
|
||||||
|
:20802000820080A6820080A78200000082000000820080A8820080A9820080AA820080AB3D
|
||||||
|
:20804000820080AC820080AD820080AE820000008200000082008E4C820080AF820080B050
|
||||||
|
:208060008200810E8200810F8200813682000000820000008200000082000000820000001A
|
||||||
|
:1D808300AE000B2707724F00005A26F9AE000B2709D6922AD7000B5A26F7CC80801F
|
||||||
|
:03808000CC87406A
|
||||||
|
:2087400052115F1F081F065F1F041F0272107F60AE50C67FAE5345A607F7AE5346A67DF779
|
||||||
|
:20876000AE5341A601F7AE5340A685F7AE500CF6AA04F7AE500DF6AA04F7CD846ECD849BC0
|
||||||
|
:208780009ACE001172F0081F10C6001012076B0FC6000F12066B0ECE00134F0F0A88131103
|
||||||
|
:2087A00084120F7B0A120E2511CE00111308C600101207C6000F12062411CE00111F08CE5A
|
||||||
|
:2087C000000F1F06AE500AF6A804F7965C89CD81655B024D2603CC883F7B01A12B2723A1FD
|
||||||
|
:2087E0002D2737A1482710A1492747A150274DA1542744A1682648AE886389CD81455B0228
|
||||||
|
:20880000203DCE00131C0064CF0013A32710232F35F40014350100132025CE00131D00645F
|
||||||
|
:20882000CF0013A301F4241735F4001435010013200DCD84A52008CD85042003CD85411E88
|
||||||
|
:2088400004C30011260A1E02C3000F2603CC8781CE00111F04CE000F1F02CD8627CC8781D3
|
||||||
|
:208860005B11810A50524F544F3A0A2B2F2D094C454420706572696F640A49097265616429
|
||||||
|
:2088800020536937303035206465766963652069640A540972656164207468656D7065729B
|
||||||
|
:1788A00061747572650A5009726561642070726573737572650A00FE
|
||||||
|
:06922E000000000001F445
|
||||||
|
:2088B700AE5005F6AA30F7AE5007F6AA30F7AE5009F6AA30F7AE5212A608F7AE521DA609BA
|
||||||
|
:2088D700F7AE521BA628F7AE521C7FAE521A7FAE5211F6AA04F772105210817B0348C700D8
|
||||||
|
:2088F70016AA01C70015815202725F000BAE5219F6A502272EAE5211F6AA02F7725F000A83
|
||||||
|
:20891700725F0009AE5211F6A502270CCE00095CCF0009A303E825ECC6000B4CC7000BA14B
|
||||||
|
:208937001025CAC6000BA1102605A601CC8A59AE5211F6AA01F7725F000A725F0009AE52C0
|
||||||
|
:2089570017F644250CCE00095CCF0009A33E8025EDCE0009A33E802605A602CC8A59AE5246
|
||||||
|
:2089770016C60016F7725F000A725F0009AE5217F69095A5022613AE5218F64D260CCE00D5
|
||||||
|
:20899700095CCF0009A33E8025E3CE0009A33E802605A603CC8A59AE5218F66B020D0227AE
|
||||||
|
:2089B70007A6046B01CC8A44A6056B01AE5219F6A5042603CC8A44725F000A725F00099012
|
||||||
|
:2089D7009E4825100D02260CCE00095CCF0009A33E8025EBCE0009A33E802604A605206219
|
||||||
|
:2089F7000D022706A6046B012043AE52167B05F7725F000A725F0009AE5217F6442513AE32
|
||||||
|
:208A17005218F64D260CCE00095CCF0009A33E8025E6CE0009A33E802604A6052024AE5298
|
||||||
|
:208A370018F64D26040F012004A6046B01AE52187FAE5211F6AA02F7AE5211F6A50226F843
|
||||||
|
:208A57007B015B02815204725F000BAE5219F6A502272EAE5211F6AA02F7725F000A725F12
|
||||||
|
:208A77000009AE5211F6A502270CCE00095CCF0009A303E825ECC6000B4CC7000BA1102586
|
||||||
|
:208A9700CAC6000BA1102605A601CC8BD1AE5211F6AA01F7725F000A725F0009AE5217F60E
|
||||||
|
:208AB70044250CCE00095CCF0009A33E8025EDCE0009A33E802605A602CC8BD1AE5216C69D
|
||||||
|
:208AD7000016F7725F000A725F0009AE5217F6A5022613AE5218F64D260CCE00095CCF0041
|
||||||
|
:208AF70009A33E8025E5CE0009A33E802605A603CC8BD1AE5218F64D2707A6046B01CC8BC1
|
||||||
|
:208B1700BCA6056B01AE5219F6A5042603CC8BBC16077B096B037B036B040A03AE5217F661
|
||||||
|
:208B37006B020D042744725F000A725F00097B02482513AE5218F64D260CCE00095CCF00F4
|
||||||
|
:208B570009A33E8025E8CE0009A33E802605A605CC8BD1AE5218F64D2706A6046B0120454E
|
||||||
|
:208B770090F6905AAE5216F720AC725F000A725F00097B02442513AE5218F64D260CCE008C
|
||||||
|
:208B9700095CCF0009A33E8025E8CE0009A33E802604A6052024AE5218F64D26040F01200D
|
||||||
|
:208BB70004A6046B01AE52187FAE5211F6AA02F7AE5211F6A50226F87B015B048188725FC2
|
||||||
|
:208BD700000BAE5219F6A502272EAE5211F6AA02F7725F000A725F0009AE5211F6A5022734
|
||||||
|
:208BF7000CCE00095CCF0009A303E825ECC6000B4CC7000BA11025CAC6000BA1102605A6C6
|
||||||
|
:208C170001CC8D04AE5211F6AA01F7725F000A725F0009AE5217F644250CCE00095CCF00FD
|
||||||
|
:208C370009A33E8025EDCE0009A33E802605A602CC8D04AE5216C60015F7725F000A725FA5
|
||||||
|
:208C57000009AE5217F6A5022613AE5218F64D260CCE00095CCF0009A33E8025E5CE00092D
|
||||||
|
:208C7700A33E802605A603CC8D04AE5218F64D2706A6046B012068AE5211F6A4F3F7A605E5
|
||||||
|
:208C97006B01AE5217F6A5022755AE5219F6A504264DAE5211F6AA02F7725F000A725F00A0
|
||||||
|
:208CB70009AE5217F6A5402613AE5218F64D260CCE00095CCF0009A33E8025E5CE0009A3EC
|
||||||
|
:208CD7003E802604A6052025AE5218F64D2706A6046B01200A0F011604AE5216F690F7AE72
|
||||||
|
:208CF70052187FAE5211F6A50226F87B015B01815202725F000BAE5219F6A502272EAE5214
|
||||||
|
:208D170011F6AA02F7725F000A725F0009AE5211F6A502270CCE00095CCF0009A303E8253E
|
||||||
|
:208D3700ECC6000B4CC7000BA11025CAC6000BA1102605A601CC8E49AE5211F6AA01F7728F
|
||||||
|
:208D57005F000A725F0009AE5217F644250CCE00095CCF0009A33E8025EDCE0009A33E8081
|
||||||
|
:208D77002605A602CC8E49AE5216C60015F7AE5211F6AA0CF7725F000A725F0009AE5217FE
|
||||||
|
:208D9700F6A4026B020D022613AE5218F64D260CCE00095CCF0009A33E8025E1CE0009A3F2
|
||||||
|
:208DB7003E802605A603CC8E49AE5218F64D2707A6046B01CC8E3B0D022708AE5219F6A541
|
||||||
|
:208DD7000427034F206CAE5211F6A4FBF7725F000A725F0009AE5217F6A5042613AE52181A
|
||||||
|
:208DF700F64D260CCE00095CCF0009A33E8025E5CE0009A33E802604A6052036AE5218F600
|
||||||
|
:208E17004D2706A6046B01201BAE5211F6AA02F7A6016B011605AE5216F690F7905CAE5219
|
||||||
|
:168E370016F690F7AE52187FAE5211F6A50226F87B015B02818055
|
||||||
|
:02923400000038
|
||||||
|
:208E4D00521D1E22A300007B21A2007B20A2002F040F0D2004A6016B0D0D0D27151E2250C0
|
||||||
|
:208E6D004F12216B1B4F12201F186B167B1B6B172008162217181620171616181714161615
|
||||||
|
:208E8D0017121E26A300007B25A2007B24A2002E1E7B27406B114F12266B104F12256B0F86
|
||||||
|
:208EAD004F12246B0E1610170B160E170920081626170B16241709160B170716091E07892F
|
||||||
|
:208ECD0090891E18891E1889CD8EF25B0817010D0D270D504F120290974F12019095200255
|
||||||
|
:208EED0016015B1D8152040F020F017B0B484F494D262E160D1E0B905859170D1F0B1E09D5
|
||||||
|
:208F0D00130D7B08120C7B07120B240D160D1E0B549056170D1F0B20080C017B016B0220A1
|
||||||
|
:208F2D00CA7B026B041E09130D7B08120C7B07120B2513160972F20D7B08120C977B071253
|
||||||
|
:208F4D000B9517091F07160D1E0B549056170D1F0B7B046B030A040D0326CA1E0916075BB0
|
||||||
|
:208F6D000481521E1E23A300007B22A2007B21A2002F040F112004A6016B110D1127151E7C
|
||||||
|
:208F8D0023504F12226B1C4F12211F146B127B1C6B132008162317141621171216141719BA
|
||||||
|
:208FAD00161217171E27A300007B26A2007B25A2002F040F162004A6016B160D16271E7B5A
|
||||||
|
:208FCD0028406B104F12276B0F4F12266B0E4F12256B0D160F170B160D170920081627179B
|
||||||
|
:208FED000B16251709160B170716091E078990891E1D891E1D89CD90235B0817017B1118B2
|
||||||
|
:20900D00164D270D504F120290974F12019095200216015B1E8152125F1F051F03A6206BDE
|
||||||
|
:20902D00027B15484F496B0116171E1590585917171F157B036B0F1E04887B076B13840814
|
||||||
|
:20904D001259090F1F047B126B067B0F6B030D01271A7B06AA016B0A7B056B097B046B0891
|
||||||
|
:20906D007B036B0716091705160717031E05131B7B04121A7B031219252B160572F21B7B77
|
||||||
|
:20908D0004121A6B0C7B03121917056B037B0C6B047B18AA0190977B1790957B16977B151F
|
||||||
|
:2090AD009517171F150A020D022703CC902E1E1716155B128152409096905C961C00431FD7
|
||||||
|
:2090CD00051E05E603961C00471F0B1E0B1F0D1E0D1F3F1E3F88E60197844290FF72A9009E
|
||||||
|
:2090ED00021E05E6031E0B1F071E071F091E091F0F1E0F88E60397844290FF965C1F151E96
|
||||||
|
:20910D0015F66B171E05F697160B90E603429F1B171E15F71E15F66B1C1E05E60197160BBC
|
||||||
|
:20912D0090E602429F1B1C1E15F79096905C93FE1F1D1E05E6011E0B1F231E231F251E254C
|
||||||
|
:20914D001F271E2788E60397844272FB1D90FF93FE1F291E05E6021E0B1F2B1E2B1F2D1E86
|
||||||
|
:20916D002D1F311E3188E60297844272FB2990FF16051E05E6021E0B1F331E331F351E358B
|
||||||
|
:20918D001F371E3788E6019784429F90F71E055C1F391E05E60290971E0BE60390421E39E1
|
||||||
|
:2091AD00FF160B1E05E6031E0B1F3D1E3D1F2F1E2F88F69784429F90F71E0B5C1F3B1E05F8
|
||||||
|
:2091CD00E60390971E0BE60290421E3BFF1E0B1C00037F1E051C00037F965CE6036B14E66F
|
||||||
|
:2091ED00026B13E6016B12F616431718164572F9131721887B13191A6B218419186B1F16C5
|
||||||
|
:1E920D0021EF02161FFFE603E602FE16491E4772F9219F1920979E191F95515B408197
|
||||||
|
:00000001FF
|
||||||
34
uart_sample/Makefile
Normal file
34
uart_sample/Makefile
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
NAME=uart
|
||||||
|
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)
|
||||||
|
|
||||||
|
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
|
||||||
192
uart_sample/interrupts.c
Normal file
192
uart_sample/interrupts.c
Normal file
@ -0,0 +1,192 @@
|
|||||||
|
/*
|
||||||
|
* 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"
|
||||||
|
|
||||||
|
// 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){
|
||||||
|
/* 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;
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
#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
uart_sample/interrupts.h
Normal file
144
uart_sample/interrupts.h
Normal 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__
|
||||||
81
uart_sample/main.c
Normal file
81
uart_sample/main.c
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
/*
|
||||||
|
* 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"
|
||||||
|
|
||||||
|
volatile unsigned long Global_time = 0L; // global time in ms
|
||||||
|
U16 paused_val = 500; // interval between LED flashing
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
unsigned long T = 0L;
|
||||||
|
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();
|
||||||
|
|
||||||
|
// 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(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"
|
||||||
|
);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}while(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
40
uart_sample/ports_definition.h
Normal file
40
uart_sample/ports_definition.h
Normal 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__
|
||||||
169
uart_sample/uart.c
Normal file
169
uart_sample/uart.c
Normal 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
uart_sample/uart.h
Normal file
45
uart_sample/uart.h
Normal 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__
|
||||||
82
uart_sample/uart.ihx
Normal file
82
uart_sample/uart.ihx
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
:2080A00080808080808080808080808080808080805204AE5240F66B047B04A520274DAEDF
|
||||||
|
:2080C0005241F66B017B04A4804D27FDAE52417B01F7AE00011F02C6000A97C6000A4CC7C9
|
||||||
|
:2080E000000A4F9572FB027B01F7C60009C1000A260FC600094CC70009A1082604725F0052
|
||||||
|
:2081000009C6000AA1082604725F000A5B048080AE5342F644241B90CE000D72A90001C670
|
||||||
|
:18812000000CA90097C6000BA9009590CF000DCF000BAE53427F8080E4
|
||||||
|
:20813800AE5240F64824F9AE52417B03F781160390F64D2718AE5240F64824F9AE5245F654
|
||||||
|
:20815800AA08F790F6905CAE5241F720E3815202C6000AC1000926034F20271605AE0001B9
|
||||||
|
:208178001F01C6000997C600094CC700094F9572FB01F690F7C60009A1082604725F00092B
|
||||||
|
:20819800A6015B0281521C5F1F031F017B21A1042303CC82757B21A1032603CC82750D21AF
|
||||||
|
:2081B8002603CC8275961C00051F1B4F5F9772FB1B7F4CA10C25F51E1B1C000AA60AF77BEA
|
||||||
|
:2081D80021A101270E7B21A102271C7B21A104272120301E1FF66B1A5F0F171F027B1A6BD6
|
||||||
|
:2081F800047B176B01201C161F90FE5F17031F0120111E1FE6036B16E602FE6B031F017B06
|
||||||
|
:20821800166B04A6096B114B0A5F894B001E07891E0789CD86535B089F887B126B13840AE9
|
||||||
|
:20823800115F417B124172FB1BAB30F74B0A5F894B001E07891E0789CD87845B081F0317F5
|
||||||
|
:20825800011E0326041E0127067B11A1FF2CB87B114C5F9772FB1B89CD81465B025B1C8196
|
||||||
|
:2082780052110F01965C5C1F101E101C000B7F1E16A300007B15A2007B14A2002E1416167A
|
||||||
|
:2082980090504F1215974F12149517161F14A6016B01A60B6B0E4B0A5F894B001E1A891ECB
|
||||||
|
:2082B8001A89CD85AE5B089F0A0E5F417B0E4172FB10AB30F74B0A5F894B001E1A891E1AAF
|
||||||
|
:2082D80089CD86D05B081F1617147B0EA1002C034F2002A6011E1626041E1427034D26B6C3
|
||||||
|
:2082F800887B0F6B10844D27140D0127100A0E7B0E6B0F5F7B0F9772FB10A62DF75F7B0FBD
|
||||||
|
:208318009772FB1089CD81465B025B1181521ACE000D1F0DCE000B1F0B5F1F091F070F0494
|
||||||
|
:208338000F020F01961C000389CD81665B024D2603CC83C97B03A12D260E1E09260A1E072B
|
||||||
|
:208358002606A6016B0420697B03A1302403CC83EB7B03A1392303CC83EBA6016B021E0997
|
||||||
|
:20837800891E09894B0A5F894B00CD88235B081F11170F7B030F195F90977B19909572F9A3
|
||||||
|
:20839800119F1910979E190F9572A200309FA2006B149EA20017096B077B146B08AE7FFFF6
|
||||||
|
:2083B80013094F12084F120724075F1F091F070F0190CE000D72F20DC6000C120C95C600A9
|
||||||
|
:2083D8000B120B9790A327109EA2009FA2002403CC833C0D0126040D0226034F201A7B09AC
|
||||||
|
:2083F800887B0B6B07846B050D0427051E05501F051E1D1605FFA6015B1A81AE849089CD13
|
||||||
|
:2084180081465B021E0389CD81465B024B0ACD813884817B03A40F6B037B03887B04A10AD6
|
||||||
|
:20843800842406AB306B032004AB576B037B0381AE849989CD81465B027B034EA40F88CD81
|
||||||
|
:20845800842B5B0188CD8138847B0388CD842B5B0188CD81388481AE5011F6AA20F7AE50B2
|
||||||
|
:2084780012F6AA20F7AE5242A611F7AE5243A606F7AE5245A62CF7810A4552524F523A20C8
|
||||||
|
:048498000030780038
|
||||||
|
:02898C000000E9
|
||||||
|
:208000008200808382000000820080A0820080A1820080A2820080A3820080A4820080A57E
|
||||||
|
:20802000820080A6820080A78200000082000000820080A8820080A9820080AA820080AB3D
|
||||||
|
:20804000820080AC820080AD820080AE8200000082000000820080AF820080B0820080B1F9
|
||||||
|
:208060008200810F8200811082008137820000008200000082000000820000008200000017
|
||||||
|
:1D808300AE00082707724F00005A26F9AE00082709D6898BD700085A26F7CC8080D0
|
||||||
|
:03808000CC849C11
|
||||||
|
:20849C0052095F1F041F0272107F60AE50C67FAE5345A607F7AE5346A67DF7AE5341A601EA
|
||||||
|
:2084BC00F7AE5340A685F7AE500CF6AA04F7AE500DF6AA04F7CD846F9ACE000D72F0041F41
|
||||||
|
:2084DC0008C6000C12036B07C6000B1202CE000F905F881309909F1208909E12015B0125BF
|
||||||
|
:2084FC0011CE000D1304C6000C1203C6000B12022411CE000D1F04CE000B1F02AE500AF666
|
||||||
|
:20851C00A804F7965C89CD81665B024D27AB7B01A12B2718A12D2730A1482704A168269969
|
||||||
|
:20853C00AE858389CD81465B02CC84D5CE000F1C0064CF000FA327102203CC84D535F40042
|
||||||
|
:20855C00103501000FCC84D5CE000F1D0064CF000FA301F42503CC84D535F40010350100F5
|
||||||
|
:20857C000FCC84D55B09810A50524F544F3A0A2B2F2D094C454420706572696F640A532F50
|
||||||
|
:12859C0073097365742F676574204D73706565640A000E
|
||||||
|
:06898E000000000001F4EE
|
||||||
|
:2085AE00521D1E22A300007B21A2007B20A2002F040F0D2004A6016B0D0D0D27151E225068
|
||||||
|
:2085CE004F12216B1B4F12201F186B167B1B6B1720081622171816201716161817141616BD
|
||||||
|
:2085EE0017121E26A300007B25A2007B24A2002E1E7B27406B114F12266B104F12256B0F2E
|
||||||
|
:20860E004F12246B0E1610170B160E170920081626170B16241709160B170716091E0789D6
|
||||||
|
:20862E0090891E18891E1889CD86535B0817010D0D270D504F120290974F120190952002A3
|
||||||
|
:20864E0016015B1D8152040F020F017B0B484F494D262E160D1E0B905859170D1F0B1E097C
|
||||||
|
:20866E00130D7B08120C7B07120B240D160D1E0B549056170D1F0B20080C017B016B022049
|
||||||
|
:20868E00CA7B026B041E09130D7B08120C7B07120B2513160972F20D7B08120C977B0712FB
|
||||||
|
:2086AE000B9517091F07160D1E0B549056170D1F0B7B046B030A040D0326CA1E0916075B58
|
||||||
|
:2086CE000481521E1E23A300007B22A2007B21A2002F040F112004A6016B110D1127151E24
|
||||||
|
:2086EE0023504F12226B1C4F12211F146B127B1C6B13200816231714162117121614171962
|
||||||
|
:20870E00161217171E27A300007B26A2007B25A2002F040F162004A6016B160D16271E7B01
|
||||||
|
:20872E0028406B104F12276B0F4F12266B0E4F12256B0D160F170B160D1709200816271742
|
||||||
|
:20874E000B16251709160B170716091E078990891E1D891E1D89CD87845B0817017B111801
|
||||||
|
:20876E00164D270D504F120290974F12019095200216015B1E8152125F1F051F03A6206B86
|
||||||
|
:20878E00027B15484F496B0116171E1590585917171F157B036B0F1E04887B076B138408BC
|
||||||
|
:2087AE001259090F1F047B126B067B0F6B030D01271A7B06AA016B0A7B056B097B046B0839
|
||||||
|
:2087CE007B036B0716091705160717031E05131B7B04121A7B031219252B160572F21B7B1F
|
||||||
|
:2087EE0004121A6B0C7B03121917056B037B0C6B047B18AA0190977B1790957B16977B15C7
|
||||||
|
:20880E009517171F150A020D022703CC878F1E1716155B128152409096905C961C00431F26
|
||||||
|
:20882E00051E05E603961C00471F0B1E0B1F0D1E0D1F3F1E3F88E60197844290FF72A90045
|
||||||
|
:20884E00021E05E6031E0B1F071E071F091E091F0F1E0F88E60397844290FF965C1F151E3D
|
||||||
|
:20886E0015F66B171E05F697160B90E603429F1B171E15F71E15F66B1C1E05E60197160B64
|
||||||
|
:20888E0090E602429F1B1C1E15F79096905C93FE1F1D1E05E6011E0B1F231E231F251E25F4
|
||||||
|
:2088AE001F271E2788E60397844272FB1D90FF93FE1F291E05E6021E0B1F2B1E2B1F2D1E2E
|
||||||
|
:2088CE002D1F311E3188E60297844272FB2990FF16051E05E6021E0B1F331E331F351E3533
|
||||||
|
:2088EE001F371E3788E6019784429F90F71E055C1F391E05E60290971E0BE60390421E3989
|
||||||
|
:20890E00FF160B1E05E6031E0B1F3D1E3D1F2F1E2F88F69784429F90F71E0B5C1F3B1E059F
|
||||||
|
:20892E00E60390971E0BE60290421E3BFF1E0B1C00037F1E051C00037F965CE6036B14E616
|
||||||
|
:20894E00026B13E6016B12F616431718164572F9131721887B13191A6B218419186B1F166C
|
||||||
|
:1E896E0021EF02161FFFE603E602FE16491E4772F9219F1920979E191F95515B40813F
|
||||||
|
:00000001FF
|
||||||
@ -195,8 +195,12 @@ int main() {
|
|||||||
set_display_buf("eab");
|
set_display_buf("eab");
|
||||||
}else{
|
}else{
|
||||||
temp_ready = 0;
|
temp_ready = 0;
|
||||||
display_int((int)temp_readed,0);
|
if(temp_readed > -100 && temp_readed < 1000){
|
||||||
display_DP_at_pos(1);
|
display_int((int)temp_readed, 0);
|
||||||
|
display_DP_at_pos(1);
|
||||||
|
}else{ // display only integer part
|
||||||
|
display_int((int)temp_readed/10, 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if(matchROM) ++starting_val;
|
if(matchROM) ++starting_val;
|
||||||
}else{ // show number
|
}else{ // show number
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user