u32 ISO7816_SendCharISR (u32 nChar_u32, u32 nTimeOutMs_u32) { int i; ToolPinSet (); for (i = 0; i < nTimeOutMs_u32; i++) { // Tx free ? if (TRUE == usart_tx_ready (EXAMPLE_INT_USART)) { // Tx transmitter buffer ? // if (TRUE == usart_txTransmitter_ready(EXAMPLE_INT_USART)) { // Yes, send char usart_write_char (EXAMPLE_INT_USART, nChar_u32); ToolPinClr (); return USART_SUCCESS; } } // Wait 1 ms vTaskDelay (CCID_TASK_DELAY_1_MS_IN_TICKS); } ToolPinClr (); return (USART_TX_BUSY); }
int usart_putchar(volatile avr32_usart_t *usart, int c) { int timeout = USART_DEFAULT_TIMEOUT; if (c == '\n') { do { if (!timeout--) return USART_FAILURE; } while (usart_write_char(usart, '\r') != USART_SUCCESS); timeout = USART_DEFAULT_TIMEOUT; } do { if (!timeout--) return USART_FAILURE; } while (usart_write_char(usart, c) != USART_SUCCESS); return USART_SUCCESS; }
int board_putchar(char c) { int timeout = USART_DEFAULT_TIMEOUT; if (c == '\n') { do { if (!timeout--) return USART_FAILURE; } while (usart_write_char(&CONFIG_CONSOLE_PORT, '\r') != USART_SUCCESS); timeout = USART_DEFAULT_TIMEOUT; } do { if (!timeout--) return USART_FAILURE; } while (usart_write_char(&CONFIG_CONSOLE_PORT, c) != USART_SUCCESS); return USART_SUCCESS; }
/*! \brief Send and Read one byte using SPI Interface. * \param c: Data to send to slave. * \return uint8_t data: Data read from slave. * \note Called from SPI_Send_Message in this file. */ uint8_t SPI_Send_Byte(uint8_t c) { uint8_t data; if (usart_write_char(&AVR32_USART2, c) == USART_SUCCESS) { data = usart_getchar(&AVR32_USART2); } else { return (uint8_t)USART_FAILURE; } return data; }
//---------------------------------------------------------------------------- //Routine für die Ausgabe eines Zeichens ueber definierte Devices // void console_write_char(char c) { if ( !console_isinit ) return; #if USE_USART if ( console_mode & CONSOLE_USART ) usart_write_char (c); #endif if ( console_mode & (CONSOLE_TELNET | CONSOLE_SYSLOG) ) { console_tx_buffer[console_tx_counter++] = c; if ( (console_tx_counter >= CONSOLE_TX_BUFFERSIZE - 1) || (c == '\n') ) { // Buffer ist voll oder Zeilenende, absenden console_flush(); } } }
/** * \ingroup usartcmdline * \b CAT-Befehl Anzeige eines Dateiinhalts auf der USART */ int16_t cat(char *outbuffer) { if (outbuffer) // nur bei USART return cmd_502(outbuffer); #if USE_MMC int ch; File *ptrFile = f16_open(argv,"r"); if (!ptrFile) return 0; ch = f16_getc(ptrFile); while ( ch > 0 ) { usart_write_char((char)ch); ch = f16_getc(ptrFile); } f16_close(ptrFile); #endif return 0; }
__interrupt #endif static void usart_int_handler(void) { int c; /* * In the code line below, the interrupt priority level does not need to * be explicitly masked as it is already because we are within the * interrupt handler. * The USART Rx interrupt flag is cleared by side effect when reading * the received character. * Waiting until the interrupt has actually been cleared is here useless * as the call to usart_write_char will take enough time for this before * the interrupt handler is left and the interrupt priority level is * unmasked by the CPU. */ usart_read_char(EXAMPLE_USART, &c); // Print the received character to USART. This is a simple echo. usart_write_char(EXAMPLE_USART, c); }
//------------------------------------------------------------------------------ void usart_write_P (const char *Buffer,...) { va_list ap; va_start (ap, Buffer); int format_flag; char str_buffer[10]; char str_null_buffer[10]; char move = 0; char Base = 0; int tmp = 0; char by; char *ptr; //Ausgabe der Zeichen for(;;) { by = pgm_read_byte(Buffer++); if(by==0) break; // end of format string if (by == '%') { by = pgm_read_byte(Buffer++); if (isdigit(by)>0) { str_null_buffer[0] = by; str_null_buffer[1] = '\0'; move = atoi(str_null_buffer); by = pgm_read_byte(Buffer++); } switch (by) { case 's': ptr = va_arg(ap,char *); while(*ptr) { usart_write_char(*ptr++); } break; case 'b': Base = 2; goto ConversionLoop; case 'c': //Int to char format_flag = va_arg(ap,int); usart_write_char (format_flag++); break; case 'i': Base = 10; goto ConversionLoop; case 'o': Base = 8; goto ConversionLoop; case 'x': Base = 16; //**************************** ConversionLoop: //**************************** itoa(va_arg(ap,int),str_buffer,Base); int b=0; while (str_buffer[b++] != 0){}; b--; if (b<move) { move -=b; for (tmp = 0;tmp<move;tmp++) { str_null_buffer[tmp] = '0'; } //tmp ++; str_null_buffer[tmp] = '\0'; strcat(str_null_buffer,str_buffer); strcpy(str_buffer,str_null_buffer); } usart_write_str (str_buffer); move =0; break; } } else {
/** * \brief Initialize ADC driver to read the board temperature sensor. * * Initializes the board's ADC driver module and configures the ADC channel * connected to the onboard NTC temperature sensor ready for conversions. */ static void init_adc(void) { // Assign and enable GPIO pin to the ADC function. gpio_enable_module_pin(ADC_TEMPERATURE_PIN, ADC_TEMPERATURE_FUNCTION); const adcifb_opt_t adcifb_opt = { .resolution = AVR32_ADCIFB_ACR_RES_12BIT, .shtim = 15, .ratio_clkadcifb_clkadc = 2, .startup = 3, .sleep_mode_enable = false }; // Enable and configure the ADCIFB module sysclk_enable_peripheral_clock(&AVR32_ADCIFB); adcifb_configure(&AVR32_ADCIFB, &adcifb_opt); // Configure the trigger (No trigger, only software trigger) adcifb_configure_trigger(&AVR32_ADCIFB, AVR32_ADCIFB_TRGMOD_NT, 0); // Enable the ADCIFB channel to NTC temperature sensor adcifb_channels_enable(&AVR32_ADCIFB, ADC_TEMPERATURE_CHANNEL); } /** * \brief Initializes the USART. * * Initializes the board USART ready for serial data to be transmitted and * received. */ static void init_usart(void) { const usart_options_t usart_options = { .baudrate = 57600, .charlength = 8, .paritytype = USART_NO_PARITY, .stopbits = USART_1_STOPBIT, .channelmode = USART_NORMAL_CHMODE }; // Initialize USART in RS232 mode with the requested settings. sysclk_enable_peripheral_clock(USART); usart_init_rs232(USART, &usart_options, sysclk_get_pba_hz()); } /** * \brief Initializes the PWM subsystem ready to generate the RGB LED PWM * waves. * * Initializes the on-chip PWM module and configures the RGB LED PWM outputs so * the the brightness of the three individual channels can be adjusted. */ static void init_pwm(void) { // GPIO pin/function map for the RGB LEDs. gpio_enable_module_pin(LED_RED_PWMA, LED_PWMA_FUNCTION); gpio_enable_module_pin(LED_GREEN_PWMA, LED_PWMA_FUNCTION); gpio_enable_module_pin(LED_BLUE_PWMA, LED_PWMA_FUNCTION); const scif_gclk_opt_t genclk3_opt = { .clock_source = SCIF_GCCTRL_CPUCLOCK, .divider = 8, .diven = true, }; // Start generic clock 3 for the PWM outputs. scif_start_gclk(AVR32_PM_GCLK_GCLK3, &genclk3_opt); // Enable RGB LED PWM. sysclk_enable_peripheral_clock(&AVR32_PWMA); pwma_config_enable(&AVR32_PWMA,EXAMPLE_PWMA_FREQUENCY,EXAMPLE_PWMA_GCLK_FREQUENCY,0); pwma_set_channels_value(&AVR32_PWMA,PWM_CHANNEL_RED | PWM_CHANNEL_BLUE| PWM_CHANNEL_GREEN,255); } /** * \brief Application main loop. */ int main(void) { board_init(); sysclk_init(); sysclk_enable_peripheral_clock(USART); // Initialize touch, ADC, USART and PWM init_adc(); init_usart(); init_pwm(); init_touch(); while (true) { uint32_t adc_data; // Read slider and button and update RGB led touch_handler(); // Wait until the ADC is ready to perform a conversion. do { } while (!adcifb_is_ready(&AVR32_ADCIFB)); // Start an ADCIFB conversion sequence. adcifb_start_conversion_sequence(&AVR32_ADCIFB); // Wait until the converted data is available. do { } while (!adcifb_is_drdy(&AVR32_ADCIFB)); // Get the last converted data. adc_data = (adcifb_get_last_data(&AVR32_ADCIFB) & 0x3FF); // Write temperature data to USART do { } while (!usart_tx_empty(USART)); usart_write_char(USART, (adc_data >> 8)); do { } while (!usart_tx_empty(USART)); usart_write_char(USART, (adc_data & 0xFF)); } }
void USART_Int_Test (void) { int i; static const gpio_map_t USART_GPIO_MAP = { {EXAMPLE_INT_USART_RX_PIN, EXAMPLE_INT_USART_RX_FUNCTION}, {EXAMPLE_INT_USART_TX_PIN, EXAMPLE_INT_USART_TX_FUNCTION} }; // USART options. static const usart_options_t USART_OPTIONS = { .baudrate = EXAMPLE_INT_USART_BAUDRATE, .charlength = 8, .paritytype = USART_NO_PARITY, .stopbits = USART_1_STOPBIT, .channelmode = USART_NORMAL_CHMODE }; pcl_switch_to_osc (PCL_OSC0, FOSC0, OSC0_STARTUP); // Assign GPIO to USART. gpio_enable_module (USART_GPIO_MAP, sizeof (USART_GPIO_MAP) / sizeof (USART_GPIO_MAP[0])); // Initialize USART in RS232 mode. usart_init_rs232 (EXAMPLE_INT_USART, &USART_OPTIONS, FOSC0); // EXAMPLE_TARGET_PBACLK_FREQ_HZ); // Disable all interrupts. Disable_global_interrupt (); // Initialize interrupt vectors. INTC_init_interrupts (); // Register the USART interrupt handler to the interrupt controller. // usart_int_handler is the interrupt handler to register. // EXAMPLE_USART_IRQ is the IRQ of the interrupt handler to register. // AVR32_INTC_INT0 is the interrupt priority level to assign to the group of // this IRQ. // void INTC_register_interrupt(__int_handler handler, unsigned int irq, unsigned int int_level); INTC_register_interrupt (&ISO7816_Usart_ISR, EXAMPLE_INT_USART_IRQ, AVR32_INTC_INT0); // Enable USART Rx interrupt. // EXAMPLE_INT_USART->ier = AVR32_USART_IER_RXRDY_MASK; EXAMPLE_INT_USART->IER.rxrdy = 1; // EXAMPLE_INT_USART->IER.txempty = 1; // // Enable all interrupts. Enable_global_interrupt (); // We have nothing left to do in the main, so we may switch to a device sleep // mode: we just need to be sure that the USART module will be still be active // in the chosen sleep mode. The sleep mode to use is the FROZEN sleep mode: // in this mode the PB clocks are still active (so the USART module which is // on the Peripheral Bus will still be active while the CPU and HSB will be // stopped). // -- // Modules communicating with external circuits should normally be disabled // before entering a sleep mode that will stop the module operation: this is not // the case for the FROZEN sleep mode. // -- // When the USART interrupt occurs, this will wake the CPU up which will then // execute the interrupt handler code then come back to the while(1) loop below // to execute the sleep instruction again. while (1) { for (i = 0; i < 100000; i++); usart_write_char (EXAMPLE_INT_USART, '*'); EXAMPLE_INT_USART->IER.txempty = 1; /**/ // If there is a chance that any PB write operations are incomplete, the CPU // should perform a read operation from any register on the PB bus before // executing the sleep instruction. // AVR32_INTC.ipr[0]; // Dummy read // Go to FROZEN sleep mode. // SLEEP(AVR32_PM_SMODE_FROZEN); // When the device wakes up due to an interrupt, once the interrupt is serviced, // go back into FROZEN sleep mode. } }