Updates as of 2-17-16 for debugging K22F LoRa platform.
Fork of lmic_MOTE_L152RC by
hal.cpp
00001 /* HAL for MOTE_L152RC */ 00002 00003 #include "mbed.h" 00004 #include "oslmic.h " 00005 #include "debug.h" 00006 00007 #define RADIO_MOSI PTD6 00008 #define RADIO_MISO PTD7 00009 #define RADIO_SCLK PTD5 00010 #define RADIO_NSS PTD4 00011 //#define RESET_PIN PTC1 /* *** TODO *** */ 00012 00013 //#define RFSW1 PTC6 //NorAm_Mote RFSwitch_CNTR_1 *** TODO *** 00014 #define RFSW2 PTC6 //NorAm_Mote RFSwitch_CNTR_2 *** TODO *** 00015 00016 static DigitalOut nss(PTD4); 00017 static SPI spi(RADIO_MOSI, RADIO_MISO, RADIO_SCLK); // (mosi, miso, sclk) 00018 00019 //static DigitalInOut rst(RESET_PIN); 00020 00021 //DigitalOut rfsw1(RFSW1); 00022 DigitalOut rfsw2(RFSW2); 00023 00024 //DigitalOut hdr_fem_csd(PTC1); /* *** TODO *** */ 00025 00026 static InterruptIn dio0(PTC2); 00027 static InterruptIn dio1(PTC4); /* *** TODO *** */ 00028 static InterruptIn dio2(PTC3); 00029 00030 //extern RTC_HandleTypeDef RtcHandle; 00031 u4_t sleepTimeout; 00032 00033 // HAL state 00034 static struct { 00035 int irqlevel; 00036 } HAL; 00037 00038 void radio_irq_handler (u1_t dio); 00039 00040 static void dio0Irq( void ){ 00041 radio_irq_handler( 0 ); 00042 } 00043 static void dio1Irq( void ){ 00044 radio_irq_handler( 1 ); 00045 } 00046 static void dio2Irq( void ){ 00047 radio_irq_handler( 2 ); 00048 } 00049 00050 void hal_disableIRQs() 00051 { 00052 __disable_irq(); 00053 HAL.irqlevel++; 00054 } 00055 00056 void hal_enableIRQs() 00057 { 00058 if (--HAL.irqlevel == 0) { 00059 __enable_irq(); 00060 } 00061 } 00062 00063 void hal_failed () 00064 { 00065 while (1) 00066 asm("nop"); 00067 } 00068 00069 //static void rtc_wkup_irq(void) 00070 //{ 00071 // HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle); 00072 //} 00073 00074 //void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc) 00075 //{ 00076 // /* Clear Wake Up Flag */ 00077 // __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); 00078 //} 00079 00080 /*void HAL_RCC_CCSCallback() 00081 { 00082 for (;;) asm("nop"); 00083 }*/ 00084 00085 00086 volatile uint32_t /*rcc_cr_a, rcc_cr_b,*/ rcc_cr_c; 00087 void hal_init (void) 00088 { 00089 debugSW("hal_init enter\r\n"); 00090 memset(&HAL, 0x00, sizeof(HAL)); 00091 hal_disableIRQs(); 00092 00093 #if USE_SMTC_RADIO_DRIVER 00094 00095 #else 00096 // configure input lines 00097 dio0.mode(PullDown); 00098 dio0.rise(dio0Irq); 00099 dio0.enable_irq(); 00100 dio1.mode(PullDown); 00101 dio1.rise(dio1Irq); 00102 dio1.enable_irq(); 00103 dio2.mode(PullDown); 00104 dio2.rise(dio2Irq); 00105 dio2.enable_irq(); 00106 // configure reset line 00107 // rst.input(); 00108 // configure spi 00109 // spi.frequency(8000000); 00110 spi.frequency(1000000); 00111 spi.format(8, 0); 00112 nss = 1; 00113 // RFSwitch_CNTR_2 = 1; 00114 #endif 00115 00116 set_time(0); // initialize RTC 00117 00118 /* Enable Ultra low power mode */ 00119 // HAL_PWREx_EnableUltraLowPower(); 00120 00121 /* Enable the fast wake up from Ultra low power mode */ 00122 // HAL_PWREx_EnableFastWakeUp(); 00123 00124 // __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&RtcHandle, RTC_FLAG_WUTF); 00125 00126 // NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)rtc_wkup_irq); 00127 // NVIC_EnableIRQ(RTC_WKUP_IRQn); 00128 00129 // hdr_fem_csd = 0; 00130 00131 hal_enableIRQs(); 00132 00133 // GPIOA->MODER |= 0x01415500; // unused pins as outputs: PA4, PA5, PA6, PA7, PA8, (PA11,PA12 USB) 00134 // GPIOB->MODER |= 0x00000401; // unused pins as outputs: PB0(HDR_DIO1), PB5 (PB10 pulled hi by LED), PB3-T_SWO 00135 // GPIOC->MODER |= 0x00000041; // unused pins as outputs: PC0(HDR_FEM_CSD), PC3(SPI3_enable) 00136 00137 debugSW("hal_init exit\r\n"); 00138 00139 } 00140 00141 time_t lastSeconds = 0xFFFFF; 00142 u4_t hal_ticks () 00143 { 00144 time_t seconds = time(NULL); 00145 if(seconds != lastSeconds) 00146 { 00147 lastSeconds = seconds; 00148 debugSW("hal_ticks enter %d\r\n",seconds << 14); 00149 } 00150 // RTC_DateTypeDef dateStruct; 00151 // RTC_TimeTypeDef timeStruct; 00152 // struct tm timeinfo; 00153 // uint16_t sub_seconds; 00154 00155 // RtcHandle.Instance = RTC; 00156 00157 // Read actual date and time 00158 // Warning: the time must be read first! 00159 // HAL_RTC_GetTime(&RtcHandle, &timeStruct, FORMAT_BIN); 00160 // HAL_RTC_GetDate(&RtcHandle, &dateStruct, FORMAT_BIN); 00161 // sub_seconds = 16384 - timeStruct.SubSeconds; // RTC_SSR counts down 00162 00163 // Setup a tm structure based on the RTC 00164 // timeinfo.tm_wday = dateStruct.WeekDay; 00165 // timeinfo.tm_mon = dateStruct.Month - 1; 00166 // timeinfo.tm_mday = dateStruct.Date; 00167 // timeinfo.tm_year = dateStruct.Year + 100; 00168 // timeinfo.tm_hour = timeStruct.Hours; 00169 // timeinfo.tm_min = timeStruct.Minutes; 00170 // timeinfo.tm_sec = timeStruct.Seconds; 00171 00172 // Convert to timestamp 00173 // time_t t = mktime(&timeinfo); 00174 00175 // 14: SSR is driven at 16384Hz 00176 // t <<= 14; 00177 // return t | sub_seconds; 00178 return seconds << 14; 00179 } 00180 00181 void hal_waitUntil (u4_t time) 00182 { 00183 debugSW("hal_waitUntil %d\r\n", time); 00184 while (hal_ticks() < time) 00185 asm("nop"); 00186 debugSW("hal_waitUntil exit\r\n"); 00187 } 00188 00189 00190 volatile char deep_sleep; 00191 /* return 1 if target time is soon, return 0 if timer was programmed */ 00192 u1_t hal_checkTimer (u4_t time) 00193 { 00194 int d = time - hal_ticks(); 00195 if(d == 0) 00196 return 1; 00197 sleepTimeout = time; 00198 debugSW("hal_checkTimer %d %d %d\r\n",time,d,sleepTimeout); 00199 00200 // HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle); 00201 // __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&RtcHandle, RTC_FLAG_WUTF); 00202 00203 // if (d < 0x10000) { // less than 4s 00204 // deep_sleep = 0; 00205 // if (d < 4) 00206 // return 1; // very soon 00207 // if (d > ms2osticks(100)) { 00208 // d -= 13; // HSE_PLL startup time 00209 // deep_sleep = 1; 00210 // } 00211 // 61.035us steps 00212 // HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, d, RTC_WAKEUPCLOCK_RTCCLK_DIV2); 00213 // } else if (d < 0x20000) { // less than 8s 00214 // d -= 6; // HSE_PLL startup time 00215 // deep_sleep = 1; 00216 // 122us steps 00217 // HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, d >> 1, RTC_WAKEUPCLOCK_RTCCLK_DIV4); 00218 // } else if (d < 0x40000) { // less than 16s 00219 // deep_sleep = 1; 00220 // 244us steps 00221 // HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, d >> 2, RTC_WAKEUPCLOCK_RTCCLK_DIV8); 00222 // } else if (d < 0x80000) { // less than 32s 00223 // deep_sleep = 1; 00224 // 488us steps 00225 // HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, d >> 3, RTC_WAKEUPCLOCK_RTCCLK_DIV16); 00226 // } else { 00227 // deep_sleep = 1; 00228 // 1s steps to 18hours 00229 // HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, d >> 14, RTC_WAKEUPCLOCK_CK_SPRE_16BITS); 00230 /* RTC_WAKEUPCLOCK_CK_SPRE_17BITS: 18h to 36h */ 00231 /*for (;;) 00232 asm("nop");*/ 00233 // } 00234 debugSW("hal_checkTimer exit\r\n"); 00235 00236 return 0; 00237 } 00238 00239 #define SLEEP_DEBUG 1 00240 00241 void hal_sleep () 00242 { 00243 debugSW("hal_sleep enter\r\n"); 00244 hal_waitUntil(sleepTimeout); 00245 // #ifdef SLEEP_DEBUG 00246 // u4_t start_tick, end_tick; 00247 // volatile uint32_t time_asleep; 00248 //#endif /* SLEEP_DEBUG */ 00249 00250 //#ifdef USE_DEBUGGER 00251 // HAL_EnableDBGStopMode(); 00252 // if (!DBGMCU->CR & DBGMCU_CR_DBG_STOP) 00253 // for (;;) asm("nop"); 00254 //#endif /* USE_DEBUGGER */ 00255 00256 //printf("%x cr:%06x isr:%04x %d\r\n", RtcHandle.Instance->WUTR, RtcHandle.Instance->CR, RtcHandle.Instance->ISR, deep_sleep); 00257 //debug_done(); 00258 00259 // if (deep_sleep) 00260 // debug_done(); // wait here if debug still printing 00261 00262 // if (__HAL_RTC_WAKEUPTIMER_GET_FLAG(&RtcHandle, RTC_FLAG_WUTF) == 0) { 00263 // set gpio for sleep 00264 //#ifdef SLEEP_DEBUG 00265 // start_tick = hal_ticks(); 00266 //#endif /* SLEEP_DEBUG */ 00267 00268 00269 // if (deep_sleep) { 00270 //#ifndef USE_DEBUGGER 00271 /* PA13 to undriven JTMS/SWDIO pin (from AF0 to GPIO), and PA2 */ 00272 // GPIOA->MODER &= 0xf7ffffdf; 00273 // GPIOB->MODER &= 0xffffdfff; // PB6 UART_TX to input 00274 //#endif 00275 // deepsleep(); // blocks until waking 00276 //#ifndef USE_DEBUGGER 00277 // /* PA13 back to JTMS/SWDIO pin (from GPIO to AF0), and PA2 */ 00278 // GPIOA->MODER |= 0x08000020; 00279 // GPIOB->MODER |= 0x00002000; // PB6 input to UART_TX 00280 //#endif 00281 // } else 00282 // sleep(); // blocks until waking 00283 00284 //#ifdef SLEEP_DEBUG 00285 // end_tick = hal_ticks(); 00286 // time_asleep = end_tick - start_tick; 00287 // printf("%u = %u - %u\r\n", time_asleep, end_tick, start_tick); 00288 //#endif /* SLEEP_DEBUG */ 00289 // restore gpio from sleep 00290 // } 00291 debugSW("hal_sleep exit\r\n"); 00292 } 00293 00294 void hal_pin_nss (u1_t val) 00295 { 00296 nss = val; 00297 } 00298 00299 u1_t hal_spi (u1_t out) 00300 { 00301 // return spi.write(out); 00302 u1_t res = spi.write(out); 00303 debugSW("hal_spi %02X %02X\r\n",out, res); 00304 return(res); 00305 } 00306 00307 //0=RX 1=TX 00308 /*void hal_pin_rxtx (u1_t val) 00309 { 00310 rxtx = !val; 00311 }*/ 00312 #define OPMODE_LORA 0x80 00313 #define OPMODE_MASK 0x07 00314 #define OPMODE_SLEEP 0x00 00315 #define OPMODE_STANDBY 0x01 00316 #define OPMODE_FSTX 0x02 00317 #define OPMODE_TX 0x03 00318 #define OPMODE_FSRX 0x04 00319 #define OPMODE_RX 0x05 00320 #define OPMODE_RX_SINGLE 0x06 00321 #define OPMODE_CAD 0x07 00322 void hal_opmode(u1_t mode, u1_t pa_boost) 00323 { 00324 debugSW("hal_opmode %02X %02X\r\n",mode, pa_boost); 00325 if (mode == OPMODE_TX) { // start of transmission 00326 if (pa_boost) { 00327 rfsw2 = 1; //revised to match PE4259 switch controls 00328 // rfsw1 = 1; 00329 } else { 00330 rfsw2 = 1; //revised to match PE4259 switch controls 00331 // rfsw1 = 0; 00332 } 00333 // hdr_fem_csd = 0; // debug 00334 } else if (mode == OPMODE_RX || mode == OPMODE_RX_SINGLE || mode == OPMODE_CAD) { // start of reception 00335 rfsw2 = 0; //revised to match PE4259 switch controls 00336 // rfsw1 = 1; 00337 // hdr_fem_csd = 1; // debug 00338 } else { // RF switch shutdown 00339 rfsw2 = 1; //revised to match PE4259 switch controls 00340 // rfsw1 = 0; 00341 // hdr_fem_csd = 0; // debug 00342 } 00343 debugSW("hal_opmode exit\r\n"); 00344 } 00345 00346 void hal_pin_rst (u1_t val) 00347 { 00348 debugSW("hal_pin_rst %02X\r\n",val); 00349 // if (val == 0 || val == 1) { // drive pin 00350 // rst.output(); 00351 // rst = val; 00352 // } else { // keep pin floating 00353 // rst.input(); 00354 // } 00355 debugSW("hal_pin_rst exit\r\n"); 00356 }
Generated on Wed Aug 3 2022 00:13:12 by 1.7.2