mbed library sources. Supersedes mbed-src. Fixed broken STM32F1xx RTC on rtc_api.c
Dependents: Nucleo_F103RB_RTC_battery_bkup_pwr_off_okay
Fork of mbed-dev by
targets/TARGET_NUVOTON/TARGET_M480/can_api.c@177:619788de047e, 2017-11-07 (annotated)
- Committer:
- maxxir
- Date:
- Tue Nov 07 16:46:29 2017 +0000
- Revision:
- 177:619788de047e
- Parent:
- 172:7d866c31b3c5
To fix broken RTC on Nucleo_F103RB / STM32F103 BluePill etc..; Used direct RTC register manipulation for STM32F1xx; rtc_read() && rtc_write() (native rtc_init() - works good); also added stub for non-working on STM32F1xx rtc_read_subseconds().
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AnnaBridge | 172:7d866c31b3c5 | 1 | /* mbed Microcontroller Library |
AnnaBridge | 172:7d866c31b3c5 | 2 | * Copyright (c) 2015-2016 Nuvoton |
AnnaBridge | 172:7d866c31b3c5 | 3 | * |
AnnaBridge | 172:7d866c31b3c5 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AnnaBridge | 172:7d866c31b3c5 | 5 | * you may not use this file except in compliance with the License. |
AnnaBridge | 172:7d866c31b3c5 | 6 | * You may obtain a copy of the License at |
AnnaBridge | 172:7d866c31b3c5 | 7 | * |
AnnaBridge | 172:7d866c31b3c5 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AnnaBridge | 172:7d866c31b3c5 | 9 | * |
AnnaBridge | 172:7d866c31b3c5 | 10 | * Unless required by applicable law or agreed to in writing, software |
AnnaBridge | 172:7d866c31b3c5 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AnnaBridge | 172:7d866c31b3c5 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AnnaBridge | 172:7d866c31b3c5 | 13 | * See the License for the specific language governing permissions and |
AnnaBridge | 172:7d866c31b3c5 | 14 | * limitations under the License. |
AnnaBridge | 172:7d866c31b3c5 | 15 | */ |
AnnaBridge | 172:7d866c31b3c5 | 16 | |
AnnaBridge | 172:7d866c31b3c5 | 17 | #include "can_api.h" |
AnnaBridge | 172:7d866c31b3c5 | 18 | #include "m480_gpio.h" |
AnnaBridge | 172:7d866c31b3c5 | 19 | #include "m480_can.h" |
AnnaBridge | 172:7d866c31b3c5 | 20 | |
AnnaBridge | 172:7d866c31b3c5 | 21 | #if DEVICE_CAN |
AnnaBridge | 172:7d866c31b3c5 | 22 | #include <string.h> |
AnnaBridge | 172:7d866c31b3c5 | 23 | #include "cmsis.h" |
AnnaBridge | 172:7d866c31b3c5 | 24 | #include "pinmap.h" |
AnnaBridge | 172:7d866c31b3c5 | 25 | #include "PeripheralPins.h" |
AnnaBridge | 172:7d866c31b3c5 | 26 | #include "nu_modutil.h" |
AnnaBridge | 172:7d866c31b3c5 | 27 | #include "nu_miscutil.h" |
AnnaBridge | 172:7d866c31b3c5 | 28 | #include "nu_bitutil.h" |
AnnaBridge | 172:7d866c31b3c5 | 29 | #include "mbed_critical.h" |
AnnaBridge | 172:7d866c31b3c5 | 30 | |
AnnaBridge | 172:7d866c31b3c5 | 31 | #define NU_CAN_DEBUG 0 |
AnnaBridge | 172:7d866c31b3c5 | 32 | #define CAN_NUM 2 |
AnnaBridge | 172:7d866c31b3c5 | 33 | |
AnnaBridge | 172:7d866c31b3c5 | 34 | static uint32_t can_irq_ids[CAN_NUM] = {0}; |
AnnaBridge | 172:7d866c31b3c5 | 35 | static can_irq_handler can0_irq_handler; |
AnnaBridge | 172:7d866c31b3c5 | 36 | static can_irq_handler can1_irq_handler; |
AnnaBridge | 172:7d866c31b3c5 | 37 | |
AnnaBridge | 172:7d866c31b3c5 | 38 | extern uint32_t CAN_GetCANBitRate(CAN_T *tCAN); |
AnnaBridge | 172:7d866c31b3c5 | 39 | extern void CAN_EnterInitMode(CAN_T *tCAN, uint8_t u8Mask); |
AnnaBridge | 172:7d866c31b3c5 | 40 | extern void CAN_LeaveInitMode(CAN_T *tCAN); |
AnnaBridge | 172:7d866c31b3c5 | 41 | extern void CAN_LeaveTestMode(CAN_T *tCAN); |
AnnaBridge | 172:7d866c31b3c5 | 42 | extern void CAN_EnterTestMode(CAN_T *tCAN, uint8_t u8TestMask); |
AnnaBridge | 172:7d866c31b3c5 | 43 | |
AnnaBridge | 172:7d866c31b3c5 | 44 | static const struct nu_modinit_s can_modinit_tab[] = { |
AnnaBridge | 172:7d866c31b3c5 | 45 | {CAN_0, CAN0_MODULE, 0, 0, CAN0_RST, CAN0_IRQn, NULL}, |
AnnaBridge | 172:7d866c31b3c5 | 46 | {CAN_1, CAN1_MODULE, 0, 0, CAN1_RST, CAN1_IRQn, NULL}, |
AnnaBridge | 172:7d866c31b3c5 | 47 | |
AnnaBridge | 172:7d866c31b3c5 | 48 | {NC, 0, 0, 0, 0, (IRQn_Type) 0, NULL} |
AnnaBridge | 172:7d866c31b3c5 | 49 | }; |
AnnaBridge | 172:7d866c31b3c5 | 50 | |
AnnaBridge | 172:7d866c31b3c5 | 51 | void can_init_freq(can_t *obj, PinName rd, PinName td, int hz) |
AnnaBridge | 172:7d866c31b3c5 | 52 | { |
AnnaBridge | 172:7d866c31b3c5 | 53 | uint32_t can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD); |
AnnaBridge | 172:7d866c31b3c5 | 54 | uint32_t can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD); |
AnnaBridge | 172:7d866c31b3c5 | 55 | obj->can = (CANName)pinmap_merge(can_td, can_rd); |
AnnaBridge | 172:7d866c31b3c5 | 56 | MBED_ASSERT((int)obj->can != NC); |
AnnaBridge | 172:7d866c31b3c5 | 57 | |
AnnaBridge | 172:7d866c31b3c5 | 58 | const struct nu_modinit_s *modinit = get_modinit(obj->can, can_modinit_tab); |
AnnaBridge | 172:7d866c31b3c5 | 59 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 172:7d866c31b3c5 | 60 | MBED_ASSERT(modinit->modname == (int) obj->can); |
AnnaBridge | 172:7d866c31b3c5 | 61 | |
AnnaBridge | 172:7d866c31b3c5 | 62 | // Reset this module |
AnnaBridge | 172:7d866c31b3c5 | 63 | SYS_ResetModule(modinit->rsetidx); |
AnnaBridge | 172:7d866c31b3c5 | 64 | |
AnnaBridge | 172:7d866c31b3c5 | 65 | NVIC_DisableIRQ(CAN0_IRQn); |
AnnaBridge | 172:7d866c31b3c5 | 66 | NVIC_DisableIRQ(CAN1_IRQn); |
AnnaBridge | 172:7d866c31b3c5 | 67 | |
AnnaBridge | 172:7d866c31b3c5 | 68 | // Enable IP clock |
AnnaBridge | 172:7d866c31b3c5 | 69 | CLK_EnableModuleClock(modinit->clkidx); |
AnnaBridge | 172:7d866c31b3c5 | 70 | |
AnnaBridge | 172:7d866c31b3c5 | 71 | if(obj->can == CAN_1) { |
AnnaBridge | 172:7d866c31b3c5 | 72 | obj->index = 1; |
AnnaBridge | 172:7d866c31b3c5 | 73 | } else |
AnnaBridge | 172:7d866c31b3c5 | 74 | obj->index = 0; |
AnnaBridge | 172:7d866c31b3c5 | 75 | |
AnnaBridge | 172:7d866c31b3c5 | 76 | pinmap_pinout(td, PinMap_CAN_TD); |
AnnaBridge | 172:7d866c31b3c5 | 77 | pinmap_pinout(rd, PinMap_CAN_RD); |
AnnaBridge | 172:7d866c31b3c5 | 78 | #if 0 |
AnnaBridge | 172:7d866c31b3c5 | 79 | /* TBD: For M487 mbed Board Transmitter Setting (RS Pin) */ |
AnnaBridge | 172:7d866c31b3c5 | 80 | GPIO_SetMode(PA, BIT2| BIT3, GPIO_MODE_OUTPUT); |
AnnaBridge | 172:7d866c31b3c5 | 81 | PA2 = 0x00; |
AnnaBridge | 172:7d866c31b3c5 | 82 | PA3 = 0x00; |
AnnaBridge | 172:7d866c31b3c5 | 83 | #endif |
AnnaBridge | 172:7d866c31b3c5 | 84 | CAN_Open((CAN_T *)NU_MODBASE(obj->can), hz, CAN_NORMAL_MODE); |
AnnaBridge | 172:7d866c31b3c5 | 85 | |
AnnaBridge | 172:7d866c31b3c5 | 86 | can_filter(obj, 0, 0, CANStandard, 0); |
AnnaBridge | 172:7d866c31b3c5 | 87 | } |
AnnaBridge | 172:7d866c31b3c5 | 88 | |
AnnaBridge | 172:7d866c31b3c5 | 89 | void can_init(can_t *obj, PinName rd, PinName td) |
AnnaBridge | 172:7d866c31b3c5 | 90 | { |
AnnaBridge | 172:7d866c31b3c5 | 91 | can_init_freq(obj, rd, td, 500000); |
AnnaBridge | 172:7d866c31b3c5 | 92 | } |
AnnaBridge | 172:7d866c31b3c5 | 93 | |
AnnaBridge | 172:7d866c31b3c5 | 94 | void can_free(can_t *obj) |
AnnaBridge | 172:7d866c31b3c5 | 95 | { |
AnnaBridge | 172:7d866c31b3c5 | 96 | |
AnnaBridge | 172:7d866c31b3c5 | 97 | const struct nu_modinit_s *modinit = get_modinit(obj->can, can_modinit_tab); |
AnnaBridge | 172:7d866c31b3c5 | 98 | |
AnnaBridge | 172:7d866c31b3c5 | 99 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 172:7d866c31b3c5 | 100 | MBED_ASSERT(modinit->modname == (int) obj->can); |
AnnaBridge | 172:7d866c31b3c5 | 101 | |
AnnaBridge | 172:7d866c31b3c5 | 102 | // Reset this module |
AnnaBridge | 172:7d866c31b3c5 | 103 | SYS_ResetModule(modinit->rsetidx); |
AnnaBridge | 172:7d866c31b3c5 | 104 | |
AnnaBridge | 172:7d866c31b3c5 | 105 | CLK_DisableModuleClock(modinit->clkidx); |
AnnaBridge | 172:7d866c31b3c5 | 106 | } |
AnnaBridge | 172:7d866c31b3c5 | 107 | |
AnnaBridge | 172:7d866c31b3c5 | 108 | int can_frequency(can_t *obj, int hz) |
AnnaBridge | 172:7d866c31b3c5 | 109 | { |
AnnaBridge | 172:7d866c31b3c5 | 110 | CAN_SetBaudRate((CAN_T *)NU_MODBASE(obj->can), hz); |
AnnaBridge | 172:7d866c31b3c5 | 111 | |
AnnaBridge | 172:7d866c31b3c5 | 112 | return CAN_GetCANBitRate((CAN_T *)NU_MODBASE(obj->can)); |
AnnaBridge | 172:7d866c31b3c5 | 113 | } |
AnnaBridge | 172:7d866c31b3c5 | 114 | |
AnnaBridge | 172:7d866c31b3c5 | 115 | static void can_irq(CANName name, int id) |
AnnaBridge | 172:7d866c31b3c5 | 116 | { |
AnnaBridge | 172:7d866c31b3c5 | 117 | |
AnnaBridge | 172:7d866c31b3c5 | 118 | CAN_T *can = (CAN_T *)NU_MODBASE(name); |
AnnaBridge | 172:7d866c31b3c5 | 119 | uint32_t u8IIDRstatus; |
AnnaBridge | 172:7d866c31b3c5 | 120 | |
AnnaBridge | 172:7d866c31b3c5 | 121 | u8IIDRstatus = can->IIDR; |
AnnaBridge | 172:7d866c31b3c5 | 122 | |
AnnaBridge | 172:7d866c31b3c5 | 123 | if(u8IIDRstatus == 0x00008000) { /* Check Status Interrupt Flag (Error status Int and Status change Int) */ |
AnnaBridge | 172:7d866c31b3c5 | 124 | /**************************/ |
AnnaBridge | 172:7d866c31b3c5 | 125 | /* Status Change interrupt*/ |
AnnaBridge | 172:7d866c31b3c5 | 126 | /**************************/ |
AnnaBridge | 172:7d866c31b3c5 | 127 | if(can->STATUS & CAN_STATUS_RXOK_Msk) { |
AnnaBridge | 172:7d866c31b3c5 | 128 | can->STATUS &= ~CAN_STATUS_RXOK_Msk; /* Clear Rx Ok status*/ |
AnnaBridge | 172:7d866c31b3c5 | 129 | if(id) |
AnnaBridge | 172:7d866c31b3c5 | 130 | can1_irq_handler(can_irq_ids[id], IRQ_RX); |
AnnaBridge | 172:7d866c31b3c5 | 131 | else |
AnnaBridge | 172:7d866c31b3c5 | 132 | can0_irq_handler(can_irq_ids[id], IRQ_RX); |
AnnaBridge | 172:7d866c31b3c5 | 133 | } |
AnnaBridge | 172:7d866c31b3c5 | 134 | |
AnnaBridge | 172:7d866c31b3c5 | 135 | if(can->STATUS & CAN_STATUS_TXOK_Msk) { |
AnnaBridge | 172:7d866c31b3c5 | 136 | can->STATUS &= ~CAN_STATUS_TXOK_Msk; /* Clear Tx Ok status*/ |
AnnaBridge | 172:7d866c31b3c5 | 137 | if(id) |
AnnaBridge | 172:7d866c31b3c5 | 138 | can1_irq_handler(can_irq_ids[id], IRQ_TX); |
AnnaBridge | 172:7d866c31b3c5 | 139 | else |
AnnaBridge | 172:7d866c31b3c5 | 140 | can0_irq_handler(can_irq_ids[id], IRQ_TX); |
AnnaBridge | 172:7d866c31b3c5 | 141 | |
AnnaBridge | 172:7d866c31b3c5 | 142 | } |
AnnaBridge | 172:7d866c31b3c5 | 143 | |
AnnaBridge | 172:7d866c31b3c5 | 144 | /**************************/ |
AnnaBridge | 172:7d866c31b3c5 | 145 | /* Error Status interrupt */ |
AnnaBridge | 172:7d866c31b3c5 | 146 | /**************************/ |
AnnaBridge | 172:7d866c31b3c5 | 147 | if(can->STATUS & CAN_STATUS_EWARN_Msk) { |
AnnaBridge | 172:7d866c31b3c5 | 148 | if(id) |
AnnaBridge | 172:7d866c31b3c5 | 149 | can1_irq_handler(can_irq_ids[id], IRQ_ERROR); |
AnnaBridge | 172:7d866c31b3c5 | 150 | else |
AnnaBridge | 172:7d866c31b3c5 | 151 | can0_irq_handler(can_irq_ids[id], IRQ_ERROR); |
AnnaBridge | 172:7d866c31b3c5 | 152 | } |
AnnaBridge | 172:7d866c31b3c5 | 153 | |
AnnaBridge | 172:7d866c31b3c5 | 154 | if(can->STATUS & CAN_STATUS_BOFF_Msk) { |
AnnaBridge | 172:7d866c31b3c5 | 155 | if(id) |
AnnaBridge | 172:7d866c31b3c5 | 156 | can1_irq_handler(can_irq_ids[id], IRQ_BUS); |
AnnaBridge | 172:7d866c31b3c5 | 157 | else |
AnnaBridge | 172:7d866c31b3c5 | 158 | can0_irq_handler(can_irq_ids[id], IRQ_BUS); |
AnnaBridge | 172:7d866c31b3c5 | 159 | } |
AnnaBridge | 172:7d866c31b3c5 | 160 | } else if (u8IIDRstatus!=0) { |
AnnaBridge | 172:7d866c31b3c5 | 161 | |
AnnaBridge | 172:7d866c31b3c5 | 162 | if(id) |
AnnaBridge | 172:7d866c31b3c5 | 163 | can1_irq_handler(can_irq_ids[id], IRQ_OVERRUN); |
AnnaBridge | 172:7d866c31b3c5 | 164 | else |
AnnaBridge | 172:7d866c31b3c5 | 165 | can0_irq_handler(can_irq_ids[id], IRQ_OVERRUN); |
AnnaBridge | 172:7d866c31b3c5 | 166 | |
AnnaBridge | 172:7d866c31b3c5 | 167 | CAN_CLR_INT_PENDING_BIT(can, ((can->IIDR) -1)); /* Clear Interrupt Pending */ |
AnnaBridge | 172:7d866c31b3c5 | 168 | |
AnnaBridge | 172:7d866c31b3c5 | 169 | } else if(can->WU_STATUS == 1) { |
AnnaBridge | 172:7d866c31b3c5 | 170 | |
AnnaBridge | 172:7d866c31b3c5 | 171 | can->WU_STATUS = 0; /* Write '0' to clear */ |
AnnaBridge | 172:7d866c31b3c5 | 172 | if(id) |
AnnaBridge | 172:7d866c31b3c5 | 173 | can1_irq_handler(can_irq_ids[id], IRQ_WAKEUP); |
AnnaBridge | 172:7d866c31b3c5 | 174 | else |
AnnaBridge | 172:7d866c31b3c5 | 175 | can0_irq_handler(can_irq_ids[id], IRQ_WAKEUP); |
AnnaBridge | 172:7d866c31b3c5 | 176 | } |
AnnaBridge | 172:7d866c31b3c5 | 177 | } |
AnnaBridge | 172:7d866c31b3c5 | 178 | |
AnnaBridge | 172:7d866c31b3c5 | 179 | void CAN0_IRQHandler(void) |
AnnaBridge | 172:7d866c31b3c5 | 180 | { |
AnnaBridge | 172:7d866c31b3c5 | 181 | can_irq(CAN_0, 0); |
AnnaBridge | 172:7d866c31b3c5 | 182 | } |
AnnaBridge | 172:7d866c31b3c5 | 183 | |
AnnaBridge | 172:7d866c31b3c5 | 184 | void CAN1_IRQHandler(void) |
AnnaBridge | 172:7d866c31b3c5 | 185 | { |
AnnaBridge | 172:7d866c31b3c5 | 186 | can_irq(CAN_1, 1); |
AnnaBridge | 172:7d866c31b3c5 | 187 | } |
AnnaBridge | 172:7d866c31b3c5 | 188 | |
AnnaBridge | 172:7d866c31b3c5 | 189 | void can_irq_init(can_t *obj, can_irq_handler handler, uint32_t id) |
AnnaBridge | 172:7d866c31b3c5 | 190 | { |
AnnaBridge | 172:7d866c31b3c5 | 191 | if(obj->index) |
AnnaBridge | 172:7d866c31b3c5 | 192 | can1_irq_handler = handler; |
AnnaBridge | 172:7d866c31b3c5 | 193 | else |
AnnaBridge | 172:7d866c31b3c5 | 194 | can0_irq_handler = handler; |
AnnaBridge | 172:7d866c31b3c5 | 195 | can_irq_ids[obj->index] = id; |
AnnaBridge | 172:7d866c31b3c5 | 196 | |
AnnaBridge | 172:7d866c31b3c5 | 197 | } |
AnnaBridge | 172:7d866c31b3c5 | 198 | |
AnnaBridge | 172:7d866c31b3c5 | 199 | void can_irq_free(can_t *obj) |
AnnaBridge | 172:7d866c31b3c5 | 200 | { |
AnnaBridge | 172:7d866c31b3c5 | 201 | CAN_DisableInt((CAN_T *)NU_MODBASE(obj->can), (CAN_CON_IE_Msk|CAN_CON_SIE_Msk|CAN_CON_EIE_Msk)); |
AnnaBridge | 172:7d866c31b3c5 | 202 | |
AnnaBridge | 172:7d866c31b3c5 | 203 | can_irq_ids[obj->index] = 0; |
AnnaBridge | 172:7d866c31b3c5 | 204 | |
AnnaBridge | 172:7d866c31b3c5 | 205 | if(!obj->index) |
AnnaBridge | 172:7d866c31b3c5 | 206 | NVIC_DisableIRQ(CAN0_IRQn); |
AnnaBridge | 172:7d866c31b3c5 | 207 | else |
AnnaBridge | 172:7d866c31b3c5 | 208 | NVIC_DisableIRQ(CAN1_IRQn); |
AnnaBridge | 172:7d866c31b3c5 | 209 | |
AnnaBridge | 172:7d866c31b3c5 | 210 | |
AnnaBridge | 172:7d866c31b3c5 | 211 | } |
AnnaBridge | 172:7d866c31b3c5 | 212 | |
AnnaBridge | 172:7d866c31b3c5 | 213 | void can_irq_set(can_t *obj, CanIrqType irq, uint32_t enable) |
AnnaBridge | 172:7d866c31b3c5 | 214 | { |
AnnaBridge | 172:7d866c31b3c5 | 215 | uint8_t u8Mask; |
AnnaBridge | 172:7d866c31b3c5 | 216 | |
AnnaBridge | 172:7d866c31b3c5 | 217 | u8Mask = ((enable != 0 )? CAN_CON_IE_Msk :0); |
AnnaBridge | 172:7d866c31b3c5 | 218 | |
AnnaBridge | 172:7d866c31b3c5 | 219 | switch (irq) { |
AnnaBridge | 172:7d866c31b3c5 | 220 | case IRQ_ERROR: |
AnnaBridge | 172:7d866c31b3c5 | 221 | case IRQ_BUS: |
AnnaBridge | 172:7d866c31b3c5 | 222 | case IRQ_PASSIVE: |
AnnaBridge | 172:7d866c31b3c5 | 223 | u8Mask = u8Mask | CAN_CON_EIE_Msk | CAN_CON_SIE_Msk; |
AnnaBridge | 172:7d866c31b3c5 | 224 | break; |
AnnaBridge | 172:7d866c31b3c5 | 225 | |
AnnaBridge | 172:7d866c31b3c5 | 226 | case IRQ_RX: |
AnnaBridge | 172:7d866c31b3c5 | 227 | case IRQ_TX: |
AnnaBridge | 172:7d866c31b3c5 | 228 | case IRQ_OVERRUN: |
AnnaBridge | 172:7d866c31b3c5 | 229 | case IRQ_WAKEUP: |
AnnaBridge | 172:7d866c31b3c5 | 230 | u8Mask = u8Mask | CAN_CON_SIE_Msk; |
AnnaBridge | 172:7d866c31b3c5 | 231 | break; |
AnnaBridge | 172:7d866c31b3c5 | 232 | |
AnnaBridge | 172:7d866c31b3c5 | 233 | default: |
AnnaBridge | 172:7d866c31b3c5 | 234 | break; |
AnnaBridge | 172:7d866c31b3c5 | 235 | |
AnnaBridge | 172:7d866c31b3c5 | 236 | } |
AnnaBridge | 172:7d866c31b3c5 | 237 | CAN_EnterInitMode((CAN_T*)NU_MODBASE(obj->can), u8Mask); |
AnnaBridge | 172:7d866c31b3c5 | 238 | |
AnnaBridge | 172:7d866c31b3c5 | 239 | CAN_LeaveInitMode((CAN_T*)NU_MODBASE(obj->can)); |
AnnaBridge | 172:7d866c31b3c5 | 240 | |
AnnaBridge | 172:7d866c31b3c5 | 241 | if(!obj->index) { |
AnnaBridge | 172:7d866c31b3c5 | 242 | NVIC_SetVector(CAN0_IRQn, (uint32_t)&CAN0_IRQHandler); |
AnnaBridge | 172:7d866c31b3c5 | 243 | NVIC_EnableIRQ(CAN0_IRQn); |
AnnaBridge | 172:7d866c31b3c5 | 244 | } else { |
AnnaBridge | 172:7d866c31b3c5 | 245 | NVIC_SetVector(CAN1_IRQn, (uint32_t)&CAN1_IRQHandler); |
AnnaBridge | 172:7d866c31b3c5 | 246 | NVIC_EnableIRQ(CAN1_IRQn); |
AnnaBridge | 172:7d866c31b3c5 | 247 | } |
AnnaBridge | 172:7d866c31b3c5 | 248 | |
AnnaBridge | 172:7d866c31b3c5 | 249 | } |
AnnaBridge | 172:7d866c31b3c5 | 250 | |
AnnaBridge | 172:7d866c31b3c5 | 251 | int can_write(can_t *obj, CAN_Message msg, int cc) |
AnnaBridge | 172:7d866c31b3c5 | 252 | { |
AnnaBridge | 172:7d866c31b3c5 | 253 | STR_CANMSG_T CMsg; |
AnnaBridge | 172:7d866c31b3c5 | 254 | |
AnnaBridge | 172:7d866c31b3c5 | 255 | CMsg.IdType = (uint32_t)msg.format; |
AnnaBridge | 172:7d866c31b3c5 | 256 | CMsg.FrameType = (uint32_t)!msg.type; |
AnnaBridge | 172:7d866c31b3c5 | 257 | CMsg.Id = msg.id; |
AnnaBridge | 172:7d866c31b3c5 | 258 | CMsg.DLC = msg.len; |
AnnaBridge | 172:7d866c31b3c5 | 259 | memcpy((void *)&CMsg.Data[0],(const void *)&msg.data[0], (unsigned int)8); |
AnnaBridge | 172:7d866c31b3c5 | 260 | |
AnnaBridge | 172:7d866c31b3c5 | 261 | return CAN_Transmit((CAN_T *)(NU_MODBASE(obj->can)), cc, &CMsg); |
AnnaBridge | 172:7d866c31b3c5 | 262 | } |
AnnaBridge | 172:7d866c31b3c5 | 263 | |
AnnaBridge | 172:7d866c31b3c5 | 264 | int can_read(can_t *obj, CAN_Message *msg, int handle) |
AnnaBridge | 172:7d866c31b3c5 | 265 | { |
AnnaBridge | 172:7d866c31b3c5 | 266 | STR_CANMSG_T CMsg; |
AnnaBridge | 172:7d866c31b3c5 | 267 | |
AnnaBridge | 172:7d866c31b3c5 | 268 | if(!CAN_Receive((CAN_T *)(NU_MODBASE(obj->can)), handle, &CMsg)) |
AnnaBridge | 172:7d866c31b3c5 | 269 | return 0; |
AnnaBridge | 172:7d866c31b3c5 | 270 | |
AnnaBridge | 172:7d866c31b3c5 | 271 | msg->format = (CANFormat)CMsg.IdType; |
AnnaBridge | 172:7d866c31b3c5 | 272 | msg->type = (CANType)!CMsg.FrameType; |
AnnaBridge | 172:7d866c31b3c5 | 273 | msg->id = CMsg.Id; |
AnnaBridge | 172:7d866c31b3c5 | 274 | msg->len = CMsg.DLC; |
AnnaBridge | 172:7d866c31b3c5 | 275 | memcpy(&msg->data[0], &CMsg.Data[0], 8); |
AnnaBridge | 172:7d866c31b3c5 | 276 | |
AnnaBridge | 172:7d866c31b3c5 | 277 | return 1; |
AnnaBridge | 172:7d866c31b3c5 | 278 | } |
AnnaBridge | 172:7d866c31b3c5 | 279 | |
AnnaBridge | 172:7d866c31b3c5 | 280 | int can_mode(can_t *obj, CanMode mode) |
AnnaBridge | 172:7d866c31b3c5 | 281 | { |
AnnaBridge | 172:7d866c31b3c5 | 282 | int success = 0; |
AnnaBridge | 172:7d866c31b3c5 | 283 | switch (mode) { |
AnnaBridge | 172:7d866c31b3c5 | 284 | case MODE_RESET: |
AnnaBridge | 172:7d866c31b3c5 | 285 | CAN_LeaveTestMode((CAN_T*)NU_MODBASE(obj->can)); |
AnnaBridge | 172:7d866c31b3c5 | 286 | success = 1; |
AnnaBridge | 172:7d866c31b3c5 | 287 | break; |
AnnaBridge | 172:7d866c31b3c5 | 288 | |
AnnaBridge | 172:7d866c31b3c5 | 289 | case MODE_NORMAL: |
AnnaBridge | 172:7d866c31b3c5 | 290 | CAN_EnterTestMode((CAN_T*)NU_MODBASE(obj->can), CAN_TEST_BASIC_Msk); |
AnnaBridge | 172:7d866c31b3c5 | 291 | success = 1; |
AnnaBridge | 172:7d866c31b3c5 | 292 | break; |
AnnaBridge | 172:7d866c31b3c5 | 293 | |
AnnaBridge | 172:7d866c31b3c5 | 294 | case MODE_SILENT: |
AnnaBridge | 172:7d866c31b3c5 | 295 | CAN_EnterTestMode((CAN_T*)NU_MODBASE(obj->can), CAN_TEST_SILENT_Msk); |
AnnaBridge | 172:7d866c31b3c5 | 296 | success = 1; |
AnnaBridge | 172:7d866c31b3c5 | 297 | break; |
AnnaBridge | 172:7d866c31b3c5 | 298 | |
AnnaBridge | 172:7d866c31b3c5 | 299 | case MODE_TEST_LOCAL: |
AnnaBridge | 172:7d866c31b3c5 | 300 | case MODE_TEST_GLOBAL: |
AnnaBridge | 172:7d866c31b3c5 | 301 | CAN_EnterTestMode((CAN_T*)NU_MODBASE(obj->can), CAN_TEST_LBACK_Msk); |
AnnaBridge | 172:7d866c31b3c5 | 302 | success = 1; |
AnnaBridge | 172:7d866c31b3c5 | 303 | break; |
AnnaBridge | 172:7d866c31b3c5 | 304 | |
AnnaBridge | 172:7d866c31b3c5 | 305 | case MODE_TEST_SILENT: |
AnnaBridge | 172:7d866c31b3c5 | 306 | CAN_EnterTestMode((CAN_T*)NU_MODBASE(obj->can), CAN_TEST_SILENT_Msk | CAN_TEST_LBACK_Msk); |
AnnaBridge | 172:7d866c31b3c5 | 307 | success = 1; |
AnnaBridge | 172:7d866c31b3c5 | 308 | break; |
AnnaBridge | 172:7d866c31b3c5 | 309 | |
AnnaBridge | 172:7d866c31b3c5 | 310 | default: |
AnnaBridge | 172:7d866c31b3c5 | 311 | success = 0; |
AnnaBridge | 172:7d866c31b3c5 | 312 | break; |
AnnaBridge | 172:7d866c31b3c5 | 313 | |
AnnaBridge | 172:7d866c31b3c5 | 314 | } |
AnnaBridge | 172:7d866c31b3c5 | 315 | |
AnnaBridge | 172:7d866c31b3c5 | 316 | |
AnnaBridge | 172:7d866c31b3c5 | 317 | return success; |
AnnaBridge | 172:7d866c31b3c5 | 318 | } |
AnnaBridge | 172:7d866c31b3c5 | 319 | |
AnnaBridge | 172:7d866c31b3c5 | 320 | int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) |
AnnaBridge | 172:7d866c31b3c5 | 321 | { |
AnnaBridge | 172:7d866c31b3c5 | 322 | return CAN_SetRxMsg((CAN_T *)NU_MODBASE(obj->can), handle, (uint32_t)format, id); |
AnnaBridge | 172:7d866c31b3c5 | 323 | } |
AnnaBridge | 172:7d866c31b3c5 | 324 | |
AnnaBridge | 172:7d866c31b3c5 | 325 | |
AnnaBridge | 172:7d866c31b3c5 | 326 | void can_reset(can_t *obj) |
AnnaBridge | 172:7d866c31b3c5 | 327 | { |
AnnaBridge | 172:7d866c31b3c5 | 328 | const struct nu_modinit_s *modinit = get_modinit(obj->can, can_modinit_tab); |
AnnaBridge | 172:7d866c31b3c5 | 329 | |
AnnaBridge | 172:7d866c31b3c5 | 330 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 172:7d866c31b3c5 | 331 | MBED_ASSERT(modinit->modname == (int) obj->can); |
AnnaBridge | 172:7d866c31b3c5 | 332 | |
AnnaBridge | 172:7d866c31b3c5 | 333 | // Reset this module |
AnnaBridge | 172:7d866c31b3c5 | 334 | SYS_ResetModule(modinit->rsetidx); |
AnnaBridge | 172:7d866c31b3c5 | 335 | |
AnnaBridge | 172:7d866c31b3c5 | 336 | } |
AnnaBridge | 172:7d866c31b3c5 | 337 | |
AnnaBridge | 172:7d866c31b3c5 | 338 | unsigned char can_rderror(can_t *obj) |
AnnaBridge | 172:7d866c31b3c5 | 339 | { |
AnnaBridge | 172:7d866c31b3c5 | 340 | CAN_T *can = (CAN_T *)NU_MODBASE(obj->can); |
AnnaBridge | 172:7d866c31b3c5 | 341 | return ((can->ERR>>8)&0xFF); |
AnnaBridge | 172:7d866c31b3c5 | 342 | } |
AnnaBridge | 172:7d866c31b3c5 | 343 | |
AnnaBridge | 172:7d866c31b3c5 | 344 | unsigned char can_tderror(can_t *obj) |
AnnaBridge | 172:7d866c31b3c5 | 345 | { |
AnnaBridge | 172:7d866c31b3c5 | 346 | CAN_T *can = (CAN_T *)NU_MODBASE(obj->can); |
AnnaBridge | 172:7d866c31b3c5 | 347 | return ((can->ERR)&0xFF); |
AnnaBridge | 172:7d866c31b3c5 | 348 | } |
AnnaBridge | 172:7d866c31b3c5 | 349 | |
AnnaBridge | 172:7d866c31b3c5 | 350 | void can_monitor(can_t *obj, int silent) |
AnnaBridge | 172:7d866c31b3c5 | 351 | { |
AnnaBridge | 172:7d866c31b3c5 | 352 | CAN_EnterTestMode((CAN_T *)NU_MODBASE(obj->can), CAN_TEST_SILENT_Msk); |
AnnaBridge | 172:7d866c31b3c5 | 353 | } |
AnnaBridge | 172:7d866c31b3c5 | 354 | |
AnnaBridge | 172:7d866c31b3c5 | 355 | #endif // DEVICE_CAN |