blob: c9c6422a6fdacb03d3b10e73ba8bd48a123f34cb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
// Main starting point for PRU code.
//
// Copyright (C) 2017 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
#include <stdint.h> // uint32_t
#include <string.h> // memset
#include <pru/io.h> // read_r31
#include <pru_cfg.h> // CT_CFG
#include <pru_iep.h> // CT_IEP
#include <pru_intc.h> // CT_INTC
#include "autoconf.h" // CONFIG_CLOCK_FREQ
#include "board/misc.h" // timer_from_us
#include "board/irq.h" // irq_disable
#include "command.h" // shutdown
#include "generic/timer_irq.h" // timer_dispatch_many
#include "internal.h" // IEP_IRQ
#include "sched.h" // sched_main
DECL_CONSTANT(MCU, "pru");
/****************************************************************
* Timers
****************************************************************/
void
irq_disable(void)
{
}
void
irq_enable(void)
{
}
irqstatus_t
irq_save(void)
{
return 0;
}
void
irq_restore(irqstatus_t flag)
{
}
static void
timer_set(uint32_t value)
{
CT_IEP.TMR_CMP0 = value;
CT_INTC.SECR0 = 1 << IEP_EVENT;
}
uint32_t
timer_read_time(void)
{
return CT_IEP.TMR_CNT;
}
static void
_irq_poll(void)
{
CT_IEP.TMR_CMP_STS = 0xff;
uint32_t next = timer_dispatch_many();
timer_set(next);
}
void
irq_poll(void)
{
if (read_r31() & (1 << (IEP_IRQ + R31_IRQ_OFFSET)))
_irq_poll();
}
static void
timer_shutdown(void)
{
// Reenable timer irq
timer_set(timer_read_time() + 50);
CT_IEP.TMR_CMP_STS = 0xff;
__delay_cycles(4);
CT_INTC.SECR0 = 1 << IEP_EVENT;
}
DECL_SHUTDOWN(timer_shutdown);
static void
timer_init(void)
{
timer_set(0);
CT_IEP.TMR_CMP_CFG = 0x01 << 1;
CT_IEP.TMR_GLB_CFG = 0x11;
timer_shutdown();
}
/****************************************************************
* Allocator
****************************************************************/
extern char _heap_start;
static void *heap_ptr = &_heap_start;
#define STACK_SIZE 256
#define END_MEM ((void*)(8*1024 - STACK_SIZE))
// Allocate an area of memory
void *
alloc_chunk(size_t size)
{
if (heap_ptr + size > END_MEM)
shutdown("alloc_chunk failed");
void *data = heap_ptr;
heap_ptr += size;
memset(data, 0, size);
return data;
}
// Allocate an array of chunks
void *
alloc_chunks(size_t size, size_t count, size_t *avail)
{
size_t can_alloc = 0;
void *p = heap_ptr;
for (; can_alloc <= count && p + size <= END_MEM; can_alloc++, p += size)
;
if (!can_alloc)
shutdown("alloc_chunks failed");
*avail = can_alloc;
return alloc_chunk(size * can_alloc);
}
/****************************************************************
* Startup
****************************************************************/
// Main entry point
int
main(void)
{
// allow access to external memory
CT_CFG.SYSCFG_bit.STANDBY_INIT = 0;
console_init();
timer_init();
sched_main();
return 0;
}
|