1 /*!
2 * \file board.c
3 *
4 * \brief Target board general functions implementation
5 *
6 * \copyright Revised BSD License, see section \ref LICENSE.
7 *
8 * \code
9 * ______ _
10 * / _____) _ | |
11 * ( (____ _____ ____ _| |_ _____ ____| |__
12 * \____ \| ___ | (_ _) ___ |/ ___) _ \
13 * _____) ) ____| | | || |_| ____( (___| | | |
14 * (______/|_____)_|_|_| \__)_____)\____)_| |_|
15 * (C)2013-2017 Semtech
16 *
17 * \endcode
18 *
19 * \author Miguel Luis ( Semtech )
20 *
21 * \author Gregory Cristian ( Semtech )
22 *
23 * \author Marten Lootsma(TWTG) on behalf of Microchip/Atmel (c)2017
24 */
25 #include <peripheral_clk_config.h>
26 #include <hal_init.h>
27 #include <hal_delay.h>
28 #include <hal_timer.h>
29 #include <hal_spi_m_sync.h>
30 #include <hal_usart_sync.h>
31 #include <hpl_rtc_base.h>
32 #include "board-config.h"
33 #include "utilities.h"
34 #include "delay.h"
35 #include "gpio.h"
36 #include "adc.h"
37 #include "spi.h"
38 #include "i2c.h"
39 #include "uart.h"
40 #include "timer.h"
41 #include "gps.h"
42 #include "rtc-board.h"
43 #include "sx1276-board.h"
44 #include "board.h"
45
46 /*
47 * MCU objects
48 */
49 Gpio_t Led1;
50 Uart_t Uart1;
51 I2c_t I2c;
52
53 /*!
54 * Flag to indicate if the MCU is Initialized
55 */
56 static bool McuInitialized = false;
57
BoardCriticalSectionBegin(uint32_t * mask)58 void BoardCriticalSectionBegin( uint32_t *mask )
59 {
60 *mask = __get_PRIMASK( );
61 __disable_irq( );
62 }
63
BoardCriticalSectionEnd(uint32_t * mask)64 void BoardCriticalSectionEnd( uint32_t *mask )
65 {
66 __set_PRIMASK( *mask );
67 }
68
BoardInitPeriph(void)69 void BoardInitPeriph( void )
70 {
71 GpioInit( &Led1, LED_1, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
72 }
73
BoardInitMcu(void)74 void BoardInitMcu( void )
75 {
76 init_mcu( );
77 delay_init( SysTick );
78
79 hri_gclk_write_PCHCTRL_reg( GCLK, EIC_GCLK_ID, CONF_GCLK_EIC_SRC | ( 1 << GCLK_PCHCTRL_CHEN_Pos ) );
80 hri_mclk_set_APBAMASK_EIC_bit( MCLK );
81
82 RtcInit( );
83
84 UartInit( &Uart1, UART_1, UART_TX, UART_RX );
85 UartConfig( &Uart1, RX_TX, 921600, UART_8_BIT, UART_1_STOP_BIT, NO_PARITY, NO_FLOW_CTRL );
86
87 SpiInit( &SX1276.Spi, SPI_1, RADIO_MOSI, RADIO_MISO, RADIO_SCLK, NC );
88 SX1276IoInit( );
89 I2cInit( &I2c, I2C_1, I2C_SCL, I2C_SDA );
90
91 McuInitialized = true;
92 SX1276IoDbgInit( );
93 SX1276IoTcxoInit( );
94 }
95
BoardResetMcu(void)96 void BoardResetMcu( void )
97 {
98 CRITICAL_SECTION_BEGIN( );
99
100 //Restart system
101 NVIC_SystemReset( );
102 }
103
BoardDeInitMcu(void)104 void BoardDeInitMcu( void )
105 {
106 SpiDeInit( &SX1276.Spi );
107 }
108
BoardGetRandomSeed(void)109 uint32_t BoardGetRandomSeed( void )
110 {
111 return 0;
112 }
113
BoardGetUniqueId(uint8_t * id)114 void BoardGetUniqueId( uint8_t *id )
115 {
116 // We don't have an ID, so use the one from Commissioning.h
117 }
118
BoardGetBatteryLevel(void)119 uint8_t BoardGetBatteryLevel( void )
120 {
121 return 0; // Battery level [0: node is connected to an external power source ...
122 }
123
GetBoardPowerSource(void)124 uint8_t GetBoardPowerSource( void )
125 {
126 return USB_POWER;
127 }
128
BoardLowPowerHandler(void)129 void BoardLowPowerHandler( void )
130 {
131 __disable_irq( );
132 /*!
133 * If an interrupt has occurred after __disable_irq( ), it is kept pending
134 * and cortex will not enter low power anyway
135 */
136
137 // Call low power handling function.
138
139 __enable_irq( );
140 }
141
142 #if !defined ( __CC_ARM )
143
144 /*
145 * Function to be used by stdout for printf etc
146 */
_write(int fd,const void * buf,size_t count)147 int _write( int fd, const void *buf, size_t count )
148 {
149 while( UartPutBuffer( &Uart1, ( uint8_t* )buf, ( uint16_t )count ) != 0 ){ };
150 return count;
151 }
152
153 /*
154 * Function to be used by stdin for scanf etc
155 */
_read(int fd,const void * buf,size_t count)156 int _read( int fd, const void *buf, size_t count )
157 {
158 size_t bytesRead = 0;
159 while( UartGetBuffer( &Uart1, ( uint8_t* )buf, count, ( uint16_t* )&bytesRead ) != 0 ){ };
160 // Echo back the character
161 while( UartPutBuffer( &Uart1, ( uint8_t* )buf, ( uint16_t )bytesRead ) != 0 ){ };
162 return bytesRead;
163 }
164
165 #else
166
167 #include <stdio.h>
168
169 // Keil compiler
fputc(int c,FILE * stream)170 int fputc( int c, FILE *stream )
171 {
172 while( UartPutChar( &Uart1, ( uint8_t )c ) != 0 );
173 return c;
174 }
175
fgetc(FILE * stream)176 int fgetc( FILE *stream )
177 {
178 uint8_t c = 0;
179 while( UartGetChar( &Uart1, &c ) != 0 );
180 // Echo back the character
181 while( UartPutChar( &Uart1, c ) != 0 );
182 return ( int )c;
183 }
184
185 #endif
186
187 #ifdef USE_FULL_ASSERT
188
189 #include <stdio.h>
190
191 /*
192 * Function Name : assert_failed
193 * Description : Reports the name of the source file and the source line number
194 * where the assert_param error has occurred.
195 * Input : - file: pointer to the source file name
196 * - line: assert_param error line source number
197 * Output : None
198 * Return : None
199 */
assert_failed(uint8_t * file,uint32_t line)200 void assert_failed( uint8_t* file, uint32_t line )
201 {
202 /* User can add his own implementation to report the file name and line number,
203 ex: printf("Wrong parameters value: file %s on line %u\n", file, line) */
204
205 printf( "Wrong parameters value: file %s on line %u\n", ( const char* )file, line );
206 /* Infinite loop */
207 while( 1 )
208 {
209 }
210 }
211 #endif
212