void board_init(void) { #ifdef KEY_RC_BOARD /* On board Button initialization */ ioport_configure_pin(BUTTON_IRQ_PIN_1,IOPORT_DIR_INPUT | IOPORT_PULL_UP); ioport_configure_pin(BUTTON_IRQ_PIN_2,IOPORT_DIR_INPUT | IOPORT_PULL_UP); ioport_configure_pin(BUTTON_IRQ_PIN_3,IOPORT_DIR_INPUT | IOPORT_PULL_UP); set_button_pins_for_normal_mode(); /* Initialize the IRQ lines' interrupt behaviour. */ DISABLE_ALL_BUTTON_IRQS(); /* LED Init */ /* LCD initialization for inactive use */ /* On board LED initialization */ ioport_configure_pin(LCD_CS_ON_BOARD, IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH); ioport_set_port_dir(IOPORT_PORTE,KEY_RC_IO_MASK,IOPORT_DIR_OUTPUT); ioport_set_port_level(IOPORT_PORTE,KEY_RC_IO_MASK,KEY_RC_IO_MASK); ioport_set_pin_dir(IOPORT_CREATE_PIN(PORTG , 2),IOPORT_DIR_INPUT); ioport_set_pin_mode(IOPORT_CREATE_PIN(PORTG , 2), IOPORT_MODE_PULLUP); LATCH_INIT(); /* Init ADC for the Accelerometer */ adc_init(); // LATCH_INIT(); /* Enable Accelerometer by enabling the PWR pin in the Latch */ acc_init(); update_latch_status(); /* Apply latch pulse to set LED status */ pulse_latch(); #else /* To identify if it is a plain or STB*/ board_identify(); /* On board LED initialization */ ioport_configure_pin(LED0_RCB,IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH); ioport_configure_pin(LED1_RCB,IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH); ioport_configure_pin(LED2_RCB,IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH); /* On board Switch initialization */ ioport_configure_pin(GPIO_PUSH_BUTTON_0,IOPORT_DIR_INPUT | IOPORT_PULL_UP); #ifdef BREAKOUT_BOARD //Enable RCB_BB RS232 level converter ioport_set_port_dir(IOPORT_PORTD,BB_SIO_MASK,IOPORT_DIR_OUTPUT); ioport_set_port_level(IOPORT_PORTD,BB_SIO_MASK,BB_SIO_VAL); #endif #endif }
// setup the board instead of board_init() as recommended by ASF.. because christmas lights, that's why. void init (void) { static usart_serial_options_t usart_options = { .baudrate = USART_SERIAL_BAUDRATE, .charlength = USART_SERIAL_CHAR_LENGTH, .paritytype = USART_SERIAL_PARITY, .stopbits = USART_SERIAL_STOP_BIT }; // initialize ASF stuff board_init(); sysclk_init(); ioport_init(); pmic_init(); pmic_set_scheduling(PMIC_SCH_FIXED_PRIORITY); // remap, enable TX, and configure USART on PORT C PORTC.REMAP |= PR_USART0_bm; PORTC.DIR |= (1 << PIN7_bp); sysclk_enable_module(SYSCLK_PORT_C, PR_USART0_bm); usart_init_rs232(USART_SERIAL, &usart_options); // setup timer for PWM tc45_enable(&TCC4); tc45_set_overflow_interrupt_callback(&TCC4, pwm_callback); tc45_set_wgm(&TCC4, TC45_WG_NORMAL); tc45_write_period(&TCC4, 256); tc45_set_overflow_interrupt_level(&TCC4, TC45_INT_LVL_MED); // enable all channels and turn off (high) ioport_set_port_dir(IOPORT_PORTA, PORTA_MASK, IOPORT_DIR_OUTPUT); ioport_set_port_dir(IOPORT_PORTD, PORTD_MASK, IOPORT_DIR_OUTPUT); ioport_set_port_dir(IOPORT_PORTR, PORTR_MASK, IOPORT_DIR_OUTPUT); ioport_set_port_level(IOPORT_PORTA, PORTA_MASK, 0xFF); ioport_set_port_level(IOPORT_PORTD, PORTD_MASK, 0xFF); ioport_set_port_level(IOPORT_PORTR, PORTR_MASK, 0xFF); for (uint8_t i=0; i<NUM_CHANNELS; i++) { compare[i] = 0; compbuff[i] = 0; } // enable status LEDs and turn off ioport_set_pin_dir(LED_STATUS, IOPORT_DIR_OUTPUT); ioport_set_pin_dir(LED_DATA, IOPORT_DIR_OUTPUT); ioport_set_pin_level(LED_STATUS, 1); ioport_set_pin_level(LED_DATA, 1); // enable interrupts and start timer for PWM cpu_irq_enable(); tc45_write_clock_source(&TCC4, TC45_CLKSEL_DIV2_gc); }
uint8_t LCDwrite(uint8_t byte, bool type) /* writes a byte to the LCD display */ /* * writes the byte (8 bits) to the LCD display as two consecutive 4 bits * type = 0 controls the display * type = 1 writes the content of the byte (usually interpreted as ASCII-code) to the display * * LCDwrite returns the actual pinout value on PIOC 26-23, which is the reverse order compared to the Due pin order of D4-D7 */ { /* In it's infinite wisdom the shield has the data pins in mirrored order compared to the Due board */ uint8_t mirror_pin[16]={0b0000,0b1000,0b0100,0b1100,0b0010,0b1010,0b0110,0b1110,0b0001,0b1001,0b0101,0b1101,0b0011,0b1011,0b0111,0b1111}; uint32_t byte32; uint8_t byte_orig; uint8_t mirrored_output = 0; byte_orig = byte; /* write the first 4 bits to the shield. */ byte = byte>>4; byte = mirror_pin[byte]; byte32 = (uint32_t)byte << 23; mirrored_output = byte<<4; ioport_set_pin_level(LCD_RS, type); ioport_set_port_level(IOPORT_PIOC, LCD_mask_D4_D7, byte32); ioport_set_pin_level(LCD_Enable, HIGH); delay(1); ioport_set_pin_level(LCD_Enable, LOW); delay(100); /* write the second 4 bits to the shield. */ byte = byte_orig; byte = byte & 0x0f; byte = mirror_pin[byte]; byte32 = (uint32_t)byte<<23; mirrored_output = mirrored_output + byte; ioport_set_port_level(IOPORT_PIOC, LCD_mask_D4_D7, byte32); ioport_set_pin_level(LCD_Enable, HIGH); delay(1); ioport_set_pin_level(LCD_Enable, LOW); delay(100); return mirrored_output; }
/* * Writes a byte to the LCD display * writes the byte (8 bits) to the LCD display as two consecutive 4 bits * type = 0 controls the display * type = 1 writes the content of the byte (usually interpreted as ASCII-code) to the display * * lcd_write returns the actual pinout value on PIOC 26-23, which is the reverse order compared to the Due pin order of D4-D7 */ uint8_t lcd_write(uint8_t byte, bool type) { uint32_t byte32; uint8_t byte_orig; uint8_t mirrored_output = 0; /* using array - choosing speed before memory space */ uint8_t mirror_pin[16] = {0b0000, 0b1000, 0b0100, 0b1100, 0b0010, 0b1010, 0b0110, 0b1110, 0b0001, 0b1001, 0b0101, 0b1101, 0b0011, 0b1011, 0b0111, 0b1111}; byte_orig = byte; /* write the first 4 bits to the shield. */ byte = byte >> 4; byte = mirror_pin[byte]; byte32 = (uint32_t) (byte << 23); mirrored_output = byte << 4; ioport_set_pin_level(LCD_RS, type); ioport_set_port_level(IOPORT_PIOC, LCD_mask_D4_D7, byte32); ioport_set_pin_level(LCD_Enable, HIGH); delay(1); ioport_set_pin_level(LCD_Enable, LOW); delay(100); /* write the second 4 bits to the shield. */ byte = byte_orig; byte = byte & 0x0f; byte = mirror_pin[byte]; byte32 = (uint32_t) (byte << 23); mirrored_output = mirrored_output + byte; ioport_set_port_level(IOPORT_PIOC, LCD_mask_D4_D7, byte32); ioport_set_pin_level(LCD_Enable, HIGH); delay(1); ioport_set_pin_level(LCD_Enable, LOW); delay(100); return mirrored_output; }
/** * \brief Test IOPORT port level is getting changed. * * This function set the direction of CONF_PORT_OUT_PIN_MASK to output mode with * pull-up enabled and read the status of pin using CONF_PORT_IN_PIN_MASK which * is configured in input mode. * The pin CONF_PORT_OUT_PIN_MASK and CONF_PORT_IN_PIN_MASK are shorted using a * jumper. * * \param test Current test case. */ static void run_ioport_port_test(const struct test_case *test) { static volatile pin_mask_t port_val; /* Set direction and pull-up on the given IOPORT */ ioport_set_port_dir(CONF_PORT, CONF_PORT_IN_PIN_MASK, IOPORT_DIR_INPUT); ioport_set_port_mode(CONF_PORT, CONF_PORT_IN_PIN_MASK, IOPORT_MODE_PULLUP); /* Set output direction on the given IOPORT */ ioport_set_port_dir(CONF_PORT, CONF_PORT_OUT_PIN_MASK, IOPORT_DIR_OUTPUT); /* Set IOPORT as high */ ioport_set_port_level(CONF_PORT, CONF_PORT_OUT_PIN_MASK, IOPORT_PIN_LEVEL_HIGH); delay_ms(10); port_val = ioport_get_port_level(CONF_PORT, CONF_PORT_IN_PIN_MASK); test_assert_true(test, port_val == CONF_PORT_IN_PIN_MASK, "IOPORT Set port level high test failed."); /* Set IOPORT as low */ ioport_set_port_level(CONF_PORT, CONF_PORT_OUT_PIN_MASK, IOPORT_PIN_LEVEL_LOW); delay_ms(10); port_val = ioport_get_port_level(CONF_PORT, CONF_PORT_IN_PIN_MASK); test_assert_true(test, port_val == 0, "IOPORT Set port level low test failed."); /* Toggle IOPORT */ ioport_toggle_port_level(CONF_PORT, CONF_PORT_OUT_PIN_MASK); delay_ms(10); port_val = ioport_get_port_level(CONF_PORT, CONF_PORT_IN_PIN_MASK); test_assert_true(test, port_val == CONF_PORT_IN_PIN_MASK, "IOPORT Set port level toggle test failed."); }
int main(void) { /* Use static volatile to make it available in debug watch */ static volatile ioport_port_mask_t port_val; sysclk_init(); board_init(); ioport_init(); delay_init(sysclk_get_cpu_hz()); /* Set output direction on the given LED IOPORTs */ ioport_set_port_dir(EXAMPLE_LED_PORT, EXAMPLE_LED_MASK, IOPORT_DIR_OUTPUT); #ifdef EXAMPLE_BUTTON_PORT /* Set direction and pullup on the given button IOPORT */ ioport_set_port_dir(EXAMPLE_BUTTON_PORT, EXAMPLE_BUTTON_MASK, IOPORT_DIR_INPUT); ioport_set_port_mode(EXAMPLE_BUTTON_PORT, EXAMPLE_BUTTON_MASK, IOPORT_MODE_PULLUP); /* Set LED IOPORTs high */ ioport_set_port_level(EXAMPLE_LED_PORT, EXAMPLE_LED_MASK, IOPORT_PIN_LEVEL_HIGH); #endif while (true) { /* Toggle LED IOPORTs with half a second interval */ ioport_toggle_port_level(EXAMPLE_LED_PORT, EXAMPLE_LED_MASK); delay_ms(500); #ifdef EXAMPLE_BUTTON_PORT /* Get value from button port */ /* Use watch with debugger to see it */ port_val = ioport_get_port_level(EXAMPLE_BUTTON_PORT, EXAMPLE_BUTTON_MASK); #endif } }
void board_init(void) { #ifndef CONF_BOARD_KEEP_WATCHDOG_AT_INIT struct wdt_dev_inst wdt_inst; struct wdt_config wdt_cfg; wdt_get_config_defaults(&wdt_cfg); wdt_init(&wdt_inst, WDT, &wdt_cfg); wdt_disable(&wdt_inst); #endif /* Initialize IOPORT */ ioport_init(); /* Initialize LEDs, turned off */ ioport_set_port_dir(LED_PORT, LED_MASK, IOPORT_DIR_OUTPUT); ioport_set_port_level(LED_PORT, LED_MASK, LED_INACTIVE); ioport_set_port_mode(LED_PORT, LED_MASK, IOPORT_MODE_DRIVE_STRENGTH); /* Initialize SW0 */ ioport_set_pin_dir(BUTTON_0_PIN, IOPORT_DIR_INPUT); ioport_set_pin_mode(BUTTON_0_PIN, IOPORT_MODE_PULLUP); #ifdef CONF_BOARD_EIC /* Set push button as external interrupt pin */ ioport_set_pin_peripheral_mode(BUTTON_0_EIC_PIN, BUTTON_0_EIC_PIN_MUX | IOPORT_MODE_PULLUP); #else /* Push button as input: already done, it's the default pin state */ #endif #if defined (CONF_BOARD_COM_PORT) ioport_set_pin_peripheral_mode(COM_PORT_RX_PIN, COM_PORT_RX_MUX); ioport_set_pin_peripheral_mode(COM_PORT_TX_PIN, COM_PORT_TX_MUX); #endif #ifdef CONF_BOARD_TWIMS0 ioport_set_pin_peripheral_mode(PIN_PA23B_TWIMS0_TWD, MUX_PA23B_TWIMS0_TWD); ioport_set_pin_peripheral_mode(PIN_PA24B_TWIMS0_TWCK, MUX_PA24B_TWIMS0_TWCK); #endif #ifdef CONF_BOARD_TWIMS3 ioport_set_pin_peripheral_mode(PIN_PB14C_TWIMS3_TWD, MUX_PB14C_TWIMS3_TWD); ioport_set_pin_peripheral_mode(PIN_PB15C_TWIMS3_TWCK, MUX_PB15C_TWIMS3_TWCK); #endif #ifdef CONF_BOARD_USART0 ioport_set_pin_peripheral_mode(EXT1_PIN_UART_RX, EXT1_UART_RX_MUX); ioport_set_pin_peripheral_mode(EXT1_PIN_UART_TX, EXT1_UART_TX_MUX); #endif #if (defined CONF_BOARD_USB_PORT) ioport_set_pin_peripheral_mode(PIN_PA25A_USBC_DM, MUX_PA25A_USBC_DM); ioport_set_pin_peripheral_mode(PIN_PA26A_USBC_DP, MUX_PA26A_USBC_DP); # if defined(CONF_BOARD_USB_VBUS_DETECT) ioport_set_pin_dir(USB_VBUS_PIN, IOPORT_DIR_INPUT); # endif # if defined(CONF_BOARD_USB_ID_DETECT) ioport_set_pin_dir(USB_ID_PIN, IOPORT_DIR_INPUT); # endif # if defined(CONF_BOARD_USB_VBUS_CONTROL) ioport_set_pin_dir(USB_VBOF_PIN, IOPORT_DIR_OUTPUT); ioport_set_pin_level(USB_VBOF_PIN, USB_VBOF_INACTIVE_LEVEL); # endif #endif #if defined(CONF_BOARD_SPI) || defined(CONF_BOARD_SD_MMC_SPI) ioport_set_pin_peripheral_mode(PIN_PA21A_SPI_MISO, MUX_PA21A_SPI_MISO); ioport_set_pin_peripheral_mode(PIN_PA22A_SPI_MOSI, MUX_PA22A_SPI_MOSI); ioport_set_pin_peripheral_mode(PIN_PC30B_SPI_SCK, MUX_PC30B_SPI_SCK); #ifdef CONF_BOARD_SD_MMC_SPI /* Setting SD detection pin */ ioport_set_pin_dir(SD_MMC_0_CD_GPIO, IOPORT_DIR_INPUT); ioport_set_pin_mode(SD_MMC_0_CD_GPIO, IOPORT_MODE_PULLUP); /* Setting SD CS pin */ ioport_set_pin_peripheral_mode(SPI_NPCS0_GPIO, SPI_NPCS0_FLAGS); #endif #ifdef CONF_BOARD_SPI_NPCS0 ioport_set_pin_peripheral_mode(PIN_PC03A_SPI_NPCS0, MUX_PC03A_SPI_NPCS0); #endif #ifdef CONF_BOARD_SPI_NPCS1 ioport_set_pin_peripheral_mode(PIN_PB13B_SPI_NPCS1, MUX_PB13B_SPI_NPCS1); #endif #ifdef CONF_BOARD_SPI_NPCS2 ioport_set_pin_peripheral_mode(PIN_PB11B_SPI_NPCS2, MUX_PB11B_SPI_NPCS2); #endif #endif #ifdef CONF_BOARD_DACC_VOUT ioport_set_pin_peripheral_mode(DACC_VOUT_PIN, DACC_VOUT_MUX); #endif #ifdef CONF_BOARD_ACIFC ioport_set_pin_peripheral_mode(PIN_PA06E_ACIFC_ACAN0, MUX_PA06E_ACIFC_ACAN0); ioport_set_pin_peripheral_mode(PIN_PA07E_ACIFC_ACAP0, MUX_PA07E_ACIFC_ACAP0); #endif }
void port_write(port_t *obj, int value) { MBED_ASSERT(obj); ioport_set_port_level(obj->port, obj->mask, value); }