Changeset 86 for ctrl/firmware/Main/SES/Core
- Timestamp:
- Feb 11, 2025, 1:09:52 PM (3 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ctrl/firmware/Main/SES/Core/Src/onewire_task.cpp
r85 r86 1 1 #include <cstdint> 2 #include <cstdio> 2 3 3 4 #include "onewire_task.h" … … 7 8 8 9 static constexpr uint16_t delay_ms = 5000U; 9 10 #ifdef DEBUG 11 static constexpr uint16_t DS18B20_480µs_DELAY = 700U; 12 static constexpr uint16_t DS18B20_400µs_DELAY = 620U; 13 static constexpr uint16_t DS18B20_80µs_DELAY = 80U; 14 static constexpr uint16_t DS18B20_60µs_DELAY = 60U; 15 static constexpr uint16_t DS18B20_50µs_DELAY = 46U; 16 static constexpr uint16_t DS18B20_45µs_DELAY = 45U; 17 static constexpr uint16_t DS18B20_15µs_DELAY = 15U; 18 static constexpr uint16_t DS18B20_2µs_DELAY = 2U; 19 static constexpr uint16_t DS18B20_1µs_DELAY = 0U; 20 #else 21 static constexpr uint16_t DS18B20_480µs_DELAY = 700U; 22 static constexpr uint16_t DS18B20_400µs_DELAY = 620U; 23 static constexpr uint16_t DS18B20_80µs_DELAY = 80U; 24 static constexpr uint16_t DS18B20_60µs_DELAY = 60U; 25 static constexpr uint16_t DS18B20_50µs_DELAY = 50U; 26 static constexpr uint16_t DS18B20_45µs_DELAY = 45U; 27 static constexpr uint16_t DS18B20_15µs_DELAY = 15U; 28 static constexpr uint16_t DS18B20_2µs_DELAY = 2U; 29 static constexpr uint16_t DS18B20_1µs_DELAY = 0U; 30 #endif 31 32 static constexpr uint8_t DS18B20_SKIP_ROM_CMD = 0xCC; 33 static constexpr uint8_t DS18B20_CONVERT_CMD = 0x44; 34 static constexpr uint8_t DS18B20_READ_SCRPAD_CMD = 0xBE; 35 36 static constexpr GPIO_PinState DS18B20_HI_LEVEL = GPIO_PIN_SET; 37 static constexpr GPIO_PinState DS18B20_LO_LEVEL = GPIO_PIN_RESET; 38 39 enum class DeviceOnBus { PRESENT, NOT_PRESENT }; 40 enum class Conversion { READY, NOT_READY }; 10 41 11 42 QueueHandle_t onewireQueue; 12 43 static onewire_msg_t owm; 13 44 14 inline void onewireDelay(uint16_t us); 45 void DS18B20_Delay(uint16_t us); 46 DeviceOnBus DS18B20_Start(void); 47 void DS18B20_Write(uint8_t data); 48 uint8_t DS18B20_Read(void); 49 Conversion DS18B20_Conversion(void); 15 50 16 51 void onewireTaskStart(void* argument) … … 18 53 UNUSED(argument); 19 54 20 onewireDelay(75);21 22 55 while (1) 23 56 { 57 DeviceOnBus presence = DS18B20_Start(); 58 if (presence == DeviceOnBus::PRESENT) 59 { 60 //printf("DS18B20-like device is detected.\n"); 61 DS18B20_Write(DS18B20_SKIP_ROM_CMD); 62 DS18B20_Write(DS18B20_CONVERT_CMD); 63 64 if (DS18B20_Conversion() == Conversion::READY) 65 { 66 printf("Conversion is finished.\n"); 67 68 presence = DS18B20_Start(); 69 if (presence == DeviceOnBus::PRESENT) 70 { 71 DS18B20_Write(DS18B20_SKIP_ROM_CMD); 72 DS18B20_Write(DS18B20_READ_SCRPAD_CMD); 73 74 uint16_t temp_lo = DS18B20_Read(); 75 uint16_t temp_hi = DS18B20_Read(); 76 77 uint16_t Temp = (temp_hi << 8U) | temp_lo; 78 float T = (float)Temp / 16.0; 79 printf("T = %f\n", T); 80 } 81 } 82 } 83 84 //DS18B20_Write(0xFA); 85 24 86 /*BaseType_t res = xQueueReceive(beeperQueue, &bm, portMAX_DELAY); 25 87 if (res == pdPASS) … … 39 101 //------------------------------------------------------------------------------ 40 102 41 uint8_t DS18B20_Start(void) 42 { 43 uint8_t Response = 0; 44 Set_Pin_Output(ONE, DS18B20_PIN); // set the pin as output 45 HAL_GPIO_WritePin (DS18B20_PORT, DS18B20_PIN, 0); // pull the pin low 46 delay (480); // delay according to datasheet 47 48 Set_Pin_Input(DS18B20_PORT, DS18B20_PIN); // set the pin as input 49 } 50 51 //------------------------------------------------------------------------------ 52 53 inline void onewireDelay(uint16_t us) 54 { 103 Conversion DS18B20_Conversion(void) 104 { 105 GPIO_InitTypeDef GPIO_InitStruct = GPIO_InitTypeDef(); 106 107 GPIO_InitStruct.Pin = ONEWIRE_TEMP_BUS_Pin; 108 109 vPortEnterCritical(); 110 111 GPIO_PinState pinState = DS18B20_LO_LEVEL; 112 auto cnt = 15000U; // Total awaiting time will be greater than max conversion time with 12bit resolution 113 do 114 { 115 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 116 HAL_GPIO_Init(ONEWIRE_TEMP_BUS_GPIO_Port, &GPIO_InitStruct); // set the pin as output 117 HAL_GPIO_WritePin(ONEWIRE_TEMP_BUS_GPIO_Port, ONEWIRE_TEMP_BUS_Pin, DS18B20_LO_LEVEL); // pull the data pin LOW 118 119 DS18B20_Delay(DS18B20_1µs_DELAY); // wait for 1 us 120 121 GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 122 HAL_GPIO_Init(ONEWIRE_TEMP_BUS_GPIO_Port, &GPIO_InitStruct); // set the pin as output 123 124 DS18B20_Delay(DS18B20_15µs_DELAY); // wait for 15 us 125 126 pinState = HAL_GPIO_ReadPin(ONEWIRE_TEMP_BUS_GPIO_Port, ONEWIRE_TEMP_BUS_Pin); 127 128 DS18B20_Delay(DS18B20_45µs_DELAY); // wait for 45 us 129 } 130 while(--cnt && pinState == DS18B20_LO_LEVEL); 131 132 vPortExitCritical(); 133 134 if (cnt) return Conversion::READY; 135 else return Conversion::NOT_READY; 136 } 137 138 //------------------------------------------------------------------------------ 139 140 uint8_t DS18B20_Read(void) 141 { 142 GPIO_InitTypeDef GPIO_InitStruct = GPIO_InitTypeDef(); 143 uint8_t value=0; 144 145 GPIO_InitStruct.Pin = ONEWIRE_TEMP_BUS_Pin; 146 //GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 147 //HAL_GPIO_Init(ONEWIRE_TEMP_BUS_GPIO_Port, &GPIO_InitStruct); // set the pin as input 148 149 vPortEnterCritical(); 150 151 for (auto i = 0U; i < 8U; i++) 152 { 153 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 154 HAL_GPIO_Init(ONEWIRE_TEMP_BUS_GPIO_Port, &GPIO_InitStruct); // set the pin as output 155 HAL_GPIO_WritePin(ONEWIRE_TEMP_BUS_GPIO_Port, ONEWIRE_TEMP_BUS_Pin, DS18B20_LO_LEVEL); // pull the data pin LOW 156 157 DS18B20_Delay(DS18B20_1µs_DELAY); // wait for 2 us 158 159 GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 160 HAL_GPIO_Init(ONEWIRE_TEMP_BUS_GPIO_Port, &GPIO_InitStruct); // set the pin as input 161 162 DS18B20_Delay(DS18B20_15µs_DELAY); // wait for 15us 163 164 if (HAL_GPIO_ReadPin(ONEWIRE_TEMP_BUS_GPIO_Port, ONEWIRE_TEMP_BUS_Pin) == DS18B20_HI_LEVEL) // if the pin is HIGH 165 value |= 1U << i; // read = 1 166 167 DS18B20_Delay(DS18B20_45µs_DELAY); // wait for 45 us 168 } 169 170 vPortExitCritical(); 171 172 return value; 173 } 174 175 //------------------------------------------------------------------------------ 176 177 void DS18B20_Write(uint8_t data) 178 { 179 GPIO_InitTypeDef GPIO_InitStruct = GPIO_InitTypeDef(); 180 181 GPIO_InitStruct.Pin = ONEWIRE_TEMP_BUS_Pin; 182 //GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 183 //GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 184 //HAL_GPIO_Init(ONEWIRE_TEMP_BUS_GPIO_Port, &GPIO_InitStruct); // set the pin as output 185 186 vPortEnterCritical(); 187 188 for (auto i = 0U; i < 8U; i++) 189 { 190 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 191 HAL_GPIO_Init(ONEWIRE_TEMP_BUS_GPIO_Port, &GPIO_InitStruct); // set the pin as output 192 HAL_GPIO_WritePin(ONEWIRE_TEMP_BUS_GPIO_Port, ONEWIRE_TEMP_BUS_Pin, DS18B20_LO_LEVEL); // pull the pin LOW 193 194 if ((data & (1U << i)) != 0U) // if the bit is high 195 { 196 // write 1 197 //GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 198 //HAL_GPIO_Init(ONEWIRE_TEMP_BUS_GPIO_Port, &GPIO_InitStruct); // set the pin as output 199 //HAL_GPIO_WritePin(ONEWIRE_TEMP_BUS_GPIO_Port, ONEWIRE_TEMP_BUS_Pin, DS18B20_LO_LEVEL); // pull the pin LOW 200 DS18B20_Delay(DS18B20_1µs_DELAY); // wait for 1 us 201 202 GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 203 HAL_GPIO_Init(ONEWIRE_TEMP_BUS_GPIO_Port, &GPIO_InitStruct); // set the pin as input 204 DS18B20_Delay(DS18B20_60µs_DELAY); // wait for 60 us 205 } 206 else // if the bit is low 207 { 208 // write 0 209 //GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 210 //HAL_GPIO_Init(ONEWIRE_TEMP_BUS_GPIO_Port, &GPIO_InitStruct); // set the pin as output 211 //HAL_GPIO_WritePin (ONEWIRE_TEMP_BUS_GPIO_Port, ONEWIRE_TEMP_BUS_Pin, DS18B20_LO_LEVEL); // pull the pin LOW 212 DS18B20_Delay(DS18B20_1µs_DELAY); // wait for 1 us 213 214 DS18B20_Delay(DS18B20_60µs_DELAY); // wait for 60 us 215 216 GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 217 HAL_GPIO_Init(ONEWIRE_TEMP_BUS_GPIO_Port, &GPIO_InitStruct); // set the pin as input 218 } 219 220 DS18B20_Delay(10); 221 } 222 223 vPortExitCritical(); 224 } 225 226 //------------------------------------------------------------------------------ 227 228 DeviceOnBus DS18B20_Start(void) 229 { 230 DeviceOnBus Response = DeviceOnBus::NOT_PRESENT; 231 GPIO_InitTypeDef GPIO_InitStruct = GPIO_InitTypeDef(); 232 233 vPortEnterCritical(); 234 235 GPIO_InitStruct.Pin = ONEWIRE_TEMP_BUS_Pin; 236 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 237 HAL_GPIO_Init(ONEWIRE_TEMP_BUS_GPIO_Port, &GPIO_InitStruct); // set the pin as output 238 239 HAL_GPIO_WritePin(ONEWIRE_TEMP_BUS_GPIO_Port, ONEWIRE_TEMP_BUS_Pin, DS18B20_LO_LEVEL); // pull the pin low 240 DS18B20_Delay(DS18B20_480µs_DELAY); // delay according to datasheet 241 242 GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 243 HAL_GPIO_Init(ONEWIRE_TEMP_BUS_GPIO_Port, &GPIO_InitStruct); // set the pin as input 244 245 DS18B20_Delay(DS18B20_80µs_DELAY); // delay according to datasheet 246 247 if (HAL_GPIO_ReadPin(ONEWIRE_TEMP_BUS_GPIO_Port, ONEWIRE_TEMP_BUS_Pin) == DS18B20_LO_LEVEL) 248 Response = DeviceOnBus::PRESENT; // if the pin is low i.e the presence pulse is detected 249 250 DS18B20_Delay(DS18B20_400µs_DELAY); 251 252 vPortExitCritical(); 253 254 return Response; 255 } 256 257 //------------------------------------------------------------------------------ 258 259 void DS18B20_Delay(uint16_t us) 260 { 261 __HAL_TIM_ENABLE(&htim6); 55 262 __HAL_TIM_SET_COUNTER(&htim6, 0); 56 __HAL_TIM_ENABLE(&htim6); 57 while (__HAL_TIM_GET_COUNTER(&htim6) < us); 263 while(__HAL_TIM_GET_COUNTER(&htim6) < us); 58 264 __HAL_TIM_DISABLE(&htim6); 59 } 265 266 /*for (auto i = 0U; i < us * 45; i++) 267 { 268 asm volatile ("nop"::); 269 }*/ 270 }
Note: See TracChangeset
for help on using the changeset viewer.