Skip to content

Commit 6e09135

Browse files
committed
Add Nuvoton NuMaker-IoT-M263A CAN bus support
1 parent c02e10b commit 6e09135

File tree

3 files changed

+350
-1
lines changed

3 files changed

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

targets/TARGET_NUVOTON/TARGET_M261/objects.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ struct trng_s {
130130
uint8_t dummy;
131131
};
132132

133+
struct can_s {
134+
CANName can;
135+
PinName pin_rd;
136+
PinName pin_td;
137+
int index;
138+
};
139+
133140
#ifdef __cplusplus
134141
}
135142
#endif

targets/targets.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8716,7 +8716,8 @@
87168716
"FLASH",
87178717
"MPU",
87188718
"WATCHDOG",
8719-
"USBDEVICE"
8719+
"USBDEVICE",
8720+
"CAN"
87208721
],
87218722
"components_add": [
87228723
"FLASHIAP"

0 commit comments

Comments
 (0)