summaryrefslogtreecommitdiff
path: root/MakefileBasedBuild/app/fwk.c
blob: 299d3922566193559821455e206092dbe80a3886 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#define ADK_INTERNAL
#include "fwk.h"
#include "dbg.h"






typedef struct PeriodicNode{
    struct PeriodicNode* next;
    PeriodicFunc pF;
    void* pD;
    uint32_t count;
    uint32_t reload;
}PeriodicNode;



volatile static uint64_t msec = 0;
volatile static PeriodicNode* periodics = 0;

static uint8_t volume = 255;

#define JOINER(x,y,z)	x ## y ## z
#define CALLER(x,y,z)	JOINER(x,y,z)
#define TIMER_NAME	CALLER(TC, TIMER_FOR_ADK,  _IrqHandler)
#define IRQ_NAME	CALLER(TC, TIMER_FOR_ADK,  _IRQn)

void TIMER_NAME(){

    uint32_t unused = TC0->TC_CHANNEL[TIMER_FOR_ADK].TC_SR;
    PeriodicNode* n = periodics;

    msec++;

    while(n){

        if(!--n->count){

            n->pF(n->pD);
            n->count = n->reload;
        }
        n = n->next;
    }
}

uint64_t fwkGetUptime(void){

    uint64_t t;

    do{
        t = msec;
    }while(t != msec);

    return t;
}

uint64_t fwkGetTicks(void){

    uint64_t hi;
    uint32_t lo;

    do{
        lo = TC0->TC_CHANNEL[TIMER_FOR_ADK].TC_CV;
        hi = msec;
    }while(lo > TC0->TC_CHANNEL[TIMER_FOR_ADK].TC_CV);

    return (hi * TICKS_PER_MS) + lo;
}

void fwkDelay(uint64_t ticks){

    uint64_t start = fwkGetTicks();

    while(fwkGetTicks() - start < ticks);
}

void periodicAdd(PeriodicFunc f, void* data, uint32_t periodMs){

    PeriodicNode* n = malloc(sizeof(PeriodicNode));
    uint32_t failed = 0;

    n->reload = n->count = periodMs;
    n->pF = f;
    n->pD = data;

    do{
        asm(
             "ldrex r0, [%1]	 \n\t"	//atomic linked list addition..booyah!
             "str   r0, [%2]     \n\t"
             "strex %0, %3, [%1] \n\t"
             :"=r"(failed)
             :"r"(&periodics), "r"(&n->next), "r"(n)
             :"r0"
        );
    }while(failed);
}

void periodicDel(PeriodicFunc f){

    uint32_t failed;

    PeriodicNode* p = 0;
    PeriodicNode* n = periodics;

    while(n && n != f){

        p = n;
        n = n->next;
    }
    if(!n) return; //nothing to delete

    if(p) p->next = n->next;
    else periodics = n->next;
    free(n);
}

static void __attribute__((naked)) cpuGetUniqIdFunc(uint32_t interfaceAddr, uint32_t cmd1, uint32_t cmd2, uint32_t* buf){

    asm(
        "    cpsid i			\n\t"   // There is some pretty scary stuff that can happen
        "    mov r12, r2		\n\t"   //  if you reset the device while this code is in
        "    str r1, [r0, #4]		\n\t"   //  the middle of doing its thing. Sometimes the
        "lbl_wait_1:			\n\t"   //  flash controller does not get reset to "code"
        "    ldr r1, [r0, #8]		\n\t"   //  mode and stays in "read unique ID" mode, even
        "    lsrs r1, #1		\n\t"   //  when the chip itself is externally reset. This
        "    bcs lbl_wait_1		\n\t"   //  will cause the unique ID to be treated like the
        "    mov r2, #0x00080000	\n\t"   //  vector table. Needless to say: the chip unique
        "    ldr r1, [r2, #0]		\n\t"   //  ID is not a suitable vector table. After reset,
        "    str r1, [r3, #0]		\n\t"   //  this will likely cause the chip to take a hard
        "    ldr r1, [r2, #4]		\n\t"   //  fault immediately. Worse yet, the hard fault
        "    str r1, [r3, #4]		\n\t"   //  handler vector will also be invalid, causing
        "    ldr r1, [r2, #8]		\n\t"   //  the chip to take yet another hard fault. This
        "    str r1, [r3, #8]		\n\t"   //  will continue forever. The debug bridge, as it
        "    ldr r1, [r2, #12]		\n\t"   //  currently is, cannot handle this chip state.
        "    str r1, [r3, #12]		\n\t"   //  It will be unable to flash or debug the chip,
        "    str r12, [r0, #4]		\n\t"   //  and you - the user - will be sad. The practical
        "lbl_wait_2:			\n\t"   //  upshot of all this: use this code rarely, and
        "    ldr r1, [r0, #8]		\n\t"   //  if you interrupt it, power-cycle the system if
        "    lsrs r1, #1		\n\t"   //  you suspect that you somehow got into this
        "    bcc lbl_wait_2		\n\t"   //  weird state. It is possible that future revs
        "    cpsie i			\n\t"   //  of SAM3X will fix this issue by properly
        "    bx  lr			\n\t"   //  resetting the flash controller at reset time.
    );
}

void cpuGetUniqId(uint32_t* dst){	//produce the 128-bit unique ID

    const unsigned numInstr = 23; //it better match the above function...
    uint16_t buffer[numInstr];
    uint16_t* src = (uint16_t*)(((uint32_t)&cpuGetUniqIdFunc) &~ 1);
    unsigned i;

    for(i = 0; i < numInstr; i++) buffer[i] = *src++;

    ((void (*)(uint32_t,uint32_t,uint32_t,uint32_t*))(((uint32_t)buffer) + 1))(0x400E0A00, 0x5A00000E, 0x5A00000F, dst);
}

void fwkInit(void){

    //disable WDT
    WDT_Disable( WDT ) ;
   
    //clock & periodic setup (MCLK/2, interrupt every ms)
    PMC_EnablePeripheral(ID_TC0);
    TC0->TC_CHANNEL[TIMER_FOR_ADK].TC_CMR = TC_CMR_TCCLKS_TIMER_CLOCK1 | TC_CMR_WAVSEL_UP_RC | TC_CMR_WAVE;
    TC0->TC_CHANNEL[TIMER_FOR_ADK].TC_CV = 0;
    TC0->TC_CHANNEL[TIMER_FOR_ADK].TC_RC = TICKS_PER_MS;
    TC0->TC_CHANNEL[TIMER_FOR_ADK].TC_IER = TC_IER_CPCS;
    TC0->TC_CHANNEL[TIMER_FOR_ADK].TC_CCR = TC_CCR_CLKEN | TC_CCR_SWTRG;
    NVIC_EnableIRQ(IRQ_NAME);

    //IO on
    PMC_EnablePeripheral(ID_PIOC);
    PMC_EnablePeripheral(ID_PIOB);
    PMC_EnablePeripheral(ID_PIOA);

    //dma setup
    PMC_EnablePeripheral(ID_DMAC);
    DMAC->DMAC_EBCIDR = 0x003F3F3F;	//disable all interrupts
    DMAC->DMAC_CHDR = 0x0000003F;	//disable all channels
    DMAC->DMAC_EN = 1;
}

#define PER	0
#define PDR	1
#define OER	4
#define ODR	5
#define SODR	12
#define CODR	13
#define PDSR	15
#define PUDR	24
#define PUER	25
#define ABSR	28

static inline volatile uint32_t* gpioGetPort(uint8_t pin){

    uint32_t addr = pin >> 5;
    addr <<= 9;
    addr += 0x400E0E00;

    return (volatile uint32_t*)addr;
}

static inline uint32_t gpioGetBit(uint8_t pin){

    return 1UL << (pin & 31);
}

void gpioSetFun(uint8_t pin, uint8_t func){

    volatile uint32_t* port = gpioGetPort(pin);
    uint32_t bit = gpioGetBit(pin);

    if(func == GPIO_FUNC_GPIO){

        port[PER] = bit;
    }
    else{

        port[PDR] = bit;
        if(func == GPIO_FUNC_A) port[ABSR] &=~ bit;
        else port[ABSR] |= bit;
    }
}

void gpioSetDir(uint8_t pin, char in){

    volatile uint32_t* port = gpioGetPort(pin);
    uint32_t bit = gpioGetBit(pin);

    if(in) port[ODR] = bit;
    else port[OER] = bit;
}

void gpioSetVal(uint8_t pin, char on){

    volatile uint32_t* port = gpioGetPort(pin);
    uint32_t bit = gpioGetBit(pin);

    if(on) port[SODR] = bit;
    else port[CODR] = bit;
}

char gpioGetVal(uint8_t pin){

    volatile uint32_t* port = gpioGetPort(pin);
    uint32_t bit = gpioGetBit(pin);

    return (port[PDSR] & bit) != 0;
}

void gpioSetPullup(uint8_t pin, char on){

    volatile uint32_t* port = gpioGetPort(pin);
    uint32_t bit = gpioGetBit(pin);

    if(on) port[PUER] = bit;
    else port[PUDR] = bit;
}

uint8_t getVolume(void){

    return volume;
}

void setVolume(uint8_t vol){

    volume = vol;
}