static BT_HANDLE uart_probe(const BT_INTEGRATED_DEVICE *pDevice, BT_ERROR *pError) { BT_ERROR Error = BT_ERR_NONE; BT_HANDLE hUart = NULL; const BT_RESOURCE *pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_ENUM, 0); if(!pResource) { Error = BT_ERR_NO_MEMORY; goto err_free_out; } if (g_USART_HANDLES[pResource->ulStart]){ Error = BT_ERR_GENERIC; goto err_free_out; } hUart = BT_CreateHandle(&oHandleInterface, sizeof(struct _BT_OPAQUE_HANDLE), pError); if(!hUart) { goto err_out; } g_USART_HANDLES[pResource->ulStart] = hUart; hUart->pDevice = pDevice; pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_MEM, 0); if(!pResource) { Error = BT_ERR_NO_MEMORY; goto err_free_out; } hUart->pRegs = (LM3Sxx_UART_REGS *) pResource->ulStart; uartSetPowerState(hUart, BT_POWER_STATE_AWAKE); // Reset all registers to their defaults! ResetUart(hUart); pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_IRQ, 0); if(!pResource) { Error = BT_ERR_GENERIC; goto err_free_out; } /* On NVIC we don't need to register interrupts, LINKER has patched vector for us * Error = BT_RegisterInterrupt(pResource->ulStart, uart_irq_handler, hUart); if(Error) { goto err_free_out; }*/ BT_SetInterruptPriority(pResource->ulStart, ((0x01 << BT_CONFIG_MACH_PRIORITY_BITS)-1)); Error = BT_EnableInterrupt(pResource->ulStart); return hUart; err_free_out: BT_DestroyHandle(hUart); err_out: if(pError) { *pError = Error; } return NULL; }
static BT_HANDLE timer_probe(const BT_INTEGRATED_DEVICE *pDevice, BT_ERROR *pError) { BT_ERROR Error = BT_ERR_NONE; BT_HANDLE hTimer = NULL; const BT_RESOURCE *pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_ENUM, 0); if(!pResource) { Error = BT_ERR_NO_MEMORY; goto err_free_out; } if (g_TIMER_HANDLES[pResource->ulStart]){ Error = BT_ERR_GENERIC; goto err_free_out; } hTimer = BT_CreateHandle(&oHandleInterface, sizeof(struct _BT_OPAQUE_HANDLE), pError); if(!hTimer) { goto err_out; } g_TIMER_HANDLES[pResource->ulStart] = hTimer; hTimer->pDevice = pDevice; pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_MEM, 0); if(!pResource) { Error = BT_ERR_NO_MEMORY; goto err_free_out; } hTimer->pRegs = (LM3Sxx_TIMER_REGS *) pResource->ulStart; TimerSetPowerState(hTimer, BT_POWER_STATE_AWAKE); ResetTimer(hTimer); pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_IRQ, 0); if(!pResource) { Error = BT_ERR_GENERIC; goto err_free_out; } /* On NVIC we don't need to register interrupts, LINKER has patched vector for us * Error = BT_RegisterInterrupt(pResource->ulStart, timer_irq_handler, hTimer); if(Error) { goto err_free_out; }*/ Error = BT_EnableInterrupt(pResource->ulStart); return hTimer; err_free_out: BT_kFree(hTimer); err_out: if(pError) { *pError = Error; } return NULL; }
static BT_HANDLE can_probe(const BT_INTEGRATED_DEVICE *pDevice, BT_ERROR *pError) { BT_ERROR Error = BT_ERR_NONE; BT_HANDLE hCan = NULL; const BT_RESOURCE *pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_ENUM, 0); if(!pResource) { Error = BT_ERR_NO_MEMORY; goto err_free_out; } if (g_CAN_HANDLES[pResource->ulStart]){ Error = BT_ERR_GENERIC; goto err_free_out; } hCan = BT_CreateHandle(&oHandleInterface, sizeof(struct _BT_OPAQUE_HANDLE), pError); if(!hCan) { goto err_out; } g_CAN_HANDLES[pResource->ulStart] = hCan; hCan->pDevice = pDevice; pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_MEM, 0); if(!pResource) { Error = BT_ERR_NO_MEMORY; goto err_free_out; } hCan->pRegs = (LPC17xx_CAN_REGS *) pResource->ulStart; canSetPowerState(hCan, BT_POWER_STATE_AWAKE); // Reset all registers to their defaults! canDisable(hCan); pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_IRQ, 0); if(!pResource) { Error = BT_ERR_GENERIC; goto err_free_out; } /* On NVIC we don't need to register interrupts, LINKER has patched vector for us * Error = BT_RegisterInterrupt(pResource->ulStart, uart_irq_handler, hCan); if(Error) { goto err_free_out; }*/ Error = BT_EnableInterrupt(pResource->ulStart); return hCan; err_free_out: BT_DestroyHandle(hCan); err_out: if(pError) { *pError = Error; } return NULL; }
static BT_HANDLE gpio_probe(const BT_INTEGRATED_DEVICE *pDevice, BT_ERROR *pError) { BT_u32 i; BT_ERROR Error; if(pError) { *pError = BT_ERR_NONE; } BT_HANDLE hGPIO = BT_CreateHandle(&oHandleInterface, sizeof(struct _BT_OPAQUE_HANDLE), pError); if(!hGPIO) { goto err_out; } const BT_RESOURCE *pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_IO, 0); if(!pResource) { Error = BT_ERR_GENERIC; goto err_free_out; } BT_u32 base = pResource->ulStart; BT_u32 total = (pResource->ulEnd - pResource->ulStart) + 1; pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_MEM, 0); if(!pResource) { Error = BT_ERR_GENERIC; goto err_free_out; } hGPIO->pRegs = (ZYNQ_GPIO_REGS *) pResource->ulStart; pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_IRQ, 0); if(!pResource) { Error = BT_ERR_GENERIC; goto err_free_out; } Error = BT_RegisterInterrupt(pResource->ulStart, gpio_irq_handler, hGPIO); if(Error) { goto err_free_out; } // Prevent Pending Interrupts for(i = 0; i < total; i += 32) { hGPIO->pRegs->bank[i/32].INT_DIS = 0xFFFFFFFF; } Error = BT_EnableInterrupt(pResource->ulStart); Error = BT_RegisterGpioController(base, total, hGPIO); if(Error) { goto err_free_irq; } return hGPIO; err_free_irq: BT_UnregisterInterrupt(pResource->ulStart, gpio_irq_handler, hGPIO); err_free_out: BT_DestroyHandle(hGPIO); if(pError) { *pError = Error; } err_out: return NULL; }
static BT_HANDLE i2c_probe(const BT_INTEGRATED_DEVICE *pDevice, BT_ERROR *pError) { BT_ERROR Error = BT_ERR_NONE; BT_HANDLE hI2C = NULL; const BT_RESOURCE *pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_MEM, 0); if(!pResource) { Error = BT_ERR_INVALID_RESOURCE; goto err_out; } hI2C = BT_CreateHandle(&oHandleInterface, sizeof(struct _BT_OPAQUE_HANDLE), pError); if(!hI2C) { Error = BT_ERR_NO_MEMORY; goto err_out; } hI2C->pRegs = (ZYNQ_I2C_REGS *) bt_ioremap((void *)pResource->ulStart, BT_SIZE_4K); pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_IRQ, 0); if(!pResource) { Error = BT_ERR_INVALID_RESOURCE; goto err_free_out; } BT_u32 ulIRQ = pResource->ulStart; Error = BT_RegisterInterrupt(ulIRQ, i2c_irq_handler, hI2C); if(Error) { Error = BT_ERR_GENERIC; // Device already in use! goto err_free_out; } BT_EnableInterrupt(ulIRQ); hI2C->pDevice = pDevice; i2c_set_clock_rate(hI2C, BT_I2C_CLOCKRATE_400kHz); hI2C->pMutex = BT_kMutexCreate(); if(!hI2C->pMutex) { Error = BT_ERR_NO_MEMORY; goto err_free_int_out; } BT_kMutexPend(hI2C->pMutex, 0); pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_BUSID, 0); if(!pResource) { Error = BT_ERR_INVALID_RESOURCE; goto err_free_int_out; } BT_u32 ulBusID = pResource->ulStart; hI2C->pRegs->CONTROL |= 0x0000000E; BT_I2C_RegisterBusWithID(hI2C, ulBusID); if(pError) { *pError = Error; } return hI2C; err_free_int_out: BT_UnregisterInterrupt(ulIRQ, i2c_irq_handler, hI2C); err_free_out: BT_DestroyHandle(hI2C); err_out: if(pError) { *pError = Error; } return NULL; }
static BT_HANDLE pwm_probe(const BT_INTEGRATED_DEVICE *pDevice, BT_ERROR *pError) { BT_ERROR Error = BT_ERR_NONE; BT_HANDLE hPwm = NULL; const BT_RESOURCE *pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_ENUM, 0); if(!pResource) { Error = BT_ERR_NO_MEMORY; goto err_free_out; } if (g_PWM_HANDLES[pResource->ulStart]){ Error = BT_ERR_GENERIC; goto err_free_out; } hPwm = BT_CreateHandle(&oHandleInterface, sizeof(struct _BT_OPAQUE_HANDLE), pError); if(!hPwm) { goto err_out; } hPwm->id = pResource->ulStart; g_PWM_HANDLES[pResource->ulStart] = hPwm; hPwm->pDevice = pDevice; pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_MEM, 0); if(!pResource) { Error = BT_ERR_NO_MEMORY; goto err_free_out; } hPwm->pRegs = (LPC17xx_PWM_REGS *) pResource->ulStart; PwmSetPowerState(hPwm, BT_POWER_STATE_AWAKE); ResetPwm(hPwm); pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_IRQ, 0); if(!pResource) { Error = BT_ERR_GENERIC; goto err_free_out; } /* On NVIC we don't need to register interrupts, LINKER has patched vector for us * Error = BT_RegisterInterrupt(pResource->ulStart, pwm_irq_handler, hPwm); if(Error) { goto err_free_out; }*/ Error = BT_EnableInterrupt(pResource->ulStart); return hPwm; err_free_out: BT_DestroyHandle(hPwm); err_out: if(pError) { *pError = Error; } return NULL; }
static BT_HANDLE mac_probe(const BT_INTEGRATED_DEVICE *pDevice, BT_ERROR *pError) { BT_ERROR Error = BT_ERR_NONE; BT_HANDLE hMAC = NULL; const BT_RESOURCE *pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_ENUM, 0); if(!pResource) { Error = BT_ERR_NO_MEMORY; goto err_free_out; } if (g_mac_HANDLES[pResource->ulStart]){ Error = BT_ERR_GENERIC; goto err_free_out; } hMAC = BT_CreateHandle(&oHandleInterface, sizeof(struct _BT_OPAQUE_HANDLE), pError); if(!hMAC) { goto err_out; } g_mac_HANDLES[pResource->ulStart] = hMAC; hMAC->pDevice = pDevice; pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_MEM, 0); if(!pResource) { Error = BT_ERR_NO_MEMORY; goto err_free_out; } hMAC->pRegs = (LM3Sxx_MAC_REGS *) pResource->ulStart; macSetPowerState(hMAC, BT_POWER_STATE_AWAKE); pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_IRQ, 0); if(!pResource) { Error = BT_ERR_GENERIC; goto err_free_out; } /* On NVIC we don't need to register interrupts, LINKER has patched vector for us * Error = BT_RegisterInterrupt(pResource->ulStart, mac_irq_handler, hMAC); if(Error) { goto err_free_out; }*/ BT_SetInterruptPriority(pResource->ulStart, ((0x01 << BT_CONFIG_MACH_PRIORITY_BITS)-1)); Error = BT_EnableInterrupt(pResource->ulStart); Error = BT_RegisterNetworkInterface(hMAC); return hMAC; err_free_out: BT_kFree(hMAC); err_out: if(pError) { *pError = Error; } return NULL; }