rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2014 Travis Geiselbrecht |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | #include <err.h> |
| 24 | #include <debug.h> |
| 25 | #include <target.h> |
| 26 | #include <compiler.h> |
| 27 | #include <dev/gpio.h> |
| 28 | #include <platform/gpio.h> |
| 29 | |
| 30 | #include <platform/lpc.h> |
| 31 | |
| 32 | /* needs to be defined for the lpcopen drivers */ |
| 33 | const uint32_t OscRateIn = 12000000; |
| 34 | const uint32_t RTCOscRateIn = 32768; |
| 35 | |
| 36 | /* The System initialization code is called prior to the application and |
| 37 | initializes the board for run-time operation. Board initialization |
| 38 | includes clock setup and default pin muxing configuration. */ |
| 39 | |
| 40 | /***************************************************************************** |
| 41 | * Private types/enumerations/variables |
| 42 | ****************************************************************************/ |
| 43 | |
| 44 | /* IOCON setup table, only items that need changing from their default pin |
| 45 | state are in this table. */ |
| 46 | STATIC const PINMUX_GRP_T ioconSetup[] = { |
| 47 | /* LEDs */ |
| 48 | {0, 25, (IOCON_MODE_INACT | IOCON_DIGMODE_EN)}, /* PIO0_25-BREAK_CTRL-RED (low enable) */ |
| 49 | {0, 3, (IOCON_MODE_INACT | IOCON_DIGMODE_EN)}, /* PIO0_3-SCT1_OUT4-GRN */ |
| 50 | {1, 1, (IOCON_MODE_INACT | IOCON_DIGMODE_EN)}, /* PIO1_1-BREAK_STS1-BLUE */ |
| 51 | |
| 52 | /* QEI, motor controler, I2C, CAN */ |
| 53 | {0, 2, (IOCON_MODE_INACT | IOCON_DIGMODE_EN)}, /* PIO0_2-QEI-SCT0_IN */ |
| 54 | {0, 30, (IOCON_MODE_INACT | IOCON_DIGMODE_EN)}, /* PIO0_30-QEI-SCT0_IN */ |
| 55 | {0, 17, (IOCON_MODE_INACT | IOCON_DIGMODE_EN)}, /* PIO0_17-QEI-SCT0_IN */ |
| 56 | {0, 25, (IOCON_MODE_INACT | IOCON_DIGMODE_EN)}, /* PIO0_25-BREAK_CTRL-RED */ |
| 57 | {1, 1, (IOCON_MODE_INACT | IOCON_DIGMODE_EN)}, /* PIO1_1-BREAK_STS1-BLUE */ |
| 58 | {0, 23, (IOCON_MODE_INACT | IOCON_DIGMODE_EN)}, /* PIO0_23-I2C_SDA */ |
| 59 | {0, 22, (IOCON_MODE_INACT | IOCON_DIGMODE_EN)}, /* PIO0_22-I2C_SCL */ |
| 60 | {0, 11, (IOCON_MODE_INACT | IOCON_DIGMODE_EN)}, /* PIO0_11-CAN_RD */ |
| 61 | {0, 31, (IOCON_MODE_INACT | IOCON_DIGMODE_EN)}, /* PIO0_31-CAN_TD */ |
| 62 | |
| 63 | /* ADC */ |
| 64 | {1, 3, (IOCON_MODE_INACT)}, /* PIO1_3-ADC1_5 */ |
| 65 | {0, 4, (IOCON_MODE_INACT)}, /* PIO0_4-ADC0_4 */ |
| 66 | {0, 5, (IOCON_MODE_INACT)}, /* PIO0_5-ADC0_3 */ |
| 67 | {0, 7, (IOCON_MODE_INACT)}, /* PIO0_7-ADC0_1 */ |
| 68 | {0, 8, (IOCON_MODE_INACT)}, /* PIO0_8-ADC0_0 */ |
| 69 | {0, 9, (IOCON_MODE_INACT)}, /* PIO0_9-ADC1_1 */ |
| 70 | {0, 10, (IOCON_MODE_INACT)}, /* PIO0_10-ADC1_2 */ |
| 71 | |
| 72 | /* Joystick */ |
| 73 | {1, 4, (IOCON_MODE_INACT | IOCON_DIGMODE_EN)}, /* PIO1_4-JOY_U */ |
| 74 | {1, 5, (IOCON_MODE_INACT | IOCON_DIGMODE_EN)}, /* PIO1_5-JOY_C */ |
| 75 | {1, 6, (IOCON_MODE_INACT | IOCON_DIGMODE_EN)}, /* PIO1_6-JOY_D */ |
| 76 | {1, 7, (IOCON_MODE_INACT | IOCON_DIGMODE_EN)}, /* PIO1_7-JOY_R */ |
| 77 | {1, 8, (IOCON_MODE_INACT | IOCON_DIGMODE_EN)}, /* PIO1_8-JOY_L */ |
| 78 | |
| 79 | /* UART */ |
| 80 | {0, 13, (IOCON_MODE_INACT | IOCON_DIGMODE_EN)}, /* PIO0_13-ISP_RX */ |
| 81 | {0, 18, (IOCON_MODE_INACT | IOCON_DIGMODE_EN)}, /* PIO0_18-ISP_TX */ |
| 82 | {0, 11, (IOCON_MODE_INACT | IOCON_DIGMODE_EN)}, |
| 83 | {0, 31, (IOCON_MODE_INACT | IOCON_DIGMODE_EN)}, |
| 84 | |
| 85 | /* USB related */ |
| 86 | {1, 11, (IOCON_MODE_PULLDOWN | IOCON_DIGMODE_EN)}, /* PIO1_11-ISP_1 (VBUS) */ |
| 87 | }; |
| 88 | |
| 89 | /* SWIM pin assignment definitions for pin assignment/muxing */ |
| 90 | typedef struct { |
| 91 | uint16_t assignedpin : 9; /* Function and mode */ |
| 92 | uint16_t port : 2; /* Pin port */ |
| 93 | uint16_t pin : 5; /* Pin number */ |
| 94 | } SWM_GRP_T; |
| 95 | |
| 96 | /* Pin muxing table, only items that need changing from their default pin |
| 97 | state are in this table. */ |
| 98 | STATIC const SWM_GRP_T swmSetup[] = { |
| 99 | /* USB related */ |
| 100 | {(uint16_t) SWM_USB_VBUS_I, 1, 11}, /* PIO1_11-ISP_1-AIN_CTRL */ |
| 101 | |
| 102 | /* UART */ |
| 103 | {(uint16_t) SWM_UART0_RXD_I, 0, 13}, /* PIO0_13-ISP_RX */ |
| 104 | {(uint16_t) SWM_UART0_TXD_O, 0, 18}, /* PIO0_18-ISP_TX */ |
| 105 | }; |
| 106 | |
| 107 | /* Setup fixed pin functions (GPIOs are fixed) */ |
| 108 | /* No fixed pins except GPIOs */ |
| 109 | #define PINENABLE0_VAL 0xFFFFFFFF |
| 110 | |
| 111 | /* No fixed pins except GPIOs */ |
| 112 | #define PINENABLE1_VAL 0x00FFFFFF |
| 113 | |
| 114 | /* Sets up system pin muxing */ |
| 115 | void Board_SetupMuxing(void) |
| 116 | { |
| 117 | /* Enable SWM and IOCON clocks */ |
| 118 | Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_IOCON); |
| 119 | Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SWM); |
| 120 | Chip_SYSCTL_PeriphReset(RESET_IOCON); |
| 121 | |
| 122 | /* IOCON setup */ |
| 123 | Chip_IOCON_SetPinMuxing(LPC_IOCON, ioconSetup, sizeof(ioconSetup) / sizeof(PINMUX_GRP_T)); |
| 124 | |
| 125 | /* SWM assignable pin setup */ |
| 126 | for (uint i = 0; i < (sizeof(swmSetup) / sizeof(SWM_GRP_T)); i++) { |
| 127 | Chip_SWM_MovablePortPinAssign((CHIP_SWM_PIN_MOVABLE_T) swmSetup[i].assignedpin, |
| 128 | swmSetup[i].port, swmSetup[i].pin); |
| 129 | } |
| 130 | |
| 131 | /* SWM fixed pin setup */ |
| 132 | // LPC_SWM->PINENABLE[0] = PINENABLE0_VAL; |
| 133 | // LPC_SWM->PINENABLE[1] = PINENABLE1_VAL; |
| 134 | |
| 135 | /* Note SWM and IOCON clocks are left on */ |
| 136 | } |
| 137 | |
| 138 | /* Initialize debug output via UART for board */ |
| 139 | void Board_Debug_Init(void) |
| 140 | { |
| 141 | /* Disables pullups/pulldowns and enable digitial mode */ |
| 142 | Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 13, (IOCON_MODE_INACT | IOCON_DIGMODE_EN)); |
| 143 | Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 18, (IOCON_MODE_INACT | IOCON_DIGMODE_EN)); |
| 144 | |
| 145 | /* UART signal muxing via SWM */ |
| 146 | Chip_SWM_MovablePortPinAssign(SWM_UART0_RXD_I, 0, 13); |
| 147 | Chip_SWM_MovablePortPinAssign(SWM_UART0_TXD_O, 0, 18); |
| 148 | } |
| 149 | |
| 150 | #define MAXLEDS 3 |
| 151 | static const uint8_t ledpins[MAXLEDS] = {25, 3, 1}; |
| 152 | static const uint8_t ledports[MAXLEDS] = {0, 0, 1}; |
| 153 | |
| 154 | /* Initializes board LED(s) */ |
| 155 | static void Board_LED_Init(void) |
| 156 | { |
| 157 | int idx; |
| 158 | |
| 159 | Chip_GPIO_Init(LPC_GPIO); |
| 160 | |
| 161 | for (idx = 0; idx < MAXLEDS; idx++) { |
| 162 | /* Set the GPIO as output with initial state off (high) */ |
| 163 | Chip_GPIO_SetPinDIROutput(LPC_GPIO, ledports[idx], ledpins[idx]); |
| 164 | Chip_GPIO_SetPinState(LPC_GPIO, ledports[idx], ledpins[idx], true); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /* Sets the state of a board LED to on or off */ |
| 169 | void Board_LED_Set(uint8_t LEDNumber, bool On) |
| 170 | { |
| 171 | if (LEDNumber < MAXLEDS) { |
| 172 | /* Toggle state, low is on, high is off */ |
| 173 | Chip_GPIO_SetPinState(LPC_GPIO, ledports[LEDNumber], ledpins[LEDNumber], !On); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /* Returns the current state of a board LED */ |
| 178 | bool Board_LED_Test(uint8_t LEDNumber) |
| 179 | { |
| 180 | bool state = false; |
| 181 | |
| 182 | if (LEDNumber < MAXLEDS) { |
| 183 | state = !Chip_GPIO_GetPinState(LPC_GPIO, ledports[LEDNumber], ledpins[LEDNumber]); |
| 184 | } |
| 185 | |
| 186 | return state; |
| 187 | } |
| 188 | |
| 189 | /* Toggles the current state of a board LED */ |
| 190 | void Board_LED_Toggle(uint8_t LEDNumber) |
| 191 | { |
| 192 | Chip_GPIO_SetPinToggle(LPC_GPIO, ledports[LEDNumber], ledpins[LEDNumber]); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | void target_early_init(void) |
| 197 | { |
| 198 | Board_SetupMuxing(); |
| 199 | |
| 200 | Board_Debug_Init(); |
| 201 | Board_LED_Init(); |
| 202 | } |
| 203 | |
| 204 | void target_init(void) |
| 205 | { |
| 206 | } |
| 207 | |
| 208 | void target_set_debug_led(unsigned int led, bool on) |
| 209 | { |
| 210 | if (led < 3) |
| 211 | Board_LED_Set(led, on); |
| 212 | } |
| 213 | |
| 214 | // vim: set ts=4 sw=4 expandtab: |