diff --git a/include/static_alloc.h b/include/memalloc.h similarity index 50% rename from include/static_alloc.h rename to include/memalloc.h index 39b1f64..159aa16 100644 --- a/include/static_alloc.h +++ b/include/memalloc.h @@ -1,9 +1,12 @@ -#ifndef STATIC_ALLOC_H_ -#define STATIC_ALLOC_H_ +#ifndef MEMALLOC_H_ +#define MEMALLOC_H_ -void * static_alloc(uint32_t size); uint32_t static_alloc_used(void); - uint32_t next_powerof2(uint32_t value); -#endif /*STATIC_ALLOC_H_*/ +void * static_alloc(uint32_t size); + +void * alloc(uint32_t size); +void free(void *p); + +#endif /*MEMALLOC_H_*/ diff --git a/main.c b/main.c index 29fc7ad..476eaa9 100644 --- a/main.c +++ b/main.c @@ -26,7 +26,7 @@ #include "at91_twi.h" #include "at91_tc1.h" -#include "static_alloc.h" +#include "memalloc.h" #include "board.h" #include diff --git a/src/at91_adc.c b/src/at91_adc.c index a9c5f9b..0b93721 100644 --- a/src/at91_adc.c +++ b/src/at91_adc.c @@ -22,14 +22,13 @@ #include "AT91SAM7S256.h" #include "board.h" #include "at91_pitc.h" -#include "static_alloc.h" static uint16_t adc_result[4]; static void at91_adc_isr(void) { AT91S_PDC *pdc = AT91C_BASE_PDC_ADC; - pdc->PDC_RPR = (uint32_t) &adc_result; + pdc->PDC_RPR = (uint32_t) &adc_result; pdc->PDC_RCR = ARRAY_SIZE(adc_result); pdc->PDC_PTCR = AT91C_PDC_RXTEN; @@ -53,21 +52,21 @@ void at91_adc_test_init(void) /* ADC Software reset */ AT91S_ADC *adc = AT91C_BASE_ADC; adc->ADC_CR = AT91C_ADC_SWRST; - - /* + + /* * ADC config: 10bit, no sleep * 4.8MHz (48MHz / ((4 +1) * 2) = 4.8MHz) - * 96 cycles Startup ((11 +1) * 8 / 4.8MHz = 20us) + * 96 cycles Startup ((11 +1) * 8 / 4.8MHz = 20us) * 3 cycles SH ((2 +1) / 4.8MHz = 625ns) * Conversion time per channel @5MHz ~2us - */ + */ adc->ADC_MR = AT91C_ADC_TRGEN_DIS | AT91C_ADC_LOWRES_10_BIT | AT91C_ADC_SLEEP_NORMAL_MODE | (AT91C_ADC_PRESCAL & (4 << 8)) | (AT91C_ADC_STARTUP & (11 << 16)) | (AT91C_ADC_SHTIM & (2 << 24)); - + /* setup PDC */ AT91S_PDC *pdc = AT91C_BASE_PDC_ADC; pdc->PDC_RPR = (uint32_t) &adc_result; @@ -83,7 +82,7 @@ void at91_adc_test_init(void) aic->AIC_SMR[AT91C_ID_ADC] = IRQPRIO_ADC | AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL; aic->AIC_SVR[AT91C_ID_ADC] = (uint32_t)at91_adc_isr; aic->AIC_IECR = (1< min. 3Hz @48MHz MCK */ #define HZ_TO_PIV(HZ) (MCK / (16 * HZ)) @@ -35,7 +35,7 @@ struct pitc_timer * alloc_pitc_timer(uint32_t interval, uint32_t (*func)(struct timer->func = func; timer->privdata = privdata; - return timer; + return timer; } void pitc_schedule_timer(struct pitc_timer *timer) @@ -54,7 +54,7 @@ static void pitc_isr(uint32_t status) { /* get Ticks and clear interrupt */ pitc_ticks += (*AT91C_PITC_PIVR & AT91C_PITC_PICNT) >> 20; - + struct pitc_timer *search, *tmp; list_for_each_entry_safe(search, tmp, &timer_list, list) { /* if this entry not scheduled yet, abort search */ @@ -84,7 +84,7 @@ void at91_pitc_init(void) { sysc_register_isr(AT91_SYSIRQ_PIT, &pitc_isr); - *AT91C_PITC_PIMR = (AT91C_PITC_PIV & HZ_TO_PIV(100)) | + *AT91C_PITC_PIMR = (AT91C_PITC_PIV & HZ_TO_PIV(100)) | AT91C_PITC_PITEN | AT91C_PITC_PITIEN; } diff --git a/src/fifo.c b/src/fifo.c index 0f150a0..63a8263 100644 --- a/src/fifo.c +++ b/src/fifo.c @@ -19,23 +19,23 @@ #include "AT91SAM7S256.h" #include "atomic.h" #include "fifo.h" -#include "static_alloc.h" +#include "memalloc.h" #define FIFO_MASK(x) ((x)->size -1) /* * get used bytes (under lock) * all other operations don't need locks: - * - only fifo_put/fifo_rxpdc are allowed to increment fifo->in + * - only fifo_put/fifo_rxpdc are allowed to increment fifo->in * - only fifo_get/fifo_txpdc are allowed to increment fifo->out - * a integer overflow (4gb) of fifo->in / fifo->out could cause trouble + * a integer overflow (4gb) of fifo->in / fifo->out could cause trouble */ static uint32_t fifo_used(struct fifo *fifo) { disable_irqs(); uint32_t used = fifo->in - fifo->out; restore_irqs(); - return used; + return used; } /* @@ -48,12 +48,12 @@ uint32_t fifo_put(struct fifo *fifo, const char *buf, uint32_t len) if (len > left) len = left; - uint32_t count = len; + uint32_t count = len; while (count--) fifo->buf[fifo->in++ & FIFO_MASK(fifo)] = *buf++; - + return len; -} +} /* * get data from fifo @@ -64,12 +64,12 @@ uint32_t fifo_get(struct fifo *fifo, char *buf, uint32_t len) uint32_t used = fifo_used(fifo); if (len > used) len = used; - + uint32_t count = len; while (count--) *buf++ = fifo->buf[fifo->out++ & FIFO_MASK(fifo)]; - return len; + return len; } /* @@ -79,16 +79,16 @@ uint32_t fifo_rxpdc(struct fifo *fifo, AT91S_PDC *pdc, uint16_t maxsize) { /* account previous PDC transfer */ fifo->in += fifo->pdc_rx; - - uint32_t free = fifo->size - fifo_used(fifo); - if (free) { + + uint32_t left = fifo->size - fifo_used(fifo); + if (left) { /* calc pointer for next transfer */ uint32_t first_idx = (fifo->in & FIFO_MASK(fifo)); pdc->PDC_RPR = (uint32_t)(fifo->buf + first_idx); - + /* check for buffer end -> split transfer */ - if (first_idx + free <= (fifo->size -1)) { - fifo->pdc_rx = free; + if (first_idx + left <= (fifo->size -1)) { + fifo->pdc_rx = left; } else { fifo->pdc_rx = fifo->size - first_idx; } @@ -97,32 +97,32 @@ uint32_t fifo_rxpdc(struct fifo *fifo, AT91S_PDC *pdc, uint16_t maxsize) if (maxsize && fifo->pdc_rx > maxsize) fifo->pdc_rx = maxsize; - /* start transfer */ + /* start transfer */ pdc->PDC_RCR = fifo->pdc_rx; - + } else { /* no data in buffer */ fifo->pdc_rx = 0; } - + return fifo->pdc_rx; } /* * transmit fifo via PDC * returns 0 if no transfer was started - */ + */ uint32_t fifo_txpdc(struct fifo *fifo, AT91S_PDC *pdc, uint16_t maxsize) { /* account previous PDC transfer */ fifo->out += fifo->pdc_tx; - + uint32_t used = fifo_used(fifo); if (used) { /* calc pointer for next transfer */ uint32_t first_idx = (fifo->out & FIFO_MASK(fifo)); pdc->PDC_TPR = (uint32_t)(fifo->buf + first_idx); - + /* check for buffer end -> split transfer */ if (first_idx + used <= (fifo->size -1)) { fifo->pdc_tx = used; @@ -134,14 +134,14 @@ uint32_t fifo_txpdc(struct fifo *fifo, AT91S_PDC *pdc, uint16_t maxsize) if (maxsize && fifo->pdc_tx > maxsize) fifo->pdc_tx = maxsize; - /* start transfer */ + /* start transfer */ pdc->PDC_TCR = fifo->pdc_tx; - + } else { /* no data in buffer */ fifo->pdc_tx = 0; } - + return fifo->pdc_tx; } diff --git a/src/static_alloc.c b/src/memalloc.c similarity index 95% rename from src/static_alloc.c rename to src/memalloc.c index afc5e1e..3976762 100644 --- a/src/static_alloc.c +++ b/src/memalloc.c @@ -20,8 +20,9 @@ #include #include "AT91SAM7S256.h" #include "board.h" -#include "static_alloc.h" +#include "memalloc.h" +// TODO: make generic version and find better place #define ALIGN(x) (((x) +3) & ~3) /* extern symbols, defined in ldscript */ @@ -91,7 +92,7 @@ static struct memchunk * alloc_add(uint32_t size) struct memchunk *newblock = static_alloc(size); /* struct at end of memory block */ - struct memchunk *endblock = (void *)newblock + size - sizeof(struct memchunk); + struct memchunk *endblock = (void *)((uint32_t)newblock + size - sizeof(struct memchunk)); /* link between them */ newblock->next = endblock; @@ -161,7 +162,7 @@ try_next: /* if we split the chunk, the remaining piece must be large enough */ if (((uint32_t)GET_NEXT(found) - (uint32_t)found) > (size + 2 * sizeof(struct memchunk))) { - struct memchunk *end = (void *)found + size; + struct memchunk *end = (void *)((uint32_t)found + size); end->next = SET_FLAGS(GET_NEXT(found), 0); found->next = SET_FLAGS(end, FLAG_CHUNKINUSE); diff --git a/src/telemetrie.c b/src/telemetrie.c index 7ccfc30..5ce39e1 100644 --- a/src/telemetrie.c +++ b/src/telemetrie.c @@ -21,6 +21,7 @@ #include #include "board.h" // ARRAY_SIZE() #include "telemetrie.h" +#include "memalloc.h" /* extern symbols, defined in ldscript */ extern struct tdc_value _tdc_value_table; @@ -60,16 +61,6 @@ int32_t tdc_transmit(uint32_t addr, const uint8_t *data, uint32_t size) return -1; } - -static void * alloc(uint32_t size) -{ - return NULL; -} - -static void free(void *p) -{ -} - /* void *blub = NULL; func(&blub);