1 ///////////////////////////////////////////////////////////////////////////////
\r
3 // IAR Atmel AVR C/C++ Compiler V4.30F/W32 12/Mar/2008 23:01:39 /
\r
4 // Copyright 1996-2007 IAR Systems. All rights reserved. /
\r
6 // Source file = C:\home\kevin\pub\src\bc100_cal\IAR\USI.c /
\r
7 // Command line = C:\home\kevin\pub\src\bc100_cal\IAR\USI.c /
\r
8 // --cpu=tiny861 -ms -o C:\home\kevin\pub\src\bc100_cal\IA /
\r
9 // R\Debug\Obj\ -lC C:\home\kevin\pub\src\bc100_cal\IAR\De /
\r
10 // bug\List\ -lB C:\home\kevin\pub\src\bc100_cal\IAR\Debug /
\r
11 // \List\ --initializers_in_flash -z2 --no_cse /
\r
12 // --no_inline --no_code_motion --no_cross_call /
\r
13 // --no_clustering --no_tbaa --debug /
\r
14 // -DENABLE_BIT_DEFINITIONS -e --require_prototypes -I /
\r
15 // "C:\Program Files\IAR Systems\Embedded Workbench /
\r
16 // 4.0\avr\INC\" -I "C:\Program Files\IAR /
\r
17 // Systems\Embedded Workbench 4.0\avr\INC\CLIB\" /
\r
18 // --eeprom_size 512 /
\r
19 // List file = C:\home\kevin\pub\src\bc100_cal\IAR\Debug\List\USI.s90 /
\r
22 ///////////////////////////////////////////////////////////////////////////////
\r
26 RSEG CSTACK:DATA:NOROOT(0)
\r
27 RSEG RSTACK:DATA:NOROOT(0)
\r
29 EXTERN ?need_segment_init
\r
33 PUBWEAK `?<Segment init: NEAR_Z>`
\r
34 PUBWEAK `??USI_OVF_ISR??INTVEC 16`
\r
51 USI_OVF_ISR SYMBOL "USI_OVF_ISR"
\r
52 `??USI_OVF_ISR??INTVEC 16` SYMBOL "??INTVEC 16", USI_OVF_ISR
\r
62 // C:\home\kevin\pub\src\bc100_cal\IAR\USI.c
\r
63 // 1 /* This file has been prepared for Doxygen automatic documentation generation.*/
\r
64 // 2 /*! \file *********************************************************************
\r
67 // 5 * Functions for use of the Universal Serial Interface
\r
69 // 7 * Contains high level functions for initializing the USI as an SPI slave,
\r
70 // 8 * interrupt handling, sending and receiving single bytes.
\r
72 // 10 * \par Application note:
\r
73 // 11 * AVR458: Charging Li-Ion Batteries with BC100 \n
\r
74 // 12 * AVR463: Charging NiMH Batteries with BC100
\r
76 // 14 * \par Documentation:
\r
77 // 15 * For comprehensive code documentation, supported compilers, compiler
\r
78 // 16 * settings and supported devices see readme.html
\r
81 // 19 * Atmel Corporation: http://www.atmel.com \n
\r
82 // 20 * Support email: avr@atmel.com \n
\r
83 // 21 * Original author: \n
\r
86 // 24 * $Revision: 2299 $
\r
88 // 26 * $URL: http://svn.norway.atmel.com/AppsAVR8/avr458_Charging_Li-Ion_Batteries_with_BC100/tag/20070904_release_1.0/code/IAR/USI.c $
\r
89 // 27 * $Date: 2007-08-23 12:55:51 +0200 (to, 23 aug 2007) $\n
\r
90 // 28 ******************************************************************************/
\r
92 // 30 #include <ioavr.h>
\r
94 ASEGN ABSOLUTE:DATA:NOROOT,038H
\r
95 // <unnamed> volatile __io _A_PORTB
\r
99 ASEGN ABSOLUTE:DATA:NOROOT,037H
\r
100 // <unnamed> volatile __io _A_DDRB
\r
104 ASEGN ABSOLUTE:DATA:NOROOT,02fH
\r
105 // <unnamed> volatile __io _A_USIDR
\r
109 ASEGN ABSOLUTE:DATA:NOROOT,02eH
\r
110 // <unnamed> volatile __io _A_USISR
\r
114 ASEGN ABSOLUTE:DATA:NOROOT,02dH
\r
115 // <unnamed> volatile __io _A_USICR
\r
118 // 31 #include <inavr.h>
\r
120 // 33 #include "enums.h"
\r
121 // 34 #include "structs.h"
\r
123 // 36 #include "main.h"
\r
124 // 37 #include "ADC.h"
\r
125 // 38 #include "battery.h"
\r
126 // 39 #include "time.h"
\r
127 // 40 #include "USI.h"
\r
130 // 43 //******************************************************************************
\r
132 // 45 //******************************************************************************
\r
133 // 46 //! SPI status struct
\r
135 RSEG NEAR_Z:DATA:NOROOT(0)
\r
136 REQUIRE `?<Segment init: NEAR_Z>`
\r
137 // 47 SPI_Status_t SPI;
\r
142 // 50 //******************************************************************************
\r
144 // 52 //*****************************************************************************
\r
145 // 53 /*! \brief USI Counter Overflow Interrupt Service Routine
\r
147 // 55 * When the USI counter overflows, a byte has been transferred.\n
\r
148 // 56 * The USIDR contents are stored and flags are updated.
\r
150 // 58 * The protocol is quite simple and has three sequential states: command,
\r
151 // 59 * address and data.
\r
152 // 60 * (Keep in mind that the Master is in charge of data clocking, which means
\r
153 // 61 * there is a one byte "delay" from when the Slave puts something to SPI till
\r
154 // 62 * the Master can read it.)
\r
156 // 64 * 1. If a non-zero byte is received in the command state, the ISR will
\r
157 // 65 * store the commands to the SPI struct (read/write, EEPROM/SRAM, number of
\r
158 // 66 * bytes). To signal that the command was received, 0xCC is put to the SPI bus.
\r
159 // 67 * If a zero byte (0x00) is received in the command state, it is simply ignored
\r
160 // 68 * because it is an invalid command.
\r
162 // 70 * 2. When a byte is received in the address state, it is stored to the SPI
\r
163 // 71 * struct. To signal that the address was received, 0xBB is put to SPI bus.
\r
165 // 73 * 3. In the data state, variables are read/written "from back to front" until
\r
166 // 74 * the byte counter reaches zero. Since the Master is in charge of the data
\r
167 // 75 * clocking, the Slave will go to command state before the last byte is
\r
168 // 76 * transferred during reading. This means that the Master should send an
\r
169 // 77 * invalid command when getting each byte, ie 0x00.
\r
171 // 79 * If the time between two transfers is 1 second or more, the Slave
\r
172 // 80 * automatically reverts to command state.
\r
174 // 82 * \note Battery charging is not automatically halted during SPI communication.
\r
175 // 83 * This means that the current charge state (current and voltage) will
\r
176 // 84 * remain constant during heavy and prolonged serial traffic.
\r
178 // 86 * \todo Variable writing not implemented yet.
\r
179 // 87 * \todo EEPROM/SRAM flag doesn't really do anything with this implementation.
\r
181 // 89 #pragma vector=USI_OVF_vect
\r
183 RSEG CODE:CODE:NOROOT(1)
\r
184 // 90 __interrupt void USI_OVF_ISR(void)
\r
205 // 92 // If the communication timed out, set ST_CMD as current state.
\r
206 // 93 if (!Time_Left(TIMER_USI)) {
\r
210 BRNE ??USI_OVF_ISR_0
\r
211 // 94 SPI.State = ST_CMD;
\r
218 // 97 // Start communication timer. If further communication doesn't happen
\r
219 // 98 // within 1 second, the SPI communication state is reset to CMD.
\r
220 // 99 Time_Set(TIMER_USI, 0, 1, 0);
\r
229 // 101 // Clear USI counter and flag completed transfer.
\r
230 // 102 USISR = (1<<USIOIF);
\r
233 // 103 SPI.XferComplete = TRUE;
\r
235 LDI R31, (SPI) >> 8
\r
240 // 105 // Process incoming data.
\r
241 // 106 switch(SPI.State) {
\r
248 BREQ ??USI_OVF_ISR_1
\r
251 RJMP ??USI_OVF_ISR_2
\r
254 RJMP ??USI_OVF_ISR_3
\r
255 RJMP ??USI_OVF_ISR_4
\r
256 // 107 // A valid SPI transfer starts with a Command Byte sent by the Master.
\r
257 // 108 case ST_CMD:
\r
258 // 109 SPI.Data = USIDR; // Store the transferred byte.
\r
264 // 111 // If the master sent 0, it is trying to get data. Ignore in this state.
\r
265 // 112 if (SPI.Data != 0) {
\r
269 RJMP ??USI_OVF_ISR_4
\r
270 // 113 // Does the master want to read or write?
\r
271 // 114 if (SPI.Data & 0x40) {
\r
273 LDI R31, (SPI) >> 8
\r
276 RJMP ??USI_OVF_ISR_5
\r
277 // 115 SPI.Read = FALSE;
\r
279 LDI R31, (SPI) >> 8
\r
283 RJMP ??USI_OVF_ISR_6
\r
285 // 117 SPI.Read = TRUE;
\r
288 LDI R31, (SPI) >> 8
\r
294 // 120 // From/to EEPROM or SRAM?
\r
295 // 121 if (SPI.Data &0x80) {
\r
298 LDI R31, (SPI) >> 8
\r
301 RJMP ??USI_OVF_ISR_7
\r
302 // 122 SPI.EEPROM = TRUE;
\r
304 LDI R31, (SPI) >> 8
\r
308 RJMP ??USI_OVF_ISR_8
\r
310 // 124 SPI.EEPROM = FALSE;
\r
313 LDI R31, (SPI) >> 8
\r
319 // 127 SPI.Count = (SPI.Data & 0x3F); // Get number of bytes to receive/send.
\r
327 // 128 SPI.State = ST_ADDR; // The Master will send the address byte next.
\r
333 // 130 SPI_Put(0xCC); // Signal that command was received.
\r
336 RJMP ??USI_OVF_ISR_4
\r
341 // 135 case ST_ADDR:
\r
342 // 136 SPI.Data = USIDR; // Store the address.
\r
347 // 137 SPI.Address = SPI.Data;
\r
351 // 138 SPI.State = ST_DATA; // The master will send/wait for data next.
\r
353 LDI R31, (SPI) >> 8
\r
358 // 140 SPI_Put(0xBB); // Signal that address was received.
\r
361 RJMP ??USI_OVF_ISR_4
\r
365 // 144 // Note well: this will process at least one byte, regardless of Count.
\r
366 // 145 case ST_DATA:
\r
367 // 146 if (SPI.Count-- > 0) {
\r
380 RJMP ??USI_OVF_ISR_9
\r
381 // 147 // Write specified variable to SPI, "back to front".
\r
382 // 148 if (SPI.Read) {
\r
384 LDI R31, (SPI) >> 8
\r
387 RJMP ??USI_OVF_ISR_10
\r
388 // 149 switch (SPI.Address) {
\r
391 BREQ ??USI_OVF_ISR_11
\r
393 BREQ ??USI_OVF_ISR_12
\r
395 BREQ ??USI_OVF_ISR_13
\r
397 BREQ ??USI_OVF_ISR_14
\r
399 BREQ ??USI_OVF_ISR_15
\r
400 RJMP ??USI_OVF_ISR_16
\r
401 // 150 case ADR_ADCS:
\r
402 // 151 SPI_Put(*(((unsigned char*)&ADCS) + (SPI.Count)));
\r
407 MOVW R31:R30, R17:R16
\r
408 SUBI R30, LOW((-(ADCS) & 0xFFFF))
\r
409 SBCI R31, (-(ADCS) & 0xFFFF) >> 8
\r
412 RJMP ??USI_OVF_ISR_4
\r
416 // 155 case ADR_BATTACTIVE:
\r
417 // 156 SPI_Put(*((unsigned char*)&BattActive + (SPI.Count)));
\r
422 MOVW R31:R30, R17:R16
\r
423 SUBI R30, LOW((-(BattActive) & 0xFFFF))
\r
424 SBCI R31, (-(BattActive) & 0xFFFF) >> 8
\r
427 RJMP ??USI_OVF_ISR_4
\r
431 // 160 case ADR_BATTDATA:
\r
432 // 161 SPI_Put(*((unsigned char*)&BattData + (SPI.Count)));
\r
437 MOVW R31:R30, R17:R16
\r
438 SUBI R30, LOW((-(BattData) & 0xFFFF))
\r
439 SBCI R31, (-(BattData) & 0xFFFF) >> 8
\r
442 RJMP ??USI_OVF_ISR_4
\r
446 // 165 case ADR_BATTCTRL:
\r
447 // 166 SPI_Put(*((__eeprom unsigned char*)&BattControl + (SPI.Count)));
\r
452 LDI R20, LOW(BattControl)
\r
453 LDI R21, (BattControl) >> 8
\r
458 RJMP ??USI_OVF_ISR_4
\r
461 // 169 case ADR_TIMERS:
\r
462 // 170 SPI_Put(*((unsigned char*)&timeval + (SPI.Count)));
\r
467 MOVW R31:R30, R17:R16
\r
468 SUBI R30, LOW((-(timeval) & 0xFFFF))
\r
469 SBCI R31, (-(timeval) & 0xFFFF) >> 8
\r
472 RJMP ??USI_OVF_ISR_4
\r
481 RJMP ??USI_OVF_ISR_4
\r
485 // 179 // Read byte from SPI
\r
486 // 180 SPI.Data = USIDR;
\r
492 // 182 // ********************************************
\r
493 // 183 // THIS FUNCTION HAS NOT BEEN FULLY IMPLEMENTED
\r
494 // 184 // ********************************************
\r
496 // 186 // Save byte to specified variable.
\r
497 // 187 switch (SPI.Address) {
\r
500 BRNE ??USI_OVF_ISR_4
\r
501 // 188 case ADR_BATTCTRL:
\r
502 // 189 *((__eeprom unsigned char*)&BattControl + SPI.Count) = SPI.Data;
\r
507 LDI R20, LOW(BattControl)
\r
508 LDI R21, (BattControl) >> 8
\r
512 RJMP ??USI_OVF_ISR_4
\r
523 // 200 SPI.State = ST_CMD;
\r
532 // 204 default: // Shouldn't end up here. (Unknown SPI-state)
\r
560 // 210 /*! \brief Initializes USI as an SPI slave
\r
562 // 212 * Initializes USI as a 3-wire SPI slave using the pins specified in USI.h for
\r
563 // 213 * I/O and clock, and USI counter overflow interrupts enabled.\n
\r
564 // 214 * Also initializes the SPI status struct.
\r
566 // 216 * \param spi_mode Specifies if USI should trigger on positive (0) or negative
\r
567 // 217 * (1) edge of clock signal
\r
569 // 219 * \note Clears the stored data
\r
571 // 221 * \todo Timer should reset SPI protocol on timeout
\r
574 RSEG CODE:CODE:NOROOT(1)
\r
575 // 223 void SPI_Init(unsigned char spi_mode)
\r
578 // 225 __disable_interrupt();
\r
581 // 227 // Configure outputs and inputs, enable pull-ups for DATAIN and CLOCK pins.
\r
582 // 228 USI_DIR_REG |= (1<<USI_DATAOUT_PIN);
\r
584 // 229 USI_DIR_REG &= ~((1<<USI_DATAIN_PIN) | (1<<USI_CLOCK_PIN));
\r
588 // 230 USI_OUT_REG |= (1<<USI_DATAIN_PIN) | (1<<USI_CLOCK_PIN);
\r
593 // 232 // Configure USI to 3-wire slave mode with overflow interrupt
\r
594 // 233 USICR = ( (1<<USIOIE) | (1<<USIWM0) | (1<<USICS1) | (spi_mode<<USICS0) );
\r
601 // 235 // Initialize the SPI struct
\r
602 // 236 SPI.Data = 0; // Clear data.
\r
605 // 237 SPI.State = ST_CMD; // Initial SPI state: wait for command.
\r
610 // 238 SPI.Read = FALSE; // Doesn't matter right now.
\r
612 LDI R31, (SPI) >> 8
\r
616 // 239 SPI.EEPROM = FALSE; // Doesn't matter right now.
\r
618 LDI R31, (SPI) >> 8
\r
622 // 240 SPI.Count = 0; // Doesn't matter right now.
\r
624 LDI R31, (SPI) >> 8
\r
628 // 241 SPI.Address = 0; // Doesn't matter right now.
\r
631 // 242 SPI.XferComplete = FALSE; // We haven't even started a transfer yet.
\r
633 LDI R31, (SPI) >> 8
\r
637 // 243 SPI.WriteCollision = FALSE; // ..And therefore a collision hasn't happened.
\r
639 LDI R31, (SPI) >> 8
\r
644 // 245 __enable_interrupt();
\r
653 // 249 // Put one byte on bus. Use this function like you would write to the SPDR
\r
654 // 250 // register in the native SPI module. Calling this function will prepare a
\r
655 // 251 // byte for the next transfer initiated by the master device. If a transfer
\r
656 // 252 // is in progress, this function will set the write collision flag and return
\r
657 // 253 // without altering the data registers.
\r
659 // 255 // Returns 0 if a write collision occurred, 1 otherwise.
\r
660 // 256 /*! \brief Write a byte to SPI bus
\r
662 // 258 * This function first checks if a transmission is in progress, and if so, flags
\r
663 // 259 * a write collision, and returns FALSE.\n
\r
664 // 260 * If a transmission is not in progress, the flags for write collision and
\r
665 // 261 * transfer complete are cleared, and the input byte is written to SPDR.\n
\r
667 // 263 * \param val The byte to send.
\r
669 // 265 * \retval FALSE A write collision happened.
\r
670 // 266 * \retval TRUE Byte written to SPDR.
\r
673 RSEG CODE:CODE:NOROOT(1)
\r
674 // 268 unsigned char SPI_Put(unsigned char val)
\r
678 // 270 // Check if transmission in progress, i.e. if USI counter doesn't equal zero.
\r
679 // 271 // If this fails, flag a write collision and return.
\r
680 // 272 if((USISR & 0x0F) != 0) {
\r
685 // 273 SPI.WriteCollision = TRUE;
\r
687 LDI R31, (SPI) >> 8
\r
691 // 274 return(FALSE);
\r
696 // 277 // Reinitialize flags.
\r
697 // 278 SPI.XferComplete = FALSE;
\r
700 LDI R31, (SPI) >> 8
\r
704 // 279 SPI.WriteCollision = FALSE;
\r
706 LDI R31, (SPI) >> 8
\r
711 // 281 USIDR = val; // Put data in USI data register.
\r
714 // 283 return (TRUE);
\r
722 // 287 // Get one byte from bus. This function only returns the previous stored
\r
723 // 288 // USIDR value. The transfer complete flag is not checked. Use this function
\r
724 // 289 // like you would read from the SPDR register in the native SPI module.
\r
725 // 290 /*! \brief Get the last byte received from SPI bus
\r
727 // 292 * This function simply returns the last byte stored to the SPI status struct,
\r
728 // 293 * without checking if a completed transfer is flagged.
\r
730 // 295 * \retval SPI.Data The last byte read from SPI.
\r
733 RSEG CODE:CODE:NOROOT(1)
\r
734 // 297 unsigned char SPI_Get(void)
\r
737 // 299 return SPI.Data;
\r
743 // 303 /*! \brief Wait for SPI transfer to complete
\r
745 // 305 * This function waits for a transfer complete to be flagged.
\r
748 RSEG CODE:CODE:NOROOT(1)
\r
749 // 307 void SPI_Wait(void)
\r
753 // 309 do { // Wait for transfer complete.
\r
754 // 310 } while (SPI.XferComplete == FALSE);
\r
756 LDI R31, (SPI) >> 8
\r
763 ASEGN ABSOLUTE:DATA:NOROOT,01cH
\r
766 ASEGN ABSOLUTE:DATA:NOROOT,01dH
\r
769 ASEGN ABSOLUTE:DATA:NOROOT,01eH
\r
772 ASEGN ABSOLUTE:DATA:NOROOT,01fH
\r
775 COMMON INTVEC:CODE:ROOT(1)
\r
777 `??USI_OVF_ISR??INTVEC 16`:
\r
780 RSEG INITTAB:CODE:NOROOT(0)
\r
781 `?<Segment init: NEAR_Z>`:
\r
782 DW SFE(NEAR_Z) - SFB(NEAR_Z)
\r
785 REQUIRE ?need_segment_init
\r
789 // 5 bytes in segment ABSOLUTE
\r
790 // 706 bytes in segment CODE
\r
791 // 6 bytes in segment INITTAB
\r
792 // 2 bytes in segment INTVEC
\r
793 // 4 bytes in segment NEAR_Z
\r
795 // 706 bytes of CODE memory (+ 8 bytes shared)
\r
796 // 4 bytes of DATA memory (+ 5 bytes shared)
\r