From 4ea1189f208eee652e9028c3fe4327293c70662b Mon Sep 17 00:00:00 2001 From: asterix Date: Wed, 5 Mar 2008 23:36:49 +0000 Subject: [PATCH] Merge from Triface project. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@1164 38d2e660-2303-0410-9eaa-f027e97ec537 --- app/triface/hw/hw_adc.h | 63 +++++++++++++++++++++ app/triface/hw/hw_boot.h | 70 +++++++++++++++++++++++ app/triface/hw/hw_buzzer.h | 55 +++++++++++++++++++ app/triface/hw/hw_input.h | 71 ++++++++++++++++++++++++ app/triface/hw/hw_sipo.h | 59 ++++++++++++++++++++ cpu/avr/drv/sipo.c | 80 +++++++++++++++++++++++++++ cpu/avr/drv/sipo.h | 51 +++++++++++++++++ net/keytag.c | 110 +++++++++++++++++++++++++++++++++++++ net/keytag.h | 81 +++++++++++++++++++++++++++ 9 files changed, 640 insertions(+) create mode 100644 app/triface/hw/hw_adc.h create mode 100644 app/triface/hw/hw_boot.h create mode 100644 app/triface/hw/hw_buzzer.h create mode 100644 app/triface/hw/hw_input.h create mode 100644 app/triface/hw/hw_sipo.h create mode 100644 cpu/avr/drv/sipo.c create mode 100644 cpu/avr/drv/sipo.h create mode 100644 net/keytag.c create mode 100644 net/keytag.h diff --git a/app/triface/hw/hw_adc.h b/app/triface/hw/hw_adc.h new file mode 100644 index 00000000..5beea2e2 --- /dev/null +++ b/app/triface/hw/hw_adc.h @@ -0,0 +1,63 @@ +/** + * \file + * ght 2006 Develer S.r.l. (http://www.develer.com/) + * + * \brief Macro for HW_AIN_H + * + * + * \version $Id$ + * + * \author Andrea Grandi + */ + +#ifndef HW_ADC_H +#define HW_ADC_H + +#include + +#define START_CONVERTION (ADCSRA |= BV(ADSC)) +#define ENABLE_ADC (ADCSRA |= BV(ADEN)) +#define SET_AI_ADLAR (ADMUX &= ~BV(ADLAR)) + +/** Microseconds to wait before starting conversion after changing a channel */ +#define STABILIZING_AI_CHANNEL_TIME 125 + +/** Number of AIN channels */ +#define ADC_CHANNEL_NUM 4 + +void adc_set_active_ain(int ai); +void adc_init(void); +void adc_set_vref_avcc(void); +int adc_read_ai_channel(int channel); + +#endif // HW_ADC_H + diff --git a/app/triface/hw/hw_boot.h b/app/triface/hw/hw_boot.h new file mode 100644 index 00000000..8deeaec3 --- /dev/null +++ b/app/triface/hw/hw_boot.h @@ -0,0 +1,70 @@ +/** + * \file + * + * + * \brief Macro for boot loader. + * + * + * \version $Id$ + * + * \author Daniele Basile + * \author Francesco Sacchi + */ + +#ifndef HW_BOOT_H +#define HW_BOOT_H + +#include + +/* Set up function pointer to RESET vector */ +void (*rom_start)(void) NORETURN = 0x0000; + +#define START_APP() rom_start() + +#define BOOT_INIT do \ +{ \ + /* Enable change of Interrupt Vectors */ \ + MCUCR = BV(IVCE); \ + /* Move interrupts to boot Flash section */ \ + MCUCR = BV(IVSEL); \ +} while(0) + +#define BOOT_END do \ +{ \ + /* Enable change of Interrupt Vectors */ \ + MCUCR = BV(IVCE); \ + /* Move interrupts to boot Flash section */ \ + MCUCR = 0; \ +} while(0) + +#endif // HW_BOOT_H + diff --git a/app/triface/hw/hw_buzzer.h b/app/triface/hw/hw_buzzer.h new file mode 100644 index 00000000..ae92ebd1 --- /dev/null +++ b/app/triface/hw/hw_buzzer.h @@ -0,0 +1,55 @@ +/** + * \file + * + * + * \brief Buzzer hardware-specific definitions + * + * \version $Id$ + * + * \author Francesco Sacchi + * Andrea Grandi + */ + +#ifndef HW_BUZZER_H +#define HW_BUZZER_H + +#include +#include + +#define BUZZER_BIT BV(PE3) +#define IS_BUZZER_ON (PORTE & BUZZER_BIT) +#define BUZZER_HW_INIT do { DDRE |= BV(DDE3); } while (0) +#define BUZZER_ON do { PORTE |= BUZZER_BIT; } while (0) +#define BUZZER_OFF do { PORTE &= ~BUZZER_BIT; } while (0) + +#endif /* HW_BUZZER_H */ + diff --git a/app/triface/hw/hw_input.h b/app/triface/hw/hw_input.h new file mode 100644 index 00000000..6ab0bba8 --- /dev/null +++ b/app/triface/hw/hw_input.h @@ -0,0 +1,71 @@ +/** + * \file + * > + * + * \brief Macro for HW_INPUT_H + * + * + * \version $Id$ + * + * \author Andrea Grandi + */ +#ifndef HW_INPUT_H +#define HW_INPUT_H + +/* Set pins as input and enable pull-up */ +#define INPUT_INIT_D do \ +{ \ + (DDRD &= ~(BV(PD4) | BV(PD5) | BV(PD6) | BV(PD7))); \ + (PORTD |= (BV(PD4) | BV(PD5) | BV(PD6) | BV(PD7))); \ +} while(0) + +#define INPUT_INIT_E do \ +{ \ + (DDRE &= ~(BV(PE4) | BV(PE5) | BV(PE6) | BV(PE7))); \ + ATOMIC((PORTE |= (BV(PE4) | BV(PE5) | BV(PE6) | BV(PE7)))); \ +} while(0) + +#define INPUT_INIT do { INPUT_INIT_D; INPUT_INIT_E;} while(0) + +INLINE uint8_t INPUT_GET(void) +{ + uint8_t out_d, out_e; + out_d = PIND; + out_e = PINE; + + out_d >>= 4; + out_e = out_e & 0xF0; + + return out_e | out_d; +} + +#endif // HW_INPUT_H diff --git a/app/triface/hw/hw_sipo.h b/app/triface/hw/hw_sipo.h new file mode 100644 index 00000000..d2f4b63b --- /dev/null +++ b/app/triface/hw/hw_sipo.h @@ -0,0 +1,59 @@ +/** + * \file + * + * + * \brief Macro for HW_SIPO_H + * + * + * \version $Id$ + * + * \author Andrea Grandi + */ +#ifndef HW_SIPO_H +#define HW_SIPO_H + +#define LOAD_HIGH (PORTB |= BV(PB3)) +#define LOAD_LOW (PORTB &= ~BV(PB3)) +#define LOAD_INIT (DDRB |= BV(PB3)) +#define SET_SCK_OUT (DDRB |= BV(PB1)) +#define SET_SOUT_OUT (DDRB |= BV(PB2)) +#define CLOCK_HIGH (PORTB |= BV(PB1)) +#define CLOCK_LOW (PORTB &= ~BV(PB1)) +#define SET_SOUT_HIGH (PORTB |= BV(PB2)) +#define SET_SOUT_LOW (PORTB &= ~BV(PB2)) +#define CLOCK_PULSE do { CLOCK_HIGH; CLOCK_LOW; } while(0) + +#define OE_OUT (DDRG |= BV(PG3)) +#define OE_LOW (PORTG &= BV(PG3)) + + +#endif // HW_SIPO_H diff --git a/cpu/avr/drv/sipo.c b/cpu/avr/drv/sipo.c new file mode 100644 index 00000000..014f9463 --- /dev/null +++ b/cpu/avr/drv/sipo.c @@ -0,0 +1,80 @@ +/** + * \file + * + * + * \version $Id$ + * + * \author Andrea Grandi + * + * \brief SIPO Module + * + * The SIPO module trasform a serial input in + * a parallel output. Please check hw_sipo.h + * file to customize hardware relative parameters. + * + */ + +#include +#include "sipo.h" + +Serial *sipo_port; + +/** Initialize the SIPO port */ +void sipo_init(void) +{ + CLOCK_LOW; + SET_SOUT_LOW; + LOAD_LOW; + SET_SCK_OUT; + SET_SOUT_OUT; + LOAD_INIT; + sipo_putchar(0x0); + OE_OUT; + OE_LOW; +} + +/** Write a char in the SIPO port and manage the LOAD pin */ +void sipo_putchar(uint8_t c) +{ + for(int i = 0; i <= 7; i++) + { + if((c & BV(i)) == 0) + SET_SOUT_LOW; + else + SET_SOUT_HIGH; + + CLOCK_PULSE; + } + + LOAD_HIGH; + LOAD_LOW; +} + diff --git a/cpu/avr/drv/sipo.h b/cpu/avr/drv/sipo.h new file mode 100644 index 00000000..15df226a --- /dev/null +++ b/cpu/avr/drv/sipo.h @@ -0,0 +1,51 @@ +/** + * \file + * + * + * \brief Macro for SIPO_H + * + * + * \version $Id$ + * + * \author Andrea Grandi + */ + +#ifndef SIPO_H +#define SIPO_H + +#include +#include + +void sipo_init(void); +void sipo_putchar(uint8_t c); + +#endif // SIPO_H + diff --git a/net/keytag.c b/net/keytag.c new file mode 100644 index 00000000..afc21b18 --- /dev/null +++ b/net/keytag.c @@ -0,0 +1,110 @@ +/** + * \file + * + * + * \author Andrea Grandi + * + * \brief Tag protocol (protocol). + * + * TAG protocol is decribed in this way: + *
+ * ---------------------------------------------------
+ * |STX (0x02)|data...(10 HEX chars)|CR|LF|ETX (0x03)|
+ * ---------------------------------------------------
+ * 
+ */ + +#include "keytag.h" + +#include +#include + +#include +#include + +static void keytag_clearPkt(struct TagPacket *pkt) +{ + pkt->sync = false; + pkt->len = 0; +} + +void keytag_init(struct TagPacket *pkt) +{ + keytag_clearPkt(pkt); +} + +void keytag_poll(struct TagPacket *pkt) +{ + int c; + + /* Get all chars from buffer */ + while ((c = ser_getchar_nowait(pkt->tag_ser)) != EOF) + { + /* Search for STX char in received chars */ + if (c == TAG_STX) + { + /* When STX is found a new packet begins */ + if (pkt->sync) + kprintf("TAG double sync!\n"); + keytag_clearPkt(pkt); + pkt->sync = true; + } + else if (pkt->sync) + { + /* Check for end of packet */ + if (c == TAG_ETX) + { + pkt->buf[TAG_MAX_PRINT_CHARS] = '\x0'; + /* Write read TAG on communication serial */ + ser_printf(pkt->comm_ser, "tag %s", pkt->buf); + pkt->sync = false; + } + else + { + /* Check for buffer overflow */ + if (pkt->len >= TAG_MAX_LEN) + { + kprintf("TAG buffer overflow\n"); + pkt->sync = false; + } + else + { + /* Add every char after STX to tag reading buffer */ + if (pkt->sync) + { + pkt->buf[pkt->len] = c; + pkt->len++; + } + } + } + } + } +} diff --git a/net/keytag.h b/net/keytag.h new file mode 100644 index 00000000..651f134c --- /dev/null +++ b/net/keytag.h @@ -0,0 +1,81 @@ +/** + * \file + * + * + * \author Andrea Grandi + * + * \brief Tag protocol. (interface). + */ + +#ifndef NET_KEYTAG_H +#define NET_KEYTAG_H + +/** + * Starting communication char (STX). + */ +#define TAG_STX 0x02 + +/** + * Ending communication char (ETX). + */ +#define TAG_ETX 0x03 + +/** + * Max buffer lenght + */ +#define TAG_MAX_LEN 14 + +#define TAG_SER_PORT 0 +#define TAG_SER_BAUDRATE 9600 + +/** + * Max number of chars to print in the communication serial + */ +#define TAG_MAX_PRINT_CHARS 12 + +#include + +/** + * Structure of a Tag packet + */ +typedef struct TagPacket +{ + struct Serial *tag_ser; // Tag serial + struct Serial *comm_ser; // Communication serial + bool sync; // Status flag: true if we find an STX + uint16_t len; // Packet lenght + uint8_t buf[TAG_MAX_LEN]; // Reception buffer +} TagPacket; + +void keytag_init(struct TagPacket *pkt); +void keytag_poll(struct TagPacket *pkt); + +#endif /* NET_TAG_H */ -- 2.25.1