/* serialEE_PrintBlock: * Prints out the block data pointed by EE_AddressData. */ void serialEE_PrintBlock(uint8_t block, uint8_t datasize) { uint8_t buffer[80]; EE_AddressStruct EE_Current; EE_Current.EE_Address = 0x00; EE_Current.EE_Block = block; uartTxString("\n\rNow reading from memory block:"); uartTx( (EE_Current.EE_Block) + '0'); uartTxString("\n\r"); /* Read until the last entry of the current block */ for( EE_Current.EE_Address = EE_START_ADDRESS; EE_Current.EE_Address < serialEE_getEndBlock(EE_Current.EE_Block); EE_Current.EE_Address += datasize) { serialEE_ReadBlock(buffer, datasize, &EE_Current); /* Parse the relevant information*/ uartTxString("\r\n"); uartTxString(buffer); } }
/* Print a NULL terminated string over UART. * string - null terminated sting to transmit over UART. */ void uartPrint(const char * string) { while (*string) { uartTx(*string++); } }
/* uartTxString: * Outputs the passed string to the UART.Tx pin * The output is true ouput, not inverted, so a MAX232 or some sort of * TTL -> +/- 15V converter is required. */ void uartTxString(uint8_t* outString) { while( *outString ) { uartTx(*outString++); } }
/** Writes nbytes of buffer to the UART */ void uartTxDump(uint8_t* buffer, uint8_t nbytes ) { uint16_t i = 0; while( i++ < nbytes ) { uartTx(*buffer++); } }
/* Send a string which resides in the program memory */ void uartTxString_P(const char* outString_P) { char c; while( (c = pgm_read_byte(outString_P++)) ) { uartTx(c); } }
void printEEPointer(EE_AddressStruct* EE_AddressData) { uartTxString("\n\rCurrent location of the EEPROM Pointer: \n\rBlock: "); uartTx( (EE_AddressData->EE_Block) + '0' ); utoa( (EE_AddressData->EE_Address / EE_PAGE_SIZE), OutputString, 10); uartTxString("\n\rPage: "); uartTxString(OutputString); utoa( EE_AddressData->EE_Address, OutputString, 10); uartTxString("\n\rAddress: "); uartTxString(OutputString); }
void serialEE_HandleBlockOverflow( EE_AddressStruct* EE_AddressData, uint8_t datasize) { /* Check that the current memory block is not full * (For 24XX16 & 24XX1025 devices only!) */ if( ((EE_AddressData->EE_Address) + datasize) > EE_BLOCK_SIZE ) { /* Record the last entry of the block */ EE_EndBlockAddress[(EE_AddressData->EE_Block)] = EE_AddressData->EE_Address; /* Select the next block */ EE_AddressData->EE_Block++; if( EE_AddressData->EE_Block >= EE_NUMBER_OF_BLOCKS) { /*ALL MEMORY BLOCKS HAVE BEEN USED UP!*/ /* Handle event here: */ uartTxString("\n\rWarning : All memory blocks have been used up!"); EE_AddressData->EE_Block = (EE_NUMBER_OF_BLOCKS - 1); } /* Reset Block address pointer */ EE_AddressData->EE_Address = 0x00; /* Print which block is currently selected */ uartTxString("\n\rNow writing to memory block:"); uartTx( (EE_AddressData->EE_Block) + '0'); uartTxString("\n\r"); } }
int main(void) { /* Watchdog */ wdt_reset(); wdt_disable(); /* Ports */ DDRB = _BV(PINB0) | _BV(PINB2) | _BV(PINB3) | _BV(PINB5); DDRD = _BV(PIND5) | _BV(PIND7); PORTB = 0xff; //& (_BV(PINB0)); PORTC = 0xff; PORTD = 0xff;// & (_BV(PIND5) | _BV(PIND6) | _BV(PIND7)); /* Power saving */ set_sleep_mode(SLEEP_MODE_IDLE); //b = fifoCreate(255); vs1002Reset(); DDRD |= _BV(DDD1); UCSR0B = _BV(TXEN0) | _BV(RXEN0); // | _BV(RXCIE0); UBRR0L = 9; /* SPI */ SPCR = _BV(SPE) | _BV(MSTR) | _BV(SPR1) | _BV(SPR0); //SPSR = _BV(SPI2X); // 0x8000 + 6144 = 0x9800, 12.288MHz XTAL + clk-doubling /* Clock */ vs1002cmd_s(); SPDR = 0x02; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x03; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x98; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x00; loop_until_bit_is_set(SPSR, SPIF); vs1002cmd_e(); /* Mode: TEST */ vs1002cmd_s(); SPDR = 0x02; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x00; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x08; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x20; // Test loop_until_bit_is_set(SPSR, SPIF); vs1002cmd_e(); /* Volume */ vs1002cmd_s(); SPDR = 0x02; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x0b; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x00; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x00; loop_until_bit_is_set(SPSR, SPIF); vs1002cmd_e(); /* Test 1.500kHz sine */ vs1002data_s(); SPDR = 0x53; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0xef; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x6e; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x28; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x00; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x00; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x00; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x00; loop_until_bit_is_set(SPSR, SPIF); vs1002data_e(); uint8_t t; for (t = 0; t < 125; t++) { _delay_ms(2); } /* End test */ vs1002data_s(); SPDR = 0x45; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x78; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x69; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x74; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x00; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x00; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x00; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x00; loop_until_bit_is_set(SPSR, SPIF); vs1002data_e(); /* Mode: PLAY */ vs1002cmd_s(); SPDR = 0x02; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x00; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x08; loop_until_bit_is_set(SPSR, SPIF); SPDR = 0x08; // STREAM loop_until_bit_is_set(SPSR, SPIF); vs1002cmd_e(); uint16_t i; uint8_t buffer[1000]; for (;;) { uartTx('.'); for (i = 0; i < 1000; i++) { loop_until_bit_is_set(UCSR0A, RXC0); buffer[i] = UDR0; } loop_until_bit_is_set(VS1002_DREQ_PORT, VS1002_DREQ_PIN); vs1002data_s(); for (i = 0; i < 1000; i++) { loop_until_bit_is_set(SPSR, SPIF); SPDR = buffer[i]; } vs1002data_e(); } }
/* entry point of application */ int main ( ) { /* first thing is to enable the watchdog - so if the app crashes, the watchdog will expire and the AS7000 will reset */ wdgEnable( ONE_SECOND ); /* count variable to see some movement on uart */ cnt = 0; /* now we want to use the UART to report */ ccuSelectUartPins( GPIO_NONE, UART_TX_PIN ); uartInitialise( ); uartOpen( UART_BAUD_RATE_FAST ); /* send a greeting via uart */ uartTxString( "AS7000 is alive\n" ); /* we need the timers to measure the real slow clock frequency */ tmrInitialise( ); slowClockFrequency = tmrSlowClockFrequency( 10 ); /* make sure you measure for less than 1 seconds, otherwise the watchdog will expire */ /* note: the real slow clock frequency is than used by the SW for further timer calculations - or sleep mode calculations */ /* print the measured frequency */ uartTxString( "SlowClockFrequency=" ); uartTxUint( slowClockFrequency ); uartTxString( "Hz\n" ); /* e.g. configure i2c as slave to be used */ ccuSelectI2CPins( 0 /* slave */, I2C_SCL_PIN, 0 /* SCL - no internal pull-up */, I2C_SDA_PIN, 0 /* SDA - no internal pull-up */ ); i2cSlaveInitialise( ); i2cSlaveOpen( I2C_SLAVE_ADDRESS ); /* drive a led through one of the gpio pins */ ccuSelectGPIOPin( LED_PIN, 1 /* digital */, CCU_NO_PULL ); gpioInitialise(); gpioConfigure( 0 /* no input */, LED_MASK /* led is output */ ); gpioWrite( LED_MASK, LED_OFF ); /* switch off */ /* enalbe interrupts in system */ __enable_irq( ); /* show how to use the delays */ delayInMicroseconds( 0 ); delayInMicroseconds( 1 ); delayInMicroseconds( 2 ); /* put the AS7000 to sleep always for 500 milliseconds */ ccuConfigureDeepSleep( 500, 0 /* no wake-up on gpio7 */, 0 /* no wake-up on gpio8 */ ); while ( 1 ) { wdgToggle( ); /* make sure the watchdog does not expire */ /* count up and report value on uart and blink led - to see something */ cnt++; if ( cnt & 1 ) { gpioWrite( LED_MASK, LED_ON ); } else { gpioWrite( LED_MASK, LED_OFF ); } uartTxUint( cnt ); uartTx( '\n' ); /* do some usefull stuff here */ /* not to be used in debugger: ccuEnterDeepSleep( ); <---- debugger does not see the CPU anymore and stops */ /* use instead: */ delayInMilliseconds( 500 ); } }
void uartTxArray(uint8_t uartId, uint8_t len, uint8_t *data) { uint8_t c; for (c = 0; c < len; c++) { uartTx(uartId, data[c]); } }
void uartNewLine(void) { uartTx('\r'); uartTx('\n'); }