Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. #include "contiki.h"
  2. #include "sys/clock.h"
  3. #include "msp432-def.h"
  4. //#include "rom_map.h"
  5. #include "driverlib.h"
  6.  
  7. static unsigned long overflow_counter;
  8.  
  9. #define TICKS_PER_SEC 1024
  10. #define INTERVAL (MAP_CS_getMCLK() / TICKS_PER_SEC)
  11.  
  12. void clock_init(void){
  13. MAP_GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_PJ, GPIO_PIN0 | GPIO_PIN1, GPIO_PRIMARY_MODULE_FUNCTION); // PJ.0 and PJ.1: LF crystal in and out
  14. MAP_GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_PJ, GPIO_PIN2 | GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION); // PJ.2 and PJ.3: HF crystal in and out
  15.  
  16. MAP_CS_setReferenceOscillatorFrequency(CS_REFO_32KHZ);
  17. MAP_CS_setExternalClockSourceFrequency(LFXTCLK_SPEED, HFXTCLK_SPEED); // this is necessary for CS_startHFXT() to work properly
  18.  
  19. MAP_CS_setDCOFrequency(DCOCLK_SPEED); // set DCO frequency (this function uses CS_setDCOCenteredFrequency and CS_tuneDCOFrequency and is not available on pre-release devices)
  20. MAP_CS_startLFXT(CS_LFXT_DRIVE0); // enable LF crystal in non-bypass mode without a timeout (lowest current drain with drive level = 0)
  21.  
  22. // enable the HF crystal:
  23. MAP_CS_startHFXT(0); // start the HF crystal
  24.  
  25. // select clock sources
  26. MAP_CS_initClockSignal(CS_MCLK, MCLK_SRC, MCLK_DIV); // MCLK = HFXTCLK
  27. MAP_CS_initClockSignal(CS_HSMCLK, HSMCLK_SRC, HSMCLK_DIV); // HSMCLK = DCOCLK
  28. MAP_CS_initClockSignal(CS_SMCLK, SMCLK_SRC, SMCLK_DIV); // SMCLK = DCOCLK
  29. MAP_CS_initClockSignal(CS_ACLK, ACLK_SRC, ACLK_DIV); // ACLK = LFXTCLK
  30. MAP_CS_initClockSignal(CS_BCLK, BCLK_SRC, BCLK_DIV); // BCLK = REFOCLk
  31.  
  32. overflow_counter = 0;
  33. MAP_SysTick_enableModule();
  34. MAP_SysTick_setPeriod(INTERVAL);
  35. MAP_Interrupt_enableSleepOnIsrExit();
  36. MAP_SysTick_enableInterrupt();
  37. MAP_Interrupt_enableMaster();
  38. }
  39.  
  40. clock_time_t clock_time(void){
  41. return (INTERVAL * overflow_counter + MAP_SysTick_getValue());
  42. }
  43.  
  44. unsigned long clock_seconds(void){
  45. return ((INTERVAL * overflow_counter + MAP_SysTick_getValue()) / MAP_CS_getMCLK());
  46. }
  47.  
  48. void clock_set_seconds(unsigned long sec){
  49. overflow_counter = ((float)TICKS_PER_SEC) * sec;
  50. }
  51.  
  52. void clock_wait(clock_time_t t){
  53. clock_time_t start_time = MAP_SysTick_getValue();
  54. while(MAP_SysTick_getValue() < (start_time + t));
  55. }
  56.  
  57. void clock_delay_usec(uint16_t dt){
  58. clock_time_t number_ticks = (MAP_CS_getMCLK() * dt) / 1000000;
  59. clock_wait(number_ticks);
  60. }
  61.  
  62. void SysTick_Handler(void){
  63. overflow_counter++;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement