From 7b766ed93ce357e89e05c5a329d72bbcdbf7c125 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Fri, 23 Nov 2012 14:50:01 -0300 Subject: [PATCH] tornado/: use UART in SPI mode to send LED patterns (at 4 Mbps) --- tornado/doc/lclk-1123.png | Bin 0 -> 4154 bytes tornado/fw/Makefile | 2 +- tornado/fw/led.c | 82 ++++++++++++++++++++++++++++++++++++++ tornado/fw/led.h | 28 +++++++++++++ tornado/fw/tornado.c | 11 +++-- 5 files changed, 116 insertions(+), 7 deletions(-) create mode 100644 tornado/doc/lclk-1123.png create mode 100644 tornado/fw/led.c create mode 100644 tornado/fw/led.h diff --git a/tornado/doc/lclk-1123.png b/tornado/doc/lclk-1123.png new file mode 100644 index 0000000000000000000000000000000000000000..86b01adf6823d251605e4d7f85ce36c25a0578fe GIT binary patch literal 4154 zcmZ`-X*iSt*M4ROV;Nf*L-r+cf*nVVW00TL1?^f>Dzj)7yq#?S9rR@Mo%!DZRtaL32T&zgbbSpaBm24-gg zO{ubzZpgJu7Yv!b#(vtK2&TY`4k0Ia@&Cg3y^k&cup8nG^{pdkHXrGy&0a);ubHS$ zEi+tNO?|M<9&}6^XJS32hjbLLBTJo|)bfoGnN`&F&BN;YR!&!ukg{!I{v*ZL@`(zW zSml&9UwAM&*_c|(_V>!~PCLnJiJ@kJ8n?>}i4z?5PvdYa; z>T;PG*?7}w?J6Q#_?~`~DQW)*N&Q~1oyBy*$@!r8a9c!oOOnnd_HfG=p*mf0Zw=7 zEr(RF>zF`$=s>$tBm;*g@3g5Pho&6_8M^b&vqVmZ_R@0p3gmP!RsX{E*ZFTvdFQq4 zsn>GDl^p4)>eJ(kG!ws&hO`l5q5878Ecv+BU<49ChqwQ1J#MkV+o~{xkr)oyVy8a$ ziqHYP8-?XS46P~<;BbikWJ9g&L6fq$XNdHO<9Dep}G!YL{hWs~4hJ*P+=yJg#v%0nr#4+x8}} zm5pXnl)F=Uu*D`IZFt8K8)SFsOJ$aG8WH~R2K#|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VvYUnq1D8-!FI_XV&5WxV@y5X!Z}Ih7gvrrZ`hdyD-*6k2=(yewG%qBr7jvt=gc|JNgty57|r zoJF6*>B_6n1ZKTi==8L|WVC;9Gw z4U!@iu;s^SnjNQynl*a<+pPQteI1X^SsjqOM)*_{ zlO7b(AKr^$PcUBSZo^sBY8F~tWGgN_tJz=5`n1>@(&kCt1*4DAZ&Ens|7CdOhJJLy zd+n28rdvkMnRWd$okbCnBAtu6K0e>W7kgRb)TI-e^Bh{`gDSSVog=uj2;Wb zv;Io`dFW6?;3;&J>c`$Ts3Jwgc5W^cTmven0z8%!ZCnZ;b-mYo@u>Gaz=utYlfKO$Jtk3JDnz50u z-iwQsGSZ*qJ#X~{fvawoE;cLR@+^AAaFCOqdxYj-PU2W4B54yhXP!;_kruOQ0{RvM z_a8Q`?YJkXWA2zfw~@}EGw+%#N`5qa{0R$UQ$Qm3{Vp?luiY^`JwucPa?Sh7klCow z)1FTah~-Q`ND0HSl^ASxPcb|TzBEpRWxpfJ^{o#;(QmyS^M zkdOch2BIt;b8Ni-$fP)^KEs7QLR&pgbF@SPJJV=#FRKg-xK{Vh<#tpN%ViVa+A&>SO@AHv?jZ)_l~ITaq>{%O-g>riiuR{VKH3fXjWSWAW)*@wEjhI(ka1$&$TfL2sO%PA?zp_Q$b uR5VpEnkQ0GSyNFlT7%mB9|pewcW=+A|88J$xBlNR0e~|yGkk79NcbOg(v=wi literal 0 HcmV?d00001 diff --git a/tornado/fw/Makefile b/tornado/fw/Makefile index 24d549e..20ad483 100644 --- a/tornado/fw/Makefile +++ b/tornado/fw/Makefile @@ -31,7 +31,7 @@ OBJCOPY = $(AVR_PREFIX)objcopy #OBJDUMP = $(AVR_PREFIX)objdump SIZE = $(AVR_PREFIX)size -OBJS = $(NAME).o $(COMMON_OBJS) +OBJS = $(NAME).o led.o $(COMMON_OBJS) BOOT_OBJS = boot.o $(COMMON_OBJS) COMMON_OBJS = diff --git a/tornado/fw/led.c b/tornado/fw/led.c new file mode 100644 index 0000000..680bae0 --- /dev/null +++ b/tornado/fw/led.c @@ -0,0 +1,82 @@ +/* + * fw/led.c - LED control + * + * Written 2012 by Werner Almesberger + * Copyright 2012 Werner Almesberger + * + * 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; either version 2 of the License, or + * (at your option) any later version. + */ + + +#include + +#include +#include +#define F_CPU 8000000UL +#include + +#include "io.h" +#include "led.h" + + +void led_show(const uint8_t p[LED_BYTES]) +{ + uint8_t i; + + for (i = 0; i != 8; i++) { + while (!(UCSR0A & (1 << UDRE0))); + UDR0 = p[i]; + } + + /* + * There doesn't seem to be any reliable way to synchronize with the + * end of the shift operation register that wouldn't cli/sei. Note + * that, since the receiver is disabled, we can't use the RX side for + * this purpose (as it is done in the atusb firmware). + * + * So instead we just wait for the worst-case delay, being one byte + * in the shift register and another byte in the transmit buffer, + * giving a total of 16 bits. + * + * This is a bit too pessimistic and produces a slight extra delay of + * about 1.8 us. (The whole pattern transmission takes 16 us.) + * See also ../doc/lclk-1123.png + */ + + _delay_us(4); /* 16 bits at 4 MHz */ + SET(LED_LCLK); + CLR(LED_LCLK); +} + + +void led_off(void) +{ + static uint8_t zero[LED_BYTES]; + + led_show(zero); +} + + +void led_init(void) +{ + CLR(LED_SCLK); + OUT(LED_SCLK); + CLR(LED_LCLK); + OUT(LED_LCLK); + + UBRR0 = 0; /* maximum data rate */ + UCSR0C = + 1 << UMSEL01 | /* Master SPI */ + 1 << UMSEL00 | + 1 << UDORD0 | /* MSB first */ + 0 << UCPHA0 | /* SPI Mode 0 */ + 0 << UCPOL0; + UCSR0B = + 1 << TXEN0; /* enable transmitter */ + UBRR0 = 0; /* the manual sez we need this */ + + led_off(); +} diff --git a/tornado/fw/led.h b/tornado/fw/led.h new file mode 100644 index 0000000..3ec539b --- /dev/null +++ b/tornado/fw/led.h @@ -0,0 +1,28 @@ +/* + * fw/led.h - LED control + * + * Written 2012 by Werner Almesberger + * Copyright 2012 Werner Almesberger + * + * 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; either version 2 of the License, or + * (at your option) any later version. + */ + + +#ifndef LED_H +#define LED_H + +#include + + +#define N_LEDS 64 +#define LED_BYTES (N_LEDS/8) + + +void led_show(const uint8_t p[LED_BYTES]); +void led_off(void); +void led_init(void); + +#endif /* !LED_H */ diff --git a/tornado/fw/tornado.c b/tornado/fw/tornado.c index a3f0367..1a89aa8 100644 --- a/tornado/fw/tornado.c +++ b/tornado/fw/tornado.c @@ -5,6 +5,7 @@ #include #include "io.h" +#include "led.h" #define HIGH(port) \ @@ -38,9 +39,6 @@ #endif -#define N_LEDS 64 - - static void send(uint8_t pattern[N_LEDS/8]) { uint8_t i, j, mask; @@ -96,7 +94,7 @@ static uint16_t adc(bool x) int main(void) { - static uint8_t p[N_LEDS/8]; + static uint8_t p[LED_BYTES]; uint8_t mode = 0; uint16_t n = 0, v; @@ -119,6 +117,7 @@ int main(void) OUT(VDD); #endif + led_init(); while (1) { while (!PIN(SW_SW)); @@ -149,9 +148,9 @@ int main(void) p[(v >> 3) & 7] &= ~(1 << (v & 7)); else p[(v >> 3) & 7] |= 1 << (v & 7); - send(p); + led_show(p); n++; } - _delay_ms(1); + _delay_ms(100); } }