60 #define ONEWIRE_UART_NUM 4
61 #define ONEWIRE_UART UART[ONEWIRE_UART_NUM]
62 #define ONEWIRE_ISR_FUNC _isr_uart4_status
63 #define ONEWIRE_IRQn UART4_RX_TX_IRQn
64 #define ONEWIRE_UART_MODULE_FREQUENCY F_BUS
65 #define ONEWIRE_RXPIN_PCR PORTE->PCR[25]
66 #define ONEWIRE_TXPIN_PCR PORTE->PCR[24]
69 #define ONEWIRE_TXPIN_PORT_CG SIM_SCGC5_PORTE_MASK
71 #define ONEWIRE_RXPIN_PORT_CG SIM_SCGC5_PORTE_MASK
74 #define ONEWIRE_D_RESET (0xF0)
75 #define ONEWIRE_D_READ (0xFF)
76 #define ONEWIRE_D_WRITE_0 (0x00)
77 #define ONEWIRE_D_WRITE_1 (0xFF)
80 #define OW_ROM_ID_LENGTH (8)
82 static volatile const uint8_t *ow_write_bytes_buf;
83 static volatile uint8_t ow_write_bytes_count;
84 static volatile uint8_t *ow_read_bytes_buf;
85 static volatile uint8_t ow_read_bytes_count;
86 static volatile uint8_t ow_write_bits;
87 static volatile uint8_t ow_read_bits;
88 static volatile uint8_t ow_isr_scratch;
90 #define OW_TIMING_FAST() \
91 ONEWIRE_UART->BDH = UART_BDH_SBR(UART_SBR(ONEWIRE_UART_MODULE_FREQUENCY, ONEWIRE_UART_BAUD_FAST) / 256); \
92 ONEWIRE_UART->BDL = UART_BDL_SBR(UART_SBR(ONEWIRE_UART_MODULE_FREQUENCY, ONEWIRE_UART_BAUD_FAST) % 256); \
93 ONEWIRE_UART->C4 = UART_C4_BRFA(UART_BRFA(ONEWIRE_UART_MODULE_FREQUENCY, ONEWIRE_UART_BAUD_FAST))
95 #define OW_TIMING_SLOW() \
96 ONEWIRE_UART->BDH = UART_BDH_SBR(UART_SBR(ONEWIRE_UART_MODULE_FREQUENCY, ONEWIRE_UART_BAUD_SLOW) / 256); \
97 ONEWIRE_UART->BDL = UART_BDL_SBR(UART_SBR(ONEWIRE_UART_MODULE_FREQUENCY, ONEWIRE_UART_BAUD_SLOW) % 256); \
98 ONEWIRE_UART->C4 = UART_C4_BRFA(UART_BRFA(ONEWIRE_UART_MODULE_FREQUENCY, ONEWIRE_UART_BAUD_SLOW))
100 #define OW_WAIT_WRITE() \
102 while(ow_write_bytes_count > 0 || ow_read_bytes_count > 0 || ow_write_bits > 0 || ow_read_bits > 0)
104 #define OW_WAIT_READ() \
106 while(ow_read_bytes_count > 0 || ow_read_bits > 0 )
108 #define OW_BUSY_WAIT() \
110 while(ow_write_bytes_count > 0 || ow_read_bytes_count > 0 || ow_write_bits > 0 || ow_read_bits > 0 || !(ONEWIRE_UART->S1 & UART_S1_TC_MASK) || (ONEWIRE_UART->S2 & UART_S2_RAF_MASK))
118 static volatile uint8_t dummy;
121 if(ow_write_bytes_count > 0) {
125 ONEWIRE_UART->C2 |= UART_C2_TE_MASK;
127 ONEWIRE_UART->C2 &= ~UART_C2_RE_MASK;
128 ow_isr_scratch = (*ow_write_bytes_buf);
131 --ow_write_bytes_count;
132 ++ow_write_bytes_buf;
134 ONEWIRE_UART->C2 &= ~(UART_C2_RIE_MASK);
136 ONEWIRE_UART->C2 |= UART_C2_TCIE_MASK;
137 }
else if(ow_read_bytes_count > 0) {
141 ONEWIRE_UART->C2 |= UART_C2_TE_MASK | UART_C2_RE_MASK;
143 --ow_read_bytes_count;
144 ow_isr_scratch = 0x00;
147 dummy = ONEWIRE_UART->D;
149 ONEWIRE_UART->CFIFO |= UART_CFIFO_RXFLUSH_MASK;
152 ONEWIRE_UART->C2 &= ~(UART_C2_TIE_MASK | UART_C2_TCIE_MASK);
154 ONEWIRE_UART->D = ONEWIRE_D_READ;
156 ONEWIRE_UART->C2 |= UART_C2_RIE_MASK;
160 ONEWIRE_UART->C2 &= ~(UART_C2_TIE_MASK | UART_C2_TCIE_MASK | UART_C2_RIE_MASK);
162 ONEWIRE_UART->C2 &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK);
175 ONEWIRE_ISR_FUNC(
void)
177 static volatile uint8_t data;
178 static volatile uint8_t status;
179 status = ONEWIRE_UART->S1;
181 if(ow_write_bits > 0) {
182 if(status & (UART_S1_TDRE_MASK | UART_S1_TC_MASK)) {
183 ONEWIRE_UART->D = ((ow_isr_scratch & 0x01) ? ONEWIRE_D_WRITE_1 : ONEWIRE_D_WRITE_0);
184 ow_isr_scratch >>= 1;
187 }
else if(ow_read_bits > 0) {
191 if(ONEWIRE_UART->RCFIFO > 0 || (status & UART_S1_RDRF_MASK)) {
193 ow_isr_scratch >>= 1;
194 data = ONEWIRE_UART->D;
195 if(data == ONEWIRE_D_READ) {
197 ow_isr_scratch |= (1 << 7);
200 if(ow_read_bits == 0) {
202 (*ow_read_bytes_buf) = ow_isr_scratch;
207 ONEWIRE_UART->D = ONEWIRE_D_READ;
224 ow_read_bytes_buf = 0;
225 ow_read_bytes_count = 0;
226 ow_write_bytes_buf = 0;
227 ow_write_bytes_count = 0;
228 ow_isr_scratch = 0x00;
233 SIM->SCGC5 |= ONEWIRE_TXPIN_PORT_CG | ONEWIRE_RXPIN_PORT_CG;
235 ONEWIRE_RXPIN_PCR = PORT_PCR_MUX(3);
240 ONEWIRE_TXPIN_PCR = PORT_PCR_MUX(3) | PORT_PCR_ODE_MASK;
246 ONEWIRE_UART->S2 = 0;
249 ONEWIRE_UART->C2 = 0;
255 ONEWIRE_UART->C1 = 0;
258 ONEWIRE_UART->PFIFO = 0;
277 MK60_ENTER_CRITICAL_REGION();
286 ONEWIRE_UART->C2 |= UART_C2_TE_MASK;
288 ONEWIRE_UART->C2 &= ~UART_C2_RE_MASK;
291 dummy = ONEWIRE_UART->D;
293 ONEWIRE_UART->CFIFO |= UART_CFIFO_RXFLUSH_MASK;
295 ONEWIRE_UART->D = ONEWIRE_D_RESET;
296 MK60_LEAVE_CRITICAL_REGION();
299 while(!(ONEWIRE_UART->S1 & UART_S1_TC_MASK));
307 ONEWIRE_UART->C2 &= ~(UART_C2_TE_MASK);
309 ONEWIRE_UART->C2 |= UART_C2_TE_MASK;
312 while(!(ONEWIRE_UART->S1 & UART_S1_TC_MASK));
334 MK60_ENTER_CRITICAL_REGION();
335 ow_write_bytes_count = count;
336 ow_write_bytes_buf = src;
337 MK60_LEAVE_CRITICAL_REGION();
340 ONEWIRE_UART->C2 |= (UART_C2_TCIE_MASK);
370 MK60_ENTER_CRITICAL_REGION();
371 ow_read_bytes_count = count;
372 ow_read_bytes_buf = dest;
373 MK60_LEAVE_CRITICAL_REGION();
376 ONEWIRE_UART->C2 |= (UART_C2_TCIE_MASK);
379 while(ow_read_bits > 0 || ow_read_bytes_count > 0);
406 for(i = 0; i < count; ++i) {
407 index = data[i] ^ crc;
408 crc = ow_crc_table[index];
431 uint8_t rom[ONEWIRE_ROM_CODE_LENGTH];
433 ow_rom_code_t rom_code;
434 static const ow_rom_cmd_t cmd = ONEWIRE_CMD_READ_ROM;
435 printf(
"READ ROM: ");
440 rom_code = *((uint64_t *)rom);
452 for(i = 0; i <
sizeof(rom) / 2; ++i) {
453 buf = (rom[2 * i] << 8) | (rom[2 * i + 1]);
457 printf(
"CRC: 0x%x\n",
ow_compute_crc(rom, ONEWIRE_ROM_CODE_LENGTH));
468 static const ow_rom_cmd_t cmd = ONEWIRE_CMD_SKIP_ROM;
469 printf(
"SKIP ROM\n");
484 static ow_rom_code_t rom_code;
485 static const ow_rom_cmd_t cmd = ONEWIRE_CMD_MATCH_ROM;
486 printf(
"MATCH ROM\n");
492 ow_write_bytes((
const uint8_t *)(&rom_code), ONEWIRE_ROM_CODE_LENGTH);
502 #if ONEWIRE_ALWAYS_SKIP_ROM
__STATIC_INLINE void NVIC_EnableIRQ(IRQn_Type IRQn)
Enable External Interrupt.
K60 clock configuration defines.
void ow_init(void)
Initialize the 1-wire driver.
One wire CRC lookup table.
void ow_match_rom(const ow_rom_code_t id)
Issue a 1-wire MATCH ROM command.
void __attribute__((interrupt))
This ISR handles most of the business interacting with the 1-wire bus.
#define SIM
Peripheral SIM base pointer.
void uart_module_enable(const unsigned int uart_num)
Enable the clock gate to an UART module.
void ow_skip_or_match_rom(const ow_rom_code_t id)
Shorthand function for MATCH ROM or SKIP ROM if id is zero.
K60 hardware register header wrapper.
One wire driver using hardware UART as a bus master.
void ow_read_bytes(uint8_t *dest, const uint8_t count)
Read a sequence of bytes from the 1-wire bus.
void ow_reset(void)
Reset the 1-wire bus.
void ow_begin_next_byte(void)
Used by the ISR handler to queue the next byte for transmission on the bus.
void ow_write_byte(const uint8_t data)
Shorthand function to write a single byte to the 1-wire bus.
void ow_skip_rom(void)
Issue a 1-wire SKIP ROM command.
Provide common UART routines for MK60DZ10.
K60 interrupt save/restore macros.
ow_rom_code_t ow_read_rom(void)
Issue a 1-wire READ ROM command.
void ow_write_bytes(const uint8_t *src, const uint8_t count)
Write a sequence of bytes to the 1-wire bus.
uint8_t ow_compute_crc(const uint8_t *data, const uint8_t count)
Compute a 1-wire 8-bit CRC.