I2C (not working yet)

This commit is contained in:
2015-05-19 12:42:29 +03:00
parent 077c50a247
commit 132ebb5787
22 changed files with 14217 additions and 11988 deletions

34
uart_sample/Makefile Normal file
View 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
View 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
View File

@@ -0,0 +1,144 @@
/*
* interrupts.h
*
* Copyright 2014 Edward V. Emelianoff <eddy@sao.ru>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#pragma once
#ifndef __INTERRUPTS_H__
#define __INTERRUPTS_H__
#include "stm8l.h"
// Top Level Interrupt
INTERRUPT_DEFINITION(TLI_IRQHandler, 0);
// Auto Wake Up Interrupt
INTERRUPT_DEFINITION(AWU_IRQHandler, 1);
// Clock Controller Interrupt
INTERRUPT_DEFINITION(CLK_IRQHandler, 2);
// External Interrupt PORTA
INTERRUPT_DEFINITION(EXTI_PORTA_IRQHandler, 3);
// External Interrupt PORTB
INTERRUPT_DEFINITION(EXTI_PORTB_IRQHandler, 4);
// External Interrupt PORTC
INTERRUPT_DEFINITION(EXTI_PORTC_IRQHandler, 5);
// External Interrupt PORTD
INTERRUPT_DEFINITION(EXTI_PORTD_IRQHandler, 6);
// External Interrupt PORTE
INTERRUPT_DEFINITION(EXTI_PORTE_IRQHandler, 7);
#ifdef STM8S903
// External Interrupt PORTF
INTERRUPT_DEFINITION(EXTI_PORTF_IRQHandler, 8);
#endif // STM8S903
#if defined (STM8S208) || defined (STM8AF52Ax)
// CAN RX Interrupt routine.
INTERRUPT_DEFINITION(CAN_RX_IRQHandler, 8);
// CAN TX Interrupt routine.
INTERRUPT_DEFINITION(CAN_TX_IRQHandler, 9);
#endif // STM8S208 || STM8AF52Ax
// SPI Interrupt routine.
INTERRUPT_DEFINITION(SPI_IRQHandler, 10);
// Timer1 Update/Overflow/Trigger/Break Interrupt
INTERRUPT_DEFINITION(TIM1_UPD_OVF_TRG_BRK_IRQHandler, 11);
// Timer1 Capture/Compare Interrupt routine.
INTERRUPT_DEFINITION(TIM1_CAP_COM_IRQHandler, 12);
#ifdef STM8S903
// Timer5 Update/Overflow/Break/Trigger Interrupt
INTERRUPT_DEFINITION(TIM5_UPD_OVF_BRK_TRG_IRQHandler, 13);
// Timer5 Capture/Compare Interrupt
INTERRUPT_DEFINITION(TIM5_CAP_COM_IRQHandler, 14);
#else // STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF62Ax or STM8AF52Ax or STM8AF626x
// Timer2 Update/Overflow/Break Interrupt
INTERRUPT_DEFINITION(TIM2_UPD_OVF_BRK_IRQHandler, 13);
// Timer2 Capture/Compare Interrupt
INTERRUPT_DEFINITION(TIM2_CAP_COM_IRQHandler, 14);
#endif // STM8S903
#if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S105) || \
defined(STM8S005) || defined (STM8AF62Ax) || defined (STM8AF52Ax) || defined (STM8AF626x)
// Timer3 Update/Overflow/Break Interrupt
INTERRUPT_DEFINITION(TIM3_UPD_OVF_BRK_IRQHandler, 15);
// Timer3 Capture/Compare Interrupt
INTERRUPT_DEFINITION(TIM3_CAP_COM_IRQHandler, 16);
#endif // STM8S208, STM8S207 or STM8S105 or STM8AF62Ax or STM8AF52Ax or STM8AF626x
#if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S103) || \
defined(STM8S003) || defined (STM8AF62Ax) || defined (STM8AF52Ax) || defined (STM8S903)
// UART1 TX Interrupt
INTERRUPT_DEFINITION(UART1_TX_IRQHandler, 17);
// UART1 RX Interrupt
INTERRUPT_DEFINITION(UART1_RX_IRQHandler, 18);
#endif // STM8S208 or STM8S207 or STM8S103 or STM8S903 or STM8AF62Ax or STM8AF52Ax
// I2C Interrupt
INTERRUPT_DEFINITION(I2C_IRQHandler, 19);
#if defined(STM8S105) || defined(STM8S005) || defined (STM8AF626x)
// UART2 TX interrupt
INTERRUPT_DEFINITION(UART2_TX_IRQHandler, 20);
// UART2 RX interrupt
INTERRUPT_DEFINITION(UART2_RX_IRQHandler, 21);
#endif // STM8S105 or STM8AF626x
#if defined(STM8S207) || defined(STM8S007) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax)
// UART3 TX interrupt
INTERRUPT_DEFINITION(UART3_TX_IRQHandler, 20);
// UART3 RX interrupt
INTERRUPT_DEFINITION(UART3_RX_IRQHandler, 21);
#endif // STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax
#if defined(STM8S207) || defined(STM8S007) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax)
// ADC2 interrupt
INTERRUPT_DEFINITION(ADC2_IRQHandler, 22);
#else // STM8S105, STM8S103 or STM8S903 or STM8AF626x
// ADC1 interrupt
INTERRUPT_DEFINITION(ADC1_IRQHandler, 22);
#endif // STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax
#ifdef STM8S903
// Timer6 Update/Overflow/Trigger Interrupt
INTERRUPT_DEFINITION(TIM6_UPD_OVF_TRG_IRQHandler, 23);
#else // STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF52Ax or STM8AF62Ax or STM8AF626x
// Timer4 Update/Overflow Interrupt
INTERRUPT_DEFINITION(TIM4_UPD_OVF_IRQHandler, 23);
#endif // STM8S903
// Eeprom EEC Interrupt
INTERRUPT_DEFINITION(EEPROM_EEC_IRQHandler, 24);
#endif // __INTERRUPTS_H__

81
uart_sample/main.c Normal file
View 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);
}

View File

@@ -0,0 +1,40 @@
/*
* ports_definition.h - definition of ports pins & so on
*
* Copyright 2014 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#pragma once
#ifndef __PORTS_DEFINITION_H__
#define __PORTS_DEFINITION_H__
#include "stm8l.h"
// macro for using in port constructions like PORT(LED_PORT, ODR) = xx
#define CONCAT(a, b) a ## _ ## b
#define PORT(a, b) CONCAT(a , b)
// on-board LED
#define LED_PORT PC
#define LED_PIN GPIO_PIN2
// UART2_TX
#define UART_PORT PD
#define UART_TX_PIN GPIO_PIN5
#endif // __PORTS_DEFINITION_H__

169
uart_sample/uart.c Normal file
View File

@@ -0,0 +1,169 @@
/*
* blinky.c
*
* Copyright 2014 Edward V. Emelianoff <eddy@sao.ru>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "ports_definition.h"
#include "uart.h"
#include "interrupts.h"
U8 UART_rx[UART_BUF_LEN]; // cycle buffer for received data
U8 UART_rx_start_i = 0; // started index of received data (from which reading starts)
U8 UART_rx_cur_i = 0; // index of current first byte in rx array (to which data will be written)
/**
* Send one byte through UART
* @param byte - data to send
*/
void UART_send_byte(U8 byte){
while(!(UART2_SR & UART_SR_TXE)); // wait until previous byte transmitted
UART2_DR = byte;
}
void uart_write(char *str){
while(*str){
while(!(UART2_SR & UART_SR_TXE));
UART2_CR2 |= UART_CR2_TEN;
UART2_DR = *str++;
}
}
/**
* Read one byte from Rx buffer
* @param byte - where to store readed data
* @return 1 in case of non-empty buffer
*/
U8 UART_read_byte(U8 *byte){
if(UART_rx_start_i == UART_rx_cur_i) // buffer is empty
return 0;
*byte = UART_rx[UART_rx_start_i++];
check_UART_pointer(UART_rx_start_i);
return 1;
}
void printUint(U8 *val, U8 len){
unsigned long Number = 0;
U8 i = len;
char ch;
U8 decimal_buff[12]; // max len of U32 == 10 + \n + \0
if(len > 4 || len == 3 || len == 0) return;
for(i = 0; i < 12; i++)
decimal_buff[i] = 0;
decimal_buff[10] = '\n';
ch = 9;
switch(len){
case 1:
Number = *((U8*)val);
break;
case 2:
Number = *((U16*)val);
break;
case 4:
Number = *((unsigned long*)val);
break;
}
do{
i = Number % 10L;
decimal_buff[ch--] = i + '0';
Number /= 10L;
}while(Number && ch > -1);
uart_write((char*)&decimal_buff[ch+1]);
}
/**
* print signed long onto terminal
* max len = 10 symbols + 1 for "-" + 1 for '\n' + 1 for 0 = 13
*/
void print_long(long Number){
U8 i, L = 0;
char ch;
char decimal_buff[12];
decimal_buff[11] = 0;
ch = 11;
if(Number < 0){
Number = -Number;
L = 1;
}
do{
i = Number % 10L;
decimal_buff[--ch] = i + '0';
Number /= 10L;
}while(Number && ch > 0);
if(ch > 0 && L) decimal_buff[--ch] = '-';
uart_write(&decimal_buff[ch]);
}
U8 readInt(int *val){
unsigned long T = Global_time;
unsigned long R = 0;
int readed;
U8 sign = 0, rb, ret = 0, bad = 0;
do{
if(!UART_read_byte(&rb)) continue;
if(rb == '-' && R == 0){ // negative number
sign = 1;
continue;
}
if(rb < '0' || rb > '9') break; // number ends with any non-digit symbol that will be omitted
ret = 1; // there's at least one digit
R = R * 10L + rb - '0';
if(R > 0x7fff){ // bad value
R = 0;
bad = 0;
}
}while(Global_time - T < 10000); // wait no longer than 10s
if(bad || !ret) return 0;
readed = (int) R;
if(sign) readed *= -1;
*val = readed;
return 1;
}
void error_msg(char *msg){
uart_write("\nERROR: ");
uart_write(msg);
UART_send_byte('\n');
}
U8 U8toHEX(U8 val){
val &= 0x0f;
if(val < 10) val += '0';
else val += 'a' - 10;
return val;
}
void printUHEX(U8 val){
uart_write("0x");
UART_send_byte(U8toHEX(val>>4)); // MSB
UART_send_byte(U8toHEX(val)); // LSB
}
void uart_init(){
// PD5 - UART2_TX
PORT(UART_PORT, DDR) |= UART_TX_PIN;
PORT(UART_PORT, CR1) |= UART_TX_PIN;
// Configure UART
// 8 bit, no parity, 1 stop (UART_CR1/3 = 0 - reset value)
// 57600 on 16MHz: BRR1=0x11, BRR2=0x06
UART2_BRR1 = 0x11; UART2_BRR2 = 0x06;
UART2_CR2 = UART_CR2_TEN | UART_CR2_REN | UART_CR2_RIEN; // Allow RX/TX, generate ints on rx
}

45
uart_sample/uart.h Normal file
View File

@@ -0,0 +1,45 @@
/*
* blinky.h
*
* Copyright 2014 Edward V. Emelianoff <eddy@sao.ru>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#pragma once
#ifndef __MAIN_H__
#define __MAIN_H__
extern volatile unsigned long Global_time; // global time in ms
#define UART_BUF_LEN 8 // max 7 bytes transmited in on operation
extern U8 UART_rx[];
extern U8 UART_rx_start_i;
extern U8 UART_rx_cur_i;
void UART_send_byte(U8 byte);
void uart_write(char *str);
void printUint(U8 *val, U8 len);
void print_long(long Number);
void error_msg(char *msg);
void uart_init();
U8 UART_read_byte(U8 *byte);
void printUHEX(U8 val);
#define check_UART_pointer(x) do{if(x == UART_BUF_LEN) x = 0;}while(0)
#endif // __MAIN_H__

82
uart_sample/uart.ihx Normal file
View 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