Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm8_esp01_msg.h"
- uint8_t gpi_pin[]={0,1,2,4,3}; //NOTE: 4, 3 are swapped because we're following the PB4/PB5 bit order
- uint8_t gpo_pin[]={5,6,7,8,9};
- #define SERIAL_PIN_OUTPUT
- #define ESP_RESET 13
- #define ESP_GPIO0 11
- #define ESP_GPIO2 12
- #define ESP_RX 14
- #define ESP_TX 15
- #define READ_ESP_GPIO0 ((GPIOD->IDR & (1<<2))!=0)
- #define READ_ESP_GPIO2 ((GPIOD->IDR & (1<<3))!=0)
- /*
- #define WATCHDOG_TIMEOUT 5000
- #define WATCHDOG_INITIAL 10000
- #define WATCHDOG_RELAX 60000
- */
- //screw it
- #define WATCHDOG_TIMEOUT 60000
- #define WATCHDOG_INITIAL 60000
- #define WATCHDOG_RELAX 60000
- uint32_t timestampNow=0;
- uint32_t timestampWatchdog=0;
- void apply_config();
- void do_esp_reset()
- {
- pinMode(ESP_GPIO0,OUTPUT);
- pinMode(ESP_GPIO2,OUTPUT);
- digitalWrite(ESP_GPIO0,HIGH);
- digitalWrite(ESP_GPIO2,HIGH);
- digitalWrite(ESP_RESET,LOW);
- delay(50);
- reset_sync_state();
- digitalWrite(ESP_RESET,HIGH);
- delay(50);
- apply_config();
- }
- void setup()
- {
- pinMode(ESP_RESET,OUTPUT);
- memset(&cfg,0,sizeof(cfg));
- do_esp_reset();
- Serial_begin(STM8_BAUDRATE);
- //Serial_begin(115200);
- #ifndef SERIAL_PIN_OUTPUT
- Serial_println_s("");
- Serial_println_s("");
- Serial_println_s("");
- #endif
- // put your setup code here, to run once:
- for(int i=0;i<sizeof(gpo_pin)/sizeof(gpo_pin[0]);i++)
- {
- pinMode(gpo_pin[i],OUTPUT);
- digitalWrite(gpo_pin[i],FALSE);
- }
- for(int i=0;i<sizeof(gpi_pin)/sizeof(gpi_pin[0]);i++)
- {
- pinMode(gpi_pin[i],INPUT);
- }
- timestampWatchdog=millis() + WATCHDOG_INITIAL;
- }
- uint8_t esp_outputs=0;
- void apply_config()
- {
- pinMode(ESP_GPIO0,(cfg.gpio0_mode>=pinmode_input0 && cfg.gpio0_mode<=pinmode_input4)?OUTPUT:INPUT);
- pinMode(ESP_GPIO2,(cfg.gpio2_mode>=pinmode_input0 && cfg.gpio2_mode<=pinmode_input4)?OUTPUT:INPUT);
- }
- uint8_t lastInputs=0xFF;
- void pet_watchdog()
- {
- if((int32_t) (timestampWatchdog-timestampNow)<WATCHDOG_TIMEOUT)
- {
- timestampWatchdog=timestampNow+WATCHDOG_TIMEOUT;
- }
- }
- void onmessage()
- {
- pet_watchdog();
- #ifndef SERIAL_PIN_OUTPUT
- Serial_print_s("message: ");
- Serial_println_u(msg.message);
- #endif
- switch(msg.message)
- {
- default:
- case message_nop:
- break;
- case message_needs_reset:
- #ifndef SERIAL_PIN_OUTPUT
- Serial_println_s("needs reset");
- #endif
- do_esp_reset();
- break;
- case message_relax_watchdog:
- timestampWatchdog=timestampNow+WATCHDOG_RELAX;
- #ifndef SERIAL_PIN_OUTPUT
- Serial_println_s("relaxing watchdog");
- #endif
- break;
- case message_set_config:
- apply_config();
- #ifndef SERIAL_PIN_OUTPUT
- Serial_print_s("configuration received... gpio0 ");
- Serial_print_u(cfg.gpio0_mode);
- Serial_print_s(" gpio2 ");
- Serial_println_u(cfg.gpio2_mode);
- #endif
- timestampWatchdog=timestampNow+WATCHDOG_TIMEOUT;
- lastInputs=0xFF;
- break;
- /* case message_tx_ir:
- #ifndef SERIAL_PIN_OUTPUT
- Serial_print_s("tx_ir received: ");
- Serial_print_u((msg.data2 << 8 | msg.data3));
- Serial_print_s(" bytes");
- Serial_print_s("\n");
- #endif
- break;*/
- }
- }
- void onpin(uint8_t pinvalue)
- {
- esp_outputs=pinvalue;
- pet_watchdog();
- }
- uint32_t bench_counter=0;
- uint8_t loop_counter=0;
- void loop()
- {
- bench_counter++;
- loop_counter++;
- while(Serial_available())
- {
- handle_receive(Serial_read());
- }
- uint8_t inputs=(GPIOA->IDR >> 1 ) & 7; //read STM8 inputs 0, 1, 2 (PA1, PA2, PA3)
- inputs |= (GPIOB->IDR >> 1) & 0x18; //read STM8 inputs 3, 4 (PB4, PB5)
- switch(cfg.gpio0_mode) //pass through selected STM8 input to ESP GPIO0 if selected
- {
- default:
- break;
- case pinmode_input0: GPIOD->ODR = (GPIOD->ODR & ~(1<<2)) | (((inputs >> 0) & 1)<<2); break;
- case pinmode_input1: GPIOD->ODR = (GPIOD->ODR & ~(1<<2)) | (((inputs >> 1) & 1)<<2); break;
- case pinmode_input2: GPIOD->ODR = (GPIOD->ODR & ~(1<<2)) | (((inputs >> 2) & 1)<<2); break;
- case pinmode_input3: GPIOD->ODR = (GPIOD->ODR & ~(1<<2)) | (((inputs >> 3) & 1)<<2); break;
- case pinmode_input4: GPIOD->ODR = (GPIOD->ODR & ~(1<<2)) | (((inputs >> 4) & 1)<<2); break;
- }
- switch(cfg.gpio2_mode) //pass through selected STM8 input to ESP GPIO2 if selected
- {
- default:
- break;
- case pinmode_input0: GPIOD->ODR = (GPIOD->ODR & ~(1<<3)) | (((inputs >> 0) & 1)<<3); break;
- case pinmode_input1: GPIOD->ODR = (GPIOD->ODR & ~(1<<3)) | (((inputs >> 1) & 1)<<3); break;
- case pinmode_input2: GPIOD->ODR = (GPIOD->ODR & ~(1<<3)) | (((inputs >> 2) & 1)<<3); break;
- case pinmode_input3: GPIOD->ODR = (GPIOD->ODR & ~(1<<3)) | (((inputs >> 3) & 1)<<3); break;
- case pinmode_input4: GPIOD->ODR = (GPIOD->ODR & ~(1<<3)) | (((inputs >> 4) & 1)<<3); break;
- }
- switch(cfg.gpio0_mode) //if an STM8 input has already been sent to ESP GPIO0 then clear it out so we don't trigger a serial update
- {
- default:
- break;
- case pinmode_input0: inputs &= ~(1 << 0); break;
- case pinmode_input1: inputs &= ~(1 << 1); break;
- case pinmode_input2: inputs &= ~(1 << 2); break;
- case pinmode_input3: inputs &= ~(1 << 3); break;
- case pinmode_input4: inputs &= ~(1 << 4); break;
- }
- switch(cfg.gpio2_mode) //if an STM8 input has already been sent to ESP GPIO2 then clear it out so we don't trigger a serial update
- {
- default:
- break;
- case pinmode_input0: inputs &= ~(1 << 0); break;
- case pinmode_input1: inputs &= ~(1 << 1); break;
- case pinmode_input2: inputs &= ~(1 << 2); break;
- case pinmode_input3: inputs &= ~(1 << 3); break;
- case pinmode_input4: inputs &= ~(1 << 4); break;
- }
- uint8_t outputs=esp_outputs; //start with the output pins as commanded from the ESP through our serial interface
- switch(cfg.gpio0_mode) //override the appropriate output pin with the current state of the ESP GPIO0 output, if selected
- {
- default:
- break;
- case pinmode_output0: outputs = (outputs & ~(1<<0)) | (READ_ESP_GPIO0 << 0); break;
- case pinmode_output1: outputs = (outputs & ~(1<<1)) | (READ_ESP_GPIO0 << 1); break;
- case pinmode_output2: outputs = (outputs & ~(1<<2)) | (READ_ESP_GPIO0 << 2); break;
- case pinmode_output3: outputs = (outputs & ~(1<<3)) | (READ_ESP_GPIO0 << 3); break;
- case pinmode_output4: outputs = (outputs & ~(1<<4)) | (READ_ESP_GPIO0 << 4); break;
- }
- switch(cfg.gpio2_mode) //override the appropriate output pin with the current state of the ESP GPIO2 output, if selected
- {
- default:
- break;
- case pinmode_output0: outputs = (outputs & ~(1<<0)) | (READ_ESP_GPIO2 << 0); break;
- case pinmode_output1: outputs = (outputs & ~(1<<1)) | (READ_ESP_GPIO2 << 1); break;
- case pinmode_output2: outputs = (outputs & ~(1<<2)) | (READ_ESP_GPIO2 << 2); break;
- case pinmode_output3: outputs = (outputs & ~(1<<3)) | (READ_ESP_GPIO2 << 3); break;
- case pinmode_output4: outputs = (outputs & ~(1<<4)) | (READ_ESP_GPIO2 << 4); break;
- }
- //write the STM8 outputs
- GPIOC->ODR = (GPIOC->ODR & 7) | ((outputs & 0x1f)<<3);
- loop_counter &= 31; //spread these operations out for improved timing stability
- switch(loop_counter)
- {
- case 8:
- if(lastInputs!=inputs) //if an input has changed, send an update message
- {
- lastInputs=inputs;
- #ifdef SERIAL_PIN_OUTPUT
- Serial_write(PIN_MSG);
- Serial_write(lastInputs);
- #endif
- }
- break;
- case 16:
- timestampNow=millis();
- if((int32_t) (timestampWatchdog-timestampNow)<0)
- {
- timestampWatchdog=timestampNow+WATCHDOG_TIMEOUT;
- #ifndef SERIAL_PIN_OUTPUT
- Serial_println_s("watchdog reset");
- #endif
- #ifdef SERIAL_PIN_OUTPUT
- do_esp_reset();
- #endif
- /* for(int i=0;i<2;i++)
- {
- digitalWrite(gpo_pin[4],HIGH);
- delay(250);
- digitalWrite(gpo_pin[4],LOW);
- delay(250);
- }*/
- }
- break;
- case 32:
- {
- static uint32_t lastMillis=0;
- if(millis()-lastMillis>=1000)
- {
- lastMillis+=1000;
- #ifndef SERIAL_PIN_OUTPUT
- // Serial_println_u(bench_counter);
- #endif
- bench_counter=0;
- int32_t diff=(int32_t) (timestampWatchdog-timestampNow);
- if(diff<3000 || diff>6000)
- {
- #ifndef SERIAL_PIN_OUTPUT
- // Serial_print_s("watchdog counter: ");
- // Serial_println_i(diff);
- #endif
- }
- }
- }
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment