Compare commits

..
8 Commits
Author SHA1 Message Date
razzor d4a0911db9 new toolchain 2009-06-21 12:52:51 +02:00
razzor d50c024cbd test 2008-06-23 15:15:56 +02:00
razzor e6155fb7ad move exception handlers in C files 2008-06-23 15:15:56 +02:00
razzor 021be80589 working semaphore & threading 2008-06-23 15:15:56 +02:00
razzor 5a37574a47 repair makefile 2008-06-23 15:15:56 +02:00
razzor 9e8158277c context, spinlock and semaphore functions 2008-06-23 15:15:56 +02:00
razzor 991b088b0d working store & restore 2008-06-23 15:15:56 +02:00
razzor 011d784923 working init-ctx version 2008-06-23 15:15:56 +02:00
20 changed files with 756 additions and 1676 deletions

No files matched your search

+2 -2
View File
@@ -10,8 +10,8 @@ SIZE = $(TOOLCHAIN)/bin/arm-elf-size
OBJCOPY = $(TOOLCHAIN)/bin/arm-elf-objcopy
OBJDUMP = $(TOOLCHAIN)/bin/arm-elf-objdump
INCDIRS = include $(TOOLCHAIN)/lib/gcc/arm-elf/4.1.1/include $(TOOLCHAIN)/arm-elf/include
LIBDIRS = $(TOOLCHAIN)/arm-elf/lib $(TOOLCHAIN)/lib/gcc/arm-elf/4.1.1
INCDIRS = include $(TOOLCHAIN)/lib/gcc/arm-elf/4.3.3/include $(TOOLCHAIN)/arm-elf/include
LIBDIRS = $(TOOLCHAIN)/arm-elf/lib $(TOOLCHAIN)/lib/gcc/arm-elf/4.3.3
# ------
+40 -52
View File
@@ -16,8 +16,10 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
.equ AIC_IVR, (256)
.equ AIC_FVR, (260)
.equ AIC_EOICR, (304)
.equ AT91C_BASE_AIC, (0xFFFFF000)
.equ AT91C_BASE_MC, (0xFFFFFF00)
.equ IRQ_Stack_Size, (3 * 8 * 4)
.equ FIQ_Stack_Size, (0 * 8 * 4)
@@ -27,53 +29,11 @@
.equ ARM_MODE_IRQ, 0x12
.equ ARM_MODE_SVC, 0x13
.equ ARM_MODE_ABT, 0x17
.equ ARM_MODE_SYS, 0x1F
.equ I_BIT, 0x80
.equ F_BIT, 0x40
.section .init_dummy, "x"
.global _hwstart
.func _hwstart
_hwstart:
ldr pc, [pc, #24] /* 0x00 Reset handler */
ldr pc, [pc, #24] /* 0x04 Undefined Instruction */
ldr pc, [pc, #20] /* 0x08 Software Interrupt */
ldr pc, [pc, #16] /* 0x0C Prefetch Abort */
ldr pc, [pc, #12] /* 0x10 Data Abort */
ldr pc, [pc, #8] /* 0x14 reserved */
ldr pc, [pc, #4] /* 0x18 IRQ */
ldr pc, [pc, #0] /* 0x1c FIQ */
.word _relocate + 0x13c000
.word 0x80000000
.endfunc
.section .init, "x"
.global _relocate
.func _relocate
_relocate:
msr CPSR_c, #ARM_MODE_SVC | I_BIT | F_BIT
/* Remap RAM to 0x00000000 */
ldr r1, =AT91C_BASE_MC
mov r2, #0x01
str r2, [r1]
/* Relocate .text section */
ldr r1, =__text_copy_start__
ldr r2, =__text_start__
ldr r3, =__text_end__
_relocate1: cmp r2, r3
ldrlo r0, [r1], #4
strlo r0, [r2], #4
blo _relocate1
ldr r0, =InitReset
mov pc, r0
.endfunc
.section .text
.global _start
.func _start
@@ -87,14 +47,14 @@ rsvdvec: ldr pc, [pc, #24] /* 0x14 reserved */
irqvec: ldr pc, [pc, #24] /* 0x18 IRQ */
fiqvec: ldr pc, [pc, #24] /* 0x1c FIQ */
.extern ABT_Handler
.extern IRQ_Handler
.extern FIQ_Handler
.extern SWI_Handler
.extern ABT_Handler
.extern IRQ_Handler
/* 0x80000000 will result in Prefetch Abort */
.word InitReset
.word 0x80000000
.word 0x80000000
.word SWI_Handler
.word ABT_Handler
.word ABT_Handler
.word 0x80000000
@@ -119,7 +79,7 @@ InitReset:
mov sp, r0
sub r0, r0, #FIQ_Stack_Size
/* store AIC Base in r8_fiq for faster access */
/* store AIC Base in ARM_MODE_FIQ:r8 for faster access */
ldr r8, =AT91C_BASE_AIC
/* Setup IRQ Mode Stack */
@@ -133,11 +93,11 @@ InitReset:
sub r0, r0, #ABT_Stack_Size
/* Setup Supervisor Mode Stack (IRQ & NMI enabled) */
msr CPSR_c, #ARM_MODE_SVC
msr CPSR_c, #ARM_MODE_SYS
mov sp, r0
/* Relocate .data section */
ldr r1, =__data_copy_start__
ldr r1, =__text_end__
ldr r2, =__data_start__
ldr r3, =__data_end__
LoopRel: cmp r2, r3
@@ -151,7 +111,7 @@ LoopRel: cmp r2, r3
ldr r2, =__bss_end__
LoopZI: cmp r1, r2
strlo r0, [r1], #4
blo LoopZI
BLO LoopZI
/* Start main() */
ldr lr, =exit
@@ -165,3 +125,31 @@ LoopZI: cmp r1, r2
/* exit dummy for newlib */
exit: b .
.endfunc
.global FIQ_Handler
.func FIQ_Handler
FIQ_Handler:
/* Save r0 to ARM_MODE_FIQ:r9 */
mov r9, r0
/* get FIQ Vector from AIC and thus clear FIQ */
ldr r0, [r8, #AIC_FVR]
/* Switch to ARM_MODE_SVC and save registers there */
msr CPSR_c, #ARM_MODE_SYS | I_BIT | F_BIT
stmfd sp!, { r1-r3, r12, lr }
/* execute FIQ in SVC_MODE */
mov r14, pc
bx r0
/* restore registers and switch back to ARM_MODE_FIQ */
ldmia sp!, { r1-r3, r12, lr }
msr CPSR_c, #ARM_MODE_FIQ | I_BIT | F_BIT
/* restore the ARM_MODE_SVC:r0 */
mov r0, r9
/* restore PC using the LR_fiq directly */
subs pc, lr, #4
.endfunc
+73 -83
View File
@@ -18,7 +18,6 @@
***************************************************************************/
#include <stdint.h>
#include "AT91SAM7S256.h"
#include "board.h"
static void empty_isr(void) {}
@@ -37,7 +36,7 @@ void at91_init1(void)
*AT91C_RSTC_RMR = (AT91C_RSTC_KEY & 0xA5 << 24) | AT91C_RSTC_URSTEN;
/* Set Flash Waitstates */
*AT91C_MC_FMR = (AT91C_MC_FMCN & ((MCK / 666666) << 16)) | AT91C_MC_FWS_1FWS;
*AT91C_MC_FMR = AT91C_MC_FWS_1FWS;
/*
* Enable main oscillator (MAINCK)
@@ -79,102 +78,93 @@ void at91_init1(void)
aic->AIC_SVR[i] = (uint32_t)empty_isr;
}
aic->AIC_SPU = (uint32_t)empty_isr;
/* end-of-interrupt signal */
do {
aic->AIC_EOICR = ~0;
} while (aic->AIC_ISR != 0x00);
}
/* TODO: make it static */
uint32_t nested_count = 0;
__attribute__((naked)) void IRQ_Handler(void)
{
asm volatile (
".equ ARM_MODE_IRQ, 0x12 \n\t"
".equ ARM_MODE_SVC, 0x13 \n\t"
".equ I_BIT, 0x80 \n\t"
".equ AIC_IVR, (256) \n\t"
".equ AIC_EOICR, (304) \n\t"
".equ AIC_IVR_OFF, (256) \n\t"
".equ AIC_EOICR_OFF, (304) \n\t"
".equ AT91C_BASE_AIC, (0xFFFFF000) \n\t"
/* Adjust and save lr_irq on IRQ stack */
"sub lr, lr, #4 \n\t"
"stmfd sp!, { lr } \n\t"
/* Save SPSR (for nested interrupts) */
"mrs r14, SPSR \n\t"
"stmfd sp!, { r14 } \n\t"
/* Save r0 on IRQ stack */
"stmfd sp!, { r0 } \n\t"
/* Write in the IVR to support Protect Mode */
"ldr r14, =AT91C_BASE_AIC \n\t"
"ldr r0, [r14, #AIC_IVR] \n\t"
"str r14, [r14, #AIC_IVR] \n\t"
/* Enable Interrupt and switch to SVC mode */
"msr CPSR_c, #ARM_MODE_SVC \n\t"
/* Save scratch/used registers and lr on SVC Stack */
"stmfd sp!, { r1-r3, r12, r14 } \n\t"
/* Branch to the routine pointed by the AIC_IVR */
"mov r14, pc \n\t"
"bx r0 \n\t"
/* Restore scratch/used registers and lr from SVC Stack */
"ldmia sp!, { r1-r3, r12, r14 } \n\t"
/* Disable Interrupt and switch back to IRQ mode */
"msr CPSR_c, #ARM_MODE_IRQ | I_BIT \n\t"
/* Mark the End of Interrupt on the AIC */
"ldr r14, =AT91C_BASE_AIC \n\t"
"str r14, [r14, #AIC_EOICR] \n\t"
/* Restore SPSR_irq and r0 from IRQ stack */
"ldmia sp!, { r0 } \n\t"
"ldmia sp!, { r14 } \n\t"
"msr SPSR_cxsf, r14 \n\t"
/* Restore adjusted lr_irq from IRQ stack */
"ldmia sp!, { pc }^ \n\t"
);
}
__attribute__((naked)) void FIQ_Handler(void)
{
asm volatile (
".equ ARM_MODE_FIQ, 0x11 \n\t"
".equ ARM_MODE_SVC, 0x13 \n\t"
".equ ARM_MODE_IRQ, 0x12 \n\t"
".equ ARM_MODE_SYS, 0x1F \n\t"
".equ I_BIT, 0x80 \n\t"
".equ F_BIT, 0x40 \n\t"
".equ AIC_FVR, (260) \n\t"
/* Manage Exception Entry */
/* Save LR_irq to IRQ stack */
"sub lr, lr, #4 \n\t"
"stmdb sp!, { lr } \n\t"
/* Save r0 to r9_fiq */
"mov r9, r0 \n\t"
/* Save r0 and SPSR to IRQ stack */
"mrs r14, SPSR \n\t"
"stmdb sp!, { r0, r14 } \n\t"
/* get FIQ Vector from AIC and thus clear FIQ */
"ldr r0, [r8, #AIC_FVR] \n\t"
/* count nested interrupts */
"ldr r14, =nested_count \n\t"
"ldr r0, [r14] \n\t"
"add r0, r0, #1 \n\t"
"str r0, [r14] \n\t"
/* Switch to SVC and save registers there */
"msr CPSR_c, #ARM_MODE_SVC | I_BIT | F_BIT \n\t"
"stmfd sp!, { r1-r3, r12, lr } \n\t"
/* De-assert the NIRQ and clear the source in Protect Mode */
"ldr r14, =AT91C_BASE_AIC \n\t"
"ldr r0, [r14, #AIC_IVR_OFF] \n\t"
"str r14, [r14, #AIC_IVR_OFF] \n\t"
/* execute FIQ in SVC_MODE */
"mov r14, pc \n\t"
"bx r0 \n\t"
/* Enable Interrupt and Switch in Supervisor Mode */
"msr CPSR_c, #ARM_MODE_SYS \n\t"
/* restore registers and switch back to FIQ */
"ldmia sp!, { r1-r3, r12, lr } \n\t"
"msr CPSR_c, #ARM_MODE_FIQ | I_BIT | F_BIT \n\t"
/* Save scratch/used registers and LR in User Stack */
"stmdb sp!, { r1-r3, r12, r14 } \n\t"
/* restore the r0 from r9_fiq */
"mov r0, r9 \n\t"
/* Branch to the routine pointed by the AIC_IVR */
"mov r14, pc \n\t"
"bx r0 \n\t"
/* Restore scratch/used registers and LR from User Stack */
"ldmia sp!, { r1-r3, r12, r14 } \n\t"
/* Disable Interrupt and switch back in IRQ mode */
"msr CPSR_c, #ARM_MODE_IRQ | I_BIT \n\t"
/* Mark the End of Interrupt on the AIC */
"ldr r14, =AT91C_BASE_AIC \n\t"
"str r14, [r14, #AIC_EOICR_OFF] \n\t"
/* count nested interrupts */
"ldr r14, =nested_count \n\t"
"ldr r0, [r14] \n\t"
"subs r0, r0, #1 \n\t"
"str r0, [r14] \n\t"
"beq irq_ctx_save \n\t"
/* Restore r0 and SPSR_irq from IRQ stack */
"ldmia sp!, { r0, r14 } \n\t"
"msr SPSR_cxsf, r14 \n\t"
/* Restore adjusted LR_irq from IRQ stack */
"ldmia sp!, { pc }^ \n\t"
/* save the userspace context to current_context */
"irq_ctx_save: \n\t"
/* get top of struct register_context */
"ldr r0, =current_context \n\t"
"ldr r0, [r0] \n\t"
"add r0, r0, #68 \n\t"
/* save usermode cpsr & r2-14 */
"mrs r1, spsr \n\t"
"stmdb r0, {r1-r14}^ \n\t"
"nop \n\t"
"sub r0, r0, #56 \n\t"
/* save r0-1 and svc_lr (= pc) */
"ldmia sp!, {r1, r2} \n\t"
"stmdb r0!, {r1, r2, r14} \n\t"
/* restore PC using the lr_fiq directly */
"subs pc, lr, #4 \n\t"
);
}
+3 -3
View File
@@ -1,7 +1,7 @@
#ifndef AT91TWI_H_
#define AT91TWI_H_
#include <stdint.h>
#include <list.h>
/* TWI slave addresses */
#define TWI_ADDR_BL1 0x21
@@ -10,7 +10,7 @@
#define TWI_ADDR_BL4 0x24
#define TWI_ADDR_EEPROM 0x40
/* TWIBOOT commands */
/* TWIBOOT commands */
#define CMD_WAIT 0x00
#define CMD_GET_INFO 0x10
#define CMD_GET_SIGNATURE 0x11
@@ -33,7 +33,7 @@
struct blmc_cmd {
uint32_t cmd; /* cmd byte(s) */
uint8_t mode; /* read/write, cmdlen (1-3 bytes) */
uint16_t size; /* data size */
uint8_t size; /* data size */
uint8_t *data; /* read/write data */
};
-14
View File
@@ -1,20 +1,6 @@
#ifndef AT91_UDP_H_
#define AT91_UDP_H_
#include <stdint.h>
void ep_transfer_send(uint32_t ep,
char *data,
uint32_t length,
void (*complete_cb)(void));
void ep_transfer_receive(uint32_t ep,
char *data,
uint32_t length,
void (*complete_cb)(void));
void ep_send_stall(uint32_t ep);
void at91_udp_init(void);
#endif /*AT91_UDP_H_*/
+6 -6
View File
@@ -12,16 +12,16 @@
#define BAUD_TO_DIV(BAUD) (MCK / (16 * BAUD))
/* LED PIOs */
#define LED_ORANGE AT91C_PIO_PA24
#define LED_GREEN AT91C_PIO_PA23
#define LED_ORANGE AT91C_PIO_PA17
#define LED_GREEN AT91C_PIO_PA18
/* Taster PIOs */
#define TAST1 AT91C_PIO_PA0
#define TAST2 AT91C_PIO_PA1
#define TAST1 AT91C_PIO_PA19
#define TAST2 AT91C_PIO_PA20
/* USB PIOs */
#define UDP_VBUS_MON AT91C_PIO_PA8
#define UDP_PULLUP AT91C_PIO_PA16
#define UDP_VBUS_MON AT91C_PIO_PA24
#define UDP_PULLUP AT91C_PIO_PA25
/* ATMEL IDs */
#define USB_VENDOR_ID 0x03EB
+70
View File
@@ -0,0 +1,70 @@
#ifndef _CONTEXT_H_
#define _CONTEXT_H_
#include <stdint.h>
#include "rtos/spinlock.h"
struct register_context {
uint32_t r0;
uint32_t r1;
uint32_t pc;
uint32_t cpsr;
uint32_t r2;
uint32_t r3;
uint32_t r4;
uint32_t r5;
uint32_t r6;
uint32_t r7;
uint32_t r8;
uint32_t r9;
uint32_t r10;
uint32_t r11;
uint32_t r12;
uint32_t sp;
uint32_t r14;
};
enum ctx_state {
CONTEXT_NULL = 0,
CONTEXT_RUNNING = 1,
CONTEXT_READY = 2,
CONTEXT_INTERRUPTED = 3,
CONTEXT_SLEEP = 4,
CONTEXT_SLEEP_QUEUE = 5,
};
struct context {
/* regs *MUST* be first in struct! */
struct register_context regs;
void *stack;
uint8_t state;
uint8_t priority;
/* next pointers for run & sleep queues */
struct context *run_queue;
struct context *sleep_queue;
};
extern struct context *current_context;
void isr_context_yield(void);
void context_yield(void);
uint8_t isr_context_wait(struct spinlock *lock);
uint8_t context_wait(struct spinlock *lock);
uint8_t context_wait_queue(struct spinlock *lock, struct context **queue);
uint8_t context_wait_pri_queue(struct spinlock *lock, struct context **queue);
void isr_context_signal(struct context *c);
void context_signal(struct context *c);
uint32_t context_signal_queue(struct context **queue);
void isr_context_interrupt(struct context *c);
void context_interrupt(struct context *c);
uint32_t context_interrupt_queue(struct context *c, struct context **queue);
struct context * create_ctx(uint32_t stacksize, uint8_t priority, void (* code)(void *arg), void *arg);
void init_context(void);
#endif /* _CONTEXT_H_ */
+22
View File
@@ -0,0 +1,22 @@
#ifndef _SEMSPHORE_H_
#define _SEMSPHORE_H_
#include <stdint.h>
#include "rtos/context.h"
#include "rtos/spinlock.h"
struct semaphore {
struct context *sleep_queue;
struct spinlock lock;
int32_t count;
};
uint8_t sem_wait(struct semaphore *sem);
void sem_post(struct semaphore *sem);
void sem_interrupt(struct semaphore *sem, struct context *c);
int32_t sem_get_count(struct semaphore *sem);
void sem_init(struct semaphore *sem, int32_t count);
#endif /* _SEMSPHORE_H_ */
+19
View File
@@ -0,0 +1,19 @@
#ifndef _SPINLOCK_H_
#define _SPINLOCK_H_
#include <stdint.h>
struct spinlock {
uint8_t locked;
uint8_t priority_unlocked;
};
void isr_spinlock_lock(struct spinlock *lock);
void spinlock_lock(struct spinlock *lock);
void isr_spinlock_unlock(struct spinlock *lock);
void spinlock_unlock(struct spinlock *lock);
void spinlock_init(struct spinlock *lock);
#endif /* _SPINLOCK_H_ */
+8 -24
View File
@@ -1,28 +1,15 @@
MEMORY
{
FLASH(rx) : ORIGIN = 0x00100000, LENGTH = 256k
CODE (rx) : ORIGIN = 0x0013C000, LENGTH = 16k
DATA (rwx) : ORIGIN = 0x00000000, LENGTH = 64k
CODE (rx) : ORIGIN = 0x00100000, LENGTH = 256k
DATA (rwx) : ORIGIN = 0x00200000, LENGTH = 64k
}
SECTIONS
{
. = ORIGIN(FLASH);
.init_dummy : {
*(.init_dummy)
} >FLASH =0
. = ORIGIN(CODE);
.init : {
*(.init)
__text_copy_start__ = .;
} >CODE =0
.text : AT (LOADADDR(.init) + SIZEOF(.init)) {
*at91_init0.o(*.text)
*at91_init1.o(*.text)
.text : {
*at91_init0.o (.text)
*at91_init1.o (.text)
*(.text)
*(.glue_7t)
*(.glue_7)
@@ -41,13 +28,10 @@ SECTIONS
*(.rodata)
*(.rodata.*)
__data_copy_start__ = (__text_copy_start__ + (__text_end__ - __text_start__));
} >DATA =0
__text_end__ = .;
} >CODE =0
__text_start__ = ADDR(.text);
__text_end__ = ADDR(.text) + SIZEOF(.text);
.data : AT (LOADADDR(.text) + SIZEOF(.text)) {
.data : AT (ADDR(.text) + SIZEOF(.text)) {
*(.data)
} >DATA =0
+71 -74
View File
@@ -1,74 +1,71 @@
/***************************************************************************
* Copyright (C) 01/2008 by Olaf Rempel *
* razzor@kopf-tisch.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; version 2 of the License *
* *
* 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 General Public License for more details. *
* *
* You should have received a copy of the GNU 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. *
***************************************************************************/
#include "AT91SAM7S256.h"
#include "at91_sysc.h"
#include "at91_dbgu.h"
#include "at91_pitc.h"
#include "at91_udp.h"
#include "at91_twi.h"
#include "memalloc.h"
#include "board.h"
#include <stdio.h>
#include <string.h>
static uint32_t pitc_test(struct pitc_timer *timer)
{
static uint32_t i;
*AT91C_PIOA_SODR = i;
i = i ^ LED_GREEN;
*AT91C_PIOA_CODR = i;
return PITC_RESTART_TIMER;
}
static struct pitc_timer pitc_test_timer = {
.interval = 10,
.func = &pitc_test,
};
int main(void)
{
/* LED outputs */
*AT91C_PIOA_PER = LED_GREEN | LED_ORANGE;
*AT91C_PIOA_OER = LED_GREEN | LED_ORANGE;
/* needed for dbgu */
at91_sysc_init();
at91_dbgu_init();
at91_dbgu_puts("==========================================================\n\rGood morning Dave\n\r");
/* timer */
at91_pitc_init();
/* twi */
at91_twi_init();
at91_twi_test();
/* usb */
at91_udp_init();
printf("static alloc: %5ld bytes\n\r", static_alloc_used());
pitc_schedule_timer(&pitc_test_timer);
while (1);
}
/***************************************************************************
* Copyright (C) 01/2008 by Olaf Rempel *
* razzor@kopf-tisch.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; version 2 of the License *
* *
* 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 General Public License for more details. *
* *
* You should have received a copy of the GNU 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. *
***************************************************************************/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "AT91SAM7S256.h"
#include "board.h"
#include "at91_sysc.h"
#include "at91_dbgu.h"
#include "at91_pio.h"
#include "rtos/context.h"
#include "rtos/semaphore.h"
static struct semaphore sem;
void testisr(uint32_t status, uint32_t input)
{
printf("testisr: sem_post()\n\r");
sem_post(&sem);
}
void testfunc(void *p)
{
while (1) {
printf("testfunc(%ld): sem_wait()\n\r", (uint32_t)p);
sem_wait(&sem);
}
}
int main(void)
{
/* LED outputs */
*AT91C_PIOA_PER = LED_GREEN | LED_ORANGE;
*AT91C_PIOA_OER = LED_GREEN | LED_ORANGE;
at91_sysc_init();
at91_dbgu_init();
at91_dbgu_puts("==========================================================\n\rGood morning Dave\n\r");
at91_pio_init();
struct context *text_ctx1 = create_ctx(512, 0x80, testfunc, (void *)1);
printf("test_ctx(1)=%p\n\r", text_ctx1);
struct context *text_ctx2 = create_ctx(512, 0x80, testfunc, (void *)2);
printf("test_ctx(2)=%p\n\r", text_ctx2);
sem_init(&sem, 0);
init_context();
}
PIO_PINCHANGE_ISR(TAST1, testisr);
+2 -2
View File
@@ -44,7 +44,7 @@ static void dbgu_isr(uint32_t status)
}
*/
if (status & AT91C_US_TXBUFE)
if (fifo_txpdc(txfifo, AT91C_BASE_PDC_DBGU, 32) == 0)
if (fifo_txpdc(txfifo, AT91C_BASE_PDC_DBGU, 16) == 0)
*AT91C_DBGU_IDR = AT91C_US_TXBUFE;
}
@@ -59,7 +59,7 @@ void at91_dbgu_init(void)
dbgu->DBGU_MR = AT91C_US_PAR_NONE | AT91C_US_CHMODE_NORMAL;
dbgu->DBGU_CR = AT91C_US_RXEN | AT91C_US_TXEN | AT91C_US_RSTSTA;
txfifo = fifo_alloc(16384);
txfifo = fifo_alloc(1024);
/* enable TX PDC */
dbgu->DBGU_PTCR = AT91C_PDC_TXTEN;
+1
View File
@@ -161,6 +161,7 @@ void at91_abt_handler(uint32_t cpsr, uint32_t *registers)
dbgu_putchar('\n');
}
__attribute__((naked)) void ABT_Handler(void)
{
asm volatile (
+35 -45
View File
@@ -16,64 +16,54 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <stdint.h>
#include <stdio.h>
#include "AT91SAM7S256.h"
#include "at91_pitc.h"
#include "at91_sysc.h"
#include "board.h"
#include "at91_pio.h"
/* PIV is 20bit -> min. 3Hz @48MHz MCK */
#define HZ_TO_PIV(HZ) (MCK / (16 * HZ))
/* extern symbols, defined in ldscript */
extern struct pio_pinchange_isr _pio_isr_table;
extern struct pio_pinchange_isr _pio_isr_table_end;
static LIST_HEAD(timer_list);
volatile static uint32_t pitc_ticks;
void pitc_schedule_timer(struct pitc_timer *timer)
static void pio_isr(void)
{
timer->nextrun = timer->interval + pitc_ticks;
uint32_t status = *AT91C_PIOA_ISR;
uint32_t input = *AT91C_PIOA_PDSR;
struct pitc_timer *search;
list_for_each_entry(search, &timer_list, list)
if (search->nextrun > timer->nextrun)
break;
list_add_tail(&timer->list, &search->list);
struct pio_pinchange_isr *isr;
for (isr = &_pio_isr_table; isr < &_pio_isr_table_end; isr++)
if (isr->mask & status)
isr->func(status, input);
}
static void pitc_isr(uint32_t status)
void pio_trigger_isr(uint32_t mask)
{
/* get Ticks and clear interrupt */
pitc_ticks += (*AT91C_PITC_PIVR & AT91C_PITC_PICNT) >> 20;
uint32_t input = *AT91C_PIOA_PDSR;
struct pitc_timer *search, *tmp;
list_for_each_entry_safe(search, tmp, &timer_list, list) {
/* if this entry not scheduled yet, abort search */
if (pitc_ticks < search->nextrun)
break;
/* remove from list */
list_del(&search->list);
/* exec handler */
if (search->func(search) == PITC_REMOVE_TIMER) {
/* one-shot timer, mark as completed */
search->nextrun = 0x00;
continue;
}
/* interval timer, reschedule it */
pitc_schedule_timer(search);
}
struct pio_pinchange_isr *isr;
for (isr = &_pio_isr_table; isr < &_pio_isr_table_end; isr++)
if (isr->mask & mask)
isr->func(mask, input);
}
uint32_t pitc_get_ticks(void)
void at91_pio_init(void)
{
return pitc_ticks;
}
/* enable PIO clock */
*AT91C_PMC_PCER = (1 << AT91C_ID_PIOA);
void at91_pitc_init(void)
{
sysc_register_isr(AT91_SYSIRQ_PIT, &pitc_isr);
/* enable pinchange interrupts */
struct pio_pinchange_isr *isr;
for (isr = &_pio_isr_table; isr < &_pio_isr_table_end; isr++)
*AT91C_PIOA_IER = isr->mask;
*AT91C_PITC_PIMR = (AT91C_PITC_PIV & HZ_TO_PIV(100)) |
AT91C_PITC_PITEN |
AT91C_PITC_PITIEN;
/* dummy read to clear interrupts */
uint32_t dummy = *AT91C_PIOA_ISR;
dummy = dummy;
/* low priority, level triggered, own vector */
AT91S_AIC *aic = AT91C_BASE_AIC;
aic->AIC_SMR[AT91C_ID_PIOA] = IRQPRIO_PIOA | AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL;
aic->AIC_SVR[AT91C_ID_PIOA] = (uint32_t)pio_isr;
aic->AIC_IECR = (1 << AT91C_ID_PIOA);
}
-262
View File
@@ -1,262 +0,0 @@
/***************************************************************************
* Copyright (C) 02/2008 by Olaf Rempel *
* razzor@kopf-tisch.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; version 2 of the License *
* *
* 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 General Public License for more details. *
* *
* You should have received a copy of the GNU 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. *
***************************************************************************/
#include <stdint.h>
#include <stdio.h>
#include "AT91SAM7S256.h"
#include "board.h"
#include "at91_twi.h"
#include "at91_pio.h"
/*
* undocumented TWI_SR flags, at least OVRE seems to be present on a sam7s256
* taken from linux-2.6.24/include/asm-arm/arch-at91/at91_twi.h
*/
#define AT91_TWI_OVRE (1<<6) /* Overrun */
#define AT91_TWI_UNRE (1<<7) /* Underrun */
/*
* while ((cldiv = ((MCK / (2 * TWI)) -3) / (1 << ckdiv)) > 255)
* ckdiv++ ;
*
* works for TWI >= 100kHz
*/
#define TWI_CLK(x) (((MCK / (2 * (x))) -3)<<8 | ((MCK / (2 * (x))) -3))
enum twi_states {
TWI_IDLE = 0x00,
TWI_ERROR,
TWI_GENERIC_CMD = 0x10,
TWI_BLMC_UPDATE = 0x20,
};
static volatile uint32_t twi_state = TWI_IDLE;
static uint8_t *twi_data;
static uint32_t twi_size;
static uint32_t twi_count;
static void twi_isr(void)
{
/* get status */
uint32_t status = *AT91C_TWI_SR;
status &= *AT91C_TWI_IMR;
/* NACK - disable all interrupts and go to state TWI_ERROR */
if (status & AT91C_TWI_NACK) {
*AT91C_TWI_IDR = AT91C_TWI_TXCOMP | AT91C_TWI_RXRDY | AT91C_TWI_TXRDY | AT91C_TWI_NACK;
twi_state = TWI_ERROR;
// TODO: TWI_BLMC_UPDATE?
return;
}
/* tx register ready for new data */
if (status & AT91C_TWI_TXRDY) {
if (twi_count != twi_size) {
/* feed next byte */
*AT91C_TWI_THR = twi_data[twi_count++];
} else {
/* wait for TXCOMP */
*AT91C_TWI_IDR = AT91C_TWI_RXRDY | AT91C_TWI_TXRDY;
*AT91C_TWI_IER = AT91C_TWI_TXCOMP | AT91C_TWI_NACK;
}
}
/* rx register has data */
if (status & AT91C_TWI_RXRDY) {
/* get data */
twi_data[twi_count++] = *AT91C_TWI_RHR;
/* transfer complete? */
if (twi_count == twi_size) {
/* send STOP and wait for TXCOMP */
*AT91C_TWI_CR = AT91C_TWI_STOP;
*AT91C_TWI_IDR = AT91C_TWI_TXRDY;
*AT91C_TWI_IER = AT91C_TWI_TXCOMP | AT91C_TWI_NACK;
}
}
/* transfer really complete? */
if (status & AT91C_TWI_TXCOMP) {
/* are we doing a blmc update? */
if (twi_state == TWI_BLMC_UPDATE) {
uint32_t addr = (*AT91C_TWI_MMR >> 16) & 0x7F;
if (addr != TWI_ADDR_BL4) {
/* increase address */
*AT91C_TWI_MMR += (1<<16);
/* send next value to next blmc */
*AT91C_TWI_THR = *twi_data++;
} else {
// TODO:
}
} else {
*AT91C_TWI_IDR = AT91C_TWI_TXCOMP | AT91C_TWI_RXRDY | AT91C_TWI_TXRDY | AT91C_TWI_NACK;
twi_state = TWI_IDLE;
}
}
}
uint32_t twi_setpwm(uint8_t *values)
{
if (twi_state != TWI_IDLE)
return 1;
twi_state = TWI_BLMC_UPDATE;
twi_data = values; /* data is not copied! */
twi_size = 0;
twi_count = 0;
*AT91C_TWI_MMR = (TWI_ADDR_BL1 << 16) | AT91C_TWI_IADRSZ_1_BYTE;
*AT91C_TWI_IADR = CMD_SET_PWM;
*AT91C_TWI_THR = *twi_data++;
*AT91C_TWI_IER = AT91C_TWI_TXRDY | AT91C_TWI_NACK;
return 0;
}
uint32_t twi_cmd(uint8_t addr, struct blmc_cmd *cmd)
{
if (twi_state != TWI_IDLE)
return 1;
/* TODO: locking needed? */
twi_state = TWI_GENERIC_CMD;
/* read transfer, or write transfer with payload */
if (cmd->mode & BLMC_CMD_READ || cmd->size != 0) {
/* set address, direction, argument count and command bytes */
*AT91C_TWI_MMR = (addr << 16) | (cmd->mode & 0xFF) << 8;
*AT91C_TWI_IADR = cmd->cmd;
/* write transfer without payload */
} else {
/* use one cmd byte as payload (needed to start transfer) */
cmd->mode--;
*AT91C_TWI_MMR = (addr << 16) | (cmd->mode & 0xFF) << 8;
*AT91C_TWI_IADR = (cmd->cmd) >> 8;
}
/* isr needs data & size parameters */
twi_data = cmd->data;
twi_size = cmd->size;
twi_count = 0;
if (cmd->mode & BLMC_CMD_READ) {
*AT91C_TWI_CR = AT91C_TWI_START;
*AT91C_TWI_IER = AT91C_TWI_RXRDY | AT91C_TWI_NACK;
} else {
*AT91C_TWI_THR = (twi_size != 0) ? cmd->data[twi_count++] : (cmd->cmd & 0xFF);
*AT91C_TWI_IER = AT91C_TWI_TXRDY | AT91C_TWI_NACK;
}
/*
* wait for end
* TODO: locking needed?
* TODO: timeout?
*/
while (twi_state != TWI_IDLE && twi_state != TWI_ERROR);
if (twi_state != TWI_IDLE) {
twi_state = TWI_IDLE;
return 1;
}
return 0;
}
void at91_twi_test(void)
{
uint32_t i;
for (i = TWI_ADDR_BL1; i <= TWI_ADDR_BL4; i++) {
struct blmc_cmd cmd = {
.cmd = CMD_BOOT_LOADER,
.mode = BLMC_CMD_WRITE | BLMC_CMD_0_ARG,
};
uint32_t ret = twi_cmd(i, &cmd);
printf("twi[0x%02lx](%ld) ", i, ret);
volatile uint32_t x;
for (x = 0; x < 200000; x++);
uint8_t buf[16];
cmd.cmd = CMD_GET_INFO,
cmd.mode = BLMC_CMD_READ | BLMC_CMD_0_ARG,
cmd.size = sizeof(buf),
cmd.data = buf,
ret = twi_cmd(i, &cmd);
printf("boot(%ld):'%s' ", ret, buf);
cmd.cmd = CMD_GET_SIGNATURE;
cmd.size = 4;
ret = twi_cmd(i, &cmd);
printf("sig(%ld):0x%02x%02x%02x\n\r", ret, buf[0], buf[1], buf[2]);
/*
cmd.cmd = CMD_BOOT_APPLICATION;
cmd.mode = BLMC_CMD_WRITE | BLMC_CMD_0_ARG;
cmd.size = 0;
ret = twi_cmd(i, &cmd);
for (x = 0; x < 400000; x++);
cmd.cmd = CMD_GET_INFO,
cmd.mode = BLMC_CMD_READ | BLMC_CMD_0_ARG,
cmd.size = sizeof(buf),
cmd.data = buf,
ret = twi_cmd(i, &cmd);
printf("app(%ld):'%s'\n\r", ret, buf);
*/
}
}
void at91_twi_init(void)
{
/* enable Clock */
*AT91C_PMC_PCER = (1 << AT91C_ID_TWI);
/* SDA & SCL from Peripheral A, Open Drain, no Pullup */
AT91S_PIO *pio = AT91C_BASE_PIOA;
/* do a software reset (bus not connected) */
*AT91C_TWI_CR = AT91C_TWI_SWRST;
pio->PIO_MDER = AT91C_PA3_TWD | AT91C_PA4_TWCK;
pio->PIO_PPUDR = AT91C_PA3_TWD | AT91C_PA4_TWCK;
pio->PIO_ASR = AT91C_PA3_TWD | AT91C_PA4_TWCK;
pio->PIO_PDR = AT91C_PA3_TWD | AT91C_PA4_TWCK;
/* set TWI Clock */
*AT91C_TWI_CWGR = TWI_CLK(400000); //| (5<<16);
/* disable all (known) interrupts */
*AT91C_TWI_IDR = AT91C_TWI_TXCOMP | AT91C_TWI_RXRDY | AT91C_TWI_TXRDY | AT91C_TWI_NACK;
/* level triggered, own vector */
AT91S_AIC *aic = AT91C_BASE_AIC;
aic->AIC_SMR[AT91C_ID_TWI] = IRQPRIO_TWI | AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL;
aic->AIC_SVR[AT91C_ID_TWI] = (uint32_t)twi_isr;
aic->AIC_IECR = (1 << AT91C_ID_TWI);
/* enable teh monster */
*AT91C_TWI_CR = AT91C_TWI_MSEN;
}
-637
View File
@@ -1,637 +0,0 @@
/***************************************************************************
* Copyright (C) 01/2008 by Olaf Rempel *
* razzor@kopf-tisch.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; version 2 of the License *
* *
* 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 General Public License for more details. *
* *
* You should have received a copy of the GNU 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. *
***************************************************************************/
#include <stdio.h>
#include "AT91SAM7S256.h"
#include "board.h"
#include "fifo.h"
#include "usb_ch9.h"
#include "usb_dfu.h"
/* from dfu.c */
extern void ep0_handle_dfu(struct usb_ctrlrequest *req);
#define csr_clear_flags(csr, flags) \
while ((csr) & (flags)) \
(csr) &= ~(flags);
#define csr_set_flags(csr, flags) \
while (((csr) & (flags)) != (flags)) \
(csr) |= (flags);
struct ep_transfer {
uint16_t length;
uint16_t curpos;
char *data;
void (*complete_cb)(void);
};
struct ep_ctx {
uint16_t maxpktsize;
uint16_t flags;
union {
struct ep_transfer *transfer;
struct fifo *fifo;
};
};
#define CTX_TRANSFER 0x01 /* ctx use ep_transfer struct */
#define CTX_FIFO 0x02 /* ctx use fifo */
#define CTX_IN 0x04 /* write to the host */
#define CTX_OUT 0x08 /* read from the host */
#define CTX_RXBANK0 0x10
#define CTX_RXBANK1 0x20
static struct ep_transfer ep0_transfer;
static struct ep_ctx ep_ctx[4];
static uint16_t current_address;
static uint16_t current_config;
static uint16_t current_interface;
static const struct usb_device_descriptor dev_descriptor = {
.bLength = sizeof(struct usb_device_descriptor),
.bDescriptorType = USB_DT_DEVICE,
.bcdUSB = 0x0110,
.bMaxPacketSize0 = 8,
.idVendor = USB_VENDOR_ID,
.idProduct = USB_PRODUCT_ID +1,
.bcdDevice = 0x0001,
.iProduct = 0x01,
.bNumConfigurations = 1,
};
struct my_config {
struct usb_config_descriptor cfg;
struct usb_interface_descriptor iface0;
struct usb_interface_descriptor iface1;
struct usb_interface_descriptor iface2;
struct usb_interface_descriptor iface3;
struct usb_interface_descriptor iface4;
struct usb_interface_descriptor iface5;
struct usb_dfu_descriptor dfu;
} __attribute__ ((packed));
static const struct my_config cfg_descriptor = {
.cfg = {
.bLength = sizeof(struct usb_config_descriptor),
.bDescriptorType = USB_DT_CONFIG,
.wTotalLength = sizeof(struct my_config),
.bNumInterfaces = 6,
.bConfigurationValue = 1,
.iConfiguration = 0x02,
.bmAttributes = USB_CONFIG_ATT_SELFPOWER | USB_CONFIG_ATT_WAKEUP,
.bMaxPower = 50,
},
.iface0 = {
.bLength = sizeof(struct usb_interface_descriptor),
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 0,
.bInterfaceClass = USB_CLASS_APP_SPEC,
.bInterfaceSubClass = 0x01, /* DFU */
.bInterfaceProtocol = 0x02,
.iInterface = 0x03,
},
.iface1 = {
.bLength = sizeof(struct usb_interface_descriptor),
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 1,
.bInterfaceClass = USB_CLASS_APP_SPEC,
.bInterfaceSubClass = 0x01, /* DFU */
.bInterfaceProtocol = 0x02,
.iInterface = 0x04,
},
.iface2 = {
.bLength = sizeof(struct usb_interface_descriptor),
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 2,
.bInterfaceClass = USB_CLASS_APP_SPEC,
.bInterfaceSubClass = 0x01, /* DFU */
.bInterfaceProtocol = 0x02,
.iInterface = 0x05,
},
.iface3 = {
.bLength = sizeof(struct usb_interface_descriptor),
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 3,
.bInterfaceClass = USB_CLASS_APP_SPEC,
.bInterfaceSubClass = 0x01, /* DFU */
.bInterfaceProtocol = 0x02,
.iInterface = 0x06,
},
.iface4 = {
.bLength = sizeof(struct usb_interface_descriptor),
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 4,
.bInterfaceClass = USB_CLASS_APP_SPEC,
.bInterfaceSubClass = 0x01, /* DFU */
.bInterfaceProtocol = 0x02,
.iInterface = 0x07,
},
.iface5 = {
.bLength = sizeof(struct usb_interface_descriptor),
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 5,
.bInterfaceClass = USB_CLASS_APP_SPEC,
.bInterfaceSubClass = 0x01, /* DFU */
.bInterfaceProtocol = 0x02,
.iInterface = 0x08,
},
.dfu = {
.bLength = sizeof(struct usb_dfu_descriptor),
.bDescriptorType = USB_TYPE_DFU,
.bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_CAN_UPLOAD | USB_DFU_MANIFEST_TOL | USB_DFU_WILL_DETACH,
.wDetachTimeOut = 0xff00,
.wTransferSize = AT91C_IFLASH_PAGE_SIZE,
.bcdDFUVersion = 0x0101,
},
};
static const struct usb_string_descriptor usb_string0 = {
/* String 0 - Language */
.bLength = sizeof(struct usb_string_descriptor) + 1 * sizeof(uint16_t),
.bDescriptorType = USB_DT_STRING,
.wData = { 0x0409 /* English */ },
};
static const struct usb_string_descriptor usb_string1 = {
/* String 1 "sam7fc" */
.bLength = sizeof(struct usb_string_descriptor) + 6 * sizeof(uint16_t),
.bDescriptorType = USB_DT_STRING,
.wData = {
0x0073, 0x0061, 0x006d, 0x0037, 0x0066, 0x0063,
},
};
static const struct usb_string_descriptor usb_string2 = {
/* String 2 "sam7fc-dfu" */
.bLength = sizeof(struct usb_string_descriptor) + 10 * sizeof(uint16_t),
.bDescriptorType = USB_DT_STRING,
.wData = {
0x0073, 0x0061, 0x006d, 0x0037, 0x0066, 0x0063, 0x002d, 0x0064,
0x0066, 0x0075,
},
};
static const struct usb_string_descriptor usb_string3 = {
/* String 3 "sam7fc-flash" */
.bLength = sizeof(struct usb_string_descriptor) + 12 * sizeof(uint16_t),
.bDescriptorType = USB_DT_STRING,
.wData = {
0x0073, 0x0061, 0x006d, 0x0037, 0x0066, 0x0063, 0x002d, 0x0066,
0x006c, 0x0061, 0x0073, 0x0068,
},
};
static const struct usb_string_descriptor usb_string4 = {
/* String 4 "sam7fc-eeprom" */
.bLength = sizeof(struct usb_string_descriptor) + 13 * sizeof(uint16_t),
.bDescriptorType = USB_DT_STRING,
.wData = {
0x0073, 0x0061, 0x006d, 0x0037, 0x0066, 0x0063, 0x002d, 0x0065,
0x0065, 0x0070, 0x0072, 0x006f, 0x006d,
},
};
static const struct usb_string_descriptor usb_string5 = {
/* String 5 "blctrl1-flash" */
.bLength = sizeof(struct usb_string_descriptor) + 13 * sizeof(uint16_t),
.bDescriptorType = USB_DT_STRING,
.wData = {
0x0062, 0x006c, 0x0063, 0x0074, 0x0072, 0x006c, 0x0031, 0x002d,
0x0066, 0x006c, 0x0061, 0x0073, 0x0068,
},
};
static const struct usb_string_descriptor usb_string6 = {
/* String 6 "blctrl2-flash" */
.bLength = sizeof(struct usb_string_descriptor) + 13 * sizeof(uint16_t),
.bDescriptorType = USB_DT_STRING,
.wData = {
0x0062, 0x006c, 0x0063, 0x0074, 0x0072, 0x006c, 0x0032, 0x002d,
0x0066, 0x006c, 0x0061, 0x0073, 0x0068,
},
};
static const struct usb_string_descriptor usb_string7 = {
/* String 7 "blctrl3-flash" */
.bLength = sizeof(struct usb_string_descriptor) + 13 * sizeof(uint16_t),
.bDescriptorType = USB_DT_STRING,
.wData = {
0x0062, 0x006c, 0x0063, 0x0074, 0x0072, 0x006c, 0x0033, 0x002d,
0x0066, 0x006c, 0x0061, 0x0073, 0x0068,
},
};
static const struct usb_string_descriptor usb_string8 = {
/* String 8 "blctrl4-flash" */
.bLength = sizeof(struct usb_string_descriptor) + 13 * sizeof(uint16_t),
.bDescriptorType = USB_DT_STRING,
.wData = {
0x0062, 0x006c, 0x0063, 0x0074, 0x0072, 0x006c, 0x0034, 0x002d,
0x0066, 0x006c, 0x0061, 0x0073, 0x0068,
},
};
static const struct usb_string_descriptor *usb_strings[] = {
&usb_string0, &usb_string1, &usb_string2, &usb_string3,
&usb_string4, &usb_string5, &usb_string6, &usb_string7,
&usb_string8,
};
void ep_transfer_send(uint32_t ep, char *data, uint32_t length,
void (*complete_cb)(void))
{
struct ep_ctx *ctx = &ep_ctx[ep];
// printf("ep_transfer_send(%ld) size=%ld(%d) flags=0x%x\n\r", ep, length, ctx->maxpktsize, ctx->flags);
if (!(ctx->flags & CTX_TRANSFER) || (ctx->flags & (CTX_IN | CTX_OUT)))
return;
/* from buffer to usb */
ctx->flags |= CTX_IN;
struct ep_transfer *transfer = ctx->transfer;
transfer->length = length;
transfer->curpos = 0;
transfer->data = data;
transfer->complete_cb = complete_cb;
uint32_t maxsize = ctx->maxpktsize;
/* get data from transfer */
while (transfer->curpos < transfer->length && maxsize--)
AT91C_UDP_FDR[ep] = transfer->data[transfer->curpos++];
/* trigger tx */
AT91C_UDP_CSR[ep] |= AT91C_UDP_TXPKTRDY;
}
void ep_transfer_receive(uint32_t ep, char *data, uint32_t length,
void (*complete_cb)(void))
{
struct ep_ctx *ctx = &ep_ctx[ep];
// printf("ep_transfer_receive(%ld) size=%ld flags=0x%x\n\r", ep, length, ctx->flags);
if (!(ctx->flags & CTX_TRANSFER) || (ctx->flags & (CTX_IN | CTX_OUT)))
return;
/* from usb to buffer */
ctx->flags |= CTX_OUT;
struct ep_transfer *transfer = ctx->transfer;
transfer->length = length;
transfer->curpos = 0;
transfer->data = data;
transfer->complete_cb = complete_cb;
}
/* stalls the endpoint */
void ep_send_stall(uint32_t ep)
{
printf("stall\n\r");
AT91C_UDP_CSR[ep] |= AT91C_UDP_FORCESTALL;
}
static void udp_configure_ep(const struct usb_endpoint_descriptor *desc)
{
/* get endpoint address, set Max Packet Size */
uint32_t ep = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
ep_ctx[ep].maxpktsize = desc->wMaxPacketSize;
/* get endpoint type (ctrl, iso, bulb, int) */
uint32_t eptype = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
if (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
eptype |= 0x04;
} else {
ep_ctx[ep].flags |= CTX_RXBANK0;
}
/* configure UDP endpoint and enable interrupt */
AT91C_UDP_CSR[ep] = AT91C_UDP_EPEDS | (eptype << 8);
*AT91C_UDP_IER = (1 << ep);
}
/*
* set local address
* (USB_REQ_SET_ADDRESS callback)
*/
static void udp_txcb_setaddress(void)
{
printf("usb address: %d\n\r", current_address);
*AT91C_UDP_FADDR = (AT91C_UDP_FEN | current_address);
*AT91C_UDP_GLBSTATE = AT91C_UDP_FADDEN;
}
/*
* configure endpoints
* (USB_REQ_SET_CONFIGURATION callback)
*/
static void udp_txcb_setconfig(void)
{
printf("usb configuration: %d\n\r", current_config);
/* set UDP to "configured" */
*AT91C_UDP_GLBSTATE = AT91C_UDP_CONFG;
}
static void udp_txcb_setinterface(void)
{
printf("claim interface %d\n\r", current_interface);
}
static void ep_handle_ctrlrequest(struct usb_ctrlrequest *req)
{
// printf("typ:0x%02x req:0x%02x val:0x%04x idx:0x%04x len:0x%04x\n\r",
// req->bRequestType, req->bRequest, req->wValue, req->wIndex, req->wLength);
switch (req->bRequestType & (USB_TYPE_MASK | USB_RECIP_MASK)) {
case (USB_TYPE_STANDARD | USB_RECIP_DEVICE): /* 0x00/0x80 */
switch (req->bRequest) {
case USB_REQ_SET_ADDRESS: /* 0x05 */
current_address = req->wValue;
ep_transfer_send(0, NULL, 0, udp_txcb_setaddress);
break;
case USB_REQ_GET_DESCRIPTOR: /* 0x06 */
switch (req->wValue >> 8) {
case USB_DT_DEVICE: /* 0x01 */
ep_transfer_send(0, (char *)&dev_descriptor,
MIN(sizeof(dev_descriptor), req->wLength),
NULL);
break;
case USB_DT_CONFIG: /* 0x02 */
ep_transfer_send(0, (char *)&cfg_descriptor,
MIN(sizeof(cfg_descriptor), req->wLength),
NULL);
break;
case USB_DT_STRING: /* 0x03 */
;
uint8_t index = req->wValue & 0xFF;
if (index < ARRAY_SIZE(usb_strings)) {
ep_transfer_send(0, (char *)usb_strings[index],
MIN(usb_strings[index]->bLength, req->wLength),
NULL);
} else {
ep_send_stall(0);
}
break;
case USB_DT_CS_DEVICE: /* 0x21 */
ep_transfer_send(0, (char *)&cfg_descriptor.dfu,
MIN(sizeof(cfg_descriptor.dfu), req->wLength),
NULL);
break;
default:
ep_send_stall(0);
break;
}
break;
case USB_REQ_SET_CONFIGURATION: /* 0x09 */
current_config = req->wValue;
ep_transfer_send(0, NULL, 0, udp_txcb_setconfig);
break;
default:
ep_send_stall(0);
break;
}
break;
case (USB_TYPE_STANDARD | USB_RECIP_INTERFACE): /* 0x01/0x81 */
switch (req->bRequest) {
case USB_REQ_SET_INTERFACE: /* 0x0b */
current_interface = req->wValue;
ep_transfer_send(0, NULL, 0, udp_txcb_setinterface);
break;
default:
ep_send_stall(0);
break;
}
break;
case (USB_TYPE_CLASS | USB_RECIP_INTERFACE): /* 0x21/0xA1 */
ep0_handle_dfu(req);
break;
default:
ep_send_stall(0);
break;
}
}
static void udp_handle_ep(uint32_t ep)
{
/* endpoint enabled? */
AT91_REG *csr = &AT91C_UDP_CSR[ep];
if (!(*csr & AT91C_UDP_EPEDS))
return;
/* clear STALLSENT interrupt */
if (*csr & AT91C_UDP_STALLSENT)
csr_clear_flags(*csr, (AT91C_UDP_STALLSENT | AT91C_UDP_FORCESTALL));
/* ctrl request packet? */
if (*csr & AT91C_UDP_RXSETUP) {
struct usb_ctrlrequest req;
uint8_t *p;
for (p = (uint8_t *)&req; p < (uint8_t *)(&req +1); p++)
*p = AT91C_UDP_FDR[ep];
/* ack bank0 *now */
if (*csr & AT91C_UDP_RX_DATA_BK0)
csr_clear_flags(*csr, AT91C_UDP_RX_DATA_BK0);
/* set data phase transfer direction */
if (req.bRequestType & USB_DIR_IN)
*csr |= AT91C_UDP_DIR;
/* clear interrupt - *MUST* use csr_clear_flags() here */
csr_clear_flags(*csr, AT91C_UDP_RXSETUP);
ep_handle_ctrlrequest(&req);
}
void (* transfer_cb)(void) = NULL;
/* transmit complete? */
if (*csr & AT91C_UDP_TXCOMP) {
struct ep_ctx *ctx = &ep_ctx[ep];
if (ctx->flags & CTX_FIFO) {
/* get data from fifo */
if (fifo_txudp(ctx->fifo, ep, ctx->maxpktsize))
AT91C_UDP_CSR[ep] |= AT91C_UDP_TXPKTRDY;
} else if ((ctx->flags & (CTX_TRANSFER | CTX_IN)) == (CTX_TRANSFER | CTX_IN)) {
/* transfer not complete */
struct ep_transfer *transfer = ctx->transfer;
if (transfer->length != transfer->curpos) {
uint32_t maxsize = ctx->maxpktsize;
/* get data from transfer */
while (transfer->curpos < transfer->length && maxsize--)
AT91C_UDP_FDR[ep] = transfer->data[transfer->curpos++];
/* trigger tx */
AT91C_UDP_CSR[ep] |= AT91C_UDP_TXPKTRDY;
/* transfer complete, execute callback */
} else {
ctx->flags &= ~CTX_IN;
transfer_cb = transfer->complete_cb;
}
}
/* clear interrupt */
*csr &= ~(AT91C_UDP_TXCOMP);
}
/* data ready to read? */
if (*csr & (AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1)) {
struct ep_ctx *ctx = &ep_ctx[ep];
uint16_t len = (*csr & AT91C_UDP_RXBYTECNT) >> 16;
// TODO: only ep0 status OUT?
if (!len && (ctx->flags & CTX_TRANSFER)) {
ctx->flags &= ~(CTX_OUT | CTX_IN);
ctx->transfer->length = 0;
ctx->transfer->curpos = 0;
}
if (ctx->flags & CTX_FIFO) {
fifo_rxudp(ctx->fifo, ep, len);
} else if ((ctx->flags & (CTX_TRANSFER | CTX_OUT)) == (CTX_TRANSFER | CTX_OUT)) {
/* transfer not complete */
struct ep_transfer *transfer = ctx->transfer;
if (transfer->length != transfer->curpos) {
/* get data from transfer */
while (transfer->curpos < transfer->length && len--)
transfer->data[transfer->curpos++] = AT91C_UDP_FDR[ep];
}
/* test again */
if (transfer->length == transfer->curpos) {
ctx->flags &= ~CTX_OUT;
transfer_cb = transfer->complete_cb;
}
}
if (ctx->flags & CTX_RXBANK0) {
if (*csr & AT91C_UDP_RX_DATA_BK0)
csr_clear_flags(*csr, AT91C_UDP_RX_DATA_BK0);
/* all but ep0 have ping pong buffers */
if (ep > 0)
ctx->flags = (ctx->flags & ~CTX_RXBANK0) | CTX_RXBANK1;
} else if (ctx->flags & CTX_RXBANK1) {
if (*csr & AT91C_UDP_RX_DATA_BK1)
csr_clear_flags(*csr, AT91C_UDP_RX_DATA_BK1);
ctx->flags = (ctx->flags & ~CTX_RXBANK1) | CTX_RXBANK0;
}
}
if (transfer_cb)
transfer_cb();
}
static void udp_isr(void)
{
uint32_t isr = *AT91C_UDP_ISR;
if (isr & AT91C_UDP_ENDBUSRES) {
printf("USB reset\n\r");
AT91S_UDP *udp = AT91C_BASE_UDP;
/* reset all endpoints */
udp->UDP_RSTEP = (AT91C_UDP_EP0 | AT91C_UDP_EP1 |
AT91C_UDP_EP2 | AT91C_UDP_EP3) ;
udp->UDP_RSTEP = 0;
/* init ep0 */
struct ep_ctx *ctx = &ep_ctx[0];
ctx->maxpktsize = 8;
ctx->flags = CTX_TRANSFER | CTX_RXBANK0;
ctx->transfer = &ep0_transfer;
ctx->transfer->length = 0;
ctx->transfer->curpos = 0;
/* Configure endpoint0 as Control EP */
udp->UDP_CSR[0] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_CTRL);
/* enable ep0 Interrupt, disable all others */
udp->UDP_IER = AT91C_UDP_EPINT0;
udp->UDP_IDR = AT91C_UDP_EPINT1 | AT91C_UDP_EPINT2 | AT91C_UDP_EPINT3 |
AT91C_UDP_RXSUSP | AT91C_UDP_RXRSM | AT91C_UDP_SOFINT |
AT91C_UDP_WAKEUP;
}
/* Handle Endpoint Interrupts */
if (isr & AT91C_UDP_EPINT0)
udp_handle_ep(0);
/* clear all unhandled interrupts */
*AT91C_UDP_ICR = isr & (AT91C_UDP_RXSUSP | AT91C_UDP_RXRSM |
AT91C_UDP_ENDBUSRES | AT91C_UDP_WAKEUP);
}
void at91_udp_init(void)
{
/* configure & disable Pullup, disable Pullup von VBUS */
AT91PS_PIO pio = AT91C_BASE_PIOA;
pio->PIO_CODR = UDP_PULLUP;
pio->PIO_PER = UDP_PULLUP;
pio->PIO_OER = UDP_PULLUP;
/* UDPCK (48MHz) = PLLCK / 2 */
*AT91C_CKGR_PLLR |= AT91C_CKGR_USBDIV_1;
/* enable UDP clocks */
*AT91C_PMC_SCER = AT91C_PMC_UDP;
*AT91C_PMC_PCER = (1 << AT91C_ID_UDP);
/* enable transmitter */
*AT91C_UDP_TXVC &= ~AT91C_UDP_TXVDIS;
/* clear & disable all UDP interrupts */
*AT91C_UDP_IDR = AT91C_UDP_EPINT0 | AT91C_UDP_EPINT1 | AT91C_UDP_EPINT2 |
AT91C_UDP_EPINT3 | AT91C_UDP_RXSUSP | AT91C_UDP_RXRSM |
AT91C_UDP_SOFINT | AT91C_UDP_WAKEUP;
*AT91C_UDP_ICR = AT91C_UDP_RXSUSP | AT91C_UDP_RXRSM | AT91C_UDP_SOFINT |
AT91C_UDP_ENDBUSRES | AT91C_UDP_WAKEUP ;
/* level triggered, own vector */
AT91S_AIC *aic = AT91C_BASE_AIC;
aic->AIC_SMR[AT91C_ID_UDP] = IRQPRIO_UDP | AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL;
aic->AIC_SVR[AT91C_ID_UDP] = (uint32_t)udp_isr;
aic->AIC_IECR = (1 << AT91C_ID_UDP);
/* usb connected -> enable pullup */
*AT91C_PIOA_CODR = UDP_PULLUP;
}
-472
View File
@@ -1,472 +0,0 @@
/***************************************************************************
* Copyright (C) 03/2008 by Olaf Rempel *
* razzor@kopf-tisch.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; version 2 of the License *
* *
* 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 General Public License for more details. *
* *
* You should have received a copy of the GNU 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. *
***************************************************************************/
#include <stdio.h>
#include "AT91SAM7S256.h"
#include "board.h"
#include "at91_twi.h"
#include "at91_udp.h"
#include "usb_ch9.h"
#include "usb_dfu.h"
static uint8_t dfu_state = DFU_STATE_dfuIDLE;
static uint8_t dfu_status = DFU_STATUS_OK;
#define RET_NOTHING 0
#define RET_STALL 1
#define RET_ZLP 2
static void handle_getstatus(void)
{
struct dfu_status dstat;
uint32_t fmr = *AT91C_MC_FSR;
switch (dfu_state) {
case DFU_STATE_dfuDNLOAD_SYNC:
case DFU_STATE_dfuDNBUSY:
if (fmr & AT91C_MC_PROGE) {
dfu_status = DFU_STATUS_errPROG;
dfu_state = DFU_STATE_dfuERROR;
} else if (fmr & AT91C_MC_LOCKE) {
dfu_status = DFU_STATUS_errWRITE;
dfu_state = DFU_STATE_dfuERROR;
} else if (fmr & AT91C_MC_FRDY) {
dfu_state = DFU_STATE_dfuDNLOAD_IDLE;
} else {
dfu_state = DFU_STATE_dfuDNBUSY;
}
break;
}
/* send status response */
dstat.bStatus = dfu_status;
dstat.bState = dfu_state;
dstat.iString = 0;
/* FIXME: set dstat.bwPollTimeout */
ep_transfer_send(0, (char *)&dstat, sizeof(dstat), NULL);
}
static void handle_getstate(void)
{
ep_transfer_send(0, (char *)&dfu_state, sizeof(dfu_state), NULL);
}
static uint16_t prog_iface;
static uint16_t prog_block;
static uint16_t prog_length;
static uint32_t prog_buf[AT91C_IFLASH_PAGE_SIZE /4];
static void handle_dnload_flash(void)
{
uint32_t *ptr = (uint32_t *)((uint8_t *)0x100000 + (prog_block * 0x100));
uint32_t i;
prog_length += 3;
for (i = 0; i < prog_length /4; i++)
*ptr++ = prog_buf[i];
if (!(*AT91C_MC_FSR & AT91C_MC_FRDY)) {
printf("flash not ready!\n\r");
return;
}
*AT91C_MC_FCR = (AT91C_MC_KEY & (0x5A << 24)) |
(((uint32_t)ptr - 0x100004) & AT91C_MC_PAGEN) |
AT91C_MC_FCMD_START_PROG;
ep_transfer_send(0, NULL, 0, NULL);
}
static void handle_dnload_eeprom(void)
{
/*
* TODO: write buf to onboard eeprom @ address
* inc. address
*/
ep_transfer_send(0, NULL, 0, NULL);
}
static void handle_dnload_blctrl(void)
{
uint32_t i2c_dev = TWI_ADDR_BL1 + (prog_iface -2);
uint8_t buf[66] = { 0x47, 0x11 };
uint32_t i;
for (i = 0; i < prog_length; i += 0x40) {
uint32_t j;
for (j = 0; j < 0x40; j++)
buf[j +2] = ((i + j) < prog_length) ? ((uint8_t *)prog_buf)[i + j] : 0xFF;
struct blmc_cmd cmd = {
.cmd = (CMD_WRITE_FLASH << 16) | (prog_block * 0x100 + i),
.mode = BLMC_CMD_WRITE | BLMC_CMD_2_ARG,
.size = sizeof(buf),
.data = buf,
};
printf("cmd:0x%08lx\n\r", cmd.cmd);
if (twi_cmd(i2c_dev, &cmd) != 0) {
printf("flash write failed\n\r");
dfu_state = DFU_STATE_dfuERROR;
dfu_status = DFU_STATUS_errUNKNOWN;
}
}
ep_transfer_send(0, NULL, 0, NULL);
}
static uint32_t handle_dnload(uint16_t iface, uint16_t block, uint16_t length)
{
printf("down:%x-%x-%x\n\r", iface, block, length);
if (length == 0) {
dfu_state = DFU_STATE_dfuMANIFEST_SYNC;
return RET_ZLP;
}
prog_iface = iface;
prog_block = block;
prog_length = length;
void *callback = NULL;
switch (iface) {
case 0: /* internal flash */
if (block >= 0x3C0) {
dfu_state = DFU_STATE_dfuERROR;
dfu_status = DFU_STATUS_errADDRESS;
return RET_STALL;
}
callback = handle_dnload_flash;
break;
case 1: /* onboard i2c-eeprom */
callback = handle_dnload_eeprom;
break;
case 2:
case 3:
case 4:
case 5: /* blctrl 1-4 */
callback = handle_dnload_blctrl;
break;
}
ep_transfer_receive(0, (char *)&prog_buf, length, callback);
return RET_NOTHING;
}
static void handle_upload(uint16_t iface, uint16_t block, uint16_t length)
{
printf("up:%x-%x-%x\n\r", iface, block, length);
char *ptr = (char *)&prog_buf;
switch (iface) {
case 0: /* internal flash */
if (block >= 0x400) {
length = 0;
break;
}
ptr = (char *)0x100000 + (block * 0x100);
break;
case 1: /* onboard i2c-eeprom */
if (block >= 0x80) {
length = 0;
break;
}
/* TODO */
break;
case 2:
case 3:
case 4:
case 5: /* blctrl 1-4 */
if (block >= 0x20) {
length = 0;
dfu_state = DFU_STATE_dfuIDLE;
break;
}
uint32_t i2c_dev = TWI_ADDR_BL1 + (iface -2);
struct blmc_cmd cmd = {
.cmd = (CMD_READ_FLASH << 16) | (block * 0x100),
.mode = BLMC_CMD_READ | BLMC_CMD_2_ARG,
.size = length,
.data = (uint8_t *)&prog_buf,
};
if (twi_cmd(i2c_dev, &cmd) != 0) {
length = 0;
dfu_state = DFU_STATE_dfuERROR;
dfu_status = DFU_STATUS_errUNKNOWN;
}
break;
}
ep_transfer_send(0, ptr, length, NULL);
}
void ep0_handle_dfu(struct usb_ctrlrequest *req)
{
uint32_t ret = RET_NOTHING;
switch (dfu_state) {
case DFU_STATE_appIDLE:
switch (req->bRequest) {
case USB_REQ_DFU_DETACH:
dfu_state = DFU_STATE_appDETACH;
ret = RET_ZLP;
break;
case USB_REQ_DFU_GETSTATUS:
handle_getstatus();
break;
case USB_REQ_DFU_GETSTATE:
handle_getstate();
break;
default:
ret = RET_STALL;
break;
}
break;
case DFU_STATE_appDETACH:
switch (req->bRequest) {
case USB_REQ_DFU_GETSTATUS:
handle_getstatus();
break;
case USB_REQ_DFU_GETSTATE:
handle_getstate();
break;
default:
dfu_state = DFU_STATE_appIDLE;
ret = RET_STALL;
break;
}
/* FIXME: implement timer to return to appIDLE */
break;
case DFU_STATE_dfuIDLE:
switch (req->bRequest) {
case USB_REQ_DFU_DNLOAD:
if (req->wLength == 0) {
dfu_state = DFU_STATE_dfuERROR;
ret = RET_STALL;
break;
}
dfu_state = DFU_STATE_dfuDNLOAD_SYNC;
ret = handle_dnload(req->wIndex, req->wValue, req->wLength);
break;
case USB_REQ_DFU_UPLOAD:
dfu_state = DFU_STATE_dfuUPLOAD_IDLE;
handle_upload(req->wIndex, req->wValue, req->wLength);
break;
case USB_REQ_DFU_GETSTATUS:
handle_getstatus();
break;
case USB_REQ_DFU_GETSTATE:
handle_getstate();
break;
case USB_REQ_DFU_ABORT:
/* no zlp? */
ret = RET_ZLP;
break;
default:
dfu_state = DFU_STATE_dfuERROR;
ret = RET_STALL;
break;
}
break;
case DFU_STATE_dfuDNLOAD_SYNC:
switch (req->bRequest) {
case USB_REQ_DFU_GETSTATUS:
handle_getstatus();
/* FIXME: state transition depending on block completeness */
break;
case USB_REQ_DFU_GETSTATE:
handle_getstate();
break;
default:
dfu_state = DFU_STATE_dfuERROR;
ret = RET_STALL;
break;
}
break;
case DFU_STATE_dfuDNBUSY:
switch (req->bRequest) {
case USB_REQ_DFU_GETSTATUS:
/* FIXME: only accept getstatus if bwPollTimeout
* has elapsed */
handle_getstatus();
break;
default:
dfu_state = DFU_STATE_dfuERROR;
ret = RET_STALL;
break;
}
break;
case DFU_STATE_dfuDNLOAD_IDLE:
switch (req->bRequest) {
case USB_REQ_DFU_DNLOAD:
dfu_state = DFU_STATE_dfuDNLOAD_SYNC;
ret = handle_dnload(req->wIndex, req->wValue, req->wLength);
break;
case USB_REQ_DFU_GETSTATUS:
handle_getstatus();
break;
case USB_REQ_DFU_GETSTATE:
handle_getstate();
break;
case USB_REQ_DFU_ABORT:
dfu_state = DFU_STATE_dfuIDLE;
ret = RET_ZLP;
break;
default:
dfu_state = DFU_STATE_dfuERROR;
ret = RET_STALL;
break;
}
break;
case DFU_STATE_dfuMANIFEST_SYNC:
switch (req->bRequest) {
case USB_REQ_DFU_GETSTATUS:
handle_getstatus();
dfu_state = DFU_STATE_dfuIDLE;
break;
case USB_REQ_DFU_GETSTATE:
handle_getstate();
break;
default:
dfu_state = DFU_STATE_dfuERROR;
ret = RET_STALL;
break;
}
break;
case DFU_STATE_dfuMANIFEST:
dfu_state = DFU_STATE_dfuERROR;
ret = RET_STALL;
break;
case DFU_STATE_dfuMANIFEST_WAIT_RST:
/* we should never go here */
break;
case DFU_STATE_dfuUPLOAD_IDLE:
switch (req->bRequest) {
case USB_REQ_DFU_UPLOAD:
/* state transition if less data then requested */
handle_upload(req->wIndex, req->wValue, req->wLength);
break;
case USB_REQ_DFU_GETSTATUS:
handle_getstatus();
break;
case USB_REQ_DFU_GETSTATE:
handle_getstate();
break;
case USB_REQ_DFU_ABORT:
dfu_state = DFU_STATE_dfuIDLE;
/* no zlp? */
ret = RET_ZLP;
break;
default:
dfu_state = DFU_STATE_dfuERROR;
ret = RET_STALL;
break;
}
break;
case DFU_STATE_dfuERROR:
switch (req->bRequest) {
case USB_REQ_DFU_GETSTATUS:
handle_getstatus();
break;
case USB_REQ_DFU_CLRSTATUS:
dfu_state = DFU_STATE_dfuIDLE;
dfu_status = DFU_STATUS_OK;
/* no zlp? */
ret = RET_ZLP;
break;
case USB_REQ_DFU_GETSTATE:
handle_getstate();
break;
default:
dfu_state = DFU_STATE_dfuERROR;
ret = RET_STALL;
break;
}
break;
}
switch (ret) {
case RET_NOTHING:
break;
case RET_ZLP:
ep_transfer_send(0, NULL, 0, NULL);
break;
case RET_STALL:
ep_send_stall(0);
break;
}
}
+308
View File
@@ -0,0 +1,308 @@
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "atomic.h"
#include "memalloc.h"
#include "rtos/context.h"
#include "rtos/spinlock.h"
/* linked list of ready contexts, #1 is the running thread */
struct context *volatile run_queue = NULL;
/* pointer to the running thread */
struct context *current_context = NULL;
/*
* SWI_Handler:
* Entry with SVC mode, IRQs are disabled, FIQ is enabled
*/
__attribute__((naked)) void SWI_Handler(void)
{
/* store register context to current_context */
asm volatile (
/* r0-1 -> svc_stack */
"stmdb sp!, {r0-r1} \n\t"
/* get top of struct register_context */
"ldr r0, =current_context \n\t"
"ldr r0, [r0] \n\t"
"add r0, r0, #68 \n\t"
/* save usermode cpsr & r2-14 */
"mrs r1, spsr \n\t"
"stmdb r0, {r1-r14}^ \n\t"
"nop \n\t"
"sub r0, r0, #56 \n\t"
/* save r0-1 and svc_lr (= pc) */
"ldmia sp!, {r1, r2} \n\t"
"stmdb r0!, {r1, r2, r14} \n\t"
);
/* we're no longer #1 in run_queue, switch to new #1 */
if (current_context != run_queue)
current_context = run_queue;
current_context->state = CONTEXT_RUNNING;
if (((uint32_t *)current_context->stack)[0] != 0xdeadbeef)
printf("<- task stack corrupt (%p)\n\r", current_context);
/* restore register context from current_context */
asm volatile (
/* get pointer to struct register_context */
"ldr r0, =current_context \n\t"
"ldr r0, [r0] \n\t"
/* get values of r0-1 and pc (= svc_lr) */
"ldmia r0!, {r1-r2,r14} \n\t"
"stmdb sp!, {r1-r2} \n\t"
/* restore usermode cpsr & r2-14 */
"ldmia r0, {r1-r14}^ \n\t"
"nop \n\t"
"msr spsr, r1 \n\t"
/* get r0-1 from svc_stack, jump back */
"ldmia sp!, {r0, r1} \n\t"
"movs pc, lr \n\t"
);
}
static uint8_t isr_context_switch(void)
{
asm volatile ("swi");
// TODO: return previous state of now running thread
return CONTEXT_READY;
}
/* inserts context into run_queue */
static void isr_context_ready(struct context *ctx)
{
struct context *q = run_queue;
struct context *volatile *qprev = &run_queue;
while (q && (q->priority <= ctx->priority)) {
qprev = &q->run_queue;
q = q->run_queue;
}
ctx->run_queue = q;
*qprev = ctx;
}
/* process yields, try to switch to process with lower or same prio */
void isr_context_yield(void)
{
run_queue = current_context->run_queue;
isr_context_ready(current_context);
if (current_context != run_queue) {
current_context->state = CONTEXT_READY;
isr_context_switch();
}
}
void context_yield(void)
{
disable_irqs();
isr_context_yield();
restore_irqs();
}
static uint8_t __isr_context_wait(struct spinlock *lock, uint8_t sleepstate)
{
isr_spinlock_unlock(lock);
run_queue = current_context->run_queue;
current_context->state = sleepstate;
uint8_t retval = isr_context_switch();
isr_spinlock_lock(lock);
return retval;
}
uint8_t isr_context_wait(struct spinlock *lock)
{
return __isr_context_wait(lock, CONTEXT_SLEEP);
}
uint8_t context_wait(struct spinlock *lock)
{
disable_irqs();
uint8_t retval = __isr_context_wait(lock, CONTEXT_SLEEP);
restore_irqs();
return retval;
}
uint8_t context_wait_queue(struct spinlock *lock, struct context **queue)
{
disable_irqs();
current_context->sleep_queue = *queue;
*queue = current_context;
uint8_t retval = __isr_context_wait(lock, CONTEXT_SLEEP_QUEUE);
restore_irqs();
return retval;
}
uint8_t context_wait_pri_queue(struct spinlock *lock, struct context **queue)
{
disable_irqs();
struct context *q = *queue;
while (q && (q->priority <= lock->priority_unlocked)) {
queue = &q->sleep_queue;
q = q->sleep_queue;
}
current_context->sleep_queue = q;
*queue = current_context;
uint8_t retval = __isr_context_wait(lock, CONTEXT_SLEEP_QUEUE);
restore_irqs();
return retval;
}
void isr_context_signal(struct context *c)
{
if (c->state == CONTEXT_SLEEP) {
c->state = CONTEXT_READY;
isr_context_ready(c);
}
}
void context_signal(struct context *c)
{
disable_irqs();
isr_context_signal(c);
restore_irqs();
}
uint32_t context_signal_queue(struct context **queue)
{
disable_irqs();
uint32_t retval = 0;
if (*queue) {
struct context *c = *queue;
*queue = c->sleep_queue;
c->state = CONTEXT_READY;
isr_context_ready(c);
retval = 1;
}
restore_irqs();
return retval;
}
void isr_context_interrupt(struct context *c)
{
if (c->state == CONTEXT_SLEEP) {
c->state = CONTEXT_INTERRUPTED;
isr_context_ready(c);
}
}
void context_interrupt(struct context *c)
{
disable_irqs();
isr_context_signal(c);
restore_irqs();
}
uint32_t context_interrupt_queue(struct context *c, struct context **queue)
{
disable_irqs();
struct context *q = *queue;
while (q && (q != c)) {
queue = &q->sleep_queue;
q = q->sleep_queue;
}
uint32_t retval = 0;
if (q) {
*queue = c->sleep_queue;
c->state = CONTEXT_INTERRUPTED;
isr_context_ready(c);
retval = 1;
}
restore_irqs();
return retval;
}
struct context * create_ctx(uint32_t stacksize, uint8_t priority, void (* code)(void *arg), void *arg)
{
uint32_t *stack = static_alloc(sizeof(struct context) + stacksize);
memset(stack, 0, stacksize);
// TODO: check on context-switch for corruption */
stack[0] = 0xdeadbeef;
struct context *ctx = (struct context *)((uint8_t *)stack + stacksize);
ctx->regs.r0 = (uint32_t)arg;
ctx->regs.pc = (uint32_t)code;
ctx->regs.cpsr = 0x0000001F;
ctx->regs.sp = (uint32_t)ctx;
ctx->regs.r1 = 0x01010101;
ctx->regs.r2 = 0x02020202;
ctx->regs.r3 = 0x03030303;
ctx->regs.r4 = 0x04040404;
ctx->regs.r5 = 0x05050505;
ctx->regs.r6 = 0x06060606;
ctx->regs.r7 = 0x07070707;
ctx->regs.r8 = 0x08080808;
ctx->regs.r9 = 0x09090909;
ctx->regs.r10 = 0x10101010;
ctx->regs.r11 = 0x11111111;
ctx->regs.r12 = 0x12121212;
ctx->regs.r14 = 0x14141414;
ctx->stack = stack;
ctx->priority = priority;
disable_irqs();
isr_context_ready(ctx);
restore_irqs();
return ctx;
}
void init_context(void)
{
/*
* create shallow idle context
* idle context runs in SVC mode, so no real stack is needed
*/
current_context = create_ctx(4, 255, NULL, NULL);
printf("idle_ctx=%p\n\r", current_context);
/* For now, we're the only thread, so this simply safes our context */
disable_irqs();
isr_context_switch();
restore_irqs();
/* idle loop */
while (1) {
// TODO: this sucks, context switch after interrupt are way better..
context_yield();
}
}
+56
View File
@@ -0,0 +1,56 @@
#include <stdint.h>
#include "rtos/context.h"
#include "rtos/semaphore.h"
#include "rtos/spinlock.h"
uint8_t sem_wait(struct semaphore *sem)
{
uint32_t retval = CONTEXT_READY;
spinlock_lock(&sem->lock);
sem->count--;
if (sem->count < 0)
retval = context_wait_pri_queue(&sem->lock, &sem->sleep_queue);
spinlock_unlock(&sem->lock);
return retval;
}
void sem_post(struct semaphore *sem)
{
spinlock_lock(&sem->lock);
sem->count++;
if (sem->count <= 0)
context_signal_queue(&sem->sleep_queue);
spinlock_unlock(&sem->lock);
}
void sem_interrupt(struct semaphore *sem, struct context *c)
{
spinlock_lock(&sem->lock);
context_interrupt_queue(c, &sem->sleep_queue);
spinlock_unlock(&sem->lock);
}
int32_t sem_get_count(struct semaphore *sem)
{
int32_t retval;
spinlock_lock(&sem->lock);
retval = sem->count;
spinlock_unlock(&sem->lock);
return retval;
}
void sem_init(struct semaphore *sem, int32_t count)
{
sem->sleep_queue = 0;
spinlock_init(&sem->lock);
sem->count = 0;
}
+40
View File
@@ -0,0 +1,40 @@
#include <stdint.h>
#include "atomic.h"
#include "rtos/context.h"
#include "rtos/spinlock.h"
void isr_spinlock_lock(struct spinlock *lock)
{
lock->priority_unlocked = current_context->priority;
current_context->priority = 0;
lock->locked = 1;
}
void spinlock_lock(struct spinlock *lock)
{
disable_irqs()
isr_spinlock_lock(lock);
restore_irqs();
}
void isr_spinlock_unlock(struct spinlock *lock)
{
current_context->priority = lock->priority_unlocked;
lock->locked = 0;
// TODO: check for context-switch...
}
void spinlock_unlock(struct spinlock *lock)
{
disable_irqs()
isr_spinlock_unlock(lock);
restore_irqs();
}
void spinlock_init(struct spinlock *lock)
{
lock->locked = 0;
}