cursada_mc2
Base de control de temperatura para EDU-CIAA-NXP
Loading...
Searching...
No Matches
onewire_driver.c
Go to the documentation of this file.
1
6#include "onewire_driver.h"
7
8#include "delay_driver.h"
9
10#define ONEWIRE_CMD_SEARCH_ROM 0xF0U
11#define ONEWIRE_CMD_MATCH_ROM 0x55U
12#define ONEWIRE_CMD_SKIP_ROM 0xCCU
13
14static void onewire_drive_low(const onewire_driver_t* driver)
15{
16 Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, driver->pin.gpio_port, driver->pin.gpio_pin);
17 Chip_GPIO_SetPinDIR(LPC_GPIO_PORT, driver->pin.gpio_port, driver->pin.gpio_pin, true);
18}
19
20static void onewire_release_line(const onewire_driver_t* driver)
21{
22 Chip_GPIO_SetPinDIR(LPC_GPIO_PORT, driver->pin.gpio_port, driver->pin.gpio_pin, false);
23}
24
25static bool onewire_read_line(const onewire_driver_t* driver)
26{
27 return (bool) Chip_GPIO_GetPinState(LPC_GPIO_PORT, driver->pin.gpio_port, driver->pin.gpio_pin);
28}
29
30bool onewire_init(onewire_driver_t* driver, const onewire_pin_config_t* pin_config)
31{
32 if ((driver == 0) || (pin_config == 0)) {
33 return false;
34 }
35
36 driver->pin = *pin_config;
37
38 Chip_SCU_PinMux(driver->pin.scu_port,
39 driver->pin.scu_pin,
40 driver->pin.scu_mode,
41 driver->pin.scu_func);
42
45
46 driver->initialized = true;
47 return true;
48}
49
51{
52 bool presence_detected = false;
53
54 if ((driver == 0) || !driver->initialized) {
55 return false;
56 }
57
58 onewire_drive_low(driver);
59 driver_delay_us(480U);
61 driver_delay_us(70U);
62
63 /* Presence pulse is active low after the master releases the bus. */
64 presence_detected = !onewire_read_line(driver);
65 driver_delay_us(410U);
66
67 return presence_detected;
68}
69
70void onewire_write_bit(const onewire_driver_t* driver, bool bit_value)
71{
72 if ((driver == 0) || !driver->initialized) {
73 return;
74 }
75
76 onewire_drive_low(driver);
77
78 if (bit_value) {
81 driver_delay_us(64U);
82 } else {
83 driver_delay_us(60U);
85 driver_delay_us(10U);
86 }
87}
88
90{
91 bool bit_value = false;
92
93 if ((driver == 0) || !driver->initialized) {
94 return false;
95 }
96
97 onewire_drive_low(driver);
100 driver_delay_us(9U);
101
102 bit_value = onewire_read_line(driver);
103 driver_delay_us(55U);
104
105 return bit_value;
106}
107
108void onewire_write_byte(const onewire_driver_t* driver, uint8_t value)
109{
110 uint8_t bit_index = 0U;
111
112 if ((driver == 0) || !driver->initialized) {
113 return;
114 }
115
116 for (bit_index = 0U; bit_index < 8U; ++bit_index) {
117 onewire_write_bit(driver, (bool) (value & 0x01U));
118 value >>= 1U;
119 }
120}
121
123{
124 uint8_t bit_index = 0U;
125 uint8_t value = 0U;
126
127 if ((driver == 0) || !driver->initialized) {
128 return 0U;
129 }
130
131 for (bit_index = 0U; bit_index < 8U; ++bit_index) {
132 if (onewire_read_bit(driver)) {
133 value |= (uint8_t) (1U << bit_index);
134 }
135 }
136
137 return value;
138}
139
141{
142 if ((driver == 0) || !driver->initialized) {
143 return;
144 }
145
147}
148
149void onewire_match_rom(const onewire_driver_t* driver, const uint8_t rom_code[ONEWIRE_ROM_CODE_SIZE])
150{
151 uint8_t byte_index = 0U;
152
153 if ((driver == 0) || (rom_code == 0) || !driver->initialized) {
154 return;
155 }
156
158
159 for (byte_index = 0U; byte_index < ONEWIRE_ROM_CODE_SIZE; ++byte_index) {
160 onewire_write_byte(driver, rom_code[byte_index]);
161 }
162}
163
165 uint8_t rom_codes[][ONEWIRE_ROM_CODE_SIZE],
166 uint8_t max_devices)
167{
168 uint8_t device_count = 0U;
169 uint8_t current_rom[ONEWIRE_ROM_CODE_SIZE] = {0U};
170 uint8_t last_discrepancy = 0U;
171 bool last_device_flag = false;
172
173 if ((driver == 0) || (rom_codes == 0) || (max_devices == 0U) || !driver->initialized) {
174 return 0U;
175 }
176
177 while (!last_device_flag && (device_count < max_devices)) {
178 uint8_t rom_byte_index = 0U;
179 uint8_t rom_bit_mask = 0x01U;
180 uint8_t bit_number = 1U;
181 uint8_t last_zero = 0U;
182 bool search_failed = false;
183
184 if (!onewire_reset(driver)) {
185 break;
186 }
187
189
190 while (bit_number <= 64U) {
191 const bool id_bit = onewire_read_bit(driver);
192 const bool cmp_id_bit = onewire_read_bit(driver);
193 bool search_direction = false;
194
195 if (id_bit && cmp_id_bit) {
196 search_failed = true;
197 break;
198 }
199
200 if (id_bit != cmp_id_bit) {
201 search_direction = id_bit;
202 } else {
203 if (bit_number < last_discrepancy) {
204 search_direction = ((current_rom[rom_byte_index] & rom_bit_mask) != 0U);
205 } else {
206 search_direction = (bit_number == last_discrepancy);
207 }
208
209 if (!search_direction) {
210 last_zero = bit_number;
211 }
212 }
213
214 if (search_direction) {
215 current_rom[rom_byte_index] |= rom_bit_mask;
216 } else {
217 current_rom[rom_byte_index] &= (uint8_t) ~rom_bit_mask;
218 }
219
220 onewire_write_bit(driver, search_direction);
221
222 bit_number++;
223 rom_bit_mask <<= 1U;
224 if (rom_bit_mask == 0U) {
225 rom_byte_index++;
226 rom_bit_mask = 0x01U;
227 }
228 }
229
230 if (search_failed || (bit_number <= 64U)) {
231 break;
232 }
233
234 for (rom_byte_index = 0U; rom_byte_index < ONEWIRE_ROM_CODE_SIZE; ++rom_byte_index) {
235 rom_codes[device_count][rom_byte_index] = current_rom[rom_byte_index];
236 }
237
238 device_count++;
239 last_discrepancy = last_zero;
240 if (last_discrepancy == 0U) {
241 last_device_flag = true;
242 }
243 }
244
245 return device_count;
246}
void driver_delay_us(uint32_t microseconds)
Realiza una espera bloqueante expresada en microsegundos.
void driver_delay_init(void)
Inicializa la base de tiempos usada por los delays bloqueantes.
Interfaz comun de delays bloqueantes basada en stopwatch de LPCOpen.
bool onewire_init(onewire_driver_t *driver, const onewire_pin_config_t *pin_config)
Inicializa un bus 1-Wire sobre el pin indicado.
static void onewire_release_line(const onewire_driver_t *driver)
static void onewire_drive_low(const onewire_driver_t *driver)
#define ONEWIRE_CMD_SKIP_ROM
#define ONEWIRE_CMD_MATCH_ROM
void onewire_write_byte(const onewire_driver_t *driver, uint8_t value)
Escribe un byte en el bus 1-Wire.
bool onewire_reset(const onewire_driver_t *driver)
Emite un reset 1-Wire y detecta presencia de dispositivos.
uint8_t onewire_search_roms(const onewire_driver_t *driver, uint8_t rom_codes[][ONEWIRE_ROM_CODE_SIZE], uint8_t max_devices)
Busca dispositivos presentes en el bus mediante el comando Search ROM.
uint8_t onewire_read_byte(const onewire_driver_t *driver)
Lee un byte desde el bus 1-Wire.
void onewire_skip_rom(const onewire_driver_t *driver)
Emite el comando Skip ROM sobre el bus.
static bool onewire_read_line(const onewire_driver_t *driver)
void onewire_write_bit(const onewire_driver_t *driver, bool bit_value)
Escribe un bit en el bus 1-Wire.
bool onewire_read_bit(const onewire_driver_t *driver)
Lee un bit desde el bus 1-Wire.
#define ONEWIRE_CMD_SEARCH_ROM
void onewire_match_rom(const onewire_driver_t *driver, const uint8_t rom_code[ONEWIRE_ROM_CODE_SIZE])
Selecciona un dispositivo concreto por su codigo ROM.
Interfaz del driver para bus 1-Wire por bit-banging.
#define ONEWIRE_ROM_CODE_SIZE
Tamano del codigo ROM de un dispositivo 1-Wire.
Estado del driver 1-Wire para un bus individual.
onewire_pin_config_t pin
Configuracion fisica del pin usado por el bus 1-Wire.