# AGENTS.md
-AVR firmware (ATmega328P, 16 MHz) for the "Wild Thumper" arm's **virtual servo**: a DYNAMIXEL bus slave with ID `0x21` on the half-duplex single-wire UART (PD2, picoUART, 115200 8N1). A protocol-2.0 1-byte WRITE (INS=0x03) to address 0x01 sets `OCR0A` (D6 LED PWM duty) and gets an 11-byte status reply; all other packets are silently ignored. The ROS 2 package side lives in the repo root and is documented in the root `AGENTS.md` — do not confuse the two.
+AVR firmware (ATmega328P, 16 MHz) for the "Wild Thumper" arm's **virtual servo**: a DYNAMIXEL bus slave with ID `0x21` on the half-duplex single-wire UART (PD2, picoUART, 115200 8N1). Packets addressed to `0x21` are dispatched by `dynamixel_slave_poll()` to a callback registered from `main.c`; today that handles a protocol-2.0 1-byte WRITE (INS=0x03) to address 0x01 → `OCR0A = 255 - value` (D6 LED PWM duty, inverted — hardware-tuned, keep the inversion) and an 11-byte status reply, and a READ (INS=0x02) of address 0x02 → 13-byte status reply carrying the MLX90614 IR thermometer object temperature as a 16-bit value in 0.1 °C (214 == 21.4 °C, low byte first); everything else is silently ignored. The ROS 2 package side lives in the repo root and is documented in the root `AGENTS.md` — do not confuse the two.
## Build / verify
-- `make` → `main.elf` / `main.hex` (WinAVR-style Makefile; sources in `SRC`/`SRCPP`: `main.c uart.c` + `dynamixel-avr/dynamixel.cpp picoUART/src/picoUART.cpp`). `make clean` removes all artifacts (`*.o *.elf *.hex *.map *.lss *.lst *.sym *.eep .dep`) — run it before committing.
+- `make` → `main.elf` / `main.hex` (WinAVR-style Makefile; sources in `SRC`/`SRCPP`: `main.c uart.c mlx90614.c i2c/twimaster.c` + `dynamixel-avr/dynamixel.cpp picoUART/src/picoUART.cpp`). `make clean` removes all artifacts (`*.o *.elf *.hex *.map *.lss *.lst *.sym *.eep .dep`) — run it before committing.
- **`picoUART/src/rxISR.cpp` is deliberately NOT in the build**: it targets the ATtiny (`GIMSK`/`PCMSK`/PCINT0, none exist on the 328P) and captures one byte only. Do not "fix" it into the build.
- Flashing: `make program` (avrdude/avrisp2/usb). No programmer is attached in this environment; the user flashes elsewhere — do not attempt to flash here.
- Debug console: 9600 baud on D0/D1 (`uart.c`, `printf`); prints "Start" at boot.
- **No-hardware verification** (this environment has no python3, no host gcc, no simavr/qemu; shell is busybox ash):
1. `make` must finish with `Errors: none`.
- 2. `sh tools/verify_slave.sh` — host-side check of the slave's CRC + decision logic: loads the real CRC table from `dynamixel-avr/dynamixel.cpp` (lines 40–71, line-range sensitive), replicates `update_crc` and the `dynamixel_slave_poll` decisions, and checks known-good Soll CRCs, valid writes, and negatives.
+ 2. `sh tools/verify_slave.sh` — host-side check of the slave's CRC + decision logic: loads the real CRC table from the `crc_table[]` definition in `dynamixel-avr/dynamixel.cpp`, replicates `update_crc` and the `dynamixel_slave_poll`/callback decisions, and checks known-good Soll CRCs, valid writes, the read-reply frame, and negatives.
3. ISR sanity: `avr-objdump -d -C main.elf | sed -n '/<__vector_5>:/,/^$/p'` (PCINT2_vect = `__vector_5`).
4. busybox ash gotcha: multi-digit positional params are broken (`$10` → `$1`+"0", also inside `$(( ))`) — use `shift`/named vars in shell scripts (see `tools/verify_slave.sh`).
## Architecture (non-obvious)
-- `main.c`: sleep-based loop `if (!dynamixel_rxready) sleep_mode(); dynamixel_slave_poll();` — the rxready check **before** sleeping is load-bearing (a completed packet must be processed before the CPU can sleep).
+- `main.c`: owns the application logic: `slave_cmd()` (registered via `dynamixel_slave_set_cb()`) implements 1-byte-write addr 0x01 → `OCR0A = 255 - value` and read addr 0x02 → 2-byte temperature result (low byte first, or ERR=0x04 on sensor failure); returns 1 (send the status reply with the given ERR + reply bytes) or 0 (stay silent). Also calls `i2c_init()` (MLX90614 bus) and runs the sleep-based loop `if (!dynamixel_rxready) sleep_mode(); dynamixel_slave_poll();` — the rxready check **before** sleeping is load-bearing (a completed packet must be processed before the CPU can sleep).
- Capture: naked `ISR(PCINT2_vect, ISR_NAKED)` in `dynamixel-avr/dynamixel.cpp` with hand-rolled SREG save/restore. PD2 = PCINT18 → `PCMSK2` (0x6D) is in memory space, so mask ops are `lds`/`andi`/`sts`, not `cbi`. Per byte: disable mask at entry, sample 8 bits using the picoUART `PURXSTART - 18`/`PURXWAIT` delays (16 MHz constants), re-arm the mask at exit so the next start bit is caught.
- Capture state (all `volatile`): `dynamixel_rxindex` (buffer is 128 — `DYNAMIXEL_PACKET_SIZE` is 128, not 24), `dxl_expected` (=LEN+7, parsed from the LEN field at byte 7, only for a header-valid packet), `dxl_last_tick` (stale watchdog), `dynamixel_rxready`.
- Stale watchdog: Timer1 free-running, prescaler 64 (4 µs/tick, set in `dynamixel_listen_init`); gap > 375 ticks (~1.5 ms) since the last captured byte → buffer restarts. TCNT0 (8-bit, wraps at 1.024 ms) cannot be used for this.
- **Skip guard**: at ISR entry, if `rxindex >= dxl_expected`, skip the capture and just re-arm — a complete packet cannot have more start bits (the master waits for the reply; `poll()` resets state right after sending it). Load-bearing: `PU_DISABLE_IRQ=1` makes `putx()` run with `cli()`, so falling edges of our own 11-byte reply TX latch and one spurious ISR fires right after the last `putx()` byte. Without the guard the captured command is processed again and a **second reply** goes on the bus.
-- `dynamixel_slave_poll()`: validates header + CRC (`update_crc`, table in `dynamixel.cpp`), ID==0x21, INS==WRITE, len==13, addr==1 → `OCR0A = value`, then replies: wait for line idle, `_delay_us(50)` turnaround, 11× `putx`. Always restores idle state afterwards: PD2 pull-up (`putx` clears it, leaving the pin floating), re-arm PCMSK2, `rxindex = 0`, `dxl_expected = DYNAMIXEL_PACKET_SIZE`.
+- `dynamixel_slave_poll()`: validates header + CRC (`update_crc`, table in `dynamixel.cpp`); if the packet is addressed to `DXL_SLAVE_ID` and has a 2-byte address field (total len ≥ 12) it calls the registered callback with `(INS, param field, param_len, &err, reply, &reply_len)` (param_len = len−10; for a write the param field is `{addr_lo, addr_hi, data...}`). Callback returns 1 → send the status reply with the callback's ERR and up to `DXL_SLAVE_REPLY_MAX` (8) reply bytes — LEN field = 4+reply_len, so 11 bytes with no data, 13 for a 16-bit read (wait for line idle, `_delay_us(50)` turnaround, `putx` per byte); 0 → stay silent. Always restores idle state afterwards: PD2 pull-up (`putx` clears it, leaving the pin floating), re-arm PCMSK2, `rxindex = 0`, `dxl_expected = DYNAMIXEL_PACKET_SIZE`.
+- MLX90614 IR thermometer: `mlx90614.c` reads the raw object-temperature register (`0x07`, data low byte first, then an unchecked PEC byte) over I2C (A4 SDA / A5 SCL) using the `i2c/` library (hardware TWI, 100 kHz; `i2c_init()` at boot). 7-bit address `0x5A` = the Adafruit library's default (the datasheet PDF in this folder cannot be read in this environment — if the sensor does not ACK, verify the address against the datasheet; it is a single `#define`). Conversion: °C = raw×0.02 − 273.15 ⇒ 0.1 °C = (raw−13657)/5. A failed read (NACK/bus busy) returns 0.
- Timer ownership: Timer0 = LED PWM (D6, ~1 kHz; `OCR0A` doubles as the virtual servo's "value" — do not reconfigure). Timer1 = stale watchdog — do not reconfigure. picoUART timing constants assume F_CPU = 16 MHz (hardcoded in the Makefile `CDEFS`).
- `dxl_packet_length()` is inline asm because -Os with GCC 15.2 dropped the high byte of the 16-bit result (see the comment in the source) — keep it in asm if touched.
\r
\r
# List C source files here. (C dependencies are automatically generated.)\r
-SRC = $(TARGET).c uart.c\r
+SRC = $(TARGET).c uart.c mlx90614.c i2c/twimaster.c\r
SRCPP = dynamixel-avr/dynamixel.cpp picoUART/src/picoUART.cpp\r
\r
# List Assembler source files here.\r
CDEFS = -DF_CPU=16000000\r
\r
# Place -I options here\r
-CINCS = -ILib_TWI_USI_Slave_EEProm_1v3 -IpicoUART/src\r
+CINCS = -ILib_TWI_USI_Slave_EEProm_1v3 -IpicoUART/src -Ii2c\r
\r
\r
# Compiler flags.\r
--- /dev/null
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
+<title>AVR-GCC libraries: I2C Master library</title>
+<link href="doxygen.css" rel="stylesheet" type="text/css">
+</head><body>
+<!-- Generated by Doxygen 1.4.1 -->
+<h1>I2C Master library</h1><hr><a name="_details"></a><h2>Detailed Description</h2>
+I2C (TWI) Master Software Library.
+<p>
+<div class="fragment"><pre class="fragment"><span class="preprocessor"> #include <i2cmaster.h></span>
+</pre></div><p>
+Basic routines for communicating with I2C slave devices. This single master implementation is limited to one bus master on the I2C bus.<p>
+This I2c library is implemented as a compact assembler software implementation of the I2C protocol which runs on any AVR (i2cmaster.S) and as a TWI hardware interface for all AVR with built-in TWI hardware (twimaster.c). Since the API for these two implementations is exactly the same, an application can be linked either against the software I2C implementation or the hardware I2C implementation.<p>
+Use 4.7k pull-up resistor on the SDA and SCL pin.<p>
+Adapt the SCL and SDA port and pin definitions and eventually the delay routine in the module i2cmaster.S to your target when using the software I2C implementation !<p>
+Adjust the CPU clock frequence F_CPU in twimaster.c or in the Makfile when using the TWI hardware implementaion.<p>
+<dl compact><dt><b>Note:</b></dt><dd>The module i2cmaster.S is based on the Atmel Application Note AVR300, corrected and adapted to GNU assembler and AVR-GCC C call interface. Replaced the incorrect quarter period delays found in AVR300 with half period delays.</dd></dl>
+<dl compact><dt><b>Author:</b></dt><dd>Peter Fleury <a href="mailto:pfleury@gmx.ch">pfleury@gmx.ch</a> <a href="http://jump.to/fleury">http://jump.to/fleury</a></dd></dl>
+<dl compact><dt><b>API Usage Example</b></dt><dd>The following code shows typical usage of this library, see example test_i2cmaster.c</dd></dl>
+<div class="fragment"><pre class="fragment"><span class="preprocessor"> #include <i2cmaster.h></span>
+
+
+<span class="preprocessor"> #define Dev24C02 0xA2 // device address of EEPROM 24C02, see datasheet</span>
+<span class="preprocessor"></span>
+ <span class="keywordtype">int</span> main(<span class="keywordtype">void</span>)
+ {
+ <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> ret;
+
+ <a class="code" href="group__pfleury__ic2master.html#ga0">i2c_init</a>(); <span class="comment">// initialize I2C library</span>
+
+ <span class="comment">// write 0x75 to EEPROM address 5 (Byte Write) </span>
+ <a class="code" href="group__pfleury__ic2master.html#ga4">i2c_start_wait</a>(Dev24C02+I2C_WRITE); <span class="comment">// set device address and write mode</span>
+ <a class="code" href="group__pfleury__ic2master.html#ga5">i2c_write</a>(0x05); <span class="comment">// write address = 5</span>
+ <a class="code" href="group__pfleury__ic2master.html#ga5">i2c_write</a>(0x75); <span class="comment">// write value 0x75 to EEPROM</span>
+ <a class="code" href="group__pfleury__ic2master.html#ga1">i2c_stop</a>(); <span class="comment">// set stop conditon = release bus</span>
+
+
+ <span class="comment">// read previously written value back from EEPROM address 5 </span>
+ <a class="code" href="group__pfleury__ic2master.html#ga4">i2c_start_wait</a>(Dev24C02+I2C_WRITE); <span class="comment">// set device address and write mode</span>
+
+ <a class="code" href="group__pfleury__ic2master.html#ga5">i2c_write</a>(0x05); <span class="comment">// write address = 5</span>
+ <a class="code" href="group__pfleury__ic2master.html#ga3">i2c_rep_start</a>(Dev24C02+I2C_READ); <span class="comment">// set device address and read mode</span>
+
+ ret = <a class="code" href="group__pfleury__ic2master.html#ga7">i2c_readNak</a>(); <span class="comment">// read one byte from EEPROM</span>
+ <a class="code" href="group__pfleury__ic2master.html#ga1">i2c_stop</a>();
+
+ <span class="keywordflow">for</span>(;;);
+ }
+</pre></div>
+<p>
+<table border="0" cellpadding="0" cellspacing="0">
+<tr><td></td></tr>
+<tr><td colspan="2"><br><h2>Defines</h2></td></tr>
+<tr><td class="memItemLeft" nowrap align="right" valign="top">#define </td><td class="memItemRight" valign="bottom"><a class="el" href="group__pfleury__ic2master.html#ga9">I2C_READ</a> 1</td></tr>
+
+<tr><td class="memItemLeft" nowrap align="right" valign="top">#define </td><td class="memItemRight" valign="bottom"><a class="el" href="group__pfleury__ic2master.html#ga10">I2C_WRITE</a> 0</td></tr>
+
+<tr><td class="memItemLeft" nowrap align="right" valign="top"><a class="anchor" name="ga11" doxytag="pfleury_ic2master::i2c_read"></a>
+#define </td><td class="memItemRight" valign="bottom"><b>i2c_read</b>(ack) (ack) ? i2c_readAck() : i2c_readNak();</td></tr>
+
+<tr><td colspan="2"><br><h2>Functions</h2></td></tr>
+<tr><td class="memItemLeft" nowrap align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="group__pfleury__ic2master.html#ga0">i2c_init</a> (void)</td></tr>
+
+<tr><td class="mdescLeft"> </td><td class="mdescRight">initialize the I2C master interace. Need to be called only once <a href="#ga0"></a><br></td></tr>
+<tr><td class="memItemLeft" nowrap align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="group__pfleury__ic2master.html#ga1">i2c_stop</a> (void)</td></tr>
+
+<tr><td class="mdescLeft"> </td><td class="mdescRight">Terminates the data transfer and releases the I2C bus. <a href="#ga1"></a><br></td></tr>
+<tr><td class="memItemLeft" nowrap align="right" valign="top">unsigned char </td><td class="memItemRight" valign="bottom"><a class="el" href="group__pfleury__ic2master.html#ga2">i2c_start</a> (unsigned char addr)</td></tr>
+
+<tr><td class="mdescLeft"> </td><td class="mdescRight">Issues a start condition and sends address and transfer direction. <a href="#ga2"></a><br></td></tr>
+<tr><td class="memItemLeft" nowrap align="right" valign="top">unsigned char </td><td class="memItemRight" valign="bottom"><a class="el" href="group__pfleury__ic2master.html#ga3">i2c_rep_start</a> (unsigned char addr)</td></tr>
+
+<tr><td class="mdescLeft"> </td><td class="mdescRight">Issues a repeated start condition and sends address and transfer direction. <a href="#ga3"></a><br></td></tr>
+<tr><td class="memItemLeft" nowrap align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="group__pfleury__ic2master.html#ga4">i2c_start_wait</a> (unsigned char addr)</td></tr>
+
+<tr><td class="mdescLeft"> </td><td class="mdescRight">Issues a start condition and sends address and transfer direction. <a href="#ga4"></a><br></td></tr>
+<tr><td class="memItemLeft" nowrap align="right" valign="top">unsigned char </td><td class="memItemRight" valign="bottom"><a class="el" href="group__pfleury__ic2master.html#ga5">i2c_write</a> (unsigned char data)</td></tr>
+
+<tr><td class="mdescLeft"> </td><td class="mdescRight">Send one byte to I2C device. <a href="#ga5"></a><br></td></tr>
+<tr><td class="memItemLeft" nowrap align="right" valign="top">unsigned char </td><td class="memItemRight" valign="bottom"><a class="el" href="group__pfleury__ic2master.html#ga6">i2c_readAck</a> (void)</td></tr>
+
+<tr><td class="mdescLeft"> </td><td class="mdescRight">read one byte from the I2C device, request more data from device <a href="#ga6"></a><br></td></tr>
+<tr><td class="memItemLeft" nowrap align="right" valign="top">unsigned char </td><td class="memItemRight" valign="bottom"><a class="el" href="group__pfleury__ic2master.html#ga7">i2c_readNak</a> (void)</td></tr>
+
+<tr><td class="mdescLeft"> </td><td class="mdescRight">read one byte from the I2C device, read is followed by a stop condition <a href="#ga7"></a><br></td></tr>
+<tr><td class="memItemLeft" nowrap align="right" valign="top">unsigned char </td><td class="memItemRight" valign="bottom"><a class="el" href="group__pfleury__ic2master.html#ga8">i2c_read</a> (unsigned char ack)</td></tr>
+
+<tr><td class="mdescLeft"> </td><td class="mdescRight">read one byte from the I2C device <a href="#ga8"></a><br></td></tr>
+</table>
+<hr><h2>Define Documentation</h2>
+<a class="anchor" name="ga9" doxytag="i2cmaster.h::I2C_READ"></a><p>
+<table class="mdTable" cellpadding="2" cellspacing="0">
+ <tr>
+ <td class="mdRow">
+ <table cellpadding="0" cellspacing="0" border="0">
+ <tr>
+ <td class="md" nowrap valign="top">#define I2C_READ 1 </td>
+ </tr>
+ </table>
+ </td>
+ </tr>
+</table>
+<table cellspacing="5" cellpadding="0" border="0">
+ <tr>
+ <td>
+
+ </td>
+ <td>
+
+<p>
+defines the data direction (reading from I2C device) in <a class="el" href="group__pfleury__ic2master.html#ga2">i2c_start()</a>,<a class="el" href="group__pfleury__ic2master.html#ga3">i2c_rep_start()</a> </td>
+ </tr>
+</table>
+<a class="anchor" name="ga10" doxytag="i2cmaster.h::I2C_WRITE"></a><p>
+<table class="mdTable" cellpadding="2" cellspacing="0">
+ <tr>
+ <td class="mdRow">
+ <table cellpadding="0" cellspacing="0" border="0">
+ <tr>
+ <td class="md" nowrap valign="top">#define I2C_WRITE 0 </td>
+ </tr>
+ </table>
+ </td>
+ </tr>
+</table>
+<table cellspacing="5" cellpadding="0" border="0">
+ <tr>
+ <td>
+
+ </td>
+ <td>
+
+<p>
+defines the data direction (writing to I2C device) in <a class="el" href="group__pfleury__ic2master.html#ga2">i2c_start()</a>,<a class="el" href="group__pfleury__ic2master.html#ga3">i2c_rep_start()</a> </td>
+ </tr>
+</table>
+<hr><h2>Function Documentation</h2>
+<a class="anchor" name="ga0" doxytag="i2cmaster.h::i2c_init"></a><p>
+<table class="mdTable" cellpadding="2" cellspacing="0">
+ <tr>
+ <td class="mdRow">
+ <table cellpadding="0" cellspacing="0" border="0">
+ <tr>
+ <td class="md" nowrap valign="top">void i2c_init </td>
+ <td class="md" valign="top">( </td>
+ <td class="md" nowrap valign="top">void </td>
+ <td class="mdname1" valign="top" nowrap> </td>
+ <td class="md" valign="top"> ) </td>
+ <td class="md" nowrap></td>
+ </tr>
+ </table>
+ </td>
+ </tr>
+</table>
+<table cellspacing="5" cellpadding="0" border="0">
+ <tr>
+ <td>
+
+ </td>
+ <td>
+
+<p>
+initialize the I2C master interace. Need to be called only once
+<p>
+<dl compact><dt><b>Parameters:</b></dt><dd>
+ <table border="0" cellspacing="2" cellpadding="0">
+ <tr><td valign="top"></td><td valign="top"><em>void</em> </td><td></td></tr>
+ </table>
+</dl>
+<dl compact><dt><b>Returns:</b></dt><dd>none </dd></dl>
+ </td>
+ </tr>
+</table>
+<a class="anchor" name="ga1" doxytag="i2cmaster.h::i2c_stop"></a><p>
+<table class="mdTable" cellpadding="2" cellspacing="0">
+ <tr>
+ <td class="mdRow">
+ <table cellpadding="0" cellspacing="0" border="0">
+ <tr>
+ <td class="md" nowrap valign="top">void i2c_stop </td>
+ <td class="md" valign="top">( </td>
+ <td class="md" nowrap valign="top">void </td>
+ <td class="mdname1" valign="top" nowrap> </td>
+ <td class="md" valign="top"> ) </td>
+ <td class="md" nowrap></td>
+ </tr>
+ </table>
+ </td>
+ </tr>
+</table>
+<table cellspacing="5" cellpadding="0" border="0">
+ <tr>
+ <td>
+
+ </td>
+ <td>
+
+<p>
+Terminates the data transfer and releases the I2C bus.
+<p>
+<dl compact><dt><b>Parameters:</b></dt><dd>
+ <table border="0" cellspacing="2" cellpadding="0">
+ <tr><td valign="top"></td><td valign="top"><em>void</em> </td><td></td></tr>
+ </table>
+</dl>
+<dl compact><dt><b>Returns:</b></dt><dd>none </dd></dl>
+ </td>
+ </tr>
+</table>
+<a class="anchor" name="ga2" doxytag="i2cmaster.h::i2c_start"></a><p>
+<table class="mdTable" cellpadding="2" cellspacing="0">
+ <tr>
+ <td class="mdRow">
+ <table cellpadding="0" cellspacing="0" border="0">
+ <tr>
+ <td class="md" nowrap valign="top">unsigned char i2c_start </td>
+ <td class="md" valign="top">( </td>
+ <td class="md" nowrap valign="top">unsigned char </td>
+ <td class="mdname1" valign="top" nowrap> <em>addr</em> </td>
+ <td class="md" valign="top"> ) </td>
+ <td class="md" nowrap></td>
+ </tr>
+ </table>
+ </td>
+ </tr>
+</table>
+<table cellspacing="5" cellpadding="0" border="0">
+ <tr>
+ <td>
+
+ </td>
+ <td>
+
+<p>
+Issues a start condition and sends address and transfer direction.
+<p>
+<dl compact><dt><b>Parameters:</b></dt><dd>
+ <table border="0" cellspacing="2" cellpadding="0">
+ <tr><td valign="top"></td><td valign="top"><em>addr</em> </td><td>address and transfer direction of I2C device </td></tr>
+ </table>
+</dl>
+<dl compact><dt><b>Return values:</b></dt><dd>
+ <table border="0" cellspacing="2" cellpadding="0">
+ <tr><td valign="top"></td><td valign="top"><em>0</em> </td><td>device accessible </td></tr>
+ <tr><td valign="top"></td><td valign="top"><em>1</em> </td><td>failed to access device </td></tr>
+ </table>
+</dl>
+ </td>
+ </tr>
+</table>
+<a class="anchor" name="ga3" doxytag="i2cmaster.h::i2c_rep_start"></a><p>
+<table class="mdTable" cellpadding="2" cellspacing="0">
+ <tr>
+ <td class="mdRow">
+ <table cellpadding="0" cellspacing="0" border="0">
+ <tr>
+ <td class="md" nowrap valign="top">unsigned char i2c_rep_start </td>
+ <td class="md" valign="top">( </td>
+ <td class="md" nowrap valign="top">unsigned char </td>
+ <td class="mdname1" valign="top" nowrap> <em>addr</em> </td>
+ <td class="md" valign="top"> ) </td>
+ <td class="md" nowrap></td>
+ </tr>
+ </table>
+ </td>
+ </tr>
+</table>
+<table cellspacing="5" cellpadding="0" border="0">
+ <tr>
+ <td>
+
+ </td>
+ <td>
+
+<p>
+Issues a repeated start condition and sends address and transfer direction.
+<p>
+<dl compact><dt><b>Parameters:</b></dt><dd>
+ <table border="0" cellspacing="2" cellpadding="0">
+ <tr><td valign="top"></td><td valign="top"><em>addr</em> </td><td>address and transfer direction of I2C device </td></tr>
+ </table>
+</dl>
+<dl compact><dt><b>Return values:</b></dt><dd>
+ <table border="0" cellspacing="2" cellpadding="0">
+ <tr><td valign="top"></td><td valign="top"><em>0</em> </td><td>device accessible </td></tr>
+ <tr><td valign="top"></td><td valign="top"><em>1</em> </td><td>failed to access device </td></tr>
+ </table>
+</dl>
+ </td>
+ </tr>
+</table>
+<a class="anchor" name="ga4" doxytag="i2cmaster.h::i2c_start_wait"></a><p>
+<table class="mdTable" cellpadding="2" cellspacing="0">
+ <tr>
+ <td class="mdRow">
+ <table cellpadding="0" cellspacing="0" border="0">
+ <tr>
+ <td class="md" nowrap valign="top">void i2c_start_wait </td>
+ <td class="md" valign="top">( </td>
+ <td class="md" nowrap valign="top">unsigned char </td>
+ <td class="mdname1" valign="top" nowrap> <em>addr</em> </td>
+ <td class="md" valign="top"> ) </td>
+ <td class="md" nowrap></td>
+ </tr>
+ </table>
+ </td>
+ </tr>
+</table>
+<table cellspacing="5" cellpadding="0" border="0">
+ <tr>
+ <td>
+
+ </td>
+ <td>
+
+<p>
+Issues a start condition and sends address and transfer direction.
+<p>
+If device is busy, use ack polling to wait until device ready <dl compact><dt><b>Parameters:</b></dt><dd>
+ <table border="0" cellspacing="2" cellpadding="0">
+ <tr><td valign="top"></td><td valign="top"><em>addr</em> </td><td>address and transfer direction of I2C device </td></tr>
+ </table>
+</dl>
+<dl compact><dt><b>Returns:</b></dt><dd>none </dd></dl>
+ </td>
+ </tr>
+</table>
+<a class="anchor" name="ga5" doxytag="i2cmaster.h::i2c_write"></a><p>
+<table class="mdTable" cellpadding="2" cellspacing="0">
+ <tr>
+ <td class="mdRow">
+ <table cellpadding="0" cellspacing="0" border="0">
+ <tr>
+ <td class="md" nowrap valign="top">unsigned char i2c_write </td>
+ <td class="md" valign="top">( </td>
+ <td class="md" nowrap valign="top">unsigned char </td>
+ <td class="mdname1" valign="top" nowrap> <em>data</em> </td>
+ <td class="md" valign="top"> ) </td>
+ <td class="md" nowrap></td>
+ </tr>
+ </table>
+ </td>
+ </tr>
+</table>
+<table cellspacing="5" cellpadding="0" border="0">
+ <tr>
+ <td>
+
+ </td>
+ <td>
+
+<p>
+Send one byte to I2C device.
+<p>
+<dl compact><dt><b>Parameters:</b></dt><dd>
+ <table border="0" cellspacing="2" cellpadding="0">
+ <tr><td valign="top"></td><td valign="top"><em>data</em> </td><td>byte to be transfered </td></tr>
+ </table>
+</dl>
+<dl compact><dt><b>Return values:</b></dt><dd>
+ <table border="0" cellspacing="2" cellpadding="0">
+ <tr><td valign="top"></td><td valign="top"><em>0</em> </td><td>write successful </td></tr>
+ <tr><td valign="top"></td><td valign="top"><em>1</em> </td><td>write failed </td></tr>
+ </table>
+</dl>
+ </td>
+ </tr>
+</table>
+<a class="anchor" name="ga6" doxytag="i2cmaster.h::i2c_readAck"></a><p>
+<table class="mdTable" cellpadding="2" cellspacing="0">
+ <tr>
+ <td class="mdRow">
+ <table cellpadding="0" cellspacing="0" border="0">
+ <tr>
+ <td class="md" nowrap valign="top">unsigned char i2c_readAck </td>
+ <td class="md" valign="top">( </td>
+ <td class="md" nowrap valign="top">void </td>
+ <td class="mdname1" valign="top" nowrap> </td>
+ <td class="md" valign="top"> ) </td>
+ <td class="md" nowrap></td>
+ </tr>
+ </table>
+ </td>
+ </tr>
+</table>
+<table cellspacing="5" cellpadding="0" border="0">
+ <tr>
+ <td>
+
+ </td>
+ <td>
+
+<p>
+read one byte from the I2C device, request more data from device
+<p>
+<dl compact><dt><b>Returns:</b></dt><dd>byte read from I2C device </dd></dl>
+ </td>
+ </tr>
+</table>
+<a class="anchor" name="ga7" doxytag="i2cmaster.h::i2c_readNak"></a><p>
+<table class="mdTable" cellpadding="2" cellspacing="0">
+ <tr>
+ <td class="mdRow">
+ <table cellpadding="0" cellspacing="0" border="0">
+ <tr>
+ <td class="md" nowrap valign="top">unsigned char i2c_readNak </td>
+ <td class="md" valign="top">( </td>
+ <td class="md" nowrap valign="top">void </td>
+ <td class="mdname1" valign="top" nowrap> </td>
+ <td class="md" valign="top"> ) </td>
+ <td class="md" nowrap></td>
+ </tr>
+ </table>
+ </td>
+ </tr>
+</table>
+<table cellspacing="5" cellpadding="0" border="0">
+ <tr>
+ <td>
+
+ </td>
+ <td>
+
+<p>
+read one byte from the I2C device, read is followed by a stop condition
+<p>
+<dl compact><dt><b>Returns:</b></dt><dd>byte read from I2C device </dd></dl>
+ </td>
+ </tr>
+</table>
+<a class="anchor" name="ga8" doxytag="i2cmaster.h::i2c_read"></a><p>
+<table class="mdTable" cellpadding="2" cellspacing="0">
+ <tr>
+ <td class="mdRow">
+ <table cellpadding="0" cellspacing="0" border="0">
+ <tr>
+ <td class="md" nowrap valign="top">unsigned char i2c_read </td>
+ <td class="md" valign="top">( </td>
+ <td class="md" nowrap valign="top">unsigned char </td>
+ <td class="mdname1" valign="top" nowrap> <em>ack</em> </td>
+ <td class="md" valign="top"> ) </td>
+ <td class="md" nowrap></td>
+ </tr>
+ </table>
+ </td>
+ </tr>
+</table>
+<table cellspacing="5" cellpadding="0" border="0">
+ <tr>
+ <td>
+
+ </td>
+ <td>
+
+<p>
+read one byte from the I2C device
+<p>
+Implemented as a macro, which calls either i2c_readAck or i2c_readNak<p>
+<dl compact><dt><b>Parameters:</b></dt><dd>
+ <table border="0" cellspacing="2" cellpadding="0">
+ <tr><td valign="top"></td><td valign="top"><em>ack</em> </td><td>1 send ack, request more data from device<br>
+ 0 send nak, read is followed by a stop condition </td></tr>
+ </table>
+</dl>
+<dl compact><dt><b>Returns:</b></dt><dd>byte read from I2C device </dd></dl>
+ </td>
+ </tr>
+</table>
+<hr size="1"><address style="align: right;"><small>Generated on Tue Mar 29 16:54:08 2005 for AVR-GCC libraries by
+<a href="http://www.doxygen.org/index.html">
+<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.1 </small></address>
+</body>
+</html>
--- /dev/null
+;*************************************************************************\r
+; Title : I2C (Single) Master Implementation\r
+; Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury\r
+; based on Atmel Appl. Note AVR300\r
+; File: $Id: i2cmaster.S,v 1.11 2003/10/16 18:16:07 peter Exp $\r
+; Software: AVR-GCC 3.3\r
+; Target: any AVR device\r
+;\r
+; DESCRIPTION\r
+; Basic routines for communicating with I2C slave devices. This\r
+; "single" master implementation is limited to one bus master on the\r
+; I2C bus. \r
+; \r
+; Based on the Atmel Application Note AVR300, corrected and adapted \r
+; to GNU assembler and AVR-GCC C call interface\r
+; Replaced the incorrect quarter period delays found in AVR300 with \r
+; half period delays. \r
+;\r
+; USAGE\r
+; These routines can be called from C, refere to file i2cmaster.h.\r
+; See example test_i2cmaster.c \r
+; Adapt the SCL and SDA port and pin definitions and eventually \r
+; the delay routine to your target !\r
+; Use 4.7k pull-up resistor on the SDA and SCL pin.\r
+;\r
+; NOTES\r
+; The I2C routines can be called either from non-interrupt or\r
+; interrupt routines, not both.\r
+;\r
+;*************************************************************************\r
+\r
+#if (__GNUC__ * 100 + __GNUC_MINOR__) < 303\r
+#error "This library requires AVR-GCC 3.3 or later, update to newer AVR-GCC compiler !"\r
+#endif\r
+\r
+\r
+#include <avr/io.h>\r
+\r
+\r
+\r
+;***** Adapt these SCA and SCL port and pin definition to your target !!\r
+;\r
+#define SDA 4 // SDA Port D, Pin 4 \r
+#define SCL 5 // SCL Port D, Pin 5\r
+#define SDA_PORT PORTD // SDA Port D\r
+#define SCL_PORT PORTD // SCL Port D \r
+\r
+;******\r
+\r
+;-- map the IO register back into the IO address space\r
+#define SDA_DDR (_SFR_IO_ADDR(SDA_PORT) - 1)\r
+#define SCL_DDR (_SFR_IO_ADDR(SCL_PORT) - 1)\r
+#define SDA_OUT _SFR_IO_ADDR(SDA_PORT)\r
+#define SCL_OUT _SFR_IO_ADDR(SCL_PORT)\r
+#define SDA_IN (_SFR_IO_ADDR(SDA_PORT) - 2)\r
+#define SCL_IN (_SFR_IO_ADDR(SCL_PORT) - 2)\r
+\r
+\r
+#ifndef __tmp_reg__\r
+#define __tmp_reg__ 0\r
+#endif\r
+\r
+\r
+ .section .text\r
+\r
+;*************************************************************************\r
+; delay half period\r
+; For I2C in normal mode (100kHz), use T/2 > 5us\r
+; For I2C in fast mode (400kHz), use T/2 > 1.3us\r
+;*************************************************************************\r
+ .stabs "",100,0,0,i2c_delay_T2\r
+ .stabs "i2cmaster.S",100,0,0,i2c_delay_T2\r
+ .func i2c_delay_T2 ; delay 5.0 microsec with 4 Mhz crystal \r
+i2c_delay_T2: ; 4 cycles\r
+ rjmp 1f ; 2 "\r
+1: rjmp 2f ; 2 "\r
+2: rjmp 3f ; 2 "\r
+3: rjmp 4f ; 2 "\r
+4: rjmp 5f ; 2 "\r
+5: rjmp 6f ; 2 "\r
+6: nop ; 1 "\r
+ ret ; 3 "\r
+ .endfunc ; total 20 cyles = 5.0 microsec with 4 Mhz crystal \r
+\r
+\r
+;*************************************************************************\r
+; Initialization of the I2C bus interface. Need to be called only once\r
+; \r
+; extern void i2c_init(void)\r
+;*************************************************************************\r
+ .global i2c_init\r
+ .func i2c_init\r
+i2c_init:\r
+ cbi SDA_DDR,SDA ;release SDA\r
+ cbi SCL_DDR,SCL ;release SCL\r
+ cbi SDA_OUT,SDA\r
+ cbi SCL_OUT,SCL\r
+ ret\r
+ .endfunc\r
+\r
+\r
+;************************************************************************* \r
+; Issues a start condition and sends address and transfer direction.\r
+; return 0 = device accessible, 1= failed to access device\r
+;\r
+; extern unsigned char i2c_start(unsigned char addr);\r
+; addr = r24, return = r25(=0):r24\r
+;*************************************************************************\r
+\r
+ .global i2c_start\r
+ .func i2c_start\r
+i2c_start:\r
+ sbi SDA_DDR,SDA ;force SDA low\r
+ rcall i2c_delay_T2 ;delay T/2\r
+ \r
+ rcall i2c_write ;write address\r
+ ret\r
+ .endfunc \r
+\r
+\r
+;*************************************************************************\r
+; Issues a repeated start condition and sends address and transfer direction.\r
+; return 0 = device accessible, 1= failed to access device\r
+;\r
+; extern unsigned char i2c_rep_start(unsigned char addr);\r
+; addr = r24, return = r25(=0):r24\r
+;*************************************************************************\r
+\r
+ .global i2c_rep_start\r
+ .func i2c_rep_start\r
+i2c_rep_start:\r
+ sbi SCL_DDR,SCL ;force SCL low\r
+ rcall i2c_delay_T2 ;delay T/2\r
+ cbi SDA_DDR,SDA ;release SDA\r
+ rcall i2c_delay_T2 ;delay T/2\r
+ cbi SCL_DDR,SCL ;release SCL\r
+ rcall i2c_delay_T2 ;delay T/2\r
+ sbi SDA_DDR,SDA ;force SDA low\r
+ rcall i2c_delay_T2 ;delay T/2\r
+ \r
+ rcall i2c_write ;write address\r
+ ret\r
+ .endfunc\r
+\r
+\r
+;************************************************************************* \r
+; Issues a start condition and sends address and transfer direction.\r
+; If device is busy, use ack polling to wait until device is ready\r
+;\r
+; extern void i2c_start_wait(unsigned char addr);\r
+; addr = r24\r
+;*************************************************************************\r
+\r
+ .global i2c_start_wait\r
+ .func i2c_start_wait\r
+i2c_start_wait:\r
+ mov __tmp_reg__,r24\r
+i2c_start_wait1:\r
+ sbi SDA_DDR,SDA ;force SDA low\r
+ rcall i2c_delay_T2 ;delay T/2\r
+ mov r24,__tmp_reg__\r
+ rcall i2c_write ;write address\r
+ tst r24 ;if device not busy -> done\r
+ breq i2c_start_wait_done\r
+ rcall i2c_stop ;terminate write operation\r
+ rjmp i2c_start_wait1 ;device busy, poll ack again\r
+i2c_start_wait_done:\r
+ ret\r
+ .endfunc \r
+\r
+\r
+;*************************************************************************\r
+; Terminates the data transfer and releases the I2C bus\r
+;\r
+; extern void i2c_stop(void)\r
+;*************************************************************************\r
+\r
+ .global i2c_stop\r
+ .func i2c_stop\r
+i2c_stop:\r
+ sbi SCL_DDR,SCL ;force SCL low\r
+ sbi SDA_DDR,SDA ;force SDA low\r
+ rcall i2c_delay_T2 ;delay T/2\r
+ cbi SCL_DDR,SCL ;release SCL\r
+ rcall i2c_delay_T2 ;delay T/2\r
+ cbi SDA_DDR,SDA ;release SDA\r
+ rcall i2c_delay_T2 ;delay T/2\r
+ ret\r
+ .endfunc\r
+\r
+\r
+;*************************************************************************\r
+; Send one byte to I2C device\r
+; return 0 = write successful, 1 = write failed\r
+;\r
+; extern unsigned char i2c_write( unsigned char data );\r
+; data = r24, return = r25(=0):r24\r
+;*************************************************************************\r
+ .global i2c_write\r
+ .func i2c_write\r
+i2c_write:\r
+ sec ;set carry flag\r
+ rol r24 ;shift in carry and out bit one\r
+ rjmp i2c_write_first\r
+i2c_write_bit:\r
+ lsl r24 ;if transmit register empty\r
+i2c_write_first:\r
+ breq i2c_get_ack\r
+ sbi SCL_DDR,SCL ;force SCL low\r
+ brcc i2c_write_low\r
+ nop\r
+ cbi SDA_DDR,SDA ;release SDA\r
+ rjmp i2c_write_high\r
+i2c_write_low:\r
+ sbi SDA_DDR,SDA ;force SDA low\r
+ rjmp i2c_write_high\r
+i2c_write_high:\r
+ rcall i2c_delay_T2 ;delay T/2\r
+ cbi SCL_DDR,SCL ;release SCL\r
+ rcall i2c_delay_T2 ;delay T/2\r
+ rjmp i2c_write_bit\r
+ \r
+i2c_get_ack:\r
+ sbi SCL_DDR,SCL ;force SCL low\r
+ cbi SDA_DDR,SDA ;release SDA\r
+ rcall i2c_delay_T2 ;delay T/2\r
+ cbi SCL_DDR,SCL ;release SCL\r
+i2c_ack_wait:\r
+ sbis SCL_IN,SCL ;wait SCL high (in case wait states are inserted)\r
+ rjmp i2c_ack_wait\r
+ \r
+ clr r24 ;return 0\r
+ sbic SDA_IN,SDA ;if SDA high -> return 1\r
+ ldi r24,1\r
+ rcall i2c_delay_T2 ;delay T/2\r
+ clr r25\r
+ ret\r
+ .endfunc\r
+\r
+\r
+\r
+;*************************************************************************\r
+; read one byte from the I2C device, send ack or nak to device\r
+; (ack=1, send ack, request more data from device \r
+; ack=0, send nak, read is followed by a stop condition)\r
+;\r
+; extern unsigned char i2c_read(unsigned char ack);\r
+; ack = r24, return = r25(=0):r24\r
+; extern unsigned char i2c_readAck(void);\r
+; extern unsigned char i2c_readNak(void);\r
+; return = r25(=0):r24\r
+;*************************************************************************\r
+ .global i2c_readAck\r
+ .global i2c_readNak\r
+ .global i2c_read \r
+ .func i2c_read\r
+i2c_readNak:\r
+ clr r24\r
+ rjmp i2c_read\r
+i2c_readAck:\r
+ ldi r24,0x01\r
+i2c_read:\r
+ ldi r23,0x01 ;data = 0x01\r
+i2c_read_bit:\r
+ sbi SCL_DDR,SCL ;force SCL low\r
+ cbi SDA_DDR,SDA ;release SDA (from previous ACK)\r
+ rcall i2c_delay_T2 ;delay T/2\r
+ \r
+ cbi SCL_DDR,SCL ;release SCL\r
+ rcall i2c_delay_T2 ;delay T/2\r
+ \r
+ clc ;clear carry flag\r
+ sbic SDA_IN,SDA ;if SDA is high\r
+ sec ; set carry flag\r
+ \r
+ rol r23 ;store bit\r
+ brcc i2c_read_bit ;while receive register not full\r
+ \r
+i2c_put_ack:\r
+ sbi SCL_DDR,SCL ;force SCL low \r
+ cpi r24,1\r
+ breq i2c_put_ack_low ;if (ack=0)\r
+ cbi SDA_DDR,SDA ; release SDA\r
+ rjmp i2c_put_ack_high\r
+i2c_put_ack_low: ;else\r
+ sbi SDA_DDR,SDA ; force SDA low\r
+i2c_put_ack_high:\r
+ rcall i2c_delay_T2 ;delay T/2\r
+ cbi SCL_DDR,SCL ;release SCL\r
+i2c_put_ack_wait:\r
+ sbis SCL_IN,SCL ;wait SCL high\r
+ rjmp i2c_put_ack_wait\r
+ rcall i2c_delay_T2 ;delay T/2\r
+ mov r24,r23\r
+ clr r25\r
+ ret\r
+ .endfunc\r
+\r
--- /dev/null
+#ifndef _I2CMASTER_H\r
+#define _I2CMASTER_H 1\r
+/************************************************************************* \r
+* Title: C include file for the I2C master interface \r
+* (i2cmaster.S or twimaster.c)\r
+* Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury\r
+* File: $Id: i2cmaster.h,v 1.10 2005/03/06 22:39:57 Peter Exp $\r
+* Software: AVR-GCC 3.4.3 / avr-libc 1.2.3\r
+* Target: any AVR device\r
+* Usage: see Doxygen manual\r
+**************************************************************************/\r
+\r
+#ifdef DOXYGEN\r
+/**\r
+ @defgroup pfleury_ic2master I2C Master library\r
+ @code #include <i2cmaster.h> @endcode\r
+ \r
+ @brief I2C (TWI) Master Software Library\r
+\r
+ Basic routines for communicating with I2C slave devices. This single master \r
+ implementation is limited to one bus master on the I2C bus. \r
+\r
+ This I2c library is implemented as a compact assembler software implementation of the I2C protocol \r
+ which runs on any AVR (i2cmaster.S) and as a TWI hardware interface for all AVR with built-in TWI hardware (twimaster.c).\r
+ Since the API for these two implementations is exactly the same, an application can be linked either against the\r
+ software I2C implementation or the hardware I2C implementation.\r
+\r
+ Use 4.7k pull-up resistor on the SDA and SCL pin.\r
+ \r
+ Adapt the SCL and SDA port and pin definitions and eventually the delay routine in the module \r
+ i2cmaster.S to your target when using the software I2C implementation ! \r
+ \r
+ Adjust the CPU clock frequence F_CPU in twimaster.c or in the Makfile when using the TWI hardware implementaion.\r
+\r
+ @note \r
+ The module i2cmaster.S is based on the Atmel Application Note AVR300, corrected and adapted \r
+ to GNU assembler and AVR-GCC C call interface.\r
+ Replaced the incorrect quarter period delays found in AVR300 with \r
+ half period delays. \r
+ \r
+ @author Peter Fleury pfleury@gmx.ch http://jump.to/fleury\r
+\r
+ @par API Usage Example\r
+ The following code shows typical usage of this library, see example test_i2cmaster.c\r
+\r
+ @code\r
+\r
+ #include <i2cmaster.h>\r
+\r
+\r
+ #define Dev24C02 0xA2 // device address of EEPROM 24C02, see datasheet\r
+\r
+ int main(void)\r
+ {\r
+ unsigned char ret;\r
+\r
+ i2c_init(); // initialize I2C library\r
+\r
+ // write 0x75 to EEPROM address 5 (Byte Write) \r
+ i2c_start_wait(Dev24C02+I2C_WRITE); // set device address and write mode\r
+ i2c_write(0x05); // write address = 5\r
+ i2c_write(0x75); // write value 0x75 to EEPROM\r
+ i2c_stop(); // set stop conditon = release bus\r
+\r
+\r
+ // read previously written value back from EEPROM address 5 \r
+ i2c_start_wait(Dev24C02+I2C_WRITE); // set device address and write mode\r
+\r
+ i2c_write(0x05); // write address = 5\r
+ i2c_rep_start(Dev24C02+I2C_READ); // set device address and read mode\r
+\r
+ ret = i2c_readNak(); // read one byte from EEPROM\r
+ i2c_stop();\r
+\r
+ for(;;);\r
+ }\r
+ @endcode\r
+\r
+*/\r
+#endif /* DOXYGEN */\r
+\r
+/**@{*/\r
+\r
+#if (__GNUC__ * 100 + __GNUC_MINOR__) < 304\r
+#error "This library requires AVR-GCC 3.4 or later, update to newer AVR-GCC compiler !"\r
+#endif\r
+\r
+#include <avr/io.h>\r
+\r
+/** defines the data direction (reading from I2C device) in i2c_start(),i2c_rep_start() */\r
+#define I2C_READ 1\r
+\r
+/** defines the data direction (writing to I2C device) in i2c_start(),i2c_rep_start() */\r
+#define I2C_WRITE 0\r
+\r
+\r
+/**\r
+ @brief initialize the I2C master interace. Need to be called only once \r
+ @param void\r
+ @return none\r
+ */\r
+extern void i2c_init(void);\r
+\r
+\r
+/** \r
+ @brief Terminates the data transfer and releases the I2C bus \r
+ @param void\r
+ @return none\r
+ */\r
+extern void i2c_stop(void);\r
+\r
+\r
+/** \r
+ @brief Issues a start condition and sends address and transfer direction \r
+ \r
+ @param addr address and transfer direction of I2C device\r
+ @retval 0 device accessible \r
+ @retval 1 failed to access device \r
+ */\r
+extern unsigned char i2c_start(unsigned char addr);\r
+\r
+\r
+/**\r
+ @brief Issues a repeated start condition and sends address and transfer direction \r
+\r
+ @param addr address and transfer direction of I2C device\r
+ @retval 0 device accessible\r
+ @retval 1 failed to access device\r
+ */\r
+extern unsigned char i2c_rep_start(unsigned char addr);\r
+\r
+\r
+/**\r
+ @brief Issues a start condition and sends address and transfer direction \r
+ \r
+ If device is busy, use ack polling to wait until device ready \r
+ @param addr address and transfer direction of I2C device\r
+ @return none\r
+ */\r
+extern void i2c_start_wait(unsigned char addr);\r
+\r
+ \r
+/**\r
+ @brief Send one byte to I2C device\r
+ @param data byte to be transfered\r
+ @retval 0 write successful\r
+ @retval 1 write failed\r
+ */\r
+extern unsigned char i2c_write(unsigned char data);\r
+\r
+\r
+/**\r
+ @brief read one byte from the I2C device, request more data from device \r
+ @return byte read from I2C device\r
+ */\r
+extern unsigned char i2c_readAck(void);\r
+\r
+/**\r
+ @brief read one byte from the I2C device, read is followed by a stop condition \r
+ @return byte read from I2C device\r
+ */\r
+extern unsigned char i2c_readNak(void);\r
+\r
+/** \r
+ @brief read one byte from the I2C device\r
+ \r
+ Implemented as a macro, which calls either i2c_readAck or i2c_readNak\r
+ \r
+ @param ack 1 send ack, request more data from device<br>\r
+ 0 send nak, read is followed by a stop condition \r
+ @return byte read from I2C device\r
+ */\r
+extern unsigned char i2c_read(unsigned char ack);\r
+#define i2c_read(ack) (ack) ? i2c_readAck() : i2c_readNak(); \r
+\r
+\r
+/**@}*/\r
+#endif\r
--- /dev/null
+# Makefile to compile and link the I2C Master library and test program\r
+#\r
+# based on\r
+# WinAVR Sample makefile written by Eric B. Weddington, Jörg Wunsch, et al.\r
+#\r
+# Note:\r
+# The warnings which appear while converting ELF to AVR Extended COFF (make extcoff)\r
+# can be ignored\r
+#\r
+#\r
+# On command line:\r
+#\r
+# make all = Make software.\r
+#\r
+# make clean = Clean out built project files.\r
+#\r
+# make coff = Convert ELF to AVR COFF (for use with AVR Studio 3.x or VMLAB).\r
+#\r
+# make extcoff = Convert ELF to AVR Extended COFF (for use with AVR Studio\r
+# 4.07 or greater).\r
+#\r
+# make program = Download the hex file to the device, using avrdude. Please\r
+# customize the avrdude settings below first!\r
+#\r
+# make filename.s = Just compile filename.c into the assembler code only\r
+#\r
+# To rebuild project do "make clean" then "make all".\r
+#\r
+\r
+\r
+# MCU name\r
+MCU = at90s8515\r
+\r
+# Output format. (can be srec, ihex, binary)\r
+FORMAT = ihex\r
+\r
+# Optimization level, can be [0, 1, 2, 3, s]. 0 turns off optimization.\r
+# (Note: 3 is not always the best optimization level. See avr-libc FAQ.)\r
+OPT = s\r
+\r
+\r
+# Target file name (without extension).\r
+TARGET = test_i2cmaster\r
+\r
+\r
+# List C source files here. (C dependencies are automatically generated.)\r
+SRC = $(TARGET).c\r
+\r
+# List Assembler source files here.\r
+# Make them always end in a capital .S. Files ending in a lowercase .s\r
+# will not be considered source files but generated files (assembler\r
+# output from the compiler), and will be deleted upon "make clean"!\r
+# Even though the DOS/Win* filesystem matches both .s and .S the same,\r
+# it will preserve the spelling of the filenames, and gcc itself does\r
+# care about how the name is spelled on its command-line.\r
+ASRC = i2cmaster.S\r
+\r
+\r
+# List any extra directories to look for include files here.\r
+# Each directory must be seperated by a space.\r
+EXTRAINCDIRS = \r
+\r
+\r
+# Optional compiler flags.\r
+# -g: generate debugging information (for GDB, or for COFF conversion)\r
+# -O*: optimization level\r
+# -f...: tuning, see gcc manual and avr-libc documentation\r
+# -Wall...: warning level\r
+# -Wa,...: tell GCC to pass this to the assembler.\r
+# -ahlms: create assembler listing\r
+CFLAGS = -g -O$(OPT) \\r
+-funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums \\r
+-Wall -Wstrict-prototypes \\r
+-Wa,-adhlns=$(<:.c=.lst) \\r
+$(patsubst %,-I%,$(EXTRAINCDIRS))\r
+\r
+\r
+# Set a "language standard" compiler flag.\r
+# Unremark just one line below to set the language standard to use.\r
+# gnu99 = C99 + GNU extensions. See GCC manual for more information.\r
+#CFLAGS += -std=c89\r
+#CFLAGS += -std=gnu89\r
+#CFLAGS += -std=c99\r
+CFLAGS += -std=gnu99\r
+\r
+\r
+\r
+# Optional assembler flags.\r
+# -Wa,...: tell GCC to pass this to the assembler.\r
+# -ahlms: create listing\r
+# -gstabs: have the assembler create line number information; note that\r
+# for use in COFF files, additional information about filenames\r
+# and function names needs to be present in the assembler source\r
+# files -- see avr-libc docs [FIXME: not yet described there]\r
+ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs \r
+\r
+\r
+\r
+# Optional linker flags.\r
+# -Wl,...: tell GCC to pass this to linker.\r
+# -Map: create map file\r
+# --cref: add cross reference to map file\r
+LDFLAGS = -Wl,-Map=$(TARGET).map,--cref\r
+\r
+\r
+\r
+# Additional libraries\r
+\r
+# Minimalistic printf version\r
+#LDFLAGS += -Wl,-u,vfprintf -lprintf_min\r
+\r
+# Floating point printf version (requires -lm below)\r
+#LDFLAGS += -Wl,-u,vfprintf -lprintf_flt\r
+\r
+# -lm = math library\r
+LDFLAGS += -lm\r
+\r
+\r
+\r
+\r
+# Programming support using avrdude. Settings and variables.\r
+\r
+# Programming hardware: alf avr910 avrisp bascom bsd \r
+# dt006 pavr picoweb pony-stk200 sp12 stk200 stk500\r
+#\r
+# Type: avrdude -c ?\r
+# to get a full listing.\r
+#\r
+AVRDUDE_PROGRAMMER = stk500\r
+\r
+\r
+AVRDUDE_PORT = com1 # programmer connected to serial device\r
+#AVRDUDE_PORT = lpt1 # programmer connected to parallel port\r
+\r
+AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex\r
+#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep\r
+\r
+AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)\r
+\r
+# Uncomment the following if you want avrdude's erase cycle counter.\r
+# Note that this counter needs to be initialized first using -Yn,\r
+# see avrdude manual.\r
+#AVRDUDE_ERASE += -y\r
+\r
+# Uncomment the following if you do /not/ wish a verification to be\r
+# performed after programming the device.\r
+#AVRDUDE_FLAGS += -V\r
+\r
+# Increase verbosity level. Please use this when submitting bug\r
+# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude> \r
+# to submit bug reports.\r
+#AVRDUDE_FLAGS += -v -v\r
+\r
+\r
+\r
+\r
+# ---------------------------------------------------------------------------\r
+\r
+# Define directories, if needed.\r
+DIRAVR = c:/winavr\r
+DIRAVRBIN = $(DIRAVR)/bin\r
+DIRAVRUTILS = $(DIRAVR)/utils/bin\r
+DIRINC = .\r
+DIRLIB = $(DIRAVR)/avr/lib\r
+\r
+\r
+# Define programs and commands.\r
+SHELL = sh\r
+\r
+CC = avr-gcc\r
+\r
+OBJCOPY = avr-objcopy\r
+OBJDUMP = avr-objdump\r
+SIZE = avr-size\r
+\r
+\r
+# Programming support using avrdude.\r
+AVRDUDE = avrdude\r
+\r
+\r
+REMOVE = rm -f\r
+COPY = cp\r
+\r
+HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex\r
+ELFSIZE = $(SIZE) -A $(TARGET).elf\r
+\r
+\r
+\r
+# Define Messages\r
+# English\r
+MSG_ERRORS_NONE = Errors: none\r
+MSG_BEGIN = -------- begin --------\r
+MSG_END = -------- end --------\r
+MSG_SIZE_BEFORE = Size before: \r
+MSG_SIZE_AFTER = Size after:\r
+MSG_COFF = Converting to AVR COFF:\r
+MSG_EXTENDED_COFF = Converting to AVR Extended COFF:\r
+MSG_FLASH = Creating load file for Flash:\r
+MSG_EEPROM = Creating load file for EEPROM:\r
+MSG_EXTENDED_LISTING = Creating Extended Listing:\r
+MSG_SYMBOL_TABLE = Creating Symbol Table:\r
+MSG_LINKING = Linking:\r
+MSG_COMPILING = Compiling:\r
+MSG_ASSEMBLING = Assembling:\r
+MSG_CLEANING = Cleaning project:\r
+\r
+\r
+\r
+\r
+# Define all object files.\r
+OBJ = $(SRC:.c=.o) $(ASRC:.S=.o) \r
+\r
+# Define all listing files.\r
+LST = $(ASRC:.S=.lst) $(SRC:.c=.lst)\r
+\r
+# Combine all necessary flags and optional flags.\r
+# Add target processor to flags.\r
+ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS)\r
+ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)\r
+\r
+\r
+\r
+# Default target.\r
+all: begin gccversion sizebefore $(TARGET).elf $(TARGET).hex $(TARGET).eep \\r
+ $(TARGET).lss $(TARGET).sym sizeafter finished end\r
+\r
+\r
+# Eye candy.\r
+# AVR Studio 3.x does not check make's exit code but relies on\r
+# the following magic strings to be generated by the compile job.\r
+begin:\r
+ @echo\r
+ @echo $(MSG_BEGIN)\r
+\r
+finished:\r
+ @echo $(MSG_ERRORS_NONE)\r
+\r
+end:\r
+ @echo $(MSG_END)\r
+ @echo\r
+\r
+\r
+# Display size of file.\r
+sizebefore:\r
+ @if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); echo; fi\r
+\r
+sizeafter:\r
+ @if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); echo; fi\r
+\r
+\r
+\r
+# Display compiler version information.\r
+gccversion : \r
+ @$(CC) --version\r
+\r
+\r
+\r
+\r
+# Convert ELF to COFF for use in debugging / simulating in\r
+# AVR Studio or VMLAB.\r
+COFFCONVERT=$(OBJCOPY) --debugging \\r
+ --change-section-address .data-0x800000 \\r
+ --change-section-address .bss-0x800000 \\r
+ --change-section-address .noinit-0x800000 \\r
+ --change-section-address .eeprom-0x810000 \r
+\r
+\r
+coff: $(TARGET).elf\r
+ @echo\r
+ @echo $(MSG_COFF) $(TARGET).cof\r
+ $(COFFCONVERT) -O coff-avr $< $(TARGET).cof\r
+\r
+\r
+extcoff: $(TARGET).elf\r
+ @echo\r
+ @echo $(MSG_EXTENDED_COFF) $(TARGET).cof\r
+ $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof\r
+\r
+\r
+\r
+\r
+# Program the device. \r
+program: $(TARGET).hex $(TARGET).eep\r
+ $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)\r
+\r
+\r
+\r
+\r
+# Create final output files (.hex, .eep) from ELF output file.\r
+%.hex: %.elf\r
+ @echo
+ @echo $(MSG_FLASH) $@
+ $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@\r
+\r
+%.eep: %.elf\r
+ @echo
+ @echo $(MSG_EEPROM) $@
+ -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \\r
+ --change-section-lma .eeprom=0 -O $(FORMAT) $< $@\r
+\r
+# Create extended listing file from ELF output file.\r
+%.lss: %.elf\r
+ @echo
+ @echo $(MSG_EXTENDED_LISTING) $@
+ $(OBJDUMP) -h -S $< > $@\r
+\r
+# Create a symbol table from ELF output file.
+%.sym: %.elf
+ @echo
+ @echo $(MSG_SYMBOL_TABLE) $@
+ avr-nm -n $< > $@\r
+\r
+\r
+\r
+# Link: create ELF output file from object files.\r
+.SECONDARY : $(TARGET).elf\r
+.PRECIOUS : $(OBJ)\r
+%.elf: $(OBJ)\r
+ @echo\r
+ @echo $(MSG_LINKING) $@\r
+ $(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS)\r
+\r
+\r
+# Compile: create object files from C source files.\r
+%.o : %.c\r
+ @echo\r
+ @echo $(MSG_COMPILING) $<\r
+ $(CC) -c $(ALL_CFLAGS) $< -o $@\r
+\r
+\r
+# Compile: create assembler files from C source files.\r
+%.s : %.c\r
+ $(CC) -S $(ALL_CFLAGS) $< -o $@\r
+\r
+\r
+# Assemble: create object files from assembler source files.\r
+%.o : %.S\r
+ @echo\r
+ @echo $(MSG_ASSEMBLING) $<\r
+ $(CC) -c $(ALL_ASFLAGS) $< -o $@\r
+\r
+\r
+\r
+\r
+\r
+\r
+# Target: clean project.\r
+clean: begin clean_list finished end\r
+\r
+clean_list :\r
+ @echo
+ @echo $(MSG_CLEANING)
+ $(REMOVE) $(TARGET).hex\r
+ $(REMOVE) $(TARGET).eep\r
+ $(REMOVE) $(TARGET).obj\r
+ $(REMOVE) $(TARGET).cof\r
+ $(REMOVE) $(TARGET).elf\r
+ $(REMOVE) $(TARGET).map\r
+ $(REMOVE) $(TARGET).obj\r
+ $(REMOVE) $(TARGET).a90\r
+ $(REMOVE) $(TARGET).sym\r
+ $(REMOVE) $(TARGET).lnk\r
+ $(REMOVE) $(TARGET).lss\r
+ $(REMOVE) $(OBJ)\r
+ $(REMOVE) $(LST)\r
+ $(REMOVE) $(SRC:.c=.s)\r
+ $(REMOVE) $(SRC:.c=.d)\r
+\r
+\r
+# Automatically generate C source code dependencies. \r
+# (Code originally taken from the GNU make user manual and modified \r
+# (See README.txt Credits).)\r
+#\r
+# Note that this will work with sh (bash) and sed that is shipped with WinAVR\r
+# (see the SHELL variable defined above).\r
+# This may not work with other shells or other seds.\r
+#\r
+%.d: %.c\r
+ set -e; $(CC) -MM $(ALL_CFLAGS) $< \\r
+ | sed 's,\(.*\)\.o[ :]*,\1.o \1.d : ,g' > $@; \\r
+ [ -s $@ ] || rm -f $@\r
+\r
+\r
+# Remove the '-' if you want to see the dependency files generated.\r
+-include $(SRC:.c=.d)\r
+\r
+\r
+\r
+# Listing of phony targets.\r
+.PHONY : all begin finish end sizebefore sizeafter gccversion coff extcoff \\r
+ clean clean_list program\r
+\r
--- /dev/null
+# Makefile to compile and link the I2C Master library and test program\r
+#\r
+# based on\r
+# WinAVR Sample makefile written by Eric B. Weddington, Jörg Wunsch, et al.\r
+#\r
+# Note:\r
+# The warnings which appear while converting ELF to AVR Extended COFF (make extcoff)\r
+# can be ignored\r
+#\r
+#\r
+# On command line:\r
+#\r
+# make all = Make software.\r
+#\r
+# make clean = Clean out built project files.\r
+#\r
+# make coff = Convert ELF to AVR COFF (for use with AVR Studio 3.x or VMLAB).\r
+#\r
+# make extcoff = Convert ELF to AVR Extended COFF (for use with AVR Studio\r
+# 4.07 or greater).\r
+#\r
+# make program = Download the hex file to the device, using avrdude. Please\r
+# customize the avrdude settings below first!\r
+#\r
+# make filename.s = Just compile filename.c into the assembler code only\r
+#\r
+# To rebuild project do "make clean" then "make all".\r
+#\r
+\r
+\r
+# MCU name\r
+MCU = at90s8515\r
+\r
+# Output format. (can be srec, ihex, binary)\r
+FORMAT = ihex\r
+\r
+# Optimization level, can be [0, 1, 2, 3, s]. 0 turns off optimization.\r
+# (Note: 3 is not always the best optimization level. See avr-libc FAQ.)\r
+OPT = s\r
+\r
+\r
+# Target file name (without extension).\r
+TARGET = test_i2cmaster\r
+\r
+\r
+# List C source files here. (C dependencies are automatically generated.)\r
+SRC = $(TARGET).c\r
+\r
+# List Assembler source files here.\r
+# Make them always end in a capital .S. Files ending in a lowercase .s\r
+# will not be considered source files but generated files (assembler\r
+# output from the compiler), and will be deleted upon "make clean"!\r
+# Even though the DOS/Win* filesystem matches both .s and .S the same,\r
+# it will preserve the spelling of the filenames, and gcc itself does\r
+# care about how the name is spelled on its command-line.\r
+ASRC = i2cmaster.S\r
+\r
+\r
+# List any extra directories to look for include files here.\r
+# Each directory must be seperated by a space.\r
+EXTRAINCDIRS = \r
+\r
+\r
+# Optional compiler flags.\r
+# -g: generate debugging information (for GDB, or for COFF conversion)\r
+# -O*: optimization level\r
+# -f...: tuning, see gcc manual and avr-libc documentation\r
+# -Wall...: warning level\r
+# -Wa,...: tell GCC to pass this to the assembler.\r
+# -ahlms: create assembler listing\r
+CFLAGS = -g -O$(OPT) \\r
+-funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums \\r
+-Wall -Wstrict-prototypes \\r
+-Wa,-adhlns=$(<:.c=.lst) \\r
+$(patsubst %,-I%,$(EXTRAINCDIRS))\r
+\r
+\r
+# Set a "language standard" compiler flag.\r
+# Unremark just one line below to set the language standard to use.\r
+# gnu99 = C99 + GNU extensions. See GCC manual for more information.\r
+#CFLAGS += -std=c89\r
+#CFLAGS += -std=gnu89\r
+#CFLAGS += -std=c99\r
+CFLAGS += -std=gnu99\r
+\r
+\r
+\r
+# Optional assembler flags.\r
+# -Wa,...: tell GCC to pass this to the assembler.\r
+# -ahlms: create listing\r
+# -gstabs: have the assembler create line number information; note that\r
+# for use in COFF files, additional information about filenames\r
+# and function names needs to be present in the assembler source\r
+# files -- see avr-libc docs [FIXME: not yet described there]\r
+ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs \r
+\r
+\r
+\r
+# Optional linker flags.\r
+# -Wl,...: tell GCC to pass this to linker.\r
+# -Map: create map file\r
+# --cref: add cross reference to map file\r
+LDFLAGS = -Wl,-Map=$(TARGET).map,--cref\r
+\r
+\r
+\r
+# Additional libraries\r
+\r
+# Minimalistic printf version\r
+#LDFLAGS += -Wl,-u,vfprintf -lprintf_min\r
+\r
+# Floating point printf version (requires -lm below)\r
+#LDFLAGS += -Wl,-u,vfprintf -lprintf_flt\r
+\r
+# -lm = math library\r
+LDFLAGS += -lm\r
+\r
+\r
+\r
+\r
+# Programming support using avrdude. Settings and variables.\r
+\r
+# Programming hardware: alf avr910 avrisp bascom bsd \r
+# dt006 pavr picoweb pony-stk200 sp12 stk200 stk500\r
+#\r
+# Type: avrdude -c ?\r
+# to get a full listing.\r
+#\r
+AVRDUDE_PROGRAMMER = stk500\r
+\r
+\r
+AVRDUDE_PORT = com1 # programmer connected to serial device\r
+#AVRDUDE_PORT = lpt1 # programmer connected to parallel port\r
+\r
+AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex\r
+#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep\r
+\r
+AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)\r
+\r
+# Uncomment the following if you want avrdude's erase cycle counter.\r
+# Note that this counter needs to be initialized first using -Yn,\r
+# see avrdude manual.\r
+#AVRDUDE_ERASE += -y\r
+\r
+# Uncomment the following if you do /not/ wish a verification to be\r
+# performed after programming the device.\r
+#AVRDUDE_FLAGS += -V\r
+\r
+# Increase verbosity level. Please use this when submitting bug\r
+# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude> \r
+# to submit bug reports.\r
+#AVRDUDE_FLAGS += -v -v\r
+\r
+\r
+\r
+\r
+# ---------------------------------------------------------------------------\r
+\r
+# Define directories, if needed.\r
+DIRAVR = c:/winavr\r
+DIRAVRBIN = $(DIRAVR)/bin\r
+DIRAVRUTILS = $(DIRAVR)/utils/bin\r
+DIRINC = .\r
+DIRLIB = $(DIRAVR)/avr/lib\r
+\r
+\r
+# Define programs and commands.\r
+SHELL = sh\r
+\r
+CC = avr-gcc\r
+\r
+OBJCOPY = avr-objcopy\r
+OBJDUMP = avr-objdump\r
+SIZE = avr-size\r
+\r
+\r
+# Programming support using avrdude.\r
+AVRDUDE = avrdude\r
+\r
+\r
+REMOVE = rm -f\r
+COPY = cp\r
+\r
+HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex\r
+ELFSIZE = $(SIZE) -A $(TARGET).elf\r
+\r
+\r
+\r
+# Define Messages\r
+# English\r
+MSG_ERRORS_NONE = Errors: none\r
+MSG_BEGIN = -------- begin --------\r
+MSG_END = -------- end --------\r
+MSG_SIZE_BEFORE = Size before: \r
+MSG_SIZE_AFTER = Size after:\r
+MSG_COFF = Converting to AVR COFF:\r
+MSG_EXTENDED_COFF = Converting to AVR Extended COFF:\r
+MSG_FLASH = Creating load file for Flash:\r
+MSG_EEPROM = Creating load file for EEPROM:\r
+MSG_EXTENDED_LISTING = Creating Extended Listing:\r
+MSG_SYMBOL_TABLE = Creating Symbol Table:\r
+MSG_LINKING = Linking:\r
+MSG_COMPILING = Compiling:\r
+MSG_ASSEMBLING = Assembling:\r
+MSG_CLEANING = Cleaning project:\r
+\r
+\r
+\r
+\r
+# Define all object files.\r
+OBJ = $(SRC:.c=.o) $(ASRC:.S=.o) \r
+\r
+# Define all listing files.\r
+LST = $(ASRC:.S=.lst) $(SRC:.c=.lst)\r
+\r
+# Combine all necessary flags and optional flags.\r
+# Add target processor to flags.\r
+ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS)\r
+ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)\r
+\r
+\r
+\r
+# Default target.\r
+all: begin gccversion sizebefore $(TARGET).elf $(TARGET).hex $(TARGET).eep \\r
+ $(TARGET).lss $(TARGET).sym sizeafter finished end\r
+\r
+\r
+# Eye candy.\r
+# AVR Studio 3.x does not check make's exit code but relies on\r
+# the following magic strings to be generated by the compile job.\r
+begin:\r
+ @echo\r
+ @echo $(MSG_BEGIN)\r
+\r
+finished:\r
+ @echo $(MSG_ERRORS_NONE)\r
+\r
+end:\r
+ @echo $(MSG_END)\r
+ @echo\r
+\r
+\r
+# Display size of file.\r
+sizebefore:\r
+ @if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); echo; fi\r
+\r
+sizeafter:\r
+ @if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); echo; fi\r
+\r
+\r
+\r
+# Display compiler version information.\r
+gccversion : \r
+ @$(CC) --version\r
+\r
+\r
+\r
+\r
+# Convert ELF to COFF for use in debugging / simulating in\r
+# AVR Studio or VMLAB.\r
+COFFCONVERT=$(OBJCOPY) --debugging \\r
+ --change-section-address .data-0x800000 \\r
+ --change-section-address .bss-0x800000 \\r
+ --change-section-address .noinit-0x800000 \\r
+ --change-section-address .eeprom-0x810000 \r
+\r
+\r
+coff: $(TARGET).elf\r
+ @echo\r
+ @echo $(MSG_COFF) $(TARGET).cof\r
+ $(COFFCONVERT) -O coff-avr $< $(TARGET).cof\r
+\r
+\r
+extcoff: $(TARGET).elf\r
+ @echo\r
+ @echo $(MSG_EXTENDED_COFF) $(TARGET).cof\r
+ $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof\r
+\r
+\r
+\r
+\r
+# Program the device. \r
+program: $(TARGET).hex $(TARGET).eep\r
+ $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)\r
+\r
+\r
+\r
+\r
+# Create final output files (.hex, .eep) from ELF output file.\r
+%.hex: %.elf\r
+ @echo
+ @echo $(MSG_FLASH) $@
+ $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@\r
+\r
+%.eep: %.elf\r
+ @echo
+ @echo $(MSG_EEPROM) $@
+ -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \\r
+ --change-section-lma .eeprom=0 -O $(FORMAT) $< $@\r
+\r
+# Create extended listing file from ELF output file.\r
+%.lss: %.elf\r
+ @echo
+ @echo $(MSG_EXTENDED_LISTING) $@
+ $(OBJDUMP) -h -S $< > $@\r
+\r
+# Create a symbol table from ELF output file.
+%.sym: %.elf
+ @echo
+ @echo $(MSG_SYMBOL_TABLE) $@
+ avr-nm -n $< > $@\r
+\r
+\r
+\r
+# Link: create ELF output file from object files.\r
+.SECONDARY : $(TARGET).elf\r
+.PRECIOUS : $(OBJ)\r
+%.elf: $(OBJ)\r
+ @echo\r
+ @echo $(MSG_LINKING) $@\r
+ $(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS)\r
+\r
+\r
+# Compile: create object files from C source files.\r
+%.o : %.c\r
+ @echo\r
+ @echo $(MSG_COMPILING) $<\r
+ $(CC) -c $(ALL_CFLAGS) $< -o $@\r
+\r
+\r
+# Compile: create assembler files from C source files.\r
+%.s : %.c\r
+ $(CC) -S $(ALL_CFLAGS) $< -o $@\r
+\r
+\r
+# Assemble: create object files from assembler source files.\r
+%.o : %.S\r
+ @echo\r
+ @echo $(MSG_ASSEMBLING) $<\r
+ $(CC) -c $(ALL_ASFLAGS) $< -o $@\r
+\r
+\r
+\r
+\r
+\r
+\r
+# Target: clean project.\r
+clean: begin clean_list finished end\r
+\r
+clean_list :\r
+ @echo
+ @echo $(MSG_CLEANING)
+ $(REMOVE) $(TARGET).hex\r
+ $(REMOVE) $(TARGET).eep\r
+ $(REMOVE) $(TARGET).obj\r
+ $(REMOVE) $(TARGET).cof\r
+ $(REMOVE) $(TARGET).elf\r
+ $(REMOVE) $(TARGET).map\r
+ $(REMOVE) $(TARGET).obj\r
+ $(REMOVE) $(TARGET).a90\r
+ $(REMOVE) $(TARGET).sym\r
+ $(REMOVE) $(TARGET).lnk\r
+ $(REMOVE) $(TARGET).lss\r
+ $(REMOVE) $(OBJ)\r
+ $(REMOVE) $(LST)\r
+ $(REMOVE) $(SRC:.c=.s)\r
+ $(REMOVE) $(SRC:.c=.d)\r
+\r
+\r
+# Automatically generate C source code dependencies. \r
+# (Code originally taken from the GNU make user manual and modified \r
+# (See README.txt Credits).)\r
+#\r
+# Note that this will work with sh (bash) and sed that is shipped with WinAVR\r
+# (see the SHELL variable defined above).\r
+# This may not work with other shells or other seds.\r
+#\r
+%.d: %.c\r
+ set -e; $(CC) -MM $(ALL_CFLAGS) $< \\r
+ | sed 's,\(.*\)\.o[ :]*,\1.o \1.d : ,g' > $@; \\r
+ [ -s $@ ] || rm -f $@\r
+\r
+\r
+# Remove the '-' if you want to see the dependency files generated.\r
+-include $(SRC:.c=.d)\r
+\r
+\r
+\r
+# Listing of phony targets.\r
+.PHONY : all begin finish end sizebefore sizeafter gccversion coff extcoff \\r
+ clean clean_list program\r
+\r
--- /dev/null
+# ----------------------------------------------------------------------------\r
+# Makefile to compile and link the TWImaster library and test program\r
+# Author: Peter Fleury\r
+# File: $Id: makefile.twimaster,v 1.1 2005/03/29 14:50:05 Peter Exp $\r
+# based on WinAVR Makefile Template written by Eric B. Weddington, Jörg Wunsch, et al.\r
+#\r
+# Adjust F_CPU below to the clock frequency in Mhz of your AVR target\r
+# \r
+# rename this file to "makefile" or invoke with "make -f makefile.twimaster"\r
+#\r
+#----------------------------------------------------------------------------\r
+# On command line:\r
+#\r
+# make all = Make software.\r
+#\r
+# make clean = Clean out built project files.\r
+#\r
+# make coff = Convert ELF to AVR COFF.\r
+#\r
+# make extcoff = Convert ELF to AVR Extended COFF.\r
+#\r
+# make program = Download the hex file to the device, using avrdude.\r
+# Please customize the avrdude settings below first!\r
+#\r
+# make debug = Start either simulavr or avarice as specified for debugging, \r
+# with avr-gdb or avr-insight as the front end for debugging.\r
+#\r
+# make filename.s = Just compile filename.c into the assembler code only.\r
+#\r
+# make filename.i = Create a preprocessed source file for use in submitting\r
+# bug reports to the GCC project.\r
+#\r
+# To rebuild project do "make clean" then "make all".\r
+#----------------------------------------------------------------------------\r
+\r
+\r
+# MCU name\r
+MCU = atmega8\r
+\r
+\r
+# Processor frequency.\r
+# This will define a symbol, F_CPU, in all source code files equal to the \r
+# processor frequency. You can then use this symbol in your source code to \r
+# calculate timings. Do NOT tack on a 'UL' at the end, this will be done\r
+# automatically to create a 32-bit value in your source code.\r
+F_CPU = 4000000\r
+\r
+\r
+# Output format. (can be srec, ihex, binary)\r
+FORMAT = ihex\r
+\r
+\r
+# Target file name (without extension).\r
+TARGET = test_i2cmaster\r
+\r
+\r
+# List C source files here. (C dependencies are automatically generated.)\r
+SRC = $(TARGET).c twimaster.c\r
+\r
+\r
+# List Assembler source files here.\r
+# Make them always end in a capital .S. Files ending in a lowercase .s\r
+# will not be considered source files but generated files (assembler\r
+# output from the compiler), and will be deleted upon "make clean"!\r
+# Even though the DOS/Win* filesystem matches both .s and .S the same,\r
+# it will preserve the spelling of the filenames, and gcc itself does\r
+# care about how the name is spelled on its command-line.\r
+ASRC = \r
+\r
+\r
+# Optimization level, can be [0, 1, 2, 3, s]. \r
+# 0 = turn off optimization. s = optimize for size.\r
+# (Note: 3 is not always the best optimization level. See avr-libc FAQ.)\r
+OPT = s\r
+\r
+\r
+# Debugging format.\r
+# Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.\r
+# AVR Studio 4.10 requires dwarf-2.\r
+# AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.\r
+DEBUG = dwarf-2\r
+\r
+\r
+# List any extra directories to look for include files here.\r
+# Each directory must be seperated by a space.\r
+# Use forward slashes for directory separators.\r
+# For a directory that has spaces, enclose it in quotes.\r
+EXTRAINCDIRS = \r
+\r
+\r
+# Compiler flag to set the C Standard level.\r
+# c89 = "ANSI" C\r
+# gnu89 = c89 plus GCC extensions\r
+# c99 = ISO C99 standard (not yet fully implemented)\r
+# gnu99 = c99 plus GCC extensions\r
+CSTANDARD = -std=gnu99\r
+\r
+\r
+# Place -D or -U options here\r
+CDEFS = -DF_CPU=$(F_CPU)UL \r
+\r
+\r
+# Place -I options here\r
+CINCS =\r
+\r
+\r
+\r
+#---------------- Compiler Options ----------------\r
+# -g*: generate debugging information\r
+# -O*: optimization level\r
+# -f...: tuning, see GCC manual and avr-libc documentation\r
+# -Wall...: warning level\r
+# -Wa,...: tell GCC to pass this to the assembler.\r
+# -adhlns...: create assembler listing\r
+CFLAGS = -g$(DEBUG)\r
+CFLAGS += $(CDEFS) $(CINCS)\r
+CFLAGS += -O$(OPT)\r
+CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums\r
+CFLAGS += -Wall -Wstrict-prototypes\r
+CFLAGS += -Wa,-adhlns=$(<:.c=.lst)\r
+CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))\r
+CFLAGS += $(CSTANDARD)\r
+\r
+\r
+#---------------- Assembler Options ----------------\r
+# -Wa,...: tell GCC to pass this to the assembler.\r
+# -ahlms: create listing\r
+# -gstabs: have the assembler create line number information; note that\r
+# for use in COFF files, additional information about filenames\r
+# and function names needs to be present in the assembler source\r
+# files -- see avr-libc docs [FIXME: not yet described there]\r
+ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs \r
+\r
+\r
+#---------------- Library Options ----------------\r
+# Minimalistic printf version\r
+PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min\r
+\r
+# Floating point printf version (requires MATH_LIB = -lm below)\r
+PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt\r
+\r
+# If this is left blank, then it will use the Standard printf version.\r
+PRINTF_LIB = \r
+#PRINTF_LIB = $(PRINTF_LIB_MIN)\r
+#PRINTF_LIB = $(PRINTF_LIB_FLOAT)\r
+\r
+\r
+# Minimalistic scanf version\r
+SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min\r
+\r
+# Floating point + %[ scanf version (requires MATH_LIB = -lm below)\r
+SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt\r
+\r
+# If this is left blank, then it will use the Standard scanf version.\r
+SCANF_LIB = \r
+#SCANF_LIB = $(SCANF_LIB_MIN)\r
+#SCANF_LIB = $(SCANF_LIB_FLOAT)\r
+\r
+\r
+MATH_LIB = -lm\r
+\r
+\r
+\r
+#---------------- External Memory Options ----------------\r
+\r
+# 64 KB of external RAM, starting after internal RAM (ATmega128!),\r
+# used for variables (.data/.bss) and heap (malloc()).\r
+#EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff\r
+\r
+# 64 KB of external RAM, starting after internal RAM (ATmega128!),\r
+# only used for heap (malloc()).\r
+#EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x80ffff\r
+\r
+EXTMEMOPTS =\r
+\r
+\r
+\r
+#---------------- Linker Options ----------------\r
+# -Wl,...: tell GCC to pass this to linker.\r
+# -Map: create map file\r
+# --cref: add cross reference to map file\r
+LDFLAGS = -Wl,-Map=$(TARGET).map,--cref\r
+LDFLAGS += $(EXTMEMOPTS)\r
+LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)\r
+\r
+\r
+\r
+#---------------- Programming Options (avrdude) ----------------\r
+\r
+# Programming hardware: alf avr910 avrisp bascom bsd \r
+# dt006 pavr picoweb pony-stk200 sp12 stk200 stk500\r
+#\r
+# Type: avrdude -c ?\r
+# to get a full listing.\r
+#\r
+AVRDUDE_PROGRAMMER = stk500\r
+\r
+# com1 = serial port. Use lpt1 to connect to parallel port.\r
+AVRDUDE_PORT = com1 # programmer connected to serial device\r
+\r
+AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex\r
+#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep\r
+\r
+\r
+# Uncomment the following if you want avrdude's erase cycle counter.\r
+# Note that this counter needs to be initialized first using -Yn,\r
+# see avrdude manual.\r
+#AVRDUDE_ERASE_COUNTER = -y\r
+\r
+# Uncomment the following if you do /not/ wish a verification to be\r
+# performed after programming the device.\r
+#AVRDUDE_NO_VERIFY = -V\r
+\r
+# Increase verbosity level. Please use this when submitting bug\r
+# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude> \r
+# to submit bug reports.\r
+#AVRDUDE_VERBOSE = -v -v\r
+\r
+AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)\r
+AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)\r
+AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)\r
+AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)\r
+\r
+\r
+\r
+#---------------- Debugging Options ----------------\r
+\r
+# For simulavr only - target MCU frequency.\r
+DEBUG_MFREQ = $(F_CPU)\r
+\r
+# Set the DEBUG_UI to either gdb or insight.\r
+# DEBUG_UI = gdb\r
+DEBUG_UI = insight\r
+\r
+# Set the debugging back-end to either avarice, simulavr.\r
+DEBUG_BACKEND = avarice\r
+#DEBUG_BACKEND = simulavr\r
+\r
+# GDB Init Filename.\r
+GDBINIT_FILE = __avr_gdbinit\r
+\r
+# When using avarice settings for the JTAG\r
+JTAG_DEV = /dev/com1\r
+\r
+# Debugging port used to communicate between GDB / avarice / simulavr.\r
+DEBUG_PORT = 4242\r
+\r
+# Debugging host used to communicate between GDB / avarice / simulavr, normally\r
+# just set to localhost unless doing some sort of crazy debugging when \r
+# avarice is running on a different computer.\r
+DEBUG_HOST = localhost\r
+\r
+\r
+\r
+#============================================================================\r
+\r
+\r
+# Define programs and commands.\r
+SHELL = sh\r
+CC = avr-gcc\r
+OBJCOPY = avr-objcopy\r
+OBJDUMP = avr-objdump\r
+SIZE = avr-size\r
+NM = avr-nm\r
+AVRDUDE = avrdude\r
+REMOVE = rm -f\r
+COPY = cp\r
+WINSHELL = cmd\r
+\r
+\r
+# Define Messages\r
+# English\r
+MSG_ERRORS_NONE = Errors: none\r
+MSG_BEGIN = -------- begin --------\r
+MSG_END = -------- end --------\r
+MSG_SIZE_BEFORE = Size before: \r
+MSG_SIZE_AFTER = Size after:\r
+MSG_COFF = Converting to AVR COFF:\r
+MSG_EXTENDED_COFF = Converting to AVR Extended COFF:\r
+MSG_FLASH = Creating load file for Flash:\r
+MSG_EEPROM = Creating load file for EEPROM:\r
+MSG_EXTENDED_LISTING = Creating Extended Listing:\r
+MSG_SYMBOL_TABLE = Creating Symbol Table:\r
+MSG_LINKING = Linking:\r
+MSG_COMPILING = Compiling:\r
+MSG_ASSEMBLING = Assembling:\r
+MSG_CLEANING = Cleaning project:\r
+\r
+\r
+\r
+\r
+# Define all object files.\r
+OBJ = $(SRC:.c=.o) $(ASRC:.S=.o) \r
+\r
+# Define all listing files.\r
+LST = $(SRC:.c=.lst) $(ASRC:.S=.lst) \r
+\r
+\r
+# Compiler flags to generate dependency files.\r
+GENDEPFLAGS = -MD -MP -MF .dep/$(@F).d\r
+\r
+\r
+# Combine all necessary flags and optional flags.\r
+# Add target processor to flags.\r
+ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)\r
+ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)\r
+\r
+\r
+\r
+\r
+\r
+# Default target.\r
+all: begin gccversion sizebefore build sizeafter end\r
+\r
+build: elf hex eep lss sym\r
+\r
+elf: $(TARGET).elf\r
+hex: $(TARGET).hex\r
+eep: $(TARGET).eep\r
+lss: $(TARGET).lss \r
+sym: $(TARGET).sym\r
+\r
+\r
+\r
+# Eye candy.\r
+# AVR Studio 3.x does not check make's exit code but relies on\r
+# the following magic strings to be generated by the compile job.\r
+begin:\r
+ @echo\r
+ @echo $(MSG_BEGIN)\r
+\r
+end:\r
+ @echo $(MSG_END)\r
+ @echo\r
+\r
+\r
+# Display size of file.\r
+HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex\r
+ELFSIZE = $(SIZE) -A $(TARGET).elf\r
+AVRMEM = avr-mem.sh $(TARGET).elf $(MCU)\r
+\r
+sizebefore:\r
+ @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \\r
+ $(AVRMEM) 2>/dev/null; echo; fi\r
+\r
+sizeafter:\r
+ @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \\r
+ $(AVRMEM) 2>/dev/null; echo; fi\r
+\r
+\r
+\r
+# Display compiler version information.\r
+gccversion : \r
+ @$(CC) --version\r
+\r
+\r
+\r
+# Program the device. \r
+program: $(TARGET).hex $(TARGET).eep\r
+ $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)\r
+\r
+\r
+# Generate avr-gdb config/init file which does the following:\r
+# define the reset signal, load the target file, connect to target, and set \r
+# a breakpoint at main().\r
+gdb-config: \r
+ @$(REMOVE) $(GDBINIT_FILE)\r
+ @echo define reset >> $(GDBINIT_FILE)\r
+ @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)\r
+ @echo end >> $(GDBINIT_FILE)\r
+ @echo file $(TARGET).elf >> $(GDBINIT_FILE)\r
+ @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)\r
+ifeq ($(DEBUG_BACKEND),simulavr)\r
+ @echo load >> $(GDBINIT_FILE)\r
+endif \r
+ @echo break main >> $(GDBINIT_FILE)\r
+ \r
+debug: gdb-config $(TARGET).elf\r
+ifeq ($(DEBUG_BACKEND), avarice)\r
+ @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.\r
+ @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \\r
+ $(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)\r
+ @$(WINSHELL) /c pause\r
+ \r
+else\r
+ @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \\r
+ $(DEBUG_MFREQ) --port $(DEBUG_PORT)\r
+endif\r
+ @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)\r
+ \r
+\r
+\r
+\r
+# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.\r
+COFFCONVERT=$(OBJCOPY) --debugging \\r
+--change-section-address .data-0x800000 \\r
+--change-section-address .bss-0x800000 \\r
+--change-section-address .noinit-0x800000 \\r
+--change-section-address .eeprom-0x810000 \r
+\r
+\r
+coff: $(TARGET).elf\r
+ @echo\r
+ @echo $(MSG_COFF) $(TARGET).cof\r
+ $(COFFCONVERT) -O coff-avr $< $(TARGET).cof\r
+\r
+\r
+extcoff: $(TARGET).elf\r
+ @echo\r
+ @echo $(MSG_EXTENDED_COFF) $(TARGET).cof\r
+ $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof\r
+\r
+\r
+\r
+# Create final output files (.hex, .eep) from ELF output file.\r
+%.hex: %.elf\r
+ @echo\r
+ @echo $(MSG_FLASH) $@\r
+ $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@\r
+\r
+%.eep: %.elf\r
+ @echo\r
+ @echo $(MSG_EEPROM) $@\r
+ -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \\r
+ --change-section-lma .eeprom=0 -O $(FORMAT) $< $@\r
+\r
+# Create extended listing file from ELF output file.\r
+%.lss: %.elf\r
+ @echo\r
+ @echo $(MSG_EXTENDED_LISTING) $@\r
+ $(OBJDUMP) -h -S $< > $@\r
+\r
+# Create a symbol table from ELF output file.\r
+%.sym: %.elf\r
+ @echo\r
+ @echo $(MSG_SYMBOL_TABLE) $@\r
+ $(NM) -n $< > $@\r
+\r
+\r
+\r
+# Link: create ELF output file from object files.\r
+.SECONDARY : $(TARGET).elf\r
+.PRECIOUS : $(OBJ)\r
+%.elf: $(OBJ)\r
+ @echo\r
+ @echo $(MSG_LINKING) $@\r
+ $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)\r
+\r
+\r
+# Compile: create object files from C source files.\r
+%.o : %.c\r
+ @echo\r
+ @echo $(MSG_COMPILING) $<\r
+ $(CC) -c $(ALL_CFLAGS) $< -o $@ \r
+\r
+\r
+# Compile: create assembler files from C source files.\r
+%.s : %.c\r
+ $(CC) -S $(ALL_CFLAGS) $< -o $@\r
+\r
+\r
+# Assemble: create object files from assembler source files.\r
+%.o : %.S\r
+ @echo\r
+ @echo $(MSG_ASSEMBLING) $<\r
+ $(CC) -c $(ALL_ASFLAGS) $< -o $@\r
+\r
+# Create preprocessed source for use in sending a bug report.\r
+%.i : %.c\r
+ $(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@ \r
+\r
+\r
+# Target: clean project.\r
+clean: begin clean_list end\r
+\r
+clean_list :\r
+ @echo\r
+ @echo $(MSG_CLEANING)\r
+ $(REMOVE) $(TARGET).hex\r
+ $(REMOVE) $(TARGET).eep\r
+ $(REMOVE) $(TARGET).cof\r
+ $(REMOVE) $(TARGET).elf\r
+ $(REMOVE) $(TARGET).map\r
+ $(REMOVE) $(TARGET).sym\r
+ $(REMOVE) $(TARGET).lss\r
+ $(REMOVE) $(OBJ)\r
+ $(REMOVE) $(LST)\r
+ $(REMOVE) $(SRC:.c=.s)\r
+ $(REMOVE) $(SRC:.c=.d)\r
+ $(REMOVE) .dep/*\r
+\r
+\r
+\r
+# Include the dependency files.\r
+-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)\r
+\r
+\r
+# Listing of phony targets.\r
+.PHONY : all begin finish end sizebefore sizeafter gccversion \\r
+build elf hex eep lss sym coff extcoff \\r
+clean clean_list program debug gdb-config\r
+\r
--- /dev/null
+/****************************************************************************\r
+Title: Access serial EEPROM 24C02 using I2C interace\r
+Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury\r
+File: $Id: test_i2cmaster.c,v 1.2 2003/12/06 17:07:18 peter Exp $\r
+Software: AVR-GCC 3.3\r
+Hardware: AT90S8515 at 4 Mhz, any AVR device can be used\r
+\r
+Description:\r
+ This example shows how the I2C library i2cmaster.S can be used to \r
+ access a serial eeprom.\r
+ Based on Atmel Application Note AVR300, adapted to AVR-GCC C interface\r
+\r
+*****************************************************************************/\r
+#include <avr/io.h>\r
+#include "i2cmaster.h"\r
+\r
+\r
+#define Dev24C02 0xA2 // device address of EEPROM 24C02, see datasheet\r
+\r
+\r
+int main(void)\r
+{\r
+ unsigned char ret;\r
+ \r
+\r
+ DDRB = 0xff; // use all pins on port B for output \r
+ PORTB = 0xff; // (active low LED's )\r
+\r
+ i2c_init(); // init I2C interface\r
+\r
+ /* write 0x75 to eeprom address 0x05 (Byte Write) */\r
+ ret = i2c_start(Dev24C02+I2C_WRITE); // set device address and write mode\r
+ if ( ret ) {\r
+ /* failed to issue start condition, possibly no device found */\r
+ i2c_stop();\r
+ PORTB=0x00; // activate all 8 LED to show error */\r
+ }else {\r
+ /* issuing start condition ok, device accessible */\r
+ i2c_write(0x05); // write address = 5\r
+ i2c_write(0x75); // ret=0 -> Ok, ret=1 -> no ACK \r
+ i2c_stop(); // set stop conditon = release bus\r
+\r
+ /* write ok, read value back from eeprom address 0x05, wait until \r
+ the device is no longer busy from the previous write operation */\r
+ i2c_start_wait(Dev24C02+I2C_WRITE); // set device address and write mode\r
+ i2c_write(0x05); // write address = 5\r
+ i2c_rep_start(Dev24C02+I2C_READ); // set device address and read mode\r
+ ret = i2c_readNak(); // read one byte\r
+ i2c_stop();\r
+ \r
+ PORTB = ~ret; // output byte on the LED's\r
+\r
+ /* write 0x70,0x71,072,073 to eeprom address 0x00..0x03 (Page Write),\r
+ wait until the device is no longer busy from the previous write operation */\r
+ i2c_start_wait(Dev24C02+I2C_WRITE); // set device address and write mode\r
+ i2c_write(0x00); // write start address = 0\r
+ i2c_write(0x70); // write data to address 0\r
+ i2c_write(0x71); // " " " " 1\r
+ i2c_write(0x72); // " " " " 2\r
+ i2c_write(0x74); // " " " " 3\r
+ i2c_stop(); // set stop conditon = release bus\r
+ \r
+ /* write ok, read value back from eeprom address 0..3 (Sequencial Read),\r
+ wait until the device is no longer busy from the previous write operation */\r
+ i2c_start_wait(Dev24C02+I2C_WRITE); // set device address and write mode\r
+ i2c_write(0x00); // write address = 0\r
+ i2c_rep_start(Dev24C02+I2C_READ); // set device address and read mode\r
+ ret = i2c_readAck(); // read one byte form address 0\r
+ ret = i2c_readAck(); // " " " " " 1\r
+ ret = i2c_readAck(); // " " " " " 2\r
+ ret = i2c_readNak(); // " " " " " 3\r
+ i2c_stop(); // set stop condition = release bus\r
+ \r
+ PORTB = ~ret; // output byte on the LED's \r
+ }\r
+ \r
+ for(;;); \r
+}\r
--- /dev/null
+/*************************************************************************\r
+ * Title: I2C master library using hardware TWI interface\r
+ * Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury\r
+ * File: $Id: twimaster.c,v 1.3 2005/07/02 11:14:21 Peter Exp $\r
+ * Software: AVR-GCC 3.4.3 / avr-libc 1.2.3\r
+ * Target: any AVR device with hardware TWI \r
+ * Usage: API compatible with I2C Software Library i2cmaster.h\r
+ **************************************************************************/\r
+#include <inttypes.h>\r
+#include <compat/twi.h>\r
+\r
+#include <i2cmaster.h>\r
+\r
+\r
+/* I2C clock in Hz */\r
+#define SCL_CLOCK 100000L\r
+\r
+\r
+/*************************************************************************\r
+ Initialization of the I2C bus interface. Need to be called only once\r
+ *************************************************************************/\r
+void i2c_init(void)\r
+{\r
+ /* initialize TWI clock: 100 kHz clock, TWPS = 0 => prescaler = 1 */\r
+\r
+ TWSR = 0; /* no prescaler */\r
+ TWBR = ((F_CPU/SCL_CLOCK)-16)/2; /* must be > 10 for stable operation */\r
+\r
+}/* i2c_init */\r
+\r
+\r
+/************************************************************************* \r
+ Issues a start condition and sends address and transfer direction.\r
+ return 0 = device accessible, 1= failed to access device\r
+ *************************************************************************/\r
+unsigned char i2c_start(unsigned char address)\r
+{\r
+ uint8_t twst;\r
+\r
+ // send START condition\r
+ TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);\r
+\r
+ // wait until transmission completed\r
+ while(!(TWCR & (1<<TWINT)));\r
+\r
+ // check value of TWI Status Register. Mask prescaler bits.\r
+ twst = TW_STATUS & 0xF8;\r
+ if ( (twst != TW_START) && (twst != TW_REP_START)) return 1;\r
+\r
+ // send device address\r
+ TWDR = address;\r
+ TWCR = (1<<TWINT) | (1<<TWEN);\r
+\r
+ // wail until transmission completed and ACK/NACK has been received\r
+ while(!(TWCR & (1<<TWINT)));\r
+\r
+ // check value of TWI Status Register. Mask prescaler bits.\r
+ twst = TW_STATUS & 0xF8;\r
+ if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;\r
+\r
+ return 0;\r
+\r
+}/* i2c_start */\r
+\r
+\r
+/*************************************************************************\r
+ Issues a start condition and sends address and transfer direction.\r
+ If device is busy, use ack polling to wait until device is ready\r
+\r
+Input: address and transfer direction of I2C device\r
+ *************************************************************************/\r
+void i2c_start_wait(unsigned char address)\r
+{\r
+ uint8_t twst;\r
+\r
+\r
+ while ( 1 )\r
+ {\r
+ // send START condition\r
+ TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);\r
+\r
+ // wait until transmission completed\r
+ while(!(TWCR & (1<<TWINT)));\r
+\r
+ // check value of TWI Status Register. Mask prescaler bits.\r
+ twst = TW_STATUS & 0xF8;\r
+ if ( (twst != TW_START) && (twst != TW_REP_START)) continue;\r
+\r
+ // send device address\r
+ TWDR = address;\r
+ TWCR = (1<<TWINT) | (1<<TWEN);\r
+\r
+ // wail until transmission completed\r
+ while(!(TWCR & (1<<TWINT)));\r
+\r
+ // check value of TWI Status Register. Mask prescaler bits.\r
+ twst = TW_STATUS & 0xF8;\r
+ if ( (twst == TW_MT_SLA_NACK )||(twst ==TW_MR_DATA_NACK) ) \r
+ { \r
+ /* device busy, send stop condition to terminate write operation */\r
+ TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);\r
+\r
+ // wait until stop condition is executed and bus released\r
+ while(TWCR & (1<<TWSTO));\r
+\r
+ continue;\r
+ }\r
+ //if( twst != TW_MT_SLA_ACK) return 1;\r
+ break;\r
+ }\r
+\r
+}/* i2c_start_wait */\r
+\r
+\r
+/*************************************************************************\r
+ Issues a repeated start condition and sends address and transfer direction \r
+\r
+Input: address and transfer direction of I2C device\r
+\r
+Return: 0 device accessible\r
+1 failed to access device\r
+ *************************************************************************/\r
+unsigned char i2c_rep_start(unsigned char address)\r
+{\r
+ return i2c_start( address );\r
+\r
+}/* i2c_rep_start */\r
+\r
+\r
+/*************************************************************************\r
+ Terminates the data transfer and releases the I2C bus\r
+ *************************************************************************/\r
+void i2c_stop(void)\r
+{\r
+ /* send stop condition */\r
+ TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);\r
+\r
+ // wait until stop condition is executed and bus released\r
+ while(TWCR & (1<<TWSTO));\r
+\r
+}/* i2c_stop */\r
+\r
+\r
+/*************************************************************************\r
+ Send one byte to I2C device\r
+\r
+Input: byte to be transfered\r
+Return: 0 write successful \r
+1 write failed\r
+ *************************************************************************/\r
+unsigned char i2c_write( unsigned char data )\r
+{ \r
+ uint8_t twst;\r
+\r
+ // send data to the previously addressed device\r
+ TWDR = data;\r
+ TWCR = (1<<TWINT) | (1<<TWEN);\r
+\r
+ // wait until transmission completed\r
+ while(!(TWCR & (1<<TWINT)));\r
+\r
+ // check value of TWI Status Register. Mask prescaler bits\r
+ twst = TW_STATUS & 0xF8;\r
+ if( twst != TW_MT_DATA_ACK) return 1;\r
+ return 0;\r
+\r
+}/* i2c_write */\r
+\r
+\r
+/*************************************************************************\r
+ Read one byte from the I2C device, request more data from device \r
+\r
+Return: byte read from I2C device\r
+ *************************************************************************/\r
+unsigned char i2c_readAck(void)\r
+{\r
+ TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);\r
+ while(!(TWCR & (1<<TWINT))); \r
+\r
+ return TWDR;\r
+\r
+}/* i2c_readAck */\r
+\r
+\r
+/*************************************************************************\r
+ Read one byte from the I2C device, read is followed by a stop condition \r
+\r
+Return: byte read from I2C device\r
+ *************************************************************************/\r
+unsigned char i2c_readNak(void)\r
+{\r
+ TWCR = (1<<TWINT) | (1<<TWEN);\r
+ while(!(TWCR & (1<<TWINT)));\r
+\r
+ return TWDR;\r
+\r
+}/* i2c_readNak */\r
#include "uart.h"
#include "dynamixel-avr/dynamixel.h"
#include "dynamixel-avr/xm.h"
+#include "mlx90614.h"
+#include "i2cmaster.h"
/**
* Nano Pinout
* A1 PC1/ADC1
* A2 PC2/ADC2
* A3 PC3/ADC3
- * A4 PC4/ADC4/SDA
- * A5 PC5/ADC5/SCL
+ * A4 PC4/ADC4/SDA I2C
+ * A5 PC5/ADC5/SCL I2C
* A6 ADC6
* A7 ADC7
* D0 PD0/RX
* http://emanual.robotis.com/docs/en/dxl/protocol2
* https://nerdralph.blogspot.com/2020/02/building-better-bit-bang-uart-picouart.html
*/
+// Application handler for the virtual servo (dynamixel_slave_set_cb).
+// 1 byte write to address 0x01: set the LED PWM duty (OCR0A, inverted).
+// Read of address 0x02: MLX90614 object temperature, 0.1 deg C (214 == 21.4 C).
+static uint8_t slave_cmd(uint8_t ins, const uint8_t* param, uint8_t param_len,
+ uint8_t* err, uint8_t* reply, uint8_t* reply_len)
+{
+ *err = 0;
+ *reply_len = 0;
+ if (param_len < 2)
+ return 0;
+ uint16_t addr = (uint16_t)(param[0] | ((uint16_t)param[1] << 8));
+ if (ins == DYNAMIXEL_WRITE && param_len == 3 && addr == 1) {
+ OCR0A = 255 - param[2];
+ return 1; // status reply, ERR = 0
+ }
+ if (ins == DYNAMIXEL_READ && addr == 2) {
+ uint16_t temp = mlx90614_read_temp_tenths();
+ if (temp == 0) {
+ *err = 0x04; // hardware error: sensor read failed
+ } else {
+ reply[0] = (uint8_t) temp; // DYNAMIXEL: low byte first
+ reply[1] = (uint8_t) (temp >> 8);
+ *reply_len = 2;
+ }
+ return 1;
+ }
+ return 0; // stay silent
+}
+
int main(void) {
DDRD = (1<<6);
PORTD = (1<<6);
+ PORTC = (1<<4) | (1<<5); // I2C Pullups
setup_uart(9600);
uart_setup_stdout();
dynamixel_init();
dynamixel_listen_init();
+ dynamixel_slave_set_cb(slave_cmd);
+ // MLX90614 IR thermometer on the I2C bus (A4 SDA / A5 SCL)
+ i2c_init();
// LED
// Timer 0: Fast PWM non-inverting mode, Top=255 => ~1kHz
--- /dev/null
+#include <stdint.h>
+#include "mlx90614.h"
+#include "i2cmaster.h"
+
+// MLX90614: raw object temperature register (0x07), data low byte first,
+// followed by a PEC byte (not checked).
+// deg C = raw * 0.02 - 273.15 => 0.1 deg C = (raw - 13657) / 5
+uint16_t mlx90614_read_temp_tenths(void)
+{
+ uint16_t raw;
+ uint8_t lo, hi;
+
+ if (i2c_start((MLX90614_ADDR << 1) | I2C_WRITE) ||
+ i2c_write(MLX90614_REG_TOBJ1) ||
+ i2c_rep_start((MLX90614_ADDR << 1) | I2C_READ))
+ return 0; // sensor absent or bus busy
+
+ lo = i2c_readAck();
+ hi = i2c_readAck();
+ i2c_readNak(); // PEC byte
+ raw = (uint16_t) lo | ((uint16_t) hi << 8);
+ if (raw == 0)
+ return 0;
+ return (uint16_t) (((int16_t) raw - 13657) / 5);
+}
--- /dev/null
+#ifndef MLX90614_H
+#define MLX90614_H
+
+#include <stdint.h>
+
+// MLX90614 IR thermometer on the I2C bus (A4 SDA / A5 SCL), driven with
+// the i2c/ library (hardware TWI, 100 kHz). Call i2c_init() once at boot.
+#define MLX90614_ADDR 0x5A // 7-bit I2C address (Adafruit library default)
+#define MLX90614_REG_TOBJ1 0x07 // raw object temperature register
+
+// Object temperature in 0.1 deg C (214 == 21.4 deg C).
+// Returns 0 if the sensor does not answer.
+uint16_t mlx90614_read_temp_tenths(void);
+
+#endif