test

Committer:
elijahsj
Date:
Mon Nov 09 00:33:19 2020 -0500
Revision:
2:4364577b5ad8
Parent:
1:8a094db1347f
copied mbed library

Who changed what in which revision?

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