Advertisement
Guest User

NAU88C22 pll set

a guest
Jun 15th, 2025
4
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. float32_t nau_mclksel[] = {1.0f, 1.5f, 2.0f, 3.0f, 4.0f, 6.0f, 8.0f, 12.0f};
  2.  
  3. uint8_t nau88c22_set_fs_pll(float fs_hz, uint8_t pck_prescaller){
  4.    
  5.     if(fs_hz < 11999)return 1;
  6.     if(fs_hz > 48001)return 2;
  7.  
  8.     float32_t frq_fs = fs_hz; // Hz
  9.     float32_t frq_imclk = 256.0f * frq_fs;
  10.     float32_t nau_mckl_in = (float32_t)(sysclk_get_cpu_hz() / pck_prescaller);
  11.    
  12.     if(nau_mckl_in > 33000000.0f)return 3;
  13.     if(nau_mckl_in < 9000000.0f)return 4;
  14.        
  15.     uint8_t mclkseldiv = 0;
  16.     uint8_t pll49mout = 0;
  17.        
  18.     float32_t nau_pll_f2 = 0.0f;
  19.     float32_t nau_pll_f1 = 0.0f;
  20.        
  21.     for(uint8_t i=0; i<8; i++){
  22.         nau_pll_f2 = 4.0f * nau_mclksel[i] * frq_imclk;
  23.         if( (nau_pll_f2 >= 80000000.0) && (nau_pll_f2 < 110000000.0f) ){
  24.             mclkseldiv = i;
  25.             break;
  26.         };
  27.     };
  28.        
  29.     if(mclkseldiv == 0){
  30.         for(uint8_t i=0; i<8; i++){
  31.             nau_pll_f2 = 2.0f * nau_mclksel[i] * frq_imclk;
  32.             if( (nau_pll_f2 >= 80000000.0) && (nau_pll_f2 < 110000000.0f) ){
  33.                 mclkseldiv = i;
  34.                 break;
  35.             };
  36.         };
  37.     };
  38.        
  39.     uint8_t nau_pres_mckl = 3;
  40.     uint8_t nau_r1_ready = 0;
  41.     uint8_t nau_nxy = 0;
  42.     float32_t nau_pll = 0.0f;
  43.     uint32_t nau_int_pll = 0;
  44.        
  45.     nau_pll_f1 = nau_mckl_in;
  46.        
  47.     float32_t nau_pll_r = nau_pll_f2 / nau_pll_f1;
  48.        
  49.     if( (nau_pll_r > 6.0f) && (nau_pll_r < 13.0f)){
  50.         nau_pres_mckl = 1;
  51.     }else{
  52.         nau_pll_r = nau_pll_f2 / (nau_pll_f1 * 2);
  53.         nau_pres_mckl = 2;
  54.     };
  55.        
  56.     if( (nau_pll_r > 6.0f) && (nau_pll_r < 13.0f)){
  57.         nau_nxy = (uint8_t)floorf(nau_pll_r);
  58.         nau_pll = nau_pll_r - (float32_t)(nau_nxy);
  59.         nau_pll *= 16777216.0f;
  60.         nau_int_pll = (uint32_t)nau_pll;
  61.         nau_r1_ready = 1;
  62.     };
  63.    
  64.     uint8_t pll_k1 = (nau_int_pll >> 18) & 0x3F;
  65.     uint16_t pll_k2 = (nau_int_pll >> 9) & 0x1FF;
  66.     uint16_t pll_k3 = (nau_int_pll >> 0) & 0x1FF;
  67.        
  68.     if(nau_r1_ready == 1){
  69.         nau8812_clr_bit(POWER_MANAGMENT_1, (1<<PLLEN));
  70.         nau8812_write(CLOCK_CONTROL_1, (1<<CLKM)|(mclkseldiv<<MCLKSEL)|(0xD));
  71.         nau8812_write(PLLN_N, ( (nau_pres_mckl-1) << PLLMCLK) | (nau_nxy) );
  72.         nau8812_write(PLL_K_1, pll_k1);
  73.         nau8812_write(PLL_K_2, pll_k2);
  74.         nau8812_write(PLL_K_3, pll_k3);
  75.     }else{
  76.         return 5;
  77.     }
  78.    
  79.     nau8812_set_bit(POWER_MANAGMENT_1, (1<<PLLEN));
  80.     return 0;
  81. }
Tags: #c
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement