Contiki 2.5
bus.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * $Id: bus.c,v 1.2 2010/01/25 23:12:09 anthony-a Exp $
32  */
33 
34 /**
35  * \file
36  * Initialization functions for the 8051 bus
37  * \author
38  * Adam Dunkels <adam@sics.se>
39  */
40 
41 #include "banked.h"
42 #include "cc2430_sfr.h"
43 #include "dev/bus.h"
44 #include "sys/clock.h"
45 
46 /*---------------------------------------------------------------------------*/
47 void
48 bus_init (void) __banked
49 {
50  CLKCON = (0x00 | OSC32K); /* 32k internal */
51  while(CLKCON != (0x00 | OSC32K));
52 
53  P1DIR |= 0x0E;
54  IEN0_EA = 1;
55 
56  /* Initialize the clock */
57  clock_init();
58 }
59 /*---------------------------------------------------------------------------*/
60 /**
61  * Read a block of code memory.
62  * The code must be placed in the lowest bank of flash.
63  *
64  * \param address address to read from flash
65  * \param buffer buffer to store data
66  * \param size number of bytes to read
67  */
68 void
69 flash_read (uint8_t *buf, uint32_t address, uint8_t size) __banked
70 {
71  buf; /*dptr0*/
72  address; /*stack-6*/
73  size; /*stack-7*/
74 
75  buf;
76 
78  __asm
79  mov dpl, r2
80  mov dph, r3
81  mov a, r0
82  push acc
83  mov a, r2
84  push acc
85  mov a, _MEMCTR
86  push acc
87 
88  mov a, _bp
89  add a, #0xf9 ;stack - 7 = size
90  mov r0,a
91  mov a, @r0 ;r2 = size
92  mov r2, a ;r2 = size
93 
94  inc r0
95  mov a, @r0
96  mov _DPL1, a ;DPTR1 = address & 0x7FFF | 0x8000
97  inc r0
98  mov a, @r0
99  orl a, #0x80
100  mov _DPH1, a
101  inc r0 ;MEMCTR = ((address >> 15 & 3) << 4) | 0x01 (bank select)
102  mov a, @r0
103  dec r0
104  rrc a
105  mov a, @r0
106  rrc a
107  rr a
108  rr a
109  anl a, #0x30
110  orl a, #1
111  mov _MEMCTR,a
112 lp1:
113  mov _DPS, #1 ;active DPTR = 1
114  clr a
115  movc a, @a+dptr ;read flash (DPTR1)
116  inc dptr
117  mov _DPS, #0 ;active DPTR = 0
118  movx @dptr,a ;write to DPTR0
119  inc dptr
120  djnz r2,lp1 ;while (--size)
121 
122  pop acc
123  mov _MEMCTR, a ;restore bank
124 
125  pop acc
126  mov r2,a
127  pop acc
128  mov r0,a
129  __endasm;
130  ENABLE_INTERRUPTS();
131  DPL1 = *buf++;
132 }
133 /*---------------------------------------------------------------------------*/