Contiki 2.5
mspi-drv.c
Go to the documentation of this file.
1 /* Copyright (c) 2010, Ulf Kulau
2  *
3  * Permission is hereby granted, free of charge, to any person
4  * obtaining a copy of this software and associated documentation
5  * files (the "Software"), to deal in the Software without
6  * restriction, including without limitation the rights to use,
7  * copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following
10  * conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 /**
26  * \addtogroup Drivers
27  * @{
28  *
29  * \addtogroup mspi_driver
30  * @{
31  */
32 
33 /**
34  * \file
35  * MSPI driver implementation
36  * \author
37  * Ulf Kulau <kulau@ibr.cs.tu-bs.de>
38  */
39 
40 #include "mspi-drv.h"
41 
42 
43 /* global variable for the selected USART port*/
44 uint8_t mspi_uart_port = MSPI_USART1;
45 
46 void mspi_init(uint8_t cs, uint8_t mode, uint16_t baud){
47  /*initialize the spi port pins, setting pins output*/
48  MSPI_CS_PORT_DDR |= (1 << MSPI_CS_PIN_0) | (1 << MSPI_CS_PIN_1) | (1 << MSPI_CS_PIN_2);
49  MSPI_CS_PORT &= ~((1 << MSPI_CS_PIN_0) | (1 << MSPI_CS_PIN_1) | (1 << MSPI_CS_PIN_2));
50 
51 #if MSPI_BUS_MANAGER
52  add_to_spi_mgr(cs, mode, baud);
53 #else
54  /*initialize MSPI*/
55  *(usart_ports[mspi_uart_port].UBRRn) = 0;
56  /*setting clock pin as output*/
57  *(usart_ports[mspi_uart_port].XCKn_DDR) |= (1 << usart_ports[mspi_uart_port].XCKn);
58  /*enable MSPI and set spi mode*/
59  *(usart_ports[mspi_uart_port].UCSRnC) = ((MSPI_ENABLE) | mode);
60  /*initialize RX and TX*/
61  *(usart_ports[mspi_uart_port].UCSRnB) = ((1 << RXEN0) | (1 << TXEN0));
62  *(usart_ports[mspi_uart_port].UBRRn) = baud;
63 #endif
64 }
65 
66 
67 void mspi_chip_select(uint8_t cs){
68 #if MSPI_BUS_MANAGER
69  if(spi_current_config != spi_bus_config[cs].checksum){
70  /*new mspi configuration is needed by this spi device*/
71  change_spi_mode(spi_bus_config[cs]);
72  spi_current_config = spi_bus_config[cs].checksum;
73  }
74 #endif
75  /*chip select*/
76  MSPI_CS_PORT |= cs_bcd[cs];
77 }
78 
79 void mspi_chip_release(uint8_t cs){
80  /*chip deselect*/
81  MSPI_CS_PORT &= ~((1 << MSPI_CS_PIN_0) | (1 << MSPI_CS_PIN_1) | (1 << MSPI_CS_PIN_2));
82 }
83 
84 
85 uint8_t mspi_transceive(uint8_t data){
86  uint8_t receive_data;
87 
88  /*wait while transmit buffer is empty*/
89  while ( !(*(usart_ports[mspi_uart_port].UCSRnA) & (1<<UDRE0)));
90  *(usart_ports[mspi_uart_port].UDRn) = data;
91 
92  /*wait to readout the MSPI data register*/
93  while ( !(*(usart_ports[mspi_uart_port].UCSRnA) & (1<<RXC0)) );
94  receive_data = *(usart_ports[mspi_uart_port].UDRn);
95 
96  return receive_data;
97 }
98 
99 void mspi_deinit(void){
100  *(usart_ports[mspi_uart_port].UCSRnA) = MSPI_DISABLE;
101  *(usart_ports[mspi_uart_port].UCSRnB) = MSPI_DISABLE;
102  *(usart_ports[mspi_uart_port].UCSRnC) = MSPI_DISABLE;
103 }
104 
105 #if MSPI_BUS_MANAGER
106 void add_to_spi_mgr(uint8_t cs, uint8_t mode, uint16_t baud){
107  spi_bus_config[cs].dev_mode = mode;
108  spi_bus_config[cs].dev_baud = baud;
109  spi_bus_config[cs].checksum = mode + baud;
110 }
111 
112 void change_spi_mode(spi_dev new_config){
113  /*initialize MSPI*/
114  *(usart_ports[mspi_uart_port].UBRRn) = 0;
115  /*setting clock pin as output*/
116  *(usart_ports[mspi_uart_port].XCKn_DDR) |= (1 << usart_ports[mspi_uart_port].XCKn);
117  /*enable MSPI and set spi mode*/
118  *(usart_ports[mspi_uart_port].UCSRnC) = ((MSPI_ENABLE) | new_config.dev_mode);
119  /*initialize RX and TX*/
120  *(usart_ports[mspi_uart_port].UCSRnB) = ((1 << RXEN0) | (1 << TXEN0));
121  *(usart_ports[mspi_uart_port].UBRRn) = new_config.dev_baud;
122 }
123 #endif