Ejemplo n.º 1
0
/**
 *	All modules MUST provide a FULL cleanup function. This must cleanup all allocations etc
 *	e.g. we will have to populate this when we add circular buffers!
 *
 **/
static BT_ERROR canCleanup(BT_HANDLE hCan) {
	canDisable(hCan);

	// Disable peripheral clock.
	disableCanPeripheralClock(hCan);

	// Free any buffers if used.
	if(hCan->eMode != BT_CAN_MODE_BUFFERED) {
		BT_CloseHandle(hCan->hRxFifo);
		BT_CloseHandle(hCan->hTxFifo);
	}

	BT_u32 i;
	BT_BOOL bClose = BT_TRUE;
	for (i = 0; i < 2; i++) {
		const BT_RESOURCE *pResource = BT_GetIntegratedResource(hCan->pDevice, BT_RESOURCE_ENUM, 0);
		if ((pResource->ulStart != i) && (g_CAN_HANDLES[i] != NULL)) {
			bClose = BT_FALSE;
		}
	}

	const BT_RESOURCE *pResource = BT_GetIntegratedResource(hCan->pDevice, BT_RESOURCE_IRQ, 0);

	if (bClose) BT_DisableInterrupt(pResource->ulStart);

	pResource = BT_GetIntegratedResource(hCan->pDevice, BT_RESOURCE_ENUM, 0);

	g_CAN_HANDLES[pResource->ulStart] = NULL;	// Finally mark the hardware as not used.

	return BT_ERR_NONE;
}
Ejemplo n.º 2
0
/**
 *	Complete a full configuration of the UART.
 **/
static BT_ERROR canSetConfig(BT_HANDLE hCan, BT_CAN_CONFIG *pConfig) {
	volatile LPC17xx_CAN_REGS *pRegs = hCan->pRegs;

	BT_ERROR Error = BT_ERR_NONE;

	canSetBaudrate(hCan, pConfig->ulBaudrate);

	switch(pConfig->eMode) {
	case BT_UART_MODE_POLLED: {
		if(hCan->eMode !=  BT_CAN_MODE_POLLED) {

			if(hCan->hTxFifo) {
				BT_CloseHandle(hCan->hTxFifo);
				hCan->hTxFifo = NULL;
			}
			if(hCan->hRxFifo) {
				BT_CloseHandle(hCan->hRxFifo);
				hCan->hRxFifo = NULL;
			}

			// Disable TX and RX interrupts
			pRegs->CANIER &= ~LPC17xx_CAN_IER_RIE;	// Disable the interrupt

			hCan->eMode = BT_CAN_MODE_POLLED;
		}
		break;
	}

	case BT_UART_MODE_BUFFERED:
	{
		if(hCan->eMode != BT_CAN_MODE_BUFFERED) {
			if(!hCan->hRxFifo && !hCan->hTxFifo) {
				hCan->hRxFifo = BT_FifoCreate(pConfig->usRxBufferSize, sizeof(BT_CAN_MESSAGE), 0, &Error);
				hCan->hTxFifo = BT_FifoCreate(pConfig->usTxBufferSize, sizeof(BT_CAN_MESSAGE), 0, &Error);

				pRegs->CANIER |= LPC17xx_CAN_IER_RIE;	// Enable the interrupt
				hCan->eMode = BT_CAN_MODE_BUFFERED;
			}
		}
		break;
	}
	}

	LPC17xx_CAN_COMMON_REGS *pCMN = CAN_COMMON;

	pCMN->LPC17xx_CAN_AFMR |= LPC17xx_CAN_AFMR_ACCBP;

	return Error;
}
Ejemplo n.º 3
0
static int bt_cat(BT_HANDLE hShell, int argc, char **argv) {

	BT_HANDLE hStdout = BT_ShellGetStdout(hShell);
	BT_ERROR Error;
	char *szpPath = 0;

	if(argc == 1) {
		szpPath = "/";
	} else if(argc == 2) {
		szpPath = argv[1];
	} else {
		bt_fprintf(hStdout, "Usage: %s [path]\n", argv[0]);
		return -1;
	}


	BT_HANDLE hFile = BT_Open(szpPath, BT_GetModeFlags("rb"), &Error);
	if(!hFile) {
		bt_fprintf(hStdout, "cat: cannot access %s: No such file or not a file\n", szpPath);
		return 0;
	}

	BT_s32 c;
	while((c = BT_GetC(hFile, 0)) >= 0) {
		bt_fprintf(hStdout, "%c", c);
	}

	bt_fprintf(hStdout, "\n");

	BT_CloseHandle(hFile);

	return 0;
}
Ejemplo n.º 4
0
long bt_sys_close(int fd) {
	BT_ERROR Error = BT_ERR_NONE;
	BT_HANDLE hFile = BT_GetFileDescriptor(fd, &Error);
	BT_SetFileDescriptor(fd, NULL);
	BT_CloseHandle(hFile);
	BT_FreeFileDescriptor(fd);

	return 0;
}
Ejemplo n.º 5
0
static BT_ERROR timer_callback_cleanup(BT_HANDLE hCallback) {
	struct _TIMER_CALLBACK_HANDLE *hCleanup = (struct _TIMER_CALLBACK_HANDLE*)hCallback;

	hCleanup->pfnCallback = NULL;
	hCleanup->pParam	  = NULL;

	BT_CloseHandle((BT_HANDLE)hCleanup);

	return BT_ERR_NONE;
}
Ejemplo n.º 6
0
BT_ERROR BT_ShellScript(const BT_i8 *path) {

	BT_ERROR Error;

	BT_HANDLE hFile = BT_Open(path, "rb", &Error);
	if(!hFile) {
		BT_kPrint("Could not open shell script %s\n", path);
		return BT_ERR_GENERIC;
	}

	BT_i8 *line = BT_kMalloc(256);
	if(!line) {
		BT_CloseHandle(hFile);
		return BT_ERR_NO_MEMORY;
	}


	BT_u32 linelen;

	while((linelen = BT_GetS(hFile, 256, line)) > 0) {
		BT_i8 *p = line;
		while(isspace((int) *p)) {
			p++;
		}

		if(*p == '#') {
			continue;	// commented line!
		}

		if(p == (line + linelen)) {
			continue;
		}

		Error = BT_ShellCommand(p);
	}

	BT_kFree(line);
	BT_CloseHandle(hFile);

	return BT_ERR_NONE;
}
Ejemplo n.º 7
0
/**
 *	All modules MUST provide a FULL cleanup function. This must cleanup all allocations etc
 *	e.g. we will have to populate this when we add circular buffers!
 *
 **/
static BT_ERROR uartCleanup(BT_HANDLE hUart) {
	ResetUart(hUart);

	// Disable peripheral clock.
	disableUartPeripheralClock(hUart);

	// Free any buffers if used.
	if(hUart->eMode == BT_UART_MODE_BUFFERED) {
		BT_CloseHandle(hUart->hTxFifo);
		BT_CloseHandle(hUart->hRxFifo);
	}

	const BT_RESOURCE *pResource = BT_GetIntegratedResource(hUart->pDevice, BT_RESOURCE_IRQ, 0);

	BT_DisableInterrupt(pResource->ulStart);

	pResource = BT_GetIntegratedResource(hUart->pDevice, BT_RESOURCE_ENUM, 0);

	g_USART_HANDLES[pResource->ulStart] = NULL;	// Finally mark the hardware as not used.

	return BT_ERR_NONE;
}
Ejemplo n.º 8
0
BT_ERROR BT_CleanupInterruptControllers() {
    BT_u32 i;
    BT_ERROR Error;

    for(i=0; i < g_ulRegistered; i++) {

        BT_CloseHandle(g_oControllers[i].hIRQ);
        if(Error) {
            //BT_kPrintf("Error cleaning %s", );
        }
    }

    return BT_ERR_NONE;
}
Ejemplo n.º 9
0
/*---------------------------------------------------------------------------*
 * Routine:  sys_mbox_free
 *---------------------------------------------------------------------------*
 * Description:
 *      Deallocates a mailbox. If there are messages still present in the
 *      mailbox when the mailbox is deallocated, it is an indication of a
 *      programming error in lwIP and the developer should be notified.
 * Inputs:
 *      sys_mbox_t mbox         -- Handle of mailbox
 * Outputs:
 *      sys_mbox_t              -- Handle to new mailbox
 *---------------------------------------------------------------------------*/
void sys_mbox_free( sys_mbox_t *pxMailBox ) {
	BT_u32 ulMessagesWaiting;

	ulMessagesWaiting = BT_QueueMessagesWaiting( *pxMailBox );

	#if SYS_STATS
	{
		if( ulMessagesWaiting != 0UL ) {
			SYS_STATS_INC( mbox.err );
		}

		SYS_STATS_DEC( mbox.used );
	}
	#endif /* SYS_STATS */

	BT_CloseHandle( *pxMailBox );
}
Ejemplo n.º 10
0
static int bt_partition_disk(BT_HANDLE hShell, int argc, char **argv) {

    if(argc > 2) {
        usage_disk(hShell);
        return -1;
    }

    if(argc == 1) {
        BT_PRSHELL("Current disk: %s\n", device_path ? device_path : "none-selected");
        return 0;
    }

    if(!device_path) {
        int retval = 0;
        BT_ERROR Error;
        BT_HANDLE hBlock = BT_Open(argv[1], 0, &Error);
        if(!hBlock) {
            BT_PRSHELL("Error: Could not open %s\n", argv[1]);
            return -1;
        }

        /**
         *  Ensure we have a valid block device handle.
         *  This API will return BT_ERR_INVALID_HANDLE, or BT_ERR_NONE.
         *
         *  Maybe we should actually add a simple API for that!
         **/
        if(BT_GetBlockGeometry(hBlock, NULL) != BT_ERR_NONE) {
            BT_PRSHELL("Error: %s is not a RAW block device\n", argv[1]);
            retval = -1;
            goto close_out;
        }

        device_path = BT_kMalloc(strlen(argv[1]));
        strcpy(device_path, argv[1]);

close_out:
        BT_CloseHandle(hBlock);
        return retval;

    } else {
        BT_PRSHELL("Already working with: %s\n", device_path);
    }

    return 0;
}
Ejemplo n.º 11
0
static int bt_partition_list(BT_HANDLE hShell, int argc, char **argv) {

    if(argc != 1) {
        usage_list(hShell);
        return -1;
    }

    if(!device_path) {
        return no_disk(hShell);
    }

    BT_BLOCK_GEOMETRY oGeom;

    BT_u32 i;
    if(!p_partition_info) {
        BT_ERROR Error;
        BT_HANDLE hBlock = BT_Open(device_path, 0, &Error);
        if(!hBlock) {
            BT_PRSHELL("Error: Could not open %s\n", argv[1]);
            return -1;
        }

        BT_GetBlockGeometry(hBlock, &oGeom);

        g_partitions = BT_PartitionCount(hBlock);
        for(i = 0; i < g_partitions; i++) {
            BT_PartitionInfo(hBlock, i, &g_partition_info[i]);
        }

        p_partition_info = g_partition_info;

        BT_CloseHandle(hBlock);
    }

    BT_PRSHELL("Device %s contains %d partitions\n", device_path, g_partitions);
    BT_PRSHELL("BlockSize    : %lu\n", oGeom.ulBlockSize);
    BT_PRSHELL("Total Blocks : %lu\n", oGeom.ulTotalBlocks);

    for(i = 0; i < g_partitions; i++) {
        BT_PRSHELL("  %d : %lu : %lu\n", i, g_partition_info[i].ulStartLBA, g_partition_info[i].ulSectorCount);
    }

    return 0;
}
Ejemplo n.º 12
0
/** Delete a semaphore
 * @param mutex the mutex to delete */
void sys_mutex_free( sys_mutex_t *Mutex ) {
	SYS_STATS_DEC( mutex.used );
	BT_CloseHandle( *Mutex );
}
Ejemplo n.º 13
0
static BT_ERROR fifo_cleanup(BT_HANDLE hFifo) {
	BT_CloseHandle(hFifo->hQueue);
	return BT_ERR_NONE;
}
Ejemplo n.º 14
0
static int bt_load_fpga(BT_HANDLE hShell, int argc, char **argv) {

	BT_HANDLE hStdout = BT_ShellGetStdout(hShell);
	BT_ERROR Error;

	if(argc != 4 && argc != 5) {
		bt_fprintf(hStdout, "Usage: %s [buffer_address] [fpga_device] [bitstream] [length]\n", argv[0]);
		return -1;
	}

	BT_u32 length = 0;
	BT_u32 addr = strtoul(argv[1], NULL, 16);
	void *p = (void *) addr;

	if(argv[3][0] == '-' && argc == 5) {
		// Image loaded and length provided.
		length = strtoul(argv[4], NULL, 10);
		goto flush;
	}

	BT_HANDLE hFile = BT_Open(argv[3], BT_GetModeFlags("rb"), &Error);
	if(!hFile) {
		bt_fprintf(hStdout, "Could not open bitstream at %s\n", argv[3]);
		return -1;
	}

	BT_HANDLE hInode = BT_GetInode(argv[3], &Error);
	if(!hInode) {
		bt_fprintf(hStdout, "Could not stat bitstream at %s\n", argv[3]);
		BT_CloseHandle(hFile);
		return -1;
	}

	BT_INODE oInode;
	BT_ReadInode(hInode, &oInode);


	BT_kPrint("Loading %s at %08X (%llu bytes)", argv[3], addr, oInode.ullFileSize);

	BT_Read(hFile, 0, oInode.ullFileSize, p, &Error);

	BT_kPrint("Load successful");

	if(hFile) {
		BT_CloseHandle(hFile);
	}

	if(hInode) {
		BT_CloseHandle(hInode);
	}

	length = oInode.ullFileSize;

flush:

	BT_DCacheFlush();

	BT_HANDLE hFPGA = BT_DeviceOpen(argv[2], &Error);
	if(!hFPGA) {
		bt_printf("Failed to open fpga device %s\n", argv[2]);
		return -1;
	}

	BT_Write(hFPGA, 0, length, p, &Error);

	BT_CloseHandle(hFPGA);

	return 0;
}
Ejemplo n.º 15
0
/**
 *	Complete a full configuration of the UART.
 **/
static BT_ERROR uartSetConfig(BT_HANDLE hUart, BT_UART_CONFIG *pConfig) {
	volatile LM3Sxx_UART_REGS *pRegs = hUart->pRegs;

	BT_ERROR Error = BT_ERR_NONE;

	uartSetBaudrate(hUart, pConfig->ulBaudrate);

	uartDisable(hUart);

    // Set parity, data length, and number of stop bits.
    pRegs->LCRH = (pConfig->ucDataBits - 5) << 5;
	pRegs->LCRH |= (pConfig->ucStopBits) << 3;
	if (pConfig->ucParity == BT_UART_PARITY_ODD)   pRegs->LCRH |= LM3Sxx_UART_LCRH_ODD;
	if (pConfig->ucParity == BT_UART_PARITY_EVEN)  pRegs->LCRH |= LM3Sxx_UART_LCRH_EVEN;
	if (pConfig->ucParity == BT_UART_PARITY_MARK)  pRegs->LCRH |= LM3Sxx_UART_LCRH_MARK;
	if (pConfig->ucParity == BT_UART_PARITY_SPACE) pRegs->LCRH |= LM3Sxx_UART_LCRH_SPACE;

    // Clear the flags register.
    pRegs->FR = 0;

    uartEnable(hUart);

	switch(pConfig->eMode) {
	case BT_UART_MODE_POLLED: {
		if(hUart->eMode !=  BT_UART_MODE_POLLED) {

			if(hUart->hTxFifo) {
				BT_CloseHandle(hUart->hTxFifo);
				hUart->hTxFifo = NULL;
			}
			if(hUart->hRxFifo) {
				BT_CloseHandle(hUart->hRxFifo);
				hUart->hRxFifo = NULL;
			}

			// Disable TX and RX interrupts
			pRegs->IM &= ~(LM3Sxx_UART_INT_RX | LM3Sxx_UART_INT_RT);	// Disable the interrupt

			hUart->eMode = BT_UART_MODE_POLLED;
		}
		break;
	}

	case BT_UART_MODE_BUFFERED:
	{
		if(hUart->eMode != BT_UART_MODE_BUFFERED) {
			if(!hUart->hRxFifo && !hUart->hTxFifo) {
				hUart->hRxFifo = BT_FifoCreate(pConfig->ulRxBufferSize, 1, 0, &Error);
				hUart->hTxFifo = BT_FifoCreate(pConfig->ulTxBufferSize, 1, 0, &Error);

				pRegs->IM |= LM3Sxx_UART_INT_RX | LM3Sxx_UART_INT_RT;	// Enable the interrupt
				hUart->eMode = BT_UART_MODE_BUFFERED;
			}
		}
		break;
	}

	default:
		// Unsupported operating mode!
		break;
	}

	return BT_ERR_NONE;
}
Ejemplo n.º 16
0
/*---------------------------------------------------------------------------*
 * Routine:  sys_sem_free
 *---------------------------------------------------------------------------*
 * Description:
 *      Deallocates a semaphore
 * Inputs:
 *      sys_sem_t sem           -- Semaphore to free
 *---------------------------------------------------------------------------*/
void sys_sem_free( sys_sem_t *pxSemaphore ) {
	SYS_STATS_DEC(sem.used);
	BT_CloseHandle( *pxSemaphore );
}
Ejemplo n.º 17
0
/**
 *	Complete a full configuration of the SPI.
 **/
static BT_ERROR spiSetConfig(BT_HANDLE hSpi, BT_SPI_CONFIG *pConfig) {
	volatile LPC17xx_SPI_REGS *pRegs = hSpi->pRegs;
	BT_ERROR Error = BT_ERR_NONE;

	const BT_RESOURCE *pResource = BT_GetIntegratedResource(hSpi->pDevice, BT_RESOURCE_ENUM, 0);

	if (pResource->ulStart == 0) {
		pRegs->CR &= ~(LPC17xx_SPI_CR_DSS_MASK << 8);
		pRegs->CR |= (pConfig->ucDataBits & LPC17xx_SPI_CR_DSS_MASK) << 8;
		pRegs->CR &= ~(LPC17xx_SPI_CR_CPOL | LPC17xx_SPI_CR_CPHA);
		if (pConfig->eCPOL)
			pRegs->CR |= LPC17xx_SPI_CR_CPOL;
		if (pConfig->eCPHA)
			pRegs->CR |= LPC17xx_SPI_CR_CPHA;
	}
	else {
		pRegs->CR0 = (pConfig->ucDataBits - 1) & LPC17xx_SPI_CR0_DSS_MASK;
		pRegs->CR0 |= pConfig->eCPOL << 6;
		pRegs->CR0 |= pConfig->eCPHA << 7;
	}

	spiSetBaudrate(hSpi, pConfig->ulBaudrate);

	switch(pConfig->eMode) {
	case BT_SPI_MODE_POLLED: {
		if(hSpi->eMode !=  BT_SPI_MODE_POLLED) {

			if(hSpi->hTxFifo) {
				BT_CloseHandle(hSpi->hTxFifo);
				hSpi->hTxFifo = NULL;
			}
			if(hSpi->hRxFifo) {
				BT_CloseHandle(hSpi->hRxFifo);
				hSpi->hRxFifo = NULL;
			}

			// Disable TX and RX interrupts
			//@@pRegs->IER &= ~LPC17xx_SPI_IER_RBRIE;	// Disable the interrupt

			hSpi->eMode = BT_SPI_MODE_POLLED;
		}
		break;
	}

	case BT_SPI_MODE_BUFFERED:
	{
		if(hSpi->eMode != BT_SPI_MODE_BUFFERED) {
			if(!hSpi->hRxFifo && !hSpi->hTxFifo) {
				hSpi->hRxFifo = BT_FifoCreate(pConfig->ulRxBufferSize, 1, 0, &Error);
				hSpi->hTxFifo = BT_FifoCreate(pConfig->ulTxBufferSize, 1, 0, &Error);

				//@@pRegs->IER |= LPC17xx_SPI_IER_RBRIE;	// Enable the interrupt
				hSpi->eMode = BT_SPI_MODE_BUFFERED;
			}
		}
		break;
	}

	default:
		// Unsupported operating mode!
		break;
	}

	return BT_ERR_NONE;
}
Ejemplo n.º 18
0
int closesocket(int s) {
	BT_CloseHandle((BT_HANDLE)s);

	return 0;
}