Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <inttypes.h>
  4.  
  5. #include "em_device.h"
  6. #include "em_chip.h"
  7. #include "em_usart.h"
  8. #include "em_cmu.h"
  9. #include "em_emu.h"
  10. #include "em_int.h"
  11. #include "em_timer.h"
  12.  
  13. #define LED_PIN (0)
  14. #define LED_PORT (gpioPortA)
  15.  
  16. static uint32_t system_count = 0;
  17. volatile uint32_t systick_count = 0;
  18. uint32_t clock_freq;
  19.  
  20. __STATIC_INLINE void __delay_tick(volatile uint32_t n);
  21. __STATIC_INLINE uint32_t get_systick(void);
  22. void __delay_ms(uint32_t milliseconds);
  23. void setup_systick_timer(void);
  24. void reset_blink(volatile uint8_t countdown);
  25.  
  26. void init_clocks(void);
  27. void init_timer0(void);
  28. void init_usart1(void);
  29. void init_portio(void);
  30.  
  31. __STATIC_INLINE void uart1_putstr(uint8_t* buffer, size_t length);
  32.  
  33. char txbuffer[128];
  34. #define printf(...) do { \
  35. sprintf(txbuffer, __VA_ARGS__); \
  36. uart1_putstr((uint8_t*)txbuffer, strlen(txbuffer)); \
  37. } while (0)
  38.  
  39. #define putch(CH) USART_Tx(USART1, CH)
  40.  
  41. volatile uint8_t timer0_ready = 0;
  42.  
  43. /**************************************************************************//**
  44. * @brief Main function
  45. *****************************************************************************/
  46. int main(void)
  47. {
  48. /* Chip errata */
  49. CHIP_Init();
  50.  
  51. /* Enter default mode */
  52. init_clocks();
  53. init_usart1();
  54. init_portio();
  55.  
  56. /* Ensure core frequency has been updated */
  57. SystemCoreClockUpdate();
  58.  
  59. /* Start Systick Timer */
  60. setup_systick_timer();
  61.  
  62. /* Setup TIMER0 */
  63. init_timer0();
  64.  
  65. reset_blink(30);
  66.  
  67. printf("\r\n**********************************\r\n");
  68. printf("*** EFM32 GECKO FIRMWARE START ***\r\n");
  69. printf("**********************************\r\n\r\n");
  70.  
  71. /* Infinite loop */
  72. while (1) {
  73. if (timer0_ready) {
  74. timer0_ready = 0;
  75.  
  76. printf("system tick = %u\r\n", get_systick());
  77.  
  78. GPIO_PinOutClear(LED_PORT, LED_PIN);
  79. __delay_ms(20);;
  80. GPIO_PinOutSet(LED_PORT, LED_PIN);
  81. }
  82. EMU_EnterEM1(); // CPU OFF
  83. }
  84. }
  85.  
  86.  
  87. void SysTick_Handler(void)
  88. {
  89. systick_count++;
  90. }
  91.  
  92. __STATIC_INLINE uint32_t get_systick(void)
  93. {
  94. return systick_count;
  95. }
  96.  
  97. /******************************************************************************
  98. * @brief Delay function
  99. *****************************************************************************/
  100. void __delay_ms(uint32_t milliseconds)
  101. {
  102. uint32_t start_tick = get_systick();
  103. while ((get_systick() - start_tick) < milliseconds) {
  104. }
  105. }
  106.  
  107.  
  108. void setup_systick_timer(void)
  109. {
  110. clock_freq = CMU_ClockFreqGet(cmuClock_CORE);
  111. /* Enable SysTick interrupt, necessary for __delay_ms() */
  112. if (SysTick_Config(CMU_ClockFreqGet(cmuClock_CORE) / 1000)) while (1) ;
  113. NVIC_EnableIRQ(SysTick_IRQn);
  114. }
  115.  
  116.  
  117. void reset_blink(volatile uint8_t countdown)
  118. {
  119. while (countdown--) {
  120. GPIO_PinOutToggle(gpioPortA, 0);
  121. __delay_ms(20);
  122. }
  123. }
  124.  
  125.  
  126. void init_clocks(void)
  127. {
  128. // $[HFXO]
  129. CMU->CTRL = (CMU->CTRL & ~_CMU_CTRL_HFXOMODE_MASK) | CMU_CTRL_HFXOMODE_XTAL;
  130.  
  131. CMU->CTRL = (CMU->CTRL & ~_CMU_CTRL_HFXOBOOST_MASK)
  132. | CMU_CTRL_HFXOBOOST_50PCENT;
  133.  
  134. SystemHFXOClockSet(32000000);
  135.  
  136. // $[Use oscillator source]
  137. CMU->CTRL = (CMU->CTRL & ~_CMU_CTRL_LFXOMODE_MASK) | CMU_CTRL_LFXOMODE_XTAL;
  138. // [Use oscillator source]$
  139.  
  140. // $[LFXO Boost Percent]
  141. CMU->CTRL = (CMU->CTRL & ~_CMU_CTRL_LFXOBOOST_MASK)
  142. | CMU_CTRL_LFXOBOOST_100PCENT;
  143. // [LFXO Boost Percent]$
  144.  
  145. // $[LFXO enable]
  146. CMU_OscillatorEnable(cmuOsc_LFXO, true, true);
  147. // [LFXO enable]$
  148.  
  149. // $[HFXO enable]
  150. CMU_OscillatorEnable(cmuOsc_HFXO, true, true);
  151. // [HFXO enable]$
  152.  
  153. // $[High Frequency Clock select]
  154. /* Using HFXO as high frequency clock, HFCLK */
  155. CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO);
  156.  
  157. /* Enable peripheral clock */
  158. CMU_ClockEnable(cmuClock_HFPER, true);
  159.  
  160. // [High Frequency Clock select]$
  161.  
  162. // $[Peripheral Clock enables]
  163. /* Enable clock for TIMER0 */
  164. CMU_ClockEnable(cmuClock_TIMER0, true);
  165.  
  166. /* Enable clock for USART1 */
  167. CMU_ClockEnable(cmuClock_USART1, true);
  168.  
  169. /* Enable clock for GPIO by default */
  170. CMU_ClockEnable(cmuClock_GPIO, true);
  171.  
  172. // [Peripheral Clock enables]$
  173. }
  174.  
  175.  
  176.  
  177. void init_usart1(void)
  178. {
  179. // $[USART_InitAsync]
  180. USART_InitAsync_TypeDef initasync = USART_INITASYNC_DEFAULT;
  181.  
  182. initasync.baudrate = 115200;
  183. initasync.databits = usartDatabits8;
  184. initasync.parity = usartNoParity;
  185. initasync.stopbits = usartStopbits1;
  186. initasync.oversampling = usartOVS16;
  187. #if defined( USART_INPUT_RXPRS ) && defined( USART_CTRL_MVDIS )
  188. initasync.mvdis = 0;
  189. initasync.prsRxEnable = 0;
  190. initasync.prsRxCh = 0;
  191. #endif
  192.  
  193. USART_InitAsync(USART1, &initasync);
  194. // [USART_InitAsync]$
  195. }
  196.  
  197.  
  198.  
  199. void init_portio(void)
  200. {
  201. // $[Port A Configuration]
  202.  
  203. /* Pin PA0 is configured to Push-pull */
  204. GPIO->P[0].MODEL = (GPIO->P[0].MODEL & ~_GPIO_P_MODEL_MODE0_MASK)
  205. | GPIO_P_MODEL_MODE0_PUSHPULL;
  206.  
  207. /* Pin PA1 is configured to Input enabled with pull-up */
  208. GPIO->P[0].DOUT |= (1 << 1);
  209. GPIO->P[0].MODEL = (GPIO->P[0].MODEL & ~_GPIO_P_MODEL_MODE1_MASK)
  210. | GPIO_P_MODEL_MODE1_INPUTPULL;
  211. // [Port A Configuration]$
  212.  
  213. // $[Port C Configuration]
  214.  
  215. /* Pin PC0 is configured to Push-pull */
  216. GPIO->P[2].MODEL = (GPIO->P[2].MODEL & ~_GPIO_P_MODEL_MODE0_MASK)
  217. | GPIO_P_MODEL_MODE0_PUSHPULL;
  218.  
  219. /* Pin PC1 is configured to Input enabled */
  220. GPIO->P[2].MODEL = (GPIO->P[2].MODEL & ~_GPIO_P_MODEL_MODE1_MASK)
  221. | GPIO_P_MODEL_MODE1_INPUT;
  222. // [Port C Configuration]$
  223.  
  224.  
  225. // $[Route Configuration]
  226. /* Enable signals RX, TX */
  227. USART1->ROUTE |= USART_ROUTE_RXPEN | USART_ROUTE_TXPEN;
  228. // [Route Configuration]$
  229. }
  230.  
  231. void init_timer0(void)
  232. {
  233. // $[TIMER0 initialization]
  234. TIMER_Init_TypeDef init = TIMER_INIT_DEFAULT;
  235.  
  236. init.enable = 1;
  237. init.debugRun = 0;
  238. init.dmaClrAct = 0;
  239. init.sync = 0;
  240. init.clkSel = timerClkSelHFPerClk;
  241. init.prescale = timerPrescale1024;
  242. init.fallAction = timerInputActionNone;
  243. init.riseAction = timerInputActionNone;
  244. init.mode = timerModeUp;
  245. init.quadModeX4 = 0;
  246. init.oneShot = 0;
  247. TIMER_Init(TIMER0, &init);
  248.  
  249. // Enable TIMER0 overflow interrupt
  250. TIMER_IntEnable(TIMER0, TIMER_IEN_OF);
  251.  
  252. TIMER_TopSet(TIMER0, (CMU_ClockFreqGet(cmuClock_CORE) / 1024) - 1);
  253.  
  254. NVIC_SetPriority(TIMER0_IRQn, 3);
  255. NVIC_EnableIRQ(TIMER0_IRQn);
  256. }
  257.  
  258.  
  259. __STATIC_INLINE void uart1_putstr(uint8_t* buffer, size_t length)
  260. {
  261. while (length--) {
  262. putch(*buffer++);
  263. }
  264. }
  265.  
  266.  
  267. void TIMER0_IRQHandler(void)
  268. {
  269. TIMER_IntClear(TIMER0, TIMER_IF_OF);
  270. timer0_ready = 1;
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement