application: example of 1-Wire slave usage

This commit is contained in:
King Kévin 2017-08-03 20:50:11 +02:00
parent 7fec0df419
commit 3c340a9157
1 changed files with 47 additions and 7 deletions

View File

@ -13,7 +13,7 @@
*
*/
/** STM32F1 application example
* @file main.c
* @file application.c
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2016-2017
*/
@ -41,6 +41,7 @@
#include "print.h" // printing utilities
#include "usart.h" // USART utilities
#include "usb_cdcacm.h" // USB CDC ACM utilities
#include "onewire_slave.h" // 1-Wire utilities
#define WATCHDOG_PERIOD 10000 /**< watchdog period in ms */
@ -191,7 +192,7 @@ void main(void)
board_setup(); // setup board
usart_setup(); // setup USART (for printing)
usb_cdcacm_setup(); // setup USB CDC ACM (for printing)
printf("welcome to the CuVoodoo STM32F1 example application\n"); // print welcome message
printf("welcome to the CuVoodoo STM32F1 1-Wire salve example application\n"); // print welcome message
#if !(DEBUG)
// show watchdog information
@ -216,6 +217,11 @@ void main(void)
time_tm = localtime(&time_rtc); // convert time
printf("date: %d-%02d-%02d %02d:%02d:%02d\n", 1900+time_tm->tm_year, time_tm->tm_mon+1, time_tm->tm_mday, time_tm->tm_hour, time_tm->tm_min, time_tm->tm_sec);
printf("setup 1-Wire bus: ");
onewire_slave_setup(0xb3, 0x0047414feedb); // setup 1-Wire peripheral to act as slave
printf("OK\n");
uint8_t onewire_slave_data[2] = {0}; // data to be transferred
// main loop
printf("command input: ready\n");
bool action = false; // if an action has been performed don't go to sleep
@ -255,18 +261,15 @@ void main(void)
}
while (button_flag) { // user pressed button
action = true; // action has been performed
printf("button pressed\n");
led_toggle(); // toggle LED
for (uint32_t i=0; i<1000000; i++) { // wait a bit to remove noise and double trigger
__asm__("nop");
}
printf("button pressed\n");
button_flag = false; // reset flag
}
while (rtc_internal_tick_flag) { // the internal RTC ticked
rtc_internal_tick_flag = false; // reset flag
action = true; // action has been performed
#if !defined(BLUE_PILL) // on the blue pill the LED is close to the 32.768 kHz oscillator and heavily influences it
led_toggle(); // toggle LED (good to indicate if main function is stuck)
//led_toggle(); // toggle LED (good to indicate if main function is stuck)
#endif
time_rtc = rtc_get_counter_val(); // get time from internal RTC (seconds since Unix Epoch)
time_tm = localtime(&time_rtc); // get time in tm format from Epoch (time zones are not handled for non-POSIX environments)
@ -274,6 +277,43 @@ void main(void)
printf("time: %02d:%02d:%02d\n", time_tm->tm_hour, time_tm->tm_min, time_tm->tm_sec);
}
}
while (onewire_slave_function_code_received) {
onewire_slave_function_code_received = false; // reset flag
action = true; // action has been performed
printf("1-Wire function command received: 0x%02x\n", onewire_slave_function_code);
if (0x55==onewire_slave_function_code) { // master will write data
onewire_slave_function_read(onewire_slave_data, LENGTH(onewire_slave_data)*8);
} else if (0xf0==onewire_slave_function_code) { // master will read data
onewire_slave_function_write(onewire_slave_data, LENGTH(onewire_slave_data)*8);
} else if (0x23==onewire_slave_function_code) { // master will first write data, then read
onewire_slave_function_read(onewire_slave_data, LENGTH(onewire_slave_data)*8);
}
}
while (onewire_slave_transfer_complete) {
onewire_slave_transfer_complete = false; // reset flag
action = true; // action has been performed
printf("1-Wire transfer complete\n");
if (0x55==onewire_slave_function_code) { // master wrote data
printf("data read: ");
for (uint8_t i=0; i<LENGTH(onewire_slave_data); i++) {
printf("0x%02x ", onewire_slave_data[i]);
}
printf("\n");
} else if (0xf0==onewire_slave_function_code) { // master read data
printf("data written: ");
for (uint8_t i=0; i<LENGTH(onewire_slave_data); i++) {
printf("0x%02x ", onewire_slave_data[i]);
}
printf("\n");
} else if (0x23==onewire_slave_function_code) { // master write then read data
printf("data read/written: ");
for (uint8_t i=0; i<LENGTH(onewire_slave_data); i++) {
printf("0x%02x ", onewire_slave_data[i]);
}
printf("\n");
onewire_slave_function_write(onewire_slave_data, LENGTH(onewire_slave_data)*8);
}
}
if (action) { // go to sleep if nothing had to be done, else recheck for activity
action = false;
} else {