Contiki 3.x
contiki-main.c
1 #include "contiki.h"
2 
3 #include <stdio.h>
4 #include <string.h>
5 
6 #include "sys/autostart.h"
7 #include "dev/leds.h"
8 
9 #include "cfs.h"
10 #include "cfs-coffee.h"
11 #include "xmem.h"
12 
13 #include "core-clocks.h"
14 #include "uart.h"
15 #include "udelay.h"
16 #include "llwu.h"
17 #include "init-net.h"
18 #include "power-control.h"
19 #include "voltage.h"
20 #include "K60.h"
21 #include "dbg-uart.h"
22 #include "devicemap.h"
23 #include "random.h"
24 #include "rtc.h"
25 
26 #define DEBUG 1
27 #if DEBUG
28 #define PRINTF(...) printf(__VA_ARGS__)
29 #else
30 #define PRINTF(...)
31 #endif
32 
33 /*---------------------------------------------------------------------------*/
34 #define COFFEE_AUTO_FORMAT 1
35 static void
36 init_cfs()
37 {
38  int fd;
39  PRINTF("Initialize xmem and coffee...\n");
40  xmem_init();
41  PRINTF("Xmem initialized.\n");
42 #ifdef COFFEE_AUTO_FORMAT
43  if((fd = cfs_open("formated", CFS_READ)) == -1) {
44  /* Storage is not formated */
45  PRINTF("Coffee not formated\n");
46  if(cfs_coffee_format() == -1) {
47  /* Format failed, bail out */
48  PRINTF("Failed to format coffee, bail out\n");
49  return;
50  }
51  if((fd = cfs_open("formated", CFS_WRITE)) == -1) {
52  /* Failed to open file to indicate formated state. */
53  PRINTF("Failed to open file to indicate formated state\n");
54  return;
55  }
56  cfs_write(fd, "DO NOT REMOVE!", strlen("DO NOT REMOVE!"));
57  }
58  cfs_close(fd);
59 #endif /* COFFEE_AUTO_FORMAT */
60  PRINTF("Coffee initialized.\r\n");
61 }
62 /*---------------------------------------------------------------------------*/
63 LLWU_CONTROL(deep_sleep);
64 
65 /* C entry point (after startup code has executed) */
66 int
67 main(void)
68 {
70 
71  /* Set up core clocks so that timings will be correct in all modules */
72  SystemInit();
73 
74  /* Update SystemCoreClock global var */
76 
77  /* Set RTC time to 0 in order to start counting seconds. */
78  rtc_time_set(0);
79  rtc_start();
80 
81  dbg_uart_init();
82  devicemap_init();
83 
84  llwu_init();
85  llwu_enable_wakeup_module(LLWU_WAKEUP_MODULE_LPTMR);
86  llwu_register(deep_sleep);
87  /* Dont allow deep sleep for now because radio cant wake up the mcu from it. */
88  /* TODO(Henrik) Fix this when a new revision is made of the hardware. */
89 #ifndef WITH_SLIP
90  llwu_set_allow(deep_sleep, 1);
91 #else
92  llwu_set_allow(deep_sleep, 0);
93 #endif
94 
95  power_control_init();
96 
97  /* Turn on power to the on board peripherals */
98  power_control_vperiph_set(1);
99 
100  udelay_init();
101  udelay(0xFFFF);
102  udelay(0xFFFF);
103  random_init((unsigned short)SIM->UIDL);
104 #ifndef WITH_SLIP
105  PRINTF("Booted\n");
106  PRINTF("CPUID: %08x\n", (unsigned int)SCB->CPUID);
107  PRINTF("UID: %08x %08x %08x %08x\n", (unsigned int)SIM->UIDH, (unsigned int)SIM->UIDMH, (unsigned int)SIM->UIDML, (unsigned int)SIM->UIDL);
108  PRINTF("Clocks:\n F_CPU: %u\n F_SYS: %u\n F_BUS: %u\n F_FLEXBUS: %u\n F_FLASH: %u\n", (unsigned int)SystemCoreClock, (unsigned int)SystemSysClock, (unsigned int)SystemBusClock, (unsigned int)SystemFlexBusClock, (unsigned int)SystemFlashClock);
109 #endif
110 
111  /*
112  * Initialize Contiki and our processes.
113  */
114  process_init();
115  process_start(&etimer_process, NULL);
116 
117  ctimer_init();
118 
119  clock_init();
120  init_cfs();
121  init_net();
122  voltage_init();
123  rtimer_init();
124 
125  autostart_start(autostart_processes);
126 
127  while(1) {
128  while(process_run() > 0);
129  llwu_sleep();
130  }
131 }
#define SCB
Definition: core_cm0.h:494
void SystemInit(void)
Initialize the system.
int cfs_open(const char *name, int flags)
Open a file.
Definition: cfs-coffee.c:996
Device I/O mappings for the Mulle platform.
#define CFS_WRITE
Specify that cfs_open() should open a file for writing.
Definition: cfs.h:104
Provide udelay routine for MK60DZ10.
void rtimer_init(void)
Initialize the real-time scheduler.
Definition: rtimer.c:61
uint32_t SystemFlexBusClock
Current FlexBus clock frequency.
void leds_arch_init(void)
Leds implementation.
Definition: leds-arch.c:48
uint32_t SystemFlashClock
Current flash clock frequency.
Power control pins for the on board power switches on the Mulle board.
Debug port initialization for the Mulle platform.
#define NULL
The null pointer.
#define SIM
Peripheral SIM base pointer.
Definition: MK60D10.h:7650
#define CFS_READ
Specify that cfs_open() should open a file for reading.
Definition: cfs.h:90
void clock_init(void)
Initialize the clock library.
Definition: clock.c:76
int main(void)
This is main...
Definition: ethconfig.c:49
void dbg_uart_init(void)
Initialize debug UART used by printf.
Definition: dbg-uart.c:52
void udelay(uint16_t us)
Microsecond busy wait.
Definition: udelay.c:26
void process_init(void)
Initialize the process module.
Definition: process.c:208
void llwu_sleep(void)
Sleep until some process is polled, ie interrupt occurs.
Definition: llwu.c:76
K60 hardware register header wrapper.
void rtc_start(void)
Enable the RTC seconds counter.
Definition: rtc.c:63
void llwu_set_allow(llwu_control_t *c, char allow)
Method for a controller to allow or disallow deep sleep.
Definition: llwu.c:54
void random_init(unsigned short seed)
Seed the cc2430 random number generator.
Definition: random.c:41
Provide common UART routines for MK60DZ10.
Header file for module for automatically starting and exiting a list of processes.
Network initialization.
void ctimer_init(void)
Initialize the callback timer library.
Definition: ctimer.c:91
void rtc_time_set(uint32_t seconds)
Set the time.
Definition: rtc.c:75
uint32_t SystemCoreClock
Current core clock frequency.
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
int cfs_coffee_format(void)
Format the storage area assigned to Coffee.
Definition: cfs-coffee.c:1346
K60 clock configuration functions.
int process_run(void)
Run the system once - call poll handlers and process one event.
Definition: process.c:302
Helper functions for reading Mulle platform board voltages.
void llwu_register(llwu_control_t *c)
Register as a controller for llwu.
Definition: llwu.c:47
Header for the Coffee file system.
void SystemCoreClockUpdate(void)
Update internal SystemCoreClock variable.
uint32_t SystemSysClock
Current system clock frequency.
uint32_t SystemBusClock
Current bus clock frequency.
void cfs_close(int fd)
Close an open file.
Definition: cfs-coffee.c:1032
CFS header file.