Contiki 3.x
spi.c
1 /*-----------------------------------------------------------------------------------------------------------------------------------*/
2 /* spi.c
3  *
4  * SPI functions for the Freescale FRDM-KL25z Freedom Board
5  *
6  * Author: Graeme Bragg
7  * ARM-ECS / Pervasive Systems Centre
8  * School of Electronics & Computer Science
9  * University of Southampton
10  *
11  *
12  * 1/5/2014 Rev.01 Includes functions for initialising SPI0 for CC1120,
13  * controlling CSn and sending data.
14  *
15  * Page references relate to the KL25 Sub-Family Reference Manual, Document
16  * No. KL25P80M48SF0RM, Rev. 3 September 2012. Available on 25/02/2013 from:
17  * http://cache.freescale.com/files/32bit/doc/ref_manual/KL25P80M48SF0RM.pdf?fr=gdc
18  *
19  * Page references to "M0 Book" refer to "The Definitive Guide to the
20  * ARM Cortex-M0" by Joseph Yiu, ISBN 978-0-12-385477-3.
21  *
22  *
23  * Copyright (c) 2014, University of Southampton, Electronics and Computer Science
24  * All rights reserved.
25  *
26  * Redistribution and use in source and binary forms, with or without
27  * modification, are permitted provided that the following conditions
28  * are met:
29  * 1. Redistributions of source code must retain the above copyright
30  * notice, this list of conditions and the following disclaimer.
31  * 2. Redistributions in binary form must reproduce the above copyright
32  * notice, this list of conditions and the following disclaimer in the
33  * documentation and/or other materials provided with the distribution.
34  * 3. Neither the name of the Institute nor the names of its contributors
35  * may be used to endorse or promote products derived from this software
36  * without specific prior written permission.
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
39  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
42  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  *
50  */
51 /*-----------------------------------------------------------------------------------------------------------------------------------*/
52 
53 #include <MKL25Z4.h> /* I/O map for MKL25Z128VLK4 */
54 #include "cpu.h"
55 
56 #include "spi.h"
57 
58 #include <stdlib.h>
59 
60 void SPI0_init(void)
61 {
62  port_enable(PORTD_EN_MASK | PORTC_EN_MASK); /* Make sure that clocks are enable to Port D and Port C */
63 
64  /* Configure SPI0. */
65  SIM_SCGC4 |= SIM_SCGC4_SPI0_MASK; /* Enable clock to SPI Module */
66 
67  PORTD_PCR3 &= ~PORT_PCR_MUX_MASK; /* Clear Port D, Pin 3 Mux. */
68  PORTD_PCR3 |= PORT_PCR_ISF_MASK | PORT_PCR_MUX(0x02); /* Clear ISF & Set Port D, Pin 3 as MISO. */
69 
70  PORTD_PCR2 &= ~PORT_PCR_MUX_MASK; /* Clear Port D, Pin 2 Mux. */
71  PORTD_PCR2 |= PORT_PCR_ISF_MASK | PORT_PCR_MUX(0x02); /* Clear ISF & Set Port D, Pin 2 as MOSI. */
72 
73  PORTC_PCR5 &= ~PORT_PCR_MUX_MASK; /* Clear Port C, Pin 5 Mux. */
74  PORTC_PCR5 |= PORT_PCR_ISF_MASK | PORT_PCR_MUX(0x02); /* Clear ISF & Set Port C, Pin 5 as Clock. */
75 
76  SPI0_C1 = (SPI_C1_MSTR_MASK | SPI_C1_SSOE_MASK); /* Set SPI0_C1: Master Mode */
77  //SPI0_C2 = SPI_C2_MODFEN_MASK; /* Set SPI0_C2: Default state */
78  SPI0_BR = (SPI_BR_SPPR(0x02) | SPI_BR_SPR(0x00)); /* Set baud rate register */
79  //printf("SPI_BR=%02x\n\r", SPI0_BR);
80 
81  SPI0_C1 |= SPI_C1_SPE_MASK; /* Enable SPI module */
82  //printf("\n\rSPI0_C1 = %d...", SPI0_C1);
83 }
84 
85 
86 /* Single SPI Send/Recieve. */
87 uint8_t
88 SPI_single_tx_rx(uint8_t in, uint8_t module) {
89 
90  if(SPI0_S & SPI_S_SPRF_MASK) { /* If there is stray data in the SPI data register, discard it. */
91  (void)SPI0_D;
92  }
93 
94  while((SPI0_S & SPI_S_SPTEF_MASK) == 0){ /* Wait for the transmitter to be empty. */
95  }
96 
97  SPI0_D = in; /* Write the data. */
98 
99  while((SPI0_S & SPI_S_SPTEF_MASK) == 0){ /* Wait for the transmitter to be empty. */
100  }
101 
102  while((SPI0_S & SPI_S_SPRF_MASK) == 0) { /* Wait for the receiver to be full. */
103  }
104 
105  return (SPI0_D);
106 }
107 
108 /* Send data to SPI Address. */
109 uint8_t SPI_tx_and_rx(uint8_t addr, uint8_t value, uint8_t module) {
110 
111  uint8_t result;
112  (void)SPI_single_tx_rx(addr,0); //Throw away the first received byte
113  result = SPI_single_tx_rx(value,0);
114  return result;
115 }
static uip_ds6_addr_t * addr
Pointer to a router list entry.
Definition: uip-nd6.c:124
CMSIS Peripheral Access Layer for MKL25Z4.
Header file for the MKL25Z NVIC functions.