From d4f56fdd5ba1bbcd10bcaf4d0bc1a54384e8d06f Mon Sep 17 00:00:00 2001 From: Zhou Ming Date: Sun, 5 Apr 2009 20:39:16 +0800 Subject: [PATCH] add printf support --- .../test-master-side/Makefile | 2 +- .../test-master-side/TWI_Master.c | 262 -------- .../test-master-side/TWI_Master.h | 110 ---- .../flight_ctl_experiment/test-master-side/main.c | 677 ++++++++------------- .../flight_ctl_experiment/test-master-side/uart.c | 20 - 5 files changed, 269 insertions(+), 802 deletions(-) delete mode 100644 people/chowming/flight_ctl_experiment/test-master-side/TWI_Master.c delete mode 100644 people/chowming/flight_ctl_experiment/test-master-side/TWI_Master.h rewrite people/chowming/flight_ctl_experiment/test-master-side/main.c (64%) diff --git a/people/chowming/flight_ctl_experiment/test-master-side/Makefile b/people/chowming/flight_ctl_experiment/test-master-side/Makefile index 33ff54d..a62f014 100644 --- a/people/chowming/flight_ctl_experiment/test-master-side/Makefile +++ b/people/chowming/flight_ctl_experiment/test-master-side/Makefile @@ -80,7 +80,7 @@ OBJDIR = . # List C source files here. (C dependencies are automatically generated.) -SRC = $(TARGET).c TWI_Master.c uart.c +SRC = $(TARGET).c twi_master.c uart.c # List C++ source files here. (C dependencies are automatically generated.) diff --git a/people/chowming/flight_ctl_experiment/test-master-side/TWI_Master.c b/people/chowming/flight_ctl_experiment/test-master-side/TWI_Master.c deleted file mode 100644 index 25856e1..0000000 --- a/people/chowming/flight_ctl_experiment/test-master-side/TWI_Master.c +++ /dev/null @@ -1,262 +0,0 @@ -/***************************************************************************** -* -* Atmel Corporation -* -* File : TWI_Master.c -* Compiler : IAR EWAAVR 2.28a/3.10c -* Revision : $Revision: 1.13 $ -* Date : $Date: 24. mai 2004 11:31:20 $ -* Updated by : $Author: ltwa $ -* -* Support mail : avr@atmel.com -* -* Supported devices : All devices with a TWI module can be used. -* The example is written for the ATmega16 -* -* AppNote : AVR315 - TWI Master Implementation -* -* Description : This is a sample driver for the TWI hardware modules. -* It is interrupt driveren. All functionality is controlled through -* passing information to and from functions. Se main.c for samples -* of how to use the driver. -* -* -****************************************************************************/ - -#if __GNUC__ > 0 -/* AVR-GCC/avr-libc */ - -# include -# include -# include -# include - -# define disable_interrupt cli -# define enable_interrupt sei - -# define delay_cycles(n) _delay_loop_2((n) / 4) - -# define FLASH_DECL(decl) decl PROGMEM -# define FLASH_READ_BYTE(addr) pgm_read_byte(addr) -# define FLASH_READ_WORD(addr) pgm_read_word(addr) - -#elif defined(__ICCAVR__) - -# include -# include - -# define disable_interrupt __disable_interrupt -# define enable_interrupt __enable_interrupt - -# define delay_cycles(n) __delay_cycles(n) - -# define FLASH_DECL(decl) __flash decl -# define FLASH_READ_BYTE(addr) *(addr) -# define FLASH_READ_WORD(addr) *(addr) - -/* - * Map interrupt vectors. - * - * Beware: nasty dragons here. Looks like a nice example for why - * you wouldn't want to use pragmas in a compiler, doesn't it? - * - * Do *NOT* try to reorder the macros below, or you'll suddenly find - * out about all kinds of IAR bugs... - */ -#define PRAGMA(x) _Pragma( #x ) -#define ISR(vec) PRAGMA( vector=vec ) __interrupt void handler_##vec(void) - -#else - -# error "Unsupported compiler." - -#endif - -#include "TWI_Master.h" - -static unsigned char TWI_buf[ TWI_BUFFER_SIZE ]; // Transceiver buffer -static unsigned char TWI_msgSize; // Number of bytes to be transmitted. -static unsigned char TWI_state = TWI_NO_STATE; // State byte. Default set to TWI_NO_STATE. - -union TWI_statusReg TWI_statusReg = {0}; // TWI_statusReg is defined in TWI_Master.h - -/**************************************************************************** -Call this function to set up the TWI master to its initial standby state. -Remember to enable interrupts from the main application after initializing the TWI. -****************************************************************************/ -void TWI_Master_Initialise(void) -{ - TWBR = TWI_TWBR; // Set bit rate register (Baudrate). Defined in header file. -// TWSR = TWI_TWPS; // Not used. Driver presumes prescaler to be 00. - TWDR = 0xFF; // Default content = SDA released. - TWCR = (1< 0 -/* AVR-GCC/avr-libc */ - -# include -# include -# include -# include - -# define disable_interrupt cli -# define enable_interrupt sei - -# define delay_cycles(n) _delay_loop_2((n) / 4) - -# define FLASH_DECL(decl) decl PROGMEM -# define FLASH_READ_BYTE(addr) pgm_read_byte(addr) -# define FLASH_READ_WORD(addr) pgm_read_word(addr) - -#elif defined(__ICCAVR__) - -# include -# include - -# define disable_interrupt __disable_interrupt -# define enable_interrupt __enable_interrupt - -# define delay_cycles(n) __delay_cycles(n) - -# define FLASH_DECL(decl) __flash decl -# define FLASH_READ_BYTE(addr) *(addr) -# define FLASH_READ_WORD(addr) *(addr) - -/* - * Map interrupt vectors. - * - * Beware: nasty dragons here. Looks like a nice example for why - * you wouldn't want to use pragmas in a compiler, doesn't it? - * - * Do *NOT* try to reorder the macros below, or you'll suddenly find - * out about all kinds of IAR bugs... - */ -#define PRAGMA(x) _Pragma( #x ) -#define ISR(vec) PRAGMA( vector=vec ) __interrupt void handler_##vec(void) - -#else - -# error "Unsupported compiler." - -#endif - -#include "TWI_Master.h" - -#define TWI_GEN_CALL 0x00 // The General Call address is 0 - -// Sample TWI transmission commands -#define TWI_CMD_MASTER_WRITE 0x10 -#define TWI_CMD_MASTER_READ 0x20 - -// Sample TWI transmission states, used in the main application. -#define SEND_DATA 0x01 -#define REQUEST_DATA 0x02 -#define READ_DATA_FROM_BUFFER 0x03 - -// add from old working timer 500hz code -#define TIMER_CLOCK_FREQ 15625.0 //2MHz for /8 prescale from 16MHz -#define TOGGLE_IO 9 //Arduino pin to toggle in timer ISR -///////////////////////////////// - -unsigned char* pchar = "Hello Quadcopter group!........"; -int timerLoadValue; -int latency; -unsigned char messageBuf[16]; -unsigned char TWI_targetSlaveAddress1, TWI_targetSlaveAddress2, TWI_targetSlaveAddress3, TWI_targetSlaveAddress4; -unsigned char temp, TWI_operation=0, - pressedButton, myCounter=0; -unsigned char which_motor=0; - -unsigned char TWI_Act_On_Failure_In_Last_Transmission ( unsigned char TWIerrorMsg ) -{ - // A failure has occurred, use TWIerrorMsg to determine the nature of the failure - // and take appropriate actions. - // Se header file for a list of possible failures messages. - - // Here is a simple sample, where if received a NACK on the slave address, - // then a retransmission will be initiated. - - if ( (TWIerrorMsg == TWI_MTX_ADR_NACK) | (TWIerrorMsg == TWI_MRX_ADR_NACK) ) - TWI_Start_Transceiver(); - - return TWIerrorMsg; -} - -unsigned int SetupTimer2(float timeoutFrequency){ - unsigned int result; //The timer load value. - //Calculate the timer load value 257 - result=(int)((257.0-(1.032*TIMER_CLOCK_FREQ/timeoutFrequency))+0.5); - //The 257 really should be 256 but I get better results with 257. - //Timer2 Settings: Timer Prescaler /8, mode 0 - //Timer clock = 16MHz/8 = 2Mhz or 0.5us - //The /8 prescale gives us a good range to work with - //so we just hard code this for now. - TCCR2A = 0; -// we setting a Prescaler of 1024 ; - TCCR2B = 1< 12) which_motor = 0; -#if 0 - unsigned char i; - unsigned int j; - for(i=0;i<1;i++){ - USART_Transmit('0'); - //kick the first moto - messageBuf[0] = (TWI_targetSlaveAddress1<>1; - TWI_targetSlaveAddress2 = 0x54>>1; - TWI_targetSlaveAddress3 = 0x56>>1; - TWI_targetSlaveAddress4 = 0x58>>1; - TWI_Master_Initialise(); - messageBuf[0] = (TWI_targetSlaveAddress1< 0 +/* AVR-GCC/avr-libc */ + +# include +# include +# include +# include +# include + +# define disable_interrupt cli +# define enable_interrupt sei + +# define delay_cycles(n) _delay_loop_2((n) / 4) + +# define FLASH_DECL(decl) decl PROGMEM +# define FLASH_READ_BYTE(addr) pgm_read_byte(addr) +# define FLASH_READ_WORD(addr) pgm_read_word(addr) + +#elif defined(__ICCAVR__) + +# include +# include + +# define disable_interrupt __disable_interrupt +# define enable_interrupt __enable_interrupt + +# define delay_cycles(n) __delay_cycles(n) + +# define FLASH_DECL(decl) __flash decl +# define FLASH_READ_BYTE(addr) *(addr) +# define FLASH_READ_WORD(addr) *(addr) + +/* + * Map interrupt vectors. + * + * Beware: nasty dragons here. Looks like a nice example for why + * you wouldn't want to use pragmas in a compiler, doesn't it? + * + * Do *NOT* try to reorder the macros below, or you'll suddenly find + * out about all kinds of IAR bugs... + */ +#define PRAGMA(x) _Pragma( #x ) +#define ISR(vec) PRAGMA( vector=vec ) __interrupt void handler_##vec(void) + +#else + +# error "Unsupported compiler." + +#endif + +#include "twi_master.h" +#include "uart.h" + +#define TWI_GEN_CALL 0x00 // The General Call address is 0 + +// Sample TWI transmission commands +#define TWI_CMD_MASTER_WRITE 0x10 +#define TWI_CMD_MASTER_READ 0x20 + +// Sample TWI transmission states, used in the main application. +#define SEND_DATA 0x01 +#define REQUEST_DATA 0x02 +#define READ_DATA_FROM_BUFFER 0x03 + +// add from old working timer 500hz code +#define TIMER_CLOCK_FREQ 15625.0 //2MHz for /8 prescale from 16MHz +#define TOGGLE_IO 9 //Arduino pin to toggle in timer ISR +///////////////////////////////// + +int timerLoadValue; +int latency; +unsigned char messageBuf[16]; +unsigned char TWI_targetSlaveAddress1, TWI_targetSlaveAddress2, TWI_targetSlaveAddress3, TWI_targetSlaveAddress4; +unsigned char temp, TWI_operation=0, + pressedButton, myCounter=0; +unsigned char which_motor=0; + +FILE uart_str = FDEV_SETUP_STREAM(USART_Transmit, USART_Receive, _FDEV_SETUP_RW); + +unsigned char TWI_Act_On_Failure_In_Last_Transmission ( unsigned char TWIerrorMsg ) +{ + // A failure has occurred, use TWIerrorMsg to determine the nature of the failure + // and take appropriate actions. + // Se header file for a list of possible failures messages. + + // Here is a simple sample, where if received a NACK on the slave address, + // then a retransmission will be initiated. + + if ( (TWIerrorMsg == TWI_MTX_ADR_NACK) | (TWIerrorMsg == TWI_MRX_ADR_NACK) ) + TWI_Start_Transceiver(); + + return TWIerrorMsg; +} + +unsigned int SetupTimer2(float timeoutFrequency){ + unsigned int result; //The timer load value. + //Calculate the timer load value 257 + result=(int)((257.0-(1.032*TIMER_CLOCK_FREQ/timeoutFrequency))+0.5); + //The 257 really should be 256 but I get better results with 257. + //Timer2 Settings: Timer Prescaler /8, mode 0 + //Timer clock = 16MHz/8 = 2Mhz or 0.5us + //The /8 prescale gives us a good range to work with + //so we just hard code this for now. + TCCR2A = 0; +// we setting a Prescaler of 1024 ; + TCCR2B = 1< 12) which_motor = 0; +} + + +int main( void ) +{ + + unsigned int j; + unsigned char cmd; + TWI_targetSlaveAddress1 = 0x52>>1; + TWI_targetSlaveAddress2 = 0x54>>1; + TWI_targetSlaveAddress3 = 0x56>>1; + TWI_targetSlaveAddress4 = 0x58>>1; + TWI_Master_Initialise(); + messageBuf[0] = (TWI_targetSlaveAddress1<0) + messageBuf[1] -= 1; + break; + case '2': + if (messageBuf[5]<255) + messageBuf[5] += 1; + break; + case 'w': + if (messageBuf[5]>0) + messageBuf[5] -= 1; + break; + case '3': + if (messageBuf[9]<255) + messageBuf[9] += 1; + break; + case 'e': + if (messageBuf[9]>0) + messageBuf[9] -= 1; + break; + case '4': + if (messageBuf[13]<255) + messageBuf[13] += 1; + break; + case 'r': + if (messageBuf[13]>0) + messageBuf[13] -= 1; + break; + case 'a': + if (messageBuf[1]<255) + messageBuf[1] += 1; + if (messageBuf[5]<255) + messageBuf[5] += 1; + if (messageBuf[9]<255) + messageBuf[9] += 1; + if (messageBuf[13]<255) + messageBuf[13] += 1; + break; + case 'z': + if (messageBuf[1]>0) + messageBuf[1] -= 1; + if (messageBuf[5]>0) + messageBuf[5] -= 1; + if (messageBuf[9]>0) + messageBuf[9] -= 1; + if (messageBuf[13]>0) + messageBuf[13] -= 1; + break; + default: + break; + } +#if 0 + USART_Transmit(((messageBuf[1]&0xf0)>>4)+0x30); + USART_Transmit((messageBuf[1]&0xf)+0x30); + USART_Transmit(' '); + USART_Transmit(((messageBuf[5]&0xf0)>>4)+0x30); + USART_Transmit((messageBuf[5]&0xf)+0x30); + USART_Transmit(' '); + USART_Transmit(((messageBuf[9]&0xf0)>>4)+0x30); + USART_Transmit((messageBuf[9]&0xf)+0x30); + USART_Transmit(' '); + USART_Transmit(((messageBuf[13]&0xf0)>>4)+0x30); + USART_Transmit((messageBuf[13]&0xf)+0x30); + USART_Transmit('\n'); + USART_Transmit('\a'); +#endif + printf("%3d %3d %3d %3d\n", messageBuf[1], messageBuf[5], \ + messageBuf[9], messageBuf[13]); + } +} + diff --git a/people/chowming/flight_ctl_experiment/test-master-side/uart.c b/people/chowming/flight_ctl_experiment/test-master-side/uart.c index 319adbf..c8d5ae6 100644 --- a/people/chowming/flight_ctl_experiment/test-master-side/uart.c +++ b/people/chowming/flight_ctl_experiment/test-master-side/uart.c @@ -30,26 +30,6 @@ static unsigned char USART_TxBuf[USART_TX_BUFFER_SIZE]; static volatile unsigned char USART_TxHead; static volatile unsigned char USART_TxTail; -/* Prototypes */ -void USART_Init( unsigned int baudrate ); -unsigned char USART_Receive( void ); -void USART_Transmit( unsigned char data ); - -#if 0 -/* Main - a simple test program*/ -int main( void ) -{ - USART_Init( 8 ); /* Set the baudrate to 19,200 bps using a 3.6864MHz crystal */ - - sei(); /* Enable interrupts => enable UART interrupts */ - - for( ; ; ) /* Forever */ - { - USART_Transmit( USART_Receive() ); /* Echo the received character */ - } -} -#endif - /* Initialize USART */ void USART_Init( unsigned int baudrate ) { -- 2.11.4.GIT