Appiko
hal_ppi.c
1 /*
2  * hal_ppi.c : Basic HAL driver to provide abstraction for PPI peripheral
3  * Copyright (C) 2019 Appiko
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 #include "hal_ppi.h"
19 #include "nrf.h"
20 
21 
22 #define MAX_PPI_CH 20
23 
24 #define PERIPHERAL_VALIDATION 0xF0000000
25 #define VALID_PERIPHERAL 0x40000000
26 #define TASK_EVT_VALIDATION_BIT_POS 8
27 #define TASK_VALIDATION_VAL 0
28 #define EVT_VALIDATION_VAL 0x100
29 
31 {
33  if(setup->ppi_id > MAX_PPI_CH)
34  {
35  ppi_status |= PPI_INVALID_CH;
36  }
37  if(((setup->event & PERIPHERAL_VALIDATION) != VALID_PERIPHERAL) ||
38  ((setup->task & PERIPHERAL_VALIDATION) != VALID_PERIPHERAL) ||
39  (((setup->fork & PERIPHERAL_VALIDATION) != VALID_PERIPHERAL) &&
40  (setup->fork != 0)) )
41  {
42  ppi_status |= PPI_INVALID_PERIPHERAL;
43  }
44  if((setup->event & (1 << TASK_EVT_VALIDATION_BIT_POS)) != EVT_VALIDATION_VAL)
45  {
46  ppi_status |= PPI_INVALID_EVENT;
47  }
48  if((setup->task & (1 << TASK_EVT_VALIDATION_BIT_POS)) != TASK_VALIDATION_VAL)
49  {
50  ppi_status |= PPI_INVALID_TASK;
51  }
52  if((setup->fork & (1 << TASK_EVT_VALIDATION_BIT_POS)) != TASK_VALIDATION_VAL)
53  {
54  ppi_status |= PPI_INVALID_FORK;
55  }
56  if(ppi_status == PPI_SETUP_SUCCESSFUL)
57  {
58  NRF_PPI->CH[setup->ppi_id].EEP = setup->event;
59  NRF_PPI->CH[setup->ppi_id].TEP = setup->task;
60  if(setup->fork != 0)
61  {
62  NRF_PPI->FORK[setup->ppi_id].TEP = setup->fork;
63  }
64  }
65  return ppi_status;
66 }
67 
68 void hal_ppi_en_ch (uint32_t ppi_id)
69 {
70  NRF_PPI->CHENSET |= 1 << ppi_id;
71 }
72 
73 void hal_ppi_dis_ch (uint32_t ppi_id)
74 {
75  NRF_PPI->CHENCLR |= 1 << ppi_id;
76 }
77 
78 void hal_ppi_set_event (uint32_t ppi_id, uint32_t new_event)
79 {
80  NRF_PPI->CH[ppi_id].EEP = new_event;
81 }
82 
83 void hal_ppi_set_task (uint32_t ppi_id, uint32_t new_task)
84 {
85  NRF_PPI->CH[ppi_id].TEP = new_task;
86 }
87 
88 void hal_ppi_set_fork (uint32_t ppi_id, uint32_t new_fork)
89 {
90  NRF_PPI->FORK[ppi_id].TEP = new_fork;
91 }
uint32_t event
Definition: hal_ppi.h:57
uint32_t fork
Definition: hal_ppi.h:61
uint32_t task
Definition: hal_ppi.h:59
void hal_ppi_set_fork(uint32_t ppi_id, uint32_t new_fork)
Function to set secondary task for given PPI channel.
Definition: hal_ppi.c:88
void hal_ppi_dis_ch(uint32_t ppi_id)
Function to disable given PPI channel.
Definition: hal_ppi.c:73
ppi_setup_status_t
Definition: hal_ppi.h:34
ppi_setup_status_t hal_ppi_set(hal_ppi_setup_t *setup)
Function to setup a PPI.
Definition: hal_ppi.c:30
void hal_ppi_set_event(uint32_t ppi_id, uint32_t new_event)
Function to set triggering event for given PPI channel.
Definition: hal_ppi.c:78
void hal_ppi_en_ch(uint32_t ppi_id)
Function to Enable given PPI channel.
Definition: hal_ppi.c:68
uint32_t ppi_id
Definition: hal_ppi.h:55
void hal_ppi_set_task(uint32_t ppi_id, uint32_t new_task)
Function to change primary task for given PPI channel.
Definition: hal_ppi.c:83