Mouse code for the MacroRat
mbed-dev/targets/TARGET_NUVOTON/TARGET_NUC472/can_api.c@18:6a4db94011d3, 2017-05-14 (annotated)
- Committer:
- sahilmgandhi
- Date:
- Sun May 14 23:18:57 2017 +0000
- Revision:
- 18:6a4db94011d3
Publishing again
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) 2015-2016 Nuvoton |
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 | |
sahilmgandhi | 18:6a4db94011d3 | 17 | #include "can_api.h" |
sahilmgandhi | 18:6a4db94011d3 | 18 | #include "nuc472_gpio.h" |
sahilmgandhi | 18:6a4db94011d3 | 19 | #include "nuc472_can.h" |
sahilmgandhi | 18:6a4db94011d3 | 20 | |
sahilmgandhi | 18:6a4db94011d3 | 21 | #if DEVICE_CAN |
sahilmgandhi | 18:6a4db94011d3 | 22 | #include <string.h> |
sahilmgandhi | 18:6a4db94011d3 | 23 | #include "cmsis.h" |
sahilmgandhi | 18:6a4db94011d3 | 24 | #include "pinmap.h" |
sahilmgandhi | 18:6a4db94011d3 | 25 | #include "PeripheralPins.h" |
sahilmgandhi | 18:6a4db94011d3 | 26 | #include "nu_modutil.h" |
sahilmgandhi | 18:6a4db94011d3 | 27 | #include "nu_miscutil.h" |
sahilmgandhi | 18:6a4db94011d3 | 28 | #include "nu_bitutil.h" |
sahilmgandhi | 18:6a4db94011d3 | 29 | #include "mbed_critical.h" |
sahilmgandhi | 18:6a4db94011d3 | 30 | |
sahilmgandhi | 18:6a4db94011d3 | 31 | #define NU_CAN_DEBUG 0 |
sahilmgandhi | 18:6a4db94011d3 | 32 | #define CAN_NUM 2 |
sahilmgandhi | 18:6a4db94011d3 | 33 | |
sahilmgandhi | 18:6a4db94011d3 | 34 | static uint32_t can_irq_ids[CAN_NUM] = {0}; |
sahilmgandhi | 18:6a4db94011d3 | 35 | static can_irq_handler can0_irq_handler; |
sahilmgandhi | 18:6a4db94011d3 | 36 | static can_irq_handler can1_irq_handler; |
sahilmgandhi | 18:6a4db94011d3 | 37 | |
sahilmgandhi | 18:6a4db94011d3 | 38 | |
sahilmgandhi | 18:6a4db94011d3 | 39 | static const struct nu_modinit_s can_modinit_tab[] = { |
sahilmgandhi | 18:6a4db94011d3 | 40 | {CAN_0, CAN0_MODULE, 0, 0, CAN0_RST, CAN0_IRQn, NULL}, |
sahilmgandhi | 18:6a4db94011d3 | 41 | {CAN_1, CAN1_MODULE, 0, 0, CAN1_RST, CAN1_IRQn, NULL}, |
sahilmgandhi | 18:6a4db94011d3 | 42 | |
sahilmgandhi | 18:6a4db94011d3 | 43 | {NC, 0, 0, 0, 0, (IRQn_Type) 0, NULL} |
sahilmgandhi | 18:6a4db94011d3 | 44 | }; |
sahilmgandhi | 18:6a4db94011d3 | 45 | |
sahilmgandhi | 18:6a4db94011d3 | 46 | |
sahilmgandhi | 18:6a4db94011d3 | 47 | void can_init(can_t *obj, PinName rd, PinName td) |
sahilmgandhi | 18:6a4db94011d3 | 48 | { |
sahilmgandhi | 18:6a4db94011d3 | 49 | uint32_t can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD); |
sahilmgandhi | 18:6a4db94011d3 | 50 | uint32_t can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD); |
sahilmgandhi | 18:6a4db94011d3 | 51 | obj->can = (CANName)pinmap_merge(can_td, can_rd); |
sahilmgandhi | 18:6a4db94011d3 | 52 | MBED_ASSERT((int)obj->can != NC); |
sahilmgandhi | 18:6a4db94011d3 | 53 | |
sahilmgandhi | 18:6a4db94011d3 | 54 | const struct nu_modinit_s *modinit = get_modinit(obj->can, can_modinit_tab); |
sahilmgandhi | 18:6a4db94011d3 | 55 | MBED_ASSERT(modinit != NULL); |
sahilmgandhi | 18:6a4db94011d3 | 56 | MBED_ASSERT(modinit->modname == obj->can); |
sahilmgandhi | 18:6a4db94011d3 | 57 | |
sahilmgandhi | 18:6a4db94011d3 | 58 | // Reset this module |
sahilmgandhi | 18:6a4db94011d3 | 59 | SYS_ResetModule(modinit->rsetidx); |
sahilmgandhi | 18:6a4db94011d3 | 60 | |
sahilmgandhi | 18:6a4db94011d3 | 61 | // Enable IP clock |
sahilmgandhi | 18:6a4db94011d3 | 62 | CLK_EnableModuleClock(modinit->clkidx); |
sahilmgandhi | 18:6a4db94011d3 | 63 | |
sahilmgandhi | 18:6a4db94011d3 | 64 | if(obj->can == CAN_1) { |
sahilmgandhi | 18:6a4db94011d3 | 65 | obj->index = 1; |
sahilmgandhi | 18:6a4db94011d3 | 66 | } |
sahilmgandhi | 18:6a4db94011d3 | 67 | else |
sahilmgandhi | 18:6a4db94011d3 | 68 | obj->index = 0; |
sahilmgandhi | 18:6a4db94011d3 | 69 | |
sahilmgandhi | 18:6a4db94011d3 | 70 | pinmap_pinout(td, PinMap_CAN_TD); |
sahilmgandhi | 18:6a4db94011d3 | 71 | pinmap_pinout(rd, PinMap_CAN_RD); |
sahilmgandhi | 18:6a4db94011d3 | 72 | |
sahilmgandhi | 18:6a4db94011d3 | 73 | /* For NCU 472 mbed Board Transmitter Setting (RS Pin) */ |
sahilmgandhi | 18:6a4db94011d3 | 74 | GPIO_SetMode(PA, BIT2| BIT3, GPIO_MODE_OUTPUT); |
sahilmgandhi | 18:6a4db94011d3 | 75 | PA2 = 0x00; |
sahilmgandhi | 18:6a4db94011d3 | 76 | PA3 = 0x00; |
sahilmgandhi | 18:6a4db94011d3 | 77 | |
sahilmgandhi | 18:6a4db94011d3 | 78 | CAN_Open((CAN_T *)NU_MODBASE(obj->can), 500000, CAN_NORMAL_MODE); |
sahilmgandhi | 18:6a4db94011d3 | 79 | |
sahilmgandhi | 18:6a4db94011d3 | 80 | can_filter(obj, 0, 0, CANStandard, 0); |
sahilmgandhi | 18:6a4db94011d3 | 81 | } |
sahilmgandhi | 18:6a4db94011d3 | 82 | |
sahilmgandhi | 18:6a4db94011d3 | 83 | |
sahilmgandhi | 18:6a4db94011d3 | 84 | void can_free(can_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 85 | { |
sahilmgandhi | 18:6a4db94011d3 | 86 | |
sahilmgandhi | 18:6a4db94011d3 | 87 | const struct nu_modinit_s *modinit = get_modinit(obj->can, can_modinit_tab); |
sahilmgandhi | 18:6a4db94011d3 | 88 | |
sahilmgandhi | 18:6a4db94011d3 | 89 | MBED_ASSERT(modinit != NULL); |
sahilmgandhi | 18:6a4db94011d3 | 90 | MBED_ASSERT(modinit->modname == obj->can); |
sahilmgandhi | 18:6a4db94011d3 | 91 | |
sahilmgandhi | 18:6a4db94011d3 | 92 | // Reset this module |
sahilmgandhi | 18:6a4db94011d3 | 93 | SYS_ResetModule(modinit->rsetidx); |
sahilmgandhi | 18:6a4db94011d3 | 94 | |
sahilmgandhi | 18:6a4db94011d3 | 95 | CLK_DisableModuleClock(modinit->clkidx); |
sahilmgandhi | 18:6a4db94011d3 | 96 | } |
sahilmgandhi | 18:6a4db94011d3 | 97 | |
sahilmgandhi | 18:6a4db94011d3 | 98 | int can_frequency(can_t *obj, int hz) |
sahilmgandhi | 18:6a4db94011d3 | 99 | { |
sahilmgandhi | 18:6a4db94011d3 | 100 | CAN_SetBaudRate((CAN_T *)NU_MODBASE(obj->can), hz); |
sahilmgandhi | 18:6a4db94011d3 | 101 | |
sahilmgandhi | 18:6a4db94011d3 | 102 | return CAN_GetCANBitRate((CAN_T *)NU_MODBASE(obj->can)); |
sahilmgandhi | 18:6a4db94011d3 | 103 | } |
sahilmgandhi | 18:6a4db94011d3 | 104 | |
sahilmgandhi | 18:6a4db94011d3 | 105 | static void can_irq(CANName name, int id) |
sahilmgandhi | 18:6a4db94011d3 | 106 | { |
sahilmgandhi | 18:6a4db94011d3 | 107 | |
sahilmgandhi | 18:6a4db94011d3 | 108 | CAN_T *can = (CAN_T *)NU_MODBASE(name); |
sahilmgandhi | 18:6a4db94011d3 | 109 | uint32_t u8IIDRstatus; |
sahilmgandhi | 18:6a4db94011d3 | 110 | |
sahilmgandhi | 18:6a4db94011d3 | 111 | u8IIDRstatus = can->IIDR; |
sahilmgandhi | 18:6a4db94011d3 | 112 | |
sahilmgandhi | 18:6a4db94011d3 | 113 | if(u8IIDRstatus == 0x00008000) { /* Check Status Interrupt Flag (Error status Int and Status change Int) */ |
sahilmgandhi | 18:6a4db94011d3 | 114 | /**************************/ |
sahilmgandhi | 18:6a4db94011d3 | 115 | /* Status Change interrupt*/ |
sahilmgandhi | 18:6a4db94011d3 | 116 | /**************************/ |
sahilmgandhi | 18:6a4db94011d3 | 117 | if(can->STATUS & CAN_STATUS_RXOK_Msk) { |
sahilmgandhi | 18:6a4db94011d3 | 118 | can->STATUS &= ~CAN_STATUS_RXOK_Msk; /* Clear Rx Ok status*/ |
sahilmgandhi | 18:6a4db94011d3 | 119 | if(id) |
sahilmgandhi | 18:6a4db94011d3 | 120 | can1_irq_handler(can_irq_ids[id] , IRQ_RX); |
sahilmgandhi | 18:6a4db94011d3 | 121 | else |
sahilmgandhi | 18:6a4db94011d3 | 122 | can0_irq_handler(can_irq_ids[id], IRQ_RX); |
sahilmgandhi | 18:6a4db94011d3 | 123 | } |
sahilmgandhi | 18:6a4db94011d3 | 124 | |
sahilmgandhi | 18:6a4db94011d3 | 125 | if(can->STATUS & CAN_STATUS_TXOK_Msk) { |
sahilmgandhi | 18:6a4db94011d3 | 126 | can->STATUS &= ~CAN_STATUS_TXOK_Msk; /* Clear Tx Ok status*/ |
sahilmgandhi | 18:6a4db94011d3 | 127 | if(id) |
sahilmgandhi | 18:6a4db94011d3 | 128 | can1_irq_handler(can_irq_ids[id] , IRQ_TX); |
sahilmgandhi | 18:6a4db94011d3 | 129 | else |
sahilmgandhi | 18:6a4db94011d3 | 130 | can0_irq_handler(can_irq_ids[id], IRQ_TX); |
sahilmgandhi | 18:6a4db94011d3 | 131 | |
sahilmgandhi | 18:6a4db94011d3 | 132 | } |
sahilmgandhi | 18:6a4db94011d3 | 133 | |
sahilmgandhi | 18:6a4db94011d3 | 134 | /**************************/ |
sahilmgandhi | 18:6a4db94011d3 | 135 | /* Error Status interrupt */ |
sahilmgandhi | 18:6a4db94011d3 | 136 | /**************************/ |
sahilmgandhi | 18:6a4db94011d3 | 137 | if(can->STATUS & CAN_STATUS_EWARN_Msk) { |
sahilmgandhi | 18:6a4db94011d3 | 138 | if(id) |
sahilmgandhi | 18:6a4db94011d3 | 139 | can1_irq_handler(can_irq_ids[id] , IRQ_ERROR); |
sahilmgandhi | 18:6a4db94011d3 | 140 | else |
sahilmgandhi | 18:6a4db94011d3 | 141 | can0_irq_handler(can_irq_ids[id], IRQ_ERROR); |
sahilmgandhi | 18:6a4db94011d3 | 142 | } |
sahilmgandhi | 18:6a4db94011d3 | 143 | |
sahilmgandhi | 18:6a4db94011d3 | 144 | if(can->STATUS & CAN_STATUS_BOFF_Msk) { |
sahilmgandhi | 18:6a4db94011d3 | 145 | if(id) |
sahilmgandhi | 18:6a4db94011d3 | 146 | can1_irq_handler(can_irq_ids[id] , IRQ_BUS); |
sahilmgandhi | 18:6a4db94011d3 | 147 | else |
sahilmgandhi | 18:6a4db94011d3 | 148 | can0_irq_handler(can_irq_ids[id], IRQ_BUS); |
sahilmgandhi | 18:6a4db94011d3 | 149 | } |
sahilmgandhi | 18:6a4db94011d3 | 150 | } else if (u8IIDRstatus!=0) { |
sahilmgandhi | 18:6a4db94011d3 | 151 | |
sahilmgandhi | 18:6a4db94011d3 | 152 | if(id) |
sahilmgandhi | 18:6a4db94011d3 | 153 | can1_irq_handler(can_irq_ids[id] , IRQ_OVERRUN); |
sahilmgandhi | 18:6a4db94011d3 | 154 | else |
sahilmgandhi | 18:6a4db94011d3 | 155 | can0_irq_handler(can_irq_ids[id], IRQ_OVERRUN); |
sahilmgandhi | 18:6a4db94011d3 | 156 | |
sahilmgandhi | 18:6a4db94011d3 | 157 | CAN_CLR_INT_PENDING_BIT(can, ((can->IIDR) -1)); /* Clear Interrupt Pending */ |
sahilmgandhi | 18:6a4db94011d3 | 158 | |
sahilmgandhi | 18:6a4db94011d3 | 159 | } else if(can->WU_STATUS == 1) { |
sahilmgandhi | 18:6a4db94011d3 | 160 | |
sahilmgandhi | 18:6a4db94011d3 | 161 | can->WU_STATUS = 0; /* Write '0' to clear */ |
sahilmgandhi | 18:6a4db94011d3 | 162 | if(id) |
sahilmgandhi | 18:6a4db94011d3 | 163 | can1_irq_handler(can_irq_ids[id] , IRQ_WAKEUP); |
sahilmgandhi | 18:6a4db94011d3 | 164 | else |
sahilmgandhi | 18:6a4db94011d3 | 165 | can0_irq_handler(can_irq_ids[id], IRQ_WAKEUP); |
sahilmgandhi | 18:6a4db94011d3 | 166 | } |
sahilmgandhi | 18:6a4db94011d3 | 167 | } |
sahilmgandhi | 18:6a4db94011d3 | 168 | |
sahilmgandhi | 18:6a4db94011d3 | 169 | void CAN0_IRQHandler(void) |
sahilmgandhi | 18:6a4db94011d3 | 170 | { |
sahilmgandhi | 18:6a4db94011d3 | 171 | can_irq(CAN_0, 0); |
sahilmgandhi | 18:6a4db94011d3 | 172 | } |
sahilmgandhi | 18:6a4db94011d3 | 173 | |
sahilmgandhi | 18:6a4db94011d3 | 174 | void CAN1_IRQHandler(void) |
sahilmgandhi | 18:6a4db94011d3 | 175 | { |
sahilmgandhi | 18:6a4db94011d3 | 176 | can_irq(CAN_1, 1); |
sahilmgandhi | 18:6a4db94011d3 | 177 | } |
sahilmgandhi | 18:6a4db94011d3 | 178 | |
sahilmgandhi | 18:6a4db94011d3 | 179 | void can_irq_init(can_t *obj, can_irq_handler handler, uint32_t id) |
sahilmgandhi | 18:6a4db94011d3 | 180 | { |
sahilmgandhi | 18:6a4db94011d3 | 181 | if(obj->index) |
sahilmgandhi | 18:6a4db94011d3 | 182 | can1_irq_handler = handler; |
sahilmgandhi | 18:6a4db94011d3 | 183 | else |
sahilmgandhi | 18:6a4db94011d3 | 184 | can0_irq_handler = handler; |
sahilmgandhi | 18:6a4db94011d3 | 185 | can_irq_ids[obj->index] = id; |
sahilmgandhi | 18:6a4db94011d3 | 186 | |
sahilmgandhi | 18:6a4db94011d3 | 187 | } |
sahilmgandhi | 18:6a4db94011d3 | 188 | |
sahilmgandhi | 18:6a4db94011d3 | 189 | void can_irq_free(can_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 190 | { |
sahilmgandhi | 18:6a4db94011d3 | 191 | CAN_DisableInt((CAN_T *)NU_MODBASE(obj->can), (CAN_CON_IE_Msk|CAN_CON_SIE_Msk|CAN_CON_EIE_Msk)); |
sahilmgandhi | 18:6a4db94011d3 | 192 | |
sahilmgandhi | 18:6a4db94011d3 | 193 | can_irq_ids[obj->index] = 0; |
sahilmgandhi | 18:6a4db94011d3 | 194 | |
sahilmgandhi | 18:6a4db94011d3 | 195 | if(!obj->index) |
sahilmgandhi | 18:6a4db94011d3 | 196 | NVIC_DisableIRQ(CAN0_IRQn); |
sahilmgandhi | 18:6a4db94011d3 | 197 | else |
sahilmgandhi | 18:6a4db94011d3 | 198 | NVIC_DisableIRQ(CAN1_IRQn); |
sahilmgandhi | 18:6a4db94011d3 | 199 | |
sahilmgandhi | 18:6a4db94011d3 | 200 | |
sahilmgandhi | 18:6a4db94011d3 | 201 | } |
sahilmgandhi | 18:6a4db94011d3 | 202 | |
sahilmgandhi | 18:6a4db94011d3 | 203 | void can_irq_set(can_t *obj, CanIrqType irq, uint32_t enable) |
sahilmgandhi | 18:6a4db94011d3 | 204 | { |
sahilmgandhi | 18:6a4db94011d3 | 205 | CAN_T *can_base = (CAN_T *) NU_MODBASE(obj->can); |
sahilmgandhi | 18:6a4db94011d3 | 206 | |
sahilmgandhi | 18:6a4db94011d3 | 207 | CAN_EnterInitMode((CAN_T*)can_base); |
sahilmgandhi | 18:6a4db94011d3 | 208 | |
sahilmgandhi | 18:6a4db94011d3 | 209 | ((CAN_T *)can_base)->CON = (((CAN_T *)can_base)->CON ) | ((enable != 0 )? CAN_CON_IE_Msk :0); |
sahilmgandhi | 18:6a4db94011d3 | 210 | |
sahilmgandhi | 18:6a4db94011d3 | 211 | switch (irq) |
sahilmgandhi | 18:6a4db94011d3 | 212 | { |
sahilmgandhi | 18:6a4db94011d3 | 213 | case IRQ_ERROR: |
sahilmgandhi | 18:6a4db94011d3 | 214 | case IRQ_BUS: |
sahilmgandhi | 18:6a4db94011d3 | 215 | case IRQ_PASSIVE: |
sahilmgandhi | 18:6a4db94011d3 | 216 | can_base->CON = can_base->CON |CAN_CON_EIE_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 217 | can_base->CON = can_base->CON |CAN_CON_SIE_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 218 | break; |
sahilmgandhi | 18:6a4db94011d3 | 219 | |
sahilmgandhi | 18:6a4db94011d3 | 220 | case IRQ_RX: |
sahilmgandhi | 18:6a4db94011d3 | 221 | case IRQ_TX: |
sahilmgandhi | 18:6a4db94011d3 | 222 | case IRQ_OVERRUN: |
sahilmgandhi | 18:6a4db94011d3 | 223 | case IRQ_WAKEUP: |
sahilmgandhi | 18:6a4db94011d3 | 224 | can_base->CON = can_base->CON |CAN_CON_SIE_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 225 | break; |
sahilmgandhi | 18:6a4db94011d3 | 226 | |
sahilmgandhi | 18:6a4db94011d3 | 227 | default: |
sahilmgandhi | 18:6a4db94011d3 | 228 | break; |
sahilmgandhi | 18:6a4db94011d3 | 229 | |
sahilmgandhi | 18:6a4db94011d3 | 230 | } |
sahilmgandhi | 18:6a4db94011d3 | 231 | |
sahilmgandhi | 18:6a4db94011d3 | 232 | CAN_LeaveInitMode(can_base); |
sahilmgandhi | 18:6a4db94011d3 | 233 | |
sahilmgandhi | 18:6a4db94011d3 | 234 | if(!obj->index) |
sahilmgandhi | 18:6a4db94011d3 | 235 | { |
sahilmgandhi | 18:6a4db94011d3 | 236 | NVIC_SetVector(CAN0_IRQn, (uint32_t)&CAN0_IRQHandler); |
sahilmgandhi | 18:6a4db94011d3 | 237 | NVIC_EnableIRQ(CAN0_IRQn); |
sahilmgandhi | 18:6a4db94011d3 | 238 | } |
sahilmgandhi | 18:6a4db94011d3 | 239 | else |
sahilmgandhi | 18:6a4db94011d3 | 240 | { |
sahilmgandhi | 18:6a4db94011d3 | 241 | NVIC_SetVector(CAN1_IRQn, (uint32_t)&CAN1_IRQHandler); |
sahilmgandhi | 18:6a4db94011d3 | 242 | NVIC_EnableIRQ(CAN1_IRQn); |
sahilmgandhi | 18:6a4db94011d3 | 243 | } |
sahilmgandhi | 18:6a4db94011d3 | 244 | |
sahilmgandhi | 18:6a4db94011d3 | 245 | } |
sahilmgandhi | 18:6a4db94011d3 | 246 | |
sahilmgandhi | 18:6a4db94011d3 | 247 | int can_write(can_t *obj, CAN_Message msg, int cc) |
sahilmgandhi | 18:6a4db94011d3 | 248 | { |
sahilmgandhi | 18:6a4db94011d3 | 249 | STR_CANMSG_T CMsg; |
sahilmgandhi | 18:6a4db94011d3 | 250 | |
sahilmgandhi | 18:6a4db94011d3 | 251 | CMsg.IdType = (uint32_t)msg.format; |
sahilmgandhi | 18:6a4db94011d3 | 252 | CMsg.FrameType = (uint32_t)!msg.type; |
sahilmgandhi | 18:6a4db94011d3 | 253 | CMsg.Id = msg.id; |
sahilmgandhi | 18:6a4db94011d3 | 254 | CMsg.DLC = msg.len; |
sahilmgandhi | 18:6a4db94011d3 | 255 | memcpy((void *)&CMsg.Data[0],(const void *)&msg.data[0], (unsigned int)8); |
sahilmgandhi | 18:6a4db94011d3 | 256 | |
sahilmgandhi | 18:6a4db94011d3 | 257 | return CAN_Transmit((CAN_T *)NU_MODBASE(obj->can), cc, &CMsg); |
sahilmgandhi | 18:6a4db94011d3 | 258 | } |
sahilmgandhi | 18:6a4db94011d3 | 259 | |
sahilmgandhi | 18:6a4db94011d3 | 260 | int can_read(can_t *obj, CAN_Message *msg, int handle) |
sahilmgandhi | 18:6a4db94011d3 | 261 | { |
sahilmgandhi | 18:6a4db94011d3 | 262 | STR_CANMSG_T CMsg; |
sahilmgandhi | 18:6a4db94011d3 | 263 | |
sahilmgandhi | 18:6a4db94011d3 | 264 | if(!CAN_Receive((CAN_T *)NU_MODBASE(obj->can), handle, &CMsg)) |
sahilmgandhi | 18:6a4db94011d3 | 265 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 266 | |
sahilmgandhi | 18:6a4db94011d3 | 267 | msg->format = (CANFormat)CMsg.IdType; |
sahilmgandhi | 18:6a4db94011d3 | 268 | msg->type = (CANType)!CMsg.FrameType; |
sahilmgandhi | 18:6a4db94011d3 | 269 | msg->id = CMsg.Id; |
sahilmgandhi | 18:6a4db94011d3 | 270 | msg->len = CMsg.DLC; |
sahilmgandhi | 18:6a4db94011d3 | 271 | memcpy(&msg->data[0], &CMsg.Data[0], 8); |
sahilmgandhi | 18:6a4db94011d3 | 272 | |
sahilmgandhi | 18:6a4db94011d3 | 273 | return 1; |
sahilmgandhi | 18:6a4db94011d3 | 274 | } |
sahilmgandhi | 18:6a4db94011d3 | 275 | |
sahilmgandhi | 18:6a4db94011d3 | 276 | int can_mode(can_t *obj, CanMode mode) |
sahilmgandhi | 18:6a4db94011d3 | 277 | { |
sahilmgandhi | 18:6a4db94011d3 | 278 | CAN_T *can_base = (CAN_T *) NU_MODBASE(obj->can); |
sahilmgandhi | 18:6a4db94011d3 | 279 | |
sahilmgandhi | 18:6a4db94011d3 | 280 | int success = 0; |
sahilmgandhi | 18:6a4db94011d3 | 281 | switch (mode) |
sahilmgandhi | 18:6a4db94011d3 | 282 | { |
sahilmgandhi | 18:6a4db94011d3 | 283 | case MODE_RESET: |
sahilmgandhi | 18:6a4db94011d3 | 284 | CAN_LeaveTestMode(can_base); |
sahilmgandhi | 18:6a4db94011d3 | 285 | success = 1; |
sahilmgandhi | 18:6a4db94011d3 | 286 | break; |
sahilmgandhi | 18:6a4db94011d3 | 287 | |
sahilmgandhi | 18:6a4db94011d3 | 288 | case MODE_NORMAL: |
sahilmgandhi | 18:6a4db94011d3 | 289 | CAN_EnterTestMode(can_base, CAN_TEST_BASIC_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 290 | success = 1; |
sahilmgandhi | 18:6a4db94011d3 | 291 | break; |
sahilmgandhi | 18:6a4db94011d3 | 292 | |
sahilmgandhi | 18:6a4db94011d3 | 293 | case MODE_SILENT: |
sahilmgandhi | 18:6a4db94011d3 | 294 | CAN_EnterTestMode(can_base, CAN_TEST_SILENT_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 295 | success = 1; |
sahilmgandhi | 18:6a4db94011d3 | 296 | break; |
sahilmgandhi | 18:6a4db94011d3 | 297 | |
sahilmgandhi | 18:6a4db94011d3 | 298 | case MODE_TEST_LOCAL: |
sahilmgandhi | 18:6a4db94011d3 | 299 | case MODE_TEST_GLOBAL: |
sahilmgandhi | 18:6a4db94011d3 | 300 | CAN_EnterTestMode(can_base, CAN_TEST_LBACK_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 301 | success = 1; |
sahilmgandhi | 18:6a4db94011d3 | 302 | break; |
sahilmgandhi | 18:6a4db94011d3 | 303 | |
sahilmgandhi | 18:6a4db94011d3 | 304 | case MODE_TEST_SILENT: |
sahilmgandhi | 18:6a4db94011d3 | 305 | CAN_EnterTestMode(can_base, CAN_TEST_SILENT_Msk | CAN_TEST_LBACK_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 306 | success = 1; |
sahilmgandhi | 18:6a4db94011d3 | 307 | break; |
sahilmgandhi | 18:6a4db94011d3 | 308 | |
sahilmgandhi | 18:6a4db94011d3 | 309 | default: |
sahilmgandhi | 18:6a4db94011d3 | 310 | success = 0; |
sahilmgandhi | 18:6a4db94011d3 | 311 | break; |
sahilmgandhi | 18:6a4db94011d3 | 312 | |
sahilmgandhi | 18:6a4db94011d3 | 313 | } |
sahilmgandhi | 18:6a4db94011d3 | 314 | |
sahilmgandhi | 18:6a4db94011d3 | 315 | |
sahilmgandhi | 18:6a4db94011d3 | 316 | return success; |
sahilmgandhi | 18:6a4db94011d3 | 317 | } |
sahilmgandhi | 18:6a4db94011d3 | 318 | |
sahilmgandhi | 18:6a4db94011d3 | 319 | int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) |
sahilmgandhi | 18:6a4db94011d3 | 320 | { |
sahilmgandhi | 18:6a4db94011d3 | 321 | return CAN_SetRxMsg((CAN_T *)NU_MODBASE(obj->can), handle , (uint32_t)format, id); |
sahilmgandhi | 18:6a4db94011d3 | 322 | } |
sahilmgandhi | 18:6a4db94011d3 | 323 | |
sahilmgandhi | 18:6a4db94011d3 | 324 | |
sahilmgandhi | 18:6a4db94011d3 | 325 | void can_reset(can_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 326 | { |
sahilmgandhi | 18:6a4db94011d3 | 327 | const struct nu_modinit_s *modinit = get_modinit(obj->can, can_modinit_tab); |
sahilmgandhi | 18:6a4db94011d3 | 328 | |
sahilmgandhi | 18:6a4db94011d3 | 329 | MBED_ASSERT(modinit != NULL); |
sahilmgandhi | 18:6a4db94011d3 | 330 | MBED_ASSERT(modinit->modname == obj->can); |
sahilmgandhi | 18:6a4db94011d3 | 331 | |
sahilmgandhi | 18:6a4db94011d3 | 332 | // Reset this module |
sahilmgandhi | 18:6a4db94011d3 | 333 | SYS_ResetModule(modinit->rsetidx); |
sahilmgandhi | 18:6a4db94011d3 | 334 | |
sahilmgandhi | 18:6a4db94011d3 | 335 | } |
sahilmgandhi | 18:6a4db94011d3 | 336 | |
sahilmgandhi | 18:6a4db94011d3 | 337 | unsigned char can_rderror(can_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 338 | { |
sahilmgandhi | 18:6a4db94011d3 | 339 | CAN_T *can = (CAN_T *)NU_MODBASE(obj->can); |
sahilmgandhi | 18:6a4db94011d3 | 340 | return ((can->ERR>>8)&0xFF); |
sahilmgandhi | 18:6a4db94011d3 | 341 | } |
sahilmgandhi | 18:6a4db94011d3 | 342 | |
sahilmgandhi | 18:6a4db94011d3 | 343 | unsigned char can_tderror(can_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 344 | { |
sahilmgandhi | 18:6a4db94011d3 | 345 | CAN_T *can = (CAN_T *)NU_MODBASE(obj->can); |
sahilmgandhi | 18:6a4db94011d3 | 346 | return ((can->ERR)&0xFF); |
sahilmgandhi | 18:6a4db94011d3 | 347 | } |
sahilmgandhi | 18:6a4db94011d3 | 348 | |
sahilmgandhi | 18:6a4db94011d3 | 349 | void can_monitor(can_t *obj, int silent) |
sahilmgandhi | 18:6a4db94011d3 | 350 | { |
sahilmgandhi | 18:6a4db94011d3 | 351 | CAN_EnterTestMode((CAN_T *)NU_MODBASE(obj->can), CAN_TEST_SILENT_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 352 | } |
sahilmgandhi | 18:6a4db94011d3 | 353 | |
sahilmgandhi | 18:6a4db94011d3 | 354 | #endif // DEVICE_CAN |