Root/
| Source at commit 30c683effd7ab362d1517950700c96cfa09e45e8 created 11 years 17 days ago. By Werner Almesberger, tornado/fw/sim/p: pass arguments to "alg"; label graphs; better separate "up" | |
|---|---|
| 1 | /* |
| 2 | * fw/led.c - LED control |
| 3 | * |
| 4 | * Written 2012 by Werner Almesberger |
| 5 | * Copyright 2012 Werner Almesberger |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | #include <stdint.h> |
| 15 | |
| 16 | #include <avr/io.h> |
| 17 | #include <avr/interrupt.h> |
| 18 | #include <avr/pgmspace.h> |
| 19 | #define F_CPU 8000000UL |
| 20 | #include <util/delay.h> |
| 21 | |
| 22 | #include "io.h" |
| 23 | #include "led.h" |
| 24 | |
| 25 | |
| 26 | void led_show(const uint8_t p[LED_BYTES]) |
| 27 | { |
| 28 | uint8_t i; |
| 29 | |
| 30 | for (i = 0; i != LED_BYTES; i++) { |
| 31 | while (!(UCSR0A & (1 << UDRE0))); |
| 32 | UDR0 = p[i]; |
| 33 | } |
| 34 | |
| 35 | /* |
| 36 | * There doesn't seem to be any reliable way to synchronize with the |
| 37 | * end of the shift operation register that wouldn't cli/sei. Note |
| 38 | * that, since the receiver is disabled, we can't use the RX side for |
| 39 | * this purpose (as it is done in the atusb firmware). |
| 40 | * |
| 41 | * So instead we just wait for the worst-case delay, being one byte |
| 42 | * in the shift register and another byte in the transmit buffer, |
| 43 | * giving a total of 16 bits. |
| 44 | * |
| 45 | * This is a bit too pessimistic and produces a slight extra delay of |
| 46 | * about 1.8 us. (The whole pattern transmission takes 16 us.) |
| 47 | * See also ../doc/lclk-1123.png |
| 48 | */ |
| 49 | |
| 50 | _delay_us(4); /* 16 bits at 4 MHz */ |
| 51 | SET(LED_LCLK); |
| 52 | CLR(LED_LCLK); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | void led_show_pgm(const prog_uint8_t p[LED_BYTES]) |
| 57 | { |
| 58 | uint8_t i; |
| 59 | |
| 60 | for (i = 0; i != LED_BYTES; i++) { |
| 61 | while (!(UCSR0A & (1 << UDRE0))); |
| 62 | UDR0 = pgm_read_byte(p+i); |
| 63 | } |
| 64 | |
| 65 | _delay_us(4); /* 16 bits at 4 MHz */ |
| 66 | SET(LED_LCLK); |
| 67 | CLR(LED_LCLK); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | void led_off(void) |
| 72 | { |
| 73 | static uint8_t zero[LED_BYTES] PROGMEM = { 0, }; |
| 74 | |
| 75 | led_show_pgm(zero); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | void led_init(void) |
| 80 | { |
| 81 | CLR(LED_SCLK); |
| 82 | OUT(LED_SCLK); |
| 83 | CLR(LED_LCLK); |
| 84 | OUT(LED_LCLK); |
| 85 | |
| 86 | UBRR0 = 0; /* maximum data rate */ |
| 87 | UCSR0C = |
| 88 | 1 << UMSEL01 | /* Master SPI */ |
| 89 | 1 << UMSEL00 | |
| 90 | 1 << UDORD0 | /* MSB first */ |
| 91 | 0 << UCPHA0 | /* SPI Mode 0 */ |
| 92 | 0 << UCPOL0; |
| 93 | UCSR0B = |
| 94 | 1 << TXEN0; /* enable transmitter */ |
| 95 | UBRR0 = 0; /* the manual sez we need this */ |
| 96 | |
| 97 | led_off(); |
| 98 | } |
| 99 | |
Branches:
master
tornado-v1
