mram  2.0.0.0
mram.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright© 2020 MikroElektronika d.o.o.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22  * OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef MRAM_H
36 #define MRAM_H
37 
38 #include "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_spi_master.h"
41 
42 
43 // -------------------------------------------------------------- PUBLIC MACROS
54 #define MRAM_MAP_MIKROBUS( cfg, mikrobus ) \
55  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
56  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
57  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
58  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
59  cfg.wp = MIKROBUS( mikrobus, MIKROBUS_RST ); \
60  cfg.hld = MIKROBUS( mikrobus, MIKROBUS_INT )
61 
67 #define MRAM_OK 0
68 #define MRAM_ERROR -1
69 
75 #define MRAM_WRITE_ENABLE_BIT 0x02
76 #define MRAM_MEMORY_BLOCK_BIT0 0x04
77 #define MRAM_MEMORY_BLOCK_BIT1 0x08
78 #define MRAM_STATUS_WRITE_DISABLE_BIT 0x80
79 #define MRAM_FIRST_ADDRESS_LOCATION 0x0000
80 #define MRAM_ADDRESS_LOCATION_0 0x000F
81 #define MRAM_ADDRESS_LOCATION_1 0x00FF
82 #define MRAM_ADDRESS_LOCATION_2 0x0FFF
83 #define MRAM_LAST_ADDRESS_LOCATION 0x7FFF
84 #define MRAM_LOGIC_HIGH 0x01
85 #define MRAM_LOGIC_LOW 0x00
86 #define MRAM_NONE_PROTECTED_MEMORY 0x82
87 #define MRAM_UPPER_QUARTER_PROTECTED_MEMORY 0x86
88 #define MRAM_UPPER_HALF_PROTECTED_MEMORY 0x8A
89 #define MRAM_ALL_PROTECTED_MEMORY 0x8E
90  // End group macro
93 // --------------------------------------------------------------- PUBLIC TYPES
102 typedef struct
103 {
104  // Output pins
105 
106  digital_out_t wp;
107  digital_out_t hld;
108  digital_out_t cs;
109 
110  // Modules
111  spi_master_t spi;
112  pin_name_t chip_select;
113 
114 } mram_t;
115 
119 typedef struct
120 {
121  // Communication gpio pins
122 
123  pin_name_t miso;
124  pin_name_t mosi;
125  pin_name_t sck;
126  pin_name_t cs;
127 
128  // Additional gpio pins
129 
130  pin_name_t wp;
131  pin_name_t hld;
132 
133  // static variable
134 
135  uint32_t spi_speed;
136  spi_master_mode_t spi_mode;
137  spi_master_chip_select_polarity_t cs_polarity;
138 
139 } mram_cfg_t;
140  // End types group
142 
143 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
144 
149 #ifdef __cplusplus
150 extern "C"{
151 #endif
152 
162 
170 err_t mram_init ( mram_t *ctx, mram_cfg_t *cfg );
171 
179 void mram_default_cfg ( mram_t *ctx );
180 
193 (
194  mram_t *ctx,
195  uint8_t *wr_buf,
196  uint16_t wr_len,
197  uint8_t *rd_buf,
198  uint16_t rd_len
199 );
200 
210 uint8_t mram_read_status_reg ( mram_t *ctx );
211 
222 uint8_t mram_check_status_bit (mram_t *ctx, uint8_t status_bit );
223 
233 uint8_t mram_enable_write (mram_t *ctx );
234 
244 uint8_t mram_disable_write ( mram_t *ctx );
245 
255 void mram_write_status_reg ( mram_t *ctx, uint8_t transfer_data);
256 
268 void mram_read_data_bytes ( mram_t *ctx, const uint16_t address, uint8_t *buffer, const uint16_t n_bytes );
269 
281 void mram_write_data_bytes ( mram_t *ctx, const uint16_t address, uint8_t *buffer, const uint16_t n_bytes );
282 
292 
301 void mram_wake_up_mode ( mram_t *ctx );
302 
312 void mram_enable_write_protect ( mram_t *ctx, uint8_t state);
313 
323 void mram_enable_hold_mode ( mram_t *ctx, uint8_t state);
324 
325 #ifdef __cplusplus
326 }
327 #endif
328 #endif // _MRAM_H_
329  // End public_function group
332 
333 // ------------------------------------------------------------------------- END
mram_disable_write
uint8_t mram_disable_write(mram_t *ctx)
Disable write.
mram_read_data_bytes
void mram_read_data_bytes(mram_t *ctx, const uint16_t address, uint8_t *buffer, const uint16_t n_bytes)
Read n byte data.
mram_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: mram.h:136
mram_t::hld
digital_out_t hld
Definition: mram.h:107
mram_cfg_t::cs
pin_name_t cs
Definition: mram.h:126
mram_cfg_t::mosi
pin_name_t mosi
Definition: mram.h:124
mram_t::spi
spi_master_t spi
Definition: mram.h:111
mram_t::cs
digital_out_t cs
Definition: mram.h:108
mram_cfg_t::wp
pin_name_t wp
Definition: mram.h:130
mram_generic_transfer
void mram_generic_transfer(mram_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len)
Generic transfer function.
mram_enable_hold_mode
void mram_enable_hold_mode(mram_t *ctx, uint8_t state)
Enable hold mode.
mram_t
Click ctx object definition.
Definition: mram.h:103
mram_t::chip_select
pin_name_t chip_select
Definition: mram.h:112
mram_cfg_t
Click configuration structure definition.
Definition: mram.h:120
mram_wake_up_mode
void mram_wake_up_mode(mram_t *ctx)
Wake up.
mram_init
err_t mram_init(mram_t *ctx, mram_cfg_t *cfg)
Initialization function.
mram_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: mram.h:137
mram_write_status_reg
void mram_write_status_reg(mram_t *ctx, uint8_t transfer_data)
Write status reg.
mram_cfg_t::sck
pin_name_t sck
Definition: mram.h:125
mram_enable_write
uint8_t mram_enable_write(mram_t *ctx)
Enable write.
mram_cfg_setup
void mram_cfg_setup(mram_cfg_t *cfg)
Config Object Initialization function.
mram_cfg_t::miso
pin_name_t miso
Definition: mram.h:123
mram_enter_sleep_mode
void mram_enter_sleep_mode(mram_t *ctx)
Sleep mode.
mram_write_data_bytes
void mram_write_data_bytes(mram_t *ctx, const uint16_t address, uint8_t *buffer, const uint16_t n_bytes)
Write n byte data.
mram_cfg_t::hld
pin_name_t hld
Definition: mram.h:131
mram_t::wp
digital_out_t wp
Definition: mram.h:106
mram_default_cfg
void mram_default_cfg(mram_t *ctx)
Click Default Configuration function.
mram_cfg_t::spi_speed
uint32_t spi_speed
Definition: mram.h:135
mram_read_status_reg
uint8_t mram_read_status_reg(mram_t *ctx)
Read status reg.
mram_check_status_bit
uint8_t mram_check_status_bit(mram_t *ctx, uint8_t status_bit)
Check status bit.
mram_enable_write_protect
void mram_enable_write_protect(mram_t *ctx, uint8_t state)
Enable write protection.