/*FUNCTION********************************************************************** * * Function Name : GPIO_DRV_WritePinOutput * Description : Set output level of individual GPIO pin to logic 1 or 0. * *END**************************************************************************/ void GPIO_DRV_WritePinOutput(uint32_t pinName, uint32_t output) { uint32_t gpioBaseAddr = g_gpioBaseAddr[GPIO_EXTRACT_PORT(pinName)]; uint32_t pin = GPIO_EXTRACT_PIN(pinName); GPIO_HAL_WritePinOutput(gpioBaseAddr, pin, output); }
/*FUNCTION********************************************************************** * * Function Name : GPIO_DRV_SetPinDir * Description : Set current direction of individual GPIO pin. * *END**************************************************************************/ void GPIO_DRV_SetPinDir(uint32_t pinName, gpio_pin_direction_t direction) { uint32_t gpioBaseAddr = g_gpioBaseAddr[GPIO_EXTRACT_PORT(pinName)]; uint32_t pin = GPIO_EXTRACT_PIN(pinName); GPIO_HAL_SetPinDir(gpioBaseAddr, pin, direction); }
void hardware_init(void) { uint8_t i; /* enable clock for PORTs */ for (i = 0; i < PORT_INSTANCE_COUNT; i++) { CLOCK_SYS_EnablePortClock(i); } #if (SD_CARD_APP) #if ((defined TWR_K64F120M) || (defined FRDM_K64F) || (defined TWR_K60D100M) || (defined TWR_K21F120M) || (defined TWR_K65F180M)) /* configure detect pin as gpio (alt1) */ uint32_t port = GPIO_EXTRACT_PORT(sdhcCdPin[0].pinName); uint32_t pin = GPIO_EXTRACT_PIN(sdhcCdPin[0].pinName); PORT_Type * portBase = g_portBase[port]; PORT_HAL_SetMuxMode(portBase, pin, kPortMuxAsGpio); configure_sdhc_pins(BOARD_SDHC_INSTANCE); #endif #endif /* Init board clock */ BOARD_ClockInit(); dbg_uart_init(); }
// handler associated to SW1 (labeled SW2 on board) void SW1_Intr_Handler(void) { static uint32_t c_ifsr; // port c interrupt flag status register uint32_t c_portBaseAddr = g_portBaseAddr[GPIO_EXTRACT_PORT(kGpioSW1)]; uint32_t portPinMask = (1 << GPIO_EXTRACT_PIN(kGpioSW1)); CPU_CRITICAL_ENTER(); // enter critical section (disable interrupts) OSIntEnter(); // notify to scheduler the beginning of an ISR ("This allows ?C/OS-III to keep track of interrupt nesting") c_ifsr = PORT_HAL_GetPortIntFlag(c_portBaseAddr); // get intr flag reg related to port C if( (c_ifsr & portPinMask) ) // check if kGpioSW1 generated the interrupt [pin 6 -> 7th flag (flags start with index 0)] { //sem_sw1_post OSSemPost(&MySem1, OS_OPT_POST_1 + OS_OPT_POST_NO_SCHED, &os_err); } GPIO_DRV_ClearPinIntFlag( kGpioSW1 ); CPU_CRITICAL_EXIT(); // renable interrupts OSIntExit(); /* notify to scheduler the end of an ISR ("determines if a higher priority task is ready-to-run. If so, the interrupt returns to the higher priority task instead of the interrupted task.") */ }
/*FUNCTION********************************************************************** * * Function Name : GPIO_DRV_ClearPinIntFlag * Description : Clear individual GPIO pin interrupt status flag. * *END**************************************************************************/ void GPIO_DRV_ClearPinIntFlag(uint32_t pinName) { PORT_Type * portBase = g_portBase[GPIO_EXTRACT_PORT(pinName)]; uint32_t pin = GPIO_EXTRACT_PIN(pinName); PORT_HAL_ClearPinIntFlag(portBase, pin); }
/*FUNCTION********************************************************************** * * Function Name : GPIO_DRV_OutputPinInit * Description : Initialize one GPIO output pin used by board. * *END**************************************************************************/ void GPIO_DRV_OutputPinInit(const gpio_output_pin_user_config_t *outputPin) { /* Get actual port and pin number.*/ uint32_t port = GPIO_EXTRACT_PORT(outputPin->pinName); uint32_t pin = GPIO_EXTRACT_PIN(outputPin->pinName); GPIO_Type * gpioBase = g_gpioBase[port]; PORT_Type * portBase = g_portBase[port]; /* Un-gate port clock*/ CLOCK_SYS_EnablePortClock(port); /* Set current pin as gpio.*/ PORT_HAL_SetMuxMode(portBase, pin, kPortMuxAsGpio); /* Set current pin as digital output.*/ GPIO_HAL_SetPinDir(gpioBase, pin, kGpioDigitalOutput); /* Configure GPIO output features. */ GPIO_HAL_WritePinOutput(gpioBase, pin, outputPin->config.outputLogic); #if FSL_FEATURE_PORT_HAS_SLEW_RATE PORT_HAL_SetSlewRateMode(portBase, pin, outputPin->config.slewRate); #endif #if FSL_FEATURE_PORT_HAS_DRIVE_STRENGTH PORT_HAL_SetDriveStrengthMode(portBase, pin, outputPin->config.driveStrength); #endif #if FSL_FEATURE_PORT_HAS_OPEN_DRAIN PORT_HAL_SetOpenDrainCmd(portBase, pin, outputPin->config.isOpenDrainEnabled); #endif }
/*FUNCTION********************************************************************** * * Function Name : GPIO_DRV_SetDigitalFilterCmd * Description : Enable or disable digital filter in one single port. * *END**************************************************************************/ void GPIO_DRV_SetDigitalFilterCmd(uint32_t pinName, bool isDigitalFilterEnabled) { PORT_Type * portBase = g_portBase[GPIO_EXTRACT_PORT(pinName)]; uint32_t pin = GPIO_EXTRACT_PIN(pinName); PORT_HAL_SetDigitalFilterCmd(portBase, pin, isDigitalFilterEnabled); }
/*FUNCTION********************************************************************** * * Function Name : GPIO_DRV_IsPinIntPending * Description : Read the individual pin-interrupt status flag. * *END**************************************************************************/ bool GPIO_DRV_IsPinIntPending(uint32_t pinName) { PORT_Type * portBase = g_portBase[GPIO_EXTRACT_PORT(pinName)]; uint32_t pin = GPIO_EXTRACT_PIN(pinName); return PORT_HAL_IsPinIntPending(portBase, pin); }
/*FUNCTION********************************************************************** * * Function Name : GPIO_DRV_TogglePinOutput * Description : Reverse current output logic of individual GPIO pin. * *END**************************************************************************/ void GPIO_DRV_TogglePinOutput(uint32_t pinName) { GPIO_Type * gpioBase = g_gpioBase[GPIO_EXTRACT_PORT(pinName)]; uint32_t pin = GPIO_EXTRACT_PIN(pinName); GPIO_HAL_TogglePinOutput(gpioBase, pin); }
/*FUNCTION********************************************************************** * * Function Name : GPIO_DRV_ReadPinInput * Description : Read current input value of individual GPIO pin. * *END**************************************************************************/ uint32_t GPIO_DRV_ReadPinInput(uint32_t pinName) { GPIO_Type * gpioBase = g_gpioBase[GPIO_EXTRACT_PORT(pinName)]; uint32_t pin = GPIO_EXTRACT_PIN(pinName); return GPIO_HAL_ReadPinInput(gpioBase, pin); }
/*FUNCTION********************************************************************** * * Function Name : GPIO_DRV_WritePinOutput * Description : Set output level of individual GPIO pin to logic 1 or 0. * *END**************************************************************************/ void GPIO_DRV_WritePinOutput(uint32_t pinName, uint32_t output) { GPIO_Type * gpioBase = g_gpioBase[GPIO_EXTRACT_PORT(pinName)]; uint32_t pin = GPIO_EXTRACT_PIN(pinName); GPIO_HAL_WritePinOutput(gpioBase, pin, output); }
/*FUNCTION********************************************************************** * * Function Name : GPIO_DRV_SetPinDir * Description : Set current direction of individual GPIO pin. * *END**************************************************************************/ void GPIO_DRV_SetPinDir(uint32_t pinName, gpio_pin_direction_t direction) { GPIO_Type * gpioBase = g_gpioBase[GPIO_EXTRACT_PORT(pinName)]; uint32_t pin = GPIO_EXTRACT_PIN(pinName); GPIO_HAL_SetPinDir(gpioBase, pin, direction); }
/*FUNCTION********************************************************************** * * Function Name : GPIO_DRV_GetPinDir * Description : Get current direction of individual GPIO pin. * *END**************************************************************************/ gpio_pin_direction_t GPIO_DRV_GetPinDir(uint32_t pinName) { GPIO_Type * gpioBase = g_gpioBase[GPIO_EXTRACT_PORT(pinName)]; uint32_t pin = GPIO_EXTRACT_PIN(pinName); return GPIO_HAL_GetPinDir(gpioBase, pin); }
/*FUNCTION********************************************************************** * * Function Name : GPIO_DRV_GetPinDir * Description : Get current direction of individual GPIO pin. * *END**************************************************************************/ gpio_pin_direction_t GPIO_DRV_GetPinDir(uint32_t pinName) { uint32_t gpioBaseAddr = g_gpioBaseAddr[GPIO_EXTRACT_PORT(pinName)]; uint32_t pin = GPIO_EXTRACT_PIN(pinName); return GPIO_HAL_GetPinDir(gpioBaseAddr, pin); }
/*! * @brief gpio IRQ handler with the same name in startup code */ void BOARD_SDHC_CD_GPIO_IRQ_HANDLER(void) { PORT_Type * gpioBase = g_portBase[GPIO_EXTRACT_PORT(kGpioSdhc0Cd)]; uint32_t pin = GPIO_EXTRACT_PIN(kGpioSdhc0Cd); if(PORT_HAL_GetPortIntFlag(gpioBase) == (1 << pin)) { sdhc_card_detection(); } /* Clear interrupt flag.*/ PORT_HAL_ClearPortIntFlag(gpioBase); }
/*! * @brief gpio IRQ handler with the same name in startup code */ void SDCARD_CD_GPIO_IRQ_HANDLER(void) { PORT_Type * gpioBase = g_portBase[GPIO_EXTRACT_PORT(kGpioSdcardCardDetection)]; uint32_t pin = GPIO_EXTRACT_PIN(kGpioSdcardCardDetection); if(PORT_HAL_GetPortIntFlag(gpioBase) == (1 << pin)) { spiSDcard_card_detection(); } /* Clear interrupt flag.*/ PORT_HAL_ClearPortIntFlag(gpioBase); }
/*FUNCTION********************************************************************** * * Function Name : GPIO_DRV_InputPinInit * Description : Initialize one GPIO input pin used by board. * *END**************************************************************************/ void GPIO_DRV_InputPinInit(const gpio_input_pin_user_config_t *inputPin) { /* Get actual port and pin number.*/ uint32_t port = GPIO_EXTRACT_PORT(inputPin->pinName); uint32_t pin = GPIO_EXTRACT_PIN(inputPin->pinName); GPIO_Type * gpioBase = g_gpioBase[port]; PORT_Type * portBase = g_portBase[port]; /* Un-gate port clock*/ CLOCK_SYS_EnablePortClock(port); /* Set current pin as gpio.*/ PORT_HAL_SetMuxMode(portBase, pin, kPortMuxAsGpio); /* Set current pin as digital input.*/ GPIO_HAL_SetPinDir(gpioBase, pin, kGpioDigitalInput); /* Configure GPIO input features. */ #if FSL_FEATURE_PORT_HAS_PULL_ENABLE PORT_HAL_SetPullCmd(portBase, pin, inputPin->config.isPullEnable); #endif #if FSL_FEATURE_PORT_HAS_PULL_SELECTION PORT_HAL_SetPullMode(portBase, pin, inputPin->config.pullSelect); #endif #if FSL_FEATURE_PORT_HAS_PASSIVE_FILTER PORT_HAL_SetPassiveFilterCmd(portBase, pin, inputPin->config.isPassiveFilterEnabled); #endif #if FSL_FEATURE_PORT_HAS_DIGITAL_FILTER PORT_HAL_SetDigitalFilterCmd(portBase, pin, inputPin->config.isDigitalFilterEnabled); #endif #if FSL_FEATURE_GPIO_HAS_INTERRUPT_VECTOR PORT_HAL_SetPinIntMode(portBase, pin, inputPin->config.interrupt); /* Configure NVIC */ if ((inputPin->config.interrupt) && (g_portIrqId[port])) { /* Enable GPIO interrupt.*/ INT_SYS_EnableIRQ(g_portIrqId[port]); } #endif }
/*FUNCTION********************************************************************** * * Function Name : gpio_output_pin_init * Description : Initialize one GPIO output pin used by board. * *END**************************************************************************/ void gpio_output_pin_init(const gpio_output_pin_user_config_t *outputPin) { /* Get actual port and pin number.*/ uint32_t gpioInstance = GPIO_EXTRACT_PORT(outputPin->pinName); uint32_t pin = GPIO_EXTRACT_PIN(outputPin->pinName); /* Un-gate port clock*/ clock_manager_set_gate(kClockModulePORT, gpioInstance, true); /* Set current pin as digital output.*/ gpio_hal_set_pin_direction(gpioInstance, pin, kGpioDigitalOutput); /* Configure GPIO output features. */ gpio_hal_write_pin_output(gpioInstance, pin, outputPin->config.outputLogic); port_hal_configure_slew_rate(gpioInstance, pin, outputPin->config.slewRate); port_hal_configure_drive_strength(gpioInstance, pin, outputPin->config.driveStrength); #if FSL_FEATURE_PORT_HAS_OPEN_DRAIN port_hal_configure_open_drain(gpioInstance, pin, outputPin->config.isOpenDrainEnabled); #endif }
/*TASK*----------------------------------------------------- * * Task Name : sdcard_task * Comments : Open device and install MFS on device * * *END*-----------------------------------------------------*/ void sdcard_task(void) { /* Body */ int dev_fd = -1, a_fd = -1; bool inserted = FALSE, last = FALSE; int32_t error_code; uint32_t isFormatted = 0; int esdhc_handle = -1; char partman_name[] = "pm:"; char partition_name[] = "pm:1"; int partition_handle; SDCARD_INIT_STRUCT sdcard_init_data; LWSEM_STRUCT lwsem; /* Create and post card detection semaphore */ if( _lwsem_create(&lwsem, 0) != MQX_OK ){ printf("\nlwsem create failed!"); _task_block(); } if( _lwsem_post(&lwsem) != MQX_OK ){ printf("\nlwsem post failed!"); _task_block(); } #ifdef BOARD_SDHC_GPIO_INSTANCE configure_gpio_pins(GPIO_EXTRACT_PORT(BOARD_SDHC_GPIO_INSTANCE)); #endif /* initialize SDHC pins */ configure_sdhc_pins(BOARD_SDHC_INSTANCE); /* install SDHC low-level driver */ _nio_dev_install("esdhc:", &nio_esdhc_dev_fn, (void*)&_bsp_esdhc_init, NULL); /* get an instance of the SDHC driver */ esdhc_handle = open("esdhc:", 0); if(esdhc_handle < 0) { printf("\nCould not open esdhc!"); _task_block(); } /* prepare init data structure */ sdcard_init_data.com_dev = esdhc_handle; sdcard_init_data.const_data = (SDCARD_CONST_INIT_STRUCT_PTR)&sdcard_esdhc_init_data; /* install device */ if (_nio_dev_install("sdcard:", &nio_sdcard_dev_fn, (void*)&sdcard_init_data, NULL) == NULL) { /* Number of sectors is returned by ioctl IO_IOCTL_GET_NUM_SECTORS function */ /* If another disc structure is desired, use MFS_FORMAT_DATA structure to */ /* define it and call standart format function instead default_format */ printf("\nError installing memory device"); _task_block(); } /* Endif */ /* install isr for card detection handling and initialize gpio pin */ _int_install_isr(g_portIrqId[GPIO_EXTRACT_PORT(CDET_PIN->pinName)], card_detect_isr, &lwsem); GPIO_DRV_InputPinInit(CDET_PIN); for(;;){ /* Wait for card insertion or removal */ if( _lwsem_wait(&lwsem) != MQX_OK ){ printf("\nlwsem_wait failed!"); _task_block(); } /* Check if card is present */ if(CDET_PIN->config.pullSelect == kPortPullDown) inserted = GPIO_DRV_ReadPinInput(CDET_PIN->pinName); else inserted = !GPIO_DRV_ReadPinInput(CDET_PIN->pinName); if(last != inserted){ last = inserted; /* Card detection switch debounce delay */ _time_delay(100); if(inserted) { /* Open the device which MFS will be installed on */ dev_fd = open("sdcard:", O_RDWR); if ( dev_fd <= 0 ) { printf("\nUnable to open SDCARD device"); _task_block(); } /* Endif */ /* Install partition manager over SD card driver */ error_code = _io_part_mgr_install(dev_fd, partman_name, 0); if (error_code != MFS_NO_ERROR) { printf("Error installing partition manager: %s\n", MFS_Error_text((uint32_t)error_code)); _task_block(); } /* Open partition */ partition_handle = open(partition_name, O_RDWR); if (partition_handle >= 0) { printf("Installing MFS over partition...\n"); /* Validate partition */ error_code = ioctl(partition_handle, IO_IOCTL_VAL_PART, NULL); if (error_code != MFS_NO_ERROR) { printf("Error validating partition: %s\n", MFS_Error_text((uint32_t)error_code)); printf("Not installing MFS.\n"); _task_block(); } /* Install MFS over partition */ error_code = _io_mfs_install(partition_handle, "a:", 0); if (error_code != MFS_NO_ERROR) { printf("Error initializing MFS over partition: %s\n", MFS_Error_text((uint32_t)error_code)); } } else { printf("Installing MFS over SD card driver...\n"); /* Install MFS over SD card driver */ error_code = _io_mfs_install(dev_fd, "a:", (_file_size)0); if (error_code != MFS_NO_ERROR) { printf("Error initializing MFS: %s\n", MFS_Error_text((uint32_t)error_code)); } } /* Open the filesystem and format detect, if format is required */ a_fd = open("a:", O_RDWR); if (0 >= a_fd) { printf("\nError while opening a:\\ (%s)", MFS_Error_text((uint32_t)errno)); _task_block(); } _io_register_file_system(a_fd, "a:"); /* We check if the device is formatted with the ioctl command. */ error_code = ioctl(a_fd, IO_IOCTL_CHECK_FORMATTED, &isFormatted); if (0 > error_code) { printf("\nError while accessing a:\\ (%s)", MFS_Error_text((uint32_t)error_code)); _task_block(); } else{ if(!isFormatted){ printf("\nNOT A DOS DISK! You must format to continue."); } } } else { /* Close the filesystem */ if ((a_fd >= 0) && (MQX_OK != close(a_fd))) { printf("Error closing filesystem.\n"); } a_fd = -1; /* Force uninstall filesystem */ error_code = _nio_dev_uninstall_force("a:", NULL); if (error_code < 0) { printf("Error uninstalling filesystem.\n"); } /* Close partition */ if ((partition_handle >= 0) && (MQX_OK != close(partition_handle))) { printf("Error closing partition.\n"); } partition_handle = -1; /* Uninstall partition manager */ error_code = _nio_dev_uninstall_force(partman_name, NULL); if (error_code < 0) { printf("Error uninstalling partition manager.\n"); } /* Close the SD card device */ if ((dev_fd >= 0) && (MQX_OK != close(dev_fd))) { printf("Error closing SD card device.\n"); } dev_fd = -1; printf ("SD card uninstalled.\n"); } } } } /* Endbody */
/***************************************************************************** * EEPROM_Init * * Initializes the EEPROM peripheral * *****************************************************************************/ ee_err_t EEPROM_Init(void) { ee_err_t retval; spiBusConfig_t spiConfig = { .bitsPerSec = 2000000, .master = TRUE, .clkActiveHigh = TRUE, .clkPhaseFirstEdge = TRUE, .MsbFirst = TRUE }; #if gEepromWriteEnable_d uint32_t i; // Mark Flash as Unerased for(i = 0; i < 64; i++) mEepromEraseBitmap[i] = 0; #endif if(Spi_Init(gEepromSpiInstance_c, &mEepromSpiState, NULL, NULL) != spiSuccess) { return ee_error; } if(Spi_Configure(gEepromSpiInstance_c, &spiConfig) != spiSuccess) { return ee_error; } GPIO_DRV_OutputPinInit(&mEepromSpiCsCfg); PORT_HAL_SetMuxMode(g_portBase[GPIO_EXTRACT_PORT(gEepromSpiCsPin_d)], GPIO_EXTRACT_PIN(gEepromSpiCsPin_d), kPortMuxAsGpio); gEepromDeassertCS_d(); return ee_ok; } /***************************************************************************** * EEPROM_ChipErase * * Erase all memory to 0xFF * *****************************************************************************/ ee_err_t EEPROM_ChipErase(void) { // make sure the write process is ready while(EEPROM_GetWriteReady() != ee_ok); return EEPROM_SendCmd(EEPROM_CMD_ERASE_BULK, EEPROM_CMD_END); } /***************************************************************************** * EEPROM_EraseBlock * * Erase a block of memory to 0xFF * *****************************************************************************/ ee_err_t EEPROM_EraseBlock(uint32_t Addr, uint32_t size) { ee_err_t status = ee_ok; if(size != EEPROM_SECTOR_SIZE) { return ee_error; } // make sure the write process is ready while(EEPROM_GetWriteReady() != ee_ok); // send the command status |= EEPROM_SendCmd( EEPROM_CMD_ERASE_SECTOR, EEPROM_CMD_CNT ); // send the address status |= EEPROM_SendAddress(Addr); if (status == ee_ok) { gEepromDeassertCS_d(); return ee_ok; } else { return ee_error; } }