extern void init_spi (void) { sysclk_enable_pba_module(SYSCLK_SPI); static const gpio_map_t SPI_GPIO_MAP = { {SPI_SCK_PIN, SPI_SCK_FUNCTION }, {SPI_MISO_PIN, SPI_MISO_FUNCTION}, {SPI_MOSI_PIN, SPI_MOSI_FUNCTION}, {SPI_NPCS0_PIN, SPI_NPCS0_FUNCTION }, {SPI_NPCS1_PIN, SPI_NPCS1_FUNCTION }, }; // Assign GPIO to SPI. gpio_enable_module(SPI_GPIO_MAP, sizeof(SPI_GPIO_MAP) / sizeof(SPI_GPIO_MAP[0])); spi_options_t spiOptions = { .reg = DAC_SPI, .baudrate = 4000000, .bits = 8, .trans_delay = 0, .spck_delay = 0, .stay_act = 1, .spi_mode = 1, .modfdis = 1 }; // Initialize as master. spi_initMaster(SPI, &spiOptions); // Set SPI selection mode: variable_ps, pcs_decode, delay. spi_selectionMode(SPI, 0, 0, 0); // Enable SPI module. spi_enable(SPI); // spi_setupChipReg( SPI, &spiOptions, FPBA_HZ ); spi_setupChipReg(SPI, &spiOptions, sysclk_get_pba_hz() ); // add ADC chip register spiOptions.reg = ADC_SPI; spiOptions.baudrate = 20000000; spiOptions.bits = 16; spiOptions.spi_mode = 2; spiOptions.spck_delay = 0; spiOptions.trans_delay = 5; spiOptions.stay_act = 0; spiOptions.modfdis = 0; spi_setupChipReg( SPI, &spiOptions, FPBA_HZ ); // spi_enable(SPI); }
/** Initialization ************************************************************/ static void qt60168_resources_init(U32 fpba_hz) { static const gpio_map_t QT60168_SPI_GPIO_MAP = { {QT60168_SPI_SCK_PIN, QT60168_SPI_SCK_FUNCTION }, // SPI Clock. {QT60168_SPI_MISO_PIN, QT60168_SPI_MISO_FUNCTION }, // MISO. {QT60168_SPI_MOSI_PIN, QT60168_SPI_MOSI_FUNCTION }, // MOSI. {QT60168_SPI_NPCS0_PIN, QT60168_SPI_NPCS0_FUNCTION} // Chip Select NPCS. }; // SPI options. spi_options_t spiOptions = { .reg = QT60168_SPI_NCPS, .baudrate = 1000000, // Defined in conf_qt60168.h. .bits = 8, // Defined in conf_qt60168.h. .spck_delay = 0, .trans_delay = 0, .stay_act = 0, .spi_mode = 3, .modfdis = 1 }; // Assign I/Os to SPI. gpio_enable_module(QT60168_SPI_GPIO_MAP, sizeof(QT60168_SPI_GPIO_MAP) / sizeof(QT60168_SPI_GPIO_MAP[0])); // Initialize as master. spi_initMaster(QT60168_SPI, &spiOptions); // Set selection mode: variable_ps, pcs_decode, delay. spi_selectionMode(QT60168_SPI, 0, 0, 0); // Enable SPI. spi_enable(QT60168_SPI); // Initialize QT60168 with SPI clock Osc0. spi_setupChipReg(QT60168_SPI, &spiOptions, 2*FOSC0); } void controller_init(U32 fcpu_hz, U32 fhsb_hz, U32 fpbb_hz, U32 fpba_hz) { qt60168_resources_init(fpba_hz); // Initialize QT60168 component. qt60168_init(fpba_hz); // Init timer to get key value. rtc_init_qt(); // Invalidate the timeout already cpu_set_timeout(0, &cpu_time_clear_wheel); }
/*! \brief Initializes QT60168 resources: GPIO and SPI */ static void ui_buttons_enable(void) { static const gpio_map_t QT60168_SPI_GPIO_MAP = { {QT60168_SPI_SCK_PIN, QT60168_SPI_SCK_FUNCTION}, {QT60168_SPI_MISO_PIN, QT60168_SPI_MISO_FUNCTION}, {QT60168_SPI_MOSI_PIN, QT60168_SPI_MOSI_FUNCTION}, {QT60168_SPI_NPCS0_PIN, QT60168_SPI_NPCS0_FUNCTION} }; /* SPI options. */ spi_options_t spiOptions = { .reg = QT60168_SPI_NCPS, .baudrate = QT60168_SPI_MASTER_SPEED, .bits = QT60168_SPI_BITS, .spck_delay = 0, .trans_delay = 0, .stay_act = 0, .spi_mode = 3, .modfdis = 1 }; /* Assign I/Os to SPI. */ gpio_enable_module(QT60168_SPI_GPIO_MAP, sizeof(QT60168_SPI_GPIO_MAP) / sizeof(QT60168_SPI_GPIO_MAP[0])); /* Initialize as master */ spi_initMaster(QT60168_SPI, &spiOptions); /* Set selection mode: variable_ps, pcs_decode, delay */ spi_selectionMode(QT60168_SPI, 0, 0, 0); /* Enable SPI */ spi_enable(QT60168_SPI); /* Initialize QT60168 with SPI clock Osc0. */ spi_setupChipReg(QT60168_SPI, &spiOptions, sysclk_get_cpu_hz()); qt60168_init(sysclk_get_cpu_hz()); } ISR(button_rtc_irq, BUTTON_RTC_IRQ_GROUP, BUTTON_RTC_IRQ_PRIORITY) { ui_buttons_read(); /* clear the interrupt flag */ rtc_clear_interrupt(&AVR32_RTC); }
static void init_spi(void) { #if defined(WL_SPI) int i; #endif #if defined(AT45DBX_SPI) static const gpio_map_t AT45DBX_SPI_GPIO_MAP = { { AT45DBX_SPI_SCK_PIN, AT45DBX_SPI_SCK_FUNCTION }, { AT45DBX_SPI_MISO_PIN, AT45DBX_SPI_MISO_FUNCTION }, { AT45DBX_SPI_MOSI_PIN, AT45DBX_SPI_MOSI_FUNCTION }, { AT45DBX_SPI_NPCS2_PIN, AT45DBX_SPI_NPCS2_FUNCTION }, }; #endif #if defined(WL_SPI) const gpio_map_t WL_SPI_GPIO_MAP = { #if defined(WL_SPI_NPCS0) WL_SPI_NPCS0, #endif WL_SPI_NPCS, WL_SPI_MISO, WL_SPI_MOSI, WL_SPI_SCK }; #endif #if defined(WL_SPI) || defined(AT45DBX_SPI) spi_options_t spiOptions = { .modfdis = 1 /* only param used by spi_initMaster() */ }; #endif #if defined(AT45DBX_SPI) gpio_enable_module(AT45DBX_SPI_GPIO_MAP, sizeof(AT45DBX_SPI_GPIO_MAP) / sizeof(AT45DBX_SPI_GPIO_MAP[0])); spi_initMaster(AT45DBX_SPI, &spiOptions); spi_selectionMode(AT45DBX_SPI, 0, 0, 0); #endif #if defined(WL_SPI) /* same pins might be initialized twice here */ gpio_enable_module(WL_SPI_GPIO_MAP, sizeof(WL_SPI_GPIO_MAP) / sizeof(WL_SPI_GPIO_MAP[0])); for (i = 0; i < sizeof(WL_SPI_GPIO_MAP)/sizeof(WL_SPI_GPIO_MAP[0]); i++) gpio_enable_pin_pull_up(WL_SPI_GPIO_MAP[i].pin); /* same SPI controller might be initialized again */ spi_initMaster(&WL_SPI, &spiOptions); spi_selectionMode(&WL_SPI, 0, 0, 0); #endif #if defined(AT45DBX_SPI) spi_enable(AT45DBX_SPI); /* put up flash reset pin */ gpio_set_gpio_pin(AT45DBX_CHIP_RESET); #endif #if defined(WL_SPI) spi_enable(&WL_SPI); #endif } static void init_rs232(void) { #ifndef NO_SERIAL #if defined(BOARD_RS232_0) const gpio_map_t BOARD_RS232_0_GPIO_MAP = { BOARD_RS232_0_TX, BOARD_RS232_0_RX, #if defined(BOARD_RS232_0_RTS) && defined (BOARD_RS232_0_CTS) BOARD_RS232_0_RTS, BOARD_RS232_0_CTS #endif }; #endif #if defined(BOARD_RS232_1) const gpio_map_t BOARD_RS232_1_GPIO_MAP = { BOARD_RS232_1_TX, BOARD_RS232_1_RX #if defined(BOARD_RS232_1_RTS) && defined (BOARD_RS232_1_CTS) BOARD_RS232_1_RTS, BOARD_RS232_1_CTS #endif }; #endif #if defined(BOARD_RS232_0) gpio_enable_module(BOARD_RS232_0_GPIO_MAP, sizeof(BOARD_RS232_0_GPIO_MAP) / sizeof(BOARD_RS232_0_GPIO_MAP[0])); #endif #if defined(BOARD_RS232_1) gpio_enable_module(BOARD_RS232_1_GPIO_MAP, sizeof(BOARD_RS232_1_GPIO_MAP) / sizeof(BOARD_RS232_1_GPIO_MAP[0])); #endif #endif /* NO_SERIAL */ } static void init_printk(void) { #ifndef NO_SERIAL #if defined(CONFIG_CONSOLE_PORT) const usart_options_t usart_options = { .baudrate = 57600, .charlength = 8, .paritytype = USART_NO_PARITY, .stopbits = USART_1_STOPBIT, .channelmode = USART_NORMAL_CHMODE }; usart_init_rs232(&CONFIG_CONSOLE_PORT, &usart_options, FPBA_HZ); #endif #endif /* NO_SERIAL */ } void board_init(void) { init_exceptions(); init_hmatrix(); init_sys_clocks(); init_interrupts(); init_rs232(); init_printk(); #ifdef WITH_SDRAM sdramc_init(FHSB_HZ); #endif init_spi(); }
/*! \brief Initializes QT60168 resources: GPIO and SPI */ static void qt60168_resources_init(int cpu_hz) { static const gpio_map_t QT60168_SPI_GPIO_MAP = { {QT60168_SPI_SCK_PIN, QT60168_SPI_SCK_FUNCTION }, // SPI Clock. {QT60168_SPI_MISO_PIN, QT60168_SPI_MISO_FUNCTION }, // MISO. {QT60168_SPI_MOSI_PIN, QT60168_SPI_MOSI_FUNCTION }, // MOSI. {QT60168_SPI_NPCS0_PIN, QT60168_SPI_NPCS0_FUNCTION} // Chip Select NPCS. }; // SPI options. spi_options_t spiOptions = { .reg = QT60168_SPI_NCPS, .baudrate = QT60168_SPI_MASTER_SPEED, // Defined in conf_qt60168.h. .bits = QT60168_SPI_BITS, // Defined in conf_qt60168.h. .spck_delay = 0, .trans_delay = 0, .stay_act = 0, .spi_mode = 3, .modfdis = 1 }; // Assign I/Os to SPI. gpio_enable_module(QT60168_SPI_GPIO_MAP, sizeof(QT60168_SPI_GPIO_MAP) / sizeof(QT60168_SPI_GPIO_MAP[0])); // Initialize as master. spi_initMaster(QT60168_SPI, &spiOptions); // Set selection mode: variable_ps, pcs_decode, delay. spi_selectionMode(QT60168_SPI, 0, 0, 0); // Enable SPI. spi_enable(QT60168_SPI); // Initialize QT60168 with SPI clock Osc0. spi_setupChipReg(QT60168_SPI, &spiOptions, cpu_hz); } bool controller_key_pressed(void) { if (controller_state == STATE_IDLE) return false; return true; } bool controller_key_released(void) { if (controller_state & STATE_BACK_RELEASED || controller_state & STATE_FCT1_RELEASED || controller_state & STATE_FCT2_RELEASED || controller_state & STATE_FCT3_RELEASED) return true; return false; } bool controller_wheel_pressed(void) { if (controller_state & STATE_WHEEL_LEFT || controller_state & STATE_WHEEL_RIGHT) return true; return false; } bool controller_key_back(void) { if (controller_state & STATE_BACK_RELEASED) { CLEAR_RELEASED_STATE(BACK); return true; } return false; } bool controller_key_reset(void) { if (controller_state & STATE_BACK_LONG_PRESS) { controller_state &= ~STATE_BACK_LONG_PRESS; return true; } return false; } bool controller_key_fct1(void) { if (controller_state & STATE_FCT1_RELEASED) { CLEAR_RELEASED_STATE(FCT1); return true; } return false; } bool controller_key_fct2(void) { if (controller_state & STATE_FCT2_RELEASED) { CLEAR_RELEASED_STATE(FCT2); return true; } return false; } bool controller_key_fct3(void) { if (controller_state & STATE_FCT3_RELEASED) { CLEAR_RELEASED_STATE(FCT3); return true; } return false; } bool controller_key_fct1_pressed(void) { if (controller_state & STATE_FCT1_PRESSED) return true; return false; } bool controller_key_fct2_pressed(void) { if (controller_state & STATE_FCT2_PRESSED) return true; return false; } bool controller_key_fct3_pressed(void) { if (controller_state & STATE_FCT3_PRESSED) return true; return false; } bool controller_wheel_right(int wheel_inc) { if (wheel_step_counter >= wheel_inc && controller_state & STATE_WHEEL_RIGHT) { wheel_step_counter -= wheel_inc; return true; } return false; } bool controller_wheel_left(int wheel_inc) { if (wheel_step_counter >= wheel_inc && controller_state & STATE_WHEEL_LEFT) { wheel_step_counter -= wheel_inc; return true; } return false; } void controller_reset(void) { controller_state = STATE_IDLE; wheel_step_counter = 0; }
static void tc_init_fast(volatile avr32_tc_t *tc) { // Options for waveform generation. static const tc_waveform_opt_t waveform_opt_1 = { .channel = FAST_TC_CHANNEL, // Channel selection. .bswtrg = TC_EVT_EFFECT_NOOP, // Software trigger effect on TIOB. .beevt = TC_EVT_EFFECT_NOOP, // External event effect on TIOB. .bcpc = TC_EVT_EFFECT_NOOP, // RC compare effect on TIOB. .bcpb = TC_EVT_EFFECT_NOOP, // RB compare effect on TIOB. .aswtrg = TC_EVT_EFFECT_NOOP, // Software trigger effect on TIOA. .aeevt = TC_EVT_EFFECT_NOOP, // External event effect on TIOA. .acpc = TC_EVT_EFFECT_NOOP, // RC compare effect on TIOA. .acpa = TC_EVT_EFFECT_NOOP, //RA compare effect on TIOA. .wavsel = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER, //Waveform selection: Up mode with automatic trigger(reset) .enetrg = false, //// External event trigger enable. .eevt = 0, //// External event selection. .eevtedg = TC_SEL_NO_EDGE, //// External event edge selection. .cpcdis = false, // Counter disable when RC compare. .cpcstop = false, // Counter clock stopped with RC compare. .burst = false, // Burst signal selection .clki = false, // Clock inversion. .tcclks = TC_CLOCK_SOURCE_TC3 // Internal source clock 3, connected to fPBA / 8. }; // Options for enabling TC interrupts static const tc_interrupt_t tc_interrupt = { .etrgs = 0, .ldrbs = 0, .ldras = 0, .cpcs = 1, // Enable interrupt on RC compare alone .cpbs = 0, .cpas = 0, .lovrs = 0, .covfs = 0 }; // Initialize the timer/counter. tc_init_waveform(tc, &waveform_opt_1); tc_write_rc(tc, FAST_TC_CHANNEL, 10); // configure the timer interrupt tc_configure_interrupts(tc, FAST_TC_CHANNEL, &tc_interrupt); } static void tc_init_slow(volatile avr32_tc_t *tc) { // Options for waveform generation. static const tc_waveform_opt_t waveform_opt_2 = { .channel = SLOW_TC_fast_CHANNEL, // Channel selection. .bswtrg = TC_EVT_EFFECT_NOOP, // Software trigger effect on TIOB. .beevt = TC_EVT_EFFECT_NOOP, // External event effect on TIOB. .bcpc = TC_EVT_EFFECT_NOOP, // RC compare effect on TIOB. .bcpb = TC_EVT_EFFECT_NOOP, // RB compare effect on TIOB. .aswtrg = TC_EVT_EFFECT_NOOP, // Software trigger effect on TIOA. .aeevt = TC_EVT_EFFECT_NOOP, // External event effect on TIOA. .acpc = TC_EVT_EFFECT_CLEAR, // RC compare effect on TIOA. .acpa = TC_EVT_EFFECT_SET, // RA compare effect on TIOA. .wavsel = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER, //Waveform selection: Up mode with automatic trigger(reset) .enetrg = false, // External event trigger enable. .eevt = 0, // External event selection. .eevtedg = TC_SEL_NO_EDGE, // External event edge selection. .cpcdis = false, // Counter disable when RC compare. .cpcstop = false, // Counter clock stopped with RC compare. .burst = false, // Burst signal selection. .clki = false, // Clock inversion. .tcclks = TC_CLOCK_SOURCE_TC3 // Internal source clock 3, connected to fPBA / 8. }; // Initialize the timer/counter. tc_init_waveform(tc, &waveform_opt_2); tc_write_rc(tc, SLOW_TC_fast_CHANNEL, 7500); //counter will count milliseconds tc_write_ra(tc, SLOW_TC_fast_CHANNEL, 3500); //configure ra so that TIOA0 is toggled static const tc_waveform_opt_t waveform_opt_3 = { .channel = SLOW_TC_slow_CHANNEL, // Channel selection. .bswtrg = TC_EVT_EFFECT_NOOP, // Software trigger effect on TIOB. .beevt = TC_EVT_EFFECT_NOOP, // External event effect on TIOB. .bcpc = TC_EVT_EFFECT_NOOP, // RC compare effect on TIOB. .bcpb = TC_EVT_EFFECT_NOOP, // RB compare effect on TIOB. .aswtrg = TC_EVT_EFFECT_NOOP, // Software trigger effect on TIOA. .aeevt = TC_EVT_EFFECT_NOOP, // External event effect on TIOA. .acpc = TC_EVT_EFFECT_NOOP, // RC compare effect on TIOA. .acpa = TC_EVT_EFFECT_NOOP, //RA compare effect on TIOA. .wavsel = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER, //Waveform selection: Up mode with automatic trigger(reset) .enetrg = false, //// External event trigger enable. .eevt = 0, //// External event selection. .eevtedg = TC_SEL_NO_EDGE, //// External event edge selection. .cpcdis = false, // Counter disable when RC compare. .cpcstop = false, // Counter clock stopped with RC compare. .burst = false, // Burst signal selection. .clki = false, // Clock inversion. .tcclks = TC_CLOCK_SOURCE_XC0 // Use XC1 as clock source. Must configure TIOA0 to be XC1 }; tc_init_waveform(tc, &waveform_opt_3); tc_write_rc(tc, SLOW_TC_slow_CHANNEL, 100); // tc_select_external_clock(tc,SLOW_TC_slow_CHANNEL,AVR32_TC_BMR_TC0XC0S_TIOA1); //use TIOA1 as XC0 } static void configure_hmatrix(uint32_t mode) { // Configure all Slave in Last Default Master #if (defined AVR32_HMATRIX) for(uint32_t i = 0; i < AVR32_HMATRIX_SLAVE_NUM; i++) { AVR32_HMATRIX.SCFG[i].defmstr_type = mode; } #endif #if (defined AVR32_HMATRIXB) for(uint32_t i = 0;i < AVR32_HMATRIXB_SLAVE_NUM; i++) { AVR32_HMATRIXB.SCFG[i].defmstr_type = mode; } #endif } void board_init(void) { /* This function is meant to contain board-specific initialization code * for, e.g., the I/O pins. The initialization can rely on application- * specific board configuration, found in conf_board.h. */ gpio_local_init(); static pcl_freq_param_t pcl_freq_param = { .cpu_f = CPU_SPEED, .pba_f = PBA_SPEED, .osc0_f = FOSC0, .osc0_startup = OSC0_STARTUP }; if (pcl_configure_clocks(&pcl_freq_param) != PASS) while (true); configure_hmatrix(AVR32_HMATRIXB_DEFMSTR_TYPE_NO_DEFAULT); AVR32_LowLevelInit(); //configure all GPIO gpio_local_enable_pin_output_driver(ADC_CONV_pin); gpio_local_clr_gpio_pin(ADC_CONV_pin); gpio_local_enable_pin_output_driver(DDS_IOUD_pin); gpio_local_clr_gpio_pin(DDS_IOUD_pin); gpio_local_enable_pin_output_driver(DDS_RESET_pin); gpio_local_clr_gpio_pin(DDS_RESET_pin); gpio_local_enable_pin_output_driver(DDS_PDN_pin); gpio_local_set_gpio_pin(DDS_PDN_pin); gpio_local_enable_pin_output_driver(DDS_P0_pin); gpio_local_clr_gpio_pin(DDS_P0_pin); gpio_local_enable_pin_output_driver(DDS_P1_pin); gpio_local_clr_gpio_pin(DDS_P1_pin); gpio_local_enable_pin_output_driver(DDS_P2_pin); gpio_local_clr_gpio_pin(DDS_P2_pin); gpio_local_enable_pin_output_driver(DDS_P3_pin); gpio_local_clr_gpio_pin(DDS_P3_pin); gpio_local_enable_pin_output_driver(RXSW_pin); gpio_local_clr_gpio_pin(RXSW_pin); gpio_local_enable_pin_output_driver(TXSW_pin); gpio_local_clr_gpio_pin(TXSW_pin); gpio_local_enable_pin_output_driver(TPAbias_pin); gpio_local_clr_gpio_pin(TPAbias_pin); gpio_local_enable_pin_output_driver(GEN1_pin); gpio_local_clr_gpio_pin(GEN1_pin); gpio_local_enable_pin_output_driver(GEN2_pin); gpio_local_clr_gpio_pin(GEN2_pin); gpio_local_enable_pin_output_driver(PWM0_pin); gpio_local_clr_gpio_pin(PWM0_pin); gpio_local_disable_pin_output_driver(SD_detect_pin); //configure all peripheral IO static const gpio_map_t GCLK_GPIO_MAP = { {AVR32_SCIF_GCLK_0_1_PIN, AVR32_SCIF_GCLK_0_1_FUNCTION} }; gpio_enable_module(GCLK_GPIO_MAP, sizeof(GCLK_GPIO_MAP) / sizeof(GCLK_GPIO_MAP[0])); genclk_enable_config(9, GENCLK_SRC_CLK_CPU, 2); static const gpio_map_t SPI_GPIO_MAP = { {SPI1_SCK_PIN, SPI1_SCK_FUNCTION}, {SPI1_MOSI_PIN, SPI1_MOSI_FUNCTION}, {SPI1_MISO_PIN, SPI1_MISO_FUNCTION}, {SPI1_NPCS2_PIN, SPI1_NPCS2_FUNCTION} }; gpio_enable_module(SPI_GPIO_MAP, sizeof(SPI_GPIO_MAP) / sizeof(SPI_GPIO_MAP[0])); spi_options_t SPI1_OPTIONS_0 = { .reg = 0, //! The SPI channel to set up. .baudrate = 30000000, //! Preferred baudrate for the SPI. .bits =16, //! Number of bits in each character (8 to 16). .spck_delay =0, //! Delay before first clock pulse after selecting slave (in PBA clock periods). .trans_delay =0, //! Delay between each transfer/character (in PBA clock periods). .stay_act =0, //! Sets this chip to stay active after last transfer to it. .spi_mode =1, //! Which SPI mode to use when transmitting. .modfdis =1 //! Disables the mode fault detection. }; spi_options_t SPI1_OPTIONS_3 = { .reg = 3, //! The SPI channel to set up. .baudrate = 30000000, //! Preferred baudrate for the SPI. .bits =8, //! Number of bits in each character (8 to 16). .spck_delay =0, //! Delay before first clock pulse after selecting slave (in PBA clock periods). .trans_delay =0, //! Delay between each transfer/character (in PBA clock periods). .stay_act =1, //! Sets this chip to stay active after last transfer to it. .spi_mode =0, //! Which SPI mode to use when transmitting. .modfdis =1 //! Disables the mode fault detection. }; spi_initMaster(SPI1, &SPI1_OPTIONS_0); spi_selectionMode(SPI1,1,0,0); spi_enable(SPI1); spi_setupChipReg(SPI1,&SPI1_OPTIONS_0,PBA_SPEED); spi_setupChipReg(SPI1,&SPI1_OPTIONS_3,PBA_SPEED); spi_selectChip(SPI1, 3); static const gpio_map_t USB_USART_GPIO_MAP = { {USB_USART_RX_PIN, USB_USART_RX_FUNCTION}, {USB_USART_TX_PIN, USB_USART_TX_FUNCTION}, {USB_USART_RTS_PIN, USB_USART_RTS_FUNCTION}, {USB_USART_CTS_PIN, USB_USART_CTS_FUNCTION} }; gpio_enable_module(USB_USART_GPIO_MAP, sizeof(USB_USART_GPIO_MAP) / sizeof(USB_USART_GPIO_MAP[0])); static const usart_options_t USB_USART_OPTIONS = { .baudrate = 3000000, .charlength = 8, .paritytype = USART_NO_PARITY, .stopbits = USART_1_STOPBIT, .channelmode = USART_NORMAL_CHMODE }; usart_init_hw_handshaking(USB_USART, &USB_USART_OPTIONS, PBA_SPEED); static const gpio_map_t LCD_USART_GPIO_MAP = { {LCD_USART_RX_PIN, LCD_USART_RX_FUNCTION}, {LCD_USART_TX_PIN, LCD_USART_TX_FUNCTION} }; gpio_enable_module(LCD_USART_GPIO_MAP, sizeof(LCD_USART_GPIO_MAP) / sizeof(LCD_USART_GPIO_MAP[0])); static const usart_options_t LCD_USART_OPTIONS = { .baudrate = 115200, .charlength = 8, .paritytype = USART_NO_PARITY, .stopbits = USART_1_STOPBIT, .channelmode = USART_NORMAL_CHMODE }; usart_init_rs232(LCD_USART, &LCD_USART_OPTIONS, PBA_SPEED); LCD_USART->cr|=AVR32_USART_CR_STTTO_MASK; //set timeout to stop until new character is received LCD_USART->rtor=230; //set to timeout in 2ms my_pdca_init_channel(LCD_USART_RX_PDCA_CHANNEL, (uint32_t)(&LCD_USART_buffer),(uint32_t)(sizeof(LCD_USART_buffer)),LCD_USART_RX_PDCA_PID,0,0, PDCA_TRANSFER_SIZE_BYTE); pdca_disable(LCD_USART_RX_PDCA_CHANNEL); my_pdca_init_channel(USB_USART_RX_PDCA_CHANNEL, (uint32_t)(&host_USART_buffer),(uint32_t)(sizeof(host_USART_buffer)),USB_USART_RX_PDCA_PID,0,0, PDCA_TRANSFER_SIZE_BYTE); pdca_disable(USB_USART_RX_PDCA_CHANNEL); USB_USART->cr|=AVR32_USART_CR_STTTO_MASK; //set timeout to stop until new character is received USB_USART->rtor=15000; //set to timeout in 1ms // GPIO pins used for SD/MMC interface static const gpio_map_t SD_MMC_SPI_GPIO_MAP = { {SPI0_SCK_PIN, SPI0_SCK_FUNCTION }, // SPI Clock. {SPI0_MISO_PIN, SPI0_MISO_FUNCTION}, // MISO. {SPI0_MOSI_PIN, SPI0_MOSI_FUNCTION}, // MOSI. {SPI0_NPCS0_PIN, SPI0_NPCS0_FUNCTION} // Chip Select NPCS. }; //SPI options. spi_options_t SD_spiOptions = { .reg = SD_MMC_SPI_NPCS, .baudrate = SD_SPI_SPEED, // Defined in conf_sd_mmc_spi.h. .bits = 8, // Defined in conf_sd_mmc_spi.h. .spck_delay = 0, .trans_delay = 0, .stay_act = 1, .spi_mode = 0, .modfdis = 1 }; // Assign I/Os to SPI. gpio_enable_module(SD_MMC_SPI_GPIO_MAP,sizeof(SD_MMC_SPI_GPIO_MAP) / sizeof(SD_MMC_SPI_GPIO_MAP[0])); // Initialize as master. spi_initMaster(SPI0, &SD_spiOptions); // Set SPI selection mode: variable_ps, pcs_decode, delay. spi_selectionMode(SPI0, 0, 0, 0); // Enable SPI module. spi_enable(SPI0); // Initialize SD/MMC driver with SPI clock (PBA). sd_mmc_spi_init(SD_spiOptions, PBA_SPEED); tc_init_fast(FAST_TC); tc_init_slow(SLOW_TC); static const gpio_map_t DACIFB_GPIO_MAP = { {AVR32_DACREF_PIN,AVR32_DACREF_FUNCTION}, {AVR32_ADCREFP_PIN,AVR32_ADCREFP_FUNCTION}, {AVR32_ADCREFN_PIN,AVR32_ADCREFN_FUNCTION}, {DAC0A_pin, DAC0A_FUNCTION}, {DAC1A_pin, DAC1A_FUNCTION} }; gpio_enable_module(DACIFB_GPIO_MAP, sizeof(DACIFB_GPIO_MAP) / sizeof(DACIFB_GPIO_MAP[0])); dacifb_opt_t dacifb_opt = { .reference = DACIFB_REFERENCE_EXT , // VDDANA Reference .channel_selection = DACIFB_CHANNEL_SELECTION_A, // Selection Channels A&B .low_power = false, // Low Power Mode .dual = false, // Dual Mode .prescaler_clock_hz = DAC_PRESCALER_CLK // Prescaler Clock (Should be 500Khz) }; // DAC Channel Configuration dacifb_channel_opt_t dacifb_channel_opt = { .auto_refresh_mode = true, // Auto Refresh Mode .trigger_mode = DACIFB_TRIGGER_MODE_MANUAL, // Trigger selection .left_adjustment = false, // Right Adjustment .data_shift = 0, // Number of Data Shift .data_round_enable = false // Data Rouding Mode }; }; volatile avr32_dacifb_t *dacifb0 = &AVR32_DACIFB0; // DACIFB IP registers address volatile avr32_dacifb_t *dacifb1 = &AVR32_DACIFB1; // DACIFB IP registers address //The factory calibration for DADIFB is broken, so use manual calibration //dacifb_get_calibration_data(DAC0,&dacifb_opt,0); dacifb_opt.gain_calibration_value=0x0090; dacifb_opt.offset_calibration_value=0x0153; // configure DACIFB0 dacifb_configure(DAC0,&dacifb_opt,PBA_SPEED); dacifb_configure_channel(DAC0,DACIFB_CHANNEL_SELECTION_A,&dacifb_channel_opt,DAC_PRESCALER_CLK); dacifb_start_channel(DAC0,DACIFB_CHANNEL_SELECTION_A,PBA_SPEED); //The factory calibration for DADIFB is broken, so use manual calibration dacifb_set_value(DAC0,DACIFB_CHANNEL_SELECTION_A,false,1024); //dacifb_get_calibration_data(DAC1, &dacifb_opt,1); dacifb_opt.gain_calibration_value=0x0084; dacifb_opt.offset_calibration_value=0x0102; // configure DACIFB1 dacifb_configure(DAC1,&dacifb_opt,PBA_SPEED); dacifb_configure_channel(DAC1,DACIFB_CHANNEL_SELECTION_A,&dacifb_channel_opt,DAC_PRESCALER_CLK); dacifb_start_channel(DAC1,DACIFB_CHANNEL_SELECTION_A,PBA_SPEED); dacifb_set_value(DAC1,DACIFB_CHANNEL_SELECTION_A,false,1024); DAC0->dr0=2048; DAC1->dr0=4095; }
/*! \brief Sets up USART for shell. * * \param pba_hz The current module frequency. */ static void init_shl_rs232(long pba_hz) { // GPIO map for USART. static const gpio_map_t SHL_USART_GPIO_MAP = { {SHL_USART_RX_PIN, SHL_USART_RX_FUNCTION}, {SHL_USART_TX_PIN, SHL_USART_TX_FUNCTION} }; // Options for USART. static const usart_options_t SHL_USART_OPTIONS = { .baudrate = SHL_USART_BAUDRATE, .charlength = 8, .paritytype = USART_NO_PARITY, .stopbits = USART_1_STOPBIT, .channelmode = USART_NORMAL_CHMODE }; // Set up GPIO for SHL_USART, size of the GPIO map is 2 here. gpio_enable_module(SHL_USART_GPIO_MAP, sizeof(SHL_USART_GPIO_MAP) / sizeof(SHL_USART_GPIO_MAP[0])); // Initialize it in RS232 mode. usart_init_rs232(SHL_USART, &SHL_USART_OPTIONS, pba_hz); } /*! \brief Initializes the dataflash memory AT45DBX resources: GPIO, SPI and AT45DBX. */ static void at45dbx_resources_init(void) { // GPIO map for SPI. static const gpio_map_t AT45DBX_SPI_GPIO_MAP = { {AT45DBX_SPI_SCK_PIN, AT45DBX_SPI_SCK_FUNCTION }, // SPI Clock. {AT45DBX_SPI_MISO_PIN, AT45DBX_SPI_MISO_FUNCTION }, // MISO. {AT45DBX_SPI_MOSI_PIN, AT45DBX_SPI_MOSI_FUNCTION }, // MOSI. #define AT45DBX_ENABLE_NPCS_PIN(NPCS, unused) \ {AT45DBX_SPI_NPCS##NPCS##_PIN, AT45DBX_SPI_NPCS##NPCS##_FUNCTION}, // Chip Select NPCS. MREPEAT(AT45DBX_MEM_CNT, AT45DBX_ENABLE_NPCS_PIN, ~) #undef AT45DBX_ENABLE_NPCS_PIN }; // Options for SPI. spi_options_t spiOptions = { .reg = AT45DBX_SPI_FIRST_NPCS, // Defined in conf_at45dbx.h. .baudrate = AT45DBX_SPI_MASTER_SPEED, // Defined in conf_at45dbx.h. .bits = AT45DBX_SPI_BITS, // Defined in conf_at45dbx.h. .spck_delay = 0, .trans_delay = 0, .stay_act = 1, .spi_mode = 0, .modfdis = 1 }; // Assign I/Os to SPI. gpio_enable_module(AT45DBX_SPI_GPIO_MAP, sizeof(AT45DBX_SPI_GPIO_MAP) / sizeof(AT45DBX_SPI_GPIO_MAP[0])); // Initialize as master. spi_initMaster(AT45DBX_SPI, &spiOptions); // Set selection mode: variable_ps, pcs_decode, delay. spi_selectionMode(AT45DBX_SPI, 0, 0, 0); // Enable SPI. spi_enable(AT45DBX_SPI); // Initialize data flash with SPI clock Osc0. at45dbx_init(spiOptions, FOSC0); } /*! \brief Main function. Execution starts here. */ int main(void) { U8 i, j; U16 file_size; Fs_index sav_index; static Fs_index mark_index; const char *part_type; U32 VarTemp; // Switch to external oscillator 0. pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP); // Initialize RS232 shell text output. init_shl_rs232(FOSC0); // Initialize AT45DBX resources: GPIO, SPI and AT45DBX. at45dbx_resources_init(); // Display memory status print(SHL_USART, MSG_WELCOME "\nMemory "); // Test if the memory is ready - using the control access memory abstraction layer (/SERVICES/MEMORY/CTRL_ACCESS/) if (mem_test_unit_ready(LUN_ID_AT45DBX_MEM) == CTRL_GOOD) { // Get and display the capacity mem_read_capacity(LUN_ID_AT45DBX_MEM, &VarTemp); print(SHL_USART, "OK:\t"); print_ulong(SHL_USART, (VarTemp + 1) >> (20 - FS_SHIFT_B_TO_SECTOR)); print(SHL_USART, " MB\n"); }
// initialize application timer extern void init_tc (volatile avr32_tc_t *tc) { // waveform options static const tc_waveform_opt_t waveform_opt = { .channel = APP_TC_CHANNEL, // channel .bswtrg = TC_EVT_EFFECT_NOOP, // software trigger action on TIOB .beevt = TC_EVT_EFFECT_NOOP, // external event action .bcpc = TC_EVT_EFFECT_NOOP, // rc compare action .bcpb = TC_EVT_EFFECT_NOOP, // rb compare .aswtrg = TC_EVT_EFFECT_NOOP, // soft trig on TIOA .aeevt = TC_EVT_EFFECT_NOOP, // etc .acpc = TC_EVT_EFFECT_NOOP, .acpa = TC_EVT_EFFECT_NOOP, // Waveform selection: Up mode with automatic trigger(reset) on RC compare. .wavsel = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER, .enetrg = false, // external event trig .eevt = 0, // extern event select .eevtedg = TC_SEL_NO_EDGE, // extern event edge .cpcdis = false, // counter disable when rc compare .cpcstop = false, // counter stopped when rc compare .burst = false, .clki = false, // Internal source clock 5, connected to fPBA / 128. .tcclks = TC_CLOCK_SOURCE_TC5 }; // Options for enabling TC interrupts static const tc_interrupt_t tc_interrupt = { .etrgs = 0, .ldrbs = 0, .ldras = 0, .cpcs = 1, // Enable interrupt on RC compare alone .cpbs = 0, .cpas = 0, .lovrs = 0, .covfs = 0 }; // Initialize the timer/counter. tc_init_waveform(tc, &waveform_opt); // set timer compare trigger. // we want it to overflow and generate an interrupt every 1 ms // so (1 / fPBA / 128) * RC = 0.001 // so RC = fPBA / 128 / 1000 tc_write_rc(tc, APP_TC_CHANNEL, (FPBA_HZ / 128 / 1000)); // configure the timer interrupt tc_configure_interrupts(tc, APP_TC_CHANNEL, &tc_interrupt); // Start the timer/counter. tc_start(tc, APP_TC_CHANNEL); } // initialize usb USARTy void init_ftdi_usart (void) { // GPIO map for USART. static const gpio_map_t FTDI_USART_GPIO_MAP = { { FTDI_USART_RX_PIN, FTDI_USART_RX_FUNCTION }, { FTDI_USART_TX_PIN, FTDI_USART_TX_FUNCTION } }; // Options for USART. static const usart_options_t FTDI_USART_OPTIONS = { .baudrate = FTDI_USART_BAUDRATE, .charlength = 8, .paritytype = USART_NO_PARITY, .stopbits = USART_1_STOPBIT, .channelmode = USART_NORMAL_CHMODE }; // Set up GPIO for FTDI_USART gpio_enable_module(FTDI_USART_GPIO_MAP, sizeof(FTDI_USART_GPIO_MAP) / sizeof(FTDI_USART_GPIO_MAP[0])); // Initialize in RS232 mode. usart_init_rs232(FTDI_USART, &FTDI_USART_OPTIONS, FPBA_HZ); } // initialize spi1: OLED, ADC, SD/MMC extern void init_spi1 (void) { static const gpio_map_t OLED_SPI_GPIO_MAP = { {OLED_SPI_SCK_PIN, OLED_SPI_SCK_FUNCTION }, {OLED_SPI_MISO_PIN, OLED_SPI_MISO_FUNCTION}, {OLED_SPI_MOSI_PIN, OLED_SPI_MOSI_FUNCTION}, {OLED_SPI_NPCS0_PIN, OLED_SPI_NPCS0_FUNCTION }, {OLED_SPI_NPCS1_PIN, OLED_SPI_NPCS1_FUNCTION }, {OLED_SPI_NPCS2_PIN, OLED_SPI_NPCS2_FUNCTION }, }; // SPI options for OLED spi_options_t spiOptions = { .reg = OLED_SPI_NPCS, .baudrate = 40000000, .bits = 8, .trans_delay = 0, .spck_delay = 0, .stay_act = 1, .spi_mode = 3, .modfdis = 1 }; // Assign GPIO to SPI. gpio_enable_module(OLED_SPI_GPIO_MAP, sizeof(OLED_SPI_GPIO_MAP) / sizeof(OLED_SPI_GPIO_MAP[0])); // Initialize as master. spi_initMaster(OLED_SPI, &spiOptions); // Set SPI selection mode: variable_ps, pcs_decode, delay. spi_selectionMode(OLED_SPI, 0, 0, 0); // Enable SPI module. spi_enable(OLED_SPI); // setup chip register for OLED spi_setupChipReg( OLED_SPI, &spiOptions, FPBA_HZ ); // add ADC chip register spiOptions.reg = ADC_SPI_NPCS; spiOptions.baudrate = 20000000; spiOptions.bits = 16; spiOptions.spi_mode = 2; spiOptions.spck_delay = 0; spiOptions.trans_delay = 5; spiOptions.stay_act = 0; spiOptions.modfdis = 0; spi_setupChipReg( ADC_SPI, &spiOptions, FPBA_HZ ); // add SD/MMC chip register spiOptions.reg = SD_MMC_SPI_NPCS; spiOptions.baudrate = SD_MMC_SPI_MASTER_SPEED; // Defined in conf_sd_mmc_spi.h; spiOptions.bits = SD_MMC_SPI_BITS; // Defined in conf_sd_mmc_spi.h; spiOptions.spck_delay = 0; spiOptions.trans_delay = 0; spiOptions.stay_act = 1; spiOptions.spi_mode = 0; spiOptions.modfdis = 1; // Initialize SD/MMC driver with SPI clock (PBA). sd_mmc_spi_init(spiOptions, FPBA_HZ); } // init PDCA (Peripheral DMA Controller A) resources for the SPI transfer and start a dummy transfer void init_local_pdca(void) { // PDCA channel for SPI RX pdca_channel_options_t pdca_options_SPI_RX ={ // pdca channel options .addr = (void *)&pdcaRxBuf, .size = FS_BUF_SIZE, // transfer size .r_addr = NULL, // next memory address after 1st transfer complete .r_size = 0, // next transfer counter not used here .pid = AVR32_PDCA_CHANNEL_USED_RX, // select peripheral ID - SPI1 RX .transfer_size = PDCA_TRANSFER_SIZE_BYTE // select size of the transfer: 8,16,32 bits }; // PDCA channel for SPI TX pdca_channel_options_t pdca_options_SPI_TX ={ // pdca channel options .addr = (void *)&pdcaTxBuf, // memory address. .size = FS_BUF_SIZE, // transfer size .r_addr = NULL, // next memory address after 1st transfer complete .r_size = 0, // next transfer counter not used here .pid = AVR32_PDCA_CHANNEL_USED_TX, // select peripheral ID - SPI1 TX .transfer_size = PDCA_TRANSFER_SIZE_BYTE // select size of the transfer: 8,16,32 bits }; // Init PDCA transmission channel pdca_init_channel(AVR32_PDCA_CHANNEL_SPI_TX, &pdca_options_SPI_TX); // Init PDCA Reception channel pdca_init_channel(AVR32_PDCA_CHANNEL_SPI_RX, &pdca_options_SPI_RX); } // intialize resources for bf533 communication: SPI, GPIO void init_bfin_resources(void) { static const gpio_map_t BFIN_SPI_GPIO_MAP = { { BFIN_SPI_SCK_PIN, BFIN_SPI_SCK_FUNCTION }, { BFIN_SPI_MISO_PIN, BFIN_SPI_MISO_FUNCTION }, { BFIN_SPI_MOSI_PIN, BFIN_SPI_MOSI_FUNCTION }, { BFIN_SPI_NPCS0_PIN, BFIN_SPI_NPCS0_FUNCTION }, }; spi_options_t spiOptions = { .reg = BFIN_SPI_NPCS, //// FIXME: //// would prefer fast baudrate / lower trans delay during boot, //// but need multiple registers for boot (fast) and run (slow) //// investigate if this is possible... // .baudrate = 20000000, // .baudrate = 10000000, // .baudrate = 5000000, .baudrate = 20000000, .bits = 8, .spck_delay = 0, // .trans_delay = 0, .trans_delay = 20, .stay_act = 1, .spi_mode = 1, .modfdis = 1 }; // assign pins to SPI. gpio_enable_module(BFIN_SPI_GPIO_MAP, sizeof(BFIN_SPI_GPIO_MAP) / sizeof(BFIN_SPI_GPIO_MAP[0])); // intialize as master spi_initMaster(BFIN_SPI, &spiOptions); // set selection mode: variable_ps, pcs_decode, delay. spi_selectionMode(BFIN_SPI, 0, 0, 0); // enable SPI. spi_enable(BFIN_SPI); // intialize the chip register spi_setupChipReg(BFIN_SPI, &spiOptions, FPBA_HZ); // enable pulldown on bfin HWAIT line //// shit! not implemented... // gpio_enable_pin_pull_down(BFIN_HWAIT_PIN); // enable pullup on bfin RESET line gpio_enable_pin_pull_up(BFIN_RESET_PIN); } // intialize two-wire interface void init_twi(void) { // TWI/I2C GPIO map static const gpio_map_t TWI_GPIO_MAP = { { TWI_DATA_PIN, TWI_DATA_FUNCTION }, { TWI_CLOCK_PIN, TWI_CLOCK_FUNCTION } }; gpio_enable_module(TWI_GPIO_MAP, sizeof(TWI_GPIO_MAP) / sizeof(TWI_GPIO_MAP[0])); } // initialize USB host stack void init_usb_host (void) { // pm_configure_usb_clock(); uhc_start(); }
int main(void) { //-------------------------USART INTERRUPT REGISTRATION.------------// // Set Clock: Oscillator needs to initialized once: First pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP); // -------------- USART INIT ----------------------------------------------- static const gpio_map_t USART_GPIO_MAP = { {AVR32_USART0_RXD_0_0_PIN, AVR32_USART0_RXD_0_0_FUNCTION}, {AVR32_USART0_TXD_0_0_PIN, AVR32_USART0_TXD_0_0_FUNCTION} }; // USART options. static const usart_options_t USART_OPTIONS = { .baudrate = USART_BAUDRATE, .charlength = 8, .paritytype = USART_NO_PARITY, .stopbits = USART_1_STOPBIT, .channelmode = USART_NORMAL_CHMODE }; // Assign GPIO to USART gpio_enable_module(USART_GPIO_MAP, sizeof(USART_GPIO_MAP) / sizeof(USART_GPIO_MAP[0])); // Init USART usart_init_rs232(USART_0, &USART_OPTIONS, FOSC0); Disable_global_interrupt(); INTC_init_interrupts(); // Init Interrupt Table: Once at first // Register USART Interrupt (hinzufügen) INTC_register_interrupt(&usart_int_handler, AVR32_USART0_IRQ, AVR32_INTC_INT0); USART_0->ier = AVR32_USART_IER_RXRDY_MASK; // Activate ISR on RX Line Enable_global_interrupt(); // ----------------------------------------------------------------------------------- // -------------------------- Display INIT ---------------------------------- // Map SPI Pins static const gpio_map_t DIP204_SPI_GPIO_MAP = { {DIP204_SPI_SCK_PIN, DIP204_SPI_SCK_FUNCTION }, // SPI Clock. {DIP204_SPI_MISO_PIN, DIP204_SPI_MISO_FUNCTION}, // MISO. {DIP204_SPI_MOSI_PIN, DIP204_SPI_MOSI_FUNCTION}, // MOSI. {DIP204_SPI_NPCS_PIN, DIP204_SPI_NPCS_FUNCTION} // Chip Select NPCS. }; // add the spi options driver structure for the LCD DIP204 spi_options_t spiOptions = { .reg = DIP204_SPI_NPCS, .baudrate = 1000000, .bits = 8, .spck_delay = 0, .trans_delay = 0, .stay_act = 1, .spi_mode = 0, .modfdis = 1 }; // SPI Inits: Assign I/Os to SPI gpio_enable_module(DIP204_SPI_GPIO_MAP, sizeof(DIP204_SPI_GPIO_MAP) / sizeof(DIP204_SPI_GPIO_MAP[0])); // Initialize as master spi_initMaster(DIP204_SPI, &spiOptions); // Set selection mode: variable_ps, pcs_decode, delay spi_selectionMode(DIP204_SPI, 0, 0, 0); // Enable SPI spi_enable(DIP204_SPI); // setup chip registers spi_setupChipReg(DIP204_SPI, &spiOptions, FOSC0); // initialize delay driver: Muss vor dip204_init() ausgeführt werden delay_init( FOSC0 ); // initialize LCD dip204_init(backlight_PWM, TRUE); // --------------------------------------------------------------------------------------- // ----------------- Timer Counter Init --------------------------------- // Timer Configs: Options for waveform generation. static const tc_waveform_opt_t WAVEFORM_OPT = { .channel = TC_CHANNEL, // Channel selection. .bswtrg = TC_EVT_EFFECT_NOOP, // Software trigger effect on TIOB. .beevt = TC_EVT_EFFECT_NOOP, // External event effect on TIOB. .bcpc = TC_EVT_EFFECT_NOOP, // RC compare effect on TIOB. .bcpb = TC_EVT_EFFECT_NOOP, // RB compare effect on TIOB. .aswtrg = TC_EVT_EFFECT_NOOP, // Software trigger effect on TIOA. .aeevt = TC_EVT_EFFECT_NOOP, // External event effect on TIOA. .acpc = TC_EVT_EFFECT_NOOP, // RC compare effect on TIOA: toggle. .acpa = TC_EVT_EFFECT_NOOP, // RA compare effect on TIOA: toggle .wavsel = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER,// Count till RC and reset (S. 649): Waveform selection .enetrg = FALSE, // External event trigger enable. .eevt = 0, // External event selection. .eevtedg = TC_SEL_NO_EDGE, // External event edge selection. .cpcdis = FALSE, // Counter disable when RC compare. .cpcstop = FALSE, // Counter clock stopped with RC compare. .burst = FALSE, // Burst signal selection. .clki = FALSE, // Clock inversion. .tcclks = TC_CLOCK_SOURCE_TC3 // Internal source clock 3, connected to fPBA / 8. }; // TC Interrupt Enable Register static const tc_interrupt_t TC_INTERRUPT = { .etrgs = 0, .ldrbs = 0, .ldras = 0, .cpcs = 1, .cpbs = 0, .cpas = 0, .lovrs = 0, .covfs = 0 }; // 0 = No Effect | 1 = Enable ( CPCS = 1 enables the RC Compare Interrupt ) // ***************** Timer Setup *********************************************** // Initialize the timer/counter. tc_init_waveform(tc, &WAVEFORM_OPT); // Initialize the timer/counter waveform. // Set the compare triggers. tc_write_rc(tc, TC_CHANNEL, RC); // Set RC value. tc_configure_interrupts(tc, TC_CHANNEL, &TC_INTERRUPT); // Start the timer/counter. tc_start(tc, TC_CHANNEL); // And start the timer/counter. // ******************************************************************************* Disable_global_interrupt(); // Register TC Interrupt INTC_register_interrupt(&tc_irq, AVR32_TC_IRQ0, AVR32_INTC_INT3); Enable_global_interrupt(); // --------------------------------------------------------------------------------------- imu_init(); //-------------------------------TWI R/W --------------------------------------------------- sensorDaten imu_data = {0}; char disp1[30], disp2[30], disp3[30], disp4[30]; short GX,GY,GZ, AX, AY, AZ; //shifted comlete Data RPY currMoveRPY; Quaternion currQuat; currQuat.q0 = 1.0; currQuat.q1 = 0; currQuat.q2 = 0; currQuat.q3 = 0; Quaternion deltaQuat; RotMatrix rot = {0}; RPY reconverted; calibrate_all(&imu_data); while(1){ if(exe){ exe = false; read_sensor(&imu_data); AX = imu_data.acc_x + imu_data.acc_x_bias; AY = imu_data.acc_y + imu_data.acc_y_bias; AZ = imu_data.acc_z + imu_data.acc_z_bias; GX = imu_data.gyro_x + imu_data.gyro_x_bias; GY = imu_data.gyro_y + imu_data.gyro_y_bias; GZ = imu_data.gyro_z + imu_data.gyro_z_bias; //convert to 1G float ax = (float)AX * (-4.0); float ay = (float)AY * (-4.0); //wegen 2^11= 2048, /2 = 1024 entspricht 4G -> 1G = (1024/4) float az = (float)AZ * (-4.0); //convert to 1°/s gx = ((float)GX/ 14.375); // in °/s gy = ((float)GY/ 14.375); gz = ((float)GZ/ 14.375); //Integration over time dGx = (gx*0.03); dGy = (gy*0.03); dGz = (gz*0.03); currMoveRPY.pitch = -dGx; currMoveRPY.roll = dGy; currMoveRPY.yaw = dGz; //aufaddieren auf den aktuellen Winkel IN GRAD gxDeg += dGx; gyDeg += dGy; gzDeg += dGz; //RPY in Quaternion umwandeln RPYtoQuat(&deltaQuat, &currMoveRPY); //normieren normQuat(&deltaQuat); //aufmultiplizeiren quatMultiplication(&deltaQuat, &currQuat, &currQuat); //nochmal normieren normQuat(&currQuat); //rücktransformation nicht nötig!! char send[80]; sprintf(send,"$,%f,%f,%f,%f,#", currQuat.q0, currQuat.q1, currQuat.q2, currQuat.q3); usart_write_line(USART_0,send); sprintf(disp1,"q0:%.3f, GX:%3.0f",currQuat.q0,gxDeg); sprintf(disp2,"q1:%.3f, GY:%3.0f",currQuat.q1, gyDeg); sprintf(disp3,"q2:%.3f, GZ:%3.0f",currQuat.q2, gzDeg); sprintf(disp4,"q3:%.3f",currQuat.q3); dip204_clear_display(); dip204_set_cursor_position(1,1); dip204_write_string(disp1); dip204_set_cursor_position(1,2); dip204_write_string(disp2); dip204_set_cursor_position(1,3); dip204_write_string(disp3); dip204_set_cursor_position(1,4); dip204_write_string(disp4); //sprintf(data,"TEST:%s",high); //print_dbg(data); } } }
// initializes the SD/MMC memory resources: GPIO, SPI and MMC //------------------------------------------------------------------- static void sd_mmc_resources_init(long pba_hz) { // GPIO pins used for SD/MMC interface static const gpio_map_t SD_MMC_SPI_GPIO_MAP = { {SD_MMC_SPI_SCK_PIN, SD_MMC_SPI_SCK_FUNCTION }, // SPI Clock. {SD_MMC_SPI_MISO_PIN, SD_MMC_SPI_MISO_FUNCTION}, // MISO. {SD_MMC_SPI_MOSI_PIN, SD_MMC_SPI_MOSI_FUNCTION}, // MOSI. {SD_MMC_SPI_NPCS_PIN, SD_MMC_SPI_NPCS_FUNCTION} // Chip Select NPCS. }; // SPI options. spi_options_t spiOptions = { .reg = SD_MMC_SPI_NPCS, .baudrate = SD_MMC_SPI_MASTER_SPEED, // Defined in conf_sd_mmc_spi.h. .bits = SD_MMC_SPI_BITS, // Defined in conf_sd_mmc_spi.h. .spck_delay = 0, .trans_delay = 0, .stay_act = 1, .spi_mode = 0, .modfdis = 1 }; // assign I/Os to SPI. gpio_enable_module(SD_MMC_SPI_GPIO_MAP, sizeof(SD_MMC_SPI_GPIO_MAP) / sizeof(SD_MMC_SPI_GPIO_MAP[0])); // initialize as master. spi_initMaster(SD_MMC_SPI, &spiOptions); // set SPI selection mode: variable_ps, pcs_decode, delay. spi_selectionMode(SD_MMC_SPI, 0, 0, 0); // enable SPI module. spi_enable(SD_MMC_SPI); // Initialize SD/MMC driver with SPI clock (PBA). sd_mmc_spi_init(spiOptions, pba_hz); } // process a USB frame //------------------------------------------------------------------- void process_frame(uint16_t framenumber) { static uint8_t cpt_sof = 0; static injectState_t state = state_START_INJECT; static uint8_t wait = 0; static uint16_t debounce = 0; static uint16_t injectToken = 0x0000; static uint16_t delay = 0; // scan process running each 2ms cpt_sof++; if( 2 > cpt_sof ) return; cpt_sof = 0; // pulse led LED_Set_Intensity( LED0, framenumber >> 1 ); // debounce switch if( debounce > 0 ) --debounce; //delay a random amount if(delay == 0){ // injection state machine switch(state) { case state_IDLE: // check switch if( gpio_get_pin_value(GPIO_JOYSTICK_PUSH) == GPIO_JOYSTICK_PUSH_PRESSED ) { // debounce if( debounce == 0 ) { state = state_START_INJECT; debounce = 250; } } break; case state_START_INJECT: file_open(FOPEN_MODE_R); state = state_INJECTING; break; case state_INJECTING: if( file_eof() ) { file_close(); state = state_IDLE; break; } injectToken = ( file_getc() | ( file_getc() << 8 ) ); if( ( injectToken&0xff ) == 0x00 ) { wait = injectToken>>8; state = state_WAIT; } else if( ( injectToken>>8 ) == 0x00 ) { state = state_KEY_DOWN; } else { state = state_MOD_DOWN; } break; case state_KEY_DOWN: udi_hid_kbd_down(injectToken&0xff); state = state_KEY_UP; delay = rand() % MAX_DELAY; break; case state_KEY_UP: udi_hid_kbd_up(injectToken&0xff); state = state_INJECTING; delay = rand() % MAX_DELAY; break; case state_MOD_DOWN: udi_hid_kbd_modifier_down(injectToken>>8); state = state_MOD_KEY_DOWN; delay = rand() % MAX_DELAY; break; case state_MOD_KEY_DOWN: udi_hid_kbd_down(injectToken&0xff); state = state_MOD_KEY_UP; delay = rand() % MAX_DELAY; break; case state_MOD_KEY_UP: udi_hid_kbd_up(injectToken&0xff); state = state_MOD_UP; delay = rand() % MAX_DELAY; break; case state_MOD_UP: udi_hid_kbd_modifier_up(injectToken>>8); state = state_INJECTING; delay = rand() % 50; break; case state_WAIT: if( --wait == 0 ) { state = state_INJECTING; } break; default: state = state_IDLE; }
// initialize application timer extern void init_tc (void) { volatile avr32_tc_t *tc = APP_TC; // waveform options static const tc_waveform_opt_t waveform_opt = { .channel = APP_TC_CHANNEL, // channel .bswtrg = TC_EVT_EFFECT_NOOP, // software trigger action on TIOB .beevt = TC_EVT_EFFECT_NOOP, // external event action .bcpc = TC_EVT_EFFECT_NOOP, // rc compare action .bcpb = TC_EVT_EFFECT_NOOP, // rb compare .aswtrg = TC_EVT_EFFECT_NOOP, // soft trig on TIOA .aeevt = TC_EVT_EFFECT_NOOP, // etc .acpc = TC_EVT_EFFECT_NOOP, .acpa = TC_EVT_EFFECT_NOOP, // Waveform selection: Up mode with automatic trigger(reset) on RC compare. .wavsel = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER, .enetrg = false, // external event trig .eevt = 0, // extern event select .eevtedg = TC_SEL_NO_EDGE, // extern event edge .cpcdis = false, // counter disable when rc compare .cpcstop = false, // counter stopped when rc compare .burst = false, .clki = false, // Internal source clock 5, connected to fPBA / 128. .tcclks = TC_CLOCK_SOURCE_TC5 }; // Options for enabling TC interrupts static const tc_interrupt_t tc_interrupt = { .etrgs = 0, .ldrbs = 0, .ldras = 0, .cpcs = 1, // Enable interrupt on RC compare alone .cpbs = 0, .cpas = 0, .lovrs = 0, .covfs = 0 }; // Initialize the timer/counter. tc_init_waveform(tc, &waveform_opt); // set timer compare trigger. // we want it to overflow and generate an interrupt every 1 ms // so (1 / fPBA / 128) * RC = 0.001 // so RC = fPBA / 128 / 1000 // tc_write_rc(tc, APP_TC_CHANNEL, (FPBA_HZ / 128000)); tc_write_rc(tc, APP_TC_CHANNEL, (FPBA_HZ / 128000)); // configure the timer interrupt tc_configure_interrupts(tc, APP_TC_CHANNEL, &tc_interrupt); // Start the timer/counter. tc_start(tc, APP_TC_CHANNEL); } extern void init_spi (void) { sysclk_enable_pba_module(SYSCLK_SPI); static const gpio_map_t SPI_GPIO_MAP = { {SPI_SCK_PIN, SPI_SCK_FUNCTION }, {SPI_MISO_PIN, SPI_MISO_FUNCTION}, {SPI_MOSI_PIN, SPI_MOSI_FUNCTION}, {SPI_NPCS0_PIN, SPI_NPCS0_FUNCTION }, {SPI_NPCS1_PIN, SPI_NPCS1_FUNCTION }, {SPI_NPCS2_PIN, SPI_NPCS2_FUNCTION }, }; // Assign GPIO to SPI. gpio_enable_module(SPI_GPIO_MAP, sizeof(SPI_GPIO_MAP) / sizeof(SPI_GPIO_MAP[0])); spi_options_t spiOptions = { .reg = DAC_SPI, .baudrate = 2000000, .bits = 8, .trans_delay = 0, .spck_delay = 0, .stay_act = 1, .spi_mode = 1, .modfdis = 1 }; // Initialize as master. spi_initMaster(SPI, &spiOptions); // Set SPI selection mode: variable_ps, pcs_decode, delay. spi_selectionMode(SPI, 0, 0, 0); // Enable SPI module. spi_enable(SPI); // spi_setupChipReg( SPI, &spiOptions, FPBA_HZ ); spi_setupChipReg(SPI, &spiOptions, sysclk_get_pba_hz() ); // add ADC chip register spiOptions.reg = ADC_SPI; spiOptions.baudrate = 20000000; spiOptions.bits = 16; spiOptions.spi_mode = 2; spiOptions.spck_delay = 0; spiOptions.trans_delay = 5; spiOptions.stay_act = 0; spiOptions.modfdis = 0; spi_setupChipReg( SPI, &spiOptions, FPBA_HZ ); // add OLED chip register spiOptions.reg = OLED_SPI; spiOptions.baudrate = 40000000; spiOptions.bits = 8; spiOptions.spi_mode = 3; spiOptions.spck_delay = 0; spiOptions.trans_delay = 0; spiOptions.stay_act = 1; spiOptions.modfdis = 1; spi_setupChipReg( SPI, &spiOptions, FPBA_HZ ); } // initialize USB host stack void init_usb_host (void) { uhc_start(); } // initialize i2c void init_i2c_master(void) { twi_options_t opt; int status; static const gpio_map_t TWI_GPIO_MAP = { {AVR32_TWI_SDA_0_0_PIN, AVR32_TWI_SDA_0_0_FUNCTION}, {AVR32_TWI_SCL_0_0_PIN, AVR32_TWI_SCL_0_0_FUNCTION} }; gpio_enable_module(TWI_GPIO_MAP, sizeof(TWI_GPIO_MAP) / sizeof(TWI_GPIO_MAP[0])); // options settings opt.pba_hz = FOSC0; opt.speed = TWI_SPEED; opt.chip = 0x50; // initialize TWI driver with options // status = twi_master_init(&AVR32_TWI, &opt); status = twi_master_init(TWI, &opt); /* // check init result if (status == TWI_SUCCESS) print_dbg("\r\ni2c init"); else print_dbg("\r\ni2c init FAIL"); */ } void init_i2c_slave(void) { twi_options_t opt; twi_slave_fct_t twi_slave_fct; int status; static const gpio_map_t TWI_GPIO_MAP = { {AVR32_TWI_SDA_0_0_PIN, AVR32_TWI_SDA_0_0_FUNCTION}, {AVR32_TWI_SCL_0_0_PIN, AVR32_TWI_SCL_0_0_FUNCTION} }; gpio_enable_module(TWI_GPIO_MAP, sizeof(TWI_GPIO_MAP) / sizeof(TWI_GPIO_MAP[0])); // options settings opt.pba_hz = FOSC0; opt.speed = TWI_SPEED; opt.chip = 0x50; // initialize TWI driver with options twi_slave_fct.rx = &twi_slave_rx; twi_slave_fct.tx = &twi_slave_tx; twi_slave_fct.stop = &twi_slave_stop; status = twi_slave_init(&AVR32_TWI, &opt, &twi_slave_fct ); /* // check init result if (status == TWI_SUCCESS) print_dbg("\r\ni2c init"); else print_dbg("\r\ni2c init FAIL"); */ }
/*! * \brief main function : do init and loop (poll if configured so) */ int main(void) { static const gpio_map_t DIP204_SPI_GPIO_MAP = { {DIP204_SPI_SCK_PIN, DIP204_SPI_SCK_FUNCTION }, // SPI Clock. {DIP204_SPI_MISO_PIN, DIP204_SPI_MISO_FUNCTION}, // MISO. {DIP204_SPI_MOSI_PIN, DIP204_SPI_MOSI_FUNCTION}, // MOSI. {DIP204_SPI_NPCS_PIN, DIP204_SPI_NPCS_FUNCTION} // Chip Select NPCS. }; // Switch the CPU main clock to oscillator 0 pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP); // Disable all interrupts. Disable_global_interrupt(); // init the interrupts INTC_init_interrupts(); // Enable all interrupts. Enable_global_interrupt(); // add the spi options driver structure for the LCD DIP204 spi_options_t spiOptions = { .reg = DIP204_SPI_NPCS, .baudrate = 1000000, .bits = 8, .spck_delay = 0, .trans_delay = 0, .stay_act = 1, .spi_mode = 0, .modfdis = 1 }; // Assign I/Os to SPI gpio_enable_module(DIP204_SPI_GPIO_MAP, sizeof(DIP204_SPI_GPIO_MAP) / sizeof(DIP204_SPI_GPIO_MAP[0])); // Initialize as master spi_initMaster(DIP204_SPI, &spiOptions); // Set selection mode: variable_ps, pcs_decode, delay spi_selectionMode(DIP204_SPI, 0, 0, 0); // Enable SPI spi_enable(DIP204_SPI); // setup chip registers spi_setupChipReg(DIP204_SPI, &spiOptions, FOSC0); // configure local push buttons dip204_example_configure_push_buttons_IT(); // configure local joystick dip204_example_configure_joystick_IT(); // initialize LCD dip204_init(backlight_PWM, true); // reset marker current_char = 0x10; // Display default message. dip204_set_cursor_position(8,1); dip204_write_string("ATMEL"); dip204_set_cursor_position(7,2); dip204_write_string("EVK1100"); dip204_set_cursor_position(6,3); dip204_write_string("AVR32 UC3"); dip204_set_cursor_position(3,4); dip204_write_string("AT32UC3A Series"); dip204_hide_cursor(); /* do a loop */ while (1) { if (display) { delay_ms(400); // A delay so that it is humanly possible to see the // character(s) before they are cleared. // Clear line 1 column 19 dip204_set_cursor_position(19,1); dip204_write_string(" "); // Clear line 2 from column 18 to column 20. dip204_set_cursor_position(18,2); dip204_write_string(" "); // 3 spaces // Clear line 3 column 19 dip204_set_cursor_position(19,3); dip204_write_string(" "); display = 0; } } }
void memories_initialization(void) { //-- Hmatrix bus configuration // This improve speed performance #ifdef AVR32_HMATRIXB union { unsigned long scfg; avr32_hmatrixb_scfg_t SCFG; } u_avr32_hmatrixb_scfg; sysclk_enable_pbb_module(SYSCLK_HMATRIX); // For the internal-flash HMATRIX slave, use last master as default. u_avr32_hmatrixb_scfg.scfg = AVR32_HMATRIXB.scfg[AVR32_HMATRIXB_SLAVE_FLASH]; u_avr32_hmatrixb_scfg.SCFG.defmstr_type = AVR32_HMATRIXB_DEFMSTR_TYPE_LAST_DEFAULT; AVR32_HMATRIXB.scfg[AVR32_HMATRIXB_SLAVE_FLASH] = u_avr32_hmatrixb_scfg.scfg; // For the internal-SRAM HMATRIX slave, use last master as default. u_avr32_hmatrixb_scfg.scfg = AVR32_HMATRIXB.scfg[AVR32_HMATRIXB_SLAVE_SRAM]; u_avr32_hmatrixb_scfg.SCFG.defmstr_type = AVR32_HMATRIXB_DEFMSTR_TYPE_LAST_DEFAULT; AVR32_HMATRIXB.scfg[AVR32_HMATRIXB_SLAVE_SRAM] = u_avr32_hmatrixb_scfg.scfg; # ifdef AVR32_HMATRIXB_SLAVE_EBI // For the EBI HMATRIX slave, use last master as default. u_avr32_hmatrixb_scfg.scfg = AVR32_HMATRIXB.scfg[AVR32_HMATRIXB_SLAVE_EBI]; u_avr32_hmatrixb_scfg.SCFG.defmstr_type = AVR32_HMATRIXB_DEFMSTR_TYPE_LAST_DEFAULT; AVR32_HMATRIXB.scfg[AVR32_HMATRIXB_SLAVE_EBI] = u_avr32_hmatrixb_scfg.scfg; # endif #endif #ifdef AVR32_HMATRIX union { unsigned long scfg; avr32_hmatrix_scfg_t SCFG; } u_avr32_hmatrix_scfg; sysclk_enable_pbb_module(SYSCLK_HMATRIX); // For the internal-flash HMATRIX slave, use last master as default. u_avr32_hmatrix_scfg.scfg = AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_FLASH]; u_avr32_hmatrix_scfg.SCFG.defmstr_type = AVR32_HMATRIX_DEFMSTR_TYPE_LAST_DEFAULT; AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_FLASH] = u_avr32_hmatrix_scfg.scfg; // For the internal-SRAM HMATRIX slave, use last master as default. u_avr32_hmatrix_scfg.scfg = AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_SRAM]; u_avr32_hmatrix_scfg.SCFG.defmstr_type = AVR32_HMATRIX_DEFMSTR_TYPE_LAST_DEFAULT; AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_SRAM] = u_avr32_hmatrix_scfg.scfg; # ifdef AVR32_HMATRIX_SLAVE_EBI // For the EBI HMATRIX slave, use last master as default. u_avr32_hmatrix_scfg.scfg = AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_EBI]; u_avr32_hmatrix_scfg.SCFG.defmstr_type = AVR32_HMATRIX_DEFMSTR_TYPE_LAST_DEFAULT; AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_EBI] = u_avr32_hmatrix_scfg.scfg; # endif #endif #ifdef AVR32_HMATRIX_MASTER_USBB_DMA union { unsigned long mcfg; avr32_hmatrix_mcfg_t MCFG; } u_avr32_hmatrix_mcfg; // For the USBB DMA HMATRIX master, use infinite length burst. u_avr32_hmatrix_mcfg.mcfg = AVR32_HMATRIX.mcfg[AVR32_HMATRIX_MASTER_USBB_DMA]; u_avr32_hmatrix_mcfg.MCFG.ulbt = AVR32_HMATRIX_ULBT_INFINITE; AVR32_HMATRIX.mcfg[AVR32_HMATRIX_MASTER_USBB_DMA] = u_avr32_hmatrix_mcfg.mcfg; // For the USBB DPRAM HMATRIX slave, use the USBB DMA as fixed default master. u_avr32_hmatrix_scfg.scfg = AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_USBB_DPRAM]; u_avr32_hmatrix_scfg.SCFG.fixed_defmstr = AVR32_HMATRIX_MASTER_USBB_DMA; u_avr32_hmatrix_scfg.SCFG.defmstr_type = AVR32_HMATRIX_DEFMSTR_TYPE_FIXED_DEFAULT; AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_USBB_DPRAM] = u_avr32_hmatrix_scfg.scfg; #endif #if (defined AT45DBX_MEM) && (AT45DBX_MEM == ENABLE) sysclk_enable_peripheral_clock(AT45DBX_SPI_MODULE); at45dbx_init(); #endif #if ((defined SD_MMC_MCI_0_MEM) && (SD_MMC_MCI_0_MEM == ENABLE)) \ || ((defined SD_MMC_MCI_1_MEM) && (SD_MMC_MCI_1_MEM == ENABLE)) sysclk_enable_pbb_module(SYSCLK_MCI); sysclk_enable_hsb_module(SYSCLK_DMACA); sd_mmc_mci_init(SD_SLOT_8BITS, sysclk_get_pbb_hz(), sysclk_get_cpu_hz()); #endif #if (defined SD_MMC_SPI_MEM) && (SD_MMC_SPI_MEM == ENABLE) // SPI options. spi_options_t spiOptions = { .reg = SD_MMC_SPI_NPCS, .baudrate = SD_MMC_SPI_MASTER_SPEED, // Defined in conf_sd_mmc_spi.h. .bits = SD_MMC_SPI_BITS, // Defined in conf_sd_mmc_spi.h. .spck_delay = 0, .trans_delay = 0, .stay_act = 1, .spi_mode = 0, .modfdis = 1 }; sysclk_enable_peripheral_clock(SD_MMC_SPI); // If the SPI used by the SD/MMC is not enabled. if (!spi_is_enabled(SD_MMC_SPI)) { // Initialize as master. spi_initMaster(SD_MMC_SPI, &spiOptions); // Set selection mode: variable_ps, pcs_decode, delay. spi_selectionMode(SD_MMC_SPI, 0, 0, 0); // Enable SPI. spi_enable(SD_MMC_SPI); } // Initialize SD/MMC with SPI PB clock. sd_mmc_spi_init(spiOptions,sysclk_get_pba_hz()); #endif // SD_MMC_SPI_MEM == ENABLE }
int main(void) { sysclk_init(); int i=0; // board_init(); sysclk_enable_pba_module(SYSCLK_SPI); // spi_reset(SPI_EXAMPLE); // spi_set_master_mode(SPI_EXAMPLE); // spi_disable_modfault(SPI_EXAMPLE); // spi_disable_loopback(SPI_EXAMPLE); // spi_set_chipselect(SPI_EXAMPLE,(1 << AVR32_SPI_MR_PCS_SIZE) - 1); // spi_disable_variable_chipselect(SPI_EXAMPLE); // spi_disable_chipselect_decoding(SPI_EXAMPLE); // spi_set_delay(SPI_EXAMPLE,0); // spi_set_chipselect_delay_bct(SPI_EXAMPLE,0,0); // spi_set_chipselect_delay_bs(SPI_EXAMPLE,0,0); // spi_set_bits_per_transfer(SPI_EXAMPLE,0, 8); // spi_set_baudrate_register(SPI_EXAMPLE,0, getBaudDiv(1000000, sysclk_get_peripheral_bus_hz(SPI_EXAMPLE))); // spi_enable_active_mode(SPI_EXAMPLE,0); // spi_set_mode(SPI_EXAMPLE,0,SPI_MODE_0); static const gpio_map_t SPI_GPIO_MAP = { {AT45DBX_SPI_SCK_PIN, AT45DBX_SPI_SCK_FUNCTION }, {AT45DBX_SPI_MISO_PIN, AT45DBX_SPI_MISO_FUNCTION}, {AT45DBX_SPI_MOSI_PIN, AT45DBX_SPI_MOSI_FUNCTION}, {AT45DBX_SPI_NPCS0_PIN, AT45DBX_SPI_NPCS0_FUNCTION }, // {AT45DBX_SPI_NPCS1_PIN, AT45DBX_SPI_NPCS1_FUNCTION }, }; // Assign GPIO to SPI. gpio_enable_module(SPI_GPIO_MAP, sizeof(SPI_GPIO_MAP) / sizeof(SPI_GPIO_MAP[0])); spi_options_t spiOptions = { .reg = 0, .baudrate = 1000000, .bits = 8, .trans_delay = 0, .spck_delay = 0, .stay_act = 1, .spi_mode = 0, .modfdis = 1 }; // Initialize as master. spi_initMaster(SPI_EXAMPLE, &spiOptions); // Set SPI selection mode: variable_ps, pcs_decode, delay. spi_selectionMode(SPI_EXAMPLE, 0, 0, 0); // Enable SPI module. spi_enable(SPI_EXAMPLE); // spi_setupChipReg( SPI, &spiOptions, FPBA_HZ ); spi_setupChipReg(SPI_EXAMPLE, &spiOptions, sysclk_get_pba_hz() ); spi_enable(SPI_EXAMPLE); while (true) { i++; delay_ms(10); // status = spi_at45dbx_mem_check(); spi_selectChip(SPI_EXAMPLE,0); spi_put(SPI_EXAMPLE,i); spi_unselectChip(SPI_EXAMPLE,0); } }
/*! \brief Initializes SD/MMC resources: GPIO, SPI and SD/MMC. */ static void sd_mmc_resources_init(void) { // GPIO pins used for SD/MMC interface static const gpio_map_t SD_MMC_SPI_GPIO_MAP = { {SD_MMC_SPI_SCK_PIN, SD_MMC_SPI_SCK_FUNCTION }, // SPI Clock. {SD_MMC_SPI_MISO_PIN, SD_MMC_SPI_MISO_FUNCTION}, // MISO. {SD_MMC_SPI_MOSI_PIN, SD_MMC_SPI_MOSI_FUNCTION}, // MOSI. {SD_MMC_SPI_NPCS_PIN, SD_MMC_SPI_NPCS_FUNCTION} // Chip Select NPCS. }; // SPI options. spi_options_t spiOptions = { .reg = SD_MMC_SPI_NPCS, .baudrate = SD_MMC_SPI_MASTER_SPEED, // Defined in conf_sd_mmc_spi.h. .bits = SD_MMC_SPI_BITS, // Defined in conf_sd_mmc_spi.h. .spck_delay = 0, .trans_delay = 0, .stay_act = 1, .spi_mode = 0, .modfdis = 1 }; // Assign I/Os to SPI. gpio_enable_module(SD_MMC_SPI_GPIO_MAP, sizeof(SD_MMC_SPI_GPIO_MAP) / sizeof(SD_MMC_SPI_GPIO_MAP[0])); // Initialize as master. spi_initMaster(SD_MMC_SPI, &spiOptions); // Set SPI selection mode: variable_ps, pcs_decode, delay. spi_selectionMode(SD_MMC_SPI, 0, 0, 0); // Enable SPI module. spi_enable(SD_MMC_SPI); // Initialize SD/MMC driver with SPI clock (PBA). sd_mmc_spi_init(spiOptions, PBA_HZ); } /*! \brief Initialize PDCA (Peripheral DMA Controller A) resources for the SPI transfer and start a dummy transfer */ void local_pdca_init(void) { // this PDCA channel is used for data reception from the SPI pdca_channel_options_t pdca_options_SPI_RX ={ // pdca channel options .addr = ram_buffer, // memory address. We take here the address of the string dummy_data. This string is located in the file dummy.h .size = 512, // transfer counter: here the size of the string .r_addr = NULL, // next memory address after 1st transfer complete .r_size = 0, // next transfer counter not used here .pid = AVR32_PDCA_CHANNEL_USED_RX, // select peripheral ID - data are on reception from SPI1 RX line .transfer_size = PDCA_TRANSFER_SIZE_BYTE // select size of the transfer: 8,16,32 bits }; // this channel is used to activate the clock of the SPI by sending a dummy variables pdca_channel_options_t pdca_options_SPI_TX ={ // pdca channel options .addr = (void *)&dummy_data, // memory address. // We take here the address of the string dummy_data. // This string is located in the file dummy.h .size = 512, // transfer counter: here the size of the string .r_addr = NULL, // next memory address after 1st transfer complete .r_size = 0, // next transfer counter not used here .pid = AVR32_PDCA_CHANNEL_USED_TX, // select peripheral ID - data are on reception from SPI1 RX line .transfer_size = PDCA_TRANSFER_SIZE_BYTE // select size of the transfer: 8,16,32 bits }; // Init PDCA transmission channel pdca_init_channel(AVR32_PDCA_CHANNEL_SPI_TX, &pdca_options_SPI_TX); // Init PDCA Reception channel pdca_init_channel(AVR32_PDCA_CHANNEL_SPI_RX, &pdca_options_SPI_RX); //! \brief Enable pdca transfer interrupt when completed INTC_register_interrupt(&pdca_int_handler, AVR32_PDCA_IRQ_0, AVR32_INTC_INT1); // pdca_channel_spi1_RX = 0 } /*! \brief Main function. Execution starts here. */ int main(void) { int i, j; // Switch the main clock to the external oscillator 0 pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP); // Initialize debug RS232 with PBA clock init_dbg_rs232(PBA_HZ); //start test print_dbg("\r\nInit SD/MMC Driver"); print_dbg("\r\nInsert SD/MMC..."); // Initialize Interrupt Controller INTC_init_interrupts(); // Initialize SD/MMC driver resources: GPIO, SPI and SD/MMC. sd_mmc_resources_init(); // Wait for a card to be inserted while (!sd_mmc_spi_mem_check()); print_dbg("\r\nCard detected!"); // Read Card capacity sd_mmc_spi_get_capacity(); print_dbg("Capacity = "); print_dbg_ulong(capacity >> 20); print_dbg(" MBytes"); // Enable all interrupts. Enable_global_interrupt(); // Initialize PDCA controller before starting a transfer local_pdca_init(); // Read the first sectors number 1, 2, 3 of the card for(j = 1; j <= 3; j++) { // Configure the PDCA channel: the address of memory ram_buffer to receive the data at sector address j pdca_load_channel( AVR32_PDCA_CHANNEL_SPI_RX, &ram_buffer, 512); pdca_load_channel( AVR32_PDCA_CHANNEL_SPI_TX, (void *)&dummy_data, 512); //send dummy to activate the clock end_of_transfer = false; // open sector number j if(sd_mmc_spi_read_open_PDCA (j)) { print_dbg("\r\nFirst 512 Bytes of Transfer number "); print_dbg_ulong(j); print_dbg(" :\r\n"); spi_write(SD_MMC_SPI,0xFF); // Write a first dummy data to synchronize transfer pdca_enable_interrupt_transfer_complete(AVR32_PDCA_CHANNEL_SPI_RX); pdca_channelrx =(volatile avr32_pdca_channel_t*) pdca_get_handler(AVR32_PDCA_CHANNEL_SPI_RX); // get the correct PDCA channel pointer pdca_channeltx =(volatile avr32_pdca_channel_t*) pdca_get_handler(AVR32_PDCA_CHANNEL_SPI_TX); // get the correct PDCA channel pointer pdca_channelrx->cr = AVR32_PDCA_TEN_MASK; // Enable RX PDCA transfer first pdca_channeltx->cr = AVR32_PDCA_TEN_MASK; // and TX PDCA transfer while(!end_of_transfer); // Display the first 2O bytes of the ram_buffer content for( i = 0; i < 20; i++) { print_dbg_char_hex( (U8)(*(ram_buffer + i))); } } else { print_dbg("\r\n! Unable to open memory \r\n"); } } print_dbg("\r\nEnd of the example.\r\n"); while (1); }
/*! \brief Initializes QT60168 resources: GPIO and SPI */ static void qt60168_resources_init(void) { static const gpio_map_t QT60168_SPI_GPIO_MAP = { {QT60168_SPI_SCK_PIN, QT60168_SPI_SCK_FUNCTION }, // SPI Clock. {QT60168_SPI_MISO_PIN, QT60168_SPI_MISO_FUNCTION }, // MISO. {QT60168_SPI_MOSI_PIN, QT60168_SPI_MOSI_FUNCTION }, // MOSI. {QT60168_SPI_NPCS0_PIN, QT60168_SPI_NPCS0_FUNCTION} // Chip Select NPCS. }; // SPI options. spi_options_t spiOptions = { .reg = QT60168_SPI_NCPS, .baudrate = QT60168_SPI_MASTER_SPEED, // Defined in conf_qt60168.h. .bits = QT60168_SPI_BITS, // Defined in conf_qt60168.h. .spck_delay = 0, .trans_delay = 0, .stay_act = 0, .spi_mode = 3, .modfdis = 1 }; // Assign I/Os to SPI. gpio_enable_module(QT60168_SPI_GPIO_MAP, sizeof(QT60168_SPI_GPIO_MAP) / sizeof(QT60168_SPI_GPIO_MAP[0])); // Initialize as master. spi_initMaster(QT60168_SPI, &spiOptions); // Set selection mode: variable_ps, pcs_decode, delay. spi_selectionMode(QT60168_SPI, 0, 0, 0); // Enable SPI. spi_enable(QT60168_SPI); // Initialize QT60168 with SPI clock Osc0. spi_setupChipReg(QT60168_SPI, &spiOptions, FOSC0); } typedef enum { DEMO_COLOR_ALL=0 , DEMO_COLOR_BLUE , DEMO_COLOR_RED , DEMO_COLOR_GREEN , DEMO_COLOR_MAX } demo_color_t; typedef enum { DEMO_DISPLAY_BOXES=0 , DEMO_DISPLAY_WHEEL , DEMO_DISPLAY_MAX } demo_display_t; /*! \brief Main function */ int main(void) { int i; bool idle=false; // Detect key transition (PRESSED -> RELEASED) U32 x_start; U32 y_start; U32 x_size; U32 y_size; U16 color; const U16 icon[QT60168_TOUCH_NUMBER_OF_SENSORS] = {0, 1*16, 2*16, 3*16, 4*16, 5*16, -1, -1, 6*16, 7*16, 8*16, 9*16, 10*16, 11*16, -1, -1}; demo_color_t demo_color=DEMO_COLOR_ALL; demo_display_t demo_display=DEMO_DISPLAY_WHEEL; bool touch_states[QT60168_TOUCH_NUMBER_OF_SENSORS]; // Switch the main clock to the external oscillator 0 pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP); // Initialize RS232 debug text output. init_dbg_rs232(FOSC0); // Initialize QT60168 resources: GPIO, SPI and QT60168. qt60168_resources_init(); // Initialize QT60168 component. qt60168_init(FOSC0); // Initialize the LCD. et024006_Init( FOSC0/*CPU*/, FOSC0/*HSB*/); // Clear the display i.e. make it black et024006_DrawFilledRect(0, 0, ET024006_WIDTH, ET024006_HEIGHT, BLACK ); // Set the backlight. gpio_set_gpio_pin(ET024006DHU_BL_PIN); // Display welcome string. et024006_PrintString("QT60168 EXAMPLE", (const unsigned char *)&FONT8x8, 110, 5, WHITE, -1); et024006_PrintString("Press the QTouch sensors.", (const unsigned char *)&FONT6x8, 95, 20, WHITE, -1); et024006_PrintString("Color: All", (const unsigned char *)&FONT6x8, 10, 200, WHITE, -1); et024006_PrintString("Display sensors", (const unsigned char *)&FONT6x8, 120, 200, WHITE, -1); et024006_DrawLine(DEMO_START_X, DEMO_START_Y-1, DEMO_START_X+DEMO_SIZE_X, DEMO_START_Y-1, WHITE ); et024006_DrawLine(DEMO_START_X, DEMO_START_Y+DEMO_SIZE_Y+1, DEMO_START_X+DEMO_SIZE_X, DEMO_START_Y+DEMO_SIZE_Y+1, WHITE ); // Memorize the status for each key. for( i=0 ; i<QT60168_TOUCH_NUMBER_OF_SENSORS ; i++ ) touch_states[i] = qt60168_is_key_pressed(i); // Set LED state in a known state. gpio_set_gpio_pin(LED0_GPIO); gpio_set_gpio_pin(LED1_GPIO); gpio_set_gpio_pin(LED2_GPIO); gpio_set_gpio_pin(LED3_GPIO); while(1) { for( i=0 ; i<QT60168_TOUCH_NUMBER_OF_SENSORS ; i++) { // Test Press event on sensors // if( !touch_states[i] && qt60168_is_key_pressed(i) ) { touch_states[i] = true; if( i==QT60168_TOUCH_SENSOR_BUTTON_0 ) { gpio_tgl_gpio_pin(LED0_GPIO); et024006_PrintString("B0", (const unsigned char *)&FONT6x8, 10, 215, WHITE, -1); demo_color=(demo_color+1) % DEMO_COLOR_MAX; // Erase previous line et024006_DrawFilledRect(10, 200, 80, 10, BLACK ); switch( demo_color ) { case DEMO_COLOR_BLUE: et024006_PrintString("Color: Blue", (const unsigned char *)&FONT6x8, 10, 200, WHITE, -1); break; case DEMO_COLOR_RED: et024006_PrintString("Color: Red", (const unsigned char *)&FONT6x8, 10, 200, WHITE, -1); break; case DEMO_COLOR_GREEN: et024006_PrintString("Color: Green", (const unsigned char *)&FONT6x8, 10, 200, WHITE, -1); break; default: et024006_PrintString("Color: All", (const unsigned char *)&FONT6x8, 10, 200, WHITE, -1); break; } } else if( i==QT60168_TOUCH_SENSOR_BUTTON_1 ) { gpio_tgl_gpio_pin(LED1_GPIO); et024006_PrintString("B1", (const unsigned char *)&FONT6x8, 30, 215, WHITE, -1); demo_display=(demo_display+1) % DEMO_DISPLAY_MAX; // Erase previous line et024006_DrawFilledRect(120, 200, 160, 10, BLACK ); switch( demo_display ) { case DEMO_DISPLAY_WHEEL: et024006_PrintString("Display sensors", (const unsigned char *)&FONT6x8, 120, 200, WHITE, -1); break; case DEMO_DISPLAY_BOXES: default: et024006_PrintString("Display random boxes", (const unsigned char *)&FONT6x8, 120, 200, WHITE, -1); break; } // Erase display et024006_DrawFilledRect(DEMO_START_X, DEMO_START_Y, DEMO_SIZE_X, DEMO_SIZE_Y, BLACK ); } else if( i==QT60168_TOUCH_SENSOR_BUTTON_2 ) { gpio_tgl_gpio_pin(LED2_GPIO); et024006_PrintString("B2", (const unsigned char *)&FONT6x8, 50, 215, WHITE, -1); } else if( i==QT60168_TOUCH_SENSOR_BUTTON_3 ) { gpio_tgl_gpio_pin(LED3_GPIO); et024006_PrintString("B3", (const unsigned char *)&FONT6x8, 70, 215, WHITE, -1); } else { // Press transition detected for the wheel idle = false; // Draw Wheel[i] et024006_DrawFilledRect(100 + icon[i], 215-2, 10, 10, WHITE ); } } // Test Release event on sensors // if(touch_states[i] && !qt60168_is_key_pressed(i)) { touch_states[i] = false; if( i==QT60168_TOUCH_SENSOR_BUTTON_0 ) { // Erase "B0" et024006_DrawFilledRect(10, 215-2, 12, 12, BLACK ); } else if( i==QT60168_TOUCH_SENSOR_BUTTON_1 ) { // Erase "B1" et024006_DrawFilledRect(30, 215-2, 12, 12, BLACK ); } else if( i==QT60168_TOUCH_SENSOR_BUTTON_2 ) { // Erase "B2" et024006_DrawFilledRect(50, 215-2, 12, 12, BLACK ); } else if( i==QT60168_TOUCH_SENSOR_BUTTON_3 ) { // Erase "B3" et024006_DrawFilledRect(70, 215-2, 12, 12, BLACK ); } else { // Erase Wheel[i] et024006_DrawFilledRect(100 + icon[i], 215-2, 10, 10, BLACK ); } } } // for... if( demo_display==DEMO_DISPLAY_WHEEL ) { if( touch_states[QT60168_TOUCH_SENSOR_BUTTON_0] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(30, 50, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_BUTTON_1] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(30, 80, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_BUTTON_2] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(30, 110, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_BUTTON_3] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(30, 140, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_0] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X + DEMO_WHEEL_RADIUS*SIN0, DEMO_WHEEL_START_Y - DEMO_WHEEL_RADIUS*COS0, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_1] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X + DEMO_WHEEL_RADIUS*SIN30, DEMO_WHEEL_START_Y - DEMO_WHEEL_RADIUS*COS30, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_2] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X + DEMO_WHEEL_RADIUS*SIN60, DEMO_WHEEL_START_Y - DEMO_WHEEL_RADIUS*COS60, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_3] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X + DEMO_WHEEL_RADIUS*SIN90, DEMO_WHEEL_START_Y - DEMO_WHEEL_RADIUS*COS90, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_4] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X + DEMO_WHEEL_RADIUS*SIN60, DEMO_WHEEL_START_Y + DEMO_WHEEL_RADIUS*COS60, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_5] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X + DEMO_WHEEL_RADIUS*SIN30, DEMO_WHEEL_START_Y + DEMO_WHEEL_RADIUS*COS30, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_6] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X - DEMO_WHEEL_RADIUS*SIN0, DEMO_WHEEL_START_Y + DEMO_WHEEL_RADIUS*COS0, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_7] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X - DEMO_WHEEL_RADIUS*SIN30, DEMO_WHEEL_START_Y + DEMO_WHEEL_RADIUS*COS30, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_8] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X - DEMO_WHEEL_RADIUS*SIN60, DEMO_WHEEL_START_Y + DEMO_WHEEL_RADIUS*COS60, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_9] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X - DEMO_WHEEL_RADIUS*SIN90, DEMO_WHEEL_START_Y + DEMO_WHEEL_RADIUS*COS90, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_10] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X - DEMO_WHEEL_RADIUS*SIN60, DEMO_WHEEL_START_Y - DEMO_WHEEL_RADIUS*COS60, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_11] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X - DEMO_WHEEL_RADIUS*SIN30, DEMO_WHEEL_START_Y - DEMO_WHEEL_RADIUS*COS30, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); } else if( !idle && ( demo_display==DEMO_DISPLAY_BOXES ) ) { // Display a box randomly on the screen. idle = true; x_start = DEMO_START_X + rand()%DEMO_SIZE_X; y_start = DEMO_START_Y + rand()%DEMO_SIZE_Y; x_size = rand()%(DEMO_START_X+DEMO_SIZE_X-x_start); y_size = rand()%(DEMO_START_Y+DEMO_SIZE_Y-y_start); color = rand()%0x10000; switch( demo_color ) { case DEMO_COLOR_BLUE: color = color & BLUE; break; case DEMO_COLOR_RED: color = color & RED; break; case DEMO_COLOR_GREEN: color = color & GREEN; break; default: break; } et024006_DrawFilledRect( x_start , y_start , x_size , y_size , color ); } } // while(1)... }
void init_LCD(void){ static const gpio_map_t DIP204_SPI_GPIO_MAP = { {DIP204_SPI_SCK_PIN, DIP204_SPI_SCK_FUNCTION }, // SPI Clock. {DIP204_SPI_MISO_PIN, DIP204_SPI_MISO_FUNCTION}, // MISO. {DIP204_SPI_MOSI_PIN, DIP204_SPI_MOSI_FUNCTION}, // MOSI. {DIP204_SPI_NPCS_PIN, DIP204_SPI_NPCS_FUNCTION} // Chip Select NPCS. }; spi_options_t spiOptions = { .reg = DIP204_SPI_NPCS, .baudrate = 1000000, .bits = 8, .spck_delay = 0, .trans_delay = 0, .stay_act = 1, .spi_mode = 0, .modfdis = 1 }; gpio_enable_module(DIP204_SPI_GPIO_MAP, sizeof(DIP204_SPI_GPIO_MAP) / sizeof(DIP204_SPI_GPIO_MAP[0])); spi_initMaster(DIP204_SPI, &spiOptions); spi_selectionMode(DIP204_SPI, 0, 0, 0); spi_enable(DIP204_SPI); spi_setupChipReg(DIP204_SPI, &spiOptions, FOSC0); dip204_init(backlight_PWM, true); clear_Display(); dip204_hide_cursor(); } void init_Potentiometer(void){ const gpio_map_t ADC_GPIO_MAP = { {EXAMPLE_ADC_POTENTIOMETER_PIN, EXAMPLE_ADC_POTENTIOMETER_FUNCTION} }; gpio_enable_module(ADC_GPIO_MAP, sizeof(ADC_GPIO_MAP) / sizeof(ADC_GPIO_MAP[0])); AVR32_ADC.mr |= 0x1 << AVR32_ADC_MR_PRESCAL_OFFSET; adc_configure(&AVR32_ADC); adc_enable(&AVR32_ADC, EXAMPLE_ADC_POTENTIOMETER_CHANNEL); adc_start(&AVR32_ADC); } void init_CurrentSensor(void){ #define AVR32_ADC_AD_1_PIN 22 const gpio_map_t ADC_GPIO_MAP = { {AVR32_ADC_AD_3_PIN, AVR32_ADC_AD_3_FUNCTION} }; gpio_enable_module(ADC_GPIO_MAP, sizeof(ADC_GPIO_MAP) / sizeof(ADC_GPIO_MAP[0])); AVR32_ADC.mr |= 0x1 << AVR32_ADC_MR_PRESCAL_OFFSET; adc_configure(&AVR32_ADC); adc_enable(&AVR32_ADC, 3); adc_start(&AVR32_ADC); } void clear_Line(int line){ for(int i = 0; i<21;i++){ dip204_set_cursor_position(i,line); dip204_write_string(" "); } } void set_Direccion(int direccion){ if(direccion == 1){ clear_Line(2); dip204_set_cursor_position(1,2); dip204_write_string("Direccion:"); dip204_set_cursor_position(12,2); dip204_write_string("Forward"); } if(direccion == 0){ clear_Line(2); dip204_set_cursor_position(1,2); dip204_write_string("Direccion:"); dip204_set_cursor_position(12,2); dip204_write_string("Reverse"); } } void set_Velocidad(int velocidad){ clear_Line(3); dip204_set_cursor_position(1,3); dip204_write_string("Velocidad:"); dip204_set_cursor_position(12,3); switch (velocidad){ case 1: dip204_write_string("1"); break; case 2: dip204_write_string("2"); break; case 3: dip204_write_string("3"); break; case 4: dip204_write_string("4"); break; case 5: dip204_write_string("5"); break; case 6: dip204_write_string("6"); break; case 7: dip204_write_string("7"); break; case 8: dip204_write_string("8"); break; case 9: dip204_write_string("9"); break; case 10: dip204_write_string("10"); break; }//SWITCH }
static void qt60168_resources_init(const pm_freq_param_t *pm_freq_param) { static const gpio_map_t QT60168_SPI_GPIO_MAP = { {QT60168_SPI_SCK_PIN, QT60168_SPI_SCK_FUNCTION }, // SPI Clock. {QT60168_SPI_MISO_PIN, QT60168_SPI_MISO_FUNCTION }, // MISO. {QT60168_SPI_MOSI_PIN, QT60168_SPI_MOSI_FUNCTION }, // MOSI. {QT60168_SPI_NPCS0_PIN, QT60168_SPI_NPCS0_FUNCTION} // Chip Select NPCS. }; // SPI options. spi_options_t spiOptions = { .reg = QT60168_SPI_NCPS, .baudrate = QT60168_SPI_MASTER_SPEED, // Defined in conf_qt60168.h. .bits = QT60168_SPI_BITS, // Defined in conf_qt60168.h. .spck_delay = 0, .trans_delay = 0, .stay_act = 0, .spi_mode = 3, .modfdis = 1 }; // Assign I/Os to SPI. gpio_enable_module(QT60168_SPI_GPIO_MAP, sizeof(QT60168_SPI_GPIO_MAP) / sizeof(QT60168_SPI_GPIO_MAP[0])); #if EXT_BOARD != SPB105 // Initialize as master. spi_initMaster(QT60168_SPI, &spiOptions); // Set selection mode: variable_ps, pcs_decode, delay. spi_selectionMode(QT60168_SPI, 0, 0, 0); // Enable SPI. spi_enable(QT60168_SPI); #endif // Initialize QT60168 with SPI clock Osc0. spi_setupChipReg(QT60168_SPI, &spiOptions, pm_freq_param->cpu_f); } #endif void gui_init(const pm_freq_param_t *pm_freq_param) { #if BOARD == EVK1100 static const gpio_map_t DIP204_SPI_GPIO_MAP = { {DIP204_SPI_SCK_PIN, DIP204_SPI_SCK_FUNCTION }, // SPI Clock. {DIP204_SPI_MISO_PIN, DIP204_SPI_MISO_FUNCTION}, // MISO. {DIP204_SPI_MOSI_PIN, DIP204_SPI_MOSI_FUNCTION}, // MOSI. {DIP204_SPI_NPCS_PIN, DIP204_SPI_NPCS_FUNCTION} // Chip Select NPCS. }; spi_options_t spiOptions = { .reg = DIP204_SPI_NPCS, .baudrate = 1000000, .bits = 8, .spck_delay = 0, .trans_delay = 0, .stay_act = 1, .spi_mode = 3, .modfdis = 1 }; #endif memset(&scroll_box_contents, 0, sizeof scroll_box_contents); memset(&title_contents, 0, sizeof title_contents); memset(&button_contents, 0, sizeof button_contents); memset(&infobox_contents, 0, sizeof infobox_contents); memset(&info_bitmap, 0, sizeof info_bitmap); #if BOARD == EVK1104 // Init touch sensor resources: GPIO, SPI and QT60168. qt60168_resources_init(pm_freq_param); // Initialize QT60168 component. qt60168_init(pm_freq_param->cpu_f); #endif #if BOARD == EVK1100 // Assign I/Os to SPI gpio_enable_module(DIP204_SPI_GPIO_MAP, sizeof(DIP204_SPI_GPIO_MAP) / sizeof(DIP204_SPI_GPIO_MAP[0])); #if 0 // Initialize as master spi_initMaster(DIP204_SPI, &spiOptions); // Set selection mode: variable_ps, pcs_decode, delay spi_selectionMode(DIP204_SPI, 0, 0, 0); // Enable SPI spi_enable(DIP204_SPI); #endif // setup chip registers spi_setupChipReg(DIP204_SPI, &spiOptions, FOSC0); // initialize LCD dip204_init(backlight_PWM, true); dip204_set_cursor_position(1,1); dip204_write_string("http server demo!"); #else // Init display et024006_Init( pm_freq_param->cpu_f, pm_freq_param->cpu_f /*HSB*/); // Turn on the display backlight gpio_set_gpio_pin(ET024006DHU_BL_PIN); #endif mod = 1; } void gui_set_title(const char *str, unsigned char line) { int len; Assert(line < 3); memset(&title_contents.title[line], 0, sizeof title_contents.title[0]); len = strlen(str); if (len >= sizeof title_contents.title[0]) { len = sizeof title_contents.title[0] - 1; } strncpy(title_contents.title[line], str, len); mod = 1; } int gui_set_button(short id, const char *label, size_t len, button_cb_t cb) { if (id >= NUM_BUTTONS) { return 0; } if (len >= sizeof button_contents.labels[id]) { len = sizeof button_contents.labels[id] - 1; } button_contents.cbs[id] = cb; strncpy(button_contents.labels[id], label, len); mod = 1; return 1; } void gui_clear_scroll_box(void) { memset(&scroll_box_contents, 0, sizeof scroll_box_contents); scroll_box_contents.dispstart = 0; }
//Initialize LCD display void init_disp (void) { static const gpio_map_t DIP204_SPI_GPIO_MAP = { {DIP204_SPI_SCK_PIN, DIP204_SPI_SCK_FUNCTION }, // SPI Clock. {DIP204_SPI_MISO_PIN, DIP204_SPI_MISO_FUNCTION}, // MISO. {DIP204_SPI_MOSI_PIN, DIP204_SPI_MOSI_FUNCTION}, // MOSI. {DIP204_SPI_NPCS_PIN, DIP204_SPI_NPCS_FUNCTION} // Chip Select NPCS. }; // add the spi options driver structure for the LCD DIP204 spi_options_t spiOptions = { .reg = DIP204_SPI_NPCS, .baudrate = 1000000, .bits = 8, .spck_delay = 0, .trans_delay = 0, .stay_act = 1, .spi_mode = 0, .modfdis = 1 }; // Assign I/Os to SPI gpio_enable_module(DIP204_SPI_GPIO_MAP, sizeof(DIP204_SPI_GPIO_MAP) / sizeof(DIP204_SPI_GPIO_MAP[0])); // Initialize as master spi_initMaster(DIP204_SPI, &spiOptions); // Set selection mode: variable_ps, pcs_decode, delay spi_selectionMode(DIP204_SPI, 0, 0, 0); // Enable SPI spi_enable(DIP204_SPI); // setup chip registers spi_setupChipReg(DIP204_SPI, &spiOptions, FOSC0); // initialize LCD dip204_init(backlight_IO, true); dip204_hide_cursor(); } int main (void) { // Switch the CPU main clock to oscillator 0 pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP); board_init(); init_disp(); U32 x = 12345678; U32 y = 87654321; U64 z =0; F32 a = 1234.5678; F32 b = 8765.4321; F32 c = 0; U32 calc_time_z = 0; U32 calc_time_c = 0; U32 cnt_1 = 0; U32 cnt_2 = 0; U32 cnt_3 = 0; U32 cnt_res_z = 0; U32 cnt_res_c = 0; char cycl_str_z[9]; char cycl_str_c[9]; char result[19]; char cycl_c[9]; char cycl_z[9]; //Calculation: //Cycle count 1 cnt_1 = Get_sys_count(); //Calculation part 1: z = x*y; //Cycle count 2 cnt_2 = Get_sys_count(); //Calculation part 2: c = a*b; //Cycle count 3 cnt_3 = Get_sys_count(); //Cycle count result cnt_res_z = cnt_2 - cnt_1 ; cnt_res_c = cnt_3 - cnt_2 ; //Use cycle count result to find calculation time calc_time_c = (cnt_res_c * 1000000 + FOSC0 - 1) / FOSC0; calc_time_z = (cnt_res_z * 1000000 + FOSC0 - 1) / FOSC0; //Compose strings for display output sprintf(result, "%f", c); sprintf(cycl_str_z, "%lu", calc_time_z); sprintf(cycl_str_c, "%lu", calc_time_c); sprintf(cycl_c, "%lu", cnt_res_c); sprintf(cycl_z, "%lu", cnt_res_z); //Display calculation time, cycles and multiplication result on monitor dip204_clear_display(); dip204_set_cursor_position(1,1); dip204_write_string("x*y=z"); dip204_set_cursor_position(1,2); dip204_write_string("Time:"); dip204_set_cursor_position(7,2); dip204_write_string(cycl_str_z); dip204_set_cursor_position(1,3); dip204_write_string("Cycles:"); dip204_set_cursor_position(9,3); dip204_write_string(cycl_z); dip204_set_cursor_position(11,1); dip204_write_string("a*b=c"); dip204_set_cursor_position(11,2); dip204_write_string("Time:"); dip204_set_cursor_position(17,2); dip204_write_string(cycl_str_c); dip204_set_cursor_position(11,3); dip204_write_string("Cycles:"); dip204_set_cursor_position(19,3); dip204_write_string(cycl_c); while (1) { } }
void controller_init(uint32_t fcpu_hz, uint32_t fhsb_hz, uint32_t fpbb_hz, uint32_t fpba_hz) { static const gpio_map_t QT60168_SPI_GPIO_MAP = { { QT60168_SPI_SCK_PIN, QT60168_SPI_SCK_FUNCTION }, // SPI Clock. { QT60168_SPI_MISO_PIN, QT60168_SPI_MISO_FUNCTION }, // MISO. { QT60168_SPI_MOSI_PIN, QT60168_SPI_MOSI_FUNCTION }, // MOSI. { QT60168_SPI_NPCS0_PIN, QT60168_SPI_NPCS0_FUNCTION } // Chip Select NPCS. }; // SPI options. spi_options_t spiOptions = { .reg = QT60168_SPI_NCPS, .baudrate = QT60168_SPI_MASTER_SPEED, // Defined in conf_qt60168.h. .bits = QT60168_SPI_BITS, // Defined in conf_qt60168.h. .spck_delay = 0, .trans_delay = 0, .stay_act = 0, .spi_mode = 3, .modfdis = 1 }; // Assign I/Os to SPI. gpio_enable_module(QT60168_SPI_GPIO_MAP, sizeof(QT60168_SPI_GPIO_MAP) / sizeof(QT60168_SPI_GPIO_MAP[0])); // Initialize as master. spi_initMaster(QT60168_SPI, &spiOptions); // Set selection mode: variable_ps, pcs_decode, delay. spi_selectionMode(QT60168_SPI, 0, 0, 0); // Enable SPI. spi_enable(QT60168_SPI); // Initialize QT60168 with SPI clock Osc0. spi_setupChipReg(QT60168_SPI, &spiOptions, fpba_hz); // Initialize QT60168 component. qt60168_init(fpba_hz); rtc_init_qt(); } bool is_joystick_up(void) { return (qwheel_status&JOYSTICK_STATUS_UP)?true:false; } bool is_joystick_down(void) { return (qwheel_status&JOYSTICK_STATUS_DOWN)?true:false; } bool is_joystick_right(void) { return (qwheel_status&JOYSTICK_STATUS_RIGHT)?true:false; } bool is_joystick_left(void) { return (qwheel_status&JOYSTICK_STATUS_LEFT)?true:false; } bool is_joystick_pressed(void) { return (qwheel_status&JOYSTICK_STATUS_PRESSED)?true:false; }