5.2.1 - Updated I2C files

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Committer:
jacobjohnson
Date:
Mon Feb 27 17:45:05 2017 +0000
Revision:
1:f30bdcd2b33b
Parent:
0:098463de4c5d
changed the inputscale from 1 to 7 in analogin_api.c.  This will need to be changed later, and accessed from the main level, but for now this allows the  adc to read a value from 0 to 3.7V, instead of just up to 1V.;

Who changed what in which revision?

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