Contiki 3.x
spirit1-arch.c
1 /*
2  * Copyright (c) 2012, STMicroelectronics.
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  *
30  */
31 /*---------------------------------------------------------------------------*/
32 #include "stm32l1xx.h"
33 #include "spirit1-arch.h"
34 #include "spirit1.h"
35 #include "st-lib.h"
36 /*---------------------------------------------------------------------------*/
37 extern void spirit1_interrupt_callback(void);
38 st_lib_spirit_bool spiritdk_timer_expired = S_FALSE;
39 /*---------------------------------------------------------------------------*/
40 /* use the SPI-port to acquire the status bytes from the radio. */
41 #define CS_TO_SCLK_DELAY 0x0100
42 /*---------------------------------------------------------------------------*/
43 extern st_lib_spi_handle_typedef st_lib_p_spi_handle;
44 /*---------------------------------------------------------------------------*/
45 uint16_t
46 spirit1_arch_refresh_status(void)
47 {
48  volatile uint16_t mcstate = 0x0000;
49  uint8_t header[2];
50  header[0] = 0x01;
51  header[1] = MC_STATE1_BASE;
52  uint32_t spi_timeout = ((uint32_t)1000); /*<! Value of Timeout when SPI communication fails */
53 
54  IRQ_DISABLE();
55 
56  /* Puts the SPI chip select low to start the transaction */
57  st_lib_radio_spi_cs_low();
58 
59  for(volatile uint16_t index = 0; index < CS_TO_SCLK_DELAY; index++) {
60  }
61 
62  /* Write the aHeader bytes and read the SPIRIT1 status bytes */
63  st_lib_hal_spi_transmit_receive(&st_lib_p_spi_handle, (uint8_t *)&header[0], (uint8_t *)&mcstate, 1, spi_timeout);
64  mcstate = mcstate << 8;
65 
66  /* Write the aHeader bytes and read the SPIRIT1 status bytes */
67  st_lib_hal_spi_transmit_receive(&st_lib_p_spi_handle, (uint8_t *)&header[1], (uint8_t *)&mcstate, 1, spi_timeout);
68 
69  /* To be sure to don't rise the Chip Select before the end of last sending */
70  while(st_lib_hal_spi_get_flag(&st_lib_p_spi_handle, SPI_FLAG_TXE) == RESET) ;
71 
72  /* Puts the SPI chip select high to end the transaction */
73  st_lib_radio_spi_cs_high();
74 
75  IRQ_ENABLE();
76 
77  return mcstate;
78 }
Header file for the STM32Cube HAL APIs.