add code folder - dont really know

This commit is contained in:
2022-09-16 18:34:56 +02:00
parent c85b45caea
commit 41811785a3
10 changed files with 646 additions and 0 deletions

40
code/src/fifo.c Normal file
View File

@@ -0,0 +1,40 @@
#include "fifo.h"
#include "uart.h"
uint8_t fifo_push(Fifo_t * fifo, uint8_t byte)
{
//if (fifo.write >= FIFO_SIZE)
// fifo.write = 0; // erhöht sicherheit
if ( ( fifo->write + 1 == fifo->read ) ||
( fifo->read == 0 && fifo->write + 1 == FIFO_SIZE ) )
return FIFO_FAIL; // voll
fifo->data[fifo->write] = byte;
fifo->write++;
if (fifo->write >= FIFO_SIZE)
fifo->write = 0;
return FIFO_SUCCESS;
}
uint8_t fifo_pop(Fifo_t * fifo, uint8_t *pByte)
{
if (fifo->read == fifo->write){
return FIFO_FAIL;
}
*pByte = fifo->data[fifo->read];
fifo->read++;
if (fifo->read >= FIFO_SIZE)
fifo->read = 0;
return FIFO_SUCCESS;
}
uint8_t fifo_peek(Fifo_t * fifo){
return fifo->data[fifo->read];
}

102
code/src/main.c Normal file
View File

@@ -0,0 +1,102 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include <stdlib.h>
#include <stddef.h>
#include "uart.h"
#include "fifo.h"
#include "util.h"
void init_clk(void)
{
// ========= System Clock configuration =========
// Set to external 16Mhz crystal, using the PLL at *2
// set it to be a 12-16Mhz crystal with a slow start-up time.
OSC.XOSCCTRL = OSC_FRQRANGE_2TO9_gc | OSC_XOSCSEL_XTAL_16KCLK_gc ;
OSC.CTRL |= OSC_XOSCEN_bm ; // enable it
while( (OSC.STATUS & OSC_XOSCRDY_bm) == 0 ){} // wait until it's stable
// The external crystal is now running and stable.
// (Note that it's not yet selected as the clock source)
// Now configure the PLL to be eXternal oscillator * 2
OSC.PLLCTRL = OSC_PLLSRC_XOSC_gc | 2 ;
// now enable the PLL...
OSC.CTRL |= OSC_PLLEN_bm ; // enable the PLL...
while( (OSC.STATUS & OSC_PLLRDY_bm) == 0 ){} // wait until it's stable
// And now, *finally*, we can switch from the internal 2Mhz clock to the PLL
CCP = CCP_IOREG_gc; // protected write follows
CLK.CTRL = CLK_SCLKSEL_PLL_gc; // The System clock is now PLL (16Mhz * 2)
// ==============================================
}
void adc_init(void){
ADCA.CTRLB = 0x00;
ADCA.REFCTRL = ADC_REFSEL_INT1V_gc;
ADCA.PRESCALER = ADC_PRESCALER_DIV512_gc;
uint8_t CalibrationByteL;
uint8_t CalibrationByteH;
NVM_CMD = NVM_CMD_READ_CALIB_ROW_gc;
CalibrationByteL = pgm_read_byte(offsetof(NVM_PROD_SIGNATURES_t, ADCACAL0));
CalibrationByteH = pgm_read_byte(offsetof(NVM_PROD_SIGNATURES_t, ADCACAL1));
NVM_CMD = NVM_CMD_NO_OPERATION_gc;
ADCA.CALL = CalibrationByteL;
ADCA.CALH = CalibrationByteH;
ADCA.CTRLA = ADC_ENABLE_bm;
ADCA.CH0.CTRL = ADC_CH_INPUTMODE_SINGLEENDED_gc;
ADCA.CH0.MUXCTRL = ADC_CH_MUXPOS_PIN1_gc;
//
// ADCA.CH1.CTRL = ADC_CH_INPUTMODE1_bm;
// ADCA.CH1.MUXCTRL = ADC_CH_MUXNEG_PIN1_gc;
//
}
uint16_t adc_read(void){
ADCA.CH0.CTRL |= ADC_CH_START_bm;
while(!(ADCA.CH0.INTFLAGS & ADC_CH_CHIF_bm));
ADCA.CH0.INTFLAGS = ADC_CH_CHIF_bm;
return ADCA.CH0.RES;
}
uint16_t adc1_read(void){
ADCA.CH1.CTRL |= ADC_CH_START_bm;
while(!(ADCA.CH1.INTFLAGS & ADC_CH_CHIF_bm));
ADCA.CH1.INTFLAGS = ADC_CH_CHIF_bm;
return ADCA.CH1.RES;
}
int main (void)
{
init_clk();
init_uart();
adc_init();
sei();
PMIC_CTRL = PMIC_LOLVLEN_bm;
PORTA.DIR = 0;
uint32_t val;
//uint32_t val1;
printf("moin!\r\n");
while(1)
{
val = adc_read();
val = val * 1000 / 4096;
printf("val: %03dmV\r\n", val);
//printf("val1: %03d\r\n", val1);
printf("\r\n");
delay(1000);
}
}

32
code/src/tags Normal file
View File

@@ -0,0 +1,32 @@
!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/
!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/
!_TAG_OUTPUT_MODE u-ctags /u-ctags or e-ctags/
!_TAG_PROGRAM_AUTHOR Universal Ctags Team //
!_TAG_PROGRAM_NAME Universal Ctags /Derived from Exuberant Ctags/
!_TAG_PROGRAM_URL https://ctags.io/ /official site/
!_TAG_PROGRAM_VERSION 0.0.0 //
ISR uart.c /^ISR(USARTD0_DRE_vect){$/;" f
ISR uart.c /^ISR(USARTD0_RXC_vect)$/;" f
PAD_RIGHT util.c /^#define PAD_RIGHT /;" d file:
PAD_ZERO util.c /^#define PAD_ZERO /;" d file:
PRINT_BUF_LEN util.c /^#define PRINT_BUF_LEN /;" d file:
bscale uart.c /^const int8_t bscale = 0;$/;" v typeref:typename:const int8_t
bsel uart.c /^const uint16_t bsel = 0;$/;" v typeref:typename:const uint16_t
delay util.c /^void delay(int ms){$/;" f typeref:typename:void
fifo_peek fifo.c /^uint8_t fifo_peek(Fifo_t * fifo){$/;" f typeref:typename:uint8_t
fifo_pop fifo.c /^uint8_t fifo_pop(Fifo_t * fifo, uint8_t *pByte)$/;" f typeref:typename:uint8_t
fifo_push fifo.c /^uint8_t fifo_push(Fifo_t * fifo, uint8_t byte)$/;" f typeref:typename:uint8_t
get_uart0_char uart.c /^char get_uart0_char(void)$/;" f typeref:typename:char
init_clk main.c /^void init_clk(void)$/;" f typeref:typename:void
init_uart uart.c /^void init_uart(void){$/;" f typeref:typename:void
main main.c /^int main (void)$/;" f typeref:typename:int
print util.c /^static int print(char **out, int *varg)$/;" f typeref:typename:int file:
printchar util.c /^static void printchar(char **str, int c)$/;" f typeref:typename:void file:
printf util.c /^int printf(const char *format, ...)$/;" f typeref:typename:int
printi util.c /^static int printi(char **out, int i, int b, int sg, int width, int pad, int letbase)$/;" f typeref:typename:int file:
prints util.c /^static int prints(char **out, const char *string, int width, int pad)$/;" f typeref:typename:int file:
putchar uart.c /^void putchar(char c)$/;" f typeref:typename:void
puts uart.c /^void puts(char * s){$/;" f typeref:typename:void
sprintf util.c /^int sprintf(char *out, const char *format, ...)$/;" f typeref:typename:int
uart0_rx_buffer uart.c /^Fifo_t uart0_rx_buffer = {{}, 0, 0};$/;" v typeref:typename:Fifo_t
uart0_tx_buffer uart.c /^Fifo_t uart0_tx_buffer = {{}, 0, 0};$/;" v typeref:typename:Fifo_t

69
code/src/uart.c Normal file
View File

@@ -0,0 +1,69 @@
#include <avr/interrupt.h>
#include "uart.h"
#include "util.h"
#include "fifo.h"
Fifo_t uart0_rx_buffer = {{}, 0, 0};
Fifo_t uart0_tx_buffer = {{}, 0, 0};
const uint16_t bsel = 13;
const int8_t bscale = 0;
void init_uart(void){
USARTD0_CTRLC= USART_CHSIZE0_bm | USART_CHSIZE1_bm;
//USARTD0_BAUDCTRLB = 0;
//USARTD0_BAUDCTRLA = 0;
USARTD0.BAUDCTRLA = bsel;
USARTD0.BAUDCTRLB = 0 | (bsel >> 8) | (bscale << USART_BSCALE0_bp);
USARTD0_CTRLB = USART_TXEN_bm | USART_RXEN_bm;
USARTD0_CTRLA=USART_RXCINTLVL0_bm;
PORTD_OUTSET = PIN3_bm;
PORTD_DIRSET = PIN3_bm;
PORTD_OUTCLR = PIN2_bm;
PORTD_DIRCLR = PIN2_bm;
}
void putchar(char c)
{
fifo_push(&uart0_tx_buffer, c);
USARTD0_CTRLA |= USART_DREINTLVL_LO_gc;
}
void puts(char * s){
while (*s) {
putchar(*s++);
}
}
char get_uart0_char(void)
{
while(1){
char buffer;
while( !(USARTD0_STATUS & USART_RXCIF_bm) );
buffer=USARTD0_DATA;
if ((USARTD0_STATUS & (USART_FERR_bm | USART_PERR_bm | USART_BUFOVF_bm))==0)
return buffer;
}
}
ISR(USARTD0_RXC_vect)
{
uint8_t tmp = get_uart0_char();
putchar(tmp);
if(( tmp == 0x0D || tmp == 0x0A))
printf("wurst\n\r");
else
fifo_push(&uart0_rx_buffer, tmp);
}
ISR(USARTD0_DRE_vect){
uint8_t tmp;
if(fifo_pop(&uart0_tx_buffer, &tmp) == FIFO_SUCCESS){
USARTD0_DATA = tmp;
} else {
USARTD0_CTRLA &= ~USART_DREINTLVL_LO_gc;
}
}

199
code/src/util.c Normal file
View File

@@ -0,0 +1,199 @@
void delay(int ms){
for(int i=0;i<ms;i++)
for(long j=0;j<32000/9;j++)
asm("nop");
}
/*
Copyright 2001 Georges Menie
https://www.menie.org/georges/embedded/small_printf_source_code.html
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
putchar is the only external dependency for this file,
if you have a working putchar, just remove the following
define. If the function should be called something else,
replace outbyte(c) by your own function call.
*/
static void printchar(char **str, int c)
{
extern int putchar(int c);
if (str) {
**str = c;
++(*str);
}
else (void)putchar(c);
}
#define PAD_RIGHT 1
#define PAD_ZERO 2
static int prints(char **out, const char *string, int width, int pad)
{
register int pc = 0, padchar = ' ';
if (width > 0) {
register int len = 0;
register const char *ptr;
for (ptr = string; *ptr; ++ptr) ++len;
if (len >= width) width = 0;
else width -= len;
if (pad & PAD_ZERO) padchar = '0';
}
if (!(pad & PAD_RIGHT)) {
for ( ; width > 0; --width) {
printchar (out, padchar);
++pc;
}
}
for ( ; *string ; ++string) {
printchar (out, *string);
++pc;
}
for ( ; width > 0; --width) {
printchar (out, padchar);
++pc;
}
return pc;
}
/* the following should be enough for 32 bit int */
#define PRINT_BUF_LEN 12
static int printi(char **out, int i, int b, int sg, int width, int pad, int letbase)
{
char print_buf[PRINT_BUF_LEN];
register char *s;
register int t, neg = 0, pc = 0;
register unsigned int u = i;
if (i == 0) {
print_buf[0] = '0';
print_buf[1] = '\0';
return prints (out, print_buf, width, pad);
}
if (sg && b == 10 && i < 0) {
neg = 1;
u = -i;
}
s = print_buf + PRINT_BUF_LEN-1;
*s = '\0';
while (u) {
t = u % b;
if( t >= 10 )
t += letbase - '0' - 10;
*--s = t + '0';
u /= b;
}
if (neg) {
if( width && (pad & PAD_ZERO) ) {
printchar (out, '-');
++pc;
--width;
}
else {
*--s = '-';
}
}
return pc + prints (out, s, width, pad);
}
static int print(char **out, int *varg)
{
register int width, pad;
register int pc = 0;
register char *format = (char *)(*varg++);
char scr[2];
for (; *format != 0; ++format) {
if (*format == '%') {
++format;
width = pad = 0;
if (*format == '\0') break;
if (*format == '%') goto out;
if (*format == '-') {
++format;
pad = PAD_RIGHT;
}
while (*format == '0') {
++format;
pad |= PAD_ZERO;
}
for ( ; *format >= '0' && *format <= '9'; ++format) {
width *= 10;
width += *format - '0';
}
if( *format == 's' ) {
register char *s = *((char **)varg++);
pc += prints (out, s?s:"(null)", width, pad);
continue;
}
if( *format == 'd' ) {
pc += printi (out, *varg++, 10, 1, width, pad, 'a');
continue;
}
if( *format == 'x' ) {
pc += printi (out, *varg++, 16, 0, width, pad, 'a');
continue;
}
if( *format == 'X' ) {
pc += printi (out, *varg++, 16, 0, width, pad, 'A');
continue;
}
if( *format == 'u' ) {
pc += printi (out, *varg++, 10, 0, width, pad, 'a');
continue;
}
if( *format == 'c' ) {
/* char are converted to int then pushed on the stack */
scr[0] = *varg++;
scr[1] = '\0';
pc += prints (out, scr, width, pad);
continue;
}
}
else {
out:
printchar (out, *format);
++pc;
}
}
if (out) **out = '\0';
return pc;
}
/* assuming sizeof(void *) == sizeof(int) */
int printf(const char *format, ...)
{
register int *varg = (int *)(&format);
return print(0, varg);
}
int sprintf(char *out, const char *format, ...)
{
register int *varg = (int *)(&format);
return print(&out, varg);
}