- added
git-svn-id: http://moon:8086/svn/projects/HendiControl@21 fda53097-d464-4ada-af97-ba876c37ca34
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* adc.c
|
||||
*
|
||||
* Created: 20.02.2019 20:46:32
|
||||
* Author: jens
|
||||
*/
|
||||
|
||||
#include <avr/io.h>
|
||||
#include "adc.h"
|
||||
|
||||
void adc_init()
|
||||
{
|
||||
PRR &= ~(1 << PRADC);
|
||||
ADCSRA |= (1 << ADEN) | 7;
|
||||
}
|
||||
|
||||
void adc_start_conversion(int ch)
|
||||
{
|
||||
ADMUX = (ADMUX & 0xF0) | (ch & 0x0F) | (1 << REFS0);
|
||||
ADCSRA |= (1 << ADSC) | (1 << ADIE);
|
||||
}
|
||||
|
||||
int adc_is_fininshed()
|
||||
{
|
||||
return (int)(0 == (ADCSRA & (1 << ADSC)));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* adc.h
|
||||
*
|
||||
* Created: 20.02.2019 20:46:44
|
||||
* Author: jens
|
||||
*/
|
||||
|
||||
|
||||
#ifndef ADC_H_
|
||||
#define ADC_H_
|
||||
|
||||
void adc_init();
|
||||
void adc_start_conversion(int ch);
|
||||
int adc_is_fininshed();
|
||||
|
||||
#endif /* ADC_H_ */
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* fifo.c
|
||||
*
|
||||
* Created: 20.02.2019 20:33:06
|
||||
* Author: jens
|
||||
*/
|
||||
|
||||
#include "fifo.h"
|
||||
|
||||
void fifo_init(Fifo *pObj, size_t capacity, size_t itemSize, const char *pName)
|
||||
{
|
||||
pObj->wi = 0;
|
||||
pObj->ri = 0;
|
||||
pObj->fill = 0;
|
||||
pObj->capacity = capacity;
|
||||
pObj->itemSize = itemSize;
|
||||
pObj->ppData = (void**)malloc(capacity);
|
||||
|
||||
for (int i=0; i < capacity; i++)
|
||||
{
|
||||
pObj->ppData[i] = (void*)malloc(itemSize);
|
||||
}
|
||||
pObj->pName = pName;
|
||||
}
|
||||
|
||||
void fifo_free(Fifo *pObj)
|
||||
{
|
||||
for (int i=0; i < pObj->capacity; i++)
|
||||
{
|
||||
free(pObj->ppData[i]);
|
||||
}
|
||||
free (pObj->ppData);
|
||||
pObj->itemSize = 0;
|
||||
pObj->capacity = 0;
|
||||
}
|
||||
|
||||
int fifo_push(Fifo *pObj, void *pItem)
|
||||
{
|
||||
if (pObj->fill >= pObj->capacity)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
memcpy(pObj->ppData[pObj->wi], pItem, pObj->itemSize);
|
||||
pObj->wi++;
|
||||
pObj->fill++;
|
||||
if (pObj->wi == pObj->capacity)
|
||||
{
|
||||
pObj->wi = 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int fifo_pop(Fifo *pObj, void *pItem)
|
||||
{
|
||||
if (!pObj->fill)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
memcpy(pItem, pObj->ppData[pObj->ri], pObj->itemSize);
|
||||
pObj->ri++;
|
||||
pObj->fill--;
|
||||
if (pObj->ri == pObj->capacity)
|
||||
{
|
||||
pObj->ri = 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int fifo_isEmpty(Fifo *pObj)
|
||||
{
|
||||
return pObj->fill == 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* fifo.h
|
||||
*
|
||||
* Created: 20.02.2019 20:35:48
|
||||
* Author: jens
|
||||
*/
|
||||
|
||||
|
||||
#ifndef FIFO_H_
|
||||
#define FIFO_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct _sFifo
|
||||
{
|
||||
void **ppData;
|
||||
size_t capacity;
|
||||
size_t itemSize;
|
||||
size_t fill;
|
||||
size_t wi;
|
||||
size_t ri;
|
||||
const char *pName;
|
||||
} Fifo;
|
||||
|
||||
void fifo_init(Fifo *pObj, size_t capacity, size_t itemSize, const char *pName);
|
||||
void fifo_free(Fifo *pObj);
|
||||
int fifo_push(Fifo *pObj, void *pItem);
|
||||
int fifo_pop(Fifo *pObj, void *pItem);
|
||||
int fifo_isEmpty(Fifo *pObj);
|
||||
|
||||
#endif /* FIFO_H_ */
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* i2c.c
|
||||
*
|
||||
* Created: 20.02.2019 20:33:30
|
||||
* Author: jens
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <avr/io.h>
|
||||
#include "i2c.h"
|
||||
|
||||
void i2c_init()
|
||||
{
|
||||
TWBR = 0xFF;
|
||||
TWSR = 0x00;
|
||||
}
|
||||
|
||||
void i2c_send(uint8_t addr, uint8_t rw, uint8_t *data, size_t size)
|
||||
{
|
||||
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
|
||||
// Send START condition
|
||||
do
|
||||
{
|
||||
while (!(TWCR &(1<<TWINT))); // Wait for TWINT Flag set. This indicates
|
||||
// that the START condition has been
|
||||
// transmitted.
|
||||
if((TWSR & 0xF8) != I2C_START)
|
||||
{
|
||||
printf("Error I2C_START\n");
|
||||
break;
|
||||
}
|
||||
TWDR = addr<<1 | rw; // Load SLA_W into TWDR Register. Clear
|
||||
// TWINT bit in TWCR to start transmission of
|
||||
// address.
|
||||
TWCR = (1<<TWINT) | (1<<TWEN);
|
||||
while (!(TWCR &(1<<TWINT))); // Wait for TWINT Flag set. This indicates
|
||||
// that the SLA+W has been transmitted, and
|
||||
// ACK/NACK has been received.
|
||||
|
||||
|
||||
if ((TWSR & 0xF8) != I2C_SLA_ACK)
|
||||
{
|
||||
printf("Error I2C_SLA_ACK\n");
|
||||
break;
|
||||
}
|
||||
// Check value of TWI Status Register. Mask
|
||||
// prescaler bits. If status different from
|
||||
// MT_SLA_ACK go to ERROR
|
||||
for (size_t i=0; i < size; i++)
|
||||
{
|
||||
|
||||
TWDR = *(data++);
|
||||
TWCR = (1<<TWINT) | (1<<TWEN);
|
||||
// Load DATA into TWDR Register. Clear
|
||||
// TWINT bit in TWCR to start transmission of
|
||||
// data.
|
||||
|
||||
while (!(TWCR & (1<<TWINT)));
|
||||
// Wait for TWINT Flag set. This indicates
|
||||
// that the DATA has been transmitted, and
|
||||
// ACK/NACK has been received}
|
||||
|
||||
if ((TWSR & 0xF8) != I2C_DATA_ACK)
|
||||
{
|
||||
printf("Error I2C_DATA_ACK\n");
|
||||
break;
|
||||
}
|
||||
// Check value of TWI Status Register. Mask
|
||||
// prescaler bits. If status different from
|
||||
// MT_DATA_ACK go to ERROR.
|
||||
}
|
||||
|
||||
} while (0);
|
||||
TWCR = (1<<TWINT)| (1<<TWEN)|(1<<TWSTO); // Transmit STOP condition.
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* i2c.h
|
||||
*
|
||||
* Created: 20.02.2019 20:40:45
|
||||
* Author: jens
|
||||
*/
|
||||
|
||||
|
||||
#ifndef I2C_H_
|
||||
#define I2C_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
enum I2C
|
||||
{
|
||||
I2C_START = 0x08,
|
||||
I2C_REPEATED_START = 0x10,
|
||||
I2C_SLA_ACK = 0x18,
|
||||
I2C_SLA_NACK = 0x20,
|
||||
I2C_DATA_ACK = 0x28,
|
||||
I2C_DATA_NACK = 0x30,
|
||||
I2C_LOST = 0x38,
|
||||
I2C_READ = 0x01,
|
||||
I2C_WRITE = 0x00
|
||||
};
|
||||
|
||||
void i2c_init();
|
||||
void i2c_send(uint8_t addr, uint8_t rw, uint8_t *data, size_t size);
|
||||
|
||||
#endif /* I2C_H_ */
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* mcp42x6.c
|
||||
*
|
||||
* Created: 20.02.2019 20:44:16
|
||||
* Author: jens
|
||||
*/
|
||||
|
||||
#include "mcp42x6.h"
|
||||
|
||||
size_t MCP47x6_write_volatile_dac(uint16_t dac_value, uint8_t *buffer, size_t size)
|
||||
{
|
||||
*(buffer++) = ~mask & (uint8_t)(dac_value >> 8);
|
||||
*(buffer++) = (uint8_t)(dac_value >> 0);
|
||||
|
||||
return 2;
|
||||
}
|
||||
|
||||
size_t MCP47x6_write_volatile_mem(uint16_t dac_value, uint8_t *buffer, size_t size)
|
||||
{
|
||||
*(buffer++) = mask & 0x40;
|
||||
dac_value <<= 4;
|
||||
*(buffer++) = (uint8_t)(dac_value >> 8);
|
||||
*(buffer++) = (uint8_t)(dac_value >> 0);
|
||||
|
||||
return 3;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* mcp42x6.h
|
||||
*
|
||||
* Created: 20.02.2019 20:44:33
|
||||
* Author: jens
|
||||
*/
|
||||
|
||||
|
||||
#ifndef MCP42X6_H_
|
||||
#define MCP42X6_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
enum MCP47x6_flags
|
||||
{
|
||||
C0 = 0x20,
|
||||
C1 = 0x40,
|
||||
C2 = 0x80,
|
||||
PDN0 = 0x10,
|
||||
PDN1 = 0x20,
|
||||
mask = 0xF0
|
||||
};
|
||||
|
||||
size_t MCP47x6_write_volatile_dac(uint16_t dac_value, uint8_t *buffer, size_t size);
|
||||
size_t MCP47x6_write_volatile_mem(uint16_t dac_value, uint8_t *buffer, size_t size);
|
||||
|
||||
#endif /* MCP42X6_H_ */
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* port.c
|
||||
*
|
||||
* Created: 20.02.2019 20:54:33
|
||||
* Author: jens
|
||||
*/
|
||||
|
||||
#include <avr/io.h>
|
||||
#include "port.h"
|
||||
|
||||
void port_init()
|
||||
{
|
||||
PORTB = PORTB | 0x03;
|
||||
DDRB = DDB1 | DDB2;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* port.h
|
||||
*
|
||||
* Created: 20.02.2019 20:54:45
|
||||
* Author: jens
|
||||
*/
|
||||
|
||||
|
||||
#ifndef PORT_H_
|
||||
#define PORT_H_
|
||||
|
||||
void port_init();
|
||||
|
||||
#endif /* PORT_H_ */
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* timer.c
|
||||
*
|
||||
* Created: 20.02.2019 20:56:18
|
||||
* Author: jens
|
||||
*/
|
||||
|
||||
#include <avr/io.h>
|
||||
#include "timer.h"
|
||||
|
||||
void timer_init(uint16_t TIMER_RELOAD)
|
||||
{
|
||||
|
||||
TCNT1 = TIMER_RELOAD;
|
||||
|
||||
TCCR1A = 0x00;
|
||||
TCCR1B = (1<<CS10) | (1<<CS12); // Timer mode with 1024 prescaler
|
||||
TIMSK1 = (1 << TOIE1) ; // Enable timer1 overflow interrupt(TOIE1)
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* timer.h
|
||||
*
|
||||
* Created: 20.02.2019 20:56:07
|
||||
* Author: jens
|
||||
*/
|
||||
|
||||
|
||||
#ifndef TIMER_H_
|
||||
#define TIMER_H_
|
||||
|
||||
void timer_init(uint16_t TIMER_RELOAD);
|
||||
|
||||
#endif /* TIMER_H_ */
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* uart.c
|
||||
*
|
||||
* Created: 20.02.2019 20:50:13
|
||||
* Author: jens
|
||||
*/
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <stdio.h>
|
||||
#include "uart.h"
|
||||
|
||||
void uart_init(uint16_t BAUD_PRESCALE)
|
||||
{
|
||||
// Set baud rate
|
||||
UBRR0L = (uint8_t)BAUD_PRESCALE;
|
||||
UBRR0H = (uint8_t)(BAUD_PRESCALE >> 8);
|
||||
|
||||
// Enable receiver and transmitter
|
||||
UCSR0B = (1<<TXEN0)|(1<<RXEN0);
|
||||
|
||||
// Enable RX interrupt
|
||||
UCSR0B |= (1<<RXCIE0);
|
||||
|
||||
}
|
||||
|
||||
void uart_putc(char c)
|
||||
{
|
||||
while((UCSR0A & (1<<UDRE0)) == 0)
|
||||
{
|
||||
}
|
||||
UDR0 = c;
|
||||
}
|
||||
|
||||
int uart_putchar(char c, FILE *stream)
|
||||
{
|
||||
uart_putc(c);
|
||||
if (c == 0x0A)
|
||||
{
|
||||
uart_putc(0x0D);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void uart_puts(char const *str)
|
||||
{
|
||||
while(*str)
|
||||
{
|
||||
char c = *(str++);
|
||||
uart_putc(c);
|
||||
if (c == 0x0A)
|
||||
{
|
||||
uart_putc(0x0D);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* uart.h
|
||||
*
|
||||
* Created: 20.02.2019 20:50:01
|
||||
* Author: jens
|
||||
*/
|
||||
|
||||
|
||||
#ifndef UART_H_
|
||||
#define UART_H_
|
||||
|
||||
void uart_init(uint16_t BAUD_PRESCALE);
|
||||
void uart_putc(char c);
|
||||
int uart_putchar(char c, FILE *stream);
|
||||
void uart_puts(char const *str);
|
||||
|
||||
#endif /* UART_H_ */
|
||||
Reference in New Issue
Block a user