forth and c on the cortex-m3forth.org/svfig/kk/04-2012-sexton.pdf · state of forth on the m3...

25
Forth and C on the Cortex-M3 Saturday, April 28, 12

Upload: nguyencong

Post on 04-May-2018

239 views

Category:

Documents


3 download

TRANSCRIPT

Forth and C on the Cortex-M3

Saturday, April 28, 12

Arm Cortex-M3!32-Bit Architecture

Low-latency Prioritized Interrupt controller - NVIC

Single-Cycle Multiply

Sophisticated Debug

C and Assembly friendly

Common across all major Micro-controller vendors

Available in Simplified (M0) and Enhanced (M4)

Saturday, April 28, 12

State of Forth on the M3

Plenty of Forths for ARM

Cortex-M3 is Thumb-2 - Few off the shelf implementations

Riscy Pygness Forth - Umbilical

MPE Forth - Commercial

Machine Initialization varies widely across vendors

Saturday, April 28, 12

Options for Forth

Port Riscy Pygness to a new flavor of M3

Strip out machine init code, and call from C

main() { ..... ; asm("b initForth"); }

Port HForth or similar direct-threaded Forths

Rely heavily upon assembler macros

Commercial Forth

Saturday, April 28, 12

Clock Trees!o FI=E >CPCMILM

o LSMN;F CHJON M?F?=NCIH

#CAOL? �� IH J;A? ��� MBIQM NB? FIAC= @IL NB? G;CH =FI=E NL??� 1B? J?LCJB?L;F <FI=EM ;L? >LCP?H <SNB? MSMN?G =FI=E MCAH;F ;H> =;H <? CH>CPC>O;FFS ?H;<F?>�>CM;<F?>� 1B? �! =FI=E MCAH;F CM;ONIG;NC=;FFS >CPC>?> >IQH NI �� *%T @IL JLIJ?L �! IJ?L;NCIH� 1B? -4* =FI=E MCAH;F CM ;MSH=BLIHIOM >CPC>? I@ NB? MSMN?G =FI=E NI JLIPC>? NB? -4* =CL=OCN QCNB GIL? L;HA? �M?N QCNB PWMDIVCH '����

#@E7� 4B?H NB? �! GI>OF? CM CH IJ?L;NCIH NB? MSMN?G =FI=E GOMN <? ;N F?;MN �� *%T�

�;9FC7 ���� "3;? �=@5< )C77

-))�� *%T�*;CH ,0

&HN?LH;F,0

��� *%T�

&HN?LH;F,0

�� E%T�

?

%C<?LH;NCIH*I>OF?

������� E%T� ? ��

-4/!+

�! FI=E

0SMN?G FI=E

51�);-4/!+ <

*,0 !&0 ;

&,0 !&0;

,0 0/ <>

�6-�00 <>

060!&3<>

20"060!&3;>

-4*!4;

20"-4*!&3 ;

-4* FI=E

? �

? �� �+ FI=E

#@E7� 1B? @CAOL? ;<IP? MBIQM ;FF @?;NOL?M ;P;CF;<F? IH ;FF 0N?FF;LCMZ #OLS=F;MM >?PC=?M� +IN ;FF J?LCJB?L;FM G;S <?;P;CF;<F? IH NBCM >?PC=?�

"*47*1'*5 ��� ����#*;&6 �267581*276��53)8(7.32 �&7&

"<67*1 �327530

Reset and clock control (RCC) RM0038

86/809 Doc ID 15965 Rev 5

Figure 11. Clock tree

1. For full details about the internal and external clock source characteristics, please refer to the “Electrical characteristics” section in your device datasheet.

Saturday, April 28, 12

Bootstrapping the M3!

Complex Initialization/Reconfiguration

Large body of demonstration/reusable code

Re-Implementation in Forth is a bad option

Opportunities for Error

Time is best spent on Application Development

Saturday, April 28, 12

Existing Vendor Code

TI StellarisWare

SysCtlClockSet() - 177 Lines of Code

ADCSequenceStepConfigure() - 90 Lines of Code

ST Micro Driverlib

SetSysClock() - 82 Lines of Code

ADC_Init() - 52 Lines of Code

Code thats rich in bit-whacking and specific rules

Saturday, April 28, 12

Partitioning

DFU Boot loader - 8.5k/0k

System Services - 13k/4k

Application Layer 31k Flash/12k Ram

System Services - 11.5k

Inactive after initial boot

USB CDC ServicesTiming Services

Frame Buffer

Application DevelopmentControl Interface

Debugging/Bringup

Saturday, April 28, 12

System Call Interface

Minimal System Call Support:

API Version

getchar, putchar

get shared variables list

set power state

get current value of millisecond counter

Saturday, April 28, 12

Forth SVC Calls

Note: Cortex-M3 Pushes R0-R4, R12, LR, PC, xPSR automatically

\ ********************************************\ SVC 0: Return the version of the API in use.\ ********************************************CODE API-Version ( -- n )

svc #0 ( Call Supervisor)str tos, [ psp, # -4 ] ! ( Push TOS)

mov tos, r0 ( return value) next,END-CODE

Saturday, April 28, 12

Catching it!Handler Looks up the SVC Number

and jumps via table to the right function:

// Return an API Versionvoid __SAPI_Version(long *frame) { frame[0] = 0x0201; return; }

R0R1R2R3R12LRPC

xPSR

Saturday, April 28, 12

Handler-Thread Sync

Interrupt handlers need to send ‘Events’ to each other and to the Thread-Mode tasks.

Unidirectional events makes this safe:

ISR: notify_1Hz = 1;

Thread resets the event register and waits for change:

0 notify_1Hz !

notify_1Hz @ if <something> then

Saturday, April 28, 12

TCB status byte

Scheduler uses non-zero value to trigger task execution or event hander

ISR can set the event bit to trigger task execution

Scheduler calls event handler then the task

50 Forth 7 Cross Compiler

Bit When set When Reset0 Task is running Task is halted1 Message pending but not read No messages2 Event triggered No events3 Event handler has been run No events (reset by user)4.. User de�ned User de�ned

Table 9.2: Task status cell

9.9 Example Task

The following example is a simple demonstration of the multitasker. Its role is to displaya hash (#) every so often, leaving the foreground Forth interpreter running. To use themultitasker you must cross-compile the �le MULTI*.FTH into your target. The samplecontrol �les have an EQUate Tasking? which, when non-zero, will compile the multitasker.

9.9.1 De�ning the task

The following code de�nes a simple task called TASK1. It displays a # every 1000 schedules.

VARIABLE DELAY \ time delay between #'s1000 DELAY ! \ initialise time delay

: ACTION1 \ -- ; task to display #'s[CHAR] $ EMIT \ Display a dollar ($)BEGIN \ Start continuous loop[CHAR] # EMIT \ Display a hash (#)DELAY @ 0 \ Reschedule Delay times?DO PAUSE LOOP

AGAIN \ Back to the start ...;

9.9.2 Initialising the multitasker

Before any tasks can be activated, the multitasker must be initialised. This is done withthe following code:

INIT-MULTI

The word INIT-MULTI initialises all the multitasker's data structures and starts multitask-ing. This word need only be executed once in a multitasking system.

9.9.3 Activating the task

To activate (run) the example task, type:

TASK TASK1

ASSIGN ACTION1 TASK1 INITIATE

This will set ACTION1 as the action of task TASK1. Immediately you will see a dollar and ahash ($#) displayed. If you press <return> a few times, you see that the Forth interpreter

Saturday, April 28, 12

Hooking it into a Thread

task clock2huetask

: launch-tasks ['] clr-event-run clock2huetask to-event ['] update-hue-out clock2huetask initiate

;

: update-hue-out ( -- ) stop begin rtc @ calc_hue rtc2hue_out ! pause again;

Saturday, April 28, 12

Bit-Banding on the M3

x x x x y y y y y y y y y y y y y y y y y y y y

x x x x 1 y y y y y y y y y y y y y y y y y y y y b b b b b 0 0

Target Address -> Bit Band Alias Transformation

Bit Number

Atomic!

Saturday, April 28, 12

Lock-Avoidance

Bit-Banding eliminates interrupt lockout

Fewer hazards

Similar instruction count

add an extra word to the TCB

if (notify_refresh_bbp != 0) { *notify_refresh_bbp = 1; }

: clr-event-run! \ --\ * Reset the current task's EVENT_RUN flag. 0 up@ tcb.bbstatus @ evt-bit# + ! ;

Saturday, April 28, 12

Dynamic Linking!Linker places C variables where it sees fit

Manually updating pre-defined constants is error-prone

Table-Driven approach allows automatic updates

runtimelink_t dynamiclinks[] = {{ (int*) sizeof(runtimelink_t), sizeof("RECORDLE"), "RECORDLEN", 0, 0},{ (int*) hsv_in, sizeof("hsv_i"), "hsv_in", sizeof(uint32_t), 3 } };

The address of this array is available via system call. Names are encoded as counted strings

Saturday, April 28, 12

Questions?

http://www.kudra.com/forth

Saturday, April 28, 12

Invented at the MIT Media Lab

Concept - Low Bandwidth Information

Weather Forecasts

Stock Prices

Mail Notification

Ambient Devices!

Saturday, April 28, 12

Hardware

STM32L Cortex-M3

CPUTI LED

Controller SPI

USB

RGB LEDS (15)

192 bits / 128 Hz

Saturday, April 28, 12

Firmware Architecture

Forth

SVC Handler TimekeepingUSB

Service

SPI/DMA/Timer Handler

RTC Handler

PendSV Handler

Display Processing

Pri 0

Pri 2

Pri 3

Thread

Saturday, April 28, 12

NVIC Priority Behavior

Saturday, April 28, 12

Power Consumption

Exclusive of LED Power

10 mA in USB Connected Mode

7.7mA waiting for a connection

1.5mA in suspend (131 kHz CPU)

Suspend/Wake Changes CPU Voltage and Frequency

More opportunities remain for power reduction

Saturday, April 28, 12

USB Integration

SendChar puts an application byte into a transmission buffer

Minimize the number of USB packets:

Send if there is a full USB buffer

Send if the buffer is old ( > 10ms)

If the transmission buffer is too full, wait for the USB interrupt handlers to drain it before returning control to the application (PendSV mechanism)

Saturday, April 28, 12

User Interface

USB CDC Device - Ascii Command Set

60 hue, $ffff 0 0 rgbset, etc.

fast pulse, slow pulse

extensible

Saturday, April 28, 12