diff options
-rw-r--r-- | app/buzzer.c | 205 | ||||
-rw-r--r-- | app/cdcacm.c | 232 | ||||
-rw-r--r-- | app/console.c | 7 | ||||
-rw-r--r-- | app/dialstr.c | 59 | ||||
-rw-r--r-- | app/gpio.c | 187 | ||||
-rw-r--r-- | app/led.c | 20 | ||||
-rw-r--r-- | app/main.c | 25 | ||||
-rw-r--r-- | app/modem.c | 130 | ||||
-rw-r--r-- | app/prototypes.h | 131 | ||||
-rw-r--r-- | app/serial.c | 41 | ||||
-rw-r--r-- | app/ticker.c | 8 | ||||
-rw-r--r-- | app/usart.c | 30 | ||||
-rw-r--r-- | app/usb.c | 33 |
13 files changed, 598 insertions, 510 deletions
diff --git a/app/buzzer.c b/app/buzzer.c index 4c45630..d6f1eb2 100644 --- a/app/buzzer.c +++ b/app/buzzer.c @@ -8,116 +8,125 @@ int ringing; #define C6 1047 #define E6 1319 -void buzzer_on(void) +void +buzzer_on (void) { - gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, - GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, - GPIO_TIM1_CH1); - gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, - GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, - GPIO_TIM1_CH1N); + gpio_set_mode (GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_TIM1_CH1); + gpio_set_mode (GPIOB, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_TIM1_CH1N); - timer_reset(TIM1); + timer_reset (TIM1); - timer_set_mode(TIM1, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_CENTER_1, TIM_CR1_DIR_UP); - timer_set_oc_mode(TIM1, TIM_OC1, TIM_OCM_PWM2); - timer_enable_oc_output(TIM1, TIM_OC1); - timer_enable_oc_output(TIM1, TIM_OC1N); - timer_enable_break_main_output(TIM1); + timer_set_mode (TIM1, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_CENTER_1, + TIM_CR1_DIR_UP); + timer_set_oc_mode (TIM1, TIM_OC1, TIM_OCM_PWM2); + timer_enable_oc_output (TIM1, TIM_OC1); + timer_enable_oc_output (TIM1, TIM_OC1N); + timer_enable_break_main_output (TIM1); - timer_set_oc_value(TIM1, TIM_OC1, period/2); - timer_set_period(TIM1, period); + timer_set_oc_value (TIM1, TIM_OC1, period / 2); + timer_set_period (TIM1, period); - timer_enable_counter(TIM1); + timer_enable_counter (TIM1); } -void buzzer_off(void) + +void +buzzer_off (void) { gpio_set_mode (GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO8); - gpio_clear(GPIOA,GPIO8); + gpio_clear (GPIOA, GPIO8); gpio_set_mode (GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO13); - gpio_clear(GPIOB,GPIO13); - timer_disable_counter(TIM1); + gpio_clear (GPIOB, GPIO13); + timer_disable_counter (TIM1); } -void buzzer_set_freq(int hz) +void +buzzer_set_freq (int hz) { - period=(48000000/4)/hz; - timer_set_oc_value(TIM1, TIM_OC1, period/2); - timer_set_period(TIM1, period); + period = (48000000 / 4) / hz; + timer_set_oc_value (TIM1, TIM_OC1, period / 2); + timer_set_period (TIM1, period); } -void buzzer_init(void) +void +buzzer_init (void) { - /*buzzer*/ - buzzer_off(); - buzzer_set_freq(440); + /*buzzer */ + buzzer_off (); + buzzer_set_freq (440); } -void ring_tick(void) +void +ring_tick (void) { -static int t; - -if (!ringing) return; -ringing--; - -if (!ringing) { - place=0; - buzzer_off(); - return; -} - - - -t++; -if (t<50) return; -t=0; - - -switch(place){ -case 1: -case 3: -case 5: -case 7: - -case 13: -case 15: -case 17: -case 19: - place++; - buzzer_set_freq(C6); - break; - -case 0: -case 12: - place++; - buzzer_set_freq(E6); - buzzer_on(); - break; -case 2: -case 4: -case 6: -case 14: -case 16: -case 18: - place++; - buzzer_set_freq(E6); - break; - -case 8: -case 20: - place++; - buzzer_off(); - break; - -default: - place++; - break; -case 59: - place=0; -} + static int t; + + if (!ringing) + return; + ringing--; + + if (!ringing) + { + place = 0; + buzzer_off (); + return; + } + + + + t++; + if (t < 50) + return; + t = 0; + + + switch (place) + { + case 1: + case 3: + case 5: + case 7: + + case 13: + case 15: + case 17: + case 19: + place++; + buzzer_set_freq (C6); + break; + + case 0: + case 12: + place++; + buzzer_set_freq (E6); + buzzer_on (); + break; + case 2: + case 4: + case 6: + case 14: + case 16: + case 18: + place++; + buzzer_set_freq (E6); + break; + + case 8: + case 20: + place++; + buzzer_off (); + break; + + default: + place++; + break; + case 59: + place = 0; + } @@ -125,19 +134,17 @@ case 59: } -void ring_off(void) +void +ring_off (void) { -place=0; -ringing=0; -buzzer_off(); + place = 0; + ringing = 0; + buzzer_off (); } -void ring(int l) +void +ring (int l) { //mb(); -ringing=l; + ringing = l; } - - - - diff --git a/app/cdcacm.c b/app/cdcacm.c index 930d5ec..05f12b9 100644 --- a/app/cdcacm.c +++ b/app/cdcacm.c @@ -2,154 +2,156 @@ static const struct usb_endpoint_descriptor comm_endp[] = { { - .bLength = USB_DT_ENDPOINT_SIZE, - .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 0x83, - .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, - .wMaxPacketSize = 16, - .bInterval = 255, - } + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x83, + .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, + .wMaxPacketSize = 16, + .bInterval = 255, + } }; static const struct usb_endpoint_descriptor data_endp[] = { { - .bLength = USB_DT_ENDPOINT_SIZE, - .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 0x01, - .bmAttributes = USB_ENDPOINT_ATTR_BULK, - .wMaxPacketSize = 64, - .bInterval = 1, - }, + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x01, + .bmAttributes = USB_ENDPOINT_ATTR_BULK, + .wMaxPacketSize = 64, + .bInterval = 1, + }, { - .bLength = USB_DT_ENDPOINT_SIZE, - .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 0x82, - .bmAttributes = USB_ENDPOINT_ATTR_BULK, - .wMaxPacketSize = 64, - .bInterval = 1, - } + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x82, + .bmAttributes = USB_ENDPOINT_ATTR_BULK, + .wMaxPacketSize = 64, + .bInterval = 1, + } }; -static const struct { +static const struct +{ struct usb_cdc_header_descriptor header; struct usb_cdc_call_management_descriptor call_mgmt; struct usb_cdc_acm_descriptor acm; struct usb_cdc_union_descriptor cdc_union; -} __attribute__((packed)) cdcacm_functional_descriptors = { - .header = { - .bFunctionLength = sizeof(struct usb_cdc_header_descriptor), - .bDescriptorType = CS_INTERFACE, - .bDescriptorSubtype = USB_CDC_TYPE_HEADER, - .bcdCDC = 0x0110, - }, - .call_mgmt = { - .bFunctionLength = - sizeof(struct usb_cdc_call_management_descriptor), - .bDescriptorType = CS_INTERFACE, - .bDescriptorSubtype = USB_CDC_TYPE_CALL_MANAGEMENT, - .bmCapabilities = 0, - .bDataInterface = 1, - }, - .acm = { - .bFunctionLength = sizeof(struct usb_cdc_acm_descriptor), - .bDescriptorType = CS_INTERFACE, - .bDescriptorSubtype = USB_CDC_TYPE_ACM, - .bmCapabilities = 0, - }, - .cdc_union = { - .bFunctionLength = sizeof(struct usb_cdc_union_descriptor), - .bDescriptorType = CS_INTERFACE, - .bDescriptorSubtype = USB_CDC_TYPE_UNION, - .bControlInterface = 0, - .bSubordinateInterface0 = 1, - } +} __attribute__ ((packed)) cdcacm_functional_descriptors = +{ + .header = + { + .bFunctionLength = + sizeof (struct usb_cdc_header_descriptor),.bDescriptorType = + CS_INTERFACE,.bDescriptorSubtype = USB_CDC_TYPE_HEADER,.bcdCDC = + 0x0110,},.call_mgmt = + { + .bFunctionLength = + sizeof (struct usb_cdc_call_management_descriptor),.bDescriptorType = + CS_INTERFACE,.bDescriptorSubtype = + USB_CDC_TYPE_CALL_MANAGEMENT,.bmCapabilities = 0,.bDataInterface = + 1,},.acm = + { + .bFunctionLength = + sizeof (struct usb_cdc_acm_descriptor),.bDescriptorType = + CS_INTERFACE,.bDescriptorSubtype = USB_CDC_TYPE_ACM,.bmCapabilities = + 0,},.cdc_union = + { + .bFunctionLength = + sizeof (struct usb_cdc_union_descriptor),.bDescriptorType = + CS_INTERFACE,.bDescriptorSubtype = + USB_CDC_TYPE_UNION,.bControlInterface = 0,.bSubordinateInterface0 = 1,} }; const struct usb_interface_descriptor comm_iface[] = { { - .bLength = USB_DT_INTERFACE_SIZE, - .bDescriptorType = USB_DT_INTERFACE, - .bInterfaceNumber = 0, - .bAlternateSetting = 0, - .bNumEndpoints = 1, - .bInterfaceClass = USB_CLASS_CDC, - .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, - .bInterfaceProtocol = USB_CDC_PROTOCOL_AT, - .iInterface = 0, - .endpoint = comm_endp, - .extra = &cdcacm_functional_descriptors, - .extralen = sizeof(cdcacm_functional_descriptors) - } + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 1, + .bInterfaceClass = USB_CLASS_CDC, + .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, + .bInterfaceProtocol = USB_CDC_PROTOCOL_AT, + .iInterface = 0, + .endpoint = comm_endp, + .extra = &cdcacm_functional_descriptors, + .extralen = sizeof (cdcacm_functional_descriptors)} }; const struct usb_interface_descriptor data_iface[] = { { - .bLength = USB_DT_INTERFACE_SIZE, - .bDescriptorType = USB_DT_INTERFACE, - .bInterfaceNumber = 1, - .bAlternateSetting = 0, - .bNumEndpoints = 2, - .bInterfaceClass = USB_CLASS_DATA, - .bInterfaceSubClass = 0, - .bInterfaceProtocol = 0, - .iInterface = 0, - .endpoint = data_endp, - } + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 1, + .bAlternateSetting = 0, + .bNumEndpoints = 2, + .bInterfaceClass = USB_CLASS_DATA, + .bInterfaceSubClass = 0, + .bInterfaceProtocol = 0, + .iInterface = 0, + .endpoint = data_endp, + } }; -int cdcacm_control_request(usbd_device *usbd_dev, - struct usb_setup_data *req, - uint8_t **buf, - uint16_t *len, - int (**complete)(usbd_device *usbd_dev, - struct usb_setup_data *req)) +int +cdcacm_control_request (usbd_device * usbd_dev, + struct usb_setup_data *req, + uint8_t ** buf, + uint16_t * len, + int (**complete) (usbd_device * usbd_dev, + struct usb_setup_data * req)) { - (void)complete; - (void)buf; - (void)usbd_dev; + (void) complete; + (void) buf; + (void) usbd_dev; - switch(req->bRequest) { - case USB_CDC_REQ_SET_CONTROL_LINE_STATE: { - /* - * This Linux cdc_acm driver requires this to be implemented - * even though it's optional in the CDC spec, and we don't - * advertise it in the ACM functional descriptor. - */ - char local_buf[10]; - struct usb_cdc_notification *notif = (void *)local_buf; + switch (req->bRequest) + { + case USB_CDC_REQ_SET_CONTROL_LINE_STATE: + { + /* + * This Linux cdc_acm driver requires this to be implemented + * even though it's optional in the CDC spec, and we don't + * advertise it in the ACM functional descriptor. + */ + char local_buf[10]; + struct usb_cdc_notification *notif = (void *) local_buf; - /* We echo signals back to host as notification. */ - notif->bmRequestType = 0xA1; - notif->bNotification = USB_CDC_NOTIFY_SERIAL_STATE; - notif->wValue = 0; - notif->wIndex = 0; - notif->wLength = 2; - local_buf[8] = req->wValue & 3; - local_buf[9] = 0; - // usbd_ep_write_packet(0x83, buf, 10); - return 1; - } - case USB_CDC_REQ_SET_LINE_CODING: - if(*len < sizeof(struct usb_cdc_line_coding)) { - return 0; + /* We echo signals back to host as notification. */ + notif->bmRequestType = 0xA1; + notif->bNotification = USB_CDC_NOTIFY_SERIAL_STATE; + notif->wValue = 0; + notif->wIndex = 0; + notif->wLength = 2; + local_buf[8] = req->wValue & 3; + local_buf[9] = 0; + // usbd_ep_write_packet(0x83, buf, 10); + return 1; + } + case USB_CDC_REQ_SET_LINE_CODING: + if (*len < sizeof (struct usb_cdc_line_coding)) + { + return 0; + } + return 1; } - return 1; - } return 0; } -void cdcacm_data_tx(void *buf,size_t len) +void +cdcacm_data_tx (void *buf, size_t len) { - usbd_ep_write_packet(usbd_dev, 0x82, buf, len); + usbd_ep_write_packet (usbd_dev, 0x82, buf, len); } -void cdcacm_data_rx_cb(usbd_device *usbd_dev, uint8_t ep) +void +cdcacm_data_rx_cb (usbd_device * usbd_dev, uint8_t ep) { - (void)ep; + (void) ep; char buf[64]; - int len = usbd_ep_read_packet(usbd_dev, 0x01, buf, 64); + int len = usbd_ep_read_packet (usbd_dev, 0x01, buf, 64); - if (len) - usart2_tx(buf,len); + if (len) + usart2_tx (buf, len); } diff --git a/app/console.c b/app/console.c index ae32e38..a0c6ab4 100644 --- a/app/console.c +++ b/app/console.c @@ -6,12 +6,13 @@ -int console_tx(void *buf,size_t len) +int +console_tx (void *buf, size_t len) { -usart1_tx(buf,len); + usart1_tx (buf, len); -return len; + return len; } diff --git a/app/dialstr.c b/app/dialstr.c index f75a8e4..1277a42 100644 --- a/app/dialstr.c +++ b/app/dialstr.c @@ -1,49 +1,54 @@ #include "project.h" #define DIGIT_TIMEOUT 4000 -static char dialstr[32]="ATDT"; -static unsigned dialstr_ptr=4; +static char dialstr[32] = "ATDT"; +static unsigned dialstr_ptr = 4; static int dialstr_timeout; -void dialstr_clear(void) +void +dialstr_clear (void) { -dialstr_ptr=4; -dialstr[dialstr_ptr]=';'; -dialstr[dialstr_ptr+1]=0; + dialstr_ptr = 4; + dialstr[dialstr_ptr] = ';'; + dialstr[dialstr_ptr + 1] = 0; } -void dialstr_digit(int digit) +void +dialstr_digit (int digit) { -if (!dialstr_timeout) dialstr_clear(); -if (dialstr_ptr>=(sizeof(dialstr)-2)) return; + if (!dialstr_timeout) + dialstr_clear (); + if (dialstr_ptr >= (sizeof (dialstr) - 2)) + return; -dialstr[dialstr_ptr]='0'+digit; -dialstr_ptr++; -dialstr[dialstr_ptr]=';'; -dialstr[dialstr_ptr+1]=0; + dialstr[dialstr_ptr] = '0' + digit; + dialstr_ptr++; + dialstr[dialstr_ptr] = ';'; + dialstr[dialstr_ptr + 1] = 0; -dialstr_timeout=DIGIT_TIMEOUT; -printf("Dialstr is now %s\r\n",dialstr+4); + dialstr_timeout = DIGIT_TIMEOUT; + printf ("Dialstr is now %s\r\n", dialstr + 4); } -void dialstr_dial(void) +void +dialstr_dial (void) { -printf("Dialing %s\r\n",dialstr+4); -modem_send(dialstr); + printf ("Dialing %s\r\n", dialstr + 4); + modem_send (dialstr); } -void dialstr_tick(void) +void +dialstr_tick (void) { -if (!dialstr_timeout) return; -dialstr_timeout--; -if (dialstr_timeout) return; + if (!dialstr_timeout) + return; + dialstr_timeout--; + if (dialstr_timeout) + return; -if (!hook) - dialstr_dial(); + if (!hook) + dialstr_dial (); } - - - @@ -1,6 +1,6 @@ #include "project.h" #define DEBOUNCE 10 -#define DIAL_BLACKOUT 90 /* (10pps)*/ +#define DIAL_BLACKOUT 90 /* (10pps) */ static int hs_poll; int hook; @@ -12,108 +12,135 @@ static int pulse_count; static int fake_hook; -static void exti5_isr(void) +static void +exti5_isr (void) { -exti_reset_request(EXTI5); -dial_poll=DEBOUNCE; + exti_reset_request (EXTI5); + dial_poll = DEBOUNCE; } -static void exti7_isr(void) +static void +exti7_isr (void) { -exti_reset_request(EXTI7); -if (dial_pulse_blackout) return; -dial_pulse_blackout=DIAL_BLACKOUT; -pulse_count++; + exti_reset_request (EXTI7); + if (dial_pulse_blackout) + return; + dial_pulse_blackout = DIAL_BLACKOUT; + pulse_count++; } -static void exti14_isr(void) +static void +exti14_isr (void) { -exti_reset_request(EXTI14); -hs_poll=DEBOUNCE; + exti_reset_request (EXTI14); + hs_poll = DEBOUNCE; } -void exti9_5_isr(void) +void +exti9_5_isr (void) { -if (exti_get_flag_status(EXTI5)) exti5_isr(); -if (exti_get_flag_status(EXTI7)) exti7_isr(); + if (exti_get_flag_status (EXTI5)) + exti5_isr (); + if (exti_get_flag_status (EXTI7)) + exti7_isr (); } -void exti15_10_isr(void) + +void +exti15_10_isr (void) { -if (exti_get_flag_status(EXTI14)) exti14_isr(); + if (exti_get_flag_status (EXTI14)) + exti14_isr (); } -static void hs_tick(void) +static void +hs_tick (void) { -int h; + int h; -if (!hs_poll) return; -hs_poll--; + if (!hs_poll) + return; + hs_poll--; -if (hs_poll) return; + if (hs_poll) + return; -h=!gpio_get(GPIOC,GPIO14); + h = !gpio_get (GPIOC, GPIO14); -h^=fake_hook; + h ^= fake_hook; -if (hook==h) return; + if (hook == h) + return; -hook=h; + hook = h; -printf("Hook is now %d\r\n",hook); + printf ("Hook is now %d\r\n", hook); -if (!hook) { - if (ringing) { - ring_off(); - answer_call(); - } -} + if (!hook) + { + if (ringing) + { + ring_off (); + answer_call (); + } + } -if (hook) { - ring_off(); - terminate_call(); -} + if (hook) + { + ring_off (); + terminate_call (); + } } -static void dial_tick(void) { -int m; -if (dial_pulse_blackout) dial_pulse_blackout--; -if (!dial_poll) return; -dial_poll--; -if (dial_poll) return; +static void +dial_tick (void) +{ + int m; + + if (dial_pulse_blackout) + dial_pulse_blackout--; + if (!dial_poll) + return; + dial_poll--; + if (dial_poll) + return; -m=!!gpio_get(GPIOA,GPIO5); + m = ! !gpio_get (GPIOA, GPIO5); -if (m) return; -if (!pulse_count) return; + if (m) + return; + if (!pulse_count) + return; -dialstr_digit((pulse_count == 10) ? 0:pulse_count); -pulse_count=0; + dialstr_digit ((pulse_count == 10) ? 0 : pulse_count); + pulse_count = 0; } -void toggle_fake_hook(void) +void +toggle_fake_hook (void) { -fake_hook^=1; -hs_poll=DEBOUNCE; + fake_hook ^= 1; + hs_poll = DEBOUNCE; } -void gpio_tick(void) +void +gpio_tick (void) { -hs_tick(); -dial_tick(); + hs_tick (); + dial_tick (); } @@ -121,55 +148,59 @@ dial_tick(); -void gpio_init(void) +void +gpio_init (void) { - /*GSM module reset pin*/ + /*GSM module reset pin */ gpio_set_mode (GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO1); - gpio_set(GPIOA,GPIO1); + gpio_set (GPIOA, GPIO1); /* Dial mute */ - gpio_set_mode (GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO4); - gpio_set(GPIOA,GPIO4); + gpio_set_mode (GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, + GPIO4); + gpio_set (GPIOA, GPIO4); gpio_set_mode (GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO5); - gpio_clear(GPIOA,GPIO5); + gpio_clear (GPIOA, GPIO5); - exti_select_source(EXTI5, GPIOA); - exti_set_trigger(EXTI5, EXTI_TRIGGER_BOTH); - exti_enable_request(EXTI5); + exti_select_source (EXTI5, GPIOA); + exti_set_trigger (EXTI5, EXTI_TRIGGER_BOTH); + exti_enable_request (EXTI5); /*Dial contacts */ - gpio_set_mode (GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO6); - gpio_set(GPIOA,GPIO6); + gpio_set_mode (GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, + GPIO6); + gpio_set (GPIOA, GPIO6); gpio_set_mode (GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO7); - gpio_clear(GPIOA,GPIO7); + gpio_clear (GPIOA, GPIO7); - exti_select_source(EXTI7, GPIOA); - exti_set_trigger(EXTI7, EXTI_TRIGGER_RISING); - exti_enable_request(EXTI7); + exti_select_source (EXTI7, GPIOA); + exti_set_trigger (EXTI7, EXTI_TRIGGER_RISING); + exti_enable_request (EXTI7); - nvic_enable_irq(NVIC_EXTI9_5_IRQ); + nvic_enable_irq (NVIC_EXTI9_5_IRQ); - /*hookswitch*/ - gpio_set_mode (GPIOC, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO15); - gpio_set(GPIOC,GPIO15); + /*hookswitch */ + gpio_set_mode (GPIOC, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, + GPIO15); + gpio_set (GPIOC, GPIO15); gpio_set_mode (GPIOC, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO14); - gpio_clear(GPIOC,GPIO14); + gpio_clear (GPIOC, GPIO14); - exti_select_source(EXTI14, GPIOC); - exti_set_trigger(EXTI14, EXTI_TRIGGER_BOTH); - exti_enable_request(EXTI14); + exti_select_source (EXTI14, GPIOC); + exti_set_trigger (EXTI14, EXTI_TRIGGER_BOTH); + exti_enable_request (EXTI14); - nvic_enable_irq(NVIC_EXTI15_10_IRQ); + nvic_enable_irq (NVIC_EXTI15_10_IRQ); } @@ -29,21 +29,21 @@ void led_tick (void) { - led_cycle++; + led_cycle++; - if (led_cycle<500) - return; + if (led_cycle < 500) + return; - led_cycle=0; + led_cycle = 0; - led_on=!led_on; + led_on = !led_on; + + if (led_on) + gpio_clear (GPIOC, GPIO13); + else + gpio_set (GPIOC, GPIO13); - if (led_on) - gpio_clear (GPIOC, GPIO13); - else - gpio_set (GPIOC, GPIO13); - } @@ -19,7 +19,7 @@ main (void) rcc_periph_clock_enable (RCC_GPIOB); rcc_periph_clock_enable (RCC_GPIOC); rcc_periph_clock_enable (RCC_AFIO); -rcc_periph_clock_enable(RCC_TIM1); + rcc_periph_clock_enable (RCC_TIM1); nvic_set_priority (NVIC_USART1_IRQ, 0x40); nvic_set_priority (NVIC_USART2_IRQ, 0x40); @@ -28,12 +28,12 @@ rcc_periph_clock_enable(RCC_TIM1); ticker_init (); led_init (); - usart_init(); + usart_init (); - gpio_init(); - buzzer_init(); + gpio_init (); + buzzer_init (); - modem_init(); + modem_init (); @@ -44,17 +44,18 @@ rcc_periph_clock_enable(RCC_TIM1); //usb_init (); - /*Reset the GSM module*/ - gpio_clear(GPIOA,GPIO1); - delay_ms(100); - gpio_set(GPIOA,GPIO1); + /*Reset the GSM module */ + gpio_clear (GPIOA, GPIO1); + delay_ms (100); + gpio_set (GPIOA, GPIO1); //usb_run (); - for (;;) { - serial_poll(); - } + for (;;) + { + serial_poll (); + } return 0; } diff --git a/app/modem.c b/app/modem.c index fa6a57a..b531761 100644 --- a/app/modem.c +++ b/app/modem.c @@ -7,93 +7,109 @@ #define BACKOFF 200 static char modem_buf[BUFFER_SIZE]; -static unsigned modem_ptr=0; +static unsigned modem_ptr = 0; static char *modem_cmd; static int timeout; -static int sleepy=SLEEPY; +static int sleepy = SLEEPY; -void modem_send(char *buf) +void +modem_send (char *buf) { -modem_cmd=buf; - -if (sleepy>=SLEEPY) { - printf("modem is sleepy\r\n"); - usart2_queue('\r'); - sleepy=0; - timeout=BACKOFF; - return; + modem_cmd = buf; + + if (sleepy >= SLEEPY) + { + printf ("modem is sleepy\r\n"); + usart2_queue ('\r'); + sleepy = 0; + timeout = BACKOFF; + return; + } + sleepy = 0; + + printf ("(re)send\r\n"); + timeout = TIMEOUT; + usart2_queue ('\r'); + usart2_tx (modem_cmd, strlen (modem_cmd)); + usart2_queue ('\r'); } - sleepy=0; -printf("(re)send\r\n"); -timeout=TIMEOUT; -usart2_queue('\r'); -usart2_tx(modem_cmd,strlen(modem_cmd)); -usart2_queue('\r'); -} - -void modem_line() +void +modem_line () { -printf("Modem said: %s\r\n",modem_buf); + printf ("Modem said: %s\r\n", modem_buf); -sleepy=0; + sleepy = 0; -if (!strncmp(modem_buf,"RING",4)) - ring(4000); + if (!strncmp (modem_buf, "RING", 4)) + ring (4000); -if (!strncmp(modem_buf,"OK",2)) { - modem_cmd=NULL; - timeout=0; -} + if (!strncmp (modem_buf, "OK", 2)) + { + modem_cmd = NULL; + timeout = 0; + } } -void modem_byte(uint8_t b) +void +modem_byte (uint8_t b) { -if (b=='\n') return; + if (b == '\n') + return; -if (b=='\r') { - if (modem_ptr) - modem_line(); - modem_ptr=0; - modem_buf[modem_ptr]=0; - return; -} - -if (modem_ptr>=(sizeof(modem_buf)-1)) - return; + if (b == '\r') + { + if (modem_ptr) + modem_line (); + modem_ptr = 0; + modem_buf[modem_ptr] = 0; + return; + } + + if (modem_ptr >= (sizeof (modem_buf) - 1)) + return; -modem_buf[modem_ptr]=b; -modem_ptr++; -modem_buf[modem_ptr]=0; + modem_buf[modem_ptr] = b; + modem_ptr++; + modem_buf[modem_ptr] = 0; } -void answer_call(void) +void +answer_call (void) { - printf("Answering call\r\n"); - modem_send("ATA"); + printf ("Answering call\r\n"); + modem_send ("ATA"); } -void terminate_call(void) { - printf("Terminating any call\r\n"); - modem_send("ATH0"); +void +terminate_call (void) +{ + printf ("Terminating any call\r\n"); + modem_send ("ATH0"); } -void modem_tick(void) { +void +modem_tick (void) +{ -if (sleepy<SLEEPY) -sleepy++; + if (sleepy < SLEEPY) + sleepy++; -if (!timeout) return; -timeout--; -if (timeout) return; + if (!timeout) + return; + timeout--; + if (timeout) + return; -modem_send(modem_cmd); + modem_send (modem_cmd); } -void modem_init(void) { +void +modem_init (void) +{ } diff --git a/app/prototypes.h b/app/prototypes.h index f9de05c..9fec585 100644 --- a/app/prototypes.h +++ b/app/prototypes.h @@ -1,9 +1,14 @@ /* dfu.c */ extern const struct usb_dfu_descriptor dfu_function; extern const struct usb_interface_descriptor dfu_iface; -extern int dfu_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, int (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)); +extern int dfu_control_request (usbd_device * usbd_dev, + struct usb_setup_data *req, uint8_t ** buf, + uint16_t * len, + int (**complete) (usbd_device * usbd_dev, + struct usb_setup_data * + req)); /* main.c */ -extern int main(void); +extern int main (void); /* usb.c */ extern const struct usb_device_descriptor dev; extern const struct usb_interface ifaces[]; @@ -11,82 +16,92 @@ extern const struct usb_config_descriptor config; extern usbd_device *usbd_dev; extern int usb_is_suspended; extern int usb_running; -extern void usb_tick(void); -extern int usb_wakeup_host(void); -extern int class_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, int (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)); -extern void usb_set_config(usbd_device *usbd_dev, uint16_t wValue); +extern void usb_tick (void); +extern int usb_wakeup_host (void); +extern int class_control_request (usbd_device * usbd_dev, + struct usb_setup_data *req, uint8_t ** buf, + uint16_t * len, + int (**complete) (usbd_device * usbd_dev, + struct usb_setup_data * + req)); +extern void usb_set_config (usbd_device * usbd_dev, uint16_t wValue); extern uint8_t usbd_control_buffer[128]; -extern void usb_init(void); -extern void usb_run(void); +extern void usb_init (void); +extern void usb_run (void); /* led.c */ -extern void led_init(void); -extern void led_tick(void); +extern void led_init (void); +extern void led_tick (void); /* ticker.c */ extern volatile uint32_t ticks; -extern void delay_us(uint32_t d); -extern void sys_tick_handler(void); -extern void delay_ms(uint32_t d); -extern int timed_out(uint32_t then, unsigned int ms); -extern void ticker_init(void); +extern void delay_us (uint32_t d); +extern void sys_tick_handler (void); +extern void delay_ms (uint32_t d); +extern int timed_out (uint32_t then, unsigned int ms); +extern void ticker_init (void); /* i2c.c */ /* lcd.c */ /* cdcacm.c */ extern const struct usb_interface_descriptor comm_iface[]; extern const struct usb_interface_descriptor data_iface[]; -extern int cdcacm_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, int (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)); -extern void cdcacm_data_tx(void *buf, size_t len); -extern void cdcacm_data_rx_cb(usbd_device *usbd_dev, uint8_t ep); +extern int cdcacm_control_request (usbd_device * usbd_dev, + struct usb_setup_data *req, uint8_t ** buf, + uint16_t * len, + int (**complete) (usbd_device * usbd_dev, + struct usb_setup_data * + req)); +extern void cdcacm_data_tx (void *buf, size_t len); +extern void cdcacm_data_rx_cb (usbd_device * usbd_dev, uint8_t ep); /* usart.c */ extern ring_t rx1_ring; extern ring_t tx1_ring; extern ring_t rx2_ring; extern ring_t tx2_ring; -extern void usart1_isr(void); -extern void usart2_isr(void); -extern int _write(int file, char *ptr, int len); -extern int usart1_tx(void *ptr, int len); -extern int usart2_tx(void *buf, size_t len); -extern void usart1_queue(uint8_t d); -extern void usart2_queue(uint8_t d); -extern void usart2_drain(void); -extern void usart1_drain(void); -extern void usart_init(void); +extern void usart1_isr (void); +extern void usart2_isr (void); +extern int _write (int file, char *ptr, int len); +extern int usart1_tx (void *ptr, int len); +extern int usart2_tx (void *buf, size_t len); +extern void usart1_queue (uint8_t d); +extern void usart2_queue (uint8_t d); +extern void usart2_drain (void); +extern void usart1_drain (void); +extern void usart_init (void); /* ring.c */ -extern void ring_init(ring_t *r, uint8_t *buf, size_t len); -extern int ring_write_byte(ring_t *r, uint8_t c); -extern int ring_read_byte(ring_t *r, uint8_t *c); -extern int ring_write(ring_t *r, uint8_t *buf, size_t len); -extern int ring_empty(ring_t *r); +extern void ring_init (ring_t * r, uint8_t * buf, size_t len); +extern int ring_write_byte (ring_t * r, uint8_t c); +extern int ring_read_byte (ring_t * r, uint8_t * c); +extern int ring_write (ring_t * r, uint8_t * buf, size_t len); +extern int ring_empty (ring_t * r); /* serial.c */ -extern void serial_poll(void); +extern void serial_poll (void); /* gpio.c */ extern int hook; -extern void exti9_5_isr(void); -extern void exti15_10_isr(void); -extern void toggle_fake_hook(void); -extern void gpio_tick(void); -extern void gpio_init(void); +extern void exti9_5_isr (void); +extern void exti15_10_isr (void); +extern void toggle_fake_hook (void); +extern void gpio_tick (void); +extern void gpio_init (void); /* console.c */ -extern int console_tx(void *buf, size_t len); +extern int console_tx (void *buf, size_t len); /* buzzer.c */ extern int ringing; -extern void buzzer_on(void); -extern void buzzer_off(void); -extern void buzzer_set_freq(int hz); -extern void buzzer_init(void); -extern void ring_tick(void); -extern void ring_off(void); -extern void ring(int l); +extern void buzzer_on (void); +extern void buzzer_off (void); +extern void buzzer_set_freq (int hz); +extern void buzzer_init (void); +extern void ring_tick (void); +extern void ring_off (void); +extern void ring (int l); /* modem.c */ -extern void modem_send(char *buf); -extern void modem_line(void); -extern void modem_byte(uint8_t b); -extern void answer_call(void); -extern void terminate_call(void); -extern void modem_tick(void); -extern void modem_init(void); +extern void modem_send (char *buf); +extern void modem_line (void); +extern void modem_byte (uint8_t b); +extern void answer_call (void); +extern void terminate_call (void); +extern void modem_tick (void); +extern void modem_init (void); /* dialstr.c */ -extern void dialstr_clear(void); -extern void dialstr_digit(int digit); -extern void dialstr_dial(void); -extern void dialstr_tick(void); +extern void dialstr_clear (void); +extern void dialstr_digit (int digit); +extern void dialstr_dial (void); +extern void dialstr_tick (void); diff --git a/app/serial.c b/app/serial.c index aa67dae..27307eb 100644 --- a/app/serial.c +++ b/app/serial.c @@ -1,30 +1,35 @@ #include "project.h" -void serial_poll(void) +void +serial_poll (void) { -uint8_t v; + uint8_t v; -if (!ring_read_byte (&rx2_ring, &v)) { -modem_byte(v); + if (!ring_read_byte (&rx2_ring, &v)) + { + modem_byte (v); //console_tx(&v,1); -} + } -if (!ring_read_byte (&rx1_ring, &v)) { + if (!ring_read_byte (&rx1_ring, &v)) + { -if (v=='@') { - toggle_fake_hook(); -} else if (v=='#') { - toggle_fake_hook(); - modem_send("ATDT120;"); -} else { + if (v == '@') + { + toggle_fake_hook (); + } + else if (v == '#') + { + toggle_fake_hook (); + modem_send ("ATDT120;"); + } + else + { - usart2_queue(v); -} -} + usart2_queue (v); + } + } } - - - diff --git a/app/ticker.c b/app/ticker.c index 05d5aeb..6341353 100644 --- a/app/ticker.c +++ b/app/ticker.c @@ -31,11 +31,11 @@ sys_tick_handler (void) #ifndef SLIM lcd_tick (); #endif - gpio_tick(); + gpio_tick (); usb_tick (); - ring_tick(); - modem_tick(); - dialstr_tick(); + ring_tick (); + modem_tick (); + dialstr_tick (); } diff --git a/app/usart.c b/app/usart.c index 402767d..f9ea210 100644 --- a/app/usart.c +++ b/app/usart.c @@ -65,7 +65,8 @@ usart2_isr (void) ring_write_byte (&rx2_ring, data); usart1_queue (data); - if (data=='\r') usart1_queue('\n'); + if (data == '\r') + usart1_queue ('\n'); } @@ -82,8 +83,9 @@ usart2_isr (void) else { usart_send_blocking (USART2, data); - usart1_queue (data); - if (data=='\r') usart1_queue('\n'); + usart1_queue (data); + if (data == '\r') + usart1_queue ('\n'); } } @@ -115,28 +117,28 @@ usart1_tx (void *ptr, int len) { int ret; - ret = ring_write (&tx1_ring, (uint8_t *) ptr, len); + ret = ring_write (&tx1_ring, (uint8_t *) ptr, len); - if (ret < 0) - ret = -ret; + if (ret < 0) + ret = -ret; - USART_CR1 (USART1) |= USART_CR1_TXEIE; - return ret; + USART_CR1 (USART1) |= USART_CR1_TXEIE; + return ret; } int -usart2_tx ( void *buf,size_t len) +usart2_tx (void *buf, size_t len) { -int ret; - ret = ring_write (&tx2_ring, buf, len); + int ret; + ret = ring_write (&tx2_ring, buf, len); - if (ret < 0) - ret = -ret; + if (ret < 0) + ret = -ret; USART_CR1 (USART2) |= USART_CR1_TXEIE; - return ret; + return ret; } @@ -20,13 +20,13 @@ const struct usb_device_descriptor dev = { const struct usb_interface ifaces[] = { { - .num_altsetting = 1, - .altsetting = comm_iface, - }, + .num_altsetting = 1, + .altsetting = comm_iface, + }, { - .num_altsetting = 1, - .altsetting = data_iface, -}, + .num_altsetting = 1, + .altsetting = data_iface, + }, { .num_altsetting = 1, .altsetting = &dfu_iface, @@ -146,13 +146,15 @@ usb_control_request (usbd_device * usbd_dev, struct usb_setup_data *req, int class_control_request (usbd_device * usbd_dev, struct usb_setup_data *req, - uint8_t ** buf, uint16_t * len, - int (**complete) (usbd_device * usbd_dev, - struct usb_setup_data * req)) + uint8_t ** buf, uint16_t * len, + int (**complete) (usbd_device * usbd_dev, + struct usb_setup_data * req)) { - if (cdcacm_control_request(usbd_dev,req,buf,len,complete)) return 1; - if (dfu_control_request(usbd_dev,req,buf,len,complete)) return 1; + if (cdcacm_control_request (usbd_dev, req, buf, len, complete)) + return 1; + if (dfu_control_request (usbd_dev, req, buf, len, complete)) + return 1; return 0; } @@ -162,9 +164,10 @@ usb_set_config (usbd_device * usbd_dev, uint16_t wValue) (void) wValue; (void) usbd_dev; - usbd_ep_setup(usbd_dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, cdcacm_data_rx_cb); - usbd_ep_setup(usbd_dev, 0x82, USB_ENDPOINT_ATTR_BULK, 64, NULL); - usbd_ep_setup(usbd_dev, 0x83, USB_ENDPOINT_ATTR_INTERRUPT, 16, NULL); + usbd_ep_setup (usbd_dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, + cdcacm_data_rx_cb); + usbd_ep_setup (usbd_dev, 0x82, USB_ENDPOINT_ATTR_BULK, 64, NULL); + usbd_ep_setup (usbd_dev, 0x83, USB_ENDPOINT_ATTR_INTERRUPT, 16, NULL); usbd_register_control_callback (usbd_dev, USB_REQ_TYPE_STANDARD | @@ -205,6 +208,6 @@ usb_run (void) for (;;) { usbd_poll (usbd_dev); - serial_poll(); + serial_poll (); } } |