|
1 | | -/* |
2 | | - / _____) _ | | |
3 | | -( (____ _____ ____ _| |_ _____ ____| |__ |
4 | | - \____ \| ___ | (_ _) ___ |/ ___) _ \ |
5 | | - _____) ) ____| | | || |_| ____( (___| | | | |
6 | | -(______/|_____)_|_|_| \__)_____)\____)_| |_| |
7 | | - (C)2013 Semtech |
8 | | -
|
9 | | -Description: LoRaMote board USB DFU bootloader |
10 | | -
|
11 | | -License: Revised BSD License, see LICENSE.TXT file include in the project |
12 | | -
|
13 | | -Maintainer: Miguel Luis and Gregory Cristian |
14 | | -*/ |
15 | | -#include "board.h" |
16 | | -#include "usbd_core.h" |
17 | | -#include "usbd_desc.h" |
18 | | -#include "usbd_dfu.h" |
19 | | -#include "usbd_dfu_flash.h" |
20 | | - |
21 | | -extern PCD_HandleTypeDef hpcd; |
22 | | - |
23 | | -USBD_HandleTypeDef USBD_Device; |
24 | | -pFunction JumpToApplication; |
25 | | -uint32_t JumpAddress; |
26 | | - |
27 | | -/* |
28 | | - * Board peripherals objects |
29 | | - */ |
30 | | -Gpio_t Led1; |
31 | | -Gpio_t Led2; |
32 | | -Gpio_t Led3; |
33 | | - |
34 | | -I2c_t I2c; |
35 | | - |
36 | | -void SystemClockConfig( void ); |
37 | | - |
38 | | -static void DelayLoop( volatile uint32_t nCount ) |
39 | | -{ |
40 | | - volatile uint32_t index = 0; |
41 | | - for( index = ( 5000 * nCount ); index != 0; index-- ) |
42 | | - { |
43 | | - } |
44 | | -} |
45 | | - |
46 | | -int main( void ) |
47 | | -{ |
48 | | - uint8_t regValue = 0; |
49 | | - uint8_t status = 0; |
50 | | - uint16_t offset = 0; |
51 | | - |
52 | | - /* STM32L1xx HAL library initialization: |
53 | | - - Configure the Flash prefetch |
54 | | - - Systick timer is configured by default as source of time base, but user |
55 | | - can eventually implement his proper time base source (a general purpose |
56 | | - timer for example or other time source), keeping in mind that Time base |
57 | | - duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and |
58 | | - handled in milliseconds basis. |
59 | | - - Set NVIC Group Priority to 4 |
60 | | - - Low Level Initialization |
61 | | - */ |
62 | | - HAL_Init( ); |
63 | | - |
64 | | - SystemClockConfig( ); |
65 | | - |
66 | | - I2cInit( &I2c, I2C_SCL, I2C_SDA ); |
67 | | - |
68 | | - GpioInit( &Led1, LED_1, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 1 ); |
69 | | - GpioInit( &Led2, LED_2, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 1 ); |
70 | | - GpioInit( &Led3, LED_3, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 1 ); |
71 | | - |
72 | | - // Init SAR |
73 | | - SX9500Init( ); |
74 | | - DelayLoop( 100 ); |
75 | | - SX9500Write( SX9500_REG_IRQMSK, 0x10 ); |
76 | | - SX9500Write( SX9500_REG_IRQSRC, 0x10 ); |
77 | | - |
78 | | - do |
79 | | - { |
80 | | - SX9500Read( SX9500_REG_IRQSRC, &status ); |
81 | | - }while( ( status & 0x10 ) == 0x00 ); // While compensation for CS0 is pending |
82 | | - |
83 | | - // Read 1st sensor offset |
84 | | - SX9500Read( SX9500_REG_OFFSETMSB, ( uint8_t* )®Value ); |
85 | | - offset = regValue << 8; |
86 | | - SX9500Read( SX9500_REG_OFFSETLSB, ( uint8_t* )®Value ); |
87 | | - offset |= regValue; |
88 | | - |
89 | | - if( offset < 2000 ) |
90 | | - { /* Test if user code is programmed starting from address 0x08007000 */ |
91 | | - if( ( ( *( volatile uint32_t* )USBD_DFU_APP_DEFAULT_ADD ) & 0x2FFE0000 ) == 0x20000000 ) |
92 | | - { |
93 | | - /* Jump to user application */ |
94 | | - JumpAddress = *( volatile uint32_t* ) ( USBD_DFU_APP_DEFAULT_ADD + 4 ); |
95 | | - JumpToApplication = ( pFunction ) JumpAddress; |
96 | | - |
97 | | - /* Initialize user application's Stack Pointer */ |
98 | | - __set_MSP( *( volatile uint32_t* ) USBD_DFU_APP_DEFAULT_ADD ); |
99 | | - JumpToApplication( ); |
100 | | - } |
101 | | - } /* Otherwise enters DFU mode to allow user to program his application */ |
102 | | - |
103 | | - /* Init Device Library */ |
104 | | - USBD_Init( &USBD_Device, &DFU_Desc, 0 ); |
105 | | - |
106 | | - /* Add Supported Class */ |
107 | | - USBD_RegisterClass( &USBD_Device, USBD_DFU_CLASS ); |
108 | | - |
109 | | - /* Add DFU Media interface */ |
110 | | - USBD_DFU_RegisterMedia( &USBD_Device, &USBD_DFU_Flash_fops ); |
111 | | - |
112 | | - /* Start Device Process */ |
113 | | - USBD_Start( &USBD_Device ); |
114 | | - |
115 | | - /* Main loop */ |
116 | | - while( 1 ) |
117 | | - { |
118 | | - GpioWrite( &Led1, 0 ); |
119 | | - GpioWrite( &Led2, 0 ); |
120 | | - GpioWrite( &Led3, 0 ); |
121 | | - DelayLoop( 500 ); |
122 | | - GpioWrite( &Led1, 1 ); |
123 | | - GpioWrite( &Led2, 1 ); |
124 | | - GpioWrite( &Led3, 1 ); |
125 | | - DelayLoop( 500 ); |
126 | | - } |
127 | | -} |
128 | | - |
129 | | -/*! |
130 | | - * System Clock Configuration |
131 | | - */ |
132 | | -void SystemClockConfig( void ) |
133 | | -{ |
134 | | - RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
135 | | - RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
136 | | - |
137 | | - /* Enable HSI Oscillator and Activate PLL with HSI as source */ |
138 | | - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; |
139 | | - RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
140 | | - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
141 | | - RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; |
142 | | - RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6; |
143 | | - RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3; |
144 | | - HAL_RCC_OscConfig(&RCC_OscInitStruct); |
145 | | - |
146 | | - /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 |
147 | | - clocks dividers */ |
148 | | - RCC_ClkInitStruct.ClockType = ( RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 ); |
149 | | - RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
150 | | - RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
151 | | - RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; |
152 | | - RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
153 | | - HAL_RCC_ClockConfig( &RCC_ClkInitStruct, FLASH_LATENCY_1 ); |
154 | | -} |
155 | | - |
156 | | -void SysTick_Handler( void ) |
157 | | -{ |
158 | | - HAL_IncTick( ); |
159 | | -} |
160 | | - |
161 | | -void USB_LP_IRQHandler( void ) |
162 | | -{ |
163 | | - HAL_PCD_IRQHandler( &hpcd ); |
164 | | -} |
165 | | - |
166 | | -#ifdef USE_FULL_ASSERT |
167 | | -/* |
168 | | - * Function Name : assert_failed |
169 | | - * Description : Reports the name of the source file and the source line number |
170 | | - * where the assert_param error has occurred. |
171 | | - * Input : - file: pointer to the source file name |
172 | | - * - line: assert_param error line source number |
173 | | - * Output : None |
174 | | - * Return : None |
175 | | - */ |
176 | | -void assert_failed( uint8_t* file, uint32_t line ) |
177 | | -{ |
178 | | - /* User can add his own implementation to report the file name and line number, |
179 | | - ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
180 | | - |
181 | | - /* Infinite loop */ |
182 | | - while( 1 ) |
183 | | - { |
184 | | - } |
185 | | -} |
186 | | -#endif |
| 1 | +/* |
| 2 | + / _____) _ | | |
| 3 | +( (____ _____ ____ _| |_ _____ ____| |__ |
| 4 | + \____ \| ___ | (_ _) ___ |/ ___) _ \ |
| 5 | + _____) ) ____| | | || |_| ____( (___| | | | |
| 6 | +(______/|_____)_|_|_| \__)_____)\____)_| |_| |
| 7 | + (C)2013 Semtech |
| 8 | +
|
| 9 | +Description: LoRaMote board USB DFU bootloader |
| 10 | +
|
| 11 | +License: Revised BSD License, see LICENSE.TXT file include in the project |
| 12 | +
|
| 13 | +Maintainer: Miguel Luis and Gregory Cristian |
| 14 | +*/ |
| 15 | +#include "board.h" |
| 16 | +#include "usbd_core.h" |
| 17 | +#include "usbd_desc.h" |
| 18 | +#include "usbd_dfu.h" |
| 19 | +#include "usbd_dfu_flash.h" |
| 20 | + |
| 21 | +extern PCD_HandleTypeDef hpcd; |
| 22 | + |
| 23 | +USBD_HandleTypeDef USBD_Device; |
| 24 | +pFunction JumpToApplication; |
| 25 | +uint32_t JumpAddress; |
| 26 | + |
| 27 | +/* |
| 28 | + * Board peripherals objects |
| 29 | + */ |
| 30 | +Gpio_t Led1; |
| 31 | +Gpio_t Led2; |
| 32 | +Gpio_t Led3; |
| 33 | + |
| 34 | +I2c_t I2c; |
| 35 | + |
| 36 | +void SystemClockConfig( void ); |
| 37 | + |
| 38 | +static void DelayLoop( volatile uint32_t nCount ) |
| 39 | +{ |
| 40 | + volatile uint32_t index = 0; |
| 41 | + for( index = ( 5000 * nCount ); index != 0; index-- ) |
| 42 | + { |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +int main( void ) |
| 47 | +{ |
| 48 | + uint8_t regValue = 0; |
| 49 | + uint8_t status = 0; |
| 50 | + uint16_t offset = 0; |
| 51 | + |
| 52 | + /* STM32L1xx HAL library initialization: |
| 53 | + - Configure the Flash prefetch |
| 54 | + - Systick timer is configured by default as source of time base, but user |
| 55 | + can eventually implement his proper time base source (a general purpose |
| 56 | + timer for example or other time source), keeping in mind that Time base |
| 57 | + duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and |
| 58 | + handled in milliseconds basis. |
| 59 | + - Set NVIC Group Priority to 4 |
| 60 | + - Low Level Initialization |
| 61 | + */ |
| 62 | + HAL_Init( ); |
| 63 | + |
| 64 | + SystemClockConfig( ); |
| 65 | + |
| 66 | + I2cInit( &I2c, I2C_SCL, I2C_SDA ); |
| 67 | + |
| 68 | + GpioInit( &Led1, LED_1, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 1 ); |
| 69 | + GpioInit( &Led2, LED_2, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 1 ); |
| 70 | + GpioInit( &Led3, LED_3, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 1 ); |
| 71 | + |
| 72 | + // Init SAR |
| 73 | + SX9500Init( ); |
| 74 | + DelayLoop( 100 ); |
| 75 | + SX9500Write( SX9500_REG_IRQMSK, 0x10 ); |
| 76 | + SX9500Write( SX9500_REG_IRQSRC, 0x10 ); |
| 77 | + |
| 78 | + do |
| 79 | + { |
| 80 | + SX9500Read( SX9500_REG_IRQSRC, &status ); |
| 81 | + }while( ( status & 0x10 ) == 0x00 ); // While compensation for CS0 is pending |
| 82 | + |
| 83 | + // Read 1st sensor offset |
| 84 | + SX9500Read( SX9500_REG_OFFSETMSB, ( uint8_t* )®Value ); |
| 85 | + offset = regValue << 8; |
| 86 | + SX9500Read( SX9500_REG_OFFSETLSB, ( uint8_t* )®Value ); |
| 87 | + offset |= regValue; |
| 88 | + |
| 89 | + if( offset < 2000 ) |
| 90 | + { /* Test if user code is programmed starting from address 0x08007000 */ |
| 91 | + if( ( ( *( volatile uint32_t* )USBD_DFU_APP_DEFAULT_ADD ) & 0x2FFE0000 ) == 0x20000000 ) |
| 92 | + { |
| 93 | + /* Jump to user application */ |
| 94 | + JumpAddress = *( volatile uint32_t* ) ( USBD_DFU_APP_DEFAULT_ADD + 4 ); |
| 95 | + JumpToApplication = ( pFunction ) JumpAddress; |
| 96 | + |
| 97 | + /* Initialize user application's Stack Pointer */ |
| 98 | + __set_MSP( *( volatile uint32_t* ) USBD_DFU_APP_DEFAULT_ADD ); |
| 99 | + JumpToApplication( ); |
| 100 | + } |
| 101 | + } /* Otherwise enters DFU mode to allow user to program his application */ |
| 102 | + |
| 103 | + /* Init Device Library */ |
| 104 | + USBD_Init( &USBD_Device, &DFU_Desc, 0 ); |
| 105 | + |
| 106 | + /* Add Supported Class */ |
| 107 | + USBD_RegisterClass( &USBD_Device, USBD_DFU_CLASS ); |
| 108 | + |
| 109 | + /* Add DFU Media interface */ |
| 110 | + USBD_DFU_RegisterMedia( &USBD_Device, &USBD_DFU_Flash_fops ); |
| 111 | + |
| 112 | + /* Start Device Process */ |
| 113 | + USBD_Start( &USBD_Device ); |
| 114 | + |
| 115 | + /* Main loop */ |
| 116 | + while( 1 ) |
| 117 | + { |
| 118 | + GpioWrite( &Led1, 0 ); |
| 119 | + GpioWrite( &Led2, 0 ); |
| 120 | + GpioWrite( &Led3, 0 ); |
| 121 | + DelayLoop( 500 ); |
| 122 | + GpioWrite( &Led1, 1 ); |
| 123 | + GpioWrite( &Led2, 1 ); |
| 124 | + GpioWrite( &Led3, 1 ); |
| 125 | + DelayLoop( 500 ); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +/*! |
| 130 | + * System Clock Configuration |
| 131 | + */ |
| 132 | +void SystemClockConfig( void ) |
| 133 | +{ |
| 134 | + RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
| 135 | + RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
| 136 | + |
| 137 | + /* Enable HSI Oscillator and Activate PLL with HSI as source */ |
| 138 | + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; |
| 139 | + RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
| 140 | + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 141 | + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; |
| 142 | + RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6; |
| 143 | + RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3; |
| 144 | + HAL_RCC_OscConfig(&RCC_OscInitStruct); |
| 145 | + |
| 146 | + /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 |
| 147 | + clocks dividers */ |
| 148 | + RCC_ClkInitStruct.ClockType = ( RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 ); |
| 149 | + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
| 150 | + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
| 151 | + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; |
| 152 | + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
| 153 | + HAL_RCC_ClockConfig( &RCC_ClkInitStruct, FLASH_LATENCY_1 ); |
| 154 | +} |
| 155 | + |
| 156 | +void SysTick_Handler( void ) |
| 157 | +{ |
| 158 | + HAL_IncTick( ); |
| 159 | +} |
| 160 | + |
| 161 | +void USB_LP_IRQHandler( void ) |
| 162 | +{ |
| 163 | + HAL_PCD_IRQHandler( &hpcd ); |
| 164 | +} |
| 165 | + |
| 166 | +#ifdef USE_FULL_ASSERT |
| 167 | +/* |
| 168 | + * Function Name : assert_failed |
| 169 | + * Description : Reports the name of the source file and the source line number |
| 170 | + * where the assert_param error has occurred. |
| 171 | + * Input : - file: pointer to the source file name |
| 172 | + * - line: assert_param error line source number |
| 173 | + * Output : None |
| 174 | + * Return : None |
| 175 | + */ |
| 176 | +void assert_failed( uint8_t* file, uint32_t line ) |
| 177 | +{ |
| 178 | + /* User can add his own implementation to report the file name and line number, |
| 179 | + ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
| 180 | + |
| 181 | + /* Infinite loop */ |
| 182 | + while( 1 ) |
| 183 | + { |
| 184 | + } |
| 185 | +} |
| 186 | +#endif |
0 commit comments