| 
									
										
										
										
											2018-12-04 13:17:12 +07:00
										 |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*!
 | 
					
						
							|  |  |  |  @file    board_mcb1800.c | 
					
						
							|  |  |  |  @author  hathach (tinyusb.org) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  @section LICENSE | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  Software License Agreement (BSD License) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  Copyright (c) 2018, hathach (tinyusb.org) | 
					
						
							|  |  |  |  All rights reserved. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  1. Redistributions of source code must retain the above copyright | 
					
						
							|  |  |  |  notice, this list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  2. Redistributions in binary form must reproduce the above copyright | 
					
						
							|  |  |  |  notice, this list of conditions and the following disclaimer in the | 
					
						
							|  |  |  |  documentation and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  3. Neither the name of the copyright holders nor the | 
					
						
							|  |  |  |  names of its contributors may be used to endorse or promote products | 
					
						
							|  |  |  |  derived from this software without specific prior written permission. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY | 
					
						
							|  |  |  |  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 
					
						
							|  |  |  |  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY | 
					
						
							|  |  |  |  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 
					
						
							|  |  |  |  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 
					
						
							|  |  |  |  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | 
					
						
							|  |  |  |  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
					
						
							|  |  |  |  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 
					
						
							|  |  |  |  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  This file is part of the tinyusb stack. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef BOARD_MCB1800
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "../board.h"
 | 
					
						
							|  |  |  | #include "tusb.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define LED_PORT  6
 | 
					
						
							|  |  |  | #define LED_PIN   24
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // MACRO TYPEDEF CONSTANT ENUM DECLARATION
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | /* System configuration variables used by chip driver */ | 
					
						
							|  |  |  | const uint32_t ExtRateIn = 0; | 
					
						
							|  |  |  | const uint32_t OscRateIn = 12000000; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-04 13:32:59 +07:00
										 |  |  | static const PINMUX_GRP_T pinmuxing[] = | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-12-04 13:17:12 +07:00
										 |  |  | 	/* Board LEDs */ | 
					
						
							|  |  |  | 	{0xD, 10, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC4)}, | 
					
						
							|  |  |  | 	{0xD, 11, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC4)}, | 
					
						
							|  |  |  | 	{0xD, 12, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC4)}, | 
					
						
							|  |  |  | 	{0xD, 13, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC4)}, | 
					
						
							|  |  |  | 	{0xD, 14, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC4)}, | 
					
						
							|  |  |  | 	{0x9, 0,  (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC0)}, | 
					
						
							|  |  |  | 	{0x9, 1,  (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC0)}, | 
					
						
							|  |  |  | 	{0x9, 2,  (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC0)}, | 
					
						
							| 
									
										
										
										
											2018-12-04 13:32:59 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-04 13:17:12 +07:00
										 |  |  | 	/*  I2S  */ | 
					
						
							|  |  |  | 	{0x3, 0,  (SCU_PINIO_FAST | SCU_MODE_FUNC2)}, | 
					
						
							|  |  |  | 	{0x6, 0,  (SCU_PINIO_FAST | SCU_MODE_FUNC4)}, | 
					
						
							|  |  |  | 	{0x7, 2,  (SCU_PINIO_FAST | SCU_MODE_FUNC2)}, | 
					
						
							|  |  |  | 	{0x6, 2,  (SCU_PINIO_FAST | SCU_MODE_FUNC3)}, | 
					
						
							|  |  |  | 	{0x7, 1,  (SCU_PINIO_FAST | SCU_MODE_FUNC2)}, | 
					
						
							|  |  |  | 	{0x6, 1,  (SCU_PINIO_FAST | SCU_MODE_FUNC3)}, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Pin clock mux values, re-used structure, value in first index is meaningless */ | 
					
						
							| 
									
										
										
										
											2018-12-04 13:32:59 +07:00
										 |  |  | static const PINMUX_GRP_T pinclockmuxing[] = | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-12-04 13:17:12 +07:00
										 |  |  | 	{0, 0,  (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_HIGHSPEEDSLEW_EN | SCU_MODE_FUNC0)}, | 
					
						
							|  |  |  | 	{0, 1,  (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_HIGHSPEEDSLEW_EN | SCU_MODE_FUNC0)}, | 
					
						
							|  |  |  | 	{0, 2,  (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_HIGHSPEEDSLEW_EN | SCU_MODE_FUNC0)}, | 
					
						
							|  |  |  | 	{0, 3,  (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_HIGHSPEEDSLEW_EN | SCU_MODE_FUNC0)}, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*------------------------------------------------------------------*/ | 
					
						
							|  |  |  | /* BOARD API
 | 
					
						
							|  |  |  |  *------------------------------------------------------------------*/ | 
					
						
							|  |  |  | // Invoked by startup code
 | 
					
						
							|  |  |  | void SystemInit(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	/* Setup system level pin muxing */ | 
					
						
							|  |  |  | 	Chip_SCU_SetPinMuxing(pinmuxing, sizeof(pinmuxing) / sizeof(PINMUX_GRP_T)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Clock pins only, group field not used */ | 
					
						
							|  |  |  | 	for (int i = 0; i < (sizeof(pinclockmuxing) / sizeof(pinclockmuxing[0])); i++) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		Chip_SCU_ClockPinMuxSet(pinclockmuxing[i].pinnum, pinclockmuxing[i].modefunc); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Chip_SetupXtalClocking(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void board_init(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   SystemCoreClockUpdate(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if CFG_TUSB_OS == OPT_OS_NONE
 | 
					
						
							|  |  |  |   SysTick_Config( SystemCoreClock / BOARD_TICKS_HZ ); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Chip_GPIO_Init(LPC_GPIO_PORT); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //------------- LED -------------//
 | 
					
						
							|  |  |  |   /* Port and bit mapping for LEDs on GPIOs */ | 
					
						
							|  |  |  |   const uint8_t ledports[] = {6, 6, 6, 6, 6, 4, 4, 4}; | 
					
						
							|  |  |  |   const uint8_t ledbits[] = {24, 25, 26, 27, 28, 12, 13, 14}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (int i = 0; i < (sizeof(ledports) / sizeof(ledports[0])); i++)  | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, ledports[i], ledbits[i]); | 
					
						
							|  |  |  |     Chip_GPIO_SetPinState(LPC_GPIO_PORT, LED_PORT, LED_PIN, false); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if 0
 | 
					
						
							|  |  |  |   //------------- BUTTON -------------//
 | 
					
						
							|  |  |  |   for(uint8_t i=0; i<BOARD_BUTTON_COUNT; i++) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     scu_pinmux(buttons[i].mux_port, buttons[i].mux_pin, GPIO_NOPULL, FUNC0); | 
					
						
							|  |  |  |     GPIO_SetDir(buttons[i].gpio_port, BIT_(buttons[i].gpio_pin), 0); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //------------- UART -------------//
 | 
					
						
							|  |  |  |   scu_pinmux(BOARD_UART_PIN_PORT, BOARD_UART_PIN_TX, MD_PDN, FUNC1); | 
					
						
							|  |  |  |   scu_pinmux(BOARD_UART_PIN_PORT, BOARD_UART_PIN_RX, MD_PLN | MD_EZI | MD_ZI, FUNC1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   UART_CFG_Type UARTConfigStruct; | 
					
						
							|  |  |  |   UART_ConfigStructInit(&UARTConfigStruct); | 
					
						
							|  |  |  |   UARTConfigStruct.Baud_rate   = CFG_UART_BAUDRATE; | 
					
						
							|  |  |  |   UARTConfigStruct.Clock_Speed = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   UART_Init(BOARD_UART_PORT, &UARTConfigStruct); | 
					
						
							|  |  |  |   UART_TxCmd(BOARD_UART_PORT, ENABLE); // Enable UART Transmit
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //------------- USB -------------//
 | 
					
						
							| 
									
										
										
										
											2018-12-05 11:53:56 +07:00
										 |  |  |   enum { | 
					
						
							|  |  |  |     USBMODE_DEVICE = 2, | 
					
						
							|  |  |  |     USBMODE_HOST   = 3 | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   enum { | 
					
						
							|  |  |  |     USBMODE_VBUS_LOW  = 0, | 
					
						
							|  |  |  |     USBMODE_VBUS_HIGH = 1 | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // USB0
 | 
					
						
							|  |  |  | #if CFG_TUSB_RHPORT0_MODE
 | 
					
						
							|  |  |  |   Chip_USB0_Init(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Reset controller
 | 
					
						
							|  |  |  |   LPC_USB0->USBCMD_D |= 0x02; | 
					
						
							| 
									
										
										
										
											2018-12-05 13:20:25 +07:00
										 |  |  |   while( LPC_USB0->USBCMD_D & 0x02 ) {} | 
					
						
							| 
									
										
										
										
											2018-12-05 11:53:56 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Set mode
 | 
					
						
							|  |  |  |   #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST
 | 
					
						
							|  |  |  |     LPC_USB0->USBMODE_H = USBMODE_HOST | (USBMODE_VBUS_HIGH << 5); | 
					
						
							|  |  |  |   #else // TODO OTG
 | 
					
						
							|  |  |  |     LPC_USB0->USBMODE_D = USBMODE_DEVICE; | 
					
						
							|  |  |  |     LPC_USB0->OTGSC = (1<<3) | (1<<0) /*| (1<<16)| (1<<24)| (1<<25)| (1<<26)| (1<<27)| (1<<28)| (1<<29)| (1<<30)*/; | 
					
						
							|  |  |  |   #endif
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // USB1
 | 
					
						
							|  |  |  | #if CFG_TUSB_RHPORT1_MODE
 | 
					
						
							|  |  |  |   Chip_USB1_Init(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Reset controller
 | 
					
						
							|  |  |  |   LPC_USB1->USBCMD_D |= 0x02; | 
					
						
							| 
									
										
										
										
											2018-12-05 13:20:25 +07:00
										 |  |  |   while( LPC_USB1->USBCMD_D & 0x02 ) {} | 
					
						
							| 
									
										
										
										
											2018-12-05 11:53:56 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Set mode
 | 
					
						
							|  |  |  |   #if CFG_TUSB_RHPORT1_MODE & OPT_MODE_HOST
 | 
					
						
							|  |  |  |     LPC_USB1->USBMODE_H = USBMODE_HOST | (USBMODE_VBUS_HIGH << 5); | 
					
						
							|  |  |  |   #else // TODO OTG
 | 
					
						
							|  |  |  |     LPC_USB1->USBMODE_D = USBMODE_DEVICE; | 
					
						
							|  |  |  |   #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // USB1 as fullspeed
 | 
					
						
							|  |  |  |   LPC_USB1->PORTSC1_D |= (1<<24); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2018-12-04 13:17:12 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-04 16:32:20 +07:00
										 |  |  | //------------- LED -------------//
 | 
					
						
							| 
									
										
										
										
											2018-12-04 13:17:12 +07:00
										 |  |  | void board_led_control(bool state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   Chip_GPIO_SetPinState(LPC_GPIO_PORT, LED_PORT, LED_PIN, state); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //------------- Buttons -------------//
 | 
					
						
							|  |  |  | static bool button_read(uint8_t id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | //  return !BIT_TEST_( GPIO_ReadValue(buttons[id].gpio_port), buttons[id].gpio_pin ); // button is active low
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | uint32_t board_buttons(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   uint32_t result = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //  for(uint8_t i=0; i<BOARD_BUTTON_COUNT; i++) result |= (button_read(i) ? BIT_(i) : 0);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //------------- UART -------------//
 | 
					
						
							|  |  |  | uint8_t  board_uart_getchar(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   //return UART_ReceiveByte(BOARD_UART_PORT);
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | void board_uart_putchar(uint8_t c) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   //UART_Send(BOARD_UART_PORT, &c, 1, BLOCKING);
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*------------------------------------------------------------------*/ | 
					
						
							|  |  |  | /* TUSB HAL MILLISECOND
 | 
					
						
							|  |  |  |  *------------------------------------------------------------------*/ | 
					
						
							|  |  |  | #if CFG_TUSB_OS == OPT_OS_NONE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | volatile uint32_t system_ticks = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SysTick_Handler (void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   system_ticks++; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | uint32_t tusb_hal_millis(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return board_tick2ms(system_ticks); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 |