Mouse code for the MacroRat
mbed-dev/targets/TARGET_ARM_SSG/TARGET_MPS2/serial_api.c@46:b156ef445742, 2017-06-03 (annotated)
- Committer:
- sahilmgandhi
- Date:
- Sat Jun 03 00:22:44 2017 +0000
- Revision:
- 46:b156ef445742
- Parent:
- 18:6a4db94011d3
Final code for internal battlebot competition.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /* mbed Microcontroller Library |
sahilmgandhi | 18:6a4db94011d3 | 2 | * Copyright (c) 2006-2015 ARM Limited |
sahilmgandhi | 18:6a4db94011d3 | 3 | * |
sahilmgandhi | 18:6a4db94011d3 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sahilmgandhi | 18:6a4db94011d3 | 5 | * you may not use this file except in compliance with the License. |
sahilmgandhi | 18:6a4db94011d3 | 6 | * You may obtain a copy of the License at |
sahilmgandhi | 18:6a4db94011d3 | 7 | * |
sahilmgandhi | 18:6a4db94011d3 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sahilmgandhi | 18:6a4db94011d3 | 9 | * |
sahilmgandhi | 18:6a4db94011d3 | 10 | * Unless required by applicable law or agreed to in writing, software |
sahilmgandhi | 18:6a4db94011d3 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sahilmgandhi | 18:6a4db94011d3 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sahilmgandhi | 18:6a4db94011d3 | 13 | * See the License for the specific language governing permissions and |
sahilmgandhi | 18:6a4db94011d3 | 14 | * limitations under the License. |
sahilmgandhi | 18:6a4db94011d3 | 15 | */ |
sahilmgandhi | 18:6a4db94011d3 | 16 | // math.h required for floating point operations for baud rate calculation |
sahilmgandhi | 18:6a4db94011d3 | 17 | #include <math.h> |
sahilmgandhi | 18:6a4db94011d3 | 18 | #include <stdio.h> |
sahilmgandhi | 18:6a4db94011d3 | 19 | #include <string.h> |
sahilmgandhi | 18:6a4db94011d3 | 20 | #include <stdlib.h> |
sahilmgandhi | 18:6a4db94011d3 | 21 | |
sahilmgandhi | 18:6a4db94011d3 | 22 | #include "serial_api.h" |
sahilmgandhi | 18:6a4db94011d3 | 23 | #include "cmsis.h" |
sahilmgandhi | 18:6a4db94011d3 | 24 | #include "pinmap.h" |
sahilmgandhi | 18:6a4db94011d3 | 25 | #include "mbed_error.h" |
sahilmgandhi | 18:6a4db94011d3 | 26 | #include "gpio_api.h" |
sahilmgandhi | 18:6a4db94011d3 | 27 | |
sahilmgandhi | 18:6a4db94011d3 | 28 | /****************************************************************************** |
sahilmgandhi | 18:6a4db94011d3 | 29 | * INITIALIZATION |
sahilmgandhi | 18:6a4db94011d3 | 30 | ******************************************************************************/ |
sahilmgandhi | 18:6a4db94011d3 | 31 | |
sahilmgandhi | 18:6a4db94011d3 | 32 | static const PinMap PinMap_UART_TX[] = { |
sahilmgandhi | 18:6a4db94011d3 | 33 | {USBTX , UART_0, 0}, |
sahilmgandhi | 18:6a4db94011d3 | 34 | {XB_TX , UART_1, 0}, |
sahilmgandhi | 18:6a4db94011d3 | 35 | {SH0_TX , UART_2, 0}, |
sahilmgandhi | 18:6a4db94011d3 | 36 | {SH1_TX , UART_3, 0}, |
sahilmgandhi | 18:6a4db94011d3 | 37 | {NC , NC , 0} |
sahilmgandhi | 18:6a4db94011d3 | 38 | }; |
sahilmgandhi | 18:6a4db94011d3 | 39 | |
sahilmgandhi | 18:6a4db94011d3 | 40 | static const PinMap PinMap_UART_RX[] = { |
sahilmgandhi | 18:6a4db94011d3 | 41 | {USBRX , UART_0, 0}, |
sahilmgandhi | 18:6a4db94011d3 | 42 | {XB_RX , UART_1, 0}, |
sahilmgandhi | 18:6a4db94011d3 | 43 | {SH0_RX , UART_2, 0}, |
sahilmgandhi | 18:6a4db94011d3 | 44 | {SH1_RX , UART_3, 0}, |
sahilmgandhi | 18:6a4db94011d3 | 45 | {NC , NC , 0} |
sahilmgandhi | 18:6a4db94011d3 | 46 | }; |
sahilmgandhi | 18:6a4db94011d3 | 47 | |
sahilmgandhi | 18:6a4db94011d3 | 48 | #define UART_NUM 4 |
sahilmgandhi | 18:6a4db94011d3 | 49 | |
sahilmgandhi | 18:6a4db94011d3 | 50 | static uart_irq_handler irq_handler; |
sahilmgandhi | 18:6a4db94011d3 | 51 | |
sahilmgandhi | 18:6a4db94011d3 | 52 | int stdio_uart_inited = 0; |
sahilmgandhi | 18:6a4db94011d3 | 53 | serial_t stdio_uart; |
sahilmgandhi | 18:6a4db94011d3 | 54 | |
sahilmgandhi | 18:6a4db94011d3 | 55 | struct serial_global_data_s { |
sahilmgandhi | 18:6a4db94011d3 | 56 | uint32_t serial_irq_id; |
sahilmgandhi | 18:6a4db94011d3 | 57 | gpio_t sw_rts, sw_cts; |
sahilmgandhi | 18:6a4db94011d3 | 58 | uint8_t count, rx_irq_set_flow, rx_irq_set_api; |
sahilmgandhi | 18:6a4db94011d3 | 59 | }; |
sahilmgandhi | 18:6a4db94011d3 | 60 | |
sahilmgandhi | 18:6a4db94011d3 | 61 | static struct serial_global_data_s uart_data[UART_NUM]; |
sahilmgandhi | 18:6a4db94011d3 | 62 | |
sahilmgandhi | 18:6a4db94011d3 | 63 | void serial_init(serial_t *obj, PinName tx, PinName rx) { |
sahilmgandhi | 18:6a4db94011d3 | 64 | int is_stdio_uart = 0; |
sahilmgandhi | 18:6a4db94011d3 | 65 | |
sahilmgandhi | 18:6a4db94011d3 | 66 | // determine the UART to use |
sahilmgandhi | 18:6a4db94011d3 | 67 | UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); |
sahilmgandhi | 18:6a4db94011d3 | 68 | UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); |
sahilmgandhi | 18:6a4db94011d3 | 69 | UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); |
sahilmgandhi | 18:6a4db94011d3 | 70 | if ((int)uart == NC) { |
sahilmgandhi | 18:6a4db94011d3 | 71 | error("Serial pinout mapping failed"); |
sahilmgandhi | 18:6a4db94011d3 | 72 | } |
sahilmgandhi | 18:6a4db94011d3 | 73 | |
sahilmgandhi | 18:6a4db94011d3 | 74 | obj->uart = (CMSDK_UART_TypeDef *)uart; |
sahilmgandhi | 18:6a4db94011d3 | 75 | //set baud rate and enable Uart in normarl mode (RX and TX enabled) |
sahilmgandhi | 18:6a4db94011d3 | 76 | switch (uart) |
sahilmgandhi | 18:6a4db94011d3 | 77 | { |
sahilmgandhi | 18:6a4db94011d3 | 78 | case UART_0: |
sahilmgandhi | 18:6a4db94011d3 | 79 | { |
sahilmgandhi | 18:6a4db94011d3 | 80 | CMSDK_UART0->CTRL = 0x00; // Disable UART when changing configuration |
sahilmgandhi | 18:6a4db94011d3 | 81 | if((int)uart_tx != NC) |
sahilmgandhi | 18:6a4db94011d3 | 82 | { |
sahilmgandhi | 18:6a4db94011d3 | 83 | CMSDK_UART0->CTRL |= 0x01; // TX enable |
sahilmgandhi | 18:6a4db94011d3 | 84 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 85 | CMSDK_UART0->CTRL &= 0xFFFE; // TX disable |
sahilmgandhi | 18:6a4db94011d3 | 86 | } |
sahilmgandhi | 18:6a4db94011d3 | 87 | |
sahilmgandhi | 18:6a4db94011d3 | 88 | |
sahilmgandhi | 18:6a4db94011d3 | 89 | if((int)uart_rx != NC) |
sahilmgandhi | 18:6a4db94011d3 | 90 | { |
sahilmgandhi | 18:6a4db94011d3 | 91 | CMSDK_UART0->CTRL |= 0x02; // RX enable |
sahilmgandhi | 18:6a4db94011d3 | 92 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 93 | CMSDK_UART0->CTRL &= 0xFFFD; // RX disable |
sahilmgandhi | 18:6a4db94011d3 | 94 | } |
sahilmgandhi | 18:6a4db94011d3 | 95 | |
sahilmgandhi | 18:6a4db94011d3 | 96 | } |
sahilmgandhi | 18:6a4db94011d3 | 97 | break; |
sahilmgandhi | 18:6a4db94011d3 | 98 | case UART_1: //XBEE SOCKET UART |
sahilmgandhi | 18:6a4db94011d3 | 99 | { |
sahilmgandhi | 18:6a4db94011d3 | 100 | CMSDK_UART1->CTRL = 0x00; // Disable UART when changing configuration |
sahilmgandhi | 18:6a4db94011d3 | 101 | if((int)tx != NC) |
sahilmgandhi | 18:6a4db94011d3 | 102 | { |
sahilmgandhi | 18:6a4db94011d3 | 103 | CMSDK_UART1->CTRL = 0x1; // TX enable |
sahilmgandhi | 18:6a4db94011d3 | 104 | CMSDK_GPIO1->ALTFUNCSET |= 0x0100; |
sahilmgandhi | 18:6a4db94011d3 | 105 | } |
sahilmgandhi | 18:6a4db94011d3 | 106 | if((int)rx != NC) |
sahilmgandhi | 18:6a4db94011d3 | 107 | { |
sahilmgandhi | 18:6a4db94011d3 | 108 | CMSDK_UART1->CTRL |= 0x2; // RX enable |
sahilmgandhi | 18:6a4db94011d3 | 109 | CMSDK_GPIO1->ALTFUNCSET |= 0x0080; |
sahilmgandhi | 18:6a4db94011d3 | 110 | } |
sahilmgandhi | 18:6a4db94011d3 | 111 | } |
sahilmgandhi | 18:6a4db94011d3 | 112 | break; |
sahilmgandhi | 18:6a4db94011d3 | 113 | case UART_2: //Sheild0 UART |
sahilmgandhi | 18:6a4db94011d3 | 114 | { |
sahilmgandhi | 18:6a4db94011d3 | 115 | CMSDK_UART3->CTRL = 0x00; // Disable UART when changing configuration |
sahilmgandhi | 18:6a4db94011d3 | 116 | if((int)tx != NC) |
sahilmgandhi | 18:6a4db94011d3 | 117 | { |
sahilmgandhi | 18:6a4db94011d3 | 118 | CMSDK_UART3->CTRL = 0x1; // TX enable |
sahilmgandhi | 18:6a4db94011d3 | 119 | CMSDK_GPIO0->ALTFUNCSET |= 0x0010; |
sahilmgandhi | 18:6a4db94011d3 | 120 | } |
sahilmgandhi | 18:6a4db94011d3 | 121 | if((int)rx != NC) |
sahilmgandhi | 18:6a4db94011d3 | 122 | { |
sahilmgandhi | 18:6a4db94011d3 | 123 | CMSDK_UART3->CTRL |= 0x2; // RX enable |
sahilmgandhi | 18:6a4db94011d3 | 124 | CMSDK_GPIO0->ALTFUNCSET |= 0x0001; |
sahilmgandhi | 18:6a4db94011d3 | 125 | } |
sahilmgandhi | 18:6a4db94011d3 | 126 | } |
sahilmgandhi | 18:6a4db94011d3 | 127 | break; |
sahilmgandhi | 18:6a4db94011d3 | 128 | case UART_3: //Sheild1 UART |
sahilmgandhi | 18:6a4db94011d3 | 129 | { |
sahilmgandhi | 18:6a4db94011d3 | 130 | CMSDK_UART4->CTRL = 0x00; // Disable UART when changing configuration |
sahilmgandhi | 18:6a4db94011d3 | 131 | if((int)tx != NC) |
sahilmgandhi | 18:6a4db94011d3 | 132 | { |
sahilmgandhi | 18:6a4db94011d3 | 133 | CMSDK_UART4->CTRL = 0x1; // TX enable |
sahilmgandhi | 18:6a4db94011d3 | 134 | CMSDK_GPIO1->ALTFUNCSET |= 0x4000; |
sahilmgandhi | 18:6a4db94011d3 | 135 | } |
sahilmgandhi | 18:6a4db94011d3 | 136 | if((int)rx != NC) |
sahilmgandhi | 18:6a4db94011d3 | 137 | { |
sahilmgandhi | 18:6a4db94011d3 | 138 | CMSDK_UART4->CTRL |= 0x2; // RX enable |
sahilmgandhi | 18:6a4db94011d3 | 139 | CMSDK_GPIO1->ALTFUNCSET |= 0x0400; |
sahilmgandhi | 18:6a4db94011d3 | 140 | } |
sahilmgandhi | 18:6a4db94011d3 | 141 | } |
sahilmgandhi | 18:6a4db94011d3 | 142 | break; |
sahilmgandhi | 18:6a4db94011d3 | 143 | } |
sahilmgandhi | 18:6a4db94011d3 | 144 | |
sahilmgandhi | 18:6a4db94011d3 | 145 | // set default baud rate and format |
sahilmgandhi | 18:6a4db94011d3 | 146 | serial_baud (obj, 9600); |
sahilmgandhi | 18:6a4db94011d3 | 147 | |
sahilmgandhi | 18:6a4db94011d3 | 148 | // pinout the chosen uart |
sahilmgandhi | 18:6a4db94011d3 | 149 | pinmap_pinout(tx, PinMap_UART_TX); |
sahilmgandhi | 18:6a4db94011d3 | 150 | pinmap_pinout(rx, PinMap_UART_RX); |
sahilmgandhi | 18:6a4db94011d3 | 151 | |
sahilmgandhi | 18:6a4db94011d3 | 152 | switch (uart) { |
sahilmgandhi | 18:6a4db94011d3 | 153 | case UART_0: obj->index = 0; break; |
sahilmgandhi | 18:6a4db94011d3 | 154 | case UART_1: obj->index = 1; break; |
sahilmgandhi | 18:6a4db94011d3 | 155 | case UART_2: obj->index = 2; break; |
sahilmgandhi | 18:6a4db94011d3 | 156 | case UART_3: obj->index = 3; break; |
sahilmgandhi | 18:6a4db94011d3 | 157 | } |
sahilmgandhi | 18:6a4db94011d3 | 158 | uart_data[obj->index].sw_rts.pin = NC; |
sahilmgandhi | 18:6a4db94011d3 | 159 | uart_data[obj->index].sw_cts.pin = NC; |
sahilmgandhi | 18:6a4db94011d3 | 160 | serial_set_flow_control(obj, FlowControlNone, NC, NC); |
sahilmgandhi | 18:6a4db94011d3 | 161 | |
sahilmgandhi | 18:6a4db94011d3 | 162 | is_stdio_uart = (uart == STDIO_UART) ? (1) : (0); |
sahilmgandhi | 18:6a4db94011d3 | 163 | |
sahilmgandhi | 18:6a4db94011d3 | 164 | if (is_stdio_uart) { |
sahilmgandhi | 18:6a4db94011d3 | 165 | stdio_uart_inited = 1; |
sahilmgandhi | 18:6a4db94011d3 | 166 | memcpy(&stdio_uart, obj, sizeof(serial_t)); |
sahilmgandhi | 18:6a4db94011d3 | 167 | } |
sahilmgandhi | 18:6a4db94011d3 | 168 | } |
sahilmgandhi | 18:6a4db94011d3 | 169 | |
sahilmgandhi | 18:6a4db94011d3 | 170 | void serial_free(serial_t *obj) { |
sahilmgandhi | 18:6a4db94011d3 | 171 | uart_data[obj->index].serial_irq_id = 0; |
sahilmgandhi | 18:6a4db94011d3 | 172 | } |
sahilmgandhi | 18:6a4db94011d3 | 173 | |
sahilmgandhi | 18:6a4db94011d3 | 174 | // serial_baud |
sahilmgandhi | 18:6a4db94011d3 | 175 | // set the baud rate, taking in to account the current SystemFrequency |
sahilmgandhi | 18:6a4db94011d3 | 176 | void serial_baud(serial_t *obj, int baudrate) { |
sahilmgandhi | 18:6a4db94011d3 | 177 | // The MPS2 has a simple divider to control the baud rate. The formula is: |
sahilmgandhi | 18:6a4db94011d3 | 178 | // |
sahilmgandhi | 18:6a4db94011d3 | 179 | // Baudrate = PCLK / BAUDDIV |
sahilmgandhi | 18:6a4db94011d3 | 180 | // |
sahilmgandhi | 18:6a4db94011d3 | 181 | // PCLK = 25 Mhz |
sahilmgandhi | 18:6a4db94011d3 | 182 | // so for a desired baud rate of 9600 |
sahilmgandhi | 18:6a4db94011d3 | 183 | // 25000000 / 9600 = 2604 |
sahilmgandhi | 18:6a4db94011d3 | 184 | // |
sahilmgandhi | 18:6a4db94011d3 | 185 | //check to see if minimum baud value entered |
sahilmgandhi | 18:6a4db94011d3 | 186 | int baudrate_div = 0; |
sahilmgandhi | 18:6a4db94011d3 | 187 | baudrate_div = SystemCoreClock / baudrate; |
sahilmgandhi | 18:6a4db94011d3 | 188 | if(baudrate >= 16){ |
sahilmgandhi | 18:6a4db94011d3 | 189 | switch ((int)obj->uart) { |
sahilmgandhi | 18:6a4db94011d3 | 190 | case UART_0: CMSDK_UART0->BAUDDIV = baudrate_div; break; |
sahilmgandhi | 18:6a4db94011d3 | 191 | case UART_1: CMSDK_UART1->BAUDDIV = baudrate_div; break; |
sahilmgandhi | 18:6a4db94011d3 | 192 | case UART_2: CMSDK_UART3->BAUDDIV = baudrate_div; break; |
sahilmgandhi | 18:6a4db94011d3 | 193 | case UART_3: CMSDK_UART4->BAUDDIV = baudrate_div; break; |
sahilmgandhi | 18:6a4db94011d3 | 194 | default: error("serial_baud"); break; |
sahilmgandhi | 18:6a4db94011d3 | 195 | } |
sahilmgandhi | 18:6a4db94011d3 | 196 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 197 | error("serial_baud"); |
sahilmgandhi | 18:6a4db94011d3 | 198 | } |
sahilmgandhi | 18:6a4db94011d3 | 199 | |
sahilmgandhi | 18:6a4db94011d3 | 200 | } |
sahilmgandhi | 18:6a4db94011d3 | 201 | |
sahilmgandhi | 18:6a4db94011d3 | 202 | void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) { |
sahilmgandhi | 18:6a4db94011d3 | 203 | } |
sahilmgandhi | 18:6a4db94011d3 | 204 | |
sahilmgandhi | 18:6a4db94011d3 | 205 | /****************************************************************************** |
sahilmgandhi | 18:6a4db94011d3 | 206 | * INTERRUPTS HANDLING |
sahilmgandhi | 18:6a4db94011d3 | 207 | ******************************************************************************/ |
sahilmgandhi | 18:6a4db94011d3 | 208 | static inline void uart_irq(uint32_t intstatus, uint32_t index, CMSDK_UART_TypeDef *puart) { |
sahilmgandhi | 18:6a4db94011d3 | 209 | SerialIrq irq_type; |
sahilmgandhi | 18:6a4db94011d3 | 210 | switch (intstatus) |
sahilmgandhi | 18:6a4db94011d3 | 211 | { |
sahilmgandhi | 18:6a4db94011d3 | 212 | case 1: |
sahilmgandhi | 18:6a4db94011d3 | 213 | { |
sahilmgandhi | 18:6a4db94011d3 | 214 | irq_type = TxIrq; |
sahilmgandhi | 18:6a4db94011d3 | 215 | } |
sahilmgandhi | 18:6a4db94011d3 | 216 | break; |
sahilmgandhi | 18:6a4db94011d3 | 217 | |
sahilmgandhi | 18:6a4db94011d3 | 218 | case 2: |
sahilmgandhi | 18:6a4db94011d3 | 219 | { |
sahilmgandhi | 18:6a4db94011d3 | 220 | irq_type = RxIrq; |
sahilmgandhi | 18:6a4db94011d3 | 221 | } |
sahilmgandhi | 18:6a4db94011d3 | 222 | break; |
sahilmgandhi | 18:6a4db94011d3 | 223 | |
sahilmgandhi | 18:6a4db94011d3 | 224 | default: return; |
sahilmgandhi | 18:6a4db94011d3 | 225 | } /* End of Switch */ |
sahilmgandhi | 18:6a4db94011d3 | 226 | |
sahilmgandhi | 18:6a4db94011d3 | 227 | if ((RxIrq == irq_type) && (NC != uart_data[index].sw_rts.pin)) |
sahilmgandhi | 18:6a4db94011d3 | 228 | { |
sahilmgandhi | 18:6a4db94011d3 | 229 | gpio_write(&uart_data[index].sw_rts, 1); |
sahilmgandhi | 18:6a4db94011d3 | 230 | // Disable interrupt if it wasn't enabled by other part of the application |
sahilmgandhi | 18:6a4db94011d3 | 231 | if (!uart_data[index].rx_irq_set_api) |
sahilmgandhi | 18:6a4db94011d3 | 232 | { |
sahilmgandhi | 18:6a4db94011d3 | 233 | /* Disable Rx interrupt */ |
sahilmgandhi | 18:6a4db94011d3 | 234 | puart->CTRL &= ~(CMSDK_UART_CTRL_RXIRQEN_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 235 | } |
sahilmgandhi | 18:6a4db94011d3 | 236 | } |
sahilmgandhi | 18:6a4db94011d3 | 237 | |
sahilmgandhi | 18:6a4db94011d3 | 238 | if (uart_data[index].serial_irq_id != 0) |
sahilmgandhi | 18:6a4db94011d3 | 239 | { |
sahilmgandhi | 18:6a4db94011d3 | 240 | if ((irq_type != RxIrq) || (uart_data[index].rx_irq_set_api)) |
sahilmgandhi | 18:6a4db94011d3 | 241 | { |
sahilmgandhi | 18:6a4db94011d3 | 242 | irq_handler(uart_data[index].serial_irq_id, irq_type); |
sahilmgandhi | 18:6a4db94011d3 | 243 | } |
sahilmgandhi | 18:6a4db94011d3 | 244 | } |
sahilmgandhi | 18:6a4db94011d3 | 245 | |
sahilmgandhi | 18:6a4db94011d3 | 246 | if( irq_type == TxIrq ) |
sahilmgandhi | 18:6a4db94011d3 | 247 | { |
sahilmgandhi | 18:6a4db94011d3 | 248 | /* Clear the TX interrupt Flag */ |
sahilmgandhi | 18:6a4db94011d3 | 249 | puart->INTCLEAR |= 0x01; |
sahilmgandhi | 18:6a4db94011d3 | 250 | } |
sahilmgandhi | 18:6a4db94011d3 | 251 | else |
sahilmgandhi | 18:6a4db94011d3 | 252 | { |
sahilmgandhi | 18:6a4db94011d3 | 253 | /* Clear the Rx interupt Flag */ |
sahilmgandhi | 18:6a4db94011d3 | 254 | puart->INTCLEAR |= 0x02; |
sahilmgandhi | 18:6a4db94011d3 | 255 | } |
sahilmgandhi | 18:6a4db94011d3 | 256 | } |
sahilmgandhi | 18:6a4db94011d3 | 257 | |
sahilmgandhi | 18:6a4db94011d3 | 258 | void uart0_irq() {uart_irq(CMSDK_UART0->INTSTATUS & 0x3, 0, (CMSDK_UART_TypeDef*)CMSDK_UART0);} |
sahilmgandhi | 18:6a4db94011d3 | 259 | void uart1_irq() {uart_irq(CMSDK_UART1->INTSTATUS & 0x3, 1, (CMSDK_UART_TypeDef*)CMSDK_UART1);} |
sahilmgandhi | 18:6a4db94011d3 | 260 | void uart2_irq() {uart_irq(CMSDK_UART3->INTSTATUS & 0x3, 2, (CMSDK_UART_TypeDef*)CMSDK_UART3);} |
sahilmgandhi | 18:6a4db94011d3 | 261 | void uart3_irq() {uart_irq(CMSDK_UART4->INTSTATUS & 0x3, 3, (CMSDK_UART_TypeDef*)CMSDK_UART4);} |
sahilmgandhi | 18:6a4db94011d3 | 262 | |
sahilmgandhi | 18:6a4db94011d3 | 263 | void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) { |
sahilmgandhi | 18:6a4db94011d3 | 264 | irq_handler = handler; |
sahilmgandhi | 18:6a4db94011d3 | 265 | uart_data[obj->index].serial_irq_id = id; |
sahilmgandhi | 18:6a4db94011d3 | 266 | } |
sahilmgandhi | 18:6a4db94011d3 | 267 | |
sahilmgandhi | 18:6a4db94011d3 | 268 | static void serial_irq_set_internal(serial_t *obj, SerialIrq irq, uint32_t enable) { |
sahilmgandhi | 18:6a4db94011d3 | 269 | /* Declare a variable of type IRQn, initialise to 0 */ |
sahilmgandhi | 18:6a4db94011d3 | 270 | IRQn_Type irq_n = (IRQn_Type)0; |
sahilmgandhi | 18:6a4db94011d3 | 271 | uint32_t vector = 0; |
sahilmgandhi | 18:6a4db94011d3 | 272 | switch ((int)obj->uart) |
sahilmgandhi | 18:6a4db94011d3 | 273 | { |
sahilmgandhi | 18:6a4db94011d3 | 274 | case UART_0: |
sahilmgandhi | 18:6a4db94011d3 | 275 | { |
sahilmgandhi | 18:6a4db94011d3 | 276 | irq_n = (( irq == TxIrq ) ? UARTTX0_IRQn : UARTRX0_IRQn); |
sahilmgandhi | 18:6a4db94011d3 | 277 | vector = (uint32_t)&uart0_irq; |
sahilmgandhi | 18:6a4db94011d3 | 278 | } |
sahilmgandhi | 18:6a4db94011d3 | 279 | break; |
sahilmgandhi | 18:6a4db94011d3 | 280 | |
sahilmgandhi | 18:6a4db94011d3 | 281 | case UART_1: |
sahilmgandhi | 18:6a4db94011d3 | 282 | { |
sahilmgandhi | 18:6a4db94011d3 | 283 | irq_n = (( irq == TxIrq ) ? UARTTX1_IRQn : UARTRX1_IRQn); |
sahilmgandhi | 18:6a4db94011d3 | 284 | vector = (uint32_t)&uart1_irq; |
sahilmgandhi | 18:6a4db94011d3 | 285 | } |
sahilmgandhi | 18:6a4db94011d3 | 286 | break; |
sahilmgandhi | 18:6a4db94011d3 | 287 | case UART_2: |
sahilmgandhi | 18:6a4db94011d3 | 288 | { |
sahilmgandhi | 18:6a4db94011d3 | 289 | irq_n = (( irq == TxIrq ) ? UARTTX3_IRQn : UARTRX3_IRQn); |
sahilmgandhi | 18:6a4db94011d3 | 290 | vector = (uint32_t)&uart2_irq; |
sahilmgandhi | 18:6a4db94011d3 | 291 | } |
sahilmgandhi | 18:6a4db94011d3 | 292 | break; |
sahilmgandhi | 18:6a4db94011d3 | 293 | case UART_3: |
sahilmgandhi | 18:6a4db94011d3 | 294 | { |
sahilmgandhi | 18:6a4db94011d3 | 295 | irq_n = (( irq == TxIrq ) ? UARTTX4_IRQn : UARTRX4_IRQn); |
sahilmgandhi | 18:6a4db94011d3 | 296 | vector = (uint32_t)&uart3_irq; |
sahilmgandhi | 18:6a4db94011d3 | 297 | } |
sahilmgandhi | 18:6a4db94011d3 | 298 | break; |
sahilmgandhi | 18:6a4db94011d3 | 299 | } |
sahilmgandhi | 18:6a4db94011d3 | 300 | |
sahilmgandhi | 18:6a4db94011d3 | 301 | if (enable) |
sahilmgandhi | 18:6a4db94011d3 | 302 | { |
sahilmgandhi | 18:6a4db94011d3 | 303 | if( irq == TxIrq ) |
sahilmgandhi | 18:6a4db94011d3 | 304 | { |
sahilmgandhi | 18:6a4db94011d3 | 305 | /* Transmit IRQ, set appripriate enable */ |
sahilmgandhi | 18:6a4db94011d3 | 306 | |
sahilmgandhi | 18:6a4db94011d3 | 307 | /* set TX interrupt enable in CTRL REG */ |
sahilmgandhi | 18:6a4db94011d3 | 308 | obj->uart->CTRL |= CMSDK_UART_CTRL_TXIRQEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 309 | } |
sahilmgandhi | 18:6a4db94011d3 | 310 | else |
sahilmgandhi | 18:6a4db94011d3 | 311 | { |
sahilmgandhi | 18:6a4db94011d3 | 312 | /* set Rx interrupt on in CTRL REG */ |
sahilmgandhi | 18:6a4db94011d3 | 313 | obj->uart->CTRL |= CMSDK_UART_CTRL_RXIRQEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 314 | } |
sahilmgandhi | 18:6a4db94011d3 | 315 | NVIC_SetVector(irq_n, vector); |
sahilmgandhi | 18:6a4db94011d3 | 316 | NVIC_EnableIRQ(irq_n); |
sahilmgandhi | 18:6a4db94011d3 | 317 | |
sahilmgandhi | 18:6a4db94011d3 | 318 | } |
sahilmgandhi | 18:6a4db94011d3 | 319 | else |
sahilmgandhi | 18:6a4db94011d3 | 320 | { /* Disable IRQ */ |
sahilmgandhi | 18:6a4db94011d3 | 321 | |
sahilmgandhi | 18:6a4db94011d3 | 322 | obj->uart->CTRL &= ~(1 << (irq + 2)); |
sahilmgandhi | 18:6a4db94011d3 | 323 | |
sahilmgandhi | 18:6a4db94011d3 | 324 | NVIC_DisableIRQ(irq_n); |
sahilmgandhi | 18:6a4db94011d3 | 325 | } |
sahilmgandhi | 18:6a4db94011d3 | 326 | } |
sahilmgandhi | 18:6a4db94011d3 | 327 | |
sahilmgandhi | 18:6a4db94011d3 | 328 | void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) { |
sahilmgandhi | 18:6a4db94011d3 | 329 | serial_irq_set_internal(obj, irq, enable); |
sahilmgandhi | 18:6a4db94011d3 | 330 | } |
sahilmgandhi | 18:6a4db94011d3 | 331 | |
sahilmgandhi | 18:6a4db94011d3 | 332 | /****************************************************************************** |
sahilmgandhi | 18:6a4db94011d3 | 333 | * READ/WRITE |
sahilmgandhi | 18:6a4db94011d3 | 334 | ******************************************************************************/ |
sahilmgandhi | 18:6a4db94011d3 | 335 | int serial_getc(serial_t *obj) { |
sahilmgandhi | 18:6a4db94011d3 | 336 | while (serial_readable(obj) == 0); |
sahilmgandhi | 18:6a4db94011d3 | 337 | int data = obj->uart->DATA; |
sahilmgandhi | 18:6a4db94011d3 | 338 | return data; |
sahilmgandhi | 18:6a4db94011d3 | 339 | } |
sahilmgandhi | 18:6a4db94011d3 | 340 | |
sahilmgandhi | 18:6a4db94011d3 | 341 | void serial_putc(serial_t *obj, int c) { |
sahilmgandhi | 18:6a4db94011d3 | 342 | while (serial_writable(obj)); |
sahilmgandhi | 18:6a4db94011d3 | 343 | obj->uart->DATA = c; |
sahilmgandhi | 18:6a4db94011d3 | 344 | } |
sahilmgandhi | 18:6a4db94011d3 | 345 | |
sahilmgandhi | 18:6a4db94011d3 | 346 | int serial_readable(serial_t *obj) { |
sahilmgandhi | 18:6a4db94011d3 | 347 | return obj->uart->STATE & 0x2; |
sahilmgandhi | 18:6a4db94011d3 | 348 | } |
sahilmgandhi | 18:6a4db94011d3 | 349 | |
sahilmgandhi | 18:6a4db94011d3 | 350 | int serial_writable(serial_t *obj) { |
sahilmgandhi | 18:6a4db94011d3 | 351 | return obj->uart->STATE & 0x1; |
sahilmgandhi | 18:6a4db94011d3 | 352 | } |
sahilmgandhi | 18:6a4db94011d3 | 353 | |
sahilmgandhi | 18:6a4db94011d3 | 354 | void serial_clear(serial_t *obj) { |
sahilmgandhi | 18:6a4db94011d3 | 355 | obj->uart->DATA = 0x00; |
sahilmgandhi | 18:6a4db94011d3 | 356 | } |
sahilmgandhi | 18:6a4db94011d3 | 357 | |
sahilmgandhi | 18:6a4db94011d3 | 358 | void serial_pinout_tx(PinName tx) { |
sahilmgandhi | 18:6a4db94011d3 | 359 | pinmap_pinout(tx, PinMap_UART_TX); |
sahilmgandhi | 18:6a4db94011d3 | 360 | } |
sahilmgandhi | 18:6a4db94011d3 | 361 | |
sahilmgandhi | 18:6a4db94011d3 | 362 | void serial_break_set(serial_t *obj) { |
sahilmgandhi | 18:6a4db94011d3 | 363 | } |
sahilmgandhi | 18:6a4db94011d3 | 364 | |
sahilmgandhi | 18:6a4db94011d3 | 365 | void serial_break_clear(serial_t *obj) { |
sahilmgandhi | 18:6a4db94011d3 | 366 | } |
sahilmgandhi | 18:6a4db94011d3 | 367 | void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, PinName txflow) { |
sahilmgandhi | 18:6a4db94011d3 | 368 | } |
sahilmgandhi | 18:6a4db94011d3 | 369 |